X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=libraries%2Flibldap%2Frequest.c;h=07da8e9bcec79d63a8c3a8c9dc862afc6082c72f;hb=403f4479bc9f9a864122d4aeecf7284408918302;hp=24c9d218501bd8486db21f8a44baa464bd7a2584;hpb=dc07e765f263ef459dcd2afd1ece01cfc85a0edd;p=openldap diff --git a/libraries/libldap/request.c b/libraries/libldap/request.c index 24c9d21850..07da8e9bce 100644 --- a/libraries/libldap/request.c +++ b/libraries/libldap/request.c @@ -1,3 +1,4 @@ +/* $OpenLDAP$ */ /* * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved. * COPYING RESTRICTIONS APPLY, see COPYRIGHT file @@ -12,7 +13,8 @@ #include "portable.h" #include -#include + +#include #include #include @@ -23,21 +25,19 @@ #include "ldap-int.h" #include "lber.h" -#if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) || defined( LDAP_API_FEATURE_X_OPENLDAP_V2_DNS ) static LDAPConn *find_connection LDAP_P(( LDAP *ld, LDAPServer *srv, int any )); static void use_connection LDAP_P(( LDAP *ld, LDAPConn *lc )); static void free_servers LDAP_P(( LDAPServer *srvlist )); -#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS || LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */ - #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS -static LDAPServer *dn2servers LDAP_P(( LDAP *ld, char *dn )); +static LDAPServer *dn2servers LDAP_P(( LDAP *ld, const char *dn )); #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */ -#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS -static BerElement *re_encode_request LDAP_P(( LDAP *ld, BerElement *origber, - int msgid, char **dnp )); -#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */ +static BerElement *re_encode_request LDAP_P(( + LDAP *ld, + BerElement *origber, + ber_int_t msgid, + char **dnp )); BerElement * @@ -45,7 +45,7 @@ ldap_alloc_ber_with_options( LDAP *ld ) { BerElement *ber; - if (( ber = ber_alloc_t( ld->ld_lberoptions )) == NULLBER ) { + if (( ber = ber_alloc_t( ld->ld_lberoptions )) == NULL ) { ld->ld_errno = LDAP_NO_MEMORY; #ifdef STR_TRANSLATION } else { @@ -71,28 +71,25 @@ ldap_set_ber_options( LDAP *ld, BerElement *ber ) } -int +ber_int_t ldap_send_initial_request( LDAP *ld, - unsigned long msgtype, + ber_tag_t msgtype, const char *dn, BerElement *ber ) { -#if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) || defined( LDAP_API_FEATURE_X_OPENLDAP_V2_DNS ) LDAPServer *servers, *srv; -#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS || LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */ Debug( LDAP_DEBUG_TRACE, "ldap_send_initial_request\n", 0, 0, 0 ); if ( ! ber_pvt_sb_in_use(&ld->ld_sb ) ) { /* not connected yet */ -#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS - if (( srv = (LDAPServer *)calloc( 1, sizeof( LDAPServer ))) == + if (( srv = (LDAPServer *)LDAP_CALLOC( 1, sizeof( LDAPServer ))) == NULL || ( ld->ld_defhost != NULL && ( srv->lsrv_host = - strdup( ld->ld_defhost )) == NULL )) + LDAP_STRDUP( ld->ld_defhost )) == NULL )) { - if (srv != NULL) free( srv ); + if (srv != NULL) LDAP_FREE( srv ); ber_free( ber, 1 ); ld->ld_errno = LDAP_NO_MEMORY; return( -1 ); @@ -103,44 +100,22 @@ ldap_send_initial_request( if (( ld->ld_defconn = ldap_new_connection( ld, &srv, 1,1,0 )) == NULL ) { - if ( ld->ld_defhost != NULL ) free( srv->lsrv_host ); - free( (char *)srv ); + if ( ld->ld_defhost != NULL ) LDAP_FREE( srv->lsrv_host ); + LDAP_FREE( (char *)srv ); ber_free( ber, 1 ); ld->ld_errno = LDAP_SERVER_DOWN; return( -1 ); } ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */ -#else /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */ - if ( open_ldap_connection( ld, &ld->ld_sb, ld->ld_defhost, - ld->ld_defport, &ld->ld_host, 0 ) < 0 ) - { - ldap_ld_free( ld, 0 ); - ber_free( ber, 1 ); - ld->ld_errno = LDAP_SERVER_DOWN; - return( -1 ); - } -#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */ - Debug( LDAP_DEBUG_TRACE, "ldap_delayed_open successful, ld_host is %s\n", ( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 ); } -#if !defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) && !defined( LDAP_API_FEATURE_X_OPENLDAP_V2_DNS ) - - if ( ber_flush( &ld->ld_sb, ber, 1 ) != 0 ) { - ld->ld_errno = LDAP_SERVER_DOWN; - return( -1 ); - } - - ld->ld_errno = LDAP_SUCCESS; - return( ld->ld_msgid ); -#else /* !LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS && !LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */ - #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS - if (( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_DNS ) == LDAP_OPT_ON ) + if ( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_DNS ) && ldap_is_dns_dn( dn ) ) { if (( servers = dn2servers( ld, dn )) == NULL ) { @@ -162,28 +137,31 @@ ldap_send_initial_request( } } #endif /* LDAP_DEBUG */ - } else { + } else #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */ + { /* * use of DNS is turned off or this is an X.500 DN... * use our default connection */ servers = NULL; -#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS } -#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */ return( ldap_send_server_request( ld, ber, ld->ld_msgid, NULL, servers, NULL, 0 )); -#endif /* !LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS && !LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */ } -#if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) || defined( LDAP_API_FEATURE_X_OPENLDAP_V2_DNS ) int -ldap_send_server_request( LDAP *ld, BerElement *ber, int msgid, LDAPRequest - *parentreq, LDAPServer *srvlist, LDAPConn *lc, int bind ) +ldap_send_server_request( + LDAP *ld, + BerElement *ber, + ber_int_t msgid, + LDAPRequest *parentreq, + LDAPServer *srvlist, + LDAPConn *lc, + int bind ) { LDAPRequest *lr; int incparent; @@ -223,7 +201,7 @@ ldap_send_server_request( LDAP *ld, BerElement *ber, int msgid, LDAPRequest } use_connection( ld, lc ); - if (( lr = (LDAPRequest *)calloc( 1, sizeof( LDAPRequest ))) == + if (( lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ))) == NULL ) { ld->ld_errno = LDAP_NO_MEMORY; ldap_free_connection( ld, lc, 0, 0 ); @@ -302,10 +280,10 @@ ldap_new_connection( LDAP *ld, LDAPServer **srvlistp, int use_ldsb, * make a new LDAP server connection * XXX open connection synchronously for now */ - if (( lc = (LDAPConn *)calloc( 1, sizeof( LDAPConn ))) == NULL || + if (( lc = (LDAPConn *)LDAP_CALLOC( 1, sizeof( LDAPConn ))) == NULL || ( !use_ldsb && ( (sb = ber_sockbuf_alloc()) == NULL ))) { if ( lc != NULL ) { - free( (char *)lc ); + LDAP_FREE( (char *)lc ); } ld->ld_errno = LDAP_NO_MEMORY; return( NULL ); @@ -329,7 +307,7 @@ ldap_new_connection( LDAP *ld, LDAPServer **srvlistp, int use_ldsb, if ( !use_ldsb ) { ber_sockbuf_free( lc->lconn_sb ); } - free( (char *)lc ); + LDAP_FREE( (char *)lc ); ld->ld_errno = LDAP_SERVER_DOWN; return( NULL ); } @@ -432,7 +410,7 @@ static void use_connection( LDAP *ld, LDAPConn *lc ) { ++lc->lconn_refcnt; - lc->lconn_lastused = time( 0 ); + lc->lconn_lastused = time( NULL ); } @@ -451,7 +429,9 @@ ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind ) } ldap_close_connection( lc->lconn_sb ); ber_pvt_sb_destroy( lc->lconn_sb ); - ber_clear( &lc->lconn_ber, 1 ); + if( lc->lconn_ber != NULL ) { + ber_free( lc->lconn_ber, 1 ); + } } prevlc = NULL; for ( tmplc = ld->ld_conns; tmplc != NULL; @@ -468,16 +448,16 @@ ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind ) } free_servers( lc->lconn_server ); if ( lc->lconn_krbinstance != NULL ) { - free( lc->lconn_krbinstance ); + LDAP_FREE( lc->lconn_krbinstance ); } if ( lc->lconn_sb != &ld->ld_sb ) { ber_sockbuf_free( lc->lconn_sb ); } - free( lc ); + LDAP_FREE( lc ); Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: actually freed\n", 0, 0, 0 ); } else { - lc->lconn_lastused = time( 0 ); + lc->lconn_lastused = time( NULL ); Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: refcnt %d\n", lc->lconn_refcnt, 0, 0 ); } @@ -535,18 +515,19 @@ ldap_dump_requests_and_responses( LDAP *ld ) } fprintf( stderr, "** Response Queue:\n" ); - if (( lm = ld->ld_responses ) == NULLMSG ) { + if (( lm = ld->ld_responses ) == NULL ) { fprintf( stderr, " Empty\n" ); } - for ( ; lm != NULLMSG; lm = lm->lm_next ) { - fprintf( stderr, " * msgid %d, type %d\n", - lm->lm_msgid, lm->lm_msgtype ); + for ( ; lm != NULL; lm = lm->lm_next ) { + fprintf( stderr, " * msgid %d, type %lu\n", + lm->lm_msgid, (unsigned long) lm->lm_msgtype ); if (( l = lm->lm_chain ) != NULL ) { fprintf( stderr, " chained responses:\n" ); - for ( ; l != NULLMSG; l = l->lm_chain ) { + for ( ; l != NULL; l = l->lm_chain ) { fprintf( stderr, - " * msgid %d, type %d\n", - l->lm_msgid, l->lm_msgtype ); + " * msgid %d, type %lu\n", + l->lm_msgid, + (unsigned long) l->lm_msgtype ); } } } @@ -587,14 +568,14 @@ ldap_free_request( LDAP *ld, LDAPRequest *lr ) } if ( lr->lr_res_error != NULL ) { - free( lr->lr_res_error ); + LDAP_FREE( lr->lr_res_error ); } if ( lr->lr_res_matched != NULL ) { - free( lr->lr_res_matched ); + LDAP_FREE( lr->lr_res_matched ); } - free( lr ); + LDAP_FREE( lr ); } @@ -606,19 +587,17 @@ free_servers( LDAPServer *srvlist ) while ( srvlist != NULL ) { nextsrv = srvlist->lsrv_next; if ( srvlist->lsrv_dn != NULL ) { - free( srvlist->lsrv_dn ); + LDAP_FREE( srvlist->lsrv_dn ); } if ( srvlist->lsrv_host != NULL ) { - free( srvlist->lsrv_host ); + LDAP_FREE( srvlist->lsrv_host ); } - free( srvlist ); + LDAP_FREE( srvlist ); srvlist = nextsrv; } } -#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS || LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */ -#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS /* * XXX merging of errors in this routine needs to be improved */ @@ -726,15 +705,15 @@ ldap_chase_referrals( LDAP *ld, LDAPRequest *lr, char **errstrp, int *hadrefp ) #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS if ( ldapref ) { #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */ - if (( srv = (LDAPServer *)calloc( 1, + if (( srv = (LDAPServer *)LDAP_CALLOC( 1, sizeof( LDAPServer ))) == NULL ) { ber_free( ber, 1 ); ld->ld_errno = LDAP_NO_MEMORY; return( -1 ); } - if (( srv->lsrv_host = strdup( tmpref )) == NULL ) { - free( (char *)srv ); + if (( srv->lsrv_host = LDAP_STRDUP( tmpref )) == NULL ) { + LDAP_FREE( (char *)srv ); ber_free( ber, 1 ); ld->ld_errno = LDAP_NO_MEMORY; return( -1 ); @@ -744,7 +723,7 @@ ldap_chase_referrals( LDAP *ld, LDAPRequest *lr, char **errstrp, int *hadrefp ) *ports++ = '\0'; srv->lsrv_port = atoi( ports ); } else { - srv->lsrv_port = openldap_ldap_global_options.ldo_defport; + srv->lsrv_port = ldap_int_global_options.ldo_defport; } #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS } else { @@ -763,11 +742,11 @@ ldap_chase_referrals( LDAP *ld, LDAPRequest *lr, char **errstrp, int *hadrefp ) } if ( !newdn && refdn != NULL ) { - free( refdn ); + LDAP_FREE( refdn ); } } - free( *errstrp ); + LDAP_FREE( *errstrp ); *errstrp = unfollowed; return(( rc == 0 ) ? count : rc ); @@ -781,11 +760,11 @@ ldap_append_referral( LDAP *ld, char **referralsp, char *s ) if ( *referralsp == NULL ) { first = 1; - *referralsp = (char *)malloc( strlen( s ) + LDAP_REF_STR_LEN + *referralsp = (char *)LDAP_MALLOC( strlen( s ) + LDAP_REF_STR_LEN + 1 ); } else { first = 0; - *referralsp = (char *)realloc( *referralsp, + *referralsp = (char *)LDAP_REALLOC( *referralsp, strlen( *referralsp ) + strlen( s ) + 2 ); } @@ -807,20 +786,22 @@ ldap_append_referral( LDAP *ld, char **referralsp, char *s ) static BerElement * -re_encode_request( LDAP *ld, BerElement *origber, int msgid, char **dnp ) +re_encode_request( LDAP *ld, BerElement *origber, ber_int_t msgid, char **dnp ) { /* * XXX this routine knows way too much about how the lber library works! */ - unsigned long along, tag; - long ver; + ber_int_t along; + ber_len_t len; + ber_tag_t tag; + ber_int_t ver; int rc; BerElement tmpber, *ber; char *orig_dn; Debug( LDAP_DEBUG_TRACE, - "re_encode_request: new msgid %d, new dn <%s>\n", - msgid, ( *dnp == NULL ) ? "NONE" : *dnp, 0 ); + "re_encode_request: new msgid %ld, new dn <%s>\n", + (long) msgid, ( *dnp == NULL ) ? "NONE" : *dnp, 0 ); tmpber = *origber; @@ -828,18 +809,18 @@ re_encode_request( LDAP *ld, BerElement *origber, int msgid, char **dnp ) * all LDAP requests are sequences that start with a message id, * followed by a sequence that is tagged with the operation code */ - if ( ber_scanf( &tmpber, "{i", &along ) != LDAP_TAG_MSGID || - ( tag = ber_skip_tag( &tmpber, &along )) == LBER_DEFAULT ) { + if ( ber_scanf( &tmpber, "{i", /*}*/ &along ) != LDAP_TAG_MSGID || + ( tag = ber_skip_tag( &tmpber, &len )) == LBER_DEFAULT ) { ld->ld_errno = LDAP_DECODING_ERROR; return( NULL ); } - if (( ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) { + if (( ber = ldap_alloc_ber_with_options( ld )) == NULL ) { return( NULL ); } /* bind requests have a version number before the DN & other stuff */ - if ( tag == LDAP_REQ_BIND && ber_get_int( &tmpber, (long *)&ver ) == + if ( tag == LDAP_REQ_BIND && ber_get_int( &tmpber, &ver ) == LBER_DEFAULT ) { ld->ld_errno = LDAP_DECODING_ERROR; ber_free( ber, 1 ); @@ -855,13 +836,13 @@ re_encode_request( LDAP *ld, BerElement *origber, int msgid, char **dnp ) if ( *dnp == NULL ) { *dnp = orig_dn; } else { - free( orig_dn ); + LDAP_FREE( orig_dn ); } if ( tag == LDAP_REQ_BIND ) { - rc = ber_printf( ber, "{it{is", msgid, tag, ver, *dnp ); + rc = ber_printf( ber, "{it{is" /*}}*/, msgid, tag, ver, *dnp ); } else { - rc = ber_printf( ber, "{it{s", msgid, tag, *dnp ); + rc = ber_printf( ber, "{it{s" /*}}*/, msgid, tag, *dnp ); } if ( rc == -1 ) { @@ -871,7 +852,7 @@ re_encode_request( LDAP *ld, BerElement *origber, int msgid, char **dnp ) if ( ber_write( ber, tmpber.ber_ptr, ( tmpber.ber_end - tmpber.ber_ptr ), 0 ) != ( tmpber.ber_end - tmpber.ber_ptr ) || - ber_printf( ber, "}}" ) == -1 ) { + ber_printf( ber, /*{{*/ "}}" ) == -1 ) { ld->ld_errno = LDAP_ENCODING_ERROR; ber_free( ber, 1 ); return( NULL ); @@ -890,7 +871,7 @@ re_encode_request( LDAP *ld, BerElement *origber, int msgid, char **dnp ) LDAPRequest * -ldap_find_request_by_msgid( LDAP *ld, int msgid ) +ldap_find_request_by_msgid( LDAP *ld, ber_int_t msgid ) { LDAPRequest *lr; @@ -902,14 +883,14 @@ ldap_find_request_by_msgid( LDAP *ld, int msgid ) return( lr ); } -#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */ #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS static LDAPServer * -dn2servers( LDAP *ld, char *dn ) /* dn can also be a domain.... */ +dn2servers( LDAP *ld, const char *dn ) /* dn can also be a domain.... */ { - char *p, *domain, *host, *server_dn, **dxs; + char *p, *host, *server_dn, **dxs; + const char *domain; int i, port; LDAPServer *srvlist, *prevsrv, *srv; @@ -927,7 +908,7 @@ dn2servers( LDAP *ld, char *dn ) /* dn can also be a domain.... */ srvlist = NULL; for ( i = 0; dxs[ i ] != NULL; ++i ) { - port = openldap_ldap_global_options.ldo_defport; + port = ldap_int_global_options.ldo_defport; server_dn = NULL; if ( strchr( dxs[ i ], ':' ) == NULL ) { host = dxs[ i ]; @@ -951,7 +932,7 @@ dn2servers( LDAP *ld, char *dn ) /* dn can also be a domain.... */ } if ( host != NULL ) { /* found a server we can use */ - if (( srv = (LDAPServer *)calloc( 1, + if (( srv = (LDAPServer *)LDAP_CALLOC( 1, sizeof( LDAPServer ))) == NULL ) { free_servers( srvlist ); srvlist = NULL; @@ -967,9 +948,9 @@ dn2servers( LDAP *ld, char *dn ) /* dn can also be a domain.... */ prevsrv = srv; /* copy in info. */ - if (( srv->lsrv_host = strdup( host )) == NULL || + if (( srv->lsrv_host = LDAP_STRDUP( host )) == NULL || ( server_dn != NULL && ( srv->lsrv_dn = - strdup( server_dn )) == NULL )) { + LDAP_STRDUP( server_dn )) == NULL )) { free_servers( srvlist ); srvlist = NULL; break; /* exit loop & return */