1 /* repl.c - log modifications for replication purposes */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2003 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
31 #include <ac/string.h>
33 #include <ac/socket.h>
35 #ifdef HAVE_SYS_FILE_H
53 if ( be->be_replica != NULL ) {
54 for ( ; be->be_replica[ i ] != NULL; i++ );
57 be->be_replica = ch_realloc( be->be_replica,
58 sizeof( struct slap_replica_info * )*( i + 2 ) );
61 = ch_calloc( sizeof( struct slap_replica_info ), 1 );
62 be->be_replica[ i ]->ri_host = ch_strdup( host );
63 be->be_replica[ i ]->ri_nsuffix = NULL;
64 be->be_replica[ i ]->ri_attrs = NULL;
65 be->be_replica[ i + 1 ] = NULL;
77 struct berval dn, ndn;
80 dn.bv_val = (char *) suffix;
81 dn.bv_len = strlen( dn.bv_val );
83 rc = dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL );
84 if( rc != LDAP_SUCCESS ) {
88 if ( select_backend( &ndn, 0, 0 ) != be ) {
93 ber_bvarray_add( &be->be_replica[nr]->ri_nsuffix, &ndn );
105 if ( be->be_replica[nr]->ri_attrs != NULL ) {
106 if ( be->be_replica[nr]->ri_exclude != exclude ) {
107 fprintf( stderr, "attr selective replication directive '%s' conflicts with previous one (discarded)\n", attrs );
108 ch_free( be->be_replica[nr]->ri_attrs );
109 be->be_replica[nr]->ri_attrs = NULL;
113 be->be_replica[nr]->ri_exclude = exclude;
114 be->be_replica[nr]->ri_attrs = str2anlist( be->be_replica[nr]->ri_attrs,
116 return ( be->be_replica[nr]->ri_attrs == NULL );
119 static struct timestamp {
125 print_vals( FILE *fp, struct berval *type, struct berval *bv );
127 replog1( struct slap_replica_info *ri, Operation *op, FILE *fp );
130 repstamp( Operation *op )
132 ldap_pvt_thread_mutex_lock( &repstamp_mutex );
133 op->o_time = slap_get_time();
134 if ( op->o_time == oldstamp.time ) {
135 op->o_tseq = ++oldstamp.seq;
137 oldstamp.time = op->o_time;
141 ldap_pvt_thread_mutex_unlock( &repstamp_mutex );
145 replog( Operation *op )
147 Modifications *ml = NULL;
151 /* undef NO_LOG_WHEN_NO_REPLICAS */
152 #ifdef NO_LOG_WHEN_NO_REPLICAS
157 if ( op->o_bd->be_replogfile == NULL && replogfile == NULL ) {
161 ldap_pvt_thread_mutex_lock( &replog_mutex );
162 if ( (fp = lock_fopen( op->o_bd->be_replogfile ? op->o_bd->be_replogfile :
163 replogfile, "a", &lfp )) == NULL ) {
164 ldap_pvt_thread_mutex_unlock( &replog_mutex );
168 for ( i = 0; op->o_bd->be_replica != NULL && op->o_bd->be_replica[i] != NULL; i++ ) {
169 /* check if dn's suffix matches legal suffixes, if any */
170 if ( op->o_bd->be_replica[i]->ri_nsuffix != NULL ) {
173 for ( j = 0; op->o_bd->be_replica[i]->ri_nsuffix[j].bv_val; j++ ) {
174 if ( dnIsSuffix( &op->o_req_ndn, &op->o_bd->be_replica[i]->ri_nsuffix[j] ) ) {
179 if ( !op->o_bd->be_replica[i]->ri_nsuffix[j].bv_val ) {
180 /* do not add "replica:" line */
184 /* See if we only want a subset of attributes */
185 if ( op->o_bd->be_replica[i]->ri_attrs != NULL &&
186 ( op->o_tag == LDAP_REQ_MODIFY || op->o_tag == LDAP_REQ_ADD || op->o_tag == LDAP_REQ_EXTENDED ) ) {
190 /* Do attribute subsets by themselves in a second pass */
194 fprintf( fp, "replica: %s\n", op->o_bd->be_replica[i]->ri_host );
195 #ifdef NO_LOG_WHEN_NO_REPLICAS
200 #ifdef NO_LOG_WHEN_NO_REPLICAS
201 if ( count == 0 && subsets == 0 ) {
202 /* if no replicas matched, drop the log
203 * (should we log it anyway?) */
204 lock_fclose( fp, lfp );
205 ldap_pvt_thread_mutex_unlock( &replog_mutex );
211 replog1( NULL, op, fp );
214 for ( i = subsets - 1; op->o_bd->be_replica[i] != NULL; i++ ) {
216 /* If no attrs, we already did this above */
217 if ( op->o_bd->be_replica[i]->ri_attrs == NULL ) {
221 /* check if dn's suffix matches legal suffixes, if any */
222 if ( op->o_bd->be_replica[i]->ri_nsuffix != NULL ) {
225 for ( j = 0; op->o_bd->be_replica[i]->ri_nsuffix[j].bv_val; j++ ) {
226 if ( dnIsSuffix( &op->o_req_ndn, &op->o_bd->be_replica[i]->ri_nsuffix[j] ) ) {
231 if ( !op->o_bd->be_replica[i]->ri_nsuffix[j].bv_val ) {
232 /* no matching suffix found, skip it */
236 switch( op->o_tag ) {
237 case LDAP_REQ_EXTENDED:
238 /* quick hack for extended operations */
239 /* assume change parameter is a Modifications* */
241 case LDAP_REQ_MODIFY:
245 /* Other operations were logged in the first pass */
248 replog1( op->o_bd->be_replica[i], op, fp );
252 lock_fclose( fp, lfp );
253 ldap_pvt_thread_mutex_unlock( &replog_mutex );
258 struct slap_replica_info *ri,
264 fprintf( fp, "replica: %s\n", ri->ri_host );
266 fprintf( fp, "time: %ld.%ld\n", op->o_time, op->o_tseq );
267 fprintf( fp, "dn: %s\n", op->o_req_dn.bv_val );
272 struct slap_replica_info *ri,
280 int dohdr = 1, ocs = -1;
281 struct berval vals[2];
283 vals[1].bv_val = NULL;
286 switch ( op->o_tag ) {
287 case LDAP_REQ_EXTENDED:
288 /* quick hack for extended operations */
289 /* assume change parameter is a Modifications* */
292 case LDAP_REQ_MODIFY:
293 for ( ml = op->orm_modlist; ml != NULL; ml = ml->sml_next ) {
294 char *did, *type = ml->sml_desc->ad_cname.bv_val;
295 switch ( ml->sml_op ) {
299 case LDAP_MOD_DELETE:
300 did = "delete"; break;
302 case LDAP_MOD_REPLACE:
303 did = "replace"; break;
305 case LDAP_MOD_INCREMENT:
306 did = "increment"; break;
308 if ( ri && ri->ri_attrs ) {
309 int is_in = ad_inlist( ml->sml_desc, ri->ri_attrs );
311 if ( ( !is_in && !ri->ri_exclude )
312 || ( is_in && ri->ri_exclude ) )
316 /* If this is objectClass, see if the value is included
317 * in any subset, otherwise drop it.
319 if ( ocs && ml->sml_desc == slap_schema.si_ad_objectClass
320 && ml->sml_bvalues ) {
323 if ( ocs == -1 ) ocs = 0;
325 for ( i=0; ml->sml_bvalues[i].bv_val; i++ ) {
327 for ( an = ri->ri_attrs; an->an_name.bv_val; an++ ) {
330 match |= an->an_oc_exclude;
331 if ( ml->sml_bvalues[i].bv_len == an->an_name.bv_len
332 && !strcasecmp(ml->sml_bvalues[i].bv_val,
333 an->an_name.bv_val ) ) {
334 match = !an->an_oc_exclude;
339 /* Objectclasses need no special treatment, drop into
344 match ^= ri->ri_exclude;
345 /* Found a match, log it */
348 rephdr( ri, op, fp );
349 fprintf( fp, "changetype: modify\n" );
353 fprintf( fp, "%s: %s\n", did, type );
356 vals[0] = an->an_name;
357 print_vals( fp, &ml->sml_desc->ad_cname, vals );
362 /* Explicit objectclasses have been handled already */
365 fprintf( fp, "-\n" );
372 rephdr( ri, op, fp );
373 fprintf( fp, "changetype: modify\n" );
376 fprintf( fp, "%s: %s\n", did, type );
377 if ( ml->sml_bvalues ) {
378 print_vals( fp, &ml->sml_desc->ad_cname, ml->sml_bvalues );
380 fprintf( fp, "-\n" );
385 for ( a = op->ora_e->e_attrs ; a != NULL; a=a->a_next ) {
386 if ( ri && ri->ri_attrs ) {
387 int is_in = ad_inlist( a->a_desc, ri->ri_attrs );
388 if ( ( !is_in && !ri->ri_exclude ) || ( is_in && ri->ri_exclude ) ) {
392 /* If the list includes objectClass names,
393 * only include those classes in the
394 * objectClass attribute
396 if ( ocs && a->a_desc == slap_schema.si_ad_objectClass ) {
399 if ( ocs == -1 ) ocs = 0;
401 for ( i=0; a->a_vals[i].bv_val; i++ ) {
403 for ( an = ri->ri_attrs; an->an_name.bv_val; an++ ) {
406 match |= an->an_oc_exclude;
407 if ( a->a_vals[i].bv_len == an->an_name.bv_len
408 && !strcasecmp(a->a_vals[i].bv_val,
409 an->an_name.bv_val ) ) {
410 match = !an->an_oc_exclude;
417 match ^= ri->ri_exclude;
420 rephdr( ri, op, fp );
421 fprintf( fp, "changetype: add\n" );
424 vals[0] = an->an_name;
425 print_vals( fp, &a->a_desc->ad_cname, vals );
432 rephdr( ri, op, fp );
433 fprintf( fp, "changetype: add\n" );
436 print_vals( fp, &a->a_desc->ad_cname, a->a_vals );
440 case LDAP_REQ_DELETE:
441 rephdr( ri, op, fp );
442 fprintf( fp, "changetype: delete\n" );
445 case LDAP_REQ_MODRDN:
446 rephdr( ri, op, fp );
447 fprintf( fp, "changetype: modrdn\n" );
448 fprintf( fp, "newrdn: %s\n", op->orr_newrdn.bv_val );
449 fprintf( fp, "deleteoldrdn: %d\n", op->orr_deleteoldrdn ? 1 : 0 );
450 if( op->orr_newSup != NULL ) {
451 fprintf( fp, "newsuperior: %s\n", op->orr_newSup->bv_val );
466 for ( i = 0, len = 0; bv && bv[i].bv_val; i++ ) {
467 if ( bv[i].bv_len > len )
471 len = LDIF_SIZE_NEEDED( type->bv_len, len ) + 1;
472 buf = (char *) ch_malloc( len );
474 for ( ; bv && bv->bv_val; bv++ ) {
476 ldif_sput( &bufp, LDIF_PUT_VALUE, type->bv_val,
477 bv->bv_val, bv->bv_len );