xref: /titanic_50/usr/src/lib/libldap5/sources/ldap/common/abandon.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
2*7c478bd9Sstevel@tonic-gate 
3*7c478bd9Sstevel@tonic-gate /*
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the Netscape Public
5*7c478bd9Sstevel@tonic-gate  * License Version 1.1 (the "License"); you may not use this file
6*7c478bd9Sstevel@tonic-gate  * except in compliance with the License. You may obtain a copy of
7*7c478bd9Sstevel@tonic-gate  * the License at http://www.mozilla.org/NPL/
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * Software distributed under the License is distributed on an "AS
10*7c478bd9Sstevel@tonic-gate  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11*7c478bd9Sstevel@tonic-gate  * implied. See the License for the specific language governing
12*7c478bd9Sstevel@tonic-gate  * rights and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * The Original Code is Mozilla Communicator client code, released
15*7c478bd9Sstevel@tonic-gate  * March 31, 1998.
16*7c478bd9Sstevel@tonic-gate  *
17*7c478bd9Sstevel@tonic-gate  * The Initial Developer of the Original Code is Netscape
18*7c478bd9Sstevel@tonic-gate  * Communications Corporation. Portions created by Netscape are
19*7c478bd9Sstevel@tonic-gate  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
20*7c478bd9Sstevel@tonic-gate  * Rights Reserved.
21*7c478bd9Sstevel@tonic-gate  *
22*7c478bd9Sstevel@tonic-gate  * Contributor(s):
23*7c478bd9Sstevel@tonic-gate  */
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate /*
26*7c478bd9Sstevel@tonic-gate  *  Copyright (c) 1990 Regents of the University of Michigan.
27*7c478bd9Sstevel@tonic-gate  *  All rights reserved.
28*7c478bd9Sstevel@tonic-gate  */
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  *  abandon.c
31*7c478bd9Sstevel@tonic-gate  */
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #if 0
34*7c478bd9Sstevel@tonic-gate #ifndef lint
35*7c478bd9Sstevel@tonic-gate static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
36*7c478bd9Sstevel@tonic-gate #endif
37*7c478bd9Sstevel@tonic-gate #endif
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #include "ldap-int.h"
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate static int do_abandon( LDAP *ld, int origid, int msgid,
42*7c478bd9Sstevel@tonic-gate     LDAPControl **serverctrls, LDAPControl **clientctrls );
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate /*
45*7c478bd9Sstevel@tonic-gate  * ldap_abandon - perform an ldap abandon operation. Parameters:
46*7c478bd9Sstevel@tonic-gate  *
47*7c478bd9Sstevel@tonic-gate  *	ld		LDAP descriptor
48*7c478bd9Sstevel@tonic-gate  *	msgid		The message id of the operation to abandon
49*7c478bd9Sstevel@tonic-gate  *
50*7c478bd9Sstevel@tonic-gate  * ldap_abandon returns 0 if everything went ok, -1 otherwise.
51*7c478bd9Sstevel@tonic-gate  *
52*7c478bd9Sstevel@tonic-gate  * Example:
53*7c478bd9Sstevel@tonic-gate  *	ldap_abandon( ld, msgid );
54*7c478bd9Sstevel@tonic-gate  */
55*7c478bd9Sstevel@tonic-gate int
56*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_abandon(LDAP * ld,int msgid)57*7c478bd9Sstevel@tonic-gate ldap_abandon( LDAP *ld, int msgid )
58*7c478bd9Sstevel@tonic-gate {
59*7c478bd9Sstevel@tonic-gate 	LDAPDebug( LDAP_DEBUG_TRACE, "ldap_abandon %d\n", msgid, 0, 0 );
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate 	if ( ldap_abandon_ext( ld, msgid, NULL, NULL ) == LDAP_SUCCESS ) {
62*7c478bd9Sstevel@tonic-gate 	    return( 0 );
63*7c478bd9Sstevel@tonic-gate 	}
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 	return( -1 );
66*7c478bd9Sstevel@tonic-gate }
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate /*
70*7c478bd9Sstevel@tonic-gate  * LDAPv3 extended abandon.
71*7c478bd9Sstevel@tonic-gate  * Returns an LDAP error code.
72*7c478bd9Sstevel@tonic-gate  */
73*7c478bd9Sstevel@tonic-gate int
74*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_abandon_ext(LDAP * ld,int msgid,LDAPControl ** serverctrls,LDAPControl ** clientctrls)75*7c478bd9Sstevel@tonic-gate ldap_abandon_ext( LDAP *ld, int msgid, LDAPControl **serverctrls,
76*7c478bd9Sstevel@tonic-gate 	LDAPControl **clientctrls )
77*7c478bd9Sstevel@tonic-gate {
78*7c478bd9Sstevel@tonic-gate 	int	rc;
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 	LDAPDebug( LDAP_DEBUG_TRACE, "ldap_abandon_ext %d\n", msgid, 0, 0 );
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 	if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
83*7c478bd9Sstevel@tonic-gate 		return( LDAP_PARAM_ERROR );
84*7c478bd9Sstevel@tonic-gate 	}
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	LDAP_MUTEX_LOCK( ld, LDAP_CONN_LOCK );
87*7c478bd9Sstevel@tonic-gate 	LDAP_MUTEX_LOCK( ld, LDAP_REQ_LOCK );
88*7c478bd9Sstevel@tonic-gate 	rc = do_abandon( ld, msgid, msgid, serverctrls, clientctrls );
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 	/*
91*7c478bd9Sstevel@tonic-gate 	 * XXXmcs should use cache function pointers to hook in memcache
92*7c478bd9Sstevel@tonic-gate 	 */
93*7c478bd9Sstevel@tonic-gate 	ldap_memcache_abandon( ld, msgid );
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate 	LDAP_MUTEX_UNLOCK( ld, LDAP_REQ_LOCK );
96*7c478bd9Sstevel@tonic-gate 	LDAP_MUTEX_UNLOCK( ld, LDAP_CONN_LOCK );
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 	return( rc );
99*7c478bd9Sstevel@tonic-gate }
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate /*
103*7c478bd9Sstevel@tonic-gate  * Abandon all outstanding requests for msgid (included child requests
104*7c478bd9Sstevel@tonic-gate  * spawned when chasing referrals).  This function calls itself recursively.
105*7c478bd9Sstevel@tonic-gate  * No locking is done is this function so it must be done by the caller.
106*7c478bd9Sstevel@tonic-gate  * Returns an LDAP error code and sets it in LDAP *ld as well
107*7c478bd9Sstevel@tonic-gate  */
108*7c478bd9Sstevel@tonic-gate static int
do_abandon(LDAP * ld,int origid,int msgid,LDAPControl ** serverctrls,LDAPControl ** clientctrls)109*7c478bd9Sstevel@tonic-gate do_abandon( LDAP *ld, int origid, int msgid, LDAPControl **serverctrls,
110*7c478bd9Sstevel@tonic-gate     LDAPControl **clientctrls )
111*7c478bd9Sstevel@tonic-gate {
112*7c478bd9Sstevel@tonic-gate 	BerElement	*ber;
113*7c478bd9Sstevel@tonic-gate 	int		i, bererr, lderr, sendabandon;
114*7c478bd9Sstevel@tonic-gate 	Sockbuf		*sb;
115*7c478bd9Sstevel@tonic-gate 	LDAPRequest	*lr = NULL;
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 	/*
118*7c478bd9Sstevel@tonic-gate 	 * An abandon request looks like this:
119*7c478bd9Sstevel@tonic-gate 	 *	AbandonRequest ::= MessageID
120*7c478bd9Sstevel@tonic-gate 	 */
121*7c478bd9Sstevel@tonic-gate 	LDAPDebug( LDAP_DEBUG_TRACE, "do_abandon origid %d, msgid %d\n",
122*7c478bd9Sstevel@tonic-gate 		origid, msgid, 0 );
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate 	/* optimistic */
125*7c478bd9Sstevel@tonic-gate 	lderr = LDAP_SUCCESS;
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate /*
128*7c478bd9Sstevel@tonic-gate  * this is not the best implementation...
129*7c478bd9Sstevel@tonic-gate  * the code special cases the when async io is enabled.
130*7c478bd9Sstevel@tonic-gate  * The logic is clear this way, at the cost of code bloat.
131*7c478bd9Sstevel@tonic-gate  * This logic should be cleaned up post nova 4.5 rtm
132*7c478bd9Sstevel@tonic-gate  */
133*7c478bd9Sstevel@tonic-gate     if (ld->ld_options & LDAP_BITOPT_ASYNC)
134*7c478bd9Sstevel@tonic-gate     {
135*7c478bd9Sstevel@tonic-gate         /* Don't send an abandon message unless there is something to abandon. */
136*7c478bd9Sstevel@tonic-gate         sendabandon = 0;
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate         /* Find the request that we are abandoning. */
139*7c478bd9Sstevel@tonic-gate         if (ld->ld_requests != NULL) {
140*7c478bd9Sstevel@tonic-gate             for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
141*7c478bd9Sstevel@tonic-gate                 if ( lr->lr_msgid == msgid ) {	/* this message */
142*7c478bd9Sstevel@tonic-gate                     if ( origid == msgid && lr->lr_parent != NULL ) {
143*7c478bd9Sstevel@tonic-gate                         /* don't let caller abandon child requests! */
144*7c478bd9Sstevel@tonic-gate                         lderr = LDAP_PARAM_ERROR;
145*7c478bd9Sstevel@tonic-gate                         goto set_errorcode_and_return;
146*7c478bd9Sstevel@tonic-gate                     }
147*7c478bd9Sstevel@tonic-gate                     if ( lr->lr_status == LDAP_REQST_INPROGRESS ) {
148*7c478bd9Sstevel@tonic-gate                         /* We only need to send an abandon message if the request
149*7c478bd9Sstevel@tonic-gate                          * is in progress.
150*7c478bd9Sstevel@tonic-gate                          */
151*7c478bd9Sstevel@tonic-gate                         sendabandon = 1;
152*7c478bd9Sstevel@tonic-gate                     }
153*7c478bd9Sstevel@tonic-gate                     break;
154*7c478bd9Sstevel@tonic-gate                 }
155*7c478bd9Sstevel@tonic-gate                 if ( lr->lr_origid == msgid ) {	/* child:  abandon it */
156*7c478bd9Sstevel@tonic-gate                     (void)do_abandon( ld, msgid, lr->lr_msgid,
157*7c478bd9Sstevel@tonic-gate                                       serverctrls, clientctrls );
158*7c478bd9Sstevel@tonic-gate                     /* we ignore errors from child abandons... */
159*7c478bd9Sstevel@tonic-gate                 }
160*7c478bd9Sstevel@tonic-gate             }
161*7c478bd9Sstevel@tonic-gate         }
162*7c478bd9Sstevel@tonic-gate     }
163*7c478bd9Sstevel@tonic-gate     else
164*7c478bd9Sstevel@tonic-gate     {
165*7c478bd9Sstevel@tonic-gate         sendabandon = 1;
166*7c478bd9Sstevel@tonic-gate         /* find the request that we are abandoning */
167*7c478bd9Sstevel@tonic-gate         for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
168*7c478bd9Sstevel@tonic-gate             if ( lr->lr_msgid == msgid ) {	/* this message */
169*7c478bd9Sstevel@tonic-gate                 break;
170*7c478bd9Sstevel@tonic-gate             }
171*7c478bd9Sstevel@tonic-gate             if ( lr->lr_origid == msgid ) {	/* child:  abandon it */
172*7c478bd9Sstevel@tonic-gate                 (void)do_abandon( ld, msgid, lr->lr_msgid,
173*7c478bd9Sstevel@tonic-gate                                   serverctrls, clientctrls );
174*7c478bd9Sstevel@tonic-gate                 /* we ignore errors from child abandons... */
175*7c478bd9Sstevel@tonic-gate             }
176*7c478bd9Sstevel@tonic-gate         }
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate         if ( lr != NULL ) {
179*7c478bd9Sstevel@tonic-gate             if ( origid == msgid && lr->lr_parent != NULL ) {
180*7c478bd9Sstevel@tonic-gate                 /* don't let caller abandon child requests! */
181*7c478bd9Sstevel@tonic-gate                 lderr = LDAP_PARAM_ERROR;
182*7c478bd9Sstevel@tonic-gate                 goto set_errorcode_and_return;
183*7c478bd9Sstevel@tonic-gate             }
184*7c478bd9Sstevel@tonic-gate             if ( lr->lr_status != LDAP_REQST_INPROGRESS ) {
185*7c478bd9Sstevel@tonic-gate                 /* no need to send abandon message */
186*7c478bd9Sstevel@tonic-gate                 sendabandon = 0;
187*7c478bd9Sstevel@tonic-gate             }
188*7c478bd9Sstevel@tonic-gate         }
189*7c478bd9Sstevel@tonic-gate     }
190*7c478bd9Sstevel@tonic-gate 	if ( ldap_msgdelete( ld, msgid ) == 0 ) {
191*7c478bd9Sstevel@tonic-gate 		/* we had all the results and deleted them */
192*7c478bd9Sstevel@tonic-gate 		goto set_errorcode_and_return;
193*7c478bd9Sstevel@tonic-gate 	}
194*7c478bd9Sstevel@tonic-gate 
195*7c478bd9Sstevel@tonic-gate 	if ( sendabandon ) {
196*7c478bd9Sstevel@tonic-gate 		/* create a message to send */
197*7c478bd9Sstevel@tonic-gate 		if (( lderr = nsldapi_alloc_ber_with_options( ld, &ber )) ==
198*7c478bd9Sstevel@tonic-gate 		    LDAP_SUCCESS ) {
199*7c478bd9Sstevel@tonic-gate 			LDAP_MUTEX_LOCK( ld, LDAP_MSGID_LOCK );
200*7c478bd9Sstevel@tonic-gate #ifdef CLDAP
201*7c478bd9Sstevel@tonic-gate 			if ( ld->ld_dbp->sb_naddr > 0 ) {
202*7c478bd9Sstevel@tonic-gate 				bererr = ber_printf( ber, "{isti",
203*7c478bd9Sstevel@tonic-gate 				    ++ld->ld_msgid, ld->ld_cldapdn,
204*7c478bd9Sstevel@tonic-gate 				    LDAP_REQ_ABANDON, msgid );
205*7c478bd9Sstevel@tonic-gate 			} else {
206*7c478bd9Sstevel@tonic-gate #endif /* CLDAP */
207*7c478bd9Sstevel@tonic-gate 				bererr = ber_printf( ber, "{iti",
208*7c478bd9Sstevel@tonic-gate 				    ++ld->ld_msgid, LDAP_REQ_ABANDON, msgid );
209*7c478bd9Sstevel@tonic-gate #ifdef CLDAP
210*7c478bd9Sstevel@tonic-gate 			}
211*7c478bd9Sstevel@tonic-gate #endif /* CLDAP */
212*7c478bd9Sstevel@tonic-gate 			LDAP_MUTEX_UNLOCK( ld, LDAP_MSGID_LOCK );
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate 			if ( bererr == -1 ||
215*7c478bd9Sstevel@tonic-gate 			    ( lderr = nsldapi_put_controls( ld, serverctrls,
216*7c478bd9Sstevel@tonic-gate 			    1, ber )) != LDAP_SUCCESS ) {
217*7c478bd9Sstevel@tonic-gate 				lderr = LDAP_ENCODING_ERROR;
218*7c478bd9Sstevel@tonic-gate 				ber_free( ber, 1 );
219*7c478bd9Sstevel@tonic-gate 			} else {
220*7c478bd9Sstevel@tonic-gate 				/* send the message */
221*7c478bd9Sstevel@tonic-gate 				if ( lr != NULL ) {
222*7c478bd9Sstevel@tonic-gate 					sb = lr->lr_conn->lconn_sb;
223*7c478bd9Sstevel@tonic-gate 				} else {
224*7c478bd9Sstevel@tonic-gate 					sb = ld->ld_sbp;
225*7c478bd9Sstevel@tonic-gate 				}
226*7c478bd9Sstevel@tonic-gate 				if ( nsldapi_ber_flush( ld, sb, ber, 1, 0 )
227*7c478bd9Sstevel@tonic-gate 				    != 0 ) {
228*7c478bd9Sstevel@tonic-gate 					lderr = LDAP_SERVER_DOWN;
229*7c478bd9Sstevel@tonic-gate 				}
230*7c478bd9Sstevel@tonic-gate 			}
231*7c478bd9Sstevel@tonic-gate 		}
232*7c478bd9Sstevel@tonic-gate 	}
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate 	if ( lr != NULL ) {
235*7c478bd9Sstevel@tonic-gate 		if ( sendabandon ) {
236*7c478bd9Sstevel@tonic-gate 			nsldapi_free_connection( ld, lr->lr_conn, NULL, NULL,
237*7c478bd9Sstevel@tonic-gate 			    0, 1 );
238*7c478bd9Sstevel@tonic-gate 		}
239*7c478bd9Sstevel@tonic-gate 		if ( origid == msgid ) {
240*7c478bd9Sstevel@tonic-gate 			nsldapi_free_request( ld, lr, 0 );
241*7c478bd9Sstevel@tonic-gate 		}
242*7c478bd9Sstevel@tonic-gate 	}
243*7c478bd9Sstevel@tonic-gate 
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate 	LDAP_MUTEX_LOCK( ld, LDAP_ABANDON_LOCK );
246*7c478bd9Sstevel@tonic-gate 	if ( ld->ld_abandoned == NULL ) {
247*7c478bd9Sstevel@tonic-gate 		if ( (ld->ld_abandoned = (int *)NSLDAPI_MALLOC( 2
248*7c478bd9Sstevel@tonic-gate 		    * sizeof(int) )) == NULL ) {
249*7c478bd9Sstevel@tonic-gate 			lderr = LDAP_NO_MEMORY;
250*7c478bd9Sstevel@tonic-gate 			LDAP_MUTEX_UNLOCK( ld, LDAP_ABANDON_LOCK );
251*7c478bd9Sstevel@tonic-gate 			goto set_errorcode_and_return;
252*7c478bd9Sstevel@tonic-gate 		}
253*7c478bd9Sstevel@tonic-gate 		i = 0;
254*7c478bd9Sstevel@tonic-gate 	} else {
255*7c478bd9Sstevel@tonic-gate 		for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
256*7c478bd9Sstevel@tonic-gate 			;	/* NULL */
257*7c478bd9Sstevel@tonic-gate 		if ( (ld->ld_abandoned = (int *)NSLDAPI_REALLOC( (char *)
258*7c478bd9Sstevel@tonic-gate 		    ld->ld_abandoned, (i + 2) * sizeof(int) )) == NULL ) {
259*7c478bd9Sstevel@tonic-gate 			lderr = LDAP_NO_MEMORY;
260*7c478bd9Sstevel@tonic-gate 			LDAP_MUTEX_UNLOCK( ld, LDAP_ABANDON_LOCK );
261*7c478bd9Sstevel@tonic-gate 			goto set_errorcode_and_return;
262*7c478bd9Sstevel@tonic-gate 		}
263*7c478bd9Sstevel@tonic-gate 	}
264*7c478bd9Sstevel@tonic-gate 	ld->ld_abandoned[i] = msgid;
265*7c478bd9Sstevel@tonic-gate 	ld->ld_abandoned[i + 1] = -1;
266*7c478bd9Sstevel@tonic-gate 	LDAP_MUTEX_UNLOCK( ld, LDAP_ABANDON_LOCK );
267*7c478bd9Sstevel@tonic-gate 
268*7c478bd9Sstevel@tonic-gate set_errorcode_and_return:
269*7c478bd9Sstevel@tonic-gate 	LDAP_SET_LDERRNO( ld, lderr, NULL, NULL );
270*7c478bd9Sstevel@tonic-gate 	return( lderr );
271*7c478bd9Sstevel@tonic-gate }
272