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

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