Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (3.1 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:            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
windows_password() {
37
    local target="$1"
38
    local password="$2"
39

    
40
    echo "@echo off" > "$target/Windows/SnfScripts/ChangeAdminPassword.cmd"
41

    
42
    if [ -z "$SNF_IMAGE_PROPERTY_USERS" ]; then
43
        SNF_IMAGE_PROPERTY_USERS="Administrator"
44
    fi
45

    
46
    for usr in $SNF_IMAGE_PROPERTY_USERS; do
47
        echo -n "Installing new password for user \`$usr'..."
48
        echo "net user $usr $password" >> \
49
            "$target/Windows/SnfScripts/ChangeAdminPassword.cmd"
50
        echo done
51
    done
52
}
53

    
54
linux_password() {
55
    local target="$1"
56
    local password="$2"
57

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

    
72
        local distro=$(get_distro $target)
73

    
74
        if [ "x$distro" = "xubuntu" -o \
75
             "x$distro" = "xfedora" ] ; then
76
            users+=("user")
77
        fi
78
    fi
79

    
80
    for i in $(seq 0 1 $((${#users[@]}-1))); do
81
        local tmp_shadow="$(mktemp)"
82
        add_cleanup rm "$tmp_shadow"
83

    
84
        echo -n "Setting ${users[$i]} password..."
85
    
86
        echo "${users[$i]}:$hash:15103:0:99999:7:::" > "$tmp_shadow"
87
        grep -v "${users[$i]}" "$target/etc/shadow" >> "$tmp_shadow"
88
        cat "$tmp_shadow" > "$target/etc/shadow"
89
        echo "done"
90
    done
91
}
92

    
93
if [ ! -d "$SNF_IMAGE_TARGET" ]; then
94
    log_error "Target dir: \`$SNF_IMAGE_TARGET' is missing"
95
fi
96

    
97
if [ -z "$SNF_IMAGE_PASSWORD" ]; then
98
    log_error "Password is missing"
99
fi
100

    
101
#trim users var
102
SNF_IMAGE_PROPERTY_USERS=$(echo $SNF_IMAGE_PROPERTY_USERS)
103

    
104
if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "windows" ]; then
105
    windows_password "$SNF_IMAGE_TARGET" "$SNF_IMAGE_PASSWORD"
106
elif [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "linux" ]; then
107
    linux_password "$SNF_IMAGE_TARGET" "$SNF_IMAGE_PASSWORD"
108
fi
109

    
110
exit 0
111

    
112
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
113