kdig_params.c 40.4 KB
Newer Older
1
/*  Copyright (C) 2016 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
Daniel Salzman's avatar
Daniel Salzman committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

17 18 19 20 21
#include <arpa/inet.h>
#include <locale.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
Daniel Salzman's avatar
Daniel Salzman committed
22

23
#include "utils/kdig/kdig_params.h"
24
#include "utils/common/cert.h"
25 26 27 28
#include "utils/common/msg.h"
#include "utils/common/params.h"
#include "utils/common/resolv.h"
#include "libknot/descriptor.h"
29
#include "libknot/libknot.h"
30
#include "contrib/base64.h"
31
#include "contrib/sockaddr.h"
32
#include "contrib/strtonum.h"
33
#include "contrib/ucw/lists.h"
Daniel Salzman's avatar
Daniel Salzman committed
34

35 36
#define PROGRAM_NAME "kdig"

37
#define DEFAULT_RETRIES_DIG	2
38
#define DEFAULT_TIMEOUT_DIG	5
39
#define DEFAULT_ALIGNMENT_SIZE	128
40

41
static const flags_t DEFAULT_FLAGS_DIG = {
42 43 44 45 46 47 48
	.aa_flag = false,
	.tc_flag = false,
	.rd_flag = true,
	.ra_flag = false,
	.z_flag  = false,
	.ad_flag = false,
	.cd_flag = false,
49
	.do_flag = false
50 51
};

52
static const style_t DEFAULT_STYLE_DIG = {
53
	.format = FORMAT_FULL,
54 55 56 57 58
	.style = {
		.wrap = false,
		.show_class = true,
		.show_ttl = true,
		.verbose = false,
59
		.empty_ttl = false,
60
		.human_ttl = false,
Daniel Salzman's avatar
Daniel Salzman committed
61
		.human_tmstamp = true,
62
		.generic = false,
63
		.ascii_to_idn = name_to_idn
64
	},
65
	.show_query = false,
66
	.show_header = true,
67
	.show_edns = true,
68 69 70 71
	.show_question = true,
	.show_answer = true,
	.show_authority = true,
	.show_additional = true,
72
	.show_tsig = true,
73
	.show_footer = true
74 75
};

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
static int opt_multiline(const char *arg, void *query)
{
	query_t *q = query;

	q->style.style.wrap = true;
	q->style.format = FORMAT_FULL;
	q->style.show_header = true;
	q->style.show_edns = true;
	q->style.show_footer = true;
	q->style.style.verbose = true;

	return KNOT_EOK;
}

static int opt_nomultiline(const char *arg, void *query)
{
	query_t *q = query;

	q->style.style.wrap = false;

	return KNOT_EOK;
}

static int opt_short(const char *arg, void *query)
{
	query_t *q = query;

	q->style.format = FORMAT_DIG;
	q->style.show_header = false;
	q->style.show_edns = false;
	q->style.show_footer = false;

	return KNOT_EOK;
}

static int opt_noshort(const char *arg, void *query)
{
	query_t *q = query;

	q->style.format = FORMAT_FULL;

	return KNOT_EOK;
}

Daniel Salzman's avatar
Daniel Salzman committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
static int opt_generic(const char *arg, void *query)
{
	query_t *q = query;

	q->style.style.generic = true;

	return KNOT_EOK;
}

static int opt_nogeneric(const char *arg, void *query)
{
	query_t *q = query;

	q->style.style.generic = false;

	return KNOT_EOK;
}

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
static int opt_aaflag(const char *arg, void *query)
{
	query_t *q = query;

	q->flags.aa_flag = true;

	return KNOT_EOK;
}

static int opt_noaaflag(const char *arg, void *query)
{
	query_t *q = query;

	q->flags.aa_flag = false;

	return KNOT_EOK;
}

static int opt_tcflag(const char *arg, void *query)
{
	query_t *q = query;

	q->flags.tc_flag = true;

	return KNOT_EOK;
}

static int opt_notcflag(const char *arg, void *query)
{
	query_t *q = query;

	q->flags.tc_flag = false;

	return KNOT_EOK;
}

static int opt_rdflag(const char *arg, void *query)
{
	query_t *q = query;

	q->flags.rd_flag = true;

	return KNOT_EOK;
}

static int opt_nordflag(const char *arg, void *query)
{
	query_t *q = query;

	q->flags.rd_flag = false;

	return KNOT_EOK;
}

static int opt_raflag(const char *arg, void *query)
{
	query_t *q = query;

	q->flags.ra_flag = true;

	return KNOT_EOK;
}

static int opt_noraflag(const char *arg, void *query)
{
	query_t *q = query;

	q->flags.ra_flag = false;

	return KNOT_EOK;
}

static int opt_zflag(const char *arg, void *query)
{
	query_t *q = query;

	q->flags.z_flag = true;

	return KNOT_EOK;
}

static int opt_nozflag(const char *arg, void *query)
{
	query_t *q = query;

	q->flags.z_flag = false;

	return KNOT_EOK;
}

static int opt_adflag(const char *arg, void *query)
{
	query_t *q = query;

	q->flags.ad_flag = true;

	return KNOT_EOK;
}

static int opt_noadflag(const char *arg, void *query)
{
	query_t *q = query;

	q->flags.ad_flag = false;

	return KNOT_EOK;
}

static int opt_cdflag(const char *arg, void *query)
{
	query_t *q = query;

	q->flags.cd_flag = true;

	return KNOT_EOK;
}

static int opt_nocdflag(const char *arg, void *query)
{
	query_t *q = query;

	q->flags.cd_flag = false;

	return KNOT_EOK;
}

static int opt_doflag(const char *arg, void *query)
{
	query_t *q = query;

	q->flags.do_flag = true;

	return KNOT_EOK;
}

static int opt_nodoflag(const char *arg, void *query)
{
	query_t *q = query;

	q->flags.do_flag = false;

	return KNOT_EOK;
}

static int opt_all(const char *arg, void *query)
{
	query_t *q = query;

	q->style.show_header = true;
	q->style.show_edns = true;
	q->style.show_question = true;
	q->style.show_answer = true;
	q->style.show_authority = true;
	q->style.show_additional = true;
292
	q->style.show_tsig = true;
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
	q->style.show_footer = true;

	return KNOT_EOK;
}

static int opt_noall(const char *arg, void *query)
{
	query_t *q = query;

	q->style.show_header = false;
	q->style.show_edns = false;
	q->style.show_query = false;
	q->style.show_question = false;
	q->style.show_answer = false;
	q->style.show_authority = false;
	q->style.show_additional = false;
309
	q->style.show_tsig = false;
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
	q->style.show_footer = false;

	return KNOT_EOK;
}

static int opt_qr(const char *arg, void *query)
{
	query_t *q = query;

	q->style.show_query = true;

	return KNOT_EOK;
}

static int opt_noqr(const char *arg, void *query)
{
	query_t *q = query;

	q->style.show_query = false;

	return KNOT_EOK;
}

static int opt_header(const char *arg, void *query)
{
	query_t *q = query;

	q->style.show_header = true;

	return KNOT_EOK;
}

static int opt_noheader(const char *arg, void *query)
{
	query_t *q = query;

	q->style.show_header = false;

	return KNOT_EOK;
}

static int opt_opt(const char *arg, void *query)
{
	query_t *q = query;

	q->style.show_edns = true;

	return KNOT_EOK;
}

static int opt_noopt(const char *arg, void *query)
{
	query_t *q = query;

	q->style.show_edns = false;

	return KNOT_EOK;
}

static int opt_question(const char *arg, void *query)
{
	query_t *q = query;

	q->style.show_question = true;

	return KNOT_EOK;
}

static int opt_noquestion(const char *arg, void *query)
{
	query_t *q = query;

	q->style.show_question = false;

	return KNOT_EOK;
}

static int opt_answer(const char *arg, void *query)
{
	query_t *q = query;

	q->style.show_answer = true;

	return KNOT_EOK;
}

static int opt_noanswer(const char *arg, void *query)
{
	query_t *q = query;

	q->style.show_answer = false;

	return KNOT_EOK;
}

static int opt_authority(const char *arg, void *query)
{
	query_t *q = query;

	q->style.show_authority = true;

	return KNOT_EOK;
}

static int opt_noauthority(const char *arg, void *query)
{
	query_t *q = query;

	q->style.show_authority = false;

	return KNOT_EOK;
}

static int opt_additional(const char *arg, void *query)
{
	query_t *q = query;

	q->style.show_additional = true;

	return KNOT_EOK;
}

static int opt_noadditional(const char *arg, void *query)
{
	query_t *q = query;

	q->style.show_additional = false;
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
	q->style.show_edns = false;
	q->style.show_tsig = false;

	return KNOT_EOK;
}

static int opt_tsig(const char *arg, void *query)
{
	query_t *q = query;

	q->style.show_tsig = true;

	return KNOT_EOK;
}

static int opt_notsig(const char *arg, void *query)
{
	query_t *q = query;

	q->style.show_tsig = false;
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514

	return KNOT_EOK;
}

static int opt_stats(const char *arg, void *query)
{
	query_t *q = query;

	q->style.show_footer = true;

	return KNOT_EOK;
}

static int opt_nostats(const char *arg, void *query)
{
	query_t *q = query;

	q->style.show_footer = false;

	return KNOT_EOK;
}

static int opt_class(const char *arg, void *query)
{
	query_t *q = query;

	q->style.style.show_class = true;

	return KNOT_EOK;
}

static int opt_noclass(const char *arg, void *query)
{
	query_t *q = query;

	q->style.style.show_class = false;

	return KNOT_EOK;
}

static int opt_ttl(const char *arg, void *query)
{
	query_t *q = query;

	q->style.style.show_ttl = true;

	return KNOT_EOK;
}

static int opt_nottl(const char *arg, void *query)
{
	query_t *q = query;

	q->style.style.show_ttl = false;

	return KNOT_EOK;
}

515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
static int opt_ignore(const char *arg, void *query)
{
	query_t *q = query;

	q->ignore_tc = true;

	return KNOT_EOK;
}

static int opt_noignore(const char *arg, void *query)
{
	query_t *q = query;

	q->ignore_tc = false;

	return KNOT_EOK;
}

533 534 535 536 537 538 539 540 541 542 543 544 545 546
static int opt_tcp(const char *arg, void *query)
{
	query_t *q = query;

	q->protocol = PROTO_TCP;

	return KNOT_EOK;
}

static int opt_notcp(const char *arg, void *query)
{
	query_t *q = query;

	q->protocol = PROTO_UDP;
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
	return opt_ignore(arg, query);
}

static int opt_tls(const char *arg, void *query)
{
	query_t *q = query;

	q->tls.enable = true;
	return opt_tcp(arg, query);
}

static int opt_notls(const char *arg, void *query)
{
	query_t *q = query;

	tls_params_clean(&q->tls);
	tls_params_init(&q->tls);
564 565 566 567

	return KNOT_EOK;
}

568
static int opt_tls_ca(const char *arg, void *query)
Ondřej Surý's avatar
Ondřej Surý committed
569 570 571 572
{
	query_t *q = query;

	if (arg == NULL) {
573 574
		q->tls.system_ca = true;
		return opt_tls(arg, query);
Ondřej Surý's avatar
Ondřej Surý committed
575
	} else {
576 577 578 579
		if (ptrlist_add(&q->tls.ca_files, strdup(arg), NULL) == NULL) {
			return KNOT_ENOMEM;
		}
		return opt_tls(arg, query);
Ondřej Surý's avatar
Ondřej Surý committed
580
	}
581
}
Ondřej Surý's avatar
Ondřej Surý committed
582

583 584 585 586 587 588 589 590 591
static int opt_notls_ca(const char *arg, void *query)
{
	query_t *q = query;

	q->tls.system_ca = false;

	ptrnode_t *node = NULL, *nxt = NULL;
	WALK_LIST_DELSAFE(node, nxt, q->tls.ca_files) {
		free(node->d);
Ondřej Surý's avatar
Ondřej Surý committed
592
	}
593 594 595
	ptrlist_free(&q->tls.ca_files, NULL);

	return KNOT_EOK;
Ondřej Surý's avatar
Ondřej Surý committed
596 597
}

598
static int opt_tls_pin(const char *arg, void *query)
Ondřej Surý's avatar
Ondřej Surý committed
599 600 601
{
	query_t *q = query;

602
	uint8_t pin[64] = { 0 };
Ondřej Surý's avatar
Ondřej Surý committed
603

604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
	int ret = base64_decode((const uint8_t *)arg, strlen(arg), pin, sizeof(pin));
	if (ret < 0) {
		ERR("invalid +tls-pin=%s\n", arg);
		return ret;
	} else if (ret != CERT_PIN_LEN) { // Check for 256-bit value.
		ERR("invalid sha256 hash length +tls-pin=%s\n", arg);
		return KNOT_EINVAL;
	}

	uint8_t *item = malloc(1 + ret); // 1 ~ leading data length.
	if (item == NULL) {
		return KNOT_ENOMEM;
	}
	item[0] = ret;
	memcpy(&item[1], pin, ret);

	if (ptrlist_add(&q->tls.pins, item, NULL) == NULL) {
		return KNOT_ENOMEM;
	}

	return opt_tls(arg, query);
Ondřej Surý's avatar
Ondřej Surý committed
625 626
}

627
static int opt_notls_pin(const char *arg, void *query)
628 629 630
{
	query_t *q = query;

631 632 633 634 635
	ptrnode_t *node = NULL, *nxt = NULL;
	WALK_LIST_DELSAFE(node, nxt, q->tls.pins) {
		free(node->d);
	}
	ptrlist_free(&q->tls.pins, NULL);
636 637 638 639

	return KNOT_EOK;
}

640
static int opt_tls_hostname(const char *arg, void *query)
641 642 643
{
	query_t *q = query;

644 645 646 647 648 649 650 651 652 653 654 655
	free(q->tls.hostname);
	q->tls.hostname = strdup(arg);

	return opt_tls(arg, query);
}

static int opt_notls_hostname(const char *arg, void *query)
{
	query_t *q = query;

	free(q->tls.hostname);
	q->tls.hostname = NULL;
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677

	return KNOT_EOK;
}

static int opt_nsid(const char *arg, void *query)
{
	query_t *q = query;

	q->nsid = true;

	return KNOT_EOK;
}

static int opt_nonsid(const char *arg, void *query)
{
	query_t *q = query;

	q->nsid = false;

	return KNOT_EOK;
}

Daniel Salzman's avatar
Daniel Salzman committed
678
static int opt_bufsize(const char *arg, void *query)
Daniel Salzman's avatar
Daniel Salzman committed
679 680 681
{
	query_t *q = query;

682
	uint16_t num = 0;
Daniel Salzman's avatar
Daniel Salzman committed
683 684 685 686
	if (str_to_u16(arg, &num) != KNOT_EOK) {
		ERR("invalid +bufsize=%s\n", arg);
		return KNOT_EINVAL;
	}
Daniel Salzman's avatar
Daniel Salzman committed
687

Daniel Salzman's avatar
Daniel Salzman committed
688 689 690 691 692 693 694
	// Disable EDNS if zero bufsize.
	if (num == 0) {
		q->udp_size = -1;
	} else if (num < KNOT_WIRE_HEADER_SIZE) {
		q->udp_size = KNOT_WIRE_HEADER_SIZE;
	} else {
		q->udp_size = num;
Daniel Salzman's avatar
Daniel Salzman committed
695
	}
Daniel Salzman's avatar
Daniel Salzman committed
696 697

	return KNOT_EOK;
Daniel Salzman's avatar
Daniel Salzman committed
698 699
}

Daniel Salzman's avatar
Daniel Salzman committed
700
static int opt_nobufsize(const char *arg, void *query)
Daniel Salzman's avatar
Daniel Salzman committed
701 702 703 704 705 706 707 708
{
	query_t *q = query;

	q->udp_size = -1;

	return KNOT_EOK;
}

709 710 711 712
static int opt_padding(const char *arg, void *query)
{
	query_t *q = query;

713
	uint16_t num = 0;
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
	if (str_to_u16(arg, &num) != KNOT_EOK) {
		ERR("invalid +padding=%s\n", arg);
		return KNOT_EINVAL;
	}

	q->padding = num;

	return KNOT_EOK;
}

static int opt_nopadding(const char *arg, void *query)
{
	query_t *q = query;

	q->padding = -1;

	return KNOT_EOK;
}

static int opt_alignment(const char *arg, void *query)
{
	query_t *q = query;

	if (arg == NULL) {
		q->alignment = DEFAULT_ALIGNMENT_SIZE;
		return KNOT_EOK;
	} else {
741
		uint16_t num = 0;
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
		if (str_to_u16(arg, &num) != KNOT_EOK || num < 2) {
			ERR("invalid +alignment=%s\n", arg);
			return KNOT_EINVAL;
		}

		q->alignment = num;
		return KNOT_EOK;
	}
}

static int opt_noalignment(const char *arg, void *query)
{
	query_t *q = query;

	q->alignment = 0;

	return KNOT_EOK;
}

761
static int opt_subnet(const char *arg, void *query)
762 763 764
{
	query_t *q = query;

765 766 767 768
	char         *sep = NULL;
	const size_t arg_len = strlen(arg);
	const char   *arg_end = arg + arg_len;
	size_t       addr_len = 0;
769

770
	knot_edns_client_subnet_t *subnet = calloc(1, sizeof(*subnet));
771 772 773
	if (subnet == NULL) {
		return KNOT_ENOMEM;
	}
774 775 776 777 778 779 780 781 782

	// Separate address and network mask.
	if ((sep = index(arg, '/')) != NULL) {
		addr_len = sep - arg;
	} else {
		addr_len = arg_len;
	}

	// Check IP address.
783 784 785 786 787 788 789 790 791

	struct sockaddr_storage ss = { 0 };
	struct addrinfo hints = { .ai_flags = AI_NUMERICHOST };
	struct addrinfo *ai = NULL;

	char *addr_str = strndup(arg, addr_len);
	if (getaddrinfo(addr_str, NULL, &hints, &ai) != 0) {
		free(addr_str);
		free(subnet);
792
		ERR("invalid address +subnet=%s\n", arg);
793 794 795 796 797 798 799 800
		return KNOT_EINVAL;
	}

	memcpy(&ss, ai->ai_addr, ai->ai_addrlen);
	freeaddrinfo(ai);
	free(addr_str);

	if (knot_edns_client_subnet_set_addr(subnet, &ss) != KNOT_EOK) {
801
		free(subnet);
802
		ERR("invalid address +subnet=%s\n", arg);
803 804 805 806
		return KNOT_EINVAL;
	}

	// Parse network mask.
807 808 809
	const char *mask = arg;
	if (mask + addr_len < arg_end) {
		mask += addr_len + 1;
810 811
		uint8_t num = 0;
		if (str_to_u8(mask, &num) != KNOT_EOK || num > subnet->source_len) {
812
			free(subnet);
813
			ERR("invalid network mask +subnet=%s\n", arg);
814 815
			return KNOT_EINVAL;
		}
816
		subnet->source_len = num;
817 818 819 820 821 822 823 824
	}

	free(q->subnet);
	q->subnet = subnet;

	return KNOT_EOK;
}

825
static int opt_nosubnet(const char *arg, void *query)
Daniel Salzman's avatar
Daniel Salzman committed
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
{
	query_t *q = query;

	free(q->subnet);
	q->subnet = NULL;

	return KNOT_EOK;
}

static int opt_edns(const char *arg, void *query)
{
	query_t *q = query;

	if (arg == NULL) {
		q->edns = 0;
		return KNOT_EOK;
	} else {
843
		uint8_t num = 0;
Daniel Salzman's avatar
Daniel Salzman committed
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
		if (str_to_u8(arg, &num) != KNOT_EOK) {
			ERR("invalid +edns=%s\n", arg);
			return KNOT_EINVAL;
		}

		q->edns = num;
		return KNOT_EOK;
	}
}

static int opt_noedns(const char *arg, void *query)
{
	query_t *q = query;

	q->edns = -1;
	opt_nodoflag(arg, query);
	opt_nonsid(arg, query);
	opt_nobufsize(arg, query);
862 863
	opt_nopadding(arg, query);
	opt_noalignment(arg, query);
864
	opt_nosubnet(arg, query);
Daniel Salzman's avatar
Daniel Salzman committed
865 866 867 868

	return KNOT_EOK;
}

869 870 871 872
static int opt_time(const char *arg, void *query)
{
	query_t *q = query;

873 874 875 876 877 878
	if (params_parse_wait(arg, &q->wait) != KNOT_EOK) {
		ERR("invalid +time=%s\n", arg);
		return KNOT_EINVAL;
	}

	return KNOT_EOK;
879 880
}

Daniel Salzman's avatar
Daniel Salzman committed
881 882 883 884 885 886 887 888 889
static int opt_notime(const char *arg, void *query)
{
	query_t *q = query;

	q->wait = DEFAULT_TIMEOUT_DIG;

	return KNOT_EOK;
}

890 891 892 893
static int opt_retry(const char *arg, void *query)
{
	query_t *q = query;

894 895 896 897 898 899
	if (str_to_u32(arg, &q->retries) != KNOT_EOK) {
		ERR("invalid +retry=%s\n", arg);
		return KNOT_EINVAL;
	}

	return KNOT_EOK;
900 901
}

Daniel Salzman's avatar
Daniel Salzman committed
902
static int opt_noretry(const char *arg, void *query)
903 904 905
{
	query_t *q = query;

Daniel Salzman's avatar
Daniel Salzman committed
906
	q->retries = DEFAULT_RETRIES_DIG;
Daniel Salzman's avatar
Daniel Salzman committed
907

Daniel Salzman's avatar
Daniel Salzman committed
908 909
	return KNOT_EOK;
}
Daniel Salzman's avatar
Daniel Salzman committed
910

Daniel Salzman's avatar
Daniel Salzman committed
911 912 913 914 915 916
static int opt_noidn(const char *arg, void *query)
{
	query_t *q = query;

	q->idn = false;
	q->style.style.ascii_to_idn = NULL;
Daniel Salzman's avatar
Daniel Salzman committed
917 918

	return KNOT_EOK;
919 920
}

921
static const param_t kdig_opts2[] = {
922 923 924 925 926 927 928 929 930 931 932
	{ "multiline",      ARG_NONE,     opt_multiline },
	{ "nomultiline",    ARG_NONE,     opt_nomultiline },

	{ "short",          ARG_NONE,     opt_short },
	{ "noshort",        ARG_NONE,     opt_noshort },

	{ "generic",        ARG_NONE,     opt_generic },
	{ "nogeneric",      ARG_NONE,     opt_nogeneric },

	{ "aaflag",         ARG_NONE,     opt_aaflag },
	{ "noaaflag",       ARG_NONE,     opt_noaaflag },
933

934 935
	{ "tcflag",         ARG_NONE,     opt_tcflag },
	{ "notcflag",       ARG_NONE,     opt_notcflag },
936

937 938
	{ "rdflag",         ARG_NONE,     opt_rdflag },
	{ "nordflag",       ARG_NONE,     opt_nordflag },
Daniel Salzman's avatar
Daniel Salzman committed
939

940 941
	{ "recurse",        ARG_NONE,     opt_rdflag },
	{ "norecurse",      ARG_NONE,     opt_nordflag },
942

943 944
	{ "raflag",         ARG_NONE,     opt_raflag },
	{ "noraflag",       ARG_NONE,     opt_noraflag },
945

946 947
	{ "zflag",          ARG_NONE,     opt_zflag },
	{ "nozflag",        ARG_NONE,     opt_nozflag },
948

949 950
	{ "adflag",         ARG_NONE,     opt_adflag },
	{ "noadflag",       ARG_NONE,     opt_noadflag },
951

952 953
	{ "cdflag",         ARG_NONE,     opt_cdflag },
	{ "nocdflag",       ARG_NONE,     opt_nocdflag },
954

955 956
	{ "dnssec",         ARG_NONE,     opt_doflag },
	{ "nodnssec",       ARG_NONE,     opt_nodoflag },
957

958 959
	{ "all",            ARG_NONE,     opt_all },
	{ "noall",          ARG_NONE,     opt_noall },
960

961 962
	{ "qr",             ARG_NONE,     opt_qr },
	{ "noqr",           ARG_NONE,     opt_noqr },
963

964 965
	{ "header",         ARG_NONE,     opt_header },
	{ "noheader",       ARG_NONE,     opt_noheader },
966

967 968
	{ "opt",            ARG_NONE,     opt_opt },
	{ "noopt",          ARG_NONE,     opt_noopt },
969

970 971
	{ "question",       ARG_NONE,     opt_question },
	{ "noquestion",     ARG_NONE,     opt_noquestion },
972

973 974
	{ "answer",         ARG_NONE,     opt_answer },
	{ "noanswer",       ARG_NONE,     opt_noanswer },
975

976 977
	{ "authority",      ARG_NONE,     opt_authority },
	{ "noauthority",    ARG_NONE,     opt_noauthority },
978

979 980
	{ "additional",     ARG_NONE,     opt_additional },
	{ "noadditional",   ARG_NONE,     opt_noadditional },
981

982 983
	{ "tsig",           ARG_NONE,     opt_tsig },
	{ "notsig",         ARG_NONE,     opt_notsig },
984

985 986
	{ "stats",          ARG_NONE,     opt_stats },
	{ "nostats",        ARG_NONE,     opt_nostats },
987

988 989
	{ "class",          ARG_NONE,     opt_class },
	{ "noclass",        ARG_NONE,     opt_noclass },
990

991 992
	{ "ttl",            ARG_NONE,     opt_ttl },
	{ "nottl",          ARG_NONE,     opt_nottl },
993

994 995
	{ "tcp",            ARG_NONE,     opt_tcp },
	{ "notcp",          ARG_NONE,     opt_notcp },
996

997 998
	{ "ignore",         ARG_NONE,     opt_ignore },
	{ "noignore",       ARG_NONE,     opt_noignore },
999

1000 1001
	{ "tls",            ARG_NONE,     opt_tls },
	{ "notls",          ARG_NONE,     opt_notls },
1002

1003 1004
	{ "tls-ca",         ARG_OPTIONAL, opt_tls_ca },
	{ "notls-ca",       ARG_NONE,     opt_notls_ca },
1005

1006 1007
	{ "tls-pin",        ARG_REQUIRED, opt_tls_pin },
	{ "notls-pin",      ARG_NONE,     opt_notls_pin },
1008

1009 1010
	{ "tls-hostname",   ARG_REQUIRED, opt_tls_hostname },
	{ "notls-hostname", ARG_NONE,     opt_notls_hostname },
1011

1012 1013
	{ "nsid",           ARG_NONE,     opt_nsid },
	{ "nonsid",         ARG_NONE,     opt_nonsid },
1014

1015 1016
	{ "bufsize",        ARG_REQUIRED, opt_bufsize },
	{ "nobufsize",      ARG_NONE,     opt_nobufsize },
1017

1018 1019
	{ "padding",        ARG_REQUIRED, opt_padding },
	{ "nopadding",      ARG_NONE,     opt_nopadding },
1020

1021 1022
	{ "alignment",      ARG_OPTIONAL, opt_alignment },
	{ "noalignment",    ARG_NONE,     opt_noalignment },
Daniel Salzman's avatar
Daniel Salzman committed
1023

1024 1025 1026 1027 1028 1029
	{ "subnet",         ARG_REQUIRED, opt_subnet },
	{ "nosubnet",       ARG_NONE,     opt_nosubnet },

	// Obsolete aliases.
	{ "client",         ARG_REQUIRED, opt_subnet },
	{ "noclient",       ARG_NONE,     opt_nosubnet },
1030

1031 1032
	{ "edns",           ARG_OPTIONAL, opt_edns },
	{ "noedns",         ARG_NONE,     opt_noedns },
1033

1034 1035 1036 1037 1038
	{ "time",           ARG_REQUIRED, opt_time },
	{ "notime",         ARG_NONE,     opt_notime },

	{ "retry",          ARG_REQUIRED, opt_retry },
	{ "noretry",        ARG_NONE,     opt_noretry },
1039

Daniel Salzman's avatar
Daniel Salzman committed
1040
	/* "idn" doesn't work since it must be called before query creation. */
