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 |
17cd4f48 |
12 | * Copyright (C) 2014 Mark Wooding |
ae8cc977 |
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.) |
81453aec |
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 |
7f8bbe29 |
19 | * the Free Software Foundation; either version 3, or (at your option) |
81453aec |
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 |
8c09a4c6 |
28 | * along with this program; if not, write to the Free Software Foundation. |
23d78b27 |
29 | * |
2658c305 |
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. |
81453aec |
33 | */ |
34 | |
81453aec |
35 | #include <sys/types.h> |
36 | #include <sys/time.h> |
37 | |
940356bd |
38 | #include <unistd.h> |
81453aec |
39 | #include <string.h> |
40 | #include <stdlib.h> |
41 | #include <stdio.h> |
42 | #include <ctype.h> |
43 | #include <errno.h> |
5a0be244 |
44 | #include <stdarg.h> |
81453aec |
45 | |
5a0be244 |
46 | #include "config.h" |
81453aec |
47 | #include "adns.h" |
aa3ffb57 |
48 | #include "client.h" |
81453aec |
49 | |
67a57aae |
50 | #ifdef ADNS_REGRESS_TEST |
51 | # include "hredirect.h" |
52 | #endif |
53 | |
81453aec |
54 | /* maximum number of concurrent DNS queries */ |
295cf525 |
55 | #define MAXMAXPENDING 64000 |
56 | #define DEFMAXPENDING 2000 |
81453aec |
57 | |
58 | /* maximum length of a line */ |
59 | #define MAXLINE 1024 |
60 | |
940356bd |
61 | /* option flags */ |
62 | #define OPT_DEBUG 1 |
63 | #define OPT_POLL 2 |
64 | |
67a57aae |
65 | static const char *const progname= "adnslogres"; |
62fa243d |
66 | static const char *config_text; |
81453aec |
67 | |
7c409027 |
68 | #define guard_null(str) ((str) ? (str) : "") |
9715e5f4 |
69 | |
d0bed398 |
70 | #define sensible_ctype(type,ch) (type((unsigned char)(ch))) |
71 | /* isfoo() functions from ctype.h can't safely be fed char - blech ! */ |
72 | |
5a0be244 |
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 | |
9715e5f4 |
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); |
81453aec |
87 | exit(1); |
88 | } |
89 | |
90 | /* |
91 | * Parse the IP address and convert to a reverse domain name. |
92 | */ |
940356bd |
93 | static char *ipaddr2domain(char *start, char **addr, char **rest) { |
81453aec |
94 | static char buf[30]; /* "123.123.123.123.in-addr.arpa.\0" */ |
95 | char *ptrs[5]; |
96 | int i; |
97 | |
940356bd |
98 | ptrs[0]= start; |
99 | retry: |
d0bed398 |
100 | while (!sensible_ctype(isdigit,*ptrs[0])) |
940356bd |
101 | if (!*ptrs[0]++) { |
102 | strcpy(buf, "invalid."); |
103 | *addr= *rest= NULL; |
104 | return buf; |
105 | } |
bb149f75 |
106 | for (i= 1; i < 5; i++) { |
107 | ptrs[i]= ptrs[i-1]; |
d0bed398 |
108 | while (sensible_ctype(isdigit,*ptrs[i]++)); |
109 | if ((i == 4 && !sensible_ctype(isspace,ptrs[i][-1])) || |
bb149f75 |
110 | (i != 4 && ptrs[i][-1] != '.') || |
111 | (ptrs[i]-ptrs[i-1] > 4)) { |
112 | ptrs[0]= ptrs[i]-1; |
940356bd |
113 | goto retry; |
bb149f75 |
114 | } |
81453aec |
115 | } |
116 | sprintf(buf, "%.*s.%.*s.%.*s.%.*s.in-addr.arpa.", |
0ca555c6 |
117 | (int)(ptrs[4]-ptrs[3]-1), ptrs[3], |
118 | (int)(ptrs[3]-ptrs[2]-1), ptrs[2], |
119 | (int)(ptrs[2]-ptrs[1]-1), ptrs[1], |
120 | (int)(ptrs[1]-ptrs[0]-1), ptrs[0]); |
81453aec |
121 | *addr= ptrs[0]; |
122 | *rest= ptrs[4]-1; |
940356bd |
123 | return buf; |
81453aec |
124 | } |
125 | |
bb149f75 |
126 | static void printline(FILE *outf, char *start, char *addr, char *rest, char *domain) { |
81453aec |
127 | if (domain) |
0ca555c6 |
128 | fprintf(outf, "%.*s%s%s", (int)(addr - start), start, domain, rest); |
81453aec |
129 | else |
bb149f75 |
130 | fputs(start, outf); |
131 | if (ferror(outf)) aargh("write output"); |
81453aec |
132 | } |
133 | |
134 | typedef struct logline { |
135 | struct logline *next; |
136 | char *start, *addr, *rest; |
137 | adns_query query; |
138 | } logline; |
139 | |
bb149f75 |
140 | static logline *readline(FILE *inf, adns_state adns, int opts) { |
81453aec |
141 | static char buf[MAXLINE]; |
142 | char *str; |
143 | logline *line; |
144 | |
bb149f75 |
145 | if (fgets(buf, MAXLINE, inf)) { |
81453aec |
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); |
bb149f75 |
152 | str= ipaddr2domain(line->start, &line->addr, &line->rest); |
940356bd |
153 | if (opts & OPT_DEBUG) |
bf41c1c2 |
154 | msg("submitting %.*s -> %s", (int)(line->rest-line->addr), guard_null(line->addr), str); |
81453aec |
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 | } |
bb149f75 |
161 | if (!feof(inf)) |
81453aec |
162 | aargh("fgets"); |
163 | return NULL; |
164 | } |
165 | |
295cf525 |
166 | static void proclog(FILE *inf, FILE *outf, int maxpending, int opts) { |
81453aec |
167 | int eof, err, len; |
168 | adns_state adns; |
169 | adns_answer *answer; |
170 | logline *head, *tail, *line; |
62fa243d |
171 | adns_initflags initflags; |
81453aec |
172 | |
62fa243d |
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 | } |
81453aec |
179 | if (errno) aargh("adns_init"); |
bb149f75 |
180 | head= tail= readline(inf, adns, opts); |
81453aec |
181 | len= 1; eof= 0; |
182 | while (head) { |
295cf525 |
183 | while (head) { |
184 | if (opts & OPT_DEBUG) |
185 | msg("%d in queue; checking %.*s", len, |
bf41c1c2 |
186 | (int)(head->rest-head->addr), guard_null(head->addr)); |
67a57aae |
187 | if (eof || len >= maxpending) { |
295cf525 |
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; |
67a57aae |
196 | if (err) { |
197 | fprintf(stderr, "%s: adns_wait/check: %s", progname, strerror(err)); |
198 | exit(1); |
199 | } |
bb149f75 |
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; |
67a57aae |
203 | free(line); |
204 | free(answer); |
bb149f75 |
205 | len--; |
81453aec |
206 | } |
207 | if (!eof) { |
bb149f75 |
208 | line= readline(inf, adns, opts); |
295cf525 |
209 | if (line) { |
210 | if (!head) head= line; |
211 | else tail->next= line; |
212 | tail= line; len++; |
213 | } else { |
81453aec |
214 | eof= 1; |
81453aec |
215 | } |
216 | } |
217 | } |
218 | adns_finish(adns); |
219 | } |
220 | |
8e5a4960 |
221 | static void printhelp(FILE *file) { |
aa3ffb57 |
222 | fputs("usage: adnslogres [<options>] [<logfile>]\n" |
223 | " adnslogres --version|--help\n" |
224 | "options: -c <concurrency> set max number of outstanding queries\n" |
225 | " -p use poll(2) instead of select(2)\n" |
226 | " -d turn on debugging\n" |
227 | " -C <config> use instead of contents of resolv.conf\n", |
228 | stdout); |
8e5a4960 |
229 | } |
230 | |
231 | static void usage(void) { |
232 | printhelp(stderr); |
bb149f75 |
233 | exit(1); |
234 | } |
235 | |
23d78b27 |
236 | int main(int argc, char *argv[]) { |
295cf525 |
237 | int c, opts, maxpending; |
238 | extern char *optarg; |
bb149f75 |
239 | FILE *inf; |
940356bd |
240 | |
aa3ffb57 |
241 | if (argv[1] && !strncmp(argv[1],"--",2)) { |
242 | if (!strcmp(argv[1],"--help")) { |
243 | printhelp(stdout); |
244 | } else if (!strcmp(argv[1],"--version")) { |
245 | fputs(VERSION_MESSAGE("adnslogres"),stdout); |
246 | } else { |
247 | usage(); |
248 | } |
8e5a4960 |
249 | if (ferror(stdout) || fclose(stdout)) { perror("stdout"); exit(1); } |
250 | exit(0); |
251 | } |
252 | |
295cf525 |
253 | maxpending= DEFMAXPENDING; |
254 | opts= 0; |
62fa243d |
255 | while ((c= getopt(argc, argv, "c:C:dp")) != -1) |
940356bd |
256 | switch (c) { |
295cf525 |
257 | case 'c': |
258 | maxpending= atoi(optarg); |
259 | if (maxpending < 1 || maxpending > MAXMAXPENDING) { |
260 | fprintf(stderr, "%s: unfeasible concurrency %d\n", progname, maxpending); |
261 | exit(1); |
262 | } |
263 | break; |
62fa243d |
264 | case 'C': |
265 | config_text= optarg; |
266 | break; |
940356bd |
267 | case 'd': |
bb149f75 |
268 | opts|= OPT_DEBUG; |
940356bd |
269 | break; |
270 | case 'p': |
bb149f75 |
271 | opts|= OPT_POLL; |
940356bd |
272 | break; |
273 | default: |
bb149f75 |
274 | usage(); |
940356bd |
275 | } |
940356bd |
276 | |
bb149f75 |
277 | argc-= optind; |
278 | argv+= optind; |
279 | |
280 | inf= NULL; |
281 | if (argc == 0) |
282 | inf= stdin; |
283 | else if (argc == 1) |
284 | inf= fopen(*argv, "r"); |
285 | else |
286 | usage(); |
287 | |
288 | if (!inf) |
289 | aargh("couldn't open input"); |
290 | |
295cf525 |
291 | proclog(inf, stdout, maxpending, opts); |
bb149f75 |
292 | |
293 | if (fclose(inf)) |
294 | aargh("fclose input"); |
295 | if (fclose(stdout)) |
296 | aargh("fclose output"); |
940356bd |
297 | |
81453aec |
298 | return 0; |
299 | } |