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 be->be_replica[ i ]->ri_uri = 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++ ) {
85 ch_free( (char *)be->be_replica[ i ]->ri_uri );
87 ber_bvarray_free( be->be_replica[ i ]->ri_nsuffix );
89 if ( be->be_replica[ i ]->ri_attrs ) {
90 AttributeName *an = be->be_replica[ i ]->ri_attrs;
93 for ( j = 0; !BER_BVISNULL( &an[ j ].an_name ); j++ )
95 ch_free( an[ j ].an_name.bv_val );
100 bindconf_free( &be->be_replica[ i ]->ri_bindconf );
102 ch_free( be->be_replica[ i ] );
105 ch_free( be->be_replica );
117 struct berval dn, ndn;
120 dn.bv_val = (char *) suffix;
121 dn.bv_len = strlen( dn.bv_val );
123 rc = dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL );
124 if( rc != LDAP_SUCCESS ) {
128 if ( select_backend( &ndn, 0, 0 ) != be ) {
133 ber_bvarray_add( &be->be_replica[nr]->ri_nsuffix, &ndn );
145 if ( be->be_replica[nr]->ri_attrs != NULL ) {
146 if ( be->be_replica[nr]->ri_exclude != exclude ) {
147 fprintf( stderr, "attr selective replication directive '%s' conflicts with previous one (discarded)\n", attrs );
148 ch_free( be->be_replica[nr]->ri_attrs );
149 be->be_replica[nr]->ri_attrs = NULL;
153 be->be_replica[nr]->ri_exclude = exclude;
154 be->be_replica[nr]->ri_attrs = str2anlist( be->be_replica[nr]->ri_attrs,
156 return ( be->be_replica[nr]->ri_attrs == NULL );
160 print_vals( FILE *fp, struct berval *type, struct berval *bv );
162 replog1( struct slap_replica_info *ri, Operation *op, FILE *fp, long now);
165 replog( Operation *op )
169 /* undef NO_LOG_WHEN_NO_REPLICAS */
170 #ifdef NO_LOG_WHEN_NO_REPLICAS
174 long now = slap_get_time();
177 replogfile = op->o_bd->be_replogfile ? op->o_bd->be_replogfile :
178 frontendDB->be_replogfile;
183 ldap_pvt_thread_mutex_lock( &replog_mutex );
184 if ( (fp = lock_fopen( replogfile, "a", &lfp )) == NULL ) {
185 ldap_pvt_thread_mutex_unlock( &replog_mutex );
189 for ( i = 0; op->o_bd->be_replica != NULL && op->o_bd->be_replica[i] != NULL; i++ ) {
190 /* check if dn's suffix matches legal suffixes, if any */
191 if ( op->o_bd->be_replica[i]->ri_nsuffix != NULL ) {
194 for ( j = 0; op->o_bd->be_replica[i]->ri_nsuffix[j].bv_val; j++ ) {
195 if ( dnIsSuffix( &op->o_req_ndn, &op->o_bd->be_replica[i]->ri_nsuffix[j] ) ) {
200 if ( !op->o_bd->be_replica[i]->ri_nsuffix[j].bv_val ) {
201 /* do not add "replica:" line */
205 /* See if we only want a subset of attributes */
206 if ( op->o_bd->be_replica[i]->ri_attrs != NULL &&
207 ( op->o_tag == LDAP_REQ_MODIFY || op->o_tag == LDAP_REQ_ADD || op->o_tag == LDAP_REQ_EXTENDED ) ) {
211 /* Do attribute subsets by themselves in a second pass */
215 fprintf( fp, "replica: %s\n", op->o_bd->be_replica[i]->ri_host );
216 #ifdef NO_LOG_WHEN_NO_REPLICAS
221 #ifdef NO_LOG_WHEN_NO_REPLICAS
222 if ( count == 0 && subsets == 0 ) {
223 /* if no replicas matched, drop the log
224 * (should we log it anyway?) */
225 lock_fclose( fp, lfp );
226 ldap_pvt_thread_mutex_unlock( &replog_mutex );
232 replog1( NULL, op, fp, now );
235 for ( i = subsets - 1; op->o_bd->be_replica[i] != NULL; i++ ) {
237 /* If no attrs, we already did this above */
238 if ( op->o_bd->be_replica[i]->ri_attrs == NULL ) {
242 /* check if dn's suffix matches legal suffixes, if any */
243 if ( op->o_bd->be_replica[i]->ri_nsuffix != NULL ) {
246 for ( j = 0; op->o_bd->be_replica[i]->ri_nsuffix[j].bv_val; j++ ) {
247 if ( dnIsSuffix( &op->o_req_ndn, &op->o_bd->be_replica[i]->ri_nsuffix[j] ) ) {
252 if ( !op->o_bd->be_replica[i]->ri_nsuffix[j].bv_val ) {
253 /* no matching suffix found, skip it */
257 switch( op->o_tag ) {
258 case LDAP_REQ_EXTENDED:
259 /* quick hack for extended operations */
260 /* assume change parameter is a Modifications* */
262 case LDAP_REQ_MODIFY:
266 /* Other operations were logged in the first pass */
269 replog1( op->o_bd->be_replica[i], op, fp, now );
273 lock_fclose( fp, lfp );
274 ldap_pvt_thread_mutex_unlock( &replog_mutex );
279 struct slap_replica_info *ri,
286 fprintf( fp, "replica: %s\n", ri->ri_host );
288 fprintf( fp, "time: %ld\n", now );
289 fprintf( fp, "dn: %s\n", op->o_req_dn.bv_val );
294 struct slap_replica_info *ri,
303 int dohdr = 1, ocs = -1;
304 struct berval vals[2];
306 vals[1].bv_val = NULL;
309 switch ( op->o_tag ) {
310 case LDAP_REQ_EXTENDED:
311 /* quick hack for extended operations */
312 /* assume change parameter is a Modifications* */
315 case LDAP_REQ_MODIFY:
316 for ( ml = op->orm_modlist; ml != NULL; ml = ml->sml_next ) {
317 char *did = NULL, *type = ml->sml_desc->ad_cname.bv_val;
318 switch ( ml->sml_op ) {
322 case LDAP_MOD_DELETE:
323 did = "delete"; break;
325 case LDAP_MOD_REPLACE:
326 did = "replace"; break;
328 case LDAP_MOD_INCREMENT:
329 did = "increment"; break;
331 if ( ri && ri->ri_attrs ) {
332 int is_in = ad_inlist( ml->sml_desc, ri->ri_attrs );
334 if ( ( !is_in && !ri->ri_exclude )
335 || ( is_in && ri->ri_exclude ) )
339 /* If this is objectClass, see if the value is included
340 * in any subset, otherwise drop it.
342 if ( ocs && ml->sml_desc == slap_schema.si_ad_objectClass
347 if ( ocs == -1 ) ocs = 0;
349 for ( i=0; ml->sml_values[i].bv_val; i++ ) {
351 for ( an = ri->ri_attrs; an->an_name.bv_val; an++ ) {
354 match |= an->an_oc_exclude;
355 if ( ml->sml_values[i].bv_len == an->an_name.bv_len
356 && !strcasecmp(ml->sml_values[i].bv_val,
357 an->an_name.bv_val ) ) {
358 match = !an->an_oc_exclude;
363 /* Objectclasses need no special treatment, drop into
368 match ^= ri->ri_exclude;
369 /* Found a match, log it */
372 rephdr( ri, op, fp, now );
373 fprintf( fp, "changetype: modify\n" );
377 fprintf( fp, "%s: %s\n", did, type );
380 vals[0] = an->an_name;
381 print_vals( fp, &ml->sml_desc->ad_cname, vals );
386 /* Explicit objectclasses have been handled already */
389 fprintf( fp, "-\n" );
396 rephdr( ri, op, fp, now );
397 fprintf( fp, "changetype: modify\n" );
400 fprintf( fp, "%s: %s\n", did, type );
401 if ( ml->sml_values ) {
402 print_vals( fp, &ml->sml_desc->ad_cname, ml->sml_values );
404 fprintf( fp, "-\n" );
409 for ( a = op->ora_e->e_attrs ; a != NULL; a=a->a_next ) {
410 if ( ri && ri->ri_attrs ) {
411 int is_in = ad_inlist( a->a_desc, ri->ri_attrs );
412 if ( ( !is_in && !ri->ri_exclude ) || ( is_in && ri->ri_exclude ) ) {
416 /* If the list includes objectClass names,
417 * only include those classes in the
418 * objectClass attribute
420 if ( ocs && a->a_desc == slap_schema.si_ad_objectClass ) {
423 if ( ocs == -1 ) ocs = 0;
425 for ( i=0; a->a_vals[i].bv_val; i++ ) {
427 for ( an = ri->ri_attrs; an->an_name.bv_val; an++ ) {
430 match |= an->an_oc_exclude;
431 if ( a->a_vals[i].bv_len == an->an_name.bv_len
432 && !strcasecmp(a->a_vals[i].bv_val,
433 an->an_name.bv_val ) ) {
434 match = !an->an_oc_exclude;
441 match ^= ri->ri_exclude;
444 rephdr( ri, op, fp, now );
445 fprintf( fp, "changetype: add\n" );
448 vals[0] = an->an_name;
449 print_vals( fp, &a->a_desc->ad_cname, vals );
456 rephdr( ri, op, fp, now );
457 fprintf( fp, "changetype: add\n" );
460 print_vals( fp, &a->a_desc->ad_cname, a->a_vals );
464 case LDAP_REQ_DELETE:
465 rephdr( ri, op, fp, now );
466 fprintf( fp, "changetype: delete\n" );
469 case LDAP_REQ_MODRDN:
470 rephdr( ri, op, fp, now );
471 fprintf( fp, "changetype: modrdn\n" );
472 fprintf( fp, "newrdn: %s\n", op->orr_newrdn.bv_val );
473 fprintf( fp, "deleteoldrdn: %d\n", op->orr_deleteoldrdn ? 1 : 0 );
474 if( op->orr_newSup != NULL ) {
475 fprintf( fp, "newsuperior: %s\n", op->orr_newSup->bv_val );
490 for ( i = 0, len = 0; bv && bv[i].bv_val; i++ ) {
491 if ( bv[i].bv_len > len )
495 len = LDIF_SIZE_NEEDED( type->bv_len, len ) + 1;
496 buf = (char *) ch_malloc( len );
498 for ( ; bv && bv->bv_val; bv++ ) {
500 ldif_sput( &bufp, LDIF_PUT_VALUE, type->bv_val,
501 bv->bv_val, bv->bv_len );