296ae1b2119ad67e3b8a31a765d25ab774878b63
[adns] / 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 Ian Jackson <ian@davenant.greenend.org.uk>
9 *
10 * It is part of adns, which is
11 * Copyright (C) 1997-2000,2003,2006,2014 Ian Jackson
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 version was originally supplied by Tony Finch, but has been
30 * modified by Ian Jackson as it was incorporated into adns and
31 * subsequently.
32 */
33
34 static const char * const cvsid =
35 "$Id$";
36
37 #include <sys/types.h>
38 #include <sys/time.h>
39
40 #include <unistd.h>
41 #include <string.h>
42 #include <stdlib.h>
43 #include <stdio.h>
44 #include <ctype.h>
45 #include <errno.h>
46 #include <stdarg.h>
47
48 #include "config.h"
49 #include "adns.h"
50 #include "client.h"
51
52 #ifdef ADNS_REGRESS_TEST
53 # include "hredirect.h"
54 #endif
55
56 /* maximum number of concurrent DNS queries */
57 #define MAXMAXPENDING 64000
58 #define DEFMAXPENDING 2000
59
60 /* maximum length of a line */
61 #define MAXLINE 1024
62
63 /* option flags */
64 #define OPT_DEBUG 1
65 #define OPT_POLL 2
66
67 static const char *const progname= "adnslogres";
68 static const char *config_text;
69
70 #define guard_null(str) ((str) ? (str) : "")
71
72 #define sensible_ctype(type,ch) (type((unsigned char)(ch)))
73 /* isfoo() functions from ctype.h can't safely be fed char - blech ! */
74
75 static void msg(const char *fmt, ...) {
76 va_list al;
77
78 fprintf(stderr, "%s: ", progname);
79 va_start(al,fmt);
80 vfprintf(stderr, fmt, al);
81 va_end(al);
82 fputc('\n',stderr);
83 }
84
85 static void aargh(const char *cause) {
86 const char *why = strerror(errno);
87 if (!why) why = "Unknown error";
88 msg("%s: %s (%d)", cause, why, errno);
89 exit(1);
90 }
91
92 /*
93 * Parse the IP address and convert to a reverse domain name.
94 */
95 static char *ipaddr2domain(char *start, char **addr, char **rest) {
96 static char buf[30]; /* "123.123.123.123.in-addr.arpa.\0" */
97 char *ptrs[5];
98 int i;
99
100 ptrs[0]= start;
101 retry:
102 while (!sensible_ctype(isdigit,*ptrs[0]))
103 if (!*ptrs[0]++) {
104 strcpy(buf, "invalid.");
105 *addr= *rest= NULL;
106 return buf;
107 }
108 for (i= 1; i < 5; i++) {
109 ptrs[i]= ptrs[i-1];
110 while (sensible_ctype(isdigit,*ptrs[i]++));
111 if ((i == 4 && !sensible_ctype(isspace,ptrs[i][-1])) ||
112 (i != 4 && ptrs[i][-1] != '.') ||
113 (ptrs[i]-ptrs[i-1] > 4)) {
114 ptrs[0]= ptrs[i]-1;
115 goto retry;
116 }
117 }
118 sprintf(buf, "%.*s.%.*s.%.*s.%.*s.in-addr.arpa.",
119 (int)(ptrs[4]-ptrs[3]-1), ptrs[3],
120 (int)(ptrs[3]-ptrs[2]-1), ptrs[2],
121 (int)(ptrs[2]-ptrs[1]-1), ptrs[1],
122 (int)(ptrs[1]-ptrs[0]-1), ptrs[0]);
123 *addr= ptrs[0];
124 *rest= ptrs[4]-1;
125 return buf;
126 }
127
128 static void printline(FILE *outf, char *start, char *addr, char *rest, char *domain) {
129 if (domain)
130 fprintf(outf, "%.*s%s%s", (int)(addr - start), start, domain, rest);
131 else
132 fputs(start, outf);
133 if (ferror(outf)) aargh("write output");
134 }
135
136 typedef struct logline {
137 struct logline *next;
138 char *start, *addr, *rest;
139 adns_query query;
140 } logline;
141
142 static logline *readline(FILE *inf, adns_state adns, int opts) {
143 static char buf[MAXLINE];
144 char *str;
145 logline *line;
146
147 if (fgets(buf, MAXLINE, inf)) {
148 str= malloc(sizeof(*line) + strlen(buf) + 1);
149 if (!str) aargh("malloc");
150 line= (logline*)str;
151 line->next= NULL;
152 line->start= str+sizeof(logline);
153 strcpy(line->start, buf);
154 str= ipaddr2domain(line->start, &line->addr, &line->rest);
155 if (opts & OPT_DEBUG)
156 msg("submitting %.*s -> %s", (int)(line->rest-line->addr), guard_null(line->addr), str);
157 if (adns_submit(adns, str, adns_r_ptr,
158 adns_qf_quoteok_cname|adns_qf_cname_loose,
159 NULL, &line->query))
160 aargh("adns_submit");
161 return line;
162 }
163 if (!feof(inf))
164 aargh("fgets");
165 return NULL;
166 }
167
168 static void proclog(FILE *inf, FILE *outf, int maxpending, int opts) {
169 int eof, err, len;
170 adns_state adns;
171 adns_answer *answer;
172 logline *head, *tail, *line;
173 adns_initflags initflags;
174
175 initflags= (opts & OPT_DEBUG) ? adns_if_debug : 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 if (argv[1] && !strncmp(argv[1],"--",2)) {
244 if (!strcmp(argv[1],"--help")) {
245 printhelp(stdout);
246 } else if (!strcmp(argv[1],"--version")) {
247 fputs(VERSION_MESSAGE("adnslogres"),stdout);
248 } else {
249 usage();
250 }
251 if (ferror(stdout) || fclose(stdout)) { perror("stdout"); exit(1); }
252 exit(0);
253 }
254
255 maxpending= DEFMAXPENDING;
256 opts= 0;
257 while ((c= getopt(argc, argv, "c:C:dp")) != -1)
258 switch (c) {
259 case 'c':
260 maxpending= atoi(optarg);
261 if (maxpending < 1 || maxpending > MAXMAXPENDING) {
262 fprintf(stderr, "%s: unfeasible concurrency %d\n", progname, maxpending);
263 exit(1);
264 }
265 break;
266 case 'C':
267 config_text= optarg;
268 break;
269 case 'd':
270 opts|= OPT_DEBUG;
271 break;
272 case 'p':
273 opts|= OPT_POLL;
274 break;
275 default:
276 usage();
277 }
278
279 argc-= optind;
280 argv+= optind;
281
282 inf= NULL;
283 if (argc == 0)
284 inf= stdin;
285 else if (argc == 1)
286 inf= fopen(*argv, "r");
287 else
288 usage();
289
290 if (!inf)
291 aargh("couldn't open input");
292
293 proclog(inf, stdout, maxpending, opts);
294
295 if (fclose(inf))
296 aargh("fclose input");
297 if (fclose(stdout))
298 aargh("fclose output");
299
300 return 0;
301 }