philipp / rpms / dhcp

Forked from rpms/dhcp 4 years ago
Clone

Blame SOURCES/dhcp-dhclient-options.patch

26a25c
diff --git a/client/clparse.c b/client/clparse.c
26a25c
index 03190c3..2033427 100644
26a25c
--- a/client/clparse.c
26a25c
+++ b/client/clparse.c
26a25c
@@ -189,6 +189,7 @@ isc_result_t read_client_conf ()
26a25c
 	/* Requested lease time, used by DHCPv6 (DHCPv4 uses the option cache)
26a25c
 	 */
26a25c
 	top_level_config.requested_lease = 7200;
26a25c
+	top_level_config.bootp_broadcast_always = 0;
26a25c
 
26a25c
 	group_allocate (&top_level_config.on_receipt, MDL);
26a25c
 	if (!top_level_config.on_receipt)
26a25c
@@ -394,7 +395,8 @@ void read_client_leases ()
26a25c
 	interface-declaration |
26a25c
 	LEASE client-lease-statement |
26a25c
 	ALIAS client-lease-statement |
26a25c
-	KEY key-definition */
26a25c
+	KEY key-definition |
26a25c
+	BOOTP_BROADCAST_ALWAYS */
26a25c
 
26a25c
 void parse_client_statement (cfile, ip, config)
26a25c
 	struct parse *cfile;
26a25c
@@ -817,6 +819,12 @@ void parse_client_statement (cfile, ip, config)
26a25c
 		parse_lease_id_format(cfile);
26a25c
 		break;
26a25c
 
26a25c
+	      case BOOTP_BROADCAST_ALWAYS:
26a25c
+		token = next_token(&val, (unsigned*)0, cfile);
26a25c
+		config -> bootp_broadcast_always = 1;
26a25c
+		parse_semi (cfile);
26a25c
+		return;
26a25c
+
26a25c
 
26a25c
 	      default:
26a25c
 		lose = 0;
26a25c
diff --git a/client/dhclient.8 b/client/dhclient.8
26a25c
index 24f8f12..aa2238d 100644
26a25c
--- a/client/dhclient.8
26a25c
+++ b/client/dhclient.8
26a25c
@@ -134,6 +134,33 @@ dhclient - Dynamic Host Configuration Protocol Client
26a25c
 .B -w
26a25c
 ]
26a25c
 [
26a25c
+.B -B
26a25c
+]
26a25c
+[
26a25c
+.B -C
26a25c
+.I dhcp-client-identifier
26a25c
+]
26a25c
+[
26a25c
+.B -H
26a25c
+.I host-name
26a25c
+]
26a25c
+[
26a25c
+.B -F
26a25c
+.I fqdn.fqdn
26a25c
+]
26a25c
+[
26a25c
+.B -V
26a25c
+.I vendor-class-identifier
26a25c
+]
26a25c
+[
26a25c
+.B --request-options
26a25c
+.I request-option-list
26a25c
+]
26a25c
+[
26a25c
+.B --timeout
26a25c
+.I timeout
26a25c
+]
26a25c
+[
26a25c
 .B --dad-wait-time
26a25c
 .I seconds
26a25c
 ]
26a25c
diff --git a/client/dhclient.c b/client/dhclient.c
26a25c
index dcf3f1a..270a960 100644
26a25c
--- a/client/dhclient.c
26a25c
+++ b/client/dhclient.c
26a25c
@@ -40,6 +40,12 @@
26a25c
 #include <isc/file.h>
26a25c
 #include <dns/result.h>
26a25c
 
26a25c
+/*
26a25c
+ * Defined in stdio.h when _GNU_SOURCE is set, but we don't want to define
26a25c
+ * that when building ISC code.
26a25c
+ */
26a25c
+extern int asprintf(char **strp, const char *fmt, ...);
26a25c
+
26a25c
 TIME default_lease_time = 43200; /* 12 hours... */
26a25c
 TIME max_lease_time = 86400; /* 24 hours... */
26a25c
 
26a25c
@@ -101,6 +107,10 @@ char *mockup_relay = NULL;
26a25c
 
26a25c
 char *progname = NULL;
