Add missing semicolon in man page
[rsync.git] / acls.c
blob0b5dec6ad5056f930a97a45ff7fb269497fb75a3
1 /*
2 * Handle passing Access Control Lists between systems.
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2006-2020 Wayne Davison
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, visit the http://fsf.org website.
22 #include "rsync.h"
23 #include "lib/sysacls.h"
25 #ifdef SUPPORT_ACLS
27 extern int dry_run;
28 extern int am_root;
29 extern int read_only;
30 extern int list_only;
31 extern int orig_umask;
32 extern int numeric_ids;
33 extern int inc_recurse;
34 extern int preserve_devices;
35 extern int preserve_specials;
37 /* Flags used to indicate what items are being transmitted for an entry. */
38 #define XMIT_USER_OBJ (1<<0)
39 #define XMIT_GROUP_OBJ (1<<1)
40 #define XMIT_MASK_OBJ (1<<2)
41 #define XMIT_OTHER_OBJ (1<<3)
42 #define XMIT_NAME_LIST (1<<4)
44 #define NO_ENTRY ((uchar)0x80) /* Default value of a NON-name-list entry. */
46 #define NAME_IS_USER (1u<<31) /* Bit used only on a name-list entry. */
48 /* When we send the access bits over the wire, we shift them 2 bits to the
49 * left and use the lower 2 bits as flags (relevant only to a name entry).
50 * This makes the protocol more efficient than sending a value that would
51 * be likely to have its highest bits set. */
52 #define XFLAG_NAME_FOLLOWS 0x0001u
53 #define XFLAG_NAME_IS_USER 0x0002u
55 /* === ACL structures === */
57 typedef struct {
58 id_t id;
59 uint32 access;
60 } id_access;
62 typedef struct {
63 id_access *idas;
64 int count;
65 } ida_entries;
67 typedef struct {
68 char *name;
69 uchar len;
70 } idname;
72 typedef struct rsync_acl {
73 ida_entries names;
74 /* These will be NO_ENTRY if there's no such entry. */
75 uchar user_obj;
76 uchar group_obj;
77 uchar mask_obj;
78 uchar other_obj;
79 } rsync_acl;
81 typedef struct {
82 rsync_acl racl;
83 SMB_ACL_T sacl;
84 } acl_duo;
86 static const rsync_acl empty_rsync_acl = {
87 {NULL, 0}, NO_ENTRY, NO_ENTRY, NO_ENTRY, NO_ENTRY
90 static item_list access_acl_list = EMPTY_ITEM_LIST;
91 static item_list default_acl_list = EMPTY_ITEM_LIST;
93 static size_t prior_access_count = (size_t)-1;
94 static size_t prior_default_count = (size_t)-1;
96 /* === Calculations on ACL types === */
98 static const char *str_acl_type(SMB_ACL_TYPE_T type)
100 switch (type) {
101 case SMB_ACL_TYPE_ACCESS:
102 #ifdef HAVE_OSX_ACLS
103 return "ACL_TYPE_EXTENDED";
104 #else
105 return "ACL_TYPE_ACCESS";
106 #endif
107 case SMB_ACL_TYPE_DEFAULT:
108 return "ACL_TYPE_DEFAULT";
109 default:
110 break;
112 return "unknown ACL type!";
115 static int calc_sacl_entries(const rsync_acl *racl)
117 /* A System ACL always gets user/group/other permission entries. */
118 return racl->names.count
119 #ifdef ACLS_NEED_MASK
121 #else
122 + (racl->mask_obj != NO_ENTRY)
123 #endif
124 + 3;
127 /* Extracts and returns the permission bits from the ACL. This cannot be
128 * called on an rsync_acl that has NO_ENTRY in any spot but the mask. */
129 static int rsync_acl_get_perms(const rsync_acl *racl)
131 return (racl->user_obj << 6)
132 + ((racl->mask_obj != NO_ENTRY ? racl->mask_obj : racl->group_obj) << 3)
133 + racl->other_obj;
136 /* Removes the permission-bit entries from the ACL because these
137 * can be reconstructed from the file's mode. */
138 static void rsync_acl_strip_perms(stat_x *sxp)
140 rsync_acl *racl = sxp->acc_acl;
142 racl->user_obj = NO_ENTRY;
143 if (racl->mask_obj == NO_ENTRY)
144 racl->group_obj = NO_ENTRY;
145 else {
146 int group_perms = (sxp->st.st_mode >> 3) & 7;
147 if (racl->group_obj == group_perms)
148 racl->group_obj = NO_ENTRY;
149 #ifndef HAVE_SOLARIS_ACLS
150 if (racl->names.count != 0 && racl->mask_obj == group_perms)
151 racl->mask_obj = NO_ENTRY;
152 #endif
154 racl->other_obj = NO_ENTRY;
157 /* Given an empty rsync_acl, fake up the permission bits. */
158 static void rsync_acl_fake_perms(rsync_acl *racl, mode_t mode)
160 racl->user_obj = (mode >> 6) & 7;
161 racl->group_obj = (mode >> 3) & 7;
162 racl->other_obj = mode & 7;
165 /* === Rsync ACL functions === */
167 static rsync_acl *create_racl(void)
169 rsync_acl *racl = new(rsync_acl);
171 if (!racl)
172 out_of_memory("create_racl");
173 *racl = empty_rsync_acl;
175 return racl;
178 static BOOL ida_entries_equal(const ida_entries *ial1, const ida_entries *ial2)
180 id_access *ida1, *ida2;
181 int count = ial1->count;
182 if (count != ial2->count)
183 return False;
184 ida1 = ial1->idas;
185 ida2 = ial2->idas;
186 for (; count--; ida1++, ida2++) {
187 if (ida1->access != ida2->access || ida1->id != ida2->id)
188 return False;
190 return True;
193 static BOOL rsync_acl_equal(const rsync_acl *racl1, const rsync_acl *racl2)
195 return racl1->user_obj == racl2->user_obj
196 && racl1->group_obj == racl2->group_obj
197 && racl1->mask_obj == racl2->mask_obj
198 && racl1->other_obj == racl2->other_obj
199 && ida_entries_equal(&racl1->names, &racl2->names);
202 /* Are the extended (non-permission-bit) entries equal? If so, the rest of
203 * the ACL will be handled by the normal mode-preservation code. This is
204 * only meaningful for access ACLs! Note: the 1st arg is a fully-populated
205 * rsync_acl, but the 2nd parameter can be a condensed rsync_acl, which means
206 * that it might have several of its permission objects set to NO_ENTRY. */
207 static BOOL rsync_acl_equal_enough(const rsync_acl *racl1,
208 const rsync_acl *racl2, mode_t m)
210 if ((racl1->mask_obj ^ racl2->mask_obj) & NO_ENTRY)
211 return False; /* One has a mask and the other doesn't */
213 /* When there's a mask, the group_obj becomes an extended entry. */
214 if (racl1->mask_obj != NO_ENTRY) {
215 /* A condensed rsync_acl with a mask can only have no
216 * group_obj when it was identical to the mask. This
217 * means that it was also identical to the group attrs
218 * from the mode. */
219 if (racl2->group_obj == NO_ENTRY) {
220 if (racl1->group_obj != ((m >> 3) & 7))
221 return False;
222 } else if (racl1->group_obj != racl2->group_obj)
223 return False;
225 return ida_entries_equal(&racl1->names, &racl2->names);
228 static void rsync_acl_free(rsync_acl *racl)
230 if (racl->names.idas)
231 free(racl->names.idas);
232 *racl = empty_rsync_acl;
235 void free_acl(stat_x *sxp)
237 if (sxp->acc_acl) {
238 rsync_acl_free(sxp->acc_acl);
239 free(sxp->acc_acl);
240 sxp->acc_acl = NULL;
242 if (sxp->def_acl) {
243 rsync_acl_free(sxp->def_acl);
244 free(sxp->def_acl);
245 sxp->def_acl = NULL;
249 #ifdef SMB_ACL_NEED_SORT
250 static int id_access_sorter(const void *r1, const void *r2)
252 id_access *ida1 = (id_access *)r1;
253 id_access *ida2 = (id_access *)r2;
254 id_t rid1 = ida1->id, rid2 = ida2->id;
255 if ((ida1->access ^ ida2->access) & NAME_IS_USER)
256 return ida1->access & NAME_IS_USER ? -1 : 1;
257 return rid1 == rid2 ? 0 : rid1 < rid2 ? -1 : 1;
259 #endif
261 /* === System ACLs === */
263 /* Unpack system ACL -> rsync ACL verbatim. Return whether we succeeded. */
264 static BOOL unpack_smb_acl(SMB_ACL_T sacl, rsync_acl *racl)
266 static item_list temp_ida_list = EMPTY_ITEM_LIST;
267 SMB_ACL_ENTRY_T entry;
268 const char *errfun;
269 int rc;
271 errfun = "sys_acl_get_entry";
272 for (rc = sys_acl_get_entry(sacl, SMB_ACL_FIRST_ENTRY, &entry);
273 rc == 1;
274 rc = sys_acl_get_entry(sacl, SMB_ACL_NEXT_ENTRY, &entry)) {
275 SMB_ACL_TAG_T tag_type;
276 uint32 access;
277 id_t g_u_id;
278 id_access *ida;
279 if ((rc = sys_acl_get_info(entry, &tag_type, &access, &g_u_id)) != 0) {
280 errfun = "sys_acl_get_info";
281 break;
283 /* continue == done with entry; break == store in temporary ida list */
284 switch (tag_type) {
285 #ifndef HAVE_OSX_ACLS
286 case SMB_ACL_USER_OBJ:
287 if (racl->user_obj == NO_ENTRY)
288 racl->user_obj = access;
289 else
290 rprintf(FINFO, "unpack_smb_acl: warning: duplicate USER_OBJ entry ignored\n");
291 continue;
292 case SMB_ACL_GROUP_OBJ:
293 if (racl->group_obj == NO_ENTRY)
294 racl->group_obj = access;
295 else
296 rprintf(FINFO, "unpack_smb_acl: warning: duplicate GROUP_OBJ entry ignored\n");
297 continue;
298 case SMB_ACL_MASK:
299 if (racl->mask_obj == NO_ENTRY)
300 racl->mask_obj = access;
301 else
302 rprintf(FINFO, "unpack_smb_acl: warning: duplicate MASK entry ignored\n");
303 continue;
304 case SMB_ACL_OTHER:
305 if (racl->other_obj == NO_ENTRY)
306 racl->other_obj = access;
307 else
308 rprintf(FINFO, "unpack_smb_acl: warning: duplicate OTHER entry ignored\n");
309 continue;
310 #endif
311 case SMB_ACL_USER:
312 access |= NAME_IS_USER;
313 break;
314 case SMB_ACL_GROUP:
315 break;
316 default:
317 rprintf(FINFO, "unpack_smb_acl: warning: entry with unrecognized tag type ignored\n");
318 continue;
320 ida = EXPAND_ITEM_LIST(&temp_ida_list, id_access, -10);
321 ida->id = g_u_id;
322 ida->access = access;
324 if (rc) {
325 rsyserr(FERROR_XFER, errno, "unpack_smb_acl: %s()", errfun);
326 rsync_acl_free(racl);
327 return False;
330 /* Transfer the count id_access items out of the temp_ida_list
331 * into the names ida_entries list in racl. */
332 if (temp_ida_list.count) {
333 #ifdef SMB_ACL_NEED_SORT
334 if (temp_ida_list.count > 1) {
335 qsort(temp_ida_list.items, temp_ida_list.count, sizeof (id_access), id_access_sorter);
337 #endif
338 if (!(racl->names.idas = new_array(id_access, temp_ida_list.count)))
339 out_of_memory("unpack_smb_acl");
340 memcpy(racl->names.idas, temp_ida_list.items, temp_ida_list.count * sizeof (id_access));
341 } else
342 racl->names.idas = NULL;
344 racl->names.count = temp_ida_list.count;
346 /* Truncate the temporary list now that its idas have been saved. */
347 temp_ida_list.count = 0;
349 return True;
352 /* Synactic sugar for system calls */
354 #define CALL_OR_ERROR(func,args,str) \
355 do { \
356 if (func args) { \
357 errfun = str; \
358 goto error_exit; \
360 } while (0)
362 #define COE(func,args) CALL_OR_ERROR(func,args,#func)
363 #define COE2(func,args) CALL_OR_ERROR(func,args,NULL)
365 #ifndef HAVE_OSX_ACLS
366 /* Store the permissions in the system ACL entry. */
367 static int store_access_in_entry(uint32 access, SMB_ACL_ENTRY_T entry)
369 if (sys_acl_set_access_bits(entry, access)) {
370 rsyserr(FERROR_XFER, errno, "store_access_in_entry sys_acl_set_access_bits()");
371 return -1;
373 return 0;
375 #endif
377 /* Pack rsync ACL -> system ACL verbatim. Return whether we succeeded. */
378 static BOOL pack_smb_acl(SMB_ACL_T *smb_acl, const rsync_acl *racl)
380 #ifdef ACLS_NEED_MASK
381 uchar mask_bits;
382 #endif
383 size_t count;
384 id_access *ida;
385 const char *errfun = NULL;
386 SMB_ACL_ENTRY_T entry;
388 if (!(*smb_acl = sys_acl_init(calc_sacl_entries(racl)))) {
389 rsyserr(FERROR_XFER, errno, "pack_smb_acl: sys_acl_init()");
390 return False;
393 #ifndef HAVE_OSX_ACLS
394 COE( sys_acl_create_entry,(smb_acl, &entry) );
395 COE( sys_acl_set_info,(entry, SMB_ACL_USER_OBJ, racl->user_obj & ~NO_ENTRY, 0) );
396 #endif
398 for (ida = racl->names.idas, count = racl->names.count; count; ida++, count--) {
399 #ifdef SMB_ACL_NEED_SORT
400 if (!(ida->access & NAME_IS_USER))
401 break;
402 #endif
403 COE( sys_acl_create_entry,(smb_acl, &entry) );
404 COE( sys_acl_set_info,
405 (entry,
406 ida->access & NAME_IS_USER ? SMB_ACL_USER : SMB_ACL_GROUP,
407 ida->access & ~NAME_IS_USER, ida->id) );
410 #ifndef HAVE_OSX_ACLS
411 COE( sys_acl_create_entry,(smb_acl, &entry) );
412 COE( sys_acl_set_info,(entry, SMB_ACL_GROUP_OBJ, racl->group_obj & ~NO_ENTRY, 0) );
414 #ifdef SMB_ACL_NEED_SORT
415 for ( ; count; ida++, count--) {
416 COE( sys_acl_create_entry,(smb_acl, &entry) );
417 COE( sys_acl_set_info,(entry, SMB_ACL_GROUP, ida->access, ida->id) );
419 #endif
421 #ifdef ACLS_NEED_MASK
422 mask_bits = racl->mask_obj == NO_ENTRY ? racl->group_obj & ~NO_ENTRY : racl->mask_obj;
423 COE( sys_acl_create_entry,(smb_acl, &entry) );
424 COE( sys_acl_set_info,(entry, SMB_ACL_MASK, mask_bits, 0) );
425 #else
426 if (racl->mask_obj != NO_ENTRY) {
427 COE( sys_acl_create_entry,(smb_acl, &entry) );
428 COE( sys_acl_set_info,(entry, SMB_ACL_MASK, racl->mask_obj, 0) );
430 #endif
432 COE( sys_acl_create_entry,(smb_acl, &entry) );
433 COE( sys_acl_set_info,(entry, SMB_ACL_OTHER, racl->other_obj & ~NO_ENTRY, 0) );
434 #endif
436 #ifdef DEBUG
437 if (sys_acl_valid(*smb_acl) < 0)
438 rprintf(FERROR_XFER, "pack_smb_acl: warning: system says the ACL I packed is invalid\n");
439 #endif
441 return True;
443 error_exit:
444 if (errfun) {
445 rsyserr(FERROR_XFER, errno, "pack_smb_acl %s()", errfun);
447 sys_acl_free_acl(*smb_acl);
448 return False;
451 static int find_matching_rsync_acl(const rsync_acl *racl, SMB_ACL_TYPE_T type,
452 const item_list *racl_list)
454 static int access_match = -1, default_match = -1;
455 int *match = type == SMB_ACL_TYPE_ACCESS ? &access_match : &default_match;
456 size_t count = racl_list->count;
458 /* If this is the first time through or we didn't match the last
459 * time, then start at the end of the list, which should be the
460 * best place to start hunting. */
461 if (*match == -1)
462 *match = racl_list->count - 1;
463 while (count--) {
464 rsync_acl *base = racl_list->items;
465 if (rsync_acl_equal(base + *match, racl))
466 return *match;
467 if (!(*match)--)
468 *match = racl_list->count - 1;
471 *match = -1;
472 return *match;
475 static int get_rsync_acl(const char *fname, rsync_acl *racl,
476 SMB_ACL_TYPE_T type, mode_t mode)
478 SMB_ACL_T sacl;
480 #ifdef SUPPORT_XATTRS
481 /* --fake-super support: load ACLs from an xattr. */
482 if (am_root < 0) {
483 char *buf;
484 size_t len;
485 int cnt;
487 if ((buf = get_xattr_acl(fname, type == SMB_ACL_TYPE_ACCESS, &len)) == NULL)
488 return 0;
489 cnt = (len - 4*4) / (4+4);
490 if (len < 4*4 || len != (size_t)cnt*(4+4) + 4*4) {
491 free(buf);
492 return -1;
495 racl->user_obj = IVAL(buf, 0);
496 if (racl->user_obj == NO_ENTRY)
497 racl->user_obj = (mode >> 6) & 7;
498 racl->group_obj = IVAL(buf, 4);
499 if (racl->group_obj == NO_ENTRY)
500 racl->group_obj = (mode >> 3) & 7;
501 racl->mask_obj = IVAL(buf, 8);
502 racl->other_obj = IVAL(buf, 12);
503 if (racl->other_obj == NO_ENTRY)
504 racl->other_obj = mode & 7;
506 if (cnt) {
507 char *bp = buf + 4*4;
508 id_access *ida;
509 if (!(ida = racl->names.idas = new_array(id_access, cnt)))
510 out_of_memory("get_rsync_acl");
511 racl->names.count = cnt;
512 for ( ; cnt--; ida++, bp += 4+4) {
513 ida->id = IVAL(bp, 0);
514 ida->access = IVAL(bp, 4);
517 free(buf);
518 return 0;
520 #endif
522 if ((sacl = sys_acl_get_file(fname, type)) != 0) {
523 BOOL ok = unpack_smb_acl(sacl, racl);
525 sys_acl_free_acl(sacl);
526 if (!ok) {
527 return -1;
529 } else if (no_acl_syscall_error(errno)) {
530 /* ACLs are not supported, so pretend we have a basic ACL. */
531 if (type == SMB_ACL_TYPE_ACCESS)
532 rsync_acl_fake_perms(racl, mode);
533 } else {
534 rsyserr(FERROR_XFER, errno, "get_acl: sys_acl_get_file(%s, %s)",
535 fname, str_acl_type(type));
536 return -1;
539 return 0;
542 /* Return the Access Control List for the given filename. */
543 int get_acl(const char *fname, stat_x *sxp)
545 sxp->acc_acl = create_racl();
547 if (S_ISREG(sxp->st.st_mode) || S_ISDIR(sxp->st.st_mode)) {
548 /* Everyone supports this. */
549 } else if (S_ISLNK(sxp->st.st_mode)) {
550 return 0;
551 } else if (IS_SPECIAL(sxp->st.st_mode)) {
552 #ifndef NO_SPECIAL_ACLS
553 if (!preserve_specials)
554 #endif
555 return 0;
556 } else if (IS_DEVICE(sxp->st.st_mode)) {
557 #ifndef NO_DEVICE_ACLS
558 if (!preserve_devices)
559 #endif
560 return 0;
561 } else if (IS_MISSING_FILE(sxp->st))
562 return 0;
564 if (get_rsync_acl(fname, sxp->acc_acl, SMB_ACL_TYPE_ACCESS,
565 sxp->st.st_mode) < 0) {
566 free_acl(sxp);
567 return -1;
570 if (S_ISDIR(sxp->st.st_mode)) {
571 sxp->def_acl = create_racl();
572 if (get_rsync_acl(fname, sxp->def_acl, SMB_ACL_TYPE_DEFAULT,
573 sxp->st.st_mode) < 0) {
574 free_acl(sxp);
575 return -1;
579 return 0;
582 /* === Send functions === */
584 /* Send the ida list over the file descriptor. */
585 static void send_ida_entries(int f, const ida_entries *idal)
587 id_access *ida;
588 size_t count = idal->count;
590 write_varint(f, idal->count);
592 for (ida = idal->idas; count--; ida++) {
593 uint32 xbits = ida->access << 2;
594 const char *name;
595 if (ida->access & NAME_IS_USER) {
596 xbits |= XFLAG_NAME_IS_USER;
597 name = numeric_ids ? NULL : add_uid(ida->id);
598 } else
599 name = numeric_ids ? NULL : add_gid(ida->id);
600 write_varint(f, ida->id);
601 if (inc_recurse && name) {
602 int len = strlen(name);
603 write_varint(f, xbits | XFLAG_NAME_FOLLOWS);
604 write_byte(f, len);
605 write_buf(f, name, len);
606 } else
607 write_varint(f, xbits);
611 static void send_rsync_acl(int f, rsync_acl *racl, SMB_ACL_TYPE_T type,
612 item_list *racl_list)
614 int ndx = find_matching_rsync_acl(racl, type, racl_list);
616 /* Send 0 (-1 + 1) to indicate that literal ACL data follows. */
617 write_varint(f, ndx + 1);
619 if (ndx < 0) {
620 rsync_acl *new_racl = EXPAND_ITEM_LIST(racl_list, rsync_acl, 1000);
621 uchar flags = 0;
623 if (racl->user_obj != NO_ENTRY)
624 flags |= XMIT_USER_OBJ;
625 if (racl->group_obj != NO_ENTRY)
626 flags |= XMIT_GROUP_OBJ;
627 if (racl->mask_obj != NO_ENTRY)
628 flags |= XMIT_MASK_OBJ;
629 if (racl->other_obj != NO_ENTRY)
630 flags |= XMIT_OTHER_OBJ;
631 if (racl->names.count)
632 flags |= XMIT_NAME_LIST;
634 write_byte(f, flags);
636 if (flags & XMIT_USER_OBJ)
637 write_varint(f, racl->user_obj);
638 if (flags & XMIT_GROUP_OBJ)
639 write_varint(f, racl->group_obj);
640 if (flags & XMIT_MASK_OBJ)
641 write_varint(f, racl->mask_obj);
642 if (flags & XMIT_OTHER_OBJ)
643 write_varint(f, racl->other_obj);
644 if (flags & XMIT_NAME_LIST)
645 send_ida_entries(f, &racl->names);
647 /* Give the allocated data to the new list object. */
648 *new_racl = *racl;
649 *racl = empty_rsync_acl;
653 /* Send the ACL from the stat_x structure down the indicated file descriptor.
654 * This also frees the ACL data. */
655 void send_acl(int f, stat_x *sxp)
657 if (!sxp->acc_acl) {
658 sxp->acc_acl = create_racl();
659 rsync_acl_fake_perms(sxp->acc_acl, sxp->st.st_mode);
661 /* Avoid sending values that can be inferred from other data. */
662 rsync_acl_strip_perms(sxp);
664 send_rsync_acl(f, sxp->acc_acl, SMB_ACL_TYPE_ACCESS, &access_acl_list);
666 if (S_ISDIR(sxp->st.st_mode)) {
667 if (!sxp->def_acl)
668 sxp->def_acl = create_racl();
670 send_rsync_acl(f, sxp->def_acl, SMB_ACL_TYPE_DEFAULT, &default_acl_list);
674 /* === Receive functions === */
676 static uint32 recv_acl_access(int f, uchar *name_follows_ptr)
678 uint32 access = read_varint(f);
680 if (name_follows_ptr) {
681 int flags = access & 3;
682 access >>= 2;
683 if (am_root >= 0 && access & ~SMB_ACL_VALID_NAME_BITS)
684 goto value_error;
685 if (flags & XFLAG_NAME_FOLLOWS)
686 *name_follows_ptr = 1;
687 else
688 *name_follows_ptr = 0;
689 if (flags & XFLAG_NAME_IS_USER)
690 access |= NAME_IS_USER;
691 } else if (am_root >= 0 && access & ~SMB_ACL_VALID_OBJ_BITS) {
692 value_error:
693 rprintf(FERROR_XFER, "recv_acl_access: value out of range: %x\n",
694 access);
695 exit_cleanup(RERR_STREAMIO);
698 return access;
701 static uchar recv_ida_entries(int f, ida_entries *ent)
703 uchar computed_mask_bits = 0;
704 int i, count = read_varint(f);
706 if (count) {
707 if (!(ent->idas = new_array(id_access, count)))
708 out_of_memory("recv_ida_entries");
709 } else
710 ent->idas = NULL;
712 ent->count = count;
714 for (i = 0; i < count; i++) {
715 uchar has_name;
716 id_t id = read_varint(f);
717 uint32 access = recv_acl_access(f, &has_name);
719 if (has_name) {
720 if (access & NAME_IS_USER)
721 id = recv_user_name(f, id);
722 else
723 id = recv_group_name(f, id, NULL);
724 } else if (access & NAME_IS_USER) {
725 if (inc_recurse && am_root && !numeric_ids)
726 id = match_uid(id);
727 } else {
728 if (inc_recurse && (!am_root || !numeric_ids))
729 id = match_gid(id, NULL);
732 ent->idas[i].id = id;
733 ent->idas[i].access = access;
734 computed_mask_bits |= access;
737 return computed_mask_bits & ~NO_ENTRY;
740 static int recv_rsync_acl(int f, item_list *racl_list, SMB_ACL_TYPE_T type, mode_t mode)
742 uchar computed_mask_bits = 0;
743 acl_duo *duo_item;
744 uchar flags;
745 int ndx = read_varint(f);
747 if (ndx < 0 || (size_t)ndx > racl_list->count) {
748 rprintf(FERROR_XFER, "recv_acl_index: %s ACL index %d > %d\n",
749 str_acl_type(type), ndx, (int)racl_list->count);
750 exit_cleanup(RERR_STREAMIO);
753 if (ndx != 0)
754 return ndx - 1;
756 ndx = racl_list->count;
757 duo_item = EXPAND_ITEM_LIST(racl_list, acl_duo, 1000);
758 duo_item->racl = empty_rsync_acl;
760 flags = read_byte(f);
762 if (flags & XMIT_USER_OBJ)
763 duo_item->racl.user_obj = recv_acl_access(f, NULL);
764 if (flags & XMIT_GROUP_OBJ)
765 duo_item->racl.group_obj = recv_acl_access(f, NULL);
766 if (flags & XMIT_MASK_OBJ)
767 duo_item->racl.mask_obj = recv_acl_access(f, NULL);
768 if (flags & XMIT_OTHER_OBJ)
769 duo_item->racl.other_obj = recv_acl_access(f, NULL);
770 if (flags & XMIT_NAME_LIST)
771 computed_mask_bits |= recv_ida_entries(f, &duo_item->racl.names);
773 #ifdef HAVE_OSX_ACLS
774 /* If we received a superfluous mask, throw it away. */
775 duo_item->racl.mask_obj = NO_ENTRY;
776 #else
777 if (duo_item->racl.names.count && duo_item->racl.mask_obj == NO_ENTRY) {
778 /* Mask must be non-empty with lists. */
779 if (type == SMB_ACL_TYPE_ACCESS)
780 computed_mask_bits = (mode >> 3) & 7;
781 else
782 computed_mask_bits |= duo_item->racl.group_obj & ~NO_ENTRY;
783 duo_item->racl.mask_obj = computed_mask_bits;
785 #endif
787 duo_item->sacl = NULL;
789 return ndx;
792 /* Receive the ACL info the sender has included for this file-list entry. */
793 void receive_acl(int f, struct file_struct *file)
795 F_ACL(file) = recv_rsync_acl(f, &access_acl_list, SMB_ACL_TYPE_ACCESS, file->mode);
797 if (S_ISDIR(file->mode))
798 F_DIR_DEFACL(file) = recv_rsync_acl(f, &default_acl_list, SMB_ACL_TYPE_DEFAULT, 0);
801 static int cache_rsync_acl(rsync_acl *racl, SMB_ACL_TYPE_T type, item_list *racl_list)
803 int ndx;
805 if (!racl)
806 ndx = -1;
807 else if ((ndx = find_matching_rsync_acl(racl, type, racl_list)) == -1) {
808 acl_duo *new_duo;
809 ndx = racl_list->count;
810 new_duo = EXPAND_ITEM_LIST(racl_list, acl_duo, 1000);
811 new_duo->racl = *racl;
812 new_duo->sacl = NULL;
813 *racl = empty_rsync_acl;
816 return ndx;
819 /* Turn the ACL data in stat_x into cached ACL data, setting the index
820 * values in the file struct. */
821 void cache_tmp_acl(struct file_struct *file, stat_x *sxp)
823 if (prior_access_count == (size_t)-1)
824 prior_access_count = access_acl_list.count;
826 F_ACL(file) = cache_rsync_acl(sxp->acc_acl, SMB_ACL_TYPE_ACCESS, &access_acl_list);
828 if (S_ISDIR(sxp->st.st_mode)) {
829 if (prior_default_count == (size_t)-1)
830 prior_default_count = default_acl_list.count;
831 F_DIR_DEFACL(file) = cache_rsync_acl(sxp->def_acl, SMB_ACL_TYPE_DEFAULT, &default_acl_list);
835 static void uncache_duo_acls(item_list *duo_list, size_t start)
837 acl_duo *duo_item = duo_list->items;
838 acl_duo *duo_start = duo_item + start;
840 duo_item += duo_list->count;
841 duo_list->count = start;
843 while (duo_item-- > duo_start) {
844 rsync_acl_free(&duo_item->racl);
845 if (duo_item->sacl)
846 sys_acl_free_acl(duo_item->sacl);
850 void uncache_tmp_acls(void)
852 if (prior_access_count != (size_t)-1) {
853 uncache_duo_acls(&access_acl_list, prior_access_count);
854 prior_access_count = (size_t)-1;
857 if (prior_default_count != (size_t)-1) {
858 uncache_duo_acls(&default_acl_list, prior_default_count);
859 prior_default_count = (size_t)-1;
863 #ifndef HAVE_OSX_ACLS
864 static mode_t change_sacl_perms(SMB_ACL_T sacl, rsync_acl *racl, mode_t old_mode, mode_t mode)
866 SMB_ACL_ENTRY_T entry;
867 const char *errfun;
868 int rc;
870 if (S_ISDIR(mode)) {
871 /* If the sticky bit is going on, it's not safe to allow all
872 * the new ACL to go into effect before it gets set. */
873 #ifdef SMB_ACL_LOSES_SPECIAL_MODE_BITS
874 if (mode & S_ISVTX)
875 mode &= ~0077;
876 #else
877 if (mode & S_ISVTX && !(old_mode & S_ISVTX))
878 mode &= ~0077;
879 } else {
880 /* If setuid or setgid is going off, it's not safe to allow all
881 * the new ACL to go into effect before they get cleared. */
882 if ((old_mode & S_ISUID && !(mode & S_ISUID))
883 || (old_mode & S_ISGID && !(mode & S_ISGID)))
884 mode &= ~0077;
885 #endif
888 errfun = "sys_acl_get_entry";
889 for (rc = sys_acl_get_entry(sacl, SMB_ACL_FIRST_ENTRY, &entry);
890 rc == 1;
891 rc = sys_acl_get_entry(sacl, SMB_ACL_NEXT_ENTRY, &entry)) {
892 SMB_ACL_TAG_T tag_type;
893 if ((rc = sys_acl_get_tag_type(entry, &tag_type)) != 0) {
894 errfun = "sys_acl_get_tag_type";
895 break;
897 switch (tag_type) {
898 case SMB_ACL_USER_OBJ:
899 COE2( store_access_in_entry,((mode >> 6) & 7, entry) );
900 break;
901 case SMB_ACL_GROUP_OBJ:
902 /* group is only empty when identical to group perms. */
903 if (racl->group_obj != NO_ENTRY)
904 break;
905 COE2( store_access_in_entry,((mode >> 3) & 7, entry) );
906 break;
907 case SMB_ACL_MASK:
908 #ifndef HAVE_SOLARIS_ACLS
909 #ifndef ACLS_NEED_MASK
910 /* mask is only empty when we don't need it. */
911 if (racl->mask_obj == NO_ENTRY)
912 break;
913 #endif
914 COE2( store_access_in_entry,((mode >> 3) & 7, entry) );
915 #endif
916 break;
917 case SMB_ACL_OTHER:
918 COE2( store_access_in_entry,(mode & 7, entry) );
919 break;
922 if (rc) {
923 error_exit:
924 if (errfun) {
925 rsyserr(FERROR_XFER, errno, "change_sacl_perms: %s()",
926 errfun);
928 return (mode_t)-1;
931 #ifdef SMB_ACL_LOSES_SPECIAL_MODE_BITS
932 /* Ensure that chmod() will be called to restore any lost setid bits. */
933 if (old_mode & (S_ISUID | S_ISGID | S_ISVTX)
934 && BITS_EQUAL(old_mode, mode, CHMOD_BITS))
935 old_mode &= ~(S_ISUID | S_ISGID | S_ISVTX);
936 #endif
938 /* Return the mode of the file on disk, as we will set them. */
939 return (old_mode & ~ACCESSPERMS) | (mode & ACCESSPERMS);
941 #endif
943 static int set_rsync_acl(const char *fname, acl_duo *duo_item,
944 SMB_ACL_TYPE_T type, stat_x *sxp, mode_t mode)
946 if (type == SMB_ACL_TYPE_DEFAULT
947 && duo_item->racl.user_obj == NO_ENTRY) {
948 int rc;
949 #ifdef SUPPORT_XATTRS
950 /* --fake-super support: delete default ACL from xattrs. */
951 if (am_root < 0)
952 rc = del_def_xattr_acl(fname);
953 else
954 #endif
955 rc = sys_acl_delete_def_file(fname);
956 if (rc < 0) {
957 rsyserr(FERROR_XFER, errno, "set_acl: sys_acl_delete_def_file(%s)",
958 fname);
959 return -1;
961 #ifdef SUPPORT_XATTRS
962 } else if (am_root < 0) {
963 /* --fake-super support: store ACLs in an xattr. */
964 int cnt = duo_item->racl.names.count;
965 size_t len = 4*4 + cnt * (4+4);
966 char *buf = new_array(char, len);
967 int rc;
969 SIVAL(buf, 0, duo_item->racl.user_obj);
970 SIVAL(buf, 4, duo_item->racl.group_obj);
971 SIVAL(buf, 8, duo_item->racl.mask_obj);
972 SIVAL(buf, 12, duo_item->racl.other_obj);
974 if (cnt) {
975 char *bp = buf + 4*4;
976 id_access *ida = duo_item->racl.names.idas;
977 for ( ; cnt--; ida++, bp += 4+4) {
978 SIVAL(bp, 0, ida->id);
979 SIVAL(bp, 4, ida->access);
982 rc = set_xattr_acl(fname, type == SMB_ACL_TYPE_ACCESS, buf, len);
983 free(buf);
984 return rc;
985 #endif
986 } else {
987 mode_t cur_mode = sxp->st.st_mode;
988 if (!duo_item->sacl
989 && !pack_smb_acl(&duo_item->sacl, &duo_item->racl))
990 return -1;
991 #ifdef HAVE_OSX_ACLS
992 mode = 0; /* eliminate compiler warning */
993 #else
994 if (type == SMB_ACL_TYPE_ACCESS) {
995 cur_mode = change_sacl_perms(duo_item->sacl, &duo_item->racl, cur_mode, mode);
996 if (cur_mode == (mode_t)-1)
997 return 0;
999 #endif
1000 if (sys_acl_set_file(fname, type, duo_item->sacl) < 0) {
1001 rsyserr(FERROR_XFER, errno, "set_acl: sys_acl_set_file(%s, %s)",
1002 fname, str_acl_type(type));
1003 return -1;
1005 if (type == SMB_ACL_TYPE_ACCESS)
1006 sxp->st.st_mode = cur_mode;
1009 return 0;
1012 /* Given a fname, this sets extended access ACL entries, the default ACL (for a
1013 * dir), and the regular mode bits on the file. Call this with fname set to
1014 * NULL to just check if the ACL is different.
1016 * If the ACL operation has a side-effect of changing the file's mode, the
1017 * sxp->st.st_mode value will be changed to match.
1019 * Returns 0 for an unchanged ACL, 1 for changed, -1 for failed. */
1020 int set_acl(const char *fname, const struct file_struct *file, stat_x *sxp, mode_t new_mode)
1022 int changed = 0;
1023 int32 ndx;
1024 BOOL eq;
1026 if (!dry_run && (read_only || list_only)) {
1027 errno = EROFS;
1028 return -1;
1031 ndx = F_ACL(file);
1032 if (ndx >= 0 && (size_t)ndx < access_acl_list.count) {
1033 acl_duo *duo_item = access_acl_list.items;
1034 duo_item += ndx;
1035 eq = sxp->acc_acl
1036 && rsync_acl_equal_enough(sxp->acc_acl, &duo_item->racl, new_mode);
1037 if (!eq) {
1038 changed = 1;
1039 if (!dry_run && fname
1040 && set_rsync_acl(fname, duo_item, SMB_ACL_TYPE_ACCESS,
1041 sxp, new_mode) < 0)
1042 return -1;
1046 if (!S_ISDIR(new_mode))
1047 return changed;
1049 ndx = F_DIR_DEFACL(file);
1050 if (ndx >= 0 && (size_t)ndx < default_acl_list.count) {
1051 acl_duo *duo_item = default_acl_list.items;
1052 duo_item += ndx;
1053 eq = sxp->def_acl && rsync_acl_equal(sxp->def_acl, &duo_item->racl);
1054 if (!eq) {
1055 changed = 1;
1056 if (!dry_run && fname
1057 && set_rsync_acl(fname, duo_item, SMB_ACL_TYPE_DEFAULT,
1058 sxp, new_mode) < 0)
1059 return -1;
1063 return changed;
1066 /* Non-incremental recursion needs to convert all the received IDs.
1067 * This is done in a single pass after receiving the whole file-list. */
1068 static void match_racl_ids(const item_list *racl_list)
1070 int list_cnt, name_cnt;
1071 acl_duo *duo_item = racl_list->items;
1072 for (list_cnt = racl_list->count; list_cnt--; duo_item++) {
1073 ida_entries *idal = &duo_item->racl.names;
1074 id_access *ida = idal->idas;
1075 for (name_cnt = idal->count; name_cnt--; ida++) {
1076 if (ida->access & NAME_IS_USER)
1077 ida->id = match_uid(ida->id);
1078 else
1079 ida->id = match_gid(ida->id, NULL);
1084 void match_acl_ids(void)
1086 match_racl_ids(&access_acl_list);
1087 match_racl_ids(&default_acl_list);
1090 /* This is used by dest_mode(). */
1091 int default_perms_for_dir(const char *dir)
1093 rsync_acl racl;
1094 SMB_ACL_T sacl;
1095 BOOL ok;
1096 int perms;
1098 if (dir == NULL)
1099 dir = ".";
1100 perms = ACCESSPERMS & ~orig_umask;
1101 /* Read the directory's default ACL. If it has none, this will successfully return an empty ACL. */
1102 sacl = sys_acl_get_file(dir, SMB_ACL_TYPE_DEFAULT);
1103 if (sacl == NULL) {
1104 /* Couldn't get an ACL. Darn. */
1105 switch (errno) {
1106 case EINVAL:
1107 /* If SMB_ACL_TYPE_DEFAULT isn't valid, then the ACLs must be non-POSIX. */
1108 break;
1109 #ifdef ENOTSUP
1110 case ENOTSUP:
1111 #endif
1112 case ENOSYS:
1113 /* No ACLs are available. */
1114 break;
1115 default:
1116 if (dry_run && errno == ENOENT) {
1117 /* We're doing a dry run, so the containing directory
1118 * wasn't actually created. Don't worry about it. */
1119 break;
1121 rprintf(FWARNING,
1122 "default_perms_for_dir: sys_acl_get_file(%s, %s): %s, falling back on umask\n",
1123 dir, str_acl_type(SMB_ACL_TYPE_DEFAULT), strerror(errno));
1125 return perms;
1128 /* Convert it. */
1129 racl = empty_rsync_acl;
1130 ok = unpack_smb_acl(sacl, &racl);
1131 sys_acl_free_acl(sacl);
1132 if (!ok) {
1133 rprintf(FWARNING, "default_perms_for_dir: unpack_smb_acl failed, falling back on umask\n");
1134 return perms;
1137 /* Apply the permission-bit entries of the default ACL, if any. */
1138 if (racl.user_obj != NO_ENTRY) {
1139 perms = rsync_acl_get_perms(&racl);
1140 if (DEBUG_GTE(ACL, 1))
1141 rprintf(FINFO, "got ACL-based default perms %o for directory %s\n", perms, dir);
1144 rsync_acl_free(&racl);
1145 return perms;
1148 #endif /* SUPPORT_ACLS */