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

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

53 54 55 56 57 58 59 60 61 62 63 64 65 66
#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);

67
static void conf_init_iface(void *scanner, char* ifname, int port)
68
{
69 70 71 72 73 74 75 76
	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;
77 78 79 80
}

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

86 87 88 89 90 91 92 93 94 95 96 97
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;
}

98
static void conf_start_remote(void *scanner, char *remote)
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);
	++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
	this_zone = malloc(sizeof(conf_zone_t));
	if (this_zone == NULL || name == NULL) {
		cf_error(scanner, "out of memory while allocating zone config");
		return;
	}
327 328

	conf_init_zone(this_zone);
329

330
	// Append mising dot to ensure FQDN
331
	size_t nlen = strlen(name);
332 333 334 335 336 337 338 339 340 341 342
	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 */
	}
343

344 345 346 347 348
	// Convert zone name to lower-case.
	for (size_t i = 0; this_zone->name[i]; i++) {
		this_zone->name[i] = tolower(this_zone->name[i]);
	}

349 350 351
	/* Check domain name. */
	knot_dname_t *dn = NULL;
	if (this_zone->name != NULL) {
352
		dn = knot_dname_from_str(this_zone->name);
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
	}
	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);
	}
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
/*! \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;
	}
}

419 420
%}

421
%pure-parser
422 423
%parse-param{void *scanner}
%lex-param{void *scanner}
424
%name-prefix = "cf_"
425

426
%union {
427 428 429 430 431 432
	struct {
		char *t;
		long i;
		size_t l;
		knot_tsig_algorithm_t alg;
	} tok;
433 434 435
}

%token END INVALID_TOKEN
436
%token <tok> TEXT
437
%token <tok> HEXSTR
438 439
%token <tok> NUM
%token <tok> INTERVAL
440
%token <tok> SIZE
441 442
%token <tok> BOOL

443
%token <tok> SYSTEM IDENTITY HOSTNAME SVERSION NSID KEY KEYS
444
%token <tok> MAX_UDP_PAYLOAD
445
%token <tok> TSIG_ALGO_NAME
446
%token <tok> WORKERS
447
%token <tok> USER
448
%token <tok> RUNDIR
449
%token <tok> PIDFILE
450 451

%token <tok> REMOTES
452 453
%token <tok> GROUPS

454
%token <tok> ZONES FILENAME
455
%token <tok> DISABLE_ANY
456 457 458 459 460 461 462
%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
463
%token <tok> UPDATE_IN
464 465
%token <tok> NOTIFY_IN
%token <tok> NOTIFY_OUT
466
%token <tok> BUILD_DIFFS
467 468 469
%token <tok> MAX_CONN_IDLE
%token <tok> MAX_CONN_HS
%token <tok> MAX_CONN_REPLY
470 471 472
%token <tok> RATE_LIMIT
%token <tok> RATE_LIMIT_SIZE
%token <tok> RATE_LIMIT_SLIP
473
%token <tok> TRANSFERS
474
%token <TOK> STORAGE
475 476
%token <tok> DNSSEC_ENABLE
%token <tok> DNSSEC_KEYDIR
477
%token <tok> SIGNATURE_LIFETIME
478 479
%token <tok> SERIAL_POLICY
%token <tok> SERIAL_POLICY_VAL
480 481 482 483

%token <tok> INTERFACES ADDRESS PORT
%token <tok> IPA
%token <tok> IPA6
484
%token <tok> VIA
485

486 487
%token <tok> CONTROL ALLOW LISTEN_ON

488 489 490 491
%token <tok> LOG
%token <tok> LOG_DEST
%token <tok> LOG_SRC
%token <tok> LOG_LEVEL
492

493 494 495 496 497 498 499 500 501
%%

config: conf_entries END { return 0; } ;

conf_entries:
 /* EMPTY */
 | conf_entries conf
 ;

502
interface_start:
503 504 505 506 507
 | 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)); }
508
 | CONTROL    { conf_start_iface(scanner, strdup($1.t)); }
509 510 511
 ;

interface:
512
 | interface PORT NUM ';' {
513
     if (this_iface->port > 0) {
514 515
       cf_error(scanner, "only one port definition is allowed in interface section\n");
     } else {
516
       SET_UINT16(this_iface->port, $3.i, "port");
517 518
     }
   }
519
 | interface ADDRESS IPA ';' {
520 521 522 523 524 525
     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;
     }
526
   }
