summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRich Megginson <rmeggins@redhat.com>2010-08-23 15:07:51 -0600
committerRich Megginson <rmeggins@redhat.com>2010-08-31 10:36:02 -0600
commit1e933d0eaf6591a96a034f3da85d9c6d54c94a88 (patch)
treec0d2097e5d49bcb3beaa021d2f9d8df70195b787
parentd0b39138e041e0053421a0002341c1f300be3e1c (diff)
downloadds-1e933d0eaf6591a96a034f3da85d9c6d54c94a88.tar.gz
ds-1e933d0eaf6591a96a034f3da85d9c6d54c94a88.tar.xz
ds-1e933d0eaf6591a96a034f3da85d9c6d54c94a88.zip
convert mozldap host list to openldap uri list
mozldap accepts hostnames in the form host1[:port1] ... hostN[:portN] in the ldap_init argument and in ldap urls/uris. DS code uses ldap_initialize() which takes a list of uris in the form ldap[s]://host1[:port1]/ ldap[s]://host2[:port2]/ ... We have to convert the host:port list into a list of LDAP uris
-rw-r--r--ldap/servers/slapd/ldaputil.c81
1 files changed, 77 insertions, 4 deletions
diff --git a/ldap/servers/slapd/ldaputil.c b/ldap/servers/slapd/ldaputil.c
index ac0db055..d411d777 100644
--- a/ldap/servers/slapd/ldaputil.c
+++ b/ldap/servers/slapd/ldaputil.c
@@ -79,6 +79,79 @@ slapi_ldap_unbind( LDAP *ld )
}
}
+/* mozldap ldap_init and ldap_url_parse accept a hostname in the form
+ host1[:port1]SPACEhost2[:port2]SPACEhostN[:portN]
+ where SPACE is a single space (0x20) character
+ for openldap, we have to convert this to a string like this:
+ PROTO://host1[:port1]/SPACEPROTO://host2[:port2]/SPACEPROTO://hostN[:portN]/
+ where PROTO is ldap or ldaps or ldapi
+ if proto is NULL, assume hostname_or_uri is really a valid ldap uri
+*/
+static char *
+convert_to_openldap_uri(const char *hostname_or_uri, int port, const char *proto)
+{
+ char *retstr = NULL;
+ char *my_copy = NULL;
+ char *start = NULL;
+ char *iter = NULL;
+ char *s = NULL;
+ const char *brkstr = " ";
+
+ if (!hostname_or_uri) {
+ return NULL;
+ }
+
+ my_copy = slapi_ch_strdup(hostname_or_uri);
+ /* see if hostname_or_uri is an ldap uri */
+ if (!proto && !PL_strncasecmp(my_copy, "ldap", 4)) {
+ start = my_copy + 4;
+ if ((*start == 's') || (*start == 'i')) {
+ start++;
+ }
+ if (!PL_strncmp(start, "://", 3)) {
+ *start = '\0';
+ proto = my_copy;
+ start += 3;
+ } else {
+ slapi_log_error(SLAPI_LOG_FATAL, "convert_to_openldap_uri",
+ "The given LDAP URI [%s] is not valid\n", hostname_or_uri);
+ goto end;
+ }
+ } else if (!proto) {
+ slapi_log_error(SLAPI_LOG_FATAL, "convert_to_openldap_uri",
+ "The given LDAP URI [%s] is not valid\n", hostname_or_uri);
+ goto end;
+ } else {
+ start = my_copy; /* just assume it's not a uri */
+ }
+
+ for (s = ldap_utf8strtok_r(my_copy, brkstr, &iter); s != NULL;
+ s = ldap_utf8strtok_r(NULL, brkstr, &iter)) {
+ char *ptr;
+ int last = 0;
+ /* strtok will grab the '/' at the end of the uri, if any,
+ so terminate parsing there */
+ if ((ptr = strchr(s, '/'))) {
+ *ptr = '\0';
+ last = 1;
+ }
+ if (retstr) {
+ retstr = PR_sprintf_append(retstr, "/ %s://%s", proto, s);
+ } else {
+ retstr = PR_smprintf("%s://%s", proto, s);
+ }
+ if (last) {
+ break;
+ }
+ }
+
+ /* add the port on the last one */
+ retstr = PR_sprintf_append(retstr, ":%d/", port);
+end:
+ slapi_ch_free_string(&my_copy);
+ return retstr;
+}
+
const char *
slapi_urlparse_err2string( int err )
{
@@ -571,7 +644,7 @@ slapi_ldap_init_ext(
if (filename) {
makeurl = slapi_ch_smprintf("ldapi://%s/", filename);
} else { /* host port */
- makeurl = slapi_ch_smprintf("ldap%s://%s:%d/", (secure == 1 ? "s" : ""), hostname, port);
+ makeurl = convert_to_openldap_uri(hostname, port, (secure == 1 ? "ldaps" : "ldap"));
}
rc = ldap_initialize(&ld, makeurl);
if (rc) {
@@ -806,11 +879,11 @@ slapi_ldap_bind(
/* openldap doesn't have a SSL/TLS yes/no flag - so grab the
ldapurl, parse it, and see if it is a secure one */
char *ldapurl = NULL;
- LDAPURLDesc *ludp = NULL;
ldap_get_option(ld, LDAP_OPT_URI, &ldapurl);
- slapi_ldap_url_parse(ldapurl, &ludp, 0, &secure);
- ldap_free_urldesc(ludp);
+ if (ldapurl && !PL_strncasecmp(ldapurl, "ldaps", 5)) {
+ secure = 1;
+ }
slapi_ch_free_string(&ldapurl);
#else /* !USE_OPENLDAP */
ldap_get_option(ld, LDAP_OPT_SSL, &secure);