1 /* repl.c - log modifications for replication purposes */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 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 );
331 if ( ( !is_in && !ri->ri_exclude )
332 || ( is_in && ri->ri_exclude ) )
336 /* If this is objectClass, see if the value is included
337 * in any subset, otherwise drop it.
339 if ( ocs && ml->sml_desc == slap_schema.si_ad_objectClass
344 if ( ocs == -1 ) ocs = 0;
346 for ( i=0; ml->sml_values[i].bv_val; i++ ) {
348 for ( an = ri->ri_attrs; an->an_name.bv_val; an++ ) {
351 match |= an->an_oc_exclude;
352 if ( ml->sml_values[i].bv_len == an->an_name.bv_len
353 && !strcasecmp(ml->sml_values[i].bv_val,
354 an->an_name.bv_val ) ) {
355 match = !an->an_oc_exclude;
360 /* Objectclasses need no special treatment, drop into
365 match ^= ri->ri_exclude;
366 /* Found a match, log it */
369 rephdr( ri, op, fp, now );
370 fprintf( fp, "changetype: modify\n" );
374 fprintf( fp, "%s: %s\n", did, type );
377 vals[0] = an->an_name;
378 print_vals( fp, &ml->sml_desc->ad_cname, vals );
383 /* Explicit objectclasses have been handled already */
386 fprintf( fp, "-\n" );
393 rephdr( ri, op, fp, now );
394 fprintf( fp, "changetype: modify\n" );
397 fprintf( fp, "%s: %s\n", did, type );
398 if ( ml->sml_values ) {
399 print_vals( fp, &ml->sml_desc->ad_cname, ml->sml_values );
401 fprintf( fp, "-\n" );
406 for ( a = op->ora_e->e_attrs ; a != NULL; a=a->a_next ) {
407 if ( ri && ri->ri_attrs ) {
408 int is_in = ad_inlist( a->a_desc, ri->ri_attrs );
409 if ( ( !is_in && !ri->ri_exclude ) || ( is_in && ri->ri_exclude ) ) {
413 /* If the list includes objectClass names,
414 * only include those classes in the
415 * objectClass attribute
417 if ( ocs && a->a_desc == slap_schema.si_ad_objectClass ) {
420 if ( ocs == -1 ) ocs = 0;
422 for ( i=0; a->a_vals[i].bv_val; i++ ) {
424 for ( an = ri->ri_attrs; an->an_name.bv_val; an++ ) {
427 match |= an->an_oc_exclude;
428 if ( a->a_vals[i].bv_len == an->an_name.bv_len
429 && !strcasecmp(a->a_vals[i].bv_val,
430 an->an_name.bv_val ) ) {
431 match = !an->an_oc_exclude;
438 match ^= ri->ri_exclude;
441 rephdr( ri, op, fp, now );
442 fprintf( fp, "changetype: add\n" );
445 vals[0] = an->an_name;
446 print_vals( fp, &a->a_desc->ad_cname, vals );
453 rephdr( ri, op, fp, now );
454 fprintf( fp, "changetype: add\n" );
457 print_vals( fp, &a->a_desc->ad_cname, a->a_vals );
461 case LDAP_REQ_DELETE:
462 rephdr( ri, op, fp, now );
463 fprintf( fp, "changetype: delete\n" );
466 case LDAP_REQ_MODRDN:
467 rephdr( ri, op, fp, now );
468 fprintf( fp, "changetype: modrdn\n" );
469 fprintf( fp, "newrdn: %s\n", op->orr_newrdn.bv_val );
470 fprintf( fp, "deleteoldrdn: %d\n", op->orr_deleteoldrdn ? 1 : 0 );
471 if( op->orr_newSup != NULL ) {
472 fprintf( fp, "newsuperior: %s\n", op->orr_newSup->bv_val );
487 for ( i = 0, len = 0; bv && bv[i].bv_val; i++ ) {
488 if ( bv[i].bv_len > len )
492 len = LDIF_SIZE_NEEDED( type->bv_len, len ) + 1;
493 buf = (char *) ch_malloc( len );
495 for ( ; bv && bv->bv_val; bv++ ) {
497 ldif_sput( &bufp, LDIF_PUT_VALUE, type->bv_val,
498 bv->bv_val, bv->bv_len );