527
 | interface ADDRESS IPA '@' NUM ';' {
528 529 530 531 532
     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;
533
       if (this_iface->port > 0) {
534 535
	 cf_error(scanner, "only one port definition is allowed in interface section\n");
       } else {
536
         SET_UINT16(this_iface->port, $5.i, "port");
537 538
       }
     }
539
   }
540
 | interface ADDRESS IPA6 ';' {
541 542 543 544 545 546
     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;
     }
547 548
   }
 | interface ADDRESS IPA6 '@' NUM ';' {
549 550 551 552 553
     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;
554
       if (this_iface->port > 0) {
555
          cf_error(scanner, "only one port definition is allowed in interface section\n");
556
       } else {
557
          SET_UINT16(this_iface->port, $5.i, "port");
558 559
       }
     }
560
   }
561 562 563 564
 ;

interfaces:
   INTERFACES '{'
565
 | interfaces interface_start '{' interface '}' {
566
   if (this_iface->address == 0) {
567
     cf_error(scanner, "interface '%s' has no defined address", this_iface->name);
568 569
   }
 }
570 571 572 573
 ;

system:
   SYSTEM '{'
Marek Vavrusa's avatar
Marek Vavrusa committed
574
 | system SVERSION TEXT ';' { new_config->version = $3.t; }
575
 | system SVERSION BOOL ';' { ident_auto(SVERSION, new_config, $3.i); }
576
 | system IDENTITY TEXT ';' { new_config->identity = $3.t; }
577 578 579 580 581 582
 | 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);
 }
583
 | system NSID HEXSTR ';' { new_config->nsid = $3.t; new_config->nsid_len = $3.l; }
584
 | system NSID TEXT ';' { new_config->nsid = $3.t; new_config->nsid_len = strlen(new_config->nsid); }
585
 | system NSID BOOL ';' { ident_auto(NSID, new_config, $3.i); }
586
 | system MAX_UDP_PAYLOAD NUM ';' {
587 588
     SET_NUM(new_config->max_udp_payload, $3.i, EDNS_MIN_UDP_PAYLOAD,
             EDNS_MAX_UDP_PAYLOAD, "max-udp-payload");
589
 }
590 591 592 593 594
 | system STORAGE TEXT ';' {
     fprintf(stderr, "warning: Config option 'system.storage' was relocated. "
                     "Use 'zones.storage' instead.\n");
     new_config->storage = $3.t;
 }
595
 | system RUNDIR TEXT ';' { new_config->rundir = $3.t; }
596
 | system PIDFILE TEXT ';' { new_config->pidfile = $3.t; }
597
 | system KEY TSIG_ALGO_NAME TEXT ';' {
598
     fprintf(stderr, "warning: Config option 'system.key' is deprecated "
599
                     "and has no effect.\n");
600 601
     free($4.t);
 }
Jan Včelák's avatar
Jan Včelák committed
602
 | system WORKERS NUM ';' {
603
     SET_NUM(new_config->workers, $3.i, 1, 255, "workers");
604
 }
605 606 607 608 609 610 611 612
 | 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 {
613
          cf_error(scanner, "invalid group name '%s'", dpos + 1);
614 615 616 617 618 619 620
        }
        *dpos = '\0'; // Cut off
     }
     struct passwd* pwd = getpwnam($3.t);
     if (pwd != NULL) {
       new_config->uid = pwd->pw_uid;
     } else {
621
       cf_error(scanner, "invalid user name '%s'", $3.t);
622
     }
Jan Včelák's avatar
Jan Včelák committed
623

624 625
     free($3.t);
 }
626 627 628 629
 | system MAX_CONN_IDLE INTERVAL ';' {
	SET_INT(new_config->max_conn_idle, $3.i, "max-conn-idle");
 }
 | system MAX_CONN_HS INTERVAL ';' {
630
	SET_INT(new_config->max_conn_hs, $3.i, "max-conn-handshake");
631 632 633 634 635 636 637 638 639 640 641 642 643 644
 }
 | 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 ';' {
645
	SET_NUM(new_config->rrl_slip, $3.i, 1, RRL_SLIP_MAX, "rate-limit-slip");
646 647 648 649
 }
 | system TRANSFERS NUM ';' {
	SET_INT(new_config->xfers, $3.i, "transfers");
 }
650 651
 ;

