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