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