1041
	{ "noidn",          ARG_NONE,     opt_noidn },
1042 1043 1044 1045

	{ NULL }
};

1046
query_t *query_create(const char *owner, const query_t *conf)
1047 1048 1049 1050 1051
{
	// Create output structure.
	query_t *query = calloc(1, sizeof(query_t));

	if (query == NULL) {
1052
		DBG_NULL;
1053 1054 1055
		return NULL;
	}

1056
	// Set the query owner if any.
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
	if (owner != NULL) {
		if ((query->owner = strdup(owner)) == NULL) {
			query_free(query);
			return NULL;
		}
	}

	// Initialize list of servers.
	init_list(&query->servers);

	// Initialization with defaults or with reference query.
	if (conf == NULL) {
1069
		query->conf = NULL;
1070
		query->local = NULL;
1071 1072 1073
		query->operation = OPERATION_QUERY;
		query->ip = IP_ALL;
		query->protocol = PROTO_ALL;
1074
		query->port = strdup("");
1075
		query->udp_size = -1;
1076 1077
		query->retries = DEFAULT_RETRIES_DIG;
		query->wait = DEFAULT_TIMEOUT_DIG;
1078
		query->ignore_tc = false;
1079 1080
		query->class_num = -1;
		query->type_num = -1;
1081
		query->serial = -1;
1082
		query->notify = false;
1083 1084
		query->flags = DEFAULT_FLAGS_DIG;
		query->style = DEFAULT_STYLE_DIG;
Daniel Salzman's avatar
Daniel Salzman committed
1085
		query->idn = true;
1086
		query->nsid = false;
1087
		query->edns = -1;
1088 1089
		query->padding = -1;
		query->alignment = 0;
1090
		tls_params_init(&query->tls);
1091
		//query->tsig_key
1092
		query->subnet = NULL;
1093
#if USE_DNSTAP
1094 1095
		query->dt_reader = NULL;
		query->dt_writer = NULL;
1096
#endif // USE_DNSTAP
1097
	} else {
1098
		query->conf = conf;
1099
		if (conf->local != NULL) {
1100 1101
			query->local = srv_info_create(conf->local->name,
			                               conf->local->service);
1102 1103 1104 1105 1106 1107 1108
			if (query->local == NULL) {
				query_free(query);
				return NULL;
			}
		} else {
			query->local = NULL;
		}
1109 1110 1111 1112 1113 1114 1115
		query->operation = conf->operation;
		query->ip = conf->ip;
		query->protocol = conf->protocol;
		query->port = strdup(conf->port);
		query->udp_size = conf->udp_size;
		query->retries = conf->retries;
		query->wait = conf->wait;
1116
		query->ignore_tc = conf->ignore_tc;
1117 1118
		query->class_num = conf->class_num;
		query->type_num = conf->type_num;
1119
		query->serial = conf->serial;
1120
		query->notify = conf->notify;
1121 1122
		query->flags = conf->flags;
		query->style = conf->style;
Daniel Salzman's avatar
Daniel Salzman committed
1123
		query->idn = conf->idn;
1124
		query->nsid = conf->nsid;
1125
		query->edns = conf->edns;
1126 1127
		query->padding = conf->padding;
		query->alignment = conf->alignment;
1128
		tls_params_copy(&query->tls, &conf->tls);
1129 1130 1131 1132 1133 1134 1135 1136
		if (conf->tsig_key.name != NULL) {
			int ret = knot_tsig_key_copy(&query->tsig_key,
			                             &conf->tsig_key);
			if (ret != KNOT_EOK) {
				query_free(query);
				return NULL;
			}
		}
1137
		if (conf->subnet != NULL) {
1138
			query->subnet = malloc(sizeof(*query->subnet));
1139 1140 1141 1142 1143 1144 1145 1146
			if (query->subnet == NULL) {
				query_free(query);
				return NULL;
			}
			*(query->subnet) = *(conf->subnet);
		} else {
			query->subnet = NULL;
		}
1147
#if USE_DNSTAP
1148 1149
		query->dt_reader = conf->dt_reader;
		query->dt_writer = conf->dt_writer;
1150
#endif // USE_DNSTAP
1151 1152
	}

1153
	// Check dynamic allocation.
1154 1155 1156 1157
	if (query->port == NULL) {
		query_free(query);
		return NULL;
	}
1158 1159 1160 1161

	return query;
}

