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