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