chiark / gitweb /
Bump version.
[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
8  *    Copyright (C) 1997-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
29 #include "adnshost.h"
30
31 adns_state ads;
32 struct outstanding_list outstanding;
33
34 static unsigned long idcounter;
35
36 void ensure_adns_init(void) {
37   int r;
38   
39   if (ads) return;
40
41   if (signal(SIGPIPE,SIG_IGN) == SIG_ERR) sysfail("ignore SIGPIPE",errno);
42   r= adns_init(&ads,
43                adns_if_noautosys|adns_if_nosigpipe |
44                (ov_env ? 0 : adns_if_noenv) |
45                ov_verbose,
46                0);
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 static void prep_query(struct query_node **qun_r, int *quflags_r) {
54   struct query_node *qun;
55   char idbuf[20];
56   
57   if (ov_pipe && !ads) usageerr("-f/--pipe not consistent with domains on command line");
58   ensure_adns_init();
59   
60   qun= malloc(sizeof(*qun));
61   qun->pqfr= ov_pqfr;
62   if (ov_id) {
63     qun->id= xstrsave(ov_id);
64   } else {
65     sprintf(idbuf,"%lu",idcounter++);
66     idcounter &= 0x0fffffffflu;
67     qun->id= xstrsave(idbuf);
68   }
69
70   *quflags_r=
71     (ov_search ? adns_qf_search : 0) |
72     (ov_tcp ? adns_qf_usevc : 0) |
73     ((ov_pqfr.show_owner || ov_format == fmt_simple) ? adns_qf_owner : 0) |
74     (ov_qc_query ? adns_qf_quoteok_query : 0) |
75     (ov_qc_anshost ? adns_qf_quoteok_anshost : 0) |
76     (ov_qc_cname ? 0 : adns_qf_quoteok_cname) |
77     ov_cname,
78     
79   *qun_r= qun;
80 }
81   
82 void of_ptr(const struct optioninfo *oi, const char *arg, const char *arg2) {
83   struct query_node *qun;
84   int quflags, r;
85   struct sockaddr_in sa;
86
87   memset(&sa,0,sizeof(sa));
88   sa.sin_family= AF_INET;
89   if (!inet_aton(arg,&sa.sin_addr)) usageerr("invalid IP address %s",arg);
90
91   prep_query(&qun,&quflags);
92   qun->owner= xstrsave(arg);
93   r= adns_submit_reverse(ads,
94                          (struct sockaddr*)&sa,
95                          ov_type == adns_r_none ? adns_r_ptr : ov_type,
96                          quflags,
97                          qun,
98                          &qun->qu);
99   if (r) sysfail("adns_submit_reverse",r);
100
101   LIST_LINK_TAIL(outstanding,qun);
102 }
103
104 void of_reverse(const struct optioninfo *oi, const char *arg, const char *arg2) {
105   struct query_node *qun;
106   int quflags, r;
107   struct sockaddr_in sa;
108
109   memset(&sa,0,sizeof(sa));
110   sa.sin_family= AF_INET;
111   if (!inet_aton(arg,&sa.sin_addr)) usageerr("invalid IP address %s",arg);
112
113   prep_query(&qun,&quflags);
114   qun->owner= xmalloc(strlen(arg) + strlen(arg2) + 2);
115   sprintf(qun->owner, "%s %s", arg,arg2);
116   r= adns_submit_reverse_any(ads,
117                              (struct sockaddr*)&sa, arg2,
118                              ov_type == adns_r_none ? adns_r_txt : ov_type,
119                              quflags,
120                              qun,
121                              &qun->qu);
122   if (r) sysfail("adns_submit_reverse",r);
123
124   LIST_LINK_TAIL(outstanding,qun);
125 }
126
127 void query_do(const char *domain) {
128   struct query_node *qun;
129   int quflags, r;
130
131   prep_query(&qun,&quflags);
132   qun->owner= xstrsave(domain);
133   r= adns_submit(ads, domain,
134                  ov_type == adns_r_none ? adns_r_addr : ov_type,
135                  quflags,
136                  qun,
137                  &qun->qu);
138   if (r) sysfail("adns_submit",r);
139
140   LIST_LINK_TAIL(outstanding,qun);
141 }
142
143 static void dequeue_query(struct query_node *qun) {
144   LIST_UNLINK(outstanding,qun);
145   free(qun->id);
146   free(qun);
147 }
148
149 static void print_withspace(const char *str) {
150   if (printf("%s ", str) == EOF) outerr();
151 }
152
153 static void print_ttl(struct query_node *qun, adns_answer *answer) {
154   unsigned long ttl;
155   time_t now;
156   
157   switch (qun->pqfr.ttl) {
158   case tm_none:
159     return;
160   case tm_rel:
161     if (time(&now) == (time_t)-1) sysfail("get current time",errno);
162     ttl= answer->expires < now ? 0 : answer->expires - now;
163     break;
164   case tm_abs:
165     ttl= answer->expires;
166     break;
167   default:
168     abort();
169   }
170   if (printf("%lu ",ttl) == EOF) outerr();
171 }
172
173 static const char *owner_show(struct query_node *qun, adns_answer *answer) {
174   return answer->owner ? answer->owner : qun->owner;
175 }
176
177 static void print_owner_ttl(struct query_node *qun, adns_answer *answer) {
178   if (qun->pqfr.show_owner) print_withspace(owner_show(qun,answer));
179   print_ttl(qun,answer);
180 }
181
182 static void check_status(adns_status st) {
183   static const adns_status statuspoints[]= {
184     adns_s_ok,
185     adns_s_max_localfail, adns_s_max_remotefail, adns_s_max_tempfail,
186     adns_s_max_misconfig, adns_s_max_misquery
187   };
188
189   const adns_status *spp;
190   int minrcode;
191
192   for (minrcode=0, spp=statuspoints;
193        spp < statuspoints + (sizeof(statuspoints)/sizeof(statuspoints[0]));
194        spp++)
195     if (st > *spp) minrcode++;
196   if (rcode < minrcode) rcode= minrcode;
197 }
198
199 static void print_status(adns_status st, struct query_node *qun, adns_answer *answer) {
200   const char *statustypeabbrev, *statusabbrev, *statusstring;
201
202   statustypeabbrev= adns_errtypeabbrev(st);
203   statusabbrev= adns_errabbrev(st);
204   statusstring= adns_strerror(st);
205   assert(!strchr(statusstring,'"'));
206
207   if (printf("%s %d %s ", statustypeabbrev, st, statusabbrev)
208       == EOF) outerr();
209   print_owner_ttl(qun,answer);
210   if (qun->pqfr.show_cname)
211     print_withspace(answer->cname ? answer->cname : "$");
212   if (printf("\"%s\"\n", statusstring) == EOF) outerr();
213 }
214
215 static void print_dnsfail(adns_status st, struct query_node *qun, adns_answer *answer) {
216   int r;
217   const char *typename, *statusstring;
218   adns_status ist;
219   
220   if (ov_format == fmt_inline) {
221     if (fputs("; failed ",stdout) == EOF) outerr();
222     print_status(st,qun,answer);
223     return;
224   }
225   assert(ov_format == fmt_simple);
226   if (st == adns_s_nxdomain) {
227     r= fprintf(stderr,"%s does not exist\n", owner_show(qun,answer));
228   } else {
229     ist= adns_rr_info(answer->type, &typename, 0,0,0,0);
230     if (st == adns_s_nodata) {
231       r= fprintf(stderr,"%s has no %s record\n", owner_show(qun,answer), typename);
232     } else {
233       statusstring= adns_strerror(st);
234       r= fprintf(stderr,"Error during DNS %s lookup for %s: %s\n",
235                  typename, owner_show(qun,answer), statusstring);
236     }
237   }
238   if (r == EOF) sysfail("write error message to stderr",errno);
239 }
240     
241 void query_done(struct query_node *qun, adns_answer *answer) {
242   adns_status st, ist;
243   int rrn, nrrs;
244   const char *rrp, *realowner, *typename;
245   char *datastr;
246
247   st= answer->status;
248   nrrs= answer->nrrs;
249   if (ov_format == fmt_asynch) {
250     check_status(st);
251     if (printf("%s %d ", qun->id, nrrs) == EOF) outerr();
252     print_status(st,qun,answer);
253   } else {
254     if (qun->pqfr.show_cname && answer->cname) {
255       print_owner_ttl(qun,answer);
256       if (qun->pqfr.show_type) print_withspace("CNAME");
257       if (printf("%s\n", answer->cname) == EOF) outerr();
258     }
259     if (st) {
260       check_status(st);
261       print_dnsfail(st,qun,answer);
262     }
263   }
264   if (qun->pqfr.show_owner) {
265     realowner= answer->cname ? answer->cname : owner_show(qun,answer);
266     assert(realowner);
267   } else {
268     realowner= 0;
269   }
270   if (nrrs) {
271     for (rrn=0, rrp = answer->rrs.untyped;
272          rrn < nrrs;
273          rrn++, rrp += answer->rrsz) {
274       if (realowner) print_withspace(realowner);
275       print_ttl(qun,answer);
276       ist= adns_rr_info(answer->type, &typename, 0, 0, rrp, &datastr);
277       if (ist == adns_s_nomemory) sysfail("adns_rr_info failed",ENOMEM);
278       assert(!ist);
279       if (qun->pqfr.show_type) print_withspace(typename);
280       if (printf("%s\n",datastr) == EOF) outerr();
281       free(datastr);
282     }
283   }
284   if (fflush(stdout)) outerr();
285   free(answer);
286   dequeue_query(qun);
287 }
288
289 void of_asynch_id(const struct optioninfo *oi, const char *arg, const char *arg2) {
290   free(ov_id);
291   ov_id= xstrsave(arg);
292 }
293
294 void of_cancel_id(const struct optioninfo *oi, const char *arg, const char *arg2) {
295   struct query_node *qun;
296
297   for (qun= outstanding.head;
298        qun && strcmp(qun->id,arg);
299        qun= qun->next);
300   if (!qun) return;
301   adns_cancel(qun->qu);
302   dequeue_query(qun);
303 }