Debian Codegraph @

acls.c

raw symbols SCIP
1 /*
2 * Handle passing Access Control Lists between systems.
3 *
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2006-2022 Wayne Davison
7 *
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.
12 *
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.
17 *
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.
20 */
21
22 #include "rsync.h"
23 #include "lib/sysacls.h"
24 #include "lib/acl.h"
25 #ifdef HAVE_LIBACL_AT
26 #include <fcntl.h> /* AT_EMPTY_PATH / AT_SYMLINK_NOFOLLOW */
27 #endif
28
29 #ifdef SUPPORT_ACLS
30
31 extern int dry_run;
32 extern int am_root;
33 extern int read_only;
34 extern int list_only;
35 extern mode_t orig_umask;
36 extern int numeric_ids;
37 extern int inc_recurse;
38 extern int preserve_devices;
39 extern int preserve_specials;
40
41 /* Flags used to indicate what items are being transmitted for an entry. */
42 #define XMIT_USER_OBJ (1<<0)
43 #define XMIT_GROUP_OBJ (1<<1)
44 #define XMIT_MASK_OBJ (1<<2)
45 #define XMIT_OTHER_OBJ (1<<3)
46 #define XMIT_NAME_LIST (1<<4)
47
48 #define NO_ENTRY ((uchar)0x80) /* Default value of a NON-name-list entry. */
49
50 #define NAME_IS_USER (1u<<31) /* Bit used only on a name-list entry. */
51
52 /* When we send the access bits over the wire, we shift them 2 bits to the
53 * left and use the lower 2 bits as flags (relevant only to a name entry).
54 * This makes the protocol more efficient than sending a value that would
55 * be likely to have its highest bits set. */
56 #define XFLAG_NAME_FOLLOWS 0x0001u
57 #define XFLAG_NAME_IS_USER 0x0002u
58
59 /* === ACL structures === */
60
61 typedef struct {
62 id_t id;
63 uint32 access;
64 } id_access;
65
66 typedef struct {
67 id_access *idas;
68 int count;
69 } ida_entries;
70
71 typedef struct {
72 char *name;
73 uchar len;
74 } idname;
75
76 typedef struct rsync_acl {
77 ida_entries names;
78 /* These will be NO_ENTRY if there's no such entry. */
79 uchar user_obj;
80 uchar group_obj;
81 uchar mask_obj;
82 uchar other_obj;
83 } rsync_acl;
84
85 typedef struct {
86 rsync_acl racl;
87 SMB_ACL_T sacl;
88 } acl_duo;
89
90 static const rsync_acl empty_rsync_acl = {
91 {NULL, 0}, NO_ENTRY, NO_ENTRY, NO_ENTRY, NO_ENTRY
92 };
93
94 static item_list access_acl_list = EMPTY_ITEM_LIST;
95 static item_list default_acl_list = EMPTY_ITEM_LIST;
96
97 static size_t prior_access_count = (size_t)-1;
98 static size_t prior_default_count = (size_t)-1;
99
100 /* === Calculations on ACL types === */
101
102 static const char *str_acl_type(SMB_ACL_TYPE_T type)
103 {
104 switch (type) {
105 case SMB_ACL_TYPE_ACCESS:
106 #ifdef HAVE_OSX_ACLS
107 return "ACL_TYPE_EXTENDED";
108 #else
109 return "ACL_TYPE_ACCESS";
110 #endif
111 case SMB_ACL_TYPE_DEFAULT:
112 return "ACL_TYPE_DEFAULT";
113 default:
114 break;
115 }
116 return "unknown ACL type!";
117 }
118
119 static int calc_sacl_entries(const rsync_acl *racl)
120 {
121 /* A System ACL always gets user/group/other permission entries. */
122 return racl->names.count
123 #ifdef ACLS_NEED_MASK
124 + 1
125 #else
126 + (racl->mask_obj != NO_ENTRY)
127 #endif
128 + 3;
129 }
130
131 /* Extracts and returns the permission bits from the ACL. This cannot be
132 * called on an rsync_acl that has NO_ENTRY in any spot but the mask. */
133 static int rsync_acl_get_perms(const rsync_acl *racl)
134 {
135 return (racl->user_obj << 6)
136 + ((racl->mask_obj != NO_ENTRY ? racl->mask_obj : racl->group_obj) << 3)
137 + racl->other_obj;
138 }
139
140 /* Removes the permission-bit entries from the ACL because these
141 * can be reconstructed from the file's mode. */
142 static void rsync_acl_strip_perms(stat_x *sxp)
143 {
144 rsync_acl *racl = sxp->acc_acl;
145
146 racl->user_obj = NO_ENTRY;
147 if (racl->mask_obj == NO_ENTRY)
148 racl->group_obj = NO_ENTRY;
149 else {
150 int group_perms = (sxp->st.st_mode >> 3) & 7;
151 if (racl->group_obj == group_perms)
152 racl->group_obj = NO_ENTRY;
153 #ifndef HAVE_SOLARIS_ACLS
154 if (racl->names.count != 0 && racl->mask_obj == group_perms)
155 racl->mask_obj = NO_ENTRY;
156 #endif
157 }
158 racl->other_obj = NO_ENTRY;
159 }
160
161 /* Given an empty rsync_acl, fake up the permission bits. */
162 static void rsync_acl_fake_perms(rsync_acl *racl, mode_t mode)
163 {
164 racl->user_obj = (mode >> 6) & 7;
165 racl->group_obj = (mode >> 3) & 7;
166 racl->other_obj = mode & 7;
167 }
168
169 /* === Rsync ACL functions === */
170
171 static rsync_acl *create_racl(void)
172 {
173 rsync_acl *racl = new(rsync_acl);
174
175 *racl = empty_rsync_acl;
176
177 return racl;
178 }
179
180 static BOOL ida_entries_equal(const ida_entries *ial1, const ida_entries *ial2)
181 {
182 id_access *ida1, *ida2;
183 int count = ial1->count;
184 if (count != ial2->count)
185 return False;
186 ida1 = ial1->idas;
187 ida2 = ial2->idas;
188 for (; count--; ida1++, ida2++) {
189 if (ida1->access != ida2->access || ida1->id != ida2->id)
190 return False;
191 }
192 return True;
193 }
194
195 static BOOL rsync_acl_equal(const rsync_acl *racl1, const rsync_acl *racl2)
196 {
197 return racl1->user_obj == racl2->user_obj
198 && racl1->group_obj == racl2->group_obj
199 && racl1->mask_obj == racl2->mask_obj
200 && racl1->other_obj == racl2->other_obj
201 && ida_entries_equal(&racl1->names, &racl2->names);
202 }
203
204 /* Are the extended (non-permission-bit) entries equal? If so, the rest of
205 * the ACL will be handled by the normal mode-preservation code. This is
206 * only meaningful for access ACLs! Note: the 1st arg is a fully-populated
207 * rsync_acl, but the 2nd parameter can be a condensed rsync_acl, which means
208 * that it might have several of its permission objects set to NO_ENTRY. */
209 static BOOL rsync_acl_equal_enough(const rsync_acl *racl1,
210 const rsync_acl *racl2, mode_t m)
211 {
212 if ((racl1->mask_obj ^ racl2->mask_obj) & NO_ENTRY)
213 return False; /* One has a mask and the other doesn't */
214
215 /* When there's a mask, the group_obj becomes an extended entry. */
216 if (racl1->mask_obj != NO_ENTRY) {
217 /* A condensed rsync_acl with a mask can only have no
218 * group_obj when it was identical to the mask. This
219 * means that it was also identical to the group attrs
220 * from the mode. */
221 if (racl2->group_obj == NO_ENTRY) {
222 if (racl1->group_obj != ((m >> 3) & 7))
223 return False;
224 } else if (racl1->group_obj != racl2->group_obj)
225 return False;
226 }
227 return ida_entries_equal(&racl1->names, &racl2->names);
228 }
229
230 static void rsync_acl_free(rsync_acl *racl)
231 {
232 if (racl->names.idas)
233 free(racl->names.idas);
234 *racl = empty_rsync_acl;
235 }
236
237 void free_acl(stat_x *sxp)
238 {
239 if (sxp->acc_acl) {
240 rsync_acl_free(sxp->acc_acl);
241 free(sxp->acc_acl);
242 sxp->acc_acl = NULL;
243 }
244 if (sxp->def_acl) {
245 rsync_acl_free(sxp->def_acl);
246 free(sxp->def_acl);
247 sxp->def_acl = NULL;
248 }
249 }
250
251 #ifdef SMB_ACL_NEED_SORT
252 static int id_access_sorter(const void *r1, const void *r2)
253 {
254 id_access *ida1 = (id_access *)r1;
255 id_access *ida2 = (id_access *)r2;
256 id_t rid1 = ida1->id, rid2 = ida2->id;
257 if ((ida1->access ^ ida2->access) & NAME_IS_USER)
258 return ida1->access & NAME_IS_USER ? -1 : 1;
259 return rid1 == rid2 ? 0 : rid1 < rid2 ? -1 : 1;
260 }
261 #endif
262
263 /* === System ACLs === */
264
265 /* Unpack system ACL -> rsync ACL verbatim. Return whether we succeeded. */
266 static BOOL unpack_smb_acl(SMB_ACL_T sacl, rsync_acl *racl)
267 {
268 static item_list temp_ida_list = EMPTY_ITEM_LIST;
269 SMB_ACL_ENTRY_T entry;
270 const char *errfun;
271 int rc;
272
273 errfun = "sys_acl_get_entry";
274 for (rc = sys_acl_get_entry(sacl, SMB_ACL_FIRST_ENTRY, &entry);
275 rc == 1;
276 rc = sys_acl_get_entry(sacl, SMB_ACL_NEXT_ENTRY, &entry)) {
277 SMB_ACL_TAG_T tag_type;
278 uint32 access;
279 id_t g_u_id;
280 id_access *ida;
281 if ((rc = sys_acl_get_info(entry, &tag_type, &access, &g_u_id)) != 0) {
282 errfun = "sys_acl_get_info";
283 break;
284 }
285 /* continue == done with entry; break == store in temporary ida list */
286 switch (tag_type) {
287 #ifndef HAVE_OSX_ACLS
288 case SMB_ACL_USER_OBJ:
289 if (racl->user_obj == NO_ENTRY)
290 racl->user_obj = access;
291 else
292 rprintf(FINFO, "unpack_smb_acl: warning: duplicate USER_OBJ entry ignored\n");
293 continue;
294 case SMB_ACL_GROUP_OBJ:
295 if (racl->group_obj == NO_ENTRY)
296 racl->group_obj = access;
297 else
298 rprintf(FINFO, "unpack_smb_acl: warning: duplicate GROUP_OBJ entry ignored\n");
299 continue;
300 case SMB_ACL_MASK:
301 if (racl->mask_obj == NO_ENTRY)
302 racl->mask_obj = access;
303 else
304 rprintf(FINFO, "unpack_smb_acl: warning: duplicate MASK entry ignored\n");
305 continue;
306 case SMB_ACL_OTHER:
307 if (racl->other_obj == NO_ENTRY)
308 racl->other_obj = access;
309 else
310 rprintf(FINFO, "unpack_smb_acl: warning: duplicate OTHER entry ignored\n");
311 continue;
312 #endif
313 case SMB_ACL_USER:
314 access |= NAME_IS_USER;
315 break;
316 case SMB_ACL_GROUP:
317 break;
318 default:
319 rprintf(FINFO, "unpack_smb_acl: warning: entry with unrecognized tag type ignored\n");
320 continue;
321 }
322 ida = EXPAND_ITEM_LIST(&temp_ida_list, id_access, -10);
323 ida->id = g_u_id;
324 ida->access = access;
325 }
326 if (rc) {
327 rsyserr(FERROR_XFER, errno, "unpack_smb_acl: %s()", errfun);
328 rsync_acl_free(racl);
329 return False;
330 }
331
332 /* Transfer the count id_access items out of the temp_ida_list
333 * into the names ida_entries list in racl. */
334 if (temp_ida_list.count) {
335 #ifdef SMB_ACL_NEED_SORT
336 if (temp_ida_list.count > 1) {
337 qsort(temp_ida_list.items, temp_ida_list.count, sizeof (id_access), id_access_sorter);
338 }
339 #endif
340 racl->names.idas = new_array(id_access, temp_ida_list.count);
341 memcpy(racl->names.idas, temp_ida_list.items, temp_ida_list.count * sizeof (id_access));
342 } else
343 racl->names.idas = NULL;
344
345 racl->names.count = temp_ida_list.count;
346
347 /* Truncate the temporary list now that its idas have been saved. */
348 temp_ida_list.count = 0;
349
350 return True;
351 }
352
353 /* Synactic sugar for system calls */
354
355 #define CALL_OR_ERROR(func,args,str) \
356 do { \
357 if (func args) { \
358 errfun = str; \
359 goto error_exit; \
360 } \
361 } while (0)
362
363 #define COE(func,args) CALL_OR_ERROR(func,args,#func)
364 #define COE2(func,args) CALL_OR_ERROR(func,args,NULL)
365
366 #ifndef HAVE_OSX_ACLS
367 /* Store the permissions in the system ACL entry. */
368 static int store_access_in_entry(uint32 access, SMB_ACL_ENTRY_T entry)
369 {
370 if (sys_acl_set_access_bits(entry, access)) {
371 rsyserr(FERROR_XFER, errno, "store_access_in_entry sys_acl_set_access_bits()");
372 return -1;
373 }
374 return 0;
375 }
376 #endif
377
378 /* Pack rsync ACL -> system ACL verbatim. Return whether we succeeded. */
379 static BOOL pack_smb_acl(SMB_ACL_T *smb_acl, const rsync_acl *racl)
380 {
381 #ifdef ACLS_NEED_MASK
382 uchar mask_bits;
383 #endif
384 size_t count;
385 id_access *ida;
386 const char *errfun = NULL;
387 SMB_ACL_ENTRY_T entry;
388
389 if (!(*smb_acl = sys_acl_init(calc_sacl_entries(racl)))) {
390 rsyserr(FERROR_XFER, errno, "pack_smb_acl: sys_acl_init()");
391 return False;
392 }
393
394 #ifndef HAVE_OSX_ACLS
395 COE( sys_acl_create_entry,(smb_acl, &entry) );
396 COE( sys_acl_set_info,(entry, SMB_ACL_USER_OBJ, racl->user_obj & ~NO_ENTRY, 0) );
397 #endif
398
399 for (ida = racl->names.idas, count = racl->names.count; count; ida++, count--) {
400 #ifdef SMB_ACL_NEED_SORT
401 if (!(ida->access & NAME_IS_USER))
402 break;
403 #endif
404 COE( sys_acl_create_entry,(smb_acl, &entry) );
405 COE( sys_acl_set_info,
406 (entry,
407 ida->access & NAME_IS_USER ? SMB_ACL_USER : SMB_ACL_GROUP,
408 ida->access & ~NAME_IS_USER, ida->id) );
409 }
410
411 #ifndef HAVE_OSX_ACLS
412 COE( sys_acl_create_entry,(smb_acl, &entry) );
413 COE( sys_acl_set_info,(entry, SMB_ACL_GROUP_OBJ, racl->group_obj & ~NO_ENTRY, 0) );
414
415 #ifdef SMB_ACL_NEED_SORT
416 for ( ; count; ida++, count--) {
417 COE( sys_acl_create_entry,(smb_acl, &entry) );
418 COE( sys_acl_set_info,(entry, SMB_ACL_GROUP, ida->access, ida->id) );
419 }
420 #endif
421
422 #ifdef ACLS_NEED_MASK
423 mask_bits = racl->mask_obj == NO_ENTRY ? racl->group_obj & ~NO_ENTRY : racl->mask_obj;
424 COE( sys_acl_create_entry,(smb_acl, &entry) );
425 COE( sys_acl_set_info,(entry, SMB_ACL_MASK, mask_bits, 0) );
426 #else
427 if (racl->mask_obj != NO_ENTRY) {
428 COE( sys_acl_create_entry,(smb_acl, &entry) );
429 COE( sys_acl_set_info,(entry, SMB_ACL_MASK, racl->mask_obj, 0) );
430 }
431 #endif
432
433 COE( sys_acl_create_entry,(smb_acl, &entry) );
434 COE( sys_acl_set_info,(entry, SMB_ACL_OTHER, racl->other_obj & ~NO_ENTRY, 0) );
435 #endif
436
437 #ifdef DEBUG
438 if (sys_acl_valid(*smb_acl) < 0)
439 rprintf(FERROR_XFER, "pack_smb_acl: warning: system says the ACL I packed is invalid\n");
440 #endif
441
442 return True;
443
444 error_exit:
445 if (errfun) {
446 rsyserr(FERROR_XFER, errno, "pack_smb_acl %s()", errfun);
447 }
448 sys_acl_free_acl(*smb_acl);
449 return False;
450 }
451
452 static int find_matching_rsync_acl(const rsync_acl *racl, SMB_ACL_TYPE_T type,
453 const item_list *racl_list)
454 {
455 static int access_match = -1, default_match = -1;
456 int *match = type == SMB_ACL_TYPE_ACCESS ? &access_match : &default_match;
457 size_t count = racl_list->count;
458
459 /* If this is the first time through or we didn't match the last
460 * time, then start at the end of the list, which should be the
461 * best place to start hunting. */
462 if (*match == -1)
463 *match = racl_list->count - 1;
464 while (count--) {
465 rsync_acl *base = racl_list->items;
466 if (rsync_acl_equal(base + *match, racl))
467 return *match;
468 if (!(*match)--)
469 *match = racl_list->count - 1;
470 }
471
472 *match = -1;
473 return *match;
474 }
475
476 /* These two bridge lib/acl.c's neutral (tag,perm,id) entry array; with
477 * HAVE_LIBACL_AT the libacl *_at path uses unpack_smb_acl/pack_smb_acl directly,
478 * so they are unused there. */
479 #if defined(SUPPORT_ACL_FD) && !defined(HAVE_LIBACL_AT)
480 /* Convert a packed system ACL into the neutral (tag,perm,id) entry array that
481 * lib/acl.c serializes. Reuses pack_smb_acl()+change_sacl_perms() output so
482 * the bytes we write match exactly what acl_set_file() would have written.
483 * Returns the entry count and a malloc'd array in *ents_p, or -1 on error. */
484 static int sacl_to_entries(SMB_ACL_T sacl, rsync_acl_ent **ents_p)
485 {
486 static item_list ent_list = EMPTY_ITEM_LIST;
487 SMB_ACL_ENTRY_T entry;
488 rsync_acl_ent *out;
489 int rc;
490
491 ent_list.count = 0;
492 for (rc = sys_acl_get_entry(sacl, SMB_ACL_FIRST_ENTRY, &entry); rc == 1;
493 rc = sys_acl_get_entry(sacl, SMB_ACL_NEXT_ENTRY, &entry)) {
494 SMB_ACL_TAG_T tag_type;
495 uint32 access;
496 id_t g_u_id;
497 rsync_acl_ent *e;
498 uint16_t tag;
499
500 if ((rc = sys_acl_get_info(entry, &tag_type, &access, &g_u_id)) != 0)
501 break;
502 switch (tag_type) {
503 case SMB_ACL_USER_OBJ: tag = RACL_USER_OBJ; break;
504 case SMB_ACL_USER: tag = RACL_USER; break;
505 case SMB_ACL_GROUP_OBJ: tag = RACL_GROUP_OBJ; break;
506 case SMB_ACL_GROUP: tag = RACL_GROUP; break;
507 case SMB_ACL_MASK: tag = RACL_MASK; break;
508 case SMB_ACL_OTHER: tag = RACL_OTHER; break;
509 default: continue; /* skip an unrecognized tag */
510 }
511 e = EXPAND_ITEM_LIST(&ent_list, rsync_acl_ent, -10);
512 e->tag = tag;
513 e->perm = access & 7;
514 e->id = (tag == RACL_USER || tag == RACL_GROUP) ? (uint32_t)g_u_id : RACL_UNDEFINED_ID;
515 }
516 if (rc) {
517 rsyserr(FERROR_XFER, errno, "sacl_to_entries: sys_acl_get_entry/info()");
518 return -1;
519 }
520
521 out = new_array(rsync_acl_ent, ent_list.count ? ent_list.count : 1);
522 if (ent_list.count)
523 memcpy(out, ent_list.items, ent_list.count * sizeof (rsync_acl_ent));
524 *ents_p = out;
525 return ent_list.count;
526 }
527
528 /* Unpack a neutral entry array (from lib/acl.c) into an rsync_acl, mirroring
529 * unpack_smb_acl()'s tag handling. */
530 static BOOL unpack_acl_entries(const rsync_acl_ent *ents, int n, rsync_acl *racl)
531 {
532 static item_list temp_ida_list = EMPTY_ITEM_LIST;
533 int i;
534
535 temp_ida_list.count = 0;
536 for (i = 0; i < n; i++) {
537 uint32 access = ents[i].perm & 7;
538 id_access *ida;
539
540 switch (ents[i].tag) {
541 case RACL_USER_OBJ:
542 if (racl->user_obj == NO_ENTRY)
543 racl->user_obj = access;
544 continue;
545 case RACL_GROUP_OBJ:
546 if (racl->group_obj == NO_ENTRY)
547 racl->group_obj = access;
548 continue;
549 case RACL_MASK:
550 if (racl->mask_obj == NO_ENTRY)
551 racl->mask_obj = access;
552 continue;
553 case RACL_OTHER:
554 if (racl->other_obj == NO_ENTRY)
555 racl->other_obj = access;
556 continue;
557 case RACL_USER:
558 access |= NAME_IS_USER;
559 break;
560 case RACL_GROUP:
561 break;
562 default:
563 continue;
564 }
565 ida = EXPAND_ITEM_LIST(&temp_ida_list, id_access, -10);
566 ida->id = ents[i].id;
567 ida->access = access;
568 }
569
570 if (temp_ida_list.count) {
571 #ifdef SMB_ACL_NEED_SORT
572 if (temp_ida_list.count > 1)
573 qsort(temp_ida_list.items, temp_ida_list.count, sizeof (id_access), id_access_sorter);
574 #endif
575 racl->names.idas = new_array(id_access, temp_ida_list.count);
576 memcpy(racl->names.idas, temp_ida_list.items, temp_ida_list.count * sizeof (id_access));
577 } else
578 racl->names.idas = NULL;
579 racl->names.count = temp_ida_list.count;
580 temp_ida_list.count = 0;
581
582 return True;
583 }
584 #endif /* SUPPORT_ACL_FD */
585
586 static int get_rsync_acl(int fd, int dirfd, const char *leaf, const char *fname,
587 rsync_acl *racl, SMB_ACL_TYPE_T type, mode_t mode)
588 {
589 SMB_ACL_T sacl;
590
591 #ifndef SUPPORT_ACL_FD
592 #ifndef HAVE_SOLARIS_ACLS
593 (void)fd; /* Solaris drives the ACL via facl(2) on fd but has no SUPPORT_ACL_FD. */
594 #endif
595 (void)dirfd;
596 (void)leaf;
597 #endif
598
599 #ifdef SUPPORT_XATTRS
600 /* --fake-super support: load ACLs from an xattr. */
601 if (am_root < 0) {
602 char *buf;
603 size_t len;
604 int cnt;
605
606 if ((buf = get_xattr_acl(fname, fd, type == SMB_ACL_TYPE_ACCESS, &len)) == NULL)
607 return 0;
608 cnt = (len - 4*4) / (4+4);
609 if (len < 4*4 || len != (size_t)cnt*(4+4) + 4*4) {
610 free(buf);
611 return -1;
612 }
613
614 racl->user_obj = IVAL(buf, 0);
615 if (racl->user_obj == NO_ENTRY)
616 racl->user_obj = (mode >> 6) & 7;
617 racl->group_obj = IVAL(buf, 4);
618 if (racl->group_obj == NO_ENTRY)
619 racl->group_obj = (mode >> 3) & 7;
620 racl->mask_obj = IVAL(buf, 8);
621 racl->other_obj = IVAL(buf, 12);
622 if (racl->other_obj == NO_ENTRY)
623 racl->other_obj = mode & 7;
624
625 if (cnt) {
626 char *bp = buf + 4*4;
627 id_access *ida = racl->names.idas = new_array(id_access, cnt);
628 racl->names.count = cnt;
629 for ( ; cnt--; ida++, bp += 4+4) {
630 ida->id = IVAL(bp, 0);
631 ida->access = IVAL(bp, 4);
632 }
633 }
634 free(buf);
635 return 0;
636 }
637 #endif
638
639 #ifdef HAVE_SOLARIS_ACLS
640 /* Solaris has no libacl *_at; read the ACL through the held fd via facl(2)
641 * when we have one. With no held fd this branch is skipped and the path-based
642 * call below reads the ACL (acceptable: a read can't redirect a write out of
643 * the tree). */
644 if (fd >= 0) {
645 if ((sacl = sys_acl_get_fd_type(fd, type)) != 0) {
646 BOOL ok = unpack_smb_acl(sacl, racl);
647
648 sys_acl_free_acl(sacl);
649 if (!ok) {
650 rsyserr(FERROR_XFER, errno, "get_acl: unpack_smb_acl(%s)", fname);
651 return -1;
652 }
653 return 0;
654 }
655 if (no_acl_syscall_error(errno)) {
656 if (type == SMB_ACL_TYPE_ACCESS)
657 rsync_acl_fake_perms(racl, mode);
658 return 0;
659 }
660 rsyserr(FERROR_XFER, errno, "get_acl: sys_acl_get_fd_type(%s, %s)",
661 fname, str_acl_type(type));
662 return -1;
663 }
664 #endif
665
666 #ifdef SUPPORT_ACL_FD
667 #ifdef HAVE_LIBACL_AT
668 /* Read the ACL via the new libacl *_at calls; fd<0 && dirfd<0
669 * (e.g. a synthetic dir) falls through to the path-based call below. */
670 if (fd >= 0 || dirfd >= 0) {
671 if (fd >= 0)
672 sacl = sys_acl_get_file_at(fd, "", AT_EMPTY_PATH, type);
673 else
674 sacl = sys_acl_get_file_at(dirfd, leaf, AT_SYMLINK_NOFOLLOW, type);
675 if (sacl != 0) {
676 BOOL ok = unpack_smb_acl(sacl, racl);
677 sys_acl_free_acl(sacl);
678 if (!ok) {
679 rsyserr(FERROR_XFER, errno, "get_acl: unpack_smb_acl(%s)", fname);
680 return -1;
681 }
682 return 0;
683 }
684 if (no_acl_syscall_error(errno)) {
685 if (type == SMB_ACL_TYPE_ACCESS)
686 rsync_acl_fake_perms(racl, mode);
687 return 0;
688 }
689 rsyserr(FERROR_XFER, errno, "get_acl: acl_get_file_at(%s, %s)",
690 fname, str_acl_type(type));
691 return -1;
692 }
693 #else
694 /* Race-safe path: read the ACL through the held O_NOFOLLOW fd, or via
695 * setxattrat(AT_SYMLINK_NOFOLLOW) on dirfd+leaf, instead of re-resolving
696 * fname. Only for real-root ACLs (am_root >= 0; the fake-super branch
697 * above already returned). */
698 if (fd >= 0 || (dirfd >= 0 && xacl_at_available())) {
699 int is_def = type == SMB_ACL_TYPE_DEFAULT;
700 rsync_acl_ent *ents = NULL;
701 int n = 0, rc;
702
703 if (fd >= 0)
704 rc = xacl_get_fd(fd, is_def, &ents, &n);
705 else
706 rc = xacl_get_at(dirfd, leaf, is_def, &ents, &n);
707 if (rc < 0) {
708 if (no_acl_syscall_error(errno)) {
709 if (type == SMB_ACL_TYPE_ACCESS)
710 rsync_acl_fake_perms(racl, mode);
711 return 0;
712 }
713 rsyserr(FERROR_XFER, errno, "get_acl: xacl_get(%s, %s)",
714 fname, str_acl_type(type));
715 return -1;
716 }
717 if (n == 0) {
718 /* No explicit ACL: mirror libacl's mode-derived access ACL
719 * (an absent default ACL stays empty). */
720 if (type == SMB_ACL_TYPE_ACCESS)
721 rsync_acl_fake_perms(racl, mode);
722 } else if (!unpack_acl_entries(ents, n, racl)) {
723 if (ents)
724 free(ents);
725 rsyserr(FERROR_XFER, errno, "get_acl: unpack_acl_entries(%s)", fname);
726 return -1;
727 }
728 if (ents)
729 free(ents);
730 return 0;
731 }
732 /* Neither a held fd nor a usable dirfd path (xacl_at_available() covers the
733 * *xattrat syscalls AND the pre-6.13 /proc/self/fd compat, so this is the
734 * BSDs / a /proc-less namespace / an un-pinnable entry): read the real
735 * destination ACL via the path-based call rather than a mode-only fake, so
736 * --acls stays functional where the race-safe primitive is unavailable. */
737 #endif /* HAVE_LIBACL_AT */
738 #endif
739
740 if ((sacl = sys_acl_get_file(fname, type)) != 0) {
741 BOOL ok = unpack_smb_acl(sacl, racl);
742
743 sys_acl_free_acl(sacl);
744 if (!ok) {
745 rsyserr(FERROR_XFER, errno, "get_acl: unpack_smb_acl(%s)", fname);
746 return -1;
747 }
748 } else if (no_acl_syscall_error(errno)) {
749 /* ACLs are not supported, so pretend we have a basic ACL. */
750 if (type == SMB_ACL_TYPE_ACCESS)
751 rsync_acl_fake_perms(racl, mode);
752 } else {
753 rsyserr(FERROR_XFER, errno, "get_acl: sys_acl_get_file(%s, %s)",
754 fname, str_acl_type(type));
755 return -1;
756 }
757
758 return 0;
759 }
760
761 /* Return the Access Control List for the given filename. When a held
762 * O_NOFOLLOW fd (or a dirfd+leaf) is available, the ACL is read race-safely
763 * through it; otherwise (fd < 0 && dirfd < 0) the path-based fallback is used. */
764 int get_acl_fdat(int fd, int dirfd, const char *leaf, const char *fname, stat_x *sxp)
765 {
766 sxp->acc_acl = create_racl();
767
768 if (S_ISREG(sxp->st.st_mode) || S_ISDIR(sxp->st.st_mode)) {
769 /* Everyone supports this. */
770 } else if (S_ISLNK(sxp->st.st_mode)) {
771 return 0;
772 } else if (IS_SPECIAL(sxp->st.st_mode)) {
773 #ifndef NO_SPECIAL_ACLS
774 if (!preserve_specials)
775 #endif
776 return 0;
777 } else if (IS_DEVICE(sxp->st.st_mode)) {
778 #ifndef NO_DEVICE_ACLS
779 if (!preserve_devices)
780 #endif
781 return 0;
782 } else if (IS_MISSING_FILE(sxp->st))
783 return 0;
784
785 if (get_rsync_acl(fd, dirfd, leaf, fname, sxp->acc_acl, SMB_ACL_TYPE_ACCESS,
786 sxp->st.st_mode) < 0) {
787 free_acl(sxp);
788 return -1;
789 }
790
791 if (S_ISDIR(sxp->st.st_mode)) {
792 sxp->def_acl = create_racl();
793 if (get_rsync_acl(fd, dirfd, leaf, fname, sxp->def_acl, SMB_ACL_TYPE_DEFAULT,
794 sxp->st.st_mode) < 0) {
795 free_acl(sxp);
796 return -1;
797 }
798 }
799
800 return 0;
801 }
802
803 int get_acl(const char *fname, stat_x *sxp)
804 {
805 return get_acl_fdat(-1, -1, NULL, fname, sxp);
806 }
807
808 /* === Send functions === */
809
810 /* Send the ida list over the file descriptor. */
811 static void send_ida_entries(int f, const ida_entries *idal)
812 {
813 id_access *ida;
814 size_t count = idal->count;
815
816 write_varint(f, idal->count);
817
818 for (ida = idal->idas; count--; ida++) {
819 uint32 xbits = ida->access << 2;
820 const char *name;
821 if (ida->access & NAME_IS_USER) {
822 xbits |= XFLAG_NAME_IS_USER;
823 name = numeric_ids ? NULL : add_uid(ida->id);
824 } else
825 name = numeric_ids ? NULL : add_gid(ida->id);
826 write_varint(f, ida->id);
827 if (inc_recurse && name) {
828 int len = strlen(name);
829 write_varint(f, xbits | XFLAG_NAME_FOLLOWS);
830 write_byte(f, len);
831 write_buf(f, name, len);
832 } else
833 write_varint(f, xbits);
834 }
835 }
836
837 static void send_rsync_acl(int f, rsync_acl *racl, SMB_ACL_TYPE_T type,
838 item_list *racl_list)
839 {
840 int ndx = find_matching_rsync_acl(racl, type, racl_list);
841
842 /* Send 0 (-1 + 1) to indicate that literal ACL data follows. */
843 write_varint(f, ndx + 1);
844
845 if (ndx < 0) {
846 rsync_acl *new_racl = EXPAND_ITEM_LIST(racl_list, rsync_acl, 1000);
847 uchar flags = 0;
848
849 if (racl->user_obj != NO_ENTRY)
850 flags |= XMIT_USER_OBJ;
851 if (racl->group_obj != NO_ENTRY)
852 flags |= XMIT_GROUP_OBJ;
853 if (racl->mask_obj != NO_ENTRY)
854 flags |= XMIT_MASK_OBJ;
855 if (racl->other_obj != NO_ENTRY)
856 flags |= XMIT_OTHER_OBJ;
857 if (racl->names.count)
858 flags |= XMIT_NAME_LIST;
859
860 write_byte(f, flags);
861
862 if (flags & XMIT_USER_OBJ)
863 write_varint(f, racl->user_obj);
864 if (flags & XMIT_GROUP_OBJ)
865 write_varint(f, racl->group_obj);
866 if (flags & XMIT_MASK_OBJ)
867 write_varint(f, racl->mask_obj);
868 if (flags & XMIT_OTHER_OBJ)
869 write_varint(f, racl->other_obj);
870 if (flags & XMIT_NAME_LIST)
871 send_ida_entries(f, &racl->names);
872
873 /* Give the allocated data to the new list object. */
874 *new_racl = *racl;
875 *racl = empty_rsync_acl;
876 }
877 }
878
879 /* Send the ACL from the stat_x structure down the indicated file descriptor.
880 * This also frees the ACL data. */
881 void send_acl(int f, stat_x *sxp)
882 {
883 if (!sxp->acc_acl) {
884 sxp->acc_acl = create_racl();
885 rsync_acl_fake_perms(sxp->acc_acl, sxp->st.st_mode);
886 }
887 /* Avoid sending values that can be inferred from other data. */
888 rsync_acl_strip_perms(sxp);
889
890 send_rsync_acl(f, sxp->acc_acl, SMB_ACL_TYPE_ACCESS, &access_acl_list);
891
892 if (S_ISDIR(sxp->st.st_mode)) {
893 if (!sxp->def_acl)
894 sxp->def_acl = create_racl();
895
896 send_rsync_acl(f, sxp->def_acl, SMB_ACL_TYPE_DEFAULT, &default_acl_list);
897 }
898 }
899
900 /* === Receive functions === */
901
902 static uint32 recv_acl_access(int f, uchar *name_follows_ptr)
903 {
904 uint32 access = read_varint(f);
905
906 if (name_follows_ptr) {
907 int flags = access & 3;
908 access >>= 2;
909 if (am_root >= 0 && access & ~SMB_ACL_VALID_NAME_BITS)
910 goto value_error;
911 if (flags & XFLAG_NAME_FOLLOWS)
912 *name_follows_ptr = 1;
913 else
914 *name_follows_ptr = 0;
915 if (flags & XFLAG_NAME_IS_USER)
916 access |= NAME_IS_USER;
917 } else if (am_root >= 0 && access & ~SMB_ACL_VALID_OBJ_BITS) {
918 value_error:
919 rprintf(FERROR_XFER, "recv_acl_access: value out of range: %x\n",
920 access);
921 exit_cleanup(RERR_STREAMIO);
922 }
923
924 return access;
925 }
926
927 static uchar recv_ida_entries(int f, ida_entries *ent)
928 {
929 uchar computed_mask_bits = 0;
930 int i, count = read_varint_bounded(f, 0, MAX_WIRE_ACL_COUNT, "ACL count");
931
932 ent->idas = count ? new_array(id_access, count) : NULL;
933 ent->count = count;
934
935 for (i = 0; i < count; i++) {
936 uchar has_name;
937 id_t id = read_varint(f);
938 uint32 access = recv_acl_access(f, &has_name);
939
940 if (has_name) {
941 if (access & NAME_IS_USER)
942 id = recv_user_name(f, id);
943 else
944 id = recv_group_name(f, id, NULL);
945 } else if (access & NAME_IS_USER) {
946 if (inc_recurse && !numeric_ids)
947 id = match_uid(id);
948 } else {
949 if (inc_recurse && (!am_root || !numeric_ids))
950 id = match_gid(id, NULL);
951 }
952
953 ent->idas[i].id = id;
954 ent->idas[i].access = access;
955 computed_mask_bits |= access;
956 }
957
958 return computed_mask_bits & ~NO_ENTRY;
959 }
960
961 static int recv_rsync_acl(int f, item_list *racl_list, SMB_ACL_TYPE_T type, mode_t mode)
962 {
963 uchar computed_mask_bits = 0;
964 acl_duo *duo_item;
965 uchar flags;
966 int ndx = read_varint(f);
967
968 if (ndx < 0 || (size_t)ndx > racl_list->count) {
969 rprintf(FERROR_XFER, "recv_acl_index: %s ACL index %d > %d\n",
970 str_acl_type(type), ndx, (int)racl_list->count);
971 exit_cleanup(RERR_STREAMIO);
972 }
973
974 if (ndx != 0)
975 return ndx - 1;
976
977 ndx = racl_list->count;
978 duo_item = EXPAND_ITEM_LIST(racl_list, acl_duo, 1000);
979 duo_item->racl = empty_rsync_acl;
980
981 flags = read_byte(f);
982
983 if (flags & XMIT_USER_OBJ)
984 duo_item->racl.user_obj = recv_acl_access(f, NULL);
985 if (flags & XMIT_GROUP_OBJ)
986 duo_item->racl.group_obj = recv_acl_access(f, NULL);
987 if (flags & XMIT_MASK_OBJ)
988 duo_item->racl.mask_obj = recv_acl_access(f, NULL);
989 if (flags & XMIT_OTHER_OBJ)
990 duo_item->racl.other_obj = recv_acl_access(f, NULL);
991 if (flags & XMIT_NAME_LIST)
992 computed_mask_bits |= recv_ida_entries(f, &duo_item->racl.names);
993
994 #ifdef HAVE_OSX_ACLS
995 /* If we received a superfluous mask, throw it away. */
996 duo_item->racl.mask_obj = NO_ENTRY;
997 (void)mode;
998 (void)computed_mask_bits;
999 #else
1000 if (duo_item->racl.names.count && duo_item->racl.mask_obj == NO_ENTRY) {
1001 /* Mask must be non-empty with lists. */
1002 if (type == SMB_ACL_TYPE_ACCESS)
1003 computed_mask_bits = (mode >> 3) & 7;
1004 else
1005 computed_mask_bits |= duo_item->racl.group_obj & ~NO_ENTRY;
1006 duo_item->racl.mask_obj = computed_mask_bits;
1007 }
1008 #endif
1009
1010 duo_item->sacl = NULL;
1011
1012 return ndx;
1013 }
1014
1015 /* Receive the ACL info the sender has included for this file-list entry. */
1016 void receive_acl(int f, struct file_struct *file)
1017 {
1018 F_ACL(file) = recv_rsync_acl(f, &access_acl_list, SMB_ACL_TYPE_ACCESS, file->mode);
1019
1020 if (S_ISDIR(file->mode))
1021 F_DIR_DEFACL(file) = recv_rsync_acl(f, &default_acl_list, SMB_ACL_TYPE_DEFAULT, 0);
1022 }
1023
1024 static int cache_rsync_acl(rsync_acl *racl, SMB_ACL_TYPE_T type, item_list *racl_list)
1025 {
1026 int ndx;
1027
1028 if (!racl)
1029 ndx = -1;
1030 else if ((ndx = find_matching_rsync_acl(racl, type, racl_list)) == -1) {
1031 acl_duo *new_duo;
1032 ndx = racl_list->count;
1033 new_duo = EXPAND_ITEM_LIST(racl_list, acl_duo, 1000);
1034 new_duo->racl = *racl;
1035 new_duo->sacl = NULL;
1036 *racl = empty_rsync_acl;
1037 }
1038
1039 return ndx;
1040 }
1041
1042 /* Turn the ACL data in stat_x into cached ACL data, setting the index
1043 * values in the file struct. */
1044 void cache_tmp_acl(struct file_struct *file, stat_x *sxp)
1045 {
1046 if (prior_access_count == (size_t)-1)
1047 prior_access_count = access_acl_list.count;
1048
1049 F_ACL(file) = cache_rsync_acl(sxp->acc_acl, SMB_ACL_TYPE_ACCESS, &access_acl_list);
1050
1051 if (S_ISDIR(sxp->st.st_mode)) {
1052 if (prior_default_count == (size_t)-1)
1053 prior_default_count = default_acl_list.count;
1054 F_DIR_DEFACL(file) = cache_rsync_acl(sxp->def_acl, SMB_ACL_TYPE_DEFAULT, &default_acl_list);
1055 }
1056 }
1057
1058 static void uncache_duo_acls(item_list *duo_list, size_t start)
1059 {
1060 acl_duo *duo_item = duo_list->items;
1061 acl_duo *duo_start = duo_item + start;
1062
1063 duo_item += duo_list->count;
1064 duo_list->count = start;
1065
1066 while (duo_item-- > duo_start) {
1067 rsync_acl_free(&duo_item->racl);
1068 if (duo_item->sacl)
1069 sys_acl_free_acl(duo_item->sacl);
1070 }
1071 }
1072
1073 void uncache_tmp_acls(void)
1074 {
1075 if (prior_access_count != (size_t)-1) {
1076 uncache_duo_acls(&access_acl_list, prior_access_count);
1077 prior_access_count = (size_t)-1;
1078 }
1079
1080 if (prior_default_count != (size_t)-1) {
1081 uncache_duo_acls(&default_acl_list, prior_default_count);
1082 prior_default_count = (size_t)-1;
1083 }
1084 }
1085
1086 #ifndef HAVE_OSX_ACLS
1087 static mode_t change_sacl_perms(SMB_ACL_T sacl, rsync_acl *racl, mode_t old_mode, mode_t mode)
1088 {
1089 SMB_ACL_ENTRY_T entry;
1090 const char *errfun;
1091 int rc;
1092
1093 if (S_ISDIR(mode)) {
1094 /* If the sticky bit is going on, it's not safe to allow all
1095 * the new ACL to go into effect before it gets set. */
1096 #ifdef SMB_ACL_LOSES_SPECIAL_MODE_BITS
1097 if (mode & S_ISVTX)
1098 mode &= ~0077;
1099 #else
1100 if (mode & S_ISVTX && !(old_mode & S_ISVTX))
1101 mode &= ~0077;
1102 } else {
1103 /* If setuid or setgid is going off, it's not safe to allow all
1104 * the new ACL to go into effect before they get cleared. */
1105 if ((old_mode & S_ISUID && !(mode & S_ISUID))
1106 || (old_mode & S_ISGID && !(mode & S_ISGID)))
1107 mode &= ~0077;
1108 #endif
1109 }
1110
1111 errfun = "sys_acl_get_entry";
1112 for (rc = sys_acl_get_entry(sacl, SMB_ACL_FIRST_ENTRY, &entry);
1113 rc == 1;
1114 rc = sys_acl_get_entry(sacl, SMB_ACL_NEXT_ENTRY, &entry)) {
1115 SMB_ACL_TAG_T tag_type;
1116 if ((rc = sys_acl_get_tag_type(entry, &tag_type)) != 0) {
1117 errfun = "sys_acl_get_tag_type";
1118 break;
1119 }
1120 switch (tag_type) {
1121 case SMB_ACL_USER_OBJ:
1122 COE2( store_access_in_entry,((mode >> 6) & 7, entry) );
1123 break;
1124 case SMB_ACL_GROUP_OBJ:
1125 /* group is only empty when identical to group perms. */
1126 if (racl->group_obj != NO_ENTRY)
1127 break;
1128 COE2( store_access_in_entry,((mode >> 3) & 7, entry) );
1129 break;
1130 case SMB_ACL_MASK:
1131 #ifndef HAVE_SOLARIS_ACLS
1132 #ifndef ACLS_NEED_MASK
1133 /* mask is only empty when we don't need it. */
1134 if (racl->mask_obj == NO_ENTRY)
1135 break;
1136 #endif
1137 COE2( store_access_in_entry,((mode >> 3) & 7, entry) );
1138 #endif
1139 break;
1140 case SMB_ACL_OTHER:
1141 COE2( store_access_in_entry,(mode & 7, entry) );
1142 break;
1143 }
1144 }
1145 if (rc) {
1146 error_exit:
1147 if (errfun) {
1148 rsyserr(FERROR_XFER, errno, "change_sacl_perms: %s()",
1149 errfun);
1150 }
1151 return (mode_t)-1;
1152 }
1153
1154 #ifdef SMB_ACL_LOSES_SPECIAL_MODE_BITS
1155 /* Ensure that chmod() will be called to restore any lost setid bits. */
1156 if (old_mode & (S_ISUID | S_ISGID | S_ISVTX)
1157 && BITS_EQUAL(old_mode, mode, CHMOD_BITS))
1158 old_mode &= ~(S_ISUID | S_ISGID | S_ISVTX);
1159 #endif
1160
1161 /* Return the mode of the file on disk, as we will set them. */
1162 return (old_mode & ~ACCESSPERMS) | (mode & ACCESSPERMS);
1163 }
1164 #endif
1165
1166 static int set_rsync_acl(int fd, int dirfd, const char *leaf, const char *fname,
1167 acl_duo *duo_item, SMB_ACL_TYPE_T type, stat_x *sxp, mode_t mode)
1168 {
1169 #ifndef SUPPORT_ACL_FD
1170 #ifndef HAVE_SOLARIS_ACLS
1171 (void)fd; /* Solaris drives the ACL via facl(2) on fd but has no SUPPORT_ACL_FD. */
1172 #endif
1173 (void)dirfd;
1174 (void)leaf;
1175 #endif
1176 if (type == SMB_ACL_TYPE_DEFAULT
1177 && duo_item->racl.user_obj == NO_ENTRY) {
1178 int rc;
1179 #ifdef SUPPORT_XATTRS
1180 /* --fake-super support: delete default ACL from xattrs. */
1181 if (am_root < 0)
1182 rc = del_def_xattr_acl(fd, fname);
1183 else
1184 #endif
1185 #ifdef SUPPORT_ACL_FD
1186 #ifdef HAVE_LIBACL_AT
1187 /* Race-safe default-ACL delete via the new libacl *_at
1188 * calls (held fd via AT_EMPTY_PATH, dirfd+leaf via AT_SYMLINK_NOFOLLOW)
1189 * -- race-safe on every Linux kernel. fd<0 && dirfd<0 falls to path. */
1190 if (fd >= 0)
1191 rc = sys_acl_delete_def_file_at(fd, "", AT_EMPTY_PATH);
1192 else if (dirfd >= 0)
1193 rc = sys_acl_delete_def_file_at(dirfd, leaf, AT_SYMLINK_NOFOLLOW);
1194 else
1195 #else
1196 /* Race-safe default-ACL delete via the held fd or dirfd+leaf. Where
1197 * neither is available (xacl_at_available() is false -- the BSDs, a
1198 * /proc-less namespace, an un-pinnable entry; every Linux with procfs
1199 * takes the dirfd path via *xattrat or the /proc/self/fd compat) -- fall
1200 * back to the path-based call, preferring the documented --acls behaviour
1201 * over refusing it where the race-safe primitive is unavailable. */
1202 if (fd >= 0)
1203 rc = xacl_del_default_fd(fd);
1204 else if (dirfd >= 0 && xacl_at_available())
1205 rc = xacl_del_default_at(dirfd, leaf);
1206 else
1207 #endif /* HAVE_LIBACL_AT */
1208 #endif
1209 #ifdef HAVE_SOLARIS_ACLS
1210 /* Solaris: delete the default ACL through the held fd via facl(2). For a
1211 * root receiver a missing held fd means the leaf was raced, so refuse rather
1212 * than let the path-based delete follow it; a plain non-root receiver keeps
1213 * the legacy path fallback (op_pin am_root != 0 rule). */
1214 if (fd >= 0)
1215 rc = sys_acl_delete_def_fd(fd);
1216 else if (secure_relpath_active() && am_root) {
1217 errno = ELOOP;
1218 rc = -1;
1219 } else
1220 #endif
1221 rc = sys_acl_delete_def_file(fname);
1222 if (rc < 0) {
1223 rsyserr(FERROR_XFER, errno, "set_acl: sys_acl_delete_def_file(%s)",
1224 fname);
1225 return -1;
1226 }
1227 #ifdef SUPPORT_XATTRS
1228 } else if (am_root < 0) {
1229 /* --fake-super support: store ACLs in an xattr. */
1230 int cnt = duo_item->racl.names.count;
1231 size_t len = 4*4 + cnt * (4+4);
1232 char *buf = new_array(char, len);
1233 int rc;
1234
1235 SIVAL(buf, 0, duo_item->racl.user_obj);
1236 SIVAL(buf, 4, duo_item->racl.group_obj);
1237 SIVAL(buf, 8, duo_item->racl.mask_obj);
1238 SIVAL(buf, 12, duo_item->racl.other_obj);
1239
1240 if (cnt) {
1241 char *bp = buf + 4*4;
1242 id_access *ida = duo_item->racl.names.idas;
1243 for ( ; cnt--; ida++, bp += 4+4) {
1244 SIVAL(bp, 0, ida->id);
1245 SIVAL(bp, 4, ida->access);
1246 }
1247 }
1248 rc = set_xattr_acl(fd, fname, type == SMB_ACL_TYPE_ACCESS, buf, len);
1249 free(buf);
1250 return rc;
1251 #endif
1252 } else {
1253 mode_t cur_mode = sxp->st.st_mode;
1254 if (!duo_item->sacl
1255 && !pack_smb_acl(&duo_item->sacl, &duo_item->racl))
1256 return -1;
1257 #ifdef HAVE_OSX_ACLS
1258 (void)mode; /* eliminate compiler warning */
1259 #else
1260 if (type == SMB_ACL_TYPE_ACCESS) {
1261 cur_mode = change_sacl_perms(duo_item->sacl, &duo_item->racl, cur_mode, mode);
1262 if (cur_mode == (mode_t)-1)
1263 return 0;
1264 }
1265 #endif
1266 #ifdef SUPPORT_ACL_FD
1267 #ifdef HAVE_LIBACL_AT
1268 /* Apply the packed/perm-reconciled ACL (duo_item->sacl)
1269 * through the new libacl *_at calls -- held fd via AT_EMPTY_PATH,
1270 * dirfd+leaf via AT_SYMLINK_NOFOLLOW -- race-safe on every Linux kernel,
1271 * and byte-identical to the path-based sys_acl_set_file() below. */
1272 if (fd >= 0 || dirfd >= 0) {
1273 int rc;
1274
1275 if (fd >= 0)
1276 rc = sys_acl_set_file_at(fd, "", AT_EMPTY_PATH, type, duo_item->sacl);
1277 else
1278 rc = sys_acl_set_file_at(dirfd, leaf, AT_SYMLINK_NOFOLLOW, type, duo_item->sacl);
1279 if (rc < 0) {
1280 rsyserr(FERROR_XFER, errno, "set_acl: acl_set_file_at(%s, %s)",
1281 fname, str_acl_type(type));
1282 return -1;
1283 }
1284 if (type == SMB_ACL_TYPE_ACCESS)
1285 sxp->st.st_mode = cur_mode;
1286 return 0;
1287 }
1288 #else
1289 /* Race-safe write: serialize the packed (and perm-reconciled)
1290 * system ACL to the kernel xattr format and apply it through the
1291 * held fd or dirfd+leaf -- never re-resolving fname. This matches
1292 * exactly what sys_acl_set_file() would have written. */
1293 if (fd >= 0 || (dirfd >= 0 && xacl_at_available())) {
1294 int is_def = type == SMB_ACL_TYPE_DEFAULT;
1295 rsync_acl_ent *ents;
1296 int n = sacl_to_entries(duo_item->sacl, &ents);
1297 int rc;
1298
1299 if (n < 0)
1300 return -1;
1301 if (fd >= 0)
1302 rc = xacl_set_fd(fd, is_def, ents, n);
1303 else
1304 rc = xacl_set_at(dirfd, leaf, is_def, ents, n);
1305 free(ents);
1306 if (rc < 0) {
1307 rsyserr(FERROR_XFER, errno, "set_acl: xacl_set(%s, %s)",
1308 fname, str_acl_type(type));
1309 return -1;
1310 }
1311 if (type == SMB_ACL_TYPE_ACCESS)
1312 sxp->st.st_mode = cur_mode;
1313 return 0;
1314 }
1315 /* No held fd and no usable dirfd path (xacl_at_available() is false --
1316 * the BSDs, a /proc-less namespace, an un-pinnable entry; every Linux
1317 * with procfs took xacl_set_at() above via *xattrat or the /proc/self/fd
1318 * compat): prefer the documented --acls behaviour over refusing it and
1319 * fall back to the path-based set. This re-resolves fname, so it still
1320 * carries the parent-symlink-race exposure on those remaining platforms;
1321 * it is the only way to honour --acls where no race-safe primitive
1322 * exists. */
1323 #endif /* HAVE_LIBACL_AT */
1324 #endif
1325 #ifdef HAVE_SOLARIS_ACLS
1326 /* Solaris: apply the ACL through the held fd via facl(2). */
1327 if (fd >= 0) {
1328 if (sys_acl_set_fd_type(fd, type, duo_item->sacl) < 0) {
1329 rsyserr(FERROR_XFER, errno, "set_acl: sys_acl_set_fd_type(%s, %s)",
1330 fname, str_acl_type(type));
1331 return -1;
1332 }
1333 if (type == SMB_ACL_TYPE_ACCESS)
1334 sxp->st.st_mode = cur_mode;
1335 return 0;
1336 }
1337 if (secure_relpath_active() && am_root) {
1338 /* Real root always can open its own freshly-staged reg/dir/fifo leaf,
1339 * so a missing held fd on a confined receiver means the leaf was raced
1340 * to a symlink; sys_acl_set_file() follows the leaf, so refuse rather
1341 * than write the attacker-supplied ACL onto a redirected inode (covers
1342 * the top-level no-slash entry the caller's slashed-path xattr_refuse
1343 * gate misses). A plain non-root receiver keeps the path-based fallback
1344 * for a legitimately un-pinnable owned leaf (e.g. a 0300 dir), matching
1345 * the operator-path op_pin rule (am_root != 0). */
1346 errno = ELOOP;
1347 rsyserr(FERROR_XFER, errno, "set_acl: refusing path-based ACL on %s (no held fd)",
1348 fname);
1349 return -1;
1350 }
1351 #endif
1352 if (sys_acl_set_file(fname, type, duo_item->sacl) < 0) {
1353 rsyserr(FERROR_XFER, errno, "set_acl: sys_acl_set_file(%s, %s)",
1354 fname, str_acl_type(type));
1355 return -1;
1356 }
1357 if (type == SMB_ACL_TYPE_ACCESS)
1358 sxp->st.st_mode = cur_mode;
1359 }
1360
1361 return 0;
1362 }
1363
1364 /* Given a fname, this sets extended access ACL entries, the default ACL (for a
1365 * dir), and the regular mode bits on the file. Call this with fname set to
1366 * NULL to just check if the ACL is different.
1367 *
1368 * When a held O_NOFOLLOW fd (or a dirfd+leaf) is supplied, the ACL is applied
1369 * race-safely through it; otherwise (fd < 0 && dirfd < 0) the path-based
1370 * fallback is used.
1371 *
1372 * If the ACL operation has a side-effect of changing the file's mode, the
1373 * sxp->st.st_mode value will be changed to match.
1374 *
1375 * Returns 0 for an unchanged ACL, 1 for changed, -1 for failed. */
1376 int set_acl_fdat(int fd, int dirfd, const char *leaf, const char *fname,
1377 const struct file_struct *file, stat_x *sxp, mode_t new_mode)
1378 {
1379 int changed = 0;
1380 int32 ndx;
1381 BOOL eq;
1382
1383 if (!dry_run && (read_only || list_only)) {
1384 errno = EROFS;
1385 return -1;
1386 }
1387
1388 ndx = F_ACL(file);
1389 if (ndx >= 0 && (size_t)ndx < access_acl_list.count) {
1390 acl_duo *duo_item = access_acl_list.items;
1391 duo_item += ndx;
1392 eq = sxp->acc_acl
1393 && rsync_acl_equal_enough(sxp->acc_acl, &duo_item->racl, new_mode);
1394 if (!eq) {
1395 changed = 1;
1396 if (!dry_run && fname
1397 && set_rsync_acl(fd, dirfd, leaf, fname, duo_item, SMB_ACL_TYPE_ACCESS,
1398 sxp, new_mode) < 0)
1399 return -1;
1400 }
1401 }
1402
1403 if (!S_ISDIR(new_mode))
1404 return changed;
1405
1406 ndx = F_DIR_DEFACL(file);
1407 if (ndx >= 0 && (size_t)ndx < default_acl_list.count) {
1408 acl_duo *duo_item = default_acl_list.items;
1409 duo_item += ndx;
1410 eq = sxp->def_acl && rsync_acl_equal(sxp->def_acl, &duo_item->racl);
1411 if (!eq) {
1412 changed = 1;
1413 if (!dry_run && fname
1414 && set_rsync_acl(fd, dirfd, leaf, fname, duo_item, SMB_ACL_TYPE_DEFAULT,
1415 sxp, new_mode) < 0)
1416 return -1;
1417 }
1418 }
1419
1420 return changed;
1421 }
1422
1423 int set_acl(const char *fname, const struct file_struct *file, stat_x *sxp, mode_t new_mode)
1424 {
1425 return set_acl_fdat(-1, -1, NULL, fname, file, sxp, new_mode);
1426 }
1427
1428 /* Non-incremental recursion needs to convert all the received IDs.
1429 * This is done in a single pass after receiving the whole file-list. */
1430 static void match_racl_ids(const item_list *racl_list)
1431 {
1432 int list_cnt, name_cnt;
1433 acl_duo *duo_item = racl_list->items;
1434 for (list_cnt = racl_list->count; list_cnt--; duo_item++) {
1435 ida_entries *idal = &duo_item->racl.names;
1436 id_access *ida = idal->idas;
1437 for (name_cnt = idal->count; name_cnt--; ida++) {
1438 if (ida->access & NAME_IS_USER)
1439 ida->id = match_uid(ida->id);
1440 else
1441 ida->id = match_gid(ida->id, NULL);
1442 }
1443 }
1444 }
1445
1446 void match_acl_ids(void)
1447 {
1448 match_racl_ids(&access_acl_list);
1449 match_racl_ids(&default_acl_list);
1450 }
1451
1452 /* This is used by dest_mode(). */
1453 int default_perms_for_dir(const char *dir)
1454 {
1455 rsync_acl racl;
1456 SMB_ACL_T sacl;
1457 BOOL ok;
1458 int perms;
1459
1460 if (dir == NULL)
1461 dir = ".";
1462 perms = ACCESSPERMS & ~orig_umask;
1463 /* Read the directory's default ACL. If it has none, this will successfully return an empty ACL. */
1464 sacl = sys_acl_get_file(dir, SMB_ACL_TYPE_DEFAULT);
1465 if (sacl == NULL) {
1466 /* Couldn't get an ACL. Darn. */
1467 switch (errno) {
1468 case EINVAL:
1469 /* If SMB_ACL_TYPE_DEFAULT isn't valid, then the ACLs must be non-POSIX. */
1470 break;
1471 #ifdef ENOTSUP
1472 case ENOTSUP:
1473 #endif
1474 case ENOSYS:
1475 /* No ACLs are available. */
1476 break;
1477 default:
1478 if (dry_run && errno == ENOENT) {
1479 /* We're doing a dry run, so the containing directory
1480 * wasn't actually created. Don't worry about it. */
1481 break;
1482 }
1483 rprintf(FWARNING,
1484 "default_perms_for_dir: sys_acl_get_file(%s, %s): %s, falling back on umask\n",
1485 dir, str_acl_type(SMB_ACL_TYPE_DEFAULT), strerror(errno));
1486 }
1487 return perms;
1488 }
1489
1490 /* Convert it. */
1491 racl = empty_rsync_acl;
1492 ok = unpack_smb_acl(sacl, &racl);
1493 sys_acl_free_acl(sacl);
1494 if (!ok) {
1495 rprintf(FWARNING, "default_perms_for_dir: unpack_smb_acl failed, falling back on umask\n");
1496 return perms;
1497 }
1498
1499 /* Apply the permission-bit entries of the default ACL, if any. */
1500 if (racl.user_obj != NO_ENTRY) {
1501 perms = rsync_acl_get_perms(&racl);
1502 if (DEBUG_GTE(ACL, 1))
1503 rprintf(FINFO, "got ACL-based default perms %o for directory %s\n", perms, dir);
1504 }
1505
1506 rsync_acl_free(&racl);
1507 return perms;
1508 }
1509
1510 #endif /* SUPPORT_ACLS */