81453aec |
1 | /* |
2 | * adnslogres.c |
3 | * - a replacement for the Apache logresolve program using adns |
4 | */ |
5 | /* |
23d78b27 |
6 | * This file is |
b8b163f9 |
7 | * Copyright (C) 1999 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 |
2658c305 |
11 | * Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk> |
b8b163f9 |
12 | * Copyright (C) 1999 Tony Finch <dot@dotat.at> |
81453aec |
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, |
23d78b27 |
26 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
27 | * |
2658c305 |
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. |
81453aec |
31 | */ |
32 | |
33 | static const char * const cvsid = |
34 | "$Id$"; |
35 | |
36 | #include <sys/types.h> |
37 | #include <sys/time.h> |
38 | |
940356bd |
39 | #include <unistd.h> |
81453aec |
40 | #include <string.h> |
41 | #include <stdlib.h> |
42 | #include <stdio.h> |
43 | #include <ctype.h> |
44 | #include <errno.h> |
5a0be244 |
45 | #include <stdarg.h> |
81453aec |
46 | |
5a0be244 |
47 | #include "config.h" |
81453aec |
48 | #include "adns.h" |
49 | |
50 | /* maximum number of concurrent DNS queries */ |
295cf525 |
51 | #define MAXMAXPENDING 64000 |
52 | #define DEFMAXPENDING 2000 |
81453aec |
53 | |
54 | /* maximum length of a line */ |
55 | #define MAXLINE 1024 |
56 | |
940356bd |
57 | /* option flags */ |
58 | #define OPT_DEBUG 1 |
59 | #define OPT_POLL 2 |
60 | |
81453aec |
61 | static const char *progname; |
62fa243d |
62 | static const char *config_text; |
81453aec |
63 | |
7c409027 |
64 | #define guard_null(str) ((str) ? (str) : "") |
9715e5f4 |
65 | |
d0bed398 |
66 | #define sensible_ctype(type,ch) (type((unsigned char)(ch))) |
67 | /* isfoo() functions from ctype.h can't safely be fed char - blech ! */ |
68 | |
5a0be244 |
69 | static void msg(const char *fmt, ...) { |
70 | va_list al; |
71 | |
72 | fprintf(stderr, "%s: ", progname); |
73 | va_start(al,fmt); |
74 | vfprintf(stderr, fmt, al); |
75 | va_end(al); |
76 | fputc('\n',stderr); |
77 | } |
78 | |
9715e5f4 |
79 | static void aargh(const char *cause) { |
80 | const char *why = strerror(errno); |
81 | if (!why) why = "Unknown error"; |
82 | msg("%s: %s (%d)", cause, why, errno); |
81453aec |
83 | exit(1); |
84 | } |
85 | |
86 | /* |
87 | * Parse the IP address and convert to a reverse domain name. |
88 | */ |
940356bd |
89 | static char *ipaddr2domain(char *start, char **addr, char **rest) { |
81453aec |
90 | static char buf[30]; /* "123.123.123.123.in-addr.arpa.\0" */ |
91 | char *ptrs[5]; |
92 | int i; |
93 | |
940356bd |
94 | ptrs[0]= start; |
95 | retry: |
d0bed398 |
96 | while (!sensible_ctype(isdigit,*ptrs[0])) |
940356bd |
97 | if (!*ptrs[0]++) { |
98 | strcpy(buf, "invalid."); |
99 | *addr= *rest= NULL; |
100 | return buf; |
101 | } |
bb149f75 |
102 | for (i= 1; i < 5; i++) { |
103 | ptrs[i]= ptrs[i-1]; |
d0bed398 |
104 | while (sensible_ctype(isdigit,*ptrs[i]++)); |
105 | if ((i == 4 && !sensible_ctype(isspace,ptrs[i][-1])) || |
bb149f75 |
106 | (i != 4 && ptrs[i][-1] != '.') || |
107 | (ptrs[i]-ptrs[i-1] > 4)) { |
108 | ptrs[0]= ptrs[i]-1; |
940356bd |
109 | goto retry; |
bb149f75 |
110 | } |
81453aec |
111 | } |
112 | sprintf(buf, "%.*s.%.*s.%.*s.%.*s.in-addr.arpa.", |
113 | ptrs[4]-ptrs[3]-1, ptrs[3], |
114 | ptrs[3]-ptrs[2]-1, ptrs[2], |
115 | ptrs[2]-ptrs[1]-1, ptrs[1], |
116 | ptrs[1]-ptrs[0]-1, ptrs[0]); |
117 | *addr= ptrs[0]; |
118 | *rest= ptrs[4]-1; |
940356bd |
119 | return buf; |
81453aec |
120 | } |
121 | |
bb149f75 |
122 | static void printline(FILE *outf, char *start, char *addr, char *rest, char *domain) { |
81453aec |
123 | if (domain) |
bb149f75 |
124 | fprintf(outf, "%.*s%s%s", addr - start, start, domain, rest); |
81453aec |
125 | else |
bb149f75 |
126 | fputs(start, outf); |
127 | if (ferror(outf)) aargh("write output"); |
81453aec |
128 | } |
129 | |
130 | typedef struct logline { |
131 | struct logline *next; |
132 | char *start, *addr, *rest; |
133 | adns_query query; |
134 | } logline; |
135 | |
bb149f75 |
136 | static logline *readline(FILE *inf, adns_state adns, int opts) { |
81453aec |
137 | static char buf[MAXLINE]; |
138 | char *str; |
139 | logline *line; |
140 | |
bb149f75 |
141 | if (fgets(buf, MAXLINE, inf)) { |
81453aec |
142 | str= malloc(sizeof(*line) + strlen(buf) + 1); |
143 | if (!str) aargh("malloc"); |
144 | line= (logline*)str; |
145 | line->next= NULL; |
146 | line->start= str+sizeof(logline); |
147 | strcpy(line->start, buf); |
bb149f75 |
148 | str= ipaddr2domain(line->start, &line->addr, &line->rest); |
940356bd |
149 | if (opts & OPT_DEBUG) |
7c409027 |
150 | msg("submitting %.*s -> %s", line->rest-line->addr, guard_null(line->addr), str); |
81453aec |
151 | if (adns_submit(adns, str, adns_r_ptr, |
152 | adns_qf_quoteok_cname|adns_qf_cname_loose, |
153 | NULL, &line->query)) |
154 | aargh("adns_submit"); |
155 | return line; |
156 | } |
bb149f75 |
157 | if (!feof(inf)) |
81453aec |
158 | aargh("fgets"); |
159 | return NULL; |
160 | } |
161 | |
295cf525 |
162 | static void proclog(FILE *inf, FILE *outf, int maxpending, int opts) { |
81453aec |
163 | int eof, err, len; |
164 | adns_state adns; |
165 | adns_answer *answer; |
166 | logline *head, *tail, *line; |
62fa243d |
167 | adns_initflags initflags; |
81453aec |
168 | |
62fa243d |
169 | initflags= (opts & OPT_DEBUG) ? adns_if_debug : 0; |
170 | if (config_text) { |
171 | errno= adns_init_strcfg(&adns, initflags, stderr, config_text); |
172 | } else { |
173 | errno= adns_init(&adns, initflags, 0); |
174 | } |
81453aec |
175 | if (errno) aargh("adns_init"); |
bb149f75 |
176 | head= tail= readline(inf, adns, opts); |
81453aec |
177 | len= 1; eof= 0; |
178 | while (head) { |
295cf525 |
179 | while (head) { |
180 | if (opts & OPT_DEBUG) |
181 | msg("%d in queue; checking %.*s", len, |
182 | head->rest-head->addr, guard_null(head->addr)); |
183 | if (eof || len > maxpending) { |
184 | if (opts & OPT_POLL) |
185 | err= adns_wait_poll(adns, &head->query, &answer, NULL); |
186 | else |
187 | err= adns_wait(adns, &head->query, &answer, NULL); |
188 | } else { |
189 | err= adns_check(adns, &head->query, &answer, NULL); |
190 | } |
191 | if (err == EAGAIN) break; |
bb149f75 |
192 | printline(outf, head->start, head->addr, head->rest, |
193 | answer->status == adns_s_ok ? *answer->rrs.str : NULL); |
194 | line= head; head= head->next; |
195 | free(line); free(answer); |
196 | len--; |
81453aec |
197 | } |
198 | if (!eof) { |
bb149f75 |
199 | line= readline(inf, adns, opts); |
295cf525 |
200 | if (line) { |
201 | if (!head) head= line; |
202 | else tail->next= line; |
203 | tail= line; len++; |
204 | } else { |
81453aec |
205 | eof= 1; |
81453aec |
206 | } |
207 | } |
208 | } |
209 | adns_finish(adns); |
210 | } |
211 | |
bb149f75 |
212 | static void usage(void) { |
62fa243d |
213 | fprintf(stderr, "usage: %s [-d] [-p] [-c concurrency] [-C config] [logfile]\n", |
214 | progname); |
bb149f75 |
215 | exit(1); |
216 | } |
217 | |
23d78b27 |
218 | int main(int argc, char *argv[]) { |
295cf525 |
219 | int c, opts, maxpending; |
220 | extern char *optarg; |
bb149f75 |
221 | FILE *inf; |
940356bd |
222 | |
bb149f75 |
223 | progname= strrchr(*argv, '/'); |
224 | if (progname) |
225 | progname++; |
226 | else |
227 | progname= *argv; |
b8b163f9 |
228 | |
295cf525 |
229 | maxpending= DEFMAXPENDING; |
230 | opts= 0; |
62fa243d |
231 | while ((c= getopt(argc, argv, "c:C:dp")) != -1) |
940356bd |
232 | switch (c) { |
295cf525 |
233 | case 'c': |
234 | maxpending= atoi(optarg); |
235 | if (maxpending < 1 || maxpending > MAXMAXPENDING) { |
236 | fprintf(stderr, "%s: unfeasible concurrency %d\n", progname, maxpending); |
237 | exit(1); |
238 | } |
239 | break; |
62fa243d |
240 | case 'C': |
241 | config_text= optarg; |
242 | break; |
940356bd |
243 | case 'd': |
bb149f75 |
244 | opts|= OPT_DEBUG; |
940356bd |
245 | break; |
246 | case 'p': |
bb149f75 |
247 | opts|= OPT_POLL; |
940356bd |
248 | break; |
249 | default: |
bb149f75 |
250 | usage(); |
940356bd |
251 | } |
940356bd |
252 | |
bb149f75 |
253 | argc-= optind; |
254 | argv+= optind; |
255 | |
256 | inf= NULL; |
257 | if (argc == 0) |
258 | inf= stdin; |
259 | else if (argc == 1) |
260 | inf= fopen(*argv, "r"); |
261 | else |
262 | usage(); |
263 | |
264 | if (!inf) |
265 | aargh("couldn't open input"); |
266 | |
295cf525 |
267 | proclog(inf, stdout, maxpending, opts); |
bb149f75 |
268 | |
269 | if (fclose(inf)) |
270 | aargh("fclose input"); |
271 | if (fclose(stdout)) |
272 | aargh("fclose output"); |
940356bd |
273 | |
81453aec |
274 | return 0; |
275 | } |