1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
|
/* Copyright (C) 2011 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
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/>.
*/
#include <config.h>
#include "utils/dig/dig_params.h"
#include <string.h> // strncmp
#include <stdio.h> // printf
#include <getopt.h> // getopt
#include <stdlib.h> // free
#include <locale.h> // setlocale
#include "common/lists.h" // list
#include "common/errcode.h" // KNOT_EOK
#include "common/descriptor.h" // KNOT_CLASS_IN
#include "utils/common/msg.h" // WARN
#include "utils/common/params.h" // parse_class
#include "utils/common/resolv.h" // get_nameservers
#define DEFAULT_RETRIES_DIG 2
#define DEFAULT_TIMEOUT_DIG 5
static const flags_t DEFAULT_FLAGS_DIG = {
.aa_flag = false,
.tc_flag = false,
.rd_flag = true,
.ra_flag = false,
.z_flag = false,
.ad_flag = false,
.cd_flag = false,
.do_flag = false
};
static const style_t DEFAULT_STYLE_DIG = {
.format = FORMAT_FULL,
.style = {
.wrap = false,
.show_class = true,
.show_ttl = true,
.verbose = false,
.human_ttl = false,
.human_tmstamp = true,
.ascii_to_idn = name_to_idn
},
.show_query = false,
.show_header = true,
.show_edns = true,
.show_question = true,
.show_answer = true,
.show_authority = true,
.show_additional = true,
.show_footer = true
};
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;
}
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;
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;
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;
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;
}
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;
q->ignore_tc = true;
return KNOT_EOK;
}
static int opt_fail(const char *arg, void *query)
{
query_t *q = query;
q->servfail_stop = true;
return KNOT_EOK;
}
static int opt_nofail(const char *arg, void *query)
{
query_t *q = query;
q->servfail_stop = false;
return KNOT_EOK;
}
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;
}
static int opt_noidn(const char *arg, void *query)
{
query_t *q = query;
q->idn = false;
q->style.style.ascii_to_idn = NULL;
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;
}
static int opt_edns(const char *arg, void *query)
{
query_t *q = query;
if (arg == NULL) {
q->edns = 0;
return KNOT_EOK;
} else if (*arg == '\0') {
ERR("missing edns version\n");
return KNOT_EFEWDATA;
} else {
char *end;
long long num = strtoll(arg, &end, 10);
// Check for bad string.
if (end == arg || *end != '\0') {
ERR("bad +edns=%s\n", arg);
return KNOT_EINVAL;
}
if (num < 0 || num > UINT8_MAX) {
ERR("+edns=%s is out of range\n", arg);
return KNOT_ERANGE;
}
q->edns = num;
return KNOT_EOK;
}
}
static int opt_noedns(const char *arg, void *query)
{
query_t *q = query;
q->edns = -1;
q->udp_size = -1;
q->flags.do_flag = false;
q->nsid = false;
return KNOT_EOK;
}
static int opt_time(const char *arg, void *query)
{
query_t *q = query;
return params_parse_wait(arg, &q->wait);
}
static int opt_retry(const char *arg, void *query)
{
query_t *q = query;
return params_parse_num(arg, &q->retries);
}
static int opt_bufsize(const char *arg, void *query)
{
query_t *q = query;
char *end;
long long num = strtoll(arg, &end, 10);
// Check for bad string.
if (end == arg || *end != '\0') {
ERR("bad +bufsize=%s\n", arg);
return KNOT_EINVAL;
}
if (num > UINT16_MAX) {
num = UINT16_MAX;
WARN("+bufsize=%s is too big, using %lld instead\n", arg, num);
} else if (num < 0) {
num = 0;
WARN("+bufsize=%s is too small, using %lld instead\n", arg, num);
}
// Disable EDNS if zero bufsize.
if (num == 0) {
q->udp_size = -1;
} else {
q->udp_size = num;
}
return KNOT_EOK;
}
static const param_t dig_opts2[] = {
{ "multiline", ARG_NONE, opt_multiline },
{ "nomultiline", ARG_NONE, opt_nomultiline },
{ "short", ARG_NONE, opt_short },
{ "noshort", ARG_NONE, opt_noshort },
{ "aaflag", ARG_NONE, opt_aaflag },
{ "noaaflag", ARG_NONE, opt_noaaflag },
{ "tcflag", ARG_NONE, opt_tcflag },
{ "notcflag", ARG_NONE, opt_notcflag },
{ "rdflag", ARG_NONE, opt_rdflag },
{ "nordflag", ARG_NONE, opt_nordflag },
{ "recurse", ARG_NONE, opt_rdflag },
{ "norecurse", ARG_NONE, opt_nordflag },
{ "raflag", ARG_NONE, opt_raflag },
{ "noraflag", ARG_NONE, opt_noraflag },
{ "zflag", ARG_NONE, opt_zflag },
{ "nozflag", ARG_NONE, opt_nozflag },
{ "adflag", ARG_NONE, opt_adflag },
{ "noadflag", ARG_NONE, opt_noadflag },
{ "cdflag", ARG_NONE, opt_cdflag },
{ "nocdflag", ARG_NONE, opt_nocdflag },
{ "dnssec", ARG_NONE, opt_doflag },
{ "nodnssec", ARG_NONE, opt_nodoflag },
{ "all", ARG_NONE, opt_all },
{ "noall", ARG_NONE, opt_noall },
{ "qr", ARG_NONE, opt_qr },
{ "noqr", ARG_NONE, opt_noqr },
{ "header", ARG_NONE, opt_header },
{ "noheader", ARG_NONE, opt_noheader },
{ "opt", ARG_NONE, opt_opt },
{ "noopt", ARG_NONE, opt_noopt },
{ "question", ARG_NONE, opt_question },
{ "noquestion", ARG_NONE, opt_noquestion },
{ "answer", ARG_NONE, opt_answer },
{ "noanswer", ARG_NONE, opt_noanswer },
{ "authority", ARG_NONE, opt_authority },
{ "noauthority", ARG_NONE, opt_noauthority },
{ "additional", ARG_NONE, opt_additional },
{ "noadditional", ARG_NONE, opt_noadditional },
{ "stats", ARG_NONE, opt_stats },
{ "nostats", ARG_NONE, opt_nostats },
{ "class", ARG_NONE, opt_class },
{ "noclass", ARG_NONE, opt_noclass },
{ "ttl", ARG_NONE, opt_ttl },
{ "nottl", ARG_NONE, opt_nottl },
{ "tcp", ARG_NONE, opt_tcp },
{ "notcp", ARG_NONE, opt_notcp },
{ "fail", ARG_NONE, opt_fail },
{ "nofail", ARG_NONE, opt_nofail },
{ "ignore", ARG_NONE, opt_ignore },
{ "noignore", ARG_NONE, opt_noignore },
/* "idn" doesn't work since it must be called before query creation. */
{ "noidn", ARG_NONE, opt_noidn },
{ "nsid", ARG_NONE, opt_nsid },
{ "nonsid", ARG_NONE, opt_nonsid },
{ "edns", ARG_OPTIONAL, opt_edns },
{ "noedns", ARG_NONE, opt_noedns },
{ "time", ARG_REQUIRED, opt_time },
{ "retry", ARG_REQUIRED, opt_retry },
{ "bufsize", ARG_REQUIRED, opt_bufsize },
{ NULL }
};
query_t* query_create(const char *owner, const query_t *conf)
{
// Create output structure.
query_t *query = calloc(1, sizeof(query_t));
if (query == NULL) {
DBG_NULL;
return NULL;
}
// Set the query owner if any.
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) {
query->local = NULL;
query->operation = OPERATION_QUERY;
query->ip = IP_ALL;
query->protocol = PROTO_ALL;
query->port = strdup("");
query->udp_size = -1;
query->retries = DEFAULT_RETRIES_DIG;
query->wait = DEFAULT_TIMEOUT_DIG;
query->ignore_tc = false;
query->servfail_stop = true;
query->class_num = -1;
query->type_num = -1;
query->xfr_serial = 0;
query->flags = DEFAULT_FLAGS_DIG;
query->style = DEFAULT_STYLE_DIG;
query->idn = true;
query->nsid = false;
query->edns = -1;
} else {
if (conf->local != NULL) {
query->local = srv_info_create(conf->local->name,
conf->local->service);
if (query->local == NULL) {
query_free(query);
return NULL;
}
} else {
query->local = NULL;
}
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;
query->ignore_tc = conf->ignore_tc;
query->servfail_stop = conf->servfail_stop;
query->class_num = conf->class_num;
query->type_num = conf->type_num;
query->xfr_serial = conf->xfr_serial;
query->flags = conf->flags;
query->style = conf->style;
query->idn = conf->idn;
query->nsid = conf->nsid;
query->edns = conf->edns;
if (knot_copy_key_params(&conf->key_params, &query->key_params)
!= KNOT_EOK) {
query_free(query);
return NULL;
}
}
// Check dynamic allocation.
if (query->port == NULL) {
query_free(query);
return NULL;
}
return query;
}
void query_free(query_t *query)
{
node_t *n = NULL, *nxt = NULL;
if (query == NULL) {
DBG_NULL;
return;
}
// Cleanup servers.
WALK_LIST_DELSAFE(n, nxt, query->servers) {
srv_info_free((srv_info_t *)n);
}
// Cleanup local address.
if (query->local != NULL) {
srv_info_free(query->local);
}
// Cleanup cryptographic content.
free_sign_context(&query->sign_ctx);
knot_free_key_params(&query->key_params);
free(query->owner);
free(query->port);
free(query);
}
int dig_init(dig_params_t *params)
{
if (params == NULL) {
DBG_NULL;
return KNOT_EINVAL;
}
memset(params, 0, sizeof(*params));
params->stop = false;
// Initialize list of queries.
init_list(¶ms->queries);
// Create config query.
if ((params->config = query_create(NULL, NULL)) == NULL) {
return KNOT_ENOMEM;
}
return KNOT_EOK;
}
void dig_clean(dig_params_t *params)
{
node_t *n = NULL, *nxt = NULL;
if (params == NULL) {
DBG_NULL;
return;
}
// Clean up queries.
WALK_LIST_DELSAFE(n, nxt, params->queries) {
query_free((query_t *)n);
}
// Clean up config.
query_free((query_t *)params->config);
// Clean up the structure.
memset(params, 0, sizeof(*params));
}
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)
{
knot_free_key_params(&query->key_params);
if (params_parse_keyfile(value, &query->key_params) != KNOT_EOK) {
return KNOT_EINVAL;
}
return KNOT_EOK;
}
static int parse_local(const char *value, query_t *query)
{
srv_info_t *local = parse_nameserver(value, "0");
if (local == NULL) {
return KNOT_EINVAL;
}
if (query->local != NULL) {
srv_info_free(query->local);
}
query->local = local;
return KNOT_EOK;
}
static int parse_name(const char *value, list_t *queries, const query_t *conf)
{
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;
}
}
// If name is not FQDN, append trailing dot.
char *fqd_name = get_fqd_name(ascii_name);
if (conf->idn) {
free(ascii_name);
}
// Create new query.
query = query_create(fqd_name, conf);
free(fqd_name);
if (query == NULL) {
return KNOT_ENOMEM;
}
// Add new query to the queries.
add_tail(queries, (node_t *)query);
return KNOT_EOK;
}
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) {
srv_info_t *server = TAIL(query->servers);
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;
}
static int parse_reverse(const char *value, list_t *queries, const query_t *conf)
{
query_t *query = NULL;
// Create reverse name.
char *reverse = get_reverse_name(value);
if (reverse == NULL) {
return KNOT_EINVAL;
}
// Create reverse query for given address.
query = query_create(reverse, conf);
free(reverse);
if (query == NULL) {
return KNOT_ENOMEM;
}
// Set type for reverse query.
query->type_num = KNOT_RRTYPE_PTR;
// Add new query to the queries.
add_tail(queries, (node_t *)query);
return KNOT_EOK;
}
static int parse_server(const char *value, dig_params_t *params)
{
query_t *query;
// Set current query (last or config).
if (list_size(¶ms->queries) > 0) {
query = TAIL(params->queries);
} else {
query = params->config;
}
return params_parse_server(value, &query->servers, query->port);
}
static int parse_tsig(const char *value, query_t *query)
{
knot_free_key_params(&query->key_params);
if (params_parse_tsig(value, &query->key_params) != KNOT_EOK) {
return KNOT_EINVAL;
}
return KNOT_EOK;
}
static int parse_type(const char *value, query_t *query)
{
uint16_t rtype;
uint32_t serial;
if (params_parse_type(value, &rtype, &serial) != KNOT_EOK) {
return KNOT_EINVAL;
}
query->type_num = rtype;
query->xfr_serial = serial;
return KNOT_EOK;
}
static void complete_servers(query_t *query, const query_t *conf)
{
node_t *n = NULL;
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;
} else {
def_port = DEFAULT_DNS_PORT;
}
// Complete specified nameservers if any.
if (list_size(&query->servers) > 0) {
WALK_LIST(n, query->servers) {
srv_info_t *s = (srv_info_t *)n;
// 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) {
WALK_LIST(n, conf->servers) {
srv_info_t *s = (srv_info_t *)n;
char *port = def_port;
// If the port is already specified, use it.
if (strlen(s->service) > 0) {
port = s->service;
}
srv_info_t *server = srv_info_create(s->name, port);
if (server == NULL) {
WARN("can't set nameserver %s port %s\n",
s->name, s->service);
return;
}
add_tail(&query->servers, (node_t *)server);
}
// Use system specific.
} else if (get_nameservers(&query->servers, def_port) <= 0) {
WARN("can't read any nameservers\n");
}
}
void complete_queries(list_t *queries, const query_t *conf)
{
query_t *q = NULL;
node_t *n = NULL;
if (queries == NULL || conf == NULL) {
DBG_NULL;
return;
}
// If there is no query, add default query: NS to ".".
if (list_size(queries) == 0) {
q = query_create(".", conf);
if (q == NULL) {
WARN("can't create query . NS IN\n");
return;
}
q->class_num = KNOT_CLASS_IN;
q->type_num = KNOT_RRTYPE_NS;
add_tail(queries, (node_t *)q);
}
WALK_LIST(n, *queries) {
query_t *q = (query_t *)n;
// Fill class number if missing.
if (q->class_num < 0) {
if (conf->class_num >= 0) {
q->class_num = conf->class_num;
} else {
q->class_num = KNOT_CLASS_IN;
}
}
// Fill type number if missing.
if (q->type_num < 0) {
if (conf->type_num >= 0) {
q->type_num = conf->type_num;
q->xfr_serial = conf->xfr_serial;
} else {
q->type_num = KNOT_RRTYPE_A;
}
}
// 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;
}
// Complete nameservers list.
complete_servers(q, conf);
}
}
static void dig_help(void)
{
printf("Usage: kdig [-4] [-6] [-dh] [-b address] [-c class] [-p port]\n"
" [-q name] [-t type] [-x address] [-k keyfile]\n"
" [-y [algo:]keyname:key] name @server\n"
"\n"
" +[no]multiline Wrap long records to more lines.\n"
" +[no]short Show record data only.\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]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]fail Stop if SERVFAIL.\n"
" +[no]ignore Don't use TCP automatically if truncated.\n"
" +[no]nsid Request NSID.\n"
" +[no]edns=N Use EDNS (=version).\n"
" +noidn Disable IDN transformation.\n"
" +time=T Set wait for reply interval in seconds.\n"
" +retry=N Set number of retries.\n"
" +bufsize=B Set EDNS buffer size.\n"
"\n"
" -h, --help Print help.\n"
" -v, --version Print program version.\n");
}
static int parse_opt1(const char *opt, const char *value, dig_params_t *params,
int *index)
{
const char *val = value;
size_t len = strlen(opt);
int add = 1;
query_t *query;
// Set current query (last or config).
if (list_size(¶ms->queries) > 0) {
query = TAIL(params->queries);
} else {
query = params->config;
}
// If there is no space between option and argument.
if (len > 1) {
val = opt + 1;
add = 0;
}
switch (opt[0]) {
case '4':
if (len > 1) {
ERR("invalid option -%s\n", opt);
return KNOT_ENOTSUP;
}
query->ip = IP_4;
break;
case '6':
if (len > 1) {
ERR("invalid option -%s\n", opt);
return KNOT_ENOTSUP;
}
query->ip = IP_6;
break;
case 'b':
if (val == NULL) {
ERR("missing address\n");
return KNOT_EINVAL;
}
if (parse_local(val, query) != KNOT_EOK) {
ERR("bad address %s\n", val);
return KNOT_EINVAL;
}
*index += add;
break;
case 'd':
msg_enable_debug(1);
break;
case 'h':
if (len > 1) {
ERR("invalid option -%s\n", opt);
return KNOT_ENOTSUP;
}
dig_help();
params->stop = true;
break;
case 'c':
if (val == NULL) {
ERR("missing class\n");
return KNOT_EINVAL;
}
if (parse_class(val, query) != KNOT_EOK) {
ERR("bad class %s\n", val);
return KNOT_EINVAL;
}
*index += add;
break;
case 'k':
if (val == NULL) {
ERR("missing filename\n");
return KNOT_EINVAL;
}
if (parse_keyfile(val, query) != KNOT_EOK) {
ERR("bad keyfile %s\n", value);
return KNOT_EINVAL;
}
*index += add;
break;
case 'p':
if (val == NULL) {
ERR("missing port\n");
return KNOT_EINVAL;
}
if (parse_port(val, query) != KNOT_EOK) {
ERR("bad port %s\n", value);
return KNOT_EINVAL;
}
*index += add;
break;
case 'q':
if (val == NULL) {
ERR("missing name\n");
return KNOT_EINVAL;
}
if (parse_name(val, ¶ms->queries, params->config)
!= KNOT_EOK) {
ERR("bad query name %s\n", val);
return KNOT_EINVAL;
}
*index += add;
break;
case 't':
if (val == NULL) {
ERR("missing type\n");
return KNOT_EINVAL;
}
if (parse_type(val, query) != KNOT_EOK) {
ERR("bad type %s\n", val);
return KNOT_EINVAL;
}
*index += add;
break;
case 'v':
if (len > 1) {
ERR("invalid option -%s\n", opt);
return KNOT_ENOTSUP;
}
printf(KDIG_VERSION);
params->stop = true;
break;
case 'x':
if (val == NULL) {
ERR("missing address\n");
return KNOT_EINVAL;
}
if (parse_reverse(val, ¶ms->queries, params->config)
!= KNOT_EOK) {
ERR("bad reverse name %s\n", val);
return KNOT_EINVAL;
}
*index += add;
break;
case 'y':
if (val == NULL) {
ERR("missing key\n");
return KNOT_EINVAL;
}
if (parse_tsig(val, query) != KNOT_EOK) {
ERR("bad key %s\n", value);
return KNOT_EINVAL;
}
*index += add;
break;
case '-':
if (strcmp(opt, "-help") == 0) {
dig_help();
params->stop = true;
} else if (strcmp(opt, "-version") == 0) {
printf(KDIG_VERSION);
params->stop = true;
} else {
ERR("invalid option: -%s\n", opt);
return KNOT_ENOTSUP;
}
break;
default:
ERR("invalid option: -%s\n", opt);
return KNOT_ENOTSUP;
}
return KNOT_EOK;
}
static int parse_opt2(const char *value, dig_params_t *params)
{
query_t *query;
// Set current query (last or config).
if (list_size(¶ms->queries) > 0) {
query = TAIL(params->queries);
} else {
query = params->config;
}
// Get option name.
const char *arg_sep = "=";
size_t opt_len = strcspn(value, arg_sep);
if (opt_len < 1) {
ERR("invalid option: +%s\n", value);
return KNOT_ENOTSUP;
}
// Get option argument if any.
const char *arg = NULL;
const char *rest = value + opt_len;
if (strlen(rest) > 0) {
arg = rest + strspn(rest, arg_sep);
}
// Check if the given option is supported.
bool unique;
int ret = best_param(value, opt_len, dig_opts2, &unique);
if (ret < 0) {
ERR("invalid option: +%s\n", value);
return KNOT_ENOTSUP;
} else if (!unique) {
ERR("ambiguous option: +%s\n", value);
return KNOT_ENOTSUP;
}
// Check argument presence.
switch (dig_opts2[ret].arg) {
case ARG_NONE:
if (arg != NULL) {
ERR("superfluous option argument: +%s\n", value);
return KNOT_ENOTSUP;
}
break;
case ARG_REQUIRED:
if (arg == NULL) {
ERR("missing argument: +%s\n", value);
return KNOT_EFEWDATA;
} else if (*arg == '\0') {
ERR("empty argument: +%s\n", value);
return KNOT_EFEWDATA;
}
break;
case ARG_OPTIONAL:
break;
}
// Call option handler.
return dig_opts2[ret].handler(arg, query);
}
static int parse_token(const char *value, dig_params_t *params)
{
query_t *query;
// Set current query (last or config).
if (list_size(¶ms->queries) > 0) {
query = TAIL(params->queries);
} else {
query = params->config;
}
// Try to guess the meaning of the token.
if (parse_type(value, query) == KNOT_EOK) {
return KNOT_EOK;
} else if (parse_class(value, query) == KNOT_EOK) {
return KNOT_EOK;
} else if (parse_name(value, ¶ms->queries, params->config)
== KNOT_EOK) {
return KNOT_EOK;
}
ERR("invalid parameter: %s\n", value);
return KNOT_EINVAL;
}
int dig_parse(dig_params_t *params, int argc, char *argv[])
{
if (params == NULL || argv == NULL) {
DBG_NULL;
return KNOT_EINVAL;
}
// Initialize parameters.
if (dig_init(params) != KNOT_EOK) {
return KNOT_ERROR;
}
#ifdef LIBIDN
// Set up localization.
if (setlocale(LC_CTYPE, "") == NULL) {
WARN("can't setlocale, disabling IDN\n");
params->config->idn = false;
params->config->style.style.ascii_to_idn = NULL;
}
#endif
// Command line parameters processing.
for (int i = 1; i < argc; i++) {
int ret = KNOT_ERROR;
// Process parameter.
switch (argv[i][0]) {
case '@':
ret = parse_server(argv[i] + 1, params);
break;
case '-':
ret = parse_opt1(argv[i] + 1, argv[i + 1], params, &i);
break;
case '+':
ret = parse_opt2(argv[i] + 1, params);
break;
default:
ret = parse_token(argv[i], params);
break;
}
// Check return.
switch (ret) {
case KNOT_EOK:
if (params->stop) {
return KNOT_EOK;
}
break;
case KNOT_ENOTSUP:
dig_help();
default: // Fall through.
return ret;
}
}
// Complete missing data in queries based on defaults.
complete_queries(¶ms->queries, params->config);
return KNOT_EOK;
}
|