[PATCH 2/2] librpc/ndr: add new LIBNDR_FLAG_STR_RAW8 for ndr_pull_string

Sean Finney seanius at seanius.net
Thu May 26 07:45:08 MDT 2011


Introduce a new flag, LIBNDR_FLAG_STR_RAW8, which indicates that libndr
should not attempt to convert the corresponding byte sequence, and place
the responsibility on the caller to do so later.

This is needed in cases where the string is known to be 8-bit and either
NULL terminated or of known length, but in an unspecified character set.
For example, when pulling PT_STRING8 properties from an exchange server
via libmapi + libndr, the codepage is neither known nor in the control
of the caller, and is determined by subsequent properties requested from
the server.  Therefore the client would like to fetch all properties in
one large batch, and convert the resulting strings locally.

This commit also includes some (basic) tests of each of the flags'
respective behaviors with the ndr push/pull string functions, in a new
source4 torture test suite ndr.ndr_string.

Signed-off-by: Sean Finney <seanius at seanius.net>
---
 librpc/idl/idl_types.h        |    6 ++
 librpc/ndr/libndr.h           |    1 +
 librpc/ndr/ndr_string.c       |   46 ++++++++--
 source4/torture/ndr/ndr.c     |    1 +
 source4/torture/ndr/string.c  |  188 +++++++++++++++++++++++++++++++++++++++++
 source4/torture/wscript_build |    2 +-
 6 files changed, 235 insertions(+), 9 deletions(-)
 create mode 100644 source4/torture/ndr/string.c

diff --git a/librpc/idl/idl_types.h b/librpc/idl/idl_types.h
index 023c040..c50efac 100644
--- a/librpc/idl/idl_types.h
+++ b/librpc/idl/idl_types.h
@@ -8,6 +8,7 @@
 #define STR_CONFORMANT  LIBNDR_FLAG_STR_CONFORMANT
 #define STR_CHARLEN	LIBNDR_FLAG_STR_CHARLEN
 #define STR_UTF8	LIBNDR_FLAG_STR_UTF8
+#define STR_RAW8	LIBNDR_FLAG_STR_RAW8
 
 /*
   a null terminated UCS2 string
@@ -25,6 +26,11 @@
 #define utf8string	[flag(STR_UTF8|STR_NULLTERM)] string
 
 /*
+  a null terminated "raw" string (null terminated byte sequence)
+*/
+#define raw8string	[flag(STR_RAW8|STR_NULLTERM)] string
+
+/*
   a null terminated UCS2 string
 */
 #define nstring_array	[flag(STR_NULLTERM|NDR_ALIGN2)] string_array
