chiark / gitweb /
Support IPv6 PTR lookups.
[adns] / client / adh-query.c
1 /*
2  * adh-query.c
3  * - useful general-purpose resolver client program
4  *   make queries and print answers
5  */
6 /*
7  *  This file is part of adns, which is
8  *    Copyright (C) 1997-2000,2003,2006  Ian Jackson
9  *    Copyright (C) 1999-2000,2003,2006  Tony Finch
10  *    Copyright (C) 1991 Massachusetts Institute of Technology
11  *  (See the file INSTALL for full details.)
12  *  
13  *  This program is free software; you can redistribute it and/or modify
14  *  it under the terms of the GNU General Public License as published by
15  *  the Free Software Foundation; either version 2, or (at your option)
16  *  any later version.
17  *  
18  *  This program is distributed in the hope that it will be useful,
19  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  *  GNU General Public License for more details.
22  *  
23  *  You should have received a copy of the GNU General Public License
24  *  along with this program; if not, write to the Free Software Foundation,
25  *  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
26  */
27
28 #include "adnshost.h"
29
30 adns_state ads;
31 struct outstanding_list outstanding;
32
33 static unsigned long idcounter;
34
35 void ensure_adns_init(void) {
36   adns_initflags initflags;
37   int r;
38   
39   if (ads) return;
40
41   if (signal(SIGPIPE,SIG_IGN) == SIG_ERR) sysfail("ignore SIGPIPE",errno);
42
43   initflags= adns_if_noautosys|adns_if_nosigpipe|ov_verbose;
44   if (!ov_env) initflags |= adns_if_noenv;
45
46   if (config_text) {
47     r= adns_init_strcfg(&ads, initflags, stderr, config_text);
48   } else {
49     r= adns_init(&ads, initflags, 0);
50   }
51   if (r) sysfail("adns_init",r);
52
53   if (ov_format == fmt_default)
54     ov_format= ov_asynch ? fmt_asynch : fmt_simple;
55 }
56
57 void type_info(adns_rrtype type, const char **typename_r,
58                const void *datap, char **data_r) {
59   static char buf[12];
60   adns_status st;
61   
62   st= adns_rr_info(type, typename_r, 0,0, datap,data_r);
63   if (st == adns_s_nomemory) sysfail("adns_rr_info failed",ENOMEM);
64   assert(!st);
65   if (typename_r && !*typename_r) {
66     sprintf(buf,"TYPE%d", (int)(type & adns_rrt_typemask));
67     *typename_r= buf;
68   }
69 }
70
71 static void prep_query(struct query_node **qun_r, int *quflags_r) {
72   struct query_node *qun;
73   char idbuf[20];
74   
75   if (ov_pipe && !ads) usageerr("-f/--pipe not consistent with domains on command line");
76   ensure_adns_init();
77   
78   qun= malloc(sizeof(*qun));
79   qun->pqfr= ov_pqfr;
80   if (ov_id) {
81     qun->id= xstrsave(ov_id);
82   } else {
83     sprintf(idbuf,"%lu",idcounter++);
84     idcounter &= 0x0fffffffflu;
85     qun->id= xstrsave(idbuf);
86   }
87
88   *quflags_r=
89     (ov_search ? adns_qf_search : 0) |
90     (ov_tcp ? adns_qf_usevc : 0) |
91     ((ov_pqfr.show_owner || ov_format == fmt_simple) ? adns_qf_owner : 0) |
92     (ov_qc_query ? adns_qf_quoteok_query : 0) |
93     (ov_qc_anshost ? adns_qf_quoteok_anshost : 0) |
94     (ov_qc_cname ? 0 : adns_qf_quoteok_cname) |
95     ov_cname,
96     
97   *qun_r= qun;
98 }
99   
100 void of_ptr(const struct optioninfo *oi, const char *arg, const char *arg2) {
101   struct query_node *qun;
102   int quflags, r;
103   struct addrinfo *ai, ai_hint = { 0 };
104   int err;
105
106   ai_hint.ai_family = AF_UNSPEC;
107   ai_hint.ai_socktype = SOCK_DGRAM;
108   ai_hint.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
109
110   err = getaddrinfo(arg, 0, &ai_hint, &ai);
111   if (err) usageerr("invalid IP address %s",arg);
112
113   prep_query(&qun,&quflags);
114   qun->owner= xstrsave(arg);
115   r= adns_submit_reverse(ads,
116                          ai->ai_addr,
117                          ov_type == adns_r_none ? adns_r_ptr : ov_type,
118                          quflags,
119                          qun,
120                          &qun->qu);
121   if (r) sysfail("adns_submit_reverse",r);
122   freeaddrinfo(ai);
123
124   LIST_LINK_TAIL(outstanding,qun);
125 }
126
127 void of_reverse(const struct optioninfo *oi, const char *arg, const char *arg2) {
128   struct query_node *qun;
129   int quflags, r;
130   struct addrinfo *ai, ai_hint = { 0 };
131   int err;
132
133   ai_hint.ai_family = AF_UNSPEC;
134   ai_hint.ai_socktype = SOCK_DGRAM;
135   ai_hint.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
136
137   err = getaddrinfo(arg, 0, &ai_hint, &ai);
138   if (err) usageerr("invalid IP address %s",arg);
139
140   prep_query(&qun,&quflags);
141   qun->owner= xmalloc(strlen(arg) + strlen(arg2) + 2);
142   sprintf(qun->owner, "%s %s", arg,arg2);
143   r= adns_submit_reverse_any(ads,
144                              ai->ai_addr, arg2,
145                              ov_type == adns_r_none ? adns_r_txt : ov_type,
146                              quflags,
147                              qun,
148                              &qun->qu);
149   if (r) sysfail("adns_submit_reverse",r);
150   freeaddrinfo(ai);
151
152   LIST_LINK_TAIL(outstanding,qun);
153 }
154
155 void query_do(const char *domain) {
156   struct query_node *qun;
157   int quflags, r;
158
159   prep_query(&qun,&quflags);
160   qun->owner= xstrsave(domain);
161   r= adns_submit(ads, domain,
162                  ov_type == adns_r_none ? adns_r_addr : ov_type,
163                  quflags,
164                  qun,
165                  &qun->qu);
166   if (r) sysfail("adns_submit",r);
167
168   LIST_LINK_TAIL(outstanding,qun);
169 }
170
171 static void dequeue_query(struct query_node *qun) {
172   LIST_UNLINK(outstanding,qun);
173   free(qun->id);
174   free(qun->owner);
175   free(qun);
176 }
177
178 static void print_withspace(const char *str) {
179   if (printf("%s ", str) == EOF) outerr();
180 }
181
182 static void print_ttl(struct query_node *qun, adns_answer *answer) {
183   unsigned long ttl;
184   time_t now;
185   
186   switch (qun->pqfr.ttl) {
187   case tm_none:
188     return;
189   case tm_rel:
190     if (time(&now) == (time_t)-1) sysfail("get current time",errno);
191     ttl= answer->expires < now ? 0 : answer->expires - now;
192     break;
193   case tm_abs:
194     ttl= answer->expires;
195     break;
196   default:
197     abort();
198   }
199   if (printf("%lu ",ttl) == EOF) outerr();
200 }
201
202 static const char *owner_show(struct query_node *qun, adns_answer *answer) {
203   return answer->owner ? answer->owner : qun->owner;
204 }
205
206 static void print_owner_ttl(struct query_node *qun, adns_answer *answer) {
207   if (qun->pqfr.show_owner) print_withspace(owner_show(qun,answer));
208   print_ttl(qun,answer);
209 }
210
211 static void check_status(adns_status st) {
212   static const adns_status statuspoints[]= {
213     adns_s_ok,
214     adns_s_max_localfail, adns_s_max_remotefail, adns_s_max_tempfail,
215     adns_s_max_misconfig, adns_s_max_misquery
216   };
217
218   const adns_status *spp;
219   int minrcode;
220
221   for (minrcode=0, spp=statuspoints;
222        spp < statuspoints + (sizeof(statuspoints)/sizeof(statuspoints[0]));
223        spp++)
224     if (st > *spp) minrcode++;
225   if (rcode < minrcode) rcode= minrcode;
226 }
227
228 static void print_status(adns_status st, struct query_node *qun, adns_answer *answer) {
229   const char *statustypeabbrev, *statusabbrev, *statusstring;
230
231   statustypeabbrev= adns_errtypeabbrev(st);
232   statusabbrev= adns_errabbrev(st);
233   statusstring= adns_strerror(st);
234   assert(!strchr(statusstring,'"'));
235
236   if (printf("%s %d %s ", statustypeabbrev, st, statusabbrev)
237       == EOF) outerr();
238   print_owner_ttl(qun,answer);
239   if (qun->pqfr.show_cname)
240     print_withspace(answer->cname ? answer->cname : "$");
241   if (printf("\"%s\"\n", statusstring) == EOF) outerr();
242 }
243
244 static void print_dnsfail(adns_status st, struct query_node *qun, adns_answer *answer) {
245   int r;
246   const char *typename, *statusstring;
247   
248   if (ov_format == fmt_inline) {
249     if (fputs("; failed ",stdout) == EOF) outerr();
250     print_status(st,qun,answer);
251     return;
252   }
253   assert(ov_format == fmt_simple);
254   if (st == adns_s_nxdomain) {
255     r= fprintf(stderr,"%s does not exist\n", owner_show(qun,answer));
256   } else {
257     type_info(answer->type, &typename, 0,0);
258     if (st == adns_s_nodata) {
259       r= fprintf(stderr,"%s has no %s record\n", owner_show(qun,answer), typename);
260     } else {
261       statusstring= adns_strerror(st);
262       r= fprintf(stderr,"Error during DNS %s lookup for %s: %s\n",
263                  typename, owner_show(qun,answer), statusstring);
264     }
265   }
266   if (r == EOF) sysfail("write error message to stderr",errno);
267 }
268     
269 void query_done(struct query_node *qun, adns_answer *answer) {
270   adns_status st;
271   int rrn, nrrs;
272   const char *rrp, *realowner, *typename;
273   char *datastr;
274
275   st= answer->status;
276   nrrs= answer->nrrs;
277   if (ov_format == fmt_asynch) {
278     check_status(st);
279     if (printf("%s %d ", qun->id, nrrs) == EOF) outerr();
280     print_status(st,qun,answer);
281   } else {
282     if (qun->pqfr.show_cname && answer->cname) {
283       print_owner_ttl(qun,answer);
284       if (qun->pqfr.show_type) print_withspace("CNAME");
285       if (printf("%s\n", answer->cname) == EOF) outerr();
286     }
287     if (st) {
288       check_status(st);
289       print_dnsfail(st,qun,answer);
290     }
291   }
292   if (qun->pqfr.show_owner) {
293     realowner= answer->cname ? answer->cname : owner_show(qun,answer);
294     assert(realowner);
295   } else {
296     realowner= 0;
297   }
298   if (nrrs) {
299     for (rrn=0, rrp = answer->rrs.untyped;
300          rrn < nrrs;
301          rrn++, rrp += answer->rrsz) {
302       if (realowner) print_withspace(realowner);
303       print_ttl(qun,answer);
304       type_info(answer->type,&typename, rrp,&datastr);
305       if (qun->pqfr.show_type) print_withspace(typename);
306       if (printf("%s\n",datastr) == EOF) outerr();
307       free(datastr);
308     }
309   }
310   if (fflush(stdout)) outerr();
311   free(answer);
312   dequeue_query(qun);
313 }
314
315 void of_asynch_id(const struct optioninfo *oi, const char *arg, const char *arg2) {
316   free(ov_id);
317   ov_id= xstrsave(arg);
318 }
319
320 void of_cancel_id(const struct optioninfo *oi, const char *arg, const char *arg2) {
321   struct query_node *qun;
322
323   for (qun= outstanding.head;
324        qun && strcmp(qun->id,arg);
325        qun= qun->next);
326   if (!qun) return;
327   adns_cancel(qun->qu);
328   dequeue_query(qun);
329 }