849075a0 |
1 | /* |
2 | * adnsheloex.c |
3 | * - look up the A record of hosts in an Exim log that failed HELO verification |
4 | */ |
5 | /* |
6 | * This file is |
7 | * Copyright (C) 2004 Tony Finch <dot@dotat.at> |
8 | * |
9 | * It is part of adns, which is |
ae8cc977 |
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.) |
849075a0 |
14 | * |
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 |
7f8bbe29 |
17 | * the Free Software Foundation; either version 3, or (at your option) |
849075a0 |
18 | * any later version. |
19 | * |
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. |
24 | * |
25 | * You should have received a copy of the GNU General Public License |
8c09a4c6 |
26 | * along with this program; if not, write to the Free Software Foundation. |
849075a0 |
27 | * |
28 | * This file is by Tony Finch, based on adnslogres.c. |
29 | */ |
30 | |
31 | static const char * const cvsid = |
32 | "$Id$"; |
33 | |
34 | #include <sys/types.h> |
35 | #include <sys/socket.h> |
36 | #include <sys/time.h> |
37 | |
38 | #include <netinet/in.h> |
39 | #include <arpa/inet.h> |
40 | |
41 | #include <unistd.h> |
42 | #include <string.h> |
43 | #include <stdlib.h> |
44 | #include <stdio.h> |
45 | #include <ctype.h> |
46 | #include <errno.h> |
47 | #include <stdarg.h> |
48 | |
49 | #include "config.h" |
50 | #include "adns.h" |
51 | #include "client.h" |
52 | |
53 | #ifdef ADNS_REGRESS_TEST |
54 | # include "hredirect.h" |
55 | #endif |
56 | |
57 | /* maximum number of concurrent DNS queries */ |
58 | #define MAXMAXPENDING 64000 |
59 | #define DEFMAXPENDING 2000 |
60 | |
61 | /* maximum length of a line */ |
62 | #define MAXLINE 1024 |
63 | |
64 | /* option flags */ |
65 | #define OPT_DEBUG 1 |
66 | #define OPT_POLL 2 |
67 | |
68 | static const char *const progname= "adnsheloex"; |
69 | static const char *config_text; |
70 | |
71 | #define guard_null(str) ((str) ? (str) : "") |
72 | |
73 | #define sensible_ctype(type,ch) (type((unsigned char)(ch))) |
74 | /* isfoo() functions from ctype.h can't safely be fed char - blech ! */ |
75 | |
76 | static void msg(const char *fmt, ...) { |
77 | va_list al; |
78 | |
79 | fprintf(stderr, "%s: ", progname); |
80 | va_start(al,fmt); |
81 | vfprintf(stderr, fmt, al); |
82 | va_end(al); |
83 | fputc('\n',stderr); |
84 | } |
85 | |
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); |
90 | exit(1); |
91 | } |
92 | |
93 | typedef struct logline { |
94 | struct logline *next; |
95 | char *start, *name, *rest, *addr; |
96 | adns_query query; |
97 | } logline; |
98 | |
99 | static logline *readline(FILE *inf, adns_state adns, int opts) { |
100 | static char buf[MAXLINE]; |
101 | char *str, *p, *q, *r; |
102 | logline *line; |
103 | |
104 | if (fgets(buf, MAXLINE, inf)) { |
105 | str= malloc(sizeof(*line) + strlen(buf) + 1); |
106 | if (!str) aargh("malloc"); |
107 | line= (logline*)str; |
108 | line->next= NULL; |
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)) { |
116 | r= strchr(p, '['); |
117 | if (!r) break; |
118 | q= strchr(p, ')'); |
119 | if (!q || q>r) break; |
120 | p= strchr(p, '('); |
121 | if (!p || p>q) break; |
122 | line->name= p+1; |
123 | line->rest= q; |
124 | line->addr= r+1; |
125 | break; |
126 | } |
127 | } |
128 | if (line->name) { |
129 | *line->rest= '\0'; |
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, |
134 | NULL, &line->query)) |
135 | aargh("adns_submit"); |
136 | *line->rest= ')'; |
137 | } else { |
138 | if (opts & OPT_DEBUG) |
139 | msg("no query"); |
140 | line->query= NULL; |
141 | } |
142 | return line; |
143 | } |
144 | if (!feof(inf)) |
145 | aargh("fgets"); |
146 | return NULL; |
147 | } |
148 | |
149 | static void proclog(FILE *inf, FILE *outf, int maxpending, int opts) { |
150 | int eof, err, len; |
151 | adns_state adns; |
152 | adns_answer *answer; |
153 | logline *head, *tail, *line; |
154 | adns_initflags initflags; |
155 | |
156 | initflags= (opts & OPT_DEBUG) ? adns_if_debug : 0; |
157 | if (config_text) { |
158 | errno= adns_init_strcfg(&adns, initflags, stderr, config_text); |
159 | } else { |
160 | errno= adns_init(&adns, initflags, 0); |
161 | } |
162 | if (errno) aargh("adns_init"); |
163 | head= tail= readline(inf, adns, opts); |
164 | len= 1; eof= 0; |
165 | while (head) { |
166 | while (head) { |
167 | if (head->query) { |
168 | if (opts & OPT_DEBUG) |
169 | msg("%d in queue; checking %.*s", len, |
bf41c1c2 |
170 | (int)(head->rest-head->name), guard_null(head->name)); |
849075a0 |
171 | if (eof || len >= maxpending) { |
172 | if (opts & OPT_POLL) |
173 | err= adns_wait_poll(adns, &head->query, &answer, NULL); |
174 | else |
175 | err= adns_wait(adns, &head->query, &answer, NULL); |
176 | } else { |
177 | err= adns_check(adns, &head->query, &answer, NULL); |
178 | } |
179 | if (err == EAGAIN) break; |
180 | if (err) { |
181 | fprintf(stderr, "%s: adns_wait/check: %s", progname, strerror(err)); |
182 | exit(1); |
183 | } |
184 | if (answer->status == adns_s_ok) { |
185 | const char *addr; |
186 | int ok = 0; |
bf41c1c2 |
187 | fprintf(outf, "%.*s", (int)(head->rest-head->start), head->start); |
849075a0 |
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); |
192 | } |
193 | fprintf(outf, "%s%s", ok ? " OK" : "", head->rest); |
194 | } else { |
195 | if (opts & OPT_DEBUG) |
196 | msg("query failed"); |
197 | fputs(head->start, outf); |
198 | } |
199 | free(answer); |
200 | len--; |
201 | } else { |
202 | if (opts & OPT_DEBUG) |
203 | msg("%d in queue; no query on this line", len); |
204 | fputs(head->start, outf); |
205 | } |
206 | line= head; head= head->next; |
207 | free(line); |
208 | } |
209 | if (!eof) { |
210 | line= readline(inf, adns, opts); |
211 | if (line) { |
212 | if (!head) head= line; |
213 | else tail->next= line; |
214 | tail= line; |
215 | if (line->query) len++; |
216 | } else { |
217 | eof= 1; |
218 | } |
219 | } |
220 | } |
221 | adns_finish(adns); |
222 | } |
223 | |
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", |
231 | stdout); |
232 | } |
233 | |
234 | static void usage(void) { |
235 | printhelp(stderr); |
236 | exit(1); |
237 | } |
238 | |
239 | int main(int argc, char *argv[]) { |
240 | int c, opts, maxpending; |
241 | extern char *optarg; |
242 | FILE *inf; |
243 | |
244 | if (argv[1] && !strncmp(argv[1],"--",2)) { |
245 | if (!strcmp(argv[1],"--help")) { |
246 | printhelp(stdout); |
247 | } else if (!strcmp(argv[1],"--version")) { |
248 | fputs(VERSION_MESSAGE("adnsheloex"),stdout); |
249 | } else { |
250 | usage(); |
251 | } |
252 | if (ferror(stdout) || fclose(stdout)) { perror("stdout"); exit(1); } |
253 | exit(0); |
254 | } |
255 | |
256 | maxpending= DEFMAXPENDING; |
257 | opts= 0; |
258 | while ((c= getopt(argc, argv, "c:C:dp")) != -1) |
259 | switch (c) { |
260 | case 'c': |
261 | maxpending= atoi(optarg); |
262 | if (maxpending < 1 || maxpending > MAXMAXPENDING) { |
263 | fprintf(stderr, "%s: unfeasible concurrency %d\n", progname, maxpending); |
264 | exit(1); |
265 | } |
266 | break; |
267 | case 'C': |
268 | config_text= optarg; |
269 | break; |
270 | case 'd': |
271 | opts|= OPT_DEBUG; |
272 | break; |
273 | case 'p': |
274 | opts|= OPT_POLL; |
275 | break; |
276 | default: |
277 | usage(); |
278 | } |
279 | |
280 | argc-= optind; |
281 | argv+= optind; |
282 | |
283 | inf= NULL; |
284 | if (argc == 0) |
285 | inf= stdin; |
286 | else if (argc == 1) |
287 | inf= fopen(*argv, "r"); |
288 | else |
289 | usage(); |
290 | |
291 | if (!inf) |
292 | aargh("couldn't open input"); |
293 | |
294 | proclog(inf, stdout, maxpending, opts); |
295 | |
296 | if (fclose(inf)) |
297 | aargh("fclose input"); |
298 | if (fclose(stdout)) |
299 | aargh("fclose output"); |
300 | |
301 | return 0; |
302 | } |