]> git.sur5r.net Git - openldap/blob - libraries/libldap/abandon.c
add ldap_int_discard(); use it in proxies (ITS#4717)
[openldap] / libraries / libldap / abandon.c
1 /* abandon.c */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2006 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* Portions  Copyright (c) 1990 Regents of the University of Michigan.
17  * All rights reserved.
18  */
19 /* Portions Copyright (C) The Internet Society (1997).
20  * ASN.1 fragments are from RFC 2251; see RFC for full legal notices.
21  */
22
23 /*
24  * An abandon request looks like this:
25  *      AbandonRequest ::= MessageID
26  */
27
28 #include "portable.h"
29
30 #include <stdio.h>
31
32 #include <ac/stdlib.h>
33
34 #include <ac/socket.h>
35 #include <ac/string.h>
36 #include <ac/time.h>
37
38 #include "ldap-int.h"
39
40 static int
41 do_abandon(
42         LDAP *ld,
43         ber_int_t origid,
44         ber_int_t msgid,
45         LDAPControl **sctrls,
46         int sendabandon );
47
48 /*
49  * ldap_abandon_ext - perform an ldap extended abandon operation.
50  *
51  * Parameters:
52  *      ld                      LDAP descriptor
53  *      msgid           The message id of the operation to abandon
54  *      scntrls         Server Controls
55  *      ccntrls         Client Controls
56  *
57  * ldap_abandon_ext returns a LDAP error code.
58  *              (LDAP_SUCCESS if everything went ok)
59  *
60  * Example:
61  *      ldap_abandon_ext( ld, msgid, scntrls, ccntrls );
62  */
63 int
64 ldap_abandon_ext(
65         LDAP *ld,
66         int msgid,
67         LDAPControl **sctrls,
68         LDAPControl **cctrls )
69 {
70         int     rc;
71
72         Debug( LDAP_DEBUG_TRACE, "ldap_abandon_ext %d\n", msgid, 0, 0 );
73
74         /* check client controls */
75 #ifdef LDAP_R_COMPILE
76         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
77 #endif
78
79         rc = ldap_int_client_controls( ld, cctrls );
80         if ( rc == LDAP_SUCCESS ) {
81                 rc = do_abandon( ld, msgid, msgid, sctrls, 1 );
82         }
83
84 #ifdef LDAP_R_COMPILE
85         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
86 #endif
87
88         return rc;
89 }
90
91
92 /*
93  * ldap_abandon - perform an ldap abandon operation. Parameters:
94  *
95  *      ld              LDAP descriptor
96  *      msgid           The message id of the operation to abandon
97  *
98  * ldap_abandon returns 0 if everything went ok, -1 otherwise.
99  *
100  * Example:
101  *      ldap_abandon( ld, msgid );
102  */
103 int
104 ldap_abandon( LDAP *ld, int msgid )
105 {
106         Debug( LDAP_DEBUG_TRACE, "ldap_abandon %d\n", msgid, 0, 0 );
107         return ldap_abandon_ext( ld, msgid, NULL, NULL ) == LDAP_SUCCESS
108                 ? 0 : -1;
109 }
110
111
112 int
113 ldap_int_discard(
114         LDAP *ld,
115         ber_int_t msgid )
116 {
117         int     rc;
118
119 #ifdef LDAP_R_COMPILE
120         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
121 #endif
122
123         rc = do_abandon( ld, msgid, msgid, NULL, 0 );
124
125 #ifdef LDAP_R_COMPILE
126         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
127 #endif
128
129         return rc;
130 }
131
132 static int
133 do_abandon(
134         LDAP *ld,
135         ber_int_t origid,
136         ber_int_t msgid,
137         LDAPControl **sctrls,
138         int sendabandon )
139 {
140         BerElement      *ber;
141         int             i, err;
142         ber_int_t       *old_abandon;
143         Sockbuf         *sb;
144         LDAPRequest     *lr;
145
146         Debug( LDAP_DEBUG_TRACE, "ldap_int_discard origid %d, msgid %d\n",
147                 origid, msgid, 0 );
148
149         /* find the request that we are abandoning */
150 start_again:;
151         lr = ld->ld_requests;
152         while ( lr != NULL ) {
153                 if ( lr->lr_msgid == msgid ) {  /* this message */
154                         break;
155                 }
156
157                 if ( lr->lr_origid == msgid ) {/* child:  abandon it */
158                         (void)do_abandon( ld, lr->lr_origid, lr->lr_msgid,
159                                 sctrls, sendabandon );
160
161                         /* restart, as lr may now be dangling... */
162                         goto start_again;
163                 }
164
165                 lr = lr->lr_next;
166         }
167
168         if ( lr != NULL ) {
169                 if ( origid == msgid && lr->lr_parent != NULL ) {
170                         /* don't let caller abandon child requests! */
171                         ld->ld_errno = LDAP_PARAM_ERROR;
172                         return( LDAP_PARAM_ERROR );
173                 }
174                 if ( lr->lr_status != LDAP_REQST_INPROGRESS ) {
175                         /* no need to send abandon message */
176                         sendabandon = 0;
177                 }
178         }
179
180         /* ldap_msgdelete locks the res_mutex. Give up the req_mutex
181          * while we're in there.
182          */
183 #ifdef LDAP_R_COMPILE
184         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
185 #endif
186         err = ldap_msgdelete( ld, msgid );
187 #ifdef LDAP_R_COMPILE
188         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
189 #endif
190         if ( err == 0 ) {
191                 ld->ld_errno = LDAP_SUCCESS;
192                 return LDAP_SUCCESS;
193         }
194
195         /* fetch again the request that we are abandoning */
196         if ( lr != NULL ) {
197                 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
198                         /* this message */
199                         if ( lr->lr_msgid == msgid ) {
200                                 break;
201                         }
202                 }
203         }
204
205         err = 0;
206         if ( sendabandon ) {
207                 if ( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, NULL ) == -1 ) {
208                         /* not connected */
209                         err = -1;
210                         ld->ld_errno = LDAP_SERVER_DOWN;
211
212                 } else if ( ( ber = ldap_alloc_ber_with_options( ld ) ) == NULL ) {
213                         /* BER element allocation failed */
214                         err = -1;
215                         ld->ld_errno = LDAP_NO_MEMORY;
216
217                 } else {
218                         /*
219                          * We already have the mutex in LDAP_R_COMPILE, so
220                          * don't try to get it again.
221                          *              LDAP_NEXT_MSGID(ld, i);
222                          */
223
224                         i = ++(ld)->ld_msgid;
225 #ifdef LDAP_CONNECTIONLESS
226                         if ( LDAP_IS_UDP(ld) ) {
227                                 err = ber_write( ber, ld->ld_options.ldo_peer,
228                                         sizeof(struct sockaddr), 0);
229                         }
230                         if ( LDAP_IS_UDP(ld) && ld->ld_options.ldo_version ==
231                                 LDAP_VERSION2 )
232                         {
233                                 char *dn = ld->ld_options.ldo_cldapdn;
234                                 if (!dn) dn = "";
235                                 err = ber_printf( ber, "{isti",  /* '}' */
236                                         i, dn,
237                                         LDAP_REQ_ABANDON, msgid );
238                         } else
239 #endif
240                         {
241                                 /* create a message to send */
242                                 err = ber_printf( ber, "{iti",  /* '}' */
243                                         i,
244                                         LDAP_REQ_ABANDON, msgid );
245                         }
246
247                         if ( err == -1 ) {
248                                 /* encoding error */
249                                 ld->ld_errno = LDAP_ENCODING_ERROR;
250
251                         } else {
252                                 /* Put Server Controls */
253                                 if ( ldap_int_put_controls( ld, sctrls, ber )
254                                         != LDAP_SUCCESS )
255                                 {
256                                         err = -1;
257
258                                 } else {
259                                         /* close '{' */
260                                         err = ber_printf( ber, /*{*/ "N}" );
261
262                                         if ( err == -1 ) {
263                                                 /* encoding error */
264                                                 ld->ld_errno = LDAP_ENCODING_ERROR;
265                                         }
266                                 }
267                         }
268
269                         if ( err == -1 ) {
270                                 ber_free( ber, 1 );
271
272                         } else {
273                                 /* send the message */
274                                 if ( lr != NULL ) {
275                                         assert( lr->lr_conn != NULL );
276                                         sb = lr->lr_conn->lconn_sb;
277                                 } else {
278                                         sb = ld->ld_sb;
279                                 }
280
281                                 if ( ber_flush2( sb, ber, LBER_FLUSH_FREE_ALWAYS ) != 0 ) {
282                                         ld->ld_errno = LDAP_SERVER_DOWN;
283                                         err = -1;
284                                 } else {
285                                         err = 0;
286                                 }
287                         }
288                 }
289         }
290
291         if ( lr != NULL ) {
292                 if ( sendabandon || lr->lr_status == LDAP_REQST_WRITING ) {
293                         ldap_free_connection( ld, lr->lr_conn, 0, 1 );
294                 }
295
296                 if ( origid == msgid ) {
297                         ldap_free_request( ld, lr );
298                 }
299         }
300
301 #ifdef LDAP_R_COMPILE
302         /* ld_abandoned is actually protected by the ld_res_mutex;
303          * give up the ld_req_mutex and get the other */
304         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
305         ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
306 #endif
307
308         /* use bisection */
309         i = 0;
310         if ( ld->ld_abandoned != NULL ) {
311                 int             begin,
312                                 end;
313
314                 assert( ld->ld_nabandoned >= 0 );
315
316                 begin = 0;
317                 end = ld->ld_nabandoned - 1;
318
319                 if ( ld->ld_nabandoned == 0 || ld->ld_abandoned[ begin ] > msgid ) {
320                         i = 0;
321
322                 } else if ( ld->ld_abandoned[ end ] < msgid ) {
323                         i = ld->ld_nabandoned;
324
325                 } else {
326                         int     pos, curid;
327
328                         while ( end >= begin ) {
329                                 pos = (begin + end)/2;
330                                 curid = ld->ld_abandoned[ pos ];
331
332                                 if ( msgid < curid ) {
333                                         end = pos - 1;
334
335                                 } else if ( msgid > curid ) {
336                                         begin = pos + 1;
337
338                                 } else {
339                                         /* already abandoned? */
340                                         i = -1;
341                                         break;
342                                 }
343                         }
344
345                         if ( i == 0 ) {
346                                 i = pos;
347                         }
348                 }
349         }
350
351         if ( i != -1 ) {
352                 int     pos = i;
353
354                 old_abandon = ld->ld_abandoned;
355
356                 ld->ld_abandoned = (ber_int_t *) LDAP_REALLOC( (char *)ld->ld_abandoned,
357                         ( ld->ld_nabandoned + 1 ) * sizeof( ber_int_t ) );
358
359                 if ( ld->ld_abandoned == NULL ) {
360                         ld->ld_abandoned = old_abandon;
361                         ld->ld_errno = LDAP_NO_MEMORY;
362                         goto done;
363                 }
364
365                 for ( i = ld->ld_nabandoned; i > pos; i-- ) {
366                         ld->ld_abandoned[ i ] = ld->ld_abandoned[ i - 1 ];
367                 }
368                 ld->ld_abandoned[ pos ] = msgid;
369                 ++ld->ld_nabandoned;
370         }
371
372         if ( err != -1 ) {
373                 ld->ld_errno = LDAP_SUCCESS;
374         }
375
376 done:;
377 #ifdef LDAP_R_COMPILE
378         ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
379         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
380 #endif
381         return( ld->ld_errno );
382 }