3 * Context CSN Management Routines
5 /* Copyright (c) 2003 by International Business Machines, Inc.
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
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.
27 #include <ac/string.h>
28 #include <ac/socket.h>
34 #include "lutil_ldap.h"
39 slap_get_commit_csn( Operation *op )
41 struct berval *max_committed_csn = NULL;
42 struct slap_csn_entry *csne = NULL, *committed_csne = NULL;
45 ldap_pvt_thread_mutex_lock( &op->o_bd->be_pcl_mutex );
47 LDAP_TAILQ_FOREACH( csne, &op->o_bd->be_pending_csn_list, csn_link ) {
48 if ( csne->opid == op->o_opid && csne->connid == op->o_connid )
53 csne->state = SLAP_CSN_COMMIT;
56 LDAP_TAILQ_FOREACH( csne, &op->o_bd->be_pending_csn_list, csn_link ) {
57 if ( csne->state == SLAP_CSN_COMMIT )
58 committed_csne = csne;
59 if ( csne->state == SLAP_CSN_PENDING )
63 ldap_pvt_thread_mutex_unlock( &op->o_bd->be_pcl_mutex );
65 if ( committed_csne ) {
66 max_committed_csn = ber_dupbv( NULL, committed_csne->csn );
69 return max_committed_csn;
73 slap_rewind_commit_csn( Operation *op )
75 struct slap_csn_entry *csne = NULL;
77 ldap_pvt_thread_mutex_lock( &op->o_bd->be_pcl_mutex );
79 LDAP_TAILQ_FOREACH( csne, &op->o_bd->be_pending_csn_list, csn_link ) {
80 if ( csne->opid == op->o_opid && csne->connid == op->o_connid )
85 csne->state = SLAP_CSN_PENDING;
88 ldap_pvt_thread_mutex_unlock( &op->o_bd->be_pcl_mutex );
92 slap_graduate_commit_csn( Operation *op )
94 struct slap_csn_entry *csne = NULL;
96 ldap_pvt_thread_mutex_lock( &op->o_bd->be_pcl_mutex );
98 LDAP_TAILQ_FOREACH( csne, &op->o_bd->be_pending_csn_list, csn_link ) {
99 if ( csne->opid == op->o_opid && csne->connid == op->o_connid )
104 LDAP_TAILQ_REMOVE( &op->o_bd->be_pending_csn_list, csne, csn_link );
105 ch_free( csne->csn->bv_val );
106 ch_free( csne->csn );
110 ldap_pvt_thread_mutex_unlock( &op->o_bd->be_pcl_mutex );
116 slap_create_context_csn_entry(
118 struct berval *context_csn
122 Modifications *mlnext;
124 Modifications *modlist;
125 Modifications **modtail = &modlist;
127 struct berval* ocbva = NULL;
128 struct berval* socbva = NULL;
129 struct berval* cnbva = NULL;
130 struct berval* ssbva = NULL;
131 struct berval* scbva = NULL;
136 char txtbuf[SLAP_TEXT_BUFLEN];
137 size_t textlen = sizeof txtbuf;
142 struct berval sub_bv = { 0, NULL };
143 struct berval psubrdn = { 0, NULL };
146 SlapReply rs = {REP_RESULT};
148 struct berval rdn = { 0, NULL };
150 char *def_filter_str = NULL;
152 ocbva = ( struct berval * ) ch_calloc( 4, sizeof( struct berval ));
153 socbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
154 cnbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
155 ssbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
156 scbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
158 ber_str2bv( "top", strlen("top"), 1, &ocbva[0] );
159 ber_str2bv( "subentry", strlen("subentry"), 1, &ocbva[1] );
160 ber_str2bv( "syncProviderSubentry",
161 strlen("syncProviderSubentry"), 1, &ocbva[2] );
163 ocbva[3].bv_val = NULL;
165 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
166 mod->sml_op = LDAP_MOD_REPLACE;
167 mod->sml_next = NULL;
168 mod->sml_desc = NULL;
169 ber_str2bv( "objectClass", strlen("objectClass"), 1, &mod->sml_type );
170 mod->sml_bvalues = ocbva;
171 mod->sml_nvalues = ocbva;
173 modtail = &mod->sml_next;
175 ber_str2bv( "syncProviderSubentry",
176 strlen("syncProviderSubentry"), 1, &socbva[0] );
177 socbva[1].bv_len = 0;
178 socbva[1].bv_val = NULL;
180 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
181 mod->sml_op = LDAP_MOD_REPLACE;
182 mod->sml_next = NULL;
183 mod->sml_desc = NULL;
184 ber_str2bv( "structuralObjectClass", strlen("structuralObjectClass"), 1, &mod->sml_type );
185 mod->sml_bvalues = socbva;
186 mod->sml_nvalues = socbva;
188 modtail = &mod->sml_next;
190 sprintf( substr, "ldapsync" );
191 sprintf( rdnstr, "cn=%s", substr );
192 ber_str2bv( substr, strlen( substr ), 1, &cnbva[0] );
193 ber_str2bv( rdnstr, strlen( rdnstr ), 1, &psubrdn );
195 cnbva[1].bv_val = NULL;
196 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
197 mod->sml_op = LDAP_MOD_REPLACE;
198 mod->sml_next = NULL;
199 mod->sml_desc = NULL;
200 ber_str2bv( "cn", strlen("cn"), 1, &mod->sml_type );
201 mod->sml_bvalues = cnbva;
202 mod->sml_nvalues = cnbva;
204 modtail = &mod->sml_next;
207 ber_dupbv( &scbva[0], context_csn );
209 scbva[1].bv_val = NULL;
210 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
211 mod->sml_op = LDAP_MOD_REPLACE;
212 mod->sml_next = NULL;
213 mod->sml_desc = NULL;
214 ber_str2bv( "contextCSN", strlen("contextCSN"), 1, &mod->sml_type );
215 mod->sml_bvalues = scbva;
216 mod->sml_nvalues = scbva;
218 modtail = &mod->sml_next;
221 ber_str2bv( "{}", strlen("{}"), 1, &ssbva[0] );
223 ssbva[1].bv_val = NULL;
224 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
225 mod->sml_op = LDAP_MOD_REPLACE;
226 mod->sml_next = NULL;
227 mod->sml_desc = NULL;
228 ber_str2bv( "subtreeSpecification",
229 strlen("subtreeSpecification"), 1, &mod->sml_type );
230 mod->sml_bvalues = ssbva;
231 mod->sml_nvalues = ssbva;
233 modtail = &mod->sml_next;
235 rc = slap_mods_check( modlist, 1, &text, txtbuf, textlen, NULL );
237 if ( rc != LDAP_SUCCESS ) {
239 LDAP_LOG( OPERATION, ERR,
240 "create_context_csn_entry: mods check (%s)\n", text, 0, 0 );
242 Debug( LDAP_DEBUG_ANY, "create_context_csn_entry: mods check (%s)\n",
247 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
249 build_new_dn( &sub_bv, &be->be_nsuffix[0], &psubrdn );
250 dnPrettyNormal( NULL, &sub_bv, &e->e_name, &e->e_nname, NULL );
251 ch_free( sub_bv.bv_val );
252 ch_free( psubrdn.bv_val );
256 rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
258 if( rc != LDAP_SUCCESS ) {
260 LDAP_LOG( OPERATION, ERR,
261 "create_context_csn_entry: mods2entry (%s)\n", text, 0, 0 );
263 Debug( LDAP_DEBUG_ANY, "create_context_csn_entry: mods2entry (%s)\n",
272 slap_contextcsn_callback(
277 if ( rs->sr_type != REP_SEARCH ) {
278 *((int*)op->o_callback->sc_private) = 0;
280 *((int*)op->o_callback->sc_private) = 1;
294 struct slap_csn_entry *pending;
296 if ( manage_ctxcsn ) {
297 pending = (struct slap_csn_entry *) ch_calloc( 1, sizeof( struct slap_csn_entry ));
303 csn->bv_len = lutil_csnstr( csnbuf, len, 0, 0 );
304 csn->bv_val = csnbuf;
306 if ( manage_ctxcsn ) {
307 ldap_pvt_thread_mutex_lock( &op->o_bd->be_pcl_mutex );
308 pending->csn = ber_dupbv( NULL, csn );
309 pending->connid = op->o_connid;
310 pending->opid = op->o_opid;
311 pending->state = SLAP_CSN_PENDING;
312 LDAP_TAILQ_INSERT_TAIL( &op->o_bd->be_pending_csn_list, pending, csn_link );
313 ldap_pvt_thread_mutex_unlock( &op->o_bd->be_pcl_mutex );