1162
void query_free(query_t *query)
1163
{
1164
	node_t *n = NULL, *nxt = NULL;
1165

1166
	if (query == NULL) {
1167
		DBG_NULL;
1168 1169 1170
		return;
	}

1171
	// Cleanup servers.
1172
	WALK_LIST_DELSAFE(n, nxt, query->servers) {
1173
		srv_info_free((srv_info_t *)n);
1174 1175
	}

1176
	// Cleanup local address.
1177
	if (query->local != NULL) {
1178
		srv_info_free(query->local);
1179
	}
1180

1181 1182
	tls_params_clean(&query->tls);

1183
	// Cleanup signing key.
1184
	knot_tsig_key_deinit(&query->tsig_key);
1185

1186
#if USE_DNSTAP
1187
	if (query->dt_reader != NULL) {
1188 1189
		dt_reader_free(query->dt_reader);
	}
1190 1191 1192 1193 1194 1195
	if (query->dt_writer != NULL) {
		// Global writer can be shared!
		if (query->conf == NULL ||
		    query->conf->dt_writer != query->dt_writer) {
			dt_writer_free(query->dt_writer);
		}
1196
	}
1197
#endif // USE_DNSTAP
1198

1199 1200
	free(query->owner);
	free(query->port);
1201
	free(query->subnet);
1202
	free(query);
1203 1204
}

