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