Blame SOURCES/0002-additional-dhclient-options.patch

df4638
From a26161b0fd45cdbeed3038ac63ff04e3b727248f Mon Sep 17 00:00:00 2001
df4638
From: Pavel Zhukov <pzhukov@redhat.com>
df4638
Date: Thu, 21 Feb 2019 10:19:47 +0100
df4638
Subject: [PATCH 02/26] additional dhclient options
df4638
Cc: pzhukov@redhat.com
df4638
df4638
---
df4638
 client/clparse.c    |  10 +-
df4638
 client/dhclient.8   |  27 ++++++
df4638
 client/dhclient.c   | 271 +++++++++++++++++++++++++++++++++++++++++++++++++++-
df4638
 common/conflex.c    |   2 +
df4638
 includes/dhcpd.h    |   3 +
df4638
 includes/dhctoken.h |   1 +
df4638
 6 files changed, 308 insertions(+), 6 deletions(-)
df4638
df4638
diff --git a/client/clparse.c b/client/clparse.c
df4638
index eaf48a8..7212e3a 100644
df4638
--- a/client/clparse.c
df4638
+++ b/client/clparse.c
df4638
@@ -189,6 +189,7 @@ isc_result_t read_client_conf ()
df4638
 	/* Requested lease time, used by DHCPv6 (DHCPv4 uses the option cache)
df4638
 	 */
df4638
 	top_level_config.requested_lease = 7200;
df4638
+	top_level_config.bootp_broadcast_always = 0;
df4638
 
df4638
 	group_allocate (&top_level_config.on_receipt, MDL);
df4638
 	if (!top_level_config.on_receipt)
df4638
@@ -394,7 +395,8 @@ void read_client_leases ()
df4638
 	interface-declaration |
df4638
 	LEASE client-lease-statement |
df4638
 	ALIAS client-lease-statement |
df4638
-	KEY key-definition */
df4638
+	KEY key-definition |
df4638
+	BOOTP_BROADCAST_ALWAYS */
df4638
 
df4638
 void parse_client_statement (cfile, ip, config)
df4638
 	struct parse *cfile;
df4638
@@ -817,6 +819,12 @@ void parse_client_statement (cfile, ip, config)
df4638
 		parse_lease_id_format(cfile);
df4638
 		break;
df4638
 
df4638
+	      case BOOTP_BROADCAST_ALWAYS:
df4638
+		token = next_token(&val, (unsigned*)0, cfile);
df4638
+		config -> bootp_broadcast_always = 1;
df4638
+		parse_semi (cfile);
df4638
+		return;
df4638
+
df4638
 
df4638
 	      default:
df4638
 		lose = 0;
df4638
diff --git a/client/dhclient.8 b/client/dhclient.8
df4638
index ebc750f..6d7fbdb 100644
df4638
--- a/client/dhclient.8
df4638
+++ b/client/dhclient.8
df4638
@@ -134,6 +134,33 @@ dhclient - Dynamic Host Configuration Protocol Client
df4638
 .B -w
df4638
 ]
df4638
 [
df4638
+.B -B
df4638
+]
df4638
+[
df4638
+.B -C
df4638
+.I dhcp-client-identifier
df4638
+]
df4638
+[
df4638
+.B -H
df4638
+.I host-name
df4638
+]
df4638
+[
df4638
+.B -F
df4638
+.I fqdn.fqdn
df4638
+]
df4638
+[
df4638
+.B -V
df4638
+.I vendor-class-identifier
df4638
+]
df4638
+[
df4638
+.B --request-options
df4638
+.I request-option-list
df4638
+]
df4638
+[
df4638
+.B --timeout
df4638
+.I timeout
df4638
+]
df4638
+[
df4638
 .B --dad-wait-time
df4638
 .I seconds
df4638
 ]
df4638
diff --git a/client/dhclient.c b/client/dhclient.c
df4638
index 825ab00..26a333c 100644
df4638
--- a/client/dhclient.c
df4638
+++ b/client/dhclient.c
df4638
@@ -41,6 +41,12 @@
df4638
 #include <sys/wait.h>
