curl-library
Ldap URL and binary entries
Date: Tue, 31 May 2005 10:30:32 +0200
The current version does not work properly with binary entries in LDAP.
I have a small patch for that, but there's room for more enhancements.
My simple version encodes the attribute value in base64 only when
there's a binary zero inside.
But RFC 2849 says you must encode as base64 as soon as there is any
unsafe character, both for the value and the dn/rdn.
Another point is that LDAP Browser/Editor finds a ";binary" option for
the entries I'm working with, but I don't where he extracts that info
from. I thought about systematically adding it when the entries have a
binary content, but I finally commented it out, and only use the double
colon to indicate base64 encoding. It would be better to find the way to
extract all the option values, and include them in the output.
Also I would like to add an option not to output as ldif, but to get
directly the raw value of an attribute (it would be left to the user to
properly set the URL so that it matches only one attribute). base64
encoding of unsafe values wouldn't be required for that case.
Together with that there would an optional parameter to get only the
n-th value of a multi-valued attribute (and either an empty or error
result if it doesn't exist).
But I wonder what would be the recommended way of adding such options ?
Would it be to add new elements to the connectdata structure ? That's
the only one element available from Curl_ldap.
Anyway here's my current patch :
RCS file: /cvsroot/curl/curl/lib/ldap.c,v
retrieving revision 1.49
diff -u -w -b -r1.49 ldap.c
--- ldap.c 11 Mar 2005 05:28:07 -0000 1.49
+++ ldap.c 31 May 2005 08:21:25 -0000
@@ -61,6 +61,7 @@
#include "strtok.h"
#include "ldap.h"
#include "memory.h"
+#include "base64.h"
#define _MPRINTF_REPLACE /* use our functions only */
#include <curl/mprintf.h>
@@ -247,8 +248,8 @@
char *(__cdecl *ldap_get_dn)(void *, void *);
char *(__cdecl *ldap_first_attribute)(void *, void *, void **);
char *(__cdecl *ldap_next_attribute)(void *, void *, void *);
- char **(__cdecl *ldap_get_values)(void *, void *, const char *);
- void (__cdecl *ldap_value_free)(char **);
+ struct berval **(__cdecl *ldap_get_values_len)(void *, void *, const
char *);
+ void (__cdecl *ldap_value_free_len)(struct berval **);
void (__cdecl *ldap_memfree)(void *);
void (__cdecl *ber_free)(void *, int);
@@ -286,8 +287,8 @@
DYNA_GET_FUNCTION(char *(__cdecl *)(void *, void *), ldap_get_dn);
DYNA_GET_FUNCTION(char *(__cdecl *)(void *, void *, void **),
ldap_first_attribute);
DYNA_GET_FUNCTION(char *(__cdecl *)(void *, void *, void *),
ldap_next_attribute);
- DYNA_GET_FUNCTION(char **(__cdecl *)(void *, void *, const char *),
ldap_get_values);
- DYNA_GET_FUNCTION(void (__cdecl *)(char **), ldap_value_free);
+ DYNA_GET_FUNCTION(struct berval **(__cdecl *)(void *, void *, const
char *), ldap_get_values_len);
+ DYNA_GET_FUNCTION(void (__cdecl *)(struct berval **),
ldap_value_free_len);
DYNA_GET_FUNCTION(void (__cdecl *)(void *), ldap_memfree);
DYNA_GET_FUNCTION(void (__cdecl *)(void *, int), ber_free);
@@ -346,21 +347,35 @@
attribute;
attribute = (*ldap_next_attribute)(server, entryIterator, ber))
{
- char **vals = (*ldap_get_values)(server, entryIterator, attribute);
+ struct berval **vals = (*ldap_get_values_len)(server,
entryIterator, attribute);
if (vals != NULL)
{
for (i = 0; (vals[i] != NULL); i++)
{
+ int isBinary = ((strlen(vals[i]->bv_val)!=vals[i]->bv_len));
+
Curl_client_write(data, CLIENTWRITE_BODY, (char *)"\t", 1);
Curl_client_write(data, CLIENTWRITE_BODY, (char*) attribute, 0);
+ if ( isBinary ) {
+// Curl_client_write(data, CLIENTWRITE_BODY, (char*)
";binary:", 0);
+ Curl_client_write(data, CLIENTWRITE_BODY, (char*) ":", 1);
+ }
Curl_client_write(data, CLIENTWRITE_BODY, (char *)": ", 2);
- Curl_client_write(data, CLIENTWRITE_BODY, vals[i], 0);
+ if ( isBinary ) {
+ char *encoded;
+ Curl_base64_encode(vals[i]->bv_val,vals[i]->bv_len, &encoded);
+ Curl_client_write(data, CLIENTWRITE_BODY, (char*) encoded, 0);
+ free(encoded);
+ }
+ else {
+ Curl_client_write(data, CLIENTWRITE_BODY, vals[i]->bv_val,
vals[i]->bv_len);
+ }
Curl_client_write(data, CLIENTWRITE_BODY, (char *)"\n", 0);
}
/* Free memory used to store values */
- (*ldap_value_free)(vals);
+ (*ldap_value_free_len)(vals);
}
Curl_client_write(data, CLIENTWRITE_BODY, (char *)"\n", 1);
Received on 2005-05-31