Fix the shell script in the root case. When run as root, make test now detects CAP_DA...
[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 5 ]; then
6 cat <<EOF
7 Usage: test_smbclient_s3.sh SERVER SERVER_IP USERNAME PASSWORD USERID
8 EOF
9 exit 1;
10 fi
11
12 SERVER="$1"
13 SERVER_IP="$2"
14 USERNAME="$3"
15 PASSWORD="$4"
16 USERID="$5"
17 SMBCLIENT="$VALGRIND ${SMBCLIENT:-$BINDIR/smbclient} $CONFIGURATION"
18 shift 5
19 ADDARGS="$*"
20
21 test x"$TEST_FUNCTIONS_SH" != x"INCLUDED" && {
22 incdir=`dirname $0`
23 . $incdir/test_functions.sh
24 }
25
26 failed=0
27
28 # Test that a noninteractive smbclient does not prompt
29 test_noninteractive_no_prompt()
30 {
31     prompt="smb"
32
33     cmd='echo du | $SMBCLIENT $CONFIGURATION "$@" -U$USERNAME%$PASSWORD //$SERVER/tmp -I $SERVER_IP $ADDARGS 2>&1'
34     eval echo "$cmd"
35     out=`eval $cmd`
36
37     if [ $? != 0 ] ; then
38         echo "$out"
39         echo "command failed"
40         false
41         return
42     fi
43
44     echo "$out" | grep $prompt >/dev/null 2>&1
45
46     if [ $? = 0 ] ; then
47         # got a prompt .. fail
48         echo matched interactive prompt in non-interactive mode
49         false
50     else
51         true
52     fi
53 }
54
55 # Test that an interactive smbclient prompts to stdout
56 test_interactive_prompt_stdout()
57 {
58     prompt="smb"
59     tmpfile=/tmp/smbclient.in.$$
60
61     cat > $tmpfile <<EOF
62 du
63 quit
64 EOF
65
66     cmd='CLI_FORCE_INTERACTIVE=yes $SMBCLIENT $CONFIGURATION "$@" -U$USERNAME%$PASSWORD //$SERVER/tmp -I $SERVER_IP $ADDARGS < $tmpfile 2>&1'
67     eval echo "$cmd"
68     out=`eval $cmd`
69     ret=$?
70     rm -f $tmpfile
71
72     if [ $ret != 0 ] ; then
73         echo "$out"
74         echo "command failed"
75         false
76         return
77     fi
78
79     echo "$out" | grep $prompt >/dev/null 2>&1
80
81     if [ $? = 0 ] ; then
82         # got a prompt .. succeed
83         true
84     else
85         echo failed to match interactive prompt on stdout
86         false
87     fi
88 }
89
90 # Test creating a bad symlink and deleting it.
91 test_bad_symlink()
92 {
93     prompt="posix_unlink deleted file /newname"
94     tmpfile=/tmp/smbclient.in.$$
95
96     cat > $tmpfile <<EOF
97 posix
98 posix_unlink newname
99 symlink badname newname
100 posix_unlink newname
101 quit
102 EOF
103
104     cmd='CLI_FORCE_INTERACTIVE=yes $SMBCLIENT $CONFIGURATION "$@" -U$USERNAME%$PASSWORD //$SERVER/tmp -I $SERVER_IP $ADDARGS < $tmpfile 2>&1'
105     eval echo "$cmd"
106     out=`eval $cmd`
107     ret=$?
108     rm -f $tmpfile
109
110     if [ $ret != 0 ] ; then
111         echo "$out"
112         echo "failed create then delete bad symlink with error $ret"
113         false
114         return
115     fi
116
117     echo "$out" | grep "$prompt" >/dev/null 2>&1
118
119     ret=$?
120     if [ $ret = 0 ] ; then
121         # got the correct prompt .. succeed
122         true
123     else
124         echo "$out"
125         echo "failed create then delete bad symlink - grep failed with $ret"
126         false
127     fi
128 }
129
130 # Test writing into a read-only directory (logon as guest) fails.
131 test_read_only_dir()
132 {
133     prompt="NT_STATUS_ACCESS_DENIED making remote directory"
134     tmpfile=/tmp/smbclient.in.$$
135
136 ##
137 ## We can't do this as non-root. We always have rights to
138 ## create the directory.
139 ##
140     if [ "$USERID" != 0 ] ; then
141         echo "skipping test_read_only_dir as non-root"
142         true
143         return
144     fi
145
146 ##
147 ## We can't do this with an encrypted connection. No credentials
148 ## to set up the channel.
149 ##
150     if [ "$ADDARGS" = "-e" ] ; then
151         echo "skipping test_read_only_dir with encrypted connection"
152         true
153         return
154     fi
155
156     cat > $tmpfile <<EOF
157 mkdir a_test_dir
158 quit
159 EOF
160
161     cmd='CLI_FORCE_INTERACTIVE=yes $SMBCLIENT $CONFIGURATION "$@" -U% //$SERVER/ro-tmp -I $SERVER_IP $ADDARGS < $tmpfile 2>&1'
162     eval echo "$cmd"
163     out=`eval $cmd`
164     ret=$?
165     rm -f $tmpfile
166
167     if [ $ret != 0 ] ; then
168         echo "$out"
169         echo "failed writing into read-only directory with error $ret"
170         false
171         return
172     fi
173
174     echo "$out" | grep "$prompt" >/dev/null 2>&1
175
176     ret=$?
177     if [ $ret = 0 ] ; then
178         # got the correct prompt .. succeed
179         true
180     else
181         echo "$out"
182         echo "failed writing into read-only directory - grep failed with $ret"
183         false
184     fi
185 }
186
187 # Test reading an owner-only file (logon as guest) fails.
188 test_owner_only_file()
189 {
190     prompt="NT_STATUS_ACCESS_DENIED opening remote file"
191     tmpfile=/tmp/smbclient.in.$$
192
193 ##
194 ## We can't do this as non-root. We always have rights to
195 ## read the file.
196 ##
197     if [ "$USERID" != 0 ] ; then
198         echo "skipping test_owner_only_file as non-root"
199         true
200         return
201     fi
202
203 ##
204 ## We can't do this with an encrypted connection. No credentials
205 ## to set up the channel.
206 ##
207     if [ "$ADDARGS" = "-e" ] ; then
208         echo "skipping test_owner_only_file with encrypted connection"
209         true
210         return
211     fi
212
213     cat > $tmpfile <<EOF
214 get unreadable_file
215 quit
216 EOF
217
218     cmd='CLI_FORCE_INTERACTIVE=yes $SMBCLIENT $CONFIGURATION "$@" -U% //$SERVER/ro-tmp -I $SERVER_IP $ADDARGS < $tmpfile 2>&1'
219     eval echo "$cmd"
220     out=`eval $cmd`
221     ret=$?
222     rm -f $tmpfile
223
224     if [ $ret != 0 ] ; then
225         echo "$out"
226         echo "failed reading owner-only file with error $ret"
227         false
228         return
229     fi
230
231     echo "$out" | grep "$prompt" >/dev/null 2>&1
232
233     ret=$?
234     if [ $ret = 0 ] ; then
235         # got the correct prompt .. succeed
236         true
237     else
238         echo "$out"
239         echo "failed reading owner-only file - grep failed with $ret"
240         false
241     fi
242 }
243
244 testit "smbclient -L $SERVER_IP" $SMBCLIENT $CONFIGURATION -L $SERVER_IP -N -p 139 || failed=`expr $failed + 1`
245 testit "smbclient -L $SERVER -I $SERVER_IP" $SMBCLIENT $CONFIGURATION -L $SERVER -I $SERVER_IP -N -p 139 || failed=`expr $failed + 1`
246
247 testit "noninteractive smbclient does not prompt" \
248     test_noninteractive_no_prompt || \
249     failed=`expr $failed + 1`
250
251 testit "noninteractive smbclient -l does not prompt" \
252    test_noninteractive_no_prompt -l /tmp || \
253     failed=`expr $failed + 1`
254
255 testit "interactive smbclient prompts on stdout" \
256    test_interactive_prompt_stdout || \
257     failed=`expr $failed + 1`
258
259 testit "interactive smbclient -l prompts on stdout" \
260    test_interactive_prompt_stdout -l /tmp || \
261     failed=`expr $failed + 1`
262
263 testit "creating a bad symlink and deleting it" \
264    test_bad_symlink || \
265    failed=`expr $failed + 1`
266
267 testit "writing into a read-only directory fails" \
268    test_read_only_dir || \
269    failed=`expr $failed + 1`
270
271 testit "Reading a owner-only file fails" \
272    test_owner_only_file || \
273    failed=`expr $failed + 1`
274
275 testok $0 $failed