The patches for 3.0.0pre6.
[rsync-patches.git] / acls.diff
1 This patch adds backward-compatibility support for the --acls option.
2 Since the main release has never had ACL support, the trunk doesn't
3 need this code.  If you want to make rsync 3.0.x communicate with an
4 older (patched) release, use this.
5
6 To use this patch, run these commands for a successful build:
7
8     patch -p1 <patches/acls.diff
9     ./configure                         (optional if already run)
10     make
11
12 diff --git a/acls.c b/acls.c
13 --- a/acls.c
14 +++ b/acls.c
15 @@ -31,6 +31,7 @@ extern int list_only;
16  extern int orig_umask;
17  extern int numeric_ids;
18  extern int inc_recurse;
19 +extern int protocol_version;
20  
21  /* Flags used to indicate what items are being transmitted for an entry. */
22  #define XMIT_USER_OBJ (1<<0)
23 @@ -97,6 +98,18 @@ static const char *str_acl_type(SMB_ACL_TYPE_T type)
24              : "unknown SMB_ACL_TYPE_T";
25  }
26  
27 +#define OTHER_TYPE(t) (SMB_ACL_TYPE_ACCESS+SMB_ACL_TYPE_DEFAULT-(t))
28 +#define BUMP_TYPE(t) ((t = OTHER_TYPE(t)) == SMB_ACL_TYPE_DEFAULT)
29 +
30 +static int old_count_racl_entries(const rsync_acl *racl)
31 +{
32 +       return racl->names.count
33 +            + (racl->user_obj != NO_ENTRY)
34 +            + (racl->group_obj != NO_ENTRY)
35 +            + (racl->mask_obj != NO_ENTRY)
36 +            + (racl->other_obj != NO_ENTRY);
37 +}
38 +
39  static int calc_sacl_entries(const rsync_acl *racl)
40  {
41         /* A System ACL always gets user/group/other permission entries. */
42 @@ -544,6 +557,96 @@ int get_acl(const char *fname, stat_x *sxp)
43         return 0;
44  }
45  
46 +/* === OLD Send functions === */
47 +
48 +/* Send the ida list over the file descriptor. */
49 +static void old_send_ida_entries(int f, const ida_entries *idal, char tag_char)
50 +{
51 +       id_access *ida;
52 +       size_t count = idal->count;
53 +       for (ida = idal->idas; count--; ida++) {
54 +               if (tag_char == 'U') {
55 +                       if (!(ida->access & NAME_IS_USER))
56 +                               continue;
57 +                       add_uid(ida->id);
58 +               } else {
59 +                       if (ida->access & NAME_IS_USER)
60 +                               continue;
61 +                       add_gid(ida->id);
62 +               }
63 +               write_byte(f, tag_char);
64 +               write_byte(f, ida->access);
65 +               write_int(f, ida->id);
66 +       }
67 +}
68 +
69 +/* Send an rsync ACL over the file descriptor. */
70 +static void old_send_rsync_acl(int f, const rsync_acl *racl)
71 +{
72 +       size_t count = old_count_racl_entries(racl);
73 +       write_int(f, count);
74 +       if (racl->user_obj != NO_ENTRY) {
75 +               write_byte(f, 'u');
76 +               write_byte(f, racl->user_obj);
77 +       }
78 +       old_send_ida_entries(f, &racl->names, 'U');
79 +       if (racl->group_obj != NO_ENTRY) {
80 +               write_byte(f, 'g');
81 +               write_byte(f, racl->group_obj);
82 +       }
83 +       old_send_ida_entries(f, &racl->names, 'G');
84 +       if (racl->mask_obj != NO_ENTRY) {
85 +               write_byte(f, 'm');
86 +               write_byte(f, racl->mask_obj);
87 +       }
88 +       if (racl->other_obj != NO_ENTRY) {
89 +               write_byte(f, 'o');
90 +               write_byte(f, racl->other_obj);
91 +       }
92 +}
93 +
94 +/* Send the ACL from the stat_x structure down the indicated file descriptor.
95 + * This also frees the ACL data. */
96 +void old_send_acl(stat_x *sxp, int f)
97 +{
98 +       SMB_ACL_TYPE_T type;
99 +       rsync_acl *racl, *new_racl;
100 +       item_list *racl_list;
101 +       int ndx;
102 +
103 +       type = SMB_ACL_TYPE_ACCESS;
104 +       racl = sxp->acc_acl;
105 +       racl_list = &access_acl_list;
106 +       do {
107 +               if (!racl) {
108 +                       racl = new(rsync_acl);
109 +                       if (!racl)
110 +                               out_of_memory("send_acl");
111 +                       *racl = empty_rsync_acl;
112 +                       if (type == SMB_ACL_TYPE_ACCESS) {
113 +                               rsync_acl_fake_perms(racl, sxp->st.st_mode);
114 +                               sxp->acc_acl = racl;
115 +                       } else
116 +                               sxp->def_acl = racl;
117 +               }
118 +
119 +               if ((ndx = find_matching_rsync_acl(racl, type, racl_list)) != -1) {
120 +                       write_byte(f, type == SMB_ACL_TYPE_ACCESS ? 'a' : 'd');
121 +                       write_int(f, ndx);
122 +               } else {
123 +                       new_racl = EXPAND_ITEM_LIST(racl_list, rsync_acl, 1000);
124 +                       write_byte(f, type == SMB_ACL_TYPE_ACCESS ? 'A' : 'D');
125 +                       old_send_rsync_acl(f, racl);
126 +                       *new_racl = *racl;
127 +                       *racl = empty_rsync_acl;
128 +               }
129 +               racl = sxp->def_acl;
130 +               racl_list = &default_acl_list;
131 +       } while (BUMP_TYPE(type) && S_ISDIR(sxp->st.st_mode));
132 +
133 +       free_acl(sxp);
134 +}
135 +
136  /* === Send functions === */
137  
138  /* Send the ida list over the file descriptor. */
139 @@ -619,6 +722,11 @@ static void send_rsync_acl(rsync_acl *racl, SMB_ACL_TYPE_T type,
140   * This also frees the ACL data. */
141  void send_acl(stat_x *sxp, int f)
142  {
143 +       if (protocol_version < 30) {
144 +               old_send_acl(sxp, f);
145 +               return;
146 +       }
147 +
148         if (!sxp->acc_acl) {
149                 sxp->acc_acl = create_racl();
150                 rsync_acl_fake_perms(sxp->acc_acl, sxp->st.st_mode);
151 @@ -636,6 +744,160 @@ void send_acl(stat_x *sxp, int f)
152         }
153  }
154  
155 +/* === OLD Receive functions */
156 +
157 +static void old_recv_rsync_acl(rsync_acl *racl, int f)
158 +{
159 +       static item_list temp_ida_list = EMPTY_ITEM_LIST;
160 +       SMB_ACL_TAG_T tag_type = 0;
161 +       uchar computed_mask_bits = 0;
162 +       id_access *ida;
163 +       size_t count;
164 +
165 +       if (!(count = read_int(f)))
166 +               return;
167 +
168 +       while (count--) {
169 +               char tag = read_byte(f);
170 +               uchar access = read_byte(f);
171 +               if (access & ~ (4 | 2 | 1)) {
172 +                       rprintf(FERROR, "old_recv_rsync_acl: bogus permset %o\n",
173 +                               access);
174 +                       exit_cleanup(RERR_STREAMIO);
175 +               }
176 +               switch (tag) {
177 +               case 'u':
178 +                       if (racl->user_obj != NO_ENTRY) {
179 +                               rprintf(FERROR, "old_recv_rsync_acl: error: duplicate USER_OBJ entry\n");
180 +                               exit_cleanup(RERR_STREAMIO);
181 +                       }
182 +                       racl->user_obj = access;
183 +                       continue;
184 +               case 'U':
185 +                       tag_type = SMB_ACL_USER;
186 +                       break;
187 +               case 'g':
188 +                       if (racl->group_obj != NO_ENTRY) {
189 +                               rprintf(FERROR, "old_recv_rsync_acl: error: duplicate GROUP_OBJ entry\n");
190 +                               exit_cleanup(RERR_STREAMIO);
191 +                       }
192 +                       racl->group_obj = access;
193 +                       continue;
194 +               case 'G':
195 +                       tag_type = SMB_ACL_GROUP;
196 +                       break;
197 +               case 'm':
198 +                       if (racl->mask_obj != NO_ENTRY) {
199 +                               rprintf(FERROR, "old_recv_rsync_acl: error: duplicate MASK entry\n");
200 +                               exit_cleanup(RERR_STREAMIO);
201 +                       }
202 +                       racl->mask_obj = access;
203 +                       continue;
204 +               case 'o':
205 +                       if (racl->other_obj != NO_ENTRY) {
206 +                               rprintf(FERROR, "old_recv_rsync_acl: error: duplicate OTHER entry\n");
207 +                               exit_cleanup(RERR_STREAMIO);
208 +                       }
209 +                       racl->other_obj = access;
210 +                       continue;
211 +               default:
212 +                       rprintf(FERROR, "old_recv_rsync_acl: unknown tag %c\n",
213 +                               tag);
214 +                       exit_cleanup(RERR_STREAMIO);
215 +               }
216 +               ida = EXPAND_ITEM_LIST(&temp_ida_list, id_access, -10);
217 +               ida->access = access | (tag_type == SMB_ACL_USER ? NAME_IS_USER : 0);
218 +               ida->id = read_int(f);
219 +               computed_mask_bits |= access;
220 +       }
221 +
222 +       /* Transfer the count id_access items out of the temp_ida_list
223 +        * into the names ida_entries list in racl. */
224 +       if (temp_ida_list.count) {
225 +#ifdef SMB_ACL_NEED_SORT
226 +               if (temp_ida_list.count > 1) {
227 +                       qsort(temp_ida_list.items, temp_ida_list.count,
228 +                             sizeof (id_access), id_access_sorter);
229 +               }
230 +#endif
231 +               if (!(racl->names.idas = new_array(id_access, temp_ida_list.count)))
232 +                       out_of_memory("unpack_smb_acl");
233 +               memcpy(racl->names.idas, temp_ida_list.items,
234 +                      temp_ida_list.count * sizeof (id_access));
235 +       } else
236 +               racl->names.idas = NULL;
237 +
238 +       racl->names.count = temp_ida_list.count;
239 +
240 +       /* Truncate the temporary list now that its idas have been saved. */
241 +       temp_ida_list.count = 0;
242 +
243 +       if (!racl->names.count) {
244 +               /* If we received a superfluous mask, throw it away. */
245 +               if (racl->mask_obj != NO_ENTRY) {
246 +                       /* Mask off the group perms with it first. */
247 +                       racl->group_obj &= racl->mask_obj | NO_ENTRY;
248 +                       racl->mask_obj = NO_ENTRY;
249 +               }
250 +       } else if (racl->mask_obj == NO_ENTRY) /* Must be non-empty with lists. */
251 +               racl->mask_obj = (computed_mask_bits | racl->group_obj) & 7;
252 +}
253 +
254 +/* Receive the ACL info the sender has included for this file-list entry. */
255 +void old_recv_acl(struct file_struct *file, int f)
256 +{
257 +       SMB_ACL_TYPE_T type;
258 +       item_list *racl_list;
259 +
260 +       if (S_ISLNK(file->mode))
261 +               return;
262 +
263 +       type = SMB_ACL_TYPE_ACCESS;
264 +       racl_list = &access_acl_list;
265 +       do {
266 +               char tag = read_byte(f);
267 +               int ndx;
268 +
269 +               if (tag == 'A' || tag == 'a') {
270 +                       if (type != SMB_ACL_TYPE_ACCESS) {
271 +                               rprintf(FERROR, "receive_acl %s: duplicate access ACL\n",
272 +                                       f_name(file, NULL));
273 +                               exit_cleanup(RERR_STREAMIO);
274 +                       }
275 +               } else if (tag == 'D' || tag == 'd') {
276 +                       if (type == SMB_ACL_TYPE_ACCESS) {
277 +                               rprintf(FERROR, "receive_acl %s: expecting access ACL; got default\n",
278 +                                       f_name(file, NULL));
279 +                               exit_cleanup(RERR_STREAMIO);
280 +                       }
281 +               } else {
282 +                       rprintf(FERROR, "receive_acl %s: unknown ACL type tag: %c\n",
283 +                               f_name(file, NULL), tag);
284 +                       exit_cleanup(RERR_STREAMIO);
285 +               }
286 +               if (tag == 'A' || tag == 'D') {
287 +                       acl_duo *duo_item;
288 +                       ndx = racl_list->count;
289 +                       duo_item = EXPAND_ITEM_LIST(racl_list, acl_duo, 1000);
290 +                       duo_item->racl = empty_rsync_acl;
291 +                       old_recv_rsync_acl(&duo_item->racl, f);
292 +                       duo_item->sacl = NULL;
293 +               } else {
294 +                       ndx = read_int(f);
295 +                       if (ndx < 0 || (size_t)ndx >= racl_list->count) {
296 +                               rprintf(FERROR, "receive_acl %s: %s ACL index %d out of range\n",
297 +                                       f_name(file, NULL), str_acl_type(type), ndx);
298 +                               exit_cleanup(RERR_STREAMIO);
299 +                       }
300 +               }
301 +               if (type == SMB_ACL_TYPE_ACCESS)
302 +                       F_ACL(file) = ndx;
303 +               else
304 +                       F_DIR_DEFACL(file) = ndx;
305 +               racl_list = &default_acl_list;
306 +       } while (BUMP_TYPE(type) && S_ISDIR(file->mode));
307 +}
308 +
309  /* === Receive functions === */
310  
311  static uint32 recv_acl_access(uchar *name_follows_ptr, int f)
312 @@ -758,6 +1020,11 @@ static int recv_rsync_acl(item_list *racl_list, SMB_ACL_TYPE_T type, int f)
313  /* Receive the ACL info the sender has included for this file-list entry. */
314  void receive_acl(struct file_struct *file, int f)
315  {
316 +       if (protocol_version < 30) {
317 +               old_recv_acl(file, f);
318 +               return;
319 +       }
320 +
321         F_ACL(file) = recv_rsync_acl(&access_acl_list, SMB_ACL_TYPE_ACCESS, f);
322  
323         if (S_ISDIR(file->mode))
324 diff --git a/compat.c b/compat.c
325 --- a/compat.c
326 +++ b/compat.c
327 @@ -175,13 +175,6 @@ void setup_protocol(int f_out,int f_in)
328         if (protocol_version < 30) {
329                 if (append_mode == 1)
330                         append_mode = 2;
331 -               if (preserve_acls && !local_server) {
332 -                       rprintf(FERROR,
333 -                           "--acls requires protocol 30 or higher"
334 -                           " (negotiated %d).\n",
335 -                           protocol_version);
336 -                       exit_cleanup(RERR_PROTOCOL);
337 -               }
338                 if (preserve_xattrs && !local_server) {
339                         rprintf(FERROR,
340                             "--xattrs requires protocol 30 or higher"