3 # Copyright (C) 2011 GRNET S.A.
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.
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.
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
21 # Provides: ChangePassword
22 # RunBefore: EnforcePersonality
23 # RunAfter: InstallUnattend
24 # Short-Description: Changes Password for specified users
28 . "@commondir@/common.sh"
30 trap task_cleanup EXIT
33 # Check if the task should be prevented from running.
36 linux_shadow="/etc/shadow"
37 freebsd_shadow="/etc/master.passwd"
38 openbsd_shadow="/etc/master.passwd"
39 netbsd_shadow="/etc/master.passwd"
41 linux_change_shadow_entry() {
46 IFS=":" read -a entry <<< "$line"
48 echo "${entry[0]}:$encrypted:15103:0:99999:7:::"
51 freebsd_change_shadow_entry() {
56 IFS=":" read -a entry <<< "$line"
58 echo "${entry[0]}:$encrypted:${entry[2]}:${entry[3]}:${entry[4]}:${entry[5]}:0:${entry[7]}:${entry[8]}:${entry[9]}"
61 openbsd_change_shadow_entry() {
62 freebsd_change_shadow_entry "$@"
65 netbsd_change_shadow_entry() {
66 freebsd_change_shadow_entry "$@"
74 echo "@echo off" > "$target/Windows/SnfScripts/ChangeAdminPassword.cmd"
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'."
80 SNF_IMAGE_PROPERTY_USERS="Administrator"
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"
92 local flavor target password encrypted users tmp_shadow method default_method
97 shadow="${flavor}_shadow"
98 if [ ! -e "$target${!shadow}" ]; then
99 log_error "No ${!shadow} found!"
104 default_method=sha512
107 default_method=blowfish
113 log_error "Unknown unix flavor: \`$flavor'"
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")
124 if [ -n "$SNF_IMAGE_PROPERTY_USERS" ]; then
125 for usr in $SNF_IMAGE_PROPERTY_USERS; do
129 warn "Image property \`USERS' is missing or empty. " \
130 "Changing the password for default user: \`root'."
134 for i in $(seq 0 1 $((${#users[@]}-1))); do
135 tmp_shadow="$(mktemp)"
136 add_cleanup rm "$tmp_shadow"
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."
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}"
152 if [ ! -d "$SNF_IMAGE_TARGET" ]; then
153 log_error "Target dir: \`$SNF_IMAGE_TARGET' is missing"
156 if [ -z "$SNF_IMAGE_PASSWORD" ]; then
157 log_error "Password is missing"
161 SNF_IMAGE_PROPERTY_USERS=$(echo $SNF_IMAGE_PROPERTY_USERS)
163 if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "windows" ]; then
164 windows_password "$SNF_IMAGE_TARGET" "$SNF_IMAGE_PASSWORD"
166 unix_password "$SNF_IMAGE_PROPERTY_OSFAMILY" "$SNF_IMAGE_TARGET" "$SNF_IMAGE_PASSWORD"
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"
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"
178 touch "$SNF_IMAGE_TARGET/etc/spwd.db"
182 # Make sure /etc/spwd.db is recreated on first boot
184 PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin
187 pwd_mkdb -p /etc/master.passwd
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
195 mv $orig_local /etc/rc.local
199 cat > "$SNF_IMAGE_TARGET/etc/rc.local" <<EOF
209 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :