29 static const char copyright[] =
30 "Copyright 2004-2014 Internet Systems Consortium.";
31 static const char arr [] =
"All rights reserved.";
32 static const char message [] =
"Internet Systems Consortium DHCP Server";
33 static const char url [] =
34 "For info, please visit https://www.isc.org/software/dhcp/";
42 #include <sys/types.h>
45 #if defined (PARANOIA)
46 # include <sys/types.h>
50 # define group real_group
57 #ifdef HAVE_LIBSYSTEMD
58 #include <systemd/sd-daemon.h>
62 static void usage(
void);
68 #if defined (NSUPDATE)
73 char std_nsupdate [] =
" \n\
74 option server.ddns-hostname = \n\
75 pick (option fqdn.hostname, option host-name); \n\
76 option server.ddns-domainname = config-option domain-name; \n\
77 option server.ddns-rev-domainname = \"in-addr.arpa.\";";
105 return ISC_R_SUCCESS;
108 static void omapi_listener_start (
void *foo)
116 if (result != ISC_R_SUCCESS)
117 log_fatal (
"Can't allocate new generic object: %s",
118 isc_result_totext (result));
121 if (result == ISC_R_SUCCESS && omapi_key)
123 (listener, verify_addr, verify_auth);
124 if (result != ISC_R_SUCCESS) {
125 log_error (
"Can't start OMAPI protocol: %s",
126 isc_result_totext (result));
127 tv.tv_sec =
cur_tv.tv_sec + 5;
128 tv.tv_usec =
cur_tv.tv_usec;
134 #if defined (PARANOIA)
136 static void setup_chroot (
char *chroot_dir) {
138 log_fatal (
"you must be root to use chroot");
140 if (chroot(chroot_dir)) {
141 log_fatal (
"chroot(\"%s\"): %m", chroot_dir);
165 char *server = (
char *)0;
169 #if defined (NSUPDATE)
173 int no_dhcpd_conf = 0;
175 int no_dhcpd_pid = 0;
177 int local_family_set = 0;
179 #if defined (TRACING)
180 char *traceinfile = (
char *)0;
181 char *traceoutfile = (
char *)0;
184 #if defined (PARANOIA)
187 char *set_chroot = 0;
196 fd = open(
"/dev/null", O_RDWR | O_CLOEXEC);
198 fd = open(
"/dev/null", O_RDWR | O_CLOEXEC);
200 fd = open(
"/dev/null", O_RDWR | O_CLOEXEC);
209 if (status != ISC_R_SUCCESS)
210 log_fatal(
"Can't initialize context: %s",
211 isc_result_totext(status));
218 if (result != ISC_R_SUCCESS)
220 isc_result_totext (result));
231 for (i = 1; i < argc; i++) {
232 if (!strcmp (argv [i],
"-p")) {
236 log_debug (
"binding to user-specified port %d",
238 }
else if (!strcmp (argv [i],
"-f")) {
242 }
else if (!strcmp (argv [i],
"-d")) {
247 }
else if (!strcmp (argv [i],
"-s")) {
251 #if defined (PARANOIA)
252 }
else if (!strcmp (argv [i],
"-user")) {
256 }
else if (!strcmp (argv [i],
"-group")) {
259 set_group = argv [i];
260 }
else if (!strcmp (argv [i],
"-chroot")) {
263 set_chroot = argv [i];
265 }
else if (!strcmp (argv [i],
"-cf")) {
270 }
else if (!strcmp (argv [i],
"-lf")) {
275 }
else if (!strcmp (argv [i],
"-pf")) {
280 }
else if (!strcmp(argv[i],
"--no-pid")) {
282 }
else if (!strcmp (argv [i],
"-t")) {
289 }
else if (!strcmp (argv [i],
"-T")) {
297 }
else if (!strcmp (argv [i],
"-q")) {
301 }
else if (!strcmp(argv[i],
"-4")) {
303 log_fatal(
"Server cannot run in both IPv4 and "
304 "IPv6 mode at the same time.");
307 local_family_set = 1;
308 }
else if (!strcmp(argv[i],
"-6")) {
310 log_fatal(
"Server cannot run in both IPv4 and "
311 "IPv6 mode at the same time.");
314 local_family_set = 1;
316 }
else if (!strcmp (argv [i],
"--version")) {
317 const char vstring[] =
"isc-dhcpd-";
325 #if defined (TRACING)
326 }
else if (!strcmp (argv [i],
"-tf")) {
329 traceoutfile = argv [i];
330 }
else if (!strcmp (argv [i],
"-play")) {
333 traceinfile = argv [i];
336 }
else if (argv [i][0] ==
'-') {
341 if (strlen(argv[i]) >=
sizeof(tmp->
name))
344 argv[i], (
long)strlen(argv[i]));
345 result = interface_allocate (&tmp,
MDL);
346 if (result != ISC_R_SUCCESS)
347 log_fatal (
"Insufficient memory to %s %s: %s",
348 "record interface", argv [i],
349 isc_result_totext (result));
350 strcpy (tmp ->
name, argv [i]);
352 interface_reference (&tmp ->
next,
361 if (!no_dhcpd_conf && (s = getenv (
"PATH_DHCPD_CONF"))) {
369 if ((s = getenv (
"PATH_DHCPD6_DB")))
375 if ((s = getenv (
"PATH_DHCPD6_PID")))
383 if (!no_dhcpd_db && (s = getenv (
"PATH_DHCPD_DB"))) {
386 if (!no_dhcpd_pid && (s = getenv (
"PATH_DHCPD_PID"))) {
400 log_fatal(
"Failed to get realpath for %s: %s", path,
417 #if defined (TRACING)
421 if (result != ISC_R_SUCCESS)
423 isc_result_totext (result));
430 #if defined (NSUPDATE)
435 #if defined (PARANOIA)
438 struct passwd *tmp_pwd;
441 log_fatal (
"you must be root to set user");
443 if (!(tmp_pwd = getpwnam(set_user)))
444 log_fatal (
"no such user: %s", set_user);
446 set_uid = tmp_pwd->pw_uid;
450 set_gid = tmp_pwd->pw_gid;
455 #define group real_group
456 struct group *tmp_grp;
459 log_fatal (
"you must be root to set group");
461 if (!(tmp_grp = getgrnam(set_group)))
462 log_fatal (
"no such group: %s", set_group);
464 set_gid = tmp_grp->gr_gid;
468 # if defined (EARLY_CHROOT)
469 if (set_chroot) setup_chroot (set_chroot);
476 if ((s = getenv (
"DHCPD_PORT"))) {
478 log_debug (
"binding to environment-specified port %d",
482 ent = getservbyname(
"dhcp",
"udp");
490 ent = getservbyname(
"dhcpv6-server",
"udp");
507 ent = getservbyname(
"dhcpv6-client",
"udp");
517 log_fatal(
"You can only specify address to send "
518 "replies to when running an IPv4 server.");
522 he = gethostbyname (server);
525 he -> h_addr_list [0],
535 gettimeofday(&
cur_tv, NULL);
544 #if defined (LDAP_CONFIGURATION)
546 #if defined (LDAP_USE_SSL)
554 log_fatal (
"Can't allocate root group!");
564 #if defined (NSUPDATE)
567 status =
new_parse(&parse, -1, std_nsupdate,
sizeof(std_nsupdate) - 1,
568 "standard name service update routine", 0);
569 if (status != ISC_R_SUCCESS)
570 log_fatal (
"can't begin parsing name service updater!");
577 log_fatal(
"can't parse standard name service updater!");
584 if (!cftest && !lftest)
587 #if defined (TRACING)
591 log_error (
"** You must specify a lease file with -lf.");
592 log_error (
" Dhcpd will not overwrite your default");
593 log_fatal (
" lease file when playing back a trace. **");
597 #if defined (DEBUG_MEMORY_LEAKAGE) && \
598 defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
610 log_fatal(
"Out of memory creating hash for active IA_NA.");
613 log_fatal(
"Out of memory creating hash for active IA_TA.");
616 log_fatal(
"Out of memory creating hash for active IA_PD.");
622 log_fatal (
"Configuration file errors encountered -- exiting");
626 #if defined (PARANOIA) && !defined (EARLY_CHROOT)
627 if (set_chroot) setup_chroot (set_chroot);
631 if (cftest && !lftest)
637 #if defined (PARANOIA)
676 &ip -> hw_address.hbuf [ip -> hw_address.hlen -
677 sizeof seed],
sizeof seed);
681 #if defined (TRACING)
696 log_fatal(
"Unable to set server identifier.");
706 if ((pid = fork ()) < 0)
719 status = read(i, pbuf, (
sizeof pbuf) - 1);
730 (pid != getpid() && kill(pid, 0) == 0))
732 "DHCP server running.");
737 i = open(
path_dhcpd_pid, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0644);
739 sprintf(pbuf,
"%d\n", (
int) getpid());
743 log_error(
"Can't create PID file %s: %m.",
748 #if defined (PARANOIA)
752 if (setgroups (0, (
void *)0))
754 if (setgid (set_gid))
755 log_fatal (
"setgid(%d): %m", (
int) set_gid);
759 if (setuid (set_uid))
760 log_fatal (
"setuid(%d): %m", (
int) set_uid);
781 (void) open(
"/dev/null", O_RDWR | O_CLOEXEC);
782 (void) open(
"/dev/null", O_RDWR | O_CLOEXEC);
783 (void) open(
"/dev/null", O_RDWR | O_CLOEXEC);
790 #if defined (DEBUG_MEMORY_LEAKAGE) || defined (DEBUG_MALLOC_POOL) || \
791 defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
792 dmalloc_cutoff_generation = dmalloc_generation;
793 dmalloc_longterm = dmalloc_outstanding;
794 dmalloc_outstanding = 0;
800 #if defined(ENABLE_GENTLE_SHUTDOWN)
808 log_info(
"Server starting service.");
812 #ifdef HAVE_LIBSYSTEMD
814 sd_notifyf(0,
"READY=1\n"
815 "STATUS=Dispatching packets...\n"
817 (
unsigned long) getpid());
838 #if defined (NSUPDATE)
839 struct in_addr local4, *local4_ptr = NULL;
840 struct in6_addr local6, *local6_ptr = NULL;
849 memset(&db, 0,
sizeof db);
856 log_fatal(
"no memory for lease db filename.");
869 log_fatal(
"no memory for pid filename.");
889 log_fatal(
"no memory for lease db filename.");
903 log_fatal(
"no memory for pid filename.");
920 log_fatal(
"invalid omapi port data length");
930 log_fatal(
"no memory for OMAPI key filename.");
936 if (result != ISC_R_SUCCESS)
938 s, isc_result_totext (result));
948 log_fatal(
"invalid local port data length");
959 log_fatal(
"invalid remote port data length");
971 log_fatal(
"invalid broadcast address data length");
982 log_fatal(
"invalid local address data length");
999 #if defined (NSUPDATE)
1002 log_fatal(
"ddns-update-style ad_hoc no longer supported");
1010 memcpy(&local4, db.
data, 4);
1011 local4_ptr = &local4;
1022 memcpy(&local6, db.
data, 16);
1023 local6_ptr = &local6;
1031 log_fatal(
"Unable to complete ddns initialization");
1036 log_fatal(
"Support for ddns-update-style not compiled in");
1073 log_fatal(
"invalid max delayed ACK count ");
1085 log_fatal(
"invalid max ack delay configuration");
1099 log_error(
"Not using fsync() to flush lease writes");
1110 omapi_listener_start (0);
1113 #if defined (FAILOVER_PROTOCOL)
1132 log_fatal(
"Usage: dhcpd [-p <UDP port #>] [-f] [-d] [-q] [-t|-T]\n"
1134 " [-4|-6] [-cf config-file] [-lf lease-file]\n"
1136 " [-cf config-file] [-lf lease-file]\n"
1140 " [-user user] [-group group] [-chroot dir]\n"
1143 " [-tf trace-output-file]\n"
1144 " [-play trace-input-file]\n"
1146 " [-pf pid-file] [--no-pid] [-s server]\n"
1165 lp = (
struct lease *)0;
1167 log_debug (
"unexpected ICMP Echo Reply from %s",
1173 #if defined (FAILOVER_PROTOCOL)
1175 !lp ->
pool -> failover_peer)
1177 log_debug (
"ICMP Echo Reply for %s late or spurious.",
1183 log_debug (
"ICMP Echo reply while lease %s valid.",
1197 lease_dereference (&lp,
MDL);
1203 struct lease *lp = vlp;
1205 #if defined (DEBUG_MEMORY_LEAKAGE)
1206 unsigned long previous_outstanding = dmalloc_outstanding;
1212 #if defined (DEBUG_MEMORY_LEAKAGE)
1213 log_info (
"generation %ld: %ld new, %ld outstanding, %ld long-term",
1215 dmalloc_outstanding - previous_outstanding,
1216 dmalloc_outstanding, dmalloc_longterm);
1218 #if defined (DEBUG_MEMORY_LEAKAGE)
1219 dmalloc_dump_outstanding ();
1227 isc_result_t status;
1232 const char *fnn =
"fallback-net";
1234 if (status != ISC_R_SUCCESS)
1235 log_fatal (
"No memory for shared subnet: %s",
1236 isc_result_totext (status));
1244 subnet = (
struct subnet *)0;
1252 log_error (
"Multiple interfaces match the %s: %s %s",
1254 subnet ->
interface -> name, ip -> name);
1259 log_fatal (
"Interface %s matches multiple shared %s",
1260 ip -> name,
"networks");
1263 shared_network_reference
1270 log_error (
"Multiple interfaces match the %s: %s %s",
1271 "same shared network",
1272 share ->
interface -> name, ip -> name);
1274 subnet_dereference (&subnet,
MDL);
1279 static TIME shutdown_time;
1280 static int omapi_connection_count;
1291 return ISC_R_SUCCESS;
1301 omapi_connection_count++;
1304 return ISC_R_SUCCESS;
1312 return ISC_R_SUCCESS;
1314 return ISC_R_SUCCESS;
1317 static isc_result_t dhcp_io_shutdown_countdown (
void *vlp)
1319 #if defined (FAILOVER_PROTOCOL)
1320 dhcp_failover_state_t *state;
1321 int failover_connection_count = 0;
1330 omapi_connection_count = 0;
1337 omapi_connection_count == 0) {
1360 #if defined (FAILOVER_PROTOCOL)
1364 if (state -> me.state ==
normal) {
1366 failover_connection_count++;
1370 failover_connection_count++;
1377 if (state -> link_to_peer)
1378 dhcp_failover_link_dereference (&state -> link_to_peer,
1383 #if defined (DEBUG_MEMORY_LEAKAGE) && \
1384 defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
1394 #if defined (DEBUG_MEMORY_LEAKAGE) && \
1395 defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
1406 !failover_connection_count &&
1413 tv.tv_sec =
cur_tv.tv_sec + 1;
1414 tv.tv_usec =
cur_tv.tv_usec;
1416 (
void (*)(
void *))dhcp_io_shutdown_countdown, 0, 0, 0);
1417 return ISC_R_SUCCESS;
1429 return ISC_R_SUCCESS;
1435 dhcp_io_shutdown_countdown (0);
1436 return ISC_R_SUCCESS;
1446 tv.tv_sec =
cur_tv.tv_sec;
1447 tv.tv_usec =
cur_tv.tv_usec + 1;
1449 (
void (*)(
void *))dhcp_io_shutdown_countdown, 0, 0, 0);
1450 return ISC_R_SUCCESS;
void do_packet6(struct interface_info *, const char *, int, int, const struct iaddr *, isc_boolean_t)
const char * path_dhcpd_db
void initialize_server_option_spaces(void)
int dhcpd_interface_setup_hook(struct interface_info *ip, struct iaddr *ia)
void mark_hosts_unavailable(void)
isc_result_t omapi_protocol_listen(omapi_object_t *, unsigned, int)
void add_enumeration(struct enumeration *enumeration)
void parse_trace_setup(void)
struct binding_scope * global_scope
omapi_object_type_t * omapi_type_connection
void(* dhcpv6_packet_handler)(struct interface_info *, const char *, int, int, const struct iaddr *, isc_boolean_t)
#define SV_DDNS_UPDATE_STYLE
const char * piaddr(const struct iaddr addr)
isc_boolean_t server_duid_isset(void)
isc_result_t end_parse(struct parse **cfile)
void lease_ping_timeout(void *vlp)
void * dmalloc(unsigned, const char *, int)
struct lease_state * state
struct universe server_universe
#define SV_DONT_USE_FSYNC
void cancel_timeout(void(*)(void *) where, void *what)
void icmp_startup(int routep, void *handler)
#define DHCP_R_INVALIDARG
#define DDNS_UPDATE_STYLE_AD_HOC
int int int log_debug(const char *,...) __attribute__((__format__(__printf__
#define SV_DDNS_LOCAL_ADDRESS4
void db_startup(int, uid_t, gid_t)
isc_result_t dhcp_interface_remove(omapi_object_t *lp, omapi_object_t *id)
isc_result_t omapi_auth_key_lookup_name(omapi_auth_key_t **, const char *)
#define DHCP_CONTEXT_PRE_DB
isc_boolean_t no_pid_file
#define SV_LEASE_FILE_NAME
void data_string_forget(struct data_string *data, const char *file, int line)
struct group * root_group
int log_error(const char *,...) __attribute__((__format__(__printf__
trace_type_t * trace_type_register(const char *, void *, void(*)(trace_type_t *, unsigned, char *), void(*)(trace_type_t *), const char *, int)
void add_timeout(struct timeval *when, void(*)(void *) where, void *what, tvref_t ref, tvunref_t unref)
#define DHCP_R_INVALIDKEY
void omapi_print_dmalloc_usage_by_caller(void)
void mark_interfaces_unavailable(void)
void do_packet(struct interface_info *interface, struct dhcp_packet *packet, unsigned len, unsigned int from_port, struct iaddr from, struct hardware *hfrom)
void log_fatal(const char *,...) __attribute__((__format__(__printf__
isc_result_t omapi_io_state_foreach(isc_result_t(*func)(omapi_object_t *, void *), void *p)
calls a given function on every object
#define DHCP_CONTEXT_POST_DB
struct executable_statement * statements
void interface_trace_setup(void)
void free_lease_state(struct lease_state *, const char *, int)
omapi_object_type_t * omapi_type_listener
int server_identifier_matched
omapi_object_type_t * omapi_type_protocol
u_int16_t validate_port(char *port)
void dhcp_signal_handler(int signal)
void postconf_initialization(int quiet)
int find_subnet(struct subnet **sp, struct iaddr addr, const char *file, int line)
void execute_statements_in_scope(struct binding_value **result, struct packet *packet, struct lease *lease, struct client_state *client_state, struct option_state *in_options, struct option_state *out_options, struct binding_scope **scope, struct group *group, struct group *limiting_group, struct on_star *on_star)
isc_result_t readconf(void)
#define FAILOVER_PROTOCOL
int option_state_allocate(struct option_state **ptr, const char *file, int line)
isc_result_t dhcp_context_create(int flags, struct in_addr *local4, struct in6_addr *local6)
int evaluate_option_cache(struct data_string *result, struct packet *packet, struct lease *lease, struct client_state *client_state, struct option_state *in_options, struct option_state *cfg_options, struct binding_scope **scope, struct option_cache *oc, const char *file, int line)
isc_result_t dhcp_failover_set_state(dhcp_failover_state_t *, enum failover_state)
void trace_seed_stop(trace_type_t *)
#define DHCPD_LOG_FACILITY
void mark_phosts_unavailable(void)
struct iaddr interface_address
void abandon_lease(struct lease *, const char *)
isc_result_t dhcp_set_control_state(control_object_state_t oldstate, control_object_state_t newstate)
int group_allocate(struct group **ptr, const char *file, int line)
void(* bootp_packet_handler)(struct interface_info *, struct dhcp_packet *, unsigned, unsigned int, struct iaddr, struct hardware *)
isc_result_t omapi_listener_destroy(omapi_object_t *, const char *, int)
int write_server_duid(void)
int(* group_write_hook)(struct group_object *)
isc_result_t omapi_object_dereference(omapi_object_t **, const char *, int)
void trace_file_replay(const char *)
isc_result_t omapi_generic_new(omapi_object_t **, const char *, int)
omapi_object_type_t * omapi_type_protocol_listener
u_int32_t getUShort(const unsigned char *)
isc_result_t trace_begin(const char *, const char *, int)
void dfree(void *, const char *, int)
omapi_object_type_t * dhcp_type_interface
#define SV_DHCPV6_PID_FILE_NAME
dhcp_failover_state_t * failover_states
struct option_cache * lookup_option(struct universe *universe, struct option_state *options, unsigned code)
void dhcp_db_objects_setup(void)
struct in_addr limited_broadcast
int int log_info(const char *,...) __attribute__((__format__(__printf__
int main(int argc, char **argv)
void trace_seed_stash(trace_type_t *, unsigned)
struct interface_info * interfaces
u_int32_t getULong(const unsigned char *)
struct shared_network * shared_network
void trace_replay_init(void)
void lease_pinged(struct iaddr from, u_int8_t *packet, int length)
dhcp_control_object_t * dhcp_control_object
isc_result_t dhcp_io_shutdown(omapi_object_t *obj, void *foo)
#define DEFAULT_HASH_SIZE
void dhcp_failover_startup(void)
int(* dhcp_interface_setup_hook)(struct interface_info *, struct iaddr *)
int quiet_interface_discovery
isc_result_t trace_init(void(*set_time)(time_t), const char *, int)
int option_state_dereference(struct option_state **ptr, const char *file, int line)
void initialize_common_option_spaces()
void trace_ddns_init(void)
struct interface_info * next
int evaluate_boolean_option_cache(int *ignorep, struct packet *packet, struct lease *lease, struct client_state *client_state, struct option_state *in_options, struct option_state *cfg_options, struct binding_scope **scope, struct option_cache *oc, const char *file, int line)
void schedule_all_ipv6_lease_timeouts()
isc_result_t set_server_duid_from_option(void)
isc_result_t omapi_protocol_configure_security(omapi_object_t *, isc_result_t(*)(omapi_object_t *, omapi_addr_t *), isc_result_t(*)(omapi_object_t *, omapi_auth_key_t *))
#define DDNS_UPDATE_STYLE_NONE
#define SV_DHCPV6_LEASE_FILE_NAME
struct in_addr local_address
int dhcp_max_agent_option_packet_length
#define SV_DDNS_LOCAL_ADDRESS6
void classification_setup(void)
isc_result_t omapi_set_int_value(omapi_object_t *, omapi_object_t *, const char *, int)
#define SV_LIMITED_BROADCAST_ADDRESS
int group_writer(struct group_object *)
const char * path_dhcpd_conf
void dhcp_reply(struct lease *)
struct enumeration syslog_enum
int parse_executable_statements(struct executable_statement **statements, struct parse *cfile, int *lose, enum expression_context case_context)
isc_result_t omapi_disconnect(omapi_object_t *, int)
const unsigned char * data
void dhcp_common_objects_setup(void)
isc_result_t generate_new_server_duid(void)
enum dhcp_shutdown_state shutdown_state
struct iaddr server_identifier
const char * path_dhcpd_pid
void trace_seed_input(trace_type_t *, unsigned, char *)
int find_lease_by_ip_addr(struct lease **, struct iaddr, const char *, int)
struct enumeration ddns_styles
void discover_interfaces(int state)
struct interface_info * interface
isc_result_t new_parse(struct parse **cfile, int file, char *inbuf, unsigned buflen, const char *name, int eolp)
#define INTERFACE_REQUESTED
void postdb_startup(void)
isc_result_t omapi_init(void)