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