Preparing for release of 3.0.1pre2
[rsync.git] / acls.c
blobd9023e2f4bf68bbad1915ae0d0fb2ccc73f80d56
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-2008 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;
35 /* Flags used to indicate what items are being transmitted for an entry. */
36 #define XMIT_USER_OBJ (1<<0)
37 #define XMIT_GROUP_OBJ (1<<1)
38 #define XMIT_MASK_OBJ (1<<2)
39 #define XMIT_OTHER_OBJ (1<<3)
40 #define XMIT_NAME_LIST (1<<4)
42 #define NO_ENTRY ((uchar)0x80) /* Default value of a NON-name-list entry. */
44 #define NAME_IS_USER (1u<<31) /* Bit used only on a name-list entry. */
46 /* When we send the access bits over the wire, we shift them 2 bits to the
47 * left and use the lower 2 bits as flags (relevant only to a name entry).
48 * This makes the protocol more efficient than sending a value that would
49 * be likely to have its hightest bits set. */
50 #define XFLAG_NAME_FOLLOWS 0x0001u
51 #define XFLAG_NAME_IS_USER 0x0002u
53 /* === ACL structures === */
55 typedef struct {
56 id_t id;
57 uint32 access;
58 } id_access;
60 typedef struct {
61 id_access *idas;
62 int count;
63 } ida_entries;
65 typedef struct {
66 char *name;
67 uchar len;
68 } idname;
70 typedef struct rsync_acl {
71 ida_entries names;
72 /* These will be NO_ENTRY if there's no such entry. */
73 uchar user_obj;
74 uchar group_obj;
75 uchar mask_obj;
76 uchar other_obj;
77 } rsync_acl;
79 typedef struct {
80 rsync_acl racl;
81 SMB_ACL_T sacl;
82 } acl_duo;
84 static const rsync_acl empty_rsync_acl = {
85 {NULL, 0}, NO_ENTRY, NO_ENTRY, NO_ENTRY, NO_ENTRY
88 static item_list access_acl_list = EMPTY_ITEM_LIST;
89 static item_list default_acl_list = EMPTY_ITEM_LIST;
91 /* === Calculations on ACL types === */
93 static const char *str_acl_type(SMB_ACL_TYPE_T type)
95 switch (type) {
96 case SMB_ACL_TYPE_ACCESS:
97 #ifdef HAVE_OSX_ACLS
98 return "ACL_TYPE_EXTENDED";
99 #else
100 return "ACL_TYPE_ACCESS";
101 #endif
102 case SMB_ACL_TYPE_DEFAULT:
103 return "ACL_TYPE_DEFAULT";
104 default:
105 break;
107 return "unknown ACL type!";
110 static int calc_sacl_entries(const rsync_acl *racl)
112 /* A System ACL always gets user/group/other permission entries. */
113 return racl->names.count
114 #ifdef ACLS_NEED_MASK
115 + 4;
116 #else
117 + (racl->mask_obj != NO_ENTRY) + 3;
118 #endif
121 /* Extracts and returns the permission bits from the ACL. This cannot be
122 * called on an rsync_acl that has NO_ENTRY in any spot but the mask. */
123 static int rsync_acl_get_perms(const rsync_acl *racl)
125 return (racl->user_obj << 6)
126 + ((racl->mask_obj != NO_ENTRY ? racl->mask_obj : racl->group_obj) << 3)
127 + racl->other_obj;
130 /* Removes the permission-bit entries from the ACL because these
131 * can be reconstructed from the file's mode. */
132 static void rsync_acl_strip_perms(rsync_acl *racl)
134 racl->user_obj = NO_ENTRY;
135 if (racl->mask_obj == NO_ENTRY)
136 racl->group_obj = NO_ENTRY;
137 else {
138 if (racl->group_obj == racl->mask_obj)
139 racl->group_obj = NO_ENTRY;
140 racl->mask_obj = NO_ENTRY;
142 racl->other_obj = NO_ENTRY;
145 /* Given an empty rsync_acl, fake up the permission bits. */
146 static void rsync_acl_fake_perms(rsync_acl *racl, mode_t mode)
148 racl->user_obj = (mode >> 6) & 7;
149 racl->group_obj = (mode >> 3) & 7;
150 racl->other_obj = mode & 7;
153 /* === Rsync ACL functions === */
155 static rsync_acl *create_racl(void)
157 rsync_acl *racl = new(rsync_acl);
159 if (!racl)
160 out_of_memory("create_racl");
161 *racl = empty_rsync_acl;
163 return racl;
166 static BOOL ida_entries_equal(const ida_entries *ial1, const ida_entries *ial2)
168 id_access *ida1, *ida2;
169 int count = ial1->count;
170 if (count != ial2->count)
171 return False;
172 ida1 = ial1->idas;
173 ida2 = ial2->idas;
174 for (; count--; ida1++, ida2++) {
175 if (ida1->access != ida2->access || ida1->id != ida2->id)
176 return False;
178 return True;
181 static BOOL rsync_acl_equal(const rsync_acl *racl1, const rsync_acl *racl2)
183 return racl1->user_obj == racl2->user_obj
184 && racl1->group_obj == racl2->group_obj
185 && racl1->mask_obj == racl2->mask_obj
186 && racl1->other_obj == racl2->other_obj
187 && ida_entries_equal(&racl1->names, &racl2->names);
190 /* Are the extended (non-permission-bit) entries equal? If so, the rest of
191 * the ACL will be handled by the normal mode-preservation code. This is
192 * only meaningful for access ACLs! Note: the 1st arg is a fully-populated
193 * rsync_acl, but the 2nd parameter can be a condensed rsync_acl, which means
194 * that it might have several of its permission objects set to NO_ENTRY. */
195 static BOOL rsync_acl_equal_enough(const rsync_acl *racl1,
196 const rsync_acl *racl2, mode_t m)
198 if ((racl1->mask_obj ^ racl2->mask_obj) & NO_ENTRY)
199 return False; /* One has a mask and the other doesn't */
201 /* When there's a mask, the group_obj becomes an extended entry. */
202 if (racl1->mask_obj != NO_ENTRY) {
203 /* A condensed rsync_acl with a mask can only have no
204 * group_obj when it was identical to the mask. This
205 * means that it was also identical to the group attrs
206 * from the mode. */
207 if (racl2->group_obj == NO_ENTRY) {
208 if (racl1->group_obj != ((m >> 3) & 7))
209 return False;
210 } else if (racl1->group_obj != racl2->group_obj)
211 return False;
213 return ida_entries_equal(&racl1->names, &racl2->names);
216 static void rsync_acl_free(rsync_acl *racl)
218 if (racl->names.idas)
219 free(racl->names.idas);
220 *racl = empty_rsync_acl;
223 void free_acl(stat_x *sxp)
225 if (sxp->acc_acl) {
226 rsync_acl_free(sxp->acc_acl);
227 free(sxp->acc_acl);
228 sxp->acc_acl = NULL;
230 if (sxp->def_acl) {
231 rsync_acl_free(sxp->def_acl);
232 free(sxp->def_acl);
233 sxp->def_acl = NULL;
237 #ifdef SMB_ACL_NEED_SORT
238 static int id_access_sorter(const void *r1, const void *r2)
240 id_access *ida1 = (id_access *)r1;
241 id_access *ida2 = (id_access *)r2;
242 id_t rid1 = ida1->id, rid2 = ida2->id;
243 if ((ida1->access ^ ida2->access) & NAME_IS_USER)
244 return ida1->access & NAME_IS_USER ? -1 : 1;
245 return rid1 == rid2 ? 0 : rid1 < rid2 ? -1 : 1;
247 #endif
249 /* === System ACLs === */
251 /* Unpack system ACL -> rsync ACL verbatim. Return whether we succeeded. */
252 static BOOL unpack_smb_acl(SMB_ACL_T sacl, rsync_acl *racl)
254 static item_list temp_ida_list = EMPTY_ITEM_LIST;
255 SMB_ACL_ENTRY_T entry;
256 const char *errfun;
257 int rc;
259 errfun = "sys_acl_get_entry";
260 for (rc = sys_acl_get_entry(sacl, SMB_ACL_FIRST_ENTRY, &entry);
261 rc == 1;
262 rc = sys_acl_get_entry(sacl, SMB_ACL_NEXT_ENTRY, &entry)) {
263 SMB_ACL_TAG_T tag_type;
264 uint32 access;
265 id_t g_u_id;
266 id_access *ida;
267 if ((rc = sys_acl_get_info(entry, &tag_type, &access, &g_u_id)) != 0) {
268 errfun = "sys_acl_get_info";
269 break;
271 /* continue == done with entry; break == store in temporary ida list */
272 switch (tag_type) {
273 #ifndef HAVE_OSX_ACLS
274 case SMB_ACL_USER_OBJ:
275 if (racl->user_obj == NO_ENTRY)
276 racl->user_obj = access;
277 else
278 rprintf(FINFO, "unpack_smb_acl: warning: duplicate USER_OBJ entry ignored\n");
279 continue;
280 case SMB_ACL_GROUP_OBJ:
281 if (racl->group_obj == NO_ENTRY)
282 racl->group_obj = access;
283 else
284 rprintf(FINFO, "unpack_smb_acl: warning: duplicate GROUP_OBJ entry ignored\n");
285 continue;
286 case SMB_ACL_MASK:
287 if (racl->mask_obj == NO_ENTRY)
288 racl->mask_obj = access;
289 else
290 rprintf(FINFO, "unpack_smb_acl: warning: duplicate MASK entry ignored\n");
291 continue;
292 case SMB_ACL_OTHER:
293 if (racl->other_obj == NO_ENTRY)
294 racl->other_obj = access;
295 else
296 rprintf(FINFO, "unpack_smb_acl: warning: duplicate OTHER entry ignored\n");
297 continue;
298 #endif
299 case SMB_ACL_USER:
300 access |= NAME_IS_USER;
301 break;
302 case SMB_ACL_GROUP:
303 break;
304 default:
305 rprintf(FINFO, "unpack_smb_acl: warning: entry with unrecognized tag type ignored\n");
306 continue;
308 ida = EXPAND_ITEM_LIST(&temp_ida_list, id_access, -10);
309 ida->id = g_u_id;
310 ida->access = access;
312 if (rc) {
313 rsyserr(FERROR_XFER, errno, "unpack_smb_acl: %s()", errfun);
314 rsync_acl_free(racl);
315 return False;
318 /* Transfer the count id_access items out of the temp_ida_list
319 * into the names ida_entries list in racl. */
320 if (temp_ida_list.count) {
321 #ifdef SMB_ACL_NEED_SORT
322 if (temp_ida_list.count > 1) {
323 qsort(temp_ida_list.items, temp_ida_list.count,
324 sizeof (id_access), id_access_sorter);
326 #endif
327 if (!(racl->names.idas = new_array(id_access, temp_ida_list.count)))
328 out_of_memory("unpack_smb_acl");
329 memcpy(racl->names.idas, temp_ida_list.items,
330 temp_ida_list.count * sizeof (id_access));
331 } else
332 racl->names.idas = NULL;
334 racl->names.count = temp_ida_list.count;
336 /* Truncate the temporary list now that its idas have been saved. */
337 temp_ida_list.count = 0;
339 #ifdef ACLS_NEED_MASK
340 if (!racl->names.count && racl->mask_obj != NO_ENTRY) {
341 /* Throw away a superfluous mask, but mask off the
342 * group perms with it first. */
343 racl->group_obj &= racl->mask_obj;
344 racl->mask_obj = NO_ENTRY;
346 #endif
348 return True;
351 /* Synactic sugar for system calls */
353 #define CALL_OR_ERROR(func,args,str) \
354 do { \
355 if (func args) { \
356 errfun = str; \
357 goto error_exit; \
359 } while (0)
361 #define COE(func,args) CALL_OR_ERROR(func,args,#func)
362 #define COE2(func,args) CALL_OR_ERROR(func,args,NULL)
364 #ifndef HAVE_OSX_ACLS
365 /* Store the permissions in the system ACL entry. */
366 static int store_access_in_entry(uint32 access, SMB_ACL_ENTRY_T entry)
368 if (sys_acl_set_access_bits(entry, access)) {
369 rsyserr(FERROR_XFER, errno, "store_access_in_entry sys_acl_set_access_bits()");
370 return -1;
372 return 0;
374 #endif
376 /* Pack rsync ACL -> system ACL verbatim. Return whether we succeeded. */
377 static BOOL pack_smb_acl(SMB_ACL_T *smb_acl, const rsync_acl *racl)
379 #ifdef ACLS_NEED_MASK
380 uchar mask_bits;
381 #endif
382 size_t count;
383 id_access *ida;
384 const char *errfun = NULL;
385 SMB_ACL_ENTRY_T entry;
387 if (!(*smb_acl = sys_acl_init(calc_sacl_entries(racl)))) {
388 rsyserr(FERROR_XFER, errno, "pack_smb_acl: sys_acl_init()");
389 return False;
392 #ifndef HAVE_OSX_ACLS
393 COE( sys_acl_create_entry,(smb_acl, &entry) );
394 COE( sys_acl_set_info,(entry, SMB_ACL_USER_OBJ, racl->user_obj & ~NO_ENTRY, 0) );
395 #endif
397 for (ida = racl->names.idas, count = racl->names.count; count; ida++, count--) {
398 #ifdef SMB_ACL_NEED_SORT
399 if (!(ida->access & NAME_IS_USER))
400 break;
401 #endif
402 COE( sys_acl_create_entry,(smb_acl, &entry) );
403 COE( sys_acl_set_info,
404 (entry,
405 ida->access & NAME_IS_USER ? SMB_ACL_USER : SMB_ACL_GROUP,
406 ida->access & ~NAME_IS_USER, ida->id) );
409 #ifndef HAVE_OSX_ACLS
410 COE( sys_acl_create_entry,(smb_acl, &entry) );
411 COE( sys_acl_set_info,(entry, SMB_ACL_GROUP_OBJ, racl->group_obj & ~NO_ENTRY, 0) );
413 #ifdef SMB_ACL_NEED_SORT
414 for ( ; count; ida++, count--) {
415 COE( sys_acl_create_entry,(smb_acl, &entry) );
416 COE( sys_acl_set_info,(entry, SMB_ACL_GROUP, ida->access, ida->id) );
418 #endif
420 #ifdef ACLS_NEED_MASK
421 mask_bits = racl->mask_obj == NO_ENTRY ? racl->group_obj & ~NO_ENTRY : racl->mask_obj;
422 COE( sys_acl_create_entry,(smb_acl, &entry) );
423 COE( sys_acl_set_info,(entry, SMB_ACL_MASK, mask_bits, NULL) );
424 #else
425 if (racl->mask_obj != NO_ENTRY) {
426 COE( sys_acl_create_entry,(smb_acl, &entry) );
427 COE( sys_acl_set_info,(entry, SMB_ACL_MASK, racl->mask_obj, 0) );
429 #endif
431 COE( sys_acl_create_entry,(smb_acl, &entry) );
432 COE( sys_acl_set_info,(entry, SMB_ACL_OTHER, racl->other_obj & ~NO_ENTRY, 0) );
433 #endif
435 #ifdef DEBUG
436 if (sys_acl_valid(*smb_acl) < 0)
437 rprintf(FERROR_XFER, "pack_smb_acl: warning: system says the ACL I packed is invalid\n");
438 #endif
440 return True;
442 error_exit:
443 if (errfun) {
444 rsyserr(FERROR_XFER, errno, "pack_smb_acl %s()", errfun);
446 sys_acl_free_acl(*smb_acl);
447 return False;
450 static int find_matching_rsync_acl(const rsync_acl *racl, SMB_ACL_TYPE_T type,
451 const item_list *racl_list)
453 static int access_match = -1, default_match = -1;
454 int *match = type == SMB_ACL_TYPE_ACCESS ? &access_match : &default_match;
455 size_t count = racl_list->count;
457 /* If this is the first time through or we didn't match the last
458 * time, then start at the end of the list, which should be the
459 * best place to start hunting. */
460 if (*match == -1)
461 *match = racl_list->count - 1;
462 while (count--) {
463 rsync_acl *base = racl_list->items;
464 if (rsync_acl_equal(base + *match, racl))
465 return *match;
466 if (!(*match)--)
467 *match = racl_list->count - 1;
470 *match = -1;
471 return *match;
474 static int get_rsync_acl(const char *fname, rsync_acl *racl,
475 SMB_ACL_TYPE_T type, mode_t mode)
477 SMB_ACL_T sacl;
479 #ifdef SUPPORT_XATTRS
480 /* --fake-super support: load ACLs from an xattr. */
481 if (am_root < 0) {
482 char *buf;
483 size_t len;
484 int cnt;
486 if ((buf = get_xattr_acl(fname, type == SMB_ACL_TYPE_ACCESS, &len)) == NULL)
487 return 0;
488 cnt = (len - 4*4) / (4+4);
489 if (len < 4*4 || len != (size_t)cnt*(4+4) + 4*4) {
490 free(buf);
491 return -1;
494 racl->user_obj = IVAL(buf, 0);
495 racl->group_obj = IVAL(buf, 4);
496 racl->mask_obj = IVAL(buf, 8);
497 racl->other_obj = IVAL(buf, 12);
499 if (cnt) {
500 char *bp = buf + 4*4;
501 id_access *ida;
502 if (!(ida = racl->names.idas = new_array(id_access, cnt)))
503 out_of_memory("get_rsync_acl");
504 racl->names.count = cnt;
505 for ( ; cnt--; ida++, bp += 4+4) {
506 ida->id = IVAL(bp, 0);
507 ida->access = IVAL(bp, 4);
510 free(buf);
511 return 0;
513 #endif
515 if ((sacl = sys_acl_get_file(fname, type)) != 0) {
516 BOOL ok = unpack_smb_acl(sacl, racl);
518 sys_acl_free_acl(sacl);
519 if (!ok) {
520 return -1;
522 } else if (no_acl_syscall_error(errno)) {
523 /* ACLs are not supported, so pretend we have a basic ACL. */
524 if (type == SMB_ACL_TYPE_ACCESS)
525 rsync_acl_fake_perms(racl, mode);
526 } else {
527 rsyserr(FERROR_XFER, errno, "get_acl: sys_acl_get_file(%s, %s)",
528 fname, str_acl_type(type));
529 return -1;
532 return 0;
535 /* Return the Access Control List for the given filename. */
536 int get_acl(const char *fname, stat_x *sxp)
538 sxp->acc_acl = create_racl();
539 if (get_rsync_acl(fname, sxp->acc_acl, SMB_ACL_TYPE_ACCESS,
540 sxp->st.st_mode) < 0) {
541 free_acl(sxp);
542 return -1;
545 if (S_ISDIR(sxp->st.st_mode)) {
546 sxp->def_acl = create_racl();
547 if (get_rsync_acl(fname, sxp->def_acl, SMB_ACL_TYPE_DEFAULT,
548 sxp->st.st_mode) < 0) {
549 free_acl(sxp);
550 return -1;
554 return 0;
557 /* === Send functions === */
559 /* Send the ida list over the file descriptor. */
560 static void send_ida_entries(const ida_entries *idal, int f)
562 id_access *ida;
563 size_t count = idal->count;
565 write_varint(f, idal->count);
567 for (ida = idal->idas; count--; ida++) {
568 uint32 xbits = ida->access << 2;
569 const char *name;
570 if (ida->access & NAME_IS_USER) {
571 xbits |= XFLAG_NAME_IS_USER;
572 name = add_uid(ida->id);
573 } else
574 name = add_gid(ida->id);
575 write_varint(f, ida->id);
576 if (inc_recurse && name) {
577 int len = strlen(name);
578 write_varint(f, xbits | XFLAG_NAME_FOLLOWS);
579 write_byte(f, len);
580 write_buf(f, name, len);
581 } else
582 write_varint(f, xbits);
586 static void send_rsync_acl(rsync_acl *racl, SMB_ACL_TYPE_T type,
587 item_list *racl_list, int f)
589 int ndx = find_matching_rsync_acl(racl, type, racl_list);
591 /* Send 0 (-1 + 1) to indicate that literal ACL data follows. */
592 write_varint(f, ndx + 1);
594 if (ndx < 0) {
595 rsync_acl *new_racl = EXPAND_ITEM_LIST(racl_list, rsync_acl, 1000);
596 uchar flags = 0;
598 if (racl->user_obj != NO_ENTRY)
599 flags |= XMIT_USER_OBJ;
600 if (racl->group_obj != NO_ENTRY)
601 flags |= XMIT_GROUP_OBJ;
602 if (racl->mask_obj != NO_ENTRY)
603 flags |= XMIT_MASK_OBJ;
604 if (racl->other_obj != NO_ENTRY)
605 flags |= XMIT_OTHER_OBJ;
606 if (racl->names.count)
607 flags |= XMIT_NAME_LIST;
609 write_byte(f, flags);
611 if (flags & XMIT_USER_OBJ)
612 write_varint(f, racl->user_obj);
613 if (flags & XMIT_GROUP_OBJ)
614 write_varint(f, racl->group_obj);
615 if (flags & XMIT_MASK_OBJ)
616 write_varint(f, racl->mask_obj);
617 if (flags & XMIT_OTHER_OBJ)
618 write_varint(f, racl->other_obj);
619 if (flags & XMIT_NAME_LIST)
620 send_ida_entries(&racl->names, f);
622 /* Give the allocated data to the new list object. */
623 *new_racl = *racl;
624 *racl = empty_rsync_acl;
628 /* Send the ACL from the stat_x structure down the indicated file descriptor.
629 * This also frees the ACL data. */
630 void send_acl(stat_x *sxp, int f)
632 if (!sxp->acc_acl) {
633 sxp->acc_acl = create_racl();
634 rsync_acl_fake_perms(sxp->acc_acl, sxp->st.st_mode);
636 /* Avoid sending values that can be inferred from other data. */
637 rsync_acl_strip_perms(sxp->acc_acl);
639 send_rsync_acl(sxp->acc_acl, SMB_ACL_TYPE_ACCESS, &access_acl_list, f);
641 if (S_ISDIR(sxp->st.st_mode)) {
642 if (!sxp->def_acl)
643 sxp->def_acl = create_racl();
645 send_rsync_acl(sxp->def_acl, SMB_ACL_TYPE_DEFAULT, &default_acl_list, f);
649 /* === Receive functions === */
651 static uint32 recv_acl_access(uchar *name_follows_ptr, int f)
653 uint32 access = read_varint(f);
655 if (name_follows_ptr) {
656 int flags = access & 3;
657 access >>= 2;
658 if (am_root >= 0 && access & ~SMB_ACL_VALID_NAME_BITS)
659 goto value_error;
660 if (flags & XFLAG_NAME_FOLLOWS)
661 *name_follows_ptr = 1;
662 else
663 *name_follows_ptr = 0;
664 if (flags & XFLAG_NAME_IS_USER)
665 access |= NAME_IS_USER;
666 } else if (am_root >= 0 && access & ~SMB_ACL_VALID_OBJ_BITS) {
667 value_error:
668 rprintf(FERROR_XFER, "recv_acl_access: value out of range: %x\n",
669 access);
670 exit_cleanup(RERR_STREAMIO);
673 return access;
676 static uchar recv_ida_entries(ida_entries *ent, int f)
678 uchar computed_mask_bits = 0;
679 int i, count = read_varint(f);
681 if (count) {
682 if (!(ent->idas = new_array(id_access, count)))
683 out_of_memory("recv_ida_entries");
684 } else
685 ent->idas = NULL;
687 ent->count = count;
689 for (i = 0; i < count; i++) {
690 uchar has_name;
691 id_t id = read_varint(f);
692 uint32 access = recv_acl_access(&has_name, f);
694 if (has_name) {
695 if (access & NAME_IS_USER)
696 id = recv_user_name(f, id);
697 else
698 id = recv_group_name(f, id, NULL);
699 } else if (access & NAME_IS_USER) {
700 if (inc_recurse && am_root && !numeric_ids)
701 id = match_uid(id);
702 } else {
703 if (inc_recurse && (!am_root || !numeric_ids))
704 id = match_gid(id, NULL);
707 ent->idas[i].id = id;
708 ent->idas[i].access = access;
709 computed_mask_bits |= access;
712 return computed_mask_bits & ~NO_ENTRY;
715 static int recv_rsync_acl(item_list *racl_list, SMB_ACL_TYPE_T type, int f)
717 uchar computed_mask_bits = 0;
718 acl_duo *duo_item;
719 uchar flags;
720 int ndx = read_varint(f);
722 if (ndx < 0 || (size_t)ndx > racl_list->count) {
723 rprintf(FERROR_XFER, "recv_acl_index: %s ACL index %d > %d\n",
724 str_acl_type(type), ndx, (int)racl_list->count);
725 exit_cleanup(RERR_STREAMIO);
728 if (ndx != 0)
729 return ndx - 1;
731 ndx = racl_list->count;
732 duo_item = EXPAND_ITEM_LIST(racl_list, acl_duo, 1000);
733 duo_item->racl = empty_rsync_acl;
735 flags = read_byte(f);
737 if (flags & XMIT_USER_OBJ)
738 duo_item->racl.user_obj = recv_acl_access(NULL, f);
739 if (flags & XMIT_GROUP_OBJ)
740 duo_item->racl.group_obj = recv_acl_access(NULL, f);
741 if (flags & XMIT_MASK_OBJ)
742 duo_item->racl.mask_obj = recv_acl_access(NULL, f);
743 if (flags & XMIT_OTHER_OBJ)
744 duo_item->racl.other_obj = recv_acl_access(NULL, f);
745 if (flags & XMIT_NAME_LIST)
746 computed_mask_bits |= recv_ida_entries(&duo_item->racl.names, f);
748 #ifdef HAVE_OSX_ACLS
749 /* If we received a superfluous mask, throw it away. */
750 duo_item->racl.mask_obj = NO_ENTRY;
751 #else
752 if (!duo_item->racl.names.count) {
753 /* If we received a superfluous mask, throw it away. */
754 if (duo_item->racl.mask_obj != NO_ENTRY) {
755 /* Mask off the group perms with it first. */
756 duo_item->racl.group_obj &= duo_item->racl.mask_obj | NO_ENTRY;
757 duo_item->racl.mask_obj = NO_ENTRY;
759 } else if (duo_item->racl.mask_obj == NO_ENTRY) /* Must be non-empty with lists. */
760 duo_item->racl.mask_obj = (computed_mask_bits | duo_item->racl.group_obj) & ~NO_ENTRY;
761 #endif
763 duo_item->sacl = NULL;
765 return ndx;
768 /* Receive the ACL info the sender has included for this file-list entry. */
769 void receive_acl(struct file_struct *file, int f)
771 F_ACL(file) = recv_rsync_acl(&access_acl_list, SMB_ACL_TYPE_ACCESS, f);
773 if (S_ISDIR(file->mode))
774 F_DIR_DEFACL(file) = recv_rsync_acl(&default_acl_list, SMB_ACL_TYPE_DEFAULT, f);
777 static int cache_rsync_acl(rsync_acl *racl, SMB_ACL_TYPE_T type, item_list *racl_list)
779 int ndx;
781 if (!racl)
782 ndx = -1;
783 else if ((ndx = find_matching_rsync_acl(racl, type, racl_list)) == -1) {
784 acl_duo *new_duo;
785 ndx = racl_list->count;
786 new_duo = EXPAND_ITEM_LIST(racl_list, acl_duo, 1000);
787 new_duo->racl = *racl;
788 new_duo->sacl = NULL;
789 *racl = empty_rsync_acl;
792 return ndx;
795 /* Turn the ACL data in stat_x into cached ACL data, setting the index
796 * values in the file struct. */
797 void cache_acl(struct file_struct *file, stat_x *sxp)
799 F_ACL(file) = cache_rsync_acl(sxp->acc_acl,
800 SMB_ACL_TYPE_ACCESS, &access_acl_list);
802 if (S_ISDIR(sxp->st.st_mode)) {
803 F_DIR_DEFACL(file) = cache_rsync_acl(sxp->def_acl,
804 SMB_ACL_TYPE_DEFAULT, &default_acl_list);
808 #ifndef HAVE_OSX_ACLS
809 static mode_t change_sacl_perms(SMB_ACL_T sacl, rsync_acl *racl, mode_t old_mode, mode_t mode)
811 SMB_ACL_ENTRY_T entry;
812 const char *errfun;
813 int rc;
815 if (S_ISDIR(mode)) {
816 /* If the sticky bit is going on, it's not safe to allow all
817 * the new ACL to go into effect before it gets set. */
818 #ifdef SMB_ACL_LOSES_SPECIAL_MODE_BITS
819 if (mode & S_ISVTX)
820 mode &= ~0077;
821 #else
822 if (mode & S_ISVTX && !(old_mode & S_ISVTX))
823 mode &= ~0077;
824 } else {
825 /* If setuid or setgid is going off, it's not safe to allow all
826 * the new ACL to go into effect before they get cleared. */
827 if ((old_mode & S_ISUID && !(mode & S_ISUID))
828 || (old_mode & S_ISGID && !(mode & S_ISGID)))
829 mode &= ~0077;
830 #endif
833 errfun = "sys_acl_get_entry";
834 for (rc = sys_acl_get_entry(sacl, SMB_ACL_FIRST_ENTRY, &entry);
835 rc == 1;
836 rc = sys_acl_get_entry(sacl, SMB_ACL_NEXT_ENTRY, &entry)) {
837 SMB_ACL_TAG_T tag_type;
838 if ((rc = sys_acl_get_tag_type(entry, &tag_type)) != 0) {
839 errfun = "sys_acl_get_tag_type";
840 break;
842 switch (tag_type) {
843 case SMB_ACL_USER_OBJ:
844 COE2( store_access_in_entry,((mode >> 6) & 7, entry) );
845 break;
846 case SMB_ACL_GROUP_OBJ:
847 /* group is only empty when identical to group perms. */
848 if (racl->group_obj != NO_ENTRY)
849 break;
850 COE2( store_access_in_entry,((mode >> 3) & 7, entry) );
851 break;
852 case SMB_ACL_MASK:
853 #ifndef ACLS_NEED_MASK
854 /* mask is only empty when we don't need it. */
855 if (racl->mask_obj == NO_ENTRY)
856 break;
857 #endif
858 COE2( store_access_in_entry,((mode >> 3) & 7, entry) );
859 break;
860 case SMB_ACL_OTHER:
861 COE2( store_access_in_entry,(mode & 7, entry) );
862 break;
865 if (rc) {
866 error_exit:
867 if (errfun) {
868 rsyserr(FERROR_XFER, errno, "change_sacl_perms: %s()",
869 errfun);
871 return (mode_t)~0;
874 #ifdef SMB_ACL_LOSES_SPECIAL_MODE_BITS
875 /* Ensure that chmod() will be called to restore any lost setid bits. */
876 if (old_mode & (S_ISUID | S_ISGID | S_ISVTX)
877 && BITS_EQUAL(old_mode, mode, CHMOD_BITS))
878 old_mode &= ~(S_ISUID | S_ISGID | S_ISVTX);
879 #endif
881 /* Return the mode of the file on disk, as we will set them. */
882 return (old_mode & ~ACCESSPERMS) | (mode & ACCESSPERMS);
884 #endif
886 static int set_rsync_acl(const char *fname, acl_duo *duo_item,
887 SMB_ACL_TYPE_T type, stat_x *sxp, mode_t mode)
889 if (type == SMB_ACL_TYPE_DEFAULT
890 && duo_item->racl.user_obj == NO_ENTRY) {
891 int rc;
892 #ifdef SUPPORT_XATTRS
893 /* --fake-super support: delete default ACL from xattrs. */
894 if (am_root < 0)
895 rc = del_def_xattr_acl(fname);
896 else
897 #endif
898 rc = sys_acl_delete_def_file(fname);
899 if (rc < 0) {
900 rsyserr(FERROR_XFER, errno, "set_acl: sys_acl_delete_def_file(%s)",
901 fname);
902 return -1;
904 #ifdef SUPPORT_XATTRS
905 } else if (am_root < 0) {
906 /* --fake-super support: store ACLs in an xattr. */
907 int cnt = duo_item->racl.names.count;
908 size_t len = 4*4 + cnt * (4+4);
909 char *buf = new_array(char, len);
910 int rc;
912 SIVAL(buf, 0, duo_item->racl.user_obj);
913 SIVAL(buf, 4, duo_item->racl.group_obj);
914 SIVAL(buf, 8, duo_item->racl.mask_obj);
915 SIVAL(buf, 12, duo_item->racl.other_obj);
917 if (cnt) {
918 char *bp = buf + 4*4;
919 id_access *ida = duo_item->racl.names.idas;
920 for ( ; cnt--; ida++, bp += 4+4) {
921 SIVAL(bp, 0, ida->id);
922 SIVAL(bp, 4, ida->access);
925 rc = set_xattr_acl(fname, type == SMB_ACL_TYPE_ACCESS, buf, len);
926 free(buf);
927 return rc;
928 #endif
929 } else {
930 mode_t cur_mode = sxp->st.st_mode;
931 if (!duo_item->sacl
932 && !pack_smb_acl(&duo_item->sacl, &duo_item->racl))
933 return -1;
934 #ifdef HAVE_OSX_ACLS
935 mode = 0; /* eliminate compiler warning */
936 #else
937 if (type == SMB_ACL_TYPE_ACCESS) {
938 cur_mode = change_sacl_perms(duo_item->sacl, &duo_item->racl,
939 cur_mode, mode);
940 if (cur_mode == (mode_t)~0)
941 return 0;
943 #endif
944 if (sys_acl_set_file(fname, type, duo_item->sacl) < 0) {
945 rsyserr(FERROR_XFER, errno, "set_acl: sys_acl_set_file(%s, %s)",
946 fname, str_acl_type(type));
947 return -1;
949 if (type == SMB_ACL_TYPE_ACCESS)
950 sxp->st.st_mode = cur_mode;
953 return 0;
956 /* Set ACL on indicated filename.
958 * This sets extended access ACL entries and default ACL. If convenient,
959 * it sets permission bits along with the access ACL and signals having
960 * done so by modifying sxp->st.st_mode.
962 * Returns 1 for unchanged, 0 for changed, -1 for failed. Call this
963 * with fname set to NULL to just check if the ACL is unchanged. */
964 int set_acl(const char *fname, const struct file_struct *file, stat_x *sxp)
966 int unchanged = 1;
967 int32 ndx;
968 BOOL eq;
970 if (!dry_run && (read_only || list_only)) {
971 errno = EROFS;
972 return -1;
975 ndx = F_ACL(file);
976 if (ndx >= 0 && (size_t)ndx < access_acl_list.count) {
977 acl_duo *duo_item = access_acl_list.items;
978 duo_item += ndx;
979 eq = sxp->acc_acl
980 && rsync_acl_equal_enough(sxp->acc_acl, &duo_item->racl, file->mode);
981 if (!eq) {
982 unchanged = 0;
983 if (!dry_run && fname
984 && set_rsync_acl(fname, duo_item, SMB_ACL_TYPE_ACCESS,
985 sxp, file->mode) < 0)
986 unchanged = -1;
990 if (!S_ISDIR(sxp->st.st_mode))
991 return unchanged;
993 ndx = F_DIR_DEFACL(file);
994 if (ndx >= 0 && (size_t)ndx < default_acl_list.count) {
995 acl_duo *duo_item = default_acl_list.items;
996 duo_item += ndx;
997 eq = sxp->def_acl && rsync_acl_equal(sxp->def_acl, &duo_item->racl);
998 if (!eq) {
999 if (unchanged > 0)
1000 unchanged = 0;
1001 if (!dry_run && fname
1002 && set_rsync_acl(fname, duo_item, SMB_ACL_TYPE_DEFAULT,
1003 sxp, file->mode) < 0)
1004 unchanged = -1;
1008 return unchanged;
1011 /* Non-incremental recursion needs to convert all the received IDs.
1012 * This is done in a single pass after receiving the whole file-list. */
1013 static void match_racl_ids(const item_list *racl_list)
1015 int list_cnt, name_cnt;
1016 acl_duo *duo_item = racl_list->items;
1017 for (list_cnt = racl_list->count; list_cnt--; duo_item++) {
1018 ida_entries *idal = &duo_item->racl.names;
1019 id_access *ida = idal->idas;
1020 for (name_cnt = idal->count; name_cnt--; ida++) {
1021 if (ida->access & NAME_IS_USER)
1022 ida->id = match_uid(ida->id);
1023 else
1024 ida->id = match_gid(ida->id, NULL);
1029 void match_acl_ids(void)
1031 match_racl_ids(&access_acl_list);
1032 match_racl_ids(&default_acl_list);
1035 /* This is used by dest_mode(). */
1036 int default_perms_for_dir(const char *dir)
1038 rsync_acl racl;
1039 SMB_ACL_T sacl;
1040 BOOL ok;
1041 int perms;
1043 if (dir == NULL)
1044 dir = ".";
1045 perms = ACCESSPERMS & ~orig_umask;
1046 /* Read the directory's default ACL. If it has none, this will successfully return an empty ACL. */
1047 sacl = sys_acl_get_file(dir, SMB_ACL_TYPE_DEFAULT);
1048 if (sacl == NULL) {
1049 /* Couldn't get an ACL. Darn. */
1050 switch (errno) {
1051 #ifdef ENOTSUP
1052 case ENOTSUP:
1053 #endif
1054 case ENOSYS:
1055 /* No ACLs are available. */
1056 break;
1057 case ENOENT:
1058 if (dry_run) {
1059 /* We're doing a dry run, so the containing directory
1060 * wasn't actually created. Don't worry about it. */
1061 break;
1063 /* Otherwise fall through. */
1064 default:
1065 rprintf(FWARNING,
1066 "default_perms_for_dir: sys_acl_get_file(%s, %s): %s, falling back on umask\n",
1067 dir, str_acl_type(SMB_ACL_TYPE_DEFAULT), strerror(errno));
1069 return perms;
1072 /* Convert it. */
1073 racl = empty_rsync_acl;
1074 ok = unpack_smb_acl(sacl, &racl);
1075 sys_acl_free_acl(sacl);
1076 if (!ok) {
1077 rprintf(FWARNING, "default_perms_for_dir: unpack_smb_acl failed, falling back on umask\n");
1078 return perms;
1081 /* Apply the permission-bit entries of the default ACL, if any. */
1082 if (racl.user_obj != NO_ENTRY) {
1083 perms = rsync_acl_get_perms(&racl);
1084 if (verbose > 2)
1085 rprintf(FINFO, "got ACL-based default perms %o for directory %s\n", perms, dir);
1088 rsync_acl_free(&racl);
1089 return perms;
1092 #endif /* SUPPORT_ACLS */