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