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