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
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
|
/* dhcpd.c
DHCP Server Daemon. */
/*
* Copyright (c) 2004-2008 by Internet Systems Consortium, Inc. ("ISC")
* Copyright (c) 1996-2003 by Internet Software Consortium
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Internet Systems Consortium, Inc.
* 950 Charter Street
* Redwood City, CA 94063
* <info@isc.org>
* http://www.isc.org/
*
* This software has been written for Internet Systems Consortium
* by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
* To learn more about Internet Systems Consortium, see
* ``http://www.isc.org/''. To learn more about Vixie Enterprises,
* see ``http://www.vix.com''. To learn more about Nominum, Inc., see
* ``http://www.nominum.com''.
*/
static char copyright[] =
"Copyright 2004-2008 Internet Systems Consortium.";
static char arr [] = "All rights reserved.";
static char message [] = "Internet Systems Consortium DHCP Server";
static char url [] = "For info, please visit http://www.isc.org/sw/dhcp/";
#include "dhcpd.h"
#include <omapip/omapip_p.h>
#include <syslog.h>
#include <errno.h>
#include <limits.h>
#include <sys/types.h>
#include <signal.h>
static void usage(void);
struct iaddr server_identifier;
int server_identifier_matched;
#if defined (NSUPDATE)
/* This stuff is always executed to figure the default values for certain
ddns variables. */
char std_nsupdate [] = " \n\
option server.ddns-hostname = \n\
pick (option fqdn.hostname, option host-name); \n\
option server.ddns-domainname = config-option domain-name; \n\
option server.ddns-ttl = encode-int(lease-time / 2, 32); \n\
option server.ddns-rev-domainname = \"in-addr.arpa.\";";
/* This is the old-style name service updater that is executed
whenever a lease is committed. It does not follow the DHCP-DNS
draft at all. */
char old_nsupdate [] = " \n\
on commit { \n\
if (not static and \n\
((config-option server.ddns-updates = null) or \n\
(config-option server.ddns-updates != 0))) { \n\
set new-ddns-fwd-name = \n\
concat (pick (config-option server.ddns-hostname, \n\
option host-name), \".\", \n\
pick (config-option server.ddns-domainname, \n\
config-option domain-name)); \n\
if (defined (ddns-fwd-name) and ddns-fwd-name != new-ddns-fwd-name) { \n\
switch (ns-update (delete (IN, A, ddns-fwd-name, leased-address))) { \n\
case NOERROR: \n\
unset ddns-fwd-name; \n\
on expiry or release { \n\
} \n\
} \n\
} \n\
\n\
if (not defined (ddns-fwd-name)) { \n\
set ddns-fwd-name = new-ddns-fwd-name; \n\
if defined (ddns-fwd-name) { \n\
switch (ns-update (not exists (IN, A, ddns-fwd-name, null), \n\
add (IN, A, ddns-fwd-name, leased-address, \n\
lease-time / 2))) { \n\
default: \n\
unset ddns-fwd-name; \n\
break; \n\
\n\
case NOERROR: \n\
set ddns-rev-name = \n\
concat (binary-to-ascii (10, 8, \".\", \n\
reverse (1, \n\
leased-address)), \".\", \n\
pick (config-option server.ddns-rev-domainname, \n\
\"in-addr.arpa.\")); \n\
switch (ns-update (delete (IN, PTR, ddns-rev-name, null), \n\
add (IN, PTR, ddns-rev-name, ddns-fwd-name, \n\
lease-time / 2))) \n\
{ \n\
default: \n\
unset ddns-rev-name; \n\
on release or expiry { \n\
switch (ns-update (delete (IN, A, ddns-fwd-name, \n\
leased-address))) { \n\
case NOERROR: \n\
unset ddns-fwd-name; \n\
break; \n\
} \n\
on release or expiry; \n\
} \n\
break; \n\
\n\
case NOERROR: \n\
on release or expiry { \n\
switch (ns-update (delete (IN, PTR, ddns-rev-name, null))) {\n\
case NOERROR: \n\
unset ddns-rev-name; \n\
break; \n\
} \n\
switch (ns-update (delete (IN, A, ddns-fwd-name, \n\
leased-address))) { \n\
case NOERROR: \n\
unset ddns-fwd-name; \n\
break; \n\
} \n\
on release or expiry; \n\
} \n\
} \n\
} \n\
} \n\
} \n\
unset new-ddns-fwd-name; \n\
} \n\
}";
int ddns_update_style;
#endif /* NSUPDATE */
const char *path_dhcpd_conf = _PATH_DHCPD_CONF;
const char *path_dhcpd_db = _PATH_DHCPD_DB;
const char *path_dhcpd_pid = _PATH_DHCPD_PID;
int dhcp_max_agent_option_packet_length = DHCP_MTU_MAX;
static omapi_auth_key_t *omapi_key = (omapi_auth_key_t *)0;
int omapi_port;
#if defined (TRACING)
trace_type_t *trace_srandom;
#endif
static isc_result_t verify_addr (omapi_object_t *l, omapi_addr_t *addr) {
return ISC_R_SUCCESS;
}
static isc_result_t verify_auth (omapi_object_t *p, omapi_auth_key_t *a) {
if (a != omapi_key)
return ISC_R_INVALIDKEY;
return ISC_R_SUCCESS;
}
static void omapi_listener_start (void *foo)
{
omapi_object_t *listener;
isc_result_t result;
listener = (omapi_object_t *)0;
result = omapi_generic_new (&listener, MDL);
if (result != ISC_R_SUCCESS)
log_fatal ("Can't allocate new generic object: %s",
isc_result_totext (result));
result = omapi_protocol_listen (listener,
(unsigned)omapi_port, 1);
if (result == ISC_R_SUCCESS && omapi_key)
result = omapi_protocol_configure_security
(listener, verify_addr, verify_auth);
if (result != ISC_R_SUCCESS) {
log_error ("Can't start OMAPI protocol: %s",
isc_result_totext (result));
add_timeout (cur_time + 5, omapi_listener_start, 0, 0, 0);
}
omapi_object_dereference (&listener, MDL);
}
#ifndef UNIT_TEST
int
main(int argc, char **argv) {
int fd;
int i, status;
struct servent *ent;
char *s;
int cftest = 0;
int lftest = 0;
#ifndef DEBUG
int pid;
char pbuf [20];
int daemon = 1;
#endif
int quiet = 0;
char *server = (char *)0;
isc_result_t result;
unsigned seed;
struct interface_info *ip;
struct parse *parse;
int lose;
int no_dhcpd_conf = 0;
int no_dhcpd_db = 0;
int no_dhcpd_pid = 0;
#ifdef DHCPv6
int local_family_set = 0;
#endif /* DHCPv6 */
#if defined (TRACING)
char *traceinfile = (char *)0;
char *traceoutfile = (char *)0;
#endif
/* Make sure that file descriptors 0 (stdin), 1, (stdout), and
2 (stderr) are open. To do this, we assume that when we
open a file the lowest available file descriptor is used. */
fd = open("/dev/null", O_RDWR);
if (fd == 0)
fd = open("/dev/null", O_RDWR);
if (fd == 1)
fd = open("/dev/null", O_RDWR);
if (fd == 2)
log_perror = 0; /* No sense logging to /dev/null. */
else if (fd != -1)
close(fd);
/* Set up the client classification system. */
classification_setup ();
/* Initialize the omapi system. */
result = omapi_init ();
if (result != ISC_R_SUCCESS)
log_fatal ("Can't initialize OMAPI: %s",
isc_result_totext (result));
/* Set up the OMAPI wrappers for common objects. */
dhcp_db_objects_setup ();
/* Set up the OMAPI wrappers for various server database internal
objects. */
dhcp_common_objects_setup ();
/* Initially, log errors to stderr as well as to syslogd. */
openlog ("dhcpd", LOG_NDELAY, DHCPD_LOG_FACILITY);
for (i = 1; i < argc; i++) {
if (!strcmp (argv [i], "-p")) {
if (++i == argc)
usage ();
local_port = validate_port (argv [i]);
log_debug ("binding to user-specified port %d",
ntohs (local_port));
} else if (!strcmp (argv [i], "-f")) {
#ifndef DEBUG
daemon = 0;
#endif
} else if (!strcmp (argv [i], "-d")) {
#ifndef DEBUG
daemon = 0;
#endif
log_perror = -1;
} else if (!strcmp (argv [i], "-s")) {
if (++i == argc)
usage ();
server = argv [i];
} else if (!strcmp (argv [i], "-cf")) {
if (++i == argc)
usage ();
path_dhcpd_conf = argv [i];
no_dhcpd_conf = 1;
} else if (!strcmp (argv [i], "-lf")) {
if (++i == argc)
usage ();
path_dhcpd_db = argv [i];
no_dhcpd_db = 1;
} else if (!strcmp (argv [i], "-pf")) {
if (++i == argc)
usage ();
path_dhcpd_pid = argv [i];
no_dhcpd_pid = 1;
} else if (!strcmp (argv [i], "-t")) {
/* test configurations only */
#ifndef DEBUG
daemon = 0;
#endif
cftest = 1;
log_perror = -1;
} else if (!strcmp (argv [i], "-T")) {
/* test configurations and lease file only */
#ifndef DEBUG
daemon = 0;
#endif
cftest = 1;
lftest = 1;
log_perror = -1;
} else if (!strcmp (argv [i], "-q")) {
quiet = 1;
quiet_interface_discovery = 1;
#ifdef DHCPv6
} else if (!strcmp(argv[i], "-4")) {
if (local_family_set && (local_family != AF_INET)) {
log_fatal("Server cannot run in both IPv4 and "
"IPv6 mode at the same time.");
}
local_family = AF_INET;
local_family_set = 1;
} else if (!strcmp(argv[i], "-6")) {
if (local_family_set && (local_family != AF_INET6)) {
log_fatal("Server cannot run in both IPv4 and "
"IPv6 mode at the same time.");
}
local_family = AF_INET6;
local_family_set = 1;
#endif /* DHCPv6 */
} else if (!strcmp (argv [i], "--version")) {
log_info("isc-dhcpd-%s", PACKAGE_VERSION);
exit (0);
#if defined (TRACING)
} else if (!strcmp (argv [i], "-tf")) {
if (++i == argc)
usage ();
traceoutfile = argv [i];
} else if (!strcmp (argv [i], "-play")) {
if (++i == argc)
usage ();
traceinfile = argv [i];
trace_replay_init ();
#endif /* TRACING */
} else if (argv [i][0] == '-') {
usage ();
} else {
struct interface_info *tmp =
(struct interface_info *)0;
result = interface_allocate (&tmp, MDL);
if (result != ISC_R_SUCCESS)
log_fatal ("Insufficient memory to %s %s: %s",
"record interface", argv [i],
isc_result_totext (result));
strcpy (tmp -> name, argv [i]);
if (interfaces) {
interface_reference (&tmp -> next,
interfaces, MDL);
interface_dereference (&interfaces, MDL);
}
interface_reference (&interfaces, tmp, MDL);
tmp -> flags = INTERFACE_REQUESTED;
}
}
if (!no_dhcpd_conf && (s = getenv ("PATH_DHCPD_CONF"))) {
path_dhcpd_conf = s;
}
#ifdef DHCPv6
if (local_family == AF_INET6) {
/* DHCPv6: override DHCPv4 lease and pid filenames */
if (!no_dhcpd_db) {
if ((s = getenv ("PATH_DHCPD6_DB")))
path_dhcpd_db = s;
else
path_dhcpd_db = _PATH_DHCPD6_DB;
}
if (!no_dhcpd_pid) {
if ((s = getenv ("PATH_DHCPD6_PID")))
path_dhcpd_pid = s;
else
path_dhcpd_pid = _PATH_DHCPD6_PID;
}
} else
#else /* !DHCPv6 */
{
if (!no_dhcpd_db && (s = getenv ("PATH_DHCPD_DB"))) {
path_dhcpd_db = s;
}
if (!no_dhcpd_pid && (s = getenv ("PATH_DHCPD_PID"))) {
path_dhcpd_pid = s;
}
}
#endif /* DHCPv6 */
/*
* convert relative path names to absolute, for files that need
* to be reopened after chdir() has been called
*/
if (path_dhcpd_db[0] != '/') {
char *path = dmalloc(PATH_MAX, MDL);
if (path == NULL)
log_fatal("No memory for filename\n");
path_dhcpd_db = realpath(path_dhcpd_db, path);
if (path_dhcpd_db == NULL)
log_fatal("%s: %s", path, strerror(errno));
}
if (!quiet) {
log_info("%s %s", message, PACKAGE_VERSION);
log_info (copyright);
log_info (arr);
log_info (url);
} else {
quiet = 0;
log_perror = 0;
}
#if defined (TRACING)
trace_init (set_time, MDL);
if (traceoutfile) {
result = trace_begin (traceoutfile, MDL);
if (result != ISC_R_SUCCESS)
log_fatal ("Unable to begin trace: %s",
isc_result_totext (result));
}
interface_trace_setup ();
parse_trace_setup ();
trace_srandom = trace_type_register ("random-seed", (void *)0,
trace_seed_input,
trace_seed_stop, MDL);
#endif
/* Default to the DHCP/BOOTP port. */
if (!local_port)
{
if ((s = getenv ("DHCPD_PORT"))) {
local_port = validate_port (s);
log_debug ("binding to environment-specified port %d",
ntohs (local_port));
} else {
if (local_family == AF_INET) {
ent = getservbyname("dhcp", "udp");
if (ent == NULL) {
local_port = htons(67);
} else {
local_port = ent->s_port;
}
} else {
/* INSIST(local_family == AF_INET6); */
ent = getservbyname("dhcpv6-server", "udp");
if (ent == NULL) {
local_port = htons(547);
} else {
local_port = ent->s_port;
}
}
#ifndef __CYGWIN32__ /* XXX */
endservent ();
#endif
}
}
if (local_family == AF_INET) {
remote_port = htons(ntohs(local_port) + 1);
} else {
/* INSIST(local_family == AF_INET6); */
ent = getservbyname("dhcpv6-client", "udp");
if (ent == NULL) {
remote_port = htons(546);
} else {
remote_port = ent->s_port;
}
}
if (server) {
if (local_family != AF_INET) {
log_fatal("You can only specify address to send "
"replies to when running an IPv4 server.");
}
if (!inet_aton (server, &limited_broadcast)) {
struct hostent *he;
he = gethostbyname (server);
if (he) {
memcpy (&limited_broadcast,
he -> h_addr_list [0],
sizeof limited_broadcast);
} else
limited_broadcast.s_addr = INADDR_BROADCAST;
}
} else {
limited_broadcast.s_addr = INADDR_BROADCAST;
}
/* Get the current time... */
time(&cur_time);
/* Set up the initial dhcp option universe. */
initialize_common_option_spaces ();
initialize_server_option_spaces ();
/* Add the ddns update style enumeration prior to parsing. */
add_enumeration (&ddns_styles);
add_enumeration (&syslog_enum);
if (!group_allocate (&root_group, MDL))
log_fatal ("Can't allocate root group!");
root_group -> authoritative = 0;
/* Set up various hooks. */
dhcp_interface_setup_hook = dhcpd_interface_setup_hook;
bootp_packet_handler = do_packet;
#ifdef DHCPv6
dhcpv6_packet_handler = do_packet6;
#endif /* DHCPv6 */
#if defined (NSUPDATE)
/* Set up the standard name service updater routine. */
parse = NULL;
status = new_parse(&parse, -1, std_nsupdate, sizeof(std_nsupdate) - 1,
"standard name service update routine", 0);
if (status != ISC_R_SUCCESS)
log_fatal ("can't begin parsing name service updater!");
if (parse != NULL) {
lose = 0;
if (!(parse_executable_statements(&root_group->statements,
parse, &lose, context_any))) {
end_parse(&parse);
log_fatal("can't parse standard name service updater!");
}
end_parse(&parse);
}
#endif
/* Initialize icmp support... */
if (!cftest && !lftest)
icmp_startup (1, lease_pinged);
#if defined (TRACING)
if (traceinfile) {
if (!no_dhcpd_db) {
log_error ("%s", "");
log_error ("** You must specify a lease file with -lf.");
log_error (" Dhcpd will not overwrite your default");
log_fatal (" lease file when playing back a trace. **");
}
trace_file_replay (traceinfile);
#if defined (DEBUG_MEMORY_LEAKAGE) && \
defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
free_everything ();
omapi_print_dmalloc_usage_by_caller ();
#endif
exit (0);
}
#endif
#ifdef DHCPv6
/* set up DHCPv6 hash */
if (!ia_na_new_hash(&ia_active, DEFAULT_HASH_SIZE, MDL)) {
log_fatal("Out of memory creating hash for active IA.");
}
#endif /* DHCPv6 */
/* Read the dhcpd.conf file... */
if (readconf () != ISC_R_SUCCESS)
log_fatal ("Configuration file errors encountered -- exiting");
postconf_initialization (quiet);
/* test option should cause an early exit */
if (cftest && !lftest)
exit(0);
group_write_hook = group_writer;
/* Start up the database... */
db_startup (lftest);
if (lftest)
exit (0);
/* Discover all the network interfaces and initialize them. */
discover_interfaces(DISCOVER_SERVER);
#ifdef DHCPv6
/*
* Remove addresses from our pools that we should not issue
* to clients.
*
* We currently have no support for this in IPv4. It is not
* as important in IPv4, as making pools with ranges that
* leave out interfaces and hosts is fairly straightforward
* using range notation, but not so handy with CIDR notation.
*/
if (local_family == AF_INET6) {
mark_hosts_unavailable();
mark_interfaces_unavailable();
}
#endif /* DHCPv6 */
/* Make up a seed for the random number generator from current
time plus the sum of the last four bytes of each
interface's hardware address interpreted as an integer.
Not much entropy, but we're booting, so we're not likely to
find anything better. */
seed = 0;
for (ip = interfaces; ip; ip = ip -> next) {
int junk;
memcpy (&junk,
&ip -> hw_address.hbuf [ip -> hw_address.hlen -
sizeof seed], sizeof seed);
seed += junk;
}
srandom (seed + cur_time);
#if defined (TRACING)
trace_seed_stash (trace_srandom, seed + cur_time);
#endif
postdb_startup ();
#ifdef DHCPv6
/*
* Set server DHCPv6 identifier.
* See dhcpv6.c for discussion of setting DUID.
*/
if (set_server_duid_from_option() == ISC_R_SUCCESS) {
write_server_duid();
} else {
if (!server_duid_isset()) {
if (generate_new_server_duid() != ISC_R_SUCCESS) {
log_fatal("Unable to set server identifier.");
}
write_server_duid();
}
}
#endif /* DHCPv6 */
#ifndef DEBUG
if (daemon) {
/* First part of becoming a daemon... */
if ((pid = fork ()) < 0)
log_fatal ("Can't fork daemon: %m");
else if (pid)
exit (0);
}
/* Read previous pid file. */
if ((i = open (path_dhcpd_pid, O_RDONLY)) >= 0) {
status = read(i, pbuf, (sizeof pbuf) - 1);
close (i);
if (status > 0) {
pbuf[status] = 0;
pid = atoi(pbuf);
/*
* If there was a previous server process and it's
* is still running, abort
*/
if (!pid || (pid != getpid() && kill(pid, 0) == 0))
log_fatal("There's already a "
"DHCP server running.");
}
}
/* Write new pid file. */
if ((i = open(path_dhcpd_pid, O_WRONLY|O_CREAT|O_TRUNC, 0644)) >= 0) {
sprintf(pbuf, "%d\n", (int) getpid());
write(i, pbuf, strlen(pbuf));
close(i);
} else {
log_error("Can't create PID file %s: %m.", path_dhcpd_pid);
}
/* If we were requested to log to stdout on the command line,
keep doing so; otherwise, stop. */
if (log_perror == -1)
log_perror = 1;
else
log_perror = 0;
if (daemon) {
/* Become session leader and get pid... */
pid = setsid();
/* Close standard I/O descriptors. */
close(0);
close(1);
close(2);
/* Reopen them on /dev/null. */
open("/dev/null", O_RDWR);
open("/dev/null", O_RDWR);
open("/dev/null", O_RDWR);
log_perror = 0; /* No sense logging to /dev/null. */
chdir("/");
}
#endif /* !DEBUG */
#if defined (DEBUG_MEMORY_LEAKAGE) || defined (DEBUG_MALLOC_POOL) || \
defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
dmalloc_cutoff_generation = dmalloc_generation;
dmalloc_longterm = dmalloc_outstanding;
dmalloc_outstanding = 0;
#endif
omapi_set_int_value ((omapi_object_t *)dhcp_control_object,
(omapi_object_t *)0, "state", server_running);
/* Receive packets and dispatch them... */
dispatch ();
/* Not reached */
return 0;
}
#endif /* !UNIT_TEST */
void postconf_initialization (int quiet)
{
struct option_state *options = (struct option_state *)0;
struct data_string db;
struct option_cache *oc;
char *s;
isc_result_t result;
struct parse *parse;
int tmp;
/* Now try to get the lease file name. */
option_state_allocate (&options, MDL);
execute_statements_in_scope ((struct binding_value **)0,
(struct packet *)0,
(struct lease *)0,
(struct client_state *)0,
(struct option_state *)0,
options, &global_scope,
root_group,
(struct group *)0);
memset (&db, 0, sizeof db);
oc = lookup_option (&server_universe, options, SV_LEASE_FILE_NAME);
if (oc &&
evaluate_option_cache (&db, (struct packet *)0,
(struct lease *)0, (struct client_state *)0,
options, (struct option_state *)0,
&global_scope, oc, MDL)) {
s = dmalloc (db.len + 1, MDL);
if (!s)
log_fatal ("no memory for lease db filename.");
memcpy (s, db.data, db.len);
s [db.len] = 0;
data_string_forget (&db, MDL);
path_dhcpd_db = s;
}
oc = lookup_option (&server_universe, options, SV_PID_FILE_NAME);
if (oc &&
evaluate_option_cache (&db, (struct packet *)0,
(struct lease *)0, (struct client_state *)0,
options, (struct option_state *)0,
&global_scope, oc, MDL)) {
s = dmalloc (db.len + 1, MDL);
if (!s)
log_fatal ("no memory for lease db filename.");
memcpy (s, db.data, db.len);
s [db.len] = 0;
data_string_forget (&db, MDL);
path_dhcpd_pid = s;
}
#ifdef DHCPv6
if (local_family == AF_INET6) {
/*
* Override lease file name with dhcpv6 lease file name,
* if it was set; then, do the same with the pid file name
*/
oc = lookup_option(&server_universe, options,
SV_DHCPV6_LEASE_FILE_NAME);
if (oc &&
evaluate_option_cache(&db, NULL, NULL, NULL,
options, NULL, &global_scope,
oc, MDL)) {
s = dmalloc (db.len + 1, MDL);
if (!s)
log_fatal ("no memory for lease db filename.");
memcpy (s, db.data, db.len);
s [db.len] = 0;
data_string_forget (&db, MDL);
path_dhcpd_db = s;
}
oc = lookup_option(&server_universe, options,
SV_DHCPV6_PID_FILE_NAME);
if (oc &&
evaluate_option_cache(&db, NULL, NULL, NULL,
options, NULL, &global_scope,
oc, MDL)) {
s = dmalloc (db.len + 1, MDL);
if (!s)
log_fatal ("no memory for lease db filename.");
memcpy (s, db.data, db.len);
s [db.len] = 0;
data_string_forget (&db, MDL);
path_dhcpd_pid = s;
}
}
#endif /* DHCPv6 */
omapi_port = -1;
oc = lookup_option (&server_universe, options, SV_OMAPI_PORT);
if (oc &&
evaluate_option_cache (&db, (struct packet *)0,
(struct lease *)0, (struct client_state *)0,
options, (struct option_state *)0,
&global_scope, oc, MDL)) {
if (db.len == 2) {
omapi_port = getUShort (db.data);
} else
log_fatal ("invalid omapi port data length");
data_string_forget (&db, MDL);
}
oc = lookup_option (&server_universe, options, SV_OMAPI_KEY);
if (oc &&
evaluate_option_cache (&db, (struct packet *)0,
(struct lease *)0, (struct client_state *)0,
options,
(struct option_state *)0,
&global_scope, oc, MDL)) {
s = dmalloc (db.len + 1, MDL);
if (!s)
log_fatal ("no memory for OMAPI key filename.");
memcpy (s, db.data, db.len);
s [db.len] = 0;
data_string_forget (&db, MDL);
result = omapi_auth_key_lookup_name (&omapi_key, s);
dfree (s, MDL);
if (result != ISC_R_SUCCESS)
log_fatal ("OMAPI key %s: %s",
s, isc_result_totext (result));
}
oc = lookup_option (&server_universe, options, SV_LOCAL_PORT);
if (oc &&
evaluate_option_cache (&db, (struct packet *)0,
(struct lease *)0, (struct client_state *)0,
options,
(struct option_state *)0,
&global_scope, oc, MDL)) {
if (db.len == 2) {
local_port = htons (getUShort (db.data));
} else
log_fatal ("invalid local port data length");
data_string_forget (&db, MDL);
}
oc = lookup_option (&server_universe, options, SV_REMOTE_PORT);
if (oc &&
evaluate_option_cache (&db, (struct packet *)0,
(struct lease *)0, (struct client_state *)0,
options, (struct option_state *)0,
&global_scope, oc, MDL)) {
if (db.len == 2) {
remote_port = htons (getUShort (db.data));
} else
log_fatal ("invalid remote port data length");
data_string_forget (&db, MDL);
}
oc = lookup_option (&server_universe, options,
SV_LIMITED_BROADCAST_ADDRESS);
if (oc &&
evaluate_option_cache (&db, (struct packet *)0,
(struct lease *)0, (struct client_state *)0,
options, (struct option_state *)0,
&global_scope, oc, MDL)) {
if (db.len == 4) {
memcpy (&limited_broadcast, db.data, 4);
} else
log_fatal ("invalid remote port data length");
data_string_forget (&db, MDL);
}
oc = lookup_option (&server_universe, options,
SV_LOCAL_ADDRESS);
if (oc &&
evaluate_option_cache (&db, (struct packet *)0,
(struct lease *)0, (struct client_state *)0,
options, (struct option_state *)0,
&global_scope, oc, MDL)) {
if (db.len == 4) {
memcpy (&local_address, db.data, 4);
} else
log_fatal ("invalid remote port data length");
data_string_forget (&db, MDL);
}
oc = lookup_option (&server_universe, options, SV_DDNS_UPDATE_STYLE);
if (oc) {
if (evaluate_option_cache (&db, (struct packet *)0,
(struct lease *)0,
(struct client_state *)0,
options,
(struct option_state *)0,
&global_scope, oc, MDL)) {
if (db.len == 1) {
ddns_update_style = db.data [0];
} else
log_fatal ("invalid dns update type");
data_string_forget (&db, MDL);
}
} else {
ddns_update_style = DDNS_UPDATE_STYLE_NONE;
}
oc = lookup_option (&server_universe, options, SV_LOG_FACILITY);
if (oc) {
if (evaluate_option_cache (&db, (struct packet *)0,
(struct lease *)0,
(struct client_state *)0,
options,
(struct option_state *)0,
&global_scope, oc, MDL)) {
if (db.len == 1) {
closelog ();
openlog ("dhcpd", LOG_NDELAY, db.data[0]);
/* Log the startup banner into the new
log file. */
if (!quiet) {
/* Don't log to stderr twice. */
tmp = log_perror;
log_perror = 0;
log_info("%s %s",
message, PACKAGE_VERSION);
log_info (copyright);
log_info (arr);
log_info (url);
log_perror = tmp;
}
} else
log_fatal ("invalid log facility");
data_string_forget (&db, MDL);
}
}
/* Don't need the options anymore. */
option_state_dereference (&options, MDL);
#if defined (NSUPDATE)
/* If old-style ddns updates have been requested, parse the
old-style ddns updater. */
if (ddns_update_style == 1) {
struct executable_statement **e, *s;
if (root_group -> statements) {
s = (struct executable_statement *)0;
if (!executable_statement_allocate (&s, MDL))
log_fatal ("no memory for ddns updater");
executable_statement_reference
(&s -> next, root_group -> statements, MDL);
executable_statement_dereference
(&root_group -> statements, MDL);
executable_statement_reference
(&root_group -> statements, s, MDL);
s -> op = statements_statement;
e = &s -> data.statements;
executable_statement_dereference (&s, MDL);
} else {
e = &root_group -> statements;
}
/* Set up the standard name service updater routine. */
parse = NULL;
result = new_parse(&parse, -1, old_nsupdate,
sizeof(old_nsupdate) - 1,
"old name service update routine", 0);
if (result != ISC_R_SUCCESS)
log_fatal ("can't begin parsing old ddns updater!");
if (parse != NULL) {
tmp = 0;
if (!(parse_executable_statements(e, parse, &tmp,
context_any))) {
end_parse(&parse);
log_fatal("can't parse standard ddns updater!");
}
}
end_parse(&parse);
}
#endif
}
void postdb_startup (void)
{
/* Initialize the omapi listener state. */
if (omapi_port != -1) {
omapi_listener_start (0);
}
#if defined (FAILOVER_PROTOCOL)
/* Initialize the failover listener state. */
dhcp_failover_startup ();
#endif
/*
* Begin our lease timeout background task.
*/
schedule_all_ipv6_lease_timeouts();
}
/* Print usage message. */
static void
usage(void) {
log_info("%s %s", message, PACKAGE_VERSION);
log_info(copyright);
log_info(arr);
log_fatal("Usage: dhcpd [-p <UDP port #>] [-f] [-d] [-q] [-t|-T]\n"
#ifdef DHCPv6
" [-4|-6] [-cf config-file] [-lf lease-file]\n"
#else /* !DHCPv6 */
" [-cf config-file] [-lf lease-file]\n"
#endif /* DHCPv6 */
#if defined (TRACING)
" [-tf trace-output-file]\n"
" [-play trace-input-file]\n"
#endif /* TRACING */
" [-pf pid-file] [-s server] [if0 [...ifN]]");
}
void lease_pinged (from, packet, length)
struct iaddr from;
u_int8_t *packet;
int length;
{
struct lease *lp;
/* Don't try to look up a pinged lease if we aren't trying to
ping one - otherwise somebody could easily make us churn by
just forging repeated ICMP EchoReply packets for us to look
up. */
if (!outstanding_pings)
return;
lp = (struct lease *)0;
if (!find_lease_by_ip_addr (&lp, from, MDL)) {
log_debug ("unexpected ICMP Echo Reply from %s",
piaddr (from));
return;
}
if (!lp -> state) {
#if defined (FAILOVER_PROTOCOL)
if (!lp -> pool ||
!lp -> pool -> failover_peer)
#endif
log_debug ("ICMP Echo Reply for %s late or spurious.",
piaddr (from));
goto out;
}
if (lp -> ends > cur_time) {
log_debug ("ICMP Echo reply while lease %s valid.",
piaddr (from));
}
/* At this point it looks like we pinged a lease and got a
response, which shouldn't have happened. */
data_string_forget (&lp -> state -> parameter_request_list, MDL);
free_lease_state (lp -> state, MDL);
lp -> state = (struct lease_state *)0;
abandon_lease (lp, "pinged before offer");
cancel_timeout (lease_ping_timeout, lp);
--outstanding_pings;
out:
lease_dereference (&lp, MDL);
}
void lease_ping_timeout (vlp)
void *vlp;
{
struct lease *lp = vlp;
#if defined (DEBUG_MEMORY_LEAKAGE)
unsigned long previous_outstanding = dmalloc_outstanding;
#endif
--outstanding_pings;
dhcp_reply (lp);
#if defined (DEBUG_MEMORY_LEAKAGE)
log_info ("generation %ld: %ld new, %ld outstanding, %ld long-term",
dmalloc_generation,
dmalloc_outstanding - previous_outstanding,
dmalloc_outstanding, dmalloc_longterm);
#endif
#if defined (DEBUG_MEMORY_LEAKAGE)
dmalloc_dump_outstanding ();
#endif
}
int dhcpd_interface_setup_hook (struct interface_info *ip, struct iaddr *ia)
{
struct subnet *subnet;
struct shared_network *share;
isc_result_t status;
/* Special case for fallback network - not sure why this is
necessary. */
if (!ia) {
const char *fnn = "fallback-net";
status = shared_network_allocate (&ip -> shared_network, MDL);
if (status != ISC_R_SUCCESS)
log_fatal ("No memory for shared subnet: %s",
isc_result_totext (status));
ip -> shared_network -> name = dmalloc (strlen (fnn) + 1, MDL);
strcpy (ip -> shared_network -> name, fnn);
return 1;
}
/* If there's a registered subnet for this address,
connect it together... */
subnet = (struct subnet *)0;
if (find_subnet (&subnet, *ia, MDL)) {
/* If this interface has multiple aliases on the same
subnet, ignore all but the first we encounter. */
if (!subnet -> interface) {
interface_reference (&subnet -> interface, ip, MDL);
subnet -> interface_address = *ia;
} else if (subnet -> interface != ip) {
log_error ("Multiple interfaces match the %s: %s %s",
"same subnet",
subnet -> interface -> name, ip -> name);
}
share = subnet -> shared_network;
if (ip -> shared_network &&
ip -> shared_network != share) {
log_fatal ("Interface %s matches multiple shared %s",
ip -> name, "networks");
} else {
if (!ip -> shared_network)
shared_network_reference
(&ip -> shared_network, share, MDL);
}
if (!share -> interface) {
interface_reference (&share -> interface, ip, MDL);
} else if (share -> interface != ip) {
log_error ("Multiple interfaces match the %s: %s %s",
"same shared network",
share -> interface -> name, ip -> name);
}
subnet_dereference (&subnet, MDL);
}
return 1;
}
static TIME shutdown_time;
static int omapi_connection_count;
enum dhcp_shutdown_state shutdown_state;
isc_result_t dhcp_io_shutdown (omapi_object_t *obj, void *foo)
{
/* Shut down all listeners. */
if (shutdown_state == shutdown_listeners &&
obj -> type == omapi_type_listener &&
obj -> inner &&
obj -> inner -> type == omapi_type_protocol_listener) {
omapi_listener_destroy (obj, MDL);
return ISC_R_SUCCESS;
}
/* Shut down all existing omapi connections. */
if (obj -> type == omapi_type_connection &&
obj -> inner &&
obj -> inner -> type == omapi_type_protocol) {
if (shutdown_state == shutdown_drop_omapi_connections) {
omapi_disconnect (obj, 1);
}
omapi_connection_count++;
if (shutdown_state == shutdown_omapi_connections) {
omapi_disconnect (obj, 0);
return ISC_R_SUCCESS;
}
}
/* Shutdown all DHCP interfaces. */
if (obj -> type == dhcp_type_interface &&
shutdown_state == shutdown_dhcp) {
dhcp_interface_remove (obj, (omapi_object_t *)0);
return ISC_R_SUCCESS;
}
return ISC_R_SUCCESS;
}
static isc_result_t dhcp_io_shutdown_countdown (void *vlp)
{
dhcp_failover_state_t *state;
#if defined (FAILOVER_PROTOCOL)
int failover_connection_count = 0;
#endif
oncemore:
if (shutdown_state == shutdown_listeners ||
shutdown_state == shutdown_omapi_connections ||
shutdown_state == shutdown_drop_omapi_connections ||
shutdown_state == shutdown_dhcp) {
omapi_connection_count = 0;
omapi_io_state_foreach (dhcp_io_shutdown, 0);
}
if ((shutdown_state == shutdown_listeners ||
shutdown_state == shutdown_omapi_connections ||
shutdown_state == shutdown_drop_omapi_connections) &&
omapi_connection_count == 0) {
shutdown_state = shutdown_dhcp;
shutdown_time = cur_time;
goto oncemore;
} else if (shutdown_state == shutdown_listeners &&
cur_time - shutdown_time > 4) {
shutdown_state = shutdown_omapi_connections;
shutdown_time = cur_time;
} else if (shutdown_state == shutdown_omapi_connections &&
cur_time - shutdown_time > 4) {
shutdown_state = shutdown_drop_omapi_connections;
shutdown_time = cur_time;
} else if (shutdown_state == shutdown_drop_omapi_connections &&
cur_time - shutdown_time > 4) {
shutdown_state = shutdown_dhcp;
shutdown_time = cur_time;
goto oncemore;
} else if (shutdown_state == shutdown_dhcp &&
cur_time - shutdown_time > 4) {
shutdown_state = shutdown_done;
shutdown_time = cur_time;
}
#if defined (FAILOVER_PROTOCOL)
/* Set all failover peers into the shutdown state. */
if (shutdown_state == shutdown_dhcp) {
for (state = failover_states; state; state = state -> next) {
if (state -> me.state == normal) {
dhcp_failover_set_state (state, shut_down);
failover_connection_count++;
}
if (state -> me.state == shut_down &&
state -> partner.state != partner_down)
failover_connection_count++;
}
}
if (shutdown_state == shutdown_done) {
for (state = failover_states; state; state = state -> next) {
if (state -> me.state == shut_down) {
if (state -> link_to_peer)
dhcp_failover_link_dereference (&state -> link_to_peer,
MDL);
dhcp_failover_set_state (state, recover);
}
}
#if defined (DEBUG_MEMORY_LEAKAGE) && \
defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
free_everything ();
omapi_print_dmalloc_usage_by_caller ();
#endif
exit (0);
}
#else
if (shutdown_state == shutdown_done) {
#if defined (DEBUG_MEMORY_LEAKAGE) && \
defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
free_everything ();
omapi_print_dmalloc_usage_by_caller ();
#endif
exit (0);
}
#endif
if (shutdown_state == shutdown_dhcp &&
#if defined(FAILOVER_PROTOCOL)
!failover_connection_count &&
#endif
ISC_TRUE) {
shutdown_state = shutdown_done;
shutdown_time = cur_time;
goto oncemore;
}
add_timeout (cur_time + 1,
(void (*)(void *))dhcp_io_shutdown_countdown, 0, 0, 0);
return ISC_R_SUCCESS;
}
isc_result_t dhcp_set_control_state (control_object_state_t oldstate,
control_object_state_t newstate)
{
if (newstate == server_shutdown) {
shutdown_time = cur_time;
shutdown_state = shutdown_listeners;
dhcp_io_shutdown_countdown (0);
return ISC_R_SUCCESS;
}
return ISC_R_INVALIDARG;
}
|