/* $OpenBSD: addrmatch.c,v 1.17 2021/04/03 06:18:40 djm Exp $ */12/*3* Copyright (c) 2004-2008 Damien Miller <[email protected]>4*5* Permission to use, copy, modify, and distribute this software for any6* purpose with or without fee is hereby granted, provided that the above7* copyright notice and this permission notice appear in all copies.8*9* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES10* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF11* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR12* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES13* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN14* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF15* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.16*/1718#include "includes.h"1920#include <sys/types.h>21#include <sys/socket.h>22#include <netinet/in.h>23#include <arpa/inet.h>2425#include <netdb.h>26#include <string.h>27#include <stdlib.h>28#include <stdio.h>29#include <stdarg.h>3031#include "addr.h"32#include "match.h"33#include "log.h"3435/*36* Match "addr" against list pattern list "_list", which may contain a37* mix of CIDR addresses and old-school wildcards.38*39* If addr is NULL, then no matching is performed, but _list is parsed40* and checked for well-formedness.41*42* Returns 1 on match found (never returned when addr == NULL).43* Returns 0 on if no match found, or no errors found when addr == NULL.44* Returns -1 on negated match found (never returned when addr == NULL).45* Returns -2 on invalid list entry.46*/47int48addr_match_list(const char *addr, const char *_list)49{50char *list, *cp, *o;51struct xaddr try_addr, match_addr;52u_int masklen, neg;53int ret = 0, r;5455if (addr != NULL && addr_pton(addr, &try_addr) != 0) {56debug2_f("couldn't parse address %.100s", addr);57return 0;58}59if ((o = list = strdup(_list)) == NULL)60return -1;61while ((cp = strsep(&list, ",")) != NULL) {62neg = *cp == '!';63if (neg)64cp++;65if (*cp == '\0') {66ret = -2;67break;68}69/* Prefer CIDR address matching */70r = addr_pton_cidr(cp, &match_addr, &masklen);71if (r == -2) {72debug2_f("inconsistent mask length for "73"match network \"%.100s\"", cp);74ret = -2;75break;76} else if (r == 0) {77if (addr != NULL && addr_netmatch(&try_addr,78&match_addr, masklen) == 0) {79foundit:80if (neg) {81ret = -1;82break;83}84ret = 1;85}86continue;87} else {88/* If CIDR parse failed, try wildcard string match */89if (addr != NULL && match_pattern(addr, cp) == 1)90goto foundit;91}92}93free(o);9495return ret;96}9798/*99* Match "addr" against list CIDR list "_list". Lexical wildcards and100* negation are not supported. If "addr" == NULL, will verify structure101* of "_list".102*103* Returns 1 on match found (never returned when addr == NULL).104* Returns 0 on if no match found, or no errors found when addr == NULL.105* Returns -1 on error106*/107int108addr_match_cidr_list(const char *addr, const char *_list)109{110char *list, *cp, *o;111struct xaddr try_addr, match_addr;112u_int masklen;113int ret = 0, r;114115if (addr != NULL && addr_pton(addr, &try_addr) != 0) {116debug2_f("couldn't parse address %.100s", addr);117return 0;118}119if ((o = list = strdup(_list)) == NULL)120return -1;121while ((cp = strsep(&list, ",")) != NULL) {122if (*cp == '\0') {123error_f("empty entry in list \"%.100s\"", o);124ret = -1;125break;126}127128/*129* NB. This function is called in pre-auth with untrusted data,130* so be extra paranoid about junk reaching getaddrino (via131* addr_pton_cidr).132*/133134/* Stop junk from reaching getaddrinfo. +3 is for masklen */135if (strlen(cp) > INET6_ADDRSTRLEN + 3) {136error_f("list entry \"%.100s\" too long", cp);137ret = -1;138break;139}140#define VALID_CIDR_CHARS "0123456789abcdefABCDEF.:/"141if (strspn(cp, VALID_CIDR_CHARS) != strlen(cp)) {142error_f("list entry \"%.100s\" contains invalid "143"characters", cp);144ret = -1;145}146147/* Prefer CIDR address matching */148r = addr_pton_cidr(cp, &match_addr, &masklen);149if (r == -1) {150error("Invalid network entry \"%.100s\"", cp);151ret = -1;152break;153} else if (r == -2) {154error("Inconsistent mask length for "155"network \"%.100s\"", cp);156ret = -1;157break;158} else if (r == 0 && addr != NULL) {159if (addr_netmatch(&try_addr, &match_addr,160masklen) == 0)161ret = 1;162continue;163}164}165free(o);166167return ret;168}169170171