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"
39 linux_change_shadow_entry() {
44 IFS=":" read -a entry <<< "$line"
46 echo "${entry[0]}:$encrypted:15103:0:99999:7:::"
49 freebsd_change_shadow_entry() {
54 IFS=":" read -a entry <<< "$line"
56 echo "${entry[0]}:$encrypted:${entry[2]}:${entry[3]}:${entry[4]}:${entry[5]}:0:${entry[7]}:${entry[8]}:${entry[9]}"
59 openbsd_change_shadow_entry() {
60 freebsd_change_shadow_entry "$@"
63 netbsd_change_shadow_entry() {
64 freebsd_change_shadow_entry "$@"
72 echo "@echo off" > "$target/Windows/SnfScripts/ChangeAdminPassword.cmd"
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'."
78 SNF_IMAGE_PROPERTY_USERS="Administrator"
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"
90 local flavor target password hash users tmp_shadow
95 shadow="${flavor}_shadow"
96 if [ ! -e "$target${!shadow}" ]; then
97 log_error "No ${!shadow} found!"
100 hash=$("@scriptsdir@/snf-passtohash.py" "$password")
104 if [ -n "$SNF_IMAGE_PROPERTY_USERS" ]; then
105 for usr in $SNF_IMAGE_PROPERTY_USERS; do
109 warn "Image property \`USERS' is missing or empty. " \
110 "Changing the password for default user: \`root'."
114 for i in $(seq 0 1 $((${#users[@]}-1))); do
115 tmp_shadow="$(mktemp)"
116 add_cleanup rm "$tmp_shadow"
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."
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}"
133 local target password hash
137 if [ ! -e "$target/etc/master.passwd" ]; then
138 log_error "No /etc/master.passwd found!"
141 hash=$("@scriptsdir@/snf-passtohash.py" "$password")
142 for i in $(seq 0 1 $((${#users[@]}-1))); do
143 tmp_master="$(mktemp)"
147 if [ ! -d "$SNF_IMAGE_TARGET" ]; then
148 log_error "Target dir: \`$SNF_IMAGE_TARGET' is missing"
151 if [ -z "$SNF_IMAGE_PASSWORD" ]; then
152 log_error "Password is missing"
156 SNF_IMAGE_PROPERTY_USERS=$(echo $SNF_IMAGE_PROPERTY_USERS)
158 if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "windows" ]; then
159 windows_password "$SNF_IMAGE_TARGET" "$SNF_IMAGE_PASSWORD"
161 unix_password "$SNF_IMAGE_PROPERTY_OSFAMILY" "$SNF_IMAGE_TARGET" "$SNF_IMAGE_PASSWORD"
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"
168 # Make sure /etc/spwd.db is recreated on first boot
170 PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin
173 pwd_mkdb -p /etc/master.passwd
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
181 mv $orig_local /etc/rc.local
185 cat > "$SNF_IMAGE_TARGET/etc/rc.local" <<EOF
195 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :