1 /* repl.c - log modifications for replication purposes */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/string.h>
14 #include <ac/socket.h>
16 #ifdef HAVE_SYS_FILE_H
34 if ( be->be_replica != NULL ) {
35 for ( ; be->be_replica[ i ] != NULL; i++ );
38 be->be_replica = ch_realloc( be->be_replica,
39 sizeof( struct slap_replica_info * )*( i + 2 ) );
42 = ch_calloc( sizeof( struct slap_replica_info ), 1 );
43 be->be_replica[ i ]->ri_host = ch_strdup( host );
44 be->be_replica[ i ]->ri_nsuffix = NULL;
45 be->be_replica[ i ]->ri_attrs = NULL;
46 be->be_replica[ i + 1 ] = NULL;
58 struct berval dn, ndn;
61 dn.bv_val = (char *) suffix;
62 dn.bv_len = strlen( dn.bv_val );
64 rc = dnNormalize2( NULL, &dn, &ndn );
65 if( rc != LDAP_SUCCESS ) {
69 if ( select_backend( &ndn, 0, 0 ) != be ) {
74 ber_bvarray_add( &be->be_replica[nr]->ri_nsuffix, &ndn );
86 if ( be->be_replica[nr]->ri_attrs != NULL ) {
87 if ( be->be_replica[nr]->ri_exclude != exclude ) {
88 fprintf( stderr, "attr selective replication directive '%s' conflicts with previous one (discarded)\n", attrs );
89 ch_free( be->be_replica[nr]->ri_attrs );
90 be->be_replica[nr]->ri_attrs = NULL;
94 be->be_replica[nr]->ri_exclude = exclude;
95 be->be_replica[nr]->ri_attrs = str2anlist( be->be_replica[nr]->ri_attrs,
97 return ( be->be_replica[nr]->ri_attrs == NULL );
101 print_vals( FILE *fp, struct berval *type, struct berval *bv );
103 replog1( struct slap_replica_info *ri, Operation *op, FILE *fp, void *first);
106 replog( Operation *op )
108 Modifications *ml = NULL;
112 /* undef NO_LOG_WHEN_NO_REPLICAS */
113 #ifdef NO_LOG_WHEN_NO_REPLICAS
117 long now = slap_get_time();
119 if ( op->o_bd->be_replogfile == NULL && replogfile == NULL ) {
123 ldap_pvt_thread_mutex_lock( &replog_mutex );
124 if ( (fp = lock_fopen( op->o_bd->be_replogfile ? op->o_bd->be_replogfile :
125 replogfile, "a", &lfp )) == NULL ) {
126 ldap_pvt_thread_mutex_unlock( &replog_mutex );
130 for ( i = 0; op->o_bd->be_replica != NULL && op->o_bd->be_replica[i] != NULL; i++ ) {
131 /* check if dn's suffix matches legal suffixes, if any */
132 if ( op->o_bd->be_replica[i]->ri_nsuffix != NULL ) {
135 for ( j = 0; op->o_bd->be_replica[i]->ri_nsuffix[j].bv_val; j++ ) {
136 if ( dnIsSuffix( &op->o_req_ndn, &op->o_bd->be_replica[i]->ri_nsuffix[j] ) ) {
141 if ( !op->o_bd->be_replica[i]->ri_nsuffix[j].bv_val ) {
142 /* do not add "replica:" line */
146 /* See if we only want a subset of attributes */
147 if ( op->o_bd->be_replica[i]->ri_attrs != NULL &&
148 ( op->o_tag == LDAP_REQ_MODIFY || op->o_tag == LDAP_REQ_ADD || op->o_tag == LDAP_REQ_EXTENDED ) ) {
152 /* Do attribute subsets by themselves in a second pass */
156 fprintf( fp, "replica: %s\n", op->o_bd->be_replica[i]->ri_host );
157 #ifdef NO_LOG_WHEN_NO_REPLICAS
162 #ifdef NO_LOG_WHEN_NO_REPLICAS
163 if ( count == 0 && subsets == 0 ) {
164 /* if no replicas matched, drop the log
165 * (should we log it anyway?) */
166 lock_fclose( fp, lfp );
167 ldap_pvt_thread_mutex_unlock( &replog_mutex );
173 fprintf( fp, "time: %ld\n", now );
174 fprintf( fp, "dn: %s\n", op->o_req_dn.bv_val );
176 replog1( NULL, op, fp, NULL );
180 for ( i = subsets - 1; op->o_bd->be_replica != NULL && op->o_bd->be_replica[i] != NULL; i++ ) {
182 /* If no attrs, we already did this above */
183 if ( op->o_bd->be_replica[i]->ri_attrs == NULL ) {
187 /* check if dn's suffix matches legal suffixes, if any */
188 if ( op->o_bd->be_replica[i]->ri_nsuffix != NULL ) {
191 for ( j = 0; op->o_bd->be_replica[i]->ri_nsuffix[j].bv_val; j++ ) {
192 if ( dnIsSuffix( &op->o_req_ndn, &op->o_bd->be_replica[i]->ri_nsuffix[j] ) ) {
197 if ( !op->o_bd->be_replica[i]->ri_nsuffix[j].bv_val ) {
198 /* do not add "replica:" line */
204 switch( op->o_tag ) {
205 case LDAP_REQ_EXTENDED:
206 /* quick hack for extended operations */
207 /* assume change parameter is a Modfications* */
209 case LDAP_REQ_MODIFY:
210 for ( ml = op->oq_modify.rs_modlist; ml != NULL; ml = ml->sml_next ) {
213 is_in = ad_inlist( ml->sml_desc, op->o_bd->be_replica[i]->ri_attrs );
214 exclude = op->o_bd->be_replica[i]->ri_exclude;
217 * there might be a more clever way to do this test,
218 * but this way, at least, is comprehensible :)
220 if ( ( is_in && !exclude ) || ( !is_in && exclude ) ) {
231 for ( a = op->oq_add.rs_e->e_attrs; a != NULL; a = a->a_next ) {
234 is_in = ad_inlist( a->a_desc, op->o_bd->be_replica[i]->ri_attrs );
235 exclude = op->o_bd->be_replica[i]->ri_exclude;
237 if ( ( is_in && !exclude ) || ( !is_in && exclude ) ) {
248 /* Other operations were logged in the first pass */
251 fprintf( fp, "replica: %s\n", op->o_bd->be_replica[i]->ri_host );
252 fprintf( fp, "time: %ld\n", now );
253 fprintf( fp, "dn: %s\n", op->o_req_dn.bv_val );
254 replog1( op->o_bd->be_replica[i], op, fp, first );
258 lock_fclose( fp, lfp );
259 ldap_pvt_thread_mutex_unlock( &replog_mutex );
265 struct slap_replica_info *ri,
274 switch ( op->o_tag ) {
275 case LDAP_REQ_EXTENDED:
276 /* quick hack for extended operations */
277 /* assume change parameter is a Modfications* */
280 case LDAP_REQ_MODIFY:
281 fprintf( fp, "changetype: modify\n" );
282 ml = first ? first : op->oq_modify.rs_modlist;
283 for ( ; ml != NULL; ml = ml->sml_next ) {
285 if ( ri && ri->ri_attrs ) {
286 int is_in = ad_inlist( ml->sml_desc, ri->ri_attrs );
288 if ( ( !is_in && !ri->ri_exclude ) || ( is_in && ri->ri_exclude ) ) {
292 type = ml->sml_desc->ad_cname.bv_val;
293 switch ( ml->sml_op ) {
295 fprintf( fp, "add: %s\n", type );
298 case LDAP_MOD_DELETE:
299 fprintf( fp, "delete: %s\n", type );
302 case LDAP_MOD_REPLACE:
303 fprintf( fp, "replace: %s\n", type );
306 if ( ml->sml_bvalues )
307 print_vals( fp, &ml->sml_desc->ad_cname, ml->sml_bvalues );
308 fprintf( fp, "-\n" );
313 fprintf( fp, "changetype: add\n" );
314 a = first ? first : op->oq_add.rs_e->e_attrs;
315 for ( ; a != NULL; a=a->a_next ) {
316 if ( ri && ri->ri_attrs ) {
317 int is_in = ad_inlist( a->a_desc, ri->ri_attrs );
318 if ( ( !is_in && !ri->ri_exclude ) || ( is_in && ri->ri_exclude ) ) {
321 /* If the list includes objectClass names,
322 * only include those classes in the
323 * objectClass attribute
325 if ( a->a_desc == slap_schema.si_ad_objectClass ) {
328 struct berval vals[2];
329 vals[1].bv_val = NULL;
331 for ( an = ri->ri_attrs; an->an_name.bv_val; an++ ) {
334 for ( i=0; a->a_vals[i].bv_val; i++ ) {
335 if ( a->a_vals[i].bv_len == an->an_name.bv_len
336 && !strcasecmp(a->a_vals[i].bv_val,
337 an->an_name.bv_val ) ) {
339 vals[0] = an->an_name;
340 print_vals( fp, &a->a_desc->ad_cname, vals );
349 print_vals( fp, &a->a_desc->ad_cname, a->a_vals );
353 case LDAP_REQ_DELETE:
354 fprintf( fp, "changetype: delete\n" );
357 case LDAP_REQ_MODRDN:
358 fprintf( fp, "changetype: modrdn\n" );
359 fprintf( fp, "newrdn: %s\n", op->oq_modrdn.rs_newrdn.bv_val );
360 fprintf( fp, "deleteoldrdn: %d\n", op->oq_modrdn.rs_deleteoldrdn ? 1 : 0 );
361 if( op->oq_modrdn.rs_newSup != NULL ) {
362 fprintf( fp, "newsuperior: %s\n", op->oq_modrdn.rs_newSup->bv_val );
377 for ( i = 0, len = 0; bv && bv[i].bv_val; i++ ) {
378 if ( bv[i].bv_len > len )
382 len = LDIF_SIZE_NEEDED( type->bv_len, len ) + 1;
383 buf = (char *) ch_malloc( len );
385 for ( ; bv && bv->bv_val; bv++ ) {
387 ldif_sput( &bufp, LDIF_PUT_VALUE, type->bv_val,
388 bv->bv_val, bv->bv_len );