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

### BEGIN TASK INFO
# Provides:		ChangePassword
Nikos Skalkotos's avatar
Nikos Skalkotos committed
5
6
# RunBefore:            UmountImage
# RunAfter:		InstallUnattend
Nikos Skalkotos's avatar
Nikos Skalkotos committed
7
# Short-Description:	Changes Password for specified users
8
### END TASK INFO
Nikos Skalkotos's avatar
Nikos Skalkotos committed
9
10

set -e
11
. "@commondir@/common.sh"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
12
13

windows_password() {
14
15
    local target="$1"
    local password="$2"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
16

17
18
    local tmp_unattend="$(mktemp)"
    add_cleanup rm "$tmp_unattend"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
19
20
21
22
23

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

    local namespace="urn:schemas-microsoft-com:unattend"
    
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"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
25

26
    cat "$tmp_unattend" > "$target/Unattend.xml"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
27
28
29
30
    echo done
}

linux_password() {
31
32
    local target="$1"
    local password="$2"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
33

34
35
    local hash=$("@scriptsdir@/snf-passtohash.py" "$password")
    if [ ! -e "$target/etc/shadow" ]; then
Nikos Skalkotos's avatar
Nikos Skalkotos committed
36
37
38
       log_error "No /etc/shadow found!" 
    fi
    
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
    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
54
55
56
    fi

    for i in $(seq 0 1 $((${#users[@]}-1))); do
57
58
        local tmp_shadow="$(mktemp)"
        add_cleanup rm "$tmp_shadow"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
59
60
61

        echo -n "Setting ${users[$i]} password..."
    
62
63
64
        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
65
66
67
68
        echo "done"
    done
}

69
70
if [ ! -d "$SNF_IMAGE_TARGET" ]; then
    log_error "Target dir: \`$SNF_IMAGE_TARGET' is missing"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
71
72
73
74
75
76
fi

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

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"
Nikos Skalkotos's avatar
Nikos Skalkotos committed
81
82
83
84
85
86
87
88
89
90
91
fi

echo "done"

cleanup
trap - EXIT

exit 0

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