chiark / gitweb /
Fix typo in changelog entry for 1.6.1
[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
7  *   Copyright (C) 1999-2000 Tony Finch <dot@dotat.at>
8  *   Copyright (C) 1999-2000,2020 Ian Jackson <ian@davenant.greenend.org.uk>
9  *
10  *  It is part of adns, which is
11  *    Copyright (C) 1997-2000,2003,2006,2014-2016,2020  Ian Jackson
12  *    Copyright (C) 2014  Mark Wooding
13  *    Copyright (C) 1999-2000,2003,2006  Tony Finch
14  *    Copyright (C) 1991 Massachusetts Institute of Technology
15  *  (See the file INSTALL for full details.)
16  *  
17  *  This program is free software; you can redistribute it and/or modify
18  *  it under the terms of the GNU General Public License as published by
19  *  the Free Software Foundation; either version 3, or (at your option)
20  *  any later version.
21  *  
22  *  This program is distributed in the hope that it will be useful,
23  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
24  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  *  GNU General Public License for more details.
26  *  
27  *  You should have received a copy of the GNU General Public License
28  *  along with this program; if not, write to the Free Software Foundation.
29  *
30  *  This version was originally supplied by Tony Finch, but has been
31  *  modified by Ian Jackson as it was incorporated into adns and
32  *  subsequently.
33  */
34
35 #include <sys/types.h>
36 #include <sys/time.h>
37
38 #include <unistd.h>
39 #include <string.h>
40 #include <stdlib.h>
41 #include <stdio.h>
42 #include <ctype.h>
43 #include <errno.h>
44 #include <stdarg.h>
45
46 #include "config.h"
47 #include "adns.h"
48 #include "client.h"
49
50 #ifdef ADNS_REGRESS_TEST
51 # include "hredirect.h"
52 #endif
53
54 /* maximum number of concurrent DNS queries */
55 #define MAXMAXPENDING 64000
56 #define DEFMAXPENDING 2000
57
58 /* maximum length of a line */
59 #define MAXLINE 1024
60
61 /* option flags */
62 #define OPT_DEBUG 1
63 #define OPT_POLL 2
64 #define OPT_CHECKC 4
65
66 static const char *const progname= "adnslogres";
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 /*
92  * Parse the IP address and convert to a reverse domain name.
93  */
94 static char *ipaddr2domain(char *start, char **addr, char **rest) {
95   static char buf[30]; /* "123.123.123.123.in-addr.arpa.\0" */
96   char *ptrs[5];
97   int i;
98
99   ptrs[0]= start;
100 retry:
101   while (!sensible_ctype(isdigit,*ptrs[0]))
102     if (!*ptrs[0]++) {
103       strcpy(buf, "invalid.");
104       *addr= *rest= NULL;
105       return buf;
106     }
107   for (i= 1; i < 5; i++) {
108     ptrs[i]= ptrs[i-1];
109     while (sensible_ctype(isdigit,*ptrs[i]++));
110     if ((i == 4 && !sensible_ctype(isspace,ptrs[i][-1])) ||
111         (i != 4 && ptrs[i][-1] != '.') ||
112         (ptrs[i]-ptrs[i-1] > 4)) {
113       ptrs[0]= ptrs[i]-1;
114       goto retry;
115     }
116   }
117   sprintf(buf, "%.*s.%.*s.%.*s.%.*s.in-addr.arpa.",
118           (int)(ptrs[4]-ptrs[3]-1), ptrs[3],
119           (int)(ptrs[3]-ptrs[2]-1), ptrs[2],
120           (int)(ptrs[2]-ptrs[1]-1), ptrs[1],
121           (int)(ptrs[1]-ptrs[0]-1), ptrs[0]);
122   *addr= ptrs[0];
123   *rest= ptrs[4]-1;
124   return buf;
125 }
126
127 static void printline(FILE *outf, char *start, char *addr, char *rest, char *domain) {
128   if (domain)
129     fprintf(outf, "%.*s%s%s", (int)(addr - start), start, domain, rest);
130   else
131     fputs(start, outf);
132   if (ferror(outf)) aargh("write output");
133 }
134
135 typedef struct logline {
136   struct logline *next;
137   char *start, *addr, *rest;
138   adns_query query;
139 } logline;
140
141 static logline *readline(FILE *inf, adns_state adns, int opts) {
142   static char buf[MAXLINE];
143   char *str;
144   logline *line;
145
146   if (fgets(buf, MAXLINE, inf)) {
147     str= malloc(sizeof(*line) + strlen(buf) + 1);
148     if (!str) aargh("malloc");
149     line= (logline*)str;
150     line->next= NULL;
151     line->start= str+sizeof(logline);
152     strcpy(line->start, buf);
153     str= ipaddr2domain(line->start, &line->addr, &line->rest);
154     if (opts & OPT_DEBUG)
155       msg("submitting %.*s -> %s", (int)(line->rest-line->addr), guard_null(line->addr), str);
156     if (adns_submit(adns, str, adns_r_ptr,
157                     adns_qf_quoteok_cname|adns_qf_cname_loose,
158                     NULL, &line->query))
159       aargh("adns_submit");
160     return line;
161   }
162   if (!feof(inf))
163     aargh("fgets");
164   return NULL;
165 }
166         
167 static void proclog(FILE *inf, FILE *outf, int maxpending, int opts) {
168   int eof, err, len;
169   adns_state adns;
170   adns_answer *answer;
171   logline *head, *tail, *line;
172   adns_initflags initflags;
173
174   initflags= (((opts & OPT_DEBUG) ? adns_if_debug : 0) |
175               ((opts & OPT_CHECKC) ? adns_if_checkc_entex : 0));
176   if (config_text) {
177     errno= adns_init_strcfg(&adns, initflags, stderr, config_text);
178   } else {
179     errno= adns_init(&adns, initflags, 0);
180   }
181   if (errno) aargh("adns_init");
182   head= tail= readline(inf, adns, opts);
183   len= 1; eof= 0;
184   while (head) {
185     while (head) {
186       if (opts & OPT_DEBUG)
187         msg("%d in queue; checking %.*s", len,
188             (int)(head->rest-head->addr), guard_null(head->addr));
189       if (eof || len >= maxpending) {
190         if (opts & OPT_POLL)
191           err= adns_wait_poll(adns, &head->query, &answer, NULL);
192         else
193           err= adns_wait(adns, &head->query, &answer, NULL);
194       } else {
195         err= adns_check(adns, &head->query, &answer, NULL);
196       }
197       if (err == EAGAIN) break;
198       if (err) {
199         fprintf(stderr, "%s: adns_wait/check: %s", progname, strerror(err));
200         exit(1);
201       }
202       printline(outf, head->start, head->addr, head->rest,
203                 answer->status == adns_s_ok ? *answer->rrs.str : NULL);
204       line= head; head= head->next;
205       free(line);
206       free(answer);
207       len--;
208     }
209     if (!eof) {
210       line= readline(inf, adns, opts);
211       if (line) {
212         if (!head) head= line;
213         else tail->next= line;
214         tail= line; len++;
215       } else {
216         eof= 1;
217       }
218     }
219   }
220   adns_finish(adns);
221 }
222
223 static void printhelp(FILE *file) {
224   fputs("usage: adnslogres [<options>] [<logfile>]\n"
225         "       adnslogres --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   opts= 0;
244
245   if (argv[1] && !strcmp(argv[1],"--checkc-freq")) {
246     opts|= OPT_CHECKC;
247     argv++; argc--;
248   }
249
250   if (argv[1] && !strncmp(argv[1],"--",2)) {
251     if (!strcmp(argv[1],"--help")) {
252       printhelp(stdout);
253     } else if (!strcmp(argv[1],"--version")) {
254       fputs(VERSION_MESSAGE("adnslogres"),stdout);
255     } else {
256       usage();
257     }
258     if (ferror(stdout) || fclose(stdout)) { perror("stdout"); exit(1); }
259     exit(0);
260   }
261
262   maxpending= DEFMAXPENDING;
263   while ((c= getopt(argc, argv, "c:C:dp")) != -1)
264     switch (c) {
265     case 'c':
266       maxpending= atoi(optarg);
267       if (maxpending < 1 || maxpending > MAXMAXPENDING) {
268        fprintf(stderr, "%s: unfeasible concurrency %d\n", progname, maxpending);
269        exit(1);
270       }
271       break;
272     case 'C':
273       config_text= optarg;
274       break;
275     case 'd':
276       opts|= OPT_DEBUG;
277       break;
278     case 'p':
279       opts|= OPT_POLL;
280       break;
281     default:
282       usage();
283     }
284
285   argc-= optind;
286   argv+= optind;
287
288   inf= NULL;
289   if (argc == 0)
290     inf= stdin;
291   else if (argc == 1)
292     inf= fopen(*argv, "r");
293   else
294     usage();
295
296   if (!inf)
297     aargh("couldn't open input");
298
299   proclog(inf, stdout, maxpending, opts);
300
301   if (fclose(inf))
302     aargh("fclose input");
303   if (fclose(stdout))
304     aargh("fclose output");
305
306   return 0;
307 }