1205
int kdig_init(kdig_params_t *params)
Daniel Salzman's avatar
Daniel Salzman committed
1206
{
1207 1208 1209 1210 1211
	if (params == NULL) {
		DBG_NULL;
		return KNOT_EINVAL;
	}

Daniel Salzman's avatar
Daniel Salzman committed
1212 1213
	memset(params, 0, sizeof(*params));

1214 1215
	params->stop = false;

1216
	// Initialize list of queries.
1217
	init_list(&params->queries);
1218

1219 1220 1221 1222
	// Create config query.
	if ((params->config = query_create(NULL, NULL)) == NULL) {
		return KNOT_ENOMEM;
	}
1223

1224
	return KNOT_EOK;
Daniel Salzman's avatar
Daniel Salzman committed
1225 1226
}

1227
void kdig_clean(kdig_params_t *params)
Daniel Salzman's avatar
Daniel Salzman committed
1228
{
1229
	node_t *n = NULL, *nxt = NULL;
Daniel Salzman's avatar
Daniel Salzman committed
1230 1231

	if (params == NULL) {
1232
		DBG_NULL;
Daniel Salzman's avatar
Daniel Salzman committed
1233 1234 1235
		return;
	}

1236 1237
	// Clean up queries.
	WALK_LIST_DELSAFE(n, nxt, params->queries) {
Daniel Salzman's avatar
Daniel Salzman committed
1238 1239 1240
		query_free((query_t *)n);
	}

1241
	// Clean up config.
1242
	query_free(params->config);
1243

Daniel Salzman's avatar
Daniel Salzman committed
1244 1245 1246 1247
	// Clean up the structure.
	memset(params, 0, sizeof(*params));
}

