match.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. int _wild_match_per(register unsigned char *m, register unsigned char *n)
  37. {
  38. /* null strings should never match */
  39. if ((m == 0) || (n == 0) || (!*n))
  40. return NOMATCH;
  41. unsigned char *ma = m, *lsm = NULL, *lsn = NULL, *lpm = NULL, *lpn = NULL;
  42. int match = 1, saved = 0, space;
  43. register int sofar = 0;
  44. while (*n) {
  45. if (*m == WILDT) { /* Match >=1 space */
  46. space = 0; /* Don't need any spaces */
  47. do {
  48. m++;
  49. space++;
  50. } /* Tally 1 more space ... */
  51. while ((*m == WILDT) || (*m == ' ')); /* for each space or ~ */
  52. sofar += space; /* Each counts as exact */
  53. while (*n == ' ') {
  54. n++;
  55. space--;
  56. } /* Do we have enough? */
  57. if (space <= 0)
  58. continue; /* Had enough spaces! */
  59. }
  60. /* Do the fallback */
  61. else {
  62. switch (*m) {
  63. case 0:
  64. do
  65. m--; /* Search backwards */
  66. while ((m > ma) && (*m == '?')); /* For first non-? char */
  67. if ((m > ma) ? ((*m == '*') && (m[-1] != QUOTE)) : (*m == '*'))
  68. return PERMATCH; /* nonquoted * = match */
  69. break;
  70. case WILDP:
  71. while (*(++m) == WILDP); /* Zap redundant %s */
  72. if (*m != WILDS) { /* Don't both if next=* */
  73. if (*n != ' ') { /* WILDS can't match ' ' */
  74. lpm = m;
  75. lpn = n; /* Save '%' fallback spot */
  76. saved += sofar;
  77. sofar = 0; /* And save tally count */
  78. }
  79. continue; /* Done with '%' */
  80. }
  81. /* FALL THROUGH */
  82. case WILDS:
  83. do
  84. m++; /* Zap redundant wilds */
  85. while ((*m == WILDS) || (*m == WILDP));
  86. lsm = m;
  87. lsn = n;
  88. lpm = 0; /* Save '*' fallback spot */
  89. match += (saved + sofar); /* Save tally count */
  90. saved = sofar = 0;
  91. continue; /* Done with '*' */
  92. case WILDQ:
  93. m++;
  94. n++;
  95. continue; /* Match one char */
  96. case QUOTE:
  97. m++; /* Handle quoting */
  98. }
  99. if (rfc_toupper(*m) == rfc_toupper(*n)) { /* If matching */
  100. m++;
  101. n++;
  102. sofar++;
  103. continue; /* Tally the match */
  104. }
  105. #ifdef WILDT
  106. }
  107. #endif
  108. if (lpm) { /* Try to fallback on '%' */
  109. n = ++lpn;
  110. m = lpm;
  111. sofar = 0; /* Restore position */
  112. if ((*n | 32) == 32)
  113. lpm = 0; /* Can't match 0 or ' ' */
  114. continue; /* Next char, please */
  115. }
  116. if (lsm) { /* Try to fallback on '*' */
  117. n = ++lsn;
  118. m = lsm; /* Restore position */
  119. saved = sofar = 0;
  120. continue; /* Next char, please */
  121. }
  122. return NOMATCH; /* No fallbacks=No match */
  123. }
  124. while ((*m == WILDS) || (*m == WILDP))
  125. m++; /* Zap leftover %s & *s */
  126. return (*m) ? NOMATCH : PERMATCH; /* End of both = match */
  127. }
  128. int _wild_match(register unsigned char *m, register unsigned char *n)
  129. {
  130. unsigned char *ma = m, *na = n;
  131. /* null strings should never match */
  132. if ((ma == 0) || (na == 0) || (!*ma) || (!*na))
  133. return NOMATCH;
  134. unsigned char *lsm = NULL, *lsn = NULL;
  135. int match = 1;
  136. register int sofar = 0;
  137. /* find the end of each string */
  138. while (*(++m));
  139. m--;
  140. while (*(++n));
  141. n--;
  142. while (n >= na) {
  143. /* If the mask runs out of chars before the string, fall back on
  144. * a wildcard or fail. */
  145. if (m < ma) {
  146. if (lsm) {
  147. n = --lsn;
  148. m = lsm;
  149. if (n < na) lsm = 0;
  150. sofar = 0;
  151. }
  152. else return NOMATCH;
  153. }
  154. switch (*m) {
  155. case WILDS: /* Matches anything */
  156. do
  157. m--; /* Zap redundant wilds */
  158. while ((m >= ma) && (*m == WILDS));
  159. lsm = m;
  160. lsn = n;
  161. match += sofar;
  162. sofar = 0; /* Update fallback pos */
  163. if (m < ma) return MATCH;
  164. continue; /* Next char, please */
  165. case WILDQ:
  166. m--;
  167. n--;
  168. continue; /* '?' always matches */
  169. }
  170. if (rfc_toupper(*m) == rfc_toupper(*n)) { /* If matching char */
  171. m--;
  172. n--;
  173. sofar++; /* Tally the match */
  174. continue; /* Next char, please */
  175. }
  176. if (lsm) { /* To to fallback on '*' */
  177. n = --lsn;
  178. m = lsm;
  179. if (n < na)
  180. lsm = 0; /* Rewind to saved pos */
  181. sofar = 0;
  182. continue; /* Next char, please */
  183. }
  184. return NOMATCH; /* No fallback=No match */
  185. }
  186. while ((m >= ma) && (*m == WILDS))
  187. m--; /* Zap leftover %s & *s */
  188. return (m >= ma) ? NOMATCH : MATCH; /* Start of both = match */
  189. }
  190. static inline int
  191. comp_with_mask(void *addr, void *dest, unsigned int mask)
  192. {
  193. if (memcmp(addr, dest, mask / 8) == 0)
  194. {
  195. int n = mask / 8;
  196. int m = ((-1) << (8 - (mask % 8)));
  197. if (mask % 8 == 0 ||
  198. (((unsigned char *) addr)[n] & m) == (((unsigned char *) dest)[n] & m))
  199. return (1);
  200. }
  201. return (0);
  202. }
  203. /* match_cidr()
  204. *
  205. * Input - mask, address
  206. * Ouput - 1 = Matched 0 = Did not match
  207. */
  208. int
  209. match_cidr(const char *s1, const char *s2)
  210. {
  211. sockname_t ipaddr, maskaddr;
  212. char address[NICKLEN + UHOSTLEN + 6] = "", mask[NICKLEN + UHOSTLEN + 6] = "", *ipmask = NULL, *ip = NULL, *len = NULL;
  213. int cidrlen, aftype;
  214. egg_bzero(&ipaddr, sizeof(ipaddr));
  215. egg_bzero(&maskaddr, sizeof(maskaddr));
  216. strcpy(mask, s1);
  217. strcpy(address, s2);
  218. ipmask = strrchr(mask, '@');
  219. if(ipmask == NULL)
  220. return 0;
  221. *ipmask++ = '\0';
  222. ip = strrchr(address, '@');
  223. if(ip == NULL)
  224. return 0;
  225. *ip++ = '\0';
  226. len = strrchr(ipmask, '/');
  227. if(len == NULL)
  228. return 0;
  229. *len++ = '\0';
  230. cidrlen = atoi(len);
  231. if(cidrlen == 0)
  232. return 0;
  233. if (strchr(ip, ':') && strchr(ipmask, ':'))
  234. aftype = ipaddr.family = maskaddr.family = AF_INET6;
  235. else if (!strchr(ip, ':') && !strchr(ipmask, ':'))
  236. aftype = ipaddr.family = maskaddr.family = AF_INET;
  237. else
  238. return 0;
  239. if (aftype == AF_INET6) {
  240. inet_pton(aftype, ip, &ipaddr.u.ipv6.sin6_addr);
  241. inet_pton(aftype, ipmask, &maskaddr.u.ipv6.sin6_addr);
  242. if (comp_with_mask(&ipaddr.u.ipv6.sin6_addr.s6_addr, &maskaddr.u.ipv6.sin6_addr.s6_addr, cidrlen) &&
  243. wild_match(mask, address))
  244. return 1;
  245. } else if (aftype == AF_INET) {
  246. inet_pton(aftype, ip, &ipaddr.u.ipv4.sin_addr);
  247. inet_pton(aftype, ipmask, &maskaddr.u.ipv4.sin_addr);
  248. if (comp_with_mask(&ipaddr.u.ipv4.sin_addr.s_addr, &maskaddr.u.ipv4.sin_addr.s_addr, cidrlen) &&
  249. wild_match(mask, address))
  250. return 1;
  251. }
  252. return 0;
  253. }