chiark / gitweb /
Shorten all per-file copyright notices
[adns.git] / client / adnslogres.c
1 /*
2  * adnslogres.c
3  * - a replacement for the Apache logresolve program using adns
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 version was originally supplied by Tony Finch, but has been
23  *  modified by Ian Jackson as it was incorporated into adns and
24  *  subsequently.
25  */
26
27 #include <sys/types.h>
28 #include <sys/time.h>
29
30 #include <unistd.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <ctype.h>
35 #include <errno.h>
36 #include <stdarg.h>
37
38 #include "config.h"
39 #include "adns.h"
40 #include "client.h"
41
42 #ifdef ADNS_REGRESS_TEST
43 # include "hredirect.h"
44 #endif
45
46 /* maximum number of concurrent DNS queries */
47 #define MAXMAXPENDING 64000
48 #define DEFMAXPENDING 2000
49
50 /* maximum length of a line */
51 #define MAXLINE 1024
52
53 /* option flags */
54 #define OPT_DEBUG 1
55 #define OPT_POLL 2
56 #define OPT_CHECKC 4
57
58 static const char *const progname= "adnslogres";
59 static const char *config_text;
60
61 #define guard_null(str) ((str) ? (str) : "")
62
63 #define sensible_ctype(type,ch) (type((unsigned char)(ch)))
64   /* isfoo() functions from ctype.h can't safely be fed char - blech ! */
65
66 static void msg(const char *fmt, ...) {
67   va_list al;
68
69   fprintf(stderr, "%s: ", progname);
70   va_start(al,fmt);
71   vfprintf(stderr, fmt, al);
72   va_end(al);
73   fputc('\n',stderr);
74 }
75
76 static void aargh(const char *cause) {
77   const char *why = strerror(errno);
78   if (!why) why = "Unknown error";
79   msg("%s: %s (%d)", cause, why, errno);
80   exit(1);
81 }
82
83 /*
84  * Parse the IP address and convert to a reverse domain name.
85  */
86 static char *ipaddr2domain(char *start, char **addr, char **rest) {
87   static char buf[30]; /* "123.123.123.123.in-addr.arpa.\0" */
88   char *ptrs[5];
89   int i;
90
91   ptrs[0]= start;
92 retry:
93   while (!sensible_ctype(isdigit,*ptrs[0]))
94     if (!*ptrs[0]++) {
95       strcpy(buf, "invalid.");
96       *addr= *rest= NULL;
97       return buf;
98     }
99   for (i= 1; i < 5; i++) {
100     ptrs[i]= ptrs[i-1];
101     while (sensible_ctype(isdigit,*ptrs[i]++));
102     if ((i == 4 && !sensible_ctype(isspace,ptrs[i][-1])) ||
103         (i != 4 && ptrs[i][-1] != '.') ||
104         (ptrs[i]-ptrs[i-1] > 4)) {
105       ptrs[0]= ptrs[i]-1;
106       goto retry;
107     }
108   }
109   sprintf(buf, "%.*s.%.*s.%.*s.%.*s.in-addr.arpa.",
110           (int)(ptrs[4]-ptrs[3]-1), ptrs[3],
111           (int)(ptrs[3]-ptrs[2]-1), ptrs[2],
112           (int)(ptrs[2]-ptrs[1]-1), ptrs[1],
113           (int)(ptrs[1]-ptrs[0]-1), ptrs[0]);
114   *addr= ptrs[0];
115   *rest= ptrs[4]-1;
116   return buf;
117 }
118
119 static void printline(FILE *outf, char *start, char *addr, char *rest, char *domain) {
120   if (domain)
121     fprintf(outf, "%.*s%s%s", (int)(addr - start), start, domain, rest);
122   else
123     fputs(start, outf);
124   if (ferror(outf)) aargh("write output");
125 }
126
127 typedef struct logline {
128   struct logline *next;
129   char *start, *addr, *rest;
130   adns_query query;
131 } logline;
132
133 static logline *readline(FILE *inf, adns_state adns, int opts) {
134   static char buf[MAXLINE];
135   char *str;
136   logline *line;
137
138   if (fgets(buf, MAXLINE, inf)) {
139     str= malloc(sizeof(*line) + strlen(buf) + 1);
140     if (!str) aargh("malloc");
141     line= (logline*)str;
142     line->next= NULL;
143     line->start= str+sizeof(logline);
144     strcpy(line->start, buf);
145     str= ipaddr2domain(line->start, &line->addr, &line->rest);
146     if (opts & OPT_DEBUG)
147       msg("submitting %.*s -> %s", (int)(line->rest-line->addr), guard_null(line->addr), str);
148     if (adns_submit(adns, str, adns_r_ptr,
149                     adns_qf_quoteok_cname|adns_qf_cname_loose,
150                     NULL, &line->query))
151       aargh("adns_submit");
152     return line;
153   }
154   if (!feof(inf))
155     aargh("fgets");
156   return NULL;
157 }
158         
159 static void proclog(FILE *inf, FILE *outf, int maxpending, int opts) {
160   int eof, err, len;
161   adns_state adns;
162   adns_answer *answer;
163   logline *head, *tail, *line;
164   adns_initflags initflags;
165
166   initflags= (((opts & OPT_DEBUG) ? adns_if_debug : 0) |
167               ((opts & OPT_CHECKC) ? adns_if_checkc_entex : 0));
168   if (config_text) {
169     errno= adns_init_strcfg(&adns, initflags, stderr, config_text);
170   } else {
171     errno= adns_init(&adns, initflags, 0);
172   }
173   if (errno) aargh("adns_init");
174   head= tail= readline(inf, adns, opts);
175   len= 1; eof= 0;
176   while (head) {
177     while (head) {
178       if (opts & OPT_DEBUG)
179         msg("%d in queue; checking %.*s", len,
180             (int)(head->rest-head->addr), guard_null(head->addr));
181       if (eof || len >= maxpending) {
182         if (opts & OPT_POLL)
183           err= adns_wait_poll(adns, &head->query, &answer, NULL);
184         else
185           err= adns_wait(adns, &head->query, &answer, NULL);
186       } else {
187         err= adns_check(adns, &head->query, &answer, NULL);
188       }
189       if (err == EAGAIN) break;
190       if (err) {
191         fprintf(stderr, "%s: adns_wait/check: %s", progname, strerror(err));
192         exit(1);
193       }
194       printline(outf, head->start, head->addr, head->rest,
195                 answer->status == adns_s_ok ? *answer->rrs.str : NULL);
196       line= head; head= head->next;
197       free(line);
198       free(answer);
199       len--;
200     }
201     if (!eof) {
202       line= readline(inf, adns, opts);
203       if (line) {
204         if (!head) head= line;
205         else tail->next= line;
206         tail= line; len++;
207       } else {
208         eof= 1;
209       }
210     }
211   }
212   adns_finish(adns);
213 }
214
215 static void printhelp(FILE *file) {
216   fputs("usage: adnslogres [<options>] [<logfile>]\n"
217         "       adnslogres --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   opts= 0;
236
237   if (argv[1] && !strcmp(argv[1],"--checkc-freq")) {
238     opts|= OPT_CHECKC;
239     argv++; argc--;
240   }
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("adnslogres"),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   while ((c= getopt(argc, argv, "c:C:dp")) != -1)
256     switch (c) {
257     case 'c':
258       maxpending= atoi(optarg);
259       if (maxpending < 1 || maxpending > MAXMAXPENDING) {
260        fprintf(stderr, "%s: unfeasible concurrency %d\n", progname, maxpending);
261        exit(1);
262       }
263       break;
264     case 'C':
265       config_text= optarg;
266       break;
267     case 'd':
268       opts|= OPT_DEBUG;
269       break;
270     case 'p':
271       opts|= OPT_POLL;
272       break;
273     default:
274       usage();
275     }
276
277   argc-= optind;
278   argv+= optind;
279
280   inf= NULL;
281   if (argc == 0)
282     inf= stdin;
283   else if (argc == 1)
284     inf= fopen(*argv, "r");
285   else
286     usage();
287
288   if (!inf)
289     aargh("couldn't open input");
290
291   proclog(inf, stdout, maxpending, opts);
292
293   if (fclose(inf))
294     aargh("fclose input");
295   if (fclose(stdout))
296     aargh("fclose output");
297
298   return 0;
299 }