X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=libraries%2Flibldap%2Furl.c;h=c5a4e0650c7f12181aaac0a97e7b315a5601533a;hb=8f4f94d4152185f2e445ec0929787095ff15f922;hp=204f2978ffe7e26e02779e04ab1daa5cad523fc9;hpb=fcbca73f90d6f4dd09e7ff34533b6ac1baff2135;p=openldap diff --git a/libraries/libldap/url.c b/libraries/libldap/url.c index 204f2978ff..c5a4e0650c 100644 --- a/libraries/libldap/url.c +++ b/libraries/libldap/url.c @@ -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. * @@ -17,96 +21,80 @@ * We also tolerate URLs that look like: and */ -#ifndef lint -static char copyright[] = "@(#) Copyright (c) 1996 Regents of the University of Michigan.\nAll rights reserved.\n"; -#endif +#include "portable.h" #include -#include -#include - -#ifdef MACOS -#include -#include "macos.h" -#endif /* MACOS */ - -#if defined( DOS ) || defined( _WIN32 ) -#include -#include -#include "msdos.h" -#endif /* DOS || _WIN32 */ - -#if !defined(MACOS) && !defined(DOS) && !defined( _WIN32 ) -#include -#include -#include -#endif /* !MACOS && !DOS && !_WIN32 */ - -#include "lber.h" -#include "ldap.h" + +#include + +#include +#include +#include +#include + #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 = ldap_send_server_request( ld, ber, ld->ld_msgid, NULL, srv, NULL, 1 ); -#else /* LDAP_REFERRALS */ - err = ldap_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;