cf-parse.y 29.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*  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/>.
*/
16 17 18 19 20 21 22
/*!
 * \file cf-parse.y
 *
 * \author Ondrej Sury <ondrej.sury@nic.cz>
 *
 * \brief Server configuration structures and API.
 */
23
%{
24

25
#include <config.h>
26
#include <limits.h>
27
#include <stdio.h>
28
#include <string.h>
29
#include <stdlib.h>
30 31
#include <pwd.h>
#include <grp.h>
32
#include "common/sockaddr.h"
Ondřej Surý's avatar
Ondřej Surý committed
33
#include "libknot/dname.h"
34
#include "libknot/binary.h"
35
#include "libknot/edns.h"
36
#include "knot/server/rrl.h"
37
#include "knot/conf/conf.h"
38
#include "knot/conf/libknotd_la-cf-parse.h" /* Automake generated header. */
39

40
extern int cf_lex (YYSTYPE *lvalp, void *scanner);
41
extern void cf_error(void *scanner, const char *format, ...);
42
extern conf_t *new_config;
43
static conf_iface_t *this_iface = 0;
44
static conf_iface_t *this_remote = 0;
45
static conf_zone_t *this_zone = 0;
46
static conf_group_t *this_group = 0;
47
static list_t *this_list = 0;
48 49
static conf_log_t *this_log = 0;
static conf_log_map_t *this_logmap = 0;
Marek Vavrusa's avatar
Marek Vavrusa committed
50
//#define YYERROR_VERBOSE 1
51

52 53 54 55 56 57 58 59 60 61 62 63 64 65
#define SET_NUM(out, in, min, max, name)				\
{									\
	if (in < min || in > max) {					\
		cf_error(scanner, "'%s' is out of range (%u-%u)",	\
		         name, min, max);				\
	} else {							\
		out = in;						\
	}								\
}

#define SET_UINT16(out, in, name) SET_NUM(out, in, 0, UINT16_MAX, name);
#define SET_INT(out, in, name) SET_NUM(out, in, 0, INT_MAX, name);
#define SET_SIZE(out, in, name) SET_NUM(out, in, 0, SIZE_MAX, name);

66
static void conf_init_iface(void *scanner, char* ifname, int port)
67
{
68 69 70 71 72 73 74 75
	this_iface = malloc(sizeof(conf_iface_t));
	if (this_iface == NULL) {
		cf_error(scanner, "not enough memory when allocating interface");
		return;
	}
	memset(this_iface, 0, sizeof(conf_iface_t));
	this_iface->name = ifname;
	this_iface->port = port;
76 77 78 79
}

static void conf_start_iface(void *scanner, char* ifname)
{
80 81 82
	conf_init_iface(scanner, ifname, -1);
	add_tail(&new_config->ifaces, &this_iface->n);
	++new_config->ifaces_count;
83 84
}

85 86 87 88 89 90 91 92 93 94 95 96
static conf_iface_t *conf_get_remote(const char *name)
{
	conf_iface_t *remote;
	WALK_LIST (remote, new_config->remotes) {
		if (strcmp(remote->name, name) == 0) {
			return remote;
		}
	}

	return NULL;
}

97
static void conf_start_remote(void *scanner, char *remote)
98
{
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
	if (conf_get_remote(remote) != NULL) {
		cf_error(scanner, "remote '%s' already defined", remote);
		return;
	}

	this_remote = malloc(sizeof(conf_iface_t));
	if (this_remote == NULL) {
		cf_error(scanner, "not enough memory when allocating remote");
		return;
	}

	memset(this_remote, 0, sizeof(conf_iface_t));
	this_remote->name = remote;
	add_tail(&new_config->remotes, &this_remote->n);
	sockaddr_init(&this_remote->via, -1);
	++new_config->remotes_count;
115 116
}

117
static void conf_remote_set_via(void *scanner, char *item) {
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
	/* Find existing node in interfaces. */
	node_t* r = 0; conf_iface_t* found = 0;
	WALK_LIST (r, new_config->ifaces) {
		if (strcmp(((conf_iface_t*)r)->name, item) == 0) {
			found = (conf_iface_t*)r;
			break;
		}
	}

	/* Check */
	if (!found) {
		cf_error(scanner, "interface '%s' is not defined", item);
	} else {
		sockaddr_set(&this_remote->via, found->family, found->address, 0);
	}
133 134
}

135
static conf_group_t *conf_get_group(const char *name)
136
{
137 138 139 140 141 142
	conf_group_t *group;
	WALK_LIST (group, new_config->groups) {
		if (strcmp(group->name, name) == 0) {
			return group;
		}
	}
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
	return NULL;
}

static void conf_start_group(void *scanner, char *name)
{
	conf_group_t *group = conf_get_group(name);
	if (group) {
		cf_error(scanner, "group '%s' already defined", name);
		return;
	}

	if (conf_get_remote(name) != NULL) {
		cf_error(scanner, "group name '%s' conflicts with remote name",
		         name);
		free(name);
		return;
	}

	/* Add new group. */

	group = calloc(1, sizeof(conf_group_t));
	if (!group) {
		cf_error(scanner, "out of memory");
		free(name);
		return;
	}

	group->name = name;
	init_list(&group->remotes);

	add_tail(&new_config->groups, &group->n);
	this_group = group;
}

static void conf_add_member_into_group(void *scanner, char *name)
{
	if (!this_group) {
		cf_error(scanner, "parser error, variable 'this_group' null");
		free(name);
		return;
	}

	if (conf_get_remote(name) == NULL) {
		cf_error(scanner, "remote '%s' is not defined", name);
		free(name);
		return;
	}

	// add the remote into the group while silently ignoring duplicates

	conf_group_remote_t *remote;
195
	node_t *n;
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
	WALK_LIST (n, this_group->remotes) {
		remote = (conf_group_remote_t *)n;
		if (strcmp(remote->name, name) == 0) {
			free(name);
			return;
		}
	}

	remote = calloc(1, sizeof(conf_group_remote_t));
	remote->name = name;
	add_tail(&this_group->remotes, &remote->n);
}

static bool set_remote_or_group(void *scanner, char *name,
				void (*install)(void *, conf_iface_t *))
{
	// search remotes

	conf_iface_t *remote = conf_get_remote(name);
	if (remote) {
		install(scanner, remote);
		return true;
	}

	// search groups

	conf_group_t *group = conf_get_group(name);
	if (group) {
		conf_group_remote_t *group_remote;
		WALK_LIST (group_remote, group->remotes) {
			remote = conf_get_remote(group_remote->name);
			if (!remote)
				continue;
			install(scanner, remote);
		}

		return true;
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

	return false;
}

static void conf_acl_item_install(void *scanner, conf_iface_t *found)
{
	// silently skip duplicates

	conf_remote_t *remote;
	WALK_LIST (remote, *this_list) {
		if (remote->remote == found) {
			return;
		}
	}

	// additional check for transfers

	if ((this_list == &this_zone->acl.xfr_in ||
	    this_list == &this_zone->acl.notify_out) && found->port == 0)
	{
		cf_error(scanner, "remote specified for XFR/IN or "
		"NOTIFY/OUT needs to have valid port!");
		return;
	}

	// add into the list

	remote = malloc(sizeof(conf_remote_t));
262
	if (!remote) {
263 264
		cf_error(scanner, "out of memory");
		return;
265 266
	}

267 268 269 270 271 272 273 274 275 276 277 278
	remote->remote = found;
	add_tail(this_list, &remote->n);
}

static void conf_acl_item(void *scanner, char *item)
{
	if (!set_remote_or_group(scanner, item, conf_acl_item_install)) {
		cf_error(scanner, "remote or group '%s' not defined", item);
	}

	free(item);
}
279

280
static int conf_key_exists(void *scanner, char *item)
281
{
282
	/* Find existing node in keys. */
283 284
	knot_dname_t *sample = knot_dname_from_str(item);
	knot_dname_to_lower(sample);
285 286 287
	conf_key_t* r = 0;
	WALK_LIST (r, new_config->keys) {
		if (knot_dname_cmp(r->k.name, sample) == 0) {
288 289
			cf_error(scanner, "key '%s' is already defined", item);
			knot_dname_free(&sample);
290 291 292 293
			return 1;
		}
	}

294
	knot_dname_free(&sample);
295
	return 0;
296 297
}

298
static int conf_key_add(void *scanner, knot_tsig_key_t **key, char *item)
299
{
300 301 302 303
	/* Reset */
	*key = 0;

	/* Find in keys */
304
	knot_dname_t *sample = knot_dname_from_str(item);
305 306 307 308 309 310 311 312 313 314 315 316 317 318
	knot_dname_to_lower(sample);

	conf_key_t* r = 0;
	WALK_LIST (r, new_config->keys) {
		if (knot_dname_cmp(r->k.name, sample) == 0) {
			*key = &r->k;
			knot_dname_free(&sample);
			return 0;
		}
	}

	cf_error(scanner, "key '%s' is not defined", item);
	knot_dname_free(&sample);
	return 1;
319 320
}

321
static void conf_zone_start(void *scanner, char *name) {
322 323 324 325 326 327 328 329 330 331 332 333 334 335
	this_zone = malloc(sizeof(conf_zone_t));
	if (this_zone == NULL || name == NULL) {
		cf_error(scanner, "out of memory while allocating zone config");
		return;
	}
	memset(this_zone, 0, sizeof(conf_zone_t));
	this_zone->enable_checks = -1; // Default policy applies
	this_zone->notify_timeout = -1; // Default policy applies
	this_zone->notify_retries = 0; // Default policy applies
	this_zone->dbsync_timeout = -1; // Default policy applies
	this_zone->disable_any = -1; // Default policy applies
	this_zone->build_diffs = -1; // Default policy applies
	this_zone->sig_lifetime = -1; // Default policy applies

336
	// Append mising dot to ensure FQDN
337
	size_t nlen = strlen(name);
338 339 340 341 342 343 344 345 346 347 348
	if (name[nlen - 1] != '.') {
		this_zone->name = malloc(nlen + 2);
		if (this_zone->name != NULL) {
			memcpy(this_zone->name, name, nlen);
			this_zone->name[nlen] = '.';
			this_zone->name[++nlen] = '\0';
		}
		free(name);
	} else {
		this_zone->name = name; /* Already FQDN */
	}
349 350

	// DNSSEC configuration
351
	this_zone->dnssec_enable = -1;
352 353 354 355 356 357 358 359 360 361 362

	/* Initialize ACL lists. */
	init_list(&this_zone->acl.xfr_in);
	init_list(&this_zone->acl.xfr_out);
	init_list(&this_zone->acl.notify_in);
	init_list(&this_zone->acl.notify_out);
	init_list(&this_zone->acl.update_in);

	/* Check domain name. */
	knot_dname_t *dn = NULL;
	if (this_zone->name != NULL) {
363
		dn = knot_dname_from_str(this_zone->name);
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
	}
	if (dn == NULL) {
		free(this_zone->name);
		free(this_zone);
		this_zone = NULL;
		cf_error(scanner, "invalid zone origin");
	} else {
	/* Check for duplicates. */
	if (hattrie_tryget(new_config->names, (const char *)dn,
	                   knot_dname_size(dn)) != NULL) {
		cf_error(scanner, "zone '%s' is already present, refusing to "
		         "duplicate", this_zone->name);
		knot_dname_free(&dn);
		free(this_zone->name);
		this_zone->name = NULL;
		/* Must not free, some versions of flex might continue after
		 * error and segfault.
		 * free(this_zone); this_zone = NULL;
		 */
		return;
	}

	/* Directly discard dname, won't be needed. */
	add_tail(&new_config->zones, &this_zone->n);
	*hattrie_get(new_config->names, (const char *)dn,
	             knot_dname_size(dn)) = (void *)1;
	++new_config->zones_count;
	knot_dname_free(&dn);
	}
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
/*! \brief Replace string value. */
static void opt_replace(char **opt, char *new_opt, bool val)
{
	/* Clear old value. */
	free(*opt);
	*opt = NULL;
	/* Replace if val is True. */
	if (val) {
		*opt = new_opt;
	} else {
		free(new_opt);
	}
}

/*! \brief Generate automatic defaults for server identity, version and NSID. */
static void ident_auto(int tok, conf_t *conf, bool val)
{
	switch(tok) {
	case SVERSION:
		opt_replace(&conf->version, strdup("Knot DNS " PACKAGE_VERSION), val);
		break;
	case IDENTITY:
		opt_replace(&conf->identity, sockaddr_hostname(), val);
		break;
	case NSID:
		opt_replace(&conf->nsid, sockaddr_hostname(), val);
		if (conf->nsid) {
			conf->nsid_len = strlen(conf->nsid);
		}
		break;
	default:
		break;
	}
}

430 431
%}

432
%pure-parser
433 434
%parse-param{void *scanner}
%lex-param{void *scanner}
435
%name-prefix = "cf_"
436

437
%union {
438 439 440 441 442 443
	struct {
		char *t;
		long i;
		size_t l;
		knot_tsig_algorithm_t alg;
	} tok;
444 445 446
}

%token END INVALID_TOKEN
447
%token <tok> TEXT
448
%token <tok> HEXSTR
449 450
%token <tok> NUM
%token <tok> INTERVAL
451
%token <tok> SIZE
452 453
%token <tok> BOOL

454
%token <tok> SYSTEM IDENTITY HOSTNAME SVERSION NSID KEY KEYS
455
%token <tok> MAX_UDP_PAYLOAD
456
%token <tok> TSIG_ALGO_NAME
457
%token <tok> WORKERS
458
%token <tok> USER
459
%token <tok> RUNDIR
460
%token <tok> PIDFILE
461 462

%token <tok> REMOTES
463 464
%token <tok> GROUPS

465
%token <tok> ZONES FILENAME
466
%token <tok> DISABLE_ANY
467 468 469 470 471 472 473
%token <tok> SEMANTIC_CHECKS
%token <tok> NOTIFY_RETRIES
%token <tok> NOTIFY_TIMEOUT
%token <tok> DBSYNC_TIMEOUT
%token <tok> IXFR_FSLIMIT
%token <tok> XFR_IN
%token <tok> XFR_OUT
474
%token <tok> UPDATE_IN
475 476
%token <tok> NOTIFY_IN
%token <tok> NOTIFY_OUT
477
%token <tok> BUILD_DIFFS
478 479 480
%token <tok> MAX_CONN_IDLE
%token <tok> MAX_CONN_HS
%token <tok> MAX_CONN_REPLY
481 482 483
%token <tok> RATE_LIMIT
%token <tok> RATE_LIMIT_SIZE
%token <tok> RATE_LIMIT_SLIP
484
%token <tok> TRANSFERS
485
%token <TOK> STORAGE
486 487
%token <tok> DNSSEC_ENABLE
%token <tok> DNSSEC_KEYDIR
488
%token <tok> SIGNATURE_LIFETIME
489 490
%token <tok> SERIAL_POLICY
%token <tok> SERIAL_POLICY_VAL
491 492 493 494

%token <tok> INTERFACES ADDRESS PORT
%token <tok> IPA
%token <tok> IPA6
495
%token <tok> VIA
496

497 498
%token <tok> CONTROL ALLOW LISTEN_ON

499 500 501 502
%token <tok> LOG
%token <tok> LOG_DEST
%token <tok> LOG_SRC
%token <tok> LOG_LEVEL
503

504 505 506 507 508 509 510 511 512
%%

config: conf_entries END { return 0; } ;

conf_entries:
 /* EMPTY */
 | conf_entries conf
 ;

513
interface_start:
514 515 516 517 518
 | TEXT { conf_start_iface(scanner, $1.t); }
 | REMOTES  { conf_start_iface(scanner, strdup($1.t)); } /* Allow strings reserved by token. */
 | LOG_SRC  { conf_start_iface(scanner, strdup($1.t)); }
 | LOG  { conf_start_iface(scanner, strdup($1.t)); }
 | LOG_LEVEL  { conf_start_iface(scanner, strdup($1.t)); }
519
 | CONTROL    { conf_start_iface(scanner, strdup($1.t)); }
520 521 522
 ;

interface:
523
 | interface PORT NUM ';' {
524
     if (this_iface->port > 0) {
525 526
       cf_error(scanner, "only one port definition is allowed in interface section\n");
     } else {
527
       SET_UINT16(this_iface->port, $3.i, "port");
528 529
     }
   }
530
 | interface ADDRESS IPA ';' {
531 532 533 534 535 536
     if (this_iface->address != 0) {
       cf_error(scanner, "only one address is allowed in interface section\n");
     } else {
       this_iface->address = $3.t;
       this_iface->family = AF_INET;
     }
537
   }
538
 | interface ADDRESS IPA '@' NUM ';' {
539 540 541 542 543
     if (this_iface->address != 0) {
       cf_error(scanner, "only one address is allowed in interface section\n");
     } else {
       this_iface->address = $3.t;
       this_iface->family = AF_INET;
544
       if (this_iface->port > 0) {
545 546
	 cf_error(scanner, "only one port definition is allowed in interface section\n");
       } else {
547
         SET_UINT16(this_iface->port, $5.i, "port");
548 549
       }
     }
550
   }
551
 | interface ADDRESS IPA6 ';' {
552 553 554 555 556 557
     if (this_iface->address != 0) {
       cf_error(scanner, "only one address is allowed in interface section\n");
     } else {
       this_iface->address = $3.t;
       this_iface->family = AF_INET6;
     }
558 559
   }
 | interface ADDRESS IPA6 '@' NUM ';' {
560 561 562 563 564
     if (this_iface->address != 0) {
       cf_error(scanner, "only one address is allowed in interface section\n");
     } else {
       this_iface->address = $3.t;
       this_iface->family = AF_INET6;
565
       if (this_iface->port > 0) {
566
          cf_error(scanner, "only one port definition is allowed in interface section\n");
567
       } else {
568
          SET_UINT16(this_iface->port, $5.i, "port");
569 570
       }
     }
571
   }
572 573 574 575
 ;

interfaces:
   INTERFACES '{'
576
 | interfaces interface_start '{' interface '}' {
577
   if (this_iface->address == 0) {
578
     cf_error(scanner, "interface '%s' has no defined address", this_iface->name);
579 580
   }
 }
581 582 583 584
 ;

system:
   SYSTEM '{'
Marek Vavrusa's avatar
Marek Vavrusa committed
585
 | system SVERSION TEXT ';' { new_config->version = $3.t; }
586
 | system SVERSION BOOL ';' { ident_auto(SVERSION, new_config, $3.i); }
587
 | system IDENTITY TEXT ';' { new_config->identity = $3.t; }
588 589 590 591 592 593
 | system IDENTITY BOOL ';' { ident_auto(IDENTITY, new_config, $3.i); }
 | system HOSTNAME TEXT ';' {
     fprintf(stderr, "warning: Config option 'system.hostname' is deprecated. "
                     "Use 'system.identity' instead.\n");
     free($3.t);
 }
594
 | system NSID HEXSTR ';' { new_config->nsid = $3.t; new_config->nsid_len = $3.l; }
595
 | system NSID TEXT ';' { new_config->nsid = $3.t; new_config->nsid_len = strlen(new_config->nsid); }
596
 | system NSID BOOL ';' { ident_auto(NSID, new_config, $3.i); }
597
 | system MAX_UDP_PAYLOAD NUM ';' {
598 599
     SET_NUM(new_config->max_udp_payload, $3.i, EDNS_MIN_UDP_PAYLOAD,
             EDNS_MAX_UDP_PAYLOAD, "max-udp-payload");
600
 }
601 602 603 604 605
 | system STORAGE TEXT ';' {
     fprintf(stderr, "warning: Config option 'system.storage' was relocated. "
                     "Use 'zones.storage' instead.\n");
     new_config->storage = $3.t;
 }
606
 | system RUNDIR TEXT ';' { new_config->rundir = $3.t; }
607
 | system PIDFILE TEXT ';' { new_config->pidfile = $3.t; }
608
 | system KEY TSIG_ALGO_NAME TEXT ';' {
609
     fprintf(stderr, "warning: Config option 'system.key' is deprecated "
610
                     "and has no effect.\n");
611 612
     free($4.t);
 }
Jan Včelák's avatar
Jan Včelák committed
613
 | system WORKERS NUM ';' {
614
     SET_NUM(new_config->workers, $3.i, 1, 255, "workers");
615
 }
616 617 618 619 620 621 622 623
 | system USER TEXT ';' {
     new_config->uid = new_config->gid = -1; // Invalidate
     char* dpos = strchr($3.t, '.'); // Find uid.gid format
     if (dpos != NULL) {
        struct group *grp = getgrnam(dpos + 1); // Skip dot
        if (grp != NULL) {
          new_config->gid = grp->gr_gid;
        } else {
624
          cf_error(scanner, "invalid group name '%s'", dpos + 1);
625 626 627 628 629 630 631
        }
        *dpos = '\0'; // Cut off
     }
     struct passwd* pwd = getpwnam($3.t);
     if (pwd != NULL) {
       new_config->uid = pwd->pw_uid;
     } else {
632
       cf_error(scanner, "invalid user name '%s'", $3.t);
633
     }
Jan Včelák's avatar
Jan Včelák committed
634

635 636
     free($3.t);
 }
637 638 639 640
 | system MAX_CONN_IDLE INTERVAL ';' {
	SET_INT(new_config->max_conn_idle, $3.i, "max-conn-idle");
 }
 | system MAX_CONN_HS INTERVAL ';' {
641
	SET_INT(new_config->max_conn_hs, $3.i, "max-conn-handshake");
642 643 644 645 646 647 648 649 650 651 652 653 654 655
 }
 | system MAX_CONN_REPLY INTERVAL ';' {
	SET_INT(new_config->max_conn_reply, $3.i, "max-conn-reply");
 }
 | system RATE_LIMIT NUM ';' {
	SET_INT(new_config->rrl, $3.i, "rate-limit");
 }
 | system RATE_LIMIT_SIZE SIZE ';' {
	SET_SIZE(new_config->rrl_size, $3.l, "rate-limit-size");
 }
 | system RATE_LIMIT_SIZE NUM ';' {
	SET_SIZE(new_config->rrl_size, $3.i, "rate-limit-size");
 }
 | system RATE_LIMIT_SLIP NUM ';' {
656
	SET_NUM(new_config->rrl_slip, $3.i, 1, RRL_SLIP_MAX, "rate-limit-slip");
657 658 659 660
 }
 | system TRANSFERS NUM ';' {
	SET_INT(new_config->xfers, $3.i, "transfers");
 }
661 662
 ;

663 664 665
keys:
   KEYS '{'
 | keys TEXT TSIG_ALGO_NAME TEXT ';' {
666
     /* Check algorithm length. */
667
     if (knot_tsig_digest_length($3.alg) == 0) {
668 669
        cf_error(scanner, "unsupported digest algorithm");
     }
Jan Včelák's avatar
Jan Včelák committed
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
     /* Normalize to FQDN */
     char *fqdn = $2.t;
     size_t fqdnl = strlen(fqdn);
     if (fqdn[fqdnl - 1] != '.') {
        fqdnl = ((fqdnl + 2)/4+1)*4; /* '.', '\0' */
        char* tmpdn = malloc(fqdnl);
	if (!tmpdn) {
	   cf_error(scanner, "out of memory when allocating string");
	   free(fqdn);
	   fqdn = NULL;
	   fqdnl = 0;
	} else {
	   strncpy(tmpdn, fqdn, fqdnl);
	   strncat(tmpdn, ".", 1);
	   free(fqdn);
	   fqdn = tmpdn;
	   fqdnl = strlen(fqdn);
	}
     }

     if (fqdn != NULL && !conf_key_exists(scanner, fqdn)) {
         knot_dname_t *dname = knot_dname_from_str(fqdn);
	 if (!dname) {
             cf_error(scanner, "key name '%s' not in valid domain name format",
                      fqdn);
696
         } else {
697
             knot_dname_to_lower(dname);
698 699
             conf_key_t *k = malloc(sizeof(conf_key_t));
             memset(k, 0, sizeof(conf_key_t));
700 701
             k->k.name = dname;
             k->k.algorithm = $3.alg;
702 703
             if (knot_binary_from_base64($4.t, &(k->k.secret)) != 0) {
                 cf_error(scanner, "invalid key secret '%s'", $4.t);
704 705
                 knot_dname_free(&dname);
                 free(k);
706 707 708 709 710
             } else {
                 add_tail(&new_config->keys, &k->n);
                 ++new_config->key_count;
             }
         }
711
     }
Jan Včelák's avatar
Jan Včelák committed
712

713
     free($4.t);
714
     free(fqdn);
715 716
}

717
remote_start:
718 719 720 721
 | TEXT { conf_start_remote(scanner, $1.t); }
 | LOG_SRC  { conf_start_remote(scanner, strdup($1.t)); }
 | LOG  { conf_start_remote(scanner, strdup($1.t)); }
 | LOG_LEVEL  { conf_start_remote(scanner, strdup($1.t)); }
722
 | CONTROL    { conf_start_remote(scanner, strdup($1.t)); }
723 724 725
 ;

remote:
726 727 728 729
 | remote PORT NUM ';' {
     if (this_remote->port != 0) {
       cf_error(scanner, "only one port definition is allowed in remote section\n");
     } else {
730
       SET_UINT16(this_remote->port, $3.i, "port");
731 732
     }
   }
733
 | remote ADDRESS IPA ';' {
734 735 736 737
     if (this_remote->address != 0) {
       cf_error(scanner, "only one address is allowed in remote section\n");
     } else {
       this_remote->address = $3.t;
738
       this_remote->prefix = IPV4_PREFIXLEN;
739 740
       this_remote->family = AF_INET;
     }
741
   }
742 743 744 745 746 747
   | remote ADDRESS IPA '/' NUM ';' {
       if (this_remote->address != 0) {
         cf_error(scanner, "only one address is allowed in remote section\n");
       } else {
         this_remote->address = $3.t;
         this_remote->family = AF_INET;
748
         SET_NUM(this_remote->prefix, $5.i, 0, IPV4_PREFIXLEN, "prefix length");
749 750
       }
     }
751
 | remote ADDRESS IPA '@' NUM ';' {
752 753 754 755 756
     if (this_remote->address != 0) {
       cf_error(scanner, "only one address is allowed in remote section\n");
     } else {
       this_remote->address = $3.t;
       this_remote->family = AF_INET;
757
       this_remote->prefix = IPV4_PREFIXLEN;
758 759 760
       if (this_remote->port != 0) {
	 cf_error(scanner, "only one port definition is allowed in remote section\n");
       } else {
761
         SET_UINT16(this_remote->port, $5.i, "port");
762 763
       }
     }
764 765
   }
 | remote ADDRESS IPA6 ';' {
766 767 768 769 770
     if (this_remote->address != 0) {
       cf_error(scanner, "only one address is allowed in remote section\n");
     } else {
       this_remote->address = $3.t;
       this_remote->family = AF_INET6;
771
       this_remote->prefix = IPV6_PREFIXLEN;
772
     }
773
   }
774 775 776 777 778 779
   | remote ADDRESS IPA6 '/' NUM ';' {
       if (this_remote->address != 0) {
         cf_error(scanner, "only one address is allowed in remote section\n");
       } else {
         this_remote->address = $3.t;
         this_remote->family = AF_INET6;
780
         SET_NUM(this_remote->prefix, $5.i, 0, IPV6_PREFIXLEN, "prefix length");
781 782
       }
     }
783
 | remote ADDRESS IPA6 '@' NUM ';' {
784 785 786 787 788
     if (this_remote->address != 0) {
       cf_error(scanner, "only one address is allowed in remote section\n");
     } else {
       this_remote->address = $3.t;
       this_remote->family = AF_INET6;
789
       this_remote->prefix = IPV6_PREFIXLEN;
790 791 792
       if (this_remote->port != 0) {
	 cf_error(scanner, "only one port definition is allowed in remote section\n");
       } else {
793
         SET_UINT16(this_remote->port, $5.i, "port");
794 795
       }
     }
796
   }
797 798 799 800 801 802
 | remote KEY TEXT ';' {
     if (this_remote->key != 0) {
       cf_error(scanner, "only one TSIG key definition is allowed in remote section\n");
     } else {
        conf_key_add(scanner, &this_remote->key, $3.t);
     }
803
     free($3.t);
804
   }
805 806 807 808 809 810 811 812
 | remote VIA IPA ';' {
     sockaddr_set(&this_remote->via, AF_INET, $3.t, 0);
     free($3.t);
   }
 | remote VIA IPA6 ';' {
     sockaddr_set(&this_remote->via, AF_INET6, $3.t, 0);
     free($3.t);
   }
813 814
 | remote VIA TEXT ';' {
     conf_remote_set_via(scanner, $3.t);
815
     free($3.t);
816
   }
817 818 819 820
 ;

remotes:
   REMOTES '{'
821
 | remotes remote_start '{' remote '}' {
822
     if (this_remote->address == 0) {
823
       cf_error(scanner, "remote '%s' has no defined address", this_remote->name);
824 825
     }
   }
826 827
 ;

828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
group_member:
 TEXT { conf_add_member_into_group(scanner, $1.t); }
 ;

group:
 /* empty */
 | group_member
 | group ',' group_member
 ;

group_start:
 TEXT { conf_start_group(scanner, $1.t); }
 ;

groups:
   GROUPS '{'
 | groups group_start '{' group '}'
 ;

847 848 849 850 851 852 853 854 855 856 857 858 859
zone_acl_start:
   XFR_IN {
      this_list = &this_zone->acl.xfr_in;
   }
 | XFR_OUT {
      this_list = &this_zone->acl.xfr_out;
   }
 | NOTIFY_IN {
      this_list = &this_zone->acl.notify_in;
   }
 | NOTIFY_OUT {
      this_list = &this_zone->acl.notify_out;
   }
860 861 862
 | UPDATE_IN {
      this_list = &this_zone->acl.update_in;
 }
863 864
 ;

865
zone_acl_item:
866 867 868 869
 | TEXT { conf_acl_item(scanner, $1.t); }
 | LOG_SRC  { conf_acl_item(scanner, strdup($1.t)); }
 | LOG  { conf_acl_item(scanner, strdup($1.t)); }
 | LOG_LEVEL  { conf_acl_item(scanner, strdup($1.t)); }
870
 | CONTROL    { conf_acl_item(scanner, strdup($1.t)); }
871 872 873 874 875 876 877
 ;

zone_acl_list:
 | zone_acl_list zone_acl_item ','
 | zone_acl_list zone_acl_item ';'
 ;

878 879 880
zone_acl:
 | zone_acl TEXT ';' {
      /* Find existing node in remotes. */
881
      node_t* r = 0; conf_iface_t* found = 0;
882
      WALK_LIST (r, new_config->remotes) {
883
	 if (strcmp(((conf_iface_t*)r)->name, $2.t) == 0) {
884 885 886 887 888 889 890
	    found = (conf_iface_t*)r;
	    break;
	 }
      }

      /* Append to list if found. */
      if (!found) {
891
	 cf_error(scanner, "remote '%s' is not defined", $2.t);
892 893 894
      } else {
	 conf_remote_t *remote = malloc(sizeof(conf_remote_t));
	 if (!remote) {
895
	    cf_error(scanner, "out of memory");
896 897 898 899 900 901 902
	 } else {
	    remote->remote = found;
	    add_tail(this_list, &remote->n);
	 }
      }

      /* Free text token. */
903
      free($2.t);
904 905 906
   }
 ;

907 908 909 910 911 912
zone_start:
 | USER  { conf_zone_start(scanner, strdup($1.t)); }
 | REMOTES { conf_zone_start(scanner, strdup($1.t)); }
 | LOG_SRC { conf_zone_start(scanner, strdup($1.t)); }
 | LOG { conf_zone_start(scanner, strdup($1.t)); }
 | LOG_LEVEL { conf_zone_start(scanner, strdup($1.t)); }
913
 | CONTROL    { conf_zone_start(scanner, strdup($1.t)); }
914
 | NUM '/' TEXT {
915
    unsigned prefix_len = 0;
916
    SET_NUM(prefix_len, $1.i, 0, 255, "origin prefix length");
917 918 919 920 921 922
    size_t len = 3 + 1 + strlen($3.t) + 1; /* <0,255> '/' rest */
    char *name = malloc(len * sizeof(char));
    if (name == NULL) {
        cf_error(scanner, "out of memory");
    } else {
        name[0] = '\0';
923
        if (snprintf(name, len, "%u/%s", prefix_len, $3.t) < 0) {
924 925 926 927 928 929 930
            cf_error(scanner,"failed to convert rfc2317 origin to string");
        }
    }
    free($3.t);
    conf_zone_start(scanner, name);
 }
 | TEXT  { conf_zone_start(scanner, $1.t); }
931 932 933 934
 ;

zone:
   zone_start '{'
935 936
 | zone zone_acl_start '{' zone_acl '}'
 | zone zone_acl_start zone_acl_list
937
 | zone FILENAME TEXT ';' { this_zone->file = $3.t; }
938
 | zone BUILD_DIFFS BOOL ';' { this_zone->build_diffs = $3.i; }
939
 | zone SEMANTIC_CHECKS BOOL ';' { this_zone->enable_checks = $3.i; }
940 941
 | zone STORAGE TEXT ';' { this_zone->storage = $3.t; }
 | zone DNSSEC_KEYDIR TEXT ';' { this_zone->dnssec_keydir = $3.t; }
942
 | zone DISABLE_ANY BOOL ';' { this_zone->disable_any = $3.i; }
943 944 945 946 947 948 949 950 951 952 953 954
 | zone DBSYNC_TIMEOUT NUM ';' {
	SET_INT(this_zone->dbsync_timeout, $3.i, "zonefile-sync");
 }
 | zone DBSYNC_TIMEOUT INTERVAL ';' {
	SET_INT(this_zone->dbsync_timeout, $3.i, "zonefile-sync");
 }
 | zone IXFR_FSLIMIT SIZE ';' {
	SET_SIZE(new_config->ixfr_fslimit, $3.l, "ixfr-fslimit");
 }
 | zone IXFR_FSLIMIT NUM ';' {
	SET_SIZE(this_zone->ixfr_fslimit, $3.i, "ixfr-fslimit");
 }
955
 | zone NOTIFY_RETRIES NUM ';' {
956
	SET_NUM(this_zone->notify_retries, $3.i, 1, INT_MAX, "notify-retries");
957 958
   }
 | zone NOTIFY_TIMEOUT NUM ';' {
959
	SET_NUM(this_zone->notify_timeout, $3.i, 1, INT_MAX, "notify-timeout");
960
   }
961
 | zone DNSSEC_ENABLE BOOL ';' { this_zone->dnssec_enable = $3.i; }
962
 | zone SIGNATURE_LIFETIME NUM ';' {
963
	SET_NUM(this_zone->sig_lifetime, $3.i, 10800, INT_MAX, "signature-lifetime");
964 965
 }
 | zone SIGNATURE_LIFETIME INTERVAL ';' {
966
	SET_NUM(this_zone->sig_lifetime, $3.i, 10800, INT_MAX, "signature-lifetime");
967
 }
968 969 970
 | zone SERIAL_POLICY SERIAL_POLICY_VAL ';' {
	this_zone->serial_policy = $3.i;
 }
971 972
 ;

973 974 975
zones:
   ZONES '{'
 | zones zone '}'
976
 | zones DISABLE_ANY BOOL ';' { new_config->disable_any = $3.i; }
977
 | zones BUILD_DIFFS BOOL ';' { new_config->build_diffs = $3.i; }
978
 | zones SEMANTIC_CHECKS BOOL ';' { new_config->zone_checks = $3.i; }
979 980 981 982 983 984
 | zones IXFR_FSLIMIT SIZE ';' {
	SET_SIZE(new_config->ixfr_fslimit, $3.l, "ixfr-fslimit");
 }
 | zones IXFR_FSLIMIT NUM ';' {
	SET_SIZE(new_config->ixfr_fslimit, $3.i, "ixfr-fslimit");
 }
985
 | zones NOTIFY_RETRIES NUM ';' {
986
	SET_NUM(new_config->notify_retries, $3.i, 1, INT_MAX, "notify-retries");
987 988
   }
 | zones NOTIFY_TIMEOUT NUM ';' {
989
	SET_NUM(new_config->notify_timeout, $3.i, 1, INT_MAX, "notify-timeout");
990
   }
991
 | zones DBSYNC_TIMEOUT NUM ';' {
992
	SET_NUM(new_config->dbsync_timeout, $3.i, 0, INT_MAX, "zonefile-sync");
993 994
 }
 | zones DBSYNC_TIMEOUT INTERVAL ';' {
995
	SET_NUM(new_config->dbsync_timeout, $3.i, 0, INT_MAX, "zonefile-sync");
996
 }
997
 | zones STORAGE TEXT ';' { new_config->storage = $3.t; }
998
 | zones DNSSEC_ENABLE BOOL ';' { new_config->dnssec_enable = $3.i; }
999
 | zones DNSSEC_KEYDIR TEXT ';' { new_config->dnssec_keydir = $3.t; }
1000
 | zones SIGNATURE_LIFETIME NUM ';' {
1001
	SET_NUM(new_config->sig_lifetime, $3.i, 10800, INT_MAX, "signature-lifetime");
1002 1003
 }
 | zones SIGNATURE_LIFETIME INTERVAL ';' {
1004
	SET_NUM(new_config->sig_lifetime, $3.i, 10800, INT_MAX, "signature-lifetime");
1005
 }
1006 1007 1008
 | zones SERIAL_POLICY SERIAL_POLICY_VAL ';' {
	new_config->serial_policy = $3.i;
 }
1009 1010
 ;

1011
log_prios_start: {
1012 1013
  this_logmap = malloc(sizeof(conf_log_map_t));
  this_logmap->source = 0;
1014
  this_logmap->prios = 0;
1015 1016 1017 1018
  add_tail(&this_log->map, &this_logmap->n);
}
;

1019 1020
log_prios:
   log_prios_start
1021 1022
 | log_prios LOG_LEVEL ',' { this_logmap->prios |= $2.i; }
 | log_prios LOG_LEVEL ';' { this_logmap->prios |= $2.i; }
1023 1024
 ;

1025
log_src:
1026
 | log_src LOG_SRC log_prios {
1027
     this_logmap->source = $2.i;
1028 1029
     this_logmap = 0;
   }
1030 1031
 ;

1032
log_dest: LOG_DEST {
1033 1034
  /* Find already existing rule. */
  this_log = 0;
1035
  node_t *n = 0;
1036 1037
  WALK_LIST(n, new_config->logs) {
    conf_log_t* log = (conf_log_t*)n;
1038
    if (log->type == $1.i) {
1039 1040 1041 1042 1043 1044 1045
      this_log = log;
      break;
    }
  }

  if (!this_log) {
    this_log = malloc(sizeof(conf_log_t));
1046
    this_log->type = $1.i;
1047 1048 1049 1050 1051
    this_log->file = 0;
    init_list(&this_log->map);
    add_tail(&new_config->logs, &this_log->n);
    ++new_config->logs_count;
  }
1052 1053 1054 1055
}
;

log_file: FILENAME TEXT {
1056 1057
  /* Find already existing rule. */
  this_log = 0;
1058
  node_t *n = 0;
1059 1060 1061
  WALK_LIST(n, new_config->logs) {
    conf_log_t* log = (conf_log_t*)n;
    if (log->type == LOGT_FILE) {
1062
      if (strcmp($2.t, log->file) == 0) {
1063
        this_log = log;
1064
	free($2.t);
1065 1066 1067 1068 1069 1070 1071 1072 1073
        break;
      }
    }
  }

  /* Create new rule. */
  if (!this_log) {
    this_log = malloc(sizeof(conf_log_t));
    this_log->type = LOGT_FILE;
1074
    this_log->file = strcpath($2.t);
1075 1076 1077 1078
    init_list(&this_log->map);
    add_tail(&new_config->logs, &this_log->n);
    ++new_config->logs_count;
  }
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
}
;

log_end: {
}
;

log_start:
 | log_start log_dest '{' log_src '}'
 | log_start log_file '{' log_src '}'
1089
 ;
1090

1091 1092
log: LOG { new_config->logs_count = 0; } '{' log_start log_end
 ;
1093

1094
ctl_listen_start:
1095
  LISTEN_ON { conf_init_iface(scanner, NULL, -1); }
1096 1097 1098 1099 1100 1101 1102 1103 1104
  ;

ctl_allow_start:
  ALLOW {
    this_list = &new_config->ctl.allow;
  }
  ;

control:
1105
   CONTROL '{' { new_config->ctl.have = true; }
1106 1107 1108 1109 1110 1111 1112
 | control ctl_listen_start '{' interface '}' {
     if (this_iface->address == 0) {
       cf_error(scanner, "control interface has no defined address");
     } else {
       new_config->ctl.iface = this_iface;
     }
 }
1113 1114 1115 1116 1117 1118
 | control ctl_listen_start TEXT ';' {
     this_iface->address = $3.t;
     this_iface->family = AF_UNIX;
     this_iface->port = 0;
     new_config->ctl.iface = this_iface;
 }
1119 1120
 | control ctl_allow_start '{' zone_acl '}'
 | control ctl_allow_start zone_acl_list
1121
 ;
1122

1123
conf: ';' | system '}' | interfaces '}' | keys '}' | remotes '}' | groups '}' | zones '}' | log '}' | control '}';
1124 1125

%%