summaryrefslogtreecommitdiffstats
path: root/source3/script/tests/test_smbpasswd.sh
blob: b4d4f405e45827745ee3014759d902d3250f5c01 (plain)
1
2
3
4
5
6
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/bin/sh
#
# Blackbox tests for smbpasswd
#
# Copyright (c) 2015-2016 Andreas Schneider <asn@samba.org>
#

if [ $# -lt 4 ]; then
	cat <<EOF
Usage: test_smbpasswd.sh SERVER USERNAME PASSWORD
EOF
	exit 1
fi

SERVER=$1
SERVER_IP=$2
USERNAME=$3
PASSWORD=$4
shift 4

incdir=$(dirname $0)/../../../testprogs/blackbox
. $incdir/subunit.sh

failed=0

samba_bindir="$BINDIR"
samba_srcdir="$SRCDIR"

samba_texpect="$samba_bindir/texpect"
samba_smbpasswd="$samba_bindir/smbpasswd"

samba_test_user="alice_smbpasswd"
samba_test_user_pwd="Secret007"
samba_test_user_new_pwd="Secret008"

create_local_smb_user()
{
	user=$1
	password=$2

	tmpfile=$PREFIX/smbpasswd_create_user_script
	cat >$tmpfile <<EOF
expect New SMB password:
send $password\n
expect Retype new SMB password:
send $password\n
EOF

	cmd='UID_WRAPPER_INITIAL_RUID=0 UID_WRAPPER_INITIAL_EUID=0 $samba_texpect $tmpfile $samba_smbpasswd -c $SMB_CONF_PATH -a $user 2>&1'
	eval echo "$cmd"
	out=$(eval $cmd)
	ret=$?

	rm -f $tmpfile

	if [ $ret -ne 0 ]; then
		echo "Failed to create smb user $user"
		return 1
	fi

	getent passwd $user
	ret=$?
	if [ $ret -ne 0 ]; then
		echo "Failed to create smb user $user"
		return 1
	fi
}

delete_local_smb_user()
{
	user=$1

	# This also deletes the unix account!
	UID_WRAPPER_INITIAL_RUID=0 UID_WRAPPER_INITIAL_EUID=0 $samba_smbpasswd -c $SMB_CONF_PATH -x $user
	ret=$?
	if [ $ret -ne 0 ]; then
		echo "Failed to delete smb user $user"
		return 1
	fi
}

test_smbpasswd()
{
	user=$1
	oldpwd=$2
	newpwd=$3

	user_id=$(id -u $user)

	tmpfile=$PREFIX/smbpasswd_change_password_script
	cat >$tmpfile <<EOF
expect Old SMB password:
send $oldpwd\n
expect New SMB password:
send $newpwd\n
expect Retype new SMB password:
send $newpwd\n
EOF

	cmd='UID_WRAPPER_INITIAL_RUID=$user_id UID_WRAPPER_INITIAL_EUID=$user_id $samba_texpect $tmpfile $samba_smbpasswd -c $SMB_CONF_PATH -r $SERVER 2>&1'
	eval echo "$cmd"
	out=$(eval $cmd)
	ret=$?
	rm -f $tmpfile
	if [ $ret -ne 0 ]; then
		echo "Failed to change user password $user"
		echo "${out}"
		return 1
	fi

	prompt="Password changed for user $user"
	echo "$out" | grep "$prompt" >/dev/null 2>&1
	ret=$?
	if [ $ret -ne 0 ]; then
		echo "Failed to change password for user $user"
		echo "$out"
		return 1
	fi
}

testit "Create user $samba_test_user" \
	create_local_smb_user $samba_test_user $samba_test_user_pwd ||
	failed=$(expr $failed + 1)

testit "Change user password" \
	test_smbpasswd $samba_test_user $samba_test_user_pwd $samba_test_user_new_pwd ||
	failed=$(expr $failed + 1)

testit "Delete user $samba_test_user" \
	delete_local_smb_user $samba_test_user ||
	failed=$(expr $failed + 1)

exit $failed