652 653 654
keys:
   KEYS '{'
 | keys TEXT TSIG_ALGO_NAME TEXT ';' {
655
     /* Check algorithm length. */
656
     if (knot_tsig_digest_length($3.alg) == 0) {
657 658
        cf_error(scanner, "unsupported digest algorithm");
     }
Jan Včelák's avatar
Jan Včelák committed
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
     /* 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);
685
         } else {
686
             knot_dname_to_lower(dname);
687 688
             conf_key_t *k = malloc(sizeof(conf_key_t));
             memset(k, 0, sizeof(conf_key_t));
689 690
             k->k.name = dname;
             k->k.algorithm = $3.alg;
691 692
             if (knot_binary_from_base64($4.t, &(k->k.secret)) != 0) {
                 cf_error(scanner, "invalid key secret '%s'", $4.t);
693 694
                 knot_dname_free(&dname);
                 free(k);
695 696 697 698 699
             } else {
                 add_tail(&new_config->keys, &k->n);
                 ++new_config->key_count;
             }
         }
700
     }
Jan Včelák's avatar
Jan Včelák committed
701

702
     free($4.t);
703
     free(fqdn);
704 705
}

706
remote_start:
707 708 709 710
 | 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)); }
711
 | CONTROL    { conf_start_remote(scanner, strdup($1.t)); }
712 713 714
 ;

remote:
715 716 717 718
 | remote PORT NUM ';' {
     if (this_remote->port != 0) {
       cf_error(scanner, "only one port definition is allowed in remote section\n");
     } else {
719
       SET_UINT16(this_remote->port, $3.i, "port");
720 721
     }
   }
722
 | remote ADDRESS IPA ';' {
723 724 725 726
     if (this_remote->address != 0) {
       cf_error(scanner, "only one address is allowed in remote section\n");
     } else {
       this_remote->address = $3.t;
727
       this_remote->prefix = IPV4_PREFIXLEN;
728 729
       this_remote->family = AF_INET;
     }
730
   }
731 732 733 734 735 736
   | 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;
737
         SET_NUM(this_remote->prefix, $5.i, 0, IPV4_PREFIXLEN, "prefix length");
738 739
       }
     }
740
 | remote ADDRESS IPA '@' NUM ';' {
741 742 743 744 745
     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;
746
       this_remote->prefix = IPV4_PREFIXLEN;
747 748 749
       if (this_remote->port != 0) {
	 cf_error(scanner, "only one port definition is allowed in remote section\n");
       } else {
750
         SET_UINT16(this_remote->port, $5.i, "port");
751 752
       }
     }
753 754
   }
 | remote ADDRESS IPA6 ';' {
755 756 757 758 759
     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;
760
       this_remote->prefix = IPV6_PREFIXLEN;
761
     }
762
   }
763 764 765 766 767 768
   | 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;
769
         SET_NUM(this_remote->prefix, $5.i, 0, IPV6_PREFIXLEN, "prefix length");
770 771
       }
     }
772
 | remote ADDRESS IPA6 '@' NUM ';' {
773 774 775 776 777
     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;
778
       this_remote->prefix = IPV6_PREFIXLEN;
779 780 781
       if (this_remote->port != 0) {
	 cf_error(scanner, "only one port definition is allowed in remote section\n");
       } else {
782
         SET_UINT16(this_remote->port, $5.i, "port");
783 784
       }
     }
785
   }
786 787 788 789 790 791
 | 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);
     }
792
     free($3.t);
793
   }
794 795 796 797 798 799 800 801
 | 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);
   }
802 803
 | remote VIA TEXT ';' {
     conf_remote_set_via(scanner, $3.t);
804
     free($3.t);
805
   }
806 807 808 809
 ;

remotes:
   REMOTES '{'
810
 | remotes remote_start '{' remote '}' {
811
     if (this_remote->address == 0) {
812
       cf_error(scanner, "remote '%s' has no defined address", this_remote->name);
813 814
     }
   }
815 816
 ;

817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
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 '}'
 ;

836 837 838 839 840 841 842 843 844 845 846 847 848
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;
   }
849 850 851
 | UPDATE_IN {
      this_list = &this_zone->acl.update_in;
 }
852 853
 ;

854
zone_acl_item:
855 856 857 858
 | 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)); }
859
 | CONTROL    { conf_acl_item(scanner, strdup($1.t)); }
860 861 862 863 864 865 866
 ;

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

867 868 869
zone_acl:
 | zone_acl TEXT ';' {
      /* Find existing node in remotes. */
870
      node_t* r = 0; conf_iface_t* found = 0;
871
      WALK_LIST (r, new_config->remotes) {
872
	 if (strcmp(((conf_iface_t*)r)->name, $2.t) == 0) {
873 874 875 876 877 878 879
	    found = (conf_iface_t*)r;
	    break;
	 }
      }

      /* Append to list if found. */
      if (!found) {
880
	 cf_error(scanner, "remote '%s' is not defined", $2.t);
881 882 883
      } else {
	 conf_remote_t *remote = malloc(sizeof(conf_remote_t));
	 if (!remote) {
884
	    cf_error(scanner, "out of memory");
885 886 887 888 889 890 891
	 } else {
	    remote->remote = found;
	    add_tail(this_list, &remote->n);
	 }
      }

      /* Free text token. */
892
      free($2.t);
893 894 895
   }
 ;

