50ChangePassword.in 3.07 KB
Newer Older
1
#! /bin/bash
Nikos Skalkotos's avatar
Nikos Skalkotos committed
2

3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Copyright (C) 2011 GRNET S.A. 
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.

Nikos Skalkotos's avatar
Nikos Skalkotos committed
20
21
### BEGIN TASK INFO
# Provides:		ChangePassword
Nikos Skalkotos's avatar
Nikos Skalkotos committed
22
23
# RunBefore:            UmountImage
# RunAfter:		InstallUnattend
Nikos Skalkotos's avatar
Nikos Skalkotos committed
24
# Short-Description:	Changes Password for specified users
25
### END TASK INFO
Nikos Skalkotos's avatar
Nikos Skalkotos committed
26
27

set -e
28
. "@commondir@/common.sh"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
29

30
31
32
trap task_cleanup EXIT
report_start_task

33
34
35
# Check if the task should be prevented from running.
check_if_excluded

Nikos Skalkotos's avatar
Nikos Skalkotos committed
36
windows_password() {
37
38
    local target="$1"
    local password="$2"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
39

Nikos Skalkotos's avatar
Nikos Skalkotos committed
40
    echo "@echo off" > "$target/Windows/SnfScripts/ChangeAdminPassword.cmd"
41
42
43
44
45
46
47
48
49
50
51

    if [ -z "$SNF_IMAGE_PROPERTY_USERS" ]; then
        SNF_IMAGE_PROPERTY_USERS="Administrator"
    fi

    for usr in $SNF_IMAGE_PROPERTY_USERS; do
        echo -n "Installing new password for user \`$usr'..."
        echo "net user $usr $password" >> \
            "$target/Windows/SnfScripts/ChangeAdminPassword.cmd"
        echo done
    done
Nikos Skalkotos's avatar
Nikos Skalkotos committed
52
53
54
}

linux_password() {
55
56
    local target="$1"
    local password="$2"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
57

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

        local distro=$(get_distro $target)

        if [ "x$distro" = "xubuntu" -o \
             "x$distro" = "xfedora" ] ; then
            users+=("user")
        fi
Nikos Skalkotos's avatar
Nikos Skalkotos committed
78
79
80
    fi

    for i in $(seq 0 1 $((${#users[@]}-1))); do
81
82
        local tmp_shadow="$(mktemp)"
        add_cleanup rm "$tmp_shadow"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
83
84
85

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

93
94
if [ ! -d "$SNF_IMAGE_TARGET" ]; then
    log_error "Target dir: \`$SNF_IMAGE_TARGET' is missing"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
95
96
97
98
99
100
fi

if [ -z "$SNF_IMAGE_PASSWORD" ]; then
    log_error "Password is missing"
fi

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

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"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
108
109
110
111
112
113
fi

exit 0

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