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