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