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