ypscheme.c 9.88 KB
Newer Older
Daniel Salzman's avatar
Daniel Salzman committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*  Copyright (C) 2015 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/>.
*/

17
#include <assert.h>
Daniel Salzman's avatar
Daniel Salzman committed
18 19
#include <stdlib.h>

20
#include "libknot/internal/macros.h"
21 22
#include "libknot/yparser/ypscheme.h"
#include "libknot/yparser/yptrafo.h"
23
#include "libknot/errcode.h"
Daniel Salzman's avatar
Daniel Salzman committed
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

/*! Initializes the referenced item. */
static int set_ref_item(
	yp_item_t *dst,
	const yp_item_t *scheme)
{
	if (scheme == NULL) {
		return KNOT_EINVAL;
	}

	// Get reference category.
	const yp_name_t *ref_name = dst->var.r.ref_name;
	const yp_item_t *ref = yp_scheme_find(ref_name, NULL, scheme);
	if (ref == NULL) {
		return KNOT_YP_EINVAL_ITEM;
	}

	dst->var.r.ref = ref;

	return KNOT_EOK;
}

/*! Copies the sub_items list and initializes pointer to the identifier item. */
static int set_grp_item(
	yp_item_t *dst,
	const yp_item_t *src,
	const yp_item_t *scheme)
{
	// Count subitems.
	size_t num = 0;
	while (src->var.g.sub_items[num].name != NULL) {
		num++;
	}

	// Allocate space for subitems + terminal zero item.
	size_t memsize = (num + 1) * sizeof(yp_item_t);
	dst->sub_items = malloc(memsize);
	if (dst->sub_items == NULL) {
		return KNOT_ENOMEM;
	}
	memset(dst->sub_items, 0, memsize);

	// Copy subitems.
	for (size_t i = 0; i < num; i++) {
		// The first item is an identifier if multi group.
		if (i == 0 && (dst->flags & YP_FMULTI)) {
			dst->var.g.id = &dst->sub_items[0];
		}

		// Copy sub-item.
		dst->sub_items[i] = src->var.g.sub_items[i];

		// Initialize sub-item.
		int ret = KNOT_EOK;
		switch (dst->sub_items[i].type) {
		case YP_TREF:
			ret = set_ref_item(dst->sub_items + i, scheme);
			break;
		case YP_TGRP: // Deeper hierarchy is not supported.
			ret = KNOT_ENOTSUP;
			break;
		default:
			break;
		}

89 90 91
		// Set the parent item.
		dst->sub_items[i].parent = dst;

Daniel Salzman's avatar
Daniel Salzman committed
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
		if (ret != KNOT_EOK) {
			free(dst->sub_items);
			dst->sub_items = NULL;
			return ret;
		}
	}

	return KNOT_EOK;
}

static int set_item(
	yp_item_t *dst,
	const yp_item_t *src,
	const yp_item_t *scheme)
{
	// Check maximal item name length.
	if ((uint8_t)src->name[0] > YP_MAX_ITEM_NAME_LEN) {
		return KNOT_ERANGE;
	}

	// Copy the static data.
	*dst = *src;

	// Item type specific preparation.
	switch (src->type) {
	case YP_TREF:
		return set_ref_item(dst, scheme);
	case YP_TGRP:
		return set_grp_item(dst, src, scheme);
	default:
		return KNOT_EOK;
	}
}

static void unset_item(
	yp_item_t *item)
{
	if (item->sub_items != NULL) {
		free(item->sub_items);
	}

	memset(item, 0, sizeof(yp_item_t));
}

int yp_scheme_copy(
	yp_item_t **dst,
	const yp_item_t *src)
{
	if (dst == NULL || src == NULL) {
		return KNOT_EINVAL;
	}

	// Count scheme items.
	size_t scheme_items = 0;
	for (const yp_item_t *item = src; item->name != NULL; item++) {
		scheme_items++;
	}

	// Allocate space for new scheme.
	size_t size = (scheme_items + 1) * sizeof(yp_item_t);
	*dst = malloc(size);
	if (*dst == NULL) {
		return KNOT_ENOMEM;
	}
	memset(*dst, 0, size);

	// Copy the scheme.
	for (int i = 0; i < scheme_items; i++) {
		if (src[i].name == NULL) {
			break;
		}

		int ret = set_item(*dst + i, src + i, *dst);
		if (ret != KNOT_EOK) {
			yp_scheme_free(*dst);
			return ret;
		}
	}

	return KNOT_EOK;
}

void yp_scheme_free(
	yp_item_t *scheme)
{
	if (scheme == NULL) {
		return;
	}

	for (yp_item_t *item = scheme; item->name != NULL; item++) {
		unset_item(item);
	}
	free(scheme);
}

