3 * - look up the A record of hosts in an Exim log that failed HELO verification
7 * Copyright (C) 2004 Tony Finch <dot@dotat.at>
9 * It is part of adns, which is
10 * Copyright (C) 1997-2000,2003,2006 Ian Jackson
11 * Copyright (C) 1999-2000,2003,2006 Tony Finch
12 * Copyright (C) 1991 Massachusetts Institute of Technology
13 * (See the file INSTALL for full details.)
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 3, or (at your option)
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software Foundation.
28 * This file is by Tony Finch, based on adnslogres.c.
31 static const char * const cvsid =
34 #include <sys/types.h>
35 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
53 #ifdef ADNS_REGRESS_TEST
54 # include "hredirect.h"
57 /* maximum number of concurrent DNS queries */
58 #define MAXMAXPENDING 64000
59 #define DEFMAXPENDING 2000
61 /* maximum length of a line */
68 static const char *const progname= "adnsheloex";
69 static const char *config_text;
71 #define guard_null(str) ((str) ? (str) : "")
73 #define sensible_ctype(type,ch) (type((unsigned char)(ch)))
74 /* isfoo() functions from ctype.h can't safely be fed char - blech ! */
76 static void msg(const char *fmt, ...) {
79 fprintf(stderr, "%s: ", progname);
81 vfprintf(stderr, fmt, al);
86 static void aargh(const char *cause) {
87 const char *why = strerror(errno);
88 if (!why) why = "Unknown error";
89 msg("%s: %s (%d)", cause, why, errno);
93 typedef struct logline {
95 char *start, *name, *rest, *addr;
99 static logline *readline(FILE *inf, adns_state adns, int opts) {
100 static char buf[MAXLINE];
101 char *str, *p, *q, *r;
104 if (fgets(buf, MAXLINE, inf)) {
105 str= malloc(sizeof(*line) + strlen(buf) + 1);
106 if (!str) aargh("malloc");
109 line->start= str+sizeof(logline);
110 strcpy(line->start, buf);
111 line->name= line->rest= line->addr= NULL;
112 /* look for unverifiable HELO information matching the regex
113 H=[a-z0-9.- ]*[(][a-z0-9.-]*[)] [[][0-9.]*[]] */
114 for (p= strchr(line->start, ' '); p; p= strchr(p+1, ' ')) {
115 if (!strncmp(p, " H=", 3)) {
119 if (!q || q>r) break;
121 if (!p || p>q) break;
130 if (opts & OPT_DEBUG)
131 msg("submitting %s", line->name);
132 if (adns_submit(adns, line->name, adns_r_a,
133 adns_qf_quoteok_query|adns_qf_quoteok_cname|adns_qf_cname_loose,
135 aargh("adns_submit");
138 if (opts & OPT_DEBUG)
149 static void proclog(FILE *inf, FILE *outf, int maxpending, int opts) {
153 logline *head, *tail, *line;
154 adns_initflags initflags;
156 initflags= (opts & OPT_DEBUG) ? adns_if_debug : 0;
158 errno= adns_init_strcfg(&adns, initflags, stderr, config_text);
160 errno= adns_init(&adns, initflags, 0);
162 if (errno) aargh("adns_init");
163 head= tail= readline(inf, adns, opts);
168 if (opts & OPT_DEBUG)
169 msg("%d in queue; checking %.*s", len,
170 (int)(head->rest-head->name), guard_null(head->name));
171 if (eof || len >= maxpending) {
173 err= adns_wait_poll(adns, &head->query, &answer, NULL);
175 err= adns_wait(adns, &head->query, &answer, NULL);
177 err= adns_check(adns, &head->query, &answer, NULL);
179 if (err == EAGAIN) break;
181 fprintf(stderr, "%s: adns_wait/check: %s", progname, strerror(err));
184 if (answer->status == adns_s_ok) {
187 fprintf(outf, "%.*s", (int)(head->rest-head->start), head->start);
188 while(answer->nrrs--) {
189 addr= inet_ntoa(answer->rrs.inaddr[answer->nrrs]);
190 ok |= !strncmp(addr, head->addr, strlen(addr));
191 fprintf(outf, " [%s]", addr);
193 fprintf(outf, "%s%s", ok ? " OK" : "", head->rest);
195 if (opts & OPT_DEBUG)
197 fputs(head->start, outf);
202 if (opts & OPT_DEBUG)
203 msg("%d in queue; no query on this line", len);
204 fputs(head->start, outf);
206 line= head; head= head->next;
210 line= readline(inf, adns, opts);
212 if (!head) head= line;
213 else tail->next= line;
215 if (line->query) len++;
224 static void printhelp(FILE *file) {
225 fputs("usage: adnsheloex [<options>] [<logfile>]\n"
226 " adnsheloex --version|--help\n"
227 "options: -c <concurrency> set max number of outstanding queries\n"
228 " -p use poll(2) instead of select(2)\n"
229 " -d turn on debugging\n"
230 " -C <config> use instead of contents of resolv.conf\n",
234 static void usage(void) {
239 int main(int argc, char *argv[]) {
240 int c, opts, maxpending;
244 if (argv[1] && !strncmp(argv[1],"--",2)) {
245 if (!strcmp(argv[1],"--help")) {
247 } else if (!strcmp(argv[1],"--version")) {
248 fputs(VERSION_MESSAGE("adnsheloex"),stdout);
252 if (ferror(stdout) || fclose(stdout)) { perror("stdout"); exit(1); }
256 maxpending= DEFMAXPENDING;
258 while ((c= getopt(argc, argv, "c:C:dp")) != -1)
261 maxpending= atoi(optarg);
262 if (maxpending < 1 || maxpending > MAXMAXPENDING) {
263 fprintf(stderr, "%s: unfeasible concurrency %d\n", progname, maxpending);
287 inf= fopen(*argv, "r");
292 aargh("couldn't open input");
294 proclog(inf, stdout, maxpending, opts);
297 aargh("fclose input");
299 aargh("fclose output");