]> git.sur5r.net Git - openldap/blob - libraries/libldap/abandon.c
27b47b5f57afd591a11fefabcdfdf852f9f07cd3
[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                                 msgid, 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         err = 0;
165         if ( sendabandon ) {
166                 if( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, NULL ) == -1 ) {
167                         /* not connected */
168                         err = -1;
169                         ld->ld_errno = LDAP_SERVER_DOWN;
170
171                 } else if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
172                         /* BER element alocation failed */
173                         err = -1;
174                         ld->ld_errno = LDAP_NO_MEMORY;
175
176                 } else {
177         /*
178          * We already have the mutex in LDAP_R_COMPILE, so
179          * don't try to get it again.
180          *              LDAP_NEXT_MSGID(ld, i);
181          */
182                         i = ++(ld)->ld_msgid;
183 #ifdef LDAP_CONNECTIONLESS
184                         if ( LDAP_IS_UDP(ld) ) {
185                             err = ber_write( ber, ld->ld_options.ldo_peer,
186                                 sizeof(struct sockaddr), 0);
187                         }
188                         if ( LDAP_IS_UDP(ld) && ld->ld_options.ldo_version ==
189                                 LDAP_VERSION2) {
190                             char *dn = ld->ld_options.ldo_cldapdn;
191                             if (!dn) dn = "";
192                             err = ber_printf( ber, "{isti",  /* '}' */
193                                 i, dn,
194                                 LDAP_REQ_ABANDON, msgid );
195                         } else
196 #endif
197                         {
198                             /* create a message to send */
199                             err = ber_printf( ber, "{iti",  /* '}' */
200                                 i,
201                                 LDAP_REQ_ABANDON, msgid );
202                         }
203
204                         if( err == -1 ) {
205                                 /* encoding error */
206                                 ld->ld_errno = LDAP_ENCODING_ERROR;
207
208                         } else {
209                                 /* Put Server Controls */
210                                 if ( ldap_int_put_controls( ld, sctrls, ber )
211                                         != LDAP_SUCCESS )
212                                 {
213                                         err = -1;
214
215                                 } else {
216                                         /* close '{' */
217                                         err = ber_printf( ber, /*{*/ "N}" );
218
219                                         if( err == -1 ) {
220                                                 /* encoding error */
221                                                 ld->ld_errno = LDAP_ENCODING_ERROR;
222                                         }
223                                 }
224                         }
225
226                         if ( err == -1 ) {
227                                 ber_free( ber, 1 );
228
229                         } else {
230                                 /* send the message */
231                                 if ( lr != NULL ) {
232                                         sb = lr->lr_conn->lconn_sb;
233                                 } else {
234                                         sb = ld->ld_sb;
235                                 }
236
237                                 if ( ber_flush( sb, ber, 1 ) != 0 ) {
238                                         ld->ld_errno = LDAP_SERVER_DOWN;
239                                         err = -1;
240                                 } else {
241                                         err = 0;
242                                 }
243                         }
244                 }
245         }
246
247         if ( lr != NULL ) {
248                 if ( sendabandon || lr->lr_status == LDAP_REQST_WRITING ) {
249                         ldap_free_connection( ld, lr->lr_conn, 0, 1 );
250                 }
251                 if ( origid == msgid ) {
252                         ldap_free_request( ld, lr );
253                 }
254         }
255
256         i = 0;
257         if ( ld->ld_abandoned != NULL ) {
258                 for ( ; ld->ld_abandoned[i] != -1; i++ )
259                         ;       /* NULL */
260         }
261
262         old_abandon = ld->ld_abandoned;
263
264         ld->ld_abandoned = (ber_int_t *) LDAP_REALLOC( (char *)
265                 ld->ld_abandoned, (i + 2) * sizeof(ber_int_t) );
266                 
267         if ( ld->ld_abandoned == NULL ) {
268                 ld->ld_abandoned = old_abandon;
269                 ld->ld_errno = LDAP_NO_MEMORY;
270                 return( ld->ld_errno );
271         }
272
273         ld->ld_abandoned[i] = msgid;
274         ld->ld_abandoned[i + 1] = -1;
275
276         if ( err != -1 ) {
277                 ld->ld_errno = LDAP_SUCCESS;
278         }
279
280         return( ld->ld_errno );
281 }