1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
static int parse_class(const char *value, query_t *query)
{
	uint16_t rclass;

	if (params_parse_class(value, &rclass) != KNOT_EOK) {
		return KNOT_EINVAL;
	}

	query->class_num = rclass;

	return KNOT_EOK;
}

static int parse_keyfile(const char *value, query_t *query)
{
1263 1264 1265 1266 1267 1268 1269
	knot_tsig_key_deinit(&query->tsig_key);

	if (knot_tsig_key_init_file(&query->tsig_key, value) != KNOT_EOK) {
		return KNOT_EINVAL;
	}

	return KNOT_EOK;
1270 1271
}

1272 1273
static int parse_local(const char *value, query_t *query)
{
1274
	srv_info_t *local = parse_nameserver(value, "0");
1275 1276 1277 1278 1279
	if (local == NULL) {
		return KNOT_EINVAL;
	}

	if (query->local != NULL) {
1280
		srv_info_free(query->local);
1281 1282 1283 1284 1285 1286 1287
	}

	query->local = local;

	return KNOT_EOK;
}

1288
static int parse_name(const char *value, list_t *queries, const query_t *conf)
Daniel Salzman's avatar
Daniel Salzman committed
1289
{
Daniel Salzman's avatar
Daniel Salzman committed
1290 1291 1292 1293 1294 1295 1296 1297 1298
	query_t	*query = NULL;
	char	*ascii_name = (char *)value;

	if (conf->idn) {
		ascii_name = name_from_idn(value);
		if (ascii_name == NULL) {
			return KNOT_EINVAL;
		}
	}
1299

Daniel Salzman's avatar
Daniel Salzman committed
1300
	// If name is not FQDN, append trailing dot.
Daniel Salzman's avatar
Daniel Salzman committed
1301 1302 1303 1304 1305
	char *fqd_name = get_fqd_name(ascii_name);

	if (conf->idn) {
		free(ascii_name);
	}
Daniel Salzman's avatar
Daniel Salzman committed
1306

1307
	// Create new query.
1308
	query = query_create(fqd_name, conf);
Daniel Salzman's avatar
Daniel Salzman committed
1309 1310 1311

	free(fqd_name);

1312 1313 1314 1315
	if (query == NULL) {
		return KNOT_ENOMEM;
	}

1316
	// Add new query to the queries.
1317
	add_tail(queries, (node_t *)query);
1318

Daniel Salzman's avatar
Daniel Salzman committed
1319 1320 1321
	return KNOT_EOK;
}

