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