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

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