summaryrefslogtreecommitdiffstats
path: root/testprogs/blackbox/test_alias_membership.sh
blob: 510940a6f42959cd96168b179e28adc782d5d343 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/bin/bash

if [ $# -lt 1 ]; then
	cat <<EOF
Usage: test_alias_membership.sh PREFIX
EOF
	exit 1
fi

PREFIX=$1
ADDC_CONFIGFILE="$PREFIX/ad_dc/etc/smb.conf"
shift 5
failed=0

. "$(dirname "$0")/subunit.sh"
. "$(dirname "$0")/common_test_fns.inc"

net_tool="${BINDIR}/net"
wbinfo="${BINDIR}/wbinfo"

# Create the following membership structure and test if exactly the 9 users
# are listed as members of the alias A1.
#
#  A1
#  |- A1U1
#  |- A1U2
#  |
#  |- G1
#  |  |- G1U1
#  |  |- G1U2
#  |
#  |- G2
#  |  |- G2U1
#  |  |- G2U2
#  |  |
#  |  |- G3
#  |     |- G3U1
#  |     |- G3U2
#  |
#  |- LG1
#     |- user1
#
#
# Local entities:
#
# Alias: A1
# Group: LG1
# User:  user1 (no need to create, already available)
#
# Domain entities:
#
# Group: ADDOMAIN/G1 ADDOMAIN/G2 ADDOMAIN/G3
# User: ADDOMAIN/A1U1 ADDOMAIN/A1U2
#       ADDOMAIN/G1U1 ADDOMAIN/G1U2
#       ADDOMAIN/G2U1 ADDOMAIN/G2U2
#       ADDOMAIN/G3U1 ADDOMAIN/G3U2


LOCALPREFIX="IDMAPRIDMEMBER"

function create_group() {
	_group_name="${1}"
	GNUPGHOME="${PREFIX}/ad_dc/gnupg" "${PYTHON}" "${BINDIR}/samba-tool" \
		group add "${_group_name}" --configfile="${ADDC_CONFIGFILE}"
	_ret=$?
	if [ ${_ret} -ne 0 ]; then
		echo "Failed to create group ${_group_name}"
		return 1
	fi
	return 0
}

function delete_group() {
	_group_name="${1}"
	GNUPGHOME="${PREFIX}/ad_dc/gnupg" "${PYTHON}" "${BINDIR}/samba-tool" \
		group delete "${_group_name}" --configfile="${ADDC_CONFIGFILE}"
	_ret=$?
	if [ ${_ret} -ne 0 ]; then
		echo "Failed to delete group ${_group_name}"
		return 1
	fi
	return 0
}

function create_user() {
	_user_name="${1}"
	_password="${2}"
	GNUPGHOME="${PREFIX}/ad_dc/gnupg" "${PYTHON}" "${BINDIR}/samba-tool" \
		user create "${_user_name}" "${_password}" \
		--configfile="${ADDC_CONFIGFILE}"
	_ret=$?
	if [ ${_ret} -ne 0 ]; then
		echo "Failed to create user ${_user_name}"
		return 1
	fi
	return 0
}

function delete_user() {
	_user_name="${1}"
	GNUPGHOME="${PREFIX}/ad_dc/gnupg" "${PYTHON}" "${BINDIR}/samba-tool" \
		user delete "${_user_name}" --configfile="${ADDC_CONFIGFILE}"
	_ret=$?
	if [ ${_ret} -ne 0 ]; then
		echo "Failed to delete user ${_user_name}"
		return 1
	fi
	return 0
}

for G in G1 G2 G3
do
	testit "create group '$G'" create_group "${G}" || failed=$((failed + 1))
done

for U in G1U1 G1U2 G2U1 G2U2 G3U1 G3U2 A1U1 A1U2
do
	testit "create user '$U'" create_user "${U}" Passw0rd.7 || failed=$((failed + 1))
done

while read -a line
do
	group=${line[0]}
	member=${line[1]}
	testit "add member '$member' to group '$group'" "$PYTHON" "$BINDIR/samba-tool" group addmembers --configfile="$ADDC_CONFIGFILE" "$group" "$member" || failed=$((failed + 1))
done <<___MEMBERS
G1 G1U1
G1 G1U2
G2 G2U1
G2 G2U2
G2 G3
G3 G3U1
G3 G3U2
___MEMBERS

testit "net sam createlocalgroup A1" "$VALGRIND" "$net_tool" sam createlocalgroup A1 || failed=$((failed + 1))
testit "net createdomaingroup LG1" "$VALGRIND" "$net_tool" sam createdomaingroup LG1 || failed=$((failed + 1))
testit "net sam addmem user1 to LG1" "$VALGRIND" "$net_tool" sam addmem LG1 "${LOCALPREFIX}\user1" || failed=$((failed + 1))

for M in "ADDOMAIN\A1U1" "ADDOMAIN\A1U2" "ADDOMAIN\G1" "ADDOMAIN\G2" "${LOCALPREFIX}\LG1"
do
	testit "net sam addmem $M to A1" "$VALGRIND" "$net_tool" sam addmem A1 "$M" || failed=$((failed + 1))
done

# do not use testit_grep (that would call 9 times wbinfo) but use grep on the
# stored output
a1_alias=$( "$wbinfo" --group-info A1 )

for U in ADDOMAIN/a1u1 "${LOCALPREFIX}/user1" ADDOMAIN/g1u1 ADDOMAIN/g1u2 ADDOMAIN/g2u1 ADDOMAIN/g2u2 ADDOMAIN/g3u1 ADDOMAIN/g3u2 ADDOMAIN/a1u2;
do
	count=$(echo "$a1_alias" | grep -c "$U")
	testit "User $U is in alias" test "$count" -eq 1 || failed=$((failed + 1))
done

# check that there are exactly 8 commas separating the 9 users
count=$(echo "$a1_alias" | grep -o , | wc -l)
testit "There are 9 users" test "$count" -eq 8 || failed=$((failed + 1))

# cleanup

for M in "ADDOMAIN\A1U1" "ADDOMAIN\A1U2" "ADDOMAIN\G1" "ADDOMAIN\G2" "${LOCALPREFIX}\LG1"
do
	testit "net sam delmem $M from A1" "$VALGRIND" "$net_tool" sam delmem A1 "$M" || failed=$((failed + 1))
done
testit "net sam delmem user1 from LG1" "$VALGRIND" "$net_tool" sam delmem LG1 "${LOCALPREFIX}\user1" || failed=$((failed + 1))
testit "net sam deletelocalgroup A1" "$VALGRIND" "$net_tool" sam deletelocalgroup A1 || failed=$((failed + 1))
testit "net sam deletedomaingroup LG1" "$VALGRIND" "$net_tool" sam deletedomaingroup LG1 || failed=$((failed + 1))

while read -a line
do
	group=${line[0]}
	member=${line[1]}
	testit "del member '$member' from group '$group'" "$PYTHON" "$BINDIR/samba-tool" group removemembers --configfile="$ADDC_CONFIGFILE" "$group" "$member" || failed=$((failed + 1))
done <<___MEMBERS
G1 G1U1
G1 G1U2
G2 G2U1
G2 G2U2
G2 G3
G3 G3U1
G3 G3U2
___MEMBERS

for G in G1 G2 G3
do
	testit "delete group '$G'" delete_group "${G}" || failed=$((failed + 1))
done

for U in G1U1 G1U2 G2U1 G2U2 G3U1 G3U2 A1U1 A1U2
do
	testit "delete user '$U'" delete_user "${U}" || failed=$((failed + 1))
done

exit $failed