match.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * match.c
  3. * wildcard matching functions
  4. *
  5. *
  6. * Once this code was working, I added support for % so that I could
  7. * use the same code both in Eggdrop and in my IrcII client.
  8. * Pleased with this, I added the option of a fourth wildcard, ~,
  9. * which matches varying amounts of whitespace (at LEAST one space,
  10. * though, for sanity reasons).
  11. *
  12. * This code would not have been possible without the prior work and
  13. * suggestions of various sourced. Special thanks to Robey for
  14. * all his time/help tracking down bugs and his ever-helpful advice.
  15. *
  16. * 04/09: Fixed the "*\*" against "*a" bug (caused an endless loop)
  17. *
  18. * Chris Fuller (aka Fred1@IRC & Fwitz@IRC)
  19. * crf@cfox.bchs.uh.edu
  20. *
  21. * I hereby release this code into the public domain
  22. *
  23. */
  24. #include "common.h"
  25. #include "match.h"
  26. #include "rfc1459.h"
  27. #include "socket.h"
  28. #define QUOTE '\\' /* quoting character (overrides wildcards) */
  29. #define WILDS '*' /* matches 0 or more characters (including spaces) */
  30. #define WILDP '%' /* matches 0 or more non-space characters */
  31. #define WILDQ '?' /* matches ecactly one character */
  32. #define WILDT '~' /* matches 1 or more spaces */
  33. #define NOMATCH 0
  34. #define MATCH (match+sofar)
  35. #define PERMATCH (match+saved+sofar)
  36. /* binds matching */
  37. int _wild_match_per(register unsigned char *m, register unsigned char *n)
  38. {
  39. /* null strings should never match */
  40. if ((m == 0) || (n == 0) || (!*n))
  41. return NOMATCH;
  42. unsigned char *ma = m, *lsm = NULL, *lsn = NULL, *lpm = NULL, *lpn = NULL;
  43. int match = 1, saved = 0, space;
  44. register int sofar = 0;
  45. while (*n) {
  46. if (*m == WILDT) { /* Match >=1 space */
  47. space = 0; /* Don't need any spaces */
  48. do {
  49. m++;
  50. space++;
  51. } /* Tally 1 more space ... */
  52. while ((*m == WILDT) || (*m == ' ')); /* for each space or ~ */
  53. sofar += space; /* Each counts as exact */
  54. while (*n == ' ') {
  55. n++;
  56. space--;
  57. } /* Do we have enough? */
  58. if (space <= 0)
  59. continue; /* Had enough spaces! */
  60. }
  61. /* Do the fallback */
  62. else {
  63. switch (*m) {
  64. case 0:
  65. do
  66. m--; /* Search backwards */
  67. while ((m > ma) && (*m == '?')); /* For first non-? char */
  68. if ((m > ma) ? ((*m == '*') && (m[-1] != QUOTE)) : (*m == '*'))
  69. return PERMATCH; /* nonquoted * = match */
  70. break;
  71. case WILDP:
  72. while (*(++m) == WILDP); /* Zap redundant %s */
  73. if (*m != WILDS) { /* Don't both if next=* */
  74. if (*n != ' ') { /* WILDS can't match ' ' */
  75. lpm = m;
  76. lpn = n; /* Save '%' fallback spot */
  77. saved += sofar;
  78. sofar = 0; /* And save tally count */
  79. }
  80. continue; /* Done with '%' */
  81. }
  82. /* FALL THROUGH */
  83. case WILDS:
  84. do
  85. m++; /* Zap redundant wilds */
  86. while ((*m == WILDS) || (*m == WILDP));
  87. lsm = m;
  88. lsn = n;
  89. lpm = 0; /* Save '*' fallback spot */
  90. match += (saved + sofar); /* Save tally count */
  91. saved = sofar = 0;
  92. continue; /* Done with '*' */
  93. case WILDQ:
  94. m++;
  95. n++;
  96. continue; /* Match one char */
  97. case QUOTE:
  98. m++; /* Handle quoting */
  99. }
  100. if (rfc_toupper(*m) == rfc_toupper(*n)) { /* If matching */
  101. m++;
  102. n++;
  103. sofar++;
  104. continue; /* Tally the match */
  105. }
  106. #ifdef WILDT
  107. }
  108. #endif
  109. if (lpm) { /* Try to fallback on '%' */
  110. n = ++lpn;
  111. m = lpm;
  112. sofar = 0; /* Restore position */
  113. if ((*n | 32) == 32)
  114. lpm = 0; /* Can't match 0 or ' ' */
  115. continue; /* Next char, please */
  116. }
  117. if (lsm) { /* Try to fallback on '*' */
  118. n = ++lsn;
  119. m = lsm; /* Restore position */
  120. saved = sofar = 0;
  121. continue; /* Next char, please */
  122. }
  123. return NOMATCH; /* No fallbacks=No match */
  124. }
  125. while ((*m == WILDS) || (*m == WILDP))
  126. m++; /* Zap leftover %s & *s */
  127. return (*m) ? NOMATCH : PERMATCH; /* End of both = match */
  128. }
  129. /* general/host matching */
  130. int _wild_match(register unsigned char *m, register unsigned char *n)
  131. {
  132. unsigned char *ma = m, *na = n;
  133. /* null strings should never match */
  134. if ((ma == 0) || (na == 0) || (!*ma) || (!*na))
  135. return NOMATCH;
  136. unsigned char *lsm = NULL, *lsn = NULL;
  137. int match = 1;
  138. register int sofar = 0;
  139. /* find the end of each string */
  140. while (*(++m));
  141. m--;
  142. while (*(++n));
  143. n--;
  144. while (n >= na) {
  145. /* If the mask runs out of chars before the string, fall back on
  146. * a wildcard or fail. */
  147. if (m < ma) {
  148. if (lsm) {
  149. n = --lsn;
  150. m = lsm;
  151. if (n < na) lsm = 0;
  152. sofar = 0;
  153. }
  154. else return NOMATCH;
  155. }
  156. switch (*m) {
  157. case WILDS: /* Matches anything */
  158. do
  159. m--; /* Zap redundant wilds */
  160. while ((m >= ma) && (*m == WILDS));
  161. lsm = m;
  162. lsn = n;
  163. match += sofar;
  164. sofar = 0; /* Update fallback pos */
  165. if (m < ma) return MATCH;
  166. continue; /* Next char, please */
  167. case WILDQ:
  168. m--;
  169. n--;
  170. continue; /* '?' always matches */
  171. }
  172. if (rfc_toupper(*m) == rfc_toupper(*n)) { /* If matching char */
  173. m--;
  174. n--;
  175. sofar++; /* Tally the match */
  176. continue; /* Next char, please */
  177. }
  178. if (lsm) { /* To to fallback on '*' */
  179. n = --lsn;
  180. m = lsm;
  181. if (n < na)
  182. lsm = 0; /* Rewind to saved pos */
  183. sofar = 0;
  184. continue; /* Next char, please */
  185. }
  186. return NOMATCH; /* No fallback=No match */
  187. }
  188. while ((m >= ma) && (*m == WILDS))
  189. m--; /* Zap leftover %s & *s */
  190. return (m >= ma) ? NOMATCH : MATCH; /* Start of both = match */
  191. }
  192. static inline int
  193. comp_with_mask(void *addr, void *dest, unsigned int mask)
  194. {
  195. if (memcmp(addr, dest, mask >> 3) == 0)
  196. {
  197. int n = mask >> 3;
  198. int m = ((-1) << (8 - (mask % 8)));
  199. if (mask % 8 == 0 ||
  200. (((unsigned char *) addr)[n] & m) == (((unsigned char *) dest)[n] & m))
  201. return (1);
  202. }
  203. return (0);
  204. }
  205. /* match_cidr()
  206. *
  207. * Input - mask, address
  208. * Ouput - + = Matched 0 = Did not match
  209. */
  210. int
  211. match_cidr(const char *s1, const char *s2)
  212. {
  213. char *ipmask = strrchr(s1, '@');
  214. if(ipmask == NULL)
  215. return 0;
  216. char *ip = strrchr(s2, '@');
  217. if(ip == NULL)
  218. return 0;
  219. char mask[NICKLEN + UHOSTLEN + 6] = "";
  220. strlcpy(mask, s1, sizeof(mask));
  221. ipmask = mask + (ipmask - s1);
  222. char address[NICKLEN + UHOSTLEN + 6] = "";
  223. strlcpy(address, s2, sizeof(address));
  224. ip = address + (ip - s2);
  225. *ipmask++ = '\0';
  226. char *len = strrchr(ipmask, '/');
  227. if(len == NULL)
  228. return 0;
  229. *len++ = '\0';
  230. int cidrlen = atoi(len);
  231. if(cidrlen == 0)
  232. return 0;
  233. *ip++ = '\0';
  234. int ret = 0;
  235. #ifdef USE_IPV6
  236. int aftype = 0;
  237. #endif
  238. sockname_t ipaddr, maskaddr;
  239. egg_bzero(&ipaddr, sizeof(ipaddr));
  240. egg_bzero(&maskaddr, sizeof(maskaddr));
  241. if (!strchr(ip, ':') && !strchr(ipmask, ':'))
  242. aftype = ipaddr.family = maskaddr.family = AF_INET;
  243. #ifdef USE_IPV6
  244. else if (strchr(ip, ':') && strchr(ipmask, ':'))
  245. aftype = ipaddr.family = maskaddr.family = AF_INET6;
  246. #endif /* USE_IPV6 */
  247. else
  248. return 0;
  249. #ifdef USE_IPV6
  250. if (aftype == AF_INET6) {
  251. inet_pton(aftype, ip, &ipaddr.u.ipv6.sin6_addr);
  252. inet_pton(aftype, ipmask, &maskaddr.u.ipv6.sin6_addr);
  253. if (comp_with_mask(&ipaddr.u.ipv6.sin6_addr.s6_addr, &maskaddr.u.ipv6.sin6_addr.s6_addr, cidrlen) &&
  254. ((ret = wild_match(mask, address))))
  255. return ret;
  256. } else if (aftype == AF_INET) {
  257. #endif /* USE_IPV6 */
  258. inet_pton(aftype, ip, &ipaddr.u.ipv4.sin_addr);
  259. inet_pton(aftype, ipmask, &maskaddr.u.ipv4.sin_addr);
  260. if (comp_with_mask(&ipaddr.u.ipv4.sin_addr.s_addr, &maskaddr.u.ipv4.sin_addr.s_addr, cidrlen) &&
  261. ((ret = wild_match(mask, address))))
  262. return ret;
  263. #ifdef USE_IPV6
  264. }
  265. #endif
  266. return 0;
  267. }