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,2014 Ian Jackson
11 * Copyright (C) 2014 Mark Wooding
12 * Copyright (C) 1999-2000,2003,2006 Tony Finch
13 * Copyright (C) 1991 Massachusetts Institute of Technology
14 * (See the file INSTALL for full details.)
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 3, or (at your option)
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software Foundation.
29 * This file is by Tony Finch, based on adnslogres.c.
32 #include <sys/types.h>
33 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
51 #ifdef ADNS_REGRESS_TEST
52 # include "hredirect.h"
55 /* maximum number of concurrent DNS queries */
56 #define MAXMAXPENDING 64000
57 #define DEFMAXPENDING 2000
59 /* maximum length of a line */
66 static const char *const progname
= "adnsheloex";
67 static const char *config_text
;
69 #define guard_null(str) ((str) ? (str) : "")
71 #define sensible_ctype(type,ch) (type((unsigned char)(ch)))
72 /* isfoo() functions from ctype.h can't safely be fed char - blech ! */
74 static void msg(const char *fmt
, ...) {
77 fprintf(stderr
, "%s: ", progname
);
79 vfprintf(stderr
, fmt
, al
);
84 static void aargh(const char *cause
) {
85 const char *why
= strerror(errno
);
86 if (!why
) why
= "Unknown error";
87 msg("%s: %s (%d)", cause
, why
, errno
);
91 typedef struct logline
{
93 char *start
, *name
, *rest
, *addr
;
97 static logline
*readline(FILE *inf
, adns_state adns
, int opts
) {
98 static char buf
[MAXLINE
];
99 char *str
, *p
, *q
, *r
;
102 if (fgets(buf
, MAXLINE
, inf
)) {
103 str
= malloc(sizeof(*line
) + strlen(buf
) + 1);
104 if (!str
) aargh("malloc");
107 line
->start
= str
+sizeof(logline
);
108 strcpy(line
->start
, buf
);
109 line
->name
= line
->rest
= line
->addr
= NULL
;
110 /* look for unverifiable HELO information matching the regex
111 H=[a-z0-9.- ]*[(][a-z0-9.-]*[)] [[][0-9.]*[]] */
112 for (p
= strchr(line
->start
, ' '); p
; p
= strchr(p
+1, ' ')) {
113 if (!strncmp(p
, " H=", 3)) {
117 if (!q
|| q
>r
) break;
119 if (!p
|| p
>q
) break;
128 if (opts
& OPT_DEBUG
)
129 msg("submitting %s", line
->name
);
130 if (adns_submit(adns
, line
->name
, adns_r_a
,
131 adns_qf_quoteok_query
|adns_qf_quoteok_cname
|adns_qf_cname_loose
,
133 aargh("adns_submit");
136 if (opts
& OPT_DEBUG
)
147 static void proclog(FILE *inf
, FILE *outf
, int maxpending
, int opts
) {
151 logline
*head
, *tail
, *line
;
152 adns_initflags initflags
;
154 initflags
= (opts
& OPT_DEBUG
) ? adns_if_debug
: 0;
156 errno
= adns_init_strcfg(&adns
, initflags
, stderr
, config_text
);
158 errno
= adns_init(&adns
, initflags
, 0);
160 if (errno
) aargh("adns_init");
161 head
= tail
= readline(inf
, adns
, opts
);
166 if (opts
& OPT_DEBUG
)
167 msg("%d in queue; checking %.*s", len
,
168 (int)(head
->rest
-head
->name
), guard_null(head
->name
));
169 if (eof
|| len
>= maxpending
) {
171 err
= adns_wait_poll(adns
, &head
->query
, &answer
, NULL
);
173 err
= adns_wait(adns
, &head
->query
, &answer
, NULL
);
175 err
= adns_check(adns
, &head
->query
, &answer
, NULL
);
177 if (err
== EAGAIN
) break;
179 fprintf(stderr
, "%s: adns_wait/check: %s", progname
, strerror(err
));
182 if (answer
->status
== adns_s_ok
) {
185 fprintf(outf
, "%.*s", (int)(head
->rest
-head
->start
), head
->start
);
186 while(answer
->nrrs
--) {
187 addr
= inet_ntoa(answer
->rrs
.inaddr
[answer
->nrrs
]);
188 ok
|= !strncmp(addr
, head
->addr
, strlen(addr
));
189 fprintf(outf
, " [%s]", addr
);
191 fprintf(outf
, "%s%s", ok ?
" OK" : "", head
->rest
);
193 if (opts
& OPT_DEBUG
)
195 fputs(head
->start
, outf
);
200 if (opts
& OPT_DEBUG
)
201 msg("%d in queue; no query on this line", len
);
202 fputs(head
->start
, outf
);
204 line
= head
; head
= head
->next
;
208 line
= readline(inf
, adns
, opts
);
210 if (!head
) head
= line
;
211 else tail
->next
= line
;
213 if (line
->query
) len
++;
222 static void printhelp(FILE *file
) {
223 fputs("usage: adnsheloex [<options>] [<logfile>]\n"
224 " adnsheloex --version|--help\n"
225 "options: -c <concurrency> set max number of outstanding queries\n"
226 " -p use poll(2) instead of select(2)\n"
227 " -d turn on debugging\n"
228 " -C <config> use instead of contents of resolv.conf\n",
232 static void usage(void) {
237 int main(int argc
, char *argv
[]) {
238 int c
, opts
, maxpending
;
242 if (argv
[1] && !strncmp(argv
[1],"--",2)) {
243 if (!strcmp(argv
[1],"--help")) {
245 } else if (!strcmp(argv
[1],"--version")) {
246 fputs(VERSION_MESSAGE("adnsheloex"),stdout
);
250 if (ferror(stdout
) || fclose(stdout
)) { perror("stdout"); exit(1); }
254 maxpending
= DEFMAXPENDING
;
256 while ((c
= getopt(argc
, argv
, "c:C:dp")) != -1)
259 maxpending
= atoi(optarg
);
260 if (maxpending
< 1 || maxpending
> MAXMAXPENDING
) {
261 fprintf(stderr
, "%s: unfeasible concurrency %d\n", progname
, maxpending
);
285 inf
= fopen(*argv
, "r");
290 aargh("couldn't open input");
292 proclog(inf
, stdout
, maxpending
, opts
);
295 aargh("fclose input");
297 aargh("fclose output");