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