Escaping of ldapi:// URIs (1.19rc4)

Ivan Nejgebauer <[email protected]>
Newsgroups gmane.mail.perdition.user
Message-ID <[email protected]>
When connecting to an LDAP server with the ldapi: URI scheme (using a UNIX 
domain socket), the "host" portion of the URI contains the full path to 
the socket special file. Forward slash characters in the path must be 
URL-escaped to avoid confusion with the slashes that delimit the 
components of the URI itself. The function perdition_ldap_uri() in 
perdition/db/ldap/perditiondb_ldap.c doesn't handle ldapi:// URIs in any 
special way, which makes it impossible to use such URIs for contacting the 
LDAP server. The attached patch adds ldapi:// recognition and path 
escaping to that function.

i.

______________________________________________
Perdition-users mailing list
[email protected]
http://lists.vergenet.net/listinfo/perdition-users
urihandling.patch (text/x-diff, 2 KB)
--- perdition/db/ldap/perditiondb_ldap.c.orig	2010-09-23 12:38:38.000000000 +0200
+++ perdition/db/ldap/perditiondb_ldap.c	2010-09-23 14:10:51.000000000 +0200
@@ -324,9 +324,27 @@
 #endif
 
 #if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
+static void append_escaped_host (char *uri, char *start, int cnt)
+{
+	int c;
+
+	uri += strlen(uri);
+	while (cnt--) {
+		c = *start++;
+		if (c == '/') {
+			*uri++ = '%';
+			*uri++ = '2';
+			*uri++ = 'F';
+		} else
+			*uri++ = c;
+	}
+	*uri = '\0';
+}
+
 static char *perdition_ldap_uri (const LDAPURLDesc *lud)
 {
 	int nhost = 1;
+	int isldapi = 0, slashcnt = 0;
 	char *uri, *start, *end;
 
 	/* Multiple hosts may be supplied, space delimited.
@@ -343,13 +361,26 @@
 			start++;
 	}
 
+	isldapi = !strcmp(lud->lud_scheme, "ldapi");
+	if (isldapi) {
+		char *h = lud->lud_host;
+
+		while (*h)
+			if (*h++ == '/')
+				slashcnt++;
+	}
 	/*
          * The '+9' on calloc is the worst case scenario of a non-default
          * LDAP port: 65535 and such. The extra bytes are for the leading
 	 * "://" and trailing ' ' or '\n'.
+	 *
+	 * slashcnt counts the number of slashes in the "hostname" (socket
+	 * path, actually) for the ldapi scheme URIs. Slashes must be
+	 * URL-escaped ("%2F") for the URI to work. This means that the
+	 * extra slashcnt*2 characters must be allocated.
          */
 	uri = calloc((strlen(lud->lud_scheme) + 9) * nhost +
-		     strlen(lud->lud_host) + 1, 1);
+		     strlen(lud->lud_host) + 1 + slashcnt * 2, 1);
 	if (!uri)
 		return NULL;
 
@@ -368,8 +399,14 @@
 			strcat(uri, " ");
 		strcat(uri, lud->lud_scheme);
 		strcat(uri, "://");
-		strncat(uri, start, end - start);
-		if (lud->lud_port != LDAP_PORT) {
+		if (isldapi)
+			append_escaped_host(uri, start, end - start);
+		else
+			strncat(uri, start, end - start);
+		/*
+		 * ldapi:// URIs don't have the :port part
+		 */
+		if (!isldapi && lud->lud_port != LDAP_PORT) {
 			strcat(uri, ":");
 			sprintf(uri + strlen(uri),
 				"%d", lud->lud_port);
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.