]> andersk Git - openssh.git/blobdiff - match.c
- Moved all the bsd-* and fake-* stuff into new libopenbsd-compat.a
[openssh.git] / match.c
diff --git a/match.c b/match.c
index d45056b88070763d2854bba47e5a55daf0cabf23..5d076ff9e7a842bf32f1685b0d283be9e16b8bc8 100644 (file)
--- a/match.c
+++ b/match.c
@@ -1,16 +1,16 @@
 /*
- * 
+ *
  * match.c
- * 
+ *
  * Author: Tatu Ylonen <ylo@cs.hut.fi>
- * 
+ *
  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
  *                    All rights reserved
- * 
+ *
  * Created: Thu Jun 22 01:17:50 1995 ylo
- * 
+ *
  * Simple pattern matching, with '*' and '?' as wildcards.
- * 
+ *
  */
 
 #include "includes.h"
@@ -23,7 +23,7 @@ RCSID("$Id$");
  * and * as wildcards), and zero if it does not match.
  */
 
-int 
+int
 match_pattern(const char *s, const char *pattern)
 {
        for (;;) {
@@ -80,3 +80,62 @@ match_pattern(const char *s, const char *pattern)
        }
        /* NOTREACHED */
 }
+
+/*
+ * Tries to match the host name (which must be in all lowercase) against the
+ * comma-separated sequence of subpatterns (each possibly preceded by ! to
+ * indicate negation).  Returns true if there is a positive match; zero
+ * otherwise.
+ */
+
+int
+match_hostname(const char *host, const char *pattern, unsigned int len)
+{
+       char sub[1024];
+       int negated;
+       int got_positive;
+       unsigned int i, subi;
+
+       got_positive = 0;
+       for (i = 0; i < len;) {
+               /* Check if the subpattern is negated. */
+               if (pattern[i] == '!') {
+                       negated = 1;
+                       i++;
+               } else
+                       negated = 0;
+
+               /*
+                * Extract the subpattern up to a comma or end.  Convert the
+                * subpattern to lowercase.
+                */
+               for (subi = 0;
+                    i < len && subi < sizeof(sub) - 1 && pattern[i] != ',';
+                    subi++, i++)
+                       sub[subi] = isupper(pattern[i]) ? tolower(pattern[i]) : pattern[i];
+               /* If subpattern too long, return failure (no match). */
+               if (subi >= sizeof(sub) - 1)
+                       return 0;
+
+               /* If the subpattern was terminated by a comma, skip the comma. */
+               if (i < len && pattern[i] == ',')
+                       i++;
+
+               /* Null-terminate the subpattern. */
+               sub[subi] = '\0';
+
+               /* Try to match the subpattern against the host name. */
+               if (match_pattern(host, sub)) {
+                       if (negated)
+                               return 0;       /* Fail */
+                       else
+                               got_positive = 1;
+               }
+       }
+
+       /*
+        * Return success if got a positive match.  If there was a negative
+        * match, we have already returned zero and never get here.
+        */
+       return got_positive;
+}
This page took 0.038547 seconds and 4 git commands to generate.