xref: /netbsd-src/external/mpl/bind/dist/bin/dig/dighost.h (revision 2f62cc9c12bc202c40224f32c879f81443fee079)
1 /*	$NetBSD: dighost.h,v 1.2 2024/02/21 22:51:01 christos Exp $	*/
2 
3 /*
4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5  *
6  * SPDX-License-Identifier: MPL-2.0
7  *
8  * This Source Code Form is subject to the terms of the Mozilla Public
9  * License, v. 2.0. If a copy of the MPL was not distributed with this
10  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11  *
12  * See the COPYRIGHT file distributed with this work for additional
13  * information regarding copyright ownership.
14  */
15 
16 #pragma once
17 
18 /*! \file */
19 
20 #include <inttypes.h>
21 #include <stdbool.h>
22 
23 #include <isc/attributes.h>
24 #include <isc/buffer.h>
25 #include <isc/formatcheck.h>
26 #include <isc/lang.h>
27 #include <isc/list.h>
28 #include <isc/magic.h>
29 #include <isc/mem.h>
30 #include <isc/netmgr.h>
31 #include <isc/print.h>
32 #include <isc/refcount.h>
33 #include <isc/sockaddr.h>
34 #include <isc/time.h>
35 
36 #include <dns/rdatalist.h>
37 
38 #include <dst/dst.h>
39 
40 #ifdef __APPLE__
41 #include <TargetConditionals.h>
42 #endif /* ifdef __APPLE__ */
43 
44 #define MXSERV 20
45 #define MXNAME (DNS_NAME_MAXTEXT + 1)
46 #define MXRD   32
47 /*% Buffer Size */
48 #define BUFSIZE	 512
49 #define COMMSIZE 0xffff
50 #ifndef RESOLV_CONF
51 /*% location of resolve.conf */
52 #define RESOLV_CONF "/etc/resolv.conf"
53 #endif /* ifndef RESOLV_CONF */
54 /*% output buffer */
55 #define OUTPUTBUF 32767
56 /*% Max RR Limit */
57 #define MAXRRLIMIT 0xffffffff
58 #define MAXTIMEOUT 0xffff
59 /*% Max number of tries */
60 #define MAXTRIES 0xffffffff
61 /*% Max number of dots */
62 #define MAXNDOTS 0xffff
63 /*% Max number of ports */
64 #define MAXPORT 0xffff
65 /*% Max serial number */
66 #define MAXSERIAL 0xffffffff
67 /*% Max query ID */
68 #define MAXQID 0xffff
69 
70 /*% Default TCP Timeout */
71 #define TCP_TIMEOUT 10
72 /*% Default UDP Timeout */
73 #define UDP_TIMEOUT 5
74 
75 #define SERVER_TIMEOUT 1
76 
77 #define LOOKUP_LIMIT 64
78 
79 #define DEFAULT_EDNS_VERSION 0
80 #define DEFAULT_EDNS_BUFSIZE 1232
81 
82 #define DEFAULT_HTTPS_QUERY "?dns="
83 
84 /*%
85  * Lookup_limit is just a limiter, keeping too many lookups from being
86  * created.  It's job is mainly to prevent the program from running away
87  * in a tight loop of constant lookups.  It's value is arbitrary.
88  */
89 
90 ISC_LANG_BEGINDECLS
91 
92 typedef struct dig_lookup dig_lookup_t;
93 typedef struct dig_query dig_query_t;
94 typedef struct dig_server dig_server_t;
95 typedef ISC_LIST(dig_server_t) dig_serverlist_t;
96 typedef struct dig_searchlist dig_searchlist_t;
97 
98 #define DIG_LOOKUP_MAGIC ISC_MAGIC('D', 'i', 'g', 'l')
99 
100 #define DIG_VALID_LOOKUP(x) ISC_MAGIC_VALID((x), DIG_LOOKUP_MAGIC)
101 
102 #define DIG_QUERY_MAGIC ISC_MAGIC('D', 'i', 'g', 'q')
103 
104 #define DIG_VALID_QUERY(x) ISC_MAGIC_VALID((x), DIG_QUERY_MAGIC)
105 
106 /*% The dig_lookup structure */
107 struct dig_lookup {
108 	unsigned int magic;
109 	isc_refcount_t references;
110 	bool aaonly, adflag, badcookie, besteffort, cdflag, cleared, comments,
111 		dns64prefix, dnssec, doing_xfr, done_as_is, ednsneg, expandaaaa,
112 		expire, fuzzing, header_only, identify, /*%< Append an "on
113 							   server <foo>" message
114 							 */
115 		identify_previous_line, /*% Prepend a "Nameserver <foo>:"
116 					   message, with newline and tab */
117 		idnin, idnout, ignore, multiline, need_search, new_search,
118 		noclass, nocrypto, nottl, ns_search_only, /*%< dig +nssearch,
119 							     host -C */
120 		ns_search_success, nsid, /*% Name Server ID (RFC 5001) */
121 		onesoa, pending,	 /*%< Pending a successful answer */
122 		print_unknown_format, qr, raflag, recurse, section_additional,
123 		section_answer, section_authority, section_question,
124 		seenbadcookie, sendcookie, servfail_stops,
125 		setqid, /*% use a speciied query ID */
126 		showbadcookie, stats, tcflag, tcp_keepalive, tcp_mode,
127 		tcp_mode_set, tls_mode, /*% connect using TLS */
128 		trace,			/*% dig +trace */
129 		trace_root, /*% initial query for either +trace or +nssearch */
130 		ttlunits, use_usec, waiting_connect, zflag;
131 	char textname[MXNAME]; /*% Name we're going to be looking up */
132 	char cmdline[MXNAME];
133 	dns_rdatatype_t rdtype;
134 	dns_rdatatype_t qrdtype;
135 	dns_rdataclass_t rdclass;
136 	bool rdtypeset;
137 	bool rdclassset;
138 	char name_space[BUFSIZE];
139 	char oname_space[BUFSIZE];
140 	isc_buffer_t namebuf;
141 	isc_buffer_t onamebuf;
142 	isc_buffer_t renderbuf;
143 	char *sendspace;
144 	dns_name_t *name;
145 	isc_interval_t interval;
146 	dns_message_t *sendmsg;
147 	dns_name_t *oname;
148 	ISC_LINK(dig_lookup_t) link;
149 	ISC_LIST(dig_query_t) q;
150 	ISC_LIST(dig_query_t) connecting;
151 	dig_query_t *current_query;
152 	dig_serverlist_t my_server_list;
153 	dig_searchlist_t *origin;
154 	dig_query_t *xfr_q;
155 	uint32_t retries;
156 	int nsfound;
157 	int16_t udpsize;
158 	int16_t edns;
159 	int16_t padding;
160 	uint32_t ixfr_serial;
161 	isc_buffer_t rdatabuf;
162 	char rdatastore[MXNAME];
163 	dst_context_t *tsigctx;
164 	isc_buffer_t *querysig;
165 	uint32_t msgcounter;
166 	dns_fixedname_t fdomain;
167 	isc_sockaddr_t *ecs_addr;
168 	char *cookie;
169 	dns_ednsopt_t *ednsopts;
170 	unsigned int ednsoptscnt;
171 	unsigned int ednsflags;
172 	dns_opcode_t opcode;
173 	int rrcomments;
174 	uint16_t qid;
175 	struct {
176 		bool http_plain;
177 		bool https_mode;
178 		bool https_get;
179 		char *https_path;
180 	};
181 	struct {
182 		bool tls_ca_set;
183 		char *tls_ca_file;
184 		bool tls_hostname_set;
185 		char *tls_hostname;
186 		bool tls_cert_file_set;
187 		char *tls_cert_file;
188 		bool tls_key_file_set;
189 		char *tls_key_file;
190 		isc_tlsctx_cache_t *tls_ctx_cache;
191 	};
192 	isc_stdtime_t fuzztime;
193 };
194 
195 /*% The dig_query structure */
196 struct dig_query {
197 	unsigned int magic;
198 	dig_lookup_t *lookup;
199 	bool started;
200 	bool first_soa_rcvd;
201 	bool second_rr_rcvd;
202 	bool first_repeat_rcvd;
203 	bool warn_id;
204 	bool canceled;
205 	uint32_t first_rr_serial;
206 	uint32_t second_rr_serial;
207 	uint32_t msg_count;
208 	uint32_t rr_count;
209 	bool ixfr_axfr;
210 	char *servname;
211 	char *userarg;
212 	isc_buffer_t sendbuf;
213 	char *recvspace, *tmpsendspace, lengthspace[4];
214 	isc_refcount_t references;
215 	isc_nmhandle_t *handle;
216 	isc_nmhandle_t *readhandle;
217 	isc_nmhandle_t *sendhandle;
218 	ISC_LINK(dig_query_t) link;
219 	ISC_LINK(dig_query_t) clink;
220 	isc_sockaddr_t sockaddr;
221 	isc_time_t time_sent;
222 	isc_time_t time_recv;
223 	uint64_t byte_count;
224 	isc_timer_t *timer;
225 };
226 
227 struct dig_server {
228 	char servername[MXNAME];
229 	char userarg[MXNAME];
230 	ISC_LINK(dig_server_t) link;
231 };
232 
233 struct dig_searchlist {
234 	char origin[MXNAME];
235 	ISC_LINK(dig_searchlist_t) link;
236 };
237 
238 typedef ISC_LIST(dig_searchlist_t) dig_searchlistlist_t;
239 typedef ISC_LIST(dig_lookup_t) dig_lookuplist_t;
240 
241 /*
242  * Externals from dighost.c
243  */
244 
245 extern dig_lookuplist_t lookup_list;
246 extern dig_serverlist_t server_list;
247 extern dig_searchlistlist_t search_list;
248 extern unsigned int extrabytes;
249 
250 extern bool check_ra, have_ipv4, have_ipv6, specified_source, usesearch,
251 	showsearch, yaml;
252 extern in_port_t port;
253 extern bool port_set;
254 extern unsigned int timeout;
255 extern isc_mem_t *mctx;
256 extern isc_refcount_t sendcount;
257 extern int ndots;
258 extern int lookup_counter;
259 extern int exitcode;
260 extern isc_sockaddr_t localaddr;
261 extern char keynametext[MXNAME];
262 extern char keyfile[MXNAME];
263 extern char keysecret[MXNAME];
264 extern const dns_name_t *hmacname;
265 extern unsigned int digestbits;
266 extern dns_tsigkey_t *tsigkey;
267 extern bool validated;
268 extern isc_taskmgr_t *taskmgr;
269 extern isc_task_t *global_task;
270 extern bool free_now;
271 extern bool debugging, debugtiming, memdebugging;
272 extern bool keep_open;
273 
274 extern const char *progname;
275 extern int tries;
276 extern int fatalexit;
277 extern bool verbose;
278 
279 /*
280  * Routines in dighost.c.
281  */
282 isc_result_t
283 get_address(char *host, in_port_t myport, isc_sockaddr_t *sockaddr);
284 
285 int
286 getaddresses(dig_lookup_t *lookup, const char *host, isc_result_t *resultp);
287 
288 isc_result_t
289 get_reverse(char *reverse, size_t len, char *value, bool strict);
290 
291 noreturn void
292 fatal(const char *format, ...) ISC_FORMAT_PRINTF(1, 2);
293 
294 void
295 warn(const char *format, ...) ISC_FORMAT_PRINTF(1, 2);
296 
297 noreturn void
298 digexit(void);
299 
300 void
301 cleanup_openssl_refs(void);
302 
303 void
304 debug(const char *format, ...) ISC_FORMAT_PRINTF(1, 2);
305 
306 void
307 check_result(isc_result_t result, const char *msg);
308 
309 bool
310 setup_lookup(dig_lookup_t *lookup);
311 
312 void
313 destroy_lookup(dig_lookup_t *lookup);
314 
315 void
316 do_lookup(dig_lookup_t *lookup);
317 
318 void
319 start_lookup(void);
320 
321 void
322 onrun_callback(isc_task_t *task, isc_event_t *event);
323 
324 int
325 dhmain(int argc, char **argv);
326 
327 void
328 setup_libs(void);
329 
330 void
331 setup_system(bool ipv4only, bool ipv6only);
332 
333 isc_result_t
334 parse_uint(uint32_t *uip, const char *value, uint32_t max, const char *desc);
335 
336 isc_result_t
337 parse_xint(uint32_t *uip, const char *value, uint32_t max, const char *desc);
338 
339 isc_result_t
340 parse_netprefix(isc_sockaddr_t **sap, const char *value);
341 
342 void
343 parse_hmac(const char *hmacstr);
344 
345 dig_lookup_t *
346 requeue_lookup(dig_lookup_t *lookold, bool servers);
347 
348 dig_lookup_t *
349 make_empty_lookup(void);
350 
351 dig_lookup_t *
352 clone_lookup(dig_lookup_t *lookold, bool servers);
353 
354 dig_server_t *
355 make_server(const char *servname, const char *userarg);
356 
357 void
358 flush_server_list(void);
359 
360 void
361 set_nameserver(char *opt);
362 
363 void
364 clone_server_list(dig_serverlist_t src, dig_serverlist_t *dest);
365 
366 void
367 cancel_all(void);
368 
369 void
370 destroy_libs(void);
371 
372 void
373 set_search_domain(char *domain);
374 
375 /*
376  * Routines to be defined in dig.c, host.c, and nslookup.c. and
377  * then assigned to the appropriate function pointer
378  */
379 extern isc_result_t (*dighost_printmessage)(dig_query_t *query,
380 					    const isc_buffer_t *msgbuf,
381 					    dns_message_t *msg, bool headers);
382 
383 /*
384  * Print an error message in the appropriate format.
385  */
386 extern void (*dighost_error)(const char *format, ...);
387 
388 /*
389  * Print a warning message in the appropriate format.
390  */
391 extern void (*dighost_warning)(const char *format, ...);
392 
393 /*
394  * Print a comment in the appropriate format.
395  */
396 extern void (*dighost_comments)(dig_lookup_t *lookup, const char *format, ...);
397 
398 /*%<
399  * Print the final result of the lookup.
400  */
401 
402 extern void (*dighost_received)(unsigned int bytes, isc_sockaddr_t *from,
403 				dig_query_t *query);
404 /*%<
405  * Print a message about where and when the response
406  * was received from, like the final comment in the
407  * output of "dig".
408  */
409 
410 extern void (*dighost_trying)(char *frm, dig_lookup_t *lookup);
411 
412 extern void (*dighost_shutdown)(void);
413 
414 extern void (*dighost_pre_exit_hook)(void);
415 
416 void
417 save_opt(dig_lookup_t *lookup, char *code, char *value);
418 
419 void
420 setup_file_key(void);
421 void
422 setup_text_key(void);
423 
424 /*
425  * Routines exported from dig.c for use by dig for iOS
426  */
427 
428 /*%
429  * Call once only to set up libraries, parse global
430  * parameters and initial command line query parameters
431  */
432 void
433 dig_setup(int argc, char **argv);
434 
435 /*%
436  * Call to supply new parameters for the next lookup
437  */
438 void
439 dig_query_setup(bool, bool, int argc, char **argv);
440 
441 /*%
442  * set the main application event cycle running
443  */
444 void
445 dig_startup(void);
446 
447 /*%
448  * Initiates the next lookup cycle
449  */
450 void
451 dig_query_start(void);
452 
453 /*%
454  * Activate/deactivate IDN filtering of output.
455  */
456 void
457 dig_idnsetup(dig_lookup_t *lookup, bool active);
458 
459 /*%
460  * Cleans up the application
461  */
462 void
463 dig_shutdown(void);
464 
465 bool
466 dig_lookup_is_tls(const dig_lookup_t *lookup);
467 
468 ISC_LANG_ENDDECLS
469