1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * Copyright 2001-2003 Sun Microsystems, Inc. All rights reserved. 3*0Sstevel@tonic-gate * Use is subject to license terms. 4*0Sstevel@tonic-gate */ 5*0Sstevel@tonic-gate 6*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 7*0Sstevel@tonic-gate 8*0Sstevel@tonic-gate /* 9*0Sstevel@tonic-gate * The contents of this file are subject to the Netscape Public 10*0Sstevel@tonic-gate * License Version 1.1 (the "License"); you may not use this file 11*0Sstevel@tonic-gate * except in compliance with the License. You may obtain a copy of 12*0Sstevel@tonic-gate * the License at http://www.mozilla.org/NPL/ 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * Software distributed under the License is distributed on an "AS 15*0Sstevel@tonic-gate * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or 16*0Sstevel@tonic-gate * implied. See the License for the specific language governing 17*0Sstevel@tonic-gate * rights and limitations under the License. 18*0Sstevel@tonic-gate * 19*0Sstevel@tonic-gate * The Original Code is Mozilla Communicator client code, released 20*0Sstevel@tonic-gate * March 31, 1998. 21*0Sstevel@tonic-gate * 22*0Sstevel@tonic-gate * The Initial Developer of the Original Code is Netscape 23*0Sstevel@tonic-gate * Communications Corporation. Portions created by Netscape are 24*0Sstevel@tonic-gate * Copyright (C) 1998-1999 Netscape Communications Corporation. All 25*0Sstevel@tonic-gate * Rights Reserved. 26*0Sstevel@tonic-gate * 27*0Sstevel@tonic-gate * Contributor(s): 28*0Sstevel@tonic-gate */ 29*0Sstevel@tonic-gate /* 30*0Sstevel@tonic-gate * Copyright (c) 1995 Regents of the University of Michigan. 31*0Sstevel@tonic-gate * All rights reserved. 32*0Sstevel@tonic-gate */ 33*0Sstevel@tonic-gate /* 34*0Sstevel@tonic-gate * request.c - sending of ldap requests; handling of referrals 35*0Sstevel@tonic-gate */ 36*0Sstevel@tonic-gate 37*0Sstevel@tonic-gate #if 0 38*0Sstevel@tonic-gate #ifndef lint 39*0Sstevel@tonic-gate static char copyright[] = "@(#) Copyright (c) 1995 Regents of the University of Michigan.\nAll rights reserved.\n"; 40*0Sstevel@tonic-gate #endif 41*0Sstevel@tonic-gate #endif 42*0Sstevel@tonic-gate 43*0Sstevel@tonic-gate #include "ldap-int.h" 44*0Sstevel@tonic-gate 45*0Sstevel@tonic-gate static LDAPConn *find_connection( LDAP *ld, LDAPServer *srv, int any ); 46*0Sstevel@tonic-gate static void use_connection( LDAP *ld, LDAPConn *lc ); 47*0Sstevel@tonic-gate static void free_servers( LDAPServer *srvlist ); 48*0Sstevel@tonic-gate static int chase_one_referral( LDAP *ld, LDAPRequest *lr, LDAPRequest *origreq, 49*0Sstevel@tonic-gate char *refurl, char *desc, int *unknownp ); 50*0Sstevel@tonic-gate static int re_encode_request( LDAP *ld, BerElement *origber, 51*0Sstevel@tonic-gate int msgid, LDAPURLDesc *ludp, BerElement **berp ); 52*0Sstevel@tonic-gate 53*0Sstevel@tonic-gate #ifdef LDAP_DNS 54*0Sstevel@tonic-gate static LDAPServer *dn2servers( LDAP *ld, char *dn ); 55*0Sstevel@tonic-gate #endif /* LDAP_DNS */ 56*0Sstevel@tonic-gate 57*0Sstevel@tonic-gate 58*0Sstevel@tonic-gate /* returns an LDAP error code and also sets error inside LDAP * */ 59*0Sstevel@tonic-gate int 60*0Sstevel@tonic-gate nsldapi_alloc_ber_with_options( LDAP *ld, BerElement **berp ) 61*0Sstevel@tonic-gate { 62*0Sstevel@tonic-gate int err; 63*0Sstevel@tonic-gate 64*0Sstevel@tonic-gate LDAP_MUTEX_LOCK( ld, LDAP_OPTION_LOCK ); 65*0Sstevel@tonic-gate if (( *berp = ber_alloc_t( ld->ld_lberoptions )) == NULLBER ) { 66*0Sstevel@tonic-gate err = LDAP_NO_MEMORY; 67*0Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, err, NULL, NULL ); 68*0Sstevel@tonic-gate } else { 69*0Sstevel@tonic-gate err = LDAP_SUCCESS; 70*0Sstevel@tonic-gate #ifdef STR_TRANSLATION 71*0Sstevel@tonic-gate nsldapi_set_ber_options( ld, *berp ); 72*0Sstevel@tonic-gate #endif /* STR_TRANSLATION */ 73*0Sstevel@tonic-gate } 74*0Sstevel@tonic-gate LDAP_MUTEX_UNLOCK( ld, LDAP_OPTION_LOCK ); 75*0Sstevel@tonic-gate 76*0Sstevel@tonic-gate return( err ); 77*0Sstevel@tonic-gate } 78*0Sstevel@tonic-gate 79*0Sstevel@tonic-gate 80*0Sstevel@tonic-gate void 81*0Sstevel@tonic-gate nsldapi_set_ber_options( LDAP *ld, BerElement *ber ) 82*0Sstevel@tonic-gate { 83*0Sstevel@tonic-gate ber->ber_options = ld->ld_lberoptions; 84*0Sstevel@tonic-gate #ifdef STR_TRANSLATION 85*0Sstevel@tonic-gate if (( ld->ld_lberoptions & LBER_OPT_TRANSLATE_STRINGS ) != 0 ) { 86*0Sstevel@tonic-gate ber_set_string_translators( ber, 87*0Sstevel@tonic-gate ld->ld_lber_encode_translate_proc, 88*0Sstevel@tonic-gate ld->ld_lber_decode_translate_proc ); 89*0Sstevel@tonic-gate } 90*0Sstevel@tonic-gate #endif /* STR_TRANSLATION */ 91*0Sstevel@tonic-gate } 92*0Sstevel@tonic-gate 93*0Sstevel@tonic-gate 94*0Sstevel@tonic-gate /* returns the message id of the request or -1 if an error occurs */ 95*0Sstevel@tonic-gate int 96*0Sstevel@tonic-gate nsldapi_send_initial_request( LDAP *ld, int msgid, unsigned long msgtype, 97*0Sstevel@tonic-gate char *dn, BerElement *ber ) 98*0Sstevel@tonic-gate { 99*0Sstevel@tonic-gate LDAPServer *servers; 100*0Sstevel@tonic-gate 101*0Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "nsldapi_send_initial_request\n", 0,0,0 ); 102*0Sstevel@tonic-gate 103*0Sstevel@tonic-gate #ifdef LDAP_DNS 104*0Sstevel@tonic-gate LDAP_MUTEX_LOCK( ld, LDAP_OPTION_LOCK ); 105*0Sstevel@tonic-gate if (( ld->ld_options & LDAP_BITOPT_DNS ) != 0 && ldap_is_dns_dn( dn )) { 106*0Sstevel@tonic-gate if (( servers = dn2servers( ld, dn )) == NULL ) { 107*0Sstevel@tonic-gate ber_free( ber, 1 ); 108*0Sstevel@tonic-gate LDAP_MUTEX_UNLOCK( ld, LDAP_OPTION_LOCK ); 109*0Sstevel@tonic-gate return( -1 ); 110*0Sstevel@tonic-gate } 111*0Sstevel@tonic-gate 112*0Sstevel@tonic-gate #ifdef LDAP_DEBUG 113*0Sstevel@tonic-gate if ( ldap_debug & LDAP_DEBUG_TRACE ) { 114*0Sstevel@tonic-gate LDAPServer *srv; 115*0Sstevel@tonic-gate char msg[256]; 116*0Sstevel@tonic-gate 117*0Sstevel@tonic-gate for ( srv = servers; srv != NULL; 118*0Sstevel@tonic-gate srv = srv->lsrv_next ) { 119*0Sstevel@tonic-gate sprintf( msg, 120*0Sstevel@tonic-gate "LDAP server %s: dn %s, port %d\n", 121*0Sstevel@tonic-gate srv->lsrv_host, ( srv->lsrv_dn == NULL ) ? 122*0Sstevel@tonic-gate "(default)" : srv->lsrv_dn, 123*0Sstevel@tonic-gate srv->lsrv_port ); 124*0Sstevel@tonic-gate ber_err_print( msg ); 125*0Sstevel@tonic-gate } 126*0Sstevel@tonic-gate } 127*0Sstevel@tonic-gate #endif /* LDAP_DEBUG */ 128*0Sstevel@tonic-gate } else { 129*0Sstevel@tonic-gate #endif /* LDAP_DNS */ 130*0Sstevel@tonic-gate /* 131*0Sstevel@tonic-gate * use of DNS is turned off or this is an LDAP DN... 132*0Sstevel@tonic-gate * use our default connection 133*0Sstevel@tonic-gate */ 134*0Sstevel@tonic-gate servers = NULL; 135*0Sstevel@tonic-gate #ifdef LDAP_DNS 136*0Sstevel@tonic-gate } 137*0Sstevel@tonic-gate LDAP_MUTEX_UNLOCK( ld, LDAP_OPTION_LOCK ); 138*0Sstevel@tonic-gate #endif /* LDAP_DNS */ 139*0Sstevel@tonic-gate 140*0Sstevel@tonic-gate return( nsldapi_send_server_request( ld, ber, msgid, NULL, 141*0Sstevel@tonic-gate servers, NULL, ( msgtype == LDAP_REQ_BIND ) ? dn : NULL, 0 )); 142*0Sstevel@tonic-gate } 143*0Sstevel@tonic-gate 144*0Sstevel@tonic-gate 145*0Sstevel@tonic-gate /* returns the message id of the request or -1 if an error occurs */ 146*0Sstevel@tonic-gate int 147*0Sstevel@tonic-gate nsldapi_send_server_request( 148*0Sstevel@tonic-gate LDAP *ld, /* session handle */ 149*0Sstevel@tonic-gate BerElement *ber, /* message to send */ 150*0Sstevel@tonic-gate int msgid, /* ID of message to send */ 151*0Sstevel@tonic-gate LDAPRequest *parentreq, /* non-NULL for referred requests */ 152*0Sstevel@tonic-gate LDAPServer *srvlist, /* servers to connect to (NULL for default) */ 153*0Sstevel@tonic-gate LDAPConn *lc, /* connection to use (NULL for default) */ 154*0Sstevel@tonic-gate char *bindreqdn, /* non-NULL for bind requests */ 155*0Sstevel@tonic-gate int bind /* perform a bind after opening new conn.? */ 156*0Sstevel@tonic-gate ) 157*0Sstevel@tonic-gate { 158*0Sstevel@tonic-gate LDAPRequest *lr; 159*0Sstevel@tonic-gate int err; 160*0Sstevel@tonic-gate int incparent; /* did we bump parent's ref count? */ 161*0Sstevel@tonic-gate 162*0Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "nsldapi_send_server_request\n", 0, 0, 0 ); 163*0Sstevel@tonic-gate 164*0Sstevel@tonic-gate incparent = 0; 165*0Sstevel@tonic-gate LDAP_MUTEX_LOCK( ld, LDAP_CONN_LOCK ); 166*0Sstevel@tonic-gate if ( lc == NULL ) { 167*0Sstevel@tonic-gate if ( srvlist == NULL ) { 168*0Sstevel@tonic-gate if ( ld->ld_defconn == NULL ) { 169*0Sstevel@tonic-gate LDAP_MUTEX_LOCK( ld, LDAP_OPTION_LOCK ); 170*0Sstevel@tonic-gate if ( bindreqdn == NULL && ( ld->ld_options 171*0Sstevel@tonic-gate & LDAP_BITOPT_RECONNECT ) != 0 ) { 172*0Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, LDAP_SERVER_DOWN, 173*0Sstevel@tonic-gate NULL, NULL ); 174*0Sstevel@tonic-gate ber_free( ber, 1 ); 175*0Sstevel@tonic-gate LDAP_MUTEX_UNLOCK( ld, LDAP_OPTION_LOCK ); 176*0Sstevel@tonic-gate LDAP_MUTEX_UNLOCK( ld, LDAP_CONN_LOCK ); 177*0Sstevel@tonic-gate return( -1 ); 178*0Sstevel@tonic-gate } 179*0Sstevel@tonic-gate LDAP_MUTEX_UNLOCK( ld, LDAP_OPTION_LOCK ); 180*0Sstevel@tonic-gate 181*0Sstevel@tonic-gate if ( nsldapi_open_ldap_defconn( ld ) < 0 ) { 182*0Sstevel@tonic-gate ber_free( ber, 1 ); 183*0Sstevel@tonic-gate LDAP_MUTEX_UNLOCK( ld, LDAP_CONN_LOCK ); 184*0Sstevel@tonic-gate return( -1 ); 185*0Sstevel@tonic-gate } 186*0Sstevel@tonic-gate } 187*0Sstevel@tonic-gate lc = ld->ld_defconn; 188*0Sstevel@tonic-gate } else { 189*0Sstevel@tonic-gate if (( lc = find_connection( ld, srvlist, 1 )) == 190*0Sstevel@tonic-gate NULL ) { 191*0Sstevel@tonic-gate if ( bind && (parentreq != NULL) ) { 192*0Sstevel@tonic-gate /* Remember the bind in the parent */ 193*0Sstevel@tonic-gate incparent = 1; 194*0Sstevel@tonic-gate ++parentreq->lr_outrefcnt; 195*0Sstevel@tonic-gate } 196*0Sstevel@tonic-gate 197*0Sstevel@tonic-gate lc = nsldapi_new_connection( ld, &srvlist, 0, 198*0Sstevel@tonic-gate 1, bind ); 199*0Sstevel@tonic-gate } 200*0Sstevel@tonic-gate free_servers( srvlist ); 201*0Sstevel@tonic-gate } 202*0Sstevel@tonic-gate } 203*0Sstevel@tonic-gate 204*0Sstevel@tonic-gate 205*0Sstevel@tonic-gate /* 206*0Sstevel@tonic-gate * the logic here is: 207*0Sstevel@tonic-gate * if 208*0Sstevel@tonic-gate * 1. no connections exists, 209*0Sstevel@tonic-gate * or 210*0Sstevel@tonic-gate * 2. if the connection is either not in the connected 211*0Sstevel@tonic-gate * or connecting state in an async io model 212*0Sstevel@tonic-gate * or 213*0Sstevel@tonic-gate * 3. the connection is notin a connected state with normal (non async io) 214*0Sstevel@tonic-gate */ 215*0Sstevel@tonic-gate if ( lc == NULL 216*0Sstevel@tonic-gate || ( (ld->ld_options & LDAP_BITOPT_ASYNC 217*0Sstevel@tonic-gate && lc->lconn_status != LDAP_CONNST_CONNECTING 218*0Sstevel@tonic-gate && lc->lconn_status != LDAP_CONNST_CONNECTED) 219*0Sstevel@tonic-gate || (!(ld->ld_options & LDAP_BITOPT_ASYNC ) 220*0Sstevel@tonic-gate && lc->lconn_status != LDAP_CONNST_CONNECTED) ) ) { 221*0Sstevel@tonic-gate 222*0Sstevel@tonic-gate ber_free( ber, 1 ); 223*0Sstevel@tonic-gate if ( lc != NULL ) { 224*0Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, LDAP_SERVER_DOWN, NULL, NULL ); 225*0Sstevel@tonic-gate } 226*0Sstevel@tonic-gate if ( incparent ) { 227*0Sstevel@tonic-gate /* Forget about the bind */ 228*0Sstevel@tonic-gate --parentreq->lr_outrefcnt; 229*0Sstevel@tonic-gate } 230*0Sstevel@tonic-gate LDAP_MUTEX_UNLOCK( ld, LDAP_CONN_LOCK ); 231*0Sstevel@tonic-gate return( -1 ); 232*0Sstevel@tonic-gate } 233*0Sstevel@tonic-gate 234*0Sstevel@tonic-gate use_connection( ld, lc ); 235*0Sstevel@tonic-gate if (( lr = (LDAPRequest *)NSLDAPI_CALLOC( 1, sizeof( LDAPRequest ))) == 236*0Sstevel@tonic-gate NULL || ( bindreqdn != NULL && ( bindreqdn = 237*0Sstevel@tonic-gate nsldapi_strdup( bindreqdn )) == NULL )) { 238*0Sstevel@tonic-gate if ( lr != NULL ) { 239*0Sstevel@tonic-gate NSLDAPI_FREE( lr ); 240*0Sstevel@tonic-gate } 241*0Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, LDAP_NO_MEMORY, NULL, NULL ); 242*0Sstevel@tonic-gate nsldapi_free_connection( ld, lc, NULL, NULL, 0, 0 ); 243*0Sstevel@tonic-gate ber_free( ber, 1 ); 244*0Sstevel@tonic-gate if ( incparent ) { 245*0Sstevel@tonic-gate /* Forget about the bind */ 246*0Sstevel@tonic-gate --parentreq->lr_outrefcnt; 247*0Sstevel@tonic-gate } 248*0Sstevel@tonic-gate LDAP_MUTEX_UNLOCK( ld, LDAP_CONN_LOCK ); 249*0Sstevel@tonic-gate return( -1 ); 250*0Sstevel@tonic-gate } 251*0Sstevel@tonic-gate lr->lr_binddn = bindreqdn; 252*0Sstevel@tonic-gate lr->lr_msgid = msgid; 253*0Sstevel@tonic-gate lr->lr_status = LDAP_REQST_INPROGRESS; 254*0Sstevel@tonic-gate lr->lr_res_errno = LDAP_SUCCESS; /* optimistic */ 255*0Sstevel@tonic-gate lr->lr_ber = ber; 256*0Sstevel@tonic-gate lr->lr_conn = lc; 257*0Sstevel@tonic-gate 258*0Sstevel@tonic-gate if ( parentreq != NULL ) { /* sub-request */ 259*0Sstevel@tonic-gate if ( !incparent ) { 260*0Sstevel@tonic-gate /* Increment if we didn't do it before the bind */ 261*0Sstevel@tonic-gate ++parentreq->lr_outrefcnt; 262*0Sstevel@tonic-gate } 263*0Sstevel@tonic-gate lr->lr_origid = parentreq->lr_origid; 264*0Sstevel@tonic-gate lr->lr_parentcnt = parentreq->lr_parentcnt + 1; 265*0Sstevel@tonic-gate lr->lr_parent = parentreq; 266*0Sstevel@tonic-gate if ( parentreq->lr_child != NULL ) { 267*0Sstevel@tonic-gate lr->lr_sibling = parentreq->lr_child; 268*0Sstevel@tonic-gate } 269*0Sstevel@tonic-gate parentreq->lr_child = lr; 270*0Sstevel@tonic-gate } else { /* original request */ 271*0Sstevel@tonic-gate lr->lr_origid = lr->lr_msgid; 272*0Sstevel@tonic-gate } 273*0Sstevel@tonic-gate 274*0Sstevel@tonic-gate LDAP_MUTEX_LOCK( ld, LDAP_REQ_LOCK ); 275*0Sstevel@tonic-gate if (( lr->lr_next = ld->ld_requests ) != NULL ) { 276*0Sstevel@tonic-gate lr->lr_next->lr_prev = lr; 277*0Sstevel@tonic-gate } 278*0Sstevel@tonic-gate ld->ld_requests = lr; 279*0Sstevel@tonic-gate lr->lr_prev = NULL; 280*0Sstevel@tonic-gate 281*0Sstevel@tonic-gate if (( err = nsldapi_ber_flush( ld, lc->lconn_sb, ber, 0, 1 )) != 0 ) { 282*0Sstevel@tonic-gate 283*0Sstevel@tonic-gate /* need to continue write later */ 284*0Sstevel@tonic-gate if (ld->ld_options & LDAP_BITOPT_ASYNC && err == -2 ) { 285*0Sstevel@tonic-gate lr->lr_status = LDAP_REQST_WRITING; 286*0Sstevel@tonic-gate nsldapi_iostatus_interest_write( ld, lc->lconn_sb ); 287*0Sstevel@tonic-gate } else { 288*0Sstevel@tonic-gate 289*0Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, LDAP_SERVER_DOWN, NULL, NULL ); 290*0Sstevel@tonic-gate nsldapi_free_request( ld, lr, 0 ); 291*0Sstevel@tonic-gate nsldapi_free_connection( ld, lc, NULL, NULL, 0, 0 ); 292*0Sstevel@tonic-gate LDAP_MUTEX_UNLOCK( ld, LDAP_REQ_LOCK ); 293*0Sstevel@tonic-gate LDAP_MUTEX_UNLOCK( ld, LDAP_CONN_LOCK ); 294*0Sstevel@tonic-gate return( -1 ); 295*0Sstevel@tonic-gate } 296*0Sstevel@tonic-gate 297*0Sstevel@tonic-gate } else { 298*0Sstevel@tonic-gate if ( parentreq == NULL ) { 299*0Sstevel@tonic-gate ber->ber_end = ber->ber_ptr; 300*0Sstevel@tonic-gate ber->ber_ptr = ber->ber_buf; 301*0Sstevel@tonic-gate } 302*0Sstevel@tonic-gate 303*0Sstevel@tonic-gate /* sent -- waiting for a response */ 304*0Sstevel@tonic-gate if (ld->ld_options & LDAP_BITOPT_ASYNC) { 305*0Sstevel@tonic-gate lc->lconn_status = LDAP_CONNST_CONNECTED; 306*0Sstevel@tonic-gate } 307*0Sstevel@tonic-gate 308*0Sstevel@tonic-gate nsldapi_iostatus_interest_read( ld, lc->lconn_sb ); 309*0Sstevel@tonic-gate } 310*0Sstevel@tonic-gate LDAP_MUTEX_UNLOCK( ld, LDAP_REQ_LOCK ); 311*0Sstevel@tonic-gate LDAP_MUTEX_UNLOCK( ld, LDAP_CONN_LOCK ); 312*0Sstevel@tonic-gate 313*0Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, LDAP_SUCCESS, NULL, NULL ); 314*0Sstevel@tonic-gate return( msgid ); 315*0Sstevel@tonic-gate } 316*0Sstevel@tonic-gate 317*0Sstevel@tonic-gate 318*0Sstevel@tonic-gate /* 319*0Sstevel@tonic-gate * returns -1 if a fatal error occurs. If async is non-zero and the flush 320*0Sstevel@tonic-gate * would block, -2 is returned. 321*0Sstevel@tonic-gate */ 322*0Sstevel@tonic-gate int 323*0Sstevel@tonic-gate nsldapi_ber_flush( LDAP *ld, Sockbuf *sb, BerElement *ber, int freeit, 324*0Sstevel@tonic-gate int async ) 325*0Sstevel@tonic-gate { 326*0Sstevel@tonic-gate int terrno; 327*0Sstevel@tonic-gate 328*0Sstevel@tonic-gate for ( ;; ) { 329*0Sstevel@tonic-gate /* 330*0Sstevel@tonic-gate * ber_flush() doesn't set errno on EOF, so we pre-set it to 331*0Sstevel@tonic-gate * zero to avoid getting tricked by leftover "EAGAIN" errors 332*0Sstevel@tonic-gate */ 333*0Sstevel@tonic-gate LDAP_SET_ERRNO( ld, 0 ); 334*0Sstevel@tonic-gate 335*0Sstevel@tonic-gate if ( ber_flush( sb, ber, freeit ) == 0 ) { 336*0Sstevel@tonic-gate return( 0 ); /* success */ 337*0Sstevel@tonic-gate } 338*0Sstevel@tonic-gate 339*0Sstevel@tonic-gate terrno = LDAP_GET_ERRNO( ld ); 340*0Sstevel@tonic-gate 341*0Sstevel@tonic-gate if (ld->ld_options & LDAP_BITOPT_ASYNC) { 342*0Sstevel@tonic-gate if ( terrno != 0 && !NSLDAPI_ERRNO_IO_INPROGRESS( terrno )) { 343*0Sstevel@tonic-gate nsldapi_connection_lost_nolock( ld, sb ); 344*0Sstevel@tonic-gate return( -1 ); /* fatal error */ 345*0Sstevel@tonic-gate } 346*0Sstevel@tonic-gate } 347*0Sstevel@tonic-gate else if ( !NSLDAPI_ERRNO_IO_INPROGRESS( terrno )) { 348*0Sstevel@tonic-gate 349*0Sstevel@tonic-gate nsldapi_connection_lost_nolock( ld, sb ); 350*0Sstevel@tonic-gate return( -1 ); /* fatal error */ 351*0Sstevel@tonic-gate } 352*0Sstevel@tonic-gate 353*0Sstevel@tonic-gate if ( async ) { 354*0Sstevel@tonic-gate return( -2 ); /* would block */ 355*0Sstevel@tonic-gate } 356*0Sstevel@tonic-gate } 357*0Sstevel@tonic-gate } 358*0Sstevel@tonic-gate 359*0Sstevel@tonic-gate LDAPConn * 360*0Sstevel@tonic-gate nsldapi_new_connection( LDAP *ld, LDAPServer **srvlistp, int use_ldsb, 361*0Sstevel@tonic-gate int connect, int bind ) 362*0Sstevel@tonic-gate { 363*0Sstevel@tonic-gate int rc; 364*0Sstevel@tonic-gate 365*0Sstevel@tonic-gate LDAPConn *lc; 366*0Sstevel@tonic-gate LDAPServer *prevsrv, *srv; 367*0Sstevel@tonic-gate Sockbuf *sb = NULL; 368*0Sstevel@tonic-gate 369*0Sstevel@tonic-gate /* 370*0Sstevel@tonic-gate * make a new LDAP server connection 371*0Sstevel@tonic-gate */ 372*0Sstevel@tonic-gate if (( lc = (LDAPConn *)NSLDAPI_CALLOC( 1, sizeof( LDAPConn ))) == NULL 373*0Sstevel@tonic-gate || ( !use_ldsb && ( sb = ber_sockbuf_alloc()) == NULL )) { 374*0Sstevel@tonic-gate if ( lc != NULL ) { 375*0Sstevel@tonic-gate NSLDAPI_FREE( (char *)lc ); 376*0Sstevel@tonic-gate } 377*0Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, LDAP_NO_MEMORY, NULL, NULL ); 378*0Sstevel@tonic-gate return( NULL ); 379*0Sstevel@tonic-gate } 380*0Sstevel@tonic-gate 381*0Sstevel@tonic-gate LDAP_MUTEX_LOCK( ld, LDAP_OPTION_LOCK ); 382*0Sstevel@tonic-gate if ( !use_ldsb ) { 383*0Sstevel@tonic-gate /* 384*0Sstevel@tonic-gate * we have allocated a new sockbuf 385*0Sstevel@tonic-gate * set I/O routines to match those in default LDAP sockbuf 386*0Sstevel@tonic-gate */ 387*0Sstevel@tonic-gate IFP sb_fn; 388*0Sstevel@tonic-gate struct lber_x_ext_io_fns extiofns; 389*0Sstevel@tonic-gate 390*0Sstevel@tonic-gate extiofns.lbextiofn_size = LBER_X_EXTIO_FNS_SIZE; 391*0Sstevel@tonic-gate 392*0Sstevel@tonic-gate if ( ber_sockbuf_get_option( ld->ld_sbp, 393*0Sstevel@tonic-gate LBER_SOCKBUF_OPT_EXT_IO_FNS, &extiofns ) == 0 ) { 394*0Sstevel@tonic-gate ber_sockbuf_set_option( sb, 395*0Sstevel@tonic-gate LBER_SOCKBUF_OPT_EXT_IO_FNS, &extiofns ); 396*0Sstevel@tonic-gate } 397*0Sstevel@tonic-gate if ( ber_sockbuf_get_option( ld->ld_sbp, 398*0Sstevel@tonic-gate LBER_SOCKBUF_OPT_READ_FN, (void *)&sb_fn ) == 0 399*0Sstevel@tonic-gate && sb_fn != NULL ) { 400*0Sstevel@tonic-gate ber_sockbuf_set_option( sb, LBER_SOCKBUF_OPT_READ_FN, 401*0Sstevel@tonic-gate (void *)sb_fn ); 402*0Sstevel@tonic-gate } 403*0Sstevel@tonic-gate if ( ber_sockbuf_get_option( ld->ld_sbp, 404*0Sstevel@tonic-gate LBER_SOCKBUF_OPT_WRITE_FN, (void *)&sb_fn ) == 0 405*0Sstevel@tonic-gate && sb_fn != NULL ) { 406*0Sstevel@tonic-gate ber_sockbuf_set_option( sb, LBER_SOCKBUF_OPT_WRITE_FN, 407*0Sstevel@tonic-gate (void *)sb_fn ); 408*0Sstevel@tonic-gate } 409*0Sstevel@tonic-gate } 410*0Sstevel@tonic-gate 411*0Sstevel@tonic-gate lc->lconn_sb = ( use_ldsb ) ? ld->ld_sbp : sb; 412*0Sstevel@tonic-gate lc->lconn_version = ld->ld_version; /* inherited */ 413*0Sstevel@tonic-gate LDAP_MUTEX_UNLOCK( ld, LDAP_OPTION_LOCK ); 414*0Sstevel@tonic-gate 415*0Sstevel@tonic-gate if ( connect ) { 416*0Sstevel@tonic-gate prevsrv = NULL; 417*0Sstevel@tonic-gate /* 418*0Sstevel@tonic-gate * save the return code for later 419*0Sstevel@tonic-gate */ 420*0Sstevel@tonic-gate for ( srv = *srvlistp; srv != NULL; srv = srv->lsrv_next ) { 421*0Sstevel@tonic-gate rc = nsldapi_connect_to_host( ld, lc->lconn_sb, 422*0Sstevel@tonic-gate srv->lsrv_host, srv->lsrv_port, 423*0Sstevel@tonic-gate ( srv->lsrv_options & LDAP_SRV_OPT_SECURE ) != 0, 424*0Sstevel@tonic-gate &lc->lconn_krbinstance ); 425*0Sstevel@tonic-gate if (rc != -1) { 426*0Sstevel@tonic-gate break; 427*0Sstevel@tonic-gate } 428*0Sstevel@tonic-gate prevsrv = srv; 429*0Sstevel@tonic-gate } 430*0Sstevel@tonic-gate 431*0Sstevel@tonic-gate if ( srv == NULL ) { 432*0Sstevel@tonic-gate if ( !use_ldsb ) { 433*0Sstevel@tonic-gate NSLDAPI_FREE( (char *)lc->lconn_sb ); 434*0Sstevel@tonic-gate } 435*0Sstevel@tonic-gate NSLDAPI_FREE( (char *)lc ); 436*0Sstevel@tonic-gate /* nsldapi_open_ldap_connection has already set ld_errno */ 437*0Sstevel@tonic-gate return( NULL ); 438*0Sstevel@tonic-gate } 439*0Sstevel@tonic-gate 440*0Sstevel@tonic-gate if ( prevsrv == NULL ) { 441*0Sstevel@tonic-gate *srvlistp = srv->lsrv_next; 442*0Sstevel@tonic-gate } else { 443*0Sstevel@tonic-gate prevsrv->lsrv_next = srv->lsrv_next; 444*0Sstevel@tonic-gate } 445*0Sstevel@tonic-gate lc->lconn_server = srv; 446*0Sstevel@tonic-gate } 447*0Sstevel@tonic-gate 448*0Sstevel@tonic-gate if (ld->ld_options & LDAP_BITOPT_ASYNC && rc == -2) 449*0Sstevel@tonic-gate { 450*0Sstevel@tonic-gate lc->lconn_status = LDAP_CONNST_CONNECTING; 451*0Sstevel@tonic-gate } 452*0Sstevel@tonic-gate else { 453*0Sstevel@tonic-gate lc->lconn_status = LDAP_CONNST_CONNECTED; 454*0Sstevel@tonic-gate } 455*0Sstevel@tonic-gate 456*0Sstevel@tonic-gate lc->lconn_next = ld->ld_conns; 457*0Sstevel@tonic-gate ld->ld_conns = lc; 458*0Sstevel@tonic-gate 459*0Sstevel@tonic-gate /* 460*0Sstevel@tonic-gate * XXX for now, we always do a synchronous bind. This will have 461*0Sstevel@tonic-gate * to change in the long run... 462*0Sstevel@tonic-gate */ 463*0Sstevel@tonic-gate if ( bind ) { 464*0Sstevel@tonic-gate int err, lderr, freepasswd, authmethod; 465*0Sstevel@tonic-gate char *binddn, *passwd; 466*0Sstevel@tonic-gate LDAPConn *savedefconn; 467*0Sstevel@tonic-gate 468*0Sstevel@tonic-gate freepasswd = err = 0; 469*0Sstevel@tonic-gate 470*0Sstevel@tonic-gate if ( ld->ld_rebind_fn == NULL ) { 471*0Sstevel@tonic-gate binddn = passwd = ""; 472*0Sstevel@tonic-gate authmethod = LDAP_AUTH_SIMPLE; 473*0Sstevel@tonic-gate } else { 474*0Sstevel@tonic-gate if (( lderr = (*ld->ld_rebind_fn)( ld, &binddn, &passwd, 475*0Sstevel@tonic-gate &authmethod, 0, ld->ld_rebind_arg )) 476*0Sstevel@tonic-gate == LDAP_SUCCESS ) { 477*0Sstevel@tonic-gate freepasswd = 1; 478*0Sstevel@tonic-gate } else { 479*0Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, lderr, NULL, NULL ); 480*0Sstevel@tonic-gate err = -1; 481*0Sstevel@tonic-gate } 482*0Sstevel@tonic-gate } 483*0Sstevel@tonic-gate 484*0Sstevel@tonic-gate 485*0Sstevel@tonic-gate if ( err == 0 ) { 486*0Sstevel@tonic-gate savedefconn = ld->ld_defconn; 487*0Sstevel@tonic-gate ld->ld_defconn = lc; 488*0Sstevel@tonic-gate ++lc->lconn_refcnt; /* avoid premature free */ 489*0Sstevel@tonic-gate 490*0Sstevel@tonic-gate /* 491*0Sstevel@tonic-gate * when binding, we will back down as low as LDAPv2 492*0Sstevel@tonic-gate * if we get back "protocol error" from bind attempts 493*0Sstevel@tonic-gate */ 494*0Sstevel@tonic-gate for ( ;; ) { 495*0Sstevel@tonic-gate /* LDAP_MUTEX_UNLOCK(ld, LDAP_CONN_LOCK); */ 496*0Sstevel@tonic-gate if (( lderr = ldap_bind_s( ld, binddn, passwd, 497*0Sstevel@tonic-gate authmethod )) == LDAP_SUCCESS ) { 498*0Sstevel@tonic-gate /* LDAP_MUTEX_LOCK(ld, LDAP_CONN_LOCK); */ 499*0Sstevel@tonic-gate break; 500*0Sstevel@tonic-gate } 501*0Sstevel@tonic-gate /* LDAP_MUTEX_LOCK(ld, LDAP_CONN_LOCK); */ 502*0Sstevel@tonic-gate if ( lc->lconn_version <= LDAP_VERSION2 503*0Sstevel@tonic-gate || lderr != LDAP_PROTOCOL_ERROR ) { 504*0Sstevel@tonic-gate err = -1; 505*0Sstevel@tonic-gate break; 506*0Sstevel@tonic-gate } 507*0Sstevel@tonic-gate --lc->lconn_version; /* try lower version */ 508*0Sstevel@tonic-gate } 509*0Sstevel@tonic-gate --lc->lconn_refcnt; 510*0Sstevel@tonic-gate ld->ld_defconn = savedefconn; 511*0Sstevel@tonic-gate } 512*0Sstevel@tonic-gate 513*0Sstevel@tonic-gate if ( freepasswd ) { 514*0Sstevel@tonic-gate (*ld->ld_rebind_fn)( ld, &binddn, &passwd, 515*0Sstevel@tonic-gate &authmethod, 1, ld->ld_rebind_arg ); 516*0Sstevel@tonic-gate } 517*0Sstevel@tonic-gate 518*0Sstevel@tonic-gate if ( err != 0 ) { 519*0Sstevel@tonic-gate nsldapi_free_connection( ld, lc, NULL, NULL, 1, 0 ); 520*0Sstevel@tonic-gate lc = NULL; 521*0Sstevel@tonic-gate } 522*0Sstevel@tonic-gate } 523*0Sstevel@tonic-gate 524*0Sstevel@tonic-gate return( lc ); 525*0Sstevel@tonic-gate } 526*0Sstevel@tonic-gate 527*0Sstevel@tonic-gate 528*0Sstevel@tonic-gate #define LDAP_CONN_SAMEHOST( h1, h2 ) \ 529*0Sstevel@tonic-gate (( (h1) == NULL && (h2) == NULL ) || \ 530*0Sstevel@tonic-gate ( (h1) != NULL && (h2) != NULL && strcasecmp( (h1), (h2) ) == 0 )) 531*0Sstevel@tonic-gate 532*0Sstevel@tonic-gate static LDAPConn * 533*0Sstevel@tonic-gate find_connection( LDAP *ld, LDAPServer *srv, int any ) 534*0Sstevel@tonic-gate /* 535*0Sstevel@tonic-gate * return an existing connection (if any) to the server srv 536*0Sstevel@tonic-gate * if "any" is non-zero, check for any server in the "srv" chain 537*0Sstevel@tonic-gate */ 538*0Sstevel@tonic-gate { 539*0Sstevel@tonic-gate LDAPConn *lc; 540*0Sstevel@tonic-gate LDAPServer *ls; 541*0Sstevel@tonic-gate 542*0Sstevel@tonic-gate for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) { 543*0Sstevel@tonic-gate for ( ls = srv; ls != NULL; ls = ls->lsrv_next ) { 544*0Sstevel@tonic-gate if ( LDAP_CONN_SAMEHOST( ls->lsrv_host, 545*0Sstevel@tonic-gate lc->lconn_server->lsrv_host ) 546*0Sstevel@tonic-gate && ls->lsrv_port == lc->lconn_server->lsrv_port 547*0Sstevel@tonic-gate && ls->lsrv_options == 548*0Sstevel@tonic-gate lc->lconn_server->lsrv_options ) { 549*0Sstevel@tonic-gate return( lc ); 550*0Sstevel@tonic-gate } 551*0Sstevel@tonic-gate if ( !any ) { 552*0Sstevel@tonic-gate break; 553*0Sstevel@tonic-gate } 554*0Sstevel@tonic-gate } 555*0Sstevel@tonic-gate } 556*0Sstevel@tonic-gate 557*0Sstevel@tonic-gate return( NULL ); 558*0Sstevel@tonic-gate } 559*0Sstevel@tonic-gate 560*0Sstevel@tonic-gate 561*0Sstevel@tonic-gate 562*0Sstevel@tonic-gate static void 563*0Sstevel@tonic-gate use_connection( LDAP *ld, LDAPConn *lc ) 564*0Sstevel@tonic-gate { 565*0Sstevel@tonic-gate ++lc->lconn_refcnt; 566*0Sstevel@tonic-gate lc->lconn_lastused = time( 0 ); 567*0Sstevel@tonic-gate } 568*0Sstevel@tonic-gate 569*0Sstevel@tonic-gate 570*0Sstevel@tonic-gate void 571*0Sstevel@tonic-gate nsldapi_free_connection( LDAP *ld, LDAPConn *lc, LDAPControl **serverctrls, 572*0Sstevel@tonic-gate LDAPControl **clientctrls, int force, int unbind ) 573*0Sstevel@tonic-gate { 574*0Sstevel@tonic-gate LDAPConn *tmplc, *prevlc; 575*0Sstevel@tonic-gate 576*0Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "nsldapi_free_connection\n", 0, 0, 0 ); 577*0Sstevel@tonic-gate 578*0Sstevel@tonic-gate if ( force || --lc->lconn_refcnt <= 0 ) { 579*0Sstevel@tonic-gate if ( lc->lconn_status == LDAP_CONNST_CONNECTED ) { 580*0Sstevel@tonic-gate nsldapi_iostatus_interest_clear( ld, lc->lconn_sb ); 581*0Sstevel@tonic-gate if ( unbind ) { 582*0Sstevel@tonic-gate nsldapi_send_unbind( ld, lc->lconn_sb, 583*0Sstevel@tonic-gate serverctrls, clientctrls ); 584*0Sstevel@tonic-gate } 585*0Sstevel@tonic-gate } 586*0Sstevel@tonic-gate nsldapi_close_connection( ld, lc->lconn_sb ); 587*0Sstevel@tonic-gate prevlc = NULL; 588*0Sstevel@tonic-gate for ( tmplc = ld->ld_conns; tmplc != NULL; 589*0Sstevel@tonic-gate tmplc = tmplc->lconn_next ) { 590*0Sstevel@tonic-gate if ( tmplc == lc ) { 591*0Sstevel@tonic-gate if ( prevlc == NULL ) { 592*0Sstevel@tonic-gate ld->ld_conns = tmplc->lconn_next; 593*0Sstevel@tonic-gate } else { 594*0Sstevel@tonic-gate prevlc->lconn_next = tmplc->lconn_next; 595*0Sstevel@tonic-gate } 596*0Sstevel@tonic-gate break; 597*0Sstevel@tonic-gate } 598*0Sstevel@tonic-gate prevlc = tmplc; 599*0Sstevel@tonic-gate } 600*0Sstevel@tonic-gate free_servers( lc->lconn_server ); 601*0Sstevel@tonic-gate if ( lc->lconn_krbinstance != NULL ) { 602*0Sstevel@tonic-gate NSLDAPI_FREE( lc->lconn_krbinstance ); 603*0Sstevel@tonic-gate } 604*0Sstevel@tonic-gate /* 605*0Sstevel@tonic-gate * if this is the default connection (lc->lconn_sb==ld->ld_sbp) 606*0Sstevel@tonic-gate * we do not free the Sockbuf here since it will be freed 607*0Sstevel@tonic-gate * later inside ldap_unbind(). 608*0Sstevel@tonic-gate */ 609*0Sstevel@tonic-gate if ( lc->lconn_sb != ld->ld_sbp ) { 610*0Sstevel@tonic-gate ber_sockbuf_free( lc->lconn_sb ); 611*0Sstevel@tonic-gate lc->lconn_sb = NULL; 612*0Sstevel@tonic-gate } 613*0Sstevel@tonic-gate if ( lc->lconn_ber != NULLBER ) { 614*0Sstevel@tonic-gate ber_free( lc->lconn_ber, 1 ); 615*0Sstevel@tonic-gate } 616*0Sstevel@tonic-gate if ( lc->lconn_binddn != NULL ) { 617*0Sstevel@tonic-gate NSLDAPI_FREE( lc->lconn_binddn ); 618*0Sstevel@tonic-gate } 619*0Sstevel@tonic-gate NSLDAPI_FREE( lc ); 620*0Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "nsldapi_free_connection: actually freed\n", 621*0Sstevel@tonic-gate 0, 0, 0 ); 622*0Sstevel@tonic-gate } else { 623*0Sstevel@tonic-gate lc->lconn_lastused = time( 0 ); 624*0Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "nsldapi_free_connection: refcnt %d\n", 625*0Sstevel@tonic-gate lc->lconn_refcnt, 0, 0 ); 626*0Sstevel@tonic-gate } 627*0Sstevel@tonic-gate } 628*0Sstevel@tonic-gate 629*0Sstevel@tonic-gate 630*0Sstevel@tonic-gate #ifdef LDAP_DEBUG 631*0Sstevel@tonic-gate void 632*0Sstevel@tonic-gate nsldapi_dump_connection( LDAP *ld, LDAPConn *lconns, int all ) 633*0Sstevel@tonic-gate { 634*0Sstevel@tonic-gate LDAPConn *lc; 635*0Sstevel@tonic-gate char msg[256]; 636*0Sstevel@tonic-gate /* CTIME for this platform doesn't use this. */ 637*0Sstevel@tonic-gate #if !defined(SUNOS4) && !defined(_WIN32) && !defined(LINUX) 638*0Sstevel@tonic-gate char buf[26]; 639*0Sstevel@tonic-gate #endif 640*0Sstevel@tonic-gate 641*0Sstevel@tonic-gate sprintf( msg, "** Connection%s:\n", all ? "s" : "" ); 642*0Sstevel@tonic-gate ber_err_print( msg ); 643*0Sstevel@tonic-gate for ( lc = lconns; lc != NULL; lc = lc->lconn_next ) { 644*0Sstevel@tonic-gate if ( lc->lconn_server != NULL ) { 645*0Sstevel@tonic-gate sprintf( msg, "* host: %s port: %d secure: %s%s\n", 646*0Sstevel@tonic-gate ( lc->lconn_server->lsrv_host == NULL ) ? "(null)" 647*0Sstevel@tonic-gate : lc->lconn_server->lsrv_host, 648*0Sstevel@tonic-gate lc->lconn_server->lsrv_port, 649*0Sstevel@tonic-gate ( lc->lconn_server->lsrv_options & 650*0Sstevel@tonic-gate LDAP_SRV_OPT_SECURE ) ? "Yes" : 651*0Sstevel@tonic-gate "No", ( lc->lconn_sb == ld->ld_sbp ) ? 652*0Sstevel@tonic-gate " (default)" : "" ); 653*0Sstevel@tonic-gate ber_err_print( msg ); 654*0Sstevel@tonic-gate } 655*0Sstevel@tonic-gate sprintf( msg, " refcnt: %d status: %s\n", lc->lconn_refcnt, 656*0Sstevel@tonic-gate ( lc->lconn_status == LDAP_CONNST_NEEDSOCKET ) ? 657*0Sstevel@tonic-gate "NeedSocket" : ( lc->lconn_status == 658*0Sstevel@tonic-gate LDAP_CONNST_CONNECTING ) ? "Connecting" : 659*0Sstevel@tonic-gate ( lc->lconn_status == LDAP_CONNST_DEAD ) ? "Dead" : 660*0Sstevel@tonic-gate "Connected" ); 661*0Sstevel@tonic-gate ber_err_print( msg ); 662*0Sstevel@tonic-gate sprintf( msg, " last used: %s", 663*0Sstevel@tonic-gate NSLDAPI_CTIME( (time_t *) &lc->lconn_lastused, buf, 664*0Sstevel@tonic-gate sizeof(buf) )); 665*0Sstevel@tonic-gate ber_err_print( msg ); 666*0Sstevel@tonic-gate if ( lc->lconn_ber != NULLBER ) { 667*0Sstevel@tonic-gate ber_err_print( " partial response has been received:\n" ); 668*0Sstevel@tonic-gate ber_dump( lc->lconn_ber, 1 ); 669*0Sstevel@tonic-gate } 670*0Sstevel@tonic-gate ber_err_print( "\n" ); 671*0Sstevel@tonic-gate 672*0Sstevel@tonic-gate if ( !all ) { 673*0Sstevel@tonic-gate break; 674*0Sstevel@tonic-gate } 675*0Sstevel@tonic-gate } 676*0Sstevel@tonic-gate } 677*0Sstevel@tonic-gate 678*0Sstevel@tonic-gate 679*0Sstevel@tonic-gate void 680*0Sstevel@tonic-gate nsldapi_dump_requests_and_responses( LDAP *ld ) 681*0Sstevel@tonic-gate { 682*0Sstevel@tonic-gate LDAPRequest *lr; 683*0Sstevel@tonic-gate LDAPMessage *lm, *l; 684*0Sstevel@tonic-gate char msg[256]; 685*0Sstevel@tonic-gate 686*0Sstevel@tonic-gate ber_err_print( "** Outstanding Requests:\n" ); 687*0Sstevel@tonic-gate LDAP_MUTEX_LOCK( ld, LDAP_REQ_LOCK ); 688*0Sstevel@tonic-gate if (( lr = ld->ld_requests ) == NULL ) { 689*0Sstevel@tonic-gate ber_err_print( " Empty\n" ); 690*0Sstevel@tonic-gate } 691*0Sstevel@tonic-gate for ( ; lr != NULL; lr = lr->lr_next ) { 692*0Sstevel@tonic-gate sprintf( msg, " * msgid %d, origid %d, status %s\n", 693*0Sstevel@tonic-gate lr->lr_msgid, lr->lr_origid, ( lr->lr_status == 694*0Sstevel@tonic-gate LDAP_REQST_INPROGRESS ) ? "InProgress" : 695*0Sstevel@tonic-gate ( lr->lr_status == LDAP_REQST_CHASINGREFS ) ? "ChasingRefs" : 696*0Sstevel@tonic-gate ( lr->lr_status == LDAP_REQST_NOTCONNECTED ) ? "NotConnected" : 697*0Sstevel@tonic-gate ( lr->lr_status == LDAP_REQST_CONNDEAD ) ? "Dead" : 698*0Sstevel@tonic-gate "Writing" ); 699*0Sstevel@tonic-gate ber_err_print( msg ); 700*0Sstevel@tonic-gate sprintf( msg, " outstanding referrals %d, parent count %d\n", 701*0Sstevel@tonic-gate lr->lr_outrefcnt, lr->lr_parentcnt ); 702*0Sstevel@tonic-gate ber_err_print( msg ); 703*0Sstevel@tonic-gate if ( lr->lr_binddn != NULL ) { 704*0Sstevel@tonic-gate sprintf( msg, " pending bind DN: <%s>\n", lr->lr_binddn ); 705*0Sstevel@tonic-gate ber_err_print( msg ); 706*0Sstevel@tonic-gate } 707*0Sstevel@tonic-gate } 708*0Sstevel@tonic-gate LDAP_MUTEX_UNLOCK( ld, LDAP_REQ_LOCK ); 709*0Sstevel@tonic-gate 710*0Sstevel@tonic-gate ber_err_print( "** Response Queue:\n" ); 711*0Sstevel@tonic-gate LDAP_MUTEX_LOCK( ld, LDAP_RESP_LOCK ); 712*0Sstevel@tonic-gate if (( lm = ld->ld_responses ) == NULLMSG ) { 713*0Sstevel@tonic-gate ber_err_print( " Empty\n" ); 714*0Sstevel@tonic-gate } 715*0Sstevel@tonic-gate for ( ; lm != NULLMSG; lm = lm->lm_next ) { 716*0Sstevel@tonic-gate sprintf( msg, " * msgid %d, type %d\n", 717*0Sstevel@tonic-gate lm->lm_msgid, lm->lm_msgtype ); 718*0Sstevel@tonic-gate ber_err_print( msg ); 719*0Sstevel@tonic-gate if (( l = lm->lm_chain ) != NULL ) { 720*0Sstevel@tonic-gate ber_err_print( " chained responses:\n" ); 721*0Sstevel@tonic-gate for ( ; l != NULLMSG; l = l->lm_chain ) { 722*0Sstevel@tonic-gate sprintf( msg, 723*0Sstevel@tonic-gate " * msgid %d, type %d\n", 724*0Sstevel@tonic-gate l->lm_msgid, l->lm_msgtype ); 725*0Sstevel@tonic-gate ber_err_print( msg ); 726*0Sstevel@tonic-gate } 727*0Sstevel@tonic-gate } 728*0Sstevel@tonic-gate } 729*0Sstevel@tonic-gate LDAP_MUTEX_UNLOCK( ld, LDAP_RESP_LOCK ); 730*0Sstevel@tonic-gate } 731*0Sstevel@tonic-gate #endif /* LDAP_DEBUG */ 732*0Sstevel@tonic-gate 733*0Sstevel@tonic-gate 734*0Sstevel@tonic-gate void 735*0Sstevel@tonic-gate nsldapi_free_request( LDAP *ld, LDAPRequest *lr, int free_conn ) 736*0Sstevel@tonic-gate { 737*0Sstevel@tonic-gate LDAPRequest *tmplr, *nextlr; 738*0Sstevel@tonic-gate 739*0Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, 740*0Sstevel@tonic-gate "nsldapi_free_request 0x%x (origid %d, msgid %d)\n", 741*0Sstevel@tonic-gate lr, lr->lr_origid, lr->lr_msgid ); 742*0Sstevel@tonic-gate 743*0Sstevel@tonic-gate if ( lr->lr_parent != NULL ) { 744*0Sstevel@tonic-gate --lr->lr_parent->lr_outrefcnt; 745*0Sstevel@tonic-gate } 746*0Sstevel@tonic-gate 747*0Sstevel@tonic-gate /* free all of our spawned referrals (child requests) */ 748*0Sstevel@tonic-gate for ( tmplr = lr->lr_child; tmplr != NULL; tmplr = nextlr ) { 749*0Sstevel@tonic-gate nextlr = tmplr->lr_sibling; 750*0Sstevel@tonic-gate nsldapi_free_request( ld, tmplr, free_conn ); 751*0Sstevel@tonic-gate } 752*0Sstevel@tonic-gate 753*0Sstevel@tonic-gate if ( free_conn ) { 754*0Sstevel@tonic-gate nsldapi_free_connection( ld, lr->lr_conn, NULL, NULL, 0, 1 ); 755*0Sstevel@tonic-gate } 756*0Sstevel@tonic-gate 757*0Sstevel@tonic-gate if ( lr->lr_prev == NULL ) { 758*0Sstevel@tonic-gate ld->ld_requests = lr->lr_next; 759*0Sstevel@tonic-gate } else { 760*0Sstevel@tonic-gate lr->lr_prev->lr_next = lr->lr_next; 761*0Sstevel@tonic-gate } 762*0Sstevel@tonic-gate 763*0Sstevel@tonic-gate if ( lr->lr_next != NULL ) { 764*0Sstevel@tonic-gate lr->lr_next->lr_prev = lr->lr_prev; 765*0Sstevel@tonic-gate } 766*0Sstevel@tonic-gate 767*0Sstevel@tonic-gate if ( lr->lr_ber != NULL ) { 768*0Sstevel@tonic-gate ber_free( lr->lr_ber, 1 ); 769*0Sstevel@tonic-gate } 770*0Sstevel@tonic-gate 771*0Sstevel@tonic-gate if ( lr->lr_res_error != NULL ) { 772*0Sstevel@tonic-gate NSLDAPI_FREE( lr->lr_res_error ); 773*0Sstevel@tonic-gate } 774*0Sstevel@tonic-gate 775*0Sstevel@tonic-gate if ( lr->lr_res_matched != NULL ) { 776*0Sstevel@tonic-gate NSLDAPI_FREE( lr->lr_res_matched ); 777*0Sstevel@tonic-gate } 778*0Sstevel@tonic-gate 779*0Sstevel@tonic-gate if ( lr->lr_binddn != NULL ) { 780*0Sstevel@tonic-gate NSLDAPI_FREE( lr->lr_binddn ); 781*0Sstevel@tonic-gate } 782*0Sstevel@tonic-gate NSLDAPI_FREE( lr ); 783*0Sstevel@tonic-gate } 784*0Sstevel@tonic-gate 785*0Sstevel@tonic-gate 786*0Sstevel@tonic-gate static void 787*0Sstevel@tonic-gate free_servers( LDAPServer *srvlist ) 788*0Sstevel@tonic-gate { 789*0Sstevel@tonic-gate LDAPServer *nextsrv; 790*0Sstevel@tonic-gate 791*0Sstevel@tonic-gate while ( srvlist != NULL ) { 792*0Sstevel@tonic-gate nextsrv = srvlist->lsrv_next; 793*0Sstevel@tonic-gate if ( srvlist->lsrv_dn != NULL ) { 794*0Sstevel@tonic-gate NSLDAPI_FREE( srvlist->lsrv_dn ); 795*0Sstevel@tonic-gate } 796*0Sstevel@tonic-gate if ( srvlist->lsrv_host != NULL ) { 797*0Sstevel@tonic-gate NSLDAPI_FREE( srvlist->lsrv_host ); 798*0Sstevel@tonic-gate } 799*0Sstevel@tonic-gate NSLDAPI_FREE( srvlist ); 800*0Sstevel@tonic-gate srvlist = nextsrv; 801*0Sstevel@tonic-gate } 802*0Sstevel@tonic-gate } 803*0Sstevel@tonic-gate 804*0Sstevel@tonic-gate 805*0Sstevel@tonic-gate /* 806*0Sstevel@tonic-gate * Initiate chasing of LDAPv2+ (Umich extension) referrals. 807*0Sstevel@tonic-gate * 808*0Sstevel@tonic-gate * Returns an LDAP error code. 809*0Sstevel@tonic-gate * 810*0Sstevel@tonic-gate * Note that *hadrefp will be set to 1 if one or more referrals were found in 811*0Sstevel@tonic-gate * "*errstrp" (even if we can't chase them) and zero if none were found. 812*0Sstevel@tonic-gate * 813*0Sstevel@tonic-gate * XXX merging of errors in this routine needs to be improved. 814*0Sstevel@tonic-gate */ 815*0Sstevel@tonic-gate int 816*0Sstevel@tonic-gate nsldapi_chase_v2_referrals( LDAP *ld, LDAPRequest *lr, char **errstrp, 817*0Sstevel@tonic-gate int *totalcountp, int *chasingcountp ) 818*0Sstevel@tonic-gate { 819*0Sstevel@tonic-gate char *p, *ref, *unfollowed; 820*0Sstevel@tonic-gate LDAPRequest *origreq; 821*0Sstevel@tonic-gate int rc, tmprc, len, unknown; 822*0Sstevel@tonic-gate 823*0Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "nsldapi_chase_v2_referrals\n", 0, 0, 0 ); 824*0Sstevel@tonic-gate 825*0Sstevel@tonic-gate *totalcountp = *chasingcountp = 0; 826*0Sstevel@tonic-gate 827*0Sstevel@tonic-gate if ( *errstrp == NULL ) { 828*0Sstevel@tonic-gate return( LDAP_SUCCESS ); 829*0Sstevel@tonic-gate } 830*0Sstevel@tonic-gate 831*0Sstevel@tonic-gate len = strlen( *errstrp ); 832*0Sstevel@tonic-gate for ( p = *errstrp; len >= LDAP_REF_STR_LEN; ++p, --len ) { 833*0Sstevel@tonic-gate if (( *p == 'R' || *p == 'r' ) && strncasecmp( p, 834*0Sstevel@tonic-gate LDAP_REF_STR, LDAP_REF_STR_LEN ) == 0 ) { 835*0Sstevel@tonic-gate *p = '\0'; 836*0Sstevel@tonic-gate p += LDAP_REF_STR_LEN; 837*0Sstevel@tonic-gate break; 838*0Sstevel@tonic-gate } 839*0Sstevel@tonic-gate } 840*0Sstevel@tonic-gate 841*0Sstevel@tonic-gate if ( len < LDAP_REF_STR_LEN ) { 842*0Sstevel@tonic-gate return( LDAP_SUCCESS ); 843*0Sstevel@tonic-gate } 844*0Sstevel@tonic-gate 845*0Sstevel@tonic-gate if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) { 846*0Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, 847*0Sstevel@tonic-gate "more than %d referral hops (dropping)\n", 848*0Sstevel@tonic-gate ld->ld_refhoplimit, 0, 0 ); 849*0Sstevel@tonic-gate return( LDAP_REFERRAL_LIMIT_EXCEEDED ); 850*0Sstevel@tonic-gate } 851*0Sstevel@tonic-gate 852*0Sstevel@tonic-gate /* find original request */ 853*0Sstevel@tonic-gate for ( origreq = lr; origreq->lr_parent != NULL; 854*0Sstevel@tonic-gate origreq = origreq->lr_parent ) { 855*0Sstevel@tonic-gate ; 856*0Sstevel@tonic-gate } 857*0Sstevel@tonic-gate 858*0Sstevel@tonic-gate unfollowed = NULL; 859*0Sstevel@tonic-gate rc = LDAP_SUCCESS; 860*0Sstevel@tonic-gate 861*0Sstevel@tonic-gate /* parse out & follow referrals */ 862*0Sstevel@tonic-gate for ( ref = p; rc == LDAP_SUCCESS && ref != NULL; ref = p ) { 863*0Sstevel@tonic-gate if (( p = strchr( ref, '\n' )) != NULL ) { 864*0Sstevel@tonic-gate *p++ = '\0'; 865*0Sstevel@tonic-gate } else { 866*0Sstevel@tonic-gate p = NULL; 867*0Sstevel@tonic-gate } 868*0Sstevel@tonic-gate 869*0Sstevel@tonic-gate ++*totalcountp; 870*0Sstevel@tonic-gate 871*0Sstevel@tonic-gate rc = chase_one_referral( ld, lr, origreq, ref, "v2 referral", 872*0Sstevel@tonic-gate &unknown ); 873*0Sstevel@tonic-gate 874*0Sstevel@tonic-gate if ( rc != LDAP_SUCCESS || unknown ) { 875*0Sstevel@tonic-gate if (( tmprc = nsldapi_append_referral( ld, &unfollowed, 876*0Sstevel@tonic-gate ref )) != LDAP_SUCCESS ) { 877*0Sstevel@tonic-gate rc = tmprc; 878*0Sstevel@tonic-gate } 879*0Sstevel@tonic-gate } else { 880*0Sstevel@tonic-gate ++*chasingcountp; 881*0Sstevel@tonic-gate } 882*0Sstevel@tonic-gate } 883*0Sstevel@tonic-gate 884*0Sstevel@tonic-gate NSLDAPI_FREE( *errstrp ); 885*0Sstevel@tonic-gate *errstrp = unfollowed; 886*0Sstevel@tonic-gate 887*0Sstevel@tonic-gate return( rc ); 888*0Sstevel@tonic-gate } 889*0Sstevel@tonic-gate 890*0Sstevel@tonic-gate 891*0Sstevel@tonic-gate /* returns an LDAP error code */ 892*0Sstevel@tonic-gate int 893*0Sstevel@tonic-gate nsldapi_chase_v3_refs( LDAP *ld, LDAPRequest *lr, char **v3refs, 894*0Sstevel@tonic-gate int is_reference, int *totalcountp, int *chasingcountp ) 895*0Sstevel@tonic-gate { 896*0Sstevel@tonic-gate int i, rc, unknown; 897*0Sstevel@tonic-gate LDAPRequest *origreq; 898*0Sstevel@tonic-gate 899*0Sstevel@tonic-gate *totalcountp = *chasingcountp = 0; 900*0Sstevel@tonic-gate 901*0Sstevel@tonic-gate if ( v3refs == NULL || v3refs[0] == NULL ) { 902*0Sstevel@tonic-gate return( LDAP_SUCCESS ); 903*0Sstevel@tonic-gate } 904*0Sstevel@tonic-gate 905*0Sstevel@tonic-gate *totalcountp = 1; 906*0Sstevel@tonic-gate 907*0Sstevel@tonic-gate if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) { 908*0Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, 909*0Sstevel@tonic-gate "more than %d referral hops (dropping)\n", 910*0Sstevel@tonic-gate ld->ld_refhoplimit, 0, 0 ); 911*0Sstevel@tonic-gate return( LDAP_REFERRAL_LIMIT_EXCEEDED ); 912*0Sstevel@tonic-gate } 913*0Sstevel@tonic-gate 914*0Sstevel@tonic-gate /* find original request */ 915*0Sstevel@tonic-gate for ( origreq = lr; origreq->lr_parent != NULL; 916*0Sstevel@tonic-gate origreq = origreq->lr_parent ) { 917*0Sstevel@tonic-gate ; 918*0Sstevel@tonic-gate } 919*0Sstevel@tonic-gate 920*0Sstevel@tonic-gate /* 921*0Sstevel@tonic-gate * in LDAPv3, we just need to follow one referral in the set. 922*0Sstevel@tonic-gate * we dp this by stopping as soon as we succeed in initiating a 923*0Sstevel@tonic-gate * chase on any referral (basically this means we were able to connect 924*0Sstevel@tonic-gate * to the server and bind). 925*0Sstevel@tonic-gate */ 926*0Sstevel@tonic-gate for ( i = 0; v3refs[i] != NULL; ++i ) { 927*0Sstevel@tonic-gate rc = chase_one_referral( ld, lr, origreq, v3refs[i], 928*0Sstevel@tonic-gate is_reference ? "v3 reference" : "v3 referral", &unknown ); 929*0Sstevel@tonic-gate if ( rc == LDAP_SUCCESS && !unknown ) { 930*0Sstevel@tonic-gate *chasingcountp = 1; 931*0Sstevel@tonic-gate break; 932*0Sstevel@tonic-gate } 933*0Sstevel@tonic-gate } 934*0Sstevel@tonic-gate 935*0Sstevel@tonic-gate /* XXXmcs: should we save unfollowed referrals somewhere? */ 936*0Sstevel@tonic-gate 937*0Sstevel@tonic-gate return( rc ); /* last error is as good as any other I guess... */ 938*0Sstevel@tonic-gate } 939*0Sstevel@tonic-gate 940*0Sstevel@tonic-gate /* 941*0Sstevel@tonic-gate * returns an LDAP error code 942*0Sstevel@tonic-gate * 943*0Sstevel@tonic-gate * XXXmcs: this function used to have #ifdef LDAP_DNS code in it but I 944*0Sstevel@tonic-gate * removed it when I improved the parsing (we don't define LDAP_DNS 945*0Sstevel@tonic-gate * here at Netscape). 946*0Sstevel@tonic-gate */ 947*0Sstevel@tonic-gate static int 948*0Sstevel@tonic-gate chase_one_referral( LDAP *ld, LDAPRequest *lr, LDAPRequest *origreq, 949*0Sstevel@tonic-gate char *refurl, char *desc, int *unknownp ) 950*0Sstevel@tonic-gate { 951*0Sstevel@tonic-gate int rc, tmprc, secure, msgid; 952*0Sstevel@tonic-gate LDAPServer *srv; 953*0Sstevel@tonic-gate BerElement *ber; 954*0Sstevel@tonic-gate LDAPURLDesc *ludp; 955*0Sstevel@tonic-gate 956*0Sstevel@tonic-gate *unknownp = 0; 957*0Sstevel@tonic-gate ludp = NULLLDAPURLDESC; 958*0Sstevel@tonic-gate 959*0Sstevel@tonic-gate if ( nsldapi_url_parse( refurl, &ludp, 0 ) != 0 ) { 960*0Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, 961*0Sstevel@tonic-gate "ignoring unknown %s <%s>\n", desc, refurl, 0 ); 962*0Sstevel@tonic-gate *unknownp = 1; 963*0Sstevel@tonic-gate rc = LDAP_SUCCESS; 964*0Sstevel@tonic-gate goto cleanup_and_return; 965*0Sstevel@tonic-gate } 966*0Sstevel@tonic-gate 967*0Sstevel@tonic-gate secure = (( ludp->lud_options & LDAP_URL_OPT_SECURE ) != 0 ); 968*0Sstevel@tonic-gate 969*0Sstevel@tonic-gate /* XXXmcs: can't tell if secure is supported by connect callback */ 970*0Sstevel@tonic-gate if ( secure && ld->ld_extconnect_fn == NULL ) { 971*0Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, 972*0Sstevel@tonic-gate "ignoring LDAPS %s <%s>\n", desc, refurl, 0 ); 973*0Sstevel@tonic-gate *unknownp = 1; 974*0Sstevel@tonic-gate rc = LDAP_SUCCESS; 975*0Sstevel@tonic-gate goto cleanup_and_return; 976*0Sstevel@tonic-gate } 977*0Sstevel@tonic-gate 978*0Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "chasing LDAP%s %s: <%s>\n", 979*0Sstevel@tonic-gate secure ? "S" : "", desc, refurl ); 980*0Sstevel@tonic-gate 981*0Sstevel@tonic-gate LDAP_MUTEX_LOCK( ld, LDAP_MSGID_LOCK ); 982*0Sstevel@tonic-gate msgid = ++ld->ld_msgid; 983*0Sstevel@tonic-gate LDAP_MUTEX_UNLOCK( ld, LDAP_MSGID_LOCK ); 984*0Sstevel@tonic-gate 985*0Sstevel@tonic-gate if (( tmprc = re_encode_request( ld, origreq->lr_ber, msgid, 986*0Sstevel@tonic-gate ludp, &ber )) != LDAP_SUCCESS ) { 987*0Sstevel@tonic-gate rc = tmprc; 988*0Sstevel@tonic-gate goto cleanup_and_return; 989*0Sstevel@tonic-gate } 990*0Sstevel@tonic-gate 991*0Sstevel@tonic-gate if (( srv = (LDAPServer *)NSLDAPI_CALLOC( 1, sizeof( LDAPServer ))) 992*0Sstevel@tonic-gate == NULL ) { 993*0Sstevel@tonic-gate ber_free( ber, 1 ); 994*0Sstevel@tonic-gate rc = LDAP_NO_MEMORY; 995*0Sstevel@tonic-gate goto cleanup_and_return; 996*0Sstevel@tonic-gate } 997*0Sstevel@tonic-gate 998*0Sstevel@tonic-gate if (ludp->lud_host == NULL && ld->ld_defhost == NULL) { 999*0Sstevel@tonic-gate srv->lsrv_host = NULL; 1000*0Sstevel@tonic-gate } else { 1001*0Sstevel@tonic-gate if (ludp->lud_host == NULL) { 1002*0Sstevel@tonic-gate srv->lsrv_host = 1003*0Sstevel@tonic-gate nsldapi_strdup( origreq->lr_conn->lconn_server->lsrv_host ); 1004*0Sstevel@tonic-gate LDAPDebug(LDAP_DEBUG_TRACE, 1005*0Sstevel@tonic-gate "chase_one_referral: using hostname '%s' from original " 1006*0Sstevel@tonic-gate "request on new request\n", 1007*0Sstevel@tonic-gate srv->lsrv_host, 0, 0); 1008*0Sstevel@tonic-gate } else { 1009*0Sstevel@tonic-gate srv->lsrv_host = nsldapi_strdup(ludp->lud_host); 1010*0Sstevel@tonic-gate LDAPDebug(LDAP_DEBUG_TRACE, 1011*0Sstevel@tonic-gate "chase_one_referral: using hostname '%s' as specified " 1012*0Sstevel@tonic-gate "on new request\n", 1013*0Sstevel@tonic-gate srv->lsrv_host, 0, 0); 1014*0Sstevel@tonic-gate } 1015*0Sstevel@tonic-gate 1016*0Sstevel@tonic-gate if (srv->lsrv_host == NULL) { 1017*0Sstevel@tonic-gate NSLDAPI_FREE((char *)srv); 1018*0Sstevel@tonic-gate ber_free(ber, 1); 1019*0Sstevel@tonic-gate rc = LDAP_NO_MEMORY; 1020*0Sstevel@tonic-gate goto cleanup_and_return; 1021*0Sstevel@tonic-gate } 1022*0Sstevel@tonic-gate } 1023*0Sstevel@tonic-gate 1024*0Sstevel@tonic-gate /* 1025*0Sstevel@tonic-gate * According to our reading of RFCs 2255 and 1738, the 1026*0Sstevel@tonic-gate * following algorithm applies: 1027*0Sstevel@tonic-gate * - no hostport (no host, no port) provided in LDAP URL, use those 1028*0Sstevel@tonic-gate * of previous request 1029*0Sstevel@tonic-gate * - no port but a host, use default LDAP port 1030*0Sstevel@tonic-gate * - else use given hostport 1031*0Sstevel@tonic-gate */ 1032*0Sstevel@tonic-gate if (ludp->lud_port == 0 && ludp->lud_host == NULL) { 1033*0Sstevel@tonic-gate srv->lsrv_port = origreq->lr_conn->lconn_server->lsrv_port; 1034*0Sstevel@tonic-gate LDAPDebug(LDAP_DEBUG_TRACE, 1035*0Sstevel@tonic-gate "chase_one_referral: using port (%d) from original " 1036*0Sstevel@tonic-gate "request on new request\n", 1037*0Sstevel@tonic-gate srv->lsrv_port, 0, 0); 1038*0Sstevel@tonic-gate } else if (ludp->lud_port == 0 && ludp->lud_host != NULL) { 1039*0Sstevel@tonic-gate srv->lsrv_port = (secure) ? LDAPS_PORT : LDAP_PORT; 1040*0Sstevel@tonic-gate LDAPDebug(LDAP_DEBUG_TRACE, 1041*0Sstevel@tonic-gate "chase_one_referral: using default port (%d) \n", 1042*0Sstevel@tonic-gate srv->lsrv_port, 0, 0); 1043*0Sstevel@tonic-gate } else { 1044*0Sstevel@tonic-gate srv->lsrv_port = ludp->lud_port; 1045*0Sstevel@tonic-gate LDAPDebug(LDAP_DEBUG_TRACE, 1046*0Sstevel@tonic-gate "chase_one_referral: using port (%d) as specified on " 1047*0Sstevel@tonic-gate "new request\n", 1048*0Sstevel@tonic-gate srv->lsrv_port, 0, 0); 1049*0Sstevel@tonic-gate } 1050*0Sstevel@tonic-gate 1051*0Sstevel@tonic-gate if ( secure ) { 1052*0Sstevel@tonic-gate srv->lsrv_options |= LDAP_SRV_OPT_SECURE; 1053*0Sstevel@tonic-gate } 1054*0Sstevel@tonic-gate 1055*0Sstevel@tonic-gate if ( nsldapi_send_server_request( ld, ber, msgid, 1056*0Sstevel@tonic-gate lr, srv, NULL, NULL, 1 ) < 0 ) { 1057*0Sstevel@tonic-gate rc = LDAP_GET_LDERRNO( ld, NULL, NULL ); 1058*0Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_ANY, "Unable to chase %s %s (%s)\n", 1059*0Sstevel@tonic-gate desc, refurl, ldap_err2string( rc )); 1060*0Sstevel@tonic-gate } else { 1061*0Sstevel@tonic-gate rc = LDAP_SUCCESS; 1062*0Sstevel@tonic-gate } 1063*0Sstevel@tonic-gate 1064*0Sstevel@tonic-gate cleanup_and_return: 1065*0Sstevel@tonic-gate if ( ludp != NULLLDAPURLDESC ) { 1066*0Sstevel@tonic-gate ldap_free_urldesc( ludp ); 1067*0Sstevel@tonic-gate } 1068*0Sstevel@tonic-gate 1069*0Sstevel@tonic-gate return( rc ); 1070*0Sstevel@tonic-gate } 1071*0Sstevel@tonic-gate 1072*0Sstevel@tonic-gate 1073*0Sstevel@tonic-gate /* returns an LDAP error code */ 1074*0Sstevel@tonic-gate int 1075*0Sstevel@tonic-gate nsldapi_append_referral( LDAP *ld, char **referralsp, char *s ) 1076*0Sstevel@tonic-gate { 1077*0Sstevel@tonic-gate int first; 1078*0Sstevel@tonic-gate 1079*0Sstevel@tonic-gate if ( *referralsp == NULL ) { 1080*0Sstevel@tonic-gate first = 1; 1081*0Sstevel@tonic-gate *referralsp = (char *)NSLDAPI_MALLOC( strlen( s ) + 1082*0Sstevel@tonic-gate LDAP_REF_STR_LEN + 1 ); 1083*0Sstevel@tonic-gate } else { 1084*0Sstevel@tonic-gate first = 0; 1085*0Sstevel@tonic-gate *referralsp = (char *)NSLDAPI_REALLOC( *referralsp, 1086*0Sstevel@tonic-gate strlen( *referralsp ) + strlen( s ) + 2 ); 1087*0Sstevel@tonic-gate } 1088*0Sstevel@tonic-gate 1089*0Sstevel@tonic-gate if ( *referralsp == NULL ) { 1090*0Sstevel@tonic-gate return( LDAP_NO_MEMORY ); 1091*0Sstevel@tonic-gate } 1092*0Sstevel@tonic-gate 1093*0Sstevel@tonic-gate if ( first ) { 1094*0Sstevel@tonic-gate strcpy( *referralsp, LDAP_REF_STR ); 1095*0Sstevel@tonic-gate } else { 1096*0Sstevel@tonic-gate strcat( *referralsp, "\n" ); 1097*0Sstevel@tonic-gate } 1098*0Sstevel@tonic-gate strcat( *referralsp, s ); 1099*0Sstevel@tonic-gate 1100*0Sstevel@tonic-gate return( LDAP_SUCCESS ); 1101*0Sstevel@tonic-gate } 1102*0Sstevel@tonic-gate 1103*0Sstevel@tonic-gate 1104*0Sstevel@tonic-gate 1105*0Sstevel@tonic-gate /* returns an LDAP error code */ 1106*0Sstevel@tonic-gate static int 1107*0Sstevel@tonic-gate re_encode_request( LDAP *ld, BerElement *origber, int msgid, LDAPURLDesc *ludp, 1108*0Sstevel@tonic-gate BerElement **berp ) 1109*0Sstevel@tonic-gate { 1110*0Sstevel@tonic-gate /* 1111*0Sstevel@tonic-gate * XXX this routine knows way too much about how the lber library works! 1112*0Sstevel@tonic-gate */ 1113*0Sstevel@tonic-gate ber_uint_t along; 1114*0Sstevel@tonic-gate ber_tag_t tag; 1115*0Sstevel@tonic-gate ber_int_t ver; 1116*0Sstevel@tonic-gate int rc; 1117*0Sstevel@tonic-gate BerElement *ber; 1118*0Sstevel@tonic-gate struct berelement tmpber; 1119*0Sstevel@tonic-gate char *dn, *orig_dn; 1120*0Sstevel@tonic-gate 1121*0Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, 1122*0Sstevel@tonic-gate "re_encode_request: new msgid %d, new dn <%s>\n", 1123*0Sstevel@tonic-gate msgid, ( ludp->lud_dn == NULL ) ? "NONE" : ludp->lud_dn, 0 ); 1124*0Sstevel@tonic-gate 1125*0Sstevel@tonic-gate tmpber = *origber; 1126*0Sstevel@tonic-gate 1127*0Sstevel@tonic-gate /* 1128*0Sstevel@tonic-gate * All LDAP requests are sequences that start with a message id. For 1129*0Sstevel@tonic-gate * everything except delete requests, this is followed by a sequence 1130*0Sstevel@tonic-gate * that is tagged with the operation code. For deletes, there is just 1131*0Sstevel@tonic-gate * a DN that is tagged with the operation code. 1132*0Sstevel@tonic-gate */ 1133*0Sstevel@tonic-gate 1134*0Sstevel@tonic-gate /* skip past msgid and get operation tag */ 1135*0Sstevel@tonic-gate if ( ber_scanf( &tmpber, "{it", &along, &tag ) == LBER_ERROR ) { 1136*0Sstevel@tonic-gate return( LDAP_DECODING_ERROR ); 1137*0Sstevel@tonic-gate } 1138*0Sstevel@tonic-gate 1139*0Sstevel@tonic-gate /* 1140*0Sstevel@tonic-gate * XXXmcs: we don't support scope or filters in search referrals yet, 1141*0Sstevel@tonic-gate * so if either were present we return an error which is probably 1142*0Sstevel@tonic-gate * better than just ignoring the extra info. 1143*0Sstevel@tonic-gate */ 1144*0Sstevel@tonic-gate if ( tag == LDAP_REQ_SEARCH && 1145*0Sstevel@tonic-gate ( ludp->lud_scope != -1 || ludp->lud_filter != NULL )) { 1146*0Sstevel@tonic-gate return( LDAP_LOCAL_ERROR ); 1147*0Sstevel@tonic-gate } 1148*0Sstevel@tonic-gate 1149*0Sstevel@tonic-gate if ( tag == LDAP_REQ_BIND ) { 1150*0Sstevel@tonic-gate /* bind requests have a version number before the DN */ 1151*0Sstevel@tonic-gate rc = ber_scanf( &tmpber, "{ia", &ver, &orig_dn ); 1152*0Sstevel@tonic-gate } else if ( tag == LDAP_REQ_DELETE ) { 1153*0Sstevel@tonic-gate /* delete requests DNs are not within a sequence */ 1154*0Sstevel@tonic-gate rc = ber_scanf( &tmpber, "a", &orig_dn ); 1155*0Sstevel@tonic-gate } else { 1156*0Sstevel@tonic-gate rc = ber_scanf( &tmpber, "{a", &orig_dn ); 1157*0Sstevel@tonic-gate } 1158*0Sstevel@tonic-gate 1159*0Sstevel@tonic-gate if ( rc == LBER_ERROR ) { 1160*0Sstevel@tonic-gate return( LDAP_DECODING_ERROR ); 1161*0Sstevel@tonic-gate } 1162*0Sstevel@tonic-gate 1163*0Sstevel@tonic-gate if ( ludp->lud_dn == NULL ) { 1164*0Sstevel@tonic-gate dn = orig_dn; 1165*0Sstevel@tonic-gate } else { 1166*0Sstevel@tonic-gate dn = ludp->lud_dn; 1167*0Sstevel@tonic-gate NSLDAPI_FREE( orig_dn ); 1168*0Sstevel@tonic-gate orig_dn = NULL; 1169*0Sstevel@tonic-gate } 1170*0Sstevel@tonic-gate 1171*0Sstevel@tonic-gate /* allocate and build the new request */ 1172*0Sstevel@tonic-gate if (( rc = nsldapi_alloc_ber_with_options( ld, &ber )) 1173*0Sstevel@tonic-gate != LDAP_SUCCESS ) { 1174*0Sstevel@tonic-gate if ( orig_dn != NULL ) { 1175*0Sstevel@tonic-gate NSLDAPI_FREE( orig_dn ); 1176*0Sstevel@tonic-gate } 1177*0Sstevel@tonic-gate return( rc ); 1178*0Sstevel@tonic-gate } 1179*0Sstevel@tonic-gate 1180*0Sstevel@tonic-gate if ( tag == LDAP_REQ_BIND ) { 1181*0Sstevel@tonic-gate rc = ber_printf( ber, "{it{is", msgid, tag, 1182*0Sstevel@tonic-gate (int)ver /* XXX lossy cast */, dn ); 1183*0Sstevel@tonic-gate } else if ( tag == LDAP_REQ_DELETE ) { 1184*0Sstevel@tonic-gate rc = ber_printf( ber, "{its}", msgid, tag, dn ); 1185*0Sstevel@tonic-gate } else { 1186*0Sstevel@tonic-gate rc = ber_printf( ber, "{it{s", msgid, tag, dn ); 1187*0Sstevel@tonic-gate } 1188*0Sstevel@tonic-gate 1189*0Sstevel@tonic-gate if ( orig_dn != NULL ) { 1190*0Sstevel@tonic-gate NSLDAPI_FREE( orig_dn ); 1191*0Sstevel@tonic-gate } 1192*0Sstevel@tonic-gate /* 1193*0Sstevel@tonic-gate * can't use "dn" or "orig_dn" from this point on (they've been freed) 1194*0Sstevel@tonic-gate */ 1195*0Sstevel@tonic-gate 1196*0Sstevel@tonic-gate if ( rc == -1 ) { 1197*0Sstevel@tonic-gate ber_free( ber, 1 ); 1198*0Sstevel@tonic-gate return( LDAP_ENCODING_ERROR ); 1199*0Sstevel@tonic-gate } 1200*0Sstevel@tonic-gate 1201*0Sstevel@tonic-gate if ( tag != LDAP_REQ_DELETE && 1202*0Sstevel@tonic-gate ( ber_write( ber, tmpber.ber_ptr, ( tmpber.ber_end - 1203*0Sstevel@tonic-gate tmpber.ber_ptr ), 0 ) != ( tmpber.ber_end - tmpber.ber_ptr ) 1204*0Sstevel@tonic-gate || ber_printf( ber, "}}" ) == -1 )) { 1205*0Sstevel@tonic-gate ber_free( ber, 1 ); 1206*0Sstevel@tonic-gate return( LDAP_ENCODING_ERROR ); 1207*0Sstevel@tonic-gate } 1208*0Sstevel@tonic-gate 1209*0Sstevel@tonic-gate #ifdef LDAP_DEBUG 1210*0Sstevel@tonic-gate if ( ldap_debug & LDAP_DEBUG_PACKETS ) { 1211*0Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_ANY, "re_encode_request new request is:\n", 1212*0Sstevel@tonic-gate 0, 0, 0 ); 1213*0Sstevel@tonic-gate ber_dump( ber, 0 ); 1214*0Sstevel@tonic-gate } 1215*0Sstevel@tonic-gate #endif /* LDAP_DEBUG */ 1216*0Sstevel@tonic-gate 1217*0Sstevel@tonic-gate *berp = ber; 1218*0Sstevel@tonic-gate return( LDAP_SUCCESS ); 1219*0Sstevel@tonic-gate } 1220*0Sstevel@tonic-gate 1221*0Sstevel@tonic-gate 1222*0Sstevel@tonic-gate LDAPRequest * 1223*0Sstevel@tonic-gate nsldapi_find_request_by_msgid( LDAP *ld, int msgid ) 1224*0Sstevel@tonic-gate { 1225*0Sstevel@tonic-gate LDAPRequest *lr; 1226*0Sstevel@tonic-gate 1227*0Sstevel@tonic-gate for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) { 1228*0Sstevel@tonic-gate if ( msgid == lr->lr_msgid ) { 1229*0Sstevel@tonic-gate break; 1230*0Sstevel@tonic-gate } 1231*0Sstevel@tonic-gate } 1232*0Sstevel@tonic-gate 1233*0Sstevel@tonic-gate return( lr ); 1234*0Sstevel@tonic-gate } 1235*0Sstevel@tonic-gate 1236*0Sstevel@tonic-gate 1237*0Sstevel@tonic-gate /* 1238*0Sstevel@tonic-gate * nsldapi_connection_lost_nolock() resets "ld" to a non-connected, known 1239*0Sstevel@tonic-gate * state. It should be called whenever a fatal error occurs on the 1240*0Sstevel@tonic-gate * Sockbuf "sb." sb == NULL means we don't know specifically where 1241*0Sstevel@tonic-gate * the problem was so we assume all connections are bad. 1242*0Sstevel@tonic-gate */ 1243*0Sstevel@tonic-gate void 1244*0Sstevel@tonic-gate nsldapi_connection_lost_nolock( LDAP *ld, Sockbuf *sb ) 1245*0Sstevel@tonic-gate { 1246*0Sstevel@tonic-gate LDAPRequest *lr; 1247*0Sstevel@tonic-gate 1248*0Sstevel@tonic-gate /* 1249*0Sstevel@tonic-gate * change status of all pending requests that are associated with "sb 1250*0Sstevel@tonic-gate * to "connection dead." 1251*0Sstevel@tonic-gate * also change the connection status to "dead" and remove it from 1252*0Sstevel@tonic-gate * the list of sockets we are interested in. 1253*0Sstevel@tonic-gate */ 1254*0Sstevel@tonic-gate for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) { 1255*0Sstevel@tonic-gate if ( sb == NULL || 1256*0Sstevel@tonic-gate ( lr->lr_conn != NULL && lr->lr_conn->lconn_sb == sb )) { 1257*0Sstevel@tonic-gate lr->lr_status = LDAP_REQST_CONNDEAD; 1258*0Sstevel@tonic-gate if ( lr->lr_conn != NULL ) { 1259*0Sstevel@tonic-gate lr->lr_conn->lconn_status = LDAP_CONNST_DEAD; 1260*0Sstevel@tonic-gate nsldapi_iostatus_interest_clear( ld, 1261*0Sstevel@tonic-gate lr->lr_conn->lconn_sb ); 1262*0Sstevel@tonic-gate } 1263*0Sstevel@tonic-gate } 1264*0Sstevel@tonic-gate } 1265*0Sstevel@tonic-gate } 1266*0Sstevel@tonic-gate 1267*0Sstevel@tonic-gate 1268*0Sstevel@tonic-gate #ifdef LDAP_DNS 1269*0Sstevel@tonic-gate static LDAPServer * 1270*0Sstevel@tonic-gate dn2servers( LDAP *ld, char *dn ) /* dn can also be a domain.... */ 1271*0Sstevel@tonic-gate { 1272*0Sstevel@tonic-gate char *p, *domain, *host, *server_dn, **dxs; 1273*0Sstevel@tonic-gate int i, port; 1274*0Sstevel@tonic-gate LDAPServer *srvlist, *prevsrv, *srv; 1275*0Sstevel@tonic-gate 1276*0Sstevel@tonic-gate if (( domain = strrchr( dn, '@' )) != NULL ) { 1277*0Sstevel@tonic-gate ++domain; 1278*0Sstevel@tonic-gate } else { 1279*0Sstevel@tonic-gate domain = dn; 1280*0Sstevel@tonic-gate } 1281*0Sstevel@tonic-gate 1282*0Sstevel@tonic-gate if (( dxs = nsldapi_getdxbyname( domain )) == NULL ) { 1283*0Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, LDAP_NO_MEMORY, NULL, NULL ); 1284*0Sstevel@tonic-gate return( NULL ); 1285*0Sstevel@tonic-gate } 1286*0Sstevel@tonic-gate 1287*0Sstevel@tonic-gate srvlist = NULL; 1288*0Sstevel@tonic-gate 1289*0Sstevel@tonic-gate for ( i = 0; dxs[ i ] != NULL; ++i ) { 1290*0Sstevel@tonic-gate port = LDAP_PORT; 1291*0Sstevel@tonic-gate server_dn = NULL; 1292*0Sstevel@tonic-gate if ( strchr( dxs[ i ], ':' ) == NULL ) { 1293*0Sstevel@tonic-gate host = dxs[ i ]; 1294*0Sstevel@tonic-gate } else if ( strlen( dxs[ i ] ) >= 7 && 1295*0Sstevel@tonic-gate strncmp( dxs[ i ], "ldap://", 7 ) == 0 ) { 1296*0Sstevel@tonic-gate host = dxs[ i ] + 7; 1297*0Sstevel@tonic-gate if (( p = strchr( host, ':' )) == NULL ) { 1298*0Sstevel@tonic-gate p = host; 1299*0Sstevel@tonic-gate } else { 1300*0Sstevel@tonic-gate *p++ = '\0'; 1301*0Sstevel@tonic-gate port = atoi( p ); 1302*0Sstevel@tonic-gate } 1303*0Sstevel@tonic-gate if (( p = strchr( p, '/' )) != NULL ) { 1304*0Sstevel@tonic-gate server_dn = ++p; 1305*0Sstevel@tonic-gate if ( *server_dn == '\0' ) { 1306*0Sstevel@tonic-gate server_dn = NULL; 1307*0Sstevel@tonic-gate } 1308*0Sstevel@tonic-gate } 1309*0Sstevel@tonic-gate } else { 1310*0Sstevel@tonic-gate host = NULL; 1311*0Sstevel@tonic-gate } 1312*0Sstevel@tonic-gate 1313*0Sstevel@tonic-gate if ( host != NULL ) { /* found a server we can use */ 1314*0Sstevel@tonic-gate if (( srv = (LDAPServer *)NSLDAPI_CALLOC( 1, 1315*0Sstevel@tonic-gate sizeof( LDAPServer ))) == NULL ) { 1316*0Sstevel@tonic-gate free_servers( srvlist ); 1317*0Sstevel@tonic-gate srvlist = NULL; 1318*0Sstevel@tonic-gate break; /* exit loop & return */ 1319*0Sstevel@tonic-gate } 1320*0Sstevel@tonic-gate 1321*0Sstevel@tonic-gate /* add to end of list of servers */ 1322*0Sstevel@tonic-gate if ( srvlist == NULL ) { 1323*0Sstevel@tonic-gate srvlist = srv; 1324*0Sstevel@tonic-gate } else { 1325*0Sstevel@tonic-gate prevsrv->lsrv_next = srv; 1326*0Sstevel@tonic-gate } 1327*0Sstevel@tonic-gate prevsrv = srv; 1328*0Sstevel@tonic-gate 1329*0Sstevel@tonic-gate /* copy in info. */ 1330*0Sstevel@tonic-gate if (( srv->lsrv_host = nsldapi_strdup( host )) == NULL 1331*0Sstevel@tonic-gate || ( server_dn != NULL && ( srv->lsrv_dn = 1332*0Sstevel@tonic-gate nsldapi_strdup( server_dn )) == NULL )) { 1333*0Sstevel@tonic-gate free_servers( srvlist ); 1334*0Sstevel@tonic-gate srvlist = NULL; 1335*0Sstevel@tonic-gate break; /* exit loop & return */ 1336*0Sstevel@tonic-gate } 1337*0Sstevel@tonic-gate srv->lsrv_port = port; 1338*0Sstevel@tonic-gate } 1339*0Sstevel@tonic-gate } 1340*0Sstevel@tonic-gate 1341*0Sstevel@tonic-gate ldap_value_free( dxs ); 1342*0Sstevel@tonic-gate 1343*0Sstevel@tonic-gate if ( srvlist == NULL ) { 1344*0Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, LDAP_SERVER_DOWN, NULL, NULL ); 1345*0Sstevel@tonic-gate } 1346*0Sstevel@tonic-gate 1347*0Sstevel@tonic-gate return( srvlist ); 1348*0Sstevel@tonic-gate } 1349*0Sstevel@tonic-gate #endif /* LDAP_DNS */ 1350