X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fwildcard.cpp;h=f9e706906b3f4bbe3339b22b36088680a035bf6d;hb=e2b0f3dc9ef4d56c71d7abda13e6139ca092e387;hp=41070d14506c8f30fdae2f48902a09406760e4c7;hpb=61b45c935dbb50c280970c9f431fd1c7ef4eb680;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/wildcard.cpp b/src/wildcard.cpp index 41070d145..f9e706906 100644 --- a/src/wildcard.cpp +++ b/src/wildcard.cpp @@ -1,90 +1,126 @@ -#include -#include "inspircd_config.h" +/* + * InspIRCd -- Internet Relay Chat Daemon + * + * Copyright (C) 2013, 2017 Sadie Powell + * Copyright (C) 2012 Robby + * Copyright (C) 2009 Uli Schlachter + * Copyright (C) 2008, 2010 Craig Edwards + * Copyright (C) 2007-2008 Robin Burchell + * Copyright (C) 2007-2008 Dennis Friis + * + * This file is part of InspIRCd. InspIRCd is free software: you can + * redistribute it and/or modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation, version 2. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + #include "inspircd.h" -void Delete(char* str,int pos) +static bool MatchInternal(const unsigned char* str, const unsigned char* mask, unsigned const char* map) { - char moo[MAXBUF]; - strcpy(moo,str); - moo[pos] = '\0'; - strcpy(str,moo); - strcat(str,moo+pos+1); + unsigned char* cp = NULL; + unsigned char* mp = NULL; + unsigned char* string = (unsigned char*)str; + unsigned char* wild = (unsigned char*)mask; + + while ((*string) && (*wild != '*')) + { + if ((map[*wild] != map[*string]) && (*wild != '?')) + { + return 0; + } + wild++; + string++; + } + + while (*string) + { + if (*wild == '*') + { + if (!*++wild) + { + return 1; + } + mp = wild; + cp = string+1; + } + else + if ((map[*wild] == map[*string]) || (*wild == '?')) + { + wild++; + string++; + } + else + { + wild = mp; + string = cp++; + } + + } + + while (*wild == '*') + { + wild++; + } + + return !*wild; } -void Insert(char* substr,char* str,int pos) +// Below here is all wrappers around MatchInternal + +bool InspIRCd::Match(const std::string& str, const std::string& mask, unsigned const char* map) { - std::string a = str; - a.insert(pos,substr); - strcpy(str,a.c_str()); + if (!map) + map = national_case_insensitive_map; + + return MatchInternal((const unsigned char*)str.c_str(), (const unsigned char*)mask.c_str(), map); } +bool InspIRCd::Match(const char* str, const char* mask, unsigned const char* map) +{ + if (!map) + map = national_case_insensitive_map; -int MWC = 0; + return MatchInternal((const unsigned char*)str, (const unsigned char*)mask, map); +} -bool match2(char* literal,char* mask) +bool InspIRCd::MatchCIDR(const std::string& str, const std::string& mask, unsigned const char* map) { + if (irc::sockets::MatchCIDR(str, mask, true)) + return true; -char OldM[MAXBUF]; -int I,I2; - -if (MWC) - return true; - -if ((strstr(mask,"*")==0) && (strlen(literal) != strlen(mask))) - return 0; - I=0; - I2=0; - while (I < strlen(mask)) - { - if (I2 >= strlen(literal)) - return 0; - - if ((mask[I]=='*') && (MWC==0)) - { - strcpy(OldM,mask); - - Delete(mask,I); - - while (strlen(mask)<255) - { - match2(literal,mask); - if (MWC==2) - return 1; - - Insert("?",mask,I); - } - strcpy(mask,OldM); - Delete(mask,I); - Insert("?",mask,I); - } - if (mask[I]=='?') - { - I++; - I2++; - continue; - } - if (mask[I] != literal[I2]) - return 0; - if (MWC) - return 1; - I++; - I2++; - } - if (strlen(literal)==strlen(mask)) - MWC=2; - + // Fall back to regular match + return InspIRCd::Match(str, mask, map); } -bool match(char* literal, char* mask) +bool InspIRCd::MatchCIDR(const char* str, const char* mask, unsigned const char* map) { - char L[10240]; - char M[10240]; - MWC = 0; - strncpy(L,literal,10240); - strncpy(M,mask,10240); - strlower(L); - strlower(M); - match2(L,M); - return (MWC == 2); + if (irc::sockets::MatchCIDR(str, mask, true)) + return true; + + // Fall back to regular match + return InspIRCd::Match(str, mask, map); } +bool InspIRCd::MatchMask(const std::string& masks, const std::string& hostname, const std::string& ipaddr) +{ + irc::spacesepstream masklist(masks); + std::string mask; + while (masklist.GetToken(mask)) + { + if (InspIRCd::Match(hostname, mask, ascii_case_insensitive_map) || + InspIRCd::MatchCIDR(ipaddr, mask, ascii_case_insensitive_map)) + { + return true; + } + } + return false; +}