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