Commit | Line | Data |
---|---|---|
0f78b815 WD |
1 | /* |
2 | * The filter include/exclude routines. | |
0f2ac855 | 3 | * |
0f78b815 WD |
4 | * Copyright (C) 1996-2001 Andrew Tridgell <tridge@samba.org> |
5 | * Copyright (C) 1996 Paul Mackerras | |
6 | * Copyright (C) 2002 Martin Pool | |
b3bf9b9d | 7 | * Copyright (C) 2003-2009 Wayne Davison |
0f2ac855 | 8 | * |
07a874fd | 9 | * This program is free software; you can redistribute it and/or modify |
8e41b68e WD |
10 | * it under the terms of the GNU General Public License as published by |
11 | * the Free Software Foundation; either version 3 of the License, or | |
12 | * (at your option) any later version. | |
0f2ac855 | 13 | * |
07a874fd MP |
14 | * This program is distributed in the hope that it will be useful, |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
0f2ac855 | 18 | * |
e7c67065 | 19 | * You should have received a copy of the GNU General Public License along |
4fd842f9 | 20 | * with this program; if not, visit the http://fsf.org website. |
07a874fd | 21 | */ |
c627d613 | 22 | |
c627d613 AT |
23 | #include "rsync.h" |
24 | ||
5a016db9 | 25 | extern int am_server; |
fdc79501 | 26 | extern int am_sender; |
40d38dc0 | 27 | extern int eol_nulls; |
6dfd07d0 | 28 | extern int io_error; |
1412da7c | 29 | extern int local_server; |
d5782b52 | 30 | extern int prune_empty_dirs; |
ccdb23bb | 31 | extern int ignore_perishable; |
fdc79501 WD |
32 | extern int delete_mode; |
33 | extern int delete_excluded; | |
34 | extern int cvs_exclude; | |
6dfd07d0 WD |
35 | extern int sanitize_paths; |
36 | extern int protocol_version; | |
bf39270e | 37 | extern int module_id; |
40d38dc0 | 38 | |
b791d680 | 39 | extern char curr_dir[MAXPATHLEN]; |
6dfd07d0 WD |
40 | extern unsigned int curr_dir_len; |
41 | extern unsigned int module_dirlen; | |
c627d613 | 42 | |
7634fc8e WD |
43 | struct filter_list_struct filter_list = { .debug_type = "" }; |
44 | struct filter_list_struct cvs_filter_list = { .debug_type = " [global CVS]" }; | |
45 | struct filter_list_struct daemon_filter_list = { .debug_type = " [daemon]" }; | |
c627d613 | 46 | |
6694ead8 | 47 | struct filter_struct *last_hit_filter; |
6dfd07d0 | 48 | |
6694ead8 MM |
49 | /* Need room enough for ":MODS " prefix, which can now include |
50 | * chmod/user/group values. */ | |
51 | #define MAX_RULE_PREFIX (256) | |
b6f06b8e | 52 | |
f5aeb6ff WD |
53 | #define SLASH_WILD3_SUFFIX "/***" |
54 | ||
7842418b | 55 | /* The dirbuf is set by push_local_filters() to the current subdirectory |
6dfd07d0 WD |
56 | * relative to curr_dir that is being processed. The path always has a |
57 | * trailing slash appended, and the variable dirbuf_len contains the length | |
58 | * of this path prefix. The path is always absolute. */ | |
59 | static char dirbuf[MAXPATHLEN+1]; | |
60 | static unsigned int dirbuf_len = 0; | |
61 | static int dirbuf_depth; | |
62 | ||
63 | /* This is True when we're scanning parent dirs for per-dir merge-files. */ | |
64 | static BOOL parent_dirscan = False; | |
65 | ||
66 | /* This array contains a list of all the currently active per-dir merge | |
67 | * files. This makes it easier to save the appropriate values when we | |
68 | * "push" down into each subdirectory. */ | |
7842418b | 69 | static struct filter_struct **mergelist_parents; |
6dfd07d0 WD |
70 | static int mergelist_cnt = 0; |
71 | static int mergelist_size = 0; | |
72 | ||
7842418b | 73 | /* Each filter_list_struct describes a singly-linked list by keeping track |
6dfd07d0 WD |
74 | * of both the head and tail pointers. The list is slightly unusual in that |
75 | * a parent-dir's content can be appended to the end of the local list in a | |
76 | * special way: the last item in the local list has its "next" pointer set | |
77 | * to point to the inherited list, but the local list's tail pointer points | |
78 | * at the end of the local list. Thus, if the local list is empty, the head | |
79 | * will be pointing at the inherited content but the tail will be NULL. To | |
80 | * help you visualize this, here are the possible list arrangements: | |
81 | * | |
82 | * Completely Empty Local Content Only | |
83 | * ================================== ==================================== | |
84 | * head -> NULL head -> Local1 -> Local2 -> NULL | |
85 | * tail -> NULL tail -------------^ | |
86 | * | |
87 | * Inherited Content Only Both Local and Inherited Content | |
88 | * ================================== ==================================== | |
89 | * head -> Parent1 -> Parent2 -> NULL head -> L1 -> L2 -> P1 -> P2 -> NULL | |
90 | * tail -> NULL tail ---------^ | |
91 | * | |
92 | * This means that anyone wanting to traverse the whole list to use it just | |
93 | * needs to start at the head and use the "next" pointers until it goes | |
94 | * NULL. To add new local content, we insert the item after the tail item | |
95 | * and update the tail (obviously, if "tail" was NULL, we insert it at the | |
96 | * head). To clear the local list, WE MUST NOT FREE THE INHERITED CONTENT | |
97 | * because it is shared between the current list and our parent list(s). | |
98 | * The easiest way to handle this is to simply truncate the list after the | |
99 | * tail item and then free the local list from the head. When inheriting | |
7842418b | 100 | * the list for a new local dir, we just save off the filter_list_struct |
6dfd07d0 WD |
101 | * values (so we can pop back to them later) and set the tail to NULL. |
102 | */ | |
103 | ||
daa8d920 MM |
104 | static void teardown_mergelist(struct filter_struct *ex) |
105 | { | |
7634fc8e | 106 | if (DEBUG_GTE(FILTER, 2)) { |
daa8d920 MM |
107 | rprintf(FINFO, "[%s] deactivating mergelist #%d%s\n", |
108 | who_am_i(), mergelist_cnt - 1, | |
109 | ex->u.mergelist->debug_type); | |
7634fc8e | 110 | } |
daa8d920 MM |
111 | |
112 | /* We should deactivate mergelists in LIFO order. */ | |
113 | assert(mergelist_cnt > 0); | |
114 | assert(ex == mergelist_parents[mergelist_cnt - 1]); | |
115 | ||
116 | /* The parent_dirscan filters should have been freed. */ | |
117 | assert(ex->u.mergelist->parent_dirscan_head == NULL); | |
118 | ||
119 | free(ex->u.mergelist->debug_type); | |
120 | free(ex->u.mergelist); | |
121 | mergelist_cnt--; | |
122 | } | |
123 | ||
6694ead8 MM |
124 | static struct filter_chmod_struct *ref_filter_chmod(struct filter_chmod_struct *chmod) |
125 | { | |
126 | chmod->ref_cnt++; | |
127 | assert(chmod->ref_cnt != 0); /* Catch overflow. */ | |
128 | return chmod; | |
129 | } | |
130 | ||
131 | static void unref_filter_chmod(struct filter_chmod_struct *chmod) | |
132 | { | |
133 | chmod->ref_cnt--; | |
134 | if (chmod->ref_cnt == 0) { | |
135 | free(chmod->modestr); | |
136 | free_chmod_mode(chmod->modes); | |
137 | free(chmod); | |
138 | } | |
139 | } | |
140 | ||
7842418b | 141 | static void free_filter(struct filter_struct *ex) |
6dfd07d0 | 142 | { |
6694ead8 MM |
143 | if (ex->flags & MATCHFLG_CHMOD) |
144 | unref_filter_chmod(ex->chmod); | |
6dfd07d0 WD |
145 | free(ex->pattern); |
146 | free(ex); | |
147 | } | |
148 | ||
daa8d920 MM |
149 | static void free_filters(struct filter_struct *head) |
150 | { | |
151 | struct filter_struct *rev_head = NULL; | |
152 | ||
153 | /* Reverse the list so we deactivate mergelists in the proper LIFO | |
154 | * order. */ | |
155 | while (head) { | |
156 | struct filter_struct *next = head->next; | |
157 | head->next = rev_head; | |
158 | rev_head = head; | |
159 | head = next; | |
160 | } | |
161 | ||
162 | while (rev_head) { | |
163 | struct filter_struct *prev = rev_head->next; | |
ebaa4296 MM |
164 | /* Tear down mergelists here, not in free_filter, so that we |
165 | * affect only real filter lists and not temporarily allocated | |
166 | * filters. */ | |
167 | if (rev_head->flags & MATCHFLG_PERDIR_MERGE) | |
168 | teardown_mergelist(rev_head); | |
daa8d920 MM |
169 | free_filter(rev_head); |
170 | rev_head = prev; | |
171 | } | |
172 | } | |
173 | ||
7842418b | 174 | /* Build a filter structure given a filter pattern. The value in "pat" |
ebaa4296 MM |
175 | * is not null-terminated. "filter" is either held or freed, so the |
176 | * caller should not free it. */ | |
177 | static void add_rule(struct filter_list_struct *listp, | |
178 | const char *pat, unsigned int pat_len, | |
179 | struct filter_struct *filter, int xflags) | |
c627d613 | 180 | { |
f8f72644 | 181 | const char *cp; |
f5aeb6ff | 182 | unsigned int pre_len, suf_len, slash_cnt = 0; |
c627d613 | 183 | |
951e826b | 184 | if (DEBUG_GTE(FILTER, 2)) { |
fdc79501 | 185 | rprintf(FINFO, "[%s] add_rule(%s%.*s%s)%s\n", |
ebaa4296 | 186 | who_am_i(), get_rule_prefix(filter, pat, 0, NULL), |
fdc79501 | 187 | (int)pat_len, pat, |
ebaa4296 | 188 | (filter->flags & MATCHFLG_DIRECTORY) ? "/" : "", |
6dfd07d0 WD |
189 | listp->debug_type); |
190 | } | |
191 | ||
4fc8140a | 192 | /* These flags also indicate that we're reading a list that |
6d7b3d52 | 193 | * needs to be filtered now, not post-filtered later. */ |
ebaa4296 MM |
194 | if (xflags & (XFLG_ANCHORED2ABS|XFLG_ABS_IF_SLASH) |
195 | && (filter->flags & MATCHFLGS_SIDES) | |
196 | == (am_sender ? MATCHFLG_RECEIVER_SIDE : MATCHFLG_SENDER_SIDE)) { | |
197 | /* This filter applies only to the other side. Drop it. */ | |
198 | free_filter(filter); | |
199 | return; | |
6d7b3d52 WD |
200 | } |
201 | ||
4c74d44d WD |
202 | if (pat_len > 1 && pat[pat_len-1] == '/') { |
203 | pat_len--; | |
ebaa4296 | 204 | filter->flags |= MATCHFLG_DIRECTORY; |
4c74d44d WD |
205 | } |
206 | ||
207 | for (cp = pat; cp < pat + pat_len; cp++) { | |
208 | if (*cp == '/') | |
209 | slash_cnt++; | |
210 | } | |
211 | ||
ebaa4296 | 212 | if (!(filter->flags & (MATCHFLG_ABS_PATH | MATCHFLG_MERGE_FILE)) |
4fc8140a | 213 | && ((xflags & (XFLG_ANCHORED2ABS|XFLG_ABS_IF_SLASH) && *pat == '/') |
4c74d44d | 214 | || (xflags & XFLG_ABS_IF_SLASH && slash_cnt))) { |
ebaa4296 | 215 | filter->flags |= MATCHFLG_ABS_PATH; |
4fc8140a | 216 | if (*pat == '/') |
4a86fbcd | 217 | pre_len = dirbuf_len - module_dirlen - 1; |
4fc8140a | 218 | else |
4a86fbcd | 219 | pre_len = 0; |
6dfd07d0 | 220 | } else |
4a86fbcd | 221 | pre_len = 0; |
4c74d44d | 222 | |
f5aeb6ff WD |
223 | /* The daemon wants dir-exclude rules to get an appended "/" + "***". */ |
224 | if (xflags & XFLG_DIR2WILD3 | |
ebaa4296 MM |
225 | && BITS_SETnUNSET(filter->flags, MATCHFLG_DIRECTORY, MATCHFLG_INCLUDE)) { |
226 | filter->flags &= ~MATCHFLG_DIRECTORY; | |
f5aeb6ff WD |
227 | suf_len = sizeof SLASH_WILD3_SUFFIX - 1; |
228 | } else | |
229 | suf_len = 0; | |
230 | ||
ebaa4296 | 231 | if (!(filter->pattern = new_array(char, pre_len + pat_len + suf_len + 1))) |
fdc79501 | 232 | out_of_memory("add_rule"); |
4c74d44d | 233 | if (pre_len) { |
ebaa4296 MM |
234 | memcpy(filter->pattern, dirbuf + module_dirlen, pre_len); |
235 | for (cp = filter->pattern; cp < filter->pattern + pre_len; cp++) { | |
4c74d44d WD |
236 | if (*cp == '/') |
237 | slash_cnt++; | |
238 | } | |
239 | } | |
ebaa4296 | 240 | strlcpy(filter->pattern + pre_len, pat, pat_len + 1); |
4a86fbcd | 241 | pat_len += pre_len; |
f5aeb6ff | 242 | if (suf_len) { |
ebaa4296 | 243 | memcpy(filter->pattern + pat_len, SLASH_WILD3_SUFFIX, suf_len+1); |
f5aeb6ff WD |
244 | pat_len += suf_len; |
245 | slash_cnt++; | |
246 | } | |
f8f72644 | 247 | |
ebaa4296 MM |
248 | if (strpbrk(filter->pattern, "*[?")) { |
249 | filter->flags |= MATCHFLG_WILD; | |
250 | if ((cp = strstr(filter->pattern, "**")) != NULL) { | |
251 | filter->flags |= MATCHFLG_WILD2; | |
170381c0 | 252 | /* If the pattern starts with **, note that. */ |
ebaa4296 MM |
253 | if (cp == filter->pattern) |
254 | filter->flags |= MATCHFLG_WILD2_PREFIX; | |
e5daa273 WD |
255 | /* If the pattern ends with ***, note that. */ |
256 | if (pat_len >= 3 | |
ebaa4296 MM |
257 | && filter->pattern[pat_len-3] == '*' |
258 | && filter->pattern[pat_len-2] == '*' | |
259 | && filter->pattern[pat_len-1] == '*') | |
260 | filter->flags |= MATCHFLG_WILD3_SUFFIX; | |
0f2ac855 | 261 | } |
2bca43f6 | 262 | } |
c627d613 | 263 | |
ebaa4296 | 264 | if (filter->flags & MATCHFLG_PERDIR_MERGE) { |
7842418b | 265 | struct filter_list_struct *lp; |
6dfd07d0 WD |
266 | unsigned int len; |
267 | int i; | |
268 | ||
ebaa4296 | 269 | if ((cp = strrchr(filter->pattern, '/')) != NULL) |
6dfd07d0 WD |
270 | cp++; |
271 | else | |
ebaa4296 | 272 | cp = filter->pattern; |
6dfd07d0 WD |
273 | |
274 | /* If the local merge file was already mentioned, don't | |
275 | * add it again. */ | |
276 | for (i = 0; i < mergelist_cnt; i++) { | |
7842418b | 277 | struct filter_struct *ex = mergelist_parents[i]; |
6dfd07d0 WD |
278 | const char *s = strrchr(ex->pattern, '/'); |
279 | if (s) | |
bf39270e | 280 | s++; |
6dfd07d0 | 281 | else |
bf39270e | 282 | s = ex->pattern; |
6dfd07d0 | 283 | len = strlen(s); |
ebaa4296 | 284 | if (len == pat_len - (cp - filter->pattern) |
6dfd07d0 | 285 | && memcmp(s, cp, len) == 0) { |
ebaa4296 | 286 | free_filter(filter); |
6dfd07d0 WD |
287 | return; |
288 | } | |
289 | } | |
290 | ||
7842418b | 291 | if (!(lp = new_array(struct filter_list_struct, 1))) |
fdc79501 | 292 | out_of_memory("add_rule"); |
daa8d920 | 293 | lp->head = lp->tail = lp->parent_dirscan_head = NULL; |
fdc79501 WD |
294 | if (asprintf(&lp->debug_type, " [per-dir %s]", cp) < 0) |
295 | out_of_memory("add_rule"); | |
ebaa4296 | 296 | filter->u.mergelist = lp; |
6dfd07d0 WD |
297 | |
298 | if (mergelist_cnt == mergelist_size) { | |
299 | mergelist_size += 5; | |
300 | mergelist_parents = realloc_array(mergelist_parents, | |
7842418b | 301 | struct filter_struct *, |
6dfd07d0 WD |
302 | mergelist_size); |
303 | if (!mergelist_parents) | |
fdc79501 | 304 | out_of_memory("add_rule"); |
6dfd07d0 | 305 | } |
7634fc8e | 306 | if (DEBUG_GTE(FILTER, 2)) { |
daa8d920 MM |
307 | rprintf(FINFO, "[%s] activating mergelist #%d%s\n", |
308 | who_am_i(), mergelist_cnt, lp->debug_type); | |
7634fc8e | 309 | } |
ebaa4296 | 310 | mergelist_parents[mergelist_cnt++] = filter; |
4c74d44d | 311 | } else |
ebaa4296 | 312 | filter->u.slash_cnt = slash_cnt; |
c1b29492 | 313 | |
6dfd07d0 | 314 | if (!listp->tail) { |
ebaa4296 MM |
315 | filter->next = listp->head; |
316 | listp->head = listp->tail = filter; | |
6dfd07d0 | 317 | } else { |
ebaa4296 MM |
318 | filter->next = listp->tail->next; |
319 | listp->tail->next = filter; | |
320 | listp->tail = filter; | |
b2aa573b | 321 | } |
2b6b4d53 AT |
322 | } |
323 | ||
7842418b | 324 | static void clear_filter_list(struct filter_list_struct *listp) |
2b6b4d53 | 325 | { |
6dfd07d0 | 326 | if (listp->tail) { |
6dfd07d0 WD |
327 | /* Truncate any inherited items from the local list. */ |
328 | listp->tail->next = NULL; | |
329 | /* Now free everything that is left. */ | |
daa8d920 | 330 | free_filters(listp->head); |
6dfd07d0 WD |
331 | } |
332 | ||
333 | listp->head = listp->tail = NULL; | |
2b6b4d53 | 334 | } |
c627d613 | 335 | |
6dfd07d0 WD |
336 | /* This returns an expanded (absolute) filename for the merge-file name if |
337 | * the name has any slashes in it OR if the parent_dirscan var is True; | |
338 | * otherwise it returns the original merge_file name. If the len_ptr value | |
339 | * is non-NULL the merge_file name is limited by the referenced length | |
340 | * value and will be updated with the length of the resulting name. We | |
341 | * always return a name that is null terminated, even if the merge_file | |
342 | * name was not. */ | |
343 | static char *parse_merge_name(const char *merge_file, unsigned int *len_ptr, | |
344 | unsigned int prefix_skip) | |
5be7fa93 | 345 | { |
6dfd07d0 WD |
346 | static char buf[MAXPATHLEN]; |
347 | char *fn, tmpbuf[MAXPATHLEN]; | |
348 | unsigned int fn_len; | |
349 | ||
350 | if (!parent_dirscan && *merge_file != '/') { | |
351 | /* Return the name unchanged it doesn't have any slashes. */ | |
352 | if (len_ptr) { | |
353 | const char *p = merge_file + *len_ptr; | |
354 | while (--p > merge_file && *p != '/') {} | |
355 | if (p == merge_file) { | |
356 | strlcpy(buf, merge_file, *len_ptr + 1); | |
357 | return buf; | |
358 | } | |
359 | } else if (strchr(merge_file, '/') == NULL) | |
360 | return (char *)merge_file; | |
361 | } | |
5be7fa93 | 362 | |
6dfd07d0 WD |
363 | fn = *merge_file == '/' ? buf : tmpbuf; |
364 | if (sanitize_paths) { | |
365 | const char *r = prefix_skip ? "/" : NULL; | |
366 | /* null-terminate the name if it isn't already */ | |
367 | if (len_ptr && merge_file[*len_ptr]) { | |
368 | char *to = fn == buf ? tmpbuf : buf; | |
369 | strlcpy(to, merge_file, *len_ptr + 1); | |
370 | merge_file = to; | |
371 | } | |
d48810ba | 372 | if (!sanitize_path(fn, merge_file, r, dirbuf_depth, SP_DEFAULT)) { |
6dfd07d0 | 373 | rprintf(FERROR, "merge-file name overflows: %s\n", |
45c49b52 | 374 | merge_file); |
6dfd07d0 WD |
375 | return NULL; |
376 | } | |
798cde47 | 377 | fn_len = strlen(fn); |
6dfd07d0 WD |
378 | } else { |
379 | strlcpy(fn, merge_file, len_ptr ? *len_ptr + 1 : MAXPATHLEN); | |
b58bfb2f | 380 | fn_len = clean_fname(fn, CFN_COLLAPSE_DOT_DOT_DIRS); |
b2aa573b | 381 | } |
0f78b815 | 382 | |
798cde47 WD |
383 | /* If the name isn't in buf yet, it's wasn't absolute. */ |
384 | if (fn != buf) { | |
9793bbb3 WD |
385 | int d_len = dirbuf_len - prefix_skip; |
386 | if (d_len + fn_len >= MAXPATHLEN) { | |
798cde47 WD |
387 | rprintf(FERROR, "merge-file name overflows: %s\n", fn); |
388 | return NULL; | |
389 | } | |
9793bbb3 WD |
390 | memcpy(buf, dirbuf + prefix_skip, d_len); |
391 | memcpy(buf + d_len, fn, fn_len + 1); | |
b58bfb2f | 392 | fn_len = clean_fname(buf, CFN_COLLAPSE_DOT_DOT_DIRS); |
6dfd07d0 | 393 | } |
6dfd07d0 | 394 | |
6dfd07d0 WD |
395 | if (len_ptr) |
396 | *len_ptr = fn_len; | |
397 | return buf; | |
398 | } | |
5be7fa93 | 399 | |
6dfd07d0 | 400 | /* Sets the dirbuf and dirbuf_len values. */ |
7842418b | 401 | void set_filter_dir(const char *dir, unsigned int dirlen) |
6dfd07d0 WD |
402 | { |
403 | unsigned int len; | |
404 | if (*dir != '/') { | |
405 | memcpy(dirbuf, curr_dir, curr_dir_len); | |
406 | dirbuf[curr_dir_len] = '/'; | |
407 | len = curr_dir_len + 1; | |
408 | if (len + dirlen >= MAXPATHLEN) | |
409 | dirlen = 0; | |
410 | } else | |
411 | len = 0; | |
412 | memcpy(dirbuf + len, dir, dirlen); | |
413 | dirbuf[dirlen + len] = '\0'; | |
b58bfb2f | 414 | dirbuf_len = clean_fname(dirbuf, CFN_COLLAPSE_DOT_DOT_DIRS); |
6dfd07d0 WD |
415 | if (dirbuf_len > 1 && dirbuf[dirbuf_len-1] == '.' |
416 | && dirbuf[dirbuf_len-2] == '/') | |
417 | dirbuf_len -= 2; | |
418 | if (dirbuf_len != 1) | |
419 | dirbuf[dirbuf_len++] = '/'; | |
420 | dirbuf[dirbuf_len] = '\0'; | |
421 | if (sanitize_paths) | |
422 | dirbuf_depth = count_dir_elements(dirbuf + module_dirlen); | |
423 | } | |
424 | ||
425 | /* This routine takes a per-dir merge-file entry and finishes its setup. | |
426 | * If the name has a path portion then we check to see if it refers to a | |
427 | * parent directory of the first transfer dir. If it does, we scan all the | |
428 | * dirs from that point through the parent dir of the transfer dir looking | |
429 | * for the per-dir merge-file in each one. */ | |
daa8d920 | 430 | static BOOL setup_merge_file(int mergelist_num, struct filter_struct *ex, |
b6f06b8e | 431 | struct filter_list_struct *lp) |
6dfd07d0 WD |
432 | { |
433 | char buf[MAXPATHLEN]; | |
434 | char *x, *y, *pat = ex->pattern; | |
435 | unsigned int len; | |
436 | ||
437 | if (!(x = parse_merge_name(pat, NULL, 0)) || *x != '/') | |
438 | return 0; | |
439 | ||
7634fc8e | 440 | if (DEBUG_GTE(FILTER, 2)) { |
daa8d920 MM |
441 | rprintf(FINFO, "[%s] performing parent_dirscan for mergelist #%d%s\n", |
442 | who_am_i(), mergelist_num, lp->debug_type); | |
7634fc8e | 443 | } |
6dfd07d0 WD |
444 | y = strrchr(x, '/'); |
445 | *y = '\0'; | |
446 | ex->pattern = strdup(y+1); | |
447 | if (!*x) | |
448 | x = "/"; | |
449 | if (*x == '/') | |
450 | strlcpy(buf, x, MAXPATHLEN); | |
451 | else | |
452 | pathjoin(buf, MAXPATHLEN, dirbuf, x); | |
453 | ||
b58bfb2f | 454 | len = clean_fname(buf, CFN_COLLAPSE_DOT_DOT_DIRS); |
6dfd07d0 WD |
455 | if (len != 1 && len < MAXPATHLEN-1) { |
456 | buf[len++] = '/'; | |
457 | buf[len] = '\0'; | |
458 | } | |
459 | /* This ensures that the specified dir is a parent of the transfer. */ | |
460 | for (x = buf, y = dirbuf; *x && *x == *y; x++, y++) {} | |
461 | if (*x) | |
462 | y += strlen(y); /* nope -- skip the scan */ | |
463 | ||
464 | parent_dirscan = True; | |
465 | while (*y) { | |
466 | char save[MAXPATHLEN]; | |
467 | strlcpy(save, y, MAXPATHLEN); | |
468 | *y = '\0'; | |
469 | dirbuf_len = y - dirbuf; | |
470 | strlcpy(x, ex->pattern, MAXPATHLEN - (x - buf)); | |
ebaa4296 MM |
471 | load_filter_file(lp, buf, ex, XFLG_ANCHORED2ABS); |
472 | if (ex->flags & MATCHFLG_NO_INHERIT) { | |
daa8d920 MM |
473 | /* Free the undesired rules to clean up any per-dir |
474 | * mergelists they defined. Otherwise pop_local_filters | |
475 | * may crash trying to restore nonexistent state for | |
476 | * those mergelists. */ | |
477 | free_filters(lp->head); | |
6dfd07d0 | 478 | lp->head = NULL; |
daa8d920 | 479 | } |
6dfd07d0 WD |
480 | lp->tail = NULL; |
481 | strlcpy(y, save, MAXPATHLEN); | |
482 | while ((*x++ = *y++) != '/') {} | |
483 | } | |
daa8d920 MM |
484 | /* Save current head for freeing when the mergelist becomes inactive. */ |
485 | lp->parent_dirscan_head = lp->head; | |
6dfd07d0 | 486 | parent_dirscan = False; |
7634fc8e | 487 | if (DEBUG_GTE(FILTER, 2)) { |
daa8d920 MM |
488 | rprintf(FINFO, "[%s] completed parent_dirscan for mergelist #%d%s\n", |
489 | who_am_i(), mergelist_num, lp->debug_type); | |
7634fc8e | 490 | } |
6dfd07d0 WD |
491 | free(pat); |
492 | return 1; | |
493 | } | |
494 | ||
daa8d920 MM |
495 | struct local_filter_state { |
496 | int mergelist_cnt; | |
2a147e9f | 497 | struct filter_list_struct mergelists[1]; |
daa8d920 MM |
498 | }; |
499 | ||
6dfd07d0 WD |
500 | /* Each time rsync changes to a new directory it call this function to |
501 | * handle all the per-dir merge-files. The "dir" value is the current path | |
502 | * relative to curr_dir (which might not be null-terminated). We copy it | |
503 | * into dirbuf so that we can easily append a file name on the end. */ | |
7842418b | 504 | void *push_local_filters(const char *dir, unsigned int dirlen) |
6dfd07d0 | 505 | { |
daa8d920 | 506 | struct local_filter_state *push; |
6dfd07d0 WD |
507 | int i; |
508 | ||
7842418b | 509 | set_filter_dir(dir, dirlen); |
7634fc8e | 510 | if (DEBUG_GTE(FILTER, 2)) { |
daa8d920 MM |
511 | rprintf(FINFO, "[%s] pushing local filters for %s\n", |
512 | who_am_i(), dirbuf); | |
7634fc8e | 513 | } |
6dfd07d0 | 514 | |
7634fc8e | 515 | if (!mergelist_cnt) { |
daa8d920 | 516 | /* No old state to save and no new merge files to push. */ |
a2b371cd | 517 | return NULL; |
7634fc8e | 518 | } |
a2b371cd | 519 | |
7634fc8e WD |
520 | push = (struct local_filter_state *)new_array(char, |
521 | sizeof (struct local_filter_state) | |
2a147e9f | 522 | + (mergelist_cnt-1) * sizeof (struct filter_list_struct)); |
6dfd07d0 | 523 | if (!push) |
7842418b | 524 | out_of_memory("push_local_filters"); |
6dfd07d0 | 525 | |
daa8d920 MM |
526 | push->mergelist_cnt = mergelist_cnt; |
527 | for (i = 0; i < mergelist_cnt; i++) { | |
528 | memcpy(&push->mergelists[i], mergelist_parents[i]->u.mergelist, | |
7842418b | 529 | sizeof (struct filter_list_struct)); |
6dfd07d0 WD |
530 | } |
531 | ||
ebaa4296 | 532 | /* Note: load_filter_file() might increase mergelist_cnt, so keep |
6dfd07d0 WD |
533 | * this loop separate from the above loop. */ |
534 | for (i = 0; i < mergelist_cnt; i++) { | |
7842418b WD |
535 | struct filter_struct *ex = mergelist_parents[i]; |
536 | struct filter_list_struct *lp = ex->u.mergelist; | |
6dfd07d0 | 537 | |
951e826b | 538 | if (DEBUG_GTE(FILTER, 2)) { |
daa8d920 MM |
539 | rprintf(FINFO, "[%s] pushing mergelist #%d%s\n", |
540 | who_am_i(), i, lp->debug_type); | |
6dfd07d0 WD |
541 | } |
542 | ||
543 | lp->tail = NULL; /* Switch any local rules to inherited. */ | |
ebaa4296 | 544 | if (ex->flags & MATCHFLG_NO_INHERIT) |
6dfd07d0 | 545 | lp->head = NULL; |
6dfd07d0 | 546 | |
ebaa4296 MM |
547 | if (ex->flags & MATCHFLG_FINISH_SETUP) { |
548 | ex->flags &= ~MATCHFLG_FINISH_SETUP; | |
daa8d920 | 549 | if (setup_merge_file(i, ex, lp)) |
7842418b | 550 | set_filter_dir(dir, dirlen); |
6dfd07d0 WD |
551 | } |
552 | ||
553 | if (strlcpy(dirbuf + dirbuf_len, ex->pattern, | |
b6f06b8e | 554 | MAXPATHLEN - dirbuf_len) < MAXPATHLEN - dirbuf_len) { |
ebaa4296 | 555 | load_filter_file(lp, dirbuf, ex, |
fdc79501 | 556 | XFLG_ANCHORED2ABS); |
b6f06b8e | 557 | } else { |
6dfd07d0 | 558 | io_error |= IOERR_GENERAL; |
342bfb5e | 559 | rprintf(FERROR, |
7842418b | 560 | "cannot add local filter rules in long-named directory: %s\n", |
6dfd07d0 WD |
561 | full_fname(dirbuf)); |
562 | } | |
563 | dirbuf[dirbuf_len] = '\0'; | |
564 | } | |
565 | ||
566 | return (void*)push; | |
567 | } | |
568 | ||
7842418b | 569 | void pop_local_filters(void *mem) |
6dfd07d0 | 570 | { |
daa8d920 | 571 | struct local_filter_state *pop = (struct local_filter_state *)mem; |
6dfd07d0 | 572 | int i; |
daa8d920 MM |
573 | int old_mergelist_cnt = pop ? pop->mergelist_cnt : 0; |
574 | ||
575 | if (DEBUG_GTE(FILTER, 2)) | |
576 | rprintf(FINFO, "[%s] popping local filters\n", who_am_i()); | |
6dfd07d0 WD |
577 | |
578 | for (i = mergelist_cnt; i-- > 0; ) { | |
7842418b WD |
579 | struct filter_struct *ex = mergelist_parents[i]; |
580 | struct filter_list_struct *lp = ex->u.mergelist; | |
6dfd07d0 | 581 | |
951e826b | 582 | if (DEBUG_GTE(FILTER, 2)) { |
daa8d920 MM |
583 | rprintf(FINFO, "[%s] popping mergelist #%d%s\n", |
584 | who_am_i(), i, lp->debug_type); | |
6dfd07d0 WD |
585 | } |
586 | ||
7842418b | 587 | clear_filter_list(lp); |
daa8d920 MM |
588 | |
589 | if (i >= old_mergelist_cnt) { | |
590 | /* This mergelist does not exist in the state to be | |
591 | * restored. Free its parent_dirscan list to clean up | |
592 | * any per-dir mergelists defined there so we don't | |
593 | * crash trying to restore nonexistent state for them | |
594 | * below. (Counterpart to setup_merge_file call in | |
595 | * push_local_filters. Must be done here, not in | |
596 | * free_filter, for LIFO order.) */ | |
7634fc8e | 597 | if (DEBUG_GTE(FILTER, 2)) { |
daa8d920 MM |
598 | rprintf(FINFO, "[%s] freeing parent_dirscan filters of mergelist #%d%s\n", |
599 | who_am_i(), i, ex->u.mergelist->debug_type); | |
7634fc8e | 600 | } |
daa8d920 MM |
601 | free_filters(lp->parent_dirscan_head); |
602 | lp->parent_dirscan_head = NULL; | |
603 | } | |
6dfd07d0 WD |
604 | } |
605 | ||
daa8d920 MM |
606 | /* If we cleaned things up properly, the only still-active mergelists |
607 | * should be those with a state to be restored. */ | |
608 | assert(mergelist_cnt == old_mergelist_cnt); | |
609 | ||
7634fc8e | 610 | if (!pop) { |
daa8d920 | 611 | /* No state to restore. */ |
a2b371cd | 612 | return; |
7634fc8e | 613 | } |
a2b371cd | 614 | |
daa8d920 MM |
615 | for (i = 0; i < mergelist_cnt; i++) { |
616 | memcpy(mergelist_parents[i]->u.mergelist, &pop->mergelists[i], | |
7842418b | 617 | sizeof (struct filter_list_struct)); |
6dfd07d0 WD |
618 | } |
619 | ||
620 | free(pop); | |
5be7fa93 WD |
621 | } |
622 | ||
fe04532a WD |
623 | void change_local_filter_dir(const char *dname, int dlen, int dir_depth) |
624 | { | |
7fa60281 | 625 | static int cur_depth = -1; |
fe04532a WD |
626 | static void *filt_array[MAXPATHLEN/2+1]; |
627 | ||
628 | if (!dname) { | |
7fa60281 WD |
629 | for ( ; cur_depth >= 0; cur_depth--) { |
630 | if (filt_array[cur_depth]) { | |
631 | pop_local_filters(filt_array[cur_depth]); | |
632 | filt_array[cur_depth] = NULL; | |
633 | } | |
634 | } | |
fe04532a WD |
635 | return; |
636 | } | |
637 | ||
638 | assert(dir_depth < MAXPATHLEN/2+1); | |
639 | ||
7fa60281 WD |
640 | for ( ; cur_depth >= dir_depth; cur_depth--) { |
641 | if (filt_array[cur_depth]) { | |
642 | pop_local_filters(filt_array[cur_depth]); | |
643 | filt_array[cur_depth] = NULL; | |
644 | } | |
645 | } | |
fe04532a | 646 | |
7fa60281 | 647 | cur_depth = dir_depth; |
fe04532a WD |
648 | filt_array[cur_depth] = push_local_filters(dname, dlen); |
649 | } | |
650 | ||
ffe8feb2 | 651 | static int rule_matches(const char *fname, struct filter_struct *ex, int name_is_dir) |
2b6b4d53 | 652 | { |
e5daa273 | 653 | int slash_handling, str_cnt = 0, anchored_match = 0; |
ebaa4296 | 654 | int ret_match = ex->flags & MATCHFLG_NEGATE ? 0 : 1; |
e5daa273 WD |
655 | char *p, *pattern = ex->pattern; |
656 | const char *strings[16]; /* more than enough */ | |
ffe8feb2 | 657 | const char *name = fname + (*fname == '/'); |
2b6b4d53 | 658 | |
9f186578 WD |
659 | if (!*name) |
660 | return 0; | |
661 | ||
ebaa4296 | 662 | if (!ex->u.slash_cnt && !(ex->flags & MATCHFLG_WILD2)) { |
770de899 WD |
663 | /* If the pattern does not have any slashes AND it does |
664 | * not have a "**" (which could match a slash), then we | |
665 | * just match the name portion of the path. */ | |
5be7fa93 WD |
666 | if ((p = strrchr(name,'/')) != NULL) |
667 | name = p+1; | |
ebaa4296 | 668 | } else if (ex->flags & MATCHFLG_ABS_PATH && *fname != '/' |
6dfd07d0 | 669 | && curr_dir_len > module_dirlen + 1) { |
770de899 WD |
670 | /* If we're matching against an absolute-path pattern, |
671 | * we need to prepend our full path info. */ | |
e5daa273 WD |
672 | strings[str_cnt++] = curr_dir + module_dirlen + 1; |
673 | strings[str_cnt++] = "/"; | |
ebaa4296 | 674 | } else if (ex->flags & MATCHFLG_WILD2_PREFIX && *fname != '/') { |
e5daa273 WD |
675 | /* Allow "**"+"/" to match at the start of the string. */ |
676 | strings[str_cnt++] = "/"; | |
5be7fa93 | 677 | } |
e5daa273 WD |
678 | strings[str_cnt++] = name; |
679 | if (name_is_dir) { | |
680 | /* Allow a trailing "/"+"***" to match the directory. */ | |
ebaa4296 | 681 | if (ex->flags & MATCHFLG_WILD3_SUFFIX) |
e5daa273 | 682 | strings[str_cnt++] = "/"; |
ebaa4296 | 683 | } else if (ex->flags & MATCHFLG_DIRECTORY) |
f2ae9e85 | 684 | return !ret_match; |
e5daa273 | 685 | strings[str_cnt] = NULL; |
2b6b4d53 | 686 | |
170381c0 | 687 | if (*pattern == '/') { |
770de899 | 688 | anchored_match = 1; |
2b6b4d53 AT |
689 | pattern++; |
690 | } | |
691 | ||
e5daa273 | 692 | if (!anchored_match && ex->u.slash_cnt |
ebaa4296 | 693 | && !(ex->flags & MATCHFLG_WILD2)) { |
170381c0 WD |
694 | /* A non-anchored match with an infix slash and no "**" |
695 | * needs to match the last slash_cnt+1 name elements. */ | |
e5daa273 | 696 | slash_handling = ex->u.slash_cnt + 1; |
ebaa4296 MM |
697 | } else if (!anchored_match && !(ex->flags & MATCHFLG_WILD2_PREFIX) |
698 | && ex->flags & MATCHFLG_WILD2) { | |
e5daa273 WD |
699 | /* A non-anchored match with an infix or trailing "**" (but not |
700 | * a prefixed "**") needs to try matching after every slash. */ | |
701 | slash_handling = -1; | |
702 | } else { | |
703 | /* The pattern matches only at the start of the path or name. */ | |
704 | slash_handling = 0; | |
705 | } | |
706 | ||
ebaa4296 | 707 | if (ex->flags & MATCHFLG_WILD) { |
e5daa273 WD |
708 | if (wildmatch_array(pattern, strings, slash_handling)) |
709 | return ret_match; | |
710 | } else if (str_cnt > 1) { | |
711 | if (litmatch_array(pattern, strings, slash_handling)) | |
f2ae9e85 | 712 | return ret_match; |
770de899 | 713 | } else if (anchored_match) { |
48ecccce | 714 | if (strcmp(name, pattern) == 0) |
f2ae9e85 | 715 | return ret_match; |
2b6b4d53 AT |
716 | } else { |
717 | int l1 = strlen(name); | |
ea2111d1 | 718 | int l2 = strlen(pattern); |
0f2ac855 | 719 | if (l2 <= l1 && |
ea2111d1 | 720 | strcmp(name+(l1-l2),pattern) == 0 && |
170381c0 | 721 | (l1==l2 || name[l1-(l2+1)] == '/')) { |
f2ae9e85 | 722 | return ret_match; |
c36cd317 | 723 | } |
2b6b4d53 AT |
724 | } |
725 | ||
f2ae9e85 | 726 | return !ret_match; |
c627d613 AT |
727 | } |
728 | ||
729 | ||
1df02d13 | 730 | static void report_filter_result(enum logcode code, char const *name, |
7634fc8e WD |
731 | struct filter_struct const *ent, |
732 | int name_is_dir, const char *type) | |
d567322f | 733 | { |
0f2ac855 | 734 | /* If a trailing slash is present to match only directories, |
fdc79501 | 735 | * then it is stripped out by add_rule(). So as a special |
0f2ac855 WD |
736 | * case we add it back in here. */ |
737 | ||
951e826b | 738 | if (DEBUG_GTE(FILTER, 1)) { |
d5782b52 WD |
739 | static char *actions[2][2] |
740 | = { {"show", "hid"}, {"risk", "protect"} }; | |
741 | const char *w = who_am_i(); | |
1df02d13 | 742 | rprintf(code, "[%s] %sing %s %s because of pattern %s%s%s\n", |
ebaa4296 | 743 | w, actions[*w!='s'][!(ent->flags&MATCHFLG_INCLUDE)], |
d5782b52 | 744 | name_is_dir ? "directory" : "file", name, ent->pattern, |
ebaa4296 | 745 | ent->flags & MATCHFLG_DIRECTORY ? "/" : "", type); |
ea847c62 | 746 | } |
d567322f MP |
747 | } |
748 | ||
749 | ||
750 | /* | |
a6536635 WD |
751 | * Return -1 if file "name" is defined to be excluded by the specified |
752 | * exclude list, 1 if it is included, and 0 if it was not matched. | |
6694ead8 | 753 | * Sets last_hit_filter to the filter that was hit, or NULL if none. |
d567322f | 754 | */ |
1df02d13 WD |
755 | int check_filter(struct filter_list_struct *listp, enum logcode code, |
756 | const char *name, int name_is_dir) | |
c627d613 | 757 | { |
7842418b | 758 | struct filter_struct *ent; |
c627d613 | 759 | |
b2aa573b | 760 | for (ent = listp->head; ent; ent = ent->next) { |
ebaa4296 | 761 | if (ignore_perishable && ent->flags & MATCHFLG_PERISHABLE) |
ccdb23bb | 762 | continue; |
ebaa4296 | 763 | if (ent->flags & MATCHFLG_PERDIR_MERGE) { |
1df02d13 | 764 | int rc = check_filter(ent->u.mergelist, code, name, |
7842418b | 765 | name_is_dir); |
6dfd07d0 WD |
766 | if (rc) |
767 | return rc; | |
768 | continue; | |
769 | } | |
ebaa4296 | 770 | if (ent->flags & MATCHFLG_CVS_IGNORE) { |
1df02d13 | 771 | int rc = check_filter(&cvs_filter_list, code, name, |
fdc79501 WD |
772 | name_is_dir); |
773 | if (rc) | |
774 | return rc; | |
775 | continue; | |
776 | } | |
7842418b | 777 | if (rule_matches(name, ent, name_is_dir)) { |
1df02d13 WD |
778 | report_filter_result(code, name, ent, name_is_dir, |
779 | listp->debug_type); | |
6694ead8 | 780 | last_hit_filter = ent; |
ebaa4296 | 781 | return ent->flags & MATCHFLG_INCLUDE ? 1 : -1; |
0f2ac855 | 782 | } |
2b6b4d53 | 783 | } |
c627d613 | 784 | |
6694ead8 | 785 | last_hit_filter = NULL; |
2b6b4d53 | 786 | return 0; |
c627d613 AT |
787 | } |
788 | ||
345e0988 | 789 | #define RULE_STRCMP(s,r) rule_strcmp((s), (r), sizeof (r) - 1) |
a1ac8edd | 790 | |
4adbb5f2 | 791 | static const uchar *rule_strcmp(const uchar *str, const char *rule, int rule_len) |
a1ac8edd | 792 | { |
4adbb5f2 | 793 | if (strncmp((char*)str, rule, rule_len) != 0) |
a1ac8edd WD |
794 | return NULL; |
795 | if (isspace(str[rule_len]) || str[rule_len] == '_' || !str[rule_len]) | |
796 | return str + rule_len - 1; | |
797 | if (str[rule_len] == ',') | |
798 | return str + rule_len; | |
799 | return NULL; | |
800 | } | |
c627d613 | 801 | |
6694ead8 MM |
802 | static char *grab_paren_value(const uchar **s_ptr) |
803 | { | |
804 | const uchar *start, *end; | |
805 | int val_sz; | |
806 | char *val; | |
807 | ||
808 | if ((*s_ptr)[1] != '(') | |
809 | return NULL; | |
810 | start = (*s_ptr) + 2; | |
811 | ||
812 | for (end = start; *end != ')'; end++) | |
813 | if (!*end || *end == ' ' || *end == '_') | |
814 | return NULL; | |
815 | ||
816 | val_sz = end - start + 1; | |
817 | val = new_array(char, val_sz); | |
818 | strlcpy(val, (const char *)start, val_sz); | |
819 | *s_ptr = end; /* remember ++s in parse_rule_tok */ | |
820 | return val; | |
821 | } | |
822 | ||
823 | static struct filter_chmod_struct *make_chmod_struct(char *modestr) | |
824 | { | |
825 | struct filter_chmod_struct *chmod; | |
826 | struct chmod_mode_struct *modes = NULL; | |
827 | ||
828 | if (!parse_chmod(modestr, &modes)) | |
829 | return NULL; | |
830 | ||
831 | if (!(chmod = new(struct filter_chmod_struct))) | |
832 | out_of_memory("make_chmod_struct"); | |
833 | chmod->ref_cnt = 1; | |
834 | chmod->modestr = modestr; | |
835 | chmod->modes = modes; | |
836 | return chmod; | |
837 | } | |
838 | ||
ebaa4296 MM |
839 | #define MATCHFLGS_FROM_CONTAINER (MATCHFLG_ABS_PATH | MATCHFLG_INCLUDE \ |
840 | | MATCHFLG_DIRECTORY | MATCHFLG_NEGATE \ | |
6694ead8 | 841 | | MATCHFLG_PERISHABLE | MATCHFLGS_ATTRS) |
ebaa4296 MM |
842 | |
843 | /* Gets the next include/exclude rule from *rulestr_ptr and advances | |
844 | * *rulestr_ptr to point beyond it. Stores the pattern's start (within | |
845 | * *rulestr_ptr) and length in *pat_ptr and *pat_len_ptr, and returns a newly | |
846 | * allocated filter_struct containing the rest of the information. Returns | |
847 | * NULL if there are no more rules in the input. | |
848 | * | |
849 | * The template provides defaults for the new rule to inherit, and the | |
850 | * template mflags and the xflags additionally affect parsing. */ | |
851 | static struct filter_struct *parse_rule_tok(const char **rulestr_ptr, | |
852 | struct filter_struct *template, int xflags, | |
853 | const char **pat_ptr, unsigned int *pat_len_ptr) | |
f8f72644 | 854 | { |
ebaa4296 | 855 | const uchar *s = (const uchar *)*rulestr_ptr; |
6694ead8 | 856 | char *val; |
ebaa4296 | 857 | struct filter_struct *filter; |
fdc79501 | 858 | unsigned int len; |
f8f72644 | 859 | |
ebaa4296 | 860 | if (template->flags & MATCHFLG_WORD_SPLIT) { |
96d3590a WD |
861 | /* Skip over any initial whitespace. */ |
862 | while (isspace(*s)) | |
f8f72644 | 863 | s++; |
6dfd07d0 | 864 | /* Update to point to real start of rule. */ |
ebaa4296 | 865 | *rulestr_ptr = (const char *)s; |
f8f72644 | 866 | } |
6dfd07d0 WD |
867 | if (!*s) |
868 | return NULL; | |
869 | ||
ebaa4296 MM |
870 | if (!(filter = new0(struct filter_struct))) |
871 | out_of_memory("parse_rule_tok"); | |
872 | ||
873 | /* Inherit from the template. Don't inherit MATCHFLGS_SIDES; we check | |
874 | * that later. */ | |
875 | filter->flags = template->flags & MATCHFLGS_FROM_CONTAINER; | |
6694ead8 MM |
876 | if (template->flags & MATCHFLG_CHMOD) |
877 | filter->chmod = ref_filter_chmod(template->chmod); | |
878 | if (template->flags & MATCHFLG_FORCE_OWNER) | |
879 | filter->force_uid = template->force_uid; | |
880 | if (template->flags & MATCHFLG_FORCE_GROUP) | |
881 | filter->force_gid = template->force_gid; | |
b6f06b8e WD |
882 | |
883 | /* Figure out what kind of a filter rule "s" is pointing at. Note | |
884 | * that if MATCHFLG_NO_PREFIXES is set, the rule is either an include | |
885 | * or an exclude based on the inheritance of the MATCHFLG_INCLUDE | |
886 | * flag (above). XFLG_OLD_PREFIXES indicates a compatibility mode | |
887 | * for old include/exclude patterns where just "+ " and "- " are | |
888 | * allowed as optional prefixes. */ | |
ebaa4296 MM |
889 | if (template->flags & MATCHFLG_NO_PREFIXES) { |
890 | if (*s == '!' && template->flags & MATCHFLG_CVS_IGNORE) | |
891 | filter->flags |= MATCHFLG_CLEAR_LIST; /* Tentative! */ | |
b6f06b8e WD |
892 | } else if (xflags & XFLG_OLD_PREFIXES) { |
893 | if (*s == '-' && s[1] == ' ') { | |
ebaa4296 | 894 | filter->flags &= ~MATCHFLG_INCLUDE; |
b6f06b8e WD |
895 | s += 2; |
896 | } else if (*s == '+' && s[1] == ' ') { | |
ebaa4296 | 897 | filter->flags |= MATCHFLG_INCLUDE; |
b6f06b8e | 898 | s += 2; |
664cf327 | 899 | } else if (*s == '!') |
ebaa4296 | 900 | filter->flags |= MATCHFLG_CLEAR_LIST; /* Tentative! */ |
b6f06b8e | 901 | } else { |
ebaa4296 MM |
902 | char ch = 0; |
903 | BOOL prefix_specifies_side = False; | |
6dfd07d0 | 904 | switch (*s) { |
a1ac8edd | 905 | case 'c': |
345e0988 | 906 | if ((s = RULE_STRCMP(s, "clear")) != NULL) |
a1ac8edd WD |
907 | ch = '!'; |
908 | break; | |
909 | case 'd': | |
345e0988 | 910 | if ((s = RULE_STRCMP(s, "dir-merge")) != NULL) |
a1ac8edd WD |
911 | ch = ':'; |
912 | break; | |
913 | case 'e': | |
345e0988 | 914 | if ((s = RULE_STRCMP(s, "exclude")) != NULL) |
a1ac8edd WD |
915 | ch = '-'; |
916 | break; | |
ed243f8c WD |
917 | case 'h': |
918 | if ((s = RULE_STRCMP(s, "hide")) != NULL) | |
919 | ch = 'H'; | |
920 | break; | |
a1ac8edd | 921 | case 'i': |
345e0988 | 922 | if ((s = RULE_STRCMP(s, "include")) != NULL) |
a1ac8edd WD |
923 | ch = '+'; |
924 | break; | |
925 | case 'm': | |
345e0988 | 926 | if ((s = RULE_STRCMP(s, "merge")) != NULL) |
a1ac8edd WD |
927 | ch = '.'; |
928 | break; | |
ed243f8c WD |
929 | case 'p': |
930 | if ((s = RULE_STRCMP(s, "protect")) != NULL) | |
931 | ch = 'P'; | |
932 | break; | |
933 | case 'r': | |
934 | if ((s = RULE_STRCMP(s, "risk")) != NULL) | |
935 | ch = 'R'; | |
936 | break; | |
937 | case 's': | |
938 | if ((s = RULE_STRCMP(s, "show")) != NULL) | |
939 | ch = 'S'; | |
940 | break; | |
a1ac8edd WD |
941 | default: |
942 | ch = *s; | |
64b761c1 WD |
943 | if (s[1] == ',') |
944 | s++; | |
a1ac8edd WD |
945 | break; |
946 | } | |
947 | switch (ch) { | |
6dfd07d0 | 948 | case ':': |
ebaa4296 MM |
949 | filter->flags |= MATCHFLG_PERDIR_MERGE |
950 | | MATCHFLG_FINISH_SETUP; | |
6dfd07d0 WD |
951 | /* FALL THROUGH */ |
952 | case '.': | |
ebaa4296 | 953 | filter->flags |= MATCHFLG_MERGE_FILE; |
6dfd07d0 WD |
954 | break; |
955 | case '+': | |
ebaa4296 | 956 | filter->flags |= MATCHFLG_INCLUDE; |
bf39270e | 957 | /* FALL THROUGH */ |
6dfd07d0 WD |
958 | case '-': |
959 | break; | |
ed243f8c | 960 | case 'S': |
ebaa4296 | 961 | filter->flags |= MATCHFLG_INCLUDE; |
ed243f8c WD |
962 | /* FALL THROUGH */ |
963 | case 'H': | |
ebaa4296 MM |
964 | filter->flags |= MATCHFLG_SENDER_SIDE; |
965 | prefix_specifies_side = True; | |
ed243f8c WD |
966 | break; |
967 | case 'R': | |
ebaa4296 | 968 | filter->flags |= MATCHFLG_INCLUDE; |
ed243f8c WD |
969 | /* FALL THROUGH */ |
970 | case 'P': | |
ebaa4296 MM |
971 | filter->flags |= MATCHFLG_RECEIVER_SIDE; |
972 | prefix_specifies_side = True; | |
ed243f8c | 973 | break; |
6dfd07d0 | 974 | case '!': |
ebaa4296 | 975 | filter->flags |= MATCHFLG_CLEAR_LIST; |
6dfd07d0 WD |
976 | break; |
977 | default: | |
ebaa4296 | 978 | rprintf(FERROR, "Unknown filter rule: `%s'\n", *rulestr_ptr); |
6dfd07d0 WD |
979 | exit_cleanup(RERR_SYNTAX); |
980 | } | |
ebaa4296 MM |
981 | while (ch != '!' && *++s && *s != ' ' && *s != '_') { |
982 | if (template->flags & MATCHFLG_WORD_SPLIT && isspace(*s)) { | |
983 | s--; | |
984 | break; | |
985 | } | |
986 | switch (*s) { | |
987 | default: | |
b6f06b8e | 988 | invalid: |
6dfd07d0 | 989 | rprintf(FERROR, |
ebaa4296 MM |
990 | "invalid modifier '%c' at position %d in filter rule: %s\n", |
991 | *s, s - (const uchar *)*rulestr_ptr, *rulestr_ptr); | |
6dfd07d0 | 992 | exit_cleanup(RERR_SYNTAX); |
6dfd07d0 | 993 | case '-': |
ebaa4296 MM |
994 | if (!BITS_SETnUNSET(filter->flags, MATCHFLG_MERGE_FILE, MATCHFLG_NO_PREFIXES)) |
995 | goto invalid; | |
996 | filter->flags |= MATCHFLG_NO_PREFIXES; | |
6dfd07d0 WD |
997 | break; |
998 | case '+': | |
ebaa4296 MM |
999 | if (!BITS_SETnUNSET(filter->flags, MATCHFLG_MERGE_FILE, MATCHFLG_NO_PREFIXES)) |
1000 | goto invalid; | |
1001 | filter->flags |= MATCHFLG_NO_PREFIXES | |
1002 | | MATCHFLG_INCLUDE; | |
6dfd07d0 | 1003 | break; |
bf39270e | 1004 | case '/': |
ebaa4296 | 1005 | filter->flags |= MATCHFLG_ABS_PATH; |
bf39270e | 1006 | break; |
f2ae9e85 | 1007 | case '!': |
ebaa4296 MM |
1008 | /* Negation really goes with the pattern, so it |
1009 | * isn't useful as a merge-file default. */ | |
1010 | if (filter->flags & MATCHFLG_MERGE_FILE) | |
1011 | goto invalid; | |
1012 | filter->flags |= MATCHFLG_NEGATE; | |
f2ae9e85 | 1013 | break; |
6dfd07d0 | 1014 | case 'C': |
ebaa4296 MM |
1015 | if (filter->flags & MATCHFLG_NO_PREFIXES || prefix_specifies_side) |
1016 | goto invalid; | |
1017 | filter->flags |= MATCHFLG_NO_PREFIXES | |
1018 | | MATCHFLG_WORD_SPLIT | |
1019 | | MATCHFLG_NO_INHERIT | |
1020 | | MATCHFLG_CVS_IGNORE; | |
6dfd07d0 WD |
1021 | break; |
1022 | case 'e': | |
ebaa4296 MM |
1023 | if (!(filter->flags & MATCHFLG_MERGE_FILE)) |
1024 | goto invalid; | |
1025 | filter->flags |= MATCHFLG_EXCLUDE_SELF; | |
6dfd07d0 | 1026 | break; |
6694ead8 MM |
1027 | case 'g': { |
1028 | gid_t gid; | |
1029 | ||
1030 | if (!(val = grab_paren_value(&s))) | |
1031 | goto invalid; | |
1032 | if (name_to_gid(val, &gid, True)) { | |
1033 | filter->flags |= MATCHFLG_FORCE_GROUP; | |
1034 | filter->force_gid = gid; | |
1035 | } else { | |
1036 | rprintf(FERROR, | |
1037 | "unknown group '%s' in filter rule: %s\n", | |
1038 | val, *rulestr_ptr); | |
1039 | exit_cleanup(RERR_SYNTAX); | |
1040 | } | |
1041 | free(val); | |
1042 | break; | |
1043 | } | |
1044 | case 'm': { | |
1045 | struct filter_chmod_struct *chmod; | |
1046 | ||
1047 | if (!(val = grab_paren_value(&s))) | |
1048 | goto invalid; | |
1049 | if ((chmod = make_chmod_struct(val))) { | |
1050 | if (filter->flags & MATCHFLG_CHMOD) | |
1051 | unref_filter_chmod(filter->chmod); | |
1052 | filter->flags |= MATCHFLG_CHMOD; | |
1053 | filter->chmod = chmod; | |
1054 | } else { | |
1055 | rprintf(FERROR, | |
1056 | "unparseable chmod string '%s' in filter rule: %s\n", | |
1057 | val, *rulestr_ptr); | |
1058 | exit_cleanup(RERR_SYNTAX); | |
1059 | } | |
1060 | break; | |
1061 | } | |
6dfd07d0 | 1062 | case 'n': |
ebaa4296 MM |
1063 | if (!(filter->flags & MATCHFLG_MERGE_FILE)) |
1064 | goto invalid; | |
1065 | filter->flags |= MATCHFLG_NO_INHERIT; | |
6dfd07d0 | 1066 | break; |
6694ead8 MM |
1067 | case 'o': { |
1068 | uid_t uid; | |
1069 | ||
1070 | if (!(val = grab_paren_value(&s))) | |
1071 | goto invalid; | |
1072 | if (name_to_uid(val, &uid, True)) { | |
1073 | filter->flags |= MATCHFLG_FORCE_OWNER; | |
1074 | filter->force_uid = uid; | |
1075 | } else { | |
1076 | rprintf(FERROR, | |
1077 | "unknown user '%s' in filter rule: %s\n", | |
1078 | val, *rulestr_ptr); | |
1079 | exit_cleanup(RERR_SYNTAX); | |
1080 | } | |
1081 | free(val); | |
1082 | break; | |
1083 | } | |
ccdb23bb | 1084 | case 'p': |
ebaa4296 | 1085 | filter->flags |= MATCHFLG_PERISHABLE; |
ccdb23bb | 1086 | break; |
ed243f8c | 1087 | case 'r': |
ebaa4296 MM |
1088 | if (prefix_specifies_side) |
1089 | goto invalid; | |
1090 | filter->flags |= MATCHFLG_RECEIVER_SIDE; | |
ed243f8c WD |
1091 | break; |
1092 | case 's': | |
ebaa4296 MM |
1093 | if (prefix_specifies_side) |
1094 | goto invalid; | |
1095 | filter->flags |= MATCHFLG_SENDER_SIDE; | |
ed243f8c | 1096 | break; |
0b2901b7 | 1097 | case 'w': |
ebaa4296 MM |
1098 | if (!(filter->flags & MATCHFLG_MERGE_FILE)) |
1099 | goto invalid; | |
1100 | filter->flags |= MATCHFLG_WORD_SPLIT; | |
6dfd07d0 WD |
1101 | break; |
1102 | } | |
1103 | } | |
1104 | if (*s) | |
1105 | s++; | |
6dfd07d0 | 1106 | } |
ebaa4296 MM |
1107 | if (template->flags & MATCHFLGS_SIDES) { |
1108 | if (filter->flags & MATCHFLGS_SIDES) { | |
1109 | /* The filter and template both specify side(s). This | |
1110 | * is dodgy (and won't work correctly if the template is | |
1111 | * a one-sided per-dir merge rule), so reject it. */ | |
1112 | rprintf(FERROR, | |
1113 | "specified-side merge file contains specified-side filter: %s\n", | |
1114 | *rulestr_ptr); | |
1115 | exit_cleanup(RERR_SYNTAX); | |
1116 | } | |
1117 | filter->flags |= template->flags & MATCHFLGS_SIDES; | |
1118 | } | |
6dfd07d0 | 1119 | |
ebaa4296 | 1120 | if (template->flags & MATCHFLG_WORD_SPLIT) { |
fdc79501 | 1121 | const uchar *cp = s; |
96d3590a WD |
1122 | /* Token ends at whitespace or the end of the string. */ |
1123 | while (!isspace(*cp) && *cp != '\0') | |
1124 | cp++; | |
1125 | len = cp - s; | |
1126 | } else | |
0eeb1cf8 | 1127 | len = strlen((char*)s); |
96d3590a | 1128 | |
ebaa4296 MM |
1129 | if (filter->flags & MATCHFLG_CLEAR_LIST) { |
1130 | if (!(filter->flags & MATCHFLG_NO_PREFIXES) | |
35a388b1 | 1131 | && !(xflags & XFLG_OLD_PREFIXES) && len) { |
6dfd07d0 | 1132 | rprintf(FERROR, |
ebaa4296 | 1133 | "'!' rule has trailing characters: %s\n", *rulestr_ptr); |
6dfd07d0 WD |
1134 | exit_cleanup(RERR_SYNTAX); |
1135 | } | |
1136 | if (len > 1) | |
ebaa4296 MM |
1137 | filter->flags &= ~MATCHFLG_CLEAR_LIST; |
1138 | } else if (!len && !(filter->flags & MATCHFLG_CVS_IGNORE)) { | |
1139 | rprintf(FERROR, "unexpected end of filter rule: %s\n", *rulestr_ptr); | |
6dfd07d0 WD |
1140 | exit_cleanup(RERR_SYNTAX); |
1141 | } | |
1142 | ||
f1678790 | 1143 | /* --delete-excluded turns an un-modified include/exclude into a |
48ecccce WD |
1144 | * sender-side rule. We also affect per-dir merge files that take |
1145 | * no prefixes as a simple optimization. */ | |
f1678790 | 1146 | if (delete_excluded |
ebaa4296 MM |
1147 | && !(filter->flags & MATCHFLGS_SIDES) |
1148 | && (!(filter->flags & MATCHFLG_PERDIR_MERGE) | |
1149 | || filter->flags & MATCHFLG_NO_PREFIXES)) | |
1150 | filter->flags |= MATCHFLG_SENDER_SIDE; | |
1151 | ||
1152 | *pat_ptr = (const char *)s; | |
1153 | *pat_len_ptr = len; | |
1154 | *rulestr_ptr = *pat_ptr + len; | |
1155 | return filter; | |
f8f72644 WD |
1156 | } |
1157 | ||
1158 | ||
0f78b815 | 1159 | static char default_cvsignore[] = |
46db1850 WD |
1160 | /* These default ignored items come from the CVS manual. */ |
1161 | "RCS SCCS CVS CVS.adm RCSLOG cvslog.* tags TAGS" | |
1162 | " .make.state .nse_depinfo *~ #* .#* ,* _$* *$" | |
1163 | " *.old *.bak *.BAK *.orig *.rej .del-*" | |
1164 | " *.a *.olb *.o *.obj *.so *.exe" | |
1165 | " *.Z *.elc *.ln core" | |
1166 | /* The rest we added to suit ourself. */ | |
0485b451 | 1167 | " .svn/ .git/ .bzr/"; |
46db1850 | 1168 | |
ebaa4296 | 1169 | static void get_cvs_excludes(struct filter_struct *template) |
46db1850 | 1170 | { |
46db1850 | 1171 | static int initialized = 0; |
c2f0e4d9 | 1172 | char *p, fname[MAXPATHLEN]; |
ebaa4296 | 1173 | uint32 save_mflags; |
46db1850 WD |
1174 | |
1175 | if (initialized) | |
1176 | return; | |
1177 | initialized = 1; | |
1178 | ||
ebaa4296 MM |
1179 | save_mflags = template->flags; |
1180 | if (protocol_version >= 30) | |
1181 | template->flags |= MATCHFLG_PERISHABLE; | |
1182 | load_filter_str(&cvs_filter_list, default_cvsignore, template, 0); | |
1183 | template->flags = save_mflags; | |
46db1850 WD |
1184 | |
1185 | p = module_id >= 0 && lp_use_chroot(module_id) ? "/" : getenv("HOME"); | |
1186 | if (p && pathjoin(fname, MAXPATHLEN, p, ".cvsignore") < MAXPATHLEN) | |
ebaa4296 MM |
1187 | load_filter_file(&cvs_filter_list, fname, template, 0); |
1188 | ||
1189 | load_filter_str(&cvs_filter_list, getenv("CVSIGNORE"), template, 0); | |
1190 | } | |
1191 | ||
46db1850 | 1192 | |
ebaa4296 MM |
1193 | struct filter_struct *filter_template(uint32 mflags) |
1194 | { | |
1195 | static struct filter_struct template; /* zero-initialized */ | |
1196 | template.flags = mflags; | |
1197 | return &template; | |
46db1850 WD |
1198 | } |
1199 | ||
1200 | ||
ebaa4296 MM |
1201 | void load_filter_str(struct filter_list_struct *listp, const char *rulestr, |
1202 | struct filter_struct *template, int xflags) | |
c627d613 | 1203 | { |
ebaa4296 MM |
1204 | struct filter_struct *filter; |
1205 | const char *pat; | |
fdc79501 | 1206 | unsigned int pat_len; |
5be7fa93 | 1207 | |
ebaa4296 | 1208 | if (!rulestr) |
5e7dbaca | 1209 | return; |
f8f72644 | 1210 | |
b2aa573b | 1211 | while (1) { |
6dfd07d0 | 1212 | /* Remember that the returned string is NOT '\0' terminated! */ |
ebaa4296 MM |
1213 | filter = parse_rule_tok(&rulestr, template, xflags, |
1214 | &pat, &pat_len); | |
1215 | if (!filter) | |
b2aa573b | 1216 | break; |
969f7ed5 | 1217 | |
6dfd07d0 | 1218 | if (pat_len >= MAXPATHLEN) { |
969f7ed5 | 1219 | rprintf(FERROR, "discarding over-long filter: %.*s\n", |
ebaa4296 MM |
1220 | (int)pat_len, pat); |
1221 | free_continue: | |
1222 | free_filter(filter); | |
6dfd07d0 WD |
1223 | continue; |
1224 | } | |
5e972dcf | 1225 | |
ebaa4296 | 1226 | if (filter->flags & MATCHFLG_CLEAR_LIST) { |
951e826b | 1227 | if (DEBUG_GTE(FILTER, 2)) { |
de91e757 | 1228 | rprintf(FINFO, |
7842418b | 1229 | "[%s] clearing filter list%s\n", |
de91e757 WD |
1230 | who_am_i(), listp->debug_type); |
1231 | } | |
7842418b | 1232 | clear_filter_list(listp); |
ebaa4296 | 1233 | goto free_continue; |
5e972dcf | 1234 | } |
b2aa573b | 1235 | |
ebaa4296 | 1236 | if (filter->flags & MATCHFLG_MERGE_FILE) { |
fdc79501 | 1237 | if (!pat_len) { |
ebaa4296 | 1238 | pat = ".cvsignore"; |
fdc79501 WD |
1239 | pat_len = 10; |
1240 | } | |
ebaa4296 MM |
1241 | if (filter->flags & MATCHFLG_EXCLUDE_SELF) { |
1242 | const char *name; | |
1243 | struct filter_struct *excl_self; | |
1244 | ||
1245 | if (!(excl_self = new0(struct filter_struct))) | |
1246 | out_of_memory("load_filter_str"); | |
1247 | /* Find the beginning of the basename. */ | |
1248 | for (name = pat + pat_len; name > pat && name[-1] != '/'; name--) | |
1249 | ; | |
1250 | add_rule(listp, name, (pat + pat_len) - name, excl_self, 0); | |
1251 | filter->flags &= ~MATCHFLG_EXCLUDE_SELF; | |
6dfd07d0 | 1252 | } |
ebaa4296 | 1253 | if (filter->flags & MATCHFLG_PERDIR_MERGE) { |
6dfd07d0 | 1254 | if (parent_dirscan) { |
ebaa4296 MM |
1255 | const char *p; |
1256 | unsigned int len = pat_len; | |
1257 | if ((p = parse_merge_name(pat, &len, | |
b6f06b8e | 1258 | module_dirlen))) |
ebaa4296 MM |
1259 | add_rule(listp, p, len, filter, 0); |
1260 | else | |
1261 | free_filter(filter); | |
6dfd07d0 WD |
1262 | continue; |
1263 | } | |
1264 | } else { | |
ebaa4296 MM |
1265 | const char *p; |
1266 | unsigned int len = pat_len; | |
1267 | if ((p = parse_merge_name(pat, &len, 0))) { | |
1268 | load_filter_file(listp, p, filter, | |
1269 | XFLG_FATAL_ERRORS); | |
1270 | } | |
1271 | free_filter(filter); | |
6dfd07d0 WD |
1272 | continue; |
1273 | } | |
f8f72644 | 1274 | } |
6dfd07d0 | 1275 | |
ebaa4296 MM |
1276 | if (filter->flags & MATCHFLG_CVS_IGNORE |
1277 | && !(filter->flags & MATCHFLG_MERGE_FILE)) | |
1278 | get_cvs_excludes(filter); | |
fdc79501 | 1279 | |
ebaa4296 | 1280 | add_rule(listp, pat, pat_len, filter, xflags); |
8c35542d | 1281 | } |
c627d613 AT |
1282 | } |
1283 | ||
c627d613 | 1284 | |
ebaa4296 MM |
1285 | void load_filter_file(struct filter_list_struct *listp, const char *fname, |
1286 | struct filter_struct *template, int xflags) | |
c627d613 | 1287 | { |
5e7dbaca | 1288 | FILE *fp; |
3fac51e2 | 1289 | char line[BIGPATHBUFLEN]; |
7cd72c79 | 1290 | char *eob = line + sizeof line - 1; |
ebaa4296 | 1291 | BOOL word_split = (template->flags & MATCHFLG_WORD_SPLIT) != 0; |
ccdff3eb | 1292 | |
5be7fa93 WD |
1293 | if (!fname || !*fname) |
1294 | return; | |
1295 | ||
5a016db9 | 1296 | if (*fname != '-' || fname[1] || am_server) { |
819bfe45 | 1297 | if (daemon_filter_list.head) { |
5a016db9 | 1298 | strlcpy(line, fname, sizeof line); |
b58bfb2f | 1299 | clean_fname(line, CFN_COLLAPSE_DOT_DOT_DIRS); |
1df02d13 | 1300 | if (check_filter(&daemon_filter_list, FLOG, line, 0) < 0) |
5a016db9 WD |
1301 | fp = NULL; |
1302 | else | |
1303 | fp = fopen(line, "rb"); | |
1304 | } else | |
1305 | fp = fopen(fname, "rb"); | |
1306 | } else | |
5e7dbaca | 1307 | fp = stdin; |
bf39270e | 1308 | |
951e826b | 1309 | if (DEBUG_GTE(FILTER, 2)) { |
ebaa4296 MM |
1310 | rprintf(FINFO, "[%s] load_filter_file(%s,%x,%x)%s\n", |
1311 | who_am_i(), fname, template->flags, xflags, | |
bf39270e WD |
1312 | fp ? "" : " [not found]"); |
1313 | } | |
1314 | ||
5e7dbaca | 1315 | if (!fp) { |
f8f72644 | 1316 | if (xflags & XFLG_FATAL_ERRORS) { |
a039749b | 1317 | rsyserr(FERROR, errno, |
6dfd07d0 | 1318 | "failed to open %sclude file %s", |
ebaa4296 | 1319 | template->flags & MATCHFLG_INCLUDE ? "in" : "ex", |
45c49b52 | 1320 | fname); |
65417579 | 1321 | exit_cleanup(RERR_FILEIO); |
2b6b4d53 | 1322 | } |
5be7fa93 | 1323 | return; |
2b6b4d53 | 1324 | } |
6dfd07d0 WD |
1325 | dirbuf[dirbuf_len] = '\0'; |
1326 | ||
ccdff3eb | 1327 | while (1) { |
5e7dbaca | 1328 | char *s = line; |
619d21ff | 1329 | int ch, overflow = 0; |
ccdff3eb | 1330 | while (1) { |
5e7dbaca | 1331 | if ((ch = getc(fp)) == EOF) { |
61e16468 WD |
1332 | if (ferror(fp) && errno == EINTR) { |
1333 | clearerr(fp); | |
ccdff3eb | 1334 | continue; |
61e16468 | 1335 | } |
ccdff3eb WD |
1336 | break; |
1337 | } | |
40d38dc0 WD |
1338 | if (word_split && isspace(ch)) |
1339 | break; | |
ccdff3eb WD |
1340 | if (eol_nulls? !ch : (ch == '\n' || ch == '\r')) |
1341 | break; | |
1342 | if (s < eob) | |
1343 | *s++ = ch; | |
619d21ff WD |
1344 | else |
1345 | overflow = 1; | |
1346 | } | |
1347 | if (overflow) { | |
7842418b | 1348 | rprintf(FERROR, "discarding over-long filter: %s...\n", line); |
619d21ff | 1349 | s = line; |
ccdff3eb WD |
1350 | } |
1351 | *s = '\0'; | |
7f0feb4d WD |
1352 | /* Skip an empty token and (when line parsing) comments. */ |
1353 | if (*line && (word_split || (*line != ';' && *line != '#'))) | |
ebaa4296 | 1354 | load_filter_str(listp, line, template, xflags); |
5e7dbaca | 1355 | if (ch == EOF) |
ccdff3eb | 1356 | break; |
2b6b4d53 | 1357 | } |
5e7dbaca | 1358 | fclose(fp); |
c627d613 AT |
1359 | } |
1360 | ||
a261103c | 1361 | /* If the "for_xfer" flag is set, the prefix is made compatible with the |
fdc79501 WD |
1362 | * current protocol_version (if possible) or a NULL is returned (if not |
1363 | * possible). */ | |
ebaa4296 | 1364 | char *get_rule_prefix(struct filter_struct *filter, const char *pat, int for_xfer, |
fdc79501 | 1365 | unsigned int *plen_ptr) |
417b5999 WD |
1366 | { |
1367 | static char buf[MAX_RULE_PREFIX+1]; | |
1368 | char *op = buf; | |
2217b30a | 1369 | int legal_len = for_xfer && protocol_version < 29 ? 1 : MAX_RULE_PREFIX-1; |
417b5999 | 1370 | |
ebaa4296 | 1371 | if (filter->flags & MATCHFLG_PERDIR_MERGE) { |
fdc79501 WD |
1372 | if (legal_len == 1) |
1373 | return NULL; | |
417b5999 | 1374 | *op++ = ':'; |
ebaa4296 | 1375 | } else if (filter->flags & MATCHFLG_INCLUDE) |
fdc79501 WD |
1376 | *op++ = '+'; |
1377 | else if (legal_len != 1 | |
1378 | || ((*pat == '-' || *pat == '+') && pat[1] == ' ')) | |
1379 | *op++ = '-'; | |
1380 | else | |
1381 | legal_len = 0; | |
1382 | ||
ebaa4296 MM |
1383 | if (filter->flags & MATCHFLG_ABS_PATH) |
1384 | *op++ = '/'; | |
1385 | if (filter->flags & MATCHFLG_NEGATE) | |
2a28dd32 | 1386 | *op++ = '!'; |
ebaa4296 | 1387 | if (filter->flags & MATCHFLG_CVS_IGNORE) |
fdc79501 WD |
1388 | *op++ = 'C'; |
1389 | else { | |
ebaa4296 | 1390 | if (filter->flags & MATCHFLG_NO_INHERIT) |
417b5999 | 1391 | *op++ = 'n'; |
ebaa4296 | 1392 | if (filter->flags & MATCHFLG_WORD_SPLIT) |
d09e800a | 1393 | *op++ = 'w'; |
ebaa4296 MM |
1394 | if (filter->flags & MATCHFLG_NO_PREFIXES) { |
1395 | if (filter->flags & MATCHFLG_INCLUDE) | |
417b5999 WD |
1396 | *op++ = '+'; |
1397 | else | |
1398 | *op++ = '-'; | |
1399 | } | |
417b5999 | 1400 | } |
ebaa4296 | 1401 | if (filter->flags & MATCHFLG_EXCLUDE_SELF) |
46db1850 | 1402 | *op++ = 'e'; |
ebaa4296 | 1403 | if (filter->flags & MATCHFLG_SENDER_SIDE |
ed243f8c WD |
1404 | && (!for_xfer || protocol_version >= 29)) |
1405 | *op++ = 's'; | |
ebaa4296 | 1406 | if (filter->flags & MATCHFLG_RECEIVER_SIDE |
ed243f8c WD |
1407 | && (!for_xfer || protocol_version >= 29 |
1408 | || (delete_excluded && am_sender))) | |
1409 | *op++ = 'r'; | |
ebaa4296 | 1410 | if (filter->flags & MATCHFLG_PERISHABLE) { |
ccdb23bb WD |
1411 | if (!for_xfer || protocol_version >= 30) |
1412 | *op++ = 'p'; | |
1413 | else if (am_sender) | |
1414 | return NULL; | |
1415 | } | |
6694ead8 MM |
1416 | if (filter->flags & MATCHFLGS_ATTRS) { |
1417 | if (!for_xfer || protocol_version >= 31) { | |
1418 | if (filter->flags & MATCHFLG_CHMOD) | |
1419 | if (!snappendf(&op, (buf + sizeof buf) - op, | |
1420 | "m(%s)", filter->chmod->modestr)) | |
1421 | return NULL; | |
1422 | if (filter->flags & MATCHFLG_FORCE_OWNER) | |
1423 | if (!snappendf(&op, (buf + sizeof buf) - op, | |
1424 | "o(%u)", (unsigned)filter->force_uid)) | |
1425 | return NULL; | |
1426 | if (filter->flags & MATCHFLG_FORCE_GROUP) | |
1427 | if (!snappendf(&op, (buf + sizeof buf) - op, | |
1428 | "g(%u)", (unsigned)filter->force_gid)) | |
1429 | return NULL; | |
1430 | } else if (!am_sender) | |
1431 | return NULL; | |
1432 | } | |
448797a1 WD |
1433 | if (op - buf > legal_len) |
1434 | return NULL; | |
2217b30a WD |
1435 | if (legal_len) |
1436 | *op++ = ' '; | |
417b5999 WD |
1437 | *op = '\0'; |
1438 | if (plen_ptr) | |
1439 | *plen_ptr = op - buf; | |
417b5999 WD |
1440 | return buf; |
1441 | } | |
c627d613 | 1442 | |
fdc79501 | 1443 | static void send_rules(int f_out, struct filter_list_struct *flp) |
c627d613 | 1444 | { |
ed243f8c | 1445 | struct filter_struct *ent, *prev = NULL; |
25cf8893 | 1446 | |
fdc79501 | 1447 | for (ent = flp->head; ent; ent = ent->next) { |
417b5999 | 1448 | unsigned int len, plen, dlen; |
ed243f8c | 1449 | int elide = 0; |
417b5999 | 1450 | char *p; |
2fb139c1 | 1451 | |
f1678790 WD |
1452 | /* Note we need to check delete_excluded here in addition to |
1453 | * the code in parse_rule_tok() because some rules may have | |
48ecccce WD |
1454 | * been added before we found the --delete-excluded option. |
1455 | * We must also elide any CVS merge-file rules to avoid a | |
1456 | * backward compatibility problem, and we elide any no-prefix | |
1457 | * merge files as an optimization (since they can only have | |
1458 | * include/exclude rules). */ | |
ebaa4296 | 1459 | if (ent->flags & MATCHFLG_SENDER_SIDE) |
ed243f8c | 1460 | elide = am_sender ? 1 : -1; |
ebaa4296 | 1461 | if (ent->flags & MATCHFLG_RECEIVER_SIDE) |
ed243f8c | 1462 | elide = elide ? 0 : am_sender ? -1 : 1; |
97bf86f8 | 1463 | else if (delete_excluded && !elide |
ebaa4296 MM |
1464 | && (!(ent->flags & MATCHFLG_PERDIR_MERGE) |
1465 | || ent->flags & MATCHFLG_NO_PREFIXES)) | |
ed243f8c WD |
1466 | elide = am_sender ? 1 : -1; |
1467 | if (elide < 0) { | |
1468 | if (prev) | |
1469 | prev->next = ent->next; | |
1470 | else | |
1471 | flp->head = ent->next; | |
1472 | } else | |
1473 | prev = ent; | |
1474 | if (elide > 0) | |
1475 | continue; | |
ebaa4296 MM |
1476 | if (ent->flags & MATCHFLG_CVS_IGNORE |
1477 | && !(ent->flags & MATCHFLG_MERGE_FILE)) { | |
98e47414 | 1478 | int f = am_sender || protocol_version < 29 ? f_out : -2; |
ed243f8c | 1479 | send_rules(f, &cvs_filter_list); |
98e47414 | 1480 | if (f == f_out) |
417b5999 | 1481 | continue; |
fdc79501 | 1482 | } |
ebaa4296 | 1483 | p = get_rule_prefix(ent, ent->pattern, 1, &plen); |
fdc79501 | 1484 | if (!p) { |
417b5999 | 1485 | rprintf(FERROR, |
fdc79501 | 1486 | "filter rules are too modern for remote rsync.\n"); |
caf8299e | 1487 | exit_cleanup(RERR_PROTOCOL); |
5f5be796 | 1488 | } |
ed243f8c WD |
1489 | if (f_out < 0) |
1490 | continue; | |
fdc79501 | 1491 | len = strlen(ent->pattern); |
ebaa4296 | 1492 | dlen = ent->flags & MATCHFLG_DIRECTORY ? 1 : 0; |
fdc79501 WD |
1493 | if (!(plen + len + dlen)) |
1494 | continue; | |
1495 | write_int(f_out, plen + len + dlen); | |
417b5999 | 1496 | if (plen) |
fdc79501 WD |
1497 | write_buf(f_out, p, plen); |
1498 | write_buf(f_out, ent->pattern, len); | |
417b5999 | 1499 | if (dlen) |
fdc79501 | 1500 | write_byte(f_out, '/'); |
0f2ac855 | 1501 | } |
ed243f8c | 1502 | flp->tail = prev; |
c627d613 AT |
1503 | } |
1504 | ||
fdc79501 WD |
1505 | /* This is only called by the client. */ |
1506 | void send_filter_list(int f_out) | |
1507 | { | |
d5782b52 WD |
1508 | int receiver_wants_list = prune_empty_dirs |
1509 | || (delete_mode && (!delete_excluded || protocol_version >= 29)); | |
448797a1 WD |
1510 | |
1511 | if (local_server || (am_sender && !receiver_wants_list)) | |
fdc79501 WD |
1512 | f_out = -1; |
1513 | if (cvs_exclude && am_sender) { | |
1514 | if (protocol_version >= 29) | |
ebaa4296 MM |
1515 | load_filter_str(&filter_list, ":C", filter_template(0), 0); |
1516 | load_filter_str(&filter_list, "-C", filter_template(0), 0); | |
fdc79501 | 1517 | } |
c627d613 | 1518 | |
ed243f8c WD |
1519 | send_rules(f_out, &filter_list); |
1520 | ||
1521 | if (f_out >= 0) | |
fdc79501 | 1522 | write_int(f_out, 0); |
fdc79501 WD |
1523 | |
1524 | if (cvs_exclude) { | |
1525 | if (!am_sender || protocol_version < 29) | |
ebaa4296 | 1526 | load_filter_str(&filter_list, ":C", filter_template(0), 0); |
fdc79501 | 1527 | if (!am_sender) |
ebaa4296 | 1528 | load_filter_str(&filter_list, "-C", filter_template(0), 0); |
fdc79501 WD |
1529 | } |
1530 | } | |
1531 | ||
1532 | /* This is only called by the server. */ | |
1533 | void recv_filter_list(int f_in) | |
c627d613 | 1534 | { |
3fac51e2 | 1535 | char line[BIGPATHBUFLEN]; |
fdc79501 | 1536 | int xflags = protocol_version >= 29 ? 0 : XFLG_OLD_PREFIXES; |
d5782b52 | 1537 | int receiver_wants_list = prune_empty_dirs |
e257c6c2 WD |
1538 | || (delete_mode |
1539 | && (!delete_excluded || protocol_version >= 29)); | |
fdc79501 WD |
1540 | unsigned int len; |
1541 | ||
448797a1 | 1542 | if (!local_server && (am_sender || receiver_wants_list)) { |
fdc79501 WD |
1543 | while ((len = read_int(f_in)) != 0) { |
1544 | if (len >= sizeof line) | |
a1f99493 | 1545 | overflow_exit("recv_rules"); |
fdc79501 | 1546 | read_sbuf(f_in, line, len); |
ebaa4296 | 1547 | load_filter_str(&filter_list, line, filter_template(0), xflags); |
fdc79501 WD |
1548 | } |
1549 | } | |
9dd891bb | 1550 | |
fdc79501 | 1551 | if (cvs_exclude) { |
1412da7c | 1552 | if (local_server || am_sender || protocol_version < 29) |
ebaa4296 | 1553 | load_filter_str(&filter_list, ":C", filter_template(0), 0); |
1412da7c | 1554 | if (local_server || am_sender) |
ebaa4296 | 1555 | load_filter_str(&filter_list, "-C", filter_template(0), 0); |
651443a7 | 1556 | } |
ed243f8c WD |
1557 | |
1558 | if (local_server) /* filter out any rules that aren't for us. */ | |
1559 | send_rules(-1, &filter_list); | |
651443a7 | 1560 | } |