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 Ian Jackson <ian@davenant.greenend.org.uk>
11 * Copyright (C) 1999-2004 Tony Finch <dot@dotat.at>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software Foundation,
25 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 * This file is by Tony Finch, based on adnslogres.c.
30 static const char * const cvsid
=
33 #include <sys/types.h>
34 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
52 #ifdef ADNS_REGRESS_TEST
53 # include "hredirect.h"
56 /* maximum number of concurrent DNS queries */
57 #define MAXMAXPENDING 64000
58 #define DEFMAXPENDING 2000
60 /* maximum length of a line */
67 static const char *const progname
= "adnsheloex";
68 static const char *config_text
;
70 #define guard_null(str) ((str) ? (str) : "")
72 #define sensible_ctype(type,ch) (type((unsigned char)(ch)))
73 /* isfoo() functions from ctype.h can't safely be fed char - blech ! */
75 static void msg(const char *fmt
, ...) {
78 fprintf(stderr
, "%s: ", progname
);
80 vfprintf(stderr
, fmt
, al
);
85 static void aargh(const char *cause
) {
86 const char *why
= strerror(errno
);
87 if (!why
) why
= "Unknown error";
88 msg("%s: %s (%d)", cause
, why
, errno
);
92 typedef struct logline
{
94 char *start
, *name
, *rest
, *addr
;
98 static logline
*readline(FILE *inf
, adns_state adns
, int opts
) {
99 static char buf
[MAXLINE
];
100 char *str
, *p
, *q
, *r
;
103 if (fgets(buf
, MAXLINE
, inf
)) {
104 str
= malloc(sizeof(*line
) + strlen(buf
) + 1);
105 if (!str
) aargh("malloc");
108 line
->start
= str
+sizeof(logline
);
109 strcpy(line
->start
, buf
);
110 line
->name
= line
->rest
= line
->addr
= NULL
;
111 /* look for unverifiable HELO information matching the regex
112 H=[a-z0-9.- ]*[(][a-z0-9.-]*[)] [[][0-9.]*[]] */
113 for (p
= strchr(line
->start
, ' '); p
; p
= strchr(p
+1, ' ')) {
114 if (!strncmp(p
, " H=", 3)) {
118 if (!q
|| q
>r
) break;
120 if (!p
|| p
>q
) break;
129 if (opts
& OPT_DEBUG
)
130 msg("submitting %s", line
->name
);
131 if (adns_submit(adns
, line
->name
, adns_r_a
,
132 adns_qf_quoteok_query
|adns_qf_quoteok_cname
|adns_qf_cname_loose
,
134 aargh("adns_submit");
137 if (opts
& OPT_DEBUG
)
148 static void proclog(FILE *inf
, FILE *outf
, int maxpending
, int opts
) {
152 logline
*head
, *tail
, *line
;
153 adns_initflags initflags
;
155 initflags
= (opts
& OPT_DEBUG
) ? adns_if_debug
: 0;
157 errno
= adns_init_strcfg(&adns
, initflags
, stderr
, config_text
);
159 errno
= adns_init(&adns
, initflags
, 0);
161 if (errno
) aargh("adns_init");
162 head
= tail
= readline(inf
, adns
, opts
);
167 if (opts
& OPT_DEBUG
)
168 msg("%d in queue; checking %.*s", len
,
169 head
->rest
-head
->name
, guard_null(head
->name
));
170 if (eof
|| len
>= maxpending
) {
172 err
= adns_wait_poll(adns
, &head
->query
, &answer
, NULL
);
174 err
= adns_wait(adns
, &head
->query
, &answer
, NULL
);
176 err
= adns_check(adns
, &head
->query
, &answer
, NULL
);
178 if (err
== EAGAIN
) break;
180 fprintf(stderr
, "%s: adns_wait/check: %s", progname
, strerror(err
));
183 if (answer
->status
== adns_s_ok
) {
186 fprintf(outf
, "%.*s", head
->rest
-head
->start
, head
->start
);
187 while(answer
->nrrs
--) {
188 addr
= inet_ntoa(answer
->rrs
.inaddr
[answer
->nrrs
]);
189 ok
|= !strncmp(addr
, head
->addr
, strlen(addr
));
190 fprintf(outf
, " [%s]", addr
);
192 fprintf(outf
, "%s%s", ok ?
" OK" : "", head
->rest
);
194 if (opts
& OPT_DEBUG
)
196 fputs(head
->start
, outf
);
201 if (opts
& OPT_DEBUG
)
202 msg("%d in queue; no query on this line", len
);
203 fputs(head
->start
, outf
);
205 line
= head
; head
= head
->next
;
209 line
= readline(inf
, adns
, opts
);
211 if (!head
) head
= line
;
212 else tail
->next
= line
;
214 if (line
->query
) len
++;
223 static void printhelp(FILE *file
) {
224 fputs("usage: adnsheloex [<options>] [<logfile>]\n"
225 " adnsheloex --version|--help\n"
226 "options: -c <concurrency> set max number of outstanding queries\n"
227 " -p use poll(2) instead of select(2)\n"
228 " -d turn on debugging\n"
229 " -C <config> use instead of contents of resolv.conf\n",
233 static void usage(void) {
238 int main(int argc
, char *argv
[]) {
239 int c
, opts
, maxpending
;
243 if (argv
[1] && !strncmp(argv
[1],"--",2)) {
244 if (!strcmp(argv
[1],"--help")) {
246 } else if (!strcmp(argv
[1],"--version")) {
247 fputs(VERSION_MESSAGE("adnsheloex"),stdout
);
251 if (ferror(stdout
) || fclose(stdout
)) { perror("stdout"); exit(1); }
255 maxpending
= DEFMAXPENDING
;
257 while ((c
= getopt(argc
, argv
, "c:C:dp")) != -1)
260 maxpending
= atoi(optarg
);
261 if (maxpending
< 1 || maxpending
> MAXMAXPENDING
) {
262 fprintf(stderr
, "%s: unfeasible concurrency %d\n", progname
, maxpending
);
286 inf
= fopen(*argv
, "r");
291 aargh("couldn't open input");
293 proclog(inf
, stdout
, maxpending
, opts
);
296 aargh("fclose input");
298 aargh("fclose output");