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

### 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
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Short-Description:	Changes Password for specified users
### END TAST INFO

set -e
. /usr/share/snf-image/common.sh

windows_password() {
    local target=$1
    local password=$2

    local tmp_unattend=`mktemp` || exit 1
    CLEANUP+=("rm $tmp_unattend")

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

    local namespace="urn:schemas-microsoft-com:unattend"
    
    $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

    cat $tmp_unattend > "$target/Unattend.xml"
    echo done
}

linux_password() {
    local target=$1
    local password=$2

    local hash=$(/usr/share/snf-image/snf-passtohash.py $password)
    if [ ! -e ${target}/etc/shadow ]; then
       log_error "No /etc/shadow found!" 
    fi
    
    declare -a users=("root")

    local distro=$(get_distro $target)

    if [ "x$disto" = "xubuntu" -o \
         "x$disto" + "xfedora" ] ; then
        users+=("user")
    fi

    for i in $(seq 0 1 $((${#users[@]}-1))); do
        local tmp_shadow=$(mktemp)
        CLEANUP+=("rm $tmp_shadow")

        echo -n "Setting ${users[$i]} password..."
    
        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
        echo "done"
    done
}

if [ -z "$SNF_IMAGE_TARGET" ]; then
    log_error "Target dir is missing"	
fi

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

if [ "$SNF_IMAGE_TYPE" = "ntfsdump" ]; then
    windows_password $SNF_IMAGE_TARGET $SNF_IMAGE_PASSWORD
elif [ "$SNF_IMAGE_TYPE" = "extdump" ]; then
    linux_password $SNF_IMAGE_TARGET $SNF_IMAGE_PASSWORD
fi

echo "done"

cleanup
trap - EXIT

exit 0

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