summaryrefslogtreecommitdiffstats
path: root/mDNSResponder/mDNSShared/dnsextd_parser.y
blob: 24d62696870d5f10c9f5da6b9ad067fab15e714c (plain) (blame)
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
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/* -*- Mode: C; tab-width: 4 -*-
 *
 * Copyright (c) 2006-2010 Apple Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mDNSEmbeddedAPI.h"
#include "DebugServices.h"
#include "dnsextd.h"

void yyerror( const char* error );
int  yylex(void);


typedef struct StringListElem
{
	char					*	string;
	struct StringListElem	*	next;
} StringListElem;


typedef struct OptionsInfo
{
	char	server_address[ 256 ];
	int		server_port;
	char	source_address[ 256 ];
	int		source_port;
	int		private_port;
	int		llq_port;
} OptionsInfo;


typedef struct ZoneInfo
{
	char	name[ 256 ];
	char	certificate_name[ 256 ];
	char	allow_clients_file[ 256 ];
	char	allow_clients[ 256 ];
	char	key[ 256 ];
} ZoneInfo;


typedef struct KeySpec
{
	char 				name[ 256 ];
	char				algorithm[ 256 ];
	char				secret[ 256 ];
	struct KeySpec	*	next;
} KeySpec;


typedef struct ZoneSpec
{
	char				name[ 256 ];
	DNSZoneSpecType		type;
	StringListElem	*	allowUpdate;
	StringListElem	*	allowQuery;
	char				key[ 256 ];
	struct ZoneSpec	*	next;
} ZoneSpec;


static StringListElem	*	g_stringList = NULL;
static StringListElem	*	g_addrList = NULL;
static KeySpec			*	g_keys;
static ZoneSpec			*	g_zones;
static ZoneSpec				g_zoneSpec;
static const char		*	g_filename;

#define YYPARSE_PARAM  context

void
SetupOptions
	(
	OptionsInfo	*	info,
	void		*	context
	);

%}

%union
{
	int			number;
	char	*	string;
}

%token	OPTIONS 
%token	LISTEN_ON 
%token	NAMESERVER
%token	PORT 
%token	ADDRESS 
%token	LLQ 
%token	PUBLIC
%token  PRIVATE
%token  ALLOWUPDATE
%token  ALLOWQUERY
%token	KEY 
%token  ALGORITHM
%token  SECRET
%token  ISSUER
%token  SERIAL
%token	ZONE
%token  TYPE
%token	ALLOW
%token	OBRACE 
%token	EBRACE 
%token	SEMICOLON
%token 	IN
%token	<string>	DOTTED_DECIMAL_ADDRESS 
%token	<string>	WILDCARD 
%token	<string>	DOMAINNAME 
%token	<string>	HOSTNAME 
%token	<string>	QUOTEDSTRING
%token	<number> 	NUMBER 

%type	<string>	addressstatement
%type	<string>	networkaddress

%%

commands:
        |        
        commands command SEMICOLON
        ;


command:
		options_set
		|
        zone_set 
		|
		key_set
        ;


options_set:
		OPTIONS optionscontent
		{
			// SetupOptions( &g_optionsInfo, context );
		}
		;

optionscontent:
		OBRACE optionsstatements EBRACE
		;

optionsstatements:
		|
		optionsstatements optionsstatement SEMICOLON
		;


optionsstatement:
		statements
		|
		LISTEN_ON addresscontent
		{
		}
		|
		LISTEN_ON PORT NUMBER addresscontent
		{
			mDNSIPPort listen_port = mDNSOpaque16fromIntVal( $3 );
			DaemonInfo* d = ( DaemonInfo* ) context;
			d->addr.sin_port = ( listen_port.NotAnInteger) ? listen_port.NotAnInteger : UnicastDNSPort.NotAnInteger;
			StringListElem* addr = g_addrList;
			while (addr != NULL)
			{
				StringListElem* next;
				// The first ipv4 address in {,} is used; the rest are ignored.
				if (inet_pton( AF_INET, addr->string, &d->addr.sin_addr ) == 0) {
					inet_pton( AF_INET, "127.0.0.1", &d->ns_addr.sin_addr );
					LogMsg("LISTEN_ON: An invalid ipv4 address, %s, detected.", addr->string);
				}
				next = addr->next;
				free(addr);
				addr = next;
			}
		}
		|
		NAMESERVER ADDRESS networkaddress
		{
		}
		|
		NAMESERVER ADDRESS networkaddress PORT NUMBER
		{
		}
		|
		PRIVATE PORT NUMBER
		{
			( ( DaemonInfo* ) context )->private_port = mDNSOpaque16fromIntVal( $3 );
		}
		|
		LLQ PORT NUMBER
		{
			( ( DaemonInfo* ) context )->llq_port = mDNSOpaque16fromIntVal( $3 );
		}
		;

key_set:
        KEY QUOTEDSTRING OBRACE SECRET QUOTEDSTRING SEMICOLON EBRACE
        {
			KeySpec	* keySpec;

			keySpec = ( KeySpec* ) malloc( sizeof( KeySpec ) );

			if ( !keySpec )
				{
				LogMsg("ERROR: memory allocation failure");
				YYABORT;
				}

			strncpy( keySpec->name, $2, sizeof( keySpec->name ) );
			strncpy( keySpec->secret, $5, sizeof( keySpec->secret ) );

			keySpec->next	= g_keys;
			g_keys			= keySpec;
        }
        ;

zone_set:
		ZONE QUOTEDSTRING zonecontent
		{
			ZoneSpec * zoneSpec;

			zoneSpec = ( ZoneSpec* ) malloc( sizeof( ZoneSpec ) );

			if ( !zoneSpec )
				{
				LogMsg("ERROR: memory allocation failure");
				YYABORT;
				}

			strncpy( zoneSpec->name, $2, sizeof( zoneSpec->name ) );
			zoneSpec->type = g_zoneSpec.type;
			strcpy( zoneSpec->key, g_zoneSpec.key );
			zoneSpec->allowUpdate = g_zoneSpec.allowUpdate;
			zoneSpec->allowQuery = g_zoneSpec.allowQuery;

			zoneSpec->next = g_zones;
			g_zones = zoneSpec;
		}
		|
		ZONE QUOTEDSTRING IN zonecontent
        {
			ZoneSpec * zoneSpec;

			zoneSpec = ( ZoneSpec* ) malloc( sizeof( ZoneSpec ) );

			if ( !zoneSpec )
				{
				LogMsg("ERROR: memory allocation failure");
				YYABORT;
				}

			strncpy( zoneSpec->name, $2, sizeof( zoneSpec->name ) );
			zoneSpec->type = g_zoneSpec.type;
			strcpy( zoneSpec->key, g_zoneSpec.key );
			zoneSpec->allowUpdate = g_zoneSpec.allowUpdate;
			zoneSpec->allowQuery = g_zoneSpec.allowQuery;

			zoneSpec->next = g_zones;
			g_zones = zoneSpec;
		}
        ;

zonecontent:
		OBRACE zonestatements EBRACE 

zonestatements:
        |
        zonestatements zonestatement SEMICOLON
        ;

zonestatement:
		TYPE PUBLIC
		{
			g_zoneSpec.type = kDNSZonePublic;
		}
		|
		TYPE PRIVATE
		{
			g_zoneSpec.type = kDNSZonePrivate;
		}
		|
		ALLOWUPDATE keycontent
		{
			g_zoneSpec.allowUpdate = g_stringList;
			g_stringList = NULL;
		}
		|
		ALLOWQUERY keycontent
		{
			g_zoneSpec.allowQuery = g_stringList;
			g_stringList = NULL;
		}
        ;

addresscontent:
		OBRACE addressstatements EBRACE
		{
		}

addressstatements:
		|
		addressstatements addressstatement SEMICOLON
		{
		}
		;

addressstatement:
		DOTTED_DECIMAL_ADDRESS
		{
			StringListElem * elem;

			elem = ( StringListElem* ) malloc( sizeof( StringListElem ) );

			if ( !elem )
			{
				LogMsg("ERROR: memory allocation failure");
				YYABORT;
			}

			elem->string = $1;

			elem->next		= g_addrList;
			g_addrList		= elem;
		}
		;


keycontent:
		OBRACE keystatements EBRACE
		{
		}

keystatements:
		|
		keystatements keystatement SEMICOLON
		{
		}
		;

keystatement:
		KEY DOMAINNAME
		{
			StringListElem * elem;

			elem = ( StringListElem* ) malloc( sizeof( StringListElem ) );

			if ( !elem )
				{
				LogMsg("ERROR: memory allocation failure");
				YYABORT;
				}

			elem->string = $2;

			elem->next		= g_stringList;
			g_stringList	= elem;
		}
		;


networkaddress:
		DOTTED_DECIMAL_ADDRESS
		|
		HOSTNAME
		|
		WILDCARD
		;

block: 
		OBRACE zonestatements EBRACE SEMICOLON
        ;

statements:
        |
		statements statement
        ;

statement:
		block
		{
			$<string>$ = NULL;
		}
		|
		QUOTEDSTRING
		{
			$<string>$ = $1;
		}
%%

int yywrap(void);

extern int yylineno;

void yyerror( const char *str )
{
        fprintf( stderr,"%s:%d: error: %s\n", g_filename, yylineno, str );
}
 
int yywrap()
{
        return 1;
} 


int
ParseConfig
	(
	DaemonInfo	*	d,
	const char	*	file
	)
	{
	extern FILE		*	yyin;
	DNSZone			*	zone;
	DomainAuthInfo	*	key;
	KeySpec			*	keySpec;
	ZoneSpec		*	zoneSpec;
	int					err = 0;

	g_filename = file;

	// Tear down the current zone specifiers

	zone = d->zones;

	while ( zone )
		{
		DNSZone * next = zone->next;

		key = zone->updateKeys;

		while ( key )
			{
			DomainAuthInfo * nextKey = key->next;

			free( key );

			key = nextKey;
			}

		key = zone->queryKeys;

		while ( key )
			{
			DomainAuthInfo * nextKey = key->next;

			free( key );

			key = nextKey;
			}

		free( zone );

		zone = next;
		}

	d->zones = NULL;
	
	yyin = fopen( file, "r" );
	require_action( yyin, exit, err = 0 );

	err = yyparse( ( void* ) d );
	require_action( !err, exit, err = 1 );

	for ( zoneSpec = g_zones; zoneSpec; zoneSpec = zoneSpec->next )
		{
		StringListElem  *   elem;
		mDNSu8			*	ok;

		zone = ( DNSZone* ) malloc( sizeof( DNSZone ) );
		require_action( zone, exit, err = 1 );
		memset( zone, 0, sizeof( DNSZone ) );

		zone->next	= d->zones;
		d->zones	= zone;

		// Fill in the domainname

		ok = MakeDomainNameFromDNSNameString( &zone->name, zoneSpec->name );
		require_action( ok, exit, err = 1 );

		// Fill in the type

		zone->type = zoneSpec->type;

		// Fill in the allow-update keys

		for ( elem = zoneSpec->allowUpdate; elem; elem = elem->next )
			{
			mDNSBool found = mDNSfalse;

			for ( keySpec = g_keys; keySpec; keySpec = keySpec->next )
				{
				if ( strcmp( elem->string, keySpec->name ) == 0 )
					{
					DomainAuthInfo	*	authInfo = malloc( sizeof( DomainAuthInfo ) );
					mDNSs32				keylen;
					require_action( authInfo, exit, err = 1 );
					memset( authInfo, 0, sizeof( DomainAuthInfo ) );

					ok = MakeDomainNameFromDNSNameString( &authInfo->keyname, keySpec->name );
					if (!ok) { free(authInfo); err = 1; goto exit; }

					keylen = DNSDigest_ConstructHMACKeyfromBase64( authInfo, keySpec->secret );
					if (keylen < 0) { free(authInfo); err = 1; goto exit; }

					authInfo->next = zone->updateKeys;
					zone->updateKeys = authInfo;

					found = mDNStrue;

					break;
					}
				}

			// Log this
			require_action( found, exit, err = 1 );
			}

		// Fill in the allow-query keys

		for ( elem = zoneSpec->allowQuery; elem; elem = elem->next )
			{
			mDNSBool found = mDNSfalse;

			for ( keySpec = g_keys; keySpec; keySpec = keySpec->next )
				{
				if ( strcmp( elem->string, keySpec->name ) == 0 )
					{
					DomainAuthInfo	*	authInfo = malloc( sizeof( DomainAuthInfo ) );
					mDNSs32				keylen;
					require_action( authInfo, exit, err = 1 );
					memset( authInfo, 0, sizeof( DomainAuthInfo ) );

					ok = MakeDomainNameFromDNSNameString( &authInfo->keyname, keySpec->name );
					if (!ok) { free(authInfo); err = 1; goto exit; }

					keylen = DNSDigest_ConstructHMACKeyfromBase64( authInfo, keySpec->secret );
					if (keylen < 0) { free(authInfo); err = 1; goto exit; }

					authInfo->next = zone->queryKeys;
					zone->queryKeys = authInfo;

					found = mDNStrue;

					break;
					}
				}

			// Log this
			require_action( found, exit, err = 1 );
			}
		}

exit:

	return err;
	}


void
SetupOptions
	(
	OptionsInfo	*	info,
	void		*	context
	)
	{
	DaemonInfo * d = ( DaemonInfo* ) context;

	if ( strlen( info->source_address ) )
		{
		inet_pton( AF_INET, info->source_address, &d->addr.sin_addr );
		}

	if ( info->source_port )
		{
		d->addr.sin_port = htons( ( mDNSu16 ) info->source_port );
		}
				
	if ( strlen( info->server_address ) )
		{
		inet_pton( AF_INET, info->server_address, &d->ns_addr.sin_addr );
		}

	if ( info->server_port )
		{
		d->ns_addr.sin_port = htons( ( mDNSu16 ) info->server_port );
		}

	if ( info->private_port )
		{
		d->private_port = mDNSOpaque16fromIntVal( info->private_port );
		}

	if ( info->llq_port )
		{
		d->llq_port = mDNSOpaque16fromIntVal( info->llq_port );
		}
	}