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