df4638
 #include <limits.h>
df4638
 
df4638
+/*
df4638
+ * Defined in stdio.h when _GNU_SOURCE is set, but we don't want to define
df4638
+ * that when building ISC code.
df4638
+ */
df4638
+extern int asprintf(char **strp, const char *fmt, ...);
df4638
+
df4638
 TIME default_lease_time = 43200; /* 12 hours... */
df4638
 TIME max_lease_time = 86400; /* 24 hours... */
df4638
 
df4638
@@ -110,6 +116,10 @@ char *mockup_relay = NULL;
df4638
 
df4638
 char *progname = NULL;
df4638
 
df4638
+int bootp_broadcast_always = 0;
df4638
+
df4638
+extern struct option *default_requested_options[];
df4638
+
df4638
 void run_stateless(int exit_mode, u_int16_t port);
df4638
 
df4638
 static isc_result_t write_duid(struct data_string *duid);
df4638
@@ -183,8 +193,12 @@ static const char use_v6command[] = "Command not used for DHCPv4: %s";
df4638
 "                [-s server-addr] [-cf config-file]\n" \
df4638
 "                [-df duid-file] [-lf lease-file]\n" \
df4638
 "                [-pf pid-file] [--no-pid] [-e VAR=val]\n" \
df4638
-"                [-sf script-file] [interface]*"
df4638
-
df4638
+"                [-sf script-file] [interface]*\n" \
df4638
+"                [-C <dhcp-client-identifier>] [-B]\n" \
df4638
+"                [-H <host-name> | -F <fqdn.fqdn>] [--timeout <timeout>]\n" \
df4638
+"                [-V <vendor-class-identifier>]\n" \
df4638
+"                [--request-options <request option list>]"
df4638
+  
df4638
 #define DHCLIENT_USAGEH "{--version|--help|-h}"
df4638
 
df4638
 static void