1322 1323 1324 1325 1326 1327
static int parse_port(const char *value, query_t *query)
{
	char **port;

	// Set current server port (last or query default).
	if (list_size(&query->servers) > 0) {
1328
		srv_info_t *server = TAIL(query->servers);
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
		port = &(server->service);
	} else {
		port = &(query->port);
	}

	char *new_port = strdup(value);

	if (new_port == NULL) {
		return KNOT_ENOMEM;
	}

	// Deallocate old string.
	free(*port);

	*port = new_port;

	return KNOT_EOK;
}

1348
static int parse_reverse(const char *value, list_t *queries, const query_t *conf)
1349
{
1350 1351 1352 1353
	query_t *query = NULL;

	// Create reverse name.
	char *reverse = get_reverse_name(value);
1354 1355 1356 1357

	if (reverse == NULL) {
		return KNOT_EINVAL;
	}
1358

1359
	// Create reverse query for given address.
1360
	query = query_create(reverse, conf);
1361 1362 1363

	free(reverse);

1364 1365 1366 1367
	if (query == NULL) {
		return KNOT_ENOMEM;
	}

1368 1369
	// Set type for reverse query.
	query->type_num = KNOT_RRTYPE_PTR;
1370

1371
	// Add new query to the queries.
1372
	add_tail(queries, (node_t *)query);
1373

1374 1375 1376
	return KNOT_EOK;
}

