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"
37 slap_get_commit_csn( Operation *op )
39 struct berval *max_committed_csn = NULL;
40 struct slap_csn_entry *csne = NULL, *committed_csne = NULL;
43 ldap_pvt_thread_mutex_lock( &op->o_bd->be_pcl_mutex );
45 LDAP_TAILQ_FOREACH( csne, &op->o_bd->be_pending_csn_list, csn_link ) {
46 if ( csne->opid == op->o_opid && csne->connid == op->o_connid )
51 csne->state = SLAP_CSN_COMMIT;
54 LDAP_TAILQ_FOREACH( csne, &op->o_bd->be_pending_csn_list, csn_link ) {
55 if ( csne->state == SLAP_CSN_COMMIT )
56 committed_csne = csne;
57 if ( csne->state == SLAP_CSN_PENDING )
61 ldap_pvt_thread_mutex_unlock( &op->o_bd->be_pcl_mutex );
63 if ( committed_csne ) {
64 max_committed_csn = ber_dupbv( NULL, committed_csne->csn );
67 return max_committed_csn;
71 slap_rewind_commit_csn( Operation *op )
73 struct slap_csn_entry *csne = NULL;
75 ldap_pvt_thread_mutex_lock( &op->o_bd->be_pcl_mutex );
77 LDAP_TAILQ_FOREACH( csne, &op->o_bd->be_pending_csn_list, csn_link ) {
78 if ( csne->opid == op->o_opid && csne->connid == op->o_connid )
83 csne->state = SLAP_CSN_PENDING;
86 ldap_pvt_thread_mutex_unlock( &op->o_bd->be_pcl_mutex );
90 slap_graduate_commit_csn( Operation *op )
92 struct slap_csn_entry *csne = NULL;
94 ldap_pvt_thread_mutex_lock( &op->o_bd->be_pcl_mutex );
96 LDAP_TAILQ_FOREACH( csne, &op->o_bd->be_pending_csn_list, csn_link ) {
97 if ( csne->opid == op->o_opid && csne->connid == op->o_connid )
102 LDAP_TAILQ_REMOVE( &op->o_bd->be_pending_csn_list, csne, csn_link );
103 ch_free( csne->csn->bv_val );
104 ch_free( csne->csn );
108 ldap_pvt_thread_mutex_unlock( &op->o_bd->be_pcl_mutex );
114 slap_create_context_csn_entry(
116 struct berval *context_csn
120 Modifications *mlnext;
122 Modifications *modlist;
123 Modifications **modtail = &modlist;
125 struct berval* ocbva = NULL;
126 struct berval* socbva = NULL;
127 struct berval* cnbva = NULL;
128 struct berval* ssbva = NULL;
129 struct berval* scbva = NULL;
134 char txtbuf[SLAP_TEXT_BUFLEN];
135 size_t textlen = sizeof txtbuf;
140 struct berval sub_bv = { 0, NULL };
141 struct berval psubrdn = { 0, NULL };
144 SlapReply rs = {REP_RESULT};
146 struct berval rdn = { 0, NULL };
148 char *def_filter_str = NULL;
150 ocbva = ( struct berval * ) ch_calloc( 4, sizeof( struct berval ));
151 socbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
152 cnbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
153 ssbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
154 scbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
156 ber_str2bv( "top", strlen("top"), 1, &ocbva[0] );
157 ber_str2bv( "subentry", strlen("subentry"), 1, &ocbva[1] );
158 ber_str2bv( "syncProviderSubentry",
159 strlen("syncProviderSubentry"), 1, &ocbva[2] );
161 ocbva[3].bv_val = NULL;
163 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
164 mod->sml_op = LDAP_MOD_REPLACE;
165 mod->sml_next = NULL;
166 mod->sml_desc = NULL;
167 ber_str2bv( "objectClass", strlen("objectClass"), 1, &mod->sml_type );
168 mod->sml_bvalues = ocbva;
169 mod->sml_nvalues = NULL;
171 modtail = &mod->sml_next;
173 ber_str2bv( "syncProviderSubentry",
174 strlen("syncProviderSubentry"), 1, &socbva[0] );
175 socbva[1].bv_len = 0;
176 socbva[1].bv_val = NULL;
178 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
179 mod->sml_op = LDAP_MOD_REPLACE;
180 mod->sml_next = NULL;
181 mod->sml_desc = NULL;
182 ber_str2bv( "structuralObjectClass", strlen("structuralObjectClass"), 1, &mod->sml_type );
183 mod->sml_bvalues = socbva;
184 mod->sml_nvalues = NULL;
186 modtail = &mod->sml_next;
188 sprintf( substr, "ldapsync" );
189 sprintf( rdnstr, "cn=%s", substr );
190 ber_str2bv( substr, strlen( substr ), 1, &cnbva[0] );
191 ber_str2bv( rdnstr, strlen( rdnstr ), 1, &psubrdn );
193 cnbva[1].bv_val = NULL;
194 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
195 mod->sml_op = LDAP_MOD_REPLACE;
196 mod->sml_next = NULL;
197 mod->sml_desc = NULL;
198 ber_str2bv( "cn", strlen("cn"), 1, &mod->sml_type );
199 mod->sml_bvalues = cnbva;
200 mod->sml_nvalues = NULL;
202 modtail = &mod->sml_next;
205 ber_dupbv( &scbva[0], context_csn );
207 scbva[1].bv_val = NULL;
208 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
209 mod->sml_op = LDAP_MOD_REPLACE;
210 mod->sml_next = NULL;
211 mod->sml_desc = NULL;
212 ber_str2bv( "contextCSN", strlen("contextCSN"), 1, &mod->sml_type );
213 mod->sml_bvalues = scbva;
214 mod->sml_nvalues = NULL;
216 modtail = &mod->sml_next;
219 ber_str2bv( "{}", strlen("{}"), 1, &ssbva[0] );
221 ssbva[1].bv_val = NULL;
222 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
223 mod->sml_op = LDAP_MOD_REPLACE;
224 mod->sml_next = NULL;
225 mod->sml_desc = NULL;
226 ber_str2bv( "subtreeSpecification",
227 strlen("subtreeSpecification"), 1, &mod->sml_type );
228 mod->sml_bvalues = ssbva;
229 mod->sml_nvalues = NULL;
231 modtail = &mod->sml_next;
233 rc = slap_mods_check( modlist, 1, &text, txtbuf, textlen, NULL );
235 if ( rc != LDAP_SUCCESS ) {
237 LDAP_LOG( OPERATION, ERR,
238 "create_context_csn_entry: mods check (%s)\n", text, 0, 0 );
240 Debug( LDAP_DEBUG_ANY, "create_context_csn_entry: mods check (%s)\n",
245 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
247 build_new_dn( &sub_bv, &be->be_nsuffix[0], &psubrdn );
248 dnPrettyNormal( NULL, &sub_bv, &e->e_name, &e->e_nname, NULL );
249 ch_free( sub_bv.bv_val );
250 ch_free( psubrdn.bv_val );
254 rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
256 if( rc != LDAP_SUCCESS ) {
258 LDAP_LOG( OPERATION, ERR,
259 "create_context_csn_entry: mods2entry (%s)\n", text, 0, 0 );
261 Debug( LDAP_DEBUG_ANY, "create_context_csn_entry: mods2entry (%s)\n",
266 for ( ml = modlist; ml != NULL; ml = mlnext ) {
267 mlnext = ml->sml_next;
275 slap_contextcsn_callback(
280 if ( rs->sr_type != REP_SEARCH ) {
281 *((int*)op->o_callback->sc_private) = 0;
283 *((int*)op->o_callback->sc_private) = 1;
297 struct slap_csn_entry *pending;
299 if ( manage_ctxcsn ) {
300 pending = (struct slap_csn_entry *) ch_calloc( 1, sizeof( struct slap_csn_entry ));
306 csn->bv_len = lutil_csnstr( csnbuf, len, 0, 0 );
307 csn->bv_val = csnbuf;
309 if ( manage_ctxcsn ) {
310 ldap_pvt_thread_mutex_lock( &op->o_bd->be_pcl_mutex );
311 pending->csn = ber_dupbv( NULL, csn );
312 pending->connid = op->o_connid;
313 pending->opid = op->o_opid;
314 pending->state = SLAP_CSN_PENDING;
315 LDAP_TAILQ_INSERT_TAIL( &op->o_bd->be_pending_csn_list, pending, csn_link );
316 ldap_pvt_thread_mutex_unlock( &op->o_bd->be_pcl_mutex );