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