9c49c9e
From 2f6b827e89305adcff45288c632785ac054adb8e Mon Sep 17 00:00:00 2001
7c09728
From: Pavel Zhukov <pzhukov@redhat.com>
7c09728
Date: Thu, 21 Feb 2019 10:36:30 +0100
9c49c9e
Subject: [PATCH 16/26] Turn on creating/sending of DUID
7c09728
Cc: pzhukov@redhat.com
7c09728
7c09728
as client identifier with DHCPv4 clients (#560361c#40, rfc4361)
7c09728
---
7c09728
 client/dhclient.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++---
7c09728
 1 file changed, 70 insertions(+), 4 deletions(-)
7c09728
7c09728
diff --git a/client/dhclient.c b/client/dhclient.c
7c09728
index 8e57da9..ccc98e4 100644
7c09728
--- a/client/dhclient.c
7c09728
+++ b/client/dhclient.c
7c09728
@@ -4021,6 +4021,59 @@ write_options(struct client_state *client, struct option_state *options,
a40b8cb
 	}
a40b8cb
 }
a40b8cb
 
a40b8cb
+int unhexchar(char c) {
a40b8cb
+
a40b8cb
+	if (c >= '0' && c <= '9')
a40b8cb
+		return c - '0';
a40b8cb
+
a40b8cb
+	if (c >= 'a' && c <= 'f')
a40b8cb
+		return c - 'a' + 10;
a40b8cb
+
a40b8cb
+	if (c >= 'A' && c <= 'F')
a40b8cb
+		return c - 'A' + 10;
a40b8cb
+
a40b8cb
+	return -1;
a40b8cb
+}
a40b8cb
+
a40b8cb
+isc_result_t
a40b8cb
+read_uuid(u_int8_t* uuid) {
a40b8cb
+	const char *id_fname = "/etc/machine-id";
a40b8cb
+	char id[32];
a40b8cb
+	size_t nread;
a40b8cb
+	FILE * file = fopen( id_fname , "r");
a40b8cb
+	if (!file) {
a40b8cb
+		log_debug("Cannot open %s", id_fname);
a40b8cb
+		return ISC_R_IOERROR;
a40b8cb
+	}
a40b8cb
+	nread = fread(id, 1, sizeof id, file);
a40b8cb
+	fclose(file);
a40b8cb
+
a40b8cb
+	if (nread < 32) {
a40b8cb
+		log_debug("Not enough data in %s", id_fname);
a40b8cb
+		return ISC_R_IOERROR;
a40b8cb
+	}
baeda11
+	int j;
baeda11
+	for (j = 0; j < 16; j++) {
a40b8cb
+		int a, b;
a40b8cb
+
a40b8cb
+		a = unhexchar(id[j*2]);
a40b8cb
+		b = unhexchar(id[j*2+1]);
a40b8cb
+
a40b8cb
+		if (a < 0 || b < 0) {
a40b8cb
+			log_debug("Wrong data in %s", id_fname);
a40b8cb
+                        return ISC_R_IOERROR;
a40b8cb
+		}
a40b8cb
+		uuid[j] = a << 4 | b;
a40b8cb
+	}
a40b8cb
+
a40b8cb
+	/* Set UUID version to 4 --- truly random generation */
a40b8cb
+	uuid[6] = (uuid[6] & 0x0F) | 0x40;
a40b8cb
+	/* Set the UUID variant to DCE */
a40b8cb
+	uuid[8] = (uuid[8] & 0x3F) | 0x80;
a40b8cb
+
a40b8cb
+	return ISC_R_SUCCESS;
a40b8cb
+}
a40b8cb
+
a40b8cb
 /*
a40b8cb
  * The "best" default DUID, since we cannot predict any information
a40b8cb
  * about the system (such as whether or not the hardware addresses are
7c09728
@@ -4041,6 +4094,7 @@ form_duid(struct data_string *duid, const char *file, int line)
a40b8cb
 	struct interface_info *ip;
a40b8cb
 	int len;
a40b8cb
 	char *str;
a40b8cb
+	u_int8_t uuid[16];
a40b8cb
 
a40b8cb
 	/* For now, just use the first interface on the list. */
a40b8cb
 	ip = interfaces;
7c09728
@@ -4061,9 +4115,16 @@ form_duid(struct data_string *duid, const char *file, int line)
a40b8cb
 	    (ip->hw_address.hlen > sizeof(ip->hw_address.hbuf)))
a40b8cb
 		log_fatal("Impossible hardware address length at %s:%d.", MDL);
a40b8cb
 
a40b8cb
-	if (duid_type == 0)
a40b8cb
-		duid_type = stateless ? DUID_LL : DUID_LLT;
a40b8cb
-
a40b8cb
+	if (duid_type == 0) {
a40b8cb
+		if (read_uuid(uuid) == ISC_R_SUCCESS)
a40b8cb
+		    duid_type = DUID_UUID;
a40b8cb
+		else
a40b8cb
+		    duid_type = stateless ? DUID_LL : DUID_LLT;
a40b8cb
+	}
a40b8cb
+	
a40b8cb
+	if (duid_type == DUID_UUID)
a40b8cb
+		len = 2 + sizeof (uuid);
a40b8cb
+	else {
a40b8cb
 	/*
a40b8cb
 	 * 2 bytes for the 'duid type' field.
a40b8cb
 	 * 2 bytes for the 'htype' field.
7c09728
@@ -4074,13 +4135,18 @@ form_duid(struct data_string *duid, const char *file, int line)
a40b8cb
 	len = 4 + (ip->hw_address.hlen - 1);
a40b8cb
 	if (duid_type == DUID_LLT)
a40b8cb
 		len += 4;
a40b8cb
+	}
a40b8cb
 	if (!buffer_allocate(&duid->buffer, len, MDL))
a40b8cb
 		log_fatal("no memory for default DUID!");
a40b8cb
 	duid->data = duid->buffer->data;
a40b8cb
 	duid->len = len;
a40b8cb
 
a40b8cb
+	if (duid_type == DUID_UUID) {
a40b8cb
+		putUShort(duid->buffer->data, DUID_UUID);
a40b8cb
+		memcpy(duid->buffer->data + 2, uuid, sizeof(uuid));
a40b8cb
+	}
a40b8cb
 	/* Basic Link Local Address type of DUID. */
a40b8cb
-	if (duid_type == DUID_LLT) {
a40b8cb
+	else if (duid_type == DUID_LLT) {
a40b8cb
 		putUShort(duid->buffer->data, DUID_LLT);
a40b8cb
 		putUShort(duid->buffer->data + 2, ip->hw_address.hbuf[0]);
a40b8cb
 		putULong(duid->buffer->data + 4, cur_time - DUID_TIME_EPOCH);
7c09728
-- 
7c09728
2.14.5
7c09728