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