26a25c
 
26a25c
+int bootp_broadcast_always = 0;
26a25c
+
26a25c
+extern struct option *default_requested_options[];
26a25c
+
26a25c
 void run_stateless(int exit_mode, u_int16_t port);
26a25c
 
26a25c
 static isc_result_t write_duid(struct data_string *duid);
26a25c
@@ -179,7 +189,11 @@ usage(const char *sfmt, const char *sarg)
26a25c
 		  "                [-s server-addr] [-cf config-file]\n"
26a25c
 		  "                [-df duid-file] [-lf lease-file]\n"
26a25c
 		  "                [-pf pid-file] [--no-pid] [-e VAR=val]\n"
26a25c
-		  "                [-sf script-file] [interface]*",
26a25c
+		  "                [-sf script-file] [interface]*\n"
26a25c
+		  "                [-C <dhcp-client-identifier>] [-B]\n"
26a25c
+		  "                [-H <host-name> | -F <fqdn.fqdn>] [--timeout <timeout>]\n"
26a25c
+		  "                [-V <vendor-class-identifier>]\n"
26a25c
+		  "                [--request-options <request option list>]",
26a25c
 		  isc_file_basename(progname));
26a25c
 }
26a25c
 
26a25c
@@ -216,6 +230,16 @@ main(int argc, char **argv) {
26a25c
 	progname = argv[0];
26a25c
 #endif
26a25c
 
26a25c
+	char *dhcp_client_identifier_arg = NULL;
26a25c
+	char *dhcp_host_name_arg = NULL;
26a25c
+	char *dhcp_fqdn_arg = NULL;
26a25c
+	char *dhcp_vendor_class_identifier_arg = NULL;
26a25c
+	char *dhclient_request_options = NULL;
26a25c
+
26a25c
+	int timeout_arg = 0;
26a25c
+	char *arg_conf = NULL;
26a25c
+	int arg_conf_len = 0;
26a25c
+
26a25c
 	/* Initialize client globals. */
26a25c
 	memset(&default_duid, 0, sizeof(default_duid));
26a25c
 
26a25c
@@ -442,6 +466,88 @@ main(int argc, char **argv) {
26a25c
 					 strlen(PACKAGE_VERSION)));
26a25c
 			IGNORE_RET(write(STDERR_FILENO, "\n", 1));
26a25c
 			exit(0);
