striconveh: Simplify last commit.
[gnulib.git] / tests / test-file-has-acl.sh
1 #!/bin/sh
2
3 # Show all commands when run with environment variable VERBOSE=yes.
4 test -z "$VERBOSE" || set -x
5
6 test "$USE_ACL" = 0 &&
7   {
8     echo "Skipping test: insufficient ACL support"
9     exit 77
10   }
11
12 # func_tmpdir
13 # creates a temporary directory.
14 # Sets variable
15 # - tmp             pathname of freshly created temporary directory
16 func_tmpdir ()
17 {
18   # Use the environment variable TMPDIR, falling back to /tmp. This allows
19   # users to specify a different temporary directory, for example, if their
20   # /tmp is filled up or too small.
21   : ${TMPDIR=/tmp}
22   {
23     # Use the mktemp program if available. If not available, hide the error
24     # message.
25     tmp=`(umask 077 && mktemp -d "$TMPDIR/glXXXXXX") 2>/dev/null` &&
26     test -n "$tmp" && test -d "$tmp"
27   } ||
28   {
29     # Use a simple mkdir command. It is guaranteed to fail if the directory
30     # already exists.  $RANDOM is bash specific and expands to empty in shells
31     # other than bash, ksh and zsh.  Its use does not increase security;
32     # rather, it minimizes the probability of failure in a very cluttered /tmp
33     # directory.
34     tmp=$TMPDIR/gl$$-$RANDOM
35     (umask 077 && mkdir "$tmp")
36   } ||
37   {
38     echo "$0: cannot create a temporary directory in $TMPDIR" >&2
39     exit 1
40   }
41 }
42
43 func_tmpdir
44 builddir=`pwd`
45 cd "$builddir" ||
46   {
47     echo "$0: cannot determine build directory (unreadable parent dir?)" >&2
48     exit 1
49   }
50 # Switch to a temporary directory, to increase the likelihood that ACLs are
51 # supported on the current file system. (/tmp is usually locally mounted,
52 # whereas the build dir is sometimes NFS-mounted.)
53 ( cd "$tmp"
54
55   # Prepare tmpfile0.
56   rm -f tmpfile[0-9] tmp.err
57   echo "Simple contents" > tmpfile0
58   chmod 600 tmpfile0
59
60   # Classification of the platform according to the programs available for
61   # manipulating ACLs.
62   # Possible values are:
63   #   linux, cygwin, freebsd, solaris, hpux, osf1, aix, macosx, irix, none.
64   # TODO: Support also native Win32 platforms (mingw).
65   acl_flavor=none
66   if (getfacl tmpfile0 >/dev/null) 2>/dev/null; then
67     # Platforms with the getfacl and setfacl programs.
68     # Linux, FreeBSD, Solaris, Cygwin.
69     if (setfacl --help >/dev/null) 2>/dev/null; then
70       # Linux, Cygwin.
71       if (LC_ALL=C setfacl --help | grep ' --set-file' >/dev/null) 2>/dev/null; then
72         # Linux.
73         acl_flavor=linux
74       else
75         acl_flavor=cygwin
76       fi
77     else
78       # FreeBSD, Solaris.
79       if (LC_ALL=C setfacl 2>&1 | grep '\-x entries' >/dev/null) 2>/dev/null; then
80         # FreeBSD.
81         acl_flavor=freebsd
82       else
83         # Solaris.
84         acl_flavor=solaris
85       fi
86     fi
87   else
88     if (lsacl / >/dev/null) 2>/dev/null; then
89       # Platforms with the lsacl and chacl programs.
90       # HP-UX, sometimes also IRIX.
91       acl_flavor=hpux
92     else
93       if (getacl tmpfile0 >/dev/null) 2>/dev/null; then
94         # Tru64.
95         acl_flavor=osf1
96       else
97         if (aclget tmpfile0 >/dev/null) 2>/dev/null; then
98           # AIX.
99           acl_flavor=aix
100         else
101           if (fsaclctl -v >/dev/null) 2>/dev/null; then
102             # MacOS X.
103             acl_flavor=macosx
104           else
105             if test -f /sbin/chacl; then
106               # IRIX.
107               acl_flavor=irix
108             fi
109           fi
110         fi
111       fi
112     fi
113   fi
114
115   # func_test_file_has_acl file expected
116   # tests the result of the file_has_acl function on file, and checks that it
117   # matches the expected value.
118   func_test_file_has_acl ()
119   {
120     res=`"$builddir"/test-file-has-acl${EXEEXT} "$1"`
121     test "$res" = "$2" || {
122       echo "file_has_acl(\"$1\") returned $res, expected $2" 1>&2
123       exit 1
124     }
125   }
126
127   # func_test_has_acl file expected
128   # tests the result of the file_has_acl function on file, and checks that it
129   # matches the expected value, also taking into account the system's 'ls'
130   # program.
131   case $acl_flavor in
132     freebsd | solaris | hpux | macosx)
133       case $acl_flavor in
134         freebsd | solaris | hpux) acl_ls_option="-ld" ;;
135         macosx)                   acl_ls_option="-lde" ;;
136       esac
137       func_test_has_acl ()
138       {
139         func_test_file_has_acl "$1" "$2"
140         case `/bin/ls $acl_ls_option "$1" | sed 1q` in
141           ??????????+*)
142             test "$2" = yes || {
143               echo "/bin/ls $acl_ls_option $1 shows an ACL, but expected $2" 1>&2
144               exit 1
145             }
146             ;;
147           ??????????" "*)
148             test "$2" = no || {
149               echo "/bin/ls $acl_ls_option $1 shows no ACL, but expected $2" 1>&2
150               exit 1
151             }
152             ;;
153         esac
154       }
155       ;;
156     irix)
157       func_test_has_acl ()
158       {
159         func_test_file_has_acl "$1" "$2"
160         case `/bin/ls -ldD "$1" | sed 1q` in
161           *" []")
162             test "$2" = no || {
163               echo "/bin/ls -ldD $1 shows no ACL, but expected $2" 1>&2
164               exit 1
165             }
166             ;;
167           *)
168             test "$2" = yes || {
169               echo "/bin/ls -ldD $1 shows an ACL, but expected $2" 1>&2
170               exit 1
171             }
172             ;;
173         esac
174       }
175       ;;
176     *)
177       func_test_has_acl ()
178       {
179         func_test_file_has_acl "$1" "$2"
180       }
181       ;;
182   esac
183
184   func_test_has_acl tmpfile0 no
185
186   mkdir tmpdir0
187   func_test_has_acl tmpdir0 no
188
189   if test $acl_flavor != none; then
190     # A POSIX compliant 'id' program.
191     if test -f /usr/xpg4/bin/id; then
192       ID=/usr/xpg4/bin/id
193     else
194       ID=id
195     fi
196     # Use a user and group id different from the current one, to avoid
197     # redundant/ambiguous ACLs.
198     myuid=`$ID -u`
199     mygid=`$ID -g`
200     auid=1
201     if test "$auid" = "$myuid"; then auid=2; fi
202     agid=1
203     if test "$agid" = "$mygid"; then agid=2; fi
204
205     case $acl_flavor in
206       linux | freebsd | solaris)
207
208         # Set an ACL for a user.
209         if setfacl -m user:$auid:1 tmpfile0; then
210
211           func_test_has_acl tmpfile0 yes
212
213           # Remove the ACL for the user.
214           case $acl_flavor in
215             linux)   setfacl -x user:$auid tmpfile0 ;;
216             freebsd) setfacl -x user:$auid:1 tmpfile0 ;;
217             *)       setfacl -d user:$auid:1 tmpfile0 ;;
218           esac
219
220           # On Linux and FreeBSD, the ACL for the mask is implicitly added.
221           # On Solaris, it is always there.
222           case $acl_flavor in
223             linux | freebsd) func_test_has_acl tmpfile0 yes ;;
224             *)               func_test_has_acl tmpfile0 no ;;
225           esac
226
227           # Remove the ACL for the mask, if it was implicitly added.
228           case $acl_flavor in
229             linux | freebsd) setfacl -x mask: tmpfile0 ;;
230             *)               setfacl -d mask: tmpfile0 ;;
231           esac
232
233           func_test_has_acl tmpfile0 no
234
235         fi
236         ;;
237
238       cygwin)
239
240         # Set an ACL for a group.
241         if setfacl -m group:0:1 tmpfile0; then
242
243           func_test_has_acl tmpfile0 yes
244
245           # Remove the ACL for the group.
246           setfacl -d group:0 tmpfile0
247
248           func_test_has_acl tmpfile0 no
249
250         fi
251         ;;
252
253       hpux)
254
255         # Set an ACL for a user.
256         orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
257         if chacl -r "${orig}($auid.%,--x)" tmpfile0; then
258
259           func_test_has_acl tmpfile0 yes
260
261           # Remove the ACL for the user.
262           chacl -d "($auid.%,--x)" tmpfile0
263
264           func_test_has_acl tmpfile0 no
265
266         fi
267         ;;
268
269       osf1)
270
271         # Set an ACL for a user.
272         setacl -u user:$auid:1 tmpfile0 2> tmp.err
273         cat tmp.err 1>&2
274         if grep 'Error:' tmp.err > /dev/null \
275            || grep 'Operation not supported' tmp.err > /dev/null; then
276           :
277         else
278
279           func_test_has_acl tmpfile0 yes
280
281           # Remove the ACL for the user.
282           setacl -x user:$auid:1 tmpfile0
283
284           func_test_has_acl tmpfile0 no
285
286         fi
287         ;;
288
289       aix)
290
291         # Set an ACL for a user.
292         { aclget tmpfile0 | sed -e 's/disabled$/enabled/'; echo "        permit --x u:$auid"; } | aclput tmpfile0
293         if aclget tmpfile0 | grep enabled > /dev/null; then
294
295           func_test_has_acl tmpfile0 yes
296
297           # Remove the ACL for the user.
298           aclget tmpfile0 | grep -v ' u:[^ ]*$' | aclput tmpfile0
299
300           func_test_has_acl tmpfile0 no
301
302         fi
303         ;;
304
305       macosx)
306
307         # Set an ACL for a user.
308         /bin/chmod +a "user:daemon allow execute" tmpfile0
309
310         func_test_has_acl tmpfile0 yes
311
312         # Remove the ACL for the user.
313         /bin/chmod -a "user:daemon allow execute" tmpfile0
314
315         func_test_has_acl tmpfile0 no
316
317         ;;
318
319       irix)
320
321         # Set an ACL for a user.
322         /sbin/chacl user::rw-,group::---,other::---,user:$auid:--x tmpfile0 2> tmp.err
323         cat tmp.err 1>&2
324         if test -s tmp.err; then :; else
325
326           func_test_has_acl tmpfile0 yes
327
328           # Remove the ACL for the user.
329           /sbin/chacl user::rw-,group::---,other::--- tmpfile0
330
331           func_test_has_acl tmpfile0 no
332
333         fi
334         ;;
335
336     esac
337   fi
338
339   rm -f tmpfile[0-9] tmp.err
340   rm -rf tmpdir0
341 ) || exit 1
342
343 rm -rf "$tmp"
344 exit 0