3341c62f4b98eac4d1b72f861f8dfd4cd9b108b3
[samba.git] / source3 / script / tests / test_smbclient_s3.sh
1 #!/bin/sh
2
3 # this runs the file serving tests that are expected to pass with samba3
4
5 if [ $# -lt 7 ]; then
6 cat <<EOF
7 Usage: test_smbclient_s3.sh SERVER SERVER_IP DOMAIN USERNAME PASSWORD USERID LOCAL_PATH PREFIX SMBCLIENT WBINFO
8 EOF
9 exit 1;
10 fi
11
12 SERVER="${1}"
13 SERVER_IP="${2}"
14 DOMAIN="${3}"
15 USERNAME="${4}"
16 PASSWORD="${5}"
17 USERID="${6}"
18 LOCAL_PATH="${7}"
19 PREFIX="${8}"
20 SMBCLIENT="${9}"
21 WBINFO="${10}"
22 SMBCLIENT="$VALGRIND ${SMBCLIENT}"
23 WBINFO="$VALGRIND ${WBINFO}"
24 shift 10
25 ADDARGS="$*"
26
27 incdir=`dirname $0`/../../../testprogs/blackbox
28 . $incdir/subunit.sh
29
30 failed=0
31
32 # Test that a noninteractive smbclient does not prompt
33 test_noninteractive_no_prompt()
34 {
35     prompt="smb"
36
37     cmd='echo du | $SMBCLIENT "$@" -U$USERNAME%$PASSWORD //$SERVER/tmp -I $SERVER_IP $ADDARGS 2>&1'
38     eval echo "$cmd"
39     out=`eval $cmd`
40
41     if [ $? != 0 ] ; then
42         echo "$out"
43         echo "command failed"
44         false
45         return
46     fi
47
48     echo "$out" | grep $prompt >/dev/null 2>&1
49
50     if [ $? = 0 ] ; then
51         # got a prompt .. fail
52         echo matched interactive prompt in non-interactive mode
53         false
54     else
55         true
56     fi
57 }
58
59 # Test that an interactive smbclient prompts to stdout
60 test_interactive_prompt_stdout()
61 {
62     prompt="smb"
63     tmpfile=$PREFIX/smbclient_interactive_prompt_commands
64
65     cat > $tmpfile <<EOF
66 du
67 quit
68 EOF
69
70     cmd='CLI_FORCE_INTERACTIVE=yes $SMBCLIENT "$@" -U$USERNAME%$PASSWORD //$SERVER/tmp -I $SERVER_IP $ADDARGS < $tmpfile 2>&1'
71     eval echo "$cmd"
72     out=`eval $cmd`
73     ret=$?
74     rm -f $tmpfile
75
76     if [ $ret != 0 ] ; then
77         echo "$out"
78         echo "command failed"
79         false
80         return
81     fi
82
83     echo "$out" | grep $prompt >/dev/null 2>&1
84
85     if [ $? = 0 ] ; then
86         # got a prompt .. succeed
87         true
88     else
89         echo failed to match interactive prompt on stdout
90         false
91     fi
92 }
93
94 # Test creating a bad symlink and deleting it.
95 test_bad_symlink()
96 {
97     prompt="posix_unlink deleted file /newname"
98     tmpfile=$PREFIX/smbclient_bad_symlinks_commands
99
100     cat > $tmpfile <<EOF
101 posix
102 posix_unlink newname
103 symlink badname newname
104 posix_unlink newname
105 quit
106 EOF
107
108     cmd='CLI_FORCE_INTERACTIVE=yes $SMBCLIENT "$@" -U$USERNAME%$PASSWORD //$SERVER/tmp -I $SERVER_IP $ADDARGS < $tmpfile 2>&1'
109     eval echo "$cmd"
110     out=`eval $cmd`
111     ret=$?
112     rm -f $tmpfile
113
114     if [ $ret != 0 ] ; then
115         echo "$out"
116         echo "failed create then delete bad symlink with error $ret"
117         false
118         return
119     fi
120
121     echo "$out" | grep "$prompt" >/dev/null 2>&1
122
123     ret=$?
124     if [ $ret = 0 ] ; then
125         # got the correct prompt .. succeed
126         true
127     else
128         echo "$out"
129         echo "failed create then delete bad symlink - grep failed with $ret"
130         false
131     fi
132 }
133
134 # Test creating a good symlink and deleting it by path.
135 test_good_symlink()
136 {
137     tmpfile=$PREFIX/smbclient.in.$$
138     slink_name="$LOCAL_PATH/slink"
139     slink_target="$LOCAL_PATH/slink_target"
140
141     touch $slink_target
142     ln -s $slink_target $slink_name
143     cat > $tmpfile <<EOF
144 del slink
145 quit
146 EOF
147
148     cmd='CLI_FORCE_INTERACTIVE=yes $SMBCLIENT "$@" -U$USERNAME%$PASSWORD //$SERVER/tmp -I $SERVER_IP $ADDARGS < $tmpfile 2>&1'
149     eval echo "$cmd"
150     out=`eval $cmd`
151     ret=$?
152     rm -f $tmpfile
153
154     if [ $ret != 0 ] ; then
155         echo "$out"
156         echo "failed delete good symlink with error $ret"
157         rm $slink_target
158         rm $slink_name
159         false
160         return
161     fi
162
163     if [ ! -e $slink_target ] ; then
164         echo "failed delete good symlink - symlink target deleted !"
165         rm $slink_target
166         rm $slink_name
167         false
168         return
169     fi
170
171     if [ -e $slink_name ] ; then
172         echo "failed delete good symlink - symlink still exists"
173         rm $slink_target
174         rm $slink_name
175         false
176     else
177         # got the correct prompt .. succeed
178         rm $slink_target
179         true
180     fi
181 }
182
183 # Test writing into a read-only directory (logon as guest) fails.
184 test_read_only_dir()
185 {
186     prompt="NT_STATUS_ACCESS_DENIED making remote directory"
187     tmpfile=$PREFIX/smbclient.in.$$
188
189 ##
190 ## We can't do this as non-root. We always have rights to
191 ## create the directory.
192 ##
193     if [ "$USERID" != 0 ] ; then
194         echo "skipping test_read_only_dir as non-root"
195         true
196         return
197     fi
198
199 ##
200 ## We can't do this with an encrypted connection. No credentials
201 ## to set up the channel.
202 ##
203     if [ "$ADDARGS" = "-e" ] ; then
204         echo "skipping test_read_only_dir with encrypted connection"
205         true
206         return
207     fi
208
209     cat > $tmpfile <<EOF
210 mkdir a_test_dir
211 quit
212 EOF
213
214     cmd='CLI_FORCE_INTERACTIVE=yes $SMBCLIENT "$@" -U% //$SERVER/ro-tmp -I $SERVER_IP $ADDARGS < $tmpfile 2>&1'
215     eval echo "$cmd"
216     out=`eval $cmd`
217     ret=$?
218     rm -f $tmpfile
219
220     if [ $ret != 0 ] ; then
221         echo "$out"
222         echo "failed writing into read-only directory with error $ret"
223
224         false
225         return
226     fi
227
228     echo "$out" | grep "$prompt" >/dev/null 2>&1
229
230     ret=$?
231     if [ $ret = 0 ] ; then
232         # got the correct prompt .. succeed
233         true
234     else
235         echo "$out"
236         echo "failed writing into read-only directory - grep failed with $ret"
237         false
238     fi
239 }
240
241
242 # Test sending a message
243 test_message()
244 {
245     tmpfile=$PREFIX/message_in.$$
246
247     cat > $tmpfile <<EOF
248 Test message from pid $$
249 EOF
250
251     cmd='$SMBCLIENT "$@" -U$USERNAME%$PASSWORD -M $SERVER -p 139 $ADDARGS -n msgtest < $tmpfile 2>&1'
252     eval echo "$cmd"
253     out=`eval $cmd`
254     ret=$?
255
256     if [ $ret != 0 ] ; then
257         echo "$out"
258         echo "failed sending message to $SERVER with error $ret"
259         false
260         rm -f $tmpfile
261         return
262     fi
263
264     # The server writes this into a file message.msgtest, via message.%m to test the % sub code
265     cmd='$SMBCLIENT "$@" -U$USERNAME%$PASSWORD //$SERVER/tmpguest -p 139 $ADDARGS -c "get message.msgtest $PREFIX/message_out.$$" 2>&1'
266     eval echo "$cmd"
267     out=`eval $cmd`
268     ret=$?
269
270     if [ $ret != 0 ] ; then
271         echo "$out"
272         echo "failed getting sent message from $SERVER with error $ret"
273         false
274         return
275     fi
276
277     if [ cmp $PREFIX/message_out.$$ $tmpfile != 0 ] ; then
278         echo "failed comparison of message from $SERVER"
279         false
280         return
281     fi
282     true
283 }
284
285 # Test reading an owner-only file (logon as guest) fails.
286 test_owner_only_file()
287 {
288     prompt="NT_STATUS_ACCESS_DENIED opening remote file"
289     tmpfile=$PREFIX/smbclient.in.$$
290
291 ##
292 ## We can't do this as non-root. We always have rights to
293 ## read the file.
294 ##
295     if [ "$USERID" != 0 ] ; then
296         echo "skipping test_owner_only_file as non-root"
297         true
298         return
299     fi
300
301 ##
302 ## We can't do this with an encrypted connection. No credentials
303 ## to set up the channel.
304 ##
305     if [ "$ADDARGS" = "-e" ] ; then
306         echo "skipping test_owner_only_file with encrypted connection"
307         true
308         return
309     fi
310
311     cat > $tmpfile <<EOF
312 get unreadable_file
313 quit
314 EOF
315
316     cmd='CLI_FORCE_INTERACTIVE=yes $SMBCLIENT "$@" -U% //$SERVER/ro-tmp -I $SERVER_IP $ADDARGS < $tmpfile 2>&1'
317     eval echo "$cmd"
318     out=`eval $cmd`
319     ret=$?
320     rm -f $tmpfile
321
322     if [ $ret != 0 ] ; then
323         echo "$out"
324         echo "failed reading owner-only file with error $ret"
325         false
326         return
327     fi
328
329     echo "$out" | grep "$prompt" >/dev/null 2>&1
330
331     ret=$?
332     if [ $ret = 0 ] ; then
333         # got the correct prompt .. succeed
334         true
335     else
336         echo "$out"
337         echo "failed reading owner-only file - grep failed with $ret"
338         false
339     fi
340 }
341
342 # Test accessing an msdfs path.
343 test_msdfs_link()
344 {
345     tmpfile=$PREFIX/smbclient.in.$$
346     prompt="  msdfs-target  "
347
348     cat > $tmpfile <<EOF
349 ls
350 cd \\msdfs-src1
351 ls msdfs-target
352 quit
353 EOF
354
355     cmd='CLI_FORCE_INTERACTIVE=yes $SMBCLIENT "$@" -U$USERNAME%$PASSWORD //$SERVER/msdfs-share -I $SERVER_IP $ADDARGS < $tmpfile 2>&1'
356     eval echo "$cmd"
357     out=`eval $cmd`
358     ret=$?
359     rm -f $tmpfile
360
361     if [ $ret != 0 ] ; then
362         echo "$out"
363         echo "failed accessing \\msdfs-src1 link with error $ret"
364         false
365         return
366     fi
367
368     echo "$out" | grep "$prompt" >/dev/null 2>&1
369
370     ret=$?
371     if [ $ret != 0 ] ; then
372         echo "$out"
373         echo "failed listing \\msdfs-src1 - grep failed with $ret"
374         false
375     fi
376
377     cat > $tmpfile <<EOF
378 ls
379 cd \\deeppath\\msdfs-src2
380 ls msdfs-target
381 quit
382 EOF
383
384     cmd='CLI_FORCE_INTERACTIVE=yes $SMBCLIENT "$@" -U$USERNAME%$PASSWORD //$SERVER/msdfs-share -I $SERVER_IP $ADDARGS < $tmpfile 2>&1'
385     eval echo "$cmd"
386     out=`eval $cmd`
387     ret=$?
388     rm -f $tmpfile
389
390     if [ $ret != 0 ] ; then
391         echo "$out"
392         echo "failed accessing \\deeppath\\msdfs-src2 link with error $ret"
393         false
394         return
395     fi
396
397     echo "$out" | grep "$prompt" >/dev/null 2>&1
398
399     ret=$?
400     if [ $ret != 0 ] ; then
401         echo "$out"
402         echo "failed listing \\deeppath\\msdfs-src2 - grep failed with $ret"
403         false
404         return
405     else
406         true
407         return
408     fi
409 }
410
411 # Test authenticating using the winbind ccache
412 test_ccache_access()
413 {
414     $WBINFO --ccache-save="${USERNAME}%${PASSWORD}"
415     ret=$?
416
417     if [ $ret != 0 ] ; then
418         echo "wbinfo failed to store creds in cache (user='${USERNAME}', pass='${PASSWORD}')"
419         false
420         return
421     fi
422
423     $SMBCLIENT //$SERVER_IP/tmp -C -U "${USERNAME}%" \
424         -c quit 2>&1
425     ret=$?
426
427     if [ $ret != 0 ] ; then
428         echo "smbclient failed to use cached credentials"
429         false
430         return
431     fi
432
433     $WBINFO --ccache-save="${USERNAME}%GarBage"
434     ret=$?
435
436     if [ $ret != 0 ] ; then
437         echo "wbinfo failed to store creds in cache (user='${USERNAME}', pass='GarBage')"
438         false
439         return
440     fi
441
442     $SMBCLIENT //$SERVER_IP/tmp -C -U "${USERNAME}%" \
443         -c quit 2>&1
444     ret=$?
445
446     if [ $ret -eq 0 ] ; then
447         echo "smbclient succeeded with wrong cached credentials"
448         false
449         return
450     fi
451
452     $WBINFO --logoff
453 }
454
455 # Test authenticating using the winbind ccache
456 test_auth_file()
457 {
458     tmpfile=$PREFIX/smbclient.in.$$
459     cat > $tmpfile <<EOF
460 username=${USERNAME}
461 password=${PASSWORD}
462 domain=${DOMAIN}
463 EOF
464     $SMBCLIENT //$SERVER_IP/tmp --authentication-file=$tmpfile \
465         -c quit 2>&1
466     ret=$?
467     rm $tmpfile
468
469     if [ $ret != 0 ] ; then
470         echo "smbclient failed to use auth file"
471         false
472         return
473     fi
474
475     cat > $tmpfile <<EOF
476 username=${USERNAME}
477 password=xxxx
478 domain=${DOMAIN}
479 EOF
480     $SMBCLIENT //$SERVER_IP/tmp --authentication-file=$tmpfile\
481         -c quit 2>&1
482     ret=$?
483     rm $tmpfile
484
485     if [ $ret -eq 0 ] ; then
486         echo "smbclient succeeded with wrong auth file credentials"
487         false
488         return
489     fi
490 }
491
492 LOGDIR_PREFIX=test_smbclient_s3
493
494 # possibly remove old logdirs:
495
496 for OLDDIR in $(find ${PREFIX} -type d -name "${LOGDIR_PREFIX}_*") ;  do
497         echo "removing old directory ${OLDDIR}"
498         rm -rf ${OLDDIR}
499 done
500
501 LOGDIR=$(mktemp -d ${PREFIX}/${LOGDIR_PREFIX}_XXXXXX)
502
503
504 testit "smbclient -L $SERVER_IP" $SMBCLIENT -L $SERVER_IP -N -p 139 || failed=`expr $failed + 1`
505 testit "smbclient -L $SERVER -I $SERVER_IP" $SMBCLIENT -L $SERVER -I $SERVER_IP -N -p 139 -c quit || failed=`expr $failed + 1`
506
507 testit "noninteractive smbclient does not prompt" \
508     test_noninteractive_no_prompt || \
509     failed=`expr $failed + 1`
510
511 testit "noninteractive smbclient -l does not prompt" \
512    test_noninteractive_no_prompt -l $LOGDIR || \
513     failed=`expr $failed + 1`
514
515 testit "interactive smbclient prompts on stdout" \
516    test_interactive_prompt_stdout || \
517     failed=`expr $failed + 1`
518
519 testit "interactive smbclient -l prompts on stdout" \
520    test_interactive_prompt_stdout -l $LOGDIR || \
521     failed=`expr $failed + 1`
522
523 testit "creating a bad symlink and deleting it" \
524    test_bad_symlink || \
525    failed=`expr $failed + 1`
526
527 testit "creating a good symlink and deleting it by path" \
528    test_good_symlink || \
529    failed=`expr $failed + 1`
530
531 testit "writing into a read-only directory fails" \
532    test_read_only_dir || \
533    failed=`expr $failed + 1`
534
535 testit "Reading a owner-only file fails" \
536    test_owner_only_file || \
537    failed=`expr $failed + 1`
538
539 testit "Accessing an MS-DFS link" \
540    test_msdfs_link || \
541    failed=`expr $failed + 1`
542
543 testit "ccache access works for smbclient" \
544     test_ccache_access || \
545     failed=`expr $failed + 1`
546
547 testit "sending a message to the remote server" \
548     test_message || \
549     failed=`expr $failed + 1`
550
551 testit "using an authentication file" \
552     test_auth_file || \
553     failed=`expr $failed + 1`
554
555 testit "rm -rf $LOGDIR" \
556     rm -rf $LOGDIR || \
557     failed=`expr $failed + 1`
558
559 testok $0 $failed