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