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 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
162 mod->sml_op = LDAP_MOD_REPLACE;
163 ber_str2bv( "objectClass", strlen("objectClass"), 1, &mod->sml_type );
164 mod->sml_bvalues = ocbva;
166 modtail = &mod->sml_next;
168 ber_str2bv( "syncProviderSubentry",
169 strlen("syncProviderSubentry"), 1, &socbva[0] );
171 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
172 mod->sml_op = LDAP_MOD_REPLACE;
173 ber_str2bv( "structuralObjectClass", strlen("structuralObjectClass"), 1, &mod->sml_type );
174 mod->sml_bvalues = socbva;
176 modtail = &mod->sml_next;
178 sprintf( substr, "ldapsync" );
179 sprintf( rdnstr, "cn=%s", substr );
180 ber_str2bv( substr, strlen( substr ), 1, &cnbva[0] );
181 ber_str2bv( rdnstr, strlen( rdnstr ), 1, &psubrdn );
182 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
183 mod->sml_op = LDAP_MOD_REPLACE;
184 ber_str2bv( "cn", strlen("cn"), 1, &mod->sml_type );
185 mod->sml_bvalues = cnbva;
187 modtail = &mod->sml_next;
190 ber_dupbv( &scbva[0], context_csn );
191 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
192 mod->sml_op = LDAP_MOD_REPLACE;
193 ber_str2bv( "contextCSN", strlen("contextCSN"), 1, &mod->sml_type );
194 mod->sml_bvalues = scbva;
196 modtail = &mod->sml_next;
199 ber_str2bv( "{}", strlen("{}"), 1, &ssbva[0] );
200 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
201 mod->sml_op = LDAP_MOD_REPLACE;
202 ber_str2bv( "subtreeSpecification",
203 strlen("subtreeSpecification"), 1, &mod->sml_type );
204 mod->sml_bvalues = ssbva;
206 modtail = &mod->sml_next;
208 rc = slap_mods_check( modlist, 1, &text, txtbuf, textlen, NULL );
210 if ( rc != LDAP_SUCCESS ) {
212 LDAP_LOG( OPERATION, ERR,
213 "create_context_csn_entry: mods check (%s)\n", text, 0, 0 );
215 Debug( LDAP_DEBUG_ANY, "create_context_csn_entry: mods check (%s)\n",
220 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
222 build_new_dn( &sub_bv, &be->be_nsuffix[0], &psubrdn );
223 dnPrettyNormal( NULL, &sub_bv, &e->e_name, &e->e_nname, NULL );
224 ch_free( sub_bv.bv_val );
225 ch_free( psubrdn.bv_val );
229 rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
231 if( rc != LDAP_SUCCESS ) {
233 LDAP_LOG( OPERATION, ERR,
234 "create_context_csn_entry: mods2entry (%s)\n", text, 0, 0 );
236 Debug( LDAP_DEBUG_ANY, "create_context_csn_entry: mods2entry (%s)\n",
241 for ( ml = modlist; ml != NULL; ml = mlnext ) {
242 mlnext = ml->sml_next;
250 slap_contextcsn_callback(
255 if ( rs->sr_type != REP_SEARCH ) {
256 *((int*)op->o_callback->sc_private) = 0;
258 *((int*)op->o_callback->sc_private) = 1;
272 struct slap_csn_entry *pending;
274 if ( manage_ctxcsn ) {
275 pending = (struct slap_csn_entry *) ch_calloc( 1, sizeof( struct slap_csn_entry ));
281 csn->bv_len = lutil_csnstr( csnbuf, len, 0, 0 );
282 csn->bv_val = csnbuf;
284 if ( manage_ctxcsn ) {
285 ldap_pvt_thread_mutex_lock( &op->o_bd->be_pcl_mutex );
286 pending->csn = ber_dupbv( NULL, csn );
287 pending->connid = op->o_connid;
288 pending->opid = op->o_opid;
289 pending->state = SLAP_CSN_PENDING;
290 LDAP_TAILQ_INSERT_TAIL( &op->o_bd->be_pending_csn_list, pending, csn_link );
291 ldap_pvt_thread_mutex_unlock( &op->o_bd->be_pcl_mutex );