1377
static int parse_server(const char *value, kdig_params_t *params)
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
{
	query_t *query;

	// Set current query (last or config).
	if (list_size(&params->queries) > 0) {
		query = TAIL(params->queries);
	} else {
		query = params->config;
	}

1388 1389 1390 1391 1392 1393
	if (params_parse_server(value, &query->servers, query->port) != KNOT_EOK) {
		ERR("invalid server @%s\n", value);
		return KNOT_EINVAL;
	}

	return KNOT_EOK;
1394 1395 1396 1397
}

static int parse_tsig(const char *value, query_t *query)
{
1398
	knot_tsig_key_deinit(&query->tsig_key);
1399

1400
	if (knot_tsig_key_init_str(&query->tsig_key, value) != KNOT_EOK) {
1401 1402 1403 1404 1405 1406 1407 1408 1409
		return KNOT_EINVAL;
	}

	return KNOT_EOK;
}

static int parse_type(const char *value, query_t *query)
{
	uint16_t rtype;
1410
	int64_t  serial;
1411
	bool     notify;
1412

1413
	if (params_parse_type(value, &rtype, &serial, &notify) != KNOT_EOK) {
1414 1415 1416 1417
		return KNOT_EINVAL;
	}

	query->type_num = rtype;
1418
	query->serial = serial;
1419 1420 1421 1422 1423 1424
	query->notify = notify;

	// If NOTIFY, reset default RD flag.
	if (query->notify) {
		query->flags.rd_flag = false;
	}
1425 1426 1427 1428

	return KNOT_EOK;
}

1429
#if USE_DNSTAP
1430
static int parse_dnstap_output(const char *value, query_t *query)
1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446
{
	if (query->dt_writer != NULL) {
		if (query->conf == NULL ||
		    query->conf->dt_writer != query->dt_writer) {
			dt_writer_free(query->dt_writer);
		}
	}

	query->dt_writer = dt_writer_create(value, "kdig " PACKAGE_VERSION);
	if (query->dt_writer == NULL) {
		return KNOT_EINVAL;
	}

	return KNOT_EOK;
}

1447
static int parse_dnstap_input(const char *value, query_t *query)
1448
{
1449
	// Just in case, shouldn't happen.
1450
	if (query->dt_reader != NULL) {
1451
		dt_reader_free(query->dt_reader);
1452 1453 1454 1455 1456 1457
	}

	query->dt_reader = dt_reader_create(value);
	if (query->dt_reader == NULL) {
		return KNOT_EINVAL;
	}
1458 1459 1460

	return KNOT_EOK;
}
1461
#endif // USE_DNSTAP
1462

