Blame src/match.c

Packit 6c0a39
/*
Packit 6c0a39
 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Packit 6c0a39
 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
Packit 6c0a39
 *                    All rights reserved
Packit 6c0a39
 * Simple pattern matching, with '*' and '?' as wildcards.
Packit 6c0a39
 *
Packit 6c0a39
 * As far as I am concerned, the code I have written for this software
Packit 6c0a39
 * can be used freely for any purpose.  Any derived versions of this
Packit 6c0a39
 * software must be clearly marked as such, and if the derived work is
Packit 6c0a39
 * incompatible with the protocol description in the RFC file, it must be
Packit 6c0a39
 * called by a name other than "ssh" or "Secure Shell".
Packit 6c0a39
 */
Packit 6c0a39
Packit 6c0a39
/*
Packit 6c0a39
 * Copyright (c) 2000 Markus Friedl.  All rights reserved.
Packit 6c0a39
 *
Packit 6c0a39
 * Redistribution and use in source and binary forms, with or without
Packit 6c0a39
 * modification, are permitted provided that the following conditions
Packit 6c0a39
 * are met:
Packit 6c0a39
 * 1. Redistributions of source code must retain the above copyright
Packit 6c0a39
 *    notice, this list of conditions and the following disclaimer.
Packit 6c0a39
 * 2. Redistributions in binary form must reproduce the above copyright
Packit 6c0a39
 *    notice, this list of conditions and the following disclaimer in the
Packit 6c0a39
 *    documentation and/or other materials provided with the distribution.
Packit 6c0a39
 *
Packit 6c0a39
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
Packit 6c0a39
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Packit 6c0a39
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Packit 6c0a39
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit 6c0a39
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
Packit 6c0a39
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit 6c0a39
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit 6c0a39
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit 6c0a39
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
Packit 6c0a39
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 6c0a39
 */
Packit 6c0a39
Packit 6c0a39
#include "config.h"
Packit 6c0a39
Packit 6c0a39
#include <ctype.h>
Packit 6c0a39
#include <sys/types.h>
Packit 6c0a39
Packit 6c0a39
#include "libssh/priv.h"
Packit 6c0a39
Packit 6c0a39
/*
Packit 6c0a39
 * Returns true if the given string matches the pattern (which may contain ?
Packit 6c0a39
 * and * as wildcards), and zero if it does not match.
Packit 6c0a39
 */
Packit 6c0a39
static int match_pattern(const char *s, const char *pattern) {
Packit 6c0a39
  if (s == NULL || pattern == NULL) {
Packit 6c0a39
    return 0;
Packit 6c0a39
  }
Packit 6c0a39
Packit 6c0a39
  for (;;) {
Packit 6c0a39
    /* If at end of pattern, accept if also at end of string. */
Packit 6c0a39
    if (*pattern == '\0') {
Packit 6c0a39
        return (*s == '\0');
Packit 6c0a39
    }
Packit 6c0a39
Packit 6c0a39
    if (*pattern == '*') {
Packit 6c0a39
      /* Skip the asterisk. */
Packit 6c0a39
      pattern++;
Packit 6c0a39
Packit 6c0a39
      /* If at end of pattern, accept immediately. */
Packit 6c0a39
      if (!*pattern)
Packit 6c0a39
        return 1;
Packit 6c0a39
Packit 6c0a39
      /* If next character in pattern is known, optimize. */
Packit 6c0a39
      if (*pattern != '?' && *pattern != '*') {
Packit 6c0a39
        /*
Packit 6c0a39
         * Look instances of the next character in
Packit 6c0a39
         * pattern, and try to match starting from
Packit 6c0a39
         * those.
Packit 6c0a39
         */
Packit 6c0a39
        for (; *s; s++)
Packit 6c0a39
          if (*s == *pattern && match_pattern(s + 1, pattern + 1)) {
Packit 6c0a39
            return 1;
Packit 6c0a39
          }
Packit 6c0a39
        /* Failed. */
Packit 6c0a39
        return 0;
Packit 6c0a39
      }
Packit 6c0a39
      /*
Packit 6c0a39
       * Move ahead one character at a time and try to
Packit 6c0a39
       * match at each position.
Packit 6c0a39
       */
Packit 6c0a39
      for (; *s; s++) {
Packit 6c0a39
        if (match_pattern(s, pattern)) {
Packit 6c0a39
          return 1;
Packit 6c0a39
        }
Packit 6c0a39
      }
Packit 6c0a39
      /* Failed. */
Packit 6c0a39
      return 0;
Packit 6c0a39
    }
Packit 6c0a39
    /*
Packit 6c0a39
     * There must be at least one more character in the string.
Packit 6c0a39
     * If we are at the end, fail.
Packit 6c0a39
     */
Packit 6c0a39
    if (!*s) {
Packit 6c0a39
      return 0;
Packit 6c0a39
    }
Packit 6c0a39
Packit 6c0a39
    /* Check if the next character of the string is acceptable. */
Packit 6c0a39
    if (*pattern != '?' && *pattern != *s) {
Packit 6c0a39
      return 0;
Packit 6c0a39
    }
Packit 6c0a39
Packit 6c0a39
    /* Move to the next character, both in string and in pattern. */
Packit 6c0a39
    s++;
Packit 6c0a39
    pattern++;
Packit 6c0a39
  }
Packit 6c0a39
Packit 6c0a39
  /* NOTREACHED */
Packit 6c0a39
  return 0;
Packit 6c0a39
}
Packit 6c0a39
Packit 6c0a39
/*
Packit 6c0a39
 * Tries to match the string against the comma-separated sequence of subpatterns
Packit 6c0a39
 * (each possibly preceded by ! to indicate negation).
Packit 6c0a39
 * Returns -1 if negation matches, 1 if there is a positive match, 0 if there is
Packit 6c0a39
 * no match at all.
Packit 6c0a39
 */
