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