selftest: prevent interpretation of escape sequences in test_give_owner.sh
[samba.git] / source3 / script / tests / test_give_owner.sh
1 #!/bin/sh
2 #
3 # this verifies that SEC_STD_WRITE_OWNER only effectively grants take-ownership
4 # permissions but NOT give-ownership.
5 #
6
7 if [ $# -lt 9 ]; then
8     echo "Usage: $0 SERVER SERVER_IP USERNAME PASSWORD PREFIX SMBCLIENT SMBCACLS NET SHARE"
9     exit 1
10 fi
11
12 SERVER="$1"
13 SERVER_IP="$2"
14 USERNAME="$3"
15 PASSWORD="$4"
16 PREFIX="$5"
17 SMBCLIENT="$6"
18 SMBCACLS="$7"
19 NET="$8"
20 SHARE="$9"
21
22 SMBCLIENT="$VALGRIND ${SMBCLIENT}"
23 SMBCACLS="$VALGRIND ${SMBCACLS}"
24 NET="$VALGRIND ${NET}"
25 failed=0
26
27 incdir=`dirname $0`/../../../testprogs/blackbox
28 . $incdir/subunit.sh
29
30 setup_testfile() {
31     local share=$1
32     local fname=$2
33     touch $PREFIX/$fname
34     $SMBCLIENT //$SERVER/$share -U $USERNAME%$PASSWORD -c "rm $fname"
35     $SMBCLIENT //$SERVER/$share -U $USERNAME%$PASSWORD -c "ls" | grep "$fname" && return 1
36     $SMBCLIENT //$SERVER/$share -U $USERNAME%$PASSWORD -c "lcd $PREFIX; put $fname" || return 1
37 }
38
39 remove_testfile() {
40     local share=$1
41     local fname=$2
42     $SMBCLIENT //$SERVER/$share -U $USERNAME%$PASSWORD -c "rm $fname"
43 }
44
45 set_win_owner() {
46     local share=$1
47     local fname=$2
48     local owner=$3
49     echo "$SMBCACLS //$SERVER/$share $fname -U $USERNAME%$PASSWORD -C '$owner'"
50     $SMBCACLS //$SERVER/$share $fname -U $USERNAME%$PASSWORD -C "$owner" || return 1
51 }
52
53 win_owner_is() {
54     local share=$1
55     local fname=$2
56     local expected_owner=$3
57     local actual_owner
58
59     echo "$SMBCACLS //$SERVER/$share $fname -U $USERNAME%$PASSWORD"
60     $SMBCACLS //$SERVER/$share $fname -U $USERNAME%$PASSWORD
61     actual_owner=$($SMBCACLS //$SERVER/$share $fname -U $USERNAME%$PASSWORD | sed -rn 's/^OWNER:(.*)/\1/p')
62     echo "actual_owner = $actual_owner"
63     if ! test "x$actual_owner" = "x$expected_owner" ; then
64         echo "Actual owner of $share/$fname is [$actual_owner] expected [$expected_owner]"
65         return 1
66     fi
67     return 0
68 }
69
70 add_ace() {
71     local share=$1
72     local fname=$2
73     local ace=$3
74
75     local_ace=$(printf '%s' "$ace" | sed 's|\\|/|')
76
77     # avoid duplicate
78     out=$($SMBCACLS //$SERVER/$share $fname -U $USERNAME%$PASSWORD)
79     if [ $? -ne 0 ] ; then
80         echo "get acl failed"
81         echo "$out"
82         return 1
83     fi
84     echo "Original ACL"
85     echo $out
86     echo "$out" | grep "$local_ace" && return 0
87
88     # add it
89     $SMBCACLS //$SERVER/$share $fname -U $USERNAME%$PASSWORD -a "$ace"
90     if [ $? -ne 0 ] ; then
91         echo "add acl failed"
92         return 1
93     fi
94
95     # check it's there
96     out=$($SMBCACLS //$SERVER/$share $fname -U $USERNAME%$PASSWORD)
97     if [ $? -ne 0 ] ; then
98         echo "get new acl failed"
99         echo "$out"
100         return 1
101     fi
102     echo "New ACL"
103     echo $out
104     echo "Checking if new ACL has \"$local_ace\""
105     echo "$out" | grep "$local_ace" || return 1
106     echo "ok"
107 }
108
109 chown_give_fails() {
110     local share=$1
111     local fname=$2
112     local user=$3
113     local expected_error=$4
114
115     # this must fail
116     out=$($SMBCACLS //$SERVER/$share $fname -U $USERNAME%$PASSWORD -C "$user") && return 1
117     # it failed, now check it returned the expected error code
118     echo "$out" | grep $expected_error || return 1
119 }
120
121 # Create a testfile
122 testit "create testfile" setup_testfile $SHARE afile || failed=`expr $failed + 1`
123 testit "verify owner" win_owner_is $SHARE afile "$SERVER/$USERNAME" || failed=`expr $failed + 1`
124
125 # Grant SeRestorePrivilege to the user and full rights on the file
126 testit "grant SeRestorePrivilege" $NET rpc rights grant $USERNAME SeRestorePrivilege -U $USERNAME%$PASSWORD -I $SERVER_IP || failed=`expr $failed + 1`
127 testit "grant full rights" add_ace $SHARE afile "ACL:$SERVER\\$USERNAME:ALLOWED/0x0/FULL" || failed=`expr $failed + 1`
128
129 # We have SeRestorePrivilege, so both give and take ownership must succeed
130 testit "give owner with SeRestorePrivilege" set_win_owner $SHARE afile "$SERVER\user1" || failed=`expr $failed + 1`
131 testit "verify owner" win_owner_is $SHARE afile "$SERVER/user1" || failed=`expr $failed + 1`
132 testit "take owner" set_win_owner $SHARE afile "$SERVER\\$USERNAME" || failed=`expr $failed + 1`
133 testit "verify owner" win_owner_is $SHARE afile "$SERVER/$USERNAME" || failed=`expr $failed + 1`
134
135 # Revoke SeRestorePrivilege, give ownership must fail now with NT_STATUS_INVALID_OWNER
136 testit "revoke SeRestorePrivilege" $NET rpc rights revoke $USERNAME SeRestorePrivilege -U $USERNAME%$PASSWORD -I $SERVER_IP || failed=`expr $failed + 1`
137 testit "give owner without SeRestorePrivilege" chown_give_fails $SHARE afile "$SERVER\user1" NT_STATUS_INVALID_OWNER || failed=`expr $failed + 1`
138
139 testit "delete testfile" remove_testfile $SHARE afile || failed=`expr $failed + 1`
140
141 exit $failed