qset-acl.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. /* qset-acl.c - set access control list equivalent to a mode
  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 and Andreas Gruenbacher, and Bruno Haible. */
  14. #include <config.h>
  15. #define ACL_INTERNAL_INLINE _GL_EXTERN_INLINE
  16. #include "acl.h"
  17. #include "acl-internal.h"
  18. /* If DESC is a valid file descriptor use fchmod to change the
  19. file's mode to MODE on systems that have fchmod. On systems
  20. that don't have fchmod and if DESC is invalid, use chmod on
  21. NAME instead.
  22. Return 0 if successful. Return -1 and set errno upon failure. */
  23. int
  24. chmod_or_fchmod (const char *name, int desc, mode_t mode)
  25. {
  26. if (HAVE_FCHMOD && desc != -1)
  27. return fchmod (desc, mode);
  28. else
  29. return chmod (name, mode);
  30. }
  31. /* Set the access control lists of a file. If DESC is a valid file
  32. descriptor, use file descriptor operations where available, else use
  33. filename based operations on NAME. If access control lists are not
  34. available, fchmod the target file to MODE. Also sets the
  35. non-permission bits of the destination file (S_ISUID, S_ISGID, S_ISVTX)
  36. to those from MODE if any are set.
  37. Return 0 if successful. Return -1 and set errno upon failure. */
  38. int
  39. qset_acl (char const *name, int desc, mode_t mode)
  40. {
  41. #if USE_ACL
  42. # if HAVE_ACL_GET_FILE
  43. /* POSIX 1003.1e draft 17 (abandoned) specific version. */
  44. /* Linux, FreeBSD, Mac OS X, IRIX, Tru64 */
  45. # if !HAVE_ACL_TYPE_EXTENDED
  46. /* Linux, FreeBSD, IRIX, Tru64 */
  47. /* We must also have acl_from_text and acl_delete_def_file.
  48. (acl_delete_def_file could be emulated with acl_init followed
  49. by acl_set_file, but acl_set_file with an empty acl is
  50. unspecified.) */
  51. # ifndef HAVE_ACL_FROM_TEXT
  52. # error Must have acl_from_text (see POSIX 1003.1e draft 17).
  53. # endif
  54. # ifndef HAVE_ACL_DELETE_DEF_FILE
  55. # error Must have acl_delete_def_file (see POSIX 1003.1e draft 17).
  56. # endif
  57. acl_t acl;
  58. int ret;
  59. if (HAVE_ACL_FROM_MODE) /* Linux */
  60. {
  61. acl = acl_from_mode (mode);
  62. if (!acl)
  63. return -1;
  64. }
  65. else /* FreeBSD, IRIX, Tru64 */
  66. {
  67. /* If we were to create the ACL using the functions acl_init(),
  68. acl_create_entry(), acl_set_tag_type(), acl_set_qualifier(),
  69. acl_get_permset(), acl_clear_perm[s](), acl_add_perm(), we
  70. would need to create a qualifier. I don't know how to do this.
  71. So create it using acl_from_text(). */
  72. # if HAVE_ACL_FREE_TEXT /* Tru64 */
  73. char acl_text[] = "u::---,g::---,o::---,";
  74. # else /* FreeBSD, IRIX */
  75. char acl_text[] = "u::---,g::---,o::---";
  76. # endif
  77. if (mode & S_IRUSR) acl_text[ 3] = 'r';
  78. if (mode & S_IWUSR) acl_text[ 4] = 'w';
  79. if (mode & S_IXUSR) acl_text[ 5] = 'x';
  80. if (mode & S_IRGRP) acl_text[10] = 'r';
  81. if (mode & S_IWGRP) acl_text[11] = 'w';
  82. if (mode & S_IXGRP) acl_text[12] = 'x';
  83. if (mode & S_IROTH) acl_text[17] = 'r';
  84. if (mode & S_IWOTH) acl_text[18] = 'w';
  85. if (mode & S_IXOTH) acl_text[19] = 'x';
  86. acl = acl_from_text (acl_text);
  87. if (!acl)
  88. return -1;
  89. }
  90. if (HAVE_ACL_SET_FD && desc != -1)
  91. ret = acl_set_fd (desc, acl);
  92. else
  93. ret = acl_set_file (name, ACL_TYPE_ACCESS, acl);
  94. if (ret != 0)
  95. {
  96. int saved_errno = errno;
  97. acl_free (acl);
  98. if (! acl_errno_valid (errno))
  99. return chmod_or_fchmod (name, desc, mode);
  100. errno = saved_errno;
  101. return -1;
  102. }
  103. else
  104. acl_free (acl);
  105. if (S_ISDIR (mode) && acl_delete_def_file (name))
  106. return -1;
  107. if (!MODE_INSIDE_ACL || (mode & (S_ISUID | S_ISGID | S_ISVTX)))
  108. {
  109. /* We did not call chmod so far, and either the mode and the ACL are
  110. separate or special bits are to be set which don't fit into ACLs. */
  111. return chmod_or_fchmod (name, desc, mode);
  112. }
  113. return 0;
  114. # else /* HAVE_ACL_TYPE_EXTENDED */
  115. /* Mac OS X */
  116. /* On Mac OS X, acl_get_file (name, ACL_TYPE_ACCESS)
  117. and acl_get_file (name, ACL_TYPE_DEFAULT)
  118. always return NULL / EINVAL. You have to use
  119. acl_get_file (name, ACL_TYPE_EXTENDED)
  120. or acl_get_fd (open (name, ...))
  121. to retrieve an ACL.
  122. On the other hand,
  123. acl_set_file (name, ACL_TYPE_ACCESS, acl)
  124. and acl_set_file (name, ACL_TYPE_DEFAULT, acl)
  125. have the same effect as
  126. acl_set_file (name, ACL_TYPE_EXTENDED, acl):
  127. Each of these calls sets the file's ACL. */
  128. acl_t acl;
  129. int ret;
  130. /* Remove the ACL if the file has ACLs. */
  131. if (HAVE_ACL_GET_FD && desc != -1)
  132. acl = acl_get_fd (desc);
  133. else
  134. acl = acl_get_file (name, ACL_TYPE_EXTENDED);
  135. if (acl)
  136. {
  137. acl_free (acl);
  138. acl = acl_init (0);
  139. if (acl)
  140. {
  141. if (HAVE_ACL_SET_FD && desc != -1)
  142. ret = acl_set_fd (desc, acl);
  143. else
  144. ret = acl_set_file (name, ACL_TYPE_EXTENDED, acl);
  145. if (ret != 0)
  146. {
  147. int saved_errno = errno;
  148. acl_free (acl);
  149. if (! acl_errno_valid (saved_errno))
  150. return chmod_or_fchmod (name, desc, mode);
  151. errno = saved_errno;
  152. return -1;
  153. }
  154. acl_free (acl);
  155. }
  156. }
  157. /* Since !MODE_INSIDE_ACL, we have to call chmod explicitly. */
  158. return chmod_or_fchmod (name, desc, mode);
  159. # endif
  160. # elif HAVE_FACL && defined GETACL /* Solaris, Cygwin, not HP-UX */
  161. int done_setacl = 0;
  162. # ifdef ACE_GETACL
  163. /* Solaris also has a different variant of ACLs, used in ZFS and NFSv4
  164. file systems (whereas the other ones are used in UFS file systems). */
  165. /* The flags in the ace_t structure changed in a binary incompatible way
  166. when ACL_NO_TRIVIAL etc. were introduced in <sys/acl.h> version 1.15.
  167. How to distinguish the two conventions at runtime?
  168. We fetch the existing ACL. In the old convention, usually three ACEs have
  169. a_flags = ACE_OWNER / ACE_GROUP / ACE_OTHER, in the range 0x0100..0x0400.
  170. In the new convention, these values are not used. */
  171. int convention;
  172. {
  173. /* Initially, try to read the entries into a stack-allocated buffer.
  174. Use malloc if it does not fit. */
  175. enum
  176. {
  177. alloc_init = 4000 / sizeof (ace_t), /* >= 3 */
  178. alloc_max = MIN (INT_MAX, SIZE_MAX / sizeof (ace_t))
  179. };
  180. ace_t buf[alloc_init];
  181. size_t alloc = alloc_init;
  182. ace_t *entries = buf;
  183. ace_t *malloced = NULL;
  184. int count;
  185. for (;;)
  186. {
  187. count = (desc != -1
  188. ? facl (desc, ACE_GETACL, alloc, entries)
  189. : acl (name, ACE_GETACL, alloc, entries));
  190. if (count < 0 && errno == ENOSPC)
  191. {
  192. /* Increase the size of the buffer. */
  193. free (malloced);
  194. if (alloc > alloc_max / 2)
  195. {
  196. errno = ENOMEM;
  197. return -1;
  198. }
  199. alloc = 2 * alloc; /* <= alloc_max */
  200. entries = malloced = (ace_t *) malloc (alloc * sizeof (ace_t));
  201. if (entries == NULL)
  202. {
  203. errno = ENOMEM;
  204. return -1;
  205. }
  206. continue;
  207. }
  208. break;
  209. }
  210. if (count <= 0)
  211. convention = -1;
  212. else
  213. {
  214. int i;
  215. convention = 0;
  216. for (i = 0; i < count; i++)
  217. if (entries[i].a_flags & (OLD_ACE_OWNER | OLD_ACE_GROUP | OLD_ACE_OTHER))
  218. {
  219. convention = 1;
  220. break;
  221. }
  222. }
  223. free (malloced);
  224. }
  225. if (convention >= 0)
  226. {
  227. ace_t entries[6];
  228. int count;
  229. int ret;
  230. if (convention)
  231. {
  232. /* Running on Solaris 10. */
  233. entries[0].a_type = OLD_ALLOW;
  234. entries[0].a_flags = OLD_ACE_OWNER;
  235. entries[0].a_who = 0; /* irrelevant */
  236. entries[0].a_access_mask = (mode >> 6) & 7;
  237. entries[1].a_type = OLD_ALLOW;
  238. entries[1].a_flags = OLD_ACE_GROUP;
  239. entries[1].a_who = 0; /* irrelevant */
  240. entries[1].a_access_mask = (mode >> 3) & 7;
  241. entries[2].a_type = OLD_ALLOW;
  242. entries[2].a_flags = OLD_ACE_OTHER;
  243. entries[2].a_who = 0;
  244. entries[2].a_access_mask = mode & 7;
  245. count = 3;
  246. }
  247. else
  248. {
  249. /* Running on Solaris 10 (newer version) or Solaris 11.
  250. The details here were found through "/bin/ls -lvd somefiles". */
  251. entries[0].a_type = NEW_ACE_ACCESS_DENIED_ACE_TYPE;
  252. entries[0].a_flags = NEW_ACE_OWNER;
  253. entries[0].a_who = 0; /* irrelevant */
  254. entries[0].a_access_mask = 0;
  255. entries[1].a_type = NEW_ACE_ACCESS_ALLOWED_ACE_TYPE;
  256. entries[1].a_flags = NEW_ACE_OWNER;
  257. entries[1].a_who = 0; /* irrelevant */
  258. entries[1].a_access_mask = NEW_ACE_WRITE_NAMED_ATTRS
  259. | NEW_ACE_WRITE_ATTRIBUTES
  260. | NEW_ACE_WRITE_ACL
  261. | NEW_ACE_WRITE_OWNER;
  262. if (mode & 0400)
  263. entries[1].a_access_mask |= NEW_ACE_READ_DATA;
  264. else
  265. entries[0].a_access_mask |= NEW_ACE_READ_DATA;
  266. if (mode & 0200)
  267. entries[1].a_access_mask |= NEW_ACE_WRITE_DATA | NEW_ACE_APPEND_DATA;
  268. else
  269. entries[0].a_access_mask |= NEW_ACE_WRITE_DATA | NEW_ACE_APPEND_DATA;
  270. if (mode & 0100)
  271. entries[1].a_access_mask |= NEW_ACE_EXECUTE;
  272. else
  273. entries[0].a_access_mask |= NEW_ACE_EXECUTE;
  274. entries[2].a_type = NEW_ACE_ACCESS_DENIED_ACE_TYPE;
  275. entries[2].a_flags = NEW_ACE_GROUP | NEW_ACE_IDENTIFIER_GROUP;
  276. entries[2].a_who = 0; /* irrelevant */
  277. entries[2].a_access_mask = 0;
  278. entries[3].a_type = NEW_ACE_ACCESS_ALLOWED_ACE_TYPE;
  279. entries[3].a_flags = NEW_ACE_GROUP | NEW_ACE_IDENTIFIER_GROUP;
  280. entries[3].a_who = 0; /* irrelevant */
  281. entries[3].a_access_mask = 0;
  282. if (mode & 0040)
  283. entries[3].a_access_mask |= NEW_ACE_READ_DATA;
  284. else
  285. entries[2].a_access_mask |= NEW_ACE_READ_DATA;
  286. if (mode & 0020)
  287. entries[3].a_access_mask |= NEW_ACE_WRITE_DATA | NEW_ACE_APPEND_DATA;
  288. else
  289. entries[2].a_access_mask |= NEW_ACE_WRITE_DATA | NEW_ACE_APPEND_DATA;
  290. if (mode & 0010)
  291. entries[3].a_access_mask |= NEW_ACE_EXECUTE;
  292. else
  293. entries[2].a_access_mask |= NEW_ACE_EXECUTE;
  294. entries[4].a_type = NEW_ACE_ACCESS_DENIED_ACE_TYPE;
  295. entries[4].a_flags = NEW_ACE_EVERYONE;
  296. entries[4].a_who = 0;
  297. entries[4].a_access_mask = NEW_ACE_WRITE_NAMED_ATTRS
  298. | NEW_ACE_WRITE_ATTRIBUTES
  299. | NEW_ACE_WRITE_ACL
  300. | NEW_ACE_WRITE_OWNER;
  301. entries[5].a_type = NEW_ACE_ACCESS_ALLOWED_ACE_TYPE;
  302. entries[5].a_flags = NEW_ACE_EVERYONE;
  303. entries[5].a_who = 0;
  304. entries[5].a_access_mask = NEW_ACE_READ_NAMED_ATTRS
  305. | NEW_ACE_READ_ATTRIBUTES
  306. | NEW_ACE_READ_ACL
  307. | NEW_ACE_SYNCHRONIZE;
  308. if (mode & 0004)
  309. entries[5].a_access_mask |= NEW_ACE_READ_DATA;
  310. else
  311. entries[4].a_access_mask |= NEW_ACE_READ_DATA;
  312. if (mode & 0002)
  313. entries[5].a_access_mask |= NEW_ACE_WRITE_DATA | NEW_ACE_APPEND_DATA;
  314. else
  315. entries[4].a_access_mask |= NEW_ACE_WRITE_DATA | NEW_ACE_APPEND_DATA;
  316. if (mode & 0001)
  317. entries[5].a_access_mask |= NEW_ACE_EXECUTE;
  318. else
  319. entries[4].a_access_mask |= NEW_ACE_EXECUTE;
  320. count = 6;
  321. }
  322. if (desc != -1)
  323. ret = facl (desc, ACE_SETACL, count, entries);
  324. else
  325. ret = acl (name, ACE_SETACL, count, entries);
  326. if (ret < 0 && errno != EINVAL && errno != ENOTSUP)
  327. {
  328. if (errno == ENOSYS)
  329. return chmod_or_fchmod (name, desc, mode);
  330. return -1;
  331. }
  332. if (ret == 0)
  333. done_setacl = 1;
  334. }
  335. # endif
  336. if (!done_setacl)
  337. {
  338. aclent_t entries[3];
  339. int ret;
  340. entries[0].a_type = USER_OBJ;
  341. entries[0].a_id = 0; /* irrelevant */
  342. entries[0].a_perm = (mode >> 6) & 7;
  343. entries[1].a_type = GROUP_OBJ;
  344. entries[1].a_id = 0; /* irrelevant */
  345. entries[1].a_perm = (mode >> 3) & 7;
  346. entries[2].a_type = OTHER_OBJ;
  347. entries[2].a_id = 0;
  348. entries[2].a_perm = mode & 7;
  349. if (desc != -1)
  350. ret = facl (desc, SETACL,
  351. sizeof (entries) / sizeof (aclent_t), entries);
  352. else
  353. ret = acl (name, SETACL,
  354. sizeof (entries) / sizeof (aclent_t), entries);
  355. if (ret < 0)
  356. {
  357. if (errno == ENOSYS || errno == EOPNOTSUPP)
  358. return chmod_or_fchmod (name, desc, mode);
  359. return -1;
  360. }
  361. }
  362. if (!MODE_INSIDE_ACL || (mode & (S_ISUID | S_ISGID | S_ISVTX)))
  363. {
  364. /* We did not call chmod so far, so the special bits have not yet
  365. been set. */
  366. return chmod_or_fchmod (name, desc, mode);
  367. }
  368. return 0;
  369. # elif HAVE_GETACL /* HP-UX */
  370. struct stat statbuf;
  371. int ret;
  372. if (desc != -1)
  373. ret = fstat (desc, &statbuf);
  374. else
  375. ret = stat (name, &statbuf);
  376. if (ret < 0)
  377. return -1;
  378. {
  379. struct acl_entry entries[3];
  380. entries[0].uid = statbuf.st_uid;
  381. entries[0].gid = ACL_NSGROUP;
  382. entries[0].mode = (mode >> 6) & 7;
  383. entries[1].uid = ACL_NSUSER;
  384. entries[1].gid = statbuf.st_gid;
  385. entries[1].mode = (mode >> 3) & 7;
  386. entries[2].uid = ACL_NSUSER;
  387. entries[2].gid = ACL_NSGROUP;
  388. entries[2].mode = mode & 7;
  389. if (desc != -1)
  390. ret = fsetacl (desc, sizeof (entries) / sizeof (struct acl_entry), entries);
  391. else
  392. ret = setacl (name, sizeof (entries) / sizeof (struct acl_entry), entries);
  393. }
  394. if (ret < 0)
  395. {
  396. if (!(errno == ENOSYS || errno == EOPNOTSUPP || errno == ENOTSUP))
  397. return -1;
  398. # if HAVE_ACLV_H /* HP-UX >= 11.11 */
  399. {
  400. struct acl entries[4];
  401. entries[0].a_type = USER_OBJ;
  402. entries[0].a_id = 0; /* irrelevant */
  403. entries[0].a_perm = (mode >> 6) & 7;
  404. entries[1].a_type = GROUP_OBJ;
  405. entries[1].a_id = 0; /* irrelevant */
  406. entries[1].a_perm = (mode >> 3) & 7;
  407. entries[2].a_type = CLASS_OBJ;
  408. entries[2].a_id = 0;
  409. entries[2].a_perm = (mode >> 3) & 7;
  410. entries[3].a_type = OTHER_OBJ;
  411. entries[3].a_id = 0;
  412. entries[3].a_perm = mode & 7;
  413. ret = aclsort (sizeof (entries) / sizeof (struct acl), 1, entries);
  414. if (ret > 0)
  415. abort ();
  416. if (ret < 0)
  417. {
  418. if (0)
  419. return chmod_or_fchmod (name, desc, mode);
  420. return -1;
  421. }
  422. ret = acl ((char *) name, ACL_SET,
  423. sizeof (entries) / sizeof (struct acl), entries);
  424. if (ret < 0)
  425. {
  426. if (errno == ENOSYS || errno == EOPNOTSUPP || errno == EINVAL)
  427. return chmod_or_fchmod (name, desc, mode);
  428. return -1;
  429. }
  430. }
  431. # else
  432. return chmod_or_fchmod (name, desc, mode);
  433. # endif
  434. }
  435. if (mode & (S_ISUID | S_ISGID | S_ISVTX))
  436. {
  437. /* We did not call chmod so far, so the special bits have not yet
  438. been set. */
  439. return chmod_or_fchmod (name, desc, mode);
  440. }
  441. return 0;
  442. # elif HAVE_ACLX_GET && defined ACL_AIX_WIP /* AIX */
  443. acl_type_list_t types;
  444. size_t types_size = sizeof (types);
  445. acl_type_t type;
  446. if (aclx_gettypes (name, &types, &types_size) < 0
  447. || types.num_entries == 0)
  448. return chmod_or_fchmod (name, desc, mode);
  449. /* XXX Do we need to clear all types of ACLs for the given file, or is it
  450. sufficient to clear the first one? */
  451. type = types.entries[0];
  452. if (type.u64 == ACL_AIXC)
  453. {
  454. union { struct acl a; char room[128]; } u;
  455. int ret;
  456. u.a.acl_len = (char *) &u.a.acl_ext[0] - (char *) &u.a; /* no entries */
  457. u.a.acl_mode = mode & ~(S_IXACL | 0777);
  458. u.a.u_access = (mode >> 6) & 7;
  459. u.a.g_access = (mode >> 3) & 7;
  460. u.a.o_access = mode & 7;
  461. if (desc != -1)
  462. ret = aclx_fput (desc, SET_ACL | SET_MODE_S_BITS,
  463. type, &u.a, u.a.acl_len, mode);
  464. else
  465. ret = aclx_put (name, SET_ACL | SET_MODE_S_BITS,
  466. type, &u.a, u.a.acl_len, mode);
  467. if (!(ret < 0 && errno == ENOSYS))
  468. return ret;
  469. }
  470. else if (type.u64 == ACL_NFS4)
  471. {
  472. union { nfs4_acl_int_t a; char room[128]; } u;
  473. nfs4_ace_int_t *ace;
  474. int ret;
  475. u.a.aclVersion = NFS4_ACL_INT_STRUCT_VERSION;
  476. u.a.aclEntryN = 0;
  477. ace = &u.a.aclEntry[0];
  478. {
  479. ace->flags = ACE4_ID_SPECIAL;
  480. ace->aceWho.special_whoid = ACE4_WHO_OWNER;
  481. ace->aceType = ACE4_ACCESS_ALLOWED_ACE_TYPE;
  482. ace->aceFlags = 0;
  483. ace->aceMask =
  484. (mode & 0400 ? ACE4_READ_DATA | ACE4_LIST_DIRECTORY : 0)
  485. | (mode & 0200
  486. ? ACE4_WRITE_DATA | ACE4_ADD_FILE | ACE4_APPEND_DATA
  487. | ACE4_ADD_SUBDIRECTORY
  488. : 0)
  489. | (mode & 0100 ? ACE4_EXECUTE : 0);
  490. ace->aceWhoString[0] = '\0';
  491. ace->entryLen = (char *) &ace->aceWhoString[4] - (char *) ace;
  492. ace = (nfs4_ace_int_t *) (char *) &ace->aceWhoString[4];
  493. u.a.aclEntryN++;
  494. }
  495. {
  496. ace->flags = ACE4_ID_SPECIAL;
  497. ace->aceWho.special_whoid = ACE4_WHO_GROUP;
  498. ace->aceType = ACE4_ACCESS_ALLOWED_ACE_TYPE;
  499. ace->aceFlags = 0;
  500. ace->aceMask =
  501. (mode & 0040 ? ACE4_READ_DATA | ACE4_LIST_DIRECTORY : 0)
  502. | (mode & 0020
  503. ? ACE4_WRITE_DATA | ACE4_ADD_FILE | ACE4_APPEND_DATA
  504. | ACE4_ADD_SUBDIRECTORY
  505. : 0)
  506. | (mode & 0010 ? ACE4_EXECUTE : 0);
  507. ace->aceWhoString[0] = '\0';
  508. ace->entryLen = (char *) &ace->aceWhoString[4] - (char *) ace;
  509. ace = (nfs4_ace_int_t *) (char *) &ace->aceWhoString[4];
  510. u.a.aclEntryN++;
  511. }
  512. {
  513. ace->flags = ACE4_ID_SPECIAL;
  514. ace->aceWho.special_whoid = ACE4_WHO_EVERYONE;
  515. ace->aceType = ACE4_ACCESS_ALLOWED_ACE_TYPE;
  516. ace->aceFlags = 0;
  517. ace->aceMask =
  518. (mode & 0004 ? ACE4_READ_DATA | ACE4_LIST_DIRECTORY : 0)
  519. | (mode & 0002
  520. ? ACE4_WRITE_DATA | ACE4_ADD_FILE | ACE4_APPEND_DATA
  521. | ACE4_ADD_SUBDIRECTORY
  522. : 0)
  523. | (mode & 0001 ? ACE4_EXECUTE : 0);
  524. ace->aceWhoString[0] = '\0';
  525. ace->entryLen = (char *) &ace->aceWhoString[4] - (char *) ace;
  526. ace = (nfs4_ace_int_t *) (char *) &ace->aceWhoString[4];
  527. u.a.aclEntryN++;
  528. }
  529. u.a.aclLength = (char *) ace - (char *) &u.a;
  530. if (desc != -1)
  531. ret = aclx_fput (desc, SET_ACL | SET_MODE_S_BITS,
  532. type, &u.a, u.a.aclLength, mode);
  533. else
  534. ret = aclx_put (name, SET_ACL | SET_MODE_S_BITS,
  535. type, &u.a, u.a.aclLength, mode);
  536. if (!(ret < 0 && errno == ENOSYS))
  537. return ret;
  538. }
  539. return chmod_or_fchmod (name, desc, mode);
  540. # elif HAVE_STATACL /* older AIX */
  541. union { struct acl a; char room[128]; } u;
  542. int ret;
  543. u.a.acl_len = (char *) &u.a.acl_ext[0] - (char *) &u.a; /* no entries */
  544. u.a.acl_mode = mode & ~(S_IXACL | 0777);
  545. u.a.u_access = (mode >> 6) & 7;
  546. u.a.g_access = (mode >> 3) & 7;
  547. u.a.o_access = mode & 7;
  548. if (desc != -1)
  549. ret = fchacl (desc, &u.a, u.a.acl_len);
  550. else
  551. ret = chacl (name, &u.a, u.a.acl_len);
  552. if (ret < 0 && errno == ENOSYS)
  553. return chmod_or_fchmod (name, desc, mode);
  554. return ret;
  555. # elif HAVE_ACLSORT /* NonStop Kernel */
  556. struct acl entries[4];
  557. int ret;
  558. entries[0].a_type = USER_OBJ;
  559. entries[0].a_id = 0; /* irrelevant */
  560. entries[0].a_perm = (mode >> 6) & 7;
  561. entries[1].a_type = GROUP_OBJ;
  562. entries[1].a_id = 0; /* irrelevant */
  563. entries[1].a_perm = (mode >> 3) & 7;
  564. entries[2].a_type = CLASS_OBJ;
  565. entries[2].a_id = 0;
  566. entries[2].a_perm = (mode >> 3) & 7;
  567. entries[3].a_type = OTHER_OBJ;
  568. entries[3].a_id = 0;
  569. entries[3].a_perm = mode & 7;
  570. ret = aclsort (sizeof (entries) / sizeof (struct acl), 1, entries);
  571. if (ret > 0)
  572. abort ();
  573. if (ret < 0)
  574. {
  575. if (0)
  576. return chmod_or_fchmod (name, desc, mode);
  577. return -1;
  578. }
  579. ret = acl ((char *) name, ACL_SET,
  580. sizeof (entries) / sizeof (struct acl), entries);
  581. if (ret < 0)
  582. {
  583. if (0)
  584. return chmod_or_fchmod (name, desc, mode);
  585. return -1;
  586. }
  587. if (mode & (S_ISUID | S_ISGID | S_ISVTX))
  588. {
  589. /* We did not call chmod so far, so the special bits have not yet
  590. been set. */
  591. return chmod_or_fchmod (name, desc, mode);
  592. }
  593. return 0;
  594. # else /* Unknown flavor of ACLs */
  595. return chmod_or_fchmod (name, desc, mode);
  596. # endif
  597. #else /* !USE_ACL */
  598. return chmod_or_fchmod (name, desc, mode);
  599. #endif
  600. }