UTF-8 and LDAP

Juergen Hasch Hasch at t-online.de
Sat Feb 23 15:50:05 GMT 2002


Am Samstag, 23. Februar 2002 19:54 schrieb Juergen Hasch:
> Hi Andrew,

> 2. Convert some strings to/from UTF-8 when calling LDAP functions.
>    I assume here that not all parameters need to be in UTF-8.
>    Namely in ldap_search only the filter expression needs to be UTF-8,
>    base and attributes should not contain non-ascii chars.
>    Received strings from LDAP get UTF-8 converted in ads_pull_string
>    and ads_dump_string.
>    For ldap_modify_ext_s I needed to allocate space for the strings after
>    UTF-8 conversion using talloc_strdup in ads_mod_ber / ads_mod_var.
>
Unfortunately there was a typo in the patch for ldap.c. I also had a small
fight with my text editor eating whitespaces at the end of the line.
Here comes the fixed version. I also UTF-8-converted the base parameter in
ldap_search_ext_s, as I found a case where german characters are used ;-)

...Juergen

--- libads/ldap.orig	Sun Feb 17 16:17:44 2002
+++ libads/ldap.c	Sun Feb 24 00:18:24 2002
@@ -64,14 +64,18 @@
 {
 	struct timeval timeout;
 	int rc;
+	fstring utf8_bind_path;
+	fstring utf8_exp;
 
 	timeout.tv_sec = ADS_SEARCH_TIMEOUT;
 	timeout.tv_usec = 0;
 	*res = NULL;
 
+	push_utf8_fstring(utf8_exp, exp);
+	push_utf8_fstring(utf8_bind_path, bind_path);
 	rc = ldap_search_ext_s(ads->ld, 
-			       bind_path, scope,
-			       exp, (char **) attrs, 0, NULL, NULL, 
+			       utf8_bind_path, scope,
+			       utf8_exp, (char **) attrs, 0, NULL, NULL, 
 			       &timeout, LDAP_NO_LIMIT, (LDAPMessage **)res);
 	return ADS_ERROR(rc);
 }
@@ -243,7 +247,8 @@
 	va_list ap;
 	int num_vals, i, do_op;
 	char *value, **values;
-
+	fstring utf8_value;
+	
 	/* count the number of values */
 	va_start(ap, name);
 	for (num_vals=0; va_arg(ap, char *); num_vals++);
@@ -254,8 +259,10 @@
 			return ADS_ERROR(LDAP_NO_MEMORY);
 		va_start(ap, name);
 		for (i=0; (value = (char *) va_arg(ap, char *)) &&
-			     i < num_vals; i++)
-			values[i] = value;
+			     i < num_vals; i++) {
+			push_utf8_fstring(utf8_value,value);
+			values[i] = talloc_strdup(ctx,utf8_value);
+			}
 		va_end(ap);
 		values[i] = NULL;
 		do_op = mod_op;
@@ -272,7 +279,8 @@
 	va_list ap;
 	int num_vals, i, do_op;
 	char *value, **values;
-
+	fstring utf8_value;
+	
 	/* count the number of values */
 	va_start(ap, name);
 	for (num_vals=0; va_arg(ap, struct berval *); num_vals++);
@@ -284,8 +292,10 @@
 			return ADS_ERROR(LDAP_NO_MEMORY);
 		va_start(ap, name);
 		for (i=0; (value = (char *) va_arg(ap, char *)) &&
-			     i < num_vals; i++)
-			values[i] = value;
+			     i < num_vals; i++) {
+			push_utf8_fstring(utf8_value,value);
+			values[i] = talloc_strdup(ctx,utf8_value);			
+			}
 		va_end(ap);
 		values[i] = NULL;
 		do_op = mod_op;
@@ -444,14 +454,14 @@
 	if (!(mods = ads_init_mods(ctx)))
 		goto done;
 	
-	ads_mod_add(ctx, &mods, "cn", hostname);
+	ads_mod_add(ctx, &mods, "cn", (char *)hostname);
 	ads_mod_add(ctx, &mods, "sAMAccountName", samAccountName);
 	ads_mod_add_var(ctx, &mods, LDAP_MOD_ADD, "objectClass",
 			"top", "person", "organizationalPerson",
 			"user", "computer", NULL);
 	ads_mod_add(ctx, &mods, "userPrincipalName", host_upn);
 	ads_mod_add(ctx, &mods, "servicePrincipalName", host_spn);
-	ads_mod_add(ctx, &mods, "dNSHostName", hostname);
+	ads_mod_add(ctx, &mods, "dNSHostName", (char *)hostname);
 	ads_mod_add(ctx, &mods, "userAccountControl", controlstr);
 	ads_mod_add(ctx, &mods, "operatingSystem", "Samba");
 	ads_mod_add(ctx, &mods, "operatingSystemVersion", VERSION);
@@ -497,8 +507,10 @@
 static void dump_string(const char *field, struct berval **values)
 {
 	int i;
+	fstring val;
 	for (i=0; values[i]; i++) {
-		printf("%s: %s\n", field, values[i]->bv_val);
+		pull_utf8_fstring(val,values[i]->bv_val);
+		d_printf("%s: %s\n", field, val);
 	}
 }
 
@@ -681,12 +693,17 @@
 {
 	char **values;
 	char *ret = NULL;
+	fstring utf8_field;
+
+	push_utf8_fstring(utf8_field,field);
+	values = ldap_get_values(ads->ld, msg, utf8_field);
 
-	values = ldap_get_values(ads->ld, msg, field);
 	if (!values) return NULL;
 	
 	if (values[0]) {
-		ret = talloc_strdup(mem_ctx, values[0]);
+		fstring utf8_string;
+		pull_utf8_fstring(utf8_string,values[0]);
+		ret = talloc_strdup(mem_ctx, utf8_string);
 	}
 	ldap_value_free(values);
 	return ret;
@@ -699,8 +716,11 @@
 		     void *msg, const char *field, uint32 *v)
 {
 	char **values;
+	fstring utf8_field;
+ 
+	push_utf8_fstring(utf8_field,field);
 
-	values = ldap_get_values(ads->ld, msg, field);
+	values = ldap_get_values(ads->ld, msg, utf8_field);
 	if (!values) return False;
 	if (!values[0]) {
 		ldap_value_free(values);
@@ -720,8 +740,10 @@
 {
 	struct berval **values;
 	BOOL ret = False;
-
-	values = ldap_get_values_len(ads->ld, msg, field);
+	fstring utf8_field;
+	
+	push_utf8_fstring(utf8_field,field);
+	values = ldap_get_values_len(ads->ld, msg, utf8_field);
 
 	if (!values) return False;
 
@@ -743,8 +765,10 @@
 	struct berval **values;
 	BOOL ret;
 	int count, i;
+	fstring utf8_field;
 
-	values = ldap_get_values_len(ads->ld, msg, field);
+	push_utf8_fstring(utf8_field,field);
+	values = ldap_get_values_len(ads->ld, msg, utf8_field);
 
 	if (!values) return 0;
 






More information about the samba-technical mailing list