Packit 6c0a39
int match_pattern_list(const char *string, const char *pattern,
Packit 6c0a39
    unsigned int len, int dolower) {
Packit 6c0a39
  char sub[1024];
Packit 6c0a39
  int negated;
Packit 6c0a39
  int got_positive;
Packit 6c0a39
  unsigned int i, subi;
Packit 6c0a39
Packit 6c0a39
  got_positive = 0;
Packit 6c0a39
  for (i = 0; i < len;) {
Packit 6c0a39
    /* Check if the subpattern is negated. */
Packit 6c0a39
    if (pattern[i] == '!') {
Packit 6c0a39
      negated = 1;
Packit 6c0a39
      i++;
Packit 6c0a39
    } else {
Packit 6c0a39
      negated = 0;
Packit 6c0a39
    }
Packit 6c0a39
Packit 6c0a39
    /*
Packit 6c0a39
     * Extract the subpattern up to a comma or end.  Convert the
Packit 6c0a39
     * subpattern to lowercase.
Packit 6c0a39
     */
Packit 6c0a39
    for (subi = 0;
Packit 6c0a39
        i < len && subi < sizeof(sub) - 1 && pattern[i] != ',';
Packit 6c0a39
        subi++, i++) {
Packit 6c0a39
      sub[subi] = dolower && isupper(pattern[i]) ?
Packit 6c0a39
        (char)tolower(pattern[i]) : pattern[i];
Packit 6c0a39
    }
Packit 6c0a39
Packit 6c0a39
    /* If subpattern too long, return failure (no match). */
Packit 6c0a39
    if (subi >= sizeof(sub) - 1) {
Packit 6c0a39
      return 0;
Packit 6c0a39
    }
Packit 6c0a39
Packit 6c0a39
    /* If the subpattern was terminated by a comma, skip the comma. */
Packit 6c0a39
    if (i < len && pattern[i] == ',') {
Packit 6c0a39
      i++;
Packit 6c0a39
    }
Packit 6c0a39
Packit 6c0a39
    /* Null-terminate the subpattern. */
Packit 6c0a39
    sub[subi] = '\0';
Packit 6c0a39
Packit 6c0a39
    /* Try to match the subpattern against the string. */
Packit 6c0a39
    if (match_pattern(string, sub)) {
Packit 6c0a39
      if (negated) {
Packit 6c0a39
        return -1;        /* Negative */
Packit 6c0a39
      } else {
Packit 6c0a39
        got_positive = 1; /* Positive */
Packit 6c0a39
      }
Packit 6c0a39
    }
Packit 6c0a39
  }
Packit 6c0a39
Packit 6c0a39
  /*
Packit 6c0a39
   * Return success if got a positive match.  If there was a negative
Packit 6c0a39
   * match, we have already returned -1 and never get here.
Packit 6c0a39
   */
Packit 6c0a39
  return got_positive;
Packit 6c0a39
}
Packit 6c0a39
Packit 6c0a39
/*
Packit 6c0a39
 * Tries to match the host name (which must be in all lowercase) against the
Packit 6c0a39
 * comma-separated sequence of subpatterns (each possibly preceded by ! to
Packit 6c0a39
 * indicate negation).
Packit 6c0a39
 * Returns -1 if negation matches, 1 if there is a positive match, 0 if there
Packit 6c0a39
 * is no match at all.
Packit 6c0a39
 */
Packit 6c0a39
int match_hostname(const char *host, const char *pattern, unsigned int len) {
Packit 6c0a39
  return match_pattern_list(host, pattern, len, 1);
Packit 6c0a39
}