chiark / gitweb /
Do address-family dispatch with lots of switchy functions.
[adns] / client / adnslogres.c
CommitLineData
ab397b6e 1/*
2 * adnslogres.c
3 * - a replacement for the Apache logresolve program using adns
4 */
5/*
2b4bbcac 6 * This file is
c6826df6 7 * Copyright (C) 1999-2000 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
39f45e7e 11 * Copyright (C) 1997-2000,2003,2006 Ian Jackson
12 * Copyright (C) 1999-2000,2003,2006 Tony Finch
13 * Copyright (C) 1991 Massachusetts Institute of Technology
14 * (See the file INSTALL for full details.)
ab397b6e 15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2, or (at your option)
19 * any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software Foundation,
2b4bbcac 28 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 *
80a72950 30 * This version was originally supplied by Tony Finch, but has been
31 * modified by Ian Jackson as it was incorporated into adns and
32 * subsequently.
ab397b6e 33 */
34
35static const char * const cvsid =
7b186d8c 36 "$Id: adnslogres.c,v 1.23 2006/05/09 19:37:58 ian Exp $";
ab397b6e 37
38#include <sys/types.h>
39#include <sys/time.h>
40
ef20fccf 41#include <unistd.h>
ab397b6e 42#include <string.h>
43#include <stdlib.h>
44#include <stdio.h>
45#include <ctype.h>
46#include <errno.h>
cc0f95d0 47#include <stdarg.h>
ab397b6e 48
cc0f95d0 49#include "config.h"
ab397b6e 50#include "adns.h"
8a5e5147 51#include "client.h"
ab397b6e 52
d024926c 53#ifdef ADNS_REGRESS_TEST
54# include "hredirect.h"
55#endif
56
ab397b6e 57/* maximum number of concurrent DNS queries */
6fb17308 58#define MAXMAXPENDING 64000
59#define DEFMAXPENDING 2000
ab397b6e 60
61/* maximum length of a line */
62#define MAXLINE 1024
63
ef20fccf 64/* option flags */
65#define OPT_DEBUG 1
66#define OPT_POLL 2
67
d024926c 68static const char *const progname= "adnslogres";
f26a66d0 69static const char *config_text;
ab397b6e 70
3b94f31a 71#define guard_null(str) ((str) ? (str) : "")
1bd9afad 72
acff310d 73#define sensible_ctype(type,ch) (type((unsigned char)(ch)))
74 /* isfoo() functions from ctype.h can't safely be fed char - blech ! */
75
cc0f95d0 76static void msg(const char *fmt, ...) {
77 va_list al;
78
79 fprintf(stderr, "%s: ", progname);
80 va_start(al,fmt);
81 vfprintf(stderr, fmt, al);
82 va_end(al);
83 fputc('\n',stderr);
84}
85
1bd9afad 86static void aargh(const char *cause) {
87 const char *why = strerror(errno);
88 if (!why) why = "Unknown error";
89 msg("%s: %s (%d)", cause, why, errno);
ab397b6e 90 exit(1);
91}
92
93/*
94 * Parse the IP address and convert to a reverse domain name.
95 */
ef20fccf 96static char *ipaddr2domain(char *start, char **addr, char **rest) {
ab397b6e 97 static char buf[30]; /* "123.123.123.123.in-addr.arpa.\0" */
98 char *ptrs[5];
99 int i;
100
ef20fccf 101 ptrs[0]= start;
102retry:
acff310d 103 while (!sensible_ctype(isdigit,*ptrs[0]))
ef20fccf 104 if (!*ptrs[0]++) {
105 strcpy(buf, "invalid.");
106 *addr= *rest= NULL;
107 return buf;
108 }
80775c8e 109 for (i= 1; i < 5; i++) {
110 ptrs[i]= ptrs[i-1];
acff310d 111 while (sensible_ctype(isdigit,*ptrs[i]++));
112 if ((i == 4 && !sensible_ctype(isspace,ptrs[i][-1])) ||
80775c8e 113 (i != 4 && ptrs[i][-1] != '.') ||
114 (ptrs[i]-ptrs[i-1] > 4)) {
115 ptrs[0]= ptrs[i]-1;
ef20fccf 116 goto retry;
80775c8e 117 }
ab397b6e 118 }
119 sprintf(buf, "%.*s.%.*s.%.*s.%.*s.in-addr.arpa.",
c2180d3e 120 (int)(ptrs[4]-ptrs[3]-1), ptrs[3],
121 (int)(ptrs[3]-ptrs[2]-1), ptrs[2],
122 (int)(ptrs[2]-ptrs[1]-1), ptrs[1],
123 (int)(ptrs[1]-ptrs[0]-1), ptrs[0]);
ab397b6e 124 *addr= ptrs[0];
125 *rest= ptrs[4]-1;
ef20fccf 126 return buf;
ab397b6e 127}
128
80775c8e 129static void printline(FILE *outf, char *start, char *addr, char *rest, char *domain) {
ab397b6e 130 if (domain)
c2180d3e 131 fprintf(outf, "%.*s%s%s", (int)(addr - start), start, domain, rest);
ab397b6e 132 else
80775c8e 133 fputs(start, outf);
134 if (ferror(outf)) aargh("write output");
ab397b6e 135}
136
137typedef struct logline {
138 struct logline *next;
139 char *start, *addr, *rest;
140 adns_query query;
141} logline;
142
80775c8e 143static logline *readline(FILE *inf, adns_state adns, int opts) {
ab397b6e 144 static char buf[MAXLINE];
145 char *str;
146 logline *line;
147
80775c8e 148 if (fgets(buf, MAXLINE, inf)) {
ab397b6e 149 str= malloc(sizeof(*line) + strlen(buf) + 1);
150 if (!str) aargh("malloc");
151 line= (logline*)str;
152 line->next= NULL;
153 line->start= str+sizeof(logline);
154 strcpy(line->start, buf);
80775c8e 155 str= ipaddr2domain(line->start, &line->addr, &line->rest);
ef20fccf 156 if (opts & OPT_DEBUG)
7b186d8c 157 msg("submitting %.*s -> %s", (int)(line->rest-line->addr), guard_null(line->addr), str);
ab397b6e 158 if (adns_submit(adns, str, adns_r_ptr,
159 adns_qf_quoteok_cname|adns_qf_cname_loose,
160 NULL, &line->query))
161 aargh("adns_submit");
162 return line;
163 }
80775c8e 164 if (!feof(inf))
ab397b6e 165 aargh("fgets");
166 return NULL;
167}
168
6fb17308 169static void proclog(FILE *inf, FILE *outf, int maxpending, int opts) {
ab397b6e 170 int eof, err, len;
171 adns_state adns;
172 adns_answer *answer;
173 logline *head, *tail, *line;
f26a66d0 174 adns_initflags initflags;
ab397b6e 175
f26a66d0 176 initflags= (opts & OPT_DEBUG) ? adns_if_debug : 0;
177 if (config_text) {
178 errno= adns_init_strcfg(&adns, initflags, stderr, config_text);
179 } else {
180 errno= adns_init(&adns, initflags, 0);
181 }
ab397b6e 182 if (errno) aargh("adns_init");
80775c8e 183 head= tail= readline(inf, adns, opts);
ab397b6e 184 len= 1; eof= 0;
185 while (head) {
6fb17308 186 while (head) {
187 if (opts & OPT_DEBUG)
188 msg("%d in queue; checking %.*s", len,
7b186d8c 189 (int)(head->rest-head->addr), guard_null(head->addr));
d024926c 190 if (eof || len >= maxpending) {
6fb17308 191 if (opts & OPT_POLL)
192 err= adns_wait_poll(adns, &head->query, &answer, NULL);
193 else
194 err= adns_wait(adns, &head->query, &answer, NULL);
195 } else {
196 err= adns_check(adns, &head->query, &answer, NULL);
197 }
198 if (err == EAGAIN) break;
d024926c 199 if (err) {
200 fprintf(stderr, "%s: adns_wait/check: %s", progname, strerror(err));
201 exit(1);
202 }
80775c8e 203 printline(outf, head->start, head->addr, head->rest,
204 answer->status == adns_s_ok ? *answer->rrs.str : NULL);
205 line= head; head= head->next;
d024926c 206 free(line);
207 free(answer);
80775c8e 208 len--;
ab397b6e 209 }
210 if (!eof) {
80775c8e 211 line= readline(inf, adns, opts);
6fb17308 212 if (line) {
213 if (!head) head= line;
214 else tail->next= line;
215 tail= line; len++;
216 } else {
ab397b6e 217 eof= 1;
ab397b6e 218 }
219 }
220 }
221 adns_finish(adns);
222}
223
aa958273 224static void printhelp(FILE *file) {
8a5e5147 225 fputs("usage: adnslogres [<options>] [<logfile>]\n"
226 " adnslogres --version|--help\n"
227 "options: -c <concurrency> set max number of outstanding queries\n"
228 " -p use poll(2) instead of select(2)\n"
229 " -d turn on debugging\n"
230 " -C <config> use instead of contents of resolv.conf\n",
231 stdout);
aa958273 232}
233
234static void usage(void) {
235 printhelp(stderr);
80775c8e 236 exit(1);
237}
238
2b4bbcac 239int main(int argc, char *argv[]) {
6fb17308 240 int c, opts, maxpending;
241 extern char *optarg;
80775c8e 242 FILE *inf;
ef20fccf 243
8a5e5147 244 if (argv[1] && !strncmp(argv[1],"--",2)) {
245 if (!strcmp(argv[1],"--help")) {
246 printhelp(stdout);
247 } else if (!strcmp(argv[1],"--version")) {
248 fputs(VERSION_MESSAGE("adnslogres"),stdout);
249 } else {
250 usage();
251 }
aa958273 252 if (ferror(stdout) || fclose(stdout)) { perror("stdout"); exit(1); }
253 exit(0);
254 }
255
6fb17308 256 maxpending= DEFMAXPENDING;
257 opts= 0;
f26a66d0 258 while ((c= getopt(argc, argv, "c:C:dp")) != -1)
ef20fccf 259 switch (c) {
6fb17308 260 case 'c':
261 maxpending= atoi(optarg);
262 if (maxpending < 1 || maxpending > MAXMAXPENDING) {
263 fprintf(stderr, "%s: unfeasible concurrency %d\n", progname, maxpending);
264 exit(1);
265 }
266 break;
f26a66d0 267 case 'C':
268 config_text= optarg;
269 break;
ef20fccf 270 case 'd':
80775c8e 271 opts|= OPT_DEBUG;
ef20fccf 272 break;
273 case 'p':
80775c8e 274 opts|= OPT_POLL;
ef20fccf 275 break;
276 default:
80775c8e 277 usage();
ef20fccf 278 }
ef20fccf 279
80775c8e 280 argc-= optind;
281 argv+= optind;
282
283 inf= NULL;
284 if (argc == 0)
285 inf= stdin;
286 else if (argc == 1)
287 inf= fopen(*argv, "r");
288 else
289 usage();
290
291 if (!inf)
292 aargh("couldn't open input");
293
6fb17308 294 proclog(inf, stdout, maxpending, opts);
80775c8e 295
296 if (fclose(inf))
297 aargh("fclose input");
298 if (fclose(stdout))
299 aargh("fclose output");
ef20fccf 300
ab397b6e 301 return 0;
302}