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