81453aec |
1 | /* |
2 | * adnslogres.c |
3 | * - a replacement for the Apache logresolve program using adns |
4 | */ |
5 | /* |
23d78b27 |
6 | * This file is |
d942707d |
7 | * Copyright (C) 1999 Tony Finch <dot@dotat.at> |
23d78b27 |
8 | * Copyright (C) 1999 Ian Jackson <ian@davenant.greenend.org.uk> |
d942707d |
9 | * |
10 | * It is part of adns, which is |
11 | * Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk> |
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 | * |
28 | * This version was originally supplied by Tony Finch, but has been |
29 | * modified by Ian Jackson as it was incorporated into adns. |
81453aec |
30 | */ |
31 | |
32 | static const char * const cvsid = |
33 | "$Id$"; |
34 | |
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> |
44 | |
45 | #include "adns.h" |
46 | |
47 | /* maximum number of concurrent DNS queries */ |
48 | #define MAXPENDING 1000 |
49 | |
50 | /* maximum length of a line */ |
51 | #define MAXLINE 1024 |
52 | |
940356bd |
53 | /* option flags */ |
54 | #define OPT_DEBUG 1 |
55 | #define OPT_POLL 2 |
56 | |
81453aec |
57 | static const char *progname; |
58 | |
9715e5f4 |
59 | #define msg(fmt, args...) fprintf(stderr, "%s: " fmt "\n", progname, ##args) |
60 | |
61 | static void aargh(const char *cause) { |
62 | const char *why = strerror(errno); |
63 | if (!why) why = "Unknown error"; |
64 | msg("%s: %s (%d)", cause, why, errno); |
81453aec |
65 | exit(1); |
66 | } |
67 | |
68 | /* |
69 | * Parse the IP address and convert to a reverse domain name. |
70 | */ |
940356bd |
71 | static char *ipaddr2domain(char *start, char **addr, char **rest) { |
81453aec |
72 | static char buf[30]; /* "123.123.123.123.in-addr.arpa.\0" */ |
73 | char *ptrs[5]; |
74 | int i; |
75 | |
940356bd |
76 | ptrs[0]= start; |
77 | retry: |
78 | while (!isdigit(*ptrs[0])) |
79 | if (!*ptrs[0]++) { |
80 | strcpy(buf, "invalid."); |
81 | *addr= *rest= NULL; |
82 | return buf; |
83 | } |
bb149f75 |
84 | for (i= 1; i < 5; i++) { |
85 | ptrs[i]= ptrs[i-1]; |
86 | while (isdigit(*ptrs[i]++)); |
87 | if ((i == 4 && !isspace(ptrs[i][-1])) || |
88 | (i != 4 && ptrs[i][-1] != '.') || |
89 | (ptrs[i]-ptrs[i-1] > 4)) { |
90 | ptrs[0]= ptrs[i]-1; |
940356bd |
91 | goto retry; |
bb149f75 |
92 | } |
81453aec |
93 | } |
94 | sprintf(buf, "%.*s.%.*s.%.*s.%.*s.in-addr.arpa.", |
95 | ptrs[4]-ptrs[3]-1, ptrs[3], |
96 | ptrs[3]-ptrs[2]-1, ptrs[2], |
97 | ptrs[2]-ptrs[1]-1, ptrs[1], |
98 | ptrs[1]-ptrs[0]-1, ptrs[0]); |
99 | *addr= ptrs[0]; |
100 | *rest= ptrs[4]-1; |
940356bd |
101 | return buf; |
81453aec |
102 | } |
103 | |
bb149f75 |
104 | static void printline(FILE *outf, char *start, char *addr, char *rest, char *domain) { |
81453aec |
105 | if (domain) |
bb149f75 |
106 | fprintf(outf, "%.*s%s%s", addr - start, start, domain, rest); |
81453aec |
107 | else |
bb149f75 |
108 | fputs(start, outf); |
109 | if (ferror(outf)) aargh("write output"); |
81453aec |
110 | } |
111 | |
112 | typedef struct logline { |
113 | struct logline *next; |
114 | char *start, *addr, *rest; |
115 | adns_query query; |
116 | } logline; |
117 | |
bb149f75 |
118 | static logline *readline(FILE *inf, adns_state adns, int opts) { |
81453aec |
119 | static char buf[MAXLINE]; |
120 | char *str; |
121 | logline *line; |
122 | |
bb149f75 |
123 | if (fgets(buf, MAXLINE, inf)) { |
81453aec |
124 | str= malloc(sizeof(*line) + strlen(buf) + 1); |
125 | if (!str) aargh("malloc"); |
126 | line= (logline*)str; |
127 | line->next= NULL; |
128 | line->start= str+sizeof(logline); |
129 | strcpy(line->start, buf); |
bb149f75 |
130 | str= ipaddr2domain(line->start, &line->addr, &line->rest); |
940356bd |
131 | if (opts & OPT_DEBUG) |
bb149f75 |
132 | msg("submitting %.*s -> %s", line->rest-line->addr, line->addr, str); |
81453aec |
133 | if (adns_submit(adns, str, adns_r_ptr, |
134 | adns_qf_quoteok_cname|adns_qf_cname_loose, |
135 | NULL, &line->query)) |
136 | aargh("adns_submit"); |
137 | return line; |
138 | } |
bb149f75 |
139 | if (!feof(inf)) |
81453aec |
140 | aargh("fgets"); |
141 | return NULL; |
142 | } |
143 | |
bb149f75 |
144 | static void proclog(FILE *inf, FILE *outf, int opts) { |
81453aec |
145 | int eof, err, len; |
146 | adns_state adns; |
147 | adns_answer *answer; |
148 | logline *head, *tail, *line; |
149 | |
940356bd |
150 | errno= adns_init(&adns, (opts & OPT_DEBUG) ? adns_if_debug : 0, 0); |
81453aec |
151 | if (errno) aargh("adns_init"); |
bb149f75 |
152 | head= tail= readline(inf, adns, opts); |
81453aec |
153 | len= 1; eof= 0; |
154 | while (head) { |
bb149f75 |
155 | if (opts & OPT_DEBUG) |
156 | msg("%d in queue; checking %.*s", len, |
157 | head->rest-head->addr, head->addr); |
81453aec |
158 | if (eof || len > MAXPENDING) |
940356bd |
159 | if (opts & OPT_POLL) |
160 | err= adns_wait_poll(adns, &head->query, &answer, NULL); |
161 | else |
162 | err= adns_wait(adns, &head->query, &answer, NULL); |
81453aec |
163 | else |
164 | err= adns_check(adns, &head->query, &answer, NULL); |
226c5eef |
165 | if (err != EAGAIN) { |
bb149f75 |
166 | printline(outf, head->start, head->addr, head->rest, |
167 | answer->status == adns_s_ok ? *answer->rrs.str : NULL); |
168 | line= head; head= head->next; |
169 | free(line); free(answer); |
170 | len--; |
81453aec |
171 | } |
172 | if (!eof) { |
bb149f75 |
173 | line= readline(inf, adns, opts); |
81453aec |
174 | if (!line) |
175 | eof= 1; |
176 | else { |
177 | if (!head) |
bb149f75 |
178 | head= line; |
81453aec |
179 | else |
bb149f75 |
180 | tail->next= line; |
181 | tail= line; |
81453aec |
182 | len++; |
183 | } |
184 | } |
185 | } |
186 | adns_finish(adns); |
187 | } |
188 | |
bb149f75 |
189 | static void usage(void) { |
190 | fprintf(stderr, "usage: %s [-d] [-p] [logfile]\n", progname); |
191 | exit(1); |
192 | } |
193 | |
23d78b27 |
194 | int main(int argc, char *argv[]) { |
940356bd |
195 | int c, opts; |
bb149f75 |
196 | FILE *inf; |
940356bd |
197 | |
bb149f75 |
198 | progname= strrchr(*argv, '/'); |
199 | if (progname) |
200 | progname++; |
201 | else |
202 | progname= *argv; |
940356bd |
203 | opts= 0; |
204 | |
bb149f75 |
205 | while ((c= getopt(argc, argv, "dp")) != -1) |
940356bd |
206 | switch (c) { |
207 | case 'd': |
bb149f75 |
208 | opts|= OPT_DEBUG; |
940356bd |
209 | break; |
210 | case 'p': |
bb149f75 |
211 | opts|= OPT_POLL; |
940356bd |
212 | break; |
213 | default: |
bb149f75 |
214 | usage(); |
940356bd |
215 | } |
940356bd |
216 | |
bb149f75 |
217 | argc-= optind; |
218 | argv+= optind; |
219 | |
220 | inf= NULL; |
221 | if (argc == 0) |
222 | inf= stdin; |
223 | else if (argc == 1) |
224 | inf= fopen(*argv, "r"); |
225 | else |
226 | usage(); |
227 | |
228 | if (!inf) |
229 | aargh("couldn't open input"); |
230 | |
231 | proclog(inf, stdout, opts); |
232 | |
233 | if (fclose(inf)) |
234 | aargh("fclose input"); |
235 | if (fclose(stdout)) |
236 | aargh("fclose output"); |
940356bd |
237 | |
81453aec |
238 | return 0; |
239 | } |