1 /*
2  * Copyright (C) 2004-2009, 2011-2015  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 2000-2003  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #ifndef DIG_H
19 #define DIG_H
20 
21 /*! \file */
22 
23 #include <dns/rdatalist.h>
24 
25 #include <dst/dst.h>
26 
27 #include <isc/boolean.h>
28 #include <isc/buffer.h>
29 #include <isc/bufferlist.h>
30 #include <isc/formatcheck.h>
31 #include <isc/lang.h>
32 #include <isc/list.h>
33 #include <isc/mem.h>
34 #include <isc/print.h>
35 #include <isc/sockaddr.h>
36 #include <isc/socket.h>
37 
38 #define MXSERV 20
39 #define MXNAME (DNS_NAME_MAXTEXT+1)
40 #define MXRD 32
41 /*% Buffer Size */
42 #define BUFSIZE 512
43 #define COMMSIZE 0xffff
44 #ifndef RESOLV_CONF
45 /*% location of resolve.conf */
46 #define RESOLV_CONF "/etc/resolv.conf"
47 #endif
48 /*% output buffer */
49 #define OUTPUTBUF 32767
50 /*% Max RR Limit */
51 #define MAXRRLIMIT 0xffffffff
52 #define MAXTIMEOUT 0xffff
53 /*% Max number of tries */
54 #define MAXTRIES 0xffffffff
55 /*% Max number of dots */
56 #define MAXNDOTS 0xffff
57 /*% Max number of ports */
58 #define MAXPORT 0xffff
59 /*% Max serial number */
60 #define MAXSERIAL 0xffffffff
61 
62 /*% Default TCP Timeout */
63 #define TCP_TIMEOUT 10
64 /*% Default UDP Timeout */
65 #define UDP_TIMEOUT 5
66 
67 #define SERVER_TIMEOUT 1
68 
69 #define LOOKUP_LIMIT 64
70 /*%
71  * Lookup_limit is just a limiter, keeping too many lookups from being
72  * created.  It's job is mainly to prevent the program from running away
73  * in a tight loop of constant lookups.  It's value is arbitrary.
74  */
75 
76 /*
77  * Defaults for the sigchase suboptions.  Consolidated here because
78  * these control the layout of dig_lookup_t (among other things).
79  */
80 #ifdef DIG_SIGCHASE
81 #ifndef DIG_SIGCHASE_BU
82 #define DIG_SIGCHASE_BU 1
83 #endif
84 #ifndef DIG_SIGCHASE_TD
85 #define DIG_SIGCHASE_TD 1
86 #endif
87 #endif
88 
89 ISC_LANG_BEGINDECLS
90 
91 typedef struct dig_lookup dig_lookup_t;
92 typedef struct dig_query dig_query_t;
93 typedef struct dig_server dig_server_t;
94 #ifdef DIG_SIGCHASE
95 typedef struct dig_message dig_message_t;
96 #endif
97 typedef ISC_LIST(dig_server_t) dig_serverlist_t;
98 typedef struct dig_searchlist dig_searchlist_t;
99 
100 /*% The dig_lookup structure */
101 struct dig_lookup {
102 	isc_boolean_t
103 		pending, /*%< Pending a successful answer */
104 		waiting_connect,
105 		doing_xfr,
106 		ns_search_only, /*%< dig +nssearch, host -C */
107 		identify, /*%< Append an "on server <foo>" message */
108 		identify_previous_line, /*% Prepend a "Nameserver <foo>:"
109 					   message, with newline and tab */
110 		ignore,
111 		recurse,
112 		aaonly,
113 		adflag,
114 		cdflag,
115 		trace, /*% dig +trace */
116 		trace_root, /*% initial query for either +trace or +nssearch */
117 		tcp_mode,
118 		tcp_mode_set,
119 		ip6_int,
120 		comments,
121 		stats,
122 		section_question,
123 		section_answer,
124 		section_authority,
125 		section_additional,
126 		servfail_stops,
127 		new_search,
128 		need_search,
129 		done_as_is,
130 		besteffort,
131 		dnssec,
132 		nsid;   /*% Name Server ID (RFC 5001) */
133 #ifdef DIG_SIGCHASE
134 isc_boolean_t	sigchase;
135 #if DIG_SIGCHASE_TD
136 	isc_boolean_t do_topdown,
137 		trace_root_sigchase,
138 		rdtype_sigchaseset,
139 		rdclass_sigchaseset;
140 	/* Name we are going to validate RRset */
141 	char textnamesigchase[MXNAME];
142 #endif
143 #endif
144 
145 	char textname[MXNAME]; /*% Name we're going to be looking up */
146 	char cmdline[MXNAME];
147 	dns_rdatatype_t rdtype;
148 	dns_rdatatype_t qrdtype;
149 #if DIG_SIGCHASE_TD
150 	dns_rdatatype_t rdtype_sigchase;
151 	dns_rdatatype_t qrdtype_sigchase;
152 	dns_rdataclass_t rdclass_sigchase;
153 #endif
154 	dns_rdataclass_t rdclass;
155 	isc_boolean_t rdtypeset;
156 	isc_boolean_t rdclassset;
157 	char namespace[BUFSIZE];
158 	char onamespace[BUFSIZE];
159 	isc_buffer_t namebuf;
160 	isc_buffer_t onamebuf;
161 	isc_buffer_t renderbuf;
162 	char *sendspace;
163 	dns_name_t *name;
164 	isc_timer_t *timer;
165 	isc_interval_t interval;
166 	dns_message_t *sendmsg;
167 	dns_name_t *oname;
168 	ISC_LINK(dig_lookup_t) link;
169 	ISC_LIST(dig_query_t) q;
170 	ISC_LIST(dig_query_t) connecting;
171 	dig_query_t *current_query;
172 	dig_serverlist_t my_server_list;
173 	dig_searchlist_t *origin;
174 	dig_query_t *xfr_q;
175 	isc_uint32_t retries;
176 	int nsfound;
177 	isc_uint16_t udpsize;
178 	isc_int16_t edns;
179 	isc_uint32_t ixfr_serial;
180 	isc_buffer_t rdatabuf;
181 	char rdatastore[MXNAME];
182 	dst_context_t *tsigctx;
183 	isc_buffer_t *querysig;
184 	isc_uint32_t msgcounter;
185 	dns_fixedname_t fdomain;
186 };
187 
188 /*% The dig_query structure */
189 struct dig_query {
190 	dig_lookup_t *lookup;
191 	isc_boolean_t waiting_connect,
192 		pending_free,
193 		waiting_senddone,
194 		first_pass,
195 		first_soa_rcvd,
196 		second_rr_rcvd,
197 		first_repeat_rcvd,
198 		recv_made,
199 		warn_id;
200 	isc_uint32_t first_rr_serial;
201 	isc_uint32_t second_rr_serial;
202 	isc_uint32_t msg_count;
203 	isc_uint32_t rr_count;
204 	isc_boolean_t ixfr_axfr;
205 	char *servname;
206 	char *userarg;
207 	isc_bufferlist_t sendlist,
208 		recvlist,
209 		lengthlist;
210 	isc_buffer_t recvbuf,
211 		lengthbuf,
212 		slbuf;
213 	char *recvspace,
214 		lengthspace[4],
215 		slspace[4];
216 	isc_socket_t *sock;
217 	ISC_LINK(dig_query_t) link;
218 	ISC_LINK(dig_query_t) clink;
219 	isc_sockaddr_t sockaddr;
220 	isc_time_t time_sent;
221 	isc_time_t time_recv;
222 	isc_uint64_t byte_count;
223 	isc_buffer_t sendbuf;
224 };
225 
226 struct dig_server {
227 	char servername[MXNAME];
228 	char userarg[MXNAME];
229 	ISC_LINK(dig_server_t) link;
230 };
231 
232 struct dig_searchlist {
233 	char origin[MXNAME];
234 	ISC_LINK(dig_searchlist_t) link;
235 };
236 #ifdef DIG_SIGCHASE
237 struct dig_message {
238 		dns_message_t *msg;
239 		ISC_LINK(dig_message_t) link;
240 };
241 #endif
242 
243 typedef ISC_LIST(dig_searchlist_t) dig_searchlistlist_t;
244 typedef ISC_LIST(dig_lookup_t) dig_lookuplist_t;
245 
246 /*
247  * Externals from dighost.c
248  */
249 
250 extern dig_lookuplist_t lookup_list;
251 extern dig_serverlist_t server_list;
252 extern dig_searchlistlist_t search_list;
253 extern unsigned int extrabytes;
254 
255 extern isc_boolean_t check_ra, have_ipv4, have_ipv6, specified_source,
256 	usesearch, showsearch, qr;
257 extern in_port_t port;
258 extern unsigned int timeout;
259 extern isc_mem_t *mctx;
260 extern int sendcount;
261 extern int ndots;
262 extern int lookup_counter;
263 extern int exitcode;
264 extern isc_sockaddr_t bind_address;
265 extern char keynametext[MXNAME];
266 extern char keyfile[MXNAME];
267 extern char keysecret[MXNAME];
268 extern dns_name_t *hmacname;
269 extern unsigned int digestbits;
270 #ifdef DIG_SIGCHASE
271 extern char trustedkey[MXNAME];
272 #endif
273 extern dns_tsigkey_t *key;
274 extern isc_boolean_t validated;
275 extern isc_taskmgr_t *taskmgr;
276 extern isc_task_t *global_task;
277 extern isc_boolean_t free_now;
278 extern isc_boolean_t debugging, debugtiming, memdebugging;
279 extern isc_boolean_t keep_open;
280 
281 extern char *progname;
282 extern int tries;
283 extern int fatalexit;
284 #ifdef WITH_IDN
285 extern int idnoptions;
286 #endif
287 
288 /*
289  * Routines in dighost.c.
290  */
291 isc_result_t
292 get_address(char *host, in_port_t port, isc_sockaddr_t *sockaddr);
293 
294 int
295 getaddresses(dig_lookup_t *lookup, const char *host, isc_result_t *resultp);
296 
297 isc_result_t
298 get_reverse(char *reverse, size_t len, char *value, isc_boolean_t ip6_int,
299 	    isc_boolean_t strict);
300 
301 ISC_PLATFORM_NORETURN_PRE void
302 fatal(const char *format, ...)
303 ISC_FORMAT_PRINTF(1, 2) ISC_PLATFORM_NORETURN_POST;
304 
305 void
306 debug(const char *format, ...) ISC_FORMAT_PRINTF(1, 2);
307 
308 void
309 check_result(isc_result_t result, const char *msg);
310 
311 isc_boolean_t
312 setup_lookup(dig_lookup_t *lookup);
313 
314 void
315 destroy_lookup(dig_lookup_t *lookup);
316 
317 void
318 do_lookup(dig_lookup_t *lookup);
319 
320 void
321 start_lookup(void);
322 
323 void
324 onrun_callback(isc_task_t *task, isc_event_t *event);
325 
326 int
327 dhmain(int argc, char **argv);
328 
329 void
330 setup_libs(void);
331 
332 void
333 setup_system(void);
334 
335 isc_result_t
336 parse_uint(isc_uint32_t *uip, const char *value, isc_uint32_t max,
337 	   const char *desc);
338 
339 void
340 parse_hmac(const char *hmacstr);
341 
342 dig_lookup_t *
343 requeue_lookup(dig_lookup_t *lookold, isc_boolean_t servers);
344 
345 dig_lookup_t *
346 make_empty_lookup(void);
347 
348 dig_lookup_t *
349 clone_lookup(dig_lookup_t *lookold, isc_boolean_t servers);
350 
351 dig_server_t *
352 make_server(const char *servname, const char *userarg);
353 
354 void
355 flush_server_list(void);
356 
357 void
358 set_nameserver(char *opt);
359 
360 void
361 clone_server_list(dig_serverlist_t src,
362 		  dig_serverlist_t *dest);
363 
364 void
365 cancel_all(void);
366 
367 void
368 destroy_libs(void);
369 
370 void
371 set_search_domain(char *domain);
372 
373 #ifdef DIG_SIGCHASE
374 void
375 clean_trustedkey(void);
376 #endif
377 
378 /*
379  * Routines to be defined in dig.c, host.c, and nslookup.c.
380  */
381 #ifdef DIG_SIGCHASE
382 isc_result_t
383 printrdataset(dns_name_t *owner_name, dns_rdataset_t *rdataset,
384 	      isc_buffer_t *target);
385 #endif
386 
387 isc_result_t
388 printmessage(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers);
389 /*%<
390  * Print the final result of the lookup.
391  */
392 
393 void
394 received(int bytes, isc_sockaddr_t *from, dig_query_t *query);
395 /*%<
396  * Print a message about where and when the response
397  * was received from, like the final comment in the
398  * output of "dig".
399  */
400 
401 void
402 trying(char *frm, dig_lookup_t *lookup);
403 
404 void
405 dighost_shutdown(void);
406 
407 char *
408 next_token(char **stringp, const char *delim);
409 
410 #ifdef DIG_SIGCHASE
411 /* Chasing functions */
412 dns_rdataset_t *
413 chase_scanname(dns_name_t *name, dns_rdatatype_t type, dns_rdatatype_t covers);
414 void
415 chase_sig(dns_message_t *msg);
416 #endif
417 
418 ISC_LANG_ENDDECLS
419 
420 #endif
421