pty: Activate the signature wrapper of forkpty.
[gnulib.git] / tests / test-set-mode-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]
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   if test $acl_flavor != none; then
128     # A POSIX compliant 'id' program.
129     if test -f /usr/xpg4/bin/id; then
130       ID=/usr/xpg4/bin/id
131     else
132       ID=id
133     fi
134     # Use a user and group id different from the current one, to avoid
135     # redundant/ambiguous ACLs.
136     myuid=`$ID -u`
137     mygid=`$ID -g`
138     auid=1
139     if test "$auid" = "$myuid"; then auid=2; fi
140     agid=1
141     if test "$agid" = "$mygid"; then agid=2; fi
142   fi
143
144   for mode in 700 400 200 100 644 650 605 011 4700 2070; do
145     rm -f tmpfile0 tmpfile1 tmpfile2
146
147     # Prepare a file with no ACL.
148     echo "Anything" > tmpfile0
149     # If a mode is not supported (e.g. 2070 on FreeBSD), we skip testing it.
150     if chmod $mode tmpfile0 2>/dev/null; then
151       modestring0=`ls -l tmpfile0 | dd ibs=1 count=10 2>/dev/null`
152
153       # Prepare a file with no ACL.
154       echo "Some contents" > tmpfile1
155       chmod 600 tmpfile1
156
157       # Try to set the ACL to only the given mode.
158       "$builddir"/test-set-mode-acl${EXEEXT} tmpfile1 $mode
159       # Verify that tmpfile1 has no ACL and has the desired mode.
160       modestring=`ls -l tmpfile1 | dd ibs=1 count=10 2>/dev/null`
161       if test "x$modestring" != "x$modestring0"; then
162         echo "mode = $mode: tmpfile1 has wrong mode: $modestring" 1>&2
163         exit 1
164       fi
165       if test `"$builddir"/test-file-has-acl${EXEEXT} tmpfile1` != no; then
166         echo "mode = $mode: tmpfile1 got an ACL" 1>&2
167         exit 1
168       fi
169
170       if test $acl_flavor != none; then
171
172         # Prepare a file with an ACL.
173         echo "Special contents" > tmpfile2
174         chmod 600 tmpfile2
175         # Set an ACL for a user (or group).
176         case $acl_flavor in
177           linux | freebsd | solaris)
178             setfacl -m user:$auid:1 tmpfile0
179             ;;
180           cygwin)
181             setfacl -m group:0:1 tmpfile0
182             ;;
183           hpux)
184             orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
185             chacl -r "${orig}($auid.%,--x)" tmpfile0
186             ;;
187           hpuxjfs)
188             orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
189             chacl -r "${orig}($auid.%,--x)" tmpfile0 \
190               || setacl -m user:$auid:1 tmpfile0
191             ;;
192           osf1)
193             setacl -u user:$auid:1 tmpfile0
194             ;;
195           nsk)
196             setacl -m user:$auid:1 tmpfile0
197             ;;
198           aix)
199             { aclget tmpfile0 | sed -e 's/disabled$/enabled/'; echo "        permit --x u:$auid"; } | aclput tmpfile0
200             ;;
201           macosx)
202             /bin/chmod +a "user:daemon allow execute" tmpfile0
203             ;;
204           irix)
205             /sbin/chacl user::rw-,group::---,other::---,user:$auid:--x tmpfile0
206             ;;
207         esac
208
209         # Try to set the ACL to only the given mode.
210         "$builddir"/test-set-mode-acl${EXEEXT} tmpfile2 $mode
211         # Verify that tmpfile2 has no ACL and has the desired mode.
212         modestring=`ls -l tmpfile2 | dd ibs=1 count=10 2>/dev/null`
213         if test "x$modestring" != "x$modestring0"; then
214           echo "mode = $mode: tmpfile2 has wrong mode: $modestring" 1>&2
215           exit 1
216         fi
217         if test `"$builddir"/test-file-has-acl${EXEEXT} tmpfile2` != no; then
218           echo "mode = $mode: tmpfile2 still has an ACL" 1>&2
219           exit 1
220         fi
221       fi
222     fi
223   done
224
225   rm -f tmpfile[0-9]
226 ) || exit 1
227
228 rm -rf "$tmp"
229 exit 0