Skip to content

Commit fe81476

Browse files
committed
Require origin is specified in wire format
Fixes NLnetLabs#115.
1 parent a314bcd commit fe81476

File tree

9 files changed

+53
-62
lines changed

9 files changed

+53
-62
lines changed

include/zone.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,8 @@ typedef struct {
314314
bool no_includes;
315315
/** Enable 1h2m3s notations for TTLS. */
316316
bool pretty_ttls;
317-
// FIXME: require origin to be in wire format? (#115)
318-
const char *origin;
317+
/** Origin in wire format. */
318+
zone_name_t origin;
319319
uint32_t default_ttl;
320320
uint16_t default_class;
321321
struct {

src/bench.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ static void usage(const char *program)
172172
exit(EXIT_FAILURE);
173173
}
174174

175+
static uint8_t root[] = { 0 };
176+
175177
int main(int argc, char *argv[])
176178
{
177179
const char *name = NULL, *program = argv[0];
@@ -215,7 +217,8 @@ int main(int argc, char *argv[])
215217
zone_buffers_t buffers = { 1, &owner, &rdata };
216218

217219
options.accept.callback = &bench_accept;
218-
options.origin = ".";
220+
options.origin.octets = root;
221+
options.origin.length = 1;
219222
options.default_ttl = 3600;
220223
options.default_class = ZONE_IN;
221224

src/zone.c

+15-50
Original file line numberDiff line numberDiff line change
@@ -47,56 +47,24 @@ static int32_t check_options(const zone_options_t *options)
4747
{
4848
if (!options->accept.callback)
4949
return ZONE_BAD_PARAMETER;
50-
if (!options->origin)
51-
return ZONE_BAD_PARAMETER;
5250
if (!options->default_ttl || options->default_ttl > INT32_MAX)
5351
return ZONE_BAD_PARAMETER;
52+
if (!options->origin.octets || !options->origin.length)
53+
return ZONE_BAD_PARAMETER;
5454

55-
// makes no sense?!?!
56-
switch (options->default_class) {
57-
case ZONE_IN:
58-
case ZONE_CS:
59-
case ZONE_CH:
60-
case ZONE_HS:
61-
break;
62-
default:
55+
const uint8_t *root = &options->origin.octets[options->origin.length - 1];
56+
if (root[0] != 0)
57+
return ZONE_BAD_PARAMETER;
58+
const uint8_t *label = &options->origin.octets[0];
59+
while (label < root) {
60+
if (root - label < label[0])
6361
return ZONE_BAD_PARAMETER;
62+
label += label[0] + 1;
6463
}
6564

66-
return 0;
67-
}
68-
69-
// FIXME: replace with parser from fallback
70-
static int parse_origin(const char *origin, uint8_t str[255], size_t *len)
71-
{
72-
size_t lab = 0, oct = 1;
73-
74-
assert(origin);
75-
76-
for (size_t i=0; ; i++) {
77-
char chr = origin[i];
78-
if (oct >= 255)
79-
return -1;
80-
81-
if (chr == '.' || chr == '\0') {
82-
if (oct - 1 == lab && lab > 0 && chr != '\0')
83-
return -1;
84-
else if ((oct - lab) - 1 > 63)
85-
return -1;
86-
str[lab] = (uint8_t)((oct - lab) - 1);
87-
if (chr != '.')
88-
break;
89-
lab = oct++;
90-
str[lab] = 0;
91-
} else {
92-
str[oct++] = chr & 0xff;
93-
}
94-
}
95-
96-
if (str[lab] != 0)
97-
return -1;
65+
if (label != root)
66+
return ZONE_BAD_PARAMETER;
9867

99-
*len = oct;
10068
return 0;
10169
}
10270

@@ -315,10 +283,8 @@ int32_t zone_open(
315283
file = parser->file = &parser->first;
316284
if ((result = open_file(parser, file, path, strlen(path))) < 0)
317285
goto error;
318-
if (parse_origin(options->origin, file->origin.octets, &file->origin.length) < 0) {
319-
result = ZONE_BAD_PARAMETER;
320-
goto error;
321-
}
286+
memcpy(file->origin.octets, options->origin.octets, options->origin.length);
287+
file->origin.length = options->origin.length;
322288
parser->buffers.size = buffers->size;
323289
parser->buffers.owner.serial = 0;
324290
parser->buffers.owner.blocks = buffers->owner;
@@ -372,9 +338,8 @@ int32_t zone_parse_string(
372338
parser->options = *options;
373339
parser->user_data = user_data;
374340
file = parser->file = &parser->first;
375-
if ((result = parse_origin(options->origin, file->origin.octets, &file->origin.length)) < 0)
376-
return result;
377-
341+
memcpy(file->origin.octets, options->origin.octets, options->origin.length);
342+
file->origin.length = options->origin.length;
378343
file->name = (char *)not_a_file;
379344
file->path = (char *)not_a_file;
380345
file->handle = NULL;

tests/base32.c

+7-4
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ static int32_t add_rr(
3535
return ZONE_SUCCESS;
3636
}
3737

38-
static const uint8_t foobar[] = {
39-
'f', 'o', 'o', 'b', 'a', 'r'
40-
};
38+
static const uint8_t foobar[] =
39+
{ 'f', 'o', 'o', 'b', 'a', 'r' };
40+
41+
static uint8_t origin[] =
42+
{ 7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 3, 'c', 'o', 'm', 0 };
4143

4244
/*!cmocka */
4345
void base32_syntax(void **state)
@@ -78,7 +80,8 @@ void base32_syntax(void **state)
7880
fprintf(stderr, "INPUT: '%s'\n", rr);
7981

8082
options.accept.callback = add_rr;
81-
options.origin = "example.com.";
83+
options.origin.octets = origin;
84+
options.origin.length = sizeof(origin);
8285
options.default_ttl = 3600;
8386
options.default_class = ZONE_IN;
8487

tests/include.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ static int32_t add_rr(
152152
return ZONE_SUCCESS;
153153
}
154154

155+
static uint8_t origin[] =
156+
{ 7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 3, 'c', 'o', 'm', 0 };
157+
155158
/*!cmocka setup:setup teardown:teardown */
156159
void include_from_string(void **state)
157160
{
@@ -164,7 +167,8 @@ void include_from_string(void **state)
164167
int32_t result;
165168

166169
options.accept.callback = &add_rr;
167-
options.origin = "example.com.";
170+
options.origin.octets = origin;
171+
options.origin.length = sizeof(origin);
168172
options.default_ttl = 3600;
169173
options.default_class = ZONE_IN;
170174

tests/ip4.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ static int32_t add_rr(
3636

3737
static const uint8_t address_192_0_2_1[] = { 192, 0, 2, 1 };
3838

39+
static uint8_t origin[] =
40+
{ 7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 3, 'c', 'o', 'm', 0 };
41+
3942
/*!cmocka */
4043
void ipv4_syntax(void **state)
4144
{
@@ -89,7 +92,8 @@ void ipv4_syntax(void **state)
8992
(void)snprintf(rr, sizeof(rr), " A %s", tests[i].address);
9093

9194
options.accept.callback = add_rr;
92-
options.origin = "example.com.";
95+
options.origin.octets = origin;
96+
options.origin.length = sizeof(origin);
9397
options.default_ttl = 3600;
9498
options.default_class = ZONE_IN;
9599

tests/svcb.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,9 @@ static int32_t add_rr(
282282
return ZONE_SUCCESS;
283283
}
284284

285+
static uint8_t origin[] =
286+
{ 7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 3, 'c', 'o', 'm', 0 };
287+
285288
/*!cmocka */
286289
void rfc9460_test_vectors(void **state)
287290
{
@@ -297,7 +300,8 @@ void rfc9460_test_vectors(void **state)
297300
int32_t result;
298301

299302
options.accept.callback = add_rr;
300-
options.origin = "example.com.";
303+
options.origin.octets = origin;
304+
options.origin.length = sizeof(origin);
301305
options.default_ttl = 3600;
302306
options.default_class = ZONE_IN;
303307

tests/time.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ static int32_t add_rr(
3535
return 0;
3636
}
3737

38+
static uint8_t origin[] =
39+
{ 7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 3, 'c', 'o', 'm', 0 };
40+
3841
/*!cmocka */
3942
void time_stamp_syntax(void **state)
4043
{
@@ -89,7 +92,8 @@ void time_stamp_syntax(void **state)
8992
(void)snprintf(rr, size, FORMAT, tests[i].timestamp);
9093

9194
options.accept.callback = add_rr;
92-
options.origin = "example.com.";
95+
options.origin.octets = origin;
96+
options.origin.length = sizeof(origin);
9397
options.default_ttl = 3600;
9498
options.default_class = ZONE_IN;
9599

tests/types.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,9 @@ static int32_t add_rr(
980980
return ZONE_SUCCESS;
981981
}
982982

983+
static uint8_t origin[] =
984+
{ 7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 3, 'c', 'o', 'm', 0 };
985+
983986
/*!cmocka */
984987
void supported_types(void **state)
985988
{
@@ -995,7 +998,8 @@ void supported_types(void **state)
995998
int32_t result;
996999

9971000
options.accept.callback = add_rr;
998-
options.origin = "example.com.";
1001+
options.origin.octets = origin;
1002+
options.origin.length = sizeof(origin);
9991003
options.default_ttl = 3600;
10001004
options.default_class = ZONE_IN;
10011005

0 commit comments

Comments
 (0)