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
|
/* Copyright (C) 2024 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 <https://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <getopt.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "utils/knsupdate/knsupdate_params.h"
#include "utils/common/msg.h"
#include "utils/common/netio.h"
#include "libknot/libknot.h"
#include "libknot/tsig.h"
#include "contrib/base64.h"
#include "contrib/mempattern.h"
#include "contrib/strtonum.h"
#include "contrib/ucw/mempool.h"
#define DEFAULT_RETRIES_NSUPDATE 3
#define DEFAULT_TIMEOUT_NSUPDATE 12
static const style_t DEFAULT_STYLE_NSUPDATE = {
.format = FORMAT_NSUPDATE,
.style = {
.wrap = false,
.show_class = true,
.show_ttl = true,
.verbose = false,
.original_ttl = false,
.empty_ttl = false,
.human_ttl = false,
.human_timestamp = true,
.generic = false,
.ascii_to_idn = NULL
},
.show_query = false,
.show_header = true,
.show_section = true,
.show_edns = false,
.show_question = true,
.show_answer = true,
.show_authority = true,
.show_additional = true,
.show_tsig = true,
.show_footer = false
};
static int parser_set_default(zs_scanner_t *s, const char *fmt, ...)
{
/* Format string. */
char buf[512]; /* Must suffice for domain name and TTL. */
va_list ap;
va_start(ap, fmt);
int n = vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
if (n < 0 || (size_t)n >= sizeof(buf)) {
return ZS_EINVAL;
}
/* Buffer must contain newline */
if (zs_set_input_string(s, buf, n) != 0 ||
zs_parse_all(s) != 0) {
return s->error.code;
}
return KNOT_EOK;
}
static int knsupdate_init(knsupdate_params_t *params)
{
memset(params, 0, sizeof(knsupdate_params_t));
/* Initialize lists. */
init_list(¶ms->qfiles);
init_list(¶ms->update_list);
init_list(¶ms->prereq_list);
tls_params_init(¶ms->tls_params);
/* Initialize memory context. */
mm_ctx_mempool(¶ms->mm, MM_DEFAULT_BLKSIZE);
/* Default server. */
params->server = srv_info_create(DEFAULT_IPV4_NAME, DEFAULT_DNS_PORT);
if (!params->server)
return KNOT_ENOMEM;
/* Default settings. */
params->ip = IP_ALL;
params->protocol = PROTO_ALL;
params->class_num = KNOT_CLASS_IN;
params->retries = DEFAULT_RETRIES_NSUPDATE;
params->wait = DEFAULT_TIMEOUT_NSUPDATE;
/* Initialize RR parser. */
if (zs_init(¶ms->parser, ".", params->class_num, 3600) != 0 ||
zs_set_processing(¶ms->parser, NULL, NULL, NULL) != 0) {
zs_deinit(¶ms->parser);
return KNOT_ENOMEM;
}
/* Default style. */
params->style = DEFAULT_STYLE_NSUPDATE;
/* Create query/answer packets. */
params->query = knot_pkt_new(NULL, KNOT_WIRE_MAX_PKTSIZE, ¶ms->mm);
params->answer = knot_pkt_new(NULL, KNOT_WIRE_MAX_PKTSIZE, ¶ms->mm);
return KNOT_EOK;
}
void knsupdate_clean(knsupdate_params_t *params)
{
if (params == NULL) {
return;
}
/* Clear current query. */
knsupdate_reset(params);
/* Free qfiles. */
ptrlist_free(¶ms->qfiles, ¶ms->mm);
srv_info_free(params->server);
srv_info_free(params->srcif);
free(params->zone);
zs_deinit(¶ms->parser);
knot_pkt_free(params->query);
knot_pkt_free(params->answer);
knot_tsig_key_deinit(¶ms->tsig_key);
tls_params_clean(¶ms->tls_params);
quic_params_clean(¶ms->quic_params);
/* Clean up the structure. */
mp_delete(params->mm.ctx);
memset(params, 0, sizeof(*params));
}
/*! \brief Free RRSet list. */
static void rr_list_free(list_t *list, knot_mm_t *mm)
{
assert(list != NULL);
assert(mm != NULL);
ptrnode_t *node;
WALK_LIST(node, *list) {
knot_rrset_t *rrset = (knot_rrset_t *)node->d;
knot_rrset_free(rrset, NULL);
}
ptrlist_free(list, mm);
}
void knsupdate_reset(knsupdate_params_t *params)
{
/* Free ADD/REMOVE RRSets. */
rr_list_free(¶ms->update_list, ¶ms->mm);
/* Free PREREQ RRSets. */
rr_list_free(¶ms->prereq_list, ¶ms->mm);
}
static void print_help(void)
{
printf("Usage:\n"
" %s [-T] [options] [filename]\n"
" %s [-S | -Q] [tls_options] [options] [filename]\n"
"\n"
"Options:\n"
" -T, --tcp Use TCP protocol.\n"
" -S, --tls Use TLS protocol.\n"
" -Q, --quic Use QUIC protocol.\n"
" -p, --port <num> Remote port.\n"
" -r, --retry <num> Number of retries over UDP.\n"
" -t, --timeout <num> Update timeout.\n"
" -y, --tsig <str> TSIG key in the form [alg:]name:key.\n"
" -k, --tsigfile <path> Path to a TSIG key file.\n"
" -d, --debug Debug mode output.\n"
" -h, --help Print the program help.\n"
" -V, --version Print the program version.\n"
"\n"
"QUIC/TLS options:\n"
" -H, --hostname <str> Remote hostname validation.\n"
" -P, --pin <base64> Certificate key PIN.\n"
" -A, --ca [<path>] Path to a CA file.\n"
" -E, --certfile <path> Path to a client certificate file.\n"
" -K, --keyfile <path> Path to a client key file.\n"
" -s, --sni <str> Remote SNI.\n",
PROGRAM_NAME, PROGRAM_NAME);
}
int knsupdate_parse(knsupdate_params_t *params, int argc, char *argv[])
{
if (params == NULL || argv == NULL) {
return KNOT_EINVAL;
}
int ret = knsupdate_init(params);
if (ret != KNOT_EOK) {
return ret;
}
const char *opts_str = "dhvTSQV::p:r:t:y:k:H:P:A::E:K:s:";
struct option opts[] = {
{ "debug", no_argument, NULL, 'd' },
{ "help", no_argument, NULL, 'h' },
{ "tcp", no_argument, NULL, 'T' },
{ "tls", no_argument, NULL, 'S' },
{ "quic", no_argument, NULL, 'Q' },
{ "version", optional_argument, NULL, 'V' },
{ "port", required_argument, NULL, 'p' },
{ "retry", required_argument, NULL, 'r' },
{ "timeout", required_argument, NULL, 't' },
{ "tsig", required_argument, NULL, 'y' },
{ "tsigfile", required_argument, NULL, 'k' },
{ "hostname", required_argument, NULL, 'H' },
{ "pin", required_argument, NULL, 'P' },
{ "ca", optional_argument, NULL, 'A' },
{ "certfile", required_argument, NULL, 'E' },
{ "keyfile", required_argument, NULL, 'K' },
{ "sni", required_argument, NULL, 's' },
{ NULL }
};
bool default_port = true;
int opt = 0;
while ((opt = getopt_long(argc, argv, opts_str, opts, NULL)) != -1) {
switch (opt) {
case 'd':
msg_enable_debug(1);
break;
case 'h':
print_help();
params->stop = true;
return KNOT_EOK;
case 'v': // Compatibility with nsupdate.
case 'T':
params->protocol = PROTO_TCP;
break;
case 'S':
params->protocol = PROTO_TCP;
params->tls_params.enable = true;
if (default_port) {
free(params->server->service);
params->server->service = strdup(DEFAULT_DNS_TLS_PORT);
}
break;
case 'Q':
params->protocol = PROTO_UDP;
params->tls_params.enable = true;
params->quic_params.enable = true;
if (default_port) {
free(params->server->service);
params->server->service = strdup(DEFAULT_DNS_QUIC_PORT);
}
break;
case 'V':
print_version(PROGRAM_NAME, optarg != NULL);
params->stop = true;
return KNOT_EOK;
case 'p':
assert(optarg);
default_port = false;
free(params->server->service);
params->server->service = strdup(optarg);
break;
case 'r':
ret = str_to_u32(optarg, ¶ms->retries);
if (ret != KNOT_EOK) {
ERR("invalid retries '%s'", optarg);
return ret;
}
break;
case 't':
ret = params_parse_wait(optarg, ¶ms->wait);
if (ret != KNOT_EOK) {
ERR("invalid timeout '%s'", optarg);
return ret;
}
break;
case 'y':
knot_tsig_key_deinit(¶ms->tsig_key);
ret = knot_tsig_key_init_str(¶ms->tsig_key, optarg);
if (ret != KNOT_EOK) {
ERR("failed to parse TSIG key '%s'", optarg);
return ret;
}
break;
case 'k':
knot_tsig_key_deinit(¶ms->tsig_key);
ret = knot_tsig_key_init_file(¶ms->tsig_key, optarg);
if (ret != KNOT_EOK) {
ERR("failed to parse TSIG keyfile '%s'", optarg);
return ret;
}
break;
case 'H':
assert(optarg);
free(params->tls_params.hostname);
params->tls_params.hostname = strdup(optarg);
break;
case 'P':
assert(optarg);
uint8_t pin[64] = { 0 };
ret = knot_base64_decode((const uint8_t *)optarg, strlen(optarg), pin, sizeof(pin));
if (ret < 0) {
ERR("invalid certificate pin %s", optarg);
return ret;
} else if (ret != CERT_PIN_LEN) { // Check for 256-bit value.
ERR("invalid SHA256 hash length of certificate pin %s", optarg);
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(¶ms->tls_params.pins, item, NULL) == NULL) {
return KNOT_ENOMEM;
}
break;
case 'A':
if (optarg == NULL) {
params->tls_params.system_ca = true;
break;
}
if (ptrlist_add(¶ms->tls_params.ca_files, strdup(optarg), NULL) == NULL) {
ERR("failed to set CA file '%s'", optarg);
return KNOT_ENOMEM;
}
break;
case 'E':
assert(optarg);
free(params->tls_params.certfile);
params->tls_params.certfile = strdup(optarg);
break;
case 'K':
assert(optarg);
free(params->tls_params.keyfile);
params->tls_params.keyfile = strdup(optarg);
break;
case 's':
assert(optarg);
free(params->tls_params.sni);
params->tls_params.sni = strdup(optarg);
break;
default:
print_help();
return KNOT_ENOTSUP;
}
}
/* Retries only for UDP. */
if (params->protocol == PROTO_TCP || params->quic_params.enable) {
params->retries = 0;
} else {
/* If wait/tries < 1 s, set 1 second for each try. */
if (params->wait > 0 &&
(uint32_t)params->wait < ( 1 + params->retries)) {
params->wait = 1;
} else {
params->wait /= (1 + params->retries);
}
}
/* Process non-option parameters. */
for (; optind < argc; ++optind) {
ptrlist_add(¶ms->qfiles, argv[optind], ¶ms->mm);
}
return KNOT_EOK;
}
int knsupdate_set_ttl(knsupdate_params_t *params, const uint32_t ttl)
{
int ret = parser_set_default(¶ms->parser, "$TTL %u\n", ttl);
if (ret != KNOT_EOK) {
ERR("failed to set default TTL, %s", zs_strerror(ret));
}
return ret;
}
int knsupdate_set_origin(knsupdate_params_t *params, const char *origin)
{
char *fqdn = get_fqd_name(origin);
int ret = parser_set_default(¶ms->parser, "$ORIGIN %s\n", fqdn);
free(fqdn);
if (ret != KNOT_EOK) {
ERR("failed to set default origin, %s", zs_strerror(ret));
}
return ret;
}
|