-/* +------------------------------------+
- * | Inspire Internet Relay Chat Daemon |
- * +------------------------------------+
+/*
+ * InspIRCd -- Internet Relay Chat Daemon
*
- * Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
- * E-mail:
- * <brain@chatspike.net>
- * <Craig@chatspike.net>
- *
- * Written by Craig Edwards, Craig McLure, and others.
- * This program is free but copyrighted software; see
- * the file COPYING for details.
+ * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
+ * Copyright (C) 2003, 2006-2008 Craig Edwards <craigedwards@brainbox.cc>
+ * Copyright (C) 2007-2008 Dennis Friis <peavey@inspircd.org>
*
- * ---------------------------------------------------
+ * 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 <http://www.gnu.org/licenses/>.
*/
-#include <string>
-#include "inspircd_config.h"
+
+/* $Core */
+
#include "inspircd.h"
+#include "hashcomp.h"
#include "inspstring.h"
-void Delete(char* str,int pos)
+static bool match_internal(const unsigned char *str, const unsigned char *mask, unsigned const char *map)
+{
+ unsigned char *cp = NULL, *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;
+}
+
+/********************************************************************
+ * Below here is all wrappers around match_internal
+ ********************************************************************/
+
+bool InspIRCd::Match(const std::string &str, const std::string &mask, unsigned const char *map)
{
- char moo[MAXBUF];
- strlcpy(moo,str,MAXBUF);
- moo[pos] = '\0';
- strlcpy(str,moo,MAXBUF);
- strlcat(str,moo+pos+1,MAXBUF);
+ if (!map)
+ map = national_case_insensitive_map;
+
+ return match_internal((const unsigned char *)str.c_str(), (const unsigned char *)mask.c_str(), map);
}
-void Insert(char* substr,char* str,int pos)
+bool InspIRCd::Match(const char *str, const char *mask, unsigned const char *map)
{
- std::string a = str;
- a.insert(pos,substr);
- strlcpy(str,a.c_str(),MAXBUF);
+ if (!map)
+ map = national_case_insensitive_map;
+ return match_internal((const unsigned char *)str, (const unsigned char *)mask, map);
}
+bool InspIRCd::MatchCIDR(const std::string &str, const std::string &mask, unsigned const char *map)
+{
+ if (irc::sockets::MatchCIDR(str, mask, true))
+ return true;
-int MWC = 0;
+ if (!map)
+ map = national_case_insensitive_map;
-bool match2(char* literal,char* mask)
+ // Fall back to regular match
+ return InspIRCd::Match(str, mask, map);
+}
+
+bool InspIRCd::MatchCIDR(const char *str, const char *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))
- {
- strlcpy(OldM,mask,MAXBUF);
-
- Delete(mask,I);
-
- while (strlen(mask)<255)
- {
- match2(literal,mask);
- if (MWC==2)
- return 1;
-
- Insert("?",mask,I);
- }
- strlcpy(mask,OldM,MAXBUF);
- 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;
+ if (!map)
+ map = national_case_insensitive_map;
+ // Fall back to regular match
+ return InspIRCd::Match(str, mask, map);
}
-bool match(const char* literal, const char* mask)
+bool InspIRCd::MatchMask(const std::string& masks, const std::string& hostname, const std::string& ipaddr)
{
- char L[10240];
- char M[10240];
- MWC = 0;
- strlcpy(L,literal,10240);
- strlcpy(M,mask,10240);
- strlower(L);
- strlower(M);
- // short circuit literals
- log(DEBUG,"Match '%s' to '%s'",L,M);
- if ((!strchr(M,'*')) && (!strchr(M,'?')))
+ std::stringstream masklist(masks);
+ std::string mask;
+ while (masklist >> mask)
{
- log(DEBUG,"Short circuiting literal");
- if (!strcasecmp(L,M))
+ if (InspIRCd::Match(hostname, mask, ascii_case_insensitive_map) ||
+ InspIRCd::MatchCIDR(ipaddr, mask, ascii_case_insensitive_map))
{
- log(DEBUG,"Literal match");
return true;
}
}
- match2(L,M);
- return (MWC == 2);
+ return false;
}
-