/*! Search the scheme for an item with the given name. */
static const yp_item_t* find_item(
	const char *name,
	size_t name_len,
	const yp_item_t *scheme)
{
	if (name == NULL || scheme == NULL) {
		return NULL;
	}

	for (const yp_item_t *item = scheme; item->name != NULL; item++) {
		if (item->name[0] != name_len) {
			continue;
		}
		if (memcmp(item->name + 1, name, name_len) == 0) {
			return item;
		}
	}

	return NULL;
}

const yp_item_t* yp_scheme_find(
	const yp_name_t *name,
	const yp_name_t *parent_name,
	const yp_item_t *scheme)
{
	if (name == NULL || scheme == NULL) {
		return NULL;
	}

	if (parent_name == NULL) {
		return find_item(name + 1, name[0], scheme);
	} else {
		const yp_item_t *parent = find_item(parent_name + 1,
		                                    parent_name[0], scheme);
		if (parent == NULL) {
			return NULL;
		}
		return find_item(name + 1, name[0], parent->sub_items);
	}
}

yp_check_ctx_t* yp_scheme_check_init(
	const yp_item_t *scheme)
{
	if (scheme == NULL) {
		return NULL;
	}

	yp_check_ctx_t *ctx = malloc(sizeof(yp_check_ctx_t));
	if (ctx == NULL) {
		return NULL;
	}
	memset(ctx, 0, sizeof(yp_check_ctx_t));

	ctx->scheme = scheme;

	return ctx;
}

248 249 250
static void reset_ctx(
	yp_check_ctx_t *ctx,
	size_t index)
Daniel Salzman's avatar
Daniel Salzman committed
251
{
252
	assert(index < YP_MAX_NODE_DEPTH);
Daniel Salzman's avatar
Daniel Salzman committed
253

254
	yp_node_t *node = &ctx->nodes[index];
Daniel Salzman's avatar
Daniel Salzman committed
255

256 257 258 259
	node->parent = (index > 0) ? &ctx->nodes[index - 1] : NULL;
	node->item = NULL;
	node->id_len = 0;
	node->data_len = 0;
Daniel Salzman's avatar
Daniel Salzman committed
260

261
	ctx->current = index;
Daniel Salzman's avatar
Daniel Salzman committed
262 263
}

264
static int check_item(
Daniel Salzman's avatar
Daniel Salzman committed
265 266 267 268
	const char *key,
	size_t key_len,
	const char *data,
	size_t data_len,
269 270
	yp_check_ctx_t *ctx,
	bool allow_key1_without_id)
Daniel Salzman's avatar
Daniel Salzman committed
271
{
272 273 274 275 276 277 278 279 280
	yp_node_t *node = &ctx->nodes[ctx->current];
	yp_node_t *parent = node->parent;
	bool is_id = false;

	if (parent != NULL) {
		// Check if valid group parent.
		if (parent->item->type != YP_TGRP) {
			return KNOT_YP_EINVAL_ITEM;
		}
Daniel Salzman's avatar
Daniel Salzman committed
281

282 283 284 285
		// Check if valid subitem.
		node->item = find_item(key, key_len, parent->item->sub_items);
	} else {
		node->item = find_item(key, key_len, ctx->scheme);
Daniel Salzman's avatar
Daniel Salzman committed
286
	}
287
	if (node->item == NULL) {
Daniel Salzman's avatar
Daniel Salzman committed
288 289 290
		return KNOT_YP_EINVAL_ITEM;
	}

291 292 293 294 295 296 297 298 299
	// Check if the parent requires id specification.
	if (parent != NULL && parent->item->var.g.id != NULL) {
		// Check if id.
		if (node->item == parent->item->var.g.id) {
			is_id = true;
			// Move current to the parent.
			--(ctx->current);
		// Check for missing id.
		} else if (parent->id_len == 0 && !allow_key1_without_id) {
Daniel Salzman's avatar
Daniel Salzman committed
300 301 302 303
			return KNOT_YP_ENOID;
		}
	}

304 305 306
	// Return if no data provided.
	if (data == NULL) {
		return KNOT_EOK;
Daniel Salzman's avatar
Daniel Salzman committed
307 308
	}

309 310 311
	// Group cannot have data.
	if (data_len != 0 && node->item->type == YP_TGRP) {
		return KNOT_YP_ENOTSUP_DATA;
Daniel Salzman's avatar
Daniel Salzman committed
312 313
	}

314 315 316 317 318 319 320 321
	// Convert item data to binary format.
	const yp_item_t *item = (node->item->type != YP_TREF) ?
	                        node->item : node->item->var.r.ref->var.g.id;
	if (is_id) {
		// Textual id must not be empty.
		if (data_len == 0) {
			return KNOT_YP_ENODATA;
		}
Daniel Salzman's avatar
Daniel Salzman committed
322

323 324 325
		parent->id_len = sizeof(((yp_node_t *)NULL)->id);
		int ret = yp_item_to_bin(item, data, data_len, parent->id,
		                         &parent->id_len);
Daniel Salzman's avatar
Daniel Salzman committed
326

327 328 329 330
		// Binary id must not be empty.
		if (ret == KNOT_EOK && parent->id_len == 0) {
			return KNOT_YP_EINVAL_DATA;
		}
Daniel Salzman's avatar
Daniel Salzman committed
331

332 333 334 335 336
		return ret;
	} else {
		node->data_len = sizeof(((yp_node_t *)NULL)->data);
		int ret = yp_item_to_bin(item, data, data_len, node->data,
		                         &node->data_len);
Daniel Salzman's avatar
Daniel Salzman committed
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
		return ret;
	}
}

