Add partial support for NetBSD and OpenBSD
[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
39 linux_change_shadow_entry() {
40     local line encrypted
41     line="$1"
42     encrypted="$2"
43
44     IFS=":" read -a entry <<< "$line"
45
46     echo "${entry[0]}:$encrypted:15103:0:99999:7:::"
47 }
48
49 freebsd_change_shadow_entry() {
50     local line encrypted
51     line="$1"
52     encrypted="$2"
53
54     IFS=":" read -a entry <<< "$line"
55
56     echo "${entry[0]}:$encrypted:${entry[2]}:${entry[3]}:${entry[4]}:${entry[5]}:0:${entry[7]}:${entry[8]}:${entry[9]}"
57 }
58
59 openbsd_change_shadow_entry() {
60     freebsd_change_shadow_entry "$@"
61 }
62
63 netbsd_change_shadow_entry() {
64     freebsd_change_shadow_entry "$@"
65 }
66
67 windows_password() {
68     local target password
69     target="$1"
70     password="$2"
71
72     echo "@echo off" > "$target/Windows/SnfScripts/ChangeAdminPassword.cmd"
73
74     if [ -z "$SNF_IMAGE_PROPERTY_USERS" ]; then
75         warn "Image property \`USERS' is missing or empty. " \
76             "Changing the password for default user: \`Administrator'."
77
78         SNF_IMAGE_PROPERTY_USERS="Administrator"
79     fi
80
81     for usr in $SNF_IMAGE_PROPERTY_USERS; do
82         echo -n "Installing new password for user \`$usr'..."
83         echo "net user $usr $password" >> \
84             "$target/Windows/SnfScripts/ChangeAdminPassword.cmd"
85         echo done
86     done
87 }
88
89 unix_password() {
90     local flavor target password hash users tmp_shadow
91     flavor="$1"
92     target="$2"
93     password="$3"
94
95     shadow="${flavor}_shadow"
96     if [ ! -e "$target${!shadow}" ]; then
97        log_error "No ${!shadow} found!"
98     fi
99
100     hash=$("@scriptsdir@/snf-passtohash.py" "$password")
101     
102     users=()
103     
104     if [ -n "$SNF_IMAGE_PROPERTY_USERS" ]; then
105         for usr in $SNF_IMAGE_PROPERTY_USERS; do
106             users+=("$usr")
107         done
108     else
109         warn "Image property \`USERS' is missing or empty. " \
110             "Changing the password for default user: \`root'."
111         users+=("root")
112     fi
113
114     for i in $(seq 0 1 $((${#users[@]}-1))); do
115         tmp_shadow="$(mktemp)"
116         add_cleanup rm "$tmp_shadow"
117
118         echo -n "Setting ${users[$i]} password..."
119         entry=$(grep "^${users[$i]}:" "$target${!shadow}")
120         if [ -z "$entry" ]; then
121             log_error "User: \`${users[$i]}' does not exist."
122         fi
123
124         new_entry="$(${flavor}_change_shadow_entry "$entry" "$hash")"
125         grep -v "${users[$i]}" "$target${!shadow}" > "$tmp_shadow"
126         echo "$new_entry" >> "$tmp_shadow"
127         cat "$tmp_shadow" > "$target${!shadow}"
128         echo "done"
129     done
130 }
131
132 freebsd_password() {
133     local target password hash
134     target="$1"
135     password="$2"
136
137     if [ ! -e "$target/etc/master.passwd" ]; then
138         log_error "No /etc/master.passwd found!"
139     fi
140
141     hash=$("@scriptsdir@/snf-passtohash.py" "$password")
142     for i in $(seq 0 1 $((${#users[@]}-1))); do
143         tmp_master="$(mktemp)"
144     done
145 }
146
147 if [ ! -d "$SNF_IMAGE_TARGET" ]; then
148     log_error "Target dir: \`$SNF_IMAGE_TARGET' is missing"
149 fi
150
151 if [ -z "$SNF_IMAGE_PASSWORD" ]; then
152     log_error "Password is missing"
153 fi
154
155 #trim users var
156 SNF_IMAGE_PROPERTY_USERS=$(echo $SNF_IMAGE_PROPERTY_USERS)
157
158 if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "windows" ]; then
159     windows_password "$SNF_IMAGE_TARGET" "$SNF_IMAGE_PASSWORD"
160 else
161     unix_password "$SNF_IMAGE_PROPERTY_OSFAMILY" "$SNF_IMAGE_TARGET" "$SNF_IMAGE_PASSWORD"
162 fi
163
164 # For FreeBSD, OpenBSD and NetBSD we need to recreate the password database too
165 if [[ "$SNF_IMAGE_PROPERTY_OSFAMILY" == *bsd ]]; then
166     rm -f "$SNF_IMAGE_TARGET/etc/spwd.db"
167
168     # Make sure /etc/spwd.db is recreated on first boot
169     rc_local=$(cat <<EOF
170 PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin
171 export PATH
172
173 pwd_mkdb -p /etc/master.passwd
174 EOF
175 )
176     if [ -e "$SNF_IMAGE_TARGET/etc/rc.local" ]; then
177         orig_local="/etc/rc.local.snf_image_$RANDOM"
178         mv "$SNF_IMAGE_TARGET/etc/rc.local" "$SNF_IMAGE_TARGET$orig_local"
179         cat > "$SNF_IMAGE_TARGET/etc/rc.local" <<EOF
180 $rc_local
181 mv $orig_local /etc/rc.local
182 . /etc/rc.local
183 EOF
184     else
185         cat > "$SNF_IMAGE_TARGET/etc/rc.local" <<EOF
186 $rc_local
187 rm -f /etc/rc.local
188 exit 0
189 EOF
190     fi
191 fi
192
193 exit 0
194
195 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
196