Debian Codegraph @

debian/patches/05_wildcard_matching

raw
1 From: Pekka Savola <[email protected]>
2 Date: 2000-09-26 06:08 UTC
3 Origin: https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=17847
4 (Though the original code needs to be patched to be case-insensitive.)
5
6 --- a/hosts_access.5
7 +++ b/hosts_access.5
8 @@ -89,6 +89,10 @@ An expression of the form `n.n.n.n/m.m.m
9 bitwise AND of the address and the `mask\'. For example, the net/mask
10 pattern `131.155.72.0/255.255.254.0\' matches every address in the
11 range `131.155.72.0\' through `131.155.73.255\'.
12 +.IP \(bu
13 +Wildcards `*\' and `?\' can be used to match hostnames or IP addresses. This
14 +method of matching cannot be used in conjunction with `net/mask\' matching,
15 +hostname matching beginning with `.\' or IP address matching ending with `.\'.
16 .SH WILDCARDS
17 The access control language supports explicit wildcards:
18 .IP ALL
19 --- a/hosts_access.c
20 +++ b/hosts_access.c
21 @@ -82,6 +82,7 @@ static int client_match();
22 static int host_match();
23 static int string_match();
24 static int masked_match();
25 +static int match_pattern_ylo();
26
27 /* Size of logical line buffer. */
28
29 @@ -289,6 +290,11 @@ char *string;
30 {
31 int n;
32
33 +#ifndef DISABLE_WILDCARD_MATCHING
34 + if (strchr(tok, '*') || strchr(tok,'?')) { /* contains '*' or '?' */
35 + return (match_pattern_ylo(string,tok));
36 + } else
37 +#endif
38 if (tok[0] == '.') { /* suffix */
39 n = strlen(string) - strlen(tok);
40 return (n > 0 && STR_EQ(tok, string + n));
41 @@ -329,3 +335,78 @@ char *string;
42 }
43 return ((addr & mask) == net);
44 }
45 +
46 +#ifndef DISABLE_WILDCARD_MATCHING
47 +/* Note: this feature has been adapted in a pretty straightforward way
48 + from Tatu Ylonen's last SSH version under free license by
49 + Pekka Savola <[email protected]>.
50 +
51 + Copyright (c) 1995 Tatu Ylonen <[email protected]>, Espoo, Finland
52 +*/
53 +
54 +/* Returns true if the given string matches the pattern (which may contain
55 + ? and * as wildcards), and zero if it does not match. */
56 +
57 +static int match_pattern_ylo(const char *s, const char *pattern)
58 +{
59 + char src;
60 + char pat;
61 + while (1)
62 + {
63 + /* If at end of pattern, accept if also at end of string. */
64 + if (!*pattern)
65 + return !*s;
66 +
67 + /* Process '*'. */
68 + if (*pattern == '*')
69 + {
70 + /* Skip the asterisk. */
71 + pattern++;
72 +
73 + /* If at end of pattern, accept immediately. */
74 + if (!*pattern)
75 + return 1;
76 +
77 + /* If next character in pattern is known, optimize. */
78 + if (*pattern != '?' && *pattern != '*')
79 + {
80 + /* Look instances of the next character in pattern, and try
81 + to match starting from those. */
82 + pat = *pattern;
83 + for (; *s; s++) {
84 + src = *s;
85 + if (toupper(src) == toupper(pat) &&
86 + match_pattern_ylo(s + 1, pattern + 1))
87 + return 1;
88 + }
89 + /* Failed. */
90 + return 0;
91 + }
92 +
93 + /* Move ahead one character at a time and try to match at each
94 + position. */
95 + for (; *s; s++)
96 + if (match_pattern_ylo(s, pattern))
97 + return 1;
98 + /* Failed. */
99 + return 0;
100 + }
101 +
102 + /* There must be at least one more character in the string. If we are
103 + at the end, fail. */
104 + if (!*s)
105 + return 0;
106 +
107 + /* Check if the next character of the string is acceptable. */
108 + pat = *pattern;
109 + src = *s;
110 + if (*pattern != '?' && toupper(pat) != toupper(src))
111 + return 0;
112 +
113 + /* Move to the next character, both in string and in pattern. */
114 + s++;
115 + pattern++;
116 + }
117 + /*NOTREACHED*/
118 +}
119 +#endif /* DISABLE_WILDCARD_MATCHING */