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