3 * - look up the A record of hosts in an Exim log that failed HELO verification
6 * This file is part of adns, which is Copyright Ian Jackson
7 * and contributors (see the file INSTALL for full details).
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3, or (at your option)
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation.
22 * This file is by Tony Finch, based on adnslogres.c.
25 #include <sys/types.h>
26 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
44 #ifdef ADNS_REGRESS_TEST
45 # include "hredirect.h"
48 /* maximum number of concurrent DNS queries */
49 #define MAXMAXPENDING 64000
50 #define DEFMAXPENDING 2000
52 /* maximum length of a line */
59 static const char *const progname= "adnsheloex";
60 static const char *config_text;
62 #define guard_null(str) ((str) ? (str) : "")
64 #define sensible_ctype(type,ch) (type((unsigned char)(ch)))
65 /* isfoo() functions from ctype.h can't safely be fed char - blech ! */
67 static void msg(const char *fmt, ...) {
70 fprintf(stderr, "%s: ", progname);
72 vfprintf(stderr, fmt, al);
77 static void aargh(const char *cause) {
78 const char *why = strerror(errno);
79 if (!why) why = "Unknown error";
80 msg("%s: %s (%d)", cause, why, errno);
84 typedef struct logline {
86 char *start, *name, *rest, *addr;
90 static logline *readline(FILE *inf, adns_state adns, int opts) {
91 static char buf[MAXLINE];
92 char *str, *p, *q, *r;
95 if (fgets(buf, MAXLINE, inf)) {
96 str= malloc(sizeof(*line) + strlen(buf) + 1);
97 if (!str) aargh("malloc");
100 line->start= str+sizeof(logline);
101 strcpy(line->start, buf);
102 line->name= line->rest= line->addr= NULL;
103 /* look for unverifiable HELO information matching the regex
104 H=[a-z0-9.- ]*[(][a-z0-9.-]*[)] [[][0-9.]*[]] */
105 for (p= strchr(line->start, ' '); p; p= strchr(p+1, ' ')) {
106 if (!strncmp(p, " H=", 3)) {
110 if (!q || q>r) break;
112 if (!p || p>q) break;
121 if (opts & OPT_DEBUG)
122 msg("submitting %s", line->name);
123 if (adns_submit(adns, line->name, adns_r_a,
124 adns_qf_quoteok_query|adns_qf_quoteok_cname|adns_qf_cname_loose,
126 aargh("adns_submit");
129 if (opts & OPT_DEBUG)
140 static void proclog(FILE *inf, FILE *outf, int maxpending, int opts) {
144 logline *head, *tail, *line;
145 adns_initflags initflags;
147 initflags= (opts & OPT_DEBUG) ? adns_if_debug : 0;
149 errno= adns_init_strcfg(&adns, initflags, stderr, config_text);
151 errno= adns_init(&adns, initflags, 0);
153 if (errno) aargh("adns_init");
154 head= tail= readline(inf, adns, opts);
159 if (opts & OPT_DEBUG)
160 msg("%d in queue; checking %.*s", len,
161 (int)(head->rest-head->name), guard_null(head->name));
162 if (eof || len >= maxpending) {
164 err= adns_wait_poll(adns, &head->query, &answer, NULL);
166 err= adns_wait(adns, &head->query, &answer, NULL);
168 err= adns_check(adns, &head->query, &answer, NULL);
170 if (err == EAGAIN) break;
172 fprintf(stderr, "%s: adns_wait/check: %s", progname, strerror(err));
175 if (answer->status == adns_s_ok) {
178 fprintf(outf, "%.*s", (int)(head->rest-head->start), head->start);
179 while(answer->nrrs--) {
180 addr= inet_ntoa(answer->rrs.inaddr[answer->nrrs]);
181 ok |= !strncmp(addr, head->addr, strlen(addr));
182 fprintf(outf, " [%s]", addr);
184 fprintf(outf, "%s%s", ok ? " OK" : "", head->rest);
186 if (opts & OPT_DEBUG)
188 fputs(head->start, outf);
193 if (opts & OPT_DEBUG)
194 msg("%d in queue; no query on this line", len);
195 fputs(head->start, outf);
197 line= head; head= head->next;
201 line= readline(inf, adns, opts);
203 if (!head) head= line;
204 else tail->next= line;
206 if (line->query) len++;
215 static void printhelp(FILE *file) {
216 fputs("usage: adnsheloex [<options>] [<logfile>]\n"
217 " adnsheloex --version|--help\n"
218 "options: -c <concurrency> set max number of outstanding queries\n"
219 " -p use poll(2) instead of select(2)\n"
220 " -d turn on debugging\n"
221 " -C <config> use instead of contents of resolv.conf\n",
225 static void usage(void) {
230 int main(int argc, char *argv[]) {
231 int c, opts, maxpending;
235 if (argv[1] && !strncmp(argv[1],"--",2)) {
236 if (!strcmp(argv[1],"--help")) {
238 } else if (!strcmp(argv[1],"--version")) {
239 fputs(VERSION_MESSAGE("adnsheloex"),stdout);
243 if (ferror(stdout) || fclose(stdout)) { perror("stdout"); exit(1); }
247 maxpending= DEFMAXPENDING;
249 while ((c= getopt(argc, argv, "c:C:dp")) != -1)
252 maxpending= atoi(optarg);
253 if (maxpending < 1 || maxpending > MAXMAXPENDING) {
254 fprintf(stderr, "%s: unfeasible concurrency %d\n", progname, maxpending);
278 inf= fopen(*argv, "r");
283 aargh("couldn't open input");
285 proclog(inf, stdout, maxpending, opts);
288 aargh("fclose input");
290 aargh("fclose output");