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