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