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

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