Blame src/match.c

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