81453aec |
1 | /* |
2 | * adnslogres.c |
3 | * - a replacement for the Apache logresolve program using adns |
4 | */ |
5 | /* |
23d78b27 |
6 | * This file is |
bef232ae |
7 | * Copyright (C) 1999-2000 Tony Finch <dot@dotat.at> |
2658c305 |
8 | * Copyright (C) 1999-2000 Ian Jackson <ian@davenant.greenend.org.uk> |
d942707d |
9 | * |
10 | * It is part of adns, which is |
26e1c3d6 |
11 | * Copyright (C) 1997-2000,2003,2006,2014 Ian Jackson |
ae8cc977 |
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.) |
81453aec |
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 |
7f8bbe29 |
18 | * the Free Software Foundation; either version 3, or (at your option) |
81453aec |
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 |
8c09a4c6 |
27 | * along with this program; if not, write to the Free Software Foundation. |
23d78b27 |
28 | * |
2658c305 |
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. |
81453aec |
32 | */ |
33 | |
34 | static const char * const cvsid = |
35 | "$Id$"; |
36 | |
37 | #include <sys/types.h> |
38 | #include <sys/time.h> |
39 | |
940356bd |
40 | #include <unistd.h> |
81453aec |
41 | #include <string.h> |
42 | #include <stdlib.h> |
43 | #include <stdio.h> |
44 | #include <ctype.h> |
45 | #include <errno.h> |
5a0be244 |
46 | #include <stdarg.h> |
81453aec |
47 | |
5a0be244 |
48 | #include "config.h" |
81453aec |
49 | #include "adns.h" |
aa3ffb57 |
50 | #include "client.h" |
81453aec |
51 | |
67a57aae |
52 | #ifdef ADNS_REGRESS_TEST |
53 | # include "hredirect.h" |
54 | #endif |
55 | |
81453aec |
56 | /* maximum number of concurrent DNS queries */ |
295cf525 |
57 | #define MAXMAXPENDING 64000 |
58 | #define DEFMAXPENDING 2000 |
81453aec |
59 | |
60 | /* maximum length of a line */ |
61 | #define MAXLINE 1024 |
62 | |
940356bd |
63 | /* option flags */ |
64 | #define OPT_DEBUG 1 |
65 | #define OPT_POLL 2 |
66 | |
67a57aae |
67 | static const char *const progname= "adnslogres"; |
62fa243d |
68 | static const char *config_text; |
81453aec |
69 | |
7c409027 |
70 | #define guard_null(str) ((str) ? (str) : "") |
9715e5f4 |
71 | |
d0bed398 |
72 | #define sensible_ctype(type,ch) (type((unsigned char)(ch))) |
73 | /* isfoo() functions from ctype.h can't safely be fed char - blech ! */ |
74 | |
5a0be244 |
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 | |
9715e5f4 |
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); |
81453aec |
89 | exit(1); |
90 | } |
91 | |
92 | /* |
93 | * Parse the IP address and convert to a reverse domain name. |
94 | */ |
940356bd |
95 | static char *ipaddr2domain(char *start, char **addr, char **rest) { |
81453aec |
96 | static char buf[30]; /* "123.123.123.123.in-addr.arpa.\0" */ |
97 | char *ptrs[5]; |
98 | int i; |
99 | |
940356bd |
100 | ptrs[0]= start; |
101 | retry: |
d0bed398 |
102 | while (!sensible_ctype(isdigit,*ptrs[0])) |
940356bd |
103 | if (!*ptrs[0]++) { |
104 | strcpy(buf, "invalid."); |
105 | *addr= *rest= NULL; |
106 | return buf; |
107 | } |
bb149f75 |
108 | for (i= 1; i < 5; i++) { |
109 | ptrs[i]= ptrs[i-1]; |
d0bed398 |
110 | while (sensible_ctype(isdigit,*ptrs[i]++)); |
111 | if ((i == 4 && !sensible_ctype(isspace,ptrs[i][-1])) || |
bb149f75 |
112 | (i != 4 && ptrs[i][-1] != '.') || |
113 | (ptrs[i]-ptrs[i-1] > 4)) { |
114 | ptrs[0]= ptrs[i]-1; |
940356bd |
115 | goto retry; |
bb149f75 |
116 | } |
81453aec |
117 | } |
118 | sprintf(buf, "%.*s.%.*s.%.*s.%.*s.in-addr.arpa.", |
0ca555c6 |
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]); |
81453aec |
123 | *addr= ptrs[0]; |
124 | *rest= ptrs[4]-1; |
940356bd |
125 | return buf; |
81453aec |
126 | } |
127 | |
bb149f75 |
128 | static void printline(FILE *outf, char *start, char *addr, char *rest, char *domain) { |
81453aec |
129 | if (domain) |
0ca555c6 |
130 | fprintf(outf, "%.*s%s%s", (int)(addr - start), start, domain, rest); |
81453aec |
131 | else |
bb149f75 |
132 | fputs(start, outf); |
133 | if (ferror(outf)) aargh("write output"); |
81453aec |
134 | } |
135 | |
136 | typedef struct logline { |
137 | struct logline *next; |
138 | char *start, *addr, *rest; |
139 | adns_query query; |
140 | } logline; |
141 | |
bb149f75 |
142 | static logline *readline(FILE *inf, adns_state adns, int opts) { |
81453aec |
143 | static char buf[MAXLINE]; |
144 | char *str; |
145 | logline *line; |
146 | |
bb149f75 |
147 | if (fgets(buf, MAXLINE, inf)) { |
81453aec |
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); |
bb149f75 |
154 | str= ipaddr2domain(line->start, &line->addr, &line->rest); |
940356bd |
155 | if (opts & OPT_DEBUG) |
bf41c1c2 |
156 | msg("submitting %.*s -> %s", (int)(line->rest-line->addr), guard_null(line->addr), str); |
81453aec |
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 | } |
bb149f75 |
163 | if (!feof(inf)) |
81453aec |
164 | aargh("fgets"); |
165 | return NULL; |
166 | } |
167 | |
295cf525 |
168 | static void proclog(FILE *inf, FILE *outf, int maxpending, int opts) { |
81453aec |
169 | int eof, err, len; |
170 | adns_state adns; |
171 | adns_answer *answer; |
172 | logline *head, *tail, *line; |
62fa243d |
173 | adns_initflags initflags; |
81453aec |
174 | |
62fa243d |
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 | } |
81453aec |
181 | if (errno) aargh("adns_init"); |
bb149f75 |
182 | head= tail= readline(inf, adns, opts); |
81453aec |
183 | len= 1; eof= 0; |
184 | while (head) { |
295cf525 |
185 | while (head) { |
186 | if (opts & OPT_DEBUG) |
187 | msg("%d in queue; checking %.*s", len, |
bf41c1c2 |
188 | (int)(head->rest-head->addr), guard_null(head->addr)); |
67a57aae |
189 | if (eof || len >= maxpending) { |
295cf525 |
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; |
67a57aae |
198 | if (err) { |
199 | fprintf(stderr, "%s: adns_wait/check: %s", progname, strerror(err)); |
200 | exit(1); |
201 | } |
bb149f75 |
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; |
67a57aae |
205 | free(line); |
206 | free(answer); |
bb149f75 |
207 | len--; |
81453aec |
208 | } |
209 | if (!eof) { |
bb149f75 |
210 | line= readline(inf, adns, opts); |
295cf525 |
211 | if (line) { |
212 | if (!head) head= line; |
213 | else tail->next= line; |
214 | tail= line; len++; |
215 | } else { |
81453aec |
216 | eof= 1; |
81453aec |
217 | } |
218 | } |
219 | } |
220 | adns_finish(adns); |
221 | } |
222 | |
8e5a4960 |
223 | static void printhelp(FILE *file) { |
aa3ffb57 |
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); |
8e5a4960 |
231 | } |
232 | |
233 | static void usage(void) { |
234 | printhelp(stderr); |
bb149f75 |
235 | exit(1); |
236 | } |
237 | |
23d78b27 |
238 | int main(int argc, char *argv[]) { |
295cf525 |
239 | int c, opts, maxpending; |
240 | extern char *optarg; |
bb149f75 |
241 | FILE *inf; |
940356bd |
242 | |
aa3ffb57 |
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 | } |
8e5a4960 |
251 | if (ferror(stdout) || fclose(stdout)) { perror("stdout"); exit(1); } |
252 | exit(0); |
253 | } |
254 | |
295cf525 |
255 | maxpending= DEFMAXPENDING; |
256 | opts= 0; |
62fa243d |
257 | while ((c= getopt(argc, argv, "c:C:dp")) != -1) |
940356bd |
258 | switch (c) { |
295cf525 |
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; |
62fa243d |
266 | case 'C': |
267 | config_text= optarg; |
268 | break; |
940356bd |
269 | case 'd': |
bb149f75 |
270 | opts|= OPT_DEBUG; |
940356bd |
271 | break; |
272 | case 'p': |
bb149f75 |
273 | opts|= OPT_POLL; |
940356bd |
274 | break; |
275 | default: |
bb149f75 |
276 | usage(); |
940356bd |
277 | } |
940356bd |
278 | |
bb149f75 |
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 | |
295cf525 |
293 | proclog(inf, stdout, maxpending, opts); |
bb149f75 |
294 | |
295 | if (fclose(inf)) |
296 | aargh("fclose input"); |
297 | if (fclose(stdout)) |
298 | aargh("fclose output"); |
940356bd |
299 | |
81453aec |
300 | return 0; |
301 | } |