Optimize finding the sum that matches our --inplace position.
[rsync/qnx.git] / acls.c
blob27a93c0b125e91b3ddffe081932a99aa38ee96a9
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-2009 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 hightest 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
120 + 4;
121 #else
122 + (racl->mask_obj != NO_ENTRY) + 3;
123 #endif
126 /* Extracts and returns the permission bits from the ACL. This cannot be
127 * called on an rsync_acl that has NO_ENTRY in any spot but the mask. */
128 static int rsync_acl_get_perms(const rsync_acl *racl)
130 return (racl->user_obj << 6)
131 + ((racl->mask_obj != NO_ENTRY ? racl->mask_obj : racl->group_obj) << 3)
132 + racl->other_obj;
135 /* Removes the permission-bit entries from the ACL because these
136 * can be reconstructed from the file's mode. */
137 static void rsync_acl_strip_perms(rsync_acl *racl)
139 racl->user_obj = NO_ENTRY;
140 if (racl->mask_obj == NO_ENTRY)
141 racl->group_obj = NO_ENTRY;
142 else {
143 if (racl->group_obj == racl->mask_obj)
144 racl->group_obj = NO_ENTRY;
145 if (racl->names.count != 0)
146 racl->mask_obj = NO_ENTRY;
148 racl->other_obj = NO_ENTRY;
151 /* Given an empty rsync_acl, fake up the permission bits. */
152 static void rsync_acl_fake_perms(rsync_acl *racl, mode_t mode)
154 racl->user_obj = (mode >> 6) & 7;
155 racl->group_obj = (mode >> 3) & 7;
156 racl->other_obj = mode & 7;
159 /* === Rsync ACL functions === */
161 static rsync_acl *create_racl(void)
163 rsync_acl *racl = new(rsync_acl);
165 if (!racl)
166 out_of_memory("create_racl");
167 *racl = empty_rsync_acl;
169 return racl;
172 static BOOL ida_entries_equal(const ida_entries *ial1, const ida_entries *ial2)
174 id_access *ida1, *ida2;
175 int count = ial1->count;
176 if (count != ial2->count)
177 return False;
178 ida1 = ial1->idas;
179 ida2 = ial2->idas;
180 for (; count--; ida1++, ida2++) {
181 if (ida1->access != ida2->access || ida1->id != ida2->id)
182 return False;
184 return True;
187 static BOOL rsync_acl_equal(const rsync_acl *racl1, const rsync_acl *racl2)
189 return racl1->user_obj == racl2->user_obj
190 && racl1->group_obj == racl2->group_obj
191 && racl1->mask_obj == racl2->mask_obj
192 && racl1->other_obj == racl2->other_obj
193 && ida_entries_equal(&racl1->names, &racl2->names);
196 /* Are the extended (non-permission-bit) entries equal? If so, the rest of
197 * the ACL will be handled by the normal mode-preservation code. This is
198 * only meaningful for access ACLs! Note: the 1st arg is a fully-populated
199 * rsync_acl, but the 2nd parameter can be a condensed rsync_acl, which means
200 * that it might have several of its permission objects set to NO_ENTRY. */
201 static BOOL rsync_acl_equal_enough(const rsync_acl *racl1,
202 const rsync_acl *racl2, mode_t m)
204 if ((racl1->mask_obj ^ racl2->mask_obj) & NO_ENTRY)
205 return False; /* One has a mask and the other doesn't */
207 /* When there's a mask, the group_obj becomes an extended entry. */
208 if (racl1->mask_obj != NO_ENTRY) {
209 /* A condensed rsync_acl with a mask can only have no
210 * group_obj when it was identical to the mask. This
211 * means that it was also identical to the group attrs
212 * from the mode. */
213 if (racl2->group_obj == NO_ENTRY) {
214 if (racl1->group_obj != ((m >> 3) & 7))
215 return False;
216 } else if (racl1->group_obj != racl2->group_obj)
217 return False;
219 return ida_entries_equal(&racl1->names, &racl2->names);
222 static void rsync_acl_free(rsync_acl *racl)
224 if (racl->names.idas)
225 free(racl->names.idas);
226 *racl = empty_rsync_acl;
229 void free_acl(stat_x *sxp)
231 if (sxp->acc_acl) {
232 rsync_acl_free(sxp->acc_acl);
233 free(sxp->acc_acl);
234 sxp->acc_acl = NULL;
236 if (sxp->def_acl) {
237 rsync_acl_free(sxp->def_acl);
238 free(sxp->def_acl);
239 sxp->def_acl = NULL;
243 #ifdef SMB_ACL_NEED_SORT
244 static int id_access_sorter(const void *r1, const void *r2)
246 id_access *ida1 = (id_access *)r1;
247 id_access *ida2 = (id_access *)r2;
248 id_t rid1 = ida1->id, rid2 = ida2->id;
249 if ((ida1->access ^ ida2->access) & NAME_IS_USER)
250 return ida1->access & NAME_IS_USER ? -1 : 1;
251 return rid1 == rid2 ? 0 : rid1 < rid2 ? -1 : 1;
253 #endif
255 /* === System ACLs === */
257 /* Unpack system ACL -> rsync ACL verbatim. Return whether we succeeded. */
258 static BOOL unpack_smb_acl(SMB_ACL_T sacl, rsync_acl *racl)
260 static item_list temp_ida_list = EMPTY_ITEM_LIST;
261 SMB_ACL_ENTRY_T entry;
262 const char *errfun;
263 int rc;
265 errfun = "sys_acl_get_entry";
266 for (rc = sys_acl_get_entry(sacl, SMB_ACL_FIRST_ENTRY, &entry);
267 rc == 1;
268 rc = sys_acl_get_entry(sacl, SMB_ACL_NEXT_ENTRY, &entry)) {
269 SMB_ACL_TAG_T tag_type;
270 uint32 access;
271 id_t g_u_id;
272 id_access *ida;
273 if ((rc = sys_acl_get_info(entry, &tag_type, &access, &g_u_id)) != 0) {
274 errfun = "sys_acl_get_info";
275 break;
277 /* continue == done with entry; break == store in temporary ida list */
278 switch (tag_type) {
279 #ifndef HAVE_OSX_ACLS
280 case SMB_ACL_USER_OBJ:
281 if (racl->user_obj == NO_ENTRY)
282 racl->user_obj = access;
283 else
284 rprintf(FINFO, "unpack_smb_acl: warning: duplicate USER_OBJ entry ignored\n");
285 continue;
286 case SMB_ACL_GROUP_OBJ:
287 if (racl->group_obj == NO_ENTRY)
288 racl->group_obj = access;
289 else
290 rprintf(FINFO, "unpack_smb_acl: warning: duplicate GROUP_OBJ entry ignored\n");
291 continue;
292 case SMB_ACL_MASK:
293 if (racl->mask_obj == NO_ENTRY)
294 racl->mask_obj = access;
295 else
296 rprintf(FINFO, "unpack_smb_acl: warning: duplicate MASK entry ignored\n");
297 continue;
298 case SMB_ACL_OTHER:
299 if (racl->other_obj == NO_ENTRY)
300 racl->other_obj = access;
301 else
302 rprintf(FINFO, "unpack_smb_acl: warning: duplicate OTHER entry ignored\n");
303 continue;
304 #endif
305 case SMB_ACL_USER:
306 access |= NAME_IS_USER;
307 break;
308 case SMB_ACL_GROUP:
309 break;
310 default:
311 rprintf(FINFO, "unpack_smb_acl: warning: entry with unrecognized tag type ignored\n");
312 continue;
314 ida = EXPAND_ITEM_LIST(&temp_ida_list, id_access, -10);
315 ida->id = g_u_id;
316 ida->access = access;
318 if (rc) {
319 rsyserr(FERROR_XFER, errno, "unpack_smb_acl: %s()", errfun);
320 rsync_acl_free(racl);
321 return False;
324 /* Transfer the count id_access items out of the temp_ida_list
325 * into the names ida_entries list in racl. */
326 if (temp_ida_list.count) {
327 #ifdef SMB_ACL_NEED_SORT
328 if (temp_ida_list.count > 1) {
329 qsort(temp_ida_list.items, temp_ida_list.count,
330 sizeof (id_access), id_access_sorter);
332 #endif
333 if (!(racl->names.idas = new_array(id_access, temp_ida_list.count)))
334 out_of_memory("unpack_smb_acl");
335 memcpy(racl->names.idas, temp_ida_list.items,
336 temp_ida_list.count * sizeof (id_access));
337 } else
338 racl->names.idas = NULL;
340 racl->names.count = temp_ida_list.count;
342 /* Truncate the temporary list now that its idas have been saved. */
343 temp_ida_list.count = 0;
345 #ifdef ACLS_NEED_MASK
346 if (!racl->names.count && racl->mask_obj != NO_ENTRY) {
347 /* Throw away a superfluous mask, but mask off the
348 * group perms with it first. */
349 racl->group_obj &= racl->mask_obj;
350 racl->mask_obj = NO_ENTRY;
352 #endif
354 return True;
357 /* Synactic sugar for system calls */
359 #define CALL_OR_ERROR(func,args,str) \
360 do { \
361 if (func args) { \
362 errfun = str; \
363 goto error_exit; \
365 } while (0)
367 #define COE(func,args) CALL_OR_ERROR(func,args,#func)
368 #define COE2(func,args) CALL_OR_ERROR(func,args,NULL)
370 #ifndef HAVE_OSX_ACLS
371 /* Store the permissions in the system ACL entry. */
372 static int store_access_in_entry(uint32 access, SMB_ACL_ENTRY_T entry)
374 if (sys_acl_set_access_bits(entry, access)) {
375 rsyserr(FERROR_XFER, errno, "store_access_in_entry sys_acl_set_access_bits()");
376 return -1;
378 return 0;
380 #endif
382 /* Pack rsync ACL -> system ACL verbatim. Return whether we succeeded. */
383 static BOOL pack_smb_acl(SMB_ACL_T *smb_acl, const rsync_acl *racl)
385 #ifdef ACLS_NEED_MASK
386 uchar mask_bits;
387 #endif
388 size_t count;
389 id_access *ida;
390 const char *errfun = NULL;
391 SMB_ACL_ENTRY_T entry;
393 if (!(*smb_acl = sys_acl_init(calc_sacl_entries(racl)))) {
394 rsyserr(FERROR_XFER, errno, "pack_smb_acl: sys_acl_init()");
395 return False;
398 #ifndef HAVE_OSX_ACLS
399 COE( sys_acl_create_entry,(smb_acl, &entry) );
400 COE( sys_acl_set_info,(entry, SMB_ACL_USER_OBJ, racl->user_obj & ~NO_ENTRY, 0) );
401 #endif
403 for (ida = racl->names.idas, count = racl->names.count; count; ida++, count--) {
404 #ifdef SMB_ACL_NEED_SORT
405 if (!(ida->access & NAME_IS_USER))
406 break;
407 #endif
408 COE( sys_acl_create_entry,(smb_acl, &entry) );
409 COE( sys_acl_set_info,
410 (entry,
411 ida->access & NAME_IS_USER ? SMB_ACL_USER : SMB_ACL_GROUP,
412 ida->access & ~NAME_IS_USER, ida->id) );
415 #ifndef HAVE_OSX_ACLS
416 COE( sys_acl_create_entry,(smb_acl, &entry) );
417 COE( sys_acl_set_info,(entry, SMB_ACL_GROUP_OBJ, racl->group_obj & ~NO_ENTRY, 0) );
419 #ifdef SMB_ACL_NEED_SORT
420 for ( ; count; ida++, count--) {
421 COE( sys_acl_create_entry,(smb_acl, &entry) );
422 COE( sys_acl_set_info,(entry, SMB_ACL_GROUP, ida->access, ida->id) );
424 #endif
426 #ifdef ACLS_NEED_MASK
427 mask_bits = racl->mask_obj == NO_ENTRY ? racl->group_obj & ~NO_ENTRY : racl->mask_obj;
428 COE( sys_acl_create_entry,(smb_acl, &entry) );
429 COE( sys_acl_set_info,(entry, SMB_ACL_MASK, mask_bits, NULL) );
430 #else
431 if (racl->mask_obj != NO_ENTRY) {
432 COE( sys_acl_create_entry,(smb_acl, &entry) );
433 COE( sys_acl_set_info,(entry, SMB_ACL_MASK, racl->mask_obj, 0) );
435 #endif
437 COE( sys_acl_create_entry,(smb_acl, &entry) );
438 COE( sys_acl_set_info,(entry, SMB_ACL_OTHER, racl->other_obj & ~NO_ENTRY, 0) );
439 #endif
441 #ifdef DEBUG
442 if (sys_acl_valid(*smb_acl) < 0)
443 rprintf(FERROR_XFER, "pack_smb_acl: warning: system says the ACL I packed is invalid\n");
444 #endif
446 return True;
448 error_exit:
449 if (errfun) {
450 rsyserr(FERROR_XFER, errno, "pack_smb_acl %s()", errfun);
452 sys_acl_free_acl(*smb_acl);
453 return False;
456 static int find_matching_rsync_acl(const rsync_acl *racl, SMB_ACL_TYPE_T type,
457 const item_list *racl_list)
459 static int access_match = -1, default_match = -1;
460 int *match = type == SMB_ACL_TYPE_ACCESS ? &access_match : &default_match;
461 size_t count = racl_list->count;
463 /* If this is the first time through or we didn't match the last
464 * time, then start at the end of the list, which should be the
465 * best place to start hunting. */
466 if (*match == -1)
467 *match = racl_list->count - 1;
468 while (count--) {
469 rsync_acl *base = racl_list->items;
470 if (rsync_acl_equal(base + *match, racl))
471 return *match;
472 if (!(*match)--)
473 *match = racl_list->count - 1;
476 *match = -1;
477 return *match;
480 static int get_rsync_acl(const char *fname, rsync_acl *racl,
481 SMB_ACL_TYPE_T type, mode_t mode)
483 SMB_ACL_T sacl;
485 #ifdef SUPPORT_XATTRS
486 /* --fake-super support: load ACLs from an xattr. */
487 if (am_root < 0) {
488 char *buf;
489 size_t len;
490 int cnt;
492 if ((buf = get_xattr_acl(fname, type == SMB_ACL_TYPE_ACCESS, &len)) == NULL)
493 return 0;
494 cnt = (len - 4*4) / (4+4);
495 if (len < 4*4 || len != (size_t)cnt*(4+4) + 4*4) {
496 free(buf);
497 return -1;
500 racl->user_obj = IVAL(buf, 0);
501 racl->group_obj = IVAL(buf, 4);
502 racl->mask_obj = IVAL(buf, 8);
503 racl->other_obj = IVAL(buf, 12);
505 if (cnt) {
506 char *bp = buf + 4*4;
507 id_access *ida;
508 if (!(ida = racl->names.idas = new_array(id_access, cnt)))
509 out_of_memory("get_rsync_acl");
510 racl->names.count = cnt;
511 for ( ; cnt--; ida++, bp += 4+4) {
512 ida->id = IVAL(bp, 0);
513 ida->access = IVAL(bp, 4);
516 free(buf);
517 return 0;
519 #endif
521 if ((sacl = sys_acl_get_file(fname, type)) != 0) {
522 BOOL ok = unpack_smb_acl(sacl, racl);
524 sys_acl_free_acl(sacl);
525 if (!ok) {
526 return -1;
528 } else if (no_acl_syscall_error(errno)) {
529 /* ACLs are not supported, so pretend we have a basic ACL. */
530 if (type == SMB_ACL_TYPE_ACCESS)
531 rsync_acl_fake_perms(racl, mode);
532 } else {
533 rsyserr(FERROR_XFER, errno, "get_acl: sys_acl_get_file(%s, %s)",
534 fname, str_acl_type(type));
535 return -1;
538 return 0;
541 /* Return the Access Control List for the given filename. */
542 int get_acl(const char *fname, stat_x *sxp)
544 sxp->acc_acl = create_racl();
546 if (S_ISREG(sxp->st.st_mode) || S_ISDIR(sxp->st.st_mode)) {
547 /* Everyone supports this. */
548 } else if (S_ISLNK(sxp->st.st_mode)) {
549 return 0;
550 } else if (IS_SPECIAL(sxp->st.st_mode)) {
551 #ifndef NO_SPECIAL_ACLS
552 if (!preserve_specials)
553 #endif
554 return 0;
555 } else if (IS_DEVICE(sxp->st.st_mode)) {
556 #ifndef NO_DEVICE_ACLS
557 if (!preserve_devices)
558 #endif
559 return 0;
562 if (get_rsync_acl(fname, sxp->acc_acl, SMB_ACL_TYPE_ACCESS,
563 sxp->st.st_mode) < 0) {
564 free_acl(sxp);
565 return -1;
568 if (S_ISDIR(sxp->st.st_mode)) {
569 sxp->def_acl = create_racl();
570 if (get_rsync_acl(fname, sxp->def_acl, SMB_ACL_TYPE_DEFAULT,
571 sxp->st.st_mode) < 0) {
572 free_acl(sxp);
573 return -1;
577 return 0;
580 /* === Send functions === */
582 /* Send the ida list over the file descriptor. */
583 static void send_ida_entries(const ida_entries *idal, int f)
585 id_access *ida;
586 size_t count = idal->count;
588 write_varint(f, idal->count);
590 for (ida = idal->idas; count--; ida++) {
591 uint32 xbits = ida->access << 2;
592 const char *name;
593 if (ida->access & NAME_IS_USER) {
594 xbits |= XFLAG_NAME_IS_USER;
595 name = add_uid(ida->id);
596 } else
597 name = add_gid(ida->id);
598 write_varint(f, ida->id);
599 if (inc_recurse && name) {
600 int len = strlen(name);
601 write_varint(f, xbits | XFLAG_NAME_FOLLOWS);
602 write_byte(f, len);
603 write_buf(f, name, len);
604 } else
605 write_varint(f, xbits);
609 static void send_rsync_acl(rsync_acl *racl, SMB_ACL_TYPE_T type,
610 item_list *racl_list, int f)
612 int ndx = find_matching_rsync_acl(racl, type, racl_list);
614 /* Send 0 (-1 + 1) to indicate that literal ACL data follows. */
615 write_varint(f, ndx + 1);
617 if (ndx < 0) {
618 rsync_acl *new_racl = EXPAND_ITEM_LIST(racl_list, rsync_acl, 1000);
619 uchar flags = 0;
621 if (racl->user_obj != NO_ENTRY)
622 flags |= XMIT_USER_OBJ;
623 if (racl->group_obj != NO_ENTRY)
624 flags |= XMIT_GROUP_OBJ;
625 if (racl->mask_obj != NO_ENTRY)
626 flags |= XMIT_MASK_OBJ;
627 if (racl->other_obj != NO_ENTRY)
628 flags |= XMIT_OTHER_OBJ;
629 if (racl->names.count)
630 flags |= XMIT_NAME_LIST;
632 write_byte(f, flags);
634 if (flags & XMIT_USER_OBJ)
635 write_varint(f, racl->user_obj);
636 if (flags & XMIT_GROUP_OBJ)
637 write_varint(f, racl->group_obj);
638 if (flags & XMIT_MASK_OBJ)
639 write_varint(f, racl->mask_obj);
640 if (flags & XMIT_OTHER_OBJ)
641 write_varint(f, racl->other_obj);
642 if (flags & XMIT_NAME_LIST)
643 send_ida_entries(&racl->names, f);
645 /* Give the allocated data to the new list object. */
646 *new_racl = *racl;
647 *racl = empty_rsync_acl;
651 /* Send the ACL from the stat_x structure down the indicated file descriptor.
652 * This also frees the ACL data. */
653 void send_acl(stat_x *sxp, int f)
655 if (!sxp->acc_acl) {
656 sxp->acc_acl = create_racl();
657 rsync_acl_fake_perms(sxp->acc_acl, sxp->st.st_mode);
659 /* Avoid sending values that can be inferred from other data. */
660 rsync_acl_strip_perms(sxp->acc_acl);
662 send_rsync_acl(sxp->acc_acl, SMB_ACL_TYPE_ACCESS, &access_acl_list, f);
664 if (S_ISDIR(sxp->st.st_mode)) {
665 if (!sxp->def_acl)
666 sxp->def_acl = create_racl();
668 send_rsync_acl(sxp->def_acl, SMB_ACL_TYPE_DEFAULT, &default_acl_list, f);
672 /* === Receive functions === */
674 static uint32 recv_acl_access(uchar *name_follows_ptr, int f)
676 uint32 access = read_varint(f);
678 if (name_follows_ptr) {
679 int flags = access & 3;
680 access >>= 2;
681 if (am_root >= 0 && access & ~SMB_ACL_VALID_NAME_BITS)
682 goto value_error;
683 if (flags & XFLAG_NAME_FOLLOWS)
684 *name_follows_ptr = 1;
685 else
686 *name_follows_ptr = 0;
687 if (flags & XFLAG_NAME_IS_USER)
688 access |= NAME_IS_USER;
689 } else if (am_root >= 0 && access & ~SMB_ACL_VALID_OBJ_BITS) {
690 value_error:
691 rprintf(FERROR_XFER, "recv_acl_access: value out of range: %x\n",
692 access);
693 exit_cleanup(RERR_STREAMIO);
696 return access;
699 static uchar recv_ida_entries(ida_entries *ent, int f)
701 uchar computed_mask_bits = 0;
702 int i, count = read_varint(f);
704 if (count) {
705 if (!(ent->idas = new_array(id_access, count)))
706 out_of_memory("recv_ida_entries");
707 } else
708 ent->idas = NULL;
710 ent->count = count;
712 for (i = 0; i < count; i++) {
713 uchar has_name;
714 id_t id = read_varint(f);
715 uint32 access = recv_acl_access(&has_name, f);
717 if (has_name) {
718 if (access & NAME_IS_USER)
719 id = recv_user_name(f, id);
720 else
721 id = recv_group_name(f, id, NULL);
722 } else if (access & NAME_IS_USER) {
723 if (inc_recurse && am_root && !numeric_ids)
724 id = match_uid(id);
725 } else {
726 if (inc_recurse && (!am_root || !numeric_ids))
727 id = match_gid(id, NULL);
730 ent->idas[i].id = id;
731 ent->idas[i].access = access;
732 computed_mask_bits |= access;
735 return computed_mask_bits & ~NO_ENTRY;
738 static int recv_rsync_acl(item_list *racl_list, SMB_ACL_TYPE_T type, int f)
740 uchar computed_mask_bits = 0;
741 acl_duo *duo_item;
742 uchar flags;
743 int ndx = read_varint(f);
745 if (ndx < 0 || (size_t)ndx > racl_list->count) {
746 rprintf(FERROR_XFER, "recv_acl_index: %s ACL index %d > %d\n",
747 str_acl_type(type), ndx, (int)racl_list->count);
748 exit_cleanup(RERR_STREAMIO);
751 if (ndx != 0)
752 return ndx - 1;
754 ndx = racl_list->count;
755 duo_item = EXPAND_ITEM_LIST(racl_list, acl_duo, 1000);
756 duo_item->racl = empty_rsync_acl;
758 flags = read_byte(f);
760 if (flags & XMIT_USER_OBJ)
761 duo_item->racl.user_obj = recv_acl_access(NULL, f);
762 if (flags & XMIT_GROUP_OBJ)
763 duo_item->racl.group_obj = recv_acl_access(NULL, f);
764 if (flags & XMIT_MASK_OBJ)
765 duo_item->racl.mask_obj = recv_acl_access(NULL, f);
766 if (flags & XMIT_OTHER_OBJ)
767 duo_item->racl.other_obj = recv_acl_access(NULL, f);
768 if (flags & XMIT_NAME_LIST)
769 computed_mask_bits |= recv_ida_entries(&duo_item->racl.names, f);
771 #ifdef HAVE_OSX_ACLS
772 /* If we received a superfluous mask, throw it away. */
773 duo_item->racl.mask_obj = NO_ENTRY;
774 #else
775 if (duo_item->racl.names.count && duo_item->racl.mask_obj == NO_ENTRY) /* Must be non-empty with lists. */
776 duo_item->racl.mask_obj = (computed_mask_bits | duo_item->racl.group_obj) & ~NO_ENTRY;
777 #endif
779 duo_item->sacl = NULL;
781 return ndx;
784 /* Receive the ACL info the sender has included for this file-list entry. */
785 void receive_acl(struct file_struct *file, int f)
787 F_ACL(file) = recv_rsync_acl(&access_acl_list, SMB_ACL_TYPE_ACCESS, f);
789 if (S_ISDIR(file->mode))
790 F_DIR_DEFACL(file) = recv_rsync_acl(&default_acl_list, SMB_ACL_TYPE_DEFAULT, f);
793 static int cache_rsync_acl(rsync_acl *racl, SMB_ACL_TYPE_T type, item_list *racl_list)
795 int ndx;
797 if (!racl)
798 ndx = -1;
799 else if ((ndx = find_matching_rsync_acl(racl, type, racl_list)) == -1) {
800 acl_duo *new_duo;
801 ndx = racl_list->count;
802 new_duo = EXPAND_ITEM_LIST(racl_list, acl_duo, 1000);
803 new_duo->racl = *racl;
804 new_duo->sacl = NULL;
805 *racl = empty_rsync_acl;
808 return ndx;
811 /* Turn the ACL data in stat_x into cached ACL data, setting the index
812 * values in the file struct. */
813 void cache_tmp_acl(struct file_struct *file, stat_x *sxp)
815 if (prior_access_count == (size_t)-1)
816 prior_access_count = access_acl_list.count;
818 F_ACL(file) = cache_rsync_acl(sxp->acc_acl,
819 SMB_ACL_TYPE_ACCESS, &access_acl_list);
821 if (S_ISDIR(sxp->st.st_mode)) {
822 if (prior_default_count == (size_t)-1)
823 prior_default_count = default_acl_list.count;
824 F_DIR_DEFACL(file) = cache_rsync_acl(sxp->def_acl,
825 SMB_ACL_TYPE_DEFAULT, &default_acl_list);
829 static void uncache_duo_acls(item_list *duo_list, size_t start)
831 acl_duo *duo_item = duo_list->items;
832 acl_duo *duo_start = duo_item + start;
834 duo_item += duo_list->count;
835 duo_list->count = start;
837 while (duo_item-- > duo_start) {
838 rsync_acl_free(&duo_item->racl);
839 if (duo_item->sacl)
840 sys_acl_free_acl(duo_item->sacl);
844 void uncache_tmp_acls(void)
846 if (prior_access_count != (size_t)-1) {
847 uncache_duo_acls(&access_acl_list, prior_access_count);
848 prior_access_count = (size_t)-1;
851 if (prior_default_count != (size_t)-1) {
852 uncache_duo_acls(&default_acl_list, prior_default_count);
853 prior_default_count = (size_t)-1;
857 #ifndef HAVE_OSX_ACLS
858 static mode_t change_sacl_perms(SMB_ACL_T sacl, rsync_acl *racl, mode_t old_mode, mode_t mode)
860 SMB_ACL_ENTRY_T entry;
861 const char *errfun;
862 int rc;
864 if (S_ISDIR(mode)) {
865 /* If the sticky bit is going on, it's not safe to allow all
866 * the new ACL to go into effect before it gets set. */
867 #ifdef SMB_ACL_LOSES_SPECIAL_MODE_BITS
868 if (mode & S_ISVTX)
869 mode &= ~0077;
870 #else
871 if (mode & S_ISVTX && !(old_mode & S_ISVTX))
872 mode &= ~0077;
873 } else {
874 /* If setuid or setgid is going off, it's not safe to allow all
875 * the new ACL to go into effect before they get cleared. */
876 if ((old_mode & S_ISUID && !(mode & S_ISUID))
877 || (old_mode & S_ISGID && !(mode & S_ISGID)))
878 mode &= ~0077;
879 #endif
882 errfun = "sys_acl_get_entry";
883 for (rc = sys_acl_get_entry(sacl, SMB_ACL_FIRST_ENTRY, &entry);
884 rc == 1;
885 rc = sys_acl_get_entry(sacl, SMB_ACL_NEXT_ENTRY, &entry)) {
886 SMB_ACL_TAG_T tag_type;
887 if ((rc = sys_acl_get_tag_type(entry, &tag_type)) != 0) {
888 errfun = "sys_acl_get_tag_type";
889 break;
891 switch (tag_type) {
892 case SMB_ACL_USER_OBJ:
893 COE2( store_access_in_entry,((mode >> 6) & 7, entry) );
894 break;
895 case SMB_ACL_GROUP_OBJ:
896 /* group is only empty when identical to group perms. */
897 if (racl->group_obj != NO_ENTRY)
898 break;
899 COE2( store_access_in_entry,((mode >> 3) & 7, entry) );
900 break;
901 case SMB_ACL_MASK:
902 #ifndef ACLS_NEED_MASK
903 /* mask is only empty when we don't need it. */
904 if (racl->mask_obj == NO_ENTRY)
905 break;
906 #endif
907 COE2( store_access_in_entry,((mode >> 3) & 7, entry) );
908 break;
909 case SMB_ACL_OTHER:
910 COE2( store_access_in_entry,(mode & 7, entry) );
911 break;
914 if (rc) {
915 error_exit:
916 if (errfun) {
917 rsyserr(FERROR_XFER, errno, "change_sacl_perms: %s()",
918 errfun);
920 return (mode_t)~0;
923 #ifdef SMB_ACL_LOSES_SPECIAL_MODE_BITS
924 /* Ensure that chmod() will be called to restore any lost setid bits. */
925 if (old_mode & (S_ISUID | S_ISGID | S_ISVTX)
926 && BITS_EQUAL(old_mode, mode, CHMOD_BITS))
927 old_mode &= ~(S_ISUID | S_ISGID | S_ISVTX);
928 #endif
930 /* Return the mode of the file on disk, as we will set them. */
931 return (old_mode & ~ACCESSPERMS) | (mode & ACCESSPERMS);
933 #endif
935 static int set_rsync_acl(const char *fname, acl_duo *duo_item,
936 SMB_ACL_TYPE_T type, stat_x *sxp, mode_t mode)
938 if (type == SMB_ACL_TYPE_DEFAULT
939 && duo_item->racl.user_obj == NO_ENTRY) {
940 int rc;
941 #ifdef SUPPORT_XATTRS
942 /* --fake-super support: delete default ACL from xattrs. */
943 if (am_root < 0)
944 rc = del_def_xattr_acl(fname);
945 else
946 #endif
947 rc = sys_acl_delete_def_file(fname);
948 if (rc < 0) {
949 rsyserr(FERROR_XFER, errno, "set_acl: sys_acl_delete_def_file(%s)",
950 fname);
951 return -1;
953 #ifdef SUPPORT_XATTRS
954 } else if (am_root < 0) {
955 /* --fake-super support: store ACLs in an xattr. */
956 int cnt = duo_item->racl.names.count;
957 size_t len = 4*4 + cnt * (4+4);
958 char *buf = new_array(char, len);
959 int rc;
961 SIVAL(buf, 0, duo_item->racl.user_obj);
962 SIVAL(buf, 4, duo_item->racl.group_obj);
963 SIVAL(buf, 8, duo_item->racl.mask_obj);
964 SIVAL(buf, 12, duo_item->racl.other_obj);
966 if (cnt) {
967 char *bp = buf + 4*4;
968 id_access *ida = duo_item->racl.names.idas;
969 for ( ; cnt--; ida++, bp += 4+4) {
970 SIVAL(bp, 0, ida->id);
971 SIVAL(bp, 4, ida->access);
974 rc = set_xattr_acl(fname, type == SMB_ACL_TYPE_ACCESS, buf, len);
975 free(buf);
976 return rc;
977 #endif
978 } else {
979 mode_t cur_mode = sxp->st.st_mode;
980 if (!duo_item->sacl
981 && !pack_smb_acl(&duo_item->sacl, &duo_item->racl))
982 return -1;
983 #ifdef HAVE_OSX_ACLS
984 mode = 0; /* eliminate compiler warning */
985 #else
986 if (type == SMB_ACL_TYPE_ACCESS) {
987 cur_mode = change_sacl_perms(duo_item->sacl, &duo_item->racl,
988 cur_mode, mode);
989 if (cur_mode == (mode_t)~0)
990 return 0;
992 #endif
993 if (sys_acl_set_file(fname, type, duo_item->sacl) < 0) {
994 rsyserr(FERROR_XFER, errno, "set_acl: sys_acl_set_file(%s, %s)",
995 fname, str_acl_type(type));
996 return -1;
998 if (type == SMB_ACL_TYPE_ACCESS)
999 sxp->st.st_mode = cur_mode;
1002 return 0;
1005 /* Set ACL on indicated filename.
1007 * This sets extended access ACL entries and default ACL. If convenient,
1008 * it sets permission bits along with the access ACL and signals having
1009 * done so by modifying sxp->st.st_mode.
1011 * Returns 1 for unchanged, 0 for changed, -1 for failed. Call this
1012 * with fname set to NULL to just check if the ACL is unchanged. */
1013 int set_acl(const char *fname, const struct file_struct *file, stat_x *sxp)
1015 int unchanged = 1;
1016 int32 ndx;
1017 BOOL eq;
1019 if (!dry_run && (read_only || list_only)) {
1020 errno = EROFS;
1021 return -1;
1024 ndx = F_ACL(file);
1025 if (ndx >= 0 && (size_t)ndx < access_acl_list.count) {
1026 acl_duo *duo_item = access_acl_list.items;
1027 duo_item += ndx;
1028 eq = sxp->acc_acl
1029 && rsync_acl_equal_enough(sxp->acc_acl, &duo_item->racl, file->mode);
1030 if (!eq) {
1031 unchanged = 0;
1032 if (!dry_run && fname
1033 && set_rsync_acl(fname, duo_item, SMB_ACL_TYPE_ACCESS,
1034 sxp, file->mode) < 0)
1035 unchanged = -1;
1039 if (!S_ISDIR(sxp->st.st_mode))
1040 return unchanged;
1042 ndx = F_DIR_DEFACL(file);
1043 if (ndx >= 0 && (size_t)ndx < default_acl_list.count) {
1044 acl_duo *duo_item = default_acl_list.items;
1045 duo_item += ndx;
1046 eq = sxp->def_acl && rsync_acl_equal(sxp->def_acl, &duo_item->racl);
1047 if (!eq) {
1048 if (unchanged > 0)
1049 unchanged = 0;
1050 if (!dry_run && fname
1051 && set_rsync_acl(fname, duo_item, SMB_ACL_TYPE_DEFAULT,
1052 sxp, file->mode) < 0)
1053 unchanged = -1;
1057 return unchanged;
1060 /* Non-incremental recursion needs to convert all the received IDs.
1061 * This is done in a single pass after receiving the whole file-list. */
1062 static void match_racl_ids(const item_list *racl_list)
1064 int list_cnt, name_cnt;
1065 acl_duo *duo_item = racl_list->items;
1066 for (list_cnt = racl_list->count; list_cnt--; duo_item++) {
1067 ida_entries *idal = &duo_item->racl.names;
1068 id_access *ida = idal->idas;
1069 for (name_cnt = idal->count; name_cnt--; ida++) {
1070 if (ida->access & NAME_IS_USER)
1071 ida->id = match_uid(ida->id);
1072 else
1073 ida->id = match_gid(ida->id, NULL);
1078 void match_acl_ids(void)
1080 match_racl_ids(&access_acl_list);
1081 match_racl_ids(&default_acl_list);
1084 /* This is used by dest_mode(). */
1085 int default_perms_for_dir(const char *dir)
1087 rsync_acl racl;
1088 SMB_ACL_T sacl;
1089 BOOL ok;
1090 int perms;
1092 if (dir == NULL)
1093 dir = ".";
1094 perms = ACCESSPERMS & ~orig_umask;
1095 /* Read the directory's default ACL. If it has none, this will successfully return an empty ACL. */
1096 sacl = sys_acl_get_file(dir, SMB_ACL_TYPE_DEFAULT);
1097 if (sacl == NULL) {
1098 /* Couldn't get an ACL. Darn. */
1099 switch (errno) {
1100 #ifdef ENOTSUP
1101 case ENOTSUP:
1102 #endif
1103 case ENOSYS:
1104 /* No ACLs are available. */
1105 break;
1106 case ENOENT:
1107 if (dry_run) {
1108 /* We're doing a dry run, so the containing directory
1109 * wasn't actually created. Don't worry about it. */
1110 break;
1112 /* Otherwise fall through. */
1113 default:
1114 rprintf(FWARNING,
1115 "default_perms_for_dir: sys_acl_get_file(%s, %s): %s, falling back on umask\n",
1116 dir, str_acl_type(SMB_ACL_TYPE_DEFAULT), strerror(errno));
1118 return perms;
1121 /* Convert it. */
1122 racl = empty_rsync_acl;
1123 ok = unpack_smb_acl(sacl, &racl);
1124 sys_acl_free_acl(sacl);
1125 if (!ok) {
1126 rprintf(FWARNING, "default_perms_for_dir: unpack_smb_acl failed, falling back on umask\n");
1127 return perms;
1130 /* Apply the permission-bit entries of the default ACL, if any. */
1131 if (racl.user_obj != NO_ENTRY) {
1132 perms = rsync_acl_get_perms(&racl);
1133 if (verbose > 2)
1134 rprintf(FINFO, "got ACL-based default perms %o for directory %s\n", perms, dir);
1137 rsync_acl_free(&racl);
1138 return perms;
1141 #endif /* SUPPORT_ACLS */