1463
static void complete_servers(query_t *query, const query_t *conf)
1464
{
1465
	node_t *n = NULL;
1466 1467 1468 1469 1470 1471 1472
	char *def_port;

	// Decide which default port use.
	if (strlen(query->port) > 0) {
		def_port = query->port;
	} else if (strlen(conf->port) > 0) {
		def_port = conf->port;
1473
	} else if (query->tls.enable) {
Ondřej Surý's avatar
Ondřej Surý committed
1474
		def_port = DEFAULT_DNS_TLS_PORT;
1475 1476 1477
	} else {
		def_port = DEFAULT_DNS_PORT;
	}
1478

1479 1480 1481
	// Complete specified nameservers if any.
	if (list_size(&query->servers) > 0) {
		WALK_LIST(n, query->servers) {
1482
			srv_info_t *s = (srv_info_t *)n;
1483

1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495
			// If the port isn't specified yet use the default one.
			if (strlen(s->service) == 0) {
				free(s->service);
				s->service = strdup(def_port);
				if (s->service == NULL) {
					WARN("can't set port %s\n", def_port);
					return;
				}
			}
		}
	// Use servers from config if any.
	} else if (list_size(&conf->servers) > 0) {
1496
		WALK_LIST(n, conf->servers) {
1497
			srv_info_t *s = (srv_info_t *)n;
1498 1499 1500 1501 1502 1503
			char     *port = def_port;

			// If the port is already specified, use it.
			if (strlen(s->service) > 0) {
				port = s->service;
			}
1504

1505
			srv_info_t *server = srv_info_create(s->name, port);
1506 1507 1508 1509 1510
			if (server == NULL) {
				WARN("can't set nameserver %s port %s\n",
				     s->name, s->service);
				return;
			}
1511
			add_tail(&query->servers, (node_t *)server);
1512 1513
		}
	// Use system specific.
1514 1515
	} else {
		get_nameservers(&query->servers, def_port);
1516 1517 1518
	}
}

1519
void complete_queries(list_t *queries, const query_t *conf)
1520
{
1521
	node_t  *n = NULL;
1522

1523 1524 1525 1526 1527
	if (queries == NULL || conf == NULL) {
		DBG_NULL;
		return;
	}

1528
	// If there is no query, add default query: NS to ".".
1529
	if (list_size(queries) == 0) {
1530
		query_t *q = query_create(".", conf);
1531
		if (q == NULL) {
1532 1533 1534
			WARN("can't create query . NS IN\n");
			return;
		}
1535 1536
		q->class_num = KNOT_CLASS_IN;
		q->type_num = KNOT_RRTYPE_NS;
1537
		add_tail(queries, (node_t *)q);
1538 1539
	}

1540
	WALK_LIST(n, *queries) {
1541 1542
		query_t *q = (query_t *)n;

1543 1544 1545 1546
		// Fill class number if missing.
		if (q->class_num < 0) {
			if (conf->class_num >= 0) {
				q->class_num = conf->class_num;
1547
			} else {
1548
				q->class_num = KNOT_CLASS_IN;
1549 1550
			}
		}
1551 1552 1553 1554 1555

		// Fill type number if missing.
		if (q->type_num < 0) {
			if (conf->type_num >= 0) {
				q->type_num = conf->type_num;
1556
				q->serial = conf->serial;
1557
			} else {
1558
				q->type_num = KNOT_RRTYPE_A;
1559 1560
			}
		}
1561

1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572
		// Set zone transfer if any.
		if (q->type_num == KNOT_RRTYPE_AXFR ||
		    q->type_num == KNOT_RRTYPE_IXFR) {
			q->operation = OPERATION_XFR;
		}

		// No retries for TCP.
		if (q->protocol == PROTO_TCP) {
			q->retries = 0;
		}

1573 1574
		// Complete nameservers list.
		complete_servers(q, conf);
1575 1576 1577
	}
}

1578
static void print_help(void)
1579
{
1580
	printf("Usage: %s [-4] [-6] [-d] [-b address] [-c class] [-p port]\n"
1581
	       "            [-q name] [-t type] [-x address] [-k keyfile]\n"
1582 1583
	       "            [-y [algo:]keyname:key] [-E tapfile] [-G tapfile]\n"
	       "            name [type] [class] [@server]\n"
1584
	       "\n"
1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618
	       "       +[no]multiline        Wrap long records to more lines.\n"
	       "       +[no]short            Show record data only.\n"
	       "       +[no]generic          Use generic representation format.\n"
	       "       +[no]aaflag           Set AA flag.\n"
	       "       +[no]tcflag           Set TC flag.\n"
	       "       +[no]rdflag           Set RD flag.\n"
	       "       +[no]recurse          Same as +[no]rdflag\n"
	       "       +[no]raflag           Set RA flag.\n"
	       "       +[no]zflag            Set zero flag bit.\n"
	       "       +[no]adflag           Set AD flag.\n"
	       "       +[no]cdflag           Set CD flag.\n"
	       "       +[no]dnssec           Set DO flag.\n"
	       "       +[no]all              Show all packet sections.\n"
	       "       +[no]qr               Show query packet.\n"
	       "       +[no]header           Show packet header.\n"
	       "       +[no]opt              Show EDNS pseudosection.\n"
	       "       +[no]question         Show question section.\n"
	       "       +[no]answer           Show answer section.\n"
	       "       +[no]authority        Show authority section.\n"
	       "       +[no]additional       Show additional section.\n"
	       "       +[no]tsig             Show TSIG pseudosection.\n"
	       "       +[no]stats            Show trailing packet statistics.\n"
	       "       +[no]class            Show DNS class.\n"
	       "       +[no]ttl              Show TTL value.\n"
	       "       +[no]tcp              Use TCP protocol.\n"
	       "       +[no]ignore           Don't use TCP automatically if truncated.\n"
	       "       +[no]tls              Use TLS with Opportunistic privacy profile.\n"
	       "       +[no]tls-ca[=FILE]    Use TLS with Out-Of-Band privacy profile.\n"
	       "       +[no]tls-pin=BASE64   Use TLS with pinned certificate.\n"
	       "       +[no]tls-hostname=STR Use TLS with remote server hostname.\n"
	       "       +[no]nsid             Request NSID.\n"
	       "       +[no]bufsize=B        Set EDNS buffer size.\n"
	       "       +[no]padding=N        Padding block size EDNS(0) padding.\n"
	       "       +[no]alignment[=N]    Set packet alignment with EDNS(0) padding.\n"
1619 1620
	       "       +[no]subnet=SUBN      Set EDNS(0) client subnet addr/prefix.\n"
	       "       +[no]edns[=N]         Use EDNS(=version).\n"
1621 1622 1623
	       "       +[no]time=T           Set wait for reply interval in seconds.\n"
	       "       +[no]retry=N          Set number of retries.\n"
	       "       +noidn                Disable IDN transformation.\n"
1624
	       "\n"
1625 1626
	       "       -h, --help            Print the program help.\n"
	       "       -V, --version         Print the program version.\n",
1627
	       PROGRAM_NAME);
1628 1629
}

1630
static int parse_opt1(const char *opt, const char *value, kdig_params_t *params,