blob: 81030da6a707833055818da56cecdc6ec75f9c71 [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 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100013
14#include "includes.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000015RCSID("$OpenBSD: match.c,v 1.11 2001/01/21 19:05:52 markus Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100016
Ben Lindstrom226cfa02001-01-22 05:34:40 +000017#include "match.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100018
Damien Miller5428f641999-11-25 11:54:57 +110019/*
20 * Returns true if the given string matches the pattern (which may contain ?
21 * and * as wildcards), and zero if it does not match.
22 */
Damien Miller95def091999-11-25 00:26:21 +110023
Damien Miller4af51302000-04-16 11:18:38 +100024int
Damien Miller95def091999-11-25 00:26:21 +110025match_pattern(const char *s, const char *pattern)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100026{
Damien Miller95def091999-11-25 00:26:21 +110027 for (;;) {
28 /* If at end of pattern, accept if also at end of string. */
29 if (!*pattern)
30 return !*s;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100031
Damien Miller95def091999-11-25 00:26:21 +110032 if (*pattern == '*') {
33 /* Skip the asterisk. */
34 pattern++;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100035
Damien Miller95def091999-11-25 00:26:21 +110036 /* If at end of pattern, accept immediately. */
37 if (!*pattern)
38 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039
Damien Miller95def091999-11-25 00:26:21 +110040 /* If next character in pattern is known, optimize. */
41 if (*pattern != '?' && *pattern != '*') {
Damien Miller5428f641999-11-25 11:54:57 +110042 /*
43 * Look instances of the next character in
44 * pattern, and try to match starting from
45 * those.
46 */
Damien Miller95def091999-11-25 00:26:21 +110047 for (; *s; s++)
48 if (*s == *pattern &&
49 match_pattern(s + 1, pattern + 1))
50 return 1;
51 /* Failed. */
52 return 0;
53 }
Damien Miller5428f641999-11-25 11:54:57 +110054 /*
55 * Move ahead one character at a time and try to
56 * match at each position.
57 */
Damien Miller95def091999-11-25 00:26:21 +110058 for (; *s; s++)
59 if (match_pattern(s, pattern))
60 return 1;
61 /* Failed. */
62 return 0;
63 }
Damien Miller5428f641999-11-25 11:54:57 +110064 /*
65 * There must be at least one more character in the string.
66 * If we are at the end, fail.
67 */
Damien Miller95def091999-11-25 00:26:21 +110068 if (!*s)
69 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070
Damien Miller5428f641999-11-25 11:54:57 +110071 /* Check if the next character of the string is acceptable. */
Damien Miller95def091999-11-25 00:26:21 +110072 if (*pattern != '?' && *pattern != *s)
73 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074
Damien Miller5428f641999-11-25 11:54:57 +110075 /* Move to the next character, both in string and in pattern. */
Damien Miller95def091999-11-25 00:26:21 +110076 s++;
77 pattern++;
78 }
79 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080}
Damien Miller450a7a12000-03-26 13:04:51 +100081
82/*
83 * Tries to match the host name (which must be in all lowercase) against the
84 * comma-separated sequence of subpatterns (each possibly preceded by ! to
Damien Millerd3a18572000-06-07 19:55:44 +100085 * indicate negation). Returns -1 if negation matches, 1 if there is
86 * a positive match, 0 if there is no match at all.
Damien Miller450a7a12000-03-26 13:04:51 +100087 */
88
89int
Ben Lindstrom46c16222000-12-22 01:43:59 +000090match_hostname(const char *host, const char *pattern, u_int len)
Damien Miller450a7a12000-03-26 13:04:51 +100091{
92 char sub[1024];
93 int negated;
94 int got_positive;
Ben Lindstrom46c16222000-12-22 01:43:59 +000095 u_int i, subi;
Damien Miller450a7a12000-03-26 13:04:51 +100096
97 got_positive = 0;
98 for (i = 0; i < len;) {
99 /* Check if the subpattern is negated. */
100 if (pattern[i] == '!') {
101 negated = 1;
102 i++;
103 } else
104 negated = 0;
105
106 /*
107 * Extract the subpattern up to a comma or end. Convert the
108 * subpattern to lowercase.
109 */
110 for (subi = 0;
111 i < len && subi < sizeof(sub) - 1 && pattern[i] != ',';
112 subi++, i++)
113 sub[subi] = isupper(pattern[i]) ? tolower(pattern[i]) : pattern[i];
114 /* If subpattern too long, return failure (no match). */
115 if (subi >= sizeof(sub) - 1)
116 return 0;
117
118 /* If the subpattern was terminated by a comma, skip the comma. */
119 if (i < len && pattern[i] == ',')
120 i++;
121
122 /* Null-terminate the subpattern. */
123 sub[subi] = '\0';
124
125 /* Try to match the subpattern against the host name. */
126 if (match_pattern(host, sub)) {
127 if (negated)
Damien Millerd3a18572000-06-07 19:55:44 +1000128 return -1; /* Negative */
Damien Miller450a7a12000-03-26 13:04:51 +1000129 else
Damien Millerd3a18572000-06-07 19:55:44 +1000130 got_positive = 1; /* Positive */
Damien Miller450a7a12000-03-26 13:04:51 +1000131 }
132 }
133
134 /*
135 * Return success if got a positive match. If there was a negative
Damien Millerd3a18572000-06-07 19:55:44 +1000136 * match, we have already returned -1 and never get here.
Damien Miller450a7a12000-03-26 13:04:51 +1000137 */
138 return got_positive;
139}