blob: 736047df1720da90dedcc5c1b3cc22f240f221d5 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11003 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * Simple pattern matching, with '*' and '?' as wildcards.
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Millere4340be2000-09-16 13:29:08 +11007 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110012 */
Ben Lindstromb9be60a2001-03-11 01:49:19 +000013/*
14 * Copyright (c) 2000 Markus Friedl. All rights reserved.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100036
37#include "includes.h"
Damien Millerc7b06362006-03-15 11:53:45 +110038
39#include <ctype.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100040
Ben Lindstrom226cfa02001-01-22 05:34:40 +000041#include "match.h"
Ben Lindstromb9be60a2001-03-11 01:49:19 +000042#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100043
Damien Miller5428f641999-11-25 11:54:57 +110044/*
45 * Returns true if the given string matches the pattern (which may contain ?
46 * and * as wildcards), and zero if it does not match.
47 */
Damien Miller95def091999-11-25 00:26:21 +110048
Damien Miller4af51302000-04-16 11:18:38 +100049int
Damien Miller95def091999-11-25 00:26:21 +110050match_pattern(const char *s, const char *pattern)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051{
Damien Miller95def091999-11-25 00:26:21 +110052 for (;;) {
53 /* If at end of pattern, accept if also at end of string. */
54 if (!*pattern)
55 return !*s;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100056
Damien Miller95def091999-11-25 00:26:21 +110057 if (*pattern == '*') {
58 /* Skip the asterisk. */
59 pattern++;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100060
Damien Miller95def091999-11-25 00:26:21 +110061 /* If at end of pattern, accept immediately. */
62 if (!*pattern)
63 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064
Damien Miller95def091999-11-25 00:26:21 +110065 /* If next character in pattern is known, optimize. */
66 if (*pattern != '?' && *pattern != '*') {
Damien Miller5428f641999-11-25 11:54:57 +110067 /*
68 * Look instances of the next character in
69 * pattern, and try to match starting from
70 * those.
71 */
Damien Miller95def091999-11-25 00:26:21 +110072 for (; *s; s++)
73 if (*s == *pattern &&
74 match_pattern(s + 1, pattern + 1))
75 return 1;
76 /* Failed. */
77 return 0;
78 }
Damien Miller5428f641999-11-25 11:54:57 +110079 /*
80 * Move ahead one character at a time and try to
81 * match at each position.
82 */
Damien Miller95def091999-11-25 00:26:21 +110083 for (; *s; s++)
84 if (match_pattern(s, pattern))
85 return 1;
86 /* Failed. */
87 return 0;
88 }
Damien Miller5428f641999-11-25 11:54:57 +110089 /*
90 * There must be at least one more character in the string.
91 * If we are at the end, fail.
92 */
Damien Miller95def091999-11-25 00:26:21 +110093 if (!*s)
94 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100095
Damien Miller5428f641999-11-25 11:54:57 +110096 /* Check if the next character of the string is acceptable. */
Damien Miller95def091999-11-25 00:26:21 +110097 if (*pattern != '?' && *pattern != *s)
98 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100099
Damien Miller5428f641999-11-25 11:54:57 +1100100 /* Move to the next character, both in string and in pattern. */
Damien Miller95def091999-11-25 00:26:21 +1100101 s++;
102 pattern++;
103 }
104 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105}
Damien Miller450a7a12000-03-26 13:04:51 +1000106
107/*
Ben Lindstrom9eab2622001-12-06 18:06:05 +0000108 * Tries to match the string against the
Damien Miller450a7a12000-03-26 13:04:51 +1000109 * comma-separated sequence of subpatterns (each possibly preceded by ! to
Damien Millerd3a18572000-06-07 19:55:44 +1000110 * indicate negation). Returns -1 if negation matches, 1 if there is
111 * a positive match, 0 if there is no match at all.
Damien Miller450a7a12000-03-26 13:04:51 +1000112 */
113
114int
Ben Lindstrom9eab2622001-12-06 18:06:05 +0000115match_pattern_list(const char *string, const char *pattern, u_int len,
116 int dolower)
Damien Miller450a7a12000-03-26 13:04:51 +1000117{
118 char sub[1024];
119 int negated;
120 int got_positive;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000121 u_int i, subi;
Damien Miller450a7a12000-03-26 13:04:51 +1000122
123 got_positive = 0;
124 for (i = 0; i < len;) {
125 /* Check if the subpattern is negated. */
126 if (pattern[i] == '!') {
127 negated = 1;
128 i++;
129 } else
130 negated = 0;
131
132 /*
133 * Extract the subpattern up to a comma or end. Convert the
134 * subpattern to lowercase.
135 */
136 for (subi = 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100137 i < len && subi < sizeof(sub) - 1 && pattern[i] != ',';
138 subi++, i++)
Ben Lindstrom9eab2622001-12-06 18:06:05 +0000139 sub[subi] = dolower && isupper(pattern[i]) ?
Damien Miller9f0f5c62001-12-21 14:45:46 +1100140 tolower(pattern[i]) : pattern[i];
Damien Miller450a7a12000-03-26 13:04:51 +1000141 /* If subpattern too long, return failure (no match). */
142 if (subi >= sizeof(sub) - 1)
143 return 0;
144
145 /* If the subpattern was terminated by a comma, skip the comma. */
146 if (i < len && pattern[i] == ',')
147 i++;
148
149 /* Null-terminate the subpattern. */
150 sub[subi] = '\0';
151
Ben Lindstrom9eab2622001-12-06 18:06:05 +0000152 /* Try to match the subpattern against the string. */
153 if (match_pattern(string, sub)) {
Damien Miller450a7a12000-03-26 13:04:51 +1000154 if (negated)
Damien Millerd3a18572000-06-07 19:55:44 +1000155 return -1; /* Negative */
Damien Miller450a7a12000-03-26 13:04:51 +1000156 else
Damien Millerd3a18572000-06-07 19:55:44 +1000157 got_positive = 1; /* Positive */
Damien Miller450a7a12000-03-26 13:04:51 +1000158 }
159 }
160
161 /*
162 * Return success if got a positive match. If there was a negative
Damien Millerd3a18572000-06-07 19:55:44 +1000163 * match, we have already returned -1 and never get here.
Damien Miller450a7a12000-03-26 13:04:51 +1000164 */
165 return got_positive;
166}
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000167
Ben Lindstromf0c50292001-06-25 05:17:53 +0000168/*
Ben Lindstrom9eab2622001-12-06 18:06:05 +0000169 * Tries to match the host name (which must be in all lowercase) against the
170 * comma-separated sequence of subpatterns (each possibly preceded by ! to
171 * indicate negation). Returns -1 if negation matches, 1 if there is
172 * a positive match, 0 if there is no match at all.
173 */
174int
175match_hostname(const char *host, const char *pattern, u_int len)
176{
177 return match_pattern_list(host, pattern, len, 1);
178}
179
180/*
Ben Lindstromf0c50292001-06-25 05:17:53 +0000181 * returns 0 if we get a negative match for the hostname or the ip
182 * or if we get no match at all. returns 1 otherwise.
183 */
184int
185match_host_and_ip(const char *host, const char *ipaddr,
186 const char *patterns)
187{
188 int mhost, mip;
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000189
Ben Lindstromf0c50292001-06-25 05:17:53 +0000190 /* negative ipaddr match */
191 if ((mip = match_hostname(ipaddr, patterns, strlen(patterns))) == -1)
192 return 0;
193 /* negative hostname match */
194 if ((mhost = match_hostname(host, patterns, strlen(patterns))) == -1)
195 return 0;
196 /* no match at all */
197 if (mhost == 0 && mip == 0)
198 return 0;
199 return 1;
200}
201
202/*
Ben Lindstrom60260022001-07-04 04:56:44 +0000203 * match user, user@host_or_ip, user@host_or_ip_list against pattern
204 */
205int
Ben Lindstrom3fb5d002002-03-05 01:42:42 +0000206match_user(const char *user, const char *host, const char *ipaddr,
Ben Lindstrom60260022001-07-04 04:56:44 +0000207 const char *pattern)
208{
209 char *p, *pat;
210 int ret;
211
212 if ((p = strchr(pattern,'@')) == NULL)
213 return match_pattern(user, pattern);
214
215 pat = xstrdup(pattern);
216 p = strchr(pat, '@');
217 *p++ = '\0';
218
219 if ((ret = match_pattern(user, pat)) == 1)
Ben Lindstrom3fb5d002002-03-05 01:42:42 +0000220 ret = match_host_and_ip(host, ipaddr, p);
Ben Lindstrom60260022001-07-04 04:56:44 +0000221 xfree(pat);
222
223 return ret;
224}
225
226/*
Ben Lindstromf0c50292001-06-25 05:17:53 +0000227 * Returns first item from client-list that is also supported by server-list,
228 * caller must xfree() returned string.
229 */
Damien Millerafc7a5d2002-02-13 13:55:30 +1100230#define MAX_PROP 40
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000231#define SEP ","
232char *
233match_list(const char *client, const char *server, u_int *next)
234{
235 char *sproposals[MAX_PROP];
236 char *c, *s, *p, *ret, *cp, *sp;
237 int i, j, nproposals;
238
239 c = cp = xstrdup(client);
240 s = sp = xstrdup(server);
241
242 for ((p = strsep(&sp, SEP)), i=0; p && *p != '\0';
Damien Miller9f0f5c62001-12-21 14:45:46 +1100243 (p = strsep(&sp, SEP)), i++) {
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000244 if (i < MAX_PROP)
245 sproposals[i] = p;
246 else
247 break;
248 }
249 nproposals = i;
250
251 for ((p = strsep(&cp, SEP)), i=0; p && *p != '\0';
Damien Miller9f0f5c62001-12-21 14:45:46 +1100252 (p = strsep(&cp, SEP)), i++) {
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000253 for (j = 0; j < nproposals; j++) {
254 if (strcmp(p, sproposals[j]) == 0) {
255 ret = xstrdup(p);
256 if (next != NULL)
257 *next = (cp == NULL) ?
Damien Millereccb9de2005-06-17 12:59:34 +1000258 strlen(c) : (u_int)(cp - c);
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000259 xfree(c);
260 xfree(s);
261 return ret;
262 }
263 }
264 }
265 if (next != NULL)
266 *next = strlen(c);
267 xfree(c);
268 xfree(s);
269 return NULL;
270}