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