Statistics
| Branch: | Tag: | Revision:

root / snf-image-helper / tasks / 50ChangePassword.in @ 9912db89

History | View | Annotate | Download (3 kB)

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 "@echo off" > "$target/Windows/SnfScripts/ChangeAdminPassword.cmd"
35

    
36
    if [ -z "$SNF_IMAGE_PROPERTY_USERS" ]; then
37
        SNF_IMAGE_PROPERTY_USERS="Administrator"
38
    fi
39

    
40
    for usr in $SNF_IMAGE_PROPERTY_USERS; do
41
        echo -n "Installing new password for user \`$usr'..."
42
        echo "net user $usr $password" >> \
43
            "$target/Windows/SnfScripts/ChangeAdminPassword.cmd"
44
        echo done
45
    done
46
}
47

    
48
linux_password() {
49
    local target="$1"
50
    local password="$2"
51

    
52
    local hash=$("@scriptsdir@/snf-passtohash.py" "$password")
53
    if [ ! -e "$target/etc/shadow" ]; then
54
       log_error "No /etc/shadow found!" 
55
    fi
56
    
57
    declare -a users
58
    
59
    if [ -n "$SNF_IMAGE_PROPERTY_USERS" ]; then
60
        for usr in $SNF_IMAGE_PROPERTY_USERS; do
61
            users+=("$usr")
62
        done
63
    else
64
        users+=("root")
65

    
66
        local distro=$(get_distro $target)
67

    
68
        if [ "x$distro" = "xubuntu" -o \
69
             "x$distro" = "xfedora" ] ; then
70
            users+=("user")
71
        fi
72
    fi
73

    
74
    for i in $(seq 0 1 $((${#users[@]}-1))); do
75
        local tmp_shadow="$(mktemp)"
76
        add_cleanup rm "$tmp_shadow"
77

    
78
        echo -n "Setting ${users[$i]} password..."
79
    
80
        echo "${users[$i]}:$hash:15103:0:99999:7:::" > "$tmp_shadow"
81
        grep -v "${users[$i]}" "$target/etc/shadow" >> "$tmp_shadow"
82
        cat "$tmp_shadow" > "$target/etc/shadow"
83
        echo "done"
84
    done
85
}
86

    
87
if [ ! -d "$SNF_IMAGE_TARGET" ]; then
88
    log_error "Target dir: \`$SNF_IMAGE_TARGET' is missing"
89
fi
90

    
91
if [ -z "$SNF_IMAGE_PASSWORD" ]; then
92
    log_error "Password is missing"
93
fi
94

    
95
#trim users var
96
SNF_IMAGE_PROPERTY_USERS=$(echo $SNF_IMAGE_PROPERTY_USERS)
97

    
98
if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "windows" ]; then
99
    windows_password "$SNF_IMAGE_TARGET" "$SNF_IMAGE_PASSWORD"
100
elif [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "linux" ]; then
101
    linux_password "$SNF_IMAGE_TARGET" "$SNF_IMAGE_PASSWORD"
102
fi
103

    
104
echo "done"
105

    
106
exit 0
107

    
108
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
109