26a25c
+		} else if (!strcmp(argv[i], "-C")) {
26a25c
+			if ((++i == argc) || (argv[i] == NULL) || (*(argv[i])=='\0')) {
26a25c
+				usage(use_noarg, argv[i-1]);
26a25c
+				exit(1);
26a25c
+			}
26a25c
+
26a25c
+			if (strlen(argv[i]) >= DHCP_MAX_OPTION_LEN) {
26a25c
+				log_error("-C option dhcp-client-identifier string \"%s\" is too long - maximum length is: %d", argv[i], DHCP_MAX_OPTION_LEN-1);
26a25c
+				exit(1);
26a25c
+			}
26a25c
+
26a25c
+			dhcp_client_identifier_arg = argv[i];
26a25c
+		} else if (!strcmp(argv[i], "-B")) {
26a25c
+			bootp_broadcast_always = 1;
26a25c
+		} else if (!strcmp(argv[i], "-H")) {
26a25c
+			if ((++i == argc) || (argv[i] == NULL) || (*(argv[i])=='\0')) {
26a25c
+				usage(use_noarg, argv[i-1]);
26a25c
+				exit(1);
26a25c
+			}
26a25c
+
26a25c
+			if (strlen(argv[i]) >= DHCP_MAX_OPTION_LEN) {
26a25c
+				log_error("-H option host-name string \"%s\" is too long - maximum length is: %d", argv[i], DHCP_MAX_OPTION_LEN-1);
26a25c
+				exit(1);
26a25c
+			}
26a25c
+
26a25c
+			if (dhcp_host_name_arg != NULL) {
26a25c
+				log_error("The -H <host-name> and -F <fqdn> arguments are mutually exclusive");
26a25c
+				exit(1);
26a25c
+			}
26a25c
+
26a25c
+			dhcp_host_name_arg = argv[i];
26a25c
+		} else if (!strcmp(argv[i], "-F")) {
26a25c
+			if ((++i == argc) || (argv[i] == NULL) || (*(argv[i])=='\0')) {
26a25c
+				usage(use_noarg, argv[i-1]);
26a25c
+				exit(1);
26a25c
+			}
26a25c
+
26a25c
+			if (strlen(argv[i]) >= DHCP_MAX_OPTION_LEN) {
26a25c
+				log_error("-F option fqdn.fqdn string \"%s\" is too long - maximum length is: %d", argv[i], DHCP_MAX_OPTION_LEN-1);
26a25c
+				exit(1);
26a25c
+			}
26a25c
+
26a25c
+			if (dhcp_fqdn_arg != NULL) {
26a25c
+				log_error("Only one -F <fqdn> argument can be specified");
26a25c
+				exit(1);
26a25c
+			}
26a25c
+
26a25c
+			if (dhcp_host_name_arg != NULL) {
26a25c
+				log_error("The -F <fqdn> and -H <host-name> arguments are mutually exclusive");
26a25c
+				exit(1);
26a25c
+			}
26a25c
+
26a25c
+			dhcp_fqdn_arg = argv[i];
26a25c
+		} else if (!strcmp(argv[i], "--timeout")) {
26a25c
+			if ((++i == argc) || (argv[i] == NULL) || (*(argv[i])=='\0')) {
26a25c
+				usage(use_noarg, argv[i-1]);
26a25c
+				exit(1);
26a25c
+			}
26a25c
+
26a25c
+			if ((timeout_arg = atoi(argv[i])) <= 0) {
26a25c
+				log_error("timeout option must be > 0 - bad value: %s",argv[i]);
26a25c
+				exit(1);
26a25c
+			}
26a25c
+		} else if (!strcmp(argv[i], "-V")) {
26a25c
+			if ((++i == argc) || (argv[i] == NULL) || (*(argv[i])=='\0')) {
26a25c
+				usage(use_noarg, argv[i-1]);
26a25c
+				exit(1);
26a25c
+			}
26a25c
+
26a25c
+			if (strlen(argv[i]) >= DHCP_MAX_OPTION_LEN) {
26a25c
+				log_error("-V option vendor-class-identifier string \"%s\" is too long - maximum length is: %d", argv[i], DHCP_MAX_OPTION_LEN-1);
26a25c
+				exit(1);
26a25c
+			}
26a25c
+
26a25c
+			dhcp_vendor_class_identifier_arg = argv[i];
26a25c
+		} else if (!strcmp(argv[i], "--request-options")) {
26a25c
+			if ((++i == argc) || (argv[i] == NULL) || (*(argv[i])=='\0')) {
26a25c
+				usage(use_noarg, argv[i-1]);
26a25c
+				exit(1);
26a25c
+			}
26a25c
+
26a25c
+			dhclient_request_options = argv[i];
26a25c
 		} else if (argv[i][0] == '-') {
26a25c
 			usage("Unknown command: %s", argv[i]);
26a25c
 		} else if (interfaces_requested < 0) {
26a25c
@@ -641,6 +747,156 @@ main(int argc, char **argv) {
26a25c
 	/* Parse the dhclient.conf file. */
26a25c
 	read_client_conf();
26a25c
 
26a25c
+	/* Parse any extra command line configuration arguments: */
26a25c
+	if ((dhcp_client_identifier_arg != NULL) && (*dhcp_client_identifier_arg != '\0')) {
26a25c
+		arg_conf_len = asprintf(&arg_conf, "send dhcp-client-identifier \"%s\";", dhcp_client_identifier_arg);
26a25c
+
26a25c
+		if ((arg_conf == 0) || (arg_conf_len <= 0))
26a25c
+			log_fatal("Unable to send -C option dhcp-client-identifier");
26a25c
+	}
26a25c
+
26a25c
+	if ((dhcp_host_name_arg != NULL) && (*dhcp_host_name_arg != '\0')) {
26a25c
+		if (arg_conf == 0) {
26a25c
+			arg_conf_len = asprintf(&arg_conf, "send host-name \"%s\";", dhcp_host_name_arg);
26a25c
+
26a25c
+			if ((arg_conf == 0) || (arg_conf_len <= 0))
26a25c
+				log_fatal("Unable to send -H option host-name");
26a25c
+		} else {
26a25c
+			char *last_arg_conf = arg_conf;
26a25c
+			arg_conf = NULL;
26a25c
+			arg_conf_len = asprintf(&arg_conf, "%s\nsend host-name \"%s\";", last_arg_conf, dhcp_host_name_arg);
26a25c
+
26a25c
+			if ((arg_conf == 0) || (arg_conf_len <= 0))
26a25c
+				log_fatal("Unable to send -H option host-name");
26a25c
+
26a25c
+			free(last_arg_conf);
26a25c
+		}
26a25c
+	}
26a25c
+
26a25c
+	if ((dhcp_fqdn_arg != NULL) && (*dhcp_fqdn_arg != '\0')) {
26a25c
+		if (arg_conf == 0) {
26a25c
+			arg_conf_len = asprintf(&arg_conf,  "send fqdn.fqdn \"%s\";", dhcp_fqdn_arg);
26a25c
+
26a25c
+			if ((arg_conf == 0) || (arg_conf_len <= 0))
26a25c
+				log_fatal("Unable to send -F option fqdn.fqdn");
26a25c
+		} else {
26a25c
+			char *last_arg_conf = arg_conf;
26a25c
+			arg_conf = NULL;
26a25c
+			arg_conf_len = asprintf(&arg_conf, "%s\nsend fqdn.fqdn \"%s\";", last_arg_conf, dhcp_fqdn_arg);
26a25c
+
26a25c
+			if ((arg_conf == 0)  || (arg_conf_len <= 0))
26a25c
+				log_fatal("Unable to send -F option fqdn.fqdn");
26a25c
+
26a25c
+			free(last_arg_conf);
26a25c
+		}
26a25c
+	}
26a25c
+
26a25c
+	if (timeout_arg) {
26a25c
+		if (arg_conf == 0) {
26a25c
+			arg_conf_len = asprintf(&arg_conf,  "timeout %d;", timeout_arg);
26a25c
+
26a25c
+			if ((arg_conf == 0) || (arg_conf_len <= 0))
26a25c
+				log_fatal("Unable to process --timeout timeout argument");
26a25c
+		} else {
26a25c
+			char *last_arg_conf = arg_conf;
26a25c
+			arg_conf = NULL;
26a25c
+			arg_conf_len = asprintf(&arg_conf, "%s\ntimeout %d;", last_arg_conf, timeout_arg);
26a25c
+
26a25c
+			if ((arg_conf == 0) || (arg_conf_len == 0))
26a25c
+				log_fatal("Unable to process --timeout timeout argument");
26a25c
+
26a25c
+			free(last_arg_conf);
26a25c
+		}
26a25c
+	}
26a25c
+
26a25c
+	if ((dhcp_vendor_class_identifier_arg != NULL) && (*dhcp_vendor_class_identifier_arg != '\0')) {
26a25c
+		if (arg_conf == 0) {
26a25c
+			arg_conf_len = asprintf(&arg_conf,  "send vendor-class-identifier \"%s\";", dhcp_vendor_class_identifier_arg);
26a25c
+
26a25c
+			if ((arg_conf == 0) || (arg_conf_len <= 0))
26a25c
+				log_fatal("Unable to send -V option vendor-class-identifier");
26a25c
+		} else {
26a25c
+			char *last_arg_conf = arg_conf;
26a25c
+			arg_conf = NULL;
26a25c
+			arg_conf_len = asprintf(&arg_conf, "%s\nsend vendor-class-identifier \"%s\";", last_arg_conf, dhcp_vendor_class_identifier_arg);
26a25c
+
26a25c
+			if ((arg_conf == 0) || (arg_conf_len <= 0))
26a25c
+				log_fatal("Unable to send -V option vendor-class-identifier");
26a25c
+
26a25c
+			free(last_arg_conf);
26a25c
+		}
26a25c
+	}
26a25c
+
26a25c
+	if (dhclient_request_options != NULL) {
26a25c
+		if (arg_conf == 0) {
26a25c
+			arg_conf_len = asprintf(&arg_conf,  "request %s;", dhclient_request_options);
26a25c
+
26a25c
+			if ((arg_conf == 0) || (arg_conf_len <= 0))
26a25c
+				log_fatal("Unable to parse --request-options <request options list> argument");
26a25c
+		} else {
26a25c
+			char *last_arg_conf = arg_conf;
26a25c
+			arg_conf = NULL;
26a25c
+			arg_conf_len = asprintf(&arg_conf, "%s\nrequest %s;", last_arg_conf, dhclient_request_options);
26a25c
+
26a25c
+			if ((arg_conf == 0)  || (arg_conf_len <= 0))
26a25c
+				log_fatal("Unable to parse --request-options <request options list> argument");
26a25c
+
26a25c
+			free(last_arg_conf);
26a25c
+		}
26a25c
+	}
26a25c
+
26a25c
+	if (arg_conf) {
26a25c
+		if (arg_conf_len == 0)
26a25c
+			if ((arg_conf_len = strlen(arg_conf)) == 0)
26a25c
+				/* huh ? cannot happen ! */
26a25c
+				log_fatal("Unable to process -C/-H/-F/--timeout/-V/--request-options configuration arguments");
26a25c
+
26a25c
+		/* parse the extra dhclient.conf configuration arguments
26a25c
+		 * into top level config: */
26a25c
+		struct parse *cfile = (struct parse *)0;
26a25c
+		const char *val = NULL;
26a25c
+		int token;
26a25c
+
26a25c
+		status = new_parse(&cfile, -1, arg_conf, arg_conf_len, "extra dhclient -C/-H/-F/--timeout/-V/--request-options configuration arguments", 0);
26a25c
+
26a25c
+		if ((status != ISC_R_SUCCESS) || (cfile -> warnings_occurred))
26a25c
+			log_fatal("Cannot parse -C/-H/-F/--timeout/-V/--request-options configuration arguments !");
26a25c
+		/* more detailed parse failures will be logged */
26a25c
+
26a25c
+		do {
26a25c
+			token = peek_token(&val, (unsigned *)0, cfile);
26a25c
+			if (token == END_OF_FILE)
26a25c
+				break;
26a25c
+
26a25c
+			parse_client_statement(cfile, (struct interface_info *)0, &top_level_config);
26a25c
+		} while (1);
26a25c
+
26a25c
+		if (cfile -> warnings_occurred)
26a25c
+			log_fatal("Cannot parse -C/-H/-F/--timeout/-V/--request-options configuration arguments !");
26a25c
+		end_parse(&cfile);
26a25c
+
26a25c
+		if (timeout_arg) {
26a25c
+			/* we just set the toplevel timeout, but per-client
26a25c
+			 * timeouts may still be at defaults.
26a25c
+			 */
26a25c
+			for (ip=interfaces; ip; ip = ip->next) {
26a25c
+				if (ip->client->config->timeout == 60)
26a25c
+					ip->client->config->timeout = timeout_arg;
26a25c
+			}
26a25c
+		}
26a25c
+
26a25c
+		if ((dhclient_request_options != 0) && (top_level_config.requested_options != default_requested_options)) {
26a25c
+			for (ip=interfaces; ip; ip = ip->next) {
26a25c
+				if (ip->client->config->requested_options == default_requested_options)
26a25c
+					ip->client->config->requested_options = top_level_config.requested_options;
26a25c
+			}
26a25c
+		}
26a25c
+
26a25c
+		free(arg_conf);
26a25c
+		arg_conf = NULL;
26a25c
+		arg_conf_len = 0;
26a25c
+	}
26a25c
+
26a25c
 	/* Parse the lease database. */
26a25c
 	read_client_leases();
26a25c
 
26a25c
@@ -3092,7 +3348,8 @@ void make_discover (client, lease)
26a25c
 	client -> packet.xid = random ();
26a25c
 	client -> packet.secs = 0; /* filled in by send_discover. */
26a25c
 
26a25c
-	if (can_receive_unicast_unconfigured (client -> interface))
26a25c
+	if ((!(bootp_broadcast_always || client->config->bootp_broadcast_always))
26a25c
+	    && can_receive_unicast_unconfigured(client->interface))
26a25c
 		client -> packet.flags = 0;
26a25c
 	else
26a25c
 		client -> packet.flags = htons (BOOTP_BROADCAST);
26a25c
@@ -3177,7 +3434,9 @@ void make_request (client, lease)
26a25c
 	} else {
26a25c
 		memset (&client -> packet.ciaddr, 0,
26a25c
 			sizeof client -> packet.ciaddr);
26a25c
-		if (can_receive_unicast_unconfigured (client -> interface))
26a25c
+		if ((!(bootp_broadcast_always ||
26a25c
+		    client ->config->bootp_broadcast_always)) &&
26a25c
+		    can_receive_unicast_unconfigured (client -> interface))
26a25c
 			client -> packet.flags = 0;
26a25c
 		else
26a25c
 			client -> packet.flags = htons (BOOTP_BROADCAST);
26a25c
@@ -3240,7 +3499,8 @@ void make_decline (client, lease)
26a25c
 	client -> packet.hops = 0;
26a25c
 	client -> packet.xid = client -> xid;
26a25c
 	client -> packet.secs = 0; /* Filled in by send_request. */
26a25c
-	if (can_receive_unicast_unconfigured (client -> interface))
26a25c
+	if ((!(bootp_broadcast_always || client->config-> bootp_broadcast_always))
26a25c
+	    && can_receive_unicast_unconfigured (client->interface))
26a25c
 		client -> packet.flags = 0;
26a25c
 	else
26a25c
 		client -> packet.flags = htons (BOOTP_BROADCAST);
26a25c
diff --git a/common/conflex.c b/common/conflex.c
26a25c
index fe994ac..bdb4a52 100644
26a25c
--- a/common/conflex.c
26a25c
+++ b/common/conflex.c
26a25c
@@ -832,6 +832,8 @@ intern(char *atom, enum dhcp_token dfv) {
26a25c
 		if (!strcasecmp(atom+1, "ig-endian")) {
26a25c
 			return TOKEN_BIG_ENDIAN;
26a25c
 		}
26a25c
+		if (!strcasecmp (atom + 1, "ootp-broadcast-always"))
26a25c
+			return BOOTP_BROADCAST_ALWAYS;
26a25c
 		break;
26a25c
 	      case 'c':
26a25c
 		if (!strcasecmp(atom + 1, "ase"))
26a25c
diff --git a/includes/dhcpd.h b/includes/dhcpd.h
26a25c
index eab09a6..cfdac23 100644
26a25c
--- a/includes/dhcpd.h
26a25c
+++ b/includes/dhcpd.h
26a25c
@@ -1251,6 +1251,9 @@ struct client_config {
26a25c
 
26a25c
 	int lease_id_format;		/* format for IDs in lease file,
26a25c
 					   TOKEN_OCTAL or TOKEN_HEX */
26a25c
+
26a25c
+	int bootp_broadcast_always;	/* If nonzero, always set the BOOTP_BROADCAST
26a25c
+					   flag in requests */
26a25c
 };
26a25c
 
26a25c
 /* Per-interface state used in the dhcp client... */
26a25c
diff --git a/includes/dhctoken.h b/includes/dhctoken.h
26a25c
index 15bbd1c..b312e7a 100644
26a25c
--- a/includes/dhctoken.h
26a25c
+++ b/includes/dhctoken.h
26a25c
@@ -373,7 +373,8 @@ enum dhcp_token {
26a25c
 	TOKEN_BIG_ENDIAN = 675,
26a25c
 	LEASE_ID_FORMAT = 676,
26a25c
 	TOKEN_HEX = 677,
26a25c
-	TOKEN_OCTAL = 678
26a25c
+	TOKEN_OCTAL = 678,
26a25c
+	BOOTP_BROADCAST_ALWAYS = 679
26a25c
 };
26a25c
 
26a25c
 #define is_identifier(x)	((x) >= FIRST_TOKEN &&	\