xref: /titanic_44/usr/src/cmd/ssh/libssh/common/match.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Author: Tatu Ylonen <ylo@cs.hut.fi>
3*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4*7c478bd9Sstevel@tonic-gate  *                    All rights reserved
5*7c478bd9Sstevel@tonic-gate  * Simple pattern matching, with '*' and '?' as wildcards.
6*7c478bd9Sstevel@tonic-gate  *
7*7c478bd9Sstevel@tonic-gate  * As far as I am concerned, the code I have written for this software
8*7c478bd9Sstevel@tonic-gate  * can be used freely for any purpose.  Any derived versions of this
9*7c478bd9Sstevel@tonic-gate  * software must be clearly marked as such, and if the derived work is
10*7c478bd9Sstevel@tonic-gate  * incompatible with the protocol description in the RFC file, it must be
11*7c478bd9Sstevel@tonic-gate  * called by a name other than "ssh" or "Secure Shell".
12*7c478bd9Sstevel@tonic-gate  */
13*7c478bd9Sstevel@tonic-gate /*
14*7c478bd9Sstevel@tonic-gate  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
15*7c478bd9Sstevel@tonic-gate  *
16*7c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
17*7c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
18*7c478bd9Sstevel@tonic-gate  * are met:
19*7c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
20*7c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
21*7c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
22*7c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
23*7c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
24*7c478bd9Sstevel@tonic-gate  *
25*7c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26*7c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27*7c478bd9Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28*7c478bd9Sstevel@tonic-gate  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29*7c478bd9Sstevel@tonic-gate  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30*7c478bd9Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31*7c478bd9Sstevel@tonic-gate  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32*7c478bd9Sstevel@tonic-gate  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33*7c478bd9Sstevel@tonic-gate  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34*7c478bd9Sstevel@tonic-gate  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35*7c478bd9Sstevel@tonic-gate  */
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #include "includes.h"
38*7c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: match.c,v 1.19 2002/03/01 13:12:10 markus Exp $");
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate #include "match.h"
43*7c478bd9Sstevel@tonic-gate #include "xmalloc.h"
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate /*
46*7c478bd9Sstevel@tonic-gate  * Returns true if the given string matches the pattern (which may contain ?
47*7c478bd9Sstevel@tonic-gate  * and * as wildcards), and zero if it does not match.
48*7c478bd9Sstevel@tonic-gate  */
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate int
match_pattern(const char * s,const char * pattern)51*7c478bd9Sstevel@tonic-gate match_pattern(const char *s, const char *pattern)
52*7c478bd9Sstevel@tonic-gate {
53*7c478bd9Sstevel@tonic-gate 	for (;;) {
54*7c478bd9Sstevel@tonic-gate 		/* If at end of pattern, accept if also at end of string. */
55*7c478bd9Sstevel@tonic-gate 		if (!*pattern)
56*7c478bd9Sstevel@tonic-gate 			return !*s;
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate 		if (*pattern == '*') {
59*7c478bd9Sstevel@tonic-gate 			/* Skip the asterisk. */
60*7c478bd9Sstevel@tonic-gate 			pattern++;
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate 			/* If at end of pattern, accept immediately. */
63*7c478bd9Sstevel@tonic-gate 			if (!*pattern)
64*7c478bd9Sstevel@tonic-gate 				return 1;
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate 			/* If next character in pattern is known, optimize. */
67*7c478bd9Sstevel@tonic-gate 			if (*pattern != '?' && *pattern != '*') {
68*7c478bd9Sstevel@tonic-gate 				/*
69*7c478bd9Sstevel@tonic-gate 				 * Look instances of the next character in
70*7c478bd9Sstevel@tonic-gate 				 * pattern, and try to match starting from
71*7c478bd9Sstevel@tonic-gate 				 * those.
72*7c478bd9Sstevel@tonic-gate 				 */
73*7c478bd9Sstevel@tonic-gate 				for (; *s; s++)
74*7c478bd9Sstevel@tonic-gate 					if (*s == *pattern &&
75*7c478bd9Sstevel@tonic-gate 					    match_pattern(s + 1, pattern + 1))
76*7c478bd9Sstevel@tonic-gate 						return 1;
77*7c478bd9Sstevel@tonic-gate 				/* Failed. */
78*7c478bd9Sstevel@tonic-gate 				return 0;
79*7c478bd9Sstevel@tonic-gate 			}
80*7c478bd9Sstevel@tonic-gate 			/*
81*7c478bd9Sstevel@tonic-gate 			 * Move ahead one character at a time and try to
82*7c478bd9Sstevel@tonic-gate 			 * match at each position.
83*7c478bd9Sstevel@tonic-gate 			 */
84*7c478bd9Sstevel@tonic-gate 			for (; *s; s++)
85*7c478bd9Sstevel@tonic-gate 				if (match_pattern(s, pattern))
86*7c478bd9Sstevel@tonic-gate 					return 1;
87*7c478bd9Sstevel@tonic-gate 			/* Failed. */
88*7c478bd9Sstevel@tonic-gate 			return 0;
89*7c478bd9Sstevel@tonic-gate 		}
90*7c478bd9Sstevel@tonic-gate 		/*
91*7c478bd9Sstevel@tonic-gate 		 * There must be at least one more character in the string.
92*7c478bd9Sstevel@tonic-gate 		 * If we are at the end, fail.
93*7c478bd9Sstevel@tonic-gate 		 */
94*7c478bd9Sstevel@tonic-gate 		if (!*s)
95*7c478bd9Sstevel@tonic-gate 			return 0;
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 		/* Check if the next character of the string is acceptable. */
98*7c478bd9Sstevel@tonic-gate 		if (*pattern != '?' && *pattern != *s)
99*7c478bd9Sstevel@tonic-gate 			return 0;
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 		/* Move to the next character, both in string and in pattern. */
102*7c478bd9Sstevel@tonic-gate 		s++;
103*7c478bd9Sstevel@tonic-gate 		pattern++;
104*7c478bd9Sstevel@tonic-gate 	}
105*7c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
106*7c478bd9Sstevel@tonic-gate }
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate /*
109*7c478bd9Sstevel@tonic-gate  * Tries to match the string against the
110*7c478bd9Sstevel@tonic-gate  * comma-separated sequence of subpatterns (each possibly preceded by ! to
111*7c478bd9Sstevel@tonic-gate  * indicate negation).  Returns -1 if negation matches, 1 if there is
112*7c478bd9Sstevel@tonic-gate  * a positive match, 0 if there is no match at all.
113*7c478bd9Sstevel@tonic-gate  */
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate int
match_pattern_list(const char * string,const char * pattern,u_int len,int dolower)116*7c478bd9Sstevel@tonic-gate match_pattern_list(const char *string, const char *pattern, u_int len,
117*7c478bd9Sstevel@tonic-gate     int dolower)
118*7c478bd9Sstevel@tonic-gate {
119*7c478bd9Sstevel@tonic-gate 	char sub[1024];
120*7c478bd9Sstevel@tonic-gate 	int negated;
121*7c478bd9Sstevel@tonic-gate 	int got_positive;
122*7c478bd9Sstevel@tonic-gate 	u_int i, subi;
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate 	got_positive = 0;
125*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < len;) {
126*7c478bd9Sstevel@tonic-gate 		/* Check if the subpattern is negated. */
127*7c478bd9Sstevel@tonic-gate 		if (pattern[i] == '!') {
128*7c478bd9Sstevel@tonic-gate 			negated = 1;
129*7c478bd9Sstevel@tonic-gate 			i++;
130*7c478bd9Sstevel@tonic-gate 		} else
131*7c478bd9Sstevel@tonic-gate 			negated = 0;
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 		/*
134*7c478bd9Sstevel@tonic-gate 		 * Extract the subpattern up to a comma or end.  Convert the
135*7c478bd9Sstevel@tonic-gate 		 * subpattern to lowercase.
136*7c478bd9Sstevel@tonic-gate 		 */
137*7c478bd9Sstevel@tonic-gate 		for (subi = 0;
138*7c478bd9Sstevel@tonic-gate 		    i < len && subi < sizeof(sub) - 1 && pattern[i] != ',';
139*7c478bd9Sstevel@tonic-gate 		    subi++, i++)
140*7c478bd9Sstevel@tonic-gate 			sub[subi] = dolower && isupper(pattern[i]) ?
141*7c478bd9Sstevel@tonic-gate 			    tolower(pattern[i]) : pattern[i];
142*7c478bd9Sstevel@tonic-gate 		/* If subpattern too long, return failure (no match). */
143*7c478bd9Sstevel@tonic-gate 		if (subi >= sizeof(sub) - 1)
144*7c478bd9Sstevel@tonic-gate 			return 0;
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate 		/* If the subpattern was terminated by a comma, skip the comma. */
147*7c478bd9Sstevel@tonic-gate 		if (i < len && pattern[i] == ',')
148*7c478bd9Sstevel@tonic-gate 			i++;
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 		/* Null-terminate the subpattern. */
151*7c478bd9Sstevel@tonic-gate 		sub[subi] = '\0';
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate 		/* Try to match the subpattern against the string. */
154*7c478bd9Sstevel@tonic-gate 		if (match_pattern(string, sub)) {
155*7c478bd9Sstevel@tonic-gate 			if (negated)
156*7c478bd9Sstevel@tonic-gate 				return -1;		/* Negative */
157*7c478bd9Sstevel@tonic-gate 			else
158*7c478bd9Sstevel@tonic-gate 				got_positive = 1;	/* Positive */
159*7c478bd9Sstevel@tonic-gate 		}
160*7c478bd9Sstevel@tonic-gate 	}
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate 	/*
163*7c478bd9Sstevel@tonic-gate 	 * Return success if got a positive match.  If there was a negative
164*7c478bd9Sstevel@tonic-gate 	 * match, we have already returned -1 and never get here.
165*7c478bd9Sstevel@tonic-gate 	 */
166*7c478bd9Sstevel@tonic-gate 	return got_positive;
167*7c478bd9Sstevel@tonic-gate }
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate /*
170*7c478bd9Sstevel@tonic-gate  * Tries to match the host name (which must be in all lowercase) against the
171*7c478bd9Sstevel@tonic-gate  * comma-separated sequence of subpatterns (each possibly preceded by ! to
172*7c478bd9Sstevel@tonic-gate  * indicate negation).  Returns -1 if negation matches, 1 if there is
173*7c478bd9Sstevel@tonic-gate  * a positive match, 0 if there is no match at all.
174*7c478bd9Sstevel@tonic-gate  */
175*7c478bd9Sstevel@tonic-gate int
match_hostname(const char * host,const char * pattern,u_int len)176*7c478bd9Sstevel@tonic-gate match_hostname(const char *host, const char *pattern, u_int len)
177*7c478bd9Sstevel@tonic-gate {
178*7c478bd9Sstevel@tonic-gate 	return match_pattern_list(host, pattern, len, 1);
179*7c478bd9Sstevel@tonic-gate }
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate /*
182*7c478bd9Sstevel@tonic-gate  * returns 0 if we get a negative match for the hostname or the ip
183*7c478bd9Sstevel@tonic-gate  * or if we get no match at all.  returns 1 otherwise.
184*7c478bd9Sstevel@tonic-gate  */
185*7c478bd9Sstevel@tonic-gate int
match_host_and_ip(const char * host,const char * ipaddr,const char * patterns)186*7c478bd9Sstevel@tonic-gate match_host_and_ip(const char *host, const char *ipaddr,
187*7c478bd9Sstevel@tonic-gate     const char *patterns)
188*7c478bd9Sstevel@tonic-gate {
189*7c478bd9Sstevel@tonic-gate 	int mhost, mip;
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate 	/* negative ipaddr match */
192*7c478bd9Sstevel@tonic-gate 	if ((mip = match_hostname(ipaddr, patterns, strlen(patterns))) == -1)
193*7c478bd9Sstevel@tonic-gate 		return 0;
194*7c478bd9Sstevel@tonic-gate 	/* negative hostname match */
195*7c478bd9Sstevel@tonic-gate 	if ((mhost = match_hostname(host, patterns, strlen(patterns))) == -1)
196*7c478bd9Sstevel@tonic-gate 		return 0;
197*7c478bd9Sstevel@tonic-gate 	/* no match at all */
198*7c478bd9Sstevel@tonic-gate 	if (mhost == 0 && mip == 0)
199*7c478bd9Sstevel@tonic-gate 		return 0;
200*7c478bd9Sstevel@tonic-gate 	return 1;
201*7c478bd9Sstevel@tonic-gate }
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate /*
204*7c478bd9Sstevel@tonic-gate  * match user, user@host_or_ip, user@host_or_ip_list against pattern
205*7c478bd9Sstevel@tonic-gate  */
206*7c478bd9Sstevel@tonic-gate int
match_user(const char * user,const char * host,const char * ipaddr,const char * pattern)207*7c478bd9Sstevel@tonic-gate match_user(const char *user, const char *host, const char *ipaddr,
208*7c478bd9Sstevel@tonic-gate     const char *pattern)
209*7c478bd9Sstevel@tonic-gate {
210*7c478bd9Sstevel@tonic-gate 	char *p, *pat;
211*7c478bd9Sstevel@tonic-gate 	int ret;
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate 	if ((p = strchr(pattern,'@')) == NULL)
214*7c478bd9Sstevel@tonic-gate 		return match_pattern(user, pattern);
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate 	pat = xstrdup(pattern);
217*7c478bd9Sstevel@tonic-gate 	p = strchr(pat, '@');
218*7c478bd9Sstevel@tonic-gate 	*p++ = '\0';
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate 	if ((ret = match_pattern(user, pat)) == 1)
221*7c478bd9Sstevel@tonic-gate 		ret = match_host_and_ip(host, ipaddr, p);
222*7c478bd9Sstevel@tonic-gate 	xfree(pat);
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate 	return ret;
225*7c478bd9Sstevel@tonic-gate }
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate /*
228*7c478bd9Sstevel@tonic-gate  * Returns first item from client-list that is also supported by server-list,
229*7c478bd9Sstevel@tonic-gate  * caller must xfree() returned string.
230*7c478bd9Sstevel@tonic-gate  */
231*7c478bd9Sstevel@tonic-gate #define	MAX_PROP	40
232*7c478bd9Sstevel@tonic-gate #define	SEP	","
233*7c478bd9Sstevel@tonic-gate char *
match_list(const char * client,const char * server,u_int * next)234*7c478bd9Sstevel@tonic-gate match_list(const char *client, const char *server, u_int *next)
235*7c478bd9Sstevel@tonic-gate {
236*7c478bd9Sstevel@tonic-gate 	char *sproposals[MAX_PROP];
237*7c478bd9Sstevel@tonic-gate 	char *c, *s, *p, *ret, *cp, *sp;
238*7c478bd9Sstevel@tonic-gate 	int i, j, nproposals;
239*7c478bd9Sstevel@tonic-gate 
240*7c478bd9Sstevel@tonic-gate 	c = cp = xstrdup(client);
241*7c478bd9Sstevel@tonic-gate 	s = sp = xstrdup(server);
242*7c478bd9Sstevel@tonic-gate 
243*7c478bd9Sstevel@tonic-gate 	for ((p = strsep(&sp, SEP)), i=0; p && *p != '\0';
244*7c478bd9Sstevel@tonic-gate 	    (p = strsep(&sp, SEP)), i++) {
245*7c478bd9Sstevel@tonic-gate 		if (i < MAX_PROP)
246*7c478bd9Sstevel@tonic-gate 			sproposals[i] = p;
247*7c478bd9Sstevel@tonic-gate 		else
248*7c478bd9Sstevel@tonic-gate 			break;
249*7c478bd9Sstevel@tonic-gate 	}
250*7c478bd9Sstevel@tonic-gate 	nproposals = i;
251*7c478bd9Sstevel@tonic-gate 
252*7c478bd9Sstevel@tonic-gate 	for ((p = strsep(&cp, SEP)), i=0; p && *p != '\0';
253*7c478bd9Sstevel@tonic-gate 	    (p = strsep(&cp, SEP)), i++) {
254*7c478bd9Sstevel@tonic-gate 		for (j = 0; j < nproposals; j++) {
255*7c478bd9Sstevel@tonic-gate 			if (strcmp(p, sproposals[j]) == 0) {
256*7c478bd9Sstevel@tonic-gate 				ret = xstrdup(p);
257*7c478bd9Sstevel@tonic-gate 				if (next != NULL)
258*7c478bd9Sstevel@tonic-gate 					*next = (cp == NULL) ?
259*7c478bd9Sstevel@tonic-gate 					    strlen(c) : cp - c;
260*7c478bd9Sstevel@tonic-gate 				xfree(c);
261*7c478bd9Sstevel@tonic-gate 				xfree(s);
262*7c478bd9Sstevel@tonic-gate 				return ret;
263*7c478bd9Sstevel@tonic-gate 			}
264*7c478bd9Sstevel@tonic-gate 		}
265*7c478bd9Sstevel@tonic-gate 	}
266*7c478bd9Sstevel@tonic-gate 	if (next != NULL)
267*7c478bd9Sstevel@tonic-gate 		*next = strlen(c);
268*7c478bd9Sstevel@tonic-gate 	xfree(c);
269*7c478bd9Sstevel@tonic-gate 	xfree(s);
270*7c478bd9Sstevel@tonic-gate 	return NULL;
271*7c478bd9Sstevel@tonic-gate }
272