summaryrefslogtreecommitdiffstats
path: root/python/samba/tests/samba_tool/user_edit.sh
blob: 342899f3f0fc2c97d7acd81db2b350522199beae (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
195
196
197
198
#!/bin/sh
#
# Test for 'samba-tool user edit'

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

SERVER="$1"
USERNAME="$2"
PASSWORD="$3"

samba_ldbsearch=ldbsearch
if test -x $BINDIR/ldbsearch; then
	samba_ldbsearch=$BINDIR/ldbsearch
fi

STpath=$(pwd)
. $STpath/testprogs/blackbox/subunit.sh

display_name="Björn"
display_name_b64="QmrDtnJu"
display_name_new="Renamed Bjoern"
# attribute value including control character
# echo -e "test \a string" | base64
display_name_con_b64="dGVzdCAHIHN0cmluZwo="

tmpeditor=$(mktemp --suffix .sh -p ${SELFTEST_TMPDIR} samba-tool-editor-XXXXXXXX)
chmod +x $tmpeditor

TEST_USER="$(mktemp -u sambatoolXXXXXX)"

create_test_user()
{
	$PYTHON ${STpath}/source4/scripting/bin/samba-tool \
		user create ${TEST_USER} --random-password \
		-H "ldap://$SERVER" "-U$USERNAME" "--password=$PASSWORD"
}

edit_user()
{
	# create editor.sh
	cat >$tmpeditor <<-'EOF'
#!/usr/bin/env bash
user_ldif="$1"
SED=$(which sed)
$SED -i -e 's/userAccountControl: 512/userAccountControl: 514/' $user_ldif
	EOF

	$PYTHON ${STpath}/source4/scripting/bin/samba-tool \
		user edit ${TEST_USER} --editor=$tmpeditor \
		-H "ldap://$SERVER" "-U$USERNAME" "--password=$PASSWORD"
}

# Test edit user - add base64 attributes
add_attribute_base64()
{
	# create editor.sh
	cat >$tmpeditor <<EOF
#!/usr/bin/env bash
user_ldif="\$1"

grep -v '^\$' \$user_ldif > \${user_ldif}.tmp
echo "displayName:: $display_name_b64" >> \${user_ldif}.tmp

mv \${user_ldif}.tmp \$user_ldif
EOF

	$PYTHON ${STpath}/source4/scripting/bin/samba-tool user edit \
		${TEST_USER} --editor=$tmpeditor \
		-H "ldap://$SERVER" "-U$USERNAME" "--password=$PASSWORD"
}

get_attribute_base64()
{
	$samba_ldbsearch "(sAMAccountName=${TEST_USER})" displayName \
		-H "ldap://$SERVER" "-U$USERNAME" "--password=$PASSWORD"
}

delete_attribute()
{
	# create editor.sh
	cat >$tmpeditor <<EOF
#!/usr/bin/env bash
user_ldif="\$1"

grep -v '^displayName' \$user_ldif >> \${user_ldif}.tmp
mv \${user_ldif}.tmp \$user_ldif
EOF
	$PYTHON ${STpath}/source4/scripting/bin/samba-tool user edit \
		${TEST_USER} --editor=$tmpeditor \
		-H "ldap://$SERVER" "-U$USERNAME" "--password=$PASSWORD"
}

# Test edit user - add base64 attribute value including control character
add_attribute_base64_control()
{
	# create editor.sh
	cat >$tmpeditor <<EOF
#!/usr/bin/env bash
user_ldif="\$1"

grep -v '^\$' \$user_ldif > \${user_ldif}.tmp
echo "displayName:: $display_name_con_b64" >> \${user_ldif}.tmp

mv \${user_ldif}.tmp \$user_ldif
EOF
	$PYTHON ${STpath}/source4/scripting/bin/samba-tool user edit \
		${TEST_USER} --editor=$tmpeditor \
		-H "ldap://$SERVER" "-U$USERNAME" "--password=$PASSWORD"
}

get_attribute_base64_control()
{
	$PYTHON ${STpath}/source4/scripting/bin/samba-tool user show \
		${TEST_USER} --attributes=displayName \
		-H "ldap://$SERVER" "-U$USERNAME" "--password=$PASSWORD"
}

get_attribute_force_no_base64()
{
	# LDB_FLAG_FORCE_NO_BASE64_LDIF should be used here.
	$PYTHON ${STpath}/source4/scripting/bin/samba-tool user show \
		${TEST_USER} --attributes=displayName \
		-H "ldap://$SERVER" "-U$USERNAME" "--password=$PASSWORD"
}

# Test edit user - change base64 attribute value including control character
change_attribute_base64_control()
{
	# create editor.sh
	cat >$tmpeditor <<EOF
#!/usr/bin/env bash
user_ldif="\$1"

sed -i -e 's/displayName:: $display_name_con_b64/displayName: $display_name/' \
	\$user_ldif
EOF
	$PYTHON ${STpath}/source4/scripting/bin/samba-tool user edit \
		${TEST_USER} --editor=$tmpeditor \
		-H "ldap://$SERVER" "-U$USERNAME" "--password=$PASSWORD"
}

# Test edit user - change attributes with LDB_FLAG_FORCE_NO_BASE64_LDIF
change_attribute_force_no_base64()
{
	# create editor.sh
	# Expects that the original attribute is available as clear text,
	# because the LDB_FLAG_FORCE_NO_BASE64_LDIF should be used here.
	cat >$tmpeditor <<EOF
#!/usr/bin/env bash
user_ldif="\$1"

sed -i -e 's/displayName: $display_name/displayName: $display_name_new/' \
	\$user_ldif
EOF

	$PYTHON ${STpath}/source4/scripting/bin/samba-tool user edit \
		${TEST_USER} --editor=$tmpeditor \
		-H "ldap://$SERVER" "-U$USERNAME" "--password=$PASSWORD"
}

get_changed_attribute_force_no_base64()
{
	$PYTHON ${STpath}/source4/scripting/bin/samba-tool user show \
		${TEST_USER} --attributes=displayName \
		-H "ldap://$SERVER" "-U$USERNAME" "--password=$PASSWORD"
}

delete_user()
{
	$PYTHON ${STpath}/source4/scripting/bin/samba-tool \
		user delete ${TEST_USER} \
		-H "ldap://$SERVER" "-U$USERNAME" "--password=$PASSWORD"
}

failed=0

testit "create_test_user" create_test_user || failed=$(expr $failed + 1)
testit "edit_user" edit_user || failed=$(expr $failed + 1)
testit "add_attribute_base64" add_attribute_base64 || failed=$(expr $failed + 1)
testit_grep "get_attribute_base64" "^displayName:: $display_name_b64" get_attribute_base64 || failed=$(expr $failed + 1)
testit "delete_attribute" delete_attribute || failed=$(expr $failed + 1)
testit "add_attribute_base64_control" add_attribute_base64_control || failed=$(expr $failed + 1)
testit_grep "get_attribute_base64_control" "^displayName:: $display_name_con_b64" get_attribute_base64_control || failed=$(expr $failed + 1)
testit "change_attribute_base64_control" change_attribute_base64_control || failed=$(expr $failed + 1)
testit_grep "get_attribute_base64" "^displayName:: $display_name_b64" get_attribute_base64 || failed=$(expr $failed + 1)
testit_grep "get_attribute_force_no_base64" "^displayName: $display_name" get_attribute_force_no_base64 || failed=$(expr $failed + 1)
testit "change_attribute_force_no_base64" change_attribute_force_no_base64 || failed=$(expr $failed + 1)
testit_grep "get_changed_attribute_force_no_base64" "^displayName: $display_name_new" get_changed_attribute_force_no_base64 || failed=$(expr $failed + 1)
testit "delete_user" delete_user || failed=$(expr $failed + 1)

rm -f $tmpeditor

exit $failed