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