pty: Activate the signature wrapper of forkpty.
[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, hpuxjfs, osf1, aix, macosx, irix, none.
64   # TODO: Support also native Windows 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       if (getacl tmpfile0 >/dev/null) 2>/dev/null; then
92         # HP-UX 11.11 or newer.
93         acl_flavor=hpuxjfs
94       else
95         # HP-UX 11.00.
96         acl_flavor=hpux
97       fi
98     else
99       if (getacl tmpfile0 >/dev/null) 2>/dev/null; then
100         # Tru64, NonStop Kernel.
101         if (getacl -m tmpfile0 >/dev/null) 2>/dev/null; then
102           # Tru64.
103           acl_flavor=osf1
104         else
105           # NonStop Kernel.
106           acl_flavor=nsk
107         fi
108       else
109         if (aclget tmpfile0 >/dev/null) 2>/dev/null; then
110           # AIX.
111           acl_flavor=aix
112         else
113           if (fsaclctl -v >/dev/null) 2>/dev/null; then
114             # Mac OS X.
115             acl_flavor=macosx
116           else
117             if test -f /sbin/chacl; then
118               # IRIX.
119               acl_flavor=irix
120             fi
121           fi
122         fi
123       fi
124     fi
125   fi
126
127   # func_test_file_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.
130   func_test_file_has_acl ()
131   {
132     res=`"$builddir"/test-file-has-acl${EXEEXT} "$1"`
133     test "$res" = "$2" || {
134       echo "file_has_acl(\"$1\") returned $res, expected $2" 1>&2
135       exit 1
136     }
137   }
138
139   # func_test_has_acl file expected
140   # tests the result of the file_has_acl function on file, and checks that it
141   # matches the expected value, also taking into account the system's 'ls'
142   # program.
143   case $acl_flavor in
144     freebsd | solaris | hpux | macosx)
145       case $acl_flavor in
146         freebsd | solaris | hpux) acl_ls_option="-ld" ;;
147         macosx)                   acl_ls_option="-lde" ;;
148       esac
149       func_test_has_acl ()
150       {
151         func_test_file_has_acl "$1" "$2"
152         case `/bin/ls $acl_ls_option "$1" | sed 1q` in
153           ??????????+*)
154             test "$2" = yes || {
155               echo "/bin/ls $acl_ls_option $1 shows an ACL, but expected $2" 1>&2
156               exit 1
157             }
158             ;;
159           ??????????" "*)
160             test "$2" = no || {
161               echo "/bin/ls $acl_ls_option $1 shows no ACL, but expected $2" 1>&2
162               exit 1
163             }
164             ;;
165         esac
166       }
167       ;;
168     irix)
169       func_test_has_acl ()
170       {
171         func_test_file_has_acl "$1" "$2"
172         case `/bin/ls -ldD "$1" | sed 1q` in
173           *" []")
174             test "$2" = no || {
175               echo "/bin/ls -ldD $1 shows no ACL, but expected $2" 1>&2
176               exit 1
177             }
178             ;;
179           *)
180             test "$2" = yes || {
181               echo "/bin/ls -ldD $1 shows an ACL, but expected $2" 1>&2
182               exit 1
183             }
184             ;;
185         esac
186       }
187       ;;
188     *)
189       func_test_has_acl ()
190       {
191         func_test_file_has_acl "$1" "$2"
192       }
193       ;;
194   esac
195
196   func_test_has_acl tmpfile0 no
197
198   mkdir tmpdir0
199   func_test_has_acl tmpdir0 no
200
201   if test $acl_flavor != none; then
202     # A POSIX compliant 'id' program.
203     if test -f /usr/xpg4/bin/id; then
204       ID=/usr/xpg4/bin/id
205     else
206       ID=id
207     fi
208     # Use a user and group id different from the current one, to avoid
209     # redundant/ambiguous ACLs.
210     myuid=`$ID -u`
211     mygid=`$ID -g`
212     auid=1
213     if test "$auid" = "$myuid"; then auid=2; fi
214     agid=1
215     if test "$agid" = "$mygid"; then agid=2; fi
216
217     case $acl_flavor in
218       linux | freebsd | solaris)
219
220         # Set an ACL for a user.
221         if setfacl -m user:$auid:1 tmpfile0; then
222
223           func_test_has_acl tmpfile0 yes
224
225           # Remove the ACL for the user.
226           case $acl_flavor in
227             linux)   setfacl -x user:$auid tmpfile0 ;;
228             freebsd) setfacl -x user:$auid:1 tmpfile0 ;;
229             *)       setfacl -d user:$auid:1 tmpfile0 ;;
230           esac
231
232           # On Linux and FreeBSD, the ACL for the mask is implicitly added.
233           # On Solaris, it is always there.
234           case $acl_flavor in
235             linux | freebsd) func_test_has_acl tmpfile0 yes ;;
236             *)               func_test_has_acl tmpfile0 no ;;
237           esac
238
239           # Remove the ACL for the mask, if it was implicitly added.
240           case $acl_flavor in
241             linux | freebsd) setfacl -x mask: tmpfile0 ;;
242             *)               setfacl -d mask: tmpfile0 ;;
243           esac
244
245           func_test_has_acl tmpfile0 no
246
247         fi
248         ;;
249
250       cygwin)
251
252         # Set an ACL for a group.
253         if setfacl -m group:0:1 tmpfile0; then
254
255           func_test_has_acl tmpfile0 yes
256
257           # Remove the ACL for the group.
258           setfacl -d group:0 tmpfile0
259
260           func_test_has_acl tmpfile0 no
261
262         fi
263         ;;
264
265       hpux | hpuxjfs)
266
267         # Set an ACL for a user.
268         orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
269         if chacl -r "${orig}($auid.%,--x)" tmpfile0; then
270
271           func_test_has_acl tmpfile0 yes
272
273           # Remove the ACL for the user.
274           chacl -d "($auid.%,--x)" tmpfile0
275
276           func_test_has_acl tmpfile0 no
277
278         else
279           if test $acl_flavor = hpuxjfs; then
280
281             # Set an ACL for a user.
282             setacl -m user:$auid:1 tmpfile0
283
284             func_test_has_acl tmpfile0 yes
285
286             # Remove the ACL for the user.
287             setacl -d user:$auid tmpfile0
288
289             func_test_has_acl tmpfile0 no
290
291           fi
292         fi
293         ;;
294
295       osf1)
296
297         # Set an ACL for a user.
298         setacl -u user:$auid:1 tmpfile0 2> tmp.err
299         cat tmp.err 1>&2
300         if grep 'Error:' tmp.err > /dev/null \
301            || grep 'Operation not supported' tmp.err > /dev/null; then
302           :
303         else
304
305           func_test_has_acl tmpfile0 yes
306
307           # Remove the ACL for the user.
308           setacl -x user:$auid:1 tmpfile0
309
310           func_test_has_acl tmpfile0 no
311
312         fi
313         ;;
314
315       nsk)
316
317         # Set an ACL for a user.
318         setacl -m user:$auid:1 tmpfile0
319
320         func_test_has_acl tmpfile0 yes
321
322         # Remove the ACL for the user.
323         setacl -d user:$auid tmpfile0
324
325         func_test_has_acl tmpfile0 no
326
327         ;;
328
329       aix)
330
331         # Set an ACL for a user.
332         { aclget tmpfile0 | sed -e 's/disabled$/enabled/'; echo "        permit --x u:$auid"; } | aclput tmpfile0
333         if aclget tmpfile0 | grep enabled > /dev/null; then
334
335           func_test_has_acl tmpfile0 yes
336
337           # Remove the ACL for the user.
338           aclget tmpfile0 | grep -v ' u:[^ ]*$' | aclput tmpfile0
339
340           func_test_has_acl tmpfile0 no
341
342         fi
343         ;;
344
345       macosx)
346
347         # Set an ACL for a user.
348         /bin/chmod +a "user:daemon allow execute" tmpfile0
349
350         func_test_has_acl tmpfile0 yes
351
352         # Remove the ACL for the user.
353         /bin/chmod -a "user:daemon allow execute" tmpfile0
354
355         func_test_has_acl tmpfile0 no
356
357         ;;
358
359       irix)
360
361         # Set an ACL for a user.
362         /sbin/chacl user::rw-,group::---,other::---,user:$auid:--x tmpfile0 2> tmp.err
363         cat tmp.err 1>&2
364         if test -s tmp.err; then :; else
365
366           func_test_has_acl tmpfile0 yes
367
368           # Remove the ACL for the user.
369           /sbin/chacl user::rw-,group::---,other::--- tmpfile0
370
371           func_test_has_acl tmpfile0 no
372
373         fi
374         ;;
375
376     esac
377   fi
378
379   rm -f tmpfile[0-9] tmp.err
380   rm -rf tmpdir0
381 ) || exit 1
382
383 rm -rf "$tmp"
384 exit 0