896 897 898 899 900 901
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)); }
902
 | CONTROL    { conf_zone_start(scanner, strdup($1.t)); }
903
 | NUM '/' TEXT {
904
    unsigned prefix_len = 0;
905
    SET_NUM(prefix_len, $1.i, 0, 255, "origin prefix length");
906 907 908 909 910 911
    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';
912
        if (snprintf(name, len, "%u/%s", prefix_len, $3.t) < 0) {
913 914 915 916 917 918 919
            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); }
920 921 922 923
 ;

zone:
   zone_start '{'
924 925
 | zone zone_acl_start '{' zone_acl '}'
 | zone zone_acl_start zone_acl_list
926
 | zone FILENAME TEXT ';' { this_zone->file = $3.t; }
927
 | zone BUILD_DIFFS BOOL ';' { this_zone->build_diffs = $3.i; }
928
 | zone SEMANTIC_CHECKS BOOL ';' { this_zone->enable_checks = $3.i; }
929 930
 | zone STORAGE TEXT ';' { this_zone->storage = $3.t; }
 | zone DNSSEC_KEYDIR TEXT ';' { this_zone->dnssec_keydir = $3.t; }
931
 | zone DISABLE_ANY BOOL ';' { this_zone->disable_any = $3.i; }
932 933 934 935 936 937 938 939 940 941 942 943
 | 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");
 }
944
 | zone NOTIFY_RETRIES NUM ';' {
945
	SET_NUM(this_zone->notify_retries, $3.i, 1, INT_MAX, "notify-retries");
946 947
   }
 | zone NOTIFY_TIMEOUT NUM ';' {
948
	SET_NUM(this_zone->notify_timeout, $3.i, 1, INT_MAX, "notify-timeout");
949
   }
950
 | zone DNSSEC_ENABLE BOOL ';' { this_zone->dnssec_enable = $3.i; }
951
 | zone SIGNATURE_LIFETIME NUM ';' {
952
	SET_NUM(this_zone->sig_lifetime, $3.i, 10800, INT_MAX, "signature-lifetime");
953 954
 }
 | zone SIGNATURE_LIFETIME INTERVAL ';' {
955
	SET_NUM(this_zone->sig_lifetime, $3.i, 10800, INT_MAX, "signature-lifetime");
956
 }
957 958 959
 | zone SERIAL_POLICY SERIAL_POLICY_VAL ';' {
	this_zone->serial_policy = $3.i;
 }
960 961
 ;

962 963 964
zones:
   ZONES '{'
 | zones zone '}'
965
 | zones DISABLE_ANY BOOL ';' { new_config->disable_any = $3.i; }
966
 | zones BUILD_DIFFS BOOL ';' { new_config->build_diffs = $3.i; }
967
 | zones SEMANTIC_CHECKS BOOL ';' { new_config->zone_checks = $3.i; }
968 969 970 971 972 973
 | 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");
 }
974
 | zones NOTIFY_RETRIES NUM ';' {
975
	SET_NUM(new_config->notify_retries, $3.i, 1, INT_MAX, "notify-retries");
976 977
   }
 | zones NOTIFY_TIMEOUT NUM ';' {
978
	SET_NUM(new_config->notify_timeout, $3.i, 1, INT_MAX, "notify-timeout");
979
   }
980
 | zones DBSYNC_TIMEOUT NUM ';' {
981
	SET_NUM(new_config->dbsync_timeout, $3.i, 0, INT_MAX, "zonefile-sync");
982 983
 }
 | zones DBSYNC_TIMEOUT INTERVAL ';' {
984
	SET_NUM(new_config->dbsync_timeout, $3.i, 0, INT_MAX, "zonefile-sync");
985
 }