df4638
@@ -243,6 +257,16 @@ main(int argc, char **argv) {
df4638
 #else
df4638
 	progname = argv[0];
df4638
 #endif
df4638
+        char *dhcp_client_identifier_arg = NULL;
df4638
+        char *dhcp_host_name_arg = NULL;
df4638
+	char *dhcp_fqdn_arg = NULL;
df4638
+	char *dhcp_vendor_class_identifier_arg = NULL;
df4638
+	char *dhclient_request_options = NULL;
df4638
+
df4638
+	int timeout_arg = 0;
df4638
+	char *arg_conf = NULL;
df4638
+	int arg_conf_len = 0;
df4638
+
df4638
 	/* Initialize client globals. */
df4638
 	memset(&default_duid, 0, sizeof(default_duid));
df4638
 
df4638
@@ -558,6 +582,89 @@ main(int argc, char **argv) {
df4638
 			std_dhcid = 1;
df4638
 		} else if (!strcmp(argv[i], "-v")) {
df4638
 			quiet = 0;
df4638
+		} else if (!strcmp(argv[i], "-C")) {
df4638
+			if ((++i == argc) || (argv[i] == NULL) || (*(argv[i])=='\0')) {
df4638
+				usage(use_noarg, argv[i-1]);
df4638
+				exit(1);
df4638
+			}
df4638
+
df4638
+			if (strlen(argv[i]) >= DHCP_MAX_OPTION_LEN) {
df4638
+				log_error("-C option dhcp-client-identifier string \"%s\" is too long - maximum length is: %d", argv[i], DHCP_MAX_OPTION_LEN-1);
df4638
+				exit(1);
df4638
+			}
df4638
+
df4638
+			dhcp_client_identifier_arg = argv[i];
df4638
+		} else if (!strcmp(argv[i], "-B")) {
df4638
+			bootp_broadcast_always = 1;
df4638
+		} else if (!strcmp(argv[i], "-H")) {
df4638
+			if ((++i == argc) || (argv[i] == NULL) || (*(argv[i])=='\0')) {
df4638
+				usage(use_noarg, argv[i-1]);
df4638
+				exit(1);
df4638
+			}
df4638
+
df4638
+			if (strlen(argv[i]) >= DHCP_MAX_OPTION_LEN) {
df4638
+				log_error("-H option host-name string \"%s\" is too long - maximum length is: %d", argv[i], DHCP_MAX_OPTION_LEN-1);
df4638
+				exit(1);
df4638
+			}
df4638
+
df4638
+			if (dhcp_host_name_arg != NULL) {
df4638
+				log_error("The -H <host-name> and -F <fqdn> arguments are mutually exclusive");
df4638
+				exit(1);
df4638
+			}
df4638
+
df4638
+			dhcp_host_name_arg = argv[i];
df4638
+		} else if (!strcmp(argv[i], "-F")) {
df4638
+			if ((++i == argc) || (argv[i] == NULL) || (*(argv[i])=='\0')) {
df4638
+				usage(use_noarg, argv[i-1]);
df4638
+				exit(1);
df4638
+			}
df4638
+
df4638
+			if (strlen(argv[i]) >= DHCP_MAX_OPTION_LEN) {
df4638
+				log_error("-F option fqdn.fqdn string \"%s\" is too long - maximum length is: %d", argv[i], DHCP_MAX_OPTION_LEN-1);
df4638
+				exit(1);
df4638
+			}
df4638
+
df4638
+			if (dhcp_fqdn_arg != NULL) {
df4638
+				log_error("Only one -F <fqdn> argument can be specified");
df4638
+				exit(1);
df4638
+			}
df4638
+
df4638
+			if (dhcp_host_name_arg != NULL) {
df4638
+				log_error("The -F <fqdn> and -H <host-name> arguments are mutually exclusive");
df4638
+				exit(1);
df4638
+			}
df4638
+
df4638
+			dhcp_fqdn_arg = argv[i];
df4638
+		} else if (!strcmp(argv[i], "--timeout")) {
df4638
+			if ((++i == argc) || (argv[i] == NULL) || (*(argv[i])=='\0')) {
df4638
+				usage(use_noarg, argv[i-1]);
df4638
+				exit(1);
df4638
+			}
df4638
+
df4638
+			if ((timeout_arg = atoi(argv[i])) <= 0) {
df4638
+				log_error("timeout option must be > 0 - bad value: %s",argv[i]);
df4638
+				exit(1);
df4638
+			}
df4638
+		} else if (!strcmp(argv[i], "-V")) {
df4638
+			if ((++i == argc) || (argv[i] == NULL) || (*(argv[i])=='\0')) {
df4638
+				usage(use_noarg, argv[i-1]);
df4638
+				exit(1);
df4638
+			}
df4638
+
df4638
+			if (strlen(argv[i]) >= DHCP_MAX_OPTION_LEN) {
df4638
+				log_error("-V option vendor-class-identifier string \"%s\" is too long - maximum length is: %d", argv[i], DHCP_MAX_OPTION_LEN-1);
df4638
+				exit(1);
df4638
+			}
df4638
+
df4638
+			dhcp_vendor_class_identifier_arg = argv[i];
df4638
+		} else if (!strcmp(argv[i], "--request-options")) {
df4638
+			if ((++i == argc) || (argv[i] == NULL) || (*(argv[i])=='\0')) {
df4638
+				usage(use_noarg, argv[i-1]);
df4638
+				exit(1);
df4638
+			}
df4638
+
df4638
+			dhclient_request_options = argv[i];
df4638
+
df4638
 		} else if (argv[i][0] == '-') {
df4638
 			usage("Unknown command: %s", argv[i]);
df4638
 		} else if (interfaces_requested < 0) {
df4638
@@ -754,6 +861,156 @@ main(int argc, char **argv) {
df4638
 	/* Parse the dhclient.conf file. */
df4638
 	read_client_conf();
df4638
 
df4638
+	/* Parse any extra command line configuration arguments: */
df4638
+	if ((dhcp_client_identifier_arg != NULL) && (*dhcp_client_identifier_arg != '\0')) {
df4638
+		arg_conf_len = asprintf(&arg_conf, "send dhcp-client-identifier \"%s\";", dhcp_client_identifier_arg);
df4638
+
df4638
+		if ((arg_conf == 0) || (arg_conf_len <= 0))
df4638
+			log_fatal("Unable to send -C option dhcp-client-identifier");
df4638
+	}
df4638
+
df4638
+	if ((dhcp_host_name_arg != NULL) && (*dhcp_host_name_arg != '\0')) {
df4638
+		if (arg_conf == 0) {
df4638
+			arg_conf_len = asprintf(&arg_conf, "send host-name \"%s\";", dhcp_host_name_arg);
df4638
+
df4638
+			if ((arg_conf == 0) || (arg_conf_len <= 0))
df4638
+				log_fatal("Unable to send -H option host-name");
df4638
+		} else {
df4638
+			char *last_arg_conf = arg_conf;
df4638
+			arg_conf = NULL;
df4638
+			arg_conf_len = asprintf(&arg_conf, "%s\nsend host-name \"%s\";", last_arg_conf, dhcp_host_name_arg);
df4638
+
df4638
+			if ((arg_conf == 0) || (arg_conf_len <= 0))
df4638
+				log_fatal("Unable to send -H option host-name");
df4638
+
df4638
+			free(last_arg_conf);
df4638
+		}
df4638
+	}
df4638
+
df4638
+	if ((dhcp_fqdn_arg != NULL) && (*dhcp_fqdn_arg != '\0')) {
df4638
+		if (arg_conf == 0) {
df4638
+			arg_conf_len = asprintf(&arg_conf,  "send fqdn.fqdn \"%s\";", dhcp_fqdn_arg);
df4638
+
df4638
+			if ((arg_conf == 0) || (arg_conf_len <= 0))
df4638
+				log_fatal("Unable to send -F option fqdn.fqdn");
df4638
+		} else {
df4638
+			char *last_arg_conf = arg_conf;
df4638
+			arg_conf = NULL;
df4638
+			arg_conf_len = asprintf(&arg_conf, "%s\nsend fqdn.fqdn \"%s\";", last_arg_conf, dhcp_fqdn_arg);
df4638
+
df4638
+			if ((arg_conf == 0)  || (arg_conf_len <= 0))
df4638
+				log_fatal("Unable to send -F option fqdn.fqdn");
df4638
+
df4638
+			free(last_arg_conf);
df4638
+		}
df4638
+	}
df4638
+
df4638
+	if (timeout_arg) {
df4638
+		if (arg_conf == 0) {
df4638
+			arg_conf_len = asprintf(&arg_conf,  "timeout %d;", timeout_arg);
df4638
+
df4638
+			if ((arg_conf == 0) || (arg_conf_len <= 0))
df4638
+				log_fatal("Unable to process --timeout timeout argument");
df4638
+		} else {
df4638
+			char *last_arg_conf = arg_conf;
df4638
+			arg_conf = NULL;
df4638
+			arg_conf_len = asprintf(&arg_conf, "%s\ntimeout %d;", last_arg_conf, timeout_arg);
df4638
+
df4638
+			if ((arg_conf == 0) || (arg_conf_len == 0))
df4638
+				log_fatal("Unable to process --timeout timeout argument");
df4638
+
df4638
+			free(last_arg_conf);
df4638
+		}
df4638
+	}
df4638
+
df4638
+	if ((dhcp_vendor_class_identifier_arg != NULL) && (*dhcp_vendor_class_identifier_arg != '\0')) {
df4638
+		if (arg_conf == 0) {
df4638
+			arg_conf_len = asprintf(&arg_conf,  "send vendor-class-identifier \"%s\";", dhcp_vendor_class_identifier_arg);
df4638
+
df4638
+			if ((arg_conf == 0) || (arg_conf_len <= 0))
df4638
+				log_fatal("Unable to send -V option vendor-class-identifier");
df4638
+		} else {
df4638
+			char *last_arg_conf = arg_conf;
df4638
+			arg_conf = NULL;
df4638
+			arg_conf_len = asprintf(&arg_conf, "%s\nsend vendor-class-identifier \"%s\";", last_arg_conf, dhcp_vendor_class_identifier_arg);
df4638
+
df4638
+			if ((arg_conf == 0) || (arg_conf_len <= 0))
df4638
+				log_fatal("Unable to send -V option vendor-class-identifier");
df4638
+
df4638
+			free(last_arg_conf);
df4638
+		}
df4638
+	}
df4638
+
df4638
+	if (dhclient_request_options != NULL) {
df4638
+		if (arg_conf == 0) {
df4638
+			arg_conf_len = asprintf(&arg_conf,  "request %s;", dhclient_request_options);
df4638
+
df4638
+			if ((arg_conf == 0) || (arg_conf_len <= 0))
df4638
+				log_fatal("Unable to parse --request-options <request options list> argument");
df4638
+		} else {
df4638
+			char *last_arg_conf = arg_conf;
df4638
+			arg_conf = NULL;
df4638
+			arg_conf_len = asprintf(&arg_conf, "%s\nrequest %s;", last_arg_conf, dhclient_request_options);
df4638
+
df4638
+			if ((arg_conf == 0)  || (arg_conf_len <= 0))
df4638
+				log_fatal("Unable to parse --request-options <request options list> argument");
df4638
+
df4638
+			free(last_arg_conf);
df4638
+		}
df4638
+	}
df4638
+
df4638
+	if (arg_conf) {
df4638
+		if (arg_conf_len == 0)
df4638
+			if ((arg_conf_len = strlen(arg_conf)) == 0)
df4638
+				/* huh ? cannot happen ! */
df4638
+				log_fatal("Unable to process -C/-H/-F/--timeout/-V/--request-options configuration arguments");
df4638
+
df4638
+		/* parse the extra dhclient.conf configuration arguments
df4638
+		 * into top level config: */
df4638
+		struct parse *cfile = (struct parse *)0;
df4638
+		const char *val = NULL;
df4638
+		int token;
df4638
+
df4638
+		status = new_parse(&cfile, -1, arg_conf, arg_conf_len, "extra dhclient -C/-H/-F/--timeout/-V/--request-options configuration arguments", 0);
df4638
+
df4638
+		if ((status != ISC_R_SUCCESS) || (cfile -> warnings_occurred))
df4638
+			log_fatal("Cannot parse -C/-H/-F/--timeout/-V/--request-options configuration arguments !");
df4638
+		/* more detailed parse failures will be logged */
df4638
+
df4638
+		do {
df4638
+			token = peek_token(&val, (unsigned *)0, cfile);
df4638
+			if (token == END_OF_FILE)
df4638
+				break;
df4638
+
df4638
+			parse_client_statement(cfile, (struct interface_info *)0, &top_level_config);
df4638
+		} while (1);
df4638
+
df4638
+		if (cfile -> warnings_occurred)
df4638
+			log_fatal("Cannot parse -C/-H/-F/--timeout/-V/--request-options configuration arguments !");
df4638
+		end_parse(&cfile);
df4638
+
df4638
+		if (timeout_arg) {
df4638
+			/* we just set the toplevel timeout, but per-client
df4638
+			 * timeouts may still be at defaults.
df4638
+			 */
df4638
+			for (ip=interfaces; ip; ip = ip->next) {
df4638
+				if (ip->client->config->timeout == 60)
df4638
+					ip->client->config->timeout = timeout_arg;
df4638
+			}
df4638
+		}
df4638
+
df4638
+		if ((dhclient_request_options != 0) && (top_level_config.requested_options != default_requested_options)) {
df4638
+			for (ip=interfaces; ip; ip = ip->next) {
df4638
+				if (ip->client->config->requested_options == default_requested_options)
df4638
+					ip->client->config->requested_options = top_level_config.requested_options;
df4638
+			}
df4638
+		}
df4638
+
df4638
+		free(arg_conf);
df4638
+		arg_conf = NULL;
df4638
+		arg_conf_len = 0;
df4638
+	}
df4638
+
df4638
 	/* Parse the lease database. */
df4638
 	read_client_leases();
df4638
 
df4638
@@ -3226,7 +3483,8 @@ void make_discover (client, lease)
df4638
 	client -> packet.xid = random ();
df4638
 	client -> packet.secs = 0; /* filled in by send_discover. */
df4638
 
df4638
-	if (can_receive_unicast_unconfigured (client -> interface))
df4638
+	if ((!(bootp_broadcast_always || client->config->bootp_broadcast_always))
df4638
+	    && can_receive_unicast_unconfigured(client->interface))
df4638
 		client -> packet.flags = 0;
df4638
 	else
df4638
 		client -> packet.flags = htons (BOOTP_BROADCAST);
df4638
@@ -3311,7 +3569,9 @@ void make_request (client, lease)
df4638
 	} else {
df4638
 		memset (&client -> packet.ciaddr, 0,
df4638
 			sizeof client -> packet.ciaddr);
df4638
-		if (can_receive_unicast_unconfigured (client -> interface))
df4638
+		if ((!(bootp_broadcast_always ||
df4638
+		    client ->config->bootp_broadcast_always)) &&
df4638
+		    can_receive_unicast_unconfigured (client -> interface))
df4638
 			client -> packet.flags = 0;
df4638
 		else
df4638
 			client -> packet.flags = htons (BOOTP_BROADCAST);
df4638
@@ -3374,7 +3634,8 @@ void make_decline (client, lease)
df4638
 	client -> packet.hops = 0;
df4638
 	client -> packet.xid = client -> xid;
df4638
 	client -> packet.secs = 0; /* Filled in by send_request. */
df4638
-	if (can_receive_unicast_unconfigured (client -> interface))
df4638
+	if ((!(bootp_broadcast_always || client->config-> bootp_broadcast_always))
df4638
+	    && can_receive_unicast_unconfigured (client->interface))
df4638
 		client -> packet.flags = 0;
df4638
 	else
df4638
 		client -> packet.flags = htons (BOOTP_BROADCAST);
df4638
diff --git a/common/conflex.c b/common/conflex.c
df4638
index 045b655..71c0bf5 100644
df4638
--- a/common/conflex.c
df4638
+++ b/common/conflex.c
df4638
@@ -832,6 +832,8 @@ intern(char *atom, enum dhcp_token dfv) {
df4638
 		if (!strcasecmp(atom+1, "ig-endian")) {
df4638
 			return TOKEN_BIG_ENDIAN;
df4638
 		}
df4638
+		if (!strcasecmp (atom + 1, "ootp-broadcast-always"))
df4638
+			return BOOTP_BROADCAST_ALWAYS;
df4638
 		break;
df4638
 	      case 'c':
df4638
 		if (!strcasecmp(atom + 1, "ase"))
df4638
diff --git a/includes/dhcpd.h b/includes/dhcpd.h
df4638
index 5930e6a..018fa34 100644
df4638
--- a/includes/dhcpd.h
df4638
+++ b/includes/dhcpd.h
df4638
@@ -1269,6 +1269,9 @@ struct client_config {
df4638
 
df4638
 	int lease_id_format;		/* format for IDs in lease file,
df4638
 					   TOKEN_OCTAL or TOKEN_HEX */
df4638
+
df4638
+	int bootp_broadcast_always;	/* If nonzero, always set the BOOTP_BROADCAST
df4638
+					   flag in requests */
df4638
 };
df4638
 
df4638
 /* Per-interface state used in the dhcp client... */
df4638
diff --git a/includes/dhctoken.h b/includes/dhctoken.h
df4638
index 5920f4f..7e7215a 100644
df4638
--- a/includes/dhctoken.h
df4638
+++ b/includes/dhctoken.h
df4638
@@ -377,6 +377,7 @@ enum dhcp_token {
df4638
 	TOKEN_HEX = 677,
df4638
 	TOKEN_OCTAL = 678,
df4638
 	KEY_ALGORITHM = 679
df4638
+        BOOTP_BROADCAST_ALWAYS = 680
df4638
 };
df4638
 
df4638
 #define is_identifier(x)	((x) >= FIRST_TOKEN &&	\
df4638
-- 
df4638
2.14.5
df4638