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