]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/url.c
find_connections fix (ITS#3280) from HEAD
[openldap] / libraries / libldap / url.c
index 85480bd9bfe5269b34b7473ff6b14d9bf1c71885..1615a77ae5784f2d56c30c745ae70a80ed40f03a 100644 (file)
@@ -1,14 +1,24 @@
+/* LIBLDAP url.c -- LDAP URL (RFC 2255) related routines */
 /* $OpenLDAP$ */
-/*
- * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
- * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
- */
-/*  Portions
- *  Copyright (c) 1996 Regents of the University of Michigan.
- *  All rights reserved.
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- *  LIBLDAP url.c -- LDAP URL (RFC 2255) related routines
+ * Copyright 1998-2004 The OpenLDAP Foundation.
+ * All rights reserved.
  *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
+ *
+ * A copy of this license is available in the file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
+ */
+/* Portions Copyright (c) 1996 Regents of the University of Michigan.
+ * All rights reserved.
+ */
+
+
+/*
  *  LDAP URLs look like this:
  *    ldap[is]://host:port[/[dn[?[attributes][?[scope][?[filter][?exts]]]]]]
  *
@@ -68,6 +78,34 @@ int ldap_pvt_url_scheme2proto( const char *scheme )
        return -1;
 }
 
+int ldap_pvt_url_scheme_port( const char *scheme, int port )
+{
+       assert( scheme );
+
+       if( port ) return port;
+       if( scheme == NULL ) return port;
+
+       if( strcmp("ldap", scheme) == 0 ) {
+               return LDAP_PORT;
+       }
+
+       if( strcmp("ldapi", scheme) == 0 ) {
+               return -1;
+       }
+
+       if( strcmp("ldaps", scheme) == 0 ) {
+               return LDAPS_PORT;
+       }
+
+#ifdef LDAP_CONNECTIONLESS
+       if( strcmp("cldap", scheme) == 0 ) {
+               return LDAP_PORT;
+       }
+#endif
+
+       return -1;
+}
+
 int
 ldap_pvt_url_scheme2tls( const char *scheme )
 {
@@ -224,7 +262,7 @@ static int str2scope( const char *p )
        if ( strcasecmp( p, "one" ) == 0 ) {
                return LDAP_SCOPE_ONELEVEL;
 
-       } else if ( strcasecmp( p, "onetree" ) == 0 ) {
+       } else if ( strcasecmp( p, "onelevel" ) == 0 ) {
                return LDAP_SCOPE_ONELEVEL;
 
        } else if ( strcasecmp( p, "base" ) == 0 ) {
@@ -355,7 +393,7 @@ char * ldap_url_desc2str( LDAPURLDesc *u )
        };
 
        if( u->lud_port ) {
-               len+=6;
+               len += sizeof(":65535") - 1;
        }
 
        if( u->lud_host ) {
@@ -527,6 +565,8 @@ ldap_url_parse_ext( LDAP_CONST char *url_in, LDAPURLDesc **ludpp )
        }
 
        if ( q != NULL ) {
+               char    *next;
+
                *q++ = '\0';
                ldap_pvt_hex_unescape( q );
 
@@ -536,7 +576,12 @@ ldap_url_parse_ext( LDAP_CONST char *url_in, LDAPURLDesc **ludpp )
                        return LDAP_URL_ERR_BADURL;
                }
 
-               ludp->lud_port = atoi( q );
+               ludp->lud_port = strtol( q, &next, 10 );
+               if ( next == NULL || next[0] != '\0' ) {
+                       LDAP_FREE( url );
+                       ldap_free_urldesc( ludp );
+                       return LDAP_URL_ERR_BADURL;
+               }
        }
 
        ldap_pvt_hex_unescape( url );
@@ -880,6 +925,12 @@ ldap_url_duplist (LDAPURLDesc *ludlist)
 
 int
 ldap_url_parselist (LDAPURLDesc **ludlist, const char *url )
+{
+       return ldap_url_parselist_ext( ludlist, url, ", " );
+}
+
+int
+ldap_url_parselist_ext (LDAPURLDesc **ludlist, const char *url, const char *sep )
 {
        int i, rc;
        LDAPURLDesc *ludp;
@@ -890,9 +941,9 @@ ldap_url_parselist (LDAPURLDesc **ludlist, const char *url )
 
        *ludlist = NULL;
 
-       urls = ldap_str2charray(url, ", ");
+       urls = ldap_str2charray(url, sep);
        if (urls == NULL)
-               return LDAP_NO_MEMORY;
+               return LDAP_URL_ERR_MEM;
 
        /* count the URLs... */
        for (i = 0; urls[i] != NULL; i++) ;
@@ -909,7 +960,7 @@ ldap_url_parselist (LDAPURLDesc **ludlist, const char *url )
                *ludlist = ludp;
        }
        ldap_charray_free(urls);
-       return LDAP_SUCCESS;
+       return LDAP_URL_SUCCESS;
 }
 
 int
@@ -970,9 +1021,14 @@ ldap_url_parsehosts(
                                }
                        }
                        if (p != NULL) {
+                               char    *next;
+
                                *p++ = 0;
                                ldap_pvt_hex_unescape(p);
-                               ludp->lud_port = atoi(p);
+                               ludp->lud_port = strtol( p, &next, 10 );
+                               if ( next == NULL || next[0] != '\0' ) {
+                                       return LDAP_PARAM_ERROR;
+                               }
                        }
                }
                ldap_pvt_hex_unescape(ludp->lud_host);
@@ -1042,9 +1098,13 @@ ldap_url_list2urls(
        /* figure out how big the string is */
        size = 1;       /* nul-term */
        for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
-               size += strlen(ludp->lud_scheme) + strlen(ludp->lud_host);
-               if (strchr(ludp->lud_host, ':'))        /* will add [ ] below */
-                       size += 2;
+               size += strlen(ludp->lud_scheme);
+               if ( ludp->lud_host ) {
+                       size += strlen(ludp->lud_host);
+                       /* will add [ ] below */
+                       if (strchr(ludp->lud_host, ':'))
+                               size += 2;
+               }
                size += sizeof(":/// ");
 
                if (ludp->lud_port != 0) {
@@ -1059,9 +1119,11 @@ ldap_url_list2urls(
 
        p = s;
        for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
-               p += sprintf(p,
-                            strchr(ludp->lud_host, ':') ? "%s://[%s]" : "%s://%s",
-                            ludp->lud_scheme, ludp->lud_host);
+               p += sprintf(p, "%s://", ludp->lud_scheme);
+               if ( ludp->lud_host ) {
+                       p += sprintf(p, strchr(ludp->lud_host, ':') 
+                                       ? "[%s]" : "%s", ludp->lud_host);
+               }
                if (ludp->lud_port != 0)
                        p += sprintf(p, ":%d", ludp->lud_port);
                *p++ = '/';