xref: /onnv-gate/usr/src/lib/libldap4/common/result.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 1998-2003 Sun Microsystems, Inc.  All rights reserved.
3*0Sstevel@tonic-gate  * Use is subject to license terms.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
7*0Sstevel@tonic-gate 
8*0Sstevel@tonic-gate /*
9*0Sstevel@tonic-gate  *  Copyright (c) 1990 Regents of the University of Michigan.
10*0Sstevel@tonic-gate  *  All rights reserved.
11*0Sstevel@tonic-gate  *
12*0Sstevel@tonic-gate  *  result.c - wait for an ldap result
13*0Sstevel@tonic-gate  */
14*0Sstevel@tonic-gate 
15*0Sstevel@tonic-gate #ifndef lint
16*0Sstevel@tonic-gate static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
17*0Sstevel@tonic-gate #endif
18*0Sstevel@tonic-gate 
19*0Sstevel@tonic-gate #include <stdio.h>
20*0Sstevel@tonic-gate #include <string.h>
21*0Sstevel@tonic-gate #ifdef MACOS
22*0Sstevel@tonic-gate #include <stdlib.h>
23*0Sstevel@tonic-gate #include <time.h>
24*0Sstevel@tonic-gate #include "macos.h"
25*0Sstevel@tonic-gate #else /* MACOS */
26*0Sstevel@tonic-gate #if defined( DOS ) || defined( _WIN32 )
27*0Sstevel@tonic-gate #include <time.h>
28*0Sstevel@tonic-gate #include "msdos.h"
29*0Sstevel@tonic-gate #ifdef PCNFS
30*0Sstevel@tonic-gate #include <tklib.h>
31*0Sstevel@tonic-gate #include <tk_errno.h>
32*0Sstevel@tonic-gate #include <bios.h>
33*0Sstevel@tonic-gate #endif /* PCNFS */
34*0Sstevel@tonic-gate #ifdef NCSA
35*0Sstevel@tonic-gate #include "externs.h"
36*0Sstevel@tonic-gate #endif /* NCSA */
37*0Sstevel@tonic-gate #else /* DOS */
38*0Sstevel@tonic-gate #include <sys/time.h>
39*0Sstevel@tonic-gate #include <sys/types.h>
40*0Sstevel@tonic-gate #include <sys/socket.h>
41*0Sstevel@tonic-gate #include <errno.h>
42*0Sstevel@tonic-gate #ifdef _AIX
43*0Sstevel@tonic-gate #include <sys/select.h>
44*0Sstevel@tonic-gate #endif /* _AIX */
45*0Sstevel@tonic-gate #include "portable.h"
46*0Sstevel@tonic-gate #endif /* DOS */
47*0Sstevel@tonic-gate #endif /* MACOS */
48*0Sstevel@tonic-gate #ifdef VMS
49*0Sstevel@tonic-gate #include "ucx_select.h"
50*0Sstevel@tonic-gate #endif
51*0Sstevel@tonic-gate #include "lber.h"
52*0Sstevel@tonic-gate #include "ldap.h"
53*0Sstevel@tonic-gate #include "ldap-private.h"
54*0Sstevel@tonic-gate #include "ldap-int.h"
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate #ifdef USE_SYSCONF
57*0Sstevel@tonic-gate #include <unistd.h>
58*0Sstevel@tonic-gate #endif /* USE_SYSCONF */
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate #ifdef NEEDPROTOS
61*0Sstevel@tonic-gate static int ldap_abandoned( LDAP *ld, int msgid );
62*0Sstevel@tonic-gate static int ldap_mark_abandoned( LDAP *ld, int msgid );
63*0Sstevel@tonic-gate static int wait4msg( LDAP *ld, int msgid, int all, struct timeval *timeout,
64*0Sstevel@tonic-gate 	LDAPMessage **result );
65*0Sstevel@tonic-gate static int read1msg( LDAP *ld, int msgid, int all, Sockbuf *sb, LDAPConn *lc,
66*0Sstevel@tonic-gate 	LDAPMessage **result );
67*0Sstevel@tonic-gate static int build_result_ber( LDAP *ld, BerElement *ber, LDAPRequest *lr );
68*0Sstevel@tonic-gate static void merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr );
69*0Sstevel@tonic-gate #ifdef CLDAP
70*0Sstevel@tonic-gate static int ldap_select1( LDAP *ld, struct timeval *timeout );
71*0Sstevel@tonic-gate #endif
72*0Sstevel@tonic-gate static int Ref_AddToRequest(LDAPRequest *lr, char **refs);
73*0Sstevel@tonic-gate static void Ref_FreeAll(LDAPRequest *lr);
74*0Sstevel@tonic-gate #else /* NEEDPROTOS */
75*0Sstevel@tonic-gate static int ldap_abandoned();
76*0Sstevel@tonic-gate static int ldap_mark_abandoned();
77*0Sstevel@tonic-gate static int wait4msg();
78*0Sstevel@tonic-gate static int read1msg();
79*0Sstevel@tonic-gate static int build_result_ber();
80*0Sstevel@tonic-gate static void merge_error_info();
81*0Sstevel@tonic-gate #ifdef CLDAP
82*0Sstevel@tonic-gate static int ldap_select1();
83*0Sstevel@tonic-gate #endif
84*0Sstevel@tonic-gate #endif /* NEEDPROTOS */
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate #if !defined( MACOS ) && !defined( DOS )
87*0Sstevel@tonic-gate extern int	errno;
88*0Sstevel@tonic-gate #endif
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate /*
91*0Sstevel@tonic-gate  * ldap_result - wait for an ldap result response to a message from the
92*0Sstevel@tonic-gate  * ldap server.  If msgid is -1, any message will be accepted, otherwise
93*0Sstevel@tonic-gate  * ldap_result will wait for a response with msgid.
94*0Sstevel@tonic-gate  * If all is LDAP_MSG_ONE the first message with id msgid will be accepted.
95*0Sstevel@tonic-gate  * If all is LDAP_MSG_RECEIVED, the received messages with the id msgid will
96*0Sstevel@tonic-gate  * be accepted.
97*0Sstevel@tonic-gate  * Otherwise, ldap_result will wait for all responses with id msgid and
98*0Sstevel@tonic-gate  * then return a pointer to the entire list of messages.  This is only
99*0Sstevel@tonic-gate  * useful for search responses, which can be of 3 message types (zero or
100*0Sstevel@tonic-gate  * more entries, zero or more references, one or more results).  The type
101*0Sstevel@tonic-gate  * of the first message* received is returned.
102*0Sstevel@tonic-gate  * When waiting, any messages that have been abandoned are discarded.
103*0Sstevel@tonic-gate  *
104*0Sstevel@tonic-gate  * Example:
105*0Sstevel@tonic-gate  *	ldap_result( s, msgid, all, timeout, result )
106*0Sstevel@tonic-gate  */
107*0Sstevel@tonic-gate int
ldap_result(LDAP * ld,int msgid,int all,struct timeval * timeout,LDAPMessage ** result)108*0Sstevel@tonic-gate ldap_result( LDAP *ld, int msgid, int all, struct timeval *timeout,
109*0Sstevel@tonic-gate 	LDAPMessage **result )
110*0Sstevel@tonic-gate {
111*0Sstevel@tonic-gate 	LDAPMessage	*lm, *lastlm, *nextlm;
112*0Sstevel@tonic-gate 	int rv;
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 	/*
115*0Sstevel@tonic-gate 	 * First, look through the list of responses we have received on
116*0Sstevel@tonic-gate 	 * this association and see if the response we're interested in
117*0Sstevel@tonic-gate 	 * is there.  If it is, return it.  If not, call wait4msg() to
118*0Sstevel@tonic-gate 	 * wait until it arrives or timeout occurs.
119*0Sstevel@tonic-gate 	 */
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate #ifdef _REENTRANT
122*0Sstevel@tonic-gate 	LOCK_RESPONSE(ld);
123*0Sstevel@tonic-gate 	LOCK_LDAP(ld);
124*0Sstevel@tonic-gate #endif
125*0Sstevel@tonic-gate 	Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 223, "ldap_result\n"), 0, 0, 0 );
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate 	*result = NULLMSG;
128*0Sstevel@tonic-gate 	lastlm = NULLMSG;
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate 	/* look in the received responses */
131*0Sstevel@tonic-gate 	for ( lm = ld->ld_responses; lm != NULLMSG; lm = nextlm ) {
132*0Sstevel@tonic-gate 		nextlm = lm->lm_next;
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 		/* if the msg has been abandonned, free it */
135*0Sstevel@tonic-gate 		if ( ldap_abandoned( ld, lm->lm_msgid ) ) {
136*0Sstevel@tonic-gate 			ldap_mark_abandoned( ld, lm->lm_msgid );
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate 			if ( lastlm == NULLMSG ) {
139*0Sstevel@tonic-gate 				ld->ld_responses = lm->lm_next;
140*0Sstevel@tonic-gate 			} else {
141*0Sstevel@tonic-gate 				lastlm->lm_next = nextlm;
142*0Sstevel@tonic-gate 			}
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 			ldap_msgfree( lm );
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate 			continue;
147*0Sstevel@tonic-gate 		}
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 		if ( msgid == LDAP_RES_ANY || lm->lm_msgid == msgid ) {
150*0Sstevel@tonic-gate 			LDAPMessage	*tmp;
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 			/* If return ONE or RECEIVED message(s) or not a search result, return lm */
153*0Sstevel@tonic-gate 			if ( all == LDAP_MSG_ONE || all == LDAP_MSG_RECEIVED
154*0Sstevel@tonic-gate 				 || (lm->lm_msgtype != LDAP_RES_SEARCH_RESULT
155*0Sstevel@tonic-gate 					&& lm->lm_msgtype != LDAP_RES_SEARCH_ENTRY
156*0Sstevel@tonic-gate 					&& lm->lm_msgtype != LDAP_RES_SEARCH_REFERENCE) )
157*0Sstevel@tonic-gate 				break;
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 			/* Search in the set of messages if one is a search result */
160*0Sstevel@tonic-gate 			for ( tmp = lm; tmp != NULLMSG; tmp = tmp->lm_chain ) {
161*0Sstevel@tonic-gate 				if ( tmp->lm_msgtype == LDAP_RES_SEARCH_RESULT )
162*0Sstevel@tonic-gate 					break;
163*0Sstevel@tonic-gate 			}
164*0Sstevel@tonic-gate 			/* No, well wait for the result message */
165*0Sstevel@tonic-gate 			if ( tmp == NULLMSG ) {
166*0Sstevel@tonic-gate #ifdef _REENTRANT
167*0Sstevel@tonic-gate 				UNLOCK_LDAP(ld);
168*0Sstevel@tonic-gate #endif
169*0Sstevel@tonic-gate 				rv = wait4msg( ld, msgid, all, timeout, result );
170*0Sstevel@tonic-gate #ifdef _REENTRANT
171*0Sstevel@tonic-gate 				UNLOCK_RESPONSE(ld);
172*0Sstevel@tonic-gate #endif
173*0Sstevel@tonic-gate 				return( rv );
174*0Sstevel@tonic-gate 			}
175*0Sstevel@tonic-gate 			/* Here we have the Search result pointed by tmp */
176*0Sstevel@tonic-gate 			break;
177*0Sstevel@tonic-gate 		}
178*0Sstevel@tonic-gate 		/* Check next response */
179*0Sstevel@tonic-gate 		lastlm = lm;
180*0Sstevel@tonic-gate 	}
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate 	/* No response matching found : Wait for one  */
183*0Sstevel@tonic-gate 	if ( lm == NULLMSG ) {
184*0Sstevel@tonic-gate #ifdef _REENTRANT
185*0Sstevel@tonic-gate 		UNLOCK_LDAP(ld);
186*0Sstevel@tonic-gate #endif
187*0Sstevel@tonic-gate 		rv = wait4msg( ld, msgid, all, timeout, result );
188*0Sstevel@tonic-gate #ifdef _REENTRANT
189*0Sstevel@tonic-gate 		UNLOCK_RESPONSE(ld);
190*0Sstevel@tonic-gate #endif
191*0Sstevel@tonic-gate 		return( rv );
192*0Sstevel@tonic-gate 	}
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate 	/* lm points to the message (chain) to return */
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate 	/* Remove message to return from ld_responses list */
197*0Sstevel@tonic-gate 	if ( lastlm == NULLMSG ) {
198*0Sstevel@tonic-gate 		if (all == LDAP_MSG_ONE && lm->lm_chain != NULLMSG){
199*0Sstevel@tonic-gate 			ld->ld_responses = lm->lm_chain;
200*0Sstevel@tonic-gate 		} else {
201*0Sstevel@tonic-gate 			ld->ld_responses = lm->lm_next;
202*0Sstevel@tonic-gate 		}
203*0Sstevel@tonic-gate 	} else {
204*0Sstevel@tonic-gate 		if (all == LDAP_MSG_ONE && lm->lm_chain != NULLMSG) {
205*0Sstevel@tonic-gate 			lastlm->lm_next = lm->lm_chain;
206*0Sstevel@tonic-gate 		} else {
207*0Sstevel@tonic-gate 			lastlm->lm_next = lm->lm_next;
208*0Sstevel@tonic-gate 		}
209*0Sstevel@tonic-gate 	}
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate 	if ( all == LDAP_MSG_ONE )
212*0Sstevel@tonic-gate 		lm->lm_chain = NULLMSG;
213*0Sstevel@tonic-gate 	/* Otherwise return the whole chain */
214*0Sstevel@tonic-gate 	/* No reponses attached */
215*0Sstevel@tonic-gate 	lm->lm_next = NULLMSG;
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate 	*result = lm;
218*0Sstevel@tonic-gate 	ld->ld_errno = LDAP_SUCCESS;
219*0Sstevel@tonic-gate 	rv = lm->lm_msgtype;
220*0Sstevel@tonic-gate #ifdef _REENTRANT
221*0Sstevel@tonic-gate 	UNLOCK_LDAP(ld);
222*0Sstevel@tonic-gate 	UNLOCK_RESPONSE(ld);
223*0Sstevel@tonic-gate #endif
224*0Sstevel@tonic-gate 	return( rv );
225*0Sstevel@tonic-gate }
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate static int
wait4msg(LDAP * ld,int msgid,int all,struct timeval * timeout,LDAPMessage ** result)228*0Sstevel@tonic-gate wait4msg( LDAP *ld, int msgid, int all, struct timeval *timeout,
229*0Sstevel@tonic-gate 	LDAPMessage **result )
230*0Sstevel@tonic-gate {
231*0Sstevel@tonic-gate 	int		rc;
232*0Sstevel@tonic-gate 	struct timeval	tv, *tvp;
233*0Sstevel@tonic-gate 	time_t		start_time, tmp_time;
234*0Sstevel@tonic-gate 	LDAPConn	*lc, *nextlc;
235*0Sstevel@tonic-gate 
236*0Sstevel@tonic-gate #ifdef LDAP_DEBUG
237*0Sstevel@tonic-gate 	if ( timeout == NULL ) {
238*0Sstevel@tonic-gate 		Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 224, "wait4msg (infinite timeout)\n"),
239*0Sstevel@tonic-gate 		    0, 0, 0 );
240*0Sstevel@tonic-gate 	} else {
241*0Sstevel@tonic-gate 		Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 225, "wait4msg (timeout %1$ld sec, %2$ld usec)\n"),
242*0Sstevel@tonic-gate 		    timeout->tv_sec, timeout->tv_usec, 0 );
243*0Sstevel@tonic-gate 	}
244*0Sstevel@tonic-gate #endif /* LDAP_DEBUG */
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate 	if ( timeout == NULL ) {
247*0Sstevel@tonic-gate 		tvp = NULL;
248*0Sstevel@tonic-gate 	} else {
249*0Sstevel@tonic-gate 		tv = *timeout;
250*0Sstevel@tonic-gate 		tvp = &tv;
251*0Sstevel@tonic-gate 		start_time = time( NULL );
252*0Sstevel@tonic-gate 	}
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate 	rc = -2;
255*0Sstevel@tonic-gate 	while ( rc == -2 ) {
256*0Sstevel@tonic-gate #ifdef LDAP_DEBUG
257*0Sstevel@tonic-gate 		if ( ldap_debug & LDAP_DEBUG_TRACE ) {
258*0Sstevel@tonic-gate 			dump_connection( ld, ld->ld_conns, 1 );
259*0Sstevel@tonic-gate 			dump_requests_and_responses( ld );
260*0Sstevel@tonic-gate 		}
261*0Sstevel@tonic-gate #endif /* LDAP_DEBUG */
262*0Sstevel@tonic-gate 		for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
263*0Sstevel@tonic-gate 			if ( lc->lconn_sb->sb_ber.ber_ptr <
264*0Sstevel@tonic-gate 			    lc->lconn_sb->sb_ber.ber_end ) {
265*0Sstevel@tonic-gate 				/* A Message is available, decode and process it */
266*0Sstevel@tonic-gate 				rc = read1msg( ld, msgid, all, lc->lconn_sb,
267*0Sstevel@tonic-gate 				    lc, result );
268*0Sstevel@tonic-gate 				break;
269*0Sstevel@tonic-gate 			}
270*0Sstevel@tonic-gate 		}
271*0Sstevel@tonic-gate 		/* There was no message available : Wait for one */
272*0Sstevel@tonic-gate 		if ( lc == NULL ) {
273*0Sstevel@tonic-gate 			rc = do_ldap_select( ld, tvp );
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate 
276*0Sstevel@tonic-gate #if defined( LDAP_DEBUG ) && !defined( MACOS ) && !defined( DOS )
277*0Sstevel@tonic-gate 			if ( rc == -1 ) {
278*0Sstevel@tonic-gate 			    Debug( LDAP_DEBUG_TRACE,
279*0Sstevel@tonic-gate 				    catgets(slapdcat, 1, 226, "do_ldap_select returned -1: errno %d\n"),
280*0Sstevel@tonic-gate 				    errno, 0, 0 );
281*0Sstevel@tonic-gate 			}
282*0Sstevel@tonic-gate #endif
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate #if !defined( MACOS ) && !defined( DOS )
285*0Sstevel@tonic-gate 			if ( rc == 0 || ( rc == -1 && (ld->ld_restart || errno != EINTR ))) {
286*0Sstevel@tonic-gate #else
287*0Sstevel@tonic-gate 			if ( rc == -1 || rc == 0 ) {
288*0Sstevel@tonic-gate #endif
289*0Sstevel@tonic-gate 				ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
290*0Sstevel@tonic-gate 				    LDAP_TIMEOUT);
291*0Sstevel@tonic-gate 				if ( rc == -1 ) {
292*0Sstevel@tonic-gate #ifdef _REENTRANT
293*0Sstevel@tonic-gate 					LOCK_LDAP(ld);
294*0Sstevel@tonic-gate #endif
295*0Sstevel@tonic-gate 					nsldapi_connection_lost_nolock( ld, NULL);
296*0Sstevel@tonic-gate #ifdef _REENTRANT
297*0Sstevel@tonic-gate 					UNLOCK_LDAP(ld);
298*0Sstevel@tonic-gate #endif
299*0Sstevel@tonic-gate 				}
300*0Sstevel@tonic-gate 				return( rc );
301*0Sstevel@tonic-gate 			}
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate 			if ( rc == -1 ) {
304*0Sstevel@tonic-gate 				rc = -2;	/* select interrupted: Continue the loop */
305*0Sstevel@tonic-gate 			} else {
306*0Sstevel@tonic-gate 				rc = -2;
307*0Sstevel@tonic-gate 				for ( lc = ld->ld_conns; rc == -2 && lc != NULL;
308*0Sstevel@tonic-gate 				    lc = nextlc ) {
309*0Sstevel@tonic-gate 					nextlc = lc->lconn_next;
310*0Sstevel@tonic-gate 					if ( lc->lconn_status == LDAP_CONNST_CONNECTED) {
311*0Sstevel@tonic-gate 						/* Check on each connection. */
312*0Sstevel@tonic-gate 						long is_ready = is_read_ready( ld, lc->lconn_sb );
313*0Sstevel@tonic-gate 
314*0Sstevel@tonic-gate 						if (is_ready > 0) {
315*0Sstevel@tonic-gate 							/* A Message is available, decode and process it */
316*0Sstevel@tonic-gate 							rc = read1msg( ld, msgid, all,
317*0Sstevel@tonic-gate 										   lc->lconn_sb, lc, result );
318*0Sstevel@tonic-gate 						} else if ( is_ready < 0){
319*0Sstevel@tonic-gate 							/* Error in the select : what to do in here ? */
320*0Sstevel@tonic-gate 							/* So far : */
321*0Sstevel@tonic-gate 							rc = -1;
322*0Sstevel@tonic-gate 						}
323*0Sstevel@tonic-gate 					}
324*0Sstevel@tonic-gate 				}
325*0Sstevel@tonic-gate 			}
326*0Sstevel@tonic-gate 		}
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate 		if ( rc == -2 && tvp != NULL ) {
329*0Sstevel@tonic-gate 			tmp_time = time( NULL );
330*0Sstevel@tonic-gate 			if (( tv.tv_sec -=  ( tmp_time - start_time )) <= 0 ) {
331*0Sstevel@tonic-gate 				/* At this point if all == LDAP_MSG_RECEIVED, we must
332*0Sstevel@tonic-gate 				   return all available messages for the msgid */
333*0Sstevel@tonic-gate 				if (all == LDAP_MSG_RECEIVED) {
334*0Sstevel@tonic-gate 					/* Search in responses if some have the correct id */
335*0Sstevel@tonic-gate 					/* if yes return the chain */
336*0Sstevel@tonic-gate 					/* Otherwise return timeout */
337*0Sstevel@tonic-gate 					break;
338*0Sstevel@tonic-gate 				}
339*0Sstevel@tonic-gate 
340*0Sstevel@tonic-gate 				rc = 0;	/* timed out */
341*0Sstevel@tonic-gate 				ld->ld_errno = LDAP_TIMEOUT;
342*0Sstevel@tonic-gate 				break;
343*0Sstevel@tonic-gate 			}
344*0Sstevel@tonic-gate 
345*0Sstevel@tonic-gate 			Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 227, "wait4msg:  %ld secs to go\n"),
346*0Sstevel@tonic-gate 				tv.tv_sec, 0, 0 );
347*0Sstevel@tonic-gate 			start_time = tmp_time;
348*0Sstevel@tonic-gate 		}
349*0Sstevel@tonic-gate 	}
350*0Sstevel@tonic-gate 
351*0Sstevel@tonic-gate 	return( rc );
352*0Sstevel@tonic-gate }
353*0Sstevel@tonic-gate 
354*0Sstevel@tonic-gate 
355*0Sstevel@tonic-gate static int
356*0Sstevel@tonic-gate read1msg( LDAP *ld, int msgid, int all, Sockbuf *sb,
357*0Sstevel@tonic-gate     LDAPConn *lc,
358*0Sstevel@tonic-gate     LDAPMessage **result )
359*0Sstevel@tonic-gate {
360*0Sstevel@tonic-gate 	BerElement	ber;
361*0Sstevel@tonic-gate 	LDAPMessage	*new, *L_res, *l, *prev, *tmp;
362*0Sstevel@tonic-gate 	int		id;
363*0Sstevel@tonic-gate 	unsigned int	tag, atag, len;
364*0Sstevel@tonic-gate 	int		foundit = 0;
365*0Sstevel@tonic-gate 	LDAPRequest	*lr, *lrparent;
366*0Sstevel@tonic-gate 	LDAPRef *theReferences;
367*0Sstevel@tonic-gate 	BerElement	tmpber;
368*0Sstevel@tonic-gate 	int		rc, refer_cnt, hadref, simple_request, samereq = 0, total_count;
369*0Sstevel@tonic-gate 	int retcode;
370*0Sstevel@tonic-gate 	int theErrCode = LDAP_SUCCESS;
371*0Sstevel@tonic-gate 	unsigned int	lderr;
372*0Sstevel@tonic-gate 	char *msgtypestr;
373*0Sstevel@tonic-gate 	char ** theRefs = NULL;
374*0Sstevel@tonic-gate 	char * theOid = NULL;
375*0Sstevel@tonic-gate 	char *lddn, *lderrmsg;
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate 	Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 228, "read1msg\n"), 0, 0, 0 );
378*0Sstevel@tonic-gate 
379*0Sstevel@tonic-gate read_from_sb:
380*0Sstevel@tonic-gate 	lderr = LDAP_SUCCESS; /* Be optimistic */
381*0Sstevel@tonic-gate 
382*0Sstevel@tonic-gate 	ber_zero_init( &ber, 0 );
383*0Sstevel@tonic-gate 	set_ber_options( ld, &ber );
384*0Sstevel@tonic-gate 
385*0Sstevel@tonic-gate 	/* get the next message */
386*0Sstevel@tonic-gate 	if ( (tag = ber_get_next( sb, &len, &ber ))
387*0Sstevel@tonic-gate 	    != LDAP_TAG_MESSAGE ) {
388*0Sstevel@tonic-gate 		ld->ld_errno = (tag == LBER_DEFAULT ? LDAP_SERVER_DOWN :
389*0Sstevel@tonic-gate 		    LDAP_LOCAL_ERROR);
390*0Sstevel@tonic-gate 		if ( tag == LBER_DEFAULT ) {
391*0Sstevel@tonic-gate #ifdef _REENTRANT
392*0Sstevel@tonic-gate 			LOCK_LDAP(ld);
393*0Sstevel@tonic-gate #endif
394*0Sstevel@tonic-gate 			nsldapi_connection_lost_nolock( ld, sb );
395*0Sstevel@tonic-gate #ifdef _REENTRANT
396*0Sstevel@tonic-gate 			UNLOCK_LDAP(ld);
397*0Sstevel@tonic-gate #endif
398*0Sstevel@tonic-gate 		}
399*0Sstevel@tonic-gate 		return( -1 );
400*0Sstevel@tonic-gate 	}
401*0Sstevel@tonic-gate 
402*0Sstevel@tonic-gate 	/* message id */
403*0Sstevel@tonic-gate 	if ( ber_get_int( &ber, &id ) == LBER_ERROR ) {
404*0Sstevel@tonic-gate 		ld->ld_errno = LDAP_DECODING_ERROR;
405*0Sstevel@tonic-gate 		return( -1 );
406*0Sstevel@tonic-gate 	}
407*0Sstevel@tonic-gate 
408*0Sstevel@tonic-gate 	/* if it's been abandoned, toss it */
409*0Sstevel@tonic-gate 	if ( ldap_abandoned( ld, (int)id ) ) {
410*0Sstevel@tonic-gate 		free( ber.ber_buf );	/* gack! */
411*0Sstevel@tonic-gate 		return( -2 );	/* continue looking */
412*0Sstevel@tonic-gate 	}
413*0Sstevel@tonic-gate 
414*0Sstevel@tonic-gate 	/* the message type */
415*0Sstevel@tonic-gate 	if ( (tag = ber_peek_tag( &ber, &len )) == LBER_ERROR ) {
416*0Sstevel@tonic-gate 		ld->ld_errno = LDAP_DECODING_ERROR;
417*0Sstevel@tonic-gate 		return( -1 );
418*0Sstevel@tonic-gate 	}
419*0Sstevel@tonic-gate 
420*0Sstevel@tonic-gate 	/* KE
421*0Sstevel@tonic-gate 	 * Treat unsolicited notification if we got one!
422*0Sstevel@tonic-gate 	 * 	id==0
423*0Sstevel@tonic-gate 	 * 	tag==LDAP_RES_EXTENDED
424*0Sstevel@tonic-gate 	 *
425*0Sstevel@tonic-gate 	 * 	resultCode==	protocolError
426*0Sstevel@tonic-gate 	 *							strongAuthRequired
427*0Sstevel@tonic-gate 	 * 						unavailable
428*0Sstevel@tonic-gate 	 *		tag==LDAP_TAG_EXT_RESPNAME
429*0Sstevel@tonic-gate 	 *		response name (oid)==1.3.6.1.1.4.1.1466.20036
430*0Sstevel@tonic-gate 	 * 	no response field
431*0Sstevel@tonic-gate 	 *
432*0Sstevel@tonic-gate 	 * Example:
433*0Sstevel@tonic-gate 	 * --------
434*0Sstevel@tonic-gate 	 * Ber format: 	{iaata}
435*0Sstevel@tonic-gate 	 * which means: 	returnCode dn errorMessage LDAP_TAG_EXT_RESPNAME "1.3.6.1.1.4.1.1466.20036"
436*0Sstevel@tonic-gate 	 */
437*0Sstevel@tonic-gate 	if ( (id==0) && (tag==LDAP_RES_EXTENDED) )
438*0Sstevel@tonic-gate 	{
439*0Sstevel@tonic-gate 		tmpber = ber;
440*0Sstevel@tonic-gate 		if (ber_scanf( &ber, "{iaa", &lderr, &lddn, &lderrmsg) != LBER_ERROR)
441*0Sstevel@tonic-gate 		{
442*0Sstevel@tonic-gate 			if (ber_peek_tag ( &ber, &atag) == LDAP_TAG_EXT_RESPNAME)
443*0Sstevel@tonic-gate 			{
444*0Sstevel@tonic-gate 			  if ( ber_get_stringa( &ber, &theOid) == LBER_ERROR )
445*0Sstevel@tonic-gate 				{
446*0Sstevel@tonic-gate 					ld->ld_errno = LDAP_DECODING_ERROR;
447*0Sstevel@tonic-gate 					return(-1);
448*0Sstevel@tonic-gate 				}
449*0Sstevel@tonic-gate 			}
450*0Sstevel@tonic-gate 			else
451*0Sstevel@tonic-gate 			{
452*0Sstevel@tonic-gate 				ld->ld_errno = LDAP_DECODING_ERROR;
453*0Sstevel@tonic-gate 				return(-1);
454*0Sstevel@tonic-gate 			}
455*0Sstevel@tonic-gate 
456*0Sstevel@tonic-gate 			if (ber_peek_tag ( &ber, &atag) == LDAP_TAG_EXT_RESPONSE)
457*0Sstevel@tonic-gate 			{
458*0Sstevel@tonic-gate 				/* this field must be absent */
459*0Sstevel@tonic-gate 				ld->ld_errno = LDAP_DECODING_ERROR;
460*0Sstevel@tonic-gate 				return(-1);
461*0Sstevel@tonic-gate 			}
462*0Sstevel@tonic-gate 			if ( ber_scanf(&ber, "}")== LBER_ERROR)
463*0Sstevel@tonic-gate 			{
464*0Sstevel@tonic-gate 				ld->ld_errno = LDAP_DECODING_ERROR;
465*0Sstevel@tonic-gate 				return(-1);
466*0Sstevel@tonic-gate 			}
467*0Sstevel@tonic-gate 
468*0Sstevel@tonic-gate 			/* make a new ldap message to return the result */
469*0Sstevel@tonic-gate 			if ( (new = (LDAPMessage *) calloc( 1, sizeof(LDAPMessage) )) == NULL )
470*0Sstevel@tonic-gate 			{
471*0Sstevel@tonic-gate 				ld->ld_errno = LDAP_NO_MEMORY;
472*0Sstevel@tonic-gate 				return(-1);
473*0Sstevel@tonic-gate 			}
474*0Sstevel@tonic-gate 			new->lm_msgid = 0;
475*0Sstevel@tonic-gate 			new->lm_msgtype = tag;
476*0Sstevel@tonic-gate 			new->lm_ber = ber_dup( &tmpber );
477*0Sstevel@tonic-gate 
478*0Sstevel@tonic-gate 			if ( 	(strncmp(theOid, "1.3.6.1.1.4.1.1466.20036", 24)==0) &&
479*0Sstevel@tonic-gate 					(lderr==LDAP_PROTOCOL_ERROR) ||
480*0Sstevel@tonic-gate 					(lderr==LDAP_STRONG_AUTH_REQUIRED) ||
481*0Sstevel@tonic-gate 					(lderr==LDAP_UNAVAILABLE) )
482*0Sstevel@tonic-gate 			{
483*0Sstevel@tonic-gate 				/* make a new ldap message to return the result */
484*0Sstevel@tonic-gate 				if ( (L_res = (LDAPMessage *) calloc( 1, sizeof(LDAPMessage) )) == NULL )
485*0Sstevel@tonic-gate 				{
486*0Sstevel@tonic-gate 					ld->ld_errno = LDAP_NO_MEMORY;
487*0Sstevel@tonic-gate 					return(-1);
488*0Sstevel@tonic-gate 				}
489*0Sstevel@tonic-gate 				L_res->lm_msgid = 0;
490*0Sstevel@tonic-gate 				L_res->lm_msgtype = tag;
491*0Sstevel@tonic-gate 				L_res->lm_ber = ber_dup( &tmpber );
492*0Sstevel@tonic-gate 				*result = L_res;
493*0Sstevel@tonic-gate 
494*0Sstevel@tonic-gate 				/* It is a notice of disconnection
495*0Sstevel@tonic-gate 				 * Return immediatly with an error code to stop
496*0Sstevel@tonic-gate 				 * reading any new message and to prevent the use
497*0Sstevel@tonic-gate 				 */
498*0Sstevel@tonic-gate 				ld->ld_errno = LDAP_SERVER_DOWN;
499*0Sstevel@tonic-gate 				ldap_insert_notif(ld, new);		/* in head */
500*0Sstevel@tonic-gate 				return(-1);
501*0Sstevel@tonic-gate 			}
502*0Sstevel@tonic-gate 			else
503*0Sstevel@tonic-gate 			{
504*0Sstevel@tonic-gate 				/* This is another notification
505*0Sstevel@tonic-gate 				 *	Keep on the processing of received messages
506*0Sstevel@tonic-gate 				 */
507*0Sstevel@tonic-gate 				ldap_add_notif(ld, new);			/* in tail */
508*0Sstevel@tonic-gate 				goto read_from_sb;
509*0Sstevel@tonic-gate 			}
510*0Sstevel@tonic-gate 		}
511*0Sstevel@tonic-gate 		else
512*0Sstevel@tonic-gate 		{
513*0Sstevel@tonic-gate 			Debug(LDAP_DEBUG_ANY, catgets(slapdcat, 1, 1673, "Error while decoding Extended Response message"), NULL, NULL, NULL);
514*0Sstevel@tonic-gate 			ld->ld_errno = LDAP_DECODING_ERROR;
515*0Sstevel@tonic-gate 			return(-1);
516*0Sstevel@tonic-gate 		}
517*0Sstevel@tonic-gate 	}
518*0Sstevel@tonic-gate 	else if (( lr = find_request_by_msgid( ld, id )) == NULL )
519*0Sstevel@tonic-gate 	{
520*0Sstevel@tonic-gate 		Debug( LDAP_DEBUG_ANY, catgets(slapdcat, 1, 229, "no request for response with msgid %ld (tossing)\n"), id, 0, 0 );
521*0Sstevel@tonic-gate 		free( ber.ber_buf );	/* gack! */
522*0Sstevel@tonic-gate 		return( -2 );	/* continue looking */
523*0Sstevel@tonic-gate 	}
524*0Sstevel@tonic-gate 
525*0Sstevel@tonic-gate 	if (tag == LDAP_RES_SEARCH_ENTRY)
526*0Sstevel@tonic-gate 		msgtypestr = catgets(slapdcat, 1, 1281, "search entry");
527*0Sstevel@tonic-gate 	else if (tag == LDAP_RES_SEARCH_REFERENCE)
528*0Sstevel@tonic-gate 		msgtypestr = catgets(slapdcat, 1, 1282, "search reference");
529*0Sstevel@tonic-gate 	else
530*0Sstevel@tonic-gate 		msgtypestr = catgets(slapdcat, 1, 1283, "result");
531*0Sstevel@tonic-gate 
532*0Sstevel@tonic-gate 	Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 230, "got %1$s msgid %2$ld, original id %3$d\n"),
533*0Sstevel@tonic-gate 		   msgtypestr, id, lr->lr_origid );
534*0Sstevel@tonic-gate 
535*0Sstevel@tonic-gate 	id = lr->lr_origid;
536*0Sstevel@tonic-gate 
537*0Sstevel@tonic-gate 	/* REFERRALS HANDLING*/
538*0Sstevel@tonic-gate 	refer_cnt = 0;
539*0Sstevel@tonic-gate 	simple_request = 0;
540*0Sstevel@tonic-gate 	hadref = 0;
541*0Sstevel@tonic-gate 	rc = -2;	/* default is to keep looking (no response found) */
542*0Sstevel@tonic-gate 	lr->lr_res_msgtype = tag;
543*0Sstevel@tonic-gate 
544*0Sstevel@tonic-gate 	if ( tag != LDAP_RES_SEARCH_ENTRY ) { /* If it's not an entry, ie it's a result or a reference */
545*0Sstevel@tonic-gate 		if ( ld->ld_version >= LDAP_VERSION2 &&
546*0Sstevel@tonic-gate 			    ( lr->lr_parent != NULL ||
547*0Sstevel@tonic-gate 				  ld->ld_follow_referral /* || ClientControl to follow referral present */ )) {
548*0Sstevel@tonic-gate 			tmpber = ber;
549*0Sstevel@tonic-gate 			if (tag == LDAP_RES_SEARCH_REFERENCE){
550*0Sstevel@tonic-gate 				/* LDAP V3 reference. Decode it */
551*0Sstevel@tonic-gate 				Debug(LDAP_DEBUG_TRACE, catgets(slapdcat, 1, -1, "LDAP search reference received. Will follow it later\n"),
552*0Sstevel@tonic-gate 					  0, 0,0);
553*0Sstevel@tonic-gate 				if (ber_scanf(&tmpber, "{v}", &theRefs) == LBER_ERROR){
554*0Sstevel@tonic-gate 					Debug ( LDAP_DEBUG_ANY, catgets(slapdcat, 1, 1284, "Error while decoding Search Reference Result message\n"),
555*0Sstevel@tonic-gate 							NULL, NULL, NULL);
556*0Sstevel@tonic-gate 					rc = -1;
557*0Sstevel@tonic-gate 					theRefs = NULL;
558*0Sstevel@tonic-gate 				} else {
559*0Sstevel@tonic-gate 					/* Store the referrals in request. We will follow them when the result arrives */
560*0Sstevel@tonic-gate 					Ref_AddToRequest(lr, theRefs);
561*0Sstevel@tonic-gate 					theRefs = NULL;
562*0Sstevel@tonic-gate 					free( ber.ber_buf );	/* gack! */
563*0Sstevel@tonic-gate 					ber.ber_buf = NULL;
564*0Sstevel@tonic-gate 					return (rc);
565*0Sstevel@tonic-gate 				}
566*0Sstevel@tonic-gate 			} else {
567*0Sstevel@tonic-gate 				if (ber_scanf( &tmpber, "{iaa", &lderr, &lr->lr_res_matched, &lr->lr_res_error) != LBER_ERROR){
568*0Sstevel@tonic-gate 					if (lderr == LDAP_PARTIAL_RESULTS){
569*0Sstevel@tonic-gate 						Debug(LDAP_DEBUG_TRACE, catgets(slapdcat, 1, -1, "LDAPv2 partial error received\n"), 0, 0,0);
570*0Sstevel@tonic-gate 						/* Ldapv2 referrals */
571*0Sstevel@tonic-gate 						theRefs = ldap_errormsg2referrals(lr->lr_res_error);
572*0Sstevel@tonic-gate 						ber_scanf(&tmpber, "}");
573*0Sstevel@tonic-gate 					} else if (lderr == LDAP_REFERRAL ){
574*0Sstevel@tonic-gate 						/* We have some referrals, decode them */
575*0Sstevel@tonic-gate 						Debug(LDAP_DEBUG_TRACE, catgets(slapdcat, 1, -1, "LDAPv3 referral error received\n"), 0, 0,0);
576*0Sstevel@tonic-gate 						if (ber_peek_tag ( &tmpber, &atag) == LDAP_TAG_REFERRAL){
577*0Sstevel@tonic-gate 							if (ber_scanf(&tmpber, "{v}}", &theRefs) == LBER_ERROR){
578*0Sstevel@tonic-gate 								Debug( LDAP_DEBUG_ANY, catgets(slapdcat, 1, 1285, "Error while decoding referrals in msg\n"),
579*0Sstevel@tonic-gate 									   NULL, NULL, NULL );
580*0Sstevel@tonic-gate 								rc = -1; /* ??? */
581*0Sstevel@tonic-gate 								theRefs = NULL;
582*0Sstevel@tonic-gate 							}
583*0Sstevel@tonic-gate 						} /* else error there should be at least one ref */
584*0Sstevel@tonic-gate 					} else if (((lderr == LDAP_NO_SUCH_OBJECT) ||
585*0Sstevel@tonic-gate 							   (lderr == LDAP_BUSY) ||
586*0Sstevel@tonic-gate 							   (lderr == LDAP_UNAVAILABLE) ||
587*0Sstevel@tonic-gate 							   (lderr == LDAP_SERVER_DOWN) ||
588*0Sstevel@tonic-gate 							   (lderr == LDAP_CONNECT_ERROR)) &&
589*0Sstevel@tonic-gate 							   (lr->lr_parent != NULL) && /* its  subrequest */
590*0Sstevel@tonic-gate 							   (lr->lr_ref_tofollow != NULL)) { /* And it has some other referral to try */
591*0Sstevel@tonic-gate 						samereq = 1;
592*0Sstevel@tonic-gate 						theRefs = lr->lr_ref_tofollow;
593*0Sstevel@tonic-gate 						lr->lr_ref_tofollow = NULL;
594*0Sstevel@tonic-gate 						lrparent = lr->lr_parent;
595*0Sstevel@tonic-gate 						/* delete lr */
596*0Sstevel@tonic-gate 						free_request(ld, lr);
597*0Sstevel@tonic-gate 						/* lr now points on parent request */
598*0Sstevel@tonic-gate 						lr = lrparent;
599*0Sstevel@tonic-gate 						/* Follow referrals */
600*0Sstevel@tonic-gate 					} else {
601*0Sstevel@tonic-gate 						/* Here we have a simple result */
602*0Sstevel@tonic-gate 						hadref = lr->lr_outrefcnt;
603*0Sstevel@tonic-gate 					}
604*0Sstevel@tonic-gate 				} else {
605*0Sstevel@tonic-gate 					Debug( LDAP_DEBUG_ANY, catgets(slapdcat, 1, 1286, "Error while decoding result for request %$1d\n"),
606*0Sstevel@tonic-gate 						   lr->lr_origid, NULL, NULL);
607*0Sstevel@tonic-gate 					rc = -1; /* ??? */
608*0Sstevel@tonic-gate 				}
609*0Sstevel@tonic-gate 			}
610*0Sstevel@tonic-gate 
611*0Sstevel@tonic-gate 			total_count = 0;
612*0Sstevel@tonic-gate 			if (tag != LDAP_RES_SEARCH_REFERENCE && lr->lr_references) {
613*0Sstevel@tonic-gate 				/* Some search references pending... Let's try to chase them */
614*0Sstevel@tonic-gate 				hadref = 1;
615*0Sstevel@tonic-gate 				theReferences = lr->lr_references;
616*0Sstevel@tonic-gate 
617*0Sstevel@tonic-gate 				Debug(LDAP_DEBUG_TRACE, catgets(slapdcat, 1, -1, "Now following the search references received\n"),
618*0Sstevel@tonic-gate 					  0, 0,0);
619*0Sstevel@tonic-gate 
620*0Sstevel@tonic-gate 				while (theReferences != NULL){
621*0Sstevel@tonic-gate 					if ((retcode = chase_referrals(ld, lr, theReferences->lref_refs , &refer_cnt, 0)) != LDAP_SUCCESS) {
622*0Sstevel@tonic-gate 						/* think about what to do */
623*0Sstevel@tonic-gate 						Debug( LDAP_DEBUG_ANY, catgets(slapdcat, 1, -1, "Error while chasing referral (%1$d)\n"),
624*0Sstevel@tonic-gate 							   retcode, NULL, NULL);
625*0Sstevel@tonic-gate 						theErrCode = LDAP_REFERRAL;
626*0Sstevel@tonic-gate 					}
627*0Sstevel@tonic-gate 					if (refer_cnt >= 0)
628*0Sstevel@tonic-gate 						total_count += refer_cnt;
629*0Sstevel@tonic-gate 					theReferences = theReferences->lref_next;
630*0Sstevel@tonic-gate 				}
631*0Sstevel@tonic-gate 				Ref_FreeAll(lr);
632*0Sstevel@tonic-gate 				if (theErrCode != LDAP_SUCCESS) {
633*0Sstevel@tonic-gate 					if (ld->ld_error != NULL && *ld->ld_error) {
634*0Sstevel@tonic-gate 						if (lr->lr_res_error)
635*0Sstevel@tonic-gate 							free(lr->lr_res_error);
636*0Sstevel@tonic-gate 						lr->lr_res_error = strdup(ld->ld_error);
637*0Sstevel@tonic-gate 					}
638*0Sstevel@tonic-gate 				}
639*0Sstevel@tonic-gate 				lr->lr_res_errno = theErrCode;
640*0Sstevel@tonic-gate 			}
641*0Sstevel@tonic-gate 			/* if theRefs != NULL we have some referrals to chase, do it */
642*0Sstevel@tonic-gate 			if (theRefs){
643*0Sstevel@tonic-gate 				hadref = 1;
644*0Sstevel@tonic-gate 				if ((retcode = chase_referrals(ld, lr, theRefs, &refer_cnt, samereq)) != LDAP_SUCCESS){
645*0Sstevel@tonic-gate 					/* think about what to do */
646*0Sstevel@tonic-gate 					Debug( LDAP_DEBUG_ANY, catgets(slapdcat, 1, -1, "Error while chasing referral (%1$d)\n"),
647*0Sstevel@tonic-gate 						   retcode, NULL, NULL);
648*0Sstevel@tonic-gate 				}
649*0Sstevel@tonic-gate 
650*0Sstevel@tonic-gate 				if (refer_cnt >= 0)
651*0Sstevel@tonic-gate 					total_count += refer_cnt;
652*0Sstevel@tonic-gate 
653*0Sstevel@tonic-gate 				ldap_value_free(theRefs);
654*0Sstevel@tonic-gate 				if (samereq){ /* Just tried another referral for same request */
655*0Sstevel@tonic-gate 					free(ber.ber_buf);
656*0Sstevel@tonic-gate 					ber.ber_buf = NULL;
657*0Sstevel@tonic-gate 					rc = -2;
658*0Sstevel@tonic-gate 					/* continue */
659*0Sstevel@tonic-gate 				}
660*0Sstevel@tonic-gate 				if (retcode != LDAP_SUCCESS) {
661*0Sstevel@tonic-gate 					if (ld->ld_version == LDAP_VERSION2){
662*0Sstevel@tonic-gate 						if (lr->lr_res_error)
663*0Sstevel@tonic-gate 							free(lr->lr_res_error);
664*0Sstevel@tonic-gate 						lr->lr_res_error = ldap_referral2error_msg(lr->lr_ref_unfollowed);
665*0Sstevel@tonic-gate 					} else if (ld->ld_error != NULL && *ld->ld_error) {
666*0Sstevel@tonic-gate 						if (lr->lr_res_error)
667*0Sstevel@tonic-gate 							free(lr->lr_res_error);
668*0Sstevel@tonic-gate 						lr->lr_res_error = strdup(ld->ld_error);
669*0Sstevel@tonic-gate 					}
670*0Sstevel@tonic-gate 				}
671*0Sstevel@tonic-gate 				lr->lr_res_errno = ld->ld_errno;
672*0Sstevel@tonic-gate 
673*0Sstevel@tonic-gate 			}  else if (theErrCode == LDAP_SUCCESS) {
674*0Sstevel@tonic-gate 				/* no referral have been chased */
675*0Sstevel@tonic-gate 				lr->lr_res_errno = (lderr == LDAP_PARTIAL_RESULTS || lderr == LDAP_REFERRAL) ? LDAP_SUCCESS : lderr;
676*0Sstevel@tonic-gate 			}
677*0Sstevel@tonic-gate 
678*0Sstevel@tonic-gate 			Debug( LDAP_DEBUG_TRACE,
679*0Sstevel@tonic-gate 				   catgets(slapdcat, 1, 231, "new result:  res_errno: %1$d, res_error: <%2$s>, res_matched: <%3$s>\n"),
680*0Sstevel@tonic-gate 				   lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
681*0Sstevel@tonic-gate 				   lr->lr_res_matched ? lr->lr_res_matched : "" );
682*0Sstevel@tonic-gate 		}
683*0Sstevel@tonic-gate 
684*0Sstevel@tonic-gate 
685*0Sstevel@tonic-gate 		Debug( LDAP_DEBUG_TRACE,
686*0Sstevel@tonic-gate 			   catgets(slapdcat, 1, 232, "read1msg:  %1$d new referrals\n"), total_count, 0, 0 );
687*0Sstevel@tonic-gate 
688*0Sstevel@tonic-gate 		if ( refer_cnt != 0 ) {	/* chasing referrals */
689*0Sstevel@tonic-gate 			free( ber.ber_buf );	/* gack! */
690*0Sstevel@tonic-gate 			ber.ber_buf = NULL;
691*0Sstevel@tonic-gate 			if ( refer_cnt < 0 ) {
692*0Sstevel@tonic-gate 				return( -1 );	/* fatal error */
693*0Sstevel@tonic-gate 			}
694*0Sstevel@tonic-gate 			lr->lr_status = LDAP_REQST_CHASINGREFS;
695*0Sstevel@tonic-gate 		} else if (tag == LDAP_RES_SEARCH_REFERENCE && !ld->ld_follow_referral) {
696*0Sstevel@tonic-gate 			/* We had a ref and we don't follow referral : Do nothing there ?! */
697*0Sstevel@tonic-gate 			Debug( LDAP_DEBUG_TRACE,
698*0Sstevel@tonic-gate 				   catgets(slapdcat, 1, -1, "read1msg: returning search reference\n"), 0, 0, 0 );
699*0Sstevel@tonic-gate 
700*0Sstevel@tonic-gate 		} else {
701*0Sstevel@tonic-gate 			/* No referral chasing */
702*0Sstevel@tonic-gate 			if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
703*0Sstevel@tonic-gate 				/* request without any referrals */
704*0Sstevel@tonic-gate 				simple_request = ( hadref ? 0 : 1 );
705*0Sstevel@tonic-gate 			} else {
706*0Sstevel@tonic-gate 				/* request with referrals or child request */
707*0Sstevel@tonic-gate 				free( ber.ber_buf );	/* gack! */
708*0Sstevel@tonic-gate 				ber.ber_buf = NULL;
709*0Sstevel@tonic-gate 			}
710*0Sstevel@tonic-gate 
711*0Sstevel@tonic-gate 
712*0Sstevel@tonic-gate  			while ( lr->lr_parent != NULL ) {
713*0Sstevel@tonic-gate  				merge_error_info( ld, lr->lr_parent, lr );
714*0Sstevel@tonic-gate  				lr = lr->lr_parent;
715*0Sstevel@tonic-gate  				if ( --lr->lr_outrefcnt > 0 ) {
716*0Sstevel@tonic-gate  					break;	/* not completedly done yet */
717*0Sstevel@tonic-gate  				}
718*0Sstevel@tonic-gate  			}
719*0Sstevel@tonic-gate 
720*0Sstevel@tonic-gate 			if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) { /* The main request has no more outstanding refs */
721*0Sstevel@tonic-gate 				id = lr->lr_msgid;
722*0Sstevel@tonic-gate 				tag = lr->lr_res_msgtype;
723*0Sstevel@tonic-gate 				Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 233, "request %1$ld done\n"),
724*0Sstevel@tonic-gate 				    id, 0, 0 );
725*0Sstevel@tonic-gate 				Debug( LDAP_DEBUG_TRACE,
726*0Sstevel@tonic-gate 					   catgets(slapdcat, 1, 234, "res_errno: %1$d, res_error: <%2$s>, res_matched: <%3$s>\n"),
727*0Sstevel@tonic-gate 					   lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
728*0Sstevel@tonic-gate 					   lr->lr_res_matched ? lr->lr_res_matched : "" );
729*0Sstevel@tonic-gate 				if ( !simple_request ) { /* We have to rebuild the result */
730*0Sstevel@tonic-gate 					if ( ber.ber_buf != NULL ) {
731*0Sstevel@tonic-gate 						free( ber.ber_buf ); /* gack! */
732*0Sstevel@tonic-gate 						ber.ber_buf = NULL;
733*0Sstevel@tonic-gate 					}
734*0Sstevel@tonic-gate 					if ( build_result_ber( ld, &ber, lr )
735*0Sstevel@tonic-gate 					    == LBER_ERROR ) {
736*0Sstevel@tonic-gate 						ld->ld_errno = LDAP_NO_MEMORY;
737*0Sstevel@tonic-gate 						rc = -1; /* fatal error */
738*0Sstevel@tonic-gate 					}
739*0Sstevel@tonic-gate 				}
740*0Sstevel@tonic-gate 
741*0Sstevel@tonic-gate 				free_request( ld, lr );
742*0Sstevel@tonic-gate 			}
743*0Sstevel@tonic-gate 
744*0Sstevel@tonic-gate 			if ( lc != NULL ) {
745*0Sstevel@tonic-gate 				free_connection( ld, lc, 0, 1 );
746*0Sstevel@tonic-gate 			}
747*0Sstevel@tonic-gate 		}
748*0Sstevel@tonic-gate 	}
749*0Sstevel@tonic-gate 
750*0Sstevel@tonic-gate 	if ( ber.ber_buf == NULL ) { /* If the buffer has been freed, return */
751*0Sstevel@tonic-gate 		return( rc );
752*0Sstevel@tonic-gate 	}
753*0Sstevel@tonic-gate 	/* End of REFERRALS */
754*0Sstevel@tonic-gate 
755*0Sstevel@tonic-gate 	/* make a new ldap message */
756*0Sstevel@tonic-gate 	if ( (new = (LDAPMessage *) calloc( 1, sizeof(LDAPMessage) ))
757*0Sstevel@tonic-gate 	    == NULL ) {
758*0Sstevel@tonic-gate 		ld->ld_errno = LDAP_NO_MEMORY;
759*0Sstevel@tonic-gate 		return( -1 );
760*0Sstevel@tonic-gate 	}
761*0Sstevel@tonic-gate 	new->lm_msgid = (int)id;
762*0Sstevel@tonic-gate 	new->lm_msgtype = tag;
763*0Sstevel@tonic-gate 	new->lm_ber = ber_dup( &ber );
764*0Sstevel@tonic-gate 
765*0Sstevel@tonic-gate #ifndef NO_CACHE
766*0Sstevel@tonic-gate 	if ( ld->ld_cache != NULL ) {
767*0Sstevel@tonic-gate 		add_result_to_cache( ld, new );
768*0Sstevel@tonic-gate 	}
769*0Sstevel@tonic-gate #endif /* NO_CACHE */
770*0Sstevel@tonic-gate 
771*0Sstevel@tonic-gate 	/* is this the one we're looking for? */
772*0Sstevel@tonic-gate 	if ( msgid == LDAP_RES_ANY || id == msgid ) {
773*0Sstevel@tonic-gate 		if ( all == LDAP_MSG_ONE	/* all apply only to search, so if not a search,return the val */
774*0Sstevel@tonic-gate 		    || (new->lm_msgtype != LDAP_RES_SEARCH_RESULT
775*0Sstevel@tonic-gate 				&& new->lm_msgtype != LDAP_RES_SEARCH_ENTRY
776*0Sstevel@tonic-gate 				&& new->lm_msgtype != LDAP_RES_SEARCH_REFERENCE) ) {
777*0Sstevel@tonic-gate 			*result = new;
778*0Sstevel@tonic-gate 			ld->ld_errno = LDAP_SUCCESS;
779*0Sstevel@tonic-gate 			return( tag );
780*0Sstevel@tonic-gate 		} else if ( new->lm_msgtype == LDAP_RES_SEARCH_RESULT) {
781*0Sstevel@tonic-gate 			foundit = 1;	/* return the chain later */
782*0Sstevel@tonic-gate 		}
783*0Sstevel@tonic-gate 	}
784*0Sstevel@tonic-gate 
785*0Sstevel@tonic-gate 	/*
786*0Sstevel@tonic-gate 	 * if not, we must add it to the list of responses.  if
787*0Sstevel@tonic-gate 	 * the msgid is already there, it must be part of an existing
788*0Sstevel@tonic-gate 	 * search response.
789*0Sstevel@tonic-gate 	 */
790*0Sstevel@tonic-gate 
791*0Sstevel@tonic-gate 	prev = NULLMSG;
792*0Sstevel@tonic-gate 	for ( l = ld->ld_responses; l != NULLMSG; l = l->lm_next ) {
793*0Sstevel@tonic-gate 		if ( l->lm_msgid == new->lm_msgid )
794*0Sstevel@tonic-gate 			break;
795*0Sstevel@tonic-gate 		prev = l;
796*0Sstevel@tonic-gate 	}
797*0Sstevel@tonic-gate 
798*0Sstevel@tonic-gate 	/* not part of an existing search response */
799*0Sstevel@tonic-gate 	if ( l == NULLMSG ) {
800*0Sstevel@tonic-gate 		if ( foundit ) { /* it a search result anyway, so return it */
801*0Sstevel@tonic-gate 			*result = new;
802*0Sstevel@tonic-gate 			ld->ld_errno = LDAP_SUCCESS;
803*0Sstevel@tonic-gate 			return( tag );
804*0Sstevel@tonic-gate 		}
805*0Sstevel@tonic-gate 
806*0Sstevel@tonic-gate 		new->lm_next = ld->ld_responses;
807*0Sstevel@tonic-gate 		ld->ld_responses = new;
808*0Sstevel@tonic-gate 		return( -2 );	/* continue looking */
809*0Sstevel@tonic-gate 	}
810*0Sstevel@tonic-gate 
811*0Sstevel@tonic-gate 	Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 235, "adding response id %1$d type %2$d:\n"),
812*0Sstevel@tonic-gate 	    new->lm_msgid, new->lm_msgtype, 0 );
813*0Sstevel@tonic-gate 
814*0Sstevel@tonic-gate 	/* part of a search response - add to end of list of entries or references */
815*0Sstevel@tonic-gate 	for ( tmp = l; tmp->lm_chain != NULLMSG &&
816*0Sstevel@tonic-gate 	    (tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY ||
817*0Sstevel@tonic-gate 		 tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_REFERENCE);
818*0Sstevel@tonic-gate 	    tmp = tmp->lm_chain )
819*0Sstevel@tonic-gate 		;	/* NULL */
820*0Sstevel@tonic-gate 	tmp->lm_chain = new;
821*0Sstevel@tonic-gate 
822*0Sstevel@tonic-gate 	/* return the whole chain if that's what we were looking for */
823*0Sstevel@tonic-gate 	if ( foundit ) {
824*0Sstevel@tonic-gate 		if ( prev == NULLMSG )
825*0Sstevel@tonic-gate 			ld->ld_responses = l->lm_next;
826*0Sstevel@tonic-gate 		else
827*0Sstevel@tonic-gate 			prev->lm_next = l->lm_next;
828*0Sstevel@tonic-gate 		*result = l;
829*0Sstevel@tonic-gate 		ld->ld_errno = LDAP_SUCCESS;
830*0Sstevel@tonic-gate 		return( l->lm_msgtype ); /* Patch 16 : was return(tag) */
831*0Sstevel@tonic-gate 	}
832*0Sstevel@tonic-gate 
833*0Sstevel@tonic-gate 	return( -2 );	/* continue looking */
834*0Sstevel@tonic-gate }
835*0Sstevel@tonic-gate 
836*0Sstevel@tonic-gate 
837*0Sstevel@tonic-gate static int
838*0Sstevel@tonic-gate build_result_ber( LDAP *ld, BerElement *ber, LDAPRequest *lr )
839*0Sstevel@tonic-gate {
840*0Sstevel@tonic-gate 	unsigned int	len;
841*0Sstevel@tonic-gate 	int		along;
842*0Sstevel@tonic-gate 
843*0Sstevel@tonic-gate 	Debug (LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 1287, "=> building_ber_error msgid %ld\n"), lr->lr_msgid, 0,0);
844*0Sstevel@tonic-gate 	ber_zero_init( ber, 0 );
845*0Sstevel@tonic-gate 	set_ber_options( ld, ber );
846*0Sstevel@tonic-gate 	if (ld->ld_version == LDAP_VERSION3){
847*0Sstevel@tonic-gate 		if ( ber_printf( ber, "{it{ess",
848*0Sstevel@tonic-gate 						 lr->lr_msgid,
849*0Sstevel@tonic-gate 						 lr->lr_res_msgtype,
850*0Sstevel@tonic-gate 						 lr->lr_res_errno,
851*0Sstevel@tonic-gate 						 lr->lr_res_matched ? lr->lr_res_matched : "",
852*0Sstevel@tonic-gate 						 lr->lr_res_error ? lr->lr_res_error : "" ) == LBER_ERROR){
853*0Sstevel@tonic-gate 			return (LBER_ERROR);
854*0Sstevel@tonic-gate 		}
855*0Sstevel@tonic-gate 		if (lr->lr_res_errno == LDAP_REFERRAL &&
856*0Sstevel@tonic-gate 			ber_printf(ber, "t{v}", LDAP_TAG_REFERRAL, lr->lr_ref_unfollowed) == LBER_ERROR){
857*0Sstevel@tonic-gate 			return (LBER_ERROR);
858*0Sstevel@tonic-gate 		}
859*0Sstevel@tonic-gate 		if (ber_printf(ber, "}}") == LBER_ERROR){
860*0Sstevel@tonic-gate 			return (LBER_ERROR);
861*0Sstevel@tonic-gate 		}
862*0Sstevel@tonic-gate 	} else {
863*0Sstevel@tonic-gate 		if ( ber_printf( ber, "{it{ess}}",
864*0Sstevel@tonic-gate 						 lr->lr_msgid,
865*0Sstevel@tonic-gate 						 lr->lr_res_msgtype,
866*0Sstevel@tonic-gate 						 lr->lr_res_errno,
867*0Sstevel@tonic-gate 						 lr->lr_res_matched ? lr->lr_res_matched : "",
868*0Sstevel@tonic-gate 						 lr->lr_res_error ? lr->lr_res_error : "" ) == LBER_ERROR ) {
869*0Sstevel@tonic-gate 			return( LBER_ERROR );
870*0Sstevel@tonic-gate 		}
871*0Sstevel@tonic-gate 	}
872*0Sstevel@tonic-gate 
873*0Sstevel@tonic-gate 	ber_reset( ber, 1 );
874*0Sstevel@tonic-gate 	if ( ber_skip_tag( ber, &len ) == LBER_ERROR ) {
875*0Sstevel@tonic-gate 		return( LBER_ERROR );
876*0Sstevel@tonic-gate 	}
877*0Sstevel@tonic-gate 
878*0Sstevel@tonic-gate 	if ( ber_get_int( ber, &along ) == LBER_ERROR ) {
879*0Sstevel@tonic-gate 		return( LBER_ERROR );
880*0Sstevel@tonic-gate 	}
881*0Sstevel@tonic-gate 
882*0Sstevel@tonic-gate 	return( ber_peek_tag( ber, &len ));
883*0Sstevel@tonic-gate }
884*0Sstevel@tonic-gate 
885*0Sstevel@tonic-gate 
886*0Sstevel@tonic-gate static void
887*0Sstevel@tonic-gate merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
888*0Sstevel@tonic-gate {
889*0Sstevel@tonic-gate 	int i, j;
890*0Sstevel@tonic-gate /*
891*0Sstevel@tonic-gate  * Merge error information in "lr" with "parentr" error code and string.
892*0Sstevel@tonic-gate  */
893*0Sstevel@tonic-gate 	if ( lr->lr_res_errno == LDAP_PARTIAL_RESULTS ) {
894*0Sstevel@tonic-gate 		parentr->lr_res_errno = lr->lr_res_errno;
895*0Sstevel@tonic-gate 		if ( lr->lr_res_error != NULL ) {
896*0Sstevel@tonic-gate 			(void)append_referral( ld, &parentr->lr_res_error,
897*0Sstevel@tonic-gate 			    lr->lr_res_error );
898*0Sstevel@tonic-gate 		}
899*0Sstevel@tonic-gate 	} else if ( lr->lr_res_errno != LDAP_SUCCESS &&
900*0Sstevel@tonic-gate 	    parentr->lr_res_errno == LDAP_SUCCESS ) {
901*0Sstevel@tonic-gate 		parentr->lr_res_errno = lr->lr_res_errno;
902*0Sstevel@tonic-gate 		if ( parentr->lr_res_error != NULL ) {
903*0Sstevel@tonic-gate 			free( parentr->lr_res_error );
904*0Sstevel@tonic-gate 		}
905*0Sstevel@tonic-gate 		parentr->lr_res_error = lr->lr_res_error;
906*0Sstevel@tonic-gate 		lr->lr_res_error = NULL;
907*0Sstevel@tonic-gate 		if ( NAME_ERROR( lr->lr_res_errno )) {
908*0Sstevel@tonic-gate 			if ( parentr->lr_res_matched != NULL ) {
909*0Sstevel@tonic-gate 				free( parentr->lr_res_matched );
910*0Sstevel@tonic-gate 			}
911*0Sstevel@tonic-gate 			parentr->lr_res_matched = lr->lr_res_matched;
912*0Sstevel@tonic-gate 			lr->lr_res_matched = NULL;
913*0Sstevel@tonic-gate 		}
914*0Sstevel@tonic-gate 		if (lr->lr_ref_unfollowed != NULL){
915*0Sstevel@tonic-gate 			for (i=0;lr->lr_ref_unfollowed[i] != NULL; i++);
916*0Sstevel@tonic-gate 			j = 0;
917*0Sstevel@tonic-gate 			if (parentr->lr_ref_unfollowed != NULL){
918*0Sstevel@tonic-gate 				for (j=0;parentr->lr_ref_unfollowed[j]!= NULL ;j++);
919*0Sstevel@tonic-gate 				j++;
920*0Sstevel@tonic-gate 			}
921*0Sstevel@tonic-gate 			parentr->lr_ref_unfollowed = (char **)realloc (parentr->lr_ref_unfollowed, (j+i+1) * sizeof(char *));
922*0Sstevel@tonic-gate 			if (parentr->lr_ref_unfollowed != NULL){
923*0Sstevel@tonic-gate 				for (i = 0; lr->lr_ref_unfollowed[i] != NULL; i++){
924*0Sstevel@tonic-gate 					parentr->lr_ref_unfollowed[j+i] = lr->lr_ref_unfollowed[i];
925*0Sstevel@tonic-gate 					lr->lr_ref_unfollowed[i] = NULL;
926*0Sstevel@tonic-gate 				}
927*0Sstevel@tonic-gate 				parentr->lr_ref_unfollowed[i+j+1] = NULL;
928*0Sstevel@tonic-gate 			} else {
929*0Sstevel@tonic-gate 				if (parentr->lr_res_errno == LDAP_SUCCESS)
930*0Sstevel@tonic-gate 					parentr->lr_res_errno =  LDAP_NO_MEMORY;
931*0Sstevel@tonic-gate 			}
932*0Sstevel@tonic-gate 		}
933*0Sstevel@tonic-gate 	}
934*0Sstevel@tonic-gate 
935*0Sstevel@tonic-gate 	Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 236, "merged parent (id %1$d) error info:  "),
936*0Sstevel@tonic-gate 	    parentr->lr_msgid, 0, 0 );
937*0Sstevel@tonic-gate 	Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 237, "result errno %1$d, error <%2$s>, matched <%3$s>\n"),
938*0Sstevel@tonic-gate 		   parentr->lr_res_errno,
939*0Sstevel@tonic-gate 		   parentr->lr_res_error ? parentr->lr_res_error : "",
940*0Sstevel@tonic-gate 		   parentr->lr_res_matched ? parentr->lr_res_matched : "" );
941*0Sstevel@tonic-gate }
942*0Sstevel@tonic-gate 
943*0Sstevel@tonic-gate #ifdef CLDAP
944*0Sstevel@tonic-gate #if !defined( MACOS ) && !defined( DOS ) && !defined( _WIN32 )
945*0Sstevel@tonic-gate static int
946*0Sstevel@tonic-gate ldap_select1( LDAP *ld, struct timeval *timeout )
947*0Sstevel@tonic-gate {
948*0Sstevel@tonic-gate 	fd_set		readfds;
949*0Sstevel@tonic-gate 	static int	tblsize;
950*0Sstevel@tonic-gate 
951*0Sstevel@tonic-gate 	if ( tblsize == 0 ) {
952*0Sstevel@tonic-gate #ifdef USE_SYSCONF
953*0Sstevel@tonic-gate 		tblsize = (int) sysconf( _SC_OPEN_MAX );
954*0Sstevel@tonic-gate #else /* USE_SYSCONF */
955*0Sstevel@tonic-gate 		tblsize = getdtablesize();
956*0Sstevel@tonic-gate #endif /* USE_SYSCONF */
957*0Sstevel@tonic-gate 	}
958*0Sstevel@tonic-gate 
959*0Sstevel@tonic-gate 	FD_ZERO( &readfds );
960*0Sstevel@tonic-gate 	FD_SET( ld->ld_sb.sb_sd, &readfds );
961*0Sstevel@tonic-gate 
962*0Sstevel@tonic-gate 	return( select( tblsize, &readfds, 0, 0, timeout ) );
963*0Sstevel@tonic-gate }
964*0Sstevel@tonic-gate #endif /* !MACOS */
965*0Sstevel@tonic-gate 
966*0Sstevel@tonic-gate 
967*0Sstevel@tonic-gate #ifdef MACOS
968*0Sstevel@tonic-gate static int
969*0Sstevel@tonic-gate ldap_select1( LDAP *ld, struct timeval *timeout )
970*0Sstevel@tonic-gate {
971*0Sstevel@tonic-gate 	return( tcpselect( ld->ld_sb.sb_sd, timeout ));
972*0Sstevel@tonic-gate }
973*0Sstevel@tonic-gate #endif /* MACOS */
974*0Sstevel@tonic-gate 
975*0Sstevel@tonic-gate 
976*0Sstevel@tonic-gate #if ( defined( DOS ) && defined( WINSOCK )) || defined( _WIN32 )
977*0Sstevel@tonic-gate static int
978*0Sstevel@tonic-gate ldap_select1( LDAP *ld, struct timeval *timeout )
979*0Sstevel@tonic-gate {
980*0Sstevel@tonic-gate     fd_set          readfds;
981*0Sstevel@tonic-gate     int             rc;
982*0Sstevel@tonic-gate 
983*0Sstevel@tonic-gate     FD_ZERO( &readfds );
984*0Sstevel@tonic-gate     FD_SET( ld->ld_sb.sb_sd, &readfds );
985*0Sstevel@tonic-gate 
986*0Sstevel@tonic-gate     rc = select( 1, &readfds, 0, 0, timeout );
987*0Sstevel@tonic-gate     return( rc == SOCKET_ERROR ? -1 : rc );
988*0Sstevel@tonic-gate }
989*0Sstevel@tonic-gate #endif /* WINSOCK || _WIN32 */
990*0Sstevel@tonic-gate 
991*0Sstevel@tonic-gate 
992*0Sstevel@tonic-gate #ifdef DOS
993*0Sstevel@tonic-gate #ifdef PCNFS
994*0Sstevel@tonic-gate static int
995*0Sstevel@tonic-gate ldap_select1( LDAP *ld, struct timeval *timeout )
996*0Sstevel@tonic-gate {
997*0Sstevel@tonic-gate 	fd_set	readfds;
998*0Sstevel@tonic-gate 	int	res;
999*0Sstevel@tonic-gate 
1000*0Sstevel@tonic-gate 	FD_ZERO( &readfds );
1001*0Sstevel@tonic-gate 	FD_SET( ld->ld_sb.sb_sd, &readfds );
1002*0Sstevel@tonic-gate 
1003*0Sstevel@tonic-gate 	res = select( FD_SETSIZE, &readfds, NULL, NULL, timeout );
1004*0Sstevel@tonic-gate 	if ( res == -1 && errno == EINTR) {
1005*0Sstevel@tonic-gate 		/* We've been CTRL-C'ed at this point.  It'd be nice to
1006*0Sstevel@tonic-gate 		   carry on but PC-NFS currently won't let us! */
1007*0Sstevel@tonic-gate 		printf("\n*** CTRL-C ***\n");
1008*0Sstevel@tonic-gate 		exit(-1);
1009*0Sstevel@tonic-gate 	}
1010*0Sstevel@tonic-gate 	return( res );
1011*0Sstevel@tonic-gate }
1012*0Sstevel@tonic-gate #endif /* PCNFS */
1013*0Sstevel@tonic-gate 
1014*0Sstevel@tonic-gate #ifdef NCSA
1015*0Sstevel@tonic-gate static int
1016*0Sstevel@tonic-gate ldap_select1( LDAP *ld, struct timeval *timeout )
1017*0Sstevel@tonic-gate {
1018*0Sstevel@tonic-gate 	int rc;
1019*0Sstevel@tonic-gate 	clock_t	endtime;
1020*0Sstevel@tonic-gate 
1021*0Sstevel@tonic-gate 	if ( timeout != NULL ) {
1022*0Sstevel@tonic-gate 		endtime = timeout->tv_sec * CLK_TCK +
1023*0Sstevel@tonic-gate 			timeout->tv_usec * CLK_TCK / 1000000 + clock();
1024*0Sstevel@tonic-gate 	}
1025*0Sstevel@tonic-gate 
1026*0Sstevel@tonic-gate 	do {
1027*0Sstevel@tonic-gate 		Stask();
1028*0Sstevel@tonic-gate 		rc = netqlen( ld->ld_sb.sb_sd );
1029*0Sstevel@tonic-gate 	} while ( rc <= 0 && ( timeout == NULL || clock() < endtime ));
1030*0Sstevel@tonic-gate 
1031*0Sstevel@tonic-gate 	return( rc > 0 ? 1 : 0 );
1032*0Sstevel@tonic-gate }
1033*0Sstevel@tonic-gate #endif /* NCSA */
1034*0Sstevel@tonic-gate #endif /* DOS */
1035*0Sstevel@tonic-gate #endif /* CLDAP */
1036*0Sstevel@tonic-gate 
1037*0Sstevel@tonic-gate 
1038*0Sstevel@tonic-gate int
1039*0Sstevel@tonic-gate ldap_msgfree( LDAPMessage *lm )
1040*0Sstevel@tonic-gate {
1041*0Sstevel@tonic-gate 	LDAPMessage	*next;
1042*0Sstevel@tonic-gate 	int		type = 0;
1043*0Sstevel@tonic-gate 
1044*0Sstevel@tonic-gate 	Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 238, "ldap_msgfree\n"), 0, 0, 0 );
1045*0Sstevel@tonic-gate 
1046*0Sstevel@tonic-gate 	for ( ; lm != NULLMSG; lm = next ) {
1047*0Sstevel@tonic-gate 		next = lm->lm_chain;
1048*0Sstevel@tonic-gate 		type = lm->lm_msgtype;
1049*0Sstevel@tonic-gate 		if (lm->lm_ber)
1050*0Sstevel@tonic-gate 			ber_free( lm->lm_ber, 1 );
1051*0Sstevel@tonic-gate 		free( (char *) lm );
1052*0Sstevel@tonic-gate 	}
1053*0Sstevel@tonic-gate 
1054*0Sstevel@tonic-gate 	return( type );
1055*0Sstevel@tonic-gate }
1056*0Sstevel@tonic-gate 
1057*0Sstevel@tonic-gate /*
1058*0Sstevel@tonic-gate  * ldap_msgdelete - delete a message.  It returns:
1059*0Sstevel@tonic-gate  *	0	if the entire message was deleted
1060*0Sstevel@tonic-gate  *	-1	if the message was not found, or only part of it was found
1061*0Sstevel@tonic-gate  */
1062*0Sstevel@tonic-gate int
1063*0Sstevel@tonic-gate ldap_msgdelete( LDAP *ld, int msgid )
1064*0Sstevel@tonic-gate {
1065*0Sstevel@tonic-gate 	LDAPMessage	*lm, *prev;
1066*0Sstevel@tonic-gate 
1067*0Sstevel@tonic-gate #ifdef _REENTRANT
1068*0Sstevel@tonic-gate 	LOCK_LDAP(ld);
1069*0Sstevel@tonic-gate 	LOCK_RESPONSE(ld);
1070*0Sstevel@tonic-gate #endif
1071*0Sstevel@tonic-gate 	Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 239, "ldap_msgdelete\n"), 0, 0, 0 );
1072*0Sstevel@tonic-gate 
1073*0Sstevel@tonic-gate 	prev = NULLMSG;
1074*0Sstevel@tonic-gate 	for ( lm = ld->ld_responses; lm != NULLMSG; lm = lm->lm_next ) {
1075*0Sstevel@tonic-gate 		if ( lm->lm_msgid == msgid )
1076*0Sstevel@tonic-gate 			break;
1077*0Sstevel@tonic-gate 		prev = lm;
1078*0Sstevel@tonic-gate 	}
1079*0Sstevel@tonic-gate 
1080*0Sstevel@tonic-gate 	if ( lm == NULLMSG ) {
1081*0Sstevel@tonic-gate #ifdef _REENTRANT
1082*0Sstevel@tonic-gate 		UNLOCK_LDAP(ld);
1083*0Sstevel@tonic-gate 		UNLOCK_RESPONSE(ld);
1084*0Sstevel@tonic-gate #endif
1085*0Sstevel@tonic-gate 		return( -1 );
1086*0Sstevel@tonic-gate 	}
1087*0Sstevel@tonic-gate 
1088*0Sstevel@tonic-gate 	if ( prev == NULLMSG )
1089*0Sstevel@tonic-gate 		ld->ld_responses = lm->lm_next;
1090*0Sstevel@tonic-gate 	else
1091*0Sstevel@tonic-gate 		prev->lm_next = lm->lm_next;
1092*0Sstevel@tonic-gate 
1093*0Sstevel@tonic-gate 	if ( ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY ) {
1094*0Sstevel@tonic-gate #ifdef _REENTRANT
1095*0Sstevel@tonic-gate 		UNLOCK_LDAP(ld);
1096*0Sstevel@tonic-gate 		UNLOCK_RESPONSE(ld);
1097*0Sstevel@tonic-gate #endif
1098*0Sstevel@tonic-gate 		return( -1 );
1099*0Sstevel@tonic-gate 	}
1100*0Sstevel@tonic-gate 
1101*0Sstevel@tonic-gate #ifdef _REENTRANT
1102*0Sstevel@tonic-gate 	UNLOCK_LDAP(ld);
1103*0Sstevel@tonic-gate 	UNLOCK_RESPONSE(ld);
1104*0Sstevel@tonic-gate #endif
1105*0Sstevel@tonic-gate 	return( 0 );
1106*0Sstevel@tonic-gate }
1107*0Sstevel@tonic-gate 
1108*0Sstevel@tonic-gate 
1109*0Sstevel@tonic-gate /*
1110*0Sstevel@tonic-gate  * return 1 if message msgid is waiting to be abandoned, 0 otherwise
1111*0Sstevel@tonic-gate  */
1112*0Sstevel@tonic-gate static int
1113*0Sstevel@tonic-gate ldap_abandoned( LDAP *ld, int msgid )
1114*0Sstevel@tonic-gate {
1115*0Sstevel@tonic-gate 	int	i;
1116*0Sstevel@tonic-gate 
1117*0Sstevel@tonic-gate 	if ( ld == NULL ) return(1);
1118*0Sstevel@tonic-gate 	if ( ld->ld_abandoned == NULL )
1119*0Sstevel@tonic-gate 		return( 0 );
1120*0Sstevel@tonic-gate 
1121*0Sstevel@tonic-gate 	for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
1122*0Sstevel@tonic-gate 		if ( ld->ld_abandoned[i] == msgid )
1123*0Sstevel@tonic-gate 			return( 1 );
1124*0Sstevel@tonic-gate 
1125*0Sstevel@tonic-gate 	return( 0 );
1126*0Sstevel@tonic-gate }
1127*0Sstevel@tonic-gate 
1128*0Sstevel@tonic-gate 
1129*0Sstevel@tonic-gate static int
1130*0Sstevel@tonic-gate ldap_mark_abandoned( LDAP *ld, int msgid )
1131*0Sstevel@tonic-gate {
1132*0Sstevel@tonic-gate 	int	i;
1133*0Sstevel@tonic-gate 
1134*0Sstevel@tonic-gate 	if ( ld->ld_abandoned == NULL )
1135*0Sstevel@tonic-gate 		return( -1 );
1136*0Sstevel@tonic-gate 
1137*0Sstevel@tonic-gate 	for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
1138*0Sstevel@tonic-gate 		if ( ld->ld_abandoned[i] == msgid )
1139*0Sstevel@tonic-gate 			break;
1140*0Sstevel@tonic-gate 
1141*0Sstevel@tonic-gate 	if ( ld->ld_abandoned[i] == -1 )
1142*0Sstevel@tonic-gate 		return( -1 );
1143*0Sstevel@tonic-gate 
1144*0Sstevel@tonic-gate 	for ( ; ld->ld_abandoned[i] != -1; i++ ) {
1145*0Sstevel@tonic-gate 		ld->ld_abandoned[i] = ld->ld_abandoned[i + 1];
1146*0Sstevel@tonic-gate 	}
1147*0Sstevel@tonic-gate 
1148*0Sstevel@tonic-gate 	return( 0 );
1149*0Sstevel@tonic-gate }
1150*0Sstevel@tonic-gate 
1151*0Sstevel@tonic-gate 
1152*0Sstevel@tonic-gate #ifdef CLDAP
1153*0Sstevel@tonic-gate int
1154*0Sstevel@tonic-gate cldap_getmsg( LDAP *ld, struct timeval *timeout, BerElement *ber )
1155*0Sstevel@tonic-gate {
1156*0Sstevel@tonic-gate 	int		rc;
1157*0Sstevel@tonic-gate 	unsigned int	tag, len;
1158*0Sstevel@tonic-gate 
1159*0Sstevel@tonic-gate #ifdef _REENTRANT
1160*0Sstevel@tonic-gate 	LOCK_LDAP(ld);
1161*0Sstevel@tonic-gate #endif
1162*0Sstevel@tonic-gate 	if ( ld->ld_sb.sb_ber.ber_ptr >= ld->ld_sb.sb_ber.ber_end ) {
1163*0Sstevel@tonic-gate 		rc = ldap_select1( ld, timeout );
1164*0Sstevel@tonic-gate 		if ( rc == -1 || rc == 0 ) {
1165*0Sstevel@tonic-gate 			ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
1166*0Sstevel@tonic-gate 			    LDAP_TIMEOUT);
1167*0Sstevel@tonic-gate #ifdef _REENTRANT
1168*0Sstevel@tonic-gate 			UNLOCK_LDAP(ld);
1169*0Sstevel@tonic-gate #endif
1170*0Sstevel@tonic-gate 			return( rc );
1171*0Sstevel@tonic-gate 		}
1172*0Sstevel@tonic-gate 	}
1173*0Sstevel@tonic-gate 
1174*0Sstevel@tonic-gate 	/* get the next message */
1175*0Sstevel@tonic-gate 	if ( (tag = ber_get_next( &ld->ld_sb, &len, ber ))
1176*0Sstevel@tonic-gate 	    != LDAP_TAG_MESSAGE ) {
1177*0Sstevel@tonic-gate 		ld->ld_errno = (tag == LBER_DEFAULT ? LDAP_SERVER_DOWN :
1178*0Sstevel@tonic-gate 		    LDAP_LOCAL_ERROR);
1179*0Sstevel@tonic-gate #ifdef _REENTRANT
1180*0Sstevel@tonic-gate 		UNLOCK_LDAP(ld);
1181*0Sstevel@tonic-gate #endif
1182*0Sstevel@tonic-gate 		return( -1 );
1183*0Sstevel@tonic-gate 	}
1184*0Sstevel@tonic-gate 
1185*0Sstevel@tonic-gate #ifdef _REENTRANT
1186*0Sstevel@tonic-gate 	UNLOCK_LDAP(ld);
1187*0Sstevel@tonic-gate #endif
1188*0Sstevel@tonic-gate 	return( tag );
1189*0Sstevel@tonic-gate }
1190*0Sstevel@tonic-gate #endif /* CLDAP */
1191*0Sstevel@tonic-gate 
1192*0Sstevel@tonic-gate /* ldapv3 API extensions */
1193*0Sstevel@tonic-gate 
1194*0Sstevel@tonic-gate int ldap_msgtype(LDAPMessage *res)
1195*0Sstevel@tonic-gate {
1196*0Sstevel@tonic-gate 	if (res == NULL)
1197*0Sstevel@tonic-gate 		return (LDAP_RES_ANY);
1198*0Sstevel@tonic-gate 	return (res->lm_msgtype);
1199*0Sstevel@tonic-gate }
1200*0Sstevel@tonic-gate 
1201*0Sstevel@tonic-gate 
1202*0Sstevel@tonic-gate int ldap_msgid(LDAPMessage *res)
1203*0Sstevel@tonic-gate {
1204*0Sstevel@tonic-gate 	if (res == NULL)
1205*0Sstevel@tonic-gate 		return (LDAP_RES_ANY);
1206*0Sstevel@tonic-gate 	return (res->lm_msgid);
1207*0Sstevel@tonic-gate }
1208*0Sstevel@tonic-gate 
1209*0Sstevel@tonic-gate int ldap_parse_result(LDAP *ld, LDAPMessage *res, int *errcodep, char **matcheddnp,
1210*0Sstevel@tonic-gate 					  char **errmsgp, char ***referralsp, LDAPControl ***serverctrlsp,
1211*0Sstevel@tonic-gate 					  int freeit)
1212*0Sstevel@tonic-gate {
1213*0Sstevel@tonic-gate 	LDAPMessage *lm;
1214*0Sstevel@tonic-gate 	BerElement ber;
1215*0Sstevel@tonic-gate 	unsigned int alen;
1216*0Sstevel@tonic-gate 	int along;
1217*0Sstevel@tonic-gate 	unsigned int tag;
1218*0Sstevel@tonic-gate 	int i;
1219*0Sstevel@tonic-gate 	size_t rc;
1220*0Sstevel@tonic-gate 	char * acharp = NULL, * a2ndcharp = NULL;
1221*0Sstevel@tonic-gate 	char ** arefs = NULL;
1222*0Sstevel@tonic-gate 
1223*0Sstevel@tonic-gate 	Debug( LDAP_DEBUG_TRACE, "ldap_parse_result\n", 0, 0, 0 );
1224*0Sstevel@tonic-gate 
1225*0Sstevel@tonic-gate 	if (res == NULLMSG)
1226*0Sstevel@tonic-gate 		return (LDAP_PARAM_ERROR);
1227*0Sstevel@tonic-gate 
1228*0Sstevel@tonic-gate 	if (matcheddnp && *matcheddnp){
1229*0Sstevel@tonic-gate 		free(*matcheddnp);
1230*0Sstevel@tonic-gate 		*matcheddnp = NULL;
1231*0Sstevel@tonic-gate 	}
1232*0Sstevel@tonic-gate 	if (errmsgp && *errmsgp){
1233*0Sstevel@tonic-gate 		free(*errmsgp);
1234*0Sstevel@tonic-gate 		*errmsgp = NULL;
1235*0Sstevel@tonic-gate 	}
1236*0Sstevel@tonic-gate 	if (referralsp && *referralsp){
1237*0Sstevel@tonic-gate 		free_strarray(*referralsp);
1238*0Sstevel@tonic-gate 		*referralsp = NULL;
1239*0Sstevel@tonic-gate 	}
1240*0Sstevel@tonic-gate 
1241*0Sstevel@tonic-gate 	if (serverctrlsp && *serverctrlsp){
1242*0Sstevel@tonic-gate 		ldap_controls_free(*serverctrlsp);
1243*0Sstevel@tonic-gate 		*serverctrlsp = NULL;
1244*0Sstevel@tonic-gate 	}
1245*0Sstevel@tonic-gate 
1246*0Sstevel@tonic-gate 	for (lm = res; lm->lm_chain != NULL; lm = lm->lm_chain)
1247*0Sstevel@tonic-gate 
1248*0Sstevel@tonic-gate 		if ( lm->lm_msgtype != LDAP_RES_SEARCH_ENTRY
1249*0Sstevel@tonic-gate 			 && lm->lm_msgtype != LDAP_RES_SEARCH_REFERENCE)
1250*0Sstevel@tonic-gate 			break;
1251*0Sstevel@tonic-gate 
1252*0Sstevel@tonic-gate 	ber = *(lm->lm_ber);
1253*0Sstevel@tonic-gate 
1254*0Sstevel@tonic-gate #ifdef _REENTRANT
1255*0Sstevel@tonic-gate 	LOCK_LDAP(ld);
1256*0Sstevel@tonic-gate #endif
1257*0Sstevel@tonic-gate 	if (ld->ld_version == LDAP_VERSION3) {
1258*0Sstevel@tonic-gate 		rc = ber_scanf( &ber, "{iaa", &along, &acharp, &a2ndcharp);
1259*0Sstevel@tonic-gate 		if (rc == LBER_ERROR){
1260*0Sstevel@tonic-gate 			if (freeit)
1261*0Sstevel@tonic-gate 				ldap_msgfree( res );
1262*0Sstevel@tonic-gate #ifdef _REENTRANT
1263*0Sstevel@tonic-gate 			UNLOCK_LDAP(ld);
1264*0Sstevel@tonic-gate #endif
1265*0Sstevel@tonic-gate 			return (LDAP_DECODING_ERROR);
1266*0Sstevel@tonic-gate 		}
1267*0Sstevel@tonic-gate 		if (matcheddnp) {
1268*0Sstevel@tonic-gate 			*matcheddnp = acharp;
1269*0Sstevel@tonic-gate 		} else {
1270*0Sstevel@tonic-gate 			ldap_memfree(acharp);
1271*0Sstevel@tonic-gate 		}
1272*0Sstevel@tonic-gate 		if (errmsgp) {
1273*0Sstevel@tonic-gate 			*errmsgp = a2ndcharp;
1274*0Sstevel@tonic-gate 		} else {
1275*0Sstevel@tonic-gate 			ldap_memfree(a2ndcharp);
1276*0Sstevel@tonic-gate 		}
1277*0Sstevel@tonic-gate 
1278*0Sstevel@tonic-gate 		if (errcodep) {
1279*0Sstevel@tonic-gate 			*errcodep = along;
1280*0Sstevel@tonic-gate 		}
1281*0Sstevel@tonic-gate 
1282*0Sstevel@tonic-gate 		if (along == LDAP_REFERRAL){
1283*0Sstevel@tonic-gate 			if (ber_peek_tag ( &ber, &tag) == LDAP_TAG_REFERRAL) {
1284*0Sstevel@tonic-gate 				rc = ber_scanf(&ber, "{v}", &arefs);
1285*0Sstevel@tonic-gate 				if (rc == LBER_ERROR){
1286*0Sstevel@tonic-gate 					/* try to free other stuff */
1287*0Sstevel@tonic-gate 					if (freeit)
1288*0Sstevel@tonic-gate 						ldap_msgfree( res );
1289*0Sstevel@tonic-gate #ifdef _REENTRANT
1290*0Sstevel@tonic-gate 					UNLOCK_LDAP(ld);
1291*0Sstevel@tonic-gate #endif
1292*0Sstevel@tonic-gate 					return (LDAP_DECODING_ERROR);
1293*0Sstevel@tonic-gate 				}
1294*0Sstevel@tonic-gate 				if (referralsp) {
1295*0Sstevel@tonic-gate 					*referralsp = arefs;
1296*0Sstevel@tonic-gate 				} else {
1297*0Sstevel@tonic-gate 					for (i = 0; arefs[i] != NULL; i++)
1298*0Sstevel@tonic-gate 						ldap_memfree(arefs[i]);
1299*0Sstevel@tonic-gate 					ldap_memfree((char *)arefs);
1300*0Sstevel@tonic-gate 				}
1301*0Sstevel@tonic-gate 			} else {
1302*0Sstevel@tonic-gate 				/* referral errcode without URL is forbiden */
1303*0Sstevel@tonic-gate 				if (freeit)
1304*0Sstevel@tonic-gate 					ldap_msgfree( res );
1305*0Sstevel@tonic-gate #ifdef _REENTRANT
1306*0Sstevel@tonic-gate 				UNLOCK_LDAP(ld);
1307*0Sstevel@tonic-gate #endif
1308*0Sstevel@tonic-gate 				return (LDAP_DECODING_ERROR);
1309*0Sstevel@tonic-gate 			}
1310*0Sstevel@tonic-gate 		}
1311*0Sstevel@tonic-gate 		rc = ber_scanf(&ber, "}");
1312*0Sstevel@tonic-gate 		if (rc == LBER_ERROR){
1313*0Sstevel@tonic-gate 			if (freeit)
1314*0Sstevel@tonic-gate 				ldap_msgfree( res );
1315*0Sstevel@tonic-gate #ifdef _REENTRANT
1316*0Sstevel@tonic-gate 			UNLOCK_LDAP(ld);
1317*0Sstevel@tonic-gate #endif
1318*0Sstevel@tonic-gate 			return (LDAP_DECODING_ERROR);
1319*0Sstevel@tonic-gate 		}
1320*0Sstevel@tonic-gate 		/* It's the end of the result but the PDU may have controls */
1321*0Sstevel@tonic-gate 		if (serverctrlsp && (ber_peek_tag(&ber, &alen) == LDAP_TAG_CONTROL_LIST)) {
1322*0Sstevel@tonic-gate 			Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 392, "Controls found in result\n"), 0, 0, 0 );
1323*0Sstevel@tonic-gate 			*serverctrlsp =  ldap_controls_decode(&ber,
1324*0Sstevel@tonic-gate 							    (int *)&rc);
1325*0Sstevel@tonic-gate 			if (*serverctrlsp == NULL) {
1326*0Sstevel@tonic-gate 				if (freeit)
1327*0Sstevel@tonic-gate 					ldap_msgfree( res );
1328*0Sstevel@tonic-gate #ifdef _REENTRANT
1329*0Sstevel@tonic-gate 				UNLOCK_LDAP(ld);
1330*0Sstevel@tonic-gate #endif
1331*0Sstevel@tonic-gate 				return (LDAP_DECODING_ERROR);
1332*0Sstevel@tonic-gate 			}
1333*0Sstevel@tonic-gate 		} else {
1334*0Sstevel@tonic-gate 			Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 393, "NO controls found in result\n"), 0, 0, 0 );
1335*0Sstevel@tonic-gate 		}
1336*0Sstevel@tonic-gate 	}
1337*0Sstevel@tonic-gate 	else if (ld->ld_version == LDAP_VERSION2) {
1338*0Sstevel@tonic-gate 		rc = ber_scanf( &ber, "{iaa}", &along, &acharp,
1339*0Sstevel@tonic-gate 		     &a2ndcharp );
1340*0Sstevel@tonic-gate 		if (rc == LBER_ERROR){
1341*0Sstevel@tonic-gate 			if (freeit)
1342*0Sstevel@tonic-gate 				ldap_msgfree( res );
1343*0Sstevel@tonic-gate #ifdef _REENTRANT
1344*0Sstevel@tonic-gate 			UNLOCK_LDAP(ld);
1345*0Sstevel@tonic-gate #endif
1346*0Sstevel@tonic-gate 			return (LDAP_DECODING_ERROR);
1347*0Sstevel@tonic-gate 		}
1348*0Sstevel@tonic-gate 		if (matcheddnp) {
1349*0Sstevel@tonic-gate 			*matcheddnp = acharp;
1350*0Sstevel@tonic-gate 		} else {
1351*0Sstevel@tonic-gate 			ldap_memfree(acharp);
1352*0Sstevel@tonic-gate 		}
1353*0Sstevel@tonic-gate 		if (errmsgp) {
1354*0Sstevel@tonic-gate 			*errmsgp = a2ndcharp;
1355*0Sstevel@tonic-gate 		} else {
1356*0Sstevel@tonic-gate 			ldap_memfree(a2ndcharp);
1357*0Sstevel@tonic-gate 		}
1358*0Sstevel@tonic-gate 		if (errcodep) {
1359*0Sstevel@tonic-gate 			*errcodep = along;
1360*0Sstevel@tonic-gate 		}
1361*0Sstevel@tonic-gate 	}
1362*0Sstevel@tonic-gate 	else {
1363*0Sstevel@tonic-gate 		rc = ber_scanf( &ber, "{ia}", &along, &a2ndcharp );
1364*0Sstevel@tonic-gate 		if (rc == LBER_ERROR){
1365*0Sstevel@tonic-gate 			if (freeit)
1366*0Sstevel@tonic-gate 				ldap_msgfree( res );
1367*0Sstevel@tonic-gate #ifdef _REENTRANT
1368*0Sstevel@tonic-gate 			UNLOCK_LDAP(ld);
1369*0Sstevel@tonic-gate #endif
1370*0Sstevel@tonic-gate 			return (LDAP_DECODING_ERROR);
1371*0Sstevel@tonic-gate 		}
1372*0Sstevel@tonic-gate 
1373*0Sstevel@tonic-gate 		if (errmsgp) {
1374*0Sstevel@tonic-gate 			*errmsgp = a2ndcharp;
1375*0Sstevel@tonic-gate 		} else {
1376*0Sstevel@tonic-gate 			ldap_memfree(a2ndcharp);
1377*0Sstevel@tonic-gate 		}
1378*0Sstevel@tonic-gate 		if (errcodep) {
1379*0Sstevel@tonic-gate 			*errcodep = along;
1380*0Sstevel@tonic-gate 		}
1381*0Sstevel@tonic-gate 	}
1382*0Sstevel@tonic-gate 
1383*0Sstevel@tonic-gate 	if ( freeit )
1384*0Sstevel@tonic-gate 		ldap_msgfree(res);
1385*0Sstevel@tonic-gate 
1386*0Sstevel@tonic-gate #ifdef _REENTRANT
1387*0Sstevel@tonic-gate 	UNLOCK_LDAP(ld);
1388*0Sstevel@tonic-gate #endif
1389*0Sstevel@tonic-gate 	return (LDAP_SUCCESS);
1390*0Sstevel@tonic-gate }
1391*0Sstevel@tonic-gate 
1392*0Sstevel@tonic-gate int ldap_parse_sasl_bind_result(LDAP *ld, LDAPMessage *res, struct berval **servercredp, int freeit)
1393*0Sstevel@tonic-gate {
1394*0Sstevel@tonic-gate 	LDAPMessage *lm;
1395*0Sstevel@tonic-gate 	BerElement ber;
1396*0Sstevel@tonic-gate 	int along;
1397*0Sstevel@tonic-gate 	unsigned int tag;
1398*0Sstevel@tonic-gate 	int i;
1399*0Sstevel@tonic-gate 	size_t rc;
1400*0Sstevel@tonic-gate 	char * acharp = NULL, *a2ndcharp = NULL;
1401*0Sstevel@tonic-gate 	char ** arefs = NULL;
1402*0Sstevel@tonic-gate 	struct berval * creds = NULL;
1403*0Sstevel@tonic-gate 
1404*0Sstevel@tonic-gate 	Debug( LDAP_DEBUG_TRACE, "ldap_parse_extended_result\n", 0, 0, 0 );
1405*0Sstevel@tonic-gate 
1406*0Sstevel@tonic-gate 	if (res == NULLMSG)
1407*0Sstevel@tonic-gate 		return (LDAP_PARAM_ERROR);
1408*0Sstevel@tonic-gate 
1409*0Sstevel@tonic-gate #ifdef _REENTRANT
1410*0Sstevel@tonic-gate 	LOCK_LDAP(ld);
1411*0Sstevel@tonic-gate #endif
1412*0Sstevel@tonic-gate 	if ((res->lm_msgtype != LDAP_RES_BIND) || (ld->ld_version != LDAP_VERSION3)){
1413*0Sstevel@tonic-gate 		ld->ld_errno = LDAP_PARAM_ERROR;
1414*0Sstevel@tonic-gate #ifdef _REENTRANT
1415*0Sstevel@tonic-gate 		UNLOCK_LDAP(ld);
1416*0Sstevel@tonic-gate #endif
1417*0Sstevel@tonic-gate 		return (LDAP_PARAM_ERROR);
1418*0Sstevel@tonic-gate 	}
1419*0Sstevel@tonic-gate #ifdef _REENTRANT
1420*0Sstevel@tonic-gate 	UNLOCK_LDAP(ld);
1421*0Sstevel@tonic-gate #endif
1422*0Sstevel@tonic-gate 
1423*0Sstevel@tonic-gate 	ber = *(res->lm_ber);
1424*0Sstevel@tonic-gate 	rc = ber_scanf( &ber, "{iaa", &along, &acharp, &a2ndcharp);
1425*0Sstevel@tonic-gate 	if (rc == LBER_ERROR){
1426*0Sstevel@tonic-gate 		if (freeit)
1427*0Sstevel@tonic-gate 			ldap_msgfree( res );
1428*0Sstevel@tonic-gate #ifdef _REENTRANT
1429*0Sstevel@tonic-gate 		LOCK_LDAP(ld);
1430*0Sstevel@tonic-gate #endif
1431*0Sstevel@tonic-gate 		ld->ld_errno = LDAP_DECODING_ERROR;
1432*0Sstevel@tonic-gate #ifdef _REENTRANT
1433*0Sstevel@tonic-gate 		UNLOCK_LDAP(ld);
1434*0Sstevel@tonic-gate #endif
1435*0Sstevel@tonic-gate 		return (LDAP_DECODING_ERROR);
1436*0Sstevel@tonic-gate 	}
1437*0Sstevel@tonic-gate 	ldap_memfree(acharp);
1438*0Sstevel@tonic-gate 	ldap_memfree(a2ndcharp);
1439*0Sstevel@tonic-gate 	if (along == LDAP_SUCCESS || along == LDAP_SASL_BIND_INPROGRESS){
1440*0Sstevel@tonic-gate 		/* Decode the serverSaslCreds if any */
1441*0Sstevel@tonic-gate 		if (ber_peek_tag ( &ber, &tag) == LDAP_TAG_SASLCREDS) {
1442*0Sstevel@tonic-gate 			rc = ber_get_stringal( &ber, &creds);
1443*0Sstevel@tonic-gate 			if (rc == LBER_ERROR ){
1444*0Sstevel@tonic-gate 				if (freeit)
1445*0Sstevel@tonic-gate 					ldap_msgfree(res);
1446*0Sstevel@tonic-gate #ifdef _REENTRANT
1447*0Sstevel@tonic-gate 				LOCK_LDAP(ld);
1448*0Sstevel@tonic-gate #endif
1449*0Sstevel@tonic-gate 				ld->ld_errno = LDAP_DECODING_ERROR;
1450*0Sstevel@tonic-gate #ifdef _REENTRANT
1451*0Sstevel@tonic-gate 				UNLOCK_LDAP(ld);
1452*0Sstevel@tonic-gate #endif
1453*0Sstevel@tonic-gate 				return (LDAP_DECODING_ERROR);
1454*0Sstevel@tonic-gate 			}
1455*0Sstevel@tonic-gate 			if (servercredp) {
1456*0Sstevel@tonic-gate 				*servercredp = creds;
1457*0Sstevel@tonic-gate 			} else {
1458*0Sstevel@tonic-gate 				ber_bvfree( creds );
1459*0Sstevel@tonic-gate 			}
1460*0Sstevel@tonic-gate 		}
1461*0Sstevel@tonic-gate 	} else if (along == LDAP_REFERRAL) {
1462*0Sstevel@tonic-gate 		if (ber_peek_tag ( &ber, &tag) == LDAP_TAG_REFERRAL){
1463*0Sstevel@tonic-gate 			rc = ber_scanf(&ber, "{v}", &arefs);
1464*0Sstevel@tonic-gate 			if (rc == LBER_ERROR){
1465*0Sstevel@tonic-gate 				/* try to free other stuff */
1466*0Sstevel@tonic-gate 				if (freeit)
1467*0Sstevel@tonic-gate 					ldap_msgfree( res );
1468*0Sstevel@tonic-gate #ifdef _REENTRANT
1469*0Sstevel@tonic-gate 				LOCK_LDAP(ld);
1470*0Sstevel@tonic-gate #endif
1471*0Sstevel@tonic-gate 				ld->ld_errno = LDAP_DECODING_ERROR;
1472*0Sstevel@tonic-gate #ifdef _REENTRANT
1473*0Sstevel@tonic-gate 				UNLOCK_LDAP(ld);
1474*0Sstevel@tonic-gate #endif
1475*0Sstevel@tonic-gate 				return (LDAP_DECODING_ERROR);
1476*0Sstevel@tonic-gate 			}
1477*0Sstevel@tonic-gate 			for (i = 0; arefs[i] != NULL; i++)
1478*0Sstevel@tonic-gate 				ldap_memfree(arefs[i]);
1479*0Sstevel@tonic-gate 			ldap_memfree((char *)arefs);
1480*0Sstevel@tonic-gate 		} else {
1481*0Sstevel@tonic-gate 			/* There should be at least one ref */
1482*0Sstevel@tonic-gate 			if (freeit)
1483*0Sstevel@tonic-gate 				ldap_msgfree( res );
1484*0Sstevel@tonic-gate #ifdef _REENTRANT
1485*0Sstevel@tonic-gate 			LOCK_LDAP(ld);
1486*0Sstevel@tonic-gate #endif
1487*0Sstevel@tonic-gate 			ld->ld_errno = LDAP_DECODING_ERROR;
1488*0Sstevel@tonic-gate #ifdef _REENTRANT
1489*0Sstevel@tonic-gate 			UNLOCK_LDAP(ld);
1490*0Sstevel@tonic-gate #endif
1491*0Sstevel@tonic-gate 			return (LDAP_DECODING_ERROR);
1492*0Sstevel@tonic-gate 		}
1493*0Sstevel@tonic-gate 	}
1494*0Sstevel@tonic-gate 
1495*0Sstevel@tonic-gate 	rc = ber_scanf(&ber, "}");
1496*0Sstevel@tonic-gate 	if (rc == LBER_ERROR){
1497*0Sstevel@tonic-gate 		if (freeit)
1498*0Sstevel@tonic-gate 			ldap_msgfree( res );
1499*0Sstevel@tonic-gate #ifdef _REENTRANT
1500*0Sstevel@tonic-gate 		LOCK_LDAP(ld);
1501*0Sstevel@tonic-gate #endif
1502*0Sstevel@tonic-gate 		ld->ld_errno = LDAP_DECODING_ERROR;
1503*0Sstevel@tonic-gate #ifdef _REENTRANT
1504*0Sstevel@tonic-gate 		UNLOCK_LDAP(ld);
1505*0Sstevel@tonic-gate #endif
1506*0Sstevel@tonic-gate 		return (LDAP_DECODING_ERROR);
1507*0Sstevel@tonic-gate 	}
1508*0Sstevel@tonic-gate 
1509*0Sstevel@tonic-gate 	if ( freeit )
1510*0Sstevel@tonic-gate 		ldap_msgfree(res);
1511*0Sstevel@tonic-gate #ifdef _REENTRANT
1512*0Sstevel@tonic-gate 	LOCK_LDAP(ld);
1513*0Sstevel@tonic-gate #endif
1514*0Sstevel@tonic-gate 	ld->ld_errno = along;
1515*0Sstevel@tonic-gate #ifdef _REENTRANT
1516*0Sstevel@tonic-gate 	UNLOCK_LDAP(ld);
1517*0Sstevel@tonic-gate #endif
1518*0Sstevel@tonic-gate 	return (along);
1519*0Sstevel@tonic-gate }
1520*0Sstevel@tonic-gate 
1521*0Sstevel@tonic-gate int ldap_parse_extended_result(LDAP *ld, LDAPMessage *res, char **resultoidp,
1522*0Sstevel@tonic-gate 							   struct berval **resultdata, int freeit)
1523*0Sstevel@tonic-gate {
1524*0Sstevel@tonic-gate 	LDAPMessage *lm;
1525*0Sstevel@tonic-gate 	BerElement ber;
1526*0Sstevel@tonic-gate 	int along;
1527*0Sstevel@tonic-gate 	unsigned int tag;
1528*0Sstevel@tonic-gate 	int i;
1529*0Sstevel@tonic-gate 	size_t rc;
1530*0Sstevel@tonic-gate 	char * acharp = NULL, *a2ndcharp = NULL, *anoid = NULL;
1531*0Sstevel@tonic-gate 	char **arefs = NULL;
1532*0Sstevel@tonic-gate 	struct berval * aresp = NULL;
1533*0Sstevel@tonic-gate 
1534*0Sstevel@tonic-gate 	Debug( LDAP_DEBUG_TRACE, "ldap_parse_sasl_bind_result\n", 0, 0, 0 );
1535*0Sstevel@tonic-gate 
1536*0Sstevel@tonic-gate 	if ( res == NULLMSG )
1537*0Sstevel@tonic-gate 		return (LDAP_PARAM_ERROR);
1538*0Sstevel@tonic-gate 
1539*0Sstevel@tonic-gate #ifdef _REENTRANT
1540*0Sstevel@tonic-gate 	LOCK_LDAP(ld);
1541*0Sstevel@tonic-gate #endif
1542*0Sstevel@tonic-gate 	if ((res->lm_msgtype != LDAP_RES_EXTENDED) || (ld->ld_version != LDAP_VERSION3))
1543*0Sstevel@tonic-gate 	{
1544*0Sstevel@tonic-gate 		if ( res->lm_msgid != 0 )
1545*0Sstevel@tonic-gate #ifdef _REENTRANT
1546*0Sstevel@tonic-gate 			UNLOCK_LDAP(ld);
1547*0Sstevel@tonic-gate #endif
1548*0Sstevel@tonic-gate 			return (LDAP_PARAM_ERROR);
1549*0Sstevel@tonic-gate 	}
1550*0Sstevel@tonic-gate #ifdef _REENTRANT
1551*0Sstevel@tonic-gate 	UNLOCK_LDAP(ld);
1552*0Sstevel@tonic-gate #endif
1553*0Sstevel@tonic-gate 
1554*0Sstevel@tonic-gate 	ber = *(res->lm_ber);
1555*0Sstevel@tonic-gate 	rc = ber_scanf( &ber, "{iaa", &along, &acharp, &a2ndcharp);
1556*0Sstevel@tonic-gate 	if (rc == LBER_ERROR){
1557*0Sstevel@tonic-gate 		if (freeit)
1558*0Sstevel@tonic-gate 			ldap_msgfree( res );
1559*0Sstevel@tonic-gate 		return (LDAP_DECODING_ERROR);
1560*0Sstevel@tonic-gate 	}
1561*0Sstevel@tonic-gate 	ldap_memfree(acharp);
1562*0Sstevel@tonic-gate 	ldap_memfree(a2ndcharp);
1563*0Sstevel@tonic-gate 
1564*0Sstevel@tonic-gate 	if (along == LDAP_REFERRAL) {
1565*0Sstevel@tonic-gate 		if (ber_peek_tag ( &ber, &tag) == LDAP_TAG_REFERRAL){
1566*0Sstevel@tonic-gate 			rc = ber_scanf(&ber, "{v}", &arefs);
1567*0Sstevel@tonic-gate 			if (rc == LBER_ERROR){
1568*0Sstevel@tonic-gate 				/* try to free other stuff */
1569*0Sstevel@tonic-gate 				if (freeit)
1570*0Sstevel@tonic-gate 					ldap_msgfree( res );
1571*0Sstevel@tonic-gate 				return (LDAP_DECODING_ERROR);
1572*0Sstevel@tonic-gate 			}
1573*0Sstevel@tonic-gate 			for (i = 0; arefs[i] != NULL; i++)
1574*0Sstevel@tonic-gate 				ldap_memfree(arefs[i]);
1575*0Sstevel@tonic-gate 			ldap_memfree((char *)arefs);
1576*0Sstevel@tonic-gate 		} else {
1577*0Sstevel@tonic-gate 			/* There should be at least one ref */
1578*0Sstevel@tonic-gate 			if (freeit)
1579*0Sstevel@tonic-gate 				ldap_msgfree( res );
1580*0Sstevel@tonic-gate 			return (LDAP_DECODING_ERROR);
1581*0Sstevel@tonic-gate 		}
1582*0Sstevel@tonic-gate 	}
1583*0Sstevel@tonic-gate 
1584*0Sstevel@tonic-gate 	if (ber_peek_tag ( &ber, &tag) == LDAP_TAG_EXT_RESPNAME) {
1585*0Sstevel@tonic-gate 		rc = ber_get_stringa( &ber, &anoid);
1586*0Sstevel@tonic-gate 		if (rc == LBER_ERROR ){
1587*0Sstevel@tonic-gate 			if (freeit)
1588*0Sstevel@tonic-gate 				ldap_msgfree(res);
1589*0Sstevel@tonic-gate 			return (LDAP_DECODING_ERROR);
1590*0Sstevel@tonic-gate 		}
1591*0Sstevel@tonic-gate 		if (resultoidp) {
1592*0Sstevel@tonic-gate 			*resultoidp = anoid;
1593*0Sstevel@tonic-gate 		} else {
1594*0Sstevel@tonic-gate 			ldap_memfree( anoid );
1595*0Sstevel@tonic-gate 		}
1596*0Sstevel@tonic-gate 	}
1597*0Sstevel@tonic-gate 	if (ber_peek_tag ( &ber, &tag) == LDAP_TAG_EXT_RESPONSE) {
1598*0Sstevel@tonic-gate 		rc = ber_get_stringal( &ber, &aresp);
1599*0Sstevel@tonic-gate 		if (rc == LBER_ERROR ){
1600*0Sstevel@tonic-gate 			if (freeit)
1601*0Sstevel@tonic-gate 				ldap_msgfree(res);
1602*0Sstevel@tonic-gate 			return (LDAP_DECODING_ERROR);
1603*0Sstevel@tonic-gate 		}
1604*0Sstevel@tonic-gate 		if (resultdata) {
1605*0Sstevel@tonic-gate 			*resultdata = aresp;
1606*0Sstevel@tonic-gate 		} else {
1607*0Sstevel@tonic-gate 			ber_bvfree( aresp );
1608*0Sstevel@tonic-gate 		}
1609*0Sstevel@tonic-gate 	}
1610*0Sstevel@tonic-gate 
1611*0Sstevel@tonic-gate 	rc = ber_scanf(&ber, "}");
1612*0Sstevel@tonic-gate 	if (rc == LBER_ERROR){
1613*0Sstevel@tonic-gate 		if (freeit)
1614*0Sstevel@tonic-gate 			ldap_msgfree( res );
1615*0Sstevel@tonic-gate 		return (LDAP_DECODING_ERROR);
1616*0Sstevel@tonic-gate 	}
1617*0Sstevel@tonic-gate 
1618*0Sstevel@tonic-gate 	if ( freeit )
1619*0Sstevel@tonic-gate 		ldap_msgfree(res);
1620*0Sstevel@tonic-gate 
1621*0Sstevel@tonic-gate 	return (along);
1622*0Sstevel@tonic-gate }
1623*0Sstevel@tonic-gate 
1624*0Sstevel@tonic-gate 
1625*0Sstevel@tonic-gate static int Ref_AddToRequest(LDAPRequest *lr, char **refs) {
1626*0Sstevel@tonic-gate 	int count;
1627*0Sstevel@tonic-gate 	LDAPRef *lref;
1628*0Sstevel@tonic-gate 	LDAPRef *newRef;
1629*0Sstevel@tonic-gate 
1630*0Sstevel@tonic-gate 	if ((newRef = (LDAPRef *)calloc(1, sizeof (LDAPRef))) == NULL){
1631*0Sstevel@tonic-gate 		return LDAP_NO_MEMORY;
1632*0Sstevel@tonic-gate 	}
1633*0Sstevel@tonic-gate 	newRef->lref_refs = refs;
1634*0Sstevel@tonic-gate 	newRef->lref_next = NULL;
1635*0Sstevel@tonic-gate 	lref = lr->lr_references;
1636*0Sstevel@tonic-gate 	if (lref == NULL){
1637*0Sstevel@tonic-gate 		lr->lr_references = newRef;
1638*0Sstevel@tonic-gate 	} else {
1639*0Sstevel@tonic-gate 		while (lref->lref_next != NULL)
1640*0Sstevel@tonic-gate 			lref = lref->lref_next;
1641*0Sstevel@tonic-gate 		lref->lref_next = newRef;
1642*0Sstevel@tonic-gate 	}
1643*0Sstevel@tonic-gate 	return LDAP_SUCCESS;
1644*0Sstevel@tonic-gate }
1645*0Sstevel@tonic-gate 
1646*0Sstevel@tonic-gate static void Ref_FreeAll(LDAPRequest *lr)
1647*0Sstevel@tonic-gate {
1648*0Sstevel@tonic-gate 	LDAPRef *lref, *next;
1649*0Sstevel@tonic-gate 	lref = lr->lr_references;
1650*0Sstevel@tonic-gate 	while (lref != NULL){
1651*0Sstevel@tonic-gate 		next = lref->lref_next;
1652*0Sstevel@tonic-gate 		ldap_value_free(lref->lref_refs);
1653*0Sstevel@tonic-gate 		free (lref);
1654*0Sstevel@tonic-gate 		lref = next;
1655*0Sstevel@tonic-gate 	}
1656*0Sstevel@tonic-gate 	lr->lr_references = NULL;
1657*0Sstevel@tonic-gate }
1658