acl-internal.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /* Internal implementation of access control lists.
  2. Copyright (C) 2002-2003, 2005-2015 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. Written by Paul Eggert, Andreas Grünbacher, and Bruno Haible. */
  14. #include "acl.h"
  15. #include <stdbool.h>
  16. #include <stdlib.h>
  17. /* All systems define the ACL related API in <sys/acl.h>. */
  18. #if HAVE_SYS_ACL_H
  19. # include <sys/acl.h>
  20. #endif
  21. #if defined HAVE_FACL && ! defined GETACLCNT && defined ACL_CNT
  22. # define GETACLCNT ACL_CNT
  23. #endif
  24. /* On Linux, additional ACL related API is available in <acl/libacl.h>. */
  25. #ifdef HAVE_ACL_LIBACL_H
  26. # include <acl/libacl.h>
  27. #endif
  28. /* On HP-UX >= 11.11, additional ACL API is available in <aclv.h>. */
  29. #if HAVE_ACLV_H
  30. # include <sys/types.h>
  31. # include <aclv.h>
  32. /* HP-UX 11.11 lacks these declarations. */
  33. extern int acl (char *, int, int, struct acl *);
  34. extern int aclsort (int, int, struct acl *);
  35. #endif
  36. #include <errno.h>
  37. #include <limits.h>
  38. #ifndef MIN
  39. # define MIN(a,b) ((a) < (b) ? (a) : (b))
  40. #endif
  41. #ifndef SIZE_MAX
  42. # define SIZE_MAX ((size_t) -1)
  43. #endif
  44. #ifndef HAVE_FCHMOD
  45. # define HAVE_FCHMOD false
  46. # define fchmod(fd, mode) (-1)
  47. #endif
  48. #ifndef _GL_INLINE_HEADER_BEGIN
  49. #error "Please include config.h first."
  50. #endif
  51. _GL_INLINE_HEADER_BEGIN
  52. #ifndef ACL_INTERNAL_INLINE
  53. # define ACL_INTERNAL_INLINE _GL_INLINE
  54. #endif
  55. #if USE_ACL
  56. # if HAVE_ACL_GET_FILE
  57. /* POSIX 1003.1e (draft 17 -- abandoned) specific version. */
  58. /* Linux, FreeBSD, Mac OS X, IRIX, Tru64 */
  59. # ifndef MIN_ACL_ENTRIES
  60. # define MIN_ACL_ENTRIES 4
  61. # endif
  62. /* POSIX 1003.1e (draft 17) */
  63. # ifdef HAVE_ACL_GET_FD
  64. /* Most platforms have a 1-argument acl_get_fd, only OSF/1 has a 2-argument
  65. macro(!). */
  66. # if HAVE_ACL_FREE_TEXT /* OSF/1 */
  67. ACL_INTERNAL_INLINE acl_t
  68. rpl_acl_get_fd (int fd)
  69. {
  70. return acl_get_fd (fd, ACL_TYPE_ACCESS);
  71. }
  72. # undef acl_get_fd
  73. # define acl_get_fd rpl_acl_get_fd
  74. # endif
  75. # else
  76. # define HAVE_ACL_GET_FD false
  77. # undef acl_get_fd
  78. # define acl_get_fd(fd) (NULL)
  79. # endif
  80. /* POSIX 1003.1e (draft 17) */
  81. # ifdef HAVE_ACL_SET_FD
  82. /* Most platforms have a 2-argument acl_set_fd, only OSF/1 has a 3-argument
  83. macro(!). */
  84. # if HAVE_ACL_FREE_TEXT /* OSF/1 */
  85. ACL_INTERNAL_INLINE int
  86. rpl_acl_set_fd (int fd, acl_t acl)
  87. {
  88. return acl_set_fd (fd, ACL_TYPE_ACCESS, acl);
  89. }
  90. # undef acl_set_fd
  91. # define acl_set_fd rpl_acl_set_fd
  92. # endif
  93. # else
  94. # define HAVE_ACL_SET_FD false
  95. # undef acl_set_fd
  96. # define acl_set_fd(fd, acl) (-1)
  97. # endif
  98. /* POSIX 1003.1e (draft 13) */
  99. # if ! HAVE_ACL_FREE_TEXT
  100. # define acl_free_text(buf) acl_free (buf)
  101. # endif
  102. /* Linux-specific */
  103. # ifndef HAVE_ACL_EXTENDED_FILE
  104. # define HAVE_ACL_EXTENDED_FILE false
  105. # define acl_extended_file(name) (-1)
  106. # endif
  107. /* Linux-specific */
  108. # ifndef HAVE_ACL_FROM_MODE
  109. # define HAVE_ACL_FROM_MODE false
  110. # define acl_from_mode(mode) (NULL)
  111. # endif
  112. /* Set to 1 if a file's mode is implicit by the ACL.
  113. Set to 0 if a file's mode is stored independently from the ACL. */
  114. # if (HAVE_ACL_COPY_EXT_NATIVE && HAVE_ACL_CREATE_ENTRY_NP) || defined __sgi /* Mac OS X, IRIX */
  115. # define MODE_INSIDE_ACL 0
  116. # else
  117. # define MODE_INSIDE_ACL 1
  118. # endif
  119. /* Return the number of entries in ACL.
  120. Return -1 and set errno upon failure to determine it. */
  121. /* Define a replacement for acl_entries if needed. (Only Linux has it.) */
  122. # if !HAVE_ACL_ENTRIES
  123. # define acl_entries rpl_acl_entries
  124. extern int acl_entries (acl_t);
  125. # endif
  126. # if HAVE_ACL_TYPE_EXTENDED /* Mac OS X */
  127. /* ACL is an ACL, from a file, stored as type ACL_TYPE_EXTENDED.
  128. Return 1 if the given ACL is non-trivial.
  129. Return 0 if it is trivial. */
  130. extern int acl_extended_nontrivial (acl_t);
  131. # else
  132. /* ACL is an ACL, from a file, stored as type ACL_TYPE_ACCESS.
  133. Return 1 if the given ACL is non-trivial.
  134. Return 0 if it is trivial, i.e. equivalent to a simple stat() mode.
  135. Return -1 and set errno upon failure to determine it. */
  136. extern int acl_access_nontrivial (acl_t);
  137. # endif
  138. # elif HAVE_FACL && defined GETACL /* Solaris, Cygwin, not HP-UX */
  139. /* Set to 1 if a file's mode is implicit by the ACL.
  140. Set to 0 if a file's mode is stored independently from the ACL. */
  141. # if defined __CYGWIN__ /* Cygwin */
  142. # define MODE_INSIDE_ACL 0
  143. # else /* Solaris */
  144. # define MODE_INSIDE_ACL 1
  145. # endif
  146. /* Return 1 if the given ACL is non-trivial.
  147. Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
  148. extern int acl_nontrivial (int count, aclent_t *entries) _GL_ATTRIBUTE_PURE;
  149. # ifdef ACE_GETACL /* Solaris 10 */
  150. /* Test an ACL retrieved with ACE_GETACL.
  151. Return 1 if the given ACL, consisting of COUNT entries, is non-trivial.
  152. Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
  153. extern int acl_ace_nontrivial (int count, ace_t *entries) _GL_ATTRIBUTE_PURE;
  154. /* Definitions for when the built executable is executed on Solaris 10
  155. (newer version) or Solaris 11. */
  156. /* For a_type. */
  157. # define OLD_ALLOW 0
  158. # define OLD_DENY 1
  159. # define NEW_ACE_ACCESS_ALLOWED_ACE_TYPE 0 /* replaces ALLOW */
  160. # define NEW_ACE_ACCESS_DENIED_ACE_TYPE 1 /* replaces DENY */
  161. /* For a_flags. */
  162. # define OLD_ACE_OWNER 0x0100
  163. # define OLD_ACE_GROUP 0x0200
  164. # define OLD_ACE_OTHER 0x0400
  165. # define NEW_ACE_OWNER 0x1000
  166. # define NEW_ACE_GROUP 0x2000
  167. # define NEW_ACE_IDENTIFIER_GROUP 0x0040
  168. # define NEW_ACE_EVERYONE 0x4000
  169. /* For a_access_mask. */
  170. # define NEW_ACE_READ_DATA 0x001 /* corresponds to 'r' */
  171. # define NEW_ACE_WRITE_DATA 0x002 /* corresponds to 'w' */
  172. # define NEW_ACE_APPEND_DATA 0x004
  173. # define NEW_ACE_READ_NAMED_ATTRS 0x008
  174. # define NEW_ACE_WRITE_NAMED_ATTRS 0x010
  175. # define NEW_ACE_EXECUTE 0x020
  176. # define NEW_ACE_DELETE_CHILD 0x040
  177. # define NEW_ACE_READ_ATTRIBUTES 0x080
  178. # define NEW_ACE_WRITE_ATTRIBUTES 0x100
  179. # define NEW_ACE_DELETE 0x10000
  180. # define NEW_ACE_READ_ACL 0x20000
  181. # define NEW_ACE_WRITE_ACL 0x40000
  182. # define NEW_ACE_WRITE_OWNER 0x80000
  183. # define NEW_ACE_SYNCHRONIZE 0x100000
  184. # endif
  185. # elif HAVE_GETACL /* HP-UX */
  186. /* Return 1 if the given ACL is non-trivial.
  187. Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
  188. extern int acl_nontrivial (int count, struct acl_entry *entries, struct stat *sb);
  189. # if HAVE_ACLV_H /* HP-UX >= 11.11 */
  190. /* Return 1 if the given ACL is non-trivial.
  191. Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
  192. extern int aclv_nontrivial (int count, struct acl *entries);
  193. # endif
  194. # elif HAVE_ACLX_GET && 0 /* AIX */
  195. /* TODO */
  196. # elif HAVE_STATACL /* older AIX */
  197. /* Return 1 if the given ACL is non-trivial.
  198. Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
  199. extern int acl_nontrivial (struct acl *a);
  200. # elif HAVE_ACLSORT /* NonStop Kernel */
  201. /* Return 1 if the given ACL is non-trivial.
  202. Return 0 if it is trivial, i.e. equivalent to a simple stat() mode. */
  203. extern int acl_nontrivial (int count, struct acl *entries);
  204. # endif
  205. #endif
  206. _GL_INLINE_HEADER_END