int yp_scheme_check_parser(
	yp_check_ctx_t *ctx,
	const yp_parser_t *parser)
{
	if (ctx == NULL || parser == NULL) {
		return KNOT_EINVAL;
	}

	int ret;

	switch (parser->event) {
	case YP_EKEY0:
353 354 355
		reset_ctx(ctx, 0);
		ret = check_item(parser->key, parser->key_len, parser->data,
		                 parser->data_len, ctx, false);
Daniel Salzman's avatar
Daniel Salzman committed
356 357
		break;
	case YP_EKEY1:
358 359 360 361 362 363 364 365 366 367 368 369
		reset_ctx(ctx, 1);
		ret = check_item(parser->key, parser->key_len, parser->data,
		                 parser->data_len, ctx, false);
		if (ret != KNOT_EOK) {
			break;
		}

		// Check for KEY1 event with id item.
		if (ctx->current != 1) {
			return KNOT_YP_ENOTSUP_ID;
		}

Daniel Salzman's avatar
Daniel Salzman committed
370 371
		break;
	case YP_EID:
372 373 374 375 376 377 378 379 380 381 382 383
		reset_ctx(ctx, 1);
		ret = check_item(parser->key, parser->key_len, parser->data,
		                 parser->data_len, ctx, false);
		if (ret != KNOT_EOK) {
			break;
		}

		// Check for ID event with nonid item.
		if (ctx->current != 0) {
			return KNOT_YP_EINVAL_ID;
		}

Daniel Salzman's avatar
Daniel Salzman committed
384 385 386 387 388 389 390 391 392
		break;
	default:
		ret = KNOT_EPARSEFAIL;
		break;
	}

	return ret;
}

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
int yp_scheme_check_str(
	yp_check_ctx_t *ctx,
	const char *key0,
	const char *key1,
	const char *id,
	const char *data)
{
	if (ctx == NULL) {
		return KNOT_EINVAL;
	}

	size_t key0_len = (key0 != NULL) ? strlen(key0) : 0;
	size_t key1_len = (key1 != NULL) ? strlen(key1) : 0;
	size_t id_len   = (id   != NULL) ? strlen(id)   : 0;
	size_t data_len = (data != NULL) ? strlen(data) : 0;

	// Key0 must always be non-empty.
	if (key0_len == 0) {
		return KNOT_YP_EINVAL_ITEM;
	}

	// Process key0.
	reset_ctx(ctx, 0);
	if (key1_len == 0) {
		int ret = check_item(key0, key0_len, data, data_len, ctx, false);
		if (ret != KNOT_EOK) {
			return ret;
		}
	} else {
		int ret = check_item(key0, key0_len, NULL, 0, ctx, false);
		if (ret != KNOT_EOK) {
			return ret;
		}
	}

	// Process id.
	if (id_len != 0) {
		if (ctx->nodes[0].item->type != YP_TGRP ||
		    ctx->nodes[0].item->var.g.id == NULL) {
			return KNOT_YP_ENOTSUP_ID;
		}
		const yp_name_t *name = ctx->nodes[0].item->var.g.id->name;

		reset_ctx(ctx, 1);
		int ret = check_item(name + 1, name[0], id, id_len, ctx, true);
		if (ret != KNOT_EOK) {
			return ret;
		}

		// Check for non-id item (should not happen).
		assert(ctx->current == 0);

		// Check for group id with data.
		if (key1_len == 0 && data != NULL) {
			return KNOT_YP_ENOTSUP_DATA;
		}
	}

	// Process key1.
	if (key1_len != 0) {
		reset_ctx(ctx, 1);
		int ret = check_item(key1, key1_len, data, data_len, ctx, true);
		if (ret != KNOT_EOK) {
			return ret;
		}

		// Check for id in key1 with extra data.
		if (ctx->current != 1 && id_len != 0 && data != NULL) {
			return KNOT_YP_ENOTSUP_DATA;
		}
	}

	return KNOT_EOK;
}

Daniel Salzman's avatar
Daniel Salzman committed
468 469 470 471 472
void yp_scheme_check_deinit(
	yp_check_ctx_t* ctx)
{
	free(ctx);
}