diff --git a/librpc/ndr/libndr.h b/librpc/ndr/libndr.h
index cbe9b40..ca3710b 100644
--- a/librpc/ndr/libndr.h
+++ b/librpc/ndr/libndr.h
@@ -122,6 +122,7 @@ struct ndr_print {
 #define LIBNDR_FLAG_STR_CONFORMANT	(1<<10)
 #define LIBNDR_FLAG_STR_CHARLEN		(1<<11)
 #define LIBNDR_FLAG_STR_UTF8		(1<<12)
+#define LIBNDR_FLAG_STR_RAW8		(1<<13)
 #define LIBNDR_STRING_FLAGS		(0x7FFC)
 
 /* set if relative pointers should *not* be marshalled in reverse order */
diff --git a/librpc/ndr/ndr_string.c b/librpc/ndr/ndr_string.c
index d0d8303..207d55b 100644
--- a/librpc/ndr/ndr_string.c
+++ b/librpc/ndr/ndr_string.c
@@ -31,7 +31,7 @@ _PUBLIC_ enum ndr_err_code ndr_pull_string(struct ndr_pull *ndr, int ndr_flags,
 	uint32_t len1, ofs, len2;
 	uint16_t len3;
 	size_t conv_src_len = 0, converted_size;
-	int chset = CH_UTF16;
+	int do_convert = 1, chset = CH_UTF16;
 	unsigned byte_mul = 2;
 	unsigned flags = ndr->flags;
 	unsigned c_len_term = 0;
@@ -56,6 +56,12 @@ _PUBLIC_ enum ndr_err_code ndr_pull_string(struct ndr_pull *ndr, int ndr_flags,
 		flags &= ~LIBNDR_FLAG_STR_UTF8;
 	}
 
+	if (flags & LIBNDR_FLAG_STR_RAW8) {
+		do_convert = 0;
+		byte_mul = 1;
+		flags &= ~LIBNDR_FLAG_STR_RAW8;
+	}
+
 	flags &= ~LIBNDR_FLAG_STR_CONFORMANT;
 	if (flags & LIBNDR_FLAG_STR_CHARLEN) {
 		c_len_term = 1;
@@ -138,7 +144,11 @@ _PUBLIC_ enum ndr_err_code ndr_pull_string(struct ndr_pull *ndr, int ndr_flags,
 	if (conv_src_len == 0) {
 		as = talloc_strdup(ndr->current_mem_ctx, "");
 	} else {
-		if (!convert_string_talloc(ndr->current_mem_ctx, chset,
+		if (!do_convert) {
+			as = talloc_strndup(ndr->current_mem_ctx,
+			                    ndr->data + ndr->offset,
+					    conv_src_len);
+		} else if (!convert_string_talloc(ndr->current_mem_ctx, chset,
 					   CH_UNIX, ndr->data + ndr->offset,
 					   conv_src_len * byte_mul,
 					   (void **)(void *)&as,
@@ -174,7 +184,7 @@ _PUBLIC_ enum ndr_err_code ndr_push_string(struct ndr_push *ndr, int ndr_flags,
 {
 	ssize_t s_len, c_len;
 	size_t d_len;
-	int chset = CH_UTF16;
+	int do_convert = 1, chset = CH_UTF16;
 	unsigned flags = ndr->flags;
 	unsigned byte_mul = 2;
 	uint8_t *dest = NULL;
@@ -201,12 +211,22 @@ _PUBLIC_ enum ndr_err_code ndr_push_string(struct ndr_push *ndr, int ndr_flags,
 		flags &= ~LIBNDR_FLAG_STR_UTF8;
 	}
 
+	if (flags & LIBNDR_FLAG_STR_RAW8) {
+		do_convert = 0;
+		byte_mul = 1;
+		flags &= ~LIBNDR_FLAG_STR_RAW8;
+	}
+
 	flags &= ~LIBNDR_FLAG_STR_CONFORMANT;
 
 	if (!(flags & LIBNDR_FLAG_STR_NOTERM)) {
 		s_len++;
 	}
-	if (!convert_string_talloc(ndr, CH_UNIX, chset, s, s_len,
+
+	if (!do_convert) {
+		d_len = s_len;
+		dest = talloc_strndup(ndr, s, s_len);
+	} else if (!convert_string_talloc(ndr, CH_UNIX, chset, s, s_len,
 				   (void **)(void *)&dest, &d_len))
 	{
 		return ndr_push_error(ndr, NDR_ERR_CHARCNV, 
@@ -276,9 +296,13 @@ _PUBLIC_ size_t ndr_string_array_size(struct ndr_push *ndr, const char *s)
 	unsigned byte_mul = 2;
 	unsigned c_len_term = 1;
 
-	c_len = s?strlen_m(s):0;
+	if (flags & LIBNDR_FLAG_STR_RAW8) {
+		c_len = s?strlen(s):0;
+	} else {
+		c_len = s?strlen_m(s):0;
+	}
 
-	if (flags & (LIBNDR_FLAG_STR_ASCII|LIBNDR_FLAG_STR_UTF8)) {
+	if (flags & (LIBNDR_FLAG_STR_ASCII|LIBNDR_FLAG_STR_RAW8|LIBNDR_FLAG_STR_UTF8)) {
 		byte_mul = 1;
 	}
 
@@ -484,16 +508,22 @@ _PUBLIC_ size_t ndr_size_string_array(const char **a, uint32_t count, int flags)
 {
 	uint32_t i;
 	size_t size = 0;
+	int rawbytes = 0;
+
+	if (flags & LIBNDR_FLAG_STR_RAW8) {
+		rawbytes = 1;
+		flags &= ~LIBNDR_FLAG_STR_RAW8;
+	}
 
 	switch (flags & LIBNDR_STRING_FLAGS) {
 	case LIBNDR_FLAG_STR_NULLTERM:
 		for (i = 0; i < count; i++) {
-			size += strlen_m_term(a[i]);
+			size += rawbytes?strlen(a[i]) + 1:strlen_m_term(a[i]);
 		}
 		break;
 	case LIBNDR_FLAG_STR_NOTERM:
 		for (i = 0; i < count; i++) {
-			size += strlen_m(a[i]);
+			size += rawbytes?strlen(a[i]):strlen_m(a[i]);
 		}
 		break;
 	default:
diff --git a/source4/torture/ndr/ndr.c b/source4/torture/ndr/ndr.c
index 36b2b55..6c564d3 100644
--- a/source4/torture/ndr/ndr.c
+++ b/source4/torture/ndr/ndr.c
@@ -355,6 +355,7 @@ struct torture_suite *torture_local_ndr(TALLOC_CTX *mem_ctx)
 	torture_suite_add_suite(suite, ndr_nbt_suite(suite));
 	torture_suite_add_suite(suite, ndr_ntlmssp_suite(suite));
 	torture_suite_add_suite(suite, ndr_backupkey_suite(suite));
+	torture_suite_add_suite(suite, ndr_string_suite(suite));
 
 	torture_suite_add_simple_test(suite, "string terminator",
 				      test_check_string_terminator);
diff --git a/source4/torture/ndr/string.c b/source4/torture/ndr/string.c
new file mode 100644
index 0000000..99c11ad
--- /dev/null
+++ b/source4/torture/ndr/string.c
@@ -0,0 +1,188 @@
+#include "includes.h"
+#include "torture/ndr/ndr.h"
+#include "torture/ndr/proto.h"
+#include "../lib/util/dlinklist.h"
+#include "param/param.h"
+
+struct ndr_string_test_data {
+	const char const *data;
+	int flags;
+	enum ndr_err_code expected_code;
+	bool should_strcmp_pass;
+	const char const *desc;
+};
+
+static const char const *ascii = "ascii";
+/* the following is equivalent to "kamelåså öäüÿéèóò" in latin1 */
+static const char const latin1[] = { 0x6b, 0x61, 0x6d, 0x65, 0x6c, 0xe5, 0x73,
+                                     0xe5, 0x20, 0xF6, 0xE4, 0xFC, 0xFF, 0xE9,
+                                     0xE8, 0xF3, 0xF2, 0x00 };
+/* the following is equivalent to "kamelåså ☺☺☺ öäüÿéèóò" in utf8 */
+static const char const utf8[] = { 0x6b, 0x61, 0x6d, 0x65, 0x6c, 0xc3, 0xa5,
+                                   0x73, 0xc3, 0xa5, 0x20, 0xE2, 0x98, 0xBA,
+                                   0xE2, 0x98, 0xBA, 0xE2, 0x98, 0xBA, 0x20,
+                                   0xc3, 0xb6, 0xc3, 0xa4, 0xc3, 0xbc, 0xc3,
+                                   0xbf, 0xc3, 0xa9, 0xc3, 0xa8, 0xc3, 0xb3,
+                                   0xc3, 0xb2, 0x00 };
+
+/* purely for convenience */
+static int fl_ascii_null = LIBNDR_FLAG_STR_ASCII|LIBNDR_FLAG_STR_NULLTERM;
+static int fl_utf8_null = LIBNDR_FLAG_STR_UTF8|LIBNDR_FLAG_STR_NULLTERM;
+static int fl_raw8_null = LIBNDR_FLAG_STR_RAW8|LIBNDR_FLAG_STR_NULLTERM;
+
+
+static bool
+test_ndr_push_string ( const char *string, int flags,
+                       enum ndr_err_code exp_ndr_err, bool strcmp_pass) {
+	TALLOC_CTX *mem_ctx;
+	struct ndr_push *ndr;
+	enum ndr_err_code err;
+	int strcmp_res;
+	bool ret = false;
+
+	fprintf (stderr, "test_ndr_push_string %s flags 0x%x expecting err 0x%x and strcmp %s: ", string, flags, exp_ndr_err, strcmp_pass?"pass":"fail");
+	if (exp_ndr_err != NDR_ERR_SUCCESS) {
+		fprintf (stderr, "(ignore any Conversion error) ");
+	}
+
+	mem_ctx = talloc_named (NULL, 0, "test_ndr_push_string");
+	ndr = talloc_zero (mem_ctx, struct ndr_push);
+	ndr_set_flags (&ndr->flags, flags);
+	err = ndr_push_string (ndr, NDR_SCALARS, string);
+	if (err != exp_ndr_err) {
+		fprintf (stderr, "FAIL: returned 0x%x but expected 0x%x\n",
+		         err, exp_ndr_err);
+	} else if (exp_ndr_err == NDR_ERR_SUCCESS) {
+		if (ndr == NULL) {
+			fprintf (stderr, "FAIL: succeeded but ndr data NULL\n");
+		} else {
+			strcmp_res = strcmp(string, ndr->data);
+			if ( strcmp_res != 0 && strcmp_pass
+			     || strcmp_res == 0 && !strcmp_pass ) {
+				fprintf (stderr, "FAIL: unexpected result for strcmp('%s','%s')\n", string, ndr->data);
+			} else {
+				ret = true;
+			}
+		}
+	} else {
+		ret = true;
+	}
+	if (ret) {
+		fprintf (stderr, "PASS\n");
+	}
+
+	talloc_free(ndr);
+	return ret;
+}
+
+static bool
+test_ndr_pull_string ( const char *string, int flags,
+                       enum ndr_err_code exp_ndr_err, bool strcmp_pass) {
+	TALLOC_CTX *mem_ctx;
+	DATA_BLOB blob;
+	struct ndr_pull *ndr;
+	enum ndr_err_code err;
+	const char *result = NULL;
+	int strcmp_res;
+	bool ret = 0;
+
+	fprintf (stderr, "test_ndr_pull_string %s flags 0x%x expecting err 0x%x and strcmp %s: ", string, flags, exp_ndr_err, strcmp_pass?"pass":"fail");
+	if (exp_ndr_err != NDR_ERR_SUCCESS) {
+		fprintf (stderr, "(ignore any Conversion error) ");
+	}
+
+	mem_ctx = talloc_named (NULL, 0, "test_ndr_pull_string");
+
+	blob.data = (char*)string;
+	blob.length = strlen(string);
+	ndr = ndr_pull_init_blob(&blob, mem_ctx);
+	ndr_set_flags (&ndr->flags, flags);
+
+	err = ndr_pull_string (ndr, NDR_SCALARS, &result);
+	if (err != exp_ndr_err) {
+		fprintf (stderr, "FAIL: returned 0x%x but expected 0x%x\n",
+		         err, exp_ndr_err);
+	} else if (exp_ndr_err == NDR_ERR_SUCCESS) {
+		if (result == NULL) {
+			fprintf (stderr, "FAIL: succeeded but result NULL\n");
+		} else {
+			strcmp_res = strcmp(string, result);
+			if (strcmp_res != 0 && strcmp_pass
+			    || strcmp_res == 0 && !strcmp_pass) {
+				fprintf (stderr, "FAIL: unexpected result for strcmp('%s','%s')\n", string, result);
+			} else {
+				ret = true;
+			}
+		}
+	} else {
+		ret = true;
+	}
+	if (ret) {
+		fprintf (stderr, "PASS\n");
+	}
+
+	talloc_free(ndr);
+	return ret;
+}
+
+static bool torture_ndr_string(struct torture_context *torture) {
+	int i;
+	struct ndr_string_test_data pushdata[] = {
+		{ascii, fl_ascii_null, NDR_ERR_SUCCESS, true,
+		 "test_ndr_push_string data: ASCII,STR_ASCII"},
+		{utf8, fl_ascii_null, NDR_ERR_CHARCNV, false,
+		 "test_ndr_push_string data: UTF8,STR_ASCII"},
+		{utf8, fl_utf8_null, NDR_ERR_SUCCESS, true,
+		 "test_ndr_push_string data: UTF8,STR_UTF8"},
+		{latin1, fl_ascii_null, NDR_ERR_CHARCNV, false,
+		 "test_ndr_push_string data: LATIN1,STR_ASCII"},
+		{latin1, fl_raw8_null, NDR_ERR_SUCCESS, true,
+		 "test_ndr_push_string data: LATIN1,STR_RAW8"},
+		{utf8, fl_raw8_null, NDR_ERR_SUCCESS, true,
+		 "test_ndr_push_string data: UTF8,STR_RAW8"},
+		{NULL, 0, 0, false, NULL}
+	};
+	struct ndr_string_test_data pulldata[] = {
+		{ascii, fl_ascii_null, NDR_ERR_SUCCESS, true,
+		 "test_ndr_pull_string data: ASCII,STR_ASCII"},
+		{utf8, fl_ascii_null, NDR_ERR_SUCCESS, false,
+		 "test_ndr_pull_string data: UTF8,STR_ASCII"},
+		{utf8, fl_utf8_null, NDR_ERR_SUCCESS, true,
+		 "test_ndr_pull_string data: UTF8,STR_UTF8"},
+		{latin1, fl_ascii_null, NDR_ERR_SUCCESS, false,
+		 "test_ndr_pull_string data: LATIN1,STR_ASCII"},
+		{latin1, fl_raw8_null, NDR_ERR_SUCCESS, true,
+		 "test_ndr_pull_string data: LATIN1,STR_RAW8"},
+		{utf8, fl_raw8_null, NDR_ERR_SUCCESS, true,
+		 "test_ndr_pull_string data: UTF8,STR_RAW8"},
+		{NULL, 0, 0, false, NULL}
+	};
+
+	for (i = 0; pushdata[i].data != NULL; ++i) {
+		struct ndr_string_test_data *nstd = &pushdata[i];
+		bool ok = test_ndr_push_string (nstd->data, nstd->flags,
+		                                nstd->expected_code,
+		                                nstd->should_strcmp_pass);
+		torture_assert(torture, ok, nstd->desc);
+	}
+
+	for (i = 0; pulldata[i].data != NULL; ++i) {
+		struct ndr_string_test_data *nstd = &pulldata[i];
+		bool ok = test_ndr_pull_string (nstd->data, nstd->flags,
+		                                nstd->expected_code,
+		                                nstd->should_strcmp_pass);
+		torture_assert(torture, ok, nstd->desc);
+	}
+
+	return true;
+}
+
+struct torture_suite *ndr_string_suite(TALLOC_CTX *ctx)
+{
+	struct torture_suite *suite = torture_suite_create(ctx, "ndr_string");
+
+	torture_suite_add_simple_test(suite, "ndr_string", torture_ndr_string);
+	suite->description = talloc_strdup(suite, "NDR - string-conversion focused push/pull tests");
+
+	return suite;
+}
diff --git a/source4/torture/wscript_build b/source4/torture/wscript_build
index 68ec4e6..106cc64 100644
--- a/source4/torture/wscript_build
+++ b/source4/torture/wscript_build
@@ -33,7 +33,7 @@ bld.RECURSE('libnetapi')
 bld.RECURSE('libsmbclient')
 
 bld.SAMBA_SUBSYSTEM('TORTURE_NDR',
-	source='ndr/ndr.c ndr/winreg.c ndr/atsvc.c ndr/lsa.c ndr/epmap.c ndr/dfs.c ndr/netlogon.c ndr/drsuapi.c ndr/spoolss.c ndr/samr.c ndr/dfsblob.c ndr/drsblobs.c ndr/nbt.c ndr/ntlmssp.c ndr/backupkey.c',
+	source='ndr/ndr.c ndr/winreg.c ndr/atsvc.c ndr/lsa.c ndr/epmap.c ndr/dfs.c ndr/netlogon.c ndr/drsuapi.c ndr/spoolss.c ndr/samr.c ndr/dfsblob.c ndr/drsblobs.c ndr/nbt.c ndr/ntlmssp.c ndr/backupkey.c ndr/string.c',
 	autoproto='ndr/proto.h',
 	deps='torture'
 	)
-- 
1.7.0.4



More information about the samba-technical mailing list