Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (2.2 kB)

1
#! /bin/bash
2

    
3
### BEGIN TASK INFO
4
# Provides:		ChangePassword
5
# RunBefore:            UmountImage
6
# RunAfter:		InstallUnattend
7
# Short-Description:	Changes Password for specified users
8
### END TASK INFO
9

    
10
set -e
11
. "@commondir@/common.sh"
12

    
13
windows_password() {
14
    local target="$1"
15
    local password="$2"
16

    
17
    local tmp_unattend="$(mktemp)"
18
    add_cleanup rm "$tmp_unattend"
19

    
20
    echo -n "Installing new admin password..."
21

    
22
    local namespace="urn:schemas-microsoft-com:unattend"
23
    
24
    "$XMLSTARLET" ed -N x=$namespace -u "/x:unattend/x:settings/x:component/x:UserAccounts/x:AdministratorPassword/x:Value" -v "$password" "$target/Unattend.xml" > "$tmp_unattend"
25

    
26
    cat "$tmp_unattend" > "$target/Unattend.xml"
27
    echo done
28
}
29

    
30
linux_password() {
31
    local target="$1"
32
    local password="$2"
33

    
34
    local hash=$("@scriptsdir@/snf-passtohash.py" "$password")
35
    if [ ! -e "$target/etc/shadow" ]; then
36
       log_error "No /etc/shadow found!" 
37
    fi
38
    
39
    declare -a users
40
    
41
    if [ -n "$SNF_IMAGE_PROPERTY_USERS" ]; then
42
        for usr in $SNF_IMAGE_PROPERTY_USERS; do
43
            users+=("$usr")
44
        done
45
    else
46
        users+=("root")
47

    
48
        local distro=$(get_distro $target)
49

    
50
        if [ "x$distro" = "xubuntu" -o \
51
             "x$distro" = "xfedora" ] ; then
52
            users+=("user")
53
        fi
54
    fi
55

    
56
    for i in $(seq 0 1 $((${#users[@]}-1))); do
57
        local tmp_shadow="$(mktemp)"
58
        add_cleanup rm "$tmp_shadow"
59

    
60
        echo -n "Setting ${users[$i]} password..."
61
    
62
        echo "${users[$i]}:$hash:15103:0:99999:7:::" > "$tmp_shadow"
63
        grep -v "${users[$i]}" "$target/etc/shadow" >> "$tmp_shadow"
64
        cat "$tmp_shadow" > "$target/etc/shadow"
65
        echo "done"
66
    done
67
}
68

    
69
if [ ! -d "$SNF_IMAGE_TARGET" ]; then
70
    log_error "Target dir: \`$SNF_IMAGE_TARGET' is missing"
71
fi
72

    
73
if [ -z "$SNF_IMAGE_PASSWORD" ]; then
74
    log_error "Password is missing"
75
fi
76

    
77
if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "windows" ]; then
78
    windows_password "$SNF_IMAGE_TARGET" "$SNF_IMAGE_PASSWORD"
79
elif [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "linux" ]; then
80
    linux_password "$SNF_IMAGE_TARGET" "$SNF_IMAGE_PASSWORD"
81
fi
82

    
83
echo "done"
84

    
85
exit 0
86

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