Add support for sha1 hashing method
[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 hash users tmp_shadow
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             hash=$("@scriptsdir@/snf-passtohash.py" "$password")
105             ;;
106         openbsd)
107             hash=$("@scriptsdir@/snf-passtohash.py" -m blowfish "$password")
108             ;;
109         netbsd)
110             hash=$("@scriptsdir@/snf-passtohash.py" -m sha1 "$password")
111             ;;
112         *)
113             log_error "Unknown unix flavor: \`$flavor'"
114             ;;
115     esac
116
117     users=()
118     
119     if [ -n "$SNF_IMAGE_PROPERTY_USERS" ]; then
120         for usr in $SNF_IMAGE_PROPERTY_USERS; do
121             users+=("$usr")
122         done
123     else
124         warn "Image property \`USERS' is missing or empty. " \
125             "Changing the password for default user: \`root'."
126         users+=("root")
127     fi
128
129     for i in $(seq 0 1 $((${#users[@]}-1))); do
130         tmp_shadow="$(mktemp)"
131         add_cleanup rm "$tmp_shadow"
132
133         echo -n "Setting ${users[$i]} password..."
134         entry=$(grep "^${users[$i]}:" "$target${!shadow}")
135         if [ -z "$entry" ]; then
136             log_error "User: \`${users[$i]}' does not exist."
137         fi
138
139         new_entry="$(${flavor}_change_shadow_entry "$entry" "$hash")"
140         grep -v "${users[$i]}" "$target${!shadow}" > "$tmp_shadow"
141         echo "$new_entry" >> "$tmp_shadow"
142         cat "$tmp_shadow" > "$target${!shadow}"
143         echo "done"
144     done
145 }
146
147 freebsd_password() {
148     local target password hash
149     target="$1"
150     password="$2"
151
152     if [ ! -e "$target/etc/master.passwd" ]; then
153         log_error "No /etc/master.passwd found!"
154     fi
155
156     hash=$("@scriptsdir@/snf-passtohash.py" "$password")
157     for i in $(seq 0 1 $((${#users[@]}-1))); do
158         tmp_master="$(mktemp)"
159     done
160 }
161
162 if [ ! -d "$SNF_IMAGE_TARGET" ]; then
163     log_error "Target dir: \`$SNF_IMAGE_TARGET' is missing"
164 fi
165
166 if [ -z "$SNF_IMAGE_PASSWORD" ]; then
167     log_error "Password is missing"
168 fi
169
170 #trim users var
171 SNF_IMAGE_PROPERTY_USERS=$(echo $SNF_IMAGE_PROPERTY_USERS)
172
173 if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "windows" ]; then
174     windows_password "$SNF_IMAGE_TARGET" "$SNF_IMAGE_PASSWORD"
175 else
176     unix_password "$SNF_IMAGE_PROPERTY_OSFAMILY" "$SNF_IMAGE_TARGET" "$SNF_IMAGE_PASSWORD"
177 fi
178
179 # For FreeBSD, OpenBSD and NetBSD we need to recreate the password databases too
180 if [[ "$SNF_IMAGE_PROPERTY_OSFAMILY" == *bsd ]]; then
181     rm -f "$SNF_IMAGE_TARGET/etc/spwd.db"
182
183     # NetBSD is very strict about the existence & non-existence of the db files
184     if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "netbsd" ]; then
185         rm -f "$SNF_IMAGE_TARGET/etc/pwd.db.tmp"
186         rm -f "$SNF_IMAGE_TARGET/etc/spwd.db.tmp"
187
188         touch "$SNF_IMAGE_TARGET/etc/spwd.db"
189     fi
190
191
192     # Make sure /etc/spwd.db is recreated on first boot
193     rc_local=$(cat <<EOF
194 PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin
195 export PATH
196
197 pwd_mkdb -p /etc/master.passwd
198 EOF
199 )
200     if [ -e "$SNF_IMAGE_TARGET/etc/rc.local" ]; then
201         orig_local="/etc/rc.local.snf_image_$RANDOM"
202         mv "$SNF_IMAGE_TARGET/etc/rc.local" "$SNF_IMAGE_TARGET$orig_local"
203         cat > "$SNF_IMAGE_TARGET/etc/rc.local" <<EOF
204 $rc_local
205 mv $orig_local /etc/rc.local
206 . /etc/rc.local
207 EOF
208     else
209         cat > "$SNF_IMAGE_TARGET/etc/rc.local" <<EOF
210 $rc_local
211 rm -f /etc/rc.local
212 exit 0
213 EOF
214     fi
215 fi
216
217 exit 0
218
219 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
220