Mass cleanup and some bug fixing
[snf-image] / snf-image-helper / tasks / 50ChangePassword.in
1 #! /bin/bash
2
3 ### BEGIN TASK INFO
4 # Provides:             ChangePassword
5 # RunBefore:            UmountImage
6 # RunAfter:             InstallUnattend
7 # Short-Description:    Changes Password for specified users
8 ### END TAST INFO
9
10 set -e
11 . "@commondir@/common.sh"
12
13 windows_password() {
14     local target="$1"
15     local password="$2"
16
17     local tmp_unattend="$(mktemp)"
18     add_cleanup rm "$tmp_unattend"
19
20     echo -n "Installing new admin password..."
21
22     local namespace="urn:schemas-microsoft-com:unattend"
23     
24     "$XMLSTARLET" ed -N x=$namespace -u "/x:unattend/x:settings/x:component/x:UserAccounts/x:AdministratorPassword/x:Value" -v "$password" "$target/Unattend.xml" > "$tmp_unattend"
25
26     cat "$tmp_unattend" > "$target/Unattend.xml"
27     echo done
28 }
29
30 linux_password() {
31     local target="$1"
32     local password="$2"
33
34     local hash=$("@scriptsdir@/snf-passtohash.py" "$password")
35     if [ ! -e "$target/etc/shadow" ]; then
36        log_error "No /etc/shadow found!" 
37     fi
38     
39     declare -a users=("root")
40
41     local distro=$(get_distro $target)
42
43     if [ "x$disto" = "xubuntu" -o \
44          "x$disto" = "xfedora" ] ; then
45         users+=("user")
46     fi
47
48     for i in $(seq 0 1 $((${#users[@]}-1))); do
49         local tmp_shadow="$(mktemp)"
50         add_cleanup rm "$tmp_shadow"
51
52         echo -n "Setting ${users[$i]} password..."
53     
54         echo "${users[$i]}:$hash:15103:0:99999:7:::" > "$tmp_shadow"
55         grep -v "${users[$i]}" "$target/etc/shadow" >> "$tmp_shadow"
56         cat "$tmp_shadow" > "$target/etc/shadow"
57         echo "done"
58     done
59 }
60
61 if [ ! -d "$SNF_IMAGE_TARGET" ]; then
62     log_error "Target dir: \`$SNF_IMAGE_TARGET' is missing"
63 fi
64
65 if [ -z "$SNF_IMAGE_PASSWORD" ]; then
66     log_error "Password is missing"
67 fi
68
69 if [ "$SNF_IMAGE_TYPE" = "ntfsdump" ]; then
70     windows_password "$SNF_IMAGE_TARGET" "$SNF_IMAGE_PASSWORD"
71 elif [ "$SNF_IMAGE_TYPE" = "extdump" ]; then
72     linux_password "$SNF_IMAGE_TARGET" "$SNF_IMAGE_PASSWORD"
73 fi
74
75 echo "done"
76
77 cleanup
78 trap - EXIT
79
80 exit 0
81
82 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
83