Fix a bug in ChagePassword task
[snf-image] / snf-image-helper / tasks / 50ChangePassword.in
1 #! /bin/bash
2
3 # Copyright (C) 2011 GRNET S.A. 
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 # 02110-1301, USA.
19
20 ### BEGIN TASK INFO
21 # Provides:             ChangePassword
22 # RunBefore:            EnforcePersonality
23 # RunAfter:             InstallUnattend
24 # Short-Description:    Changes Password for specified users
25 ### END TASK INFO
26
27 set -e
28 . "@commondir@/common.sh"
29
30 trap task_cleanup EXIT
31 report_task_start
32
33 # Check if the task should be prevented from running.
34 check_if_excluded
35
36 linux_shadow="/etc/shadow"
37 freebsd_shadow="/etc/master.passwd"
38 openbsd_shadow="/etc/master.passwd"
39 netbsd_shadow="/etc/master.passwd"
40
41 linux_change_shadow_entry() {
42     local line encrypted
43     line="$1"
44     encrypted="$2"
45
46     IFS=":" read -a entry <<< "$line"
47
48     echo "${entry[0]}:$encrypted:15103:0:99999:7:::"
49 }
50
51 freebsd_change_shadow_entry() {
52     local line encrypted
53     line="$1"
54     encrypted="$2"
55
56     IFS=":" read -a entry <<< "$line"
57
58     echo "${entry[0]}:$encrypted:${entry[2]}:${entry[3]}:${entry[4]}:${entry[5]}:0:${entry[7]}:${entry[8]}:${entry[9]}"
59 }
60
61 openbsd_change_shadow_entry() {
62     freebsd_change_shadow_entry "$@"
63 }
64
65 netbsd_change_shadow_entry() {
66     freebsd_change_shadow_entry "$@"
67 }
68
69 windows_password() {
70     local target password
71     target="$1"
72     password="$2"
73
74     echo "@echo off" > "$target/Windows/SnfScripts/ChangeAdminPassword.cmd"
75
76     if [ -z "$SNF_IMAGE_PROPERTY_USERS" ]; then
77         warn "Image property \`USERS' is missing or empty. " \
78             "Changing the password for default user: \`Administrator'."
79
80         SNF_IMAGE_PROPERTY_USERS="Administrator"
81     fi
82
83     for usr in $SNF_IMAGE_PROPERTY_USERS; do
84         echo -n "Installing new password for user \`$usr'..."
85         echo "net user $usr $password" >> \
86             "$target/Windows/SnfScripts/ChangeAdminPassword.cmd"
87         echo done
88     done
89 }
90
91 unix_password() {
92     local flavor target password encrypted users tmp_shadow method default_method
93     flavor="$1"
94     target="$2"
95     password="$3"
96
97     shadow="${flavor}_shadow"
98     if [ ! -e "$target${!shadow}" ]; then
99        log_error "No ${!shadow} found!"
100     fi
101
102     case "$flavor" in
103         linux|freebsd)
104             default_method=sha512
105             ;;
106         openbsd)
107             default_method=blowfish
108             ;;
109         netbsd)
110             default_method=sha1
111             ;;
112         *)
113             log_error "Unknown unix flavor: \`$flavor'"
114             ;;
115     esac
116
117     method="${SNF_IMAGE_PROPERTY_PASSWORD_HASHING_METHOD:-$default_method}"
118     echo -n "Encrypting password with \`$method' method ... "
119     encrypted=$("@scriptsdir@/snf-passtohash.py" -m "$method" "$password")
120     echo "done"
121
122     users=()
123     
124     if [ -n "$SNF_IMAGE_PROPERTY_USERS" ]; then
125         for usr in $SNF_IMAGE_PROPERTY_USERS; do
126             users+=("$usr")
127         done
128     else
129         warn "Image property \`USERS' is missing or empty. " \
130             "Changing the password for default user: \`root'."
131         users+=("root")
132     fi
133
134     for i in $(seq 0 1 $((${#users[@]}-1))); do
135         tmp_shadow="$(mktemp)"
136         add_cleanup rm "$tmp_shadow"
137
138         echo -n "Setting ${users[$i]} password ... "
139         entry=$(grep "^${users[$i]}:" "$target${!shadow}")
140         if [ -z "$entry" ]; then
141             log_error "User: \`${users[$i]}' does not exist."
142         fi
143
144         new_entry="$(${flavor}_change_shadow_entry "$entry" "$encrypted")"
145         grep -v "^${users[$i]}:" "$target${!shadow}" > "$tmp_shadow"
146         echo "$new_entry" >> "$tmp_shadow"
147         cat "$tmp_shadow" > "$target${!shadow}"
148         echo "done"
149     done
150 }
151
152 if [ ! -d "$SNF_IMAGE_TARGET" ]; then
153     log_error "Target dir: \`$SNF_IMAGE_TARGET' is missing"
154 fi
155
156 if [ -z "$SNF_IMAGE_PASSWORD" ]; then
157     log_error "Password is missing"
158 fi
159
160 #trim users var
161 SNF_IMAGE_PROPERTY_USERS=$(echo $SNF_IMAGE_PROPERTY_USERS)
162
163 if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "windows" ]; then
164     windows_password "$SNF_IMAGE_TARGET" "$SNF_IMAGE_PASSWORD"
165 else
166     unix_password "$SNF_IMAGE_PROPERTY_OSFAMILY" "$SNF_IMAGE_TARGET" "$SNF_IMAGE_PASSWORD"
167 fi
168
169 # For FreeBSD, OpenBSD and NetBSD we need to recreate the password databases too
170 if [[ "$SNF_IMAGE_PROPERTY_OSFAMILY" == *bsd ]]; then
171     rm -f "$SNF_IMAGE_TARGET/etc/spwd.db"
172
173     # NetBSD is very strict about the existence & non-existence of the db files
174     if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "netbsd" ]; then
175         rm -f "$SNF_IMAGE_TARGET/etc/pwd.db.tmp"
176         rm -f "$SNF_IMAGE_TARGET/etc/spwd.db.tmp"
177
178         touch "$SNF_IMAGE_TARGET/etc/spwd.db"
179     fi
180
181
182     # Make sure /etc/spwd.db is recreated on first boot
183     rc_local=$(cat <<EOF
184 PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin
185 export PATH
186
187 pwd_mkdb -p /etc/master.passwd
188 EOF
189 )
190     if [ -e "$SNF_IMAGE_TARGET/etc/rc.local" ]; then
191         orig_local="/etc/rc.local.snf_image_$RANDOM"
192         mv "$SNF_IMAGE_TARGET/etc/rc.local" "$SNF_IMAGE_TARGET$orig_local"
193         cat > "$SNF_IMAGE_TARGET/etc/rc.local" <<EOF
194 $rc_local
195 mv $orig_local /etc/rc.local
196 . /etc/rc.local
197 EOF
198     else
199         cat > "$SNF_IMAGE_TARGET/etc/rc.local" <<EOF
200 $rc_local
201 rm -f /etc/rc.local
202 exit 0
203 EOF
204     fi
205 fi
206
207 exit 0
208
209 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
210