986
 | zones STORAGE TEXT ';' { new_config->storage = $3.t; }
987
 | zones DNSSEC_ENABLE BOOL ';' { new_config->dnssec_enable = $3.i; }
988
 | zones DNSSEC_KEYDIR TEXT ';' { new_config->dnssec_keydir = $3.t; }
989
 | zones SIGNATURE_LIFETIME NUM ';' {
990
	SET_NUM(new_config->sig_lifetime, $3.i, 10800, INT_MAX, "signature-lifetime");
991 992
 }
 | zones SIGNATURE_LIFETIME INTERVAL ';' {
993
	SET_NUM(new_config->sig_lifetime, $3.i, 10800, INT_MAX, "signature-lifetime");
994
 }
995 996 997
 | zones SERIAL_POLICY SERIAL_POLICY_VAL ';' {
	new_config->serial_policy = $3.i;
 }
998 999
 ;

1000
log_prios_start: {
1001 1002
  this_logmap = malloc(sizeof(conf_log_map_t));
  this_logmap->source = 0;
1003
  this_logmap->prios = 0;
1004 1005 1006 1007
  add_tail(&this_log->map, &this_logmap->n);
}
;

1008 1009
log_prios:
   log_prios_start
1010 1011
 | log_prios LOG_LEVEL ',' { this_logmap->prios |= $2.i; }
 | log_prios LOG_LEVEL ';' { this_logmap->prios |= $2.i; }
1012 1013
 ;

1014
log_src:
1015
 | log_src LOG_SRC log_prios {
1016
     this_logmap->source = $2.i;
1017 1018
     this_logmap = 0;
   }
1019 1020
 ;

1021
log_dest: LOG_DEST {
1022 1023
  /* Find already existing rule. */
  this_log = 0;
1024
  node_t *n = 0;
1025 1026
  WALK_LIST(n, new_config->logs) {
    conf_log_t* log = (conf_log_t*)n;
1027
    if (log->type == $1.i) {
1028 1029 1030 1031 1032 1033 1034
      this_log = log;
      break;
    }
  }

  if (!this_log) {
    this_log = malloc(sizeof(conf_log_t));
1035
    this_log->type = $1.i;
1036 1037 1038 1039 1040
    this_log->file = 0;
    init_list(&this_log->map);
    add_tail(&new_config->logs, &this_log->n);
    ++new_config->logs_count;
  }
1041 1042 1043 1044
}
;

log_file: FILENAME TEXT {
1045 1046
  /* Find already existing rule. */
  this_log = 0;
1047
  node_t *n = 0;
1048 1049 1050
  WALK_LIST(n, new_config->logs) {
    conf_log_t* log = (conf_log_t*)n;
    if (log->type == LOGT_FILE) {
1051
      if (strcmp($2.t, log->file) == 0) {
1052
        this_log = log;
1053
	free($2.t);
1054 1055 1056 1057 1058 1059 1060 1061 1062
        break;
      }
    }
  }

  /* Create new rule. */
  if (!this_log) {
    this_log = malloc(sizeof(conf_log_t));
    this_log->type = LOGT_FILE;
1063
    this_log->file = strcpath($2.t);
1064 1065 1066 1067
    init_list(&this_log->map);
    add_tail(&new_config->logs, &this_log->n);
    ++new_config->logs_count;
  }
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
}
;

log_end: {
}
;

log_start:
 | log_start log_dest '{' log_src '}'
 | log_start log_file '{' log_src '}'
1078
 ;
1079

1080 1081
log: LOG { new_config->logs_count = 0; } '{' log_start log_end
 ;
1082

1083
ctl_listen_start:
1084
  LISTEN_ON { conf_init_iface(scanner, NULL, -1); }
1085 1086 1087 1088 1089 1090 1091 1092 1093
  ;

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

control:
1094
   CONTROL '{' { new_config->ctl.have = true; }
1095 1096 1097 1098 1099 1100 1101
 | 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;
     }
 }
1102 1103 1104 1105 1106 1107
 | 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;
 }
1108 1109
 | control ctl_allow_start '{' zone_acl '}'
 | control ctl_allow_start zone_acl_list
1110
 ;
1111

1112
conf: ';' | system '}' | interfaces '}' | keys '}' | remotes '}' | groups '}' | zones '}' | log '}' | control '}';
1113 1114

%%