X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=libraries%2Flibldap%2Frequest.c;h=b289bfcb655506db9dfda03ead490632afe2384e;hb=e323437c6aa2cc7791dfc14c31fe0439b71c56e2;hp=97197ce2a03eff0a8a72cdfdd36833752b32b31a;hpb=9ef1a740c24a155cfc7e7088f5da50de4dbc5088;p=openldap diff --git a/libraries/libldap/request.c b/libraries/libldap/request.c index 97197ce2a0..b289bfcb65 100644 --- a/libraries/libldap/request.c +++ b/libraries/libldap/request.c @@ -1,14 +1,21 @@ /* $OpenLDAP$ */ -/* - * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved. - * COPYING RESTRICTIONS APPLY, see COPYRIGHT file +/* This work is part of OpenLDAP Software . + * + * 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 + * . */ -/* Portions - * Copyright (c) 1995 Regents of the University of Michigan. - * All rights reserved. +/* Portions Copyright (c) 1995 Regents of the University of Michigan. + * All rights reserved. */ -/*--- - * This notice applies to changes, created by or for Novell, Inc., +/* This notice applies to changes, created by or for Novell, Inc., * to preexisting works for which notices appear elsewhere in this file. * * Copyright (C) 1999, 2000 Novell, Inc. All Rights Reserved. @@ -25,6 +32,10 @@ * Modification to OpenLDAP source by Novell, Inc. * April 2000 sfs Added code to chase V3 referrals * request.c - sending of ldap requests; handling of referrals + *--- + * Note: A verbatim copy of version 2.0.1 of the OpenLDAP Public License + * can be found in the file "build/LICENSE-2.0.1" in this distribution + * of OpenLDAP Software. */ #include "portable.h" @@ -45,14 +56,13 @@ static LDAPConn *find_connection LDAP_P(( LDAP *ld, LDAPURLDesc *srv, int any )); static void use_connection LDAP_P(( LDAP *ld, LDAPConn *lc )); - -static BerElement *re_encode_request LDAP_P(( - LDAP *ld, +static BerElement * +re_encode_request( LDAP *ld, BerElement *origber, - ber_int_t msgid, - char **dnp, - int *type)); - + ber_int_t msgid, + int sref, + LDAPURLDesc *srv, + int *type ); BerElement * ldap_alloc_ber_with_options( LDAP *ld ) @@ -79,12 +89,17 @@ ldap_send_initial_request( LDAP *ld, ber_tag_t msgtype, const char *dn, - BerElement *ber ) + BerElement *ber, + ber_int_t msgid) { LDAPURLDesc *servers; int rc; +#ifdef NEW_LOGGING + LDAP_LOG ( OPERATION, ENTRY, "ldap_send_initial_request\n", 0, 0, 0 ); +#else Debug( LDAP_DEBUG_TRACE, "ldap_send_initial_request\n", 0, 0, 0 ); +#endif if ( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, NULL ) == -1 ) { /* not connected yet */ @@ -95,9 +110,15 @@ ldap_send_initial_request( return( -1 ); } +#ifdef NEW_LOGGING + LDAP_LOG ( OPERATION, DETAIL1, + "ldap_send_initial_request: ldap_open_defconn: successful\n" , + 0, 0, 0); +#else Debug( LDAP_DEBUG_TRACE, - "ldap_delayed_open successful, ld_host is %s\n", - ( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 ); + "ldap_open_defconn: successful\n", + 0, 0, 0 ); +#endif } { @@ -108,14 +129,65 @@ ldap_send_initial_request( servers = NULL; } - rc = ldap_send_server_request( ld, ber, ld->ld_msgid, NULL, +#ifdef LDAP_CONNECTIONLESS + if (LDAP_IS_UDP(ld)) { + if (msgtype == LDAP_REQ_BIND) { + if (ld->ld_options.ldo_cldapdn) + ldap_memfree(ld->ld_options.ldo_cldapdn); + ld->ld_options.ldo_cldapdn = ldap_strdup(dn); + return 0; + } + if (msgtype != LDAP_REQ_ABANDON && msgtype != LDAP_REQ_SEARCH) + return LDAP_PARAM_ERROR; + } +#endif +#ifdef LDAP_R_COMPILE + ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex ); +#endif + rc = ldap_send_server_request( ld, ber, msgid, NULL, servers, NULL, NULL ); +#ifdef LDAP_R_COMPILE + ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex ); +#endif if (servers) ldap_free_urllist(servers); return(rc); } +int +ldap_int_flush_request( + LDAP *ld, + LDAPRequest *lr +) +{ + LDAPConn *lc = lr->lr_conn; + + if ( ber_flush( lc->lconn_sb, lr->lr_ber, 0 ) != 0 ) { + if ( errno == EAGAIN ) { + /* need to continue write later */ + lr->lr_status = LDAP_REQST_WRITING; + ldap_mark_select_write( ld, lc->lconn_sb ); + ld->ld_errno = LDAP_BUSY; + return -2; + } else { + ld->ld_errno = LDAP_SERVER_DOWN; + ldap_free_request( ld, lr ); + ldap_free_connection( ld, lc, 0, 0 ); + return( -1 ); + } + } else { + if ( lr->lr_parent == NULL ) { + lr->lr_ber->ber_end = lr->lr_ber->ber_ptr; + lr->lr_ber->ber_ptr = lr->lr_ber->ber_buf; + } + lr->lr_status = LDAP_REQST_INPROGRESS; + + /* sent -- waiting for a response */ + ldap_mark_select_read( ld, lc->lconn_sb ); + } + return 0; +} int ldap_send_server_request( @@ -128,9 +200,13 @@ ldap_send_server_request( LDAPreqinfo *bind ) { LDAPRequest *lr; - int incparent; + int incparent, rc; +#ifdef NEW_LOGGING + LDAP_LOG ( OPERATION, ENTRY, "ldap_send_server_request\n", 0, 0, 0 ); +#else Debug( LDAP_DEBUG_TRACE, "ldap_send_server_request\n", 0, 0, 0 ); +#endif incparent = 0; ld->ld_errno = LDAP_SUCCESS; /* optimistic */ @@ -164,6 +240,20 @@ ldap_send_server_request( } use_connection( ld, lc ); + + /* If we still have an incomplete write, try to finish it before + * dealing with the new request. If we don't finish here, return + * LDAP_BUSY and let the caller retry later. We only allow a single + * request to be in WRITING state. + */ + rc = 0; + if ( ld->ld_requests && + ld->ld_requests->lr_status == LDAP_REQST_WRITING && + ldap_int_flush_request( ld, ld->ld_requests ) < 0 ) { + rc = -1; + } + if ( rc ) return rc; + if (( lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ))) == NULL ) { ld->ld_errno = LDAP_NO_MEMORY; @@ -188,8 +278,8 @@ ldap_send_server_request( lr->lr_origid = parentreq->lr_origid; lr->lr_parentcnt = parentreq->lr_parentcnt + 1; lr->lr_parent = parentreq; - lr->lr_refnext = parentreq->lr_refnext; - parentreq->lr_refnext = lr; + lr->lr_refnext = parentreq->lr_child; + parentreq->lr_child = lr; } else { /* original request */ lr->lr_origid = lr->lr_msgid; } @@ -200,33 +290,11 @@ ldap_send_server_request( ld->ld_requests = lr; lr->lr_prev = NULL; - if ( ber_flush( lc->lconn_sb, ber, 0 ) != 0 ) { -#ifdef notyet - if ( errno == EWOULDBLOCK ) { - /* need to continue write later */ - lr->lr_status = LDAP_REQST_WRITING; - ldap_mark_select_write( ld, lc->lconn_sb ); - } else { -#else /* notyet */ - ld->ld_errno = LDAP_SERVER_DOWN; - ldap_free_request( ld, lr ); - ldap_free_connection( ld, lc, 0, 0 ); - return( -1 ); -#endif /* notyet */ -#ifdef notyet - } -#endif /* notyet */ - } else { - if ( parentreq == NULL ) { - ber->ber_end = ber->ber_ptr; - ber->ber_ptr = ber->ber_buf; - } - - /* sent -- waiting for a response */ - ldap_mark_select_read( ld, lc->lconn_sb ); + ld->ld_errno = LDAP_SUCCESS; + if ( ldap_int_flush_request( ld, lr ) == -1 ) { + msgid = -1; } - ld->ld_errno = LDAP_SUCCESS; return( msgid ); } @@ -236,9 +304,13 @@ ldap_new_connection( LDAP *ld, LDAPURLDesc *srvlist, int use_ldsb, { LDAPConn *lc; LDAPURLDesc *srv; - Sockbuf *sb; + Sockbuf *sb = NULL; +#ifdef NEW_LOGGING + LDAP_LOG ( OPERATION, ENTRY, "ldap_new_connection\n", 0, 0, 0 ); +#else Debug( LDAP_DEBUG_TRACE, "ldap_new_connection\n", 0, 0, 0 ); +#endif /* * make a new LDAP server connection * XXX open connection synchronously for now @@ -256,9 +328,7 @@ ldap_new_connection( LDAP *ld, LDAPURLDesc *srvlist, int use_ldsb, if ( connect ) { for ( srv = srvlist; srv != NULL; srv = srv->lud_next ) { - if ( open_ldap_connection( ld, lc->lconn_sb, - srv, &lc->lconn_krbinstance, 0 ) != -1 ) - { + if ( ldap_int_open_connection( ld, lc, srv, 0 ) != -1 ) { break; } } @@ -292,7 +362,7 @@ ldap_new_connection( LDAP *ld, LDAPURLDesc *srvlist, int use_ldsb, */ lc->lconn_rebind_inprogress = 1; /* V3 rebind function */ - if ( ld->ld_rebindproc != NULL) { + if ( ld->ld_rebind_proc != NULL) { LDAPURLDesc *srvfunc; if( ( srvfunc = ldap_url_dup( srvlist)) == NULL) { ld->ld_errno = LDAP_NO_MEMORY; @@ -302,8 +372,24 @@ ldap_new_connection( LDAP *ld, LDAPURLDesc *srvlist, int use_ldsb, ++lc->lconn_refcnt; /* avoid premature free */ ld->ld_defconn = lc; - Debug( LDAP_DEBUG_TRACE, "Call application rebindproc\n", 0, 0, 0); - err = (*ld->ld_rebindproc)( ld, bind->ri_url, bind->ri_request, bind->ri_msgid); +#ifdef NEW_LOGGING + LDAP_LOG ( OPERATION, DETAIL1, + "ldap_new_connection: Call application rebind_proc\n", + 0, 0, 0 ); +#else + Debug( LDAP_DEBUG_TRACE, "Call application rebind_proc\n", 0, 0, 0); +#endif +#ifdef LDAP_R_COMPILE + ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex ); + ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex ); +#endif + err = (*ld->ld_rebind_proc)( ld, + bind->ri_url, bind->ri_request, bind->ri_msgid, + ld->ld_rebind_params ); +#ifdef LDAP_R_COMPILE + ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex ); + ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex ); +#endif ld->ld_defconn = savedefconn; --lc->lconn_refcnt; @@ -320,10 +406,24 @@ ldap_new_connection( LDAP *ld, LDAPURLDesc *srvlist, int use_ldsb, ++lc->lconn_refcnt; /* avoid premature free */ ld->ld_defconn = lc; +#ifdef NEW_LOGGING + LDAP_LOG ( OPERATION, DETAIL1, + "ldap_new_connection: anonymous rebind via ldap_bind_s\n", + 0, 0, 0 ); +#else Debug( LDAP_DEBUG_TRACE, "anonymous rebind via ldap_bind_s\n", 0, 0, 0); +#endif +#ifdef LDAP_R_COMPILE + ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex ); + ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex ); +#endif if ( ldap_bind_s( ld, "", "", LDAP_AUTH_SIMPLE ) != LDAP_SUCCESS ) { err = -1; } +#ifdef LDAP_R_COMPILE + ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex ); + ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex ); +#endif ld->ld_defconn = savedefconn; --lc->lconn_refcnt; @@ -353,10 +453,11 @@ find_connection( LDAP *ld, LDAPURLDesc *srv, int any ) for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) { for ( ls = srv; ls != NULL; ls = ls->lud_next ) { if ( lc->lconn_server->lud_host != NULL && - ls->lud_host != NULL && strcasecmp( - ls->lud_host, lc->lconn_server->lud_host ) == 0 + *lc->lconn_server->lud_host != '\0' && + ls->lud_host != NULL && *ls->lud_host != '\0' && + strcasecmp( ls->lud_host, lc->lconn_server->lud_host ) == 0 && ls->lud_port == lc->lconn_server->lud_port ) { - return( lc ); + return lc; } if ( !any ) { break; @@ -364,7 +465,7 @@ find_connection( LDAP *ld, LDAPURLDesc *srv, int any ) } } - return( NULL ); + return NULL; } @@ -382,7 +483,11 @@ ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind ) { LDAPConn *tmplc, *prevlc; +#ifdef NEW_LOGGING + LDAP_LOG ( OPERATION, ENTRY, "ldap_free_connection\n", 0, 0, 0 ); +#else Debug( LDAP_DEBUG_TRACE, "ldap_free_connection\n", 0, 0, 0 ); +#endif if ( force || --lc->lconn_refcnt <= 0 ) { if ( lc->lconn_status == LDAP_CONNST_CONNECTED ) { @@ -396,6 +501,8 @@ ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind ) ber_free( lc->lconn_ber, 1 ); } + ldap_int_sasl_close( ld, lc ); + prevlc = NULL; for ( tmplc = ld->ld_conns; tmplc != NULL; tmplc = tmplc->lconn_next ) { @@ -410,9 +517,11 @@ ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind ) prevlc = tmplc; } ldap_free_urllist( lc->lconn_server ); +#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND if ( lc->lconn_krbinstance != NULL ) { LDAP_FREE( lc->lconn_krbinstance ); } +#endif if ( lc->lconn_sb != ld->ld_sb ) { ber_sockbuf_free( lc->lconn_sb ); } @@ -424,12 +533,22 @@ ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind ) LDAP_FREE( lc->lconn_rebind_queue); } LDAP_FREE( lc ); +#ifdef NEW_LOGGING + LDAP_LOG ( OPERATION, RESULTS, + "ldap_free_connection: actually freed\n", 0, 0, 0 ); +#else Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: actually freed\n", 0, 0, 0 ); +#endif } else { lc->lconn_lastused = time( NULL ); +#ifdef NEW_LOGGING + LDAP_LOG ( OPERATION, RESULTS, "ldap_free_connection: refcnt %d\n", + lc->lconn_refcnt, 0, 0 ); +#else Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: refcnt %d\n", lc->lconn_refcnt, 0, 0 ); +#endif } } @@ -485,6 +604,9 @@ ldap_dump_requests_and_responses( LDAP *ld ) LDAPRequest *lr; LDAPMessage *lm, *l; +#ifdef LDAP_R_COMPILE + ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex ); +#endif fprintf( stderr, "** Outstanding Requests:\n" ); if (( lr = ld->ld_requests ) == NULL ) { fprintf( stderr, " Empty\n" ); @@ -500,7 +622,9 @@ ldap_dump_requests_and_responses( LDAP *ld ) fprintf( stderr, " outstanding referrals %d, parent count %d\n", lr->lr_outrefcnt, lr->lr_parentcnt ); } - +#ifdef LDAP_R_COMPILE + ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex ); +#endif fprintf( stderr, "** Response Queue:\n" ); if (( lm = ld->ld_responses ) == NULL ) { fprintf( stderr, " Empty\n" ); @@ -521,25 +645,9 @@ ldap_dump_requests_and_responses( LDAP *ld ) } #endif /* LDAP_DEBUG */ - void -ldap_free_request( LDAP *ld, LDAPRequest *lr ) +ldap_free_request_int( LDAP *ld, LDAPRequest *lr ) { - LDAPRequest *tmplr, *nextlr; - - Debug( LDAP_DEBUG_TRACE, "ldap_free_request (origid %d, msgid %d)\n", - lr->lr_origid, lr->lr_msgid, 0 ); - - if ( lr->lr_parent != NULL ) { - --lr->lr_parent->lr_outrefcnt; - } else { - /* free all referrals (child requests) */ - for ( tmplr = lr->lr_refnext; tmplr != NULL; tmplr = nextlr ) { - nextlr = tmplr->lr_refnext; - ldap_free_request( ld, tmplr ); - } - } - if ( lr->lr_prev == NULL ) { ld->ld_requests = lr->lr_next; } else { @@ -565,6 +673,33 @@ ldap_free_request( LDAP *ld, LDAPRequest *lr ) LDAP_FREE( lr ); } +void +ldap_free_request( LDAP *ld, LDAPRequest *lr ) +{ + LDAPRequest **ttmplr; + +#ifdef NEW_LOGGING + LDAP_LOG ( OPERATION, ARGS, "ldap_free_request (origid %d, msgid %d)\n", + lr->lr_origid, lr->lr_msgid, 0 ); +#else + Debug( LDAP_DEBUG_TRACE, "ldap_free_request (origid %d, msgid %d)\n", + lr->lr_origid, lr->lr_msgid, 0 ); +#endif + + /* free all referrals (child requests) */ + while ( lr->lr_child ) + ldap_free_request( ld, lr->lr_child ); + + if ( lr->lr_parent != NULL ) { + --lr->lr_parent->lr_outrefcnt; + for ( ttmplr = &lr->lr_parent->lr_child; *ttmplr && *ttmplr != lr; ttmplr = &(*ttmplr)->lr_refnext ); + if ( *ttmplr == lr ) + *ttmplr = lr->lr_refnext; + } + ldap_free_request_int( ld, lr ); +} + + /* * Chase v3 referrals * @@ -573,13 +708,14 @@ ldap_free_request( LDAP *ld, LDAPRequest *lr ) * (IN) lr = LDAP Request structure * (IN) refs = array of pointers to referral strings that we will chase * The array will be free'd by this function when no longer needed + * (IN) sref != 0 if following search reference * (OUT) errstrp = Place to return a string of referrals which could not be followed * (OUT) hadrefp = 1 if sucessfully followed referral * * Return value - number of referrals followed */ int -ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, char **errstrp, int *hadrefp ) +ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, int sref, char **errstrp, int *hadrefp ) { char *unfollowed; int unfollowedcnt = 0; @@ -588,13 +724,18 @@ ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, char **errstrp, BerElement *ber; char **refarray = NULL; LDAPConn *lc; - int rc, count, i, j; + int rc, count, i, j, id; + int parent_was_reference; LDAPreqinfo rinfo; ld->ld_errno = LDAP_SUCCESS; /* optimistic */ *hadrefp = 0; +#ifdef NEW_LOGGING + LDAP_LOG ( OPERATION, ENTRY, "ldap_chase_v3referrals\n", 0, 0, 0 ); +#else Debug( LDAP_DEBUG_TRACE, "ldap_chase_v3referrals\n", 0, 0, 0 ); +#endif unfollowed = NULL; rc = count = 0; @@ -607,16 +748,30 @@ ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, char **errstrp, /* Check for hop limit exceeded */ if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) { +#ifdef NEW_LOGGING + LDAP_LOG ( OPERATION, DETAIL1, + "ldap_chase_v3referrals: more than %d referral hops (dropping)\n", + ld->ld_refhoplimit, 0, 0 ); +#else Debug( LDAP_DEBUG_ANY, "more than %d referral hops (dropping)\n", ld->ld_refhoplimit, 0, 0 ); +#endif ld->ld_errno = LDAP_REFERRAL_LIMIT_EXCEEDED; rc = -1; goto done; } + /* check if parent request was a search reference */ + parent_was_reference = ( lr->lr_parent && + lr->lr_parent->lr_res_msgtype == LDAP_RES_SEARCH_REFERENCE ) ? + 1 : 0; + /* find original request */ - for ( origreq = lr; origreq->lr_parent != NULL; origreq = origreq->lr_parent ) { - ; + for ( origreq = lr; + origreq->lr_parent != NULL; + origreq = origreq->lr_parent ) + { + /* empty */ ; } refarray = refs; @@ -624,12 +779,19 @@ ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, char **errstrp, /* parse out & follow referrals */ for( i=0; refarray[i] != NULL; i++) { /* Parse the referral URL */ - if (( rc = ldap_url_parse( refarray[i], &srv)) != LDAP_SUCCESS) { + if (( rc = ldap_url_parse_ext( refarray[i], &srv)) != LDAP_SUCCESS) { ld->ld_errno = rc; rc = -1; goto done; } + if( srv->lud_crit_exts ) { + /* we do not support any extensions */ + ld->ld_errno = LDAP_NOT_SUPPORTED; + rc = -1; + goto done; + } + /* treat ldap://hostpart and ldap://hostpart/ the same */ if ( srv->lud_dn && srv->lud_dn[0] == '\0' ) { LDAP_FREE( srv->lud_dn ); @@ -646,26 +808,41 @@ ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, char **errstrp, * if two search references come in one behind the other * for the same server with different contexts. */ - Debug( LDAP_DEBUG_TRACE, "ldap_chase_v3referrals: queue referral \"%s\"\n", +#ifdef NEW_LOGGING + LDAP_LOG ( OPERATION, DETAIL1, + "ldap_chase_v3referrals: queue referral \"%s\"\n", + refarray[i], 0, 0 ); +#else + Debug( LDAP_DEBUG_TRACE, + "ldap_chase_v3referrals: queue referral \"%s\"\n", refarray[i], 0, 0); +#endif if( lc->lconn_rebind_queue == NULL ) { /* Create a referral list */ - if( (lc->lconn_rebind_queue = (char ***)LDAP_MALLOC( sizeof(void *) * 2)) == NULL) { + lc->lconn_rebind_queue = + (char ***) LDAP_MALLOC( sizeof(void *) * 2); + + if( lc->lconn_rebind_queue == NULL) { ld->ld_errno = LDAP_NO_MEMORY; rc = -1; goto done; } + lc->lconn_rebind_queue[0] = refarray; lc->lconn_rebind_queue[1] = NULL; refarray = NULL; + } else { /* Count how many referral arrays we already have */ for( j = 0; lc->lconn_rebind_queue[j] != NULL; j++) { - ; + /* empty */; } + /* Add the new referral to the list */ - if( (lc->lconn_rebind_queue = (char ***)LDAP_REALLOC( - lc->lconn_rebind_queue, sizeof(void *) * (j + 2))) == NULL) { + lc->lconn_rebind_queue = (char ***) LDAP_REALLOC( + lc->lconn_rebind_queue, sizeof(void *) * (j + 2)); + + if( lc->lconn_rebind_queue == NULL ) { ld->ld_errno = LDAP_NO_MEMORY; rc = -1; goto done; @@ -674,6 +851,7 @@ ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, char **errstrp, lc->lconn_rebind_queue[j+1] = NULL; refarray = NULL; } + /* We have queued the referral/reference, now just return */ rc = 0; *hadrefp = 1; @@ -685,24 +863,54 @@ ldap_chase_v3referrals( LDAP *ld, LDAPRequest *lr, char **refs, char **errstrp, * Note: In the future we also need to replace the filter if one * was provided with the search reference */ - if (( ber = re_encode_request( ld, origreq->lr_ber, - ++ld->ld_msgid, &srv->lud_dn, &rinfo.ri_request )) == NULL ) { + + /* For references we don't want old dn if new dn empty */ + if ( sref && srv->lud_dn == NULL ) { + srv->lud_dn = LDAP_STRDUP( "" ); + } + + LDAP_NEXT_MSGID( ld, id ); + ber = re_encode_request( ld, origreq->lr_ber, id, + sref, srv, &rinfo.ri_request ); + + if( ber == NULL ) { ld->ld_errno = LDAP_ENCODING_ERROR; rc = -1; goto done; } - Debug( LDAP_DEBUG_TRACE, "ldap_chase_v3referral: msgid %d, url \"%s\"\n", +#ifdef NEW_LOGGING + LDAP_LOG ( OPERATION, DETAIL1, + "ldap_chase_v3referrals: msgid %d, url \"%s\"\n", + lr->lr_msgid, refarray[i], 0 ); +#else + Debug( LDAP_DEBUG_TRACE, + "ldap_chase_v3referral: msgid %d, url \"%s\"\n", lr->lr_msgid, refarray[i], 0); +#endif /* Send the new request to the server - may require a bind */ rinfo.ri_msgid = origreq->lr_origid; rinfo.ri_url = refarray[i]; - if ( (rc = ldap_send_server_request( ld, ber, ld->ld_msgid, - origreq, srv, NULL, &rinfo )) < 0 ) { +#ifdef LDAP_R_COMPILE + ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex ); +#endif + rc = ldap_send_server_request( ld, ber, id, + (sref && !parent_was_reference) ? origreq : lr, + srv, NULL, &rinfo ); +#ifdef LDAP_R_COMPILE + ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex ); +#endif + if ( rc < 0 ) { /* Failure, try next referral in the list */ +#ifdef NEW_LOGGING + LDAP_LOG ( OPERATION, ERR, + "ldap_chase_v3referrals: Unable to chase referral \"%s\" (%s)\n", + refarray[i], ldap_err2string( ld->ld_errno ), 0 ); +#else Debug( LDAP_DEBUG_ANY, "Unable to chase referral \"%s\" (%s)\n", refarray[i], ldap_err2string( ld->ld_errno ), 0); +#endif unfollowedcnt += ldap_append_referral( ld, &unfollowed, refarray[i]); ldap_free_urllist(srv); srv = NULL; @@ -766,16 +974,25 @@ done: * XXX merging of errors in this routine needs to be improved */ int -ldap_chase_referrals( LDAP *ld, LDAPRequest *lr, char **errstrp, int *hadrefp ) +ldap_chase_referrals( LDAP *ld, + LDAPRequest *lr, + char **errstrp, + int sref, + int *hadrefp ) { - int rc, count, len, newdn; - char *p, *ports, *ref, *tmpref, *refdn, *unfollowed; + int rc, count, id; + unsigned len; + char *p, *ref, *unfollowed; LDAPRequest *origreq; LDAPURLDesc *srv; BerElement *ber; LDAPreqinfo rinfo; +#ifdef NEW_LOGGING + LDAP_LOG ( OPERATION, ENTRY, "ldap_chase_referrals\n", 0, 0, 0 ); +#else Debug( LDAP_DEBUG_TRACE, "ldap_chase_referrals\n", 0, 0, 0 ); +#endif ld->ld_errno = LDAP_SUCCESS; /* optimistic */ *hadrefp = 0; @@ -786,8 +1003,7 @@ ldap_chase_referrals( LDAP *ld, LDAPRequest *lr, char **errstrp, int *hadrefp ) len = strlen( *errstrp ); for ( p = *errstrp; len >= LDAP_REF_STR_LEN; ++p, --len ) { - if (( *p == 'R' || *p == 'r' ) && strncasecmp( p, - LDAP_REF_STR, LDAP_REF_STR_LEN ) == 0 ) { + if ( strncasecmp( p, LDAP_REF_STR, LDAP_REF_STR_LEN ) == 0 ) { *p = '\0'; p += LDAP_REF_STR_LEN; break; @@ -799,9 +1015,15 @@ ldap_chase_referrals( LDAP *ld, LDAPRequest *lr, char **errstrp, int *hadrefp ) } if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) { +#ifdef NEW_LOGGING + LDAP_LOG ( OPERATION, ENTRY, + "ldap_chase_referrals: more than %d referral hops (dropping)\n", + ld->ld_refhoplimit, 0, 0 ); +#else Debug( LDAP_DEBUG_ANY, "more than %d referral hops (dropping)\n", ld->ld_refhoplimit, 0, 0 ); +#endif /* XXX report as error in ld->ld_errno? */ return( 0 ); } @@ -809,7 +1031,7 @@ ldap_chase_referrals( LDAP *ld, LDAPRequest *lr, char **errstrp, int *hadrefp ) /* find original request */ for ( origreq = lr; origreq->lr_parent != NULL; origreq = origreq->lr_parent ) { - ; + /* empty */; } unfollowed = NULL; @@ -817,95 +1039,83 @@ ldap_chase_referrals( LDAP *ld, LDAPRequest *lr, char **errstrp, int *hadrefp ) /* parse out & follow referrals */ for ( ref = p; rc == 0 && ref != NULL; ref = p ) { - if (( p = strchr( ref, '\n' )) != NULL ) { *p++ = '\0'; } else { p = NULL; } - ldap_pvt_hex_unescape( ref ); - len = strlen( ref ); - - /* FIXME: we should use the URL Parser */ + rc = ldap_url_parse_ext( ref, &srv ); - if ( len > LDAP_LDAP_REF_STR_LEN && strncasecmp( ref, - LDAP_LDAP_REF_STR, LDAP_LDAP_REF_STR_LEN ) == 0 ) { - Debug( LDAP_DEBUG_TRACE, - "chasing LDAP referral: <%s>\n", ref, 0, 0 ); - tmpref = ref + LDAP_LDAP_REF_STR_LEN; - } else { + if ( rc != LDAP_URL_SUCCESS ) { +#ifdef NEW_LOGGING + LDAP_LOG ( OPERATION, DETAIL1, + "ldap_chase_referrals: ignoring unknown referral <%s>\n", + ref, 0, 0 ); +#else Debug( LDAP_DEBUG_TRACE, "ignoring unknown referral <%s>\n", ref, 0, 0 ); +#endif rc = ldap_append_referral( ld, &unfollowed, ref ); *hadrefp = 1; continue; } - /* copy the complete referral for rebind process */ - rinfo.ri_url = LDAP_STRDUP( ref ); + if( srv->lud_dn != NULL && srv->lud_dn == '\0' ) { + LDAP_FREE( srv->lud_dn ); + srv->lud_dn = NULL; + } + +#ifdef NEW_LOGGING + LDAP_LOG ( OPERATION, DETAIL1, + "ldap_chase_referrals: chasing LDAP referral <%s>\n", ref, 0, 0 ); +#else + Debug( LDAP_DEBUG_TRACE, + "chasing LDAP referral: <%s>\n", ref, 0, 0 ); +#endif *hadrefp = 1; - if (( refdn = strchr( tmpref, '/' )) != NULL ) { - *refdn++ = '\0'; - newdn = refdn[0] != '?' && refdn[0] != '\0'; - if( !newdn ) refdn = NULL; - } else { - newdn = 0; - } + LDAP_NEXT_MSGID( ld, id ); + ber = re_encode_request( ld, origreq->lr_ber, + id, sref, srv, &rinfo.ri_request ); - if (( ber = re_encode_request( ld, origreq->lr_ber, - ++ld->ld_msgid, &refdn, &rinfo.ri_request )) == NULL ) { - return( -1 ); + if( ber == NULL ) { + return -1 ; } - if (( srv = (LDAPURLDesc *)LDAP_CALLOC( 1, - sizeof( LDAPURLDesc ))) == NULL ) { - ber_free( ber, 1 ); - ld->ld_errno = LDAP_NO_MEMORY; - return( -1 ); - } + /* copy the complete referral for rebind process */ + rinfo.ri_url = LDAP_STRDUP( ref ); - if (( srv->lud_scheme = LDAP_STRDUP("ldap")) == NULL ) { - LDAP_FREE( (char *)srv ); - ber_free( ber, 1 ); - ld->ld_errno = LDAP_NO_MEMORY; - return( -1 ); - } + rinfo.ri_msgid = origreq->lr_origid; - if (( srv->lud_host = LDAP_STRDUP( tmpref )) == NULL ) { - LDAP_FREE( (char *)srv ); - ber_free( ber, 1 ); - ld->ld_errno = LDAP_NO_MEMORY; - return( -1 ); - } +#ifdef LDAP_R_COMPILE + ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex ); +#endif + rc = ldap_send_server_request( ld, ber, id, + lr, srv, NULL, &rinfo ); +#ifdef LDAP_R_COMPILE + ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex ); +#endif - if (( ports = strchr( srv->lud_host, ':' )) != NULL ) { - *ports++ = '\0'; - srv->lud_port = atoi( ports ); - } else { - srv->lud_port = (LDAP_INT_GLOBAL_OPT())->ldo_defport; - } + LDAP_FREE( rinfo.ri_url ); - rinfo.ri_msgid = origreq->lr_origid; - if ( srv != NULL && ldap_send_server_request( ld, ber, ld->ld_msgid, - lr, srv, NULL, &rinfo ) >= 0 ) { + if( rc >= 0 ) { ++count; } else { +#ifdef NEW_LOGGING + LDAP_LOG ( OPERATION, ERR, + "ldap_chase_referrals: Unable to chase referral <%s>\n", + ldap_err2string( ld->ld_errno), 0, 0 ); +#else Debug( LDAP_DEBUG_ANY, "Unable to chase referral (%s)\n", ldap_err2string( ld->ld_errno ), 0, 0 ); +#endif rc = ldap_append_referral( ld, &unfollowed, ref ); } - LDAP_FREE( rinfo.ri_url); - if (srv != NULL) - ldap_free_urllist(srv); - - if ( !newdn && refdn != NULL ) { - LDAP_FREE( refdn ); - } + ldap_free_urllist(srv); } LDAP_FREE( *errstrp ); @@ -948,21 +1158,37 @@ ldap_append_referral( LDAP *ld, char **referralsp, char *s ) static BerElement * -re_encode_request( LDAP *ld, BerElement *origber, ber_int_t msgid, char **dnp, int *type ) +re_encode_request( LDAP *ld, + BerElement *origber, + ber_int_t msgid, + int sref, + LDAPURLDesc *srv, + int *type ) { -/* - * XXX this routine knows way too much about how the lber library works! - */ + /* + * XXX this routine knows way too much about how the lber library works! + */ ber_int_t along; ber_tag_t tag; + ber_tag_t rtag; ber_int_t ver; + ber_int_t scope; int rc; BerElement tmpber, *ber; char *orig_dn; - + char *dn; + +#ifdef NEW_LOGGING + LDAP_LOG ( OPERATION, ARGS, + "re_encode_request: new msgid %ld, new dn <%s>\n", + (long) msgid, + ( srv == NULL || srv->lud_dn == NULL ) ? "NONE" : srv->lud_dn, 0 ); +#else Debug( LDAP_DEBUG_TRACE, "re_encode_request: new msgid %ld, new dn <%s>\n", - (long) msgid, ( *dnp == NULL ) ? "NONE" : *dnp, 0 ); + (long) msgid, + ( srv == NULL || srv->lud_dn == NULL) ? "NONE" : srv->lud_dn, 0 ); +#endif tmpber = *origber; @@ -972,9 +1198,9 @@ re_encode_request( LDAP *ld, BerElement *origber, ber_int_t msgid, char **dnp, i * tagged with the operation code. For delete, the provided DN * is not wrapped by a sequence. */ - rc = ber_scanf( &tmpber, "{it", /*}*/ &along, &tag ); + rtag = ber_scanf( &tmpber, "{it", /*}*/ &along, &tag ); - if ( rc == LBER_ERROR ) { + if ( rtag == LBER_ERROR ) { ld->ld_errno = LDAP_DECODING_ERROR; return( NULL ); } @@ -982,73 +1208,115 @@ re_encode_request( LDAP *ld, BerElement *origber, ber_int_t msgid, char **dnp, i assert( tag != 0); if ( tag == LDAP_REQ_BIND ) { /* bind requests have a version number before the DN & other stuff */ - rc = ber_scanf( &tmpber, "{ia" /*}*/, &ver, &orig_dn ); + rtag = ber_scanf( &tmpber, "{ia" /*}*/, &ver, &orig_dn ); } else if ( tag == LDAP_REQ_DELETE ) { /* delete requests don't have a DN wrapping sequence */ - rc = ber_scanf( &tmpber, "a", &orig_dn ); + rtag = ber_scanf( &tmpber, "a", &orig_dn ); + + } else if ( tag == LDAP_REQ_SEARCH ) { + /* search requests need to be re-scope-ed */ + rtag = ber_scanf( &tmpber, "{ae" /*"}"*/, &orig_dn, &scope ); + + if( srv->lud_scope != LDAP_SCOPE_DEFAULT ) { + /* use the scope provided in reference */ + scope = srv->lud_scope; + + } else if ( sref ) { + /* use scope implied by previous operation + * base -> base + * one -> base + * subtree -> subtree + * subordinate -> subtree + */ + switch( scope ) { + default: + case LDAP_SCOPE_BASE: + case LDAP_SCOPE_ONELEVEL: + scope = LDAP_SCOPE_BASE; + break; + case LDAP_SCOPE_SUBTREE: +#ifdef LDAP_FEATURE_SUBORDINATE_SCOPE + case LDAP_SCOPE_SUBORDINATE: +#endif + scope = LDAP_SCOPE_SUBTREE; + break; + } + } } else { - rc = ber_scanf( &tmpber, "{a" /*}*/, &orig_dn ); + rtag = ber_scanf( &tmpber, "{a" /*}*/, &orig_dn ); } - if( rc == LBER_ERROR ) { + if( rtag == LBER_ERROR ) { ld->ld_errno = LDAP_DECODING_ERROR; return NULL; } - if ( *dnp == NULL ) { - *dnp = orig_dn; - } else { - LDAP_FREE( orig_dn ); + if (( ber = ldap_alloc_ber_with_options( ld )) == NULL ) { + return NULL; } - if (( ber = ldap_alloc_ber_with_options( ld )) == NULL ) { - return( NULL ); + if ( srv->lud_dn == NULL ) { + dn = orig_dn; + } else { + dn = srv->lud_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, dn ); } else if ( tag == LDAP_REQ_DELETE ) { - rc = ber_printf( ber, "{its}", msgid, tag, *dnp ); + rc = ber_printf( ber, "{itsN}", msgid, tag, dn ); + } else if ( tag == LDAP_REQ_SEARCH ) { + rc = ber_printf( ber, "{it{se" /*}}*/, msgid, tag, dn, scope ); } else { - rc = ber_printf( ber, "{it{s" /*}}*/, msgid, tag, *dnp ); + rc = ber_printf( ber, "{it{s" /*}}*/, msgid, tag, dn ); } + LDAP_FREE( orig_dn ); + if ( rc == -1 ) { ld->ld_errno = LDAP_ENCODING_ERROR; ber_free( ber, 1 ); - return( NULL ); + return NULL; } if ( tag != LDAP_REQ_DELETE && ( 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, /*{{*/ "N}N}" ) == -1 ) ) { ld->ld_errno = LDAP_ENCODING_ERROR; ber_free( ber, 1 ); - return( NULL ); + return NULL; } #ifdef LDAP_DEBUG if ( ldap_debug & LDAP_DEBUG_PACKETS ) { +#ifdef NEW_LOGGING + LDAP_LOG ( OPERATION, DETAIL1, + "re_encode_request: new request is:\n", 0, 0, 0 ); +#else Debug( LDAP_DEBUG_ANY, "re_encode_request new request is:\n", 0, 0, 0 ); +#endif ber_log_dump( LDAP_DEBUG_BER, ldap_debug, ber, 0 ); } #endif /* LDAP_DEBUG */ *type = tag; /* return request type */ - return( ber ); + return ber; } LDAPRequest * ldap_find_request_by_msgid( LDAP *ld, ber_int_t msgid ) { - LDAPRequest *lr; + LDAPRequest *lr; +#ifdef LDAP_R_COMPILE + ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex ); +#endif for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) { if( lr->lr_status == LDAP_REQST_COMPLETED ) { continue; /* Skip completed requests */ @@ -1057,6 +1325,9 @@ ldap_find_request_by_msgid( LDAP *ld, ber_int_t msgid ) break; } } +#ifdef LDAP_R_COMPILE + ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex ); +#endif return( lr ); }