1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 2006 Apple Computer, Inc. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
22 #include "mDNSEmbeddedAPI.h"
23 #include "DebugServices.h"
26 void yyerror( const char* error );
30 typedef struct StringListElem
33 struct StringListElem * next;
37 typedef struct OptionsInfo
39 char server_address[ 256 ];
41 char source_address[ 256 ];
48 typedef struct ZoneInfo
51 char certificate_name[ 256 ];
52 char allow_clients_file[ 256 ];
53 char allow_clients[ 256 ];
58 typedef struct KeySpec
61 char algorithm[ 256 ];
63 struct KeySpec * next;
67 typedef struct ZoneSpec
71 StringListElem * allowUpdate;
72 StringListElem * allowQuery;
74 struct ZoneSpec * next;
78 static StringListElem * g_stringList = NULL;
79 static KeySpec * g_keys;
80 static ZoneSpec * g_zones;
81 static ZoneSpec g_zoneSpec;
82 static const char * g_filename;
84 #define YYPARSE_PARAM context
123 %token <string> DOTTED_DECIMAL_ADDRESS
124 %token <string> WILDCARD
125 %token <string> DOMAINNAME
126 %token <string> HOSTNAME
127 %token <string> QUOTEDSTRING
128 %token <number> NUMBER
130 %type <string> addressstatement
131 %type <string> networkaddress
137 commands command SEMICOLON
151 OPTIONS optionscontent
153 // SetupOptions( &g_optionsInfo, context );
158 OBRACE optionsstatements EBRACE
163 optionsstatements optionsstatement SEMICOLON
170 LISTEN_ON addresscontent
174 LISTEN_ON PORT NUMBER addresscontent
178 NAMESERVER ADDRESS networkaddress
182 NAMESERVER ADDRESS networkaddress PORT NUMBER
188 ( ( DaemonInfo* ) context )->private_port = mDNSOpaque16fromIntVal( $3 );
193 ( ( DaemonInfo* ) context )->llq_port = mDNSOpaque16fromIntVal( $3 );
198 KEY QUOTEDSTRING OBRACE SECRET QUOTEDSTRING SEMICOLON EBRACE
202 keySpec = ( KeySpec* ) malloc( sizeof( KeySpec ) );
206 LogMsg("ERROR: memory allocation failure");
210 strncpy( keySpec->name, $2, sizeof( keySpec->name ) );
211 strncpy( keySpec->secret, $5, sizeof( keySpec->secret ) );
213 keySpec->next = g_keys;
219 ZONE QUOTEDSTRING zonecontent
223 zoneSpec = ( ZoneSpec* ) malloc( sizeof( ZoneSpec ) );
227 LogMsg("ERROR: memory allocation failure");
231 strncpy( zoneSpec->name, $2, sizeof( zoneSpec->name ) );
232 zoneSpec->type = g_zoneSpec.type;
233 strcpy( zoneSpec->key, g_zoneSpec.key );
234 zoneSpec->allowUpdate = g_zoneSpec.allowUpdate;
235 zoneSpec->allowQuery = g_zoneSpec.allowQuery;
237 zoneSpec->next = g_zones;
241 ZONE QUOTEDSTRING IN zonecontent
245 zoneSpec = ( ZoneSpec* ) malloc( sizeof( ZoneSpec ) );
249 LogMsg("ERROR: memory allocation failure");
253 strncpy( zoneSpec->name, $2, sizeof( zoneSpec->name ) );
254 zoneSpec->type = g_zoneSpec.type;
255 strcpy( zoneSpec->key, g_zoneSpec.key );
256 zoneSpec->allowUpdate = g_zoneSpec.allowUpdate;
257 zoneSpec->allowQuery = g_zoneSpec.allowQuery;
259 zoneSpec->next = g_zones;
265 OBRACE zonestatements EBRACE
269 zonestatements zonestatement SEMICOLON
275 g_zoneSpec.type = kDNSZonePublic;
280 g_zoneSpec.type = kDNSZonePrivate;
283 ALLOWUPDATE keycontent
285 g_zoneSpec.allowUpdate = g_stringList;
289 ALLOWQUERY keycontent
291 g_zoneSpec.allowQuery = g_stringList;
297 OBRACE addressstatements EBRACE
303 addressstatements addressstatement SEMICOLON
309 DOTTED_DECIMAL_ADDRESS
316 OBRACE keystatements EBRACE
322 keystatements keystatement SEMICOLON
330 StringListElem * elem;
332 elem = ( StringListElem* ) malloc( sizeof( StringListElem ) );
336 LogMsg("ERROR: memory allocation failure");
342 elem->next = g_stringList;
349 DOTTED_DECIMAL_ADDRESS
357 OBRACE zonestatements EBRACE SEMICOLON
381 void yyerror( const char *str )
383 fprintf( stderr,"%s:%d: error: %s\n", g_filename, yylineno, str );
401 DomainAuthInfo * key;
408 // Tear down the current zone specifiers
414 DNSZone * next = zone->next;
416 key = zone->updateKeys;
420 DomainAuthInfo * nextKey = key->next;
427 key = zone->queryKeys;
431 DomainAuthInfo * nextKey = key->next;
445 yyin = fopen( file, "r" );
446 require_action( yyin, exit, err = 0 );
448 err = yyparse( ( void* ) d );
449 require_action( !err, exit, err = 1 );
451 for ( zoneSpec = g_zones; zoneSpec; zoneSpec = zoneSpec->next )
453 StringListElem * elem;
456 zone = ( DNSZone* ) malloc( sizeof( DNSZone ) );
457 require_action( zone, exit, err = 1 );
458 memset( zone, 0, sizeof( DNSZone ) );
460 zone->next = d->zones;
463 // Fill in the domainname
465 ok = MakeDomainNameFromDNSNameString( &zone->name, zoneSpec->name );
466 require_action( ok, exit, err = 1 );
470 zone->type = zoneSpec->type;
472 // Fill in the allow-update keys
474 for ( elem = zoneSpec->allowUpdate; elem; elem = elem->next )
476 mDNSBool found = mDNSfalse;
478 for ( keySpec = g_keys; keySpec; keySpec = keySpec->next )
480 if ( strcmp( elem->string, keySpec->name ) == 0 )
482 DomainAuthInfo * authInfo = malloc( sizeof( DomainAuthInfo ) );
484 require_action( authInfo, exit, err = 1 );
485 memset( authInfo, 0, sizeof( DomainAuthInfo ) );
487 ok = MakeDomainNameFromDNSNameString( &authInfo->keyname, keySpec->name );
488 if (!ok) { free(authInfo); err = 1; goto exit; }
490 keylen = DNSDigest_ConstructHMACKeyfromBase64( authInfo, keySpec->secret );
491 if (keylen < 0) { free(authInfo); err = 1; goto exit; }
493 authInfo->next = zone->updateKeys;
494 zone->updateKeys = authInfo;
503 require_action( found, exit, err = 1 );
506 // Fill in the allow-query keys
508 for ( elem = zoneSpec->allowQuery; elem; elem = elem->next )
510 mDNSBool found = mDNSfalse;
512 for ( keySpec = g_keys; keySpec; keySpec = keySpec->next )
514 if ( strcmp( elem->string, keySpec->name ) == 0 )
516 DomainAuthInfo * authInfo = malloc( sizeof( DomainAuthInfo ) );
518 require_action( authInfo, exit, err = 1 );
519 memset( authInfo, 0, sizeof( DomainAuthInfo ) );
521 ok = MakeDomainNameFromDNSNameString( &authInfo->keyname, keySpec->name );
522 if (!ok) { free(authInfo); err = 1; goto exit; }
524 keylen = DNSDigest_ConstructHMACKeyfromBase64( authInfo, keySpec->secret );
525 if (keylen < 0) { free(authInfo); err = 1; goto exit; }
527 authInfo->next = zone->queryKeys;
528 zone->queryKeys = authInfo;
537 require_action( found, exit, err = 1 );
554 DaemonInfo * d = ( DaemonInfo* ) context;
556 if ( strlen( info->source_address ) )
558 inet_pton( AF_INET, info->source_address, &d->addr.sin_addr );
561 if ( info->source_port )
563 d->addr.sin_port = htons( ( mDNSu16 ) info->source_port );
566 if ( strlen( info->server_address ) )
568 inet_pton( AF_INET, info->server_address, &d->ns_addr.sin_addr );
571 if ( info->server_port )
573 d->ns_addr.sin_port = htons( ( mDNSu16 ) info->server_port );
576 if ( info->private_port )
578 d->private_port = mDNSOpaque16fromIntVal( info->private_port );
581 if ( info->llq_port )
583 d->llq_port = mDNSOpaque16fromIntVal( info->llq_port );