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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
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
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
|
/* $OpenBSD: clparse.c,v 1.18 2004/09/15 18:15:18 henning Exp $ */
/* Parser for dhclient config and lease files... */
/*
* Copyright (c) 1997 The Internet Software Consortium.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of The Internet Software Consortium nor the names
* of its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
* CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This software has been written for the Internet Software Consortium
* by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
* Enterprises. To learn more about the Internet Software Consortium,
* see ``http://www.vix.com/isc''. To learn more about Vixie
* Enterprises, see ``http://www.vix.com''.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include "dhcpd.h"
#include "dhctoken.h"
struct client_config top_level_config;
struct interface_info *dummy_interfaces;
extern struct interface_info *ifi;
char client_script_name[] = "/sbin/dhclient-script";
/*
* client-conf-file :== client-declarations EOF
* client-declarations :== <nil>
* | client-declaration
* | client-declarations client-declaration
*/
int
read_client_conf(void)
{
FILE *cfile;
char *val;
int token;
struct client_config *config;
new_parse(path_dhclient_conf);
/* Set up the initial dhcp option universe. */
initialize_universes();
/* Initialize the top level client configuration. */
memset(&top_level_config, 0, sizeof(top_level_config));
/* Set some defaults... */
top_level_config.timeout = 60;
top_level_config.select_interval = 0;
top_level_config.reboot_timeout = 10;
top_level_config.retry_interval = 300;
top_level_config.backoff_cutoff = 15;
top_level_config.initial_interval = 3;
top_level_config.bootp_policy = ACCEPT;
top_level_config.script_name = client_script_name;
top_level_config.requested_options
[top_level_config.requested_option_count++] = DHO_SUBNET_MASK;
top_level_config.requested_options
[top_level_config.requested_option_count++] = DHO_BROADCAST_ADDRESS;
top_level_config.requested_options
[top_level_config.requested_option_count++] = DHO_TIME_OFFSET;
top_level_config.requested_options
[top_level_config.requested_option_count++] = DHO_CLASSLESS_ROUTES;
top_level_config.requested_options
[top_level_config.requested_option_count++] = DHO_ROUTERS;
top_level_config.requested_options
[top_level_config.requested_option_count++] = DHO_DOMAIN_NAME;
top_level_config.requested_options
[top_level_config.requested_option_count++] =
DHO_DOMAIN_NAME_SERVERS;
top_level_config.requested_options
[top_level_config.requested_option_count++] = DHO_HOST_NAME;
top_level_config.requested_options
[top_level_config.requested_option_count++] = DHO_DOMAIN_SEARCH;
if ((cfile = fopen(path_dhclient_conf, "r")) != NULL) {
do {
token = peek_token(&val, cfile);
if (token == EOF)
break;
parse_client_statement(cfile, NULL, &top_level_config);
} while (1);
token = next_token(&val, cfile); /* Clear the peek buffer */
fclose(cfile);
}
/*
* Set up state and config structures for clients that don't
* have per-interface configuration declarations.
*/
config = NULL;
if (!ifi->client) {
ifi->client = malloc(sizeof(struct client_state));
if (!ifi->client)
error("no memory for client state.");
memset(ifi->client, 0, sizeof(*(ifi->client)));
}
if (!ifi->client->config) {
if (!config) {
config = malloc(sizeof(struct client_config));
if (!config)
error("no memory for client config.");
memcpy(config, &top_level_config,
sizeof(top_level_config));
}
ifi->client->config = config;
}
return (!warnings_occurred);
}
/*
* lease-file :== client-lease-statements EOF
* client-lease-statements :== <nil>
* | client-lease-statements LEASE client-lease-statement
*/
void
read_client_leases(void)
{
FILE *cfile;
char *val;
int token;
new_parse(path_dhclient_db);
/* Open the lease file. If we can't open it, just return -
we can safely trust the server to remember our state. */
if ((cfile = fopen(path_dhclient_db, "r")) == NULL)
return;
do {
token = next_token(&val, cfile);
if (token == EOF)
break;
if (token != LEASE) {
warning("Corrupt lease file - possible data loss!");
skip_to_semi(cfile);
break;
} else
parse_client_lease_statement(cfile, 0);
} while (1);
fclose(cfile);
}
/*
* client-declaration :==
* SEND option-decl |
* DEFAULT option-decl |
* SUPERSEDE option-decl |
* PREPEND option-decl |
* APPEND option-decl |
* hardware-declaration |
* REQUEST option-list |
* REQUIRE option-list |
* TIMEOUT number |
* RETRY number |
* REBOOT number |
* SELECT_TIMEOUT number |
* SCRIPT string |
* interface-declaration |
* LEASE client-lease-statement |
* ALIAS client-lease-statement
*/
void
parse_client_statement(FILE *cfile, struct interface_info *ip,
struct client_config *config)
{
int token;
char *val;
struct option *option;
switch (next_token(&val, cfile)) {
case SEND:
parse_option_decl(cfile, &config->send_options[0]);
return;
case DEFAULT:
option = parse_option_decl(cfile, &config->defaults[0]);
if (option)
config->default_actions[option->code] = ACTION_DEFAULT;
return;
case SUPERSEDE:
option = parse_option_decl(cfile, &config->defaults[0]);
if (option)
config->default_actions[option->code] =
ACTION_SUPERSEDE;
return;
case APPEND:
option = parse_option_decl(cfile, &config->defaults[0]);
if (option)
config->default_actions[option->code] = ACTION_APPEND;
return;
case PREPEND:
option = parse_option_decl(cfile, &config->defaults[0]);
if (option)
config->default_actions[option->code] = ACTION_PREPEND;
return;
case MEDIA:
parse_string_list(cfile, &config->media, 1);
return;
case HARDWARE:
if (ip)
parse_hardware_param(cfile, &ip->hw_address);
else {
parse_warn("hardware address parameter %s",
"not allowed here.");
skip_to_semi(cfile);
}
return;
case REQUEST:
config->requested_option_count =
parse_option_list(cfile, config->requested_options);
return;
case REQUIRE:
memset(config->required_options, 0,
sizeof(config->required_options));
parse_option_list(cfile, config->required_options);
return;
case TIMEOUT:
parse_lease_time(cfile, &config->timeout);
return;
case RETRY:
parse_lease_time(cfile, &config->retry_interval);
return;
case SELECT_TIMEOUT:
parse_lease_time(cfile, &config->select_interval);
return;
case REBOOT:
parse_lease_time(cfile, &config->reboot_timeout);
return;
case BACKOFF_CUTOFF:
parse_lease_time(cfile, &config->backoff_cutoff);
return;
case INITIAL_INTERVAL:
parse_lease_time(cfile, &config->initial_interval);
return;
case SCRIPT:
config->script_name = parse_string(cfile);
return;
case INTERFACE:
if (ip)
parse_warn("nested interface declaration.");
parse_interface_declaration(cfile, config);
return;
case LEASE:
parse_client_lease_statement(cfile, 1);
return;
case ALIAS:
parse_client_lease_statement(cfile, 2);
return;
case REJECT:
parse_reject_statement(cfile, config);
return;
default:
parse_warn("expecting a statement.");
skip_to_semi(cfile);
break;
}
token = next_token(&val, cfile);
if (token != SEMI) {
parse_warn("semicolon expected.");
skip_to_semi(cfile);
}
}
int
parse_X(FILE *cfile, u_int8_t *buf, int max)
{
int token;
char *val;
int len;
token = peek_token(&val, cfile);
if (token == NUMBER_OR_NAME || token == NUMBER) {
len = 0;
do {
token = next_token(&val, cfile);
if (token != NUMBER && token != NUMBER_OR_NAME) {
parse_warn("expecting hexadecimal constant.");
skip_to_semi(cfile);
return (0);
}
convert_num(&buf[len], val, 16, 8);
if (len++ > max) {
parse_warn("hexadecimal constant too long.");
skip_to_semi(cfile);
return (0);
}
token = peek_token(&val, cfile);
if (token == COLON)
token = next_token(&val, cfile);
} while (token == COLON);
val = (char *)buf;
} else if (token == STRING) {
token = next_token(&val, cfile);
len = strlen(val);
if (len + 1 > max) {
parse_warn("string constant too long.");
skip_to_semi(cfile);
return (0);
}
memcpy(buf, val, len + 1);
} else {
parse_warn("expecting string or hexadecimal data");
skip_to_semi(cfile);
return (0);
}
return (len);
}
/*
* option-list :== option_name |
* option_list COMMA option_name
*/
int
parse_option_list(FILE *cfile, u_int8_t *list)
{
int ix, i;
int token;
char *val;
ix = 0;
do {
token = next_token(&val, cfile);
if (!is_identifier(token)) {
parse_warn("expected option name.");
skip_to_semi(cfile);
return (0);
}
for (i = 0; i < 256; i++)
if (!strcasecmp(dhcp_options[i].name, val))
break;
if (i == 256) {
parse_warn("%s: unexpected option name.", val);
skip_to_semi(cfile);
return (0);
}
list[ix++] = i;
if (ix == 256) {
parse_warn("%s: too many options.", val);
skip_to_semi(cfile);
return (0);
}
token = next_token(&val, cfile);
} while (token == COMMA);
if (token != SEMI) {
parse_warn("expecting semicolon.");
skip_to_semi(cfile);
return (0);
}
return (ix);
}
/*
* interface-declaration :==
* INTERFACE string LBRACE client-declarations RBRACE
*/
void
parse_interface_declaration(FILE *cfile, struct client_config *outer_config)
{
int token;
char *val;
struct interface_info *ip;
token = next_token(&val, cfile);
if (token != STRING) {
parse_warn("expecting interface name (in quotes).");
skip_to_semi(cfile);
return;
}
ip = interface_or_dummy(val);
if (!ip->client)
make_client_state(ip);
if (!ip->client->config)
make_client_config(ip, outer_config);
token = next_token(&val, cfile);
if (token != LBRACE) {
parse_warn("expecting left brace.");
skip_to_semi(cfile);
return;
}
do {
token = peek_token(&val, cfile);
if (token == EOF) {
parse_warn("unterminated interface declaration.");
return;
}
if (token == RBRACE)
break;
parse_client_statement(cfile, ip, ip->client->config);
} while (1);
token = next_token(&val, cfile);
}
struct interface_info *
interface_or_dummy(char *name)
{
struct interface_info *ip;
/* Find the interface (if any) that matches the name. */
if (!strcmp(ifi->name, name))
return (ifi);
/* If it's not a real interface, see if it's on the dummy list. */
for (ip = dummy_interfaces; ip; ip = ip->next)
if (!strcmp(ip->name, name))
return (ip);
/*
* If we didn't find an interface, make a dummy interface as a
* placeholder.
*/
ip = malloc(sizeof(*ip));
if (!ip)
error("Insufficient memory to record interface %s", name);
memset(ip, 0, sizeof(*ip));
strlcpy(ip->name, name, IFNAMSIZ);
ip->next = dummy_interfaces;
dummy_interfaces = ip;
return (ip);
}
void
make_client_state(struct interface_info *ip)
{
ip->client = malloc(sizeof(*(ip->client)));
if (!ip->client)
error("no memory for state on %s", ip->name);
memset(ip->client, 0, sizeof(*(ip->client)));
}
void
make_client_config(struct interface_info *ip, struct client_config *config)
{
ip->client->config = malloc(sizeof(struct client_config));
if (!ip->client->config)
error("no memory for config for %s", ip->name);
memset(ip->client->config, 0, sizeof(*(ip->client->config)));
memcpy(ip->client->config, config, sizeof(*config));
}
/*
* client-lease-statement :==
* RBRACE client-lease-declarations LBRACE
*
* client-lease-declarations :==
* <nil> |
* client-lease-declaration |
* client-lease-declarations client-lease-declaration
*/
void
parse_client_lease_statement(FILE *cfile, int is_static)
{
struct client_lease *lease, *lp, *pl;
struct interface_info *ip;
int token;
char *val;
token = next_token(&val, cfile);
if (token != LBRACE) {
parse_warn("expecting left brace.");
skip_to_semi(cfile);
return;
}
lease = malloc(sizeof(struct client_lease));
if (!lease)
error("no memory for lease.");
memset(lease, 0, sizeof(*lease));
lease->is_static = is_static;
ip = NULL;
do {
token = peek_token(&val, cfile);
if (token == EOF) {
parse_warn("unterminated lease declaration.");
return;
}
if (token == RBRACE)
break;
parse_client_lease_declaration(cfile, lease, &ip);
} while (1);
token = next_token(&val, cfile);
/* If the lease declaration didn't include an interface
* declaration that we recognized, it's of no use to us.
*/
if (!ip) {
free_client_lease(lease);
return;
}
/* Make sure there's a client state structure... */
if (!ip->client)
make_client_state(ip);
/* If this is an alias lease, it doesn't need to be sorted in. */
if (is_static == 2) {
ip->client->alias = lease;
return;
}
/*
* The new lease may supersede a lease that's not the active
* lease but is still on the lease list, so scan the lease list
* looking for a lease with the same address, and if we find it,
* toss it.
*/
pl = NULL;
for (lp = ip->client->leases; lp; lp = lp->next) {
if (lp->address.len == lease->address.len &&
!memcmp(lp->address.iabuf, lease->address.iabuf,
lease->address.len)) {
if (pl)
pl->next = lp->next;
else
ip->client->leases = lp->next;
free_client_lease(lp);
break;
}
}
/*
* If this is a preloaded lease, just put it on the list of
* recorded leases - don't make it the active lease.
*/
if (is_static) {
lease->next = ip->client->leases;
ip->client->leases = lease;
return;
}
/*
* The last lease in the lease file on a particular interface is
* the active lease for that interface. Of course, we don't
* know what the last lease in the file is until we've parsed
* the whole file, so at this point, we assume that the lease we
* just parsed is the active lease for its interface. If
* there's already an active lease for the interface, and this
* lease is for the same ip address, then we just toss the old
* active lease and replace it with this one. If this lease is
* for a different address, then if the old active lease has
* expired, we dump it; if not, we put it on the list of leases
* for this interface which are still valid but no longer
* active.
*/
if (ip->client->active) {
if (ip->client->active->expiry < cur_time)
free_client_lease(ip->client->active);
else if (ip->client->active->address.len ==
lease->address.len &&
!memcmp(ip->client->active->address.iabuf,
lease->address.iabuf, lease->address.len))
free_client_lease(ip->client->active);
else {
ip->client->active->next = ip->client->leases;
ip->client->leases = ip->client->active;
}
}
ip->client->active = lease;
/* Phew. */
}
/*
* client-lease-declaration :==
* BOOTP |
* INTERFACE string |
* FIXED_ADDR ip_address |
* FILENAME string |
* SERVER_NAME string |
* OPTION option-decl |
* RENEW time-decl |
* REBIND time-decl |
* EXPIRE time-decl
*/
void
parse_client_lease_declaration(FILE *cfile, struct client_lease *lease,
struct interface_info **ipp)
{
int token;
char *val;
struct interface_info *ip;
switch (next_token(&val, cfile)) {
case BOOTP:
lease->is_bootp = 1;
break;
case INTERFACE:
token = next_token(&val, cfile);
if (token != STRING) {
parse_warn("expecting interface name (in quotes).");
skip_to_semi(cfile);
break;
}
ip = interface_or_dummy(val);
*ipp = ip;
break;
case FIXED_ADDR:
if (!parse_ip_addr(cfile, &lease->address))
return;
break;
case MEDIUM:
parse_string_list(cfile, &lease->medium, 0);
return;
case FILENAME:
lease->filename = parse_string(cfile);
return;
case NEXT_SERVER:
if (!parse_ip_addr(cfile, &lease->nextserver))
return;
break;
case SERVER_NAME:
lease->server_name = parse_string(cfile);
return;
case RENEW:
lease->renewal = parse_date(cfile);
return;
case REBIND:
lease->rebind = parse_date(cfile);
return;
case EXPIRE:
lease->expiry = parse_date(cfile);
return;
case OPTION:
parse_option_decl(cfile, lease->options);
return;
default:
parse_warn("expecting lease declaration.");
skip_to_semi(cfile);
break;
}
token = next_token(&val, cfile);
if (token != SEMI) {
parse_warn("expecting semicolon.");
skip_to_semi(cfile);
}
}
struct option *
parse_option_decl(FILE *cfile, struct option_data *options)
{
char *val;
int token;
u_int8_t buf[4];
u_int8_t hunkbuf[1024];
int hunkix = 0;
char *vendor;
char *fmt;
struct universe *universe;
struct option *option;
struct iaddr ip_addr;
u_int8_t *dp;
int len;
int nul_term = 0;
token = next_token(&val, cfile);
if (!is_identifier(token)) {
parse_warn("expecting identifier after option keyword.");
if (token != SEMI)
skip_to_semi(cfile);
return (NULL);
}
if ((vendor = strdup(val)) == NULL)
error("no memory for vendor information.");
token = peek_token(&val, cfile);
if (token == DOT) {
/* Go ahead and take the DOT token... */
token = next_token(&val, cfile);
/* The next token should be an identifier... */
token = next_token(&val, cfile);
if (!is_identifier(token)) {
parse_warn("expecting identifier after '.'");
if (token != SEMI)
skip_to_semi(cfile);
return (NULL);
}
/* Look up the option name hash table for the specified
vendor. */
universe = ((struct universe *)hash_lookup(&universe_hash,
(unsigned char *)vendor, 0));
/* If it's not there, we can't parse the rest of the
declaration. */
if (!universe) {
parse_warn("no vendor named %s.", vendor);
skip_to_semi(cfile);
return (NULL);
}
} else {
/* Use the default hash table, which contains all the
standard dhcp option names. */
val = vendor;
universe = &dhcp_universe;
}
/* Look up the actual option info... */
option = (struct option *)hash_lookup(universe->hash,
(unsigned char *)val, 0);
/* If we didn't get an option structure, it's an undefined option. */
if (!option) {
if (val == vendor)
parse_warn("no option named %s", val);
else
parse_warn("no option named %s for vendor %s",
val, vendor);
skip_to_semi(cfile);
return (NULL);
}
/* Free the initial identifier token. */
free(vendor);
/* Parse the option data... */
do {
for (fmt = option->format; *fmt; fmt++) {
if (*fmt == 'A')
break;
switch (*fmt) {
case 'X':
len = parse_X(cfile, &hunkbuf[hunkix],
sizeof(hunkbuf) - hunkix);
hunkix += len;
break;
case 't': /* Text string... */
token = next_token(&val, cfile);
if (token != STRING) {
parse_warn("expecting string.");
skip_to_semi(cfile);
return (NULL);
}
len = strlen(val);
if (hunkix + len + 1 > sizeof(hunkbuf)) {
parse_warn("option data buffer %s",
"overflow");
skip_to_semi(cfile);
return (NULL);
}
memcpy(&hunkbuf[hunkix], val, len + 1);
nul_term = 1;
hunkix += len;
break;
case 'I': /* IP address. */
if (!parse_ip_addr(cfile, &ip_addr))
return (NULL);
len = ip_addr.len;
dp = ip_addr.iabuf;
alloc:
if (hunkix + len > sizeof(hunkbuf)) {
parse_warn("option data buffer "
"overflow");
skip_to_semi(cfile);
return (NULL);
}
memcpy(&hunkbuf[hunkix], dp, len);
hunkix += len;
break;
case 'L': /* Unsigned 32-bit integer... */
case 'l': /* Signed 32-bit integer... */
token = next_token(&val, cfile);
if (token != NUMBER) {
need_number:
parse_warn("expecting number.");
if (token != SEMI)
skip_to_semi(cfile);
return (NULL);
}
convert_num(buf, val, 0, 32);
len = 4;
dp = buf;
goto alloc;
case 's': /* Signed 16-bit integer. */
case 'S': /* Unsigned 16-bit integer. */
token = next_token(&val, cfile);
if (token != NUMBER)
goto need_number;
convert_num(buf, val, 0, 16);
len = 2;
dp = buf;
goto alloc;
case 'b': /* Signed 8-bit integer. */
case 'B': /* Unsigned 8-bit integer. */
token = next_token(&val, cfile);
if (token != NUMBER)
goto need_number;
convert_num(buf, val, 0, 8);
len = 1;
dp = buf;
goto alloc;
case 'f': /* Boolean flag. */
token = next_token(&val, cfile);
if (!is_identifier(token)) {
parse_warn("expecting identifier.");
bad_flag:
if (token != SEMI)
skip_to_semi(cfile);
return (NULL);
}
if (!strcasecmp(val, "true") ||
!strcasecmp(val, "on"))
buf[0] = 1;
else if (!strcasecmp(val, "false") ||
!strcasecmp(val, "off"))
buf[0] = 0;
else {
parse_warn("expecting boolean.");
goto bad_flag;
}
len = 1;
dp = buf;
goto alloc;
default:
warning("Bad format %c in parse_option_param.",
*fmt);
skip_to_semi(cfile);
return (NULL);
}
}
token = next_token(&val, cfile);
} while (*fmt == 'A' && token == COMMA);
if (token != SEMI) {
parse_warn("semicolon expected.");
skip_to_semi(cfile);
return (NULL);
}
options[option->code].data = malloc(hunkix + nul_term);
if (!options[option->code].data)
error("out of memory allocating option data.");
memcpy(options[option->code].data, hunkbuf, hunkix + nul_term);
options[option->code].len = hunkix;
return (option);
}
void
parse_string_list(FILE *cfile, struct string_list **lp, int multiple)
{
int token;
char *val;
size_t valsize;
struct string_list *cur, *tmp;
/* Find the last medium in the media list. */
if (*lp)
for (cur = *lp; cur->next; cur = cur->next)
; /* nothing */
else
cur = NULL;
do {
token = next_token(&val, cfile);
if (token != STRING) {
parse_warn("Expecting media options.");
skip_to_semi(cfile);
return;
}
valsize = strlen(val) + 1;
tmp = new_string_list(valsize);
if (tmp == NULL)
error("no memory for string list entry.");
memcpy(tmp->string, val, valsize);
tmp->next = NULL;
/* Store this medium at the end of the media list. */
if (cur)
cur->next = tmp;
else
*lp = tmp;
cur = tmp;
token = next_token(&val, cfile);
} while (multiple && token == COMMA);
if (token != SEMI) {
parse_warn("expecting semicolon.");
skip_to_semi(cfile);
}
}
void
parse_reject_statement(FILE *cfile, struct client_config *config)
{
int token;
char *val;
struct iaddr addr;
struct iaddrlist *list;
do {
if (!parse_ip_addr(cfile, &addr)) {
parse_warn("expecting IP address.");
skip_to_semi(cfile);
return;
}
list = malloc(sizeof(struct iaddrlist));
if (!list)
error("no memory for reject list!");
list->addr = addr;
list->next = config->reject_list;
config->reject_list = list;
token = next_token(&val, cfile);
} while (token == COMMA);
if (token != SEMI) {
parse_warn("expecting semicolon.");
skip_to_semi(cfile);
}
}
|