Blame SOURCES/0008-library-add-_adcli_bin_sid_to_str.patch

7ddab3
From f28edf4e887cf8616fa21dacc2b0f0d31f5f92fb Mon Sep 17 00:00:00 2001
7ddab3
From: Sumit Bose <sbose@redhat.com>
7ddab3
Date: Tue, 30 Jan 2018 14:37:05 +0100
7ddab3
Subject: [PATCH 08/23] library: add _adcli_bin_sid_to_str()
7ddab3
7ddab3
Convert a binary SID to the string representation.
7ddab3
7ddab3
https://bugs.freedesktop.org/show_bug.cgi?id=100118
7ddab3
7ddab3
Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
7ddab3
---
7ddab3
 library/adprivate.h |   4 ++
7ddab3
 library/adutil.c    | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++
7ddab3
 2 files changed, 117 insertions(+)
7ddab3
7ddab3
diff --git a/library/adprivate.h b/library/adprivate.h
7ddab3
index fc146af..e99f9fc 100644
7ddab3
--- a/library/adprivate.h
7ddab3
+++ b/library/adprivate.h
7ddab3
@@ -31,6 +31,7 @@
7ddab3
 #include <limits.h>
7ddab3
 #include <stdlib.h>
7ddab3
 #include <stdio.h>
7ddab3
+#include <stdint.h>
7ddab3
 
7ddab3
 #include <ldap.h>
7ddab3
 
7ddab3
@@ -132,6 +133,9 @@ int            _adcli_str_has_prefix         (const char *str,
7ddab3
 int            _adcli_str_has_suffix         (const char *str,
7ddab3
 		                              const char *suffix);
7ddab3
 
7ddab3
+char *          _adcli_bin_sid_to_str        (const uint8_t *data,
7ddab3
+                                              size_t len);
7ddab3
+
7ddab3
 char *         _adcli_str_dupn               (void *data,
7ddab3
                                               size_t len);
7ddab3
 
7ddab3
diff --git a/library/adutil.c b/library/adutil.c
7ddab3
index cd40f45..829cdd9 100644
7ddab3
--- a/library/adutil.c
7ddab3
+++ b/library/adutil.c
7ddab3
@@ -293,6 +293,83 @@ _adcli_strv_set (char ***field,
7ddab3
 	*field = newval;
7ddab3
 }
7ddab3
 
7ddab3
+char *
7ddab3
+_adcli_bin_sid_to_str (const uint8_t *data,
7ddab3
+                       size_t len)
7ddab3
+{
7ddab3
+	uint8_t sid_rev_num;
7ddab3
+	int8_t num_auths;
7ddab3
+	uint8_t id_auth[6];
7ddab3
+	uint32_t id_auth_val;
7ddab3
+	uint32_t sub_auths[15];
7ddab3
+	uint32_t val;
7ddab3
+	size_t p = 0;
7ddab3
+	size_t c;
7ddab3
+	int nc;
7ddab3
+	char *sid_buf;
7ddab3
+	size_t sid_buf_len;
7ddab3
+
7ddab3
+	if (data == NULL || len < 8) {
7ddab3
+		return NULL;
7ddab3
+	}
7ddab3
+
7ddab3
+	sid_rev_num = (uint8_t) data [p];
7ddab3
+	p++;
7ddab3
+
7ddab3
+	num_auths = (int8_t) data[p];
7ddab3
+	p++;
7ddab3
+
7ddab3
+	if (num_auths > 15 || len < 8 + (num_auths * sizeof (uint32_t))) {
7ddab3
+		return NULL;
7ddab3
+	}
7ddab3
+
7ddab3
+	for (c = 0; c < 6; c++) {
7ddab3
+		id_auth[c] = (uint8_t) data[p];
7ddab3
+		p++;
7ddab3
+	}
7ddab3
+
7ddab3
+	/* Only 32bits are used for the string representation */
7ddab3
+	id_auth_val = (id_auth[2] << 24) +
7ddab3
+	              (id_auth[3] << 16) +
7ddab3
+	              (id_auth[4] << 8) +
7ddab3
+	              (id_auth[5]);
7ddab3
+
7ddab3
+	for (c = 0; c < num_auths; c++) {
7ddab3
+		memcpy (&val, data + p, sizeof (uint32_t));
7ddab3
+		sub_auths[c] = le32toh (val);
7ddab3
+
7ddab3
+		p += sizeof (uint32_t);
7ddab3
+	}
7ddab3
+
7ddab3
+	sid_buf_len = 17 + (num_auths * 11);
7ddab3
+	sid_buf = calloc (1, sid_buf_len);
7ddab3
+	if (sid_buf == NULL) {
7ddab3
+		return NULL;
7ddab3
+	}
7ddab3
+
7ddab3
+	nc = snprintf (sid_buf, sid_buf_len, "S-%u-%lu", sid_rev_num,
7ddab3
+	              (unsigned long) id_auth_val);
7ddab3
+	if (nc < 0 || nc >= sid_buf_len) {
7ddab3
+		free (sid_buf);
7ddab3
+		return NULL;
7ddab3
+	}
7ddab3
+
7ddab3
+	p = 0;
7ddab3
+	for (c = 0; c < num_auths; c++) {
7ddab3
+		p += nc;
7ddab3
+		sid_buf_len -= nc;
7ddab3
+
7ddab3
+		nc = snprintf (sid_buf + p, sid_buf_len, "-%lu",
7ddab3
+		               (unsigned long) sub_auths[c]);
7ddab3
+		if (nc < 0 || nc >= sid_buf_len) {
7ddab3
+			free (sid_buf);
7ddab3
+			return NULL;
7ddab3
+		}
7ddab3
+	}
7ddab3
+
7ddab3
+	return sid_buf;
7ddab3
+}
7ddab3
+
7ddab3
 char *
7ddab3
 _adcli_str_dupn (void *data,
7ddab3
                  size_t len)
7ddab3
@@ -508,6 +585,41 @@ test_check_nt_time_string_lifetime (void)
7ddab3
 	assert (_adcli_check_nt_time_string_lifetime ("130645404000000000", 100000));
7ddab3
 }
