]> git.sur5r.net Git - openldap/blob - libraries/libldap/abandon.c
ITS#837: fix server down abandon bug
[openldap] / libraries / libldap / abandon.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*  Portions
7  *  Copyright (c) 1990 Regents of the University of Michigan.
8  *  All rights reserved.
9  *
10  *  abandon.c
11  */
12
13 /*
14  * An abandon request looks like this:
15  *      AbandonRequest ::= MessageID
16  */
17
18 #include "portable.h"
19
20 #include <stdio.h>
21
22 #include <ac/stdlib.h>
23
24 #include <ac/socket.h>
25 #include <ac/string.h>
26 #include <ac/time.h>
27
28 #include "ldap-int.h"
29
30 static int do_abandon LDAP_P((
31         LDAP *ld,
32         ber_int_t origid,
33         ber_int_t msgid,
34         LDAPControl **sctrls,
35         LDAPControl **cctrls));
36
37 /*
38  * ldap_abandon_ext - perform an ldap extended abandon operation.
39  *
40  * Parameters:
41  *      ld                      LDAP descriptor
42  *      msgid           The message id of the operation to abandon
43  *      scntrls         Server Controls
44  *      ccntrls         Client Controls
45  *
46  * ldap_abandon_ext returns a LDAP error code.
47  *              (LDAP_SUCCESS if everything went ok)
48  *
49  * Example:
50  *      ldap_abandon_ext( ld, msgid, scntrls, ccntrls );
51  */
52 int
53 ldap_abandon_ext(
54         LDAP *ld,
55         int msgid,
56         LDAPControl **sctrls,
57         LDAPControl **cctrls )
58 {
59         Debug( LDAP_DEBUG_TRACE, "ldap_abandon_ext %d\n", msgid, 0, 0 );
60
61         return do_abandon( ld, msgid, msgid, sctrls, cctrls );
62 }
63
64
65 /*
66  * ldap_abandon - perform an ldap abandon operation. Parameters:
67  *
68  *      ld              LDAP descriptor
69  *      msgid           The message id of the operation to abandon
70  *
71  * ldap_abandon returns 0 if everything went ok, -1 otherwise.
72  *
73  * Example:
74  *      ldap_abandon( ld, msgid );
75  */
76 int
77 ldap_abandon( LDAP *ld, int msgid )
78 {
79         Debug( LDAP_DEBUG_TRACE, "ldap_abandon %d\n", msgid, 0, 0 );
80         return do_abandon( ld, msgid, msgid, NULL, NULL ) == LDAP_SUCCESS
81                 ? 0 : -1;
82 }
83
84
85 static int
86 do_abandon(
87         LDAP *ld,
88         ber_int_t origid,
89         ber_int_t msgid,
90         LDAPControl **sctrls,
91         LDAPControl **cctrls)
92 {
93         BerElement      *ber;
94         int             i, err, sendabandon;
95         ber_int_t *old_abandon;
96         Sockbuf         *sb;
97         LDAPRequest     *lr;
98
99         Debug( LDAP_DEBUG_TRACE, "do_abandon origid %d, msgid %d\n",
100                 origid, msgid, 0 );
101
102         sendabandon = 1;
103
104         /* find the request that we are abandoning */
105         for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
106                 if ( lr->lr_msgid == msgid ) {  /* this message */
107                         break;
108                 }
109                 if ( lr->lr_origid == msgid ) {/* child:  abandon it */
110                         (void) do_abandon( ld,
111                                 msgid, lr->lr_msgid, sctrls, cctrls );
112                 }
113         }
114
115         if ( lr != NULL ) {
116                 if ( origid == msgid && lr->lr_parent != NULL ) {
117                         /* don't let caller abandon child requests! */
118                         ld->ld_errno = LDAP_PARAM_ERROR;
119                         return( LDAP_PARAM_ERROR );
120                 }
121                 if ( lr->lr_status != LDAP_REQST_INPROGRESS ) {
122                         /* no need to send abandon message */
123                         sendabandon = 0;
124                 }
125         }
126
127         if ( ldap_msgdelete( ld, msgid ) == 0 ) {
128                 ld->ld_errno = LDAP_SUCCESS;
129                 return LDAP_SUCCESS;
130         }
131
132         err = 0;
133         if ( sendabandon ) {
134                 if( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, NULL ) == -1 ) {
135                         /* not connected */
136                         err = -1;
137                         ld->ld_errno = LDAP_SERVER_DOWN;
138
139                 } else if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
140                         /* BER element alocation failed */
141                         err = -1;
142                         ld->ld_errno = LDAP_NO_MEMORY;
143
144                 } else {
145                         /* create a message to send */
146                         err = ber_printf( ber, "{iti",  /* '}' */
147                                 ++ld->ld_msgid,
148                             LDAP_REQ_ABANDON, msgid );
149
150                         if( err == -1 ) {
151                                 /* encoding error */
152                                 ld->ld_errno = LDAP_ENCODING_ERROR;
153
154                         } else {
155                                 /* Put Server Controls */
156                                 if ( ldap_int_put_controls( ld, sctrls, ber )
157                                         != LDAP_SUCCESS )
158                                 {
159                                         err = -1;
160
161                                 } else {
162                                         /* close '{' */
163                                         err = ber_printf( ber, /*{*/ "N}" );
164
165                                         if( err == -1 ) {
166                                                 /* encoding error */
167                                                 ld->ld_errno = LDAP_ENCODING_ERROR;
168                                         }
169                                 }
170                         }
171
172                         if ( err == -1 ) {
173                                 ber_free( ber, 1 );
174
175                         } else {
176                                 /* send the message */
177                                 if ( lr != NULL ) {
178                                         sb = lr->lr_conn->lconn_sb;
179                                 } else {
180                                         sb = ld->ld_sb;
181                                 }
182
183                                 if ( ber_flush( sb, ber, 1 ) != 0 ) {
184                                         ld->ld_errno = LDAP_SERVER_DOWN;
185                                         err = -1;
186                                 } else {
187                                         err = 0;
188                                 }
189                         }
190                 }
191         }
192
193         if ( lr != NULL ) {
194                 if ( sendabandon ) {
195                         ldap_free_connection( ld, lr->lr_conn, 0, 1 );
196                 }
197                 if ( origid == msgid ) {
198                         ldap_free_request( ld, lr );
199                 }
200         }
201
202         i = 0;
203         if ( ld->ld_abandoned != NULL ) {
204                 for ( ; ld->ld_abandoned[i] != -1; i++ )
205                         ;       /* NULL */
206         }
207
208         old_abandon = ld->ld_abandoned;
209
210         ld->ld_abandoned = (ber_int_t *) LDAP_REALLOC( (char *)
211                 ld->ld_abandoned, (i + 2) * sizeof(ber_int_t) );
212                 
213         if ( ld->ld_abandoned == NULL ) {
214                 ld->ld_abandoned = old_abandon;
215                 ld->ld_errno = LDAP_NO_MEMORY;
216                 return( ld->ld_errno );
217         }
218
219         ld->ld_abandoned[i] = msgid;
220         ld->ld_abandoned[i + 1] = -1;
221
222         if ( err != -1 ) {
223                 ld->ld_errno = LDAP_SUCCESS;
224         }
225
226         return( ld->ld_errno );
227 }