]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/url.c
Set ciphers from slapd.conf.
[openldap] / libraries / libldap / url.c
index 08b47efd6be668222fdd938388f11b4294d18fc2..c5a4e0650c7f12181aaac0a97e7b315a5601533a 100644 (file)
@@ -1,4 +1,8 @@
 /*
+ * Copyright 1998-1999 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.
  *
  *  We also tolerate URLs that look like: <ldapurl> and <URL:ldapurl>
  */
 
-#ifndef lint 
-static char copyright[] = "@(#) Copyright (c) 1996 Regents of the University of Michigan.\nAll rights reserved.\n";
-#endif
+#include "portable.h"
 
 #include <stdio.h>
-#include <string.h>
-#include <ctype.h>
-
-#ifdef MACOS
-#include <stdlib.h>
-#include "macos.h"
-#endif /* MACOS */
-
-#if defined( DOS ) || defined( _WIN32 )
-#include <stdlib.h>
-#include <malloc.h>
-#include "msdos.h"
-#endif /* DOS || _WIN32 */
-
-#if !defined(MACOS) && !defined(DOS) && !defined( _WIN32 )
-#include <sys/time.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#endif /* !MACOS && !DOS && !_WIN32 */
-
-#include "lber.h"
-#include "ldap.h"
+
+#include <ac/stdlib.h>
+
+#include <ac/ctype.h>
+#include <ac/socket.h>
+#include <ac/string.h>
+#include <ac/time.h>
+
 #include "ldap-int.h"
 
 
-#ifdef NEEDPROTOS
-static int skip_url_prefix( char **urlp, int *enclosedp );
-static void hex_unescape( char *s );
+/* local functions */
+static const char* skip_url_prefix LDAP_P(( const char *url, int *enclosedp ));
+static void hex_unescape LDAP_P(( char *s ));
 static int unhex( char c );
-#else /* NEEDPROTOS */
-static int skip_url_prefix();
-static void hex_unescape();
-static int unhex();
-#endif /* NEEDPROTOS */
 
 
 int
-ldap_is_ldap_url( char *url )
+ldap_is_ldap_url( LDAP_CONST char *url )
 {
        int     enclosed;
 
-       return( url != NULL && skip_url_prefix( &url, &enclosed ));
+       return( url != NULL && skip_url_prefix( url, &enclosed ) != NULL );
 }
 
 
