You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

378 lines
9.9 KiB

  1. /* rblcheck.c
  2. dnsbl (rbl) checker application
  3. Copyright (C) 2005 Michael Tokarev <mjt@corpit.ru>
  4. This file is part of UDNS library, an async DNS stub resolver.
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library, in file named COPYING.LGPL; if not,
  15. write to the Free Software Foundation, Inc., 59 Temple Place,
  16. Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include "config.h"
  20. #endif
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #ifdef __MINGW32__
  25. # include <winsock2.h>
  26. #else
  27. # include <unistd.h>
  28. # include <sys/types.h>
  29. # include <sys/socket.h>
  30. # include <netinet/in.h>
  31. #endif
  32. #include <time.h>
  33. #include <errno.h>
  34. #include <stdarg.h>
  35. #include "udns.h"
  36. #ifndef HAVE_GETOPT
  37. # include "getopt.c"
  38. #endif
  39. static const char *version = "udns-rblcheck 0.4";
  40. static char *progname;
  41. static void error(int die, const char *fmt, ...) {
  42. va_list ap;
  43. fprintf(stderr, "%s: ", progname);
  44. va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap);
  45. putc('\n', stderr);
  46. fflush(stderr);
  47. if (die)
  48. exit(1);
  49. }
  50. struct rblookup {
  51. struct ipcheck *parent;
  52. struct in_addr key;
  53. const char *zone;
  54. struct dns_rr_a4 *addr;
  55. struct dns_rr_txt *txt;
  56. };
  57. struct ipcheck {
  58. const char *name;
  59. int naddr;
  60. int listed;
  61. struct rblookup *lookup;
  62. };
  63. #define notlisted ((void*)1)
  64. static int nzones, nzalloc;
  65. static const char **zones;
  66. static int do_txt;
  67. static int stopfirst;
  68. static int verbose = 1;
  69. /* verbosity level:
  70. * <0 - only bare As/TXTs
  71. * 0 - what RBL result
  72. * 1(default) - what is listed by RBL: result
  73. * 2 - what is[not ]listed by RBL: result, name lookups
  74. */
  75. static int listed;
  76. static int failures;
  77. static void *ecalloc(int size, int cnt) {
  78. void *t = calloc(size, cnt);
  79. if (!t)
  80. error(1, "out of memory");
  81. return t;
  82. }
  83. static void addzone(const char *zone) {
  84. if (nzones >= nzalloc) {
  85. const char **zs = (const char**)ecalloc(sizeof(char*), (nzalloc += 16));
  86. if (zones) {
  87. memcpy(zs, zones, nzones * sizeof(char*));
  88. free(zones);
  89. }
  90. zones = zs;
  91. }
  92. zones[nzones++] = zone;
  93. }
  94. static int addzonefile(const char *fname) {
  95. FILE *f = fopen(fname, "r");
  96. char linebuf[2048];
  97. if (!f)
  98. return 0;
  99. while(fgets(linebuf, sizeof(linebuf), f)) {
  100. char *p = linebuf, *e;
  101. while(*p == ' ' || *p == '\t') ++p;
  102. if (*p == '#' || *p == '\n') continue;
  103. e = p;
  104. while(*e && *e != ' ' && *e != '\t' && *e != '\n')
  105. ++e;
  106. *e++ = '\0';
  107. p = memcpy(ecalloc(e - p, 1), p, e - p); // strdup
  108. addzone(p);
  109. }
  110. fclose(f);
  111. return 1;
  112. }
  113. static void dnserror(struct rblookup *ipl, const char *what) {
  114. char buf[4*4];
  115. error(0, "unable to %s for %s (%s): %s",
  116. what, dns_ntop(AF_INET, &ipl->key, buf, sizeof(buf)),
  117. ipl->zone, dns_strerror(dns_status(0)));
  118. ++failures;
  119. }
  120. static void display_result(struct ipcheck *ipc) {
  121. int j;
  122. struct rblookup *l, *le;
  123. char buf[4*4];
  124. if (!ipc->naddr) return;
  125. for (l = ipc->lookup, le = l + nzones * ipc->naddr; l < le; ++l) {
  126. if (!l->addr) continue;
  127. if (verbose < 2 && l->addr == notlisted) continue;
  128. if (verbose >= 0) {
  129. dns_ntop(AF_INET, &l->key, buf, sizeof(buf));
  130. if (ipc->name) printf("%s[%s]", ipc->name, buf);
  131. else printf("%s", buf);
  132. }
  133. if (l->addr == notlisted) {
  134. printf(" is NOT listed by %s\n", l->zone);
  135. continue;
  136. }
  137. else if (verbose >= 1)
  138. printf(" is listed by %s: ", l->zone);
  139. else if (verbose >= 0)
  140. printf(" %s ", l->zone);
  141. if (verbose >= 1 || !do_txt)
  142. for (j = 0; j < l->addr->dnsa4_nrr; ++j)
  143. printf("%s%s", j ? " " : "",
  144. dns_ntop(AF_INET, &l->addr->dnsa4_addr[j], buf, sizeof(buf)));
  145. if (!do_txt) ;
  146. else if (l->txt) {
  147. for(j = 0; j < l->txt->dnstxt_nrr; ++j) {
  148. unsigned char *t = l->txt->dnstxt_txt[j].txt;
  149. unsigned char *e = t + l->txt->dnstxt_txt[j].len;
  150. printf("%s\"", verbose > 0 ? "\n\t" : j ? " " : "");
  151. while(t < e) {
  152. if (*t < ' ' || *t >= 127) printf("\\x%02x", *t);
  153. else if (*t == '\\' || *t == '"') printf("\\%c", *t);
  154. else putchar(*t);
  155. ++t;
  156. }
  157. putchar('"');
  158. }
  159. free(l->txt);
  160. }
  161. else
  162. printf("%s<no text available>", verbose > 0 ? "\n\t" : "");
  163. free(l->addr);
  164. putchar('\n');
  165. }
  166. free(ipc->lookup);
  167. }
  168. static void txtcb(struct dns_ctx *ctx, struct dns_rr_txt *r, void *data) {
  169. struct rblookup *ipl = data;
  170. if (r) {
  171. ipl->txt = r;
  172. ++ipl->parent->listed;
  173. }
  174. else if (dns_status(ctx) != DNS_E_NXDOMAIN)
  175. dnserror(ipl, "lookup DNSBL TXT record");
  176. }
  177. static void a4cb(struct dns_ctx *ctx, struct dns_rr_a4 *r, void *data) {
  178. struct rblookup *ipl = data;
  179. if (r) {
  180. ipl->addr = r;
  181. ++listed;
  182. if (do_txt) {
  183. if (dns_submit_a4dnsbl_txt(0, &ipl->key, ipl->zone, txtcb, ipl))
  184. return;
  185. dnserror(ipl, "submit DNSBL TXT record");
  186. }
  187. ++ipl->parent->listed;
  188. }
  189. else if (dns_status(ctx) != DNS_E_NXDOMAIN)
  190. dnserror(ipl, "lookup DNSBL A record");
  191. else
  192. ipl->addr = notlisted;
  193. }
  194. static int
  195. submit_a_queries(struct ipcheck *ipc,
  196. int naddr, const struct in_addr *addr) {
  197. int z, a;
  198. struct rblookup *rl = ecalloc(sizeof(*rl), nzones * naddr);
  199. ipc->lookup = rl;
  200. ipc->naddr = naddr;
  201. for(a = 0; a < naddr; ++a) {
  202. for(z = 0; z < nzones; ++z) {
  203. rl->key = addr[a];
  204. rl->zone = zones[z];
  205. rl->parent = ipc;
  206. if (!dns_submit_a4dnsbl(0, &rl->key, rl->zone, a4cb, rl))
  207. dnserror(rl, "submit DNSBL A query");
  208. ++rl;
  209. }
  210. }
  211. return 0;
  212. }
  213. static void namecb(struct dns_ctx *ctx, struct dns_rr_a4 *rr, void *data) {
  214. struct ipcheck *ipc = data;
  215. if (rr) {
  216. submit_a_queries(ipc, rr->dnsa4_nrr, rr->dnsa4_addr);
  217. free(rr);
  218. }
  219. else {
  220. error(0, "unable to lookup `%s': %s",
  221. ipc->name, dns_strerror(dns_status(ctx)));
  222. ++failures;
  223. }
  224. }
  225. static int submit(struct ipcheck *ipc) {
  226. struct in_addr addr;
  227. if (dns_pton(AF_INET, ipc->name, &addr) > 0) {
  228. submit_a_queries(ipc, 1, &addr);
  229. ipc->name = NULL;
  230. }
  231. else if (!dns_submit_a4(0, ipc->name, 0, namecb, ipc)) {
  232. error(0, "unable to submit name query for %s: %s\n",
  233. ipc->name, dns_strerror(dns_status(0)));
  234. ++failures;
  235. }
  236. return 0;
  237. }
  238. static void waitdns(struct ipcheck *ipc) {
  239. struct timeval tv;
  240. fd_set fds;
  241. int c;
  242. int fd = dns_sock(NULL);
  243. time_t now = 0;
  244. FD_ZERO(&fds);
  245. while((c = dns_timeouts(NULL, -1, now)) > 0) {
  246. FD_SET(fd, &fds);
  247. tv.tv_sec = c;
  248. tv.tv_usec = 0;
  249. c = select(fd+1, &fds, NULL, NULL, &tv);
  250. now = time(NULL);
  251. if (c > 0)
  252. dns_ioevent(NULL, now);
  253. if (stopfirst && ipc->listed)
  254. break;
  255. }
  256. }
  257. int main(int argc, char **argv) {
  258. int c;
  259. struct ipcheck ipc;
  260. char *nameserver = NULL;
  261. int zgiven = 0;
  262. if (!(progname = strrchr(argv[0], '/'))) progname = argv[0];
  263. else argv[0] = ++progname;
  264. while((c = getopt(argc, argv, "hqtvms:S:cn:")) != EOF) switch(c) {
  265. case 's': ++zgiven; addzone(optarg); break;
  266. case 'S':
  267. ++zgiven;
  268. if (addzonefile(optarg)) break;
  269. error(1, "unable to read zonefile `%s'", optarg);
  270. case 'c': ++zgiven; nzones = 0; break;
  271. case 'q': --verbose; break;
  272. case 'v': ++verbose; break;
  273. case 't': do_txt = 1; break;
  274. case 'n': nameserver = optarg; break;
  275. case 'm': ++stopfirst; break;
  276. case 'h':
  277. printf("%s: %s (udns library version %s).\n",
  278. progname, version, dns_version());
  279. printf("Usage is: %s [options] address..\n", progname);
  280. printf(
  281. "Where options are:\n"
  282. " -h - print this help and exit\n"
  283. " -s service - add the service (DNSBL zone) to the serice list\n"
  284. " -S service-file - add the DNSBL zone(s) read from the given file\n"
  285. " -c - clear service list\n"
  286. " -v - increase verbosity level (more -vs => more verbose)\n"
  287. " -q - decrease verbosity level (opposite of -v)\n"
  288. " -t - obtain and print TXT records if any\n"
  289. " -m - stop checking after first address match in any list\n"
  290. " -n ipaddr - use the given nameserver instead of the default\n"
  291. "(if no -s or -S option is given, use $RBLCHECK_ZONES, ~/.rblcheckrc\n"
  292. "or /etc/rblcheckrc in that order)\n"
  293. );
  294. return 0;
  295. default:
  296. error(1, "use `%s -h' for help", progname);
  297. }
  298. if (!zgiven) {
  299. char *s = getenv("RBLCHECK_ZONES");
  300. if (s) {
  301. char *k;
  302. s = strdup(s);
  303. for(k = strtok(s, " \t"); k; k = strtok(NULL, " \t"))
  304. addzone(k);
  305. free(s);
  306. }
  307. else { /* probably worthless on windows? */
  308. char *path;
  309. char *home = getenv("HOME");
  310. if (!home) home = ".";
  311. path = malloc(strlen(home) + 1 + sizeof(".rblcheckrc"));
  312. sprintf(path, "%s/.rblcheckrc", home);
  313. if (!addzonefile(path))
  314. addzonefile("/etc/rblcheckrc");
  315. free(path);
  316. }
  317. }
  318. if (!nzones)
  319. error(1, "no service (zone) list specified (-s or -S option)");
  320. argv += optind;
  321. argc -= optind;
  322. if (!argc)
  323. return 0;
  324. if (dns_init(NULL, 0) < 0)
  325. error(1, "unable to initialize DNS library: %s", strerror(errno));
  326. if (nameserver) {
  327. dns_add_serv(NULL, NULL);
  328. if (dns_add_serv(NULL, nameserver) < 0)
  329. error(1, "wrong IP address for a nameserver: `%s'", nameserver);
  330. }
  331. if (dns_open(NULL) < 0)
  332. error(1, "unable to initialize DNS library: %s", strerror(errno));
  333. for (c = 0; c < argc; ++c) {
  334. if (c && (verbose > 1 || (verbose == 1 && do_txt))) putchar('\n');
  335. memset(&ipc, 0, sizeof(ipc));
  336. ipc.name = argv[c];
  337. submit(&ipc);
  338. waitdns(&ipc);
  339. display_result(&ipc);
  340. if (stopfirst > 1 && listed) break;
  341. }
  342. return listed ? 100 : failures ? 2 : 0;
  343. }