Licensing: Add copyright for Mark Wooding
[adns] / client / adnslogres.c
CommitLineData
81453aec 1/*
2 * adnslogres.c
3 * - a replacement for the Apache logresolve program using adns
4 */
5/*
23d78b27 6 * This file is
bef232ae 7 * Copyright (C) 1999-2000 Tony Finch <dot@dotat.at>
2658c305 8 * Copyright (C) 1999-2000 Ian Jackson <ian@davenant.greenend.org.uk>
d942707d 9 *
10 * It is part of adns, which is
26e1c3d6 11 * Copyright (C) 1997-2000,2003,2006,2014 Ian Jackson
17cd4f48 12 * Copyright (C) 2014 Mark Wooding
ae8cc977 13 * Copyright (C) 1999-2000,2003,2006 Tony Finch
14 * Copyright (C) 1991 Massachusetts Institute of Technology
15 * (See the file INSTALL for full details.)
81453aec 16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
7f8bbe29 19 * the Free Software Foundation; either version 3, or (at your option)
81453aec 20 * any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
8c09a4c6 28 * along with this program; if not, write to the Free Software Foundation.
23d78b27 29 *
2658c305 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.
81453aec 33 */
34
35static const char * const cvsid =
36 "$Id$";
37
38#include <sys/types.h>
39#include <sys/time.h>
40
940356bd 41#include <unistd.h>
81453aec 42#include <string.h>
43#include <stdlib.h>
44#include <stdio.h>
45#include <ctype.h>
46#include <errno.h>
5a0be244 47#include <stdarg.h>
81453aec 48
5a0be244 49#include "config.h"
81453aec 50#include "adns.h"
aa3ffb57 51#include "client.h"
81453aec 52
67a57aae 53#ifdef ADNS_REGRESS_TEST
54# include "hredirect.h"
55#endif
56
81453aec 57/* maximum number of concurrent DNS queries */
295cf525 58#define MAXMAXPENDING 64000
59#define DEFMAXPENDING 2000
81453aec 60
61/* maximum length of a line */
62#define MAXLINE 1024
63
940356bd 64/* option flags */
65#define OPT_DEBUG 1
66#define OPT_POLL 2
67
67a57aae 68static const char *const progname= "adnslogres";
62fa243d 69static const char *config_text;
81453aec 70
7c409027 71#define guard_null(str) ((str) ? (str) : "")
9715e5f4 72
d0bed398 73#define sensible_ctype(type,ch) (type((unsigned char)(ch)))
74 /* isfoo() functions from ctype.h can't safely be fed char - blech ! */
75
5a0be244 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
9715e5f4 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);
81453aec 90 exit(1);
91}
92
93/*
94 * Parse the IP address and convert to a reverse domain name.
95 */
940356bd 96static char *ipaddr2domain(char *start, char **addr, char **rest) {
81453aec 97 static char buf[30]; /* "123.123.123.123.in-addr.arpa.\0" */
98 char *ptrs[5];
99 int i;
100
940356bd 101 ptrs[0]= start;
102retry:
d0bed398 103 while (!sensible_ctype(isdigit,*ptrs[0]))
940356bd 104 if (!*ptrs[0]++) {
105 strcpy(buf, "invalid.");
106 *addr= *rest= NULL;
107 return buf;
108 }
bb149f75 109 for (i= 1; i < 5; i++) {
110 ptrs[i]= ptrs[i-1];
d0bed398 111 while (sensible_ctype(isdigit,*ptrs[i]++));
112 if ((i == 4 && !sensible_ctype(isspace,ptrs[i][-1])) ||
bb149f75 113 (i != 4 && ptrs[i][-1] != '.') ||
114 (ptrs[i]-ptrs[i-1] > 4)) {
115 ptrs[0]= ptrs[i]-1;
940356bd 116 goto retry;
bb149f75 117 }
81453aec 118 }
119 sprintf(buf, "%.*s.%.*s.%.*s.%.*s.in-addr.arpa.",
0ca555c6 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]);
81453aec 124 *addr= ptrs[0];
125 *rest= ptrs[4]-1;
940356bd 126 return buf;
81453aec 127}
128
bb149f75 129static void printline(FILE *outf, char *start, char *addr, char *rest, char *domain) {
81453aec 130 if (domain)
0ca555c6 131 fprintf(outf, "%.*s%s%s", (int)(addr - start), start, domain, rest);
81453aec 132 else
bb149f75 133 fputs(start, outf);
134 if (ferror(outf)) aargh("write output");
81453aec 135}
136
137typedef struct logline {
138 struct logline *next;
139 char *start, *addr, *rest;
140 adns_query query;
141} logline;
142
bb149f75 143static logline *readline(FILE *inf, adns_state adns, int opts) {
81453aec 144 static char buf[MAXLINE];
145 char *str;
146 logline *line;
147
bb149f75 148 if (fgets(buf, MAXLINE, inf)) {
81453aec 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);
bb149f75 155 str= ipaddr2domain(line->start, &line->addr, &line->rest);
940356bd 156 if (opts & OPT_DEBUG)
bf41c1c2 157 msg("submitting %.*s -> %s", (int)(line->rest-line->addr), guard_null(line->addr), str);
81453aec 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 }
bb149f75 164 if (!feof(inf))
81453aec 165 aargh("fgets");
166 return NULL;
167}
168
295cf525 169static void proclog(FILE *inf, FILE *outf, int maxpending, int opts) {
81453aec 170 int eof, err, len;
171 adns_state adns;
172 adns_answer *answer;
173 logline *head, *tail, *line;
62fa243d 174 adns_initflags initflags;
81453aec 175
62fa243d 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 }
81453aec 182 if (errno) aargh("adns_init");
bb149f75 183 head= tail= readline(inf, adns, opts);
81453aec 184 len= 1; eof= 0;
185 while (head) {
295cf525 186 while (head) {
187 if (opts & OPT_DEBUG)
188 msg("%d in queue; checking %.*s", len,
bf41c1c2 189 (int)(head->rest-head->addr), guard_null(head->addr));
67a57aae 190 if (eof || len >= maxpending) {
295cf525 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;
67a57aae 199 if (err) {
200 fprintf(stderr, "%s: adns_wait/check: %s", progname, strerror(err));
201 exit(1);
202 }
bb149f75 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;
67a57aae 206 free(line);
207 free(answer);
bb149f75 208 len--;
81453aec 209 }
210 if (!eof) {
bb149f75 211 line= readline(inf, adns, opts);
295cf525 212 if (line) {
213 if (!head) head= line;
214 else tail->next= line;
215 tail= line; len++;
216 } else {
81453aec 217 eof= 1;
81453aec 218 }
219 }
220 }
221 adns_finish(adns);
222}
223
8e5a4960 224static void printhelp(FILE *file) {
aa3ffb57 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);
8e5a4960 232}
233
234static void usage(void) {
235 printhelp(stderr);
bb149f75 236 exit(1);
237}
238
23d78b27 239int main(int argc, char *argv[]) {
295cf525 240 int c, opts, maxpending;
241 extern char *optarg;
bb149f75 242 FILE *inf;
940356bd 243
aa3ffb57 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 }
8e5a4960 252 if (ferror(stdout) || fclose(stdout)) { perror("stdout"); exit(1); }
253 exit(0);
254 }
255
295cf525 256 maxpending= DEFMAXPENDING;
257 opts= 0;
62fa243d 258 while ((c= getopt(argc, argv, "c:C:dp")) != -1)
940356bd 259 switch (c) {
295cf525 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;
62fa243d 267 case 'C':
268 config_text= optarg;
269 break;
940356bd 270 case 'd':
bb149f75 271 opts|= OPT_DEBUG;
940356bd 272 break;
273 case 'p':
bb149f75 274 opts|= OPT_POLL;
940356bd 275 break;
276 default:
bb149f75 277 usage();
940356bd 278 }
940356bd 279
bb149f75 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
295cf525 294 proclog(inf, stdout, maxpending, opts);
bb149f75 295
296 if (fclose(inf))
297 aargh("fclose input");
298 if (fclose(stdout))
299 aargh("fclose output");
940356bd 300
81453aec 301 return 0;
302}