]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/url.c
ldap_url_parse_ext(): Kill duplicate init + pointless free(NULL) of lud_filter.
[openldap] / libraries / libldap / url.c
index e5909bac417e7b1905e9e039e17445050e88e901..5be36f872117026a5de4fc2dac14c55b0afe4f34 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/>.
+ *
+ * Copyright 1998-2004 The OpenLDAP Foundation.
+ * All rights reserved.
  *
- *  LIBLDAP url.c -- LDAP URL (RFC 2255) related routines
+ * 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]]]]]]
  *
@@ -224,7 +234,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 ) {
@@ -235,6 +245,14 @@ static int str2scope( const char *p )
 
        } else if ( strcasecmp( p, "subtree" ) == 0 ) {
                return LDAP_SCOPE_SUBTREE;
+
+#ifdef LDAP_SCOPE_SUBORDINATE
+       } else if ( strcasecmp( p, "subordinate" ) == 0 ) {
+               return LDAP_SCOPE_SUBORDINATE;
+
+       } else if ( strcasecmp( p, "children" ) == 0 ) {
+               return LDAP_SCOPE_SUBORDINATE;
+#endif
        }
 
        return( -1 );
@@ -331,10 +349,13 @@ char * ldap_url_desc2str( LDAPURLDesc *u )
        if ( len ) len++; /* ? */
 
        switch( u->lud_scope ) {
+               case LDAP_SCOPE_BASE:
                case LDAP_SCOPE_ONELEVEL:
                case LDAP_SCOPE_SUBTREE:
-               case LDAP_SCOPE_BASE:
-                       len += sizeof("base");
+#ifdef LDAP_FEATURE_SUBORDINATE_SCOPE
+               case LDAP_SCOPE_SUBORDINATE:
+#endif
+                       len += sizeof("subordinate");
                        if( !sep ) sep = 3;
                        break;
 
@@ -355,7 +376,7 @@ char * ldap_url_desc2str( LDAPURLDesc *u )
        };
 
        if( u->lud_port ) {
-               len+=6;
+               len += sizeof(":65535") - 1;
        }
 
        if( u->lud_host ) {
@@ -403,6 +424,12 @@ char * ldap_url_desc2str( LDAPURLDesc *u )
                strcpy( &s[sofar], "sub" );
                sofar += sizeof("sub") - 1;
                break;
+#ifdef LDAP_FEATURE_SUBORDINATE_SCOPE
+       case LDAP_SCOPE_SUBORDINATE:
+               strcpy( &s[sofar], "children" );
+               sofar += sizeof("children") - 1;
+               break;
+#endif
        }
 
        if( sep < 4 ) goto done;
@@ -491,7 +518,6 @@ ldap_url_parse_ext( LDAP_CONST char *url_in, LDAPURLDesc **ludpp )
        ludp->lud_port = 0;
        ludp->lud_dn = NULL;
        ludp->lud_attrs = NULL;
-       ludp->lud_filter = NULL;
        ludp->lud_scope = LDAP_SCOPE_DEFAULT;
        ludp->lud_filter = NULL;
        ludp->lud_exts = NULL;
@@ -527,6 +553,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 +564,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 );
@@ -691,7 +724,6 @@ ldap_url_parse_ext( LDAP_CONST char *url_in, LDAPURLDesc **ludpp )
                        return LDAP_URL_ERR_BADFILTER;
                }
 
-               LDAP_FREE( ludp->lud_filter );
                ludp->lud_filter = LDAP_STRDUP( p );
 
                if( ludp->lud_filter == NULL ) {
@@ -976,9 +1008,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);