-static int
-skip_url_prefix( char **urlp, int *enclosedp )
+static const char*
+skip_url_prefix( const char *url, int *enclosedp )
 {
 /*
  * return non-zero if this looks like a LDAP URL; zero if not
  * if non-zero returned, *urlp will be moved past "ldap://" part of URL
  */
-       if ( *urlp == NULL ) {
-               return( 0 );
+       char* p;
+
+       if ( url == NULL ) {
+               return( NULL );
        }
 
+       p = (char *) url;
+
        /* skip leading '<' (if any) */
-       if ( **urlp == '<' ) {
+       if ( *p == '<' ) {
                *enclosedp = 1;
-               ++*urlp;
+               ++p;
        } else {
                *enclosedp = 0;
        }
 
        /* skip leading "URL:" (if any) */
-       if ( strlen( *urlp ) >= LDAP_URL_URLCOLON_LEN && strncasecmp(
-           *urlp, LDAP_URL_URLCOLON, LDAP_URL_URLCOLON_LEN ) == 0 ) {
-               *urlp += LDAP_URL_URLCOLON_LEN;
+       if ( strlen( p ) >= LDAP_URL_URLCOLON_LEN
+               && strncasecmp( p, LDAP_URL_URLCOLON, LDAP_URL_URLCOLON_LEN ) == 0 )
+       {
+               p += LDAP_URL_URLCOLON_LEN;
        }
 
        /* check for missing "ldap://" prefix */
-       if ( strlen( *urlp ) < LDAP_URL_PREFIX_LEN ||
-           strncasecmp( *urlp, LDAP_URL_PREFIX, LDAP_URL_PREFIX_LEN ) != 0 ) {
-               return( 0 );
+       if ( strlen( p ) < LDAP_URL_PREFIX_LEN ||
+           strncasecmp( p, LDAP_URL_PREFIX, LDAP_URL_PREFIX_LEN ) != 0 ) {
+               return( NULL );
        }
 
        /* skip over "ldap://" prefix and return success */
-       *urlp += LDAP_URL_PREFIX_LEN;
-       return( 1 );
+       p += LDAP_URL_PREFIX_LEN;
+       return( p );
 }
 
 
 
 int
-ldap_url_parse( char *url, LDAPURLDesc **ludpp )
+ldap_url_parse( LDAP_CONST char *url_in, LDAPURLDesc **ludpp )
 {
 /*
  *  Pick apart the pieces of an LDAP URL.
@@ -115,27 +103,33 @@ ldap_url_parse( char *url, LDAPURLDesc **ludpp )
        LDAPURLDesc     *ludp;
        char            *attrs, *p, *q;
        int             enclosed, i, nattrs;
+       const char *url_tmp;
+       char *url;
 
-       Debug( LDAP_DEBUG_TRACE, "ldap_url_parse(%s)\n", url, 0, 0 );
+       Debug( LDAP_DEBUG_TRACE, "ldap_url_parse(%s)\n", url_in, 0, 0 );
 
        *ludpp = NULL;  /* pessimistic */
 
-       if ( !skip_url_prefix( &url, &enclosed )) {
+       url_tmp = skip_url_prefix( url_in, &enclosed );
+
+       if ( url_tmp == NULL ) {
                return( LDAP_URL_ERR_NOTLDAP );
        }
 
-       /* allocate return struct */
-       if (( ludp = (LDAPURLDesc *)calloc( 1, sizeof( LDAPURLDesc )))
-           == NULLLDAPURLDESC ) {
+       /* make working copy of the remainder of the URL */
+       if (( url = LDAP_STRDUP( url_tmp )) == NULL ) {
                return( LDAP_URL_ERR_MEM );
        }
 
-       /* make working copy of the remainder of the URL */
-       if (( url = strdup( url )) == NULL ) {
-               ldap_free_urldesc( ludp );
+       /* allocate return struct */
+       if (( ludp = (LDAPURLDesc *)LDAP_CALLOC( 1, sizeof( LDAPURLDesc )))
+           == NULLLDAPURLDESC )
+       {
+               LDAP_FREE( url );
                return( LDAP_URL_ERR_MEM );
        }
 
+
        if ( enclosed && *((p = url + strlen( url ) - 1)) == '>' ) {
                *p = '\0';
        }
@@ -219,7 +213,7 @@ ldap_url_parse( char *url, LDAPURLDesc **ludpp )
                    }
                }
 
-               if (( ludp->lud_attrs = (char **)calloc( nattrs + 1,
+               if (( ludp->lud_attrs = (char **)LDAP_CALLOC( nattrs + 1,
                    sizeof( char * ))) == NULL ) {
                        ldap_free_urldesc( ludp );
                        return( LDAP_URL_ERR_MEM );
@@ -245,72 +239,63 @@ ldap_free_urldesc( LDAPURLDesc *ludp )
 {
        if ( ludp != NULLLDAPURLDESC ) {
                if ( ludp->lud_string != NULL ) {
-                       free( ludp->lud_string );
+                       LDAP_FREE( ludp->lud_string );
                }
                if ( ludp->lud_attrs != NULL ) {
-                       free( ludp->lud_attrs );
+                       LDAP_FREE( ludp->lud_attrs );
                }
-               free( ludp );
+               LDAP_FREE( ludp );
        }
 }
 
 
 
 int
-ldap_url_search( LDAP *ld, char *url, int attrsonly )
+ldap_url_search( LDAP *ld, LDAP_CONST char *url, int attrsonly )
 {
        int             err;
        LDAPURLDesc     *ludp;
        BerElement      *ber;
-#ifdef LDAP_REFERRALS
        LDAPServer      *srv = NULL;
-#endif /* LDAP_REFERRALS */
 
        if ( ldap_url_parse( url, &ludp ) != 0 ) {
                ld->ld_errno = LDAP_PARAM_ERROR;
                return( -1 );
        }
 
-       if (( ber = ldap_build_search_req( ld, ludp->lud_dn, ludp->lud_scope,
-           ludp->lud_filter, ludp->lud_attrs, attrsonly )) == NULLBER ) {
+       ber = ldap_build_search_req( ld, ludp->lud_dn, ludp->lud_scope,
+           ludp->lud_filter, ludp->lud_attrs, attrsonly, NULL, NULL,
+               -1, -1 );
+
+       if ( ber == NULL ) {
                return( -1 );
        }
 
        err = 0;
 
        if ( ludp->lud_host != NULL || ludp->lud_port != 0 ) {
-#ifdef LDAP_REFERRALS
-               if (( srv = (LDAPServer *)calloc( 1, sizeof( LDAPServer )))
-                   == NULL || ( srv->lsrv_host = strdup( ludp->lud_host ==
+               if (( srv = (LDAPServer *)LDAP_CALLOC( 1, sizeof( LDAPServer )))
+                   == NULL || ( srv->lsrv_host = LDAP_STRDUP( ludp->lud_host ==
                    NULL ? ld->ld_defhost : ludp->lud_host )) == NULL ) {
                        if ( srv != NULL ) {
-                               free( srv );
+                               LDAP_FREE( srv );
                        }
                        ld->ld_errno = LDAP_NO_MEMORY;
                        err = -1;
                } else {
                        if ( ludp->lud_port == 0 ) {
-                               srv->lsrv_port = LDAP_PORT;
+                               srv->lsrv_port = ldap_int_global_options.ldo_defport;
                        } else {
-                                srv->lsrv_port = ludp->lud_port;
+                               srv->lsrv_port = ludp->lud_port;
                        }
                }
-#else /* LDAP_REFERRALS */
-               ld->ld_errno = LDAP_LOCAL_ERROR;
-               err = -1;
-#endif /* LDAP_REFERRALS */
        }
 
        if ( err != 0 ) {
                ber_free( ber, 1 );
        } else {
-#ifdef LDAP_REFERRALS
-               err = send_server_request( ld, ber, ld->ld_msgid, NULL, srv,
+               err = ldap_send_server_request( ld, ber, ld->ld_msgid, NULL, srv,
                    NULL, 1 );
-#else /* LDAP_REFERRALS */
-               err = send_initial_request( ld, LDAP_REQ_SEARCH,
-                   ludp->lud_dn, ber );
-#endif /* LDAP_REFERRALS */
        }
 
        ldap_free_urldesc( ludp );
@@ -320,7 +305,7 @@ ldap_url_search( LDAP *ld, char *url, int attrsonly )
 
 
 int
-ldap_url_search_st( LDAP *ld, char *url, int attrsonly,
+ldap_url_search_st( LDAP *ld, LDAP_CONST char *url, int attrsonly,
        struct timeval *timeout, LDAPMessage **res )
 {
        int     msgid;
@@ -344,7 +329,8 @@ ldap_url_search_st( LDAP *ld, char *url, int attrsonly,
 
 
 int
-ldap_url_search_s( LDAP *ld, char *url, int attrsonly, LDAPMessage **res )
+ldap_url_search_s(
+       LDAP *ld, LDAP_CONST char *url, int attrsonly, LDAPMessage **res )
 {
        int     msgid;