2 * The filter include/exclude routines.
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-2020 Wayne Davison
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.
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.
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.
24 #include "default-cvsignore.h"
30 extern int local_server;
31 extern int prune_empty_dirs;
32 extern int ignore_perishable;
33 extern int delete_mode;
34 extern int delete_excluded;
35 extern int cvs_exclude;
36 extern int sanitize_paths;
37 extern int protocol_version;
40 extern char curr_dir[MAXPATHLEN];
41 extern unsigned int curr_dir_len;
42 extern unsigned int module_dirlen;
44 filter_rule_list filter_list = { .debug_type = "" };
45 filter_rule_list cvs_filter_list = { .debug_type = " [global CVS]" };
46 filter_rule_list daemon_filter_list = { .debug_type = " [daemon]" };
48 int saw_xattr_filter = 0;
50 /* Need room enough for ":MODS " prefix plus some room to grow. */
51 #define MAX_RULE_PREFIX (16)
53 #define SLASH_WILD3_SUFFIX "/***"
55 /* The dirbuf is set by push_local_filters() to the current subdirectory
56 * relative to curr_dir that is being processed. The path always has a
57 * trailing slash appended, and the variable dirbuf_len contains the length
58 * of this path prefix. The path is always absolute. */
59 static char dirbuf[MAXPATHLEN+1];
60 static unsigned int dirbuf_len = 0;
61 static int dirbuf_depth;
63 /* This is True when we're scanning parent dirs for per-dir merge-files. */
64 static BOOL parent_dirscan = False;
66 /* This array contains a list of all the currently active per-dir merge
67 * files. This makes it easier to save the appropriate values when we
68 * "push" down into each subdirectory. */
69 static filter_rule **mergelist_parents;
70 static int mergelist_cnt = 0;
71 static int mergelist_size = 0;
73 /* Each filter_list_struct describes a singly-linked list by keeping track
74 * of both the head and tail pointers. The list is slightly unusual in that
75 * a parent-dir's content can be appended to the end of the local list in a
76 * special way: the last item in the local list has its "next" pointer set
77 * to point to the inherited list, but the local list's tail pointer points
78 * at the end of the local list. Thus, if the local list is empty, the head
79 * will be pointing at the inherited content but the tail will be NULL. To
80 * help you visualize this, here are the possible list arrangements:
82 * Completely Empty Local Content Only
83 * ================================== ====================================
84 * head -> NULL head -> Local1 -> Local2 -> NULL
85 * tail -> NULL tail -------------^
87 * Inherited Content Only Both Local and Inherited Content
88 * ================================== ====================================
89 * head -> Parent1 -> Parent2 -> NULL head -> L1 -> L2 -> P1 -> P2 -> NULL
90 * tail -> NULL tail ---------^
92 * This means that anyone wanting to traverse the whole list to use it just
93 * needs to start at the head and use the "next" pointers until it goes
94 * NULL. To add new local content, we insert the item after the tail item
95 * and update the tail (obviously, if "tail" was NULL, we insert it at the
96 * head). To clear the local list, WE MUST NOT FREE THE INHERITED CONTENT
97 * because it is shared between the current list and our parent list(s).
98 * The easiest way to handle this is to simply truncate the list after the
99 * tail item and then free the local list from the head. When inheriting
100 * the list for a new local dir, we just save off the filter_list_struct
101 * values (so we can pop back to them later) and set the tail to NULL.
104 static void teardown_mergelist(filter_rule *ex)
108 if (!ex->u.mergelist)
111 if (DEBUG_GTE(FILTER, 2)) {
112 rprintf(FINFO, "[%s] deactivating mergelist #%d%s\n",
113 who_am_i(), mergelist_cnt - 1,
114 ex->u.mergelist->debug_type);
117 free(ex->u.mergelist->debug_type);
118 free(ex->u.mergelist);
120 for (j = 0; j < mergelist_cnt; j++) {
121 if (mergelist_parents[j] == ex) {
122 mergelist_parents[j] = NULL;
126 while (mergelist_cnt && mergelist_parents[mergelist_cnt-1] == NULL)
130 static void free_filter(filter_rule *ex)
132 if (ex->rflags & FILTRULE_PERDIR_MERGE)
133 teardown_mergelist(ex);
138 static void free_filters(filter_rule *ent)
141 filter_rule *next = ent->next;
147 /* Build a filter structure given a filter pattern. The value in "pat"
148 * is not null-terminated. "rule" is either held or freed, so the
149 * caller should not free it. */
150 static void add_rule(filter_rule_list *listp, const char *pat, unsigned int pat_len,
151 filter_rule *rule, int xflags)
154 unsigned int pre_len, suf_len, slash_cnt = 0;
156 if (DEBUG_GTE(FILTER, 2)) {
157 rprintf(FINFO, "[%s] add_rule(%s%.*s%s)%s\n",
158 who_am_i(), get_rule_prefix(rule, pat, 0, NULL),
160 (rule->rflags & FILTRULE_DIRECTORY) ? "/" : "",
164 /* These flags also indicate that we're reading a list that
165 * needs to be filtered now, not post-filtered later. */
166 if (xflags & (XFLG_ANCHORED2ABS|XFLG_ABS_IF_SLASH)
167 && (rule->rflags & FILTRULES_SIDES)
168 == (am_sender ? FILTRULE_RECEIVER_SIDE : FILTRULE_SENDER_SIDE)) {
169 /* This filter applies only to the other side. Drop it. */
174 if (pat_len > 1 && pat[pat_len-1] == '/') {
176 rule->rflags |= FILTRULE_DIRECTORY;
179 for (cp = pat; cp < pat + pat_len; cp++) {
184 if (!(rule->rflags & (FILTRULE_ABS_PATH | FILTRULE_MERGE_FILE))
185 && ((xflags & (XFLG_ANCHORED2ABS|XFLG_ABS_IF_SLASH) && *pat == '/')
186 || (xflags & XFLG_ABS_IF_SLASH && slash_cnt))) {
187 rule->rflags |= FILTRULE_ABS_PATH;
189 pre_len = dirbuf_len - module_dirlen - 1;
195 /* The daemon wants dir-exclude rules to get an appended "/" + "***". */
196 if (xflags & XFLG_DIR2WILD3
197 && BITS_SETnUNSET(rule->rflags, FILTRULE_DIRECTORY, FILTRULE_INCLUDE)) {
198 rule->rflags &= ~FILTRULE_DIRECTORY;
199 suf_len = sizeof SLASH_WILD3_SUFFIX - 1;
203 if (!(rule->pattern = new_array(char, pre_len + pat_len + suf_len + 1)))
204 out_of_memory("add_rule");
206 memcpy(rule->pattern, dirbuf + module_dirlen, pre_len);
207 for (cp = rule->pattern; cp < rule->pattern + pre_len; cp++) {
212 strlcpy(rule->pattern + pre_len, pat, pat_len + 1);
215 memcpy(rule->pattern + pat_len, SLASH_WILD3_SUFFIX, suf_len+1);
220 if (strpbrk(rule->pattern, "*[?")) {
221 rule->rflags |= FILTRULE_WILD;
222 if ((cp = strstr(rule->pattern, "**")) != NULL) {
223 rule->rflags |= FILTRULE_WILD2;
224 /* If the pattern starts with **, note that. */
225 if (cp == rule->pattern)
226 rule->rflags |= FILTRULE_WILD2_PREFIX;
227 /* If the pattern ends with ***, note that. */
229 && rule->pattern[pat_len-3] == '*'
230 && rule->pattern[pat_len-2] == '*'
231 && rule->pattern[pat_len-1] == '*')
232 rule->rflags |= FILTRULE_WILD3_SUFFIX;
236 if (rule->rflags & FILTRULE_PERDIR_MERGE) {
237 filter_rule_list *lp;
241 if ((cp = strrchr(rule->pattern, '/')) != NULL)
246 /* If the local merge file was already mentioned, don't
248 for (i = 0; i < mergelist_cnt; i++) {
249 filter_rule *ex = mergelist_parents[i];
253 s = strrchr(ex->pattern, '/');
259 if (len == pat_len - (cp - rule->pattern) && memcmp(s, cp, len) == 0) {
265 if (!(lp = new_array0(filter_rule_list, 1)))
266 out_of_memory("add_rule");
267 if (asprintf(&lp->debug_type, " [per-dir %s]", cp) < 0)
268 out_of_memory("add_rule");
269 rule->u.mergelist = lp;
271 if (mergelist_cnt == mergelist_size) {
273 mergelist_parents = realloc_array(mergelist_parents,
276 if (!mergelist_parents)
277 out_of_memory("add_rule");
279 if (DEBUG_GTE(FILTER, 2)) {
280 rprintf(FINFO, "[%s] activating mergelist #%d%s\n",
281 who_am_i(), mergelist_cnt, lp->debug_type);
283 mergelist_parents[mergelist_cnt++] = rule;
285 rule->u.slash_cnt = slash_cnt;
288 rule->next = listp->head;
289 listp->head = listp->tail = rule;
291 rule->next = listp->tail->next;
292 listp->tail->next = rule;
297 /* This frees any non-inherited items, leaving just inherited items on the list. */
298 static void pop_filter_list(filter_rule_list *listp)
300 filter_rule *inherited;
305 inherited = listp->tail->next;
307 /* Truncate any inherited items from the local list. */
308 listp->tail->next = NULL;
309 /* Now free everything that is left. */
310 free_filters(listp->head);
312 listp->head = inherited;
316 /* This returns an expanded (absolute) filename for the merge-file name if
317 * the name has any slashes in it OR if the parent_dirscan var is True;
318 * otherwise it returns the original merge_file name. If the len_ptr value
319 * is non-NULL the merge_file name is limited by the referenced length
320 * value and will be updated with the length of the resulting name. We
321 * always return a name that is null terminated, even if the merge_file
323 static char *parse_merge_name(const char *merge_file, unsigned int *len_ptr,
324 unsigned int prefix_skip)
326 static char buf[MAXPATHLEN];
327 char *fn, tmpbuf[MAXPATHLEN];
330 if (!parent_dirscan && *merge_file != '/') {
331 /* Return the name unchanged it doesn't have any slashes. */
333 const char *p = merge_file + *len_ptr;
334 while (--p > merge_file && *p != '/') {}
335 if (p == merge_file) {
336 strlcpy(buf, merge_file, *len_ptr + 1);
339 } else if (strchr(merge_file, '/') == NULL)
340 return (char *)merge_file;
343 fn = *merge_file == '/' ? buf : tmpbuf;
344 if (sanitize_paths) {
345 const char *r = prefix_skip ? "/" : NULL;
346 /* null-terminate the name if it isn't already */
347 if (len_ptr && merge_file[*len_ptr]) {
348 char *to = fn == buf ? tmpbuf : buf;
349 strlcpy(to, merge_file, *len_ptr + 1);
352 if (!sanitize_path(fn, merge_file, r, dirbuf_depth, SP_DEFAULT)) {
353 rprintf(FERROR, "merge-file name overflows: %s\n",
359 strlcpy(fn, merge_file, len_ptr ? *len_ptr + 1 : MAXPATHLEN);
360 fn_len = clean_fname(fn, CFN_COLLAPSE_DOT_DOT_DIRS);
363 /* If the name isn't in buf yet, it wasn't absolute. */
365 int d_len = dirbuf_len - prefix_skip;
366 if (d_len + fn_len >= MAXPATHLEN) {
367 rprintf(FERROR, "merge-file name overflows: %s\n", fn);
370 memcpy(buf, dirbuf + prefix_skip, d_len);
371 memcpy(buf + d_len, fn, fn_len + 1);
372 fn_len = clean_fname(buf, CFN_COLLAPSE_DOT_DOT_DIRS);
380 /* Sets the dirbuf and dirbuf_len values. */
381 void set_filter_dir(const char *dir, unsigned int dirlen)
385 memcpy(dirbuf, curr_dir, curr_dir_len);
386 dirbuf[curr_dir_len] = '/';
387 len = curr_dir_len + 1;
388 if (len + dirlen >= MAXPATHLEN)
392 memcpy(dirbuf + len, dir, dirlen);
393 dirbuf[dirlen + len] = '\0';
394 dirbuf_len = clean_fname(dirbuf, CFN_COLLAPSE_DOT_DOT_DIRS);
395 if (dirbuf_len > 1 && dirbuf[dirbuf_len-1] == '.'
396 && dirbuf[dirbuf_len-2] == '/')
399 dirbuf[dirbuf_len++] = '/';
400 dirbuf[dirbuf_len] = '\0';
402 dirbuf_depth = count_dir_elements(dirbuf + module_dirlen);
405 /* This routine takes a per-dir merge-file entry and finishes its setup.
406 * If the name has a path portion then we check to see if it refers to a
407 * parent directory of the first transfer dir. If it does, we scan all the
408 * dirs from that point through the parent dir of the transfer dir looking
409 * for the per-dir merge-file in each one. */
410 static BOOL setup_merge_file(int mergelist_num, filter_rule *ex,
411 filter_rule_list *lp)
413 char buf[MAXPATHLEN];
414 char *x, *y, *pat = ex->pattern;
417 if (!(x = parse_merge_name(pat, NULL, 0)) || *x != '/')
420 if (DEBUG_GTE(FILTER, 2)) {
421 rprintf(FINFO, "[%s] performing parent_dirscan for mergelist #%d%s\n",
422 who_am_i(), mergelist_num, lp->debug_type);
426 ex->pattern = strdup(y+1);
430 strlcpy(buf, x, MAXPATHLEN);
432 pathjoin(buf, MAXPATHLEN, dirbuf, x);
434 len = clean_fname(buf, CFN_COLLAPSE_DOT_DOT_DIRS);
435 if (len != 1 && len < MAXPATHLEN-1) {
439 /* This ensures that the specified dir is a parent of the transfer. */
440 for (x = buf, y = dirbuf; *x && *x == *y; x++, y++) {}
442 y += strlen(y); /* nope -- skip the scan */
444 parent_dirscan = True;
446 char save[MAXPATHLEN];
447 strlcpy(save, y, MAXPATHLEN);
449 dirbuf_len = y - dirbuf;
450 strlcpy(x, ex->pattern, MAXPATHLEN - (x - buf));
451 parse_filter_file(lp, buf, ex, XFLG_ANCHORED2ABS);
452 if (ex->rflags & FILTRULE_NO_INHERIT) {
453 /* Free the undesired rules to clean up any per-dir
454 * mergelists they defined. Otherwise pop_local_filters
455 * may crash trying to restore nonexistent state for
456 * those mergelists. */
457 free_filters(lp->head);
461 strlcpy(y, save, MAXPATHLEN);
462 while ((*x++ = *y++) != '/') {}
464 parent_dirscan = False;
465 if (DEBUG_GTE(FILTER, 2)) {
466 rprintf(FINFO, "[%s] completed parent_dirscan for mergelist #%d%s\n",
467 who_am_i(), mergelist_num, lp->debug_type);
473 struct local_filter_state {
475 filter_rule_list mergelists[1];
478 /* Each time rsync changes to a new directory it call this function to
479 * handle all the per-dir merge-files. The "dir" value is the current path
480 * relative to curr_dir (which might not be null-terminated). We copy it
481 * into dirbuf so that we can easily append a file name on the end. */
482 void *push_local_filters(const char *dir, unsigned int dirlen)
484 struct local_filter_state *push;
487 set_filter_dir(dir, dirlen);
488 if (DEBUG_GTE(FILTER, 2)) {
489 rprintf(FINFO, "[%s] pushing local filters for %s\n",
493 if (!mergelist_cnt) {
494 /* No old state to save and no new merge files to push. */
498 push = (struct local_filter_state *)new_array(char,
499 sizeof (struct local_filter_state)
500 + (mergelist_cnt-1) * sizeof (filter_rule_list));
502 out_of_memory("push_local_filters");
504 push->mergelist_cnt = mergelist_cnt;
505 for (i = 0; i < mergelist_cnt; i++) {
506 filter_rule *ex = mergelist_parents[i];
509 memcpy(&push->mergelists[i], ex->u.mergelist, sizeof (filter_rule_list));
512 /* Note: parse_filter_file() might increase mergelist_cnt, so keep
513 * this loop separate from the above loop. */
514 for (i = 0; i < mergelist_cnt; i++) {
515 filter_rule *ex = mergelist_parents[i];
516 filter_rule_list *lp;
519 lp = ex->u.mergelist;
521 if (DEBUG_GTE(FILTER, 2)) {
522 rprintf(FINFO, "[%s] pushing mergelist #%d%s\n",
523 who_am_i(), i, lp->debug_type);
526 lp->tail = NULL; /* Switch any local rules to inherited. */
527 if (ex->rflags & FILTRULE_NO_INHERIT)
530 if (ex->rflags & FILTRULE_FINISH_SETUP) {
531 ex->rflags &= ~FILTRULE_FINISH_SETUP;
532 if (setup_merge_file(i, ex, lp))
533 set_filter_dir(dir, dirlen);
536 if (strlcpy(dirbuf + dirbuf_len, ex->pattern,
537 MAXPATHLEN - dirbuf_len) < MAXPATHLEN - dirbuf_len) {
538 parse_filter_file(lp, dirbuf, ex,
541 io_error |= IOERR_GENERAL;
543 "cannot add local filter rules in long-named directory: %s\n",
546 dirbuf[dirbuf_len] = '\0';
552 void pop_local_filters(void *mem)
554 struct local_filter_state *pop = (struct local_filter_state *)mem;
556 int old_mergelist_cnt = pop ? pop->mergelist_cnt : 0;
558 if (DEBUG_GTE(FILTER, 2))
559 rprintf(FINFO, "[%s] popping local filters\n", who_am_i());
561 for (i = mergelist_cnt; i-- > 0; ) {
562 filter_rule *ex = mergelist_parents[i];
563 filter_rule_list *lp;
566 lp = ex->u.mergelist;
568 if (DEBUG_GTE(FILTER, 2)) {
569 rprintf(FINFO, "[%s] popping mergelist #%d%s\n",
570 who_am_i(), i, lp->debug_type);
574 if (i >= old_mergelist_cnt && lp->head) {
575 /* This mergelist does not exist in the state to be restored, but it
576 * still has inherited rules. This can sometimes happen if a per-dir
577 * merge file calls setup_merge_file() in push_local_filters() and that
578 * leaves some inherited rules that aren't in the pushed list state. */
579 if (DEBUG_GTE(FILTER, 2)) {
580 rprintf(FINFO, "[%s] freeing parent_dirscan filters of mergelist #%d%s\n",
581 who_am_i(), i, ex->u.mergelist->debug_type);
588 return; /* No state to restore. */
590 for (i = 0; i < old_mergelist_cnt; i++) {
591 filter_rule *ex = mergelist_parents[i];
594 memcpy(ex->u.mergelist, &pop->mergelists[i], sizeof (filter_rule_list));
600 void change_local_filter_dir(const char *dname, int dlen, int dir_depth)
602 static int cur_depth = -1;
603 static void *filt_array[MAXPATHLEN/2+1];
606 for ( ; cur_depth >= 0; cur_depth--) {
607 if (filt_array[cur_depth]) {
608 pop_local_filters(filt_array[cur_depth]);
609 filt_array[cur_depth] = NULL;
615 assert(dir_depth < MAXPATHLEN/2+1);
617 for ( ; cur_depth >= dir_depth; cur_depth--) {
618 if (filt_array[cur_depth]) {
619 pop_local_filters(filt_array[cur_depth]);
620 filt_array[cur_depth] = NULL;
624 cur_depth = dir_depth;
625 filt_array[cur_depth] = push_local_filters(dname, dlen);
628 static int rule_matches(const char *fname, filter_rule *ex, int name_flags)
630 int slash_handling, str_cnt = 0, anchored_match = 0;
631 int ret_match = ex->rflags & FILTRULE_NEGATE ? 0 : 1;
632 char *p, *pattern = ex->pattern;
633 const char *strings[16]; /* more than enough */
634 const char *name = fname + (*fname == '/');
639 if (!(name_flags & NAME_IS_XATTR) ^ !(ex->rflags & FILTRULE_XATTR))
642 if (!ex->u.slash_cnt && !(ex->rflags & FILTRULE_WILD2)) {
643 /* If the pattern does not have any slashes AND it does
644 * not have a "**" (which could match a slash), then we
645 * just match the name portion of the path. */
646 if ((p = strrchr(name,'/')) != NULL)
648 } else if (ex->rflags & FILTRULE_ABS_PATH && *fname != '/'
649 && curr_dir_len > module_dirlen + 1) {
650 /* If we're matching against an absolute-path pattern,
651 * we need to prepend our full path info. */
652 strings[str_cnt++] = curr_dir + module_dirlen + 1;
653 strings[str_cnt++] = "/";
654 } else if (ex->rflags & FILTRULE_WILD2_PREFIX && *fname != '/') {
655 /* Allow "**"+"/" to match at the start of the string. */
656 strings[str_cnt++] = "/";
658 strings[str_cnt++] = name;
659 if (name_flags & NAME_IS_DIR) {
660 /* Allow a trailing "/"+"***" to match the directory. */
661 if (ex->rflags & FILTRULE_WILD3_SUFFIX)
662 strings[str_cnt++] = "/";
663 } else if (ex->rflags & FILTRULE_DIRECTORY)
665 strings[str_cnt] = NULL;
667 if (*pattern == '/') {
672 if (!anchored_match && ex->u.slash_cnt
673 && !(ex->rflags & FILTRULE_WILD2)) {
674 /* A non-anchored match with an infix slash and no "**"
675 * needs to match the last slash_cnt+1 name elements. */
676 slash_handling = ex->u.slash_cnt + 1;
677 } else if (!anchored_match && !(ex->rflags & FILTRULE_WILD2_PREFIX)
678 && ex->rflags & FILTRULE_WILD2) {
679 /* A non-anchored match with an infix or trailing "**" (but not
680 * a prefixed "**") needs to try matching after every slash. */
683 /* The pattern matches only at the start of the path or name. */
687 if (ex->rflags & FILTRULE_WILD) {
688 if (wildmatch_array(pattern, strings, slash_handling))
690 } else if (str_cnt > 1) {
691 if (litmatch_array(pattern, strings, slash_handling))
693 } else if (anchored_match) {
694 if (strcmp(name, pattern) == 0)
697 int l1 = strlen(name);
698 int l2 = strlen(pattern);
700 strcmp(name+(l1-l2),pattern) == 0 &&
701 (l1==l2 || name[l1-(l2+1)] == '/')) {
709 static void report_filter_result(enum logcode code, char const *name,
710 filter_rule const *ent,
711 int name_flags, const char *type)
713 /* If a trailing slash is present to match only directories,
714 * then it is stripped out by add_rule(). So as a special
715 * case we add it back in here. */
717 if (DEBUG_GTE(FILTER, 1)) {
718 static char *actions[2][2]
719 = { {"show", "hid"}, {"risk", "protect"} };
720 const char *w = who_am_i();
721 const char *t = name_flags & NAME_IS_XATTR ? "xattr"
722 : name_flags & NAME_IS_DIR ? "directory"
724 rprintf(code, "[%s] %sing %s %s because of pattern %s%s%s\n",
725 w, actions[*w!='s'][!(ent->rflags & FILTRULE_INCLUDE)],
726 t, name, ent->pattern,
727 ent->rflags & FILTRULE_DIRECTORY ? "/" : "", type);
731 /* This function is used to check if a file should be included/excluded
732 * from the list of files based on its name and type etc. The value of
733 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
734 int name_is_excluded(const char *fname, int name_flags, int filter_level)
736 if (daemon_filter_list.head && check_filter(&daemon_filter_list, FLOG, fname, name_flags) < 0) {
737 if (!(name_flags & NAME_IS_XATTR))
742 if (filter_level != ALL_FILTERS)
745 if (filter_list.head && check_filter(&filter_list, FINFO, fname, name_flags) < 0)
751 /* Return -1 if file "name" is defined to be excluded by the specified
752 * exclude list, 1 if it is included, and 0 if it was not matched. */
753 int check_filter(filter_rule_list *listp, enum logcode code,
754 const char *name, int name_flags)
758 for (ent = listp->head; ent; ent = ent->next) {
759 if (ignore_perishable && ent->rflags & FILTRULE_PERISHABLE)
761 if (ent->rflags & FILTRULE_PERDIR_MERGE) {
762 int rc = check_filter(ent->u.mergelist, code, name, name_flags);
767 if (ent->rflags & FILTRULE_CVS_IGNORE) {
768 int rc = check_filter(&cvs_filter_list, code, name, name_flags);
773 if (rule_matches(name, ent, name_flags)) {
774 report_filter_result(code, name, ent, name_flags, listp->debug_type);
775 return ent->rflags & FILTRULE_INCLUDE ? 1 : -1;
782 #define RULE_STRCMP(s,r) rule_strcmp((s), (r), sizeof (r) - 1)
784 static const uchar *rule_strcmp(const uchar *str, const char *rule, int rule_len)
786 if (strncmp((char*)str, rule, rule_len) != 0)
788 if (isspace(str[rule_len]) || str[rule_len] == '_' || !str[rule_len])
789 return str + rule_len - 1;
790 if (str[rule_len] == ',')
791 return str + rule_len;
795 #define FILTRULES_FROM_CONTAINER (FILTRULE_ABS_PATH | FILTRULE_INCLUDE \
796 | FILTRULE_DIRECTORY | FILTRULE_NEGATE \
797 | FILTRULE_PERISHABLE)
799 /* Gets the next include/exclude rule from *rulestr_ptr and advances
800 * *rulestr_ptr to point beyond it. Stores the pattern's start (within
801 * *rulestr_ptr) and length in *pat_ptr and *pat_len_ptr, and returns a newly
802 * allocated filter_rule containing the rest of the information. Returns
803 * NULL if there are no more rules in the input.
805 * The template provides defaults for the new rule to inherit, and the
806 * template rflags and the xflags additionally affect parsing. */
807 static filter_rule *parse_rule_tok(const char **rulestr_ptr,
808 const filter_rule *template, int xflags,
809 const char **pat_ptr, unsigned int *pat_len_ptr)
811 const uchar *s = (const uchar *)*rulestr_ptr;
815 if (template->rflags & FILTRULE_WORD_SPLIT) {
816 /* Skip over any initial whitespace. */
819 /* Update to point to real start of rule. */
820 *rulestr_ptr = (const char *)s;
825 if (!(rule = new0(filter_rule)))
826 out_of_memory("parse_rule_tok");
828 /* Inherit from the template. Don't inherit FILTRULES_SIDES; we check
830 rule->rflags = template->rflags & FILTRULES_FROM_CONTAINER;
832 /* Figure out what kind of a filter rule "s" is pointing at. Note
833 * that if FILTRULE_NO_PREFIXES is set, the rule is either an include
834 * or an exclude based on the inheritance of the FILTRULE_INCLUDE
835 * flag (above). XFLG_OLD_PREFIXES indicates a compatibility mode
836 * for old include/exclude patterns where just "+ " and "- " are
837 * allowed as optional prefixes. */
838 if (template->rflags & FILTRULE_NO_PREFIXES) {
839 if (*s == '!' && template->rflags & FILTRULE_CVS_IGNORE)
840 rule->rflags |= FILTRULE_CLEAR_LIST; /* Tentative! */
841 } else if (xflags & XFLG_OLD_PREFIXES) {
842 if (*s == '-' && s[1] == ' ') {
843 rule->rflags &= ~FILTRULE_INCLUDE;
845 } else if (*s == '+' && s[1] == ' ') {
846 rule->rflags |= FILTRULE_INCLUDE;
848 } else if (*s == '!')
849 rule->rflags |= FILTRULE_CLEAR_LIST; /* Tentative! */
852 BOOL prefix_specifies_side = False;
855 if ((s = RULE_STRCMP(s, "clear")) != NULL)
859 if ((s = RULE_STRCMP(s, "dir-merge")) != NULL)
863 if ((s = RULE_STRCMP(s, "exclude")) != NULL)
867 if ((s = RULE_STRCMP(s, "hide")) != NULL)
871 if ((s = RULE_STRCMP(s, "include")) != NULL)
875 if ((s = RULE_STRCMP(s, "merge")) != NULL)
879 if ((s = RULE_STRCMP(s, "protect")) != NULL)
883 if ((s = RULE_STRCMP(s, "risk")) != NULL)
887 if ((s = RULE_STRCMP(s, "show")) != NULL)
898 rule->rflags |= FILTRULE_PERDIR_MERGE
899 | FILTRULE_FINISH_SETUP;
902 rule->rflags |= FILTRULE_MERGE_FILE;
905 rule->rflags |= FILTRULE_INCLUDE;
910 rule->rflags |= FILTRULE_INCLUDE;
913 rule->rflags |= FILTRULE_SENDER_SIDE;
914 prefix_specifies_side = True;
917 rule->rflags |= FILTRULE_INCLUDE;
920 rule->rflags |= FILTRULE_RECEIVER_SIDE;
921 prefix_specifies_side = True;
924 rule->rflags |= FILTRULE_CLEAR_LIST;
927 rprintf(FERROR, "Unknown filter rule: `%s'\n", *rulestr_ptr);
928 exit_cleanup(RERR_SYNTAX);
930 while (ch != '!' && *++s && *s != ' ' && *s != '_') {
931 if (template->rflags & FILTRULE_WORD_SPLIT && isspace(*s)) {
939 "invalid modifier '%c' at position %d in filter rule: %s\n",
940 *s, (int)(s - (const uchar *)*rulestr_ptr), *rulestr_ptr);
941 exit_cleanup(RERR_SYNTAX);
943 if (!BITS_SETnUNSET(rule->rflags, FILTRULE_MERGE_FILE, FILTRULE_NO_PREFIXES))
945 rule->rflags |= FILTRULE_NO_PREFIXES;
948 if (!BITS_SETnUNSET(rule->rflags, FILTRULE_MERGE_FILE, FILTRULE_NO_PREFIXES))
950 rule->rflags |= FILTRULE_NO_PREFIXES
954 rule->rflags |= FILTRULE_ABS_PATH;
957 /* Negation really goes with the pattern, so it
958 * isn't useful as a merge-file default. */
959 if (rule->rflags & FILTRULE_MERGE_FILE)
961 rule->rflags |= FILTRULE_NEGATE;
964 if (rule->rflags & FILTRULE_NO_PREFIXES || prefix_specifies_side)
966 rule->rflags |= FILTRULE_NO_PREFIXES
967 | FILTRULE_WORD_SPLIT
968 | FILTRULE_NO_INHERIT
969 | FILTRULE_CVS_IGNORE;
972 if (!(rule->rflags & FILTRULE_MERGE_FILE))
974 rule->rflags |= FILTRULE_EXCLUDE_SELF;
977 if (!(rule->rflags & FILTRULE_MERGE_FILE))
979 rule->rflags |= FILTRULE_NO_INHERIT;
982 rule->rflags |= FILTRULE_PERISHABLE;
985 if (prefix_specifies_side)
987 rule->rflags |= FILTRULE_RECEIVER_SIDE;
990 if (prefix_specifies_side)
992 rule->rflags |= FILTRULE_SENDER_SIDE;
995 if (!(rule->rflags & FILTRULE_MERGE_FILE))
997 rule->rflags |= FILTRULE_WORD_SPLIT;
1000 rule->rflags |= FILTRULE_XATTR;
1001 saw_xattr_filter = 1;
1008 if (template->rflags & FILTRULES_SIDES) {
1009 if (rule->rflags & FILTRULES_SIDES) {
1010 /* The filter and template both specify side(s). This
1011 * is dodgy (and won't work correctly if the template is
1012 * a one-sided per-dir merge rule), so reject it. */
1014 "specified-side merge file contains specified-side filter: %s\n",
1016 exit_cleanup(RERR_SYNTAX);
1018 rule->rflags |= template->rflags & FILTRULES_SIDES;
1021 if (template->rflags & FILTRULE_WORD_SPLIT) {
1022 const uchar *cp = s;
1023 /* Token ends at whitespace or the end of the string. */
1024 while (!isspace(*cp) && *cp != '\0')
1028 len = strlen((char*)s);
1030 if (rule->rflags & FILTRULE_CLEAR_LIST) {
1031 if (!(rule->rflags & FILTRULE_NO_PREFIXES)
1032 && !(xflags & XFLG_OLD_PREFIXES) && len) {
1034 "'!' rule has trailing characters: %s\n", *rulestr_ptr);
1035 exit_cleanup(RERR_SYNTAX);
1038 rule->rflags &= ~FILTRULE_CLEAR_LIST;
1039 } else if (!len && !(rule->rflags & FILTRULE_CVS_IGNORE)) {
1040 rprintf(FERROR, "unexpected end of filter rule: %s\n", *rulestr_ptr);
1041 exit_cleanup(RERR_SYNTAX);
1044 /* --delete-excluded turns an un-modified include/exclude into a sender-side rule. */
1046 && !(rule->rflags & (FILTRULES_SIDES|FILTRULE_MERGE_FILE|FILTRULE_PERDIR_MERGE)))
1047 rule->rflags |= FILTRULE_SENDER_SIDE;
1049 *pat_ptr = (const char *)s;
1051 *rulestr_ptr = *pat_ptr + len;
1055 static void get_cvs_excludes(uint32 rflags)
1057 static int initialized = 0;
1058 char *p, fname[MAXPATHLEN];
1064 parse_filter_str(&cvs_filter_list, DEFAULT_CVSIGNORE,
1065 rule_template(rflags | (protocol_version >= 30 ? FILTRULE_PERISHABLE : 0)),
1068 p = module_id >= 0 && lp_use_chroot(module_id) ? "/" : getenv("HOME");
1069 if (p && pathjoin(fname, MAXPATHLEN, p, ".cvsignore") < MAXPATHLEN)
1070 parse_filter_file(&cvs_filter_list, fname, rule_template(rflags), 0);
1072 parse_filter_str(&cvs_filter_list, getenv("CVSIGNORE"), rule_template(rflags), 0);
1075 const filter_rule *rule_template(uint32 rflags)
1077 static filter_rule template; /* zero-initialized */
1078 template.rflags = rflags;
1082 void parse_filter_str(filter_rule_list *listp, const char *rulestr,
1083 const filter_rule *template, int xflags)
1087 unsigned int pat_len;
1095 /* Remember that the returned string is NOT '\0' terminated! */
1096 if (!(rule = parse_rule_tok(&rulestr, template, xflags, &pat, &pat_len)))
1099 if (pat_len >= MAXPATHLEN) {
1100 rprintf(FERROR, "discarding over-long filter: %.*s\n",
1107 new_rflags = rule->rflags;
1108 if (new_rflags & FILTRULE_CLEAR_LIST) {
1109 if (DEBUG_GTE(FILTER, 2)) {
1111 "[%s] clearing filter list%s\n",
1112 who_am_i(), listp->debug_type);
1114 pop_filter_list(listp);
1119 if (new_rflags & FILTRULE_MERGE_FILE) {
1124 if (new_rflags & FILTRULE_EXCLUDE_SELF) {
1126 filter_rule *excl_self;
1128 if (!(excl_self = new0(filter_rule)))
1129 out_of_memory("parse_filter_str");
1130 /* Find the beginning of the basename and add an exclude for it. */
1131 for (name = pat + pat_len; name > pat && name[-1] != '/'; name--) {}
1132 add_rule(listp, name, (pat + pat_len) - name, excl_self, 0);
1133 rule->rflags &= ~FILTRULE_EXCLUDE_SELF;
1135 if (new_rflags & FILTRULE_PERDIR_MERGE) {
1136 if (parent_dirscan) {
1138 unsigned int len = pat_len;
1139 if ((p = parse_merge_name(pat, &len, module_dirlen)))
1140 add_rule(listp, p, len, rule, 0);
1147 unsigned int len = pat_len;
1148 if ((p = parse_merge_name(pat, &len, 0)))
1149 parse_filter_file(listp, p, rule, XFLG_FATAL_ERRORS);
1155 add_rule(listp, pat, pat_len, rule, xflags);
1157 if (new_rflags & FILTRULE_CVS_IGNORE
1158 && !(new_rflags & FILTRULE_MERGE_FILE))
1159 get_cvs_excludes(new_rflags);
1163 void parse_filter_file(filter_rule_list *listp, const char *fname, const filter_rule *template, int xflags)
1166 char line[BIGPATHBUFLEN];
1167 char *eob = line + sizeof line - 1;
1168 BOOL word_split = (template->rflags & FILTRULE_WORD_SPLIT) != 0;
1170 if (!fname || !*fname)
1173 if (*fname != '-' || fname[1] || am_server) {
1174 if (daemon_filter_list.head) {
1175 strlcpy(line, fname, sizeof line);
1176 clean_fname(line, CFN_COLLAPSE_DOT_DOT_DIRS);
1177 if (check_filter(&daemon_filter_list, FLOG, line, 0) < 0)
1180 fp = fopen(line, "rb");
1182 fp = fopen(fname, "rb");
1186 if (DEBUG_GTE(FILTER, 2)) {
1187 rprintf(FINFO, "[%s] parse_filter_file(%s,%x,%x)%s\n",
1188 who_am_i(), fname, template->rflags, xflags,
1189 fp ? "" : " [not found]");
1193 if (xflags & XFLG_FATAL_ERRORS) {
1194 rsyserr(FERROR, errno,
1195 "failed to open %sclude file %s",
1196 template->rflags & FILTRULE_INCLUDE ? "in" : "ex",
1198 exit_cleanup(RERR_FILEIO);
1202 dirbuf[dirbuf_len] = '\0';
1206 int ch, overflow = 0;
1208 if ((ch = getc(fp)) == EOF) {
1209 if (ferror(fp) && errno == EINTR) {
1215 if (word_split && isspace(ch))
1217 if (eol_nulls? !ch : (ch == '\n' || ch == '\r'))
1225 rprintf(FERROR, "discarding over-long filter: %s...\n", line);
1229 /* Skip an empty token and (when line parsing) comments. */
1230 if (*line && (word_split || (*line != ';' && *line != '#')))
1231 parse_filter_str(listp, line, template, xflags);
1238 /* If the "for_xfer" flag is set, the prefix is made compatible with the
1239 * current protocol_version (if possible) or a NULL is returned (if not
1241 char *get_rule_prefix(filter_rule *rule, const char *pat, int for_xfer,
1242 unsigned int *plen_ptr)
1244 static char buf[MAX_RULE_PREFIX+1];
1246 int legal_len = for_xfer && protocol_version < 29 ? 1 : MAX_RULE_PREFIX-1;
1248 if (rule->rflags & FILTRULE_PERDIR_MERGE) {
1252 } else if (rule->rflags & FILTRULE_INCLUDE)
1254 else if (legal_len != 1
1255 || ((*pat == '-' || *pat == '+') && pat[1] == ' '))
1260 if (rule->rflags & FILTRULE_ABS_PATH)
1262 if (rule->rflags & FILTRULE_NEGATE)
1264 if (rule->rflags & FILTRULE_CVS_IGNORE)
1267 if (rule->rflags & FILTRULE_NO_INHERIT)
1269 if (rule->rflags & FILTRULE_WORD_SPLIT)
1271 if (rule->rflags & FILTRULE_NO_PREFIXES) {
1272 if (rule->rflags & FILTRULE_INCLUDE)
1278 if (rule->rflags & FILTRULE_EXCLUDE_SELF)
1280 if (rule->rflags & FILTRULE_XATTR)
1282 if (rule->rflags & FILTRULE_SENDER_SIDE
1283 && (!for_xfer || protocol_version >= 29))
1285 if (rule->rflags & FILTRULE_RECEIVER_SIDE
1286 && (!for_xfer || protocol_version >= 29
1287 || (delete_excluded && am_sender)))
1289 if (rule->rflags & FILTRULE_PERISHABLE) {
1290 if (!for_xfer || protocol_version >= 30)
1295 if (op - buf > legal_len)
1301 *plen_ptr = op - buf;
1305 static void send_rules(int f_out, filter_rule_list *flp)
1307 filter_rule *ent, *prev = NULL;
1309 for (ent = flp->head; ent; ent = ent->next) {
1310 unsigned int len, plen, dlen;
1314 /* Note we need to check delete_excluded here in addition to
1315 * the code in parse_rule_tok() because some rules may have
1316 * been added before we found the --delete-excluded option.
1317 * We must also elide any CVS merge-file rules to avoid a
1318 * backward compatibility problem, and we elide any no-prefix
1319 * merge files as an optimization (since they can only have
1320 * include/exclude rules). */
1321 if (ent->rflags & FILTRULE_SENDER_SIDE)
1322 elide = am_sender ? 1 : -1;
1323 if (ent->rflags & FILTRULE_RECEIVER_SIDE)
1324 elide = elide ? 0 : am_sender ? -1 : 1;
1325 else if (delete_excluded && !elide
1326 && (!(ent->rflags & FILTRULE_PERDIR_MERGE)
1327 || ent->rflags & FILTRULE_NO_PREFIXES))
1328 elide = am_sender ? 1 : -1;
1331 prev->next = ent->next;
1333 flp->head = ent->next;
1338 if (ent->rflags & FILTRULE_CVS_IGNORE
1339 && !(ent->rflags & FILTRULE_MERGE_FILE)) {
1340 int f = am_sender || protocol_version < 29 ? f_out : -2;
1341 send_rules(f, &cvs_filter_list);
1345 p = get_rule_prefix(ent, ent->pattern, 1, &plen);
1348 "filter rules are too modern for remote rsync.\n");
1349 exit_cleanup(RERR_PROTOCOL);
1353 len = strlen(ent->pattern);
1354 dlen = ent->rflags & FILTRULE_DIRECTORY ? 1 : 0;
1355 if (!(plen + len + dlen))
1357 write_int(f_out, plen + len + dlen);
1359 write_buf(f_out, p, plen);
1360 write_buf(f_out, ent->pattern, len);
1362 write_byte(f_out, '/');
1367 /* This is only called by the client. */
1368 void send_filter_list(int f_out)
1370 int receiver_wants_list = prune_empty_dirs
1371 || (delete_mode && (!delete_excluded || protocol_version >= 29));
1373 if (local_server || (am_sender && !receiver_wants_list))
1375 if (cvs_exclude && am_sender) {
1376 if (protocol_version >= 29)
1377 parse_filter_str(&filter_list, ":C", rule_template(0), 0);
1378 parse_filter_str(&filter_list, "-C", rule_template(0), 0);
1381 send_rules(f_out, &filter_list);
1384 write_int(f_out, 0);
1387 if (!am_sender || protocol_version < 29)
1388 parse_filter_str(&filter_list, ":C", rule_template(0), 0);
1390 parse_filter_str(&filter_list, "-C", rule_template(0), 0);
1394 /* This is only called by the server. */
1395 void recv_filter_list(int f_in)
1397 char line[BIGPATHBUFLEN];
1398 int xflags = protocol_version >= 29 ? 0 : XFLG_OLD_PREFIXES;
1399 int receiver_wants_list = prune_empty_dirs
1400 || (delete_mode && (!delete_excluded || protocol_version >= 29));
1403 if (!local_server && (am_sender || receiver_wants_list)) {
1404 while ((len = read_int(f_in)) != 0) {
1405 if (len >= sizeof line)
1406 overflow_exit("recv_rules");
1407 read_sbuf(f_in, line, len);
1408 parse_filter_str(&filter_list, line, rule_template(0), xflags);
1413 if (local_server || am_sender || protocol_version < 29)
1414 parse_filter_str(&filter_list, ":C", rule_template(0), 0);
1415 if (local_server || am_sender)
1416 parse_filter_str(&filter_list, "-C", rule_template(0), 0);
1419 if (local_server) /* filter out any rules that aren't for us. */
1420 send_rules(-1, &filter_list);