]> git.sur5r.net Git - openldap/blob - libraries/libldap/abandon.c
don't risk dangling pointers
[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 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 start_again:;
128         lr = ld->ld_requests;
129         while ( lr != NULL ) {
130                 if ( lr->lr_msgid == msgid ) {  /* this message */
131                         break;
132                 }
133
134                 if ( lr->lr_origid == msgid ) {/* child:  abandon it */
135                         (void)do_abandon( ld,
136                                 lr->lr_origid, lr->lr_msgid, sctrls, cctrls );
137
138                         /* restart, as lr may now be dangling... */
139                         goto start_again;
140                 }
141
142                 lr = lr->lr_next;
143         }
144
145         if ( lr != NULL ) {
146                 if ( origid == msgid && lr->lr_parent != NULL ) {
147                         /* don't let caller abandon child requests! */
148                         ld->ld_errno = LDAP_PARAM_ERROR;
149                         return( LDAP_PARAM_ERROR );
150                 }
151                 if ( lr->lr_status != LDAP_REQST_INPROGRESS ) {
152                         /* no need to send abandon message */
153                         sendabandon = 0;
154                 }
155         }
156
157 /* ldap_msgdelete locks the res_mutex. Give up the req_mutex
158  * while we're in there.
159  */
160 #ifdef LDAP_R_COMPILE
161         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
162 #endif
163         err = ldap_msgdelete( ld, msgid );
164 #ifdef LDAP_R_COMPILE
165         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
166 #endif
167         if ( err == 0 ) {
168                 ld->ld_errno = LDAP_SUCCESS;
169                 return LDAP_SUCCESS;
170         }
171
172         /* fetch again the request that we are abandoning */
173         if ( lr != NULL ) {
174                 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
175                         if ( lr->lr_msgid == msgid ) {  /* this message */
176                                 break;
177                         }
178                 }
179         }
180
181         err = 0;
182         if ( sendabandon ) {
183                 if( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, NULL ) == -1 ) {
184                         /* not connected */
185                         err = -1;
186                         ld->ld_errno = LDAP_SERVER_DOWN;
187
188                 } else if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
189                         /* BER element alocation failed */
190                         err = -1;
191                         ld->ld_errno = LDAP_NO_MEMORY;
192
193                 } else {
194         /*
195          * We already have the mutex in LDAP_R_COMPILE, so
196          * don't try to get it again.
197          *              LDAP_NEXT_MSGID(ld, i);
198          */
199                         i = ++(ld)->ld_msgid;
200 #ifdef LDAP_CONNECTIONLESS
201                         if ( LDAP_IS_UDP(ld) ) {
202                                 err = ber_write( ber, ld->ld_options.ldo_peer,
203                                         sizeof(struct sockaddr), 0);
204                         }
205                         if ( LDAP_IS_UDP(ld) && ld->ld_options.ldo_version ==
206                                 LDAP_VERSION2 )
207                         {
208                                 char *dn = ld->ld_options.ldo_cldapdn;
209                                 if (!dn) dn = "";
210                                 err = ber_printf( ber, "{isti",  /* '}' */
211                                         i, dn,
212                                         LDAP_REQ_ABANDON, msgid );
213                         } else
214 #endif
215                         {
216                                 /* create a message to send */
217                                 err = ber_printf( ber, "{iti",  /* '}' */
218                                         i,
219                                         LDAP_REQ_ABANDON, msgid );
220                         }
221
222                         if( err == -1 ) {
223                                 /* encoding error */
224                                 ld->ld_errno = LDAP_ENCODING_ERROR;
225
226                         } else {
227                                 /* Put Server Controls */
228                                 if ( ldap_int_put_controls( ld, sctrls, ber )
229                                         != LDAP_SUCCESS )
230                                 {
231                                         err = -1;
232
233                                 } else {
234                                         /* close '{' */
235                                         err = ber_printf( ber, /*{*/ "N}" );
236
237                                         if( err == -1 ) {
238                                                 /* encoding error */
239                                                 ld->ld_errno = LDAP_ENCODING_ERROR;
240                                         }
241                                 }
242                         }
243
244                         if ( err == -1 ) {
245                                 ber_free( ber, 1 );
246
247                         } else {
248                                 /* send the message */
249                                 if ( lr != NULL ) {
250                                         assert( lr->lr_conn != NULL );
251                                         sb = lr->lr_conn->lconn_sb;
252                                 } else {
253                                         sb = ld->ld_sb;
254                                 }
255
256                                 if ( ber_flush2( sb, ber, LBER_FLUSH_FREE_ALWAYS ) != 0 ) {
257                                         ld->ld_errno = LDAP_SERVER_DOWN;
258                                         err = -1;
259                                 } else {
260                                         err = 0;
261                                 }
262                         }
263                 }
264         }
265
266         if ( lr != NULL ) {
267                 if ( sendabandon || lr->lr_status == LDAP_REQST_WRITING ) {
268                         ldap_free_connection( ld, lr->lr_conn, 0, 1 );
269                 }
270                 if ( origid == msgid ) {
271                         ldap_free_request( ld, lr );
272                 }
273         }
274
275 #ifdef LDAP_R_COMPILE
276         /* ld_abandoned is actually protected by the ld_res_mutex;
277          * give up the ld_req_mutex and get the other */
278         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
279         ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
280 #endif
281         i = 0;
282         if ( ld->ld_abandoned != NULL ) {
283                 for ( ; ld->ld_abandoned[i] != -1; i++ )
284                         ;       /* NULL */
285         }
286
287         old_abandon = ld->ld_abandoned;
288
289         ld->ld_abandoned = (ber_int_t *) LDAP_REALLOC( (char *)
290                 ld->ld_abandoned, (i + 2) * sizeof(ber_int_t) );
291                 
292         if ( ld->ld_abandoned == NULL ) {
293                 ld->ld_abandoned = old_abandon;
294                 ld->ld_errno = LDAP_NO_MEMORY;
295                 goto done;
296         }
297
298         ld->ld_abandoned[i] = msgid;
299         ld->ld_abandoned[i + 1] = -1;
300
301         if ( err != -1 ) {
302                 ld->ld_errno = LDAP_SUCCESS;
303         }
304
305 done:;
306 #ifdef LDAP_R_COMPILE
307         ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
308         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
309 #endif
310         return( ld->ld_errno );
311 }