clock-time: change to LGPLv2+.
[gnulib.git] / tests / test-copy-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] tmpaclout[0-2]
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, NonStop Kernel.
95         if (getacl -m tmpfile0 >/dev/null) 2>/dev/null; then
96           # Tru64.
97           acl_flavor=osf1
98         else
99           # NonStop Kernel.
100           acl_flavor=nsk
101         fi
102       else
103         if (aclget tmpfile0 >/dev/null) 2>/dev/null; then
104           # AIX.
105           acl_flavor=aix
106         else
107           if (fsaclctl -v >/dev/null) 2>/dev/null; then
108             # MacOS X.
109             acl_flavor=macosx
110           else
111             if test -f /sbin/chacl; then
112               # IRIX.
113               acl_flavor=irix
114             fi
115           fi
116         fi
117       fi
118     fi
119   fi
120
121   # Define a function to test for the same ACLs, from the point of view of
122   # the programs.
123   # func_test_same_acls file1 file2
124   case $acl_flavor in
125     linux | cygwin | freebsd | solaris)
126       func_test_same_acls ()
127       {
128         getfacl "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
129         getfacl "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
130         cmp tmpaclout1 tmpaclout2 > /dev/null
131       }
132       ;;
133     hpux)
134       func_test_same_acls ()
135       {
136         lsacl "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
137         lsacl "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
138         cmp tmpaclout1 tmpaclout2 > /dev/null
139       }
140       ;;
141     osf1 | nsk)
142       func_test_same_acls ()
143       {
144         getacl "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
145         getacl "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
146         cmp tmpaclout1 tmpaclout2 > /dev/null
147       }
148       ;;
149     aix)
150       func_test_same_acls ()
151       {
152         aclget "$1" > tmpaclout1
153         aclget "$2" > tmpaclout2
154         cmp tmpaclout1 tmpaclout2 > /dev/null
155       }
156       ;;
157     macosx)
158       func_test_same_acls ()
159       {
160         /bin/ls -le "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
161         /bin/ls -le "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
162         cmp tmpaclout1 tmpaclout2 > /dev/null
163       }
164       ;;
165     irix)
166       func_test_same_acls ()
167       {
168         /bin/ls -lD "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
169         /bin/ls -lD "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
170         cmp tmpaclout1 tmpaclout2 > /dev/null
171       }
172       ;;
173     none)
174       func_test_same_acls ()
175       {
176         :
177       }
178       ;;
179   esac
180
181   # func_test_copy file1 file2
182   # copies file1 to file2 and verifies the permissions and ACLs are the same
183   # on both.
184   func_test_copy ()
185   {
186     echo "Simple contents" > "$2"
187     chmod 600 "$2"
188     "$builddir"/test-copy-acl${EXEEXT} "$1" "$2" || exit 1
189     "$builddir"/test-sameacls${EXEEXT} "$1" "$2" || exit 1
190     func_test_same_acls                "$1" "$2" || exit 1
191   }
192
193   func_test_copy tmpfile0 tmpfile1
194
195   if test $acl_flavor != none; then
196     # A POSIX compliant 'id' program.
197     if test -f /usr/xpg4/bin/id; then
198       ID=/usr/xpg4/bin/id
199     else
200       ID=id
201     fi
202     # Use a user and group id different from the current one, to avoid
203     # redundant/ambiguous ACLs.
204     myuid=`$ID -u`
205     mygid=`$ID -g`
206     auid=1
207     if test "$auid" = "$myuid"; then auid=2; fi
208     agid=1
209     if test "$agid" = "$mygid"; then agid=2; fi
210
211     case $acl_flavor in
212       linux | freebsd | solaris)
213
214         # Set an ACL for a user.
215         setfacl -m user:$auid:1 tmpfile0
216
217         func_test_copy tmpfile0 tmpfile2
218
219         # Set an ACL for a group.
220         setfacl -m group:$agid:4 tmpfile0
221
222         func_test_copy tmpfile0 tmpfile3
223
224         # Set an ACL for other.
225         case $acl_flavor in
226           freebsd) setfacl -m other::4 tmpfile0 ;;
227           solaris) chmod o+r tmpfile0 ;;
228           *)       setfacl -m other:4 tmpfile0 ;;
229         esac
230
231         func_test_copy tmpfile0 tmpfile4
232
233         # Remove the ACL for the user.
234         case $acl_flavor in
235           linux)   setfacl -x user:$auid tmpfile0 ;;
236           freebsd) setfacl -x user:$auid:1 tmpfile0 ;;
237           *)       setfacl -d user:$auid:1 tmpfile0 ;;
238         esac
239
240         func_test_copy tmpfile0 tmpfile5
241
242         # Remove the ACL for other.
243         case $acl_flavor in
244           linux | solaris) ;; # impossible
245           freebsd) setfacl -x other::4 tmpfile0 ;;
246           *)       setfacl -d other:4 tmpfile0 ;;
247         esac
248
249         func_test_copy tmpfile0 tmpfile6
250
251         # Remove the ACL for the group.
252         case $acl_flavor in
253           linux)   setfacl -x group:$agid tmpfile0 ;;
254           freebsd) setfacl -x group:$agid:4 tmpfile0 ;;
255           *)       setfacl -d group:$agid:4 tmpfile0 ;;
256         esac
257
258         func_test_copy tmpfile0 tmpfile7
259
260         # Delete all optional ACLs.
261         case $acl_flavor in
262           linux | freebsd)
263             setfacl -m user:$auid:1 tmpfile0
264             setfacl -b tmpfile0
265             ;;
266           *)
267             setfacl -s user::6,group::0,other:0 tmpfile0 ;;
268         esac
269
270         func_test_copy tmpfile0 tmpfile8
271
272         # Copy ACLs from a file that has no ACLs.
273         echo > tmpfile9
274         chmod a+x tmpfile9
275         case $acl_flavor in
276           linux)   getfacl tmpfile9 | setfacl --set-file=- tmpfile0 ;;
277           freebsd) ;;
278           *)       getfacl tmpfile9 | setfacl -f - tmpfile0 ;;
279         esac
280         rm -f tmpfile9
281
282         func_test_copy tmpfile0 tmpfile9
283
284         ;;
285
286       cygwin)
287
288         # Set an ACL for a group.
289         setfacl -m group:0:1 tmpfile0
290
291         func_test_copy tmpfile0 tmpfile2
292
293         # Set an ACL for other.
294         setfacl -m other:4 tmpfile0
295
296         func_test_copy tmpfile0 tmpfile4
297
298         # Remove the ACL for the group.
299         setfacl -d group:0 tmpfile0
300
301         func_test_copy tmpfile0 tmpfile5
302
303         # Remove the ACL for other.
304         setfacl -d other:4 tmpfile0
305
306         func_test_copy tmpfile0 tmpfile6
307
308         # Delete all optional ACLs.
309         setfacl -s user::6,group::0,other:0 tmpfile0
310
311         func_test_copy tmpfile0 tmpfile8
312
313         # Copy ACLs from a file that has no ACLs.
314         echo > tmpfile9
315         chmod a+x tmpfile9
316         getfacl tmpfile9 | setfacl -f - tmpfile0
317         rm -f tmpfile9
318
319         func_test_copy tmpfile0 tmpfile9
320
321         ;;
322
323       hpux)
324
325         # Set an ACL for a user.
326         orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
327         chacl -r "${orig}($auid.%,--x)" tmpfile0
328
329         func_test_copy tmpfile0 tmpfile2
330
331         # Set an ACL for a group.
332         orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
333         chacl -r "${orig}(%.$agid,r--)" tmpfile0
334
335         func_test_copy tmpfile0 tmpfile3
336
337         # Set an ACL for other.
338         orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
339         chacl -r "${orig}(%.%,r--)" tmpfile0
340
341         func_test_copy tmpfile0 tmpfile4
342
343         # Remove the ACL for the user.
344         chacl -d "($auid.%,--x)" tmpfile0
345
346         func_test_copy tmpfile0 tmpfile5
347
348         # Remove the ACL for the group.
349         chacl -d "(%.$agid,r--)" tmpfile0
350
351         func_test_copy tmpfile0 tmpfile6
352
353         # Delete all optional ACLs.
354         chacl -z tmpfile0
355
356         func_test_copy tmpfile0 tmpfile8
357
358         # Copy ACLs from a file that has no ACLs.
359         echo > tmpfile9
360         chmod a+x tmpfile9
361         orig=`lsacl tmpfile9 | sed -e 's/ tmpfile9$//'`
362         rm -f tmpfile9
363         chacl -r "${orig}" tmpfile0
364
365         func_test_copy tmpfile0 tmpfile9
366
367         ;;
368
369       osf1)
370
371         # Set an ACL for a user.
372         setacl -u user:$auid:1 tmpfile0
373
374         func_test_copy tmpfile0 tmpfile2
375
376         # Set an ACL for a group.
377         setacl -u group:$agid:4 tmpfile0
378
379         func_test_copy tmpfile0 tmpfile3
380
381         # Set an ACL for other.
382         setacl -u other::4 tmpfile0
383
384         func_test_copy tmpfile0 tmpfile4
385
386         # Remove the ACL for the user.
387         setacl -x user:$auid:1 tmpfile0
388
389         func_test_copy tmpfile0 tmpfile5
390
391         if false; then # would give an error "can't set ACL: Invalid argument"
392           # Remove the ACL for other.
393           setacl -x other::4 tmpfile0
394
395           func_test_copy tmpfile0 tmpfile6
396         fi
397
398         # Remove the ACL for the group.
399         setacl -x group:$agid:4 tmpfile0
400
401         func_test_copy tmpfile0 tmpfile7
402
403         # Delete all optional ACLs.
404         setacl -u user:$auid:1 tmpfile0
405         setacl -b tmpfile0
406
407         func_test_copy tmpfile0 tmpfile8
408
409         # Copy ACLs from a file that has no ACLs.
410         echo > tmpfile9
411         chmod a+x tmpfile9
412         getacl tmpfile9 > tmpaclout0
413         setacl -b -U tmpaclout0 tmpfile0
414         rm -f tmpfile9
415
416         func_test_copy tmpfile0 tmpfile9
417
418         ;;
419
420       nsk)
421
422         # Set an ACL for a user.
423         setacl -m user:$auid:1 tmpfile0
424
425         func_test_copy tmpfile0 tmpfile2
426
427         # Set an ACL for a group.
428         setacl -m group:$agid:4 tmpfile0
429
430         func_test_copy tmpfile0 tmpfile3
431
432         # Set an ACL for other.
433         setacl -m other:4 tmpfile0
434
435         func_test_copy tmpfile0 tmpfile4
436
437         # Remove the ACL for the user.
438         setacl -d user:$auid tmpfile0
439
440         func_test_copy tmpfile0 tmpfile5
441
442         # Remove the ACL for the group.
443         setacl -d group:$agid tmpfile0
444
445         func_test_copy tmpfile0 tmpfile6
446
447         # Delete all optional ACLs.
448         setacl -m user:$auid:1 tmpfile0
449         setacl -s user::6,group::0,class:7,other:0 tmpfile0
450
451         func_test_copy tmpfile0 tmpfile8
452
453         # Copy ACLs from a file that has no ACLs.
454         echo > tmpfile9
455         chmod a+x tmpfile9
456         getacl tmpfile9 > tmpaclout0
457         setacl -f tmpaclout0 tmpfile0
458         rm -f tmpfile9
459
460         func_test_copy tmpfile0 tmpfile9
461
462         ;;
463
464       aix)
465
466         # Set an ACL for a user.
467         { aclget tmpfile0 | sed -e 's/disabled$/enabled/'; echo "        permit --x u:$auid"; } | aclput tmpfile0
468
469         func_test_copy tmpfile0 tmpfile2
470
471         # Set an ACL for a group.
472         { aclget tmpfile0 | sed -e 's/disabled$/enabled/'; echo "        permit r-- g:$agid"; } | aclput tmpfile0
473
474         func_test_copy tmpfile0 tmpfile3
475
476         # Set an ACL for other.
477         chmod o+r tmpfile0
478
479         func_test_copy tmpfile0 tmpfile4
480
481         # Remove the ACL for the user.
482         aclget tmpfile0 | grep -v ' u:[^ ]*$' | aclput tmpfile0
483
484         func_test_copy tmpfile0 tmpfile5
485
486         # Remove the ACL for the group.
487         aclget tmpfile0 | grep -v ' g:[^ ]*$' | aclput tmpfile0
488
489         func_test_copy tmpfile0 tmpfile7
490
491         # Delete all optional ACLs.
492         aclget tmpfile0 | sed -e 's/enabled$/disabled/' | sed -e '/disabled$/q' | aclput tmpfile0
493
494         func_test_copy tmpfile0 tmpfile8
495
496         # Copy ACLs from a file that has no ACLs.
497         echo > tmpfile9
498         chmod a+x tmpfile9
499         aclget tmpfile9 | aclput tmpfile0
500         rm -f tmpfile9
501
502         func_test_copy tmpfile0 tmpfile9
503
504         ;;
505
506       macosx)
507
508         # Set an ACL for a user.
509         /bin/chmod +a "user:daemon allow execute" tmpfile0
510
511         func_test_copy tmpfile0 tmpfile2
512
513         # Set an ACL for a group.
514         /bin/chmod +a "group:daemon allow read" tmpfile0
515
516         func_test_copy tmpfile0 tmpfile3
517
518         # Set an ACL for other.
519         chmod o+r tmpfile0
520
521         func_test_copy tmpfile0 tmpfile4
522
523         # Remove the ACL for the user.
524         /bin/chmod -a "user:daemon allow execute" tmpfile0
525
526         func_test_copy tmpfile0 tmpfile5
527
528         # Remove the ACL for the group.
529         /bin/chmod -a "group:daemon allow read" tmpfile0
530
531         func_test_copy tmpfile0 tmpfile7
532
533         # Delete all optional ACLs.
534         /bin/chmod -N tmpfile0
535
536         func_test_copy tmpfile0 tmpfile8
537
538         # Copy ACLs from a file that has no ACLs.
539         echo > tmpfile9
540         chmod a+x tmpfile9
541         { /bin/ls -le tmpfile9 | sed -n -e 's/^ [0-9][0-9]*: //p'; echo; } | /bin/chmod -E tmpfile0
542         rm -f tmpfile9
543
544         func_test_copy tmpfile0 tmpfile9
545
546         ;;
547
548       irix)
549
550         # Set an ACL for a user.
551         /sbin/chacl user::rw-,group::---,other::---,user:$auid:--x tmpfile0
552
553         func_test_copy tmpfile0 tmpfile2
554
555         # Set an ACL for a group.
556         /sbin/chacl user::rw-,group::---,other::---,user:$auid:--x,group:$agid:r-- tmpfile0
557
558         func_test_copy tmpfile0 tmpfile3
559
560         # Set an ACL for other.
561         /sbin/chacl user::rw-,group::---,user:$auid:--x,group:$agid:r--,other::r-- tmpfile0
562
563         func_test_copy tmpfile0 tmpfile4
564
565         # Remove the ACL for the user.
566         /sbin/chacl user::rw-,group::---,group:$agid:r--,other::r-- tmpfile0
567
568         func_test_copy tmpfile0 tmpfile5
569
570         # Remove the ACL for the group.
571         /sbin/chacl user::rw-,group::---,other::r-- tmpfile0
572
573         func_test_copy tmpfile0 tmpfile7
574
575         ;;
576
577     esac
578   fi
579
580   rm -f tmpfile[0-9] tmpaclout[0-2]
581 ) || exit 1
582
583 rm -rf "$tmp"
584 exit 0