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