7ddab3
 
7ddab3
+static void
7ddab3
+test_bin_sid_to_str (void)
7ddab3
+{
7ddab3
+	uint8_t sid1[] = { 0x01, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05,
7ddab3
+	                   0x15, 0x00, 0x00, 0x00, 0xF8, 0x12, 0x13, 0xDC,
7ddab3
+	                   0x47, 0xF3, 0x1C, 0x76, 0x47, 0x2F, 0x2E, 0xD7,
7ddab3
+	                   0x51, 0x04, 0x00, 0x00 };
7ddab3
+
7ddab3
+	uint8_t sid2[] = { 0x01, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05,
7ddab3
+	                   0x15, 0x00, 0x00, 0x00, 0xF8, 0x12, 0x13, 0xDC,
7ddab3
+	                   0x47, 0xF3, 0x1C, 0x76, 0x47, 0x2F, 0x2E, 0xD7};
7ddab3
+
7ddab3
+	uint8_t sid3[] = { 0x01, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05,
7ddab3
+	                   0x15, 0x00, 0x00, 0x00, 0x29, 0xC9, 0x4F, 0xD9,
7ddab3
+	                   0xC2, 0x3C, 0xC3, 0x78, 0x36, 0x55, 0x87, 0xF8};
7ddab3
+
7ddab3
+
7ddab3
+	char *str;
7ddab3
+
7ddab3
+	str = _adcli_bin_sid_to_str (sid1, sizeof (sid1));
7ddab3
+	assert (str != NULL);
7ddab3
+	assert (strcmp (str, "S-1-5-21-3692237560-1981608775-3610128199-1105") == 0);
7ddab3
+	free (str);
7ddab3
+
7ddab3
+	str = _adcli_bin_sid_to_str (sid2, sizeof (sid2));
7ddab3
+	assert (str != NULL);
7ddab3
+	assert (strcmp (str, "S-1-5-21-3692237560-1981608775-3610128199") == 0);
7ddab3
+	free (str);
7ddab3
+
7ddab3
+	str = _adcli_bin_sid_to_str (sid3, sizeof (sid2));
7ddab3
+	assert (str != NULL);
7ddab3
+	assert (strcmp (str, "S-1-5-21-3645884713-2026060994-4169618742") == 0);
7ddab3
+	free (str);
7ddab3
+}
7ddab3
+
7ddab3
 int
7ddab3
 main (int argc,
7ddab3
       char *argv[])
7ddab3
@@ -516,6 +628,7 @@ main (int argc,
7ddab3
 	test_func (test_strv_dup, "/util/strv_dup");
7ddab3
 	test_func (test_strv_count, "/util/strv_count");
7ddab3
 	test_func (test_check_nt_time_string_lifetime, "/util/check_nt_time_string_lifetime");
7ddab3
+	test_func (test_bin_sid_to_str, "/util/bin_sid_to_str");
7ddab3
 	return test_run (argc, argv);
7ddab3
 }
7ddab3
 
7ddab3
-- 
7ddab3
2.14.4
7ddab3