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