Statistics
| Branch: | Tag: | Revision:

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

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
# Check if the task should be prevented from running.
31
check_if_excluded
32

    
33
windows_password() {
34
    local target="$1"
35
    local password="$2"
36

    
37
    echo "@echo off" > "$target/Windows/SnfScripts/ChangeAdminPassword.cmd"
38

    
39
    if [ -z "$SNF_IMAGE_PROPERTY_USERS" ]; then
40
        SNF_IMAGE_PROPERTY_USERS="Administrator"
41
    fi
42

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

    
51
linux_password() {
52
    local target="$1"
53
    local password="$2"
54

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

    
69
        local distro=$(get_distro $target)
70

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

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

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

    
90
if [ ! -d "$SNF_IMAGE_TARGET" ]; then
91
    log_error "Target dir: \`$SNF_IMAGE_TARGET' is missing"
92
fi
93

    
94
if [ -z "$SNF_IMAGE_PASSWORD" ]; then
95
    log_error "Password is missing"
96
fi
97

    
98
#trim users var
99
SNF_IMAGE_PROPERTY_USERS=$(echo $SNF_IMAGE_PROPERTY_USERS)
100

    
101
if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "windows" ]; then
102
    windows_password "$SNF_IMAGE_TARGET" "$SNF_IMAGE_PASSWORD"
103
elif [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "linux" ]; then
104
    linux_password "$SNF_IMAGE_TARGET" "$SNF_IMAGE_PASSWORD"
105
fi
106

    
107
exit 0
108

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