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