Make option variables a little clearer by using separate FOO_ndx
[rsync/rsync.git] / acls.c
CommitLineData
1c3344a1
WD
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 Wayne Davison
7 *
8 * This program is free software; you can redistribute it and/or modify
1c53e517
WD
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
1c3344a1
WD
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21
22#include "rsync.h"
23#include "lib/sysacls.h"
24
25#ifdef SUPPORT_ACLS
26
27extern int dry_run;
28extern int am_root;
29extern int read_only;
30extern int list_only;
31extern int orig_umask;
32extern int protocol_version;
33extern int numeric_ids;
34extern int inc_recurse;
35
33233b49 36/* Flags used to indicate what items are being transmitted for an entry. */
1c3344a1 37#define XMIT_USER_OBJ (1<<0)
030820d5
WD
38#define XMIT_GROUP_OBJ (1<<1)
39#define XMIT_MASK_OBJ (1<<2)
40#define XMIT_OTHER_OBJ (1<<3)
41#define XMIT_NAME_LIST (1<<4)
1c3344a1 42
030820d5
WD
43#define NO_ENTRY ((uchar)0x80) /* Default value of a NON-name-list entry. */
44
45#define NAME_IS_USER (1u<<31) /* Bit used only on a name-list entry. */
46
47/* When we send the access bits over the wire, we shift them 2 bits to the
48 * left and use the lower 2 bits as flags (relevant only to a name entry).
49 * This makes the protocol more efficient than sending a value that would
50 * be likely to have its hightest bits set. */
51#define XFLAG_NAME_FOLLOWS 0x0001u
52#define XFLAG_NAME_IS_USER 0x0002u
33233b49 53
1c3344a1
WD
54/* === ACL structures === */
55
56typedef struct {
57 id_t id;
030820d5 58 uint32 access;
1c3344a1
WD
59} id_access;
60
61typedef struct {
62 id_access *idas;
63 int count;
64} ida_entries;
65
66typedef struct {
67 char *name;
68 uchar len;
69} idname;
70
1c3344a1 71typedef struct rsync_acl {
030820d5 72 ida_entries names;
1c3344a1
WD
73 /* These will be NO_ENTRY if there's no such entry. */
74 uchar user_obj;
75 uchar group_obj;
76 uchar mask_obj;
77 uchar other_obj;
78} rsync_acl;
79
80typedef struct {
81 rsync_acl racl;
82 SMB_ACL_T sacl;
83} acl_duo;
84
85static const rsync_acl empty_rsync_acl = {
030820d5 86 {NULL, 0}, NO_ENTRY, NO_ENTRY, NO_ENTRY, NO_ENTRY
1c3344a1
WD
87};
88
89static item_list access_acl_list = EMPTY_ITEM_LIST;
90static item_list default_acl_list = EMPTY_ITEM_LIST;
91
92/* === Calculations on ACL types === */
93
94static const char *str_acl_type(SMB_ACL_TYPE_T type)
95{
96 return type == SMB_ACL_TYPE_ACCESS ? "SMB_ACL_TYPE_ACCESS"
97 : type == SMB_ACL_TYPE_DEFAULT ? "SMB_ACL_TYPE_DEFAULT"
98 : "unknown SMB_ACL_TYPE_T";
99}
100
101static int calc_sacl_entries(const rsync_acl *racl)
102{
103 /* A System ACL always gets user/group/other permission entries. */
030820d5 104 return racl->names.count
1c3344a1
WD
105#ifdef ACLS_NEED_MASK
106 + 4;
107#else
108 + (racl->mask_obj != NO_ENTRY) + 3;
109#endif
110}
111
112/* Extracts and returns the permission bits from the ACL. This cannot be
113 * called on an rsync_acl that has NO_ENTRY in any spot but the mask. */
114static int rsync_acl_get_perms(const rsync_acl *racl)
115{
116 return (racl->user_obj << 6)
117 + ((racl->mask_obj != NO_ENTRY ? racl->mask_obj : racl->group_obj) << 3)
118 + racl->other_obj;
119}
120
121/* Removes the permission-bit entries from the ACL because these
122 * can be reconstructed from the file's mode. */
123static void rsync_acl_strip_perms(rsync_acl *racl)
124{
125 racl->user_obj = NO_ENTRY;
126 if (racl->mask_obj == NO_ENTRY)
127 racl->group_obj = NO_ENTRY;
128 else {
129 if (racl->group_obj == racl->mask_obj)
130 racl->group_obj = NO_ENTRY;
131 racl->mask_obj = NO_ENTRY;
132 }
133 racl->other_obj = NO_ENTRY;
134}
135
136/* Given an empty rsync_acl, fake up the permission bits. */
137static void rsync_acl_fake_perms(rsync_acl *racl, mode_t mode)
138{
139 racl->user_obj = (mode >> 6) & 7;
140 racl->group_obj = (mode >> 3) & 7;
141 racl->other_obj = mode & 7;
142}
143
144/* === Rsync ACL functions === */
145
146static rsync_acl *create_racl(void)
147{
148 rsync_acl *racl = new(rsync_acl);
149
150 if (!racl)
151 out_of_memory("create_racl");
152 *racl = empty_rsync_acl;
153
154 return racl;
155}
156
157static BOOL ida_entries_equal(const ida_entries *ial1, const ida_entries *ial2)
158{
159 id_access *ida1, *ida2;
160 int count = ial1->count;
161 if (count != ial2->count)
162 return False;
163 ida1 = ial1->idas;
164 ida2 = ial2->idas;
165 for (; count--; ida1++, ida2++) {
166 if (ida1->access != ida2->access || ida1->id != ida2->id)
167 return False;
168 }
169 return True;
170}
171
172static BOOL rsync_acl_equal(const rsync_acl *racl1, const rsync_acl *racl2)
173{
174 return racl1->user_obj == racl2->user_obj
175 && racl1->group_obj == racl2->group_obj
176 && racl1->mask_obj == racl2->mask_obj
177 && racl1->other_obj == racl2->other_obj
030820d5 178 && ida_entries_equal(&racl1->names, &racl2->names);
1c3344a1
WD
179}
180
181/* Are the extended (non-permission-bit) entries equal? If so, the rest of
182 * the ACL will be handled by the normal mode-preservation code. This is
183 * only meaningful for access ACLs! Note: the 1st arg is a fully-populated
184 * rsync_acl, but the 2nd parameter can be a condensed rsync_acl, which means
185 * that it might have several of its permission objects set to NO_ENTRY. */
186static BOOL rsync_acl_equal_enough(const rsync_acl *racl1,
187 const rsync_acl *racl2, mode_t m)
188{
189 if ((racl1->mask_obj ^ racl2->mask_obj) & NO_ENTRY)
190 return False; /* One has a mask and the other doesn't */
191
192 /* When there's a mask, the group_obj becomes an extended entry. */
193 if (racl1->mask_obj != NO_ENTRY) {
194 /* A condensed rsync_acl with a mask can only have no
195 * group_obj when it was identical to the mask. This
196 * means that it was also identical to the group attrs
197 * from the mode. */
198 if (racl2->group_obj == NO_ENTRY) {
199 if (racl1->group_obj != ((m >> 3) & 7))
200 return False;
201 } else if (racl1->group_obj != racl2->group_obj)
202 return False;
203 }
030820d5 204 return ida_entries_equal(&racl1->names, &racl2->names);
1c3344a1
WD
205}
206
207static void rsync_acl_free(rsync_acl *racl)
208{
030820d5
WD
209 if (racl->names.idas)
210 free(racl->names.idas);
1c3344a1
WD
211 *racl = empty_rsync_acl;
212}
213
214void free_acl(statx *sxp)
215{
216 if (sxp->acc_acl) {
217 rsync_acl_free(sxp->acc_acl);
218 free(sxp->acc_acl);
219 sxp->acc_acl = NULL;
220 }
221 if (sxp->def_acl) {
222 rsync_acl_free(sxp->def_acl);
223 free(sxp->def_acl);
224 sxp->def_acl = NULL;
225 }
226}
227
030820d5 228#ifdef SMB_ACL_NEED_SORT
1c3344a1
WD
229static int id_access_sorter(const void *r1, const void *r2)
230{
231 id_access *ida1 = (id_access *)r1;
232 id_access *ida2 = (id_access *)r2;
233 id_t rid1 = ida1->id, rid2 = ida2->id;
030820d5
WD
234 if ((ida1->access ^ ida2->access) & NAME_IS_USER)
235 return ida1->access & NAME_IS_USER ? -1 : 1;
1c3344a1
WD
236 return rid1 == rid2 ? 0 : rid1 < rid2 ? -1 : 1;
237}
030820d5 238#endif
1c3344a1
WD
239
240/* === System ACLs === */
241
1c3344a1
WD
242/* Unpack system ACL -> rsync ACL verbatim. Return whether we succeeded. */
243static BOOL unpack_smb_acl(SMB_ACL_T sacl, rsync_acl *racl)
244{
245 static item_list temp_ida_list = EMPTY_ITEM_LIST;
1c3344a1
WD
246 SMB_ACL_ENTRY_T entry;
247 const char *errfun;
248 int rc;
249
250 errfun = "sys_acl_get_entry";
251 for (rc = sys_acl_get_entry(sacl, SMB_ACL_FIRST_ENTRY, &entry);
252 rc == 1;
253 rc = sys_acl_get_entry(sacl, SMB_ACL_NEXT_ENTRY, &entry)) {
254 SMB_ACL_TAG_T tag_type;
030820d5 255 uint32 access;
1c3344a1
WD
256 void *qualifier;
257 id_access *ida;
030820d5 258 if ((rc = sys_acl_get_tag_type(entry, &tag_type)) != 0) {
1c3344a1
WD
259 errfun = "sys_acl_get_tag_type";
260 break;
261 }
030820d5
WD
262 if ((rc = sys_acl_get_access_bits(entry, &access)) != 0) {
263 errfun = "sys_acl_get_access_bits";
1c3344a1
WD
264 break;
265 }
1c3344a1
WD
266 /* continue == done with entry; break == store in temporary ida list */
267 switch (tag_type) {
268 case SMB_ACL_USER_OBJ:
269 if (racl->user_obj == NO_ENTRY)
270 racl->user_obj = access;
271 else
272 rprintf(FINFO, "unpack_smb_acl: warning: duplicate USER_OBJ entry ignored\n");
273 continue;
274 case SMB_ACL_USER:
030820d5 275 access |= NAME_IS_USER;
1c3344a1
WD
276 break;
277 case SMB_ACL_GROUP_OBJ:
278 if (racl->group_obj == NO_ENTRY)
279 racl->group_obj = access;
280 else
281 rprintf(FINFO, "unpack_smb_acl: warning: duplicate GROUP_OBJ entry ignored\n");
282 continue;
283 case SMB_ACL_GROUP:
284 break;
285 case SMB_ACL_MASK:
286 if (racl->mask_obj == NO_ENTRY)
287 racl->mask_obj = access;
288 else
289 rprintf(FINFO, "unpack_smb_acl: warning: duplicate MASK entry ignored\n");
290 continue;
291 case SMB_ACL_OTHER:
292 if (racl->other_obj == NO_ENTRY)
293 racl->other_obj = access;
294 else
295 rprintf(FINFO, "unpack_smb_acl: warning: duplicate OTHER entry ignored\n");
296 continue;
297 default:
298 rprintf(FINFO, "unpack_smb_acl: warning: entry with unrecognized tag type ignored\n");
299 continue;
300 }
301 if (!(qualifier = sys_acl_get_qualifier(entry))) {
302 errfun = "sys_acl_get_tag_type";
303 rc = EINVAL;
304 break;
305 }
1c3344a1
WD
306 ida = EXPAND_ITEM_LIST(&temp_ida_list, id_access, -10);
307 ida->id = *((id_t *)qualifier);
308 ida->access = access;
309 sys_acl_free_qualifier(qualifier, tag_type);
310 }
311 if (rc) {
312 rsyserr(FERROR, errno, "unpack_smb_acl: %s()", errfun);
313 rsync_acl_free(racl);
314 return False;
315 }
1c3344a1 316
030820d5
WD
317 /* Transfer the count id_access items out of the temp_ida_list
318 * into the names ida_entries list in racl. */
319 if (temp_ida_list.count) {
320#ifdef SMB_ACL_NEED_SORT
321 if (temp_ida_list.count > 1) {
322 qsort(temp_ida_list.items, temp_ida_list.count,
323 sizeof (id_access), id_access_sorter);
324 }
325#endif
326 if (!(racl->names.idas = new_array(id_access, temp_ida_list.count)))
327 out_of_memory("unpack_smb_acl");
328 memcpy(racl->names.idas, temp_ida_list.items,
329 temp_ida_list.count * sizeof (id_access));
330 } else
331 racl->names.idas = NULL;
332
333 racl->names.count = temp_ida_list.count;
334
335 /* Truncate the temporary list now that its idas have been saved. */
336 temp_ida_list.count = 0;
1c3344a1
WD
337
338#ifdef ACLS_NEED_MASK
030820d5 339 if (!racl->names.count && racl->mask_obj != NO_ENTRY) {
1c3344a1
WD
340 /* Throw away a superfluous mask, but mask off the
341 * group perms with it first. */
342 racl->group_obj &= racl->mask_obj;
343 racl->mask_obj = NO_ENTRY;
344 }
345#endif
346
347 return True;
348}
349
350/* Synactic sugar for system calls */
351
352#define CALL_OR_ERROR(func,args,str) \
353 do { \
354 if (func args) { \
355 errfun = str; \
356 goto error_exit; \
357 } \
358 } while (0)
359
360#define COE(func,args) CALL_OR_ERROR(func,args,#func)
361#define COE2(func,args) CALL_OR_ERROR(func,args,NULL)
362
363/* Store the permissions in the system ACL entry. */
030820d5 364static int store_access_in_entry(uint32 access, SMB_ACL_ENTRY_T entry)
1c3344a1 365{
030820d5
WD
366 if (sys_acl_set_access_bits(entry, access)) {
367 rsyserr(FERROR, errno, "store_access_in_entry sys_acl_set_access_bits()");
368 return -1;
369 }
1c3344a1 370 return 0;
1c3344a1
WD
371}
372
373/* Pack rsync ACL -> system ACL verbatim. Return whether we succeeded. */
374static BOOL pack_smb_acl(SMB_ACL_T *smb_acl, const rsync_acl *racl)
375{
376#ifdef ACLS_NEED_MASK
377 uchar mask_bits;
378#endif
379 size_t count;
380 id_access *ida;
381 const char *errfun = NULL;
382 SMB_ACL_ENTRY_T entry;
383
384 if (!(*smb_acl = sys_acl_init(calc_sacl_entries(racl)))) {
385 rsyserr(FERROR, errno, "pack_smb_acl: sys_acl_init()");
386 return False;
387 }
388
389 COE( sys_acl_create_entry,(smb_acl, &entry) );
390 COE( sys_acl_set_tag_type,(entry, SMB_ACL_USER_OBJ) );
030820d5 391 COE2( store_access_in_entry,(racl->user_obj & ~NO_ENTRY, entry) );
1c3344a1 392
030820d5
WD
393 for (ida = racl->names.idas, count = racl->names.count; count; ida++, count--) {
394#ifdef SMB_ACL_NEED_SORT
395 if (!(ida->access & NAME_IS_USER))
396 break;
397#endif
1c3344a1 398 COE( sys_acl_create_entry,(smb_acl, &entry) );
030820d5 399#ifdef SMB_ACL_NEED_SORT
1c3344a1 400 COE( sys_acl_set_tag_type,(entry, SMB_ACL_USER) );
030820d5
WD
401#else
402 COE( sys_acl_set_tag_type,(entry, ida->access & NAME_IS_USER
403 ? SMB_ACL_USER : SMB_ACL_GROUP) );
404#endif
1c3344a1 405 COE( sys_acl_set_qualifier,(entry, (void*)&ida->id) );
030820d5 406 COE2( store_access_in_entry,(ida->access & ~NAME_IS_USER, entry) );
1c3344a1
WD
407 }
408
409 COE( sys_acl_create_entry,(smb_acl, &entry) );
410 COE( sys_acl_set_tag_type,(entry, SMB_ACL_GROUP_OBJ) );
030820d5 411 COE2( store_access_in_entry,(racl->group_obj & ~NO_ENTRY, entry) );
1c3344a1 412
030820d5
WD
413#ifdef SMB_ACL_NEED_SORT
414 for ( ; count; ida++, count--) {
1c3344a1
WD
415 COE( sys_acl_create_entry,(smb_acl, &entry) );
416 COE( sys_acl_set_tag_type,(entry, SMB_ACL_GROUP) );
417 COE( sys_acl_set_qualifier,(entry, (void*)&ida->id) );
418 COE2( store_access_in_entry,(ida->access, entry) );
419 }
030820d5 420#endif
1c3344a1
WD
421
422#ifdef ACLS_NEED_MASK
030820d5 423 mask_bits = racl->mask_obj == NO_ENTRY ? racl->group_obj & ~NO_ENTRY : racl->mask_obj;
1c3344a1
WD
424 COE( sys_acl_create_entry,(smb_acl, &entry) );
425 COE( sys_acl_set_tag_type,(entry, SMB_ACL_MASK) );
426 COE2( store_access_in_entry,(mask_bits, entry) );
427#else
428 if (racl->mask_obj != NO_ENTRY) {
429 COE( sys_acl_create_entry,(smb_acl, &entry) );
430 COE( sys_acl_set_tag_type,(entry, SMB_ACL_MASK) );
431 COE2( store_access_in_entry,(racl->mask_obj, entry) );
432 }
433#endif
434
435 COE( sys_acl_create_entry,(smb_acl, &entry) );
436 COE( sys_acl_set_tag_type,(entry, SMB_ACL_OTHER) );
030820d5 437 COE2( store_access_in_entry,(racl->other_obj & ~NO_ENTRY, entry) );
1c3344a1
WD
438
439#ifdef DEBUG
440 if (sys_acl_valid(*smb_acl) < 0)
441 rprintf(FERROR, "pack_smb_acl: warning: system says the ACL I packed is invalid\n");
442#endif
443
444 return True;
445
446 error_exit:
447 if (errfun) {
448 rsyserr(FERROR, errno, "pack_smb_acl %s()", errfun);
449 }
450 sys_acl_free_acl(*smb_acl);
451 return False;
452}
453
454static int find_matching_rsync_acl(const rsync_acl *racl, SMB_ACL_TYPE_T type,
455 const item_list *racl_list)
456{
457 static int access_match = -1, default_match = -1;
458 int *match = type == SMB_ACL_TYPE_ACCESS ? &access_match : &default_match;
459 size_t count = racl_list->count;
460
461 /* If this is the first time through or we didn't match the last
462 * time, then start at the end of the list, which should be the
463 * best place to start hunting. */
464 if (*match == -1)
465 *match = racl_list->count - 1;
466 while (count--) {
467 rsync_acl *base = racl_list->items;
468 if (rsync_acl_equal(base + *match, racl))
469 return *match;
470 if (!(*match)--)
471 *match = racl_list->count - 1;
472 }
473
474 *match = -1;
475 return *match;
476}
477
478static int get_rsync_acl(const char *fname, rsync_acl *racl,
479 SMB_ACL_TYPE_T type, mode_t mode)
480{
481 SMB_ACL_T sacl = sys_acl_get_file(fname, type);
482
483 if (sacl) {
484 BOOL ok = unpack_smb_acl(sacl, racl);
485
486 sys_acl_free_acl(sacl);
487 if (!ok) {
488 return -1;
489 }
d4a013ed 490 } else if (no_acl_syscall_error(errno)) {
1c3344a1
WD
491 /* ACLs are not supported, so pretend we have a basic ACL. */
492 if (type == SMB_ACL_TYPE_ACCESS)
493 rsync_acl_fake_perms(racl, mode);
494 } else {
495 rsyserr(FERROR, errno, "get_acl: sys_acl_get_file(%s, %s)",
496 fname, str_acl_type(type));
497 return -1;
498 }
499
500 return 0;
501}
502
503/* Return the Access Control List for the given filename. */
504int get_acl(const char *fname, statx *sxp)
505{
506 sxp->acc_acl = create_racl();
507 if (get_rsync_acl(fname, sxp->acc_acl, SMB_ACL_TYPE_ACCESS,
508 sxp->st.st_mode) < 0) {
509 free_acl(sxp);
510 return -1;
511 }
512
513 if (S_ISDIR(sxp->st.st_mode)) {
514 sxp->def_acl = create_racl();
515 if (get_rsync_acl(fname, sxp->def_acl, SMB_ACL_TYPE_DEFAULT,
516 sxp->st.st_mode) < 0) {
517 free_acl(sxp);
518 return -1;
519 }
520 }
521
522 return 0;
523}
524
525/* === Send functions === */
526
527/* The general strategy with the tag_type <-> character mapping is that
528 * lowercase implies that no qualifier follows, where uppercase does.
529 * A similar idiom for the ACL type (access or default) itself, but
530 * lowercase in this instance means there's no ACL following, so the
531 * ACL is a repeat, so the receiver should reuse the last of the same
532 * type ACL. */
533
534/* Send the ida list over the file descriptor. */
030820d5 535static void send_ida_entries(const ida_entries *idal, int f)
1c3344a1
WD
536{
537 id_access *ida;
538 size_t count = idal->count;
539
f31514ad 540 write_varint(f, idal->count);
1c3344a1
WD
541
542 for (ida = idal->idas; count--; ida++) {
030820d5
WD
543 uint32 xbits = ida->access << 2;
544 char *name;
545 if (ida->access & NAME_IS_USER) {
546 xbits |= XFLAG_NAME_IS_USER;
547 name = add_uid(ida->id);
548 } else
549 name = add_gid(ida->id);
f31514ad 550 write_varint(f, ida->id);
1c3344a1
WD
551 if (inc_recurse && name) {
552 int len = strlen(name);
030820d5 553 write_varint(f, xbits | XFLAG_NAME_FOLLOWS);
1c3344a1
WD
554 write_byte(f, len);
555 write_buf(f, name, len);
556 } else
030820d5 557 write_varint(f, xbits);
1c3344a1
WD
558 }
559}
560
561static void send_rsync_acl(rsync_acl *racl, SMB_ACL_TYPE_T type,
562 item_list *racl_list, int f)
563{
564 int ndx = find_matching_rsync_acl(racl, type, racl_list);
565
566 /* Send 0 (-1 + 1) to indicate that literal ACL data follows. */
f31514ad 567 write_varint(f, ndx + 1);
1c3344a1
WD
568
569 if (ndx < 0) {
570 rsync_acl *new_racl = EXPAND_ITEM_LIST(racl_list, rsync_acl, 1000);
571 uchar flags = 0;
572
573 if (racl->user_obj != NO_ENTRY)
574 flags |= XMIT_USER_OBJ;
1c3344a1
WD
575 if (racl->group_obj != NO_ENTRY)
576 flags |= XMIT_GROUP_OBJ;
1c3344a1
WD
577 if (racl->mask_obj != NO_ENTRY)
578 flags |= XMIT_MASK_OBJ;
1c3344a1
WD
579 if (racl->other_obj != NO_ENTRY)
580 flags |= XMIT_OTHER_OBJ;
030820d5
WD
581 if (racl->names.count)
582 flags |= XMIT_NAME_LIST;
1c3344a1
WD
583
584 write_byte(f, flags);
585
586 if (flags & XMIT_USER_OBJ)
030820d5 587 write_varint(f, racl->user_obj);
1c3344a1 588 if (flags & XMIT_GROUP_OBJ)
030820d5 589 write_varint(f, racl->group_obj);
1c3344a1 590 if (flags & XMIT_MASK_OBJ)
030820d5 591 write_varint(f, racl->mask_obj);
1c3344a1 592 if (flags & XMIT_OTHER_OBJ)
030820d5
WD
593 write_varint(f, racl->other_obj);
594 if (flags & XMIT_NAME_LIST)
595 send_ida_entries(&racl->names, f);
1c3344a1
WD
596
597 /* Give the allocated data to the new list object. */
598 *new_racl = *racl;
599 *racl = empty_rsync_acl;
600 }
601}
602
603/* Send the ACL from the statx structure down the indicated file descriptor.
604 * This also frees the ACL data. */
605void send_acl(statx *sxp, int f)
606{
1c3344a1
WD
607 if (!sxp->acc_acl) {
608 sxp->acc_acl = create_racl();
609 rsync_acl_fake_perms(sxp->acc_acl, sxp->st.st_mode);
610 }
611 /* Avoid sending values that can be inferred from other data. */
612 rsync_acl_strip_perms(sxp->acc_acl);
613
614 send_rsync_acl(sxp->acc_acl, SMB_ACL_TYPE_ACCESS, &access_acl_list, f);
615
616 if (S_ISDIR(sxp->st.st_mode)) {
617 if (!sxp->def_acl)
618 sxp->def_acl = create_racl();
619
620 send_rsync_acl(sxp->def_acl, SMB_ACL_TYPE_DEFAULT, &default_acl_list, f);
621 }
622}
623
624/* === Receive functions === */
625
030820d5
WD
626static uint32 recv_acl_access(uchar *name_follows_ptr, int f)
627{
628 uint32 access = read_varint(f);
629
630 if (name_follows_ptr) {
631 int flags = access & 3;
632 access >>= 2;
633 if (access & ~SMB_ACL_VALID_NAME_BITS)
634 goto value_error;
635 if (flags & XFLAG_NAME_FOLLOWS)
636 *name_follows_ptr = 1;
637 else
638 *name_follows_ptr = 0;
639 if (flags & XFLAG_NAME_IS_USER)
640 access |= NAME_IS_USER;
641 } else if (access & ~SMB_ACL_VALID_OBJ_BITS) {
642 value_error:
643 rprintf(FERROR, "recv_acl_access: value out of range: %x\n",
644 access);
1c3344a1
WD
645 exit_cleanup(RERR_STREAMIO);
646 }
647
648 return access;
649}
650
030820d5 651static uchar recv_ida_entries(ida_entries *ent, int f)
1c3344a1
WD
652{
653 uchar computed_mask_bits = 0;
f31514ad 654 int i, count = read_varint(f);
1c3344a1
WD
655
656 if (count) {
33233b49 657 if (!(ent->idas = new_array(id_access, count)))
1c3344a1
WD
658 out_of_memory("recv_ida_entries");
659 } else
660 ent->idas = NULL;
661
662 ent->count = count;
663
664 for (i = 0; i < count; i++) {
a217c453 665 uchar has_name;
f31514ad 666 id_t id = read_varint(f);
030820d5 667 uint32 access = recv_acl_access(&has_name, f);
1c3344a1
WD
668
669 if (has_name) {
030820d5 670 if (access & NAME_IS_USER)
1c3344a1
WD
671 id = recv_user_name(f, id);
672 else
33233b49 673 id = recv_group_name(f, id, NULL);
030820d5 674 } else if (access & NAME_IS_USER) {
1c3344a1
WD
675 if (inc_recurse && am_root && !numeric_ids)
676 id = match_uid(id);
677 } else {
678 if (inc_recurse && (!am_root || !numeric_ids))
33233b49 679 id = match_gid(id, NULL);
1c3344a1
WD
680 }
681
682 ent->idas[i].id = id;
683 ent->idas[i].access = access;
684 computed_mask_bits |= access;
685 }
686
030820d5 687 return computed_mask_bits & ~NO_ENTRY;
1c3344a1
WD
688}
689
690static int recv_rsync_acl(item_list *racl_list, SMB_ACL_TYPE_T type, int f)
691{
692 uchar computed_mask_bits = 0;
693 acl_duo *duo_item;
694 uchar flags;
f31514ad 695 int ndx = read_varint(f);
1c3344a1
WD
696
697 if (ndx < 0 || (size_t)ndx > racl_list->count) {
698 rprintf(FERROR, "recv_acl_index: %s ACL index %d > %d\n",
e5abce8e 699 str_acl_type(type), ndx, (int)racl_list->count);
1c3344a1
WD
700 exit_cleanup(RERR_STREAMIO);
701 }
702
703 if (ndx != 0)
704 return ndx - 1;
705
706 ndx = racl_list->count;
707 duo_item = EXPAND_ITEM_LIST(racl_list, acl_duo, 1000);
708 duo_item->racl = empty_rsync_acl;
709
710 flags = read_byte(f);
711
712 if (flags & XMIT_USER_OBJ)
713 duo_item->racl.user_obj = recv_acl_access(NULL, f);
1c3344a1
WD
714 if (flags & XMIT_GROUP_OBJ)
715 duo_item->racl.group_obj = recv_acl_access(NULL, f);
1c3344a1
WD
716 if (flags & XMIT_MASK_OBJ)
717 duo_item->racl.mask_obj = recv_acl_access(NULL, f);
1c3344a1
WD
718 if (flags & XMIT_OTHER_OBJ)
719 duo_item->racl.other_obj = recv_acl_access(NULL, f);
030820d5
WD
720 if (flags & XMIT_NAME_LIST)
721 computed_mask_bits |= recv_ida_entries(&duo_item->racl.names, f);
1c3344a1 722
030820d5 723 if (!duo_item->racl.names.count) {
1c3344a1
WD
724 /* If we received a superfluous mask, throw it away. */
725 if (duo_item->racl.mask_obj != NO_ENTRY) {
726 /* Mask off the group perms with it first. */
727 duo_item->racl.group_obj &= duo_item->racl.mask_obj | NO_ENTRY;
728 duo_item->racl.mask_obj = NO_ENTRY;
729 }
730 } else if (duo_item->racl.mask_obj == NO_ENTRY) /* Must be non-empty with lists. */
030820d5 731 duo_item->racl.mask_obj = (computed_mask_bits | duo_item->racl.group_obj) & ~NO_ENTRY;
1c3344a1
WD
732
733 duo_item->sacl = NULL;
734
735 return ndx;
736}
737
738/* Receive the ACL info the sender has included for this file-list entry. */
739void receive_acl(struct file_struct *file, int f)
740{
741 F_ACL(file) = recv_rsync_acl(&access_acl_list, SMB_ACL_TYPE_ACCESS, f);
742
743 if (S_ISDIR(file->mode))
744 F_DEF_ACL(file) = recv_rsync_acl(&default_acl_list, SMB_ACL_TYPE_DEFAULT, f);
745}
746
747static int cache_rsync_acl(rsync_acl *racl, SMB_ACL_TYPE_T type, item_list *racl_list)
748{
749 int ndx;
750
751 if (!racl)
752 ndx = -1;
753 else if ((ndx = find_matching_rsync_acl(racl, type, racl_list)) == -1) {
754 acl_duo *new_duo;
755 ndx = racl_list->count;
756 new_duo = EXPAND_ITEM_LIST(racl_list, acl_duo, 1000);
757 new_duo->racl = *racl;
758 new_duo->sacl = NULL;
759 *racl = empty_rsync_acl;
760 }
761
762 return ndx;
763}
764
765/* Turn the ACL data in statx into cached ACL data, setting the index
766 * values in the file struct. */
767void cache_acl(struct file_struct *file, statx *sxp)
768{
769 F_ACL(file) = cache_rsync_acl(sxp->acc_acl,
770 SMB_ACL_TYPE_ACCESS, &access_acl_list);
771
772 if (S_ISDIR(sxp->st.st_mode)) {
773 F_DEF_ACL(file) = cache_rsync_acl(sxp->def_acl,
774 SMB_ACL_TYPE_DEFAULT, &default_acl_list);
775 }
776}
777
778static mode_t change_sacl_perms(SMB_ACL_T sacl, rsync_acl *racl, mode_t old_mode, mode_t mode)
779{
780 SMB_ACL_ENTRY_T entry;
781 const char *errfun;
782 int rc;
783
784 if (S_ISDIR(mode)) {
785 /* If the sticky bit is going on, it's not safe to allow all
786 * the new ACL to go into effect before it gets set. */
787#ifdef SMB_ACL_LOSES_SPECIAL_MODE_BITS
788 if (mode & S_ISVTX)
789 mode &= ~0077;
790#else
791 if (mode & S_ISVTX && !(old_mode & S_ISVTX))
792 mode &= ~0077;
793 } else {
794 /* If setuid or setgid is going off, it's not safe to allow all
795 * the new ACL to go into effect before they get cleared. */
796 if ((old_mode & S_ISUID && !(mode & S_ISUID))
797 || (old_mode & S_ISGID && !(mode & S_ISGID)))
798 mode &= ~0077;
799#endif
800 }
801
802 errfun = "sys_acl_get_entry";
803 for (rc = sys_acl_get_entry(sacl, SMB_ACL_FIRST_ENTRY, &entry);
804 rc == 1;
805 rc = sys_acl_get_entry(sacl, SMB_ACL_NEXT_ENTRY, &entry)) {
806 SMB_ACL_TAG_T tag_type;
030820d5 807 if ((rc = sys_acl_get_tag_type(entry, &tag_type)) != 0) {
1c3344a1
WD
808 errfun = "sys_acl_get_tag_type";
809 break;
810 }
811 switch (tag_type) {
812 case SMB_ACL_USER_OBJ:
813 COE2( store_access_in_entry,((mode >> 6) & 7, entry) );
814 break;
815 case SMB_ACL_GROUP_OBJ:
816 /* group is only empty when identical to group perms. */
817 if (racl->group_obj != NO_ENTRY)
818 break;
819 COE2( store_access_in_entry,((mode >> 3) & 7, entry) );
820 break;
821 case SMB_ACL_MASK:
822#ifndef ACLS_NEED_MASK
823 /* mask is only empty when we don't need it. */
824 if (racl->mask_obj == NO_ENTRY)
825 break;
826#endif
827 COE2( store_access_in_entry,((mode >> 3) & 7, entry) );
828 break;
829 case SMB_ACL_OTHER:
830 COE2( store_access_in_entry,(mode & 7, entry) );
831 break;
832 }
833 }
834 if (rc) {
835 error_exit:
836 if (errfun) {
837 rsyserr(FERROR, errno, "change_sacl_perms: %s()",
838 errfun);
839 }
840 return (mode_t)~0;
841 }
842
843#ifdef SMB_ACL_LOSES_SPECIAL_MODE_BITS
844 /* Ensure that chmod() will be called to restore any lost setid bits. */
845 if (old_mode & (S_ISUID | S_ISGID | S_ISVTX)
846 && BITS_EQUAL(old_mode, mode, CHMOD_BITS))
847 old_mode &= ~(S_ISUID | S_ISGID | S_ISVTX);
848#endif
849
850 /* Return the mode of the file on disk, as we will set them. */
851 return (old_mode & ~ACCESSPERMS) | (mode & ACCESSPERMS);
852}
853
854static int set_rsync_acl(const char *fname, acl_duo *duo_item,
855 SMB_ACL_TYPE_T type, statx *sxp, mode_t mode)
856{
857 if (type == SMB_ACL_TYPE_DEFAULT
858 && duo_item->racl.user_obj == NO_ENTRY) {
859 if (sys_acl_delete_def_file(fname) < 0) {
860 rsyserr(FERROR, errno, "set_acl: sys_acl_delete_def_file(%s)",
861 fname);
862 return -1;
863 }
864 } else {
865 mode_t cur_mode = sxp->st.st_mode;
866 if (!duo_item->sacl
867 && !pack_smb_acl(&duo_item->sacl, &duo_item->racl))
868 return -1;
869 if (type == SMB_ACL_TYPE_ACCESS) {
870 cur_mode = change_sacl_perms(duo_item->sacl, &duo_item->racl,
871 cur_mode, mode);
872 if (cur_mode == (mode_t)~0)
873 return 0;
874 }
875 if (sys_acl_set_file(fname, type, duo_item->sacl) < 0) {
876 rsyserr(FERROR, errno, "set_acl: sys_acl_set_file(%s, %s)",
877 fname, str_acl_type(type));
878 return -1;
879 }
880 if (type == SMB_ACL_TYPE_ACCESS)
881 sxp->st.st_mode = cur_mode;
882 }
883
884 return 0;
885}
886
887/* Set ACL on indicated filename.
888 *
889 * This sets extended access ACL entries and default ACL. If convenient,
890 * it sets permission bits along with the access ACL and signals having
891 * done so by modifying sxp->st.st_mode.
892 *
893 * Returns 1 for unchanged, 0 for changed, -1 for failed. Call this
894 * with fname set to NULL to just check if the ACL is unchanged. */
895int set_acl(const char *fname, const struct file_struct *file, statx *sxp)
896{
897 int unchanged = 1;
898 int32 ndx;
899 BOOL eq;
900
901 if (!dry_run && (read_only || list_only)) {
902 errno = EROFS;
903 return -1;
904 }
905
906 ndx = F_ACL(file);
907 if (ndx >= 0 && (size_t)ndx < access_acl_list.count) {
908 acl_duo *duo_item = access_acl_list.items;
909 duo_item += ndx;
910 eq = sxp->acc_acl
911 && rsync_acl_equal_enough(sxp->acc_acl, &duo_item->racl, file->mode);
912 if (!eq) {
913 unchanged = 0;
914 if (!dry_run && fname
915 && set_rsync_acl(fname, duo_item, SMB_ACL_TYPE_ACCESS,
916 sxp, file->mode) < 0)
917 unchanged = -1;
918 }
919 }
920
921 if (!S_ISDIR(sxp->st.st_mode))
922 return unchanged;
923
924 ndx = F_DEF_ACL(file);
925 if (ndx >= 0 && (size_t)ndx < default_acl_list.count) {
926 acl_duo *duo_item = default_acl_list.items;
927 duo_item += ndx;
928 eq = sxp->def_acl && rsync_acl_equal(sxp->def_acl, &duo_item->racl);
929 if (!eq) {
930 if (unchanged > 0)
931 unchanged = 0;
932 if (!dry_run && fname
933 && set_rsync_acl(fname, duo_item, SMB_ACL_TYPE_DEFAULT,
934 sxp, file->mode) < 0)
935 unchanged = -1;
936 }
937 }
938
939 return unchanged;
940}
941
33233b49
WD
942/* Non-incremental recursion needs to convert all the received IDs.
943 * This is done in a single pass after receiving the whole file-list. */
a217c453 944static void match_racl_ids(const item_list *racl_list)
1c3344a1 945{
a217c453
WD
946 int list_cnt, name_cnt;
947 acl_duo *duo_item = racl_list->items;
948 for (list_cnt = racl_list->count; list_cnt--; duo_item++) {
030820d5 949 ida_entries *idal = &duo_item->racl.names;
33233b49 950 id_access *ida = idal->idas;
030820d5
WD
951 for (name_cnt = idal->count; name_cnt--; ida++) {
952 if (ida->access & NAME_IS_USER)
953 ida->id = match_uid(ida->id);
954 else
955 ida->id = match_gid(ida->id, NULL);
956 }
1c3344a1 957 }
1c3344a1
WD
958}
959
a217c453 960void match_acl_ids(void)
1c3344a1 961{
a217c453
WD
962 match_racl_ids(&access_acl_list);
963 match_racl_ids(&default_acl_list);
1c3344a1
WD
964}
965
966/* This is used by dest_mode(). */
967int default_perms_for_dir(const char *dir)
968{
969 rsync_acl racl;
970 SMB_ACL_T sacl;
971 BOOL ok;
972 int perms;
973
974 if (dir == NULL)
975 dir = ".";
976 perms = ACCESSPERMS & ~orig_umask;
977 /* Read the directory's default ACL. If it has none, this will successfully return an empty ACL. */
978 sacl = sys_acl_get_file(dir, SMB_ACL_TYPE_DEFAULT);
979 if (sacl == NULL) {
980 /* Couldn't get an ACL. Darn. */
981 switch (errno) {
d4a013ed 982#ifdef ENOTSUP
1c3344a1 983 case ENOTSUP:
d4a013ed 984#endif
1c3344a1
WD
985 case ENOSYS:
986 /* No ACLs are available. */
987 break;
988 case ENOENT:
989 if (dry_run) {
990 /* We're doing a dry run, so the containing directory
991 * wasn't actually created. Don't worry about it. */
992 break;
993 }
994 /* Otherwise fall through. */
995 default:
996 rprintf(FERROR, "default_perms_for_dir: sys_acl_get_file(%s, %s): %s, falling back on umask\n",
997 dir, str_acl_type(SMB_ACL_TYPE_DEFAULT), strerror(errno));
998 }
999 return perms;
1000 }
1001
1002 /* Convert it. */
1003 racl = empty_rsync_acl;
1004 ok = unpack_smb_acl(sacl, &racl);
1005 sys_acl_free_acl(sacl);
1006 if (!ok) {
1007 rprintf(FERROR, "default_perms_for_dir: unpack_smb_acl failed, falling back on umask\n");
1008 return perms;
1009 }
1010
1011 /* Apply the permission-bit entries of the default ACL, if any. */
1012 if (racl.user_obj != NO_ENTRY) {
1013 perms = rsync_acl_get_perms(&racl);
1014 if (verbose > 2)
1015 rprintf(FINFO, "got ACL-based default perms %o for directory %s\n", perms, dir);
1016 }
1017
1018 rsync_acl_free(&racl);
1019 return perms;
1020}
1021
1022#endif /* SUPPORT_ACLS */