chiark / gitweb /
c1a22c0018e1f13b5f310a74fdba2804fdfdd8bc
[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 Tony Finch <dot@dotat.at>
8  *   Copyright (C) 1999-2000 Ian Jackson <ian@davenant.greenend.org.uk>
9  *
10  *  It is part of adns, which is
11  *    Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
12  *    Copyright (C) 1999 Tony Finch <dot@dotat.at>
13  *  
14  *  This program is free software; you can redistribute it and/or modify
15  *  it under the terms of the GNU General Public License as published by
16  *  the Free Software Foundation; either version 2, or (at your option)
17  *  any later version.
18  *  
19  *  This program is distributed in the hope that it will be useful,
20  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *  GNU General Public License for more details.
23  *  
24  *  You should have received a copy of the GNU General Public License
25  *  along with this program; if not, write to the Free Software Foundation,
26  *  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27  *
28  *  This version was originally supplied by Tony Finch, but has been
29  *  modified by Ian Jackson as it was incorporated into adns and
30  *  subsequently.
31  */
32
33 static const char * const cvsid =
34         "$Id$";
35
36 #include <sys/types.h>
37 #include <sys/time.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
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
65 static const char *const progname= "adnslogres";
66 static const char *config_text;
67
68 #define guard_null(str) ((str) ? (str) : "")
69
70 #define sensible_ctype(type,ch) (type((unsigned char)(ch)))
71   /* isfoo() functions from ctype.h can't safely be fed char - blech ! */
72
73 static void msg(const char *fmt, ...) {
74   va_list al;
75
76   fprintf(stderr, "%s: ", progname);
77   va_start(al,fmt);
78   vfprintf(stderr, fmt, al);
79   va_end(al);
80   fputc('\n',stderr);
81 }
82
83 static void aargh(const char *cause) {
84   const char *why = strerror(errno);
85   if (!why) why = "Unknown error";
86   msg("%s: %s (%d)", cause, why, errno);
87   exit(1);
88 }
89
90 /*
91  * Parse the IP address and convert to a reverse domain name.
92  */
93 static char *ipaddr2domain(char *start, char **addr, char **rest) {
94   static char buf[30]; /* "123.123.123.123.in-addr.arpa.\0" */
95   char *ptrs[5];
96   int i;
97
98   ptrs[0]= start;
99 retry:
100   while (!sensible_ctype(isdigit,*ptrs[0]))
101     if (!*ptrs[0]++) {
102       strcpy(buf, "invalid.");
103       *addr= *rest= NULL;
104       return buf;
105     }
106   for (i= 1; i < 5; i++) {
107     ptrs[i]= ptrs[i-1];
108     while (sensible_ctype(isdigit,*ptrs[i]++));
109     if ((i == 4 && !sensible_ctype(isspace,ptrs[i][-1])) ||
110         (i != 4 && ptrs[i][-1] != '.') ||
111         (ptrs[i]-ptrs[i-1] > 4)) {
112       ptrs[0]= ptrs[i]-1;
113       goto retry;
114     }
115   }
116   sprintf(buf, "%.*s.%.*s.%.*s.%.*s.in-addr.arpa.",
117           ptrs[4]-ptrs[3]-1, ptrs[3],
118           ptrs[3]-ptrs[2]-1, ptrs[2],
119           ptrs[2]-ptrs[1]-1, ptrs[1],
120           ptrs[1]-ptrs[0]-1, ptrs[0]);
121   *addr= ptrs[0];
122   *rest= ptrs[4]-1;
123   return buf;
124 }
125
126 static void printline(FILE *outf, char *start, char *addr, char *rest, char *domain) {
127   if (domain)
128     fprintf(outf, "%.*s%s%s", addr - start, start, domain, rest);
129   else
130     fputs(start, outf);
131   if (ferror(outf)) aargh("write output");
132 }
133
134 typedef struct logline {
135   struct logline *next;
136   char *start, *addr, *rest;
137   adns_query query;
138 } logline;
139
140 static logline *readline(FILE *inf, adns_state adns, int opts) {
141   static char buf[MAXLINE];
142   char *str;
143   logline *line;
144
145   if (fgets(buf, MAXLINE, inf)) {
146     str= malloc(sizeof(*line) + strlen(buf) + 1);
147     if (!str) aargh("malloc");
148     line= (logline*)str;
149     line->next= NULL;
150     line->start= str+sizeof(logline);
151     strcpy(line->start, buf);
152     str= ipaddr2domain(line->start, &line->addr, &line->rest);
153     if (opts & OPT_DEBUG)
154       msg("submitting %.*s -> %s", line->rest-line->addr, guard_null(line->addr), str);
155     if (adns_submit(adns, str, adns_r_ptr,
156                     adns_qf_quoteok_cname|adns_qf_cname_loose,
157                     NULL, &line->query))
158       aargh("adns_submit");
159     return line;
160   }
161   if (!feof(inf))
162     aargh("fgets");
163   return NULL;
164 }
165         
166 static void proclog(FILE *inf, FILE *outf, int maxpending, int opts) {
167   int eof, err, len;
168   adns_state adns;
169   adns_answer *answer;
170   logline *head, *tail, *line;
171   adns_initflags initflags;
172
173   initflags= (opts & OPT_DEBUG) ? adns_if_debug : 0;
174   if (config_text) {
175     errno= adns_init_strcfg(&adns, initflags, stderr, config_text);
176   } else {
177     errno= adns_init(&adns, initflags, 0);
178   }
179   if (errno) aargh("adns_init");
180   head= tail= readline(inf, adns, opts);
181   len= 1; eof= 0;
182   while (head) {
183     while (head) {
184       if (opts & OPT_DEBUG)
185         msg("%d in queue; checking %.*s", len,
186             head->rest-head->addr, guard_null(head->addr));
187       if (eof || len >= maxpending) {
188         if (opts & OPT_POLL)
189           err= adns_wait_poll(adns, &head->query, &answer, NULL);
190         else
191           err= adns_wait(adns, &head->query, &answer, NULL);
192       } else {
193         err= adns_check(adns, &head->query, &answer, NULL);
194       }
195       if (err == EAGAIN) break;
196       if (err) {
197         fprintf(stderr, "%s: adns_wait/check: %s", progname, strerror(err));
198         exit(1);
199       }
200       printline(outf, head->start, head->addr, head->rest,
201                 answer->status == adns_s_ok ? *answer->rrs.str : NULL);
202       line= head; head= head->next;
203       free(line);
204       free(answer);
205       len--;
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; len++;
213       } else {
214         eof= 1;
215       }
216     }
217   }
218   adns_finish(adns);
219 }
220
221 static void usage(void) {
222   fprintf(stderr, "usage: %s [-d] [-p] [-c concurrency] [-C config] [logfile]\n",
223           progname);
224   exit(1);
225 }
226
227 int main(int argc, char *argv[]) {
228   int c, opts, maxpending;
229   extern char *optarg;
230   FILE *inf;
231
232   maxpending= DEFMAXPENDING;
233   opts= 0;
234   while ((c= getopt(argc, argv, "c:C:dp")) != -1)
235     switch (c) {
236     case 'c':
237       maxpending= atoi(optarg);
238       if (maxpending < 1 || maxpending > MAXMAXPENDING) {
239        fprintf(stderr, "%s: unfeasible concurrency %d\n", progname, maxpending);
240        exit(1);
241       }
242       break;
243     case 'C':
244       config_text= optarg;
245       break;
246     case 'd':
247       opts|= OPT_DEBUG;
248       break;
249     case 'p':
250       opts|= OPT_POLL;
251       break;
252     default:
253       usage();
254     }
255
256   argc-= optind;
257   argv+= optind;
258
259   inf= NULL;
260   if (argc == 0)
261     inf= stdin;
262   else if (argc == 1)
263     inf= fopen(*argv, "r");
264   else
265     usage();
266
267   if (!inf)
268     aargh("couldn't open input");
269
270   proclog(inf, stdout, maxpending, opts);
271
272   if (fclose(inf))
273     aargh("fclose input");
274   if (fclose(stdout))
275     aargh("fclose output");
276
277   return 0;
278 }