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