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