philipp / rpms / dhcp

Forked from rpms/dhcp 4 years ago
Clone

Blame SOURCES/dhcp-duid_uuid.patch

324fcf
diff -up dhcp-4.3.4/client/dhclient.c.duid_uuid dhcp-4.3.4/client/dhclient.c
324fcf
--- dhcp-4.3.4/client/dhclient.c.duid_uuid	2016-04-29 12:58:14.846150838 +0200
324fcf
+++ dhcp-4.3.4/client/dhclient.c	2016-04-29 12:58:14.851150839 +0200
324fcf
@@ -3868,6 +3868,59 @@ write_options(struct client_state *clien
324fcf
 	}
324fcf
 }
324fcf
 
324fcf
+int unhexchar(char c) {
324fcf
+
324fcf
+	if (c >= '0' && c <= '9')
324fcf
+		return c - '0';
324fcf
+
324fcf
+	if (c >= 'a' && c <= 'f')
324fcf
+		return c - 'a' + 10;
324fcf
+
324fcf
+	if (c >= 'A' && c <= 'F')
324fcf
+		return c - 'A' + 10;
324fcf
+
324fcf
+	return -1;
324fcf
+}
324fcf
+
324fcf
+isc_result_t
324fcf
+read_uuid(u_int8_t* uuid) {
324fcf
+	const char *id_fname = "/etc/machine-id";
324fcf
+	char id[32];
324fcf
+	size_t nread;
324fcf
+	FILE * file = fopen( id_fname , "r");
324fcf
+	if (!file) {
324fcf
+		log_debug("Cannot open %s", id_fname);
324fcf
+		return ISC_R_IOERROR;
324fcf
+	}
324fcf
+	nread = fread(id, 1, sizeof id, file);
324fcf
+	fclose(file);
324fcf
+
324fcf
+	if (nread < 32) {
324fcf
+		log_debug("Not enough data in %s", id_fname);
324fcf
+		return ISC_R_IOERROR;
324fcf
+	}
324fcf
+	int j;
324fcf
+	for (j = 0; j < 16; j++) {
324fcf
+		int a, b;
324fcf
+
324fcf
+		a = unhexchar(id[j*2]);
324fcf
+		b = unhexchar(id[j*2+1]);
324fcf
+
324fcf
+		if (a < 0 || b < 0) {
324fcf
+			log_debug("Wrong data in %s", id_fname);
324fcf
+                        return ISC_R_IOERROR;
324fcf
+		}
324fcf
+		uuid[j] = a << 4 | b;
324fcf
+	}
324fcf
+
324fcf
+	/* Set UUID version to 4 --- truly random generation */
324fcf
+	uuid[6] = (uuid[6] & 0x0F) | 0x40;
324fcf
+	/* Set the UUID variant to DCE */
324fcf
+	uuid[8] = (uuid[8] & 0x3F) | 0x80;
324fcf
+
324fcf
+	return ISC_R_SUCCESS;
324fcf
+}
324fcf
+
324fcf
 /*
324fcf
  * The "best" default DUID, since we cannot predict any information
324fcf
  * about the system (such as whether or not the hardware addresses are
324fcf
@@ -3888,6 +3941,7 @@ form_duid(struct data_string *duid, cons
324fcf
 	struct interface_info *ip;
324fcf
 	int len;
324fcf
 	char *str;
324fcf
+	u_int8_t uuid[16];
324fcf
 
324fcf
 	/* For now, just use the first interface on the list. */
324fcf
 	ip = interfaces;
324fcf
@@ -3908,9 +3962,16 @@ form_duid(struct data_string *duid, cons
324fcf
 	    (ip->hw_address.hlen > sizeof(ip->hw_address.hbuf)))
324fcf
 		log_fatal("Impossible hardware address length at %s:%d.", MDL);
324fcf
 
324fcf
-	if (duid_type == 0)
324fcf
-		duid_type = stateless ? DUID_LL : DUID_LLT;
324fcf
-
324fcf
+	if (duid_type == 0) {
324fcf
+		if (read_uuid(uuid) == ISC_R_SUCCESS)
324fcf
+		    duid_type = DUID_UUID;
324fcf
+		else
324fcf
+		    duid_type = stateless ? DUID_LL : DUID_LLT;
324fcf
+	}
324fcf
+	
324fcf
+	if (duid_type == DUID_UUID)
324fcf
+		len = 2 + sizeof (uuid);
324fcf
+	else {
324fcf
 	/*
324fcf
 	 * 2 bytes for the 'duid type' field.
324fcf
 	 * 2 bytes for the 'htype' field.
324fcf
@@ -3921,13 +3982,18 @@ form_duid(struct data_string *duid, cons
324fcf
 	len = 4 + (ip->hw_address.hlen - 1);
324fcf
 	if (duid_type == DUID_LLT)
324fcf
 		len += 4;
324fcf
+	}
324fcf
 	if (!buffer_allocate(&duid->buffer, len, MDL))
324fcf
 		log_fatal("no memory for default DUID!");
324fcf
 	duid->data = duid->buffer->data;
324fcf
 	duid->len = len;
324fcf
 
324fcf
+	if (duid_type == DUID_UUID) {
324fcf
+		putUShort(duid->buffer->data, DUID_UUID);
324fcf
+		memcpy(duid->buffer->data + 2, uuid, sizeof(uuid));
324fcf
+	}
324fcf
 	/* Basic Link Local Address type of DUID. */
324fcf
-	if (duid_type == DUID_LLT) {
324fcf
+	else if (duid_type == DUID_LLT) {
324fcf
 		putUShort(duid->buffer->data, DUID_LLT);
324fcf
 		putUShort(duid->buffer->data + 2, ip->hw_address.hbuf[0]);
324fcf
 		putULong(duid->buffer->data + 4, cur_time - DUID_TIME_EPOCH);