New function for testing triviality of ACL with POSIX-draft like API.
[gnulib.git] / lib / file-has-acl.c
1 /* Test whether a file has a nontrivial access control list.
2
3    Copyright (C) 2002-2003, 2005-2008 Free Software Foundation, Inc.
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18    Written by Paul Eggert and Andreas Gruenbacher.  */
19
20 #include <config.h>
21
22 #include "acl.h"
23
24 #include "acl-internal.h"
25
26
27 #if USE_ACL && HAVE_ACL_GET_FILE
28
29 /* ACL is an ACL, from a file, stored as type ACL_TYPE_ACCESS.
30    Return 1 if the given ACL is non-trivial.
31    Return 0 if it is trivial, i.e. equivalent to a simple stat() mode.
32    Return -1 and set errno upon failure to determine it.  */
33 int
34 acl_access_nontrivial (acl_t acl)
35 {
36 # if MODE_INSIDE_ACL /* Linux, FreeBSD, IRIX, Tru64 */
37   /* acl is non-trivial if it has some entries other than for "user::",
38      "group::", and "other::".  Normally these three should be present
39      at least, allowing us to write
40         return (3 < acl_entries (acl));
41      but the following code is more robust.  */
42 #  if HAVE_ACL_FIRST_ENTRY /* Linux, FreeBSD */
43
44   acl_entry_t ace;
45   int at_end;
46
47   for (at_end = acl_get_entry (acl, ACL_FIRST_ENTRY, &ace);
48        !at_end;
49        at_end = acl_get_entry (acl, ACL_NEXT_ENTRY, &ace))
50     {
51       acl_tag_t tag;
52       if (acl_get_tag_type (ace, &tag) < 0)
53         return -1;
54       if (!(tag == ACL_USER_OBJ || tag == ACL_GROUP_OBJ || tag == ACL_OTHER))
55         return 1;
56     }
57   return 0;
58
59 #  else /* IRIX, Tru64 */
60 #   if HAVE_ACL_TO_SHORT_TEXT /* IRIX */
61   /* Don't use acl_get_entry: it is undocumented.  */
62
63   int count = acl->acl_cnt;
64   int i;
65
66   for (i = 0; i < count; i++)
67     {
68       acl_entry_t ace = &acl->acl_entry[i];
69       acl_tag_t tag = ace->ae_tag;
70
71       if (!(tag == ACL_USER_OBJ || tag == ACL_GROUP_OBJ
72             || tag == ACL_OTHER_OBJ))
73         return 1;
74     }
75   return 0;
76
77 #   endif
78 #   if HAVE_ACL_FREE_TEXT /* Tru64 */
79   /* Don't use acl_get_entry: it takes only one argument and does not work.  */
80
81   int count = acl->acl_num;
82   acl_entry_t ace;
83
84   for (ace = acl->acl_first; count > 0; ace = ace->next, count--)
85     {
86       acl_tag_t tag;
87       acl_perm_t perm;
88
89       tag = ace->entry->acl_type;
90       if (!(tag == ACL_USER_OBJ || tag == ACL_GROUP_OBJ || tag == ACL_OTHER))
91         return 1;
92
93       perm = ace->entry->acl_perm;
94       /* On Tru64, perm can also contain non-standard bits such as
95          PERM_INSERT, PERM_DELETE, PERM_MODIFY, PERM_LOOKUP, ... */
96       if ((perm & ~(ACL_READ | ACL_WRITE | ACL_EXECUTE)) != 0)
97         return 1;
98     }
99   return 0;
100
101 #   endif
102 #  endif
103 # else /* MacOS X */
104
105   /* acl is non-trivial if it is non-empty.  */
106   return (acl_entries (acl) > 0);
107 # endif
108 }
109
110 #endif
111
112
113 /* Return 1 if NAME has a nontrivial access control list, 0 if NAME
114    only has no or a base access control list, and -1 (setting errno)
115    on error.  SB must be set to the stat buffer of FILE.  */
116
117 int
118 file_has_acl (char const *name, struct stat const *sb)
119 {
120 #if USE_ACL
121   if (! S_ISLNK (sb->st_mode))
122     {
123 # if HAVE_ACL_GET_FILE
124
125       /* POSIX 1003.1e (draft 17 -- abandoned) specific version.  */
126       /* Linux, FreeBSD, MacOS X, IRIX, Tru64 */
127       int ret;
128
129       if (HAVE_ACL_EXTENDED_FILE)
130         ret = acl_extended_file (name);
131       else
132         {
133           acl_t acl = acl_get_file (name, ACL_TYPE_ACCESS);
134           if (acl)
135             {
136               int saved_errno;
137
138               ret = acl_access_nontrivial (acl);
139               saved_errno = errno;
140               acl_free (acl);
141               errno = saved_errno;
142               if (ret == 0 && S_ISDIR (sb->st_mode))
143                 {
144                   acl = acl_get_file (name, ACL_TYPE_DEFAULT);
145                   if (acl)
146                     {
147                       ret = (0 < acl_entries (acl));
148                       acl_free (acl);
149                     }
150                   else
151                     ret = -1;
152                 }
153             }
154           else
155             ret = -1;
156         }
157       if (ret < 0)
158         return ACL_NOT_WELL_SUPPORTED (errno) ? 0 : -1;
159       return ret;
160
161 # elif HAVE_ACL && defined GETACLCNT /* Solaris, Cygwin, not HP-UX */
162
163 #  if HAVE_ACL_TRIVIAL
164
165       /* Solaris 10, which also has NFSv4 and ZFS style ACLs.  */
166       return acl_trivial (name);
167
168 #  else /* Solaris, Cygwin, general case */
169
170       /* Solaris 2.5 through Solaris 10, Cygwin, and contemporaneous versions
171          of Unixware.  The acl() call returns the access and default ACL both
172          at once.  */
173       int n = acl (name, GETACLCNT, 0, NULL);
174       return n < 0 ? (errno == ENOSYS ? 0 : -1) : (MIN_ACL_ENTRIES < n);
175
176 #  endif
177
178 # endif
179     }
180 #endif
181
182   /* FIXME: Add support for AIX, Irix, and Tru64.  Please see Samba's
183      source/lib/sysacls.c file for fix-related ideas.  */
184
185   return 0;
186 }