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 2, 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,
27 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 * This file is by Tony Finch, based on adnslogres.c.
32 static const char * const cvsid
=
35 #include <sys/types.h>
36 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
54 #ifdef ADNS_REGRESS_TEST
55 # include "hredirect.h"
58 /* maximum number of concurrent DNS queries */
59 #define MAXMAXPENDING 64000
60 #define DEFMAXPENDING 2000
62 /* maximum length of a line */
69 static const char *const progname
= "adnsheloex";
70 static const char *config_text
;
72 #define guard_null(str) ((str) ? (str) : "")
74 #define sensible_ctype(type,ch) (type((unsigned char)(ch)))
75 /* isfoo() functions from ctype.h can't safely be fed char - blech ! */
77 static void msg(const char *fmt
, ...) {
80 fprintf(stderr
, "%s: ", progname
);
82 vfprintf(stderr
, fmt
, al
);
87 static void aargh(const char *cause
) {
88 const char *why
= strerror(errno
);
89 if (!why
) why
= "Unknown error";
90 msg("%s: %s (%d)", cause
, why
, errno
);
94 typedef struct logline
{
96 char *start
, *name
, *rest
, *addr
;
100 static logline
*readline(FILE *inf
, adns_state adns
, int opts
) {
101 static char buf
[MAXLINE
];
102 char *str
, *p
, *q
, *r
;
105 if (fgets(buf
, MAXLINE
, inf
)) {
106 str
= malloc(sizeof(*line
) + strlen(buf
) + 1);
107 if (!str
) aargh("malloc");
110 line
->start
= str
+sizeof(logline
);
111 strcpy(line
->start
, buf
);
112 line
->name
= line
->rest
= line
->addr
= NULL
;
113 /* look for unverifiable HELO information matching the regex
114 H=[a-z0-9.- ]*[(][a-z0-9.-]*[)] [[][0-9.]*[]] */
115 for (p
= strchr(line
->start
, ' '); p
; p
= strchr(p
+1, ' ')) {
116 if (!strncmp(p
, " H=", 3)) {
120 if (!q
|| q
>r
) break;
122 if (!p
|| p
>q
) break;
131 if (opts
& OPT_DEBUG
)
132 msg("submitting %s", line
->name
);
133 if (adns_submit(adns
, line
->name
, adns_r_a
,
134 adns_qf_quoteok_query
|adns_qf_quoteok_cname
|adns_qf_cname_loose
,
136 aargh("adns_submit");
139 if (opts
& OPT_DEBUG
)
150 static void proclog(FILE *inf
, FILE *outf
, int maxpending
, int opts
) {
154 logline
*head
, *tail
, *line
;
155 adns_initflags initflags
;
157 initflags
= (opts
& OPT_DEBUG
) ? adns_if_debug
: 0;
159 errno
= adns_init_strcfg(&adns
, initflags
, stderr
, config_text
);
161 errno
= adns_init(&adns
, initflags
, 0);
163 if (errno
) aargh("adns_init");
164 head
= tail
= readline(inf
, adns
, opts
);
169 if (opts
& OPT_DEBUG
)
170 msg("%d in queue; checking %.*s", len
,
171 (int)(head
->rest
-head
->name
), guard_null(head
->name
));
172 if (eof
|| len
>= maxpending
) {
174 err
= adns_wait_poll(adns
, &head
->query
, &answer
, NULL
);
176 err
= adns_wait(adns
, &head
->query
, &answer
, NULL
);
178 err
= adns_check(adns
, &head
->query
, &answer
, NULL
);
180 if (err
== EAGAIN
) break;
182 fprintf(stderr
, "%s: adns_wait/check: %s", progname
, strerror(err
));
185 if (answer
->status
== adns_s_ok
) {
188 fprintf(outf
, "%.*s", (int)(head
->rest
-head
->start
), head
->start
);
189 while(answer
->nrrs
--) {
190 addr
= inet_ntoa(answer
->rrs
.inaddr
[answer
->nrrs
]);
191 ok
|= !strncmp(addr
, head
->addr
, strlen(addr
));
192 fprintf(outf
, " [%s]", addr
);
194 fprintf(outf
, "%s%s", ok ?
" OK" : "", head
->rest
);
196 if (opts
& OPT_DEBUG
)
198 fputs(head
->start
, outf
);
203 if (opts
& OPT_DEBUG
)
204 msg("%d in queue; no query on this line", len
);
205 fputs(head
->start
, outf
);
207 line
= head
; head
= head
->next
;
211 line
= readline(inf
, adns
, opts
);
213 if (!head
) head
= line
;
214 else tail
->next
= line
;
216 if (line
->query
) len
++;
225 static void printhelp(FILE *file
) {
226 fputs("usage: adnsheloex [<options>] [<logfile>]\n"
227 " adnsheloex --version|--help\n"
228 "options: -c <concurrency> set max number of outstanding queries\n"
229 " -p use poll(2) instead of select(2)\n"
230 " -d turn on debugging\n"
231 " -C <config> use instead of contents of resolv.conf\n",
235 static void usage(void) {
240 int main(int argc
, char *argv
[]) {
241 int c
, opts
, maxpending
;
245 if (argv
[1] && !strncmp(argv
[1],"--",2)) {
246 if (!strcmp(argv
[1],"--help")) {
248 } else if (!strcmp(argv
[1],"--version")) {
249 fputs(VERSION_MESSAGE("adnsheloex"),stdout
);
253 if (ferror(stdout
) || fclose(stdout
)) { perror("stdout"); exit(1); }
257 maxpending
= DEFMAXPENDING
;
259 while ((c
= getopt(argc
, argv
, "c:C:dp")) != -1)
262 maxpending
= atoi(optarg
);
263 if (maxpending
< 1 || maxpending
> MAXMAXPENDING
) {
264 fprintf(stderr
, "%s: unfeasible concurrency %d\n", progname
, maxpending
);
288 inf
= fopen(*argv
, "r");
293 aargh("couldn't open input");
295 proclog(inf
, stdout
, maxpending
, opts
);
298 aargh("fclose input");
300 aargh("fclose output");