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