]> git.sur5r.net Git - openldap/blob - libraries/libldap/abandon.c
plug leaks; cleanup
[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-2005 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 do_abandon LDAP_P((
41         LDAP *ld,
42         ber_int_t origid,
43         ber_int_t msgid,
44         LDAPControl **sctrls,
45         LDAPControl **cctrls));
46
47 /*
48  * ldap_abandon_ext - perform an ldap extended abandon operation.
49  *
50  * Parameters:
51  *      ld                      LDAP descriptor
52  *      msgid           The message id of the operation to abandon
53  *      scntrls         Server Controls
54  *      ccntrls         Client Controls
55  *
56  * ldap_abandon_ext returns a LDAP error code.
57  *              (LDAP_SUCCESS if everything went ok)
58  *
59  * Example:
60  *      ldap_abandon_ext( ld, msgid, scntrls, ccntrls );
61  */
62 int
63 ldap_abandon_ext(
64         LDAP *ld,
65         int msgid,
66         LDAPControl **sctrls,
67         LDAPControl **cctrls )
68 {
69         int rc;
70         Debug( LDAP_DEBUG_TRACE, "ldap_abandon_ext %d\n", msgid, 0, 0 );
71
72         /* check client controls */
73 #ifdef LDAP_R_COMPILE
74         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
75 #endif
76         rc = ldap_int_client_controls( ld, cctrls );
77         if( rc == LDAP_SUCCESS )
78                 rc = do_abandon( ld, msgid, msgid, sctrls, cctrls );
79
80 #ifdef LDAP_R_COMPILE
81         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
82 #endif
83         return rc;
84 }
85
86
87 /*
88  * ldap_abandon - perform an ldap abandon operation. Parameters:
89  *
90  *      ld              LDAP descriptor
91  *      msgid           The message id of the operation to abandon
92  *
93  * ldap_abandon returns 0 if everything went ok, -1 otherwise.
94  *
95  * Example:
96  *      ldap_abandon( ld, msgid );
97  */
98 int
99 ldap_abandon( LDAP *ld, int msgid )
100 {
101         Debug( LDAP_DEBUG_TRACE, "ldap_abandon %d\n", msgid, 0, 0 );
102         return ldap_abandon_ext( ld, msgid, NULL, NULL ) == LDAP_SUCCESS
103                 ? 0 : -1;
104 }
105
106
107 static int
108 do_abandon(
109         LDAP *ld,
110         ber_int_t origid,
111         ber_int_t msgid,
112         LDAPControl **sctrls,
113         LDAPControl **cctrls)
114 {
115         BerElement      *ber;
116         int             i, err, sendabandon;
117         ber_int_t *old_abandon;
118         Sockbuf         *sb;
119         LDAPRequest     *lr;
120
121         Debug( LDAP_DEBUG_TRACE, "do_abandon origid %d, msgid %d\n",
122                 origid, msgid, 0 );
123
124         sendabandon = 1;
125
126         /* find the request that we are abandoning */
127         for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
128                 if ( lr->lr_msgid == msgid ) {  /* this message */
129                         break;
130                 }
131                 if ( lr->lr_origid == msgid ) {/* child:  abandon it */
132                         (void) do_abandon( ld,
133                                 lr->lr_origid, lr->lr_msgid, sctrls, cctrls );
134                 }
135         }
136
137         if ( lr != NULL ) {
138                 if ( origid == msgid && lr->lr_parent != NULL ) {
139                         /* don't let caller abandon child requests! */
140                         ld->ld_errno = LDAP_PARAM_ERROR;
141                         return( LDAP_PARAM_ERROR );
142                 }
143                 if ( lr->lr_status != LDAP_REQST_INPROGRESS ) {
144                         /* no need to send abandon message */
145                         sendabandon = 0;
146                 }
147         }
148
149 /* ldap_msgdelete locks the res_mutex. Give up the req_mutex
150  * while we're in there.
151  */
152 #ifdef LDAP_R_COMPILE
153         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
154 #endif
155         err = ldap_msgdelete( ld, msgid );
156 #ifdef LDAP_R_COMPILE
157         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
158 #endif
159         if ( err == 0 ) {
160                 ld->ld_errno = LDAP_SUCCESS;
161                 return LDAP_SUCCESS;
162         }
163
164         /* fetch again the request that we are abandoning */
165         if ( lr != NULL ) {
166                 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
167                         if ( lr->lr_msgid == msgid ) {  /* this message */
168                                 break;
169                         }
170                 }
171         }
172
173         err = 0;
174         if ( sendabandon ) {
175                 if( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, NULL ) == -1 ) {
176                         /* not connected */
177                         err = -1;
178                         ld->ld_errno = LDAP_SERVER_DOWN;
179
180                 } else if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
181                         /* BER element alocation failed */
182                         err = -1;
183                         ld->ld_errno = LDAP_NO_MEMORY;
184
185                 } else {
186         /*
187          * We already have the mutex in LDAP_R_COMPILE, so
188          * don't try to get it again.
189          *              LDAP_NEXT_MSGID(ld, i);
190          */
191                         i = ++(ld)->ld_msgid;
192 #ifdef LDAP_CONNECTIONLESS
193                         if ( LDAP_IS_UDP(ld) ) {
194                             err = ber_write( ber, ld->ld_options.ldo_peer,
195                                 sizeof(struct sockaddr), 0);
196                         }
197                         if ( LDAP_IS_UDP(ld) && ld->ld_options.ldo_version ==
198                                 LDAP_VERSION2) {
199                             char *dn = ld->ld_options.ldo_cldapdn;
200                             if (!dn) dn = "";
201                             err = ber_printf( ber, "{isti",  /* '}' */
202                                 i, dn,
203                                 LDAP_REQ_ABANDON, msgid );
204                         } else
205 #endif
206                         {
207                             /* create a message to send */
208                             err = ber_printf( ber, "{iti",  /* '}' */
209                                 i,
210                                 LDAP_REQ_ABANDON, msgid );
211                         }
212
213                         if( err == -1 ) {
214                                 /* encoding error */
215                                 ld->ld_errno = LDAP_ENCODING_ERROR;
216
217                         } else {
218                                 /* Put Server Controls */
219                                 if ( ldap_int_put_controls( ld, sctrls, ber )
220                                         != LDAP_SUCCESS )
221                                 {
222                                         err = -1;
223
224                                 } else {
225                                         /* close '{' */
226                                         err = ber_printf( ber, /*{*/ "N}" );
227
228                                         if( err == -1 ) {
229                                                 /* encoding error */
230                                                 ld->ld_errno = LDAP_ENCODING_ERROR;
231                                         }
232                                 }
233                         }
234
235                         if ( err == -1 ) {
236                                 ber_free( ber, 1 );
237
238                         } else {
239                                 /* send the message */
240                                 if ( lr != NULL ) {
241                                         assert( lr->lr_conn != NULL );
242                                         sb = lr->lr_conn->lconn_sb;
243                                 } else {
244                                         sb = ld->ld_sb;
245                                 }
246
247                                 if ( ber_flush( sb, ber, 1 ) != 0 ) {
248                                         ld->ld_errno = LDAP_SERVER_DOWN;
249                                         err = -1;
250                                 } else {
251                                         err = 0;
252                                 }
253                         }
254                 }
255         }
256
257         if ( lr != NULL ) {
258                 if ( sendabandon || lr->lr_status == LDAP_REQST_WRITING ) {
259                         ldap_free_connection( ld, lr->lr_conn, 0, 1 );
260                 }
261                 if ( origid == msgid ) {
262                         ldap_free_request( ld, lr );
263                 }
264         }
265
266 #ifdef LDAP_R_COMPILE
267         /* ld_abandoned is actually protected by the ld_res_mutex;
268          * give up the ld_req_mutex and get the other */
269         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
270         ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
271 #endif
272         i = 0;
273         if ( ld->ld_abandoned != NULL ) {
274                 for ( ; ld->ld_abandoned[i] != -1; i++ )
275                         ;       /* NULL */
276         }
277
278         old_abandon = ld->ld_abandoned;
279
280         ld->ld_abandoned = (ber_int_t *) LDAP_REALLOC( (char *)
281                 ld->ld_abandoned, (i + 2) * sizeof(ber_int_t) );
282                 
283         if ( ld->ld_abandoned == NULL ) {
284                 ld->ld_abandoned = old_abandon;
285                 ld->ld_errno = LDAP_NO_MEMORY;
286                 goto done;
287         }
288
289         ld->ld_abandoned[i] = msgid;
290         ld->ld_abandoned[i + 1] = -1;
291
292         if ( err != -1 ) {
293                 ld->ld_errno = LDAP_SUCCESS;
294         }
295
296 done:;
297 #ifdef LDAP_R_COMPILE
298         ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
299         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
300 #endif
301         return( ld->ld_errno );
302 }