Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 4 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 5 | * Simple pattern matching, with '*' and '?' as wildcards. |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 6 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 7 | * 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 Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 12 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 13 | |
| 14 | #include "includes.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 15 | RCSID("$OpenBSD: match.c,v 1.11 2001/01/21 19:05:52 markus Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 16 | |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 17 | #include "match.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 18 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 19 | /* |
| 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 Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 23 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 24 | int |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 25 | match_pattern(const char *s, const char *pattern) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 26 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 27 | for (;;) { |
| 28 | /* If at end of pattern, accept if also at end of string. */ |
| 29 | if (!*pattern) |
| 30 | return !*s; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 31 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 32 | if (*pattern == '*') { |
| 33 | /* Skip the asterisk. */ |
| 34 | pattern++; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 35 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 36 | /* If at end of pattern, accept immediately. */ |
| 37 | if (!*pattern) |
| 38 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 39 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 40 | /* If next character in pattern is known, optimize. */ |
| 41 | if (*pattern != '?' && *pattern != '*') { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 42 | /* |
| 43 | * Look instances of the next character in |
| 44 | * pattern, and try to match starting from |
| 45 | * those. |
| 46 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 47 | for (; *s; s++) |
| 48 | if (*s == *pattern && |
| 49 | match_pattern(s + 1, pattern + 1)) |
| 50 | return 1; |
| 51 | /* Failed. */ |
| 52 | return 0; |
| 53 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 54 | /* |
| 55 | * Move ahead one character at a time and try to |
| 56 | * match at each position. |
| 57 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 58 | for (; *s; s++) |
| 59 | if (match_pattern(s, pattern)) |
| 60 | return 1; |
| 61 | /* Failed. */ |
| 62 | return 0; |
| 63 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 64 | /* |
| 65 | * There must be at least one more character in the string. |
| 66 | * If we are at the end, fail. |
| 67 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 68 | if (!*s) |
| 69 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 70 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 71 | /* Check if the next character of the string is acceptable. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 72 | if (*pattern != '?' && *pattern != *s) |
| 73 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 74 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 75 | /* Move to the next character, both in string and in pattern. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 76 | s++; |
| 77 | pattern++; |
| 78 | } |
| 79 | /* NOTREACHED */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 80 | } |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 81 | |
| 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 Miller | d3a1857 | 2000-06-07 19:55:44 +1000 | [diff] [blame] | 85 | * indicate negation). Returns -1 if negation matches, 1 if there is |
| 86 | * a positive match, 0 if there is no match at all. |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 87 | */ |
| 88 | |
| 89 | int |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 90 | match_hostname(const char *host, const char *pattern, u_int len) |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 91 | { |
| 92 | char sub[1024]; |
| 93 | int negated; |
| 94 | int got_positive; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 95 | u_int i, subi; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 96 | |
| 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 Miller | d3a1857 | 2000-06-07 19:55:44 +1000 | [diff] [blame] | 128 | return -1; /* Negative */ |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 129 | else |
Damien Miller | d3a1857 | 2000-06-07 19:55:44 +1000 | [diff] [blame] | 130 | got_positive = 1; /* Positive */ |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * Return success if got a positive match. If there was a negative |
Damien Miller | d3a1857 | 2000-06-07 19:55:44 +1000 | [diff] [blame] | 136 | * match, we have already returned -1 and never get here. |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 137 | */ |
| 138 | return got_positive; |
| 139 | } |