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