1 /* repl.c - log modifications for replication purposes */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2007 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
51 assert( host != NULL );
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 ber_str2bv( uri, 0, 0, &be->be_replica[ i ]->ri_bindconf.sb_uri );
63 be->be_replica[ i ]->ri_host = host;
64 be->be_replica[ i ]->ri_nsuffix = NULL;
65 be->be_replica[ i ]->ri_attrs = NULL;
66 be->be_replica[ i + 1 ] = NULL;
79 if ( be->be_replica == NULL ) {
83 for ( ; be->be_replica[ i ] != NULL; i++ ) {
84 ber_bvarray_free( be->be_replica[ i ]->ri_nsuffix );
86 if ( be->be_replica[ i ]->ri_attrs ) {
87 AttributeName *an = be->be_replica[ i ]->ri_attrs;
90 for ( j = 0; !BER_BVISNULL( &an[ j ].an_name ); j++ )
92 ch_free( an[ j ].an_name.bv_val );
97 bindconf_free( &be->be_replica[ i ]->ri_bindconf );
99 ch_free( be->be_replica[ i ] );
102 ch_free( be->be_replica );
114 struct berval dn, ndn;
117 dn.bv_val = (char *) suffix;
118 dn.bv_len = strlen( dn.bv_val );
120 rc = dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL );
121 if( rc != LDAP_SUCCESS ) {
125 if ( select_backend( &ndn, 0, 0 ) != be ) {
130 ber_bvarray_add( &be->be_replica[nr]->ri_nsuffix, &ndn );
142 if ( be->be_replica[nr]->ri_attrs != NULL ) {
143 if ( be->be_replica[nr]->ri_exclude != exclude ) {
144 fprintf( stderr, "attr selective replication directive '%s' conflicts with previous one (discarded)\n", attrs );
145 ch_free( be->be_replica[nr]->ri_attrs );
146 be->be_replica[nr]->ri_attrs = NULL;
150 be->be_replica[nr]->ri_exclude = exclude;
151 be->be_replica[nr]->ri_attrs = str2anlist( be->be_replica[nr]->ri_attrs,
153 return ( be->be_replica[nr]->ri_attrs == NULL );
157 print_vals( FILE *fp, struct berval *type, struct berval *bv );
159 replog1( struct slap_replica_info *ri, Operation *op, FILE *fp, long now);
162 replog( Operation *op )
166 /* undef NO_LOG_WHEN_NO_REPLICAS */
167 #ifdef NO_LOG_WHEN_NO_REPLICAS
171 long now = slap_get_time();
174 replogfile = op->o_bd->be_replogfile ? op->o_bd->be_replogfile :
175 frontendDB->be_replogfile;
180 ldap_pvt_thread_mutex_lock( &replog_mutex );
181 if ( (fp = lock_fopen( replogfile, "a", &lfp )) == NULL ) {
182 ldap_pvt_thread_mutex_unlock( &replog_mutex );
186 for ( i = 0; op->o_bd->be_replica != NULL && op->o_bd->be_replica[i] != NULL; i++ ) {
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 */
202 /* See if we only want a subset of attributes */
203 if ( op->o_bd->be_replica[i]->ri_attrs != NULL &&
204 ( op->o_tag == LDAP_REQ_MODIFY || op->o_tag == LDAP_REQ_ADD || op->o_tag == LDAP_REQ_EXTENDED ) ) {
208 /* Do attribute subsets by themselves in a second pass */
212 fprintf( fp, "replica: %s\n", op->o_bd->be_replica[i]->ri_host );
213 #ifdef NO_LOG_WHEN_NO_REPLICAS
218 #ifdef NO_LOG_WHEN_NO_REPLICAS
219 if ( count == 0 && subsets == 0 ) {
220 /* if no replicas matched, drop the log
221 * (should we log it anyway?) */
222 lock_fclose( fp, lfp );
223 ldap_pvt_thread_mutex_unlock( &replog_mutex );
229 replog1( NULL, op, fp, now );
232 for ( i = subsets - 1; op->o_bd->be_replica[i] != NULL; i++ ) {
234 /* If no attrs, we already did this above */
235 if ( op->o_bd->be_replica[i]->ri_attrs == NULL ) {
239 /* check if dn's suffix matches legal suffixes, if any */
240 if ( op->o_bd->be_replica[i]->ri_nsuffix != NULL ) {
243 for ( j = 0; op->o_bd->be_replica[i]->ri_nsuffix[j].bv_val; j++ ) {
244 if ( dnIsSuffix( &op->o_req_ndn, &op->o_bd->be_replica[i]->ri_nsuffix[j] ) ) {
249 if ( !op->o_bd->be_replica[i]->ri_nsuffix[j].bv_val ) {
250 /* no matching suffix found, skip it */
254 switch( op->o_tag ) {
255 case LDAP_REQ_EXTENDED:
256 /* quick hack for extended operations */
257 /* assume change parameter is a Modifications* */
259 case LDAP_REQ_MODIFY:
263 /* Other operations were logged in the first pass */
266 replog1( op->o_bd->be_replica[i], op, fp, now );
270 lock_fclose( fp, lfp );
271 ldap_pvt_thread_mutex_unlock( &replog_mutex );
276 struct slap_replica_info *ri,
283 fprintf( fp, "replica: %s\n", ri->ri_host );
285 fprintf( fp, "time: %ld\n", now );
286 fprintf( fp, "dn: %s\n", op->o_req_dn.bv_val );
291 struct slap_replica_info *ri,
300 int dohdr = 1, ocs = -1;
301 struct berval vals[2];
303 vals[1].bv_val = NULL;
306 switch ( op->o_tag ) {
307 case LDAP_REQ_EXTENDED:
308 /* quick hack for extended operations */
309 /* assume change parameter is a Modifications* */
312 case LDAP_REQ_MODIFY:
313 for ( ml = op->orm_modlist; ml != NULL; ml = ml->sml_next ) {
314 char *did = NULL, *type = ml->sml_desc->ad_cname.bv_val;
315 switch ( ml->sml_op ) {
319 case LDAP_MOD_DELETE:
320 did = "delete"; break;
322 case LDAP_MOD_REPLACE:
323 did = "replace"; break;
325 case LDAP_MOD_INCREMENT:
326 did = "increment"; break;
328 if ( ri && ri->ri_attrs ) {
329 int is_in = ad_inlist( ml->sml_desc, ri->ri_attrs );
332 * 1) the attribute is not in the list,
333 * and it's not an exclusion list
334 * 2) the attribute is in the list
335 * and it's an exclusion list,
336 * and either the objectClass attribute
337 * has already been dealt with or
338 * this is not the objectClass attr
340 if ( ( !is_in && !ri->ri_exclude )
341 || ( ( is_in && ri->ri_exclude )
342 && ( !ocs || ml->sml_desc != slap_schema.si_ad_objectClass ) ) )
347 /* If this is objectClass, see if the value is included
348 * in any subset, otherwise drop it.
350 if ( ocs && ml->sml_desc == slap_schema.si_ad_objectClass
355 if ( ocs == -1 ) ocs = 0;
357 for ( i=0; ml->sml_values[i].bv_val; i++ ) {
359 for ( an = ri->ri_attrs; an->an_name.bv_val; an++ ) {
361 struct berval bv = an->an_name;
364 match |= an->an_oc_exclude;
366 switch ( bv.bv_val[ 0 ] ) {
375 if ( ml->sml_values[i].bv_len == bv.bv_len
376 && !strcasecmp(ml->sml_values[i].bv_val,
379 match = !an->an_oc_exclude;
384 /* Objectclasses need no special treatment, drop into
389 match ^= ri->ri_exclude;
390 /* Found a match, log it */
393 rephdr( ri, op, fp, now );
394 fprintf( fp, "changetype: modify\n" );
398 fprintf( fp, "%s: %s\n", did, type );
401 vals[0] = ml->sml_values[i];
402 print_vals( fp, &ml->sml_desc->ad_cname, vals );
407 /* Explicit objectclasses have been handled already */
410 fprintf( fp, "-\n" );
417 rephdr( ri, op, fp, now );
418 fprintf( fp, "changetype: modify\n" );
421 fprintf( fp, "%s: %s\n", did, type );
422 if ( ml->sml_values ) {
423 print_vals( fp, &ml->sml_desc->ad_cname, ml->sml_values );
425 fprintf( fp, "-\n" );
430 for ( a = op->ora_e->e_attrs ; a != NULL; a=a->a_next ) {
431 if ( ri && ri->ri_attrs ) {
432 int is_in = ad_inlist( a->a_desc, ri->ri_attrs );
435 * 1) the attribute is not in the list,
436 * and it's not an exclusion list
437 * 2) the attribute is in the list
438 * and it's an exclusion list,
439 * and either the objectClass attribute
440 * has already been dealt with or
441 * this is not the objectClass attr
443 if ( ( !is_in && !ri->ri_exclude )
444 || ( ( is_in && ri->ri_exclude )
445 && ( !ocs || a->a_desc != slap_schema.si_ad_objectClass ) ) )
450 /* If the list includes objectClass names,
451 * only include those classes in the
452 * objectClass attribute
454 if ( ocs && a->a_desc == slap_schema.si_ad_objectClass ) {
457 if ( ocs == -1 ) ocs = 0;
459 for ( i=0; a->a_vals[i].bv_val; i++ ) {
461 for ( an = ri->ri_attrs; an->an_name.bv_val; an++ ) {
463 struct berval bv = an->an_name;
466 match |= an->an_oc_exclude;
468 switch ( bv.bv_val[ 0 ] ) {
477 if ( a->a_vals[i].bv_len == bv.bv_len
478 && !strcasecmp(a->a_vals[i].bv_val,
481 match = !an->an_oc_exclude;
488 match ^= ri->ri_exclude;
491 rephdr( ri, op, fp, now );
492 fprintf( fp, "changetype: add\n" );
495 vals[0] = a->a_nvals[i];
496 print_vals( fp, &a->a_desc->ad_cname, vals );
503 rephdr( ri, op, fp, now );
504 fprintf( fp, "changetype: add\n" );
507 print_vals( fp, &a->a_desc->ad_cname, a->a_vals );
511 case LDAP_REQ_DELETE:
512 rephdr( ri, op, fp, now );
513 fprintf( fp, "changetype: delete\n" );
516 case LDAP_REQ_MODRDN:
517 rephdr( ri, op, fp, now );
518 fprintf( fp, "changetype: modrdn\n" );
519 fprintf( fp, "newrdn: %s\n", op->orr_newrdn.bv_val );
520 fprintf( fp, "deleteoldrdn: %d\n", op->orr_deleteoldrdn ? 1 : 0 );
521 if( op->orr_newSup != NULL ) {
522 fprintf( fp, "newsuperior: %s\n", op->orr_newSup->bv_val );
537 for ( i = 0, len = 0; bv && bv[i].bv_val; i++ ) {
538 if ( bv[i].bv_len > len )
542 len = LDIF_SIZE_NEEDED( type->bv_len, len ) + 1;
543 buf = (char *) ch_malloc( len );
545 for ( ; bv && bv->bv_val; bv++ ) {
547 ldif_sput( &bufp, LDIF_PUT_VALUE, type->bv_val,
548 bv->bv_val, bv->bv_len );