]> git.sur5r.net Git - openldap/blob - servers/slapd/ctxcsn.c
ITS#2751 additional fix
[openldap] / servers / slapd / ctxcsn.c
1 /* $OpenLDAP$ */
2 /*
3  * Context CSN Management Routines
4  */
5 /* Copyright (c) 2003 by International Business Machines, Inc.
6  *
7  * International Business Machines, Inc. (hereinafter called IBM) grants
8  * permission under its copyrights to use, copy, modify, and distribute this
9  * Software with or without fee, provided that the above copyright notice and
10  * all paragraphs of this notice appear in all copies, and that the name of IBM
11  * not be used in connection with the marketing of any product incorporating
12  * the Software or modifications thereof, without specific, written prior
13  * permission.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
16  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
17  * PARTICULAR PURPOSE.  IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
18  * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
19  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
20  * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
21  */
22
23 #include "portable.h"
24
25 #include <stdio.h>
26
27 #include <ac/string.h>
28 #include <ac/socket.h>
29 #include <db.h>
30
31 #include "ldap_pvt.h"
32 #include "lutil.h"
33 #include "slap.h"
34 #include "lutil_ldap.h"
35
36 const struct berval slap_ldapsync_bv = BER_BVC("ldapsync");
37 const struct berval slap_ldapsync_cn_bv = BER_BVC("cn=ldapsync");
38
39 void
40 slap_get_commit_csn( Operation *op, struct berval *csn )
41 {
42         struct slap_csn_entry *csne = NULL, *committed_csne = NULL;
43         int i = 0;
44
45         csn->bv_val = NULL;
46         csn->bv_len = 0;
47
48         ldap_pvt_thread_mutex_lock( &op->o_bd->be_pcl_mutex );
49
50         LDAP_TAILQ_FOREACH( csne, &op->o_bd->be_pending_csn_list, csn_link ) {
51                 if ( csne->opid == op->o_opid && csne->connid == op->o_connid )
52                         break;
53         }
54
55         if ( csne ) {
56                 csne->state = SLAP_CSN_COMMIT;
57         }
58
59         LDAP_TAILQ_FOREACH( csne, &op->o_bd->be_pending_csn_list, csn_link ) {
60                 if ( csne->state == SLAP_CSN_COMMIT )
61                         committed_csne = csne;
62                 if ( csne->state == SLAP_CSN_PENDING )
63                         break;
64         }
65
66         ldap_pvt_thread_mutex_unlock( &op->o_bd->be_pcl_mutex );
67
68         if ( committed_csne ) {
69                 ber_dupbv( csn, committed_csne->csn );
70         }
71 }
72
73 void
74 slap_rewind_commit_csn( Operation *op )
75 {
76         struct slap_csn_entry *csne = NULL;
77
78         ldap_pvt_thread_mutex_lock( &op->o_bd->be_pcl_mutex );
79
80         LDAP_TAILQ_FOREACH( csne, &op->o_bd->be_pending_csn_list, csn_link ) {
81                 if ( csne->opid == op->o_opid && csne->connid == op->o_connid )
82                         break;
83         }
84
85         if ( csne ) {
86                 csne->state = SLAP_CSN_PENDING;
87         }
88         
89         ldap_pvt_thread_mutex_unlock( &op->o_bd->be_pcl_mutex );
90 }
91
92 void
93 slap_graduate_commit_csn( Operation *op )
94 {
95         struct slap_csn_entry *csne = NULL;
96
97         if ( op == NULL )
98                 return;
99
100         if ( op->o_bd == NULL )
101                 return;
102
103         ldap_pvt_thread_mutex_lock( &op->o_bd->be_pcl_mutex );
104
105         LDAP_TAILQ_FOREACH( csne, &op->o_bd->be_pending_csn_list, csn_link ) {
106                 if ( csne->opid == op->o_opid && csne->connid == op->o_connid )
107                         break;
108         }
109
110         if ( csne ) {
111                 LDAP_TAILQ_REMOVE( &op->o_bd->be_pending_csn_list, csne, csn_link );
112                 ch_free( csne->csn->bv_val );
113                 ch_free( csne->csn );
114                 ch_free( csne );
115         }
116
117         ldap_pvt_thread_mutex_unlock( &op->o_bd->be_pcl_mutex );
118
119         return;
120 }
121
122 static struct berval ocbva[] = {
123         BER_BVC("top"),
124         BER_BVC("subentry"),
125         BER_BVC("syncProviderSubentry"),
126         {0,NULL}
127 };
128
129 Entry *
130 slap_create_context_csn_entry(
131         Backend *be,
132         struct berval *context_csn
133 )
134 {
135         Entry* e;
136         int rc;
137
138         struct berval bv;
139
140         e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
141
142         attr_merge( e, slap_schema.si_ad_objectClass, ocbva, NULL );
143
144         attr_merge_one( e, slap_schema.si_ad_structuralObjectClass, &ocbva[1], NULL );
145
146         attr_merge_one( e, slap_schema.si_ad_cn, (struct berval *)&slap_ldapsync_bv, NULL );
147
148         if ( context_csn ) {
149                 attr_merge_one( e, slap_schema.si_ad_contextCSN,
150                         context_csn, NULL );
151         }
152
153         bv.bv_val = "{}";
154         bv.bv_len = sizeof("{}")-1;
155         attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
156
157         build_new_dn( &e->e_name, &be->be_nsuffix[0], (struct berval *)&slap_ldapsync_cn_bv, NULL );
158         ber_dupbv( &e->e_nname, &e->e_name );
159
160         return e;
161 }
162
163 static int
164 slap_contextcsn_callback(
165         Operation* op,
166         SlapReply* rs
167 )
168 {
169         if ( rs->sr_type != REP_SEARCH ) {
170                 *((int*)op->o_callback->sc_private) = 0;
171         } else {
172                 *((int*)op->o_callback->sc_private) = 1;
173         }
174         return LDAP_SUCCESS;
175 }
176
177 int
178 slap_get_csn(
179         Operation *op,
180         char *csnbuf,
181         int     len,
182         struct berval *csn,
183         int manage_ctxcsn
184 )
185 {
186         struct  slap_csn_entry *pending;
187
188         if ( csn == NULL )
189                 return LDAP_OTHER;
190
191         if ( manage_ctxcsn ) {
192                 pending = (struct slap_csn_entry *) ch_calloc( 1, sizeof( struct slap_csn_entry ));
193         }
194
195         csn->bv_len = lutil_csnstr( csnbuf, len, 0, 0 );
196         csn->bv_val = csnbuf;
197
198         if ( manage_ctxcsn ) {
199                 ldap_pvt_thread_mutex_lock( &op->o_bd->be_pcl_mutex );
200                 pending->csn = ber_dupbv( NULL, csn );
201                 pending->connid = op->o_connid;
202                 pending->opid = op->o_opid;
203                 pending->state = SLAP_CSN_PENDING;
204                 LDAP_TAILQ_INSERT_TAIL( &op->o_bd->be_pending_csn_list, pending, csn_link );
205                 ldap_pvt_thread_mutex_unlock( &op->o_bd->be_pcl_mutex );
206         }
207
208         return LDAP_SUCCESS;
209 }