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