]> git.sur5r.net Git - openldap/blob - libraries/libldap/abandon.c
Happy new year
[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-2004 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                         LDAP_NEXT_MSGID(ld, i);
190 #ifdef LDAP_CONNECTIONLESS
191                         if ( LDAP_IS_UDP(ld) ) {
192                             err = ber_write( ber, ld->ld_options.ldo_peer,
193                                 sizeof(struct sockaddr), 0);
194                         }
195                         if ( LDAP_IS_UDP(ld) && ld->ld_options.ldo_version ==
196                                 LDAP_VERSION2) {
197                             char *dn = ld->ld_options.ldo_cldapdn;
198                             if (!dn) dn = "";
199                             err = ber_printf( ber, "{isti",  /* '}' */
200                                 i, dn,
201                                 LDAP_REQ_ABANDON, msgid );
202                         } else
203 #endif
204                         {
205                             /* create a message to send */
206                             err = ber_printf( ber, "{iti",  /* '}' */
207                                 i,
208                                 LDAP_REQ_ABANDON, msgid );
209                         }
210
211                         if( err == -1 ) {
212                                 /* encoding error */
213                                 ld->ld_errno = LDAP_ENCODING_ERROR;
214
215                         } else {
216                                 /* Put Server Controls */
217                                 if ( ldap_int_put_controls( ld, sctrls, ber )
218                                         != LDAP_SUCCESS )
219                                 {
220                                         err = -1;
221
222                                 } else {
223                                         /* close '{' */
224                                         err = ber_printf( ber, /*{*/ "N}" );
225
226                                         if( err == -1 ) {
227                                                 /* encoding error */
228                                                 ld->ld_errno = LDAP_ENCODING_ERROR;
229                                         }
230                                 }
231                         }
232
233                         if ( err == -1 ) {
234                                 ber_free( ber, 1 );
235
236                         } else {
237                                 /* send the message */
238                                 if ( lr != NULL ) {
239                                         sb = lr->lr_conn->lconn_sb;
240                                 } else {
241                                         sb = ld->ld_sb;
242                                 }
243
244                                 if ( ber_flush( sb, ber, 1 ) != 0 ) {
245                                         ld->ld_errno = LDAP_SERVER_DOWN;
246                                         err = -1;
247                                 } else {
248                                         err = 0;
249                                 }
250                         }
251                 }
252         }
253
254         if ( lr != NULL ) {
255                 if ( sendabandon || lr->lr_status == LDAP_REQST_WRITING ) {
256                         ldap_free_connection( ld, lr->lr_conn, 0, 1 );
257                 }
258                 if ( origid == msgid ) {
259                         ldap_free_request( ld, lr );
260                 }
261         }
262
263         i = 0;
264         if ( ld->ld_abandoned != NULL ) {
265                 for ( ; ld->ld_abandoned[i] != -1; i++ )
266                         ;       /* NULL */
267         }
268
269         old_abandon = ld->ld_abandoned;
270
271         ld->ld_abandoned = (ber_int_t *) LDAP_REALLOC( (char *)
272                 ld->ld_abandoned, (i + 2) * sizeof(ber_int_t) );
273                 
274         if ( ld->ld_abandoned == NULL ) {
275                 ld->ld_abandoned = old_abandon;
276                 ld->ld_errno = LDAP_NO_MEMORY;
277                 return( ld->ld_errno );
278         }
279
280         ld->ld_abandoned[i] = msgid;
281         ld->ld_abandoned[i + 1] = -1;
282
283         if ( err != -1 ) {
284                 ld->ld_errno = LDAP_SUCCESS;
285         }
286
287         return( ld->ld_errno );
288 }