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