Fix copyright and license notices throughout
[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:            UmountImage
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 windows_password() {
31     local target="$1"
32     local password="$2"
33
34     echo -n "Installing new admin password..."
35
36     echo "@echo off" > "$target/Windows/SnfScripts/ChangeAdminPassword.cmd"
37     echo "net user Administrator $password" >> \
38         "$target/Windows/SnfScripts/ChangeAdminPassword.cmd"
39     echo done
40 }
41
42 linux_password() {
43     local target="$1"
44     local password="$2"
45
46     local hash=$("@scriptsdir@/snf-passtohash.py" "$password")
47     if [ ! -e "$target/etc/shadow" ]; then
48        log_error "No /etc/shadow found!" 
49     fi
50     
51     declare -a users
52     
53     if [ -n "$SNF_IMAGE_PROPERTY_USERS" ]; then
54         for usr in $SNF_IMAGE_PROPERTY_USERS; do
55             users+=("$usr")
56         done
57     else
58         users+=("root")
59
60         local distro=$(get_distro $target)
61
62         if [ "x$distro" = "xubuntu" -o \
63              "x$distro" = "xfedora" ] ; then
64             users+=("user")
65         fi
66     fi
67
68     for i in $(seq 0 1 $((${#users[@]}-1))); do
69         local tmp_shadow="$(mktemp)"
70         add_cleanup rm "$tmp_shadow"
71
72         echo -n "Setting ${users[$i]} password..."
73     
74         echo "${users[$i]}:$hash:15103:0:99999:7:::" > "$tmp_shadow"
75         grep -v "${users[$i]}" "$target/etc/shadow" >> "$tmp_shadow"
76         cat "$tmp_shadow" > "$target/etc/shadow"
77         echo "done"
78     done
79 }
80
81 if [ ! -d "$SNF_IMAGE_TARGET" ]; then
82     log_error "Target dir: \`$SNF_IMAGE_TARGET' is missing"
83 fi
84
85 if [ -z "$SNF_IMAGE_PASSWORD" ]; then
86     log_error "Password is missing"
87 fi
88
89 if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "windows" ]; then
90     windows_password "$SNF_IMAGE_TARGET" "$SNF_IMAGE_PASSWORD"
91 elif [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "linux" ]; then
92     linux_password "$SNF_IMAGE_TARGET" "$SNF_IMAGE_PASSWORD"
93 fi
94
95 echo "done"
96
97 exit 0
98
99 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
100