1#!/bin/bash
2
3# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7# Customizes a Chrome OS release image by setting the chronos user password.
8
9# Usage: ./set_chronos_password.sh <image.bin> <chronos_password> [--force]
10
11# Load common constants and variables.
12. "$(dirname "$0")/common.sh"
13
14change_chronos_password() {
15  local rootfs=$1
16  local password=$2
17  echo "Setting chronos password..."
18  local crypted_password="$(echo $password | openssl passwd -1 -stdin)"
19  local temp_shadow="$rootfs/etc/tempshadow"
20  echo "chronos:$crypted_password:14500:0:99999::::" \
21    | sudo tee "$temp_shadow" > /dev/null
22  sudo grep -Ev ^chronos: "$rootfs/etc/shadow" \
23    | sudo tee -a "$temp_shadow" > /dev/null
24  sudo mv -f "$temp_shadow" "$rootfs/etc/shadow"
25}
26
27main() {
28  set -e
29
30  local image=$1
31  local chronos_password=$2
32  if [ $# -ne 2 ] && [ $# -ne 3 ] || [ ! $3 = "--force" ] ; then
33    echo "Usage: $PROG <image.bin> <chronos_password> [--force]"
34    exit 1
35  fi
36
37  local rootfs=$(make_temp_dir)
38  if [ $# -eq 2 ]; then
39    mount_image_partition_ro "$image" 3 "$rootfs"
40    if ! no_chronos_password "$rootfs"; then
41      echo "Password is already set [use --force if you'd like to update it]"
42      exit 1
43    fi
44    # Prepare for remounting read/write.
45    sudo umount $rootfs
46  fi
47  mount_image_partition "$image" 3 "$rootfs"
48  change_chronos_password "$rootfs" "$chronos_password"
49  touch "$image"  # Updates the image modification time.
50  echo "Password Set."
51}
52
53main $@
54