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