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-2019 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.
29 extern int local_server
;
30 extern int prune_empty_dirs
;
31 extern int ignore_perishable
;
32 extern int delete_mode
;
33 extern int delete_excluded
;
34 extern int cvs_exclude
;
35 extern int sanitize_paths
;
36 extern int protocol_version
;
39 extern char curr_dir
[MAXPATHLEN
];
40 extern unsigned int curr_dir_len
;
41 extern unsigned int module_dirlen
;
43 filter_rule_list filter_list
= { .debug_type
= "" };
44 filter_rule_list cvs_filter_list
= { .debug_type
= " [global CVS]" };
45 filter_rule_list daemon_filter_list
= { .debug_type
= " [daemon]" };
47 int saw_xattr_filter
= 0;
49 /* Need room enough for ":MODS " prefix plus some room to grow. */
50 #define MAX_RULE_PREFIX (16)
52 #define SLASH_WILD3_SUFFIX "/***"
54 /* The dirbuf is set by push_local_filters() to the current subdirectory
55 * relative to curr_dir that is being processed. The path always has a
56 * trailing slash appended, and the variable dirbuf_len contains the length
57 * of this path prefix. The path is always absolute. */
58 static char dirbuf
[MAXPATHLEN
+1];
59 static unsigned int dirbuf_len
= 0;
60 static int dirbuf_depth
;
62 /* This is True when we're scanning parent dirs for per-dir merge-files. */
63 static BOOL parent_dirscan
= False
;
65 /* This array contains a list of all the currently active per-dir merge
66 * files. This makes it easier to save the appropriate values when we
67 * "push" down into each subdirectory. */
68 static filter_rule
**mergelist_parents
;
69 static int mergelist_cnt
= 0;
70 static int mergelist_size
= 0;
72 /* Each filter_list_struct describes a singly-linked list by keeping track
73 * of both the head and tail pointers. The list is slightly unusual in that
74 * a parent-dir's content can be appended to the end of the local list in a
75 * special way: the last item in the local list has its "next" pointer set
76 * to point to the inherited list, but the local list's tail pointer points
77 * at the end of the local list. Thus, if the local list is empty, the head
78 * will be pointing at the inherited content but the tail will be NULL. To
79 * help you visualize this, here are the possible list arrangements:
81 * Completely Empty Local Content Only
82 * ================================== ====================================
83 * head -> NULL head -> Local1 -> Local2 -> NULL
84 * tail -> NULL tail -------------^
86 * Inherited Content Only Both Local and Inherited Content
87 * ================================== ====================================
88 * head -> Parent1 -> Parent2 -> NULL head -> L1 -> L2 -> P1 -> P2 -> NULL
89 * tail -> NULL tail ---------^
91 * This means that anyone wanting to traverse the whole list to use it just
92 * needs to start at the head and use the "next" pointers until it goes
93 * NULL. To add new local content, we insert the item after the tail item
94 * and update the tail (obviously, if "tail" was NULL, we insert it at the
95 * head). To clear the local list, WE MUST NOT FREE THE INHERITED CONTENT
96 * because it is shared between the current list and our parent list(s).
97 * The easiest way to handle this is to simply truncate the list after the
98 * tail item and then free the local list from the head. When inheriting
99 * the list for a new local dir, we just save off the filter_list_struct
100 * values (so we can pop back to them later) and set the tail to NULL.
103 static void teardown_mergelist(filter_rule
*ex
)
107 if (!ex
->u
.mergelist
)
110 if (DEBUG_GTE(FILTER
, 2)) {
111 rprintf(FINFO
, "[%s] deactivating mergelist #%d%s\n",
112 who_am_i(), mergelist_cnt
- 1,
113 ex
->u
.mergelist
->debug_type
);
116 free(ex
->u
.mergelist
->debug_type
);
117 free(ex
->u
.mergelist
);
119 for (j
= 0; j
< mergelist_cnt
; j
++) {
120 if (mergelist_parents
[j
] == ex
) {
121 mergelist_parents
[j
] = NULL
;
125 while (mergelist_cnt
&& mergelist_parents
[mergelist_cnt
-1] == NULL
)
129 static void free_filter(filter_rule
*ex
)
131 if (ex
->rflags
& FILTRULE_PERDIR_MERGE
)
132 teardown_mergelist(ex
);
137 static void free_filters(filter_rule
*ent
)
140 filter_rule
*next
= ent
->next
;
146 /* Build a filter structure given a filter pattern. The value in "pat"
147 * is not null-terminated. "rule" is either held or freed, so the
148 * caller should not free it. */
149 static void add_rule(filter_rule_list
*listp
, const char *pat
, unsigned int pat_len
,
150 filter_rule
*rule
, int xflags
)
153 unsigned int pre_len
, suf_len
, slash_cnt
= 0;
155 if (DEBUG_GTE(FILTER
, 2)) {
156 rprintf(FINFO
, "[%s] add_rule(%s%.*s%s)%s\n",
157 who_am_i(), get_rule_prefix(rule
, pat
, 0, NULL
),
159 (rule
->rflags
& FILTRULE_DIRECTORY
) ? "/" : "",
163 /* These flags also indicate that we're reading a list that
164 * needs to be filtered now, not post-filtered later. */
165 if (xflags
& (XFLG_ANCHORED2ABS
|XFLG_ABS_IF_SLASH
)
166 && (rule
->rflags
& FILTRULES_SIDES
)
167 == (am_sender
? FILTRULE_RECEIVER_SIDE
: FILTRULE_SENDER_SIDE
)) {
168 /* This filter applies only to the other side. Drop it. */
173 if (pat_len
> 1 && pat
[pat_len
-1] == '/') {
175 rule
->rflags
|= FILTRULE_DIRECTORY
;
178 for (cp
= pat
; cp
< pat
+ pat_len
; cp
++) {
183 if (!(rule
->rflags
& (FILTRULE_ABS_PATH
| FILTRULE_MERGE_FILE
))
184 && ((xflags
& (XFLG_ANCHORED2ABS
|XFLG_ABS_IF_SLASH
) && *pat
== '/')
185 || (xflags
& XFLG_ABS_IF_SLASH
&& slash_cnt
))) {
186 rule
->rflags
|= FILTRULE_ABS_PATH
;
188 pre_len
= dirbuf_len
- module_dirlen
- 1;
194 /* The daemon wants dir-exclude rules to get an appended "/" + "***". */
195 if (xflags
& XFLG_DIR2WILD3
196 && BITS_SETnUNSET(rule
->rflags
, FILTRULE_DIRECTORY
, FILTRULE_INCLUDE
)) {
197 rule
->rflags
&= ~FILTRULE_DIRECTORY
;
198 suf_len
= sizeof SLASH_WILD3_SUFFIX
- 1;
202 if (!(rule
->pattern
= new_array(char, pre_len
+ pat_len
+ suf_len
+ 1)))
203 out_of_memory("add_rule");
205 memcpy(rule
->pattern
, dirbuf
+ module_dirlen
, pre_len
);
206 for (cp
= rule
->pattern
; cp
< rule
->pattern
+ pre_len
; cp
++) {
211 strlcpy(rule
->pattern
+ pre_len
, pat
, pat_len
+ 1);
214 memcpy(rule
->pattern
+ pat_len
, SLASH_WILD3_SUFFIX
, suf_len
+1);
219 if (strpbrk(rule
->pattern
, "*[?")) {
220 rule
->rflags
|= FILTRULE_WILD
;
221 if ((cp
= strstr(rule
->pattern
, "**")) != NULL
) {
222 rule
->rflags
|= FILTRULE_WILD2
;
223 /* If the pattern starts with **, note that. */
224 if (cp
== rule
->pattern
)
225 rule
->rflags
|= FILTRULE_WILD2_PREFIX
;
226 /* If the pattern ends with ***, note that. */
228 && rule
->pattern
[pat_len
-3] == '*'
229 && rule
->pattern
[pat_len
-2] == '*'
230 && rule
->pattern
[pat_len
-1] == '*')
231 rule
->rflags
|= FILTRULE_WILD3_SUFFIX
;
235 if (rule
->rflags
& FILTRULE_PERDIR_MERGE
) {
236 filter_rule_list
*lp
;
240 if ((cp
= strrchr(rule
->pattern
, '/')) != NULL
)
245 /* If the local merge file was already mentioned, don't
247 for (i
= 0; i
< mergelist_cnt
; i
++) {
248 filter_rule
*ex
= mergelist_parents
[i
];
252 s
= strrchr(ex
->pattern
, '/');
258 if (len
== pat_len
- (cp
- rule
->pattern
) && memcmp(s
, cp
, len
) == 0) {
264 if (!(lp
= new_array0(filter_rule_list
, 1)))
265 out_of_memory("add_rule");
266 if (asprintf(&lp
->debug_type
, " [per-dir %s]", cp
) < 0)
267 out_of_memory("add_rule");
268 rule
->u
.mergelist
= lp
;
270 if (mergelist_cnt
== mergelist_size
) {
272 mergelist_parents
= realloc_array(mergelist_parents
,
275 if (!mergelist_parents
)
276 out_of_memory("add_rule");
278 if (DEBUG_GTE(FILTER
, 2)) {
279 rprintf(FINFO
, "[%s] activating mergelist #%d%s\n",
280 who_am_i(), mergelist_cnt
, lp
->debug_type
);
282 mergelist_parents
[mergelist_cnt
++] = rule
;
284 rule
->u
.slash_cnt
= slash_cnt
;
287 rule
->next
= listp
->head
;
288 listp
->head
= listp
->tail
= rule
;
290 rule
->next
= listp
->tail
->next
;
291 listp
->tail
->next
= rule
;
296 /* This frees any non-inherited items, leaving just inherited items on the list. */
297 static void pop_filter_list(filter_rule_list
*listp
)
299 filter_rule
*inherited
;
304 inherited
= listp
->tail
->next
;
306 /* Truncate any inherited items from the local list. */
307 listp
->tail
->next
= NULL
;
308 /* Now free everything that is left. */
309 free_filters(listp
->head
);
311 listp
->head
= inherited
;
315 /* This returns an expanded (absolute) filename for the merge-file name if
316 * the name has any slashes in it OR if the parent_dirscan var is True;
317 * otherwise it returns the original merge_file name. If the len_ptr value
318 * is non-NULL the merge_file name is limited by the referenced length
319 * value and will be updated with the length of the resulting name. We
320 * always return a name that is null terminated, even if the merge_file
322 static char *parse_merge_name(const char *merge_file
, unsigned int *len_ptr
,
323 unsigned int prefix_skip
)
325 static char buf
[MAXPATHLEN
];
326 char *fn
, tmpbuf
[MAXPATHLEN
];
329 if (!parent_dirscan
&& *merge_file
!= '/') {
330 /* Return the name unchanged it doesn't have any slashes. */
332 const char *p
= merge_file
+ *len_ptr
;
333 while (--p
> merge_file
&& *p
!= '/') {}
334 if (p
== merge_file
) {
335 strlcpy(buf
, merge_file
, *len_ptr
+ 1);
338 } else if (strchr(merge_file
, '/') == NULL
)
339 return (char *)merge_file
;
342 fn
= *merge_file
== '/' ? buf
: tmpbuf
;
343 if (sanitize_paths
) {
344 const char *r
= prefix_skip
? "/" : NULL
;
345 /* null-terminate the name if it isn't already */
346 if (len_ptr
&& merge_file
[*len_ptr
]) {
347 char *to
= fn
== buf
? tmpbuf
: buf
;
348 strlcpy(to
, merge_file
, *len_ptr
+ 1);
351 if (!sanitize_path(fn
, merge_file
, r
, dirbuf_depth
, SP_DEFAULT
)) {
352 rprintf(FERROR
, "merge-file name overflows: %s\n",
358 strlcpy(fn
, merge_file
, len_ptr
? *len_ptr
+ 1 : MAXPATHLEN
);
359 fn_len
= clean_fname(fn
, CFN_COLLAPSE_DOT_DOT_DIRS
);
362 /* If the name isn't in buf yet, it wasn't absolute. */
364 int d_len
= dirbuf_len
- prefix_skip
;
365 if (d_len
+ fn_len
>= MAXPATHLEN
) {
366 rprintf(FERROR
, "merge-file name overflows: %s\n", fn
);
369 memcpy(buf
, dirbuf
+ prefix_skip
, d_len
);
370 memcpy(buf
+ d_len
, fn
, fn_len
+ 1);
371 fn_len
= clean_fname(buf
, CFN_COLLAPSE_DOT_DOT_DIRS
);
379 /* Sets the dirbuf and dirbuf_len values. */
380 void set_filter_dir(const char *dir
, unsigned int dirlen
)
384 memcpy(dirbuf
, curr_dir
, curr_dir_len
);
385 dirbuf
[curr_dir_len
] = '/';
386 len
= curr_dir_len
+ 1;
387 if (len
+ dirlen
>= MAXPATHLEN
)
391 memcpy(dirbuf
+ len
, dir
, dirlen
);
392 dirbuf
[dirlen
+ len
] = '\0';
393 dirbuf_len
= clean_fname(dirbuf
, CFN_COLLAPSE_DOT_DOT_DIRS
);
394 if (dirbuf_len
> 1 && dirbuf
[dirbuf_len
-1] == '.'
395 && dirbuf
[dirbuf_len
-2] == '/')
398 dirbuf
[dirbuf_len
++] = '/';
399 dirbuf
[dirbuf_len
] = '\0';
401 dirbuf_depth
= count_dir_elements(dirbuf
+ module_dirlen
);
404 /* This routine takes a per-dir merge-file entry and finishes its setup.
405 * If the name has a path portion then we check to see if it refers to a
406 * parent directory of the first transfer dir. If it does, we scan all the
407 * dirs from that point through the parent dir of the transfer dir looking
408 * for the per-dir merge-file in each one. */
409 static BOOL
setup_merge_file(int mergelist_num
, filter_rule
*ex
,
410 filter_rule_list
*lp
)
412 char buf
[MAXPATHLEN
];
413 char *x
, *y
, *pat
= ex
->pattern
;
416 if (!(x
= parse_merge_name(pat
, NULL
, 0)) || *x
!= '/')
419 if (DEBUG_GTE(FILTER
, 2)) {
420 rprintf(FINFO
, "[%s] performing parent_dirscan for mergelist #%d%s\n",
421 who_am_i(), mergelist_num
, lp
->debug_type
);
425 ex
->pattern
= strdup(y
+1);
429 strlcpy(buf
, x
, MAXPATHLEN
);
431 pathjoin(buf
, MAXPATHLEN
, dirbuf
, x
);
433 len
= clean_fname(buf
, CFN_COLLAPSE_DOT_DOT_DIRS
);
434 if (len
!= 1 && len
< MAXPATHLEN
-1) {
438 /* This ensures that the specified dir is a parent of the transfer. */
439 for (x
= buf
, y
= dirbuf
; *x
&& *x
== *y
; x
++, y
++) {}
441 y
+= strlen(y
); /* nope -- skip the scan */
443 parent_dirscan
= True
;
445 char save
[MAXPATHLEN
];
446 strlcpy(save
, y
, MAXPATHLEN
);
448 dirbuf_len
= y
- dirbuf
;
449 strlcpy(x
, ex
->pattern
, MAXPATHLEN
- (x
- buf
));
450 parse_filter_file(lp
, buf
, ex
, XFLG_ANCHORED2ABS
);
451 if (ex
->rflags
& FILTRULE_NO_INHERIT
) {
452 /* Free the undesired rules to clean up any per-dir
453 * mergelists they defined. Otherwise pop_local_filters
454 * may crash trying to restore nonexistent state for
455 * those mergelists. */
456 free_filters(lp
->head
);
460 strlcpy(y
, save
, MAXPATHLEN
);
461 while ((*x
++ = *y
++) != '/') {}
463 parent_dirscan
= False
;
464 if (DEBUG_GTE(FILTER
, 2)) {
465 rprintf(FINFO
, "[%s] completed parent_dirscan for mergelist #%d%s\n",
466 who_am_i(), mergelist_num
, lp
->debug_type
);
472 struct local_filter_state
{
474 filter_rule_list mergelists
[1];
477 /* Each time rsync changes to a new directory it call this function to
478 * handle all the per-dir merge-files. The "dir" value is the current path
479 * relative to curr_dir (which might not be null-terminated). We copy it
480 * into dirbuf so that we can easily append a file name on the end. */
481 void *push_local_filters(const char *dir
, unsigned int dirlen
)
483 struct local_filter_state
*push
;
486 set_filter_dir(dir
, dirlen
);
487 if (DEBUG_GTE(FILTER
, 2)) {
488 rprintf(FINFO
, "[%s] pushing local filters for %s\n",
492 if (!mergelist_cnt
) {
493 /* No old state to save and no new merge files to push. */
497 push
= (struct local_filter_state
*)new_array(char,
498 sizeof (struct local_filter_state
)
499 + (mergelist_cnt
-1) * sizeof (filter_rule_list
));
501 out_of_memory("push_local_filters");
503 push
->mergelist_cnt
= mergelist_cnt
;
504 for (i
= 0; i
< mergelist_cnt
; i
++) {
505 filter_rule
*ex
= mergelist_parents
[i
];
508 memcpy(&push
->mergelists
[i
], ex
->u
.mergelist
, sizeof (filter_rule_list
));
511 /* Note: parse_filter_file() might increase mergelist_cnt, so keep
512 * this loop separate from the above loop. */
513 for (i
= 0; i
< mergelist_cnt
; i
++) {
514 filter_rule
*ex
= mergelist_parents
[i
];
515 filter_rule_list
*lp
;
518 lp
= ex
->u
.mergelist
;
520 if (DEBUG_GTE(FILTER
, 2)) {
521 rprintf(FINFO
, "[%s] pushing mergelist #%d%s\n",
522 who_am_i(), i
, lp
->debug_type
);
525 lp
->tail
= NULL
; /* Switch any local rules to inherited. */
526 if (ex
->rflags
& FILTRULE_NO_INHERIT
)
529 if (ex
->rflags
& FILTRULE_FINISH_SETUP
) {
530 ex
->rflags
&= ~FILTRULE_FINISH_SETUP
;
531 if (setup_merge_file(i
, ex
, lp
))
532 set_filter_dir(dir
, dirlen
);
535 if (strlcpy(dirbuf
+ dirbuf_len
, ex
->pattern
,
536 MAXPATHLEN
- dirbuf_len
) < MAXPATHLEN
- dirbuf_len
) {
537 parse_filter_file(lp
, dirbuf
, ex
,
540 io_error
|= IOERR_GENERAL
;
542 "cannot add local filter rules in long-named directory: %s\n",
545 dirbuf
[dirbuf_len
] = '\0';
551 void pop_local_filters(void *mem
)
553 struct local_filter_state
*pop
= (struct local_filter_state
*)mem
;
555 int old_mergelist_cnt
= pop
? pop
->mergelist_cnt
: 0;
557 if (DEBUG_GTE(FILTER
, 2))
558 rprintf(FINFO
, "[%s] popping local filters\n", who_am_i());
560 for (i
= mergelist_cnt
; i
-- > 0; ) {
561 filter_rule
*ex
= mergelist_parents
[i
];
562 filter_rule_list
*lp
;
565 lp
= ex
->u
.mergelist
;
567 if (DEBUG_GTE(FILTER
, 2)) {
568 rprintf(FINFO
, "[%s] popping mergelist #%d%s\n",
569 who_am_i(), i
, lp
->debug_type
);
573 if (i
>= old_mergelist_cnt
&& lp
->head
) {
574 /* This mergelist does not exist in the state to be restored, but it
575 * still has inherited rules. This can sometimes happen if a per-dir
576 * merge file calls setup_merge_file() in push_local_filters() and that
577 * leaves some inherited rules that aren't in the pushed list state. */
578 if (DEBUG_GTE(FILTER
, 2)) {
579 rprintf(FINFO
, "[%s] freeing parent_dirscan filters of mergelist #%d%s\n",
580 who_am_i(), i
, ex
->u
.mergelist
->debug_type
);
587 return; /* No state to restore. */
589 for (i
= 0; i
< old_mergelist_cnt
; i
++) {
590 filter_rule
*ex
= mergelist_parents
[i
];
593 memcpy(ex
->u
.mergelist
, &pop
->mergelists
[i
], sizeof (filter_rule_list
));
599 void change_local_filter_dir(const char *dname
, int dlen
, int dir_depth
)
601 static int cur_depth
= -1;
602 static void *filt_array
[MAXPATHLEN
/2+1];
605 for ( ; cur_depth
>= 0; cur_depth
--) {
606 if (filt_array
[cur_depth
]) {
607 pop_local_filters(filt_array
[cur_depth
]);
608 filt_array
[cur_depth
] = NULL
;
614 assert(dir_depth
< MAXPATHLEN
/2+1);
616 for ( ; cur_depth
>= dir_depth
; cur_depth
--) {
617 if (filt_array
[cur_depth
]) {
618 pop_local_filters(filt_array
[cur_depth
]);
619 filt_array
[cur_depth
] = NULL
;
623 cur_depth
= dir_depth
;
624 filt_array
[cur_depth
] = push_local_filters(dname
, dlen
);
627 static int rule_matches(const char *fname
, filter_rule
*ex
, int name_flags
)
629 int slash_handling
, str_cnt
= 0, anchored_match
= 0;
630 int ret_match
= ex
->rflags
& FILTRULE_NEGATE
? 0 : 1;
631 char *p
, *pattern
= ex
->pattern
;
632 const char *strings
[16]; /* more than enough */
633 const char *name
= fname
+ (*fname
== '/');
638 if (!(name_flags
& NAME_IS_XATTR
) ^ !(ex
->rflags
& FILTRULE_XATTR
))
641 if (!ex
->u
.slash_cnt
&& !(ex
->rflags
& FILTRULE_WILD2
)) {
642 /* If the pattern does not have any slashes AND it does
643 * not have a "**" (which could match a slash), then we
644 * just match the name portion of the path. */
645 if ((p
= strrchr(name
,'/')) != NULL
)
647 } else if (ex
->rflags
& FILTRULE_ABS_PATH
&& *fname
!= '/'
648 && curr_dir_len
> module_dirlen
+ 1) {
649 /* If we're matching against an absolute-path pattern,
650 * we need to prepend our full path info. */
651 strings
[str_cnt
++] = curr_dir
+ module_dirlen
+ 1;
652 strings
[str_cnt
++] = "/";
653 } else if (ex
->rflags
& FILTRULE_WILD2_PREFIX
&& *fname
!= '/') {
654 /* Allow "**"+"/" to match at the start of the string. */
655 strings
[str_cnt
++] = "/";
657 strings
[str_cnt
++] = name
;
658 if (name_flags
& NAME_IS_DIR
) {
659 /* Allow a trailing "/"+"***" to match the directory. */
660 if (ex
->rflags
& FILTRULE_WILD3_SUFFIX
)
661 strings
[str_cnt
++] = "/";
662 } else if (ex
->rflags
& FILTRULE_DIRECTORY
)
664 strings
[str_cnt
] = NULL
;
666 if (*pattern
== '/') {
671 if (!anchored_match
&& ex
->u
.slash_cnt
672 && !(ex
->rflags
& FILTRULE_WILD2
)) {
673 /* A non-anchored match with an infix slash and no "**"
674 * needs to match the last slash_cnt+1 name elements. */
675 slash_handling
= ex
->u
.slash_cnt
+ 1;
676 } else if (!anchored_match
&& !(ex
->rflags
& FILTRULE_WILD2_PREFIX
)
677 && ex
->rflags
& FILTRULE_WILD2
) {
678 /* A non-anchored match with an infix or trailing "**" (but not
679 * a prefixed "**") needs to try matching after every slash. */
682 /* The pattern matches only at the start of the path or name. */
686 if (ex
->rflags
& FILTRULE_WILD
) {
687 if (wildmatch_array(pattern
, strings
, slash_handling
))
689 } else if (str_cnt
> 1) {
690 if (litmatch_array(pattern
, strings
, slash_handling
))
692 } else if (anchored_match
) {
693 if (strcmp(name
, pattern
) == 0)
696 int l1
= strlen(name
);
697 int l2
= strlen(pattern
);
699 strcmp(name
+(l1
-l2
),pattern
) == 0 &&
700 (l1
==l2
|| name
[l1
-(l2
+1)] == '/')) {
708 static void report_filter_result(enum logcode code
, char const *name
,
709 filter_rule
const *ent
,
710 int name_flags
, const char *type
)
712 /* If a trailing slash is present to match only directories,
713 * then it is stripped out by add_rule(). So as a special
714 * case we add it back in here. */
716 if (DEBUG_GTE(FILTER
, 1)) {
717 static char *actions
[2][2]
718 = { {"show", "hid"}, {"risk", "protect"} };
719 const char *w
= who_am_i();
720 const char *t
= name_flags
& NAME_IS_XATTR
? "xattr"
721 : name_flags
& NAME_IS_DIR
? "directory"
723 rprintf(code
, "[%s] %sing %s %s because of pattern %s%s%s\n",
724 w
, actions
[*w
!='s'][!(ent
->rflags
& FILTRULE_INCLUDE
)],
725 t
, name
, ent
->pattern
,
726 ent
->rflags
& FILTRULE_DIRECTORY
? "/" : "", type
);
730 /* This function is used to check if a file should be included/excluded
731 * from the list of files based on its name and type etc. The value of
732 * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
733 int name_is_excluded(const char *fname
, int name_flags
, int filter_level
)
735 if (daemon_filter_list
.head
&& check_filter(&daemon_filter_list
, FLOG
, fname
, name_flags
) < 0) {
736 if (!(name_flags
& NAME_IS_XATTR
))
741 if (filter_level
!= ALL_FILTERS
)
744 if (filter_list
.head
&& check_filter(&filter_list
, FINFO
, fname
, name_flags
) < 0)
750 /* Return -1 if file "name" is defined to be excluded by the specified
751 * exclude list, 1 if it is included, and 0 if it was not matched. */
752 int check_filter(filter_rule_list
*listp
, enum logcode code
,
753 const char *name
, int name_flags
)
757 for (ent
= listp
->head
; ent
; ent
= ent
->next
) {
758 if (ignore_perishable
&& ent
->rflags
& FILTRULE_PERISHABLE
)
760 if (ent
->rflags
& FILTRULE_PERDIR_MERGE
) {
761 int rc
= check_filter(ent
->u
.mergelist
, code
, name
, name_flags
);
766 if (ent
->rflags
& FILTRULE_CVS_IGNORE
) {
767 int rc
= check_filter(&cvs_filter_list
, code
, name
, name_flags
);
772 if (rule_matches(name
, ent
, name_flags
)) {
773 report_filter_result(code
, name
, ent
, name_flags
, listp
->debug_type
);
774 return ent
->rflags
& FILTRULE_INCLUDE
? 1 : -1;
781 #define RULE_STRCMP(s,r) rule_strcmp((s), (r), sizeof (r) - 1)
783 static const uchar
*rule_strcmp(const uchar
*str
, const char *rule
, int rule_len
)
785 if (strncmp((char*)str
, rule
, rule_len
) != 0)
787 if (isspace(str
[rule_len
]) || str
[rule_len
] == '_' || !str
[rule_len
])
788 return str
+ rule_len
- 1;
789 if (str
[rule_len
] == ',')
790 return str
+ rule_len
;
794 #define FILTRULES_FROM_CONTAINER (FILTRULE_ABS_PATH | FILTRULE_INCLUDE \
795 | FILTRULE_DIRECTORY | FILTRULE_NEGATE \
796 | FILTRULE_PERISHABLE)
798 /* Gets the next include/exclude rule from *rulestr_ptr and advances
799 * *rulestr_ptr to point beyond it. Stores the pattern's start (within
800 * *rulestr_ptr) and length in *pat_ptr and *pat_len_ptr, and returns a newly
801 * allocated filter_rule containing the rest of the information. Returns
802 * NULL if there are no more rules in the input.
804 * The template provides defaults for the new rule to inherit, and the
805 * template rflags and the xflags additionally affect parsing. */
806 static filter_rule
*parse_rule_tok(const char **rulestr_ptr
,
807 const filter_rule
*template, int xflags
,
808 const char **pat_ptr
, unsigned int *pat_len_ptr
)
810 const uchar
*s
= (const uchar
*)*rulestr_ptr
;
814 if (template->rflags
& FILTRULE_WORD_SPLIT
) {
815 /* Skip over any initial whitespace. */
818 /* Update to point to real start of rule. */
819 *rulestr_ptr
= (const char *)s
;
824 if (!(rule
= new0(filter_rule
)))
825 out_of_memory("parse_rule_tok");
827 /* Inherit from the template. Don't inherit FILTRULES_SIDES; we check
829 rule
->rflags
= template->rflags
& FILTRULES_FROM_CONTAINER
;
831 /* Figure out what kind of a filter rule "s" is pointing at. Note
832 * that if FILTRULE_NO_PREFIXES is set, the rule is either an include
833 * or an exclude based on the inheritance of the FILTRULE_INCLUDE
834 * flag (above). XFLG_OLD_PREFIXES indicates a compatibility mode
835 * for old include/exclude patterns where just "+ " and "- " are
836 * allowed as optional prefixes. */
837 if (template->rflags
& FILTRULE_NO_PREFIXES
) {
838 if (*s
== '!' && template->rflags
& FILTRULE_CVS_IGNORE
)
839 rule
->rflags
|= FILTRULE_CLEAR_LIST
; /* Tentative! */
840 } else if (xflags
& XFLG_OLD_PREFIXES
) {
841 if (*s
== '-' && s
[1] == ' ') {
842 rule
->rflags
&= ~FILTRULE_INCLUDE
;
844 } else if (*s
== '+' && s
[1] == ' ') {
845 rule
->rflags
|= FILTRULE_INCLUDE
;
847 } else if (*s
== '!')
848 rule
->rflags
|= FILTRULE_CLEAR_LIST
; /* Tentative! */
851 BOOL prefix_specifies_side
= False
;
854 if ((s
= RULE_STRCMP(s
, "clear")) != NULL
)
858 if ((s
= RULE_STRCMP(s
, "dir-merge")) != NULL
)
862 if ((s
= RULE_STRCMP(s
, "exclude")) != NULL
)
866 if ((s
= RULE_STRCMP(s
, "hide")) != NULL
)
870 if ((s
= RULE_STRCMP(s
, "include")) != NULL
)
874 if ((s
= RULE_STRCMP(s
, "merge")) != NULL
)
878 if ((s
= RULE_STRCMP(s
, "protect")) != NULL
)
882 if ((s
= RULE_STRCMP(s
, "risk")) != NULL
)
886 if ((s
= RULE_STRCMP(s
, "show")) != NULL
)
897 rule
->rflags
|= FILTRULE_PERDIR_MERGE
898 | FILTRULE_FINISH_SETUP
;
901 rule
->rflags
|= FILTRULE_MERGE_FILE
;
904 rule
->rflags
|= FILTRULE_INCLUDE
;
909 rule
->rflags
|= FILTRULE_INCLUDE
;
912 rule
->rflags
|= FILTRULE_SENDER_SIDE
;
913 prefix_specifies_side
= True
;
916 rule
->rflags
|= FILTRULE_INCLUDE
;
919 rule
->rflags
|= FILTRULE_RECEIVER_SIDE
;
920 prefix_specifies_side
= True
;
923 rule
->rflags
|= FILTRULE_CLEAR_LIST
;
926 rprintf(FERROR
, "Unknown filter rule: `%s'\n", *rulestr_ptr
);
927 exit_cleanup(RERR_SYNTAX
);
929 while (ch
!= '!' && *++s
&& *s
!= ' ' && *s
!= '_') {
930 if (template->rflags
& FILTRULE_WORD_SPLIT
&& isspace(*s
)) {
938 "invalid modifier '%c' at position %d in filter rule: %s\n",
939 *s
, (int)(s
- (const uchar
*)*rulestr_ptr
), *rulestr_ptr
);
940 exit_cleanup(RERR_SYNTAX
);
942 if (!BITS_SETnUNSET(rule
->rflags
, FILTRULE_MERGE_FILE
, FILTRULE_NO_PREFIXES
))
944 rule
->rflags
|= FILTRULE_NO_PREFIXES
;
947 if (!BITS_SETnUNSET(rule
->rflags
, FILTRULE_MERGE_FILE
, FILTRULE_NO_PREFIXES
))
949 rule
->rflags
|= FILTRULE_NO_PREFIXES
953 rule
->rflags
|= FILTRULE_ABS_PATH
;
956 /* Negation really goes with the pattern, so it
957 * isn't useful as a merge-file default. */
958 if (rule
->rflags
& FILTRULE_MERGE_FILE
)
960 rule
->rflags
|= FILTRULE_NEGATE
;
963 if (rule
->rflags
& FILTRULE_NO_PREFIXES
|| prefix_specifies_side
)
965 rule
->rflags
|= FILTRULE_NO_PREFIXES
966 | FILTRULE_WORD_SPLIT
967 | FILTRULE_NO_INHERIT
968 | FILTRULE_CVS_IGNORE
;
971 if (!(rule
->rflags
& FILTRULE_MERGE_FILE
))
973 rule
->rflags
|= FILTRULE_EXCLUDE_SELF
;
976 if (!(rule
->rflags
& FILTRULE_MERGE_FILE
))
978 rule
->rflags
|= FILTRULE_NO_INHERIT
;
981 rule
->rflags
|= FILTRULE_PERISHABLE
;
984 if (prefix_specifies_side
)
986 rule
->rflags
|= FILTRULE_RECEIVER_SIDE
;
989 if (prefix_specifies_side
)
991 rule
->rflags
|= FILTRULE_SENDER_SIDE
;
994 if (!(rule
->rflags
& FILTRULE_MERGE_FILE
))
996 rule
->rflags
|= FILTRULE_WORD_SPLIT
;
999 rule
->rflags
|= FILTRULE_XATTR
;
1000 saw_xattr_filter
= 1;
1007 if (template->rflags
& FILTRULES_SIDES
) {
1008 if (rule
->rflags
& FILTRULES_SIDES
) {
1009 /* The filter and template both specify side(s). This
1010 * is dodgy (and won't work correctly if the template is
1011 * a one-sided per-dir merge rule), so reject it. */
1013 "specified-side merge file contains specified-side filter: %s\n",
1015 exit_cleanup(RERR_SYNTAX
);
1017 rule
->rflags
|= template->rflags
& FILTRULES_SIDES
;
1020 if (template->rflags
& FILTRULE_WORD_SPLIT
) {
1021 const uchar
*cp
= s
;
1022 /* Token ends at whitespace or the end of the string. */
1023 while (!isspace(*cp
) && *cp
!= '\0')
1027 len
= strlen((char*)s
);
1029 if (rule
->rflags
& FILTRULE_CLEAR_LIST
) {
1030 if (!(rule
->rflags
& FILTRULE_NO_PREFIXES
)
1031 && !(xflags
& XFLG_OLD_PREFIXES
) && len
) {
1033 "'!' rule has trailing characters: %s\n", *rulestr_ptr
);
1034 exit_cleanup(RERR_SYNTAX
);
1037 rule
->rflags
&= ~FILTRULE_CLEAR_LIST
;
1038 } else if (!len
&& !(rule
->rflags
& FILTRULE_CVS_IGNORE
)) {
1039 rprintf(FERROR
, "unexpected end of filter rule: %s\n", *rulestr_ptr
);
1040 exit_cleanup(RERR_SYNTAX
);
1043 /* --delete-excluded turns an un-modified include/exclude into a sender-side rule. */
1045 && !(rule
->rflags
& (FILTRULES_SIDES
|FILTRULE_MERGE_FILE
|FILTRULE_PERDIR_MERGE
)))
1046 rule
->rflags
|= FILTRULE_SENDER_SIDE
;
1048 *pat_ptr
= (const char *)s
;
1050 *rulestr_ptr
= *pat_ptr
+ len
;
1054 static char default_cvsignore
[] =
1055 /* These default ignored items come from the CVS manual. */
1056 "RCS SCCS CVS CVS.adm RCSLOG cvslog.* tags TAGS"
1057 " .make.state .nse_depinfo *~ #* .#* ,* _$* *$"
1058 " *.old *.bak *.BAK *.orig *.rej .del-*"
1059 " *.a *.olb *.o *.obj *.so *.exe"
1060 " *.Z *.elc *.ln core"
1061 /* The rest we added to suit ourself. */
1062 " .svn/ .git/ .hg/ .bzr/";
1064 static void get_cvs_excludes(uint32 rflags
)
1066 static int initialized
= 0;
1067 char *p
, fname
[MAXPATHLEN
];
1073 parse_filter_str(&cvs_filter_list
, default_cvsignore
,
1074 rule_template(rflags
| (protocol_version
>= 30 ? FILTRULE_PERISHABLE
: 0)),
1077 p
= module_id
>= 0 && lp_use_chroot(module_id
) ? "/" : getenv("HOME");
1078 if (p
&& pathjoin(fname
, MAXPATHLEN
, p
, ".cvsignore") < MAXPATHLEN
)
1079 parse_filter_file(&cvs_filter_list
, fname
, rule_template(rflags
), 0);
1081 parse_filter_str(&cvs_filter_list
, getenv("CVSIGNORE"), rule_template(rflags
), 0);
1084 const filter_rule
*rule_template(uint32 rflags
)
1086 static filter_rule
template; /* zero-initialized */
1087 template.rflags
= rflags
;
1091 void parse_filter_str(filter_rule_list
*listp
, const char *rulestr
,
1092 const filter_rule
*template, int xflags
)
1096 unsigned int pat_len
;
1104 /* Remember that the returned string is NOT '\0' terminated! */
1105 if (!(rule
= parse_rule_tok(&rulestr
, template, xflags
, &pat
, &pat_len
)))
1108 if (pat_len
>= MAXPATHLEN
) {
1109 rprintf(FERROR
, "discarding over-long filter: %.*s\n",
1116 new_rflags
= rule
->rflags
;
1117 if (new_rflags
& FILTRULE_CLEAR_LIST
) {
1118 if (DEBUG_GTE(FILTER
, 2)) {
1120 "[%s] clearing filter list%s\n",
1121 who_am_i(), listp
->debug_type
);
1123 pop_filter_list(listp
);
1128 if (new_rflags
& FILTRULE_MERGE_FILE
) {
1133 if (new_rflags
& FILTRULE_EXCLUDE_SELF
) {
1135 filter_rule
*excl_self
;
1137 if (!(excl_self
= new0(filter_rule
)))
1138 out_of_memory("parse_filter_str");
1139 /* Find the beginning of the basename and add an exclude for it. */
1140 for (name
= pat
+ pat_len
; name
> pat
&& name
[-1] != '/'; name
--) {}
1141 add_rule(listp
, name
, (pat
+ pat_len
) - name
, excl_self
, 0);
1142 rule
->rflags
&= ~FILTRULE_EXCLUDE_SELF
;
1144 if (new_rflags
& FILTRULE_PERDIR_MERGE
) {
1145 if (parent_dirscan
) {
1147 unsigned int len
= pat_len
;
1148 if ((p
= parse_merge_name(pat
, &len
, module_dirlen
)))
1149 add_rule(listp
, p
, len
, rule
, 0);
1156 unsigned int len
= pat_len
;
1157 if ((p
= parse_merge_name(pat
, &len
, 0)))
1158 parse_filter_file(listp
, p
, rule
, XFLG_FATAL_ERRORS
);
1164 add_rule(listp
, pat
, pat_len
, rule
, xflags
);
1166 if (new_rflags
& FILTRULE_CVS_IGNORE
1167 && !(new_rflags
& FILTRULE_MERGE_FILE
))
1168 get_cvs_excludes(new_rflags
);
1172 void parse_filter_file(filter_rule_list
*listp
, const char *fname
, const filter_rule
*template, int xflags
)
1175 char line
[BIGPATHBUFLEN
];
1176 char *eob
= line
+ sizeof line
- 1;
1177 BOOL word_split
= (template->rflags
& FILTRULE_WORD_SPLIT
) != 0;
1179 if (!fname
|| !*fname
)
1182 if (*fname
!= '-' || fname
[1] || am_server
) {
1183 if (daemon_filter_list
.head
) {
1184 strlcpy(line
, fname
, sizeof line
);
1185 clean_fname(line
, CFN_COLLAPSE_DOT_DOT_DIRS
);
1186 if (check_filter(&daemon_filter_list
, FLOG
, line
, 0) < 0)
1189 fp
= fopen(line
, "rb");
1191 fp
= fopen(fname
, "rb");
1195 if (DEBUG_GTE(FILTER
, 2)) {
1196 rprintf(FINFO
, "[%s] parse_filter_file(%s,%x,%x)%s\n",
1197 who_am_i(), fname
, template->rflags
, xflags
,
1198 fp
? "" : " [not found]");
1202 if (xflags
& XFLG_FATAL_ERRORS
) {
1203 rsyserr(FERROR
, errno
,
1204 "failed to open %sclude file %s",
1205 template->rflags
& FILTRULE_INCLUDE
? "in" : "ex",
1207 exit_cleanup(RERR_FILEIO
);
1211 dirbuf
[dirbuf_len
] = '\0';
1215 int ch
, overflow
= 0;
1217 if ((ch
= getc(fp
)) == EOF
) {
1218 if (ferror(fp
) && errno
== EINTR
) {
1224 if (word_split
&& isspace(ch
))
1226 if (eol_nulls
? !ch
: (ch
== '\n' || ch
== '\r'))
1234 rprintf(FERROR
, "discarding over-long filter: %s...\n", line
);
1238 /* Skip an empty token and (when line parsing) comments. */
1239 if (*line
&& (word_split
|| (*line
!= ';' && *line
!= '#')))
1240 parse_filter_str(listp
, line
, template, xflags
);
1247 /* If the "for_xfer" flag is set, the prefix is made compatible with the
1248 * current protocol_version (if possible) or a NULL is returned (if not
1250 char *get_rule_prefix(filter_rule
*rule
, const char *pat
, int for_xfer
,
1251 unsigned int *plen_ptr
)
1253 static char buf
[MAX_RULE_PREFIX
+1];
1255 int legal_len
= for_xfer
&& protocol_version
< 29 ? 1 : MAX_RULE_PREFIX
-1;
1257 if (rule
->rflags
& FILTRULE_PERDIR_MERGE
) {
1261 } else if (rule
->rflags
& FILTRULE_INCLUDE
)
1263 else if (legal_len
!= 1
1264 || ((*pat
== '-' || *pat
== '+') && pat
[1] == ' '))
1269 if (rule
->rflags
& FILTRULE_ABS_PATH
)
1271 if (rule
->rflags
& FILTRULE_NEGATE
)
1273 if (rule
->rflags
& FILTRULE_CVS_IGNORE
)
1276 if (rule
->rflags
& FILTRULE_NO_INHERIT
)
1278 if (rule
->rflags
& FILTRULE_WORD_SPLIT
)
1280 if (rule
->rflags
& FILTRULE_NO_PREFIXES
) {
1281 if (rule
->rflags
& FILTRULE_INCLUDE
)
1287 if (rule
->rflags
& FILTRULE_EXCLUDE_SELF
)
1289 if (rule
->rflags
& FILTRULE_XATTR
)
1291 if (rule
->rflags
& FILTRULE_SENDER_SIDE
1292 && (!for_xfer
|| protocol_version
>= 29))
1294 if (rule
->rflags
& FILTRULE_RECEIVER_SIDE
1295 && (!for_xfer
|| protocol_version
>= 29
1296 || (delete_excluded
&& am_sender
)))
1298 if (rule
->rflags
& FILTRULE_PERISHABLE
) {
1299 if (!for_xfer
|| protocol_version
>= 30)
1304 if (op
- buf
> legal_len
)
1310 *plen_ptr
= op
- buf
;
1314 static void send_rules(int f_out
, filter_rule_list
*flp
)
1316 filter_rule
*ent
, *prev
= NULL
;
1318 for (ent
= flp
->head
; ent
; ent
= ent
->next
) {
1319 unsigned int len
, plen
, dlen
;
1323 /* Note we need to check delete_excluded here in addition to
1324 * the code in parse_rule_tok() because some rules may have
1325 * been added before we found the --delete-excluded option.
1326 * We must also elide any CVS merge-file rules to avoid a
1327 * backward compatibility problem, and we elide any no-prefix
1328 * merge files as an optimization (since they can only have
1329 * include/exclude rules). */
1330 if (ent
->rflags
& FILTRULE_SENDER_SIDE
)
1331 elide
= am_sender
? 1 : -1;
1332 if (ent
->rflags
& FILTRULE_RECEIVER_SIDE
)
1333 elide
= elide
? 0 : am_sender
? -1 : 1;
1334 else if (delete_excluded
&& !elide
1335 && (!(ent
->rflags
& FILTRULE_PERDIR_MERGE
)
1336 || ent
->rflags
& FILTRULE_NO_PREFIXES
))
1337 elide
= am_sender
? 1 : -1;
1340 prev
->next
= ent
->next
;
1342 flp
->head
= ent
->next
;
1347 if (ent
->rflags
& FILTRULE_CVS_IGNORE
1348 && !(ent
->rflags
& FILTRULE_MERGE_FILE
)) {
1349 int f
= am_sender
|| protocol_version
< 29 ? f_out
: -2;
1350 send_rules(f
, &cvs_filter_list
);
1354 p
= get_rule_prefix(ent
, ent
->pattern
, 1, &plen
);
1357 "filter rules are too modern for remote rsync.\n");
1358 exit_cleanup(RERR_PROTOCOL
);
1362 len
= strlen(ent
->pattern
);
1363 dlen
= ent
->rflags
& FILTRULE_DIRECTORY
? 1 : 0;
1364 if (!(plen
+ len
+ dlen
))
1366 write_int(f_out
, plen
+ len
+ dlen
);
1368 write_buf(f_out
, p
, plen
);
1369 write_buf(f_out
, ent
->pattern
, len
);
1371 write_byte(f_out
, '/');
1376 /* This is only called by the client. */
1377 void send_filter_list(int f_out
)
1379 int receiver_wants_list
= prune_empty_dirs
1380 || (delete_mode
&& (!delete_excluded
|| protocol_version
>= 29));
1382 if (local_server
|| (am_sender
&& !receiver_wants_list
))
1384 if (cvs_exclude
&& am_sender
) {
1385 if (protocol_version
>= 29)
1386 parse_filter_str(&filter_list
, ":C", rule_template(0), 0);
1387 parse_filter_str(&filter_list
, "-C", rule_template(0), 0);
1390 send_rules(f_out
, &filter_list
);
1393 write_int(f_out
, 0);
1396 if (!am_sender
|| protocol_version
< 29)
1397 parse_filter_str(&filter_list
, ":C", rule_template(0), 0);
1399 parse_filter_str(&filter_list
, "-C", rule_template(0), 0);
1403 /* This is only called by the server. */
1404 void recv_filter_list(int f_in
)
1406 char line
[BIGPATHBUFLEN
];
1407 int xflags
= protocol_version
>= 29 ? 0 : XFLG_OLD_PREFIXES
;
1408 int receiver_wants_list
= prune_empty_dirs
1410 && (!delete_excluded
|| protocol_version
>= 29));
1413 if (!local_server
&& (am_sender
|| receiver_wants_list
)) {
1414 while ((len
= read_int(f_in
)) != 0) {
1415 if (len
>= sizeof line
)
1416 overflow_exit("recv_rules");
1417 read_sbuf(f_in
, line
, len
);
1418 parse_filter_str(&filter_list
, line
, rule_template(0), xflags
);
1423 if (local_server
|| am_sender
|| protocol_version
< 29)
1424 parse_filter_str(&filter_list
, ":C", rule_template(0), 0);
1425 if (local_server
|| am_sender
)
1426 parse_filter_str(&filter_list
, "-C", rule_template(0), 0);
1429 if (local_server
) /* filter out any rules that aren't for us. */
1430 send_rules(-1, &filter_list
);