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