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
54 if ( be->be_replica != NULL ) {
55 for ( ; be->be_replica[ i ] != NULL; i++ );
58 be->be_replica = ch_realloc( be->be_replica,
59 sizeof( struct slap_replica_info * )*( i + 2 ) );
62 = ch_calloc( sizeof( struct slap_replica_info ), 1 );
63 be->be_replica[ i ]->ri_uri = uri;
64 be->be_replica[ i ]->ri_host = host;
65 be->be_replica[ i ]->ri_nsuffix = NULL;
66 be->be_replica[ i ]->ri_attrs = NULL;
67 be->be_replica[ i + 1 ] = NULL;
79 struct berval dn, ndn;
82 dn.bv_val = (char *) suffix;
83 dn.bv_len = strlen( dn.bv_val );
85 rc = dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL );
86 if( rc != LDAP_SUCCESS ) {
90 if ( select_backend( &ndn, 0, 0 ) != be ) {
95 ber_bvarray_add( &be->be_replica[nr]->ri_nsuffix, &ndn );
107 if ( be->be_replica[nr]->ri_attrs != NULL ) {
108 if ( be->be_replica[nr]->ri_exclude != exclude ) {
109 fprintf( stderr, "attr selective replication directive '%s' conflicts with previous one (discarded)\n", attrs );
110 ch_free( be->be_replica[nr]->ri_attrs );
111 be->be_replica[nr]->ri_attrs = NULL;
115 be->be_replica[nr]->ri_exclude = exclude;
116 be->be_replica[nr]->ri_attrs = str2anlist( be->be_replica[nr]->ri_attrs,
118 return ( be->be_replica[nr]->ri_attrs == NULL );
122 print_vals( FILE *fp, struct berval *type, struct berval *bv );
124 replog1( struct slap_replica_info *ri, Operation *op, FILE *fp, long now);
127 replog( Operation *op )
129 Modifications *ml = NULL;
133 /* undef NO_LOG_WHEN_NO_REPLICAS */
134 #ifdef NO_LOG_WHEN_NO_REPLICAS
138 long now = slap_get_time();
141 replogfile = op->o_bd->be_replogfile ? op->o_bd->be_replogfile :
142 frontendDB->be_replogfile;
147 ldap_pvt_thread_mutex_lock( &replog_mutex );
148 if ( (fp = lock_fopen( replogfile, "a", &lfp )) == NULL ) {
149 ldap_pvt_thread_mutex_unlock( &replog_mutex );
153 for ( i = 0; op->o_bd->be_replica != NULL && op->o_bd->be_replica[i] != NULL; i++ ) {
154 /* check if dn's suffix matches legal suffixes, if any */
155 if ( op->o_bd->be_replica[i]->ri_nsuffix != NULL ) {
158 for ( j = 0; op->o_bd->be_replica[i]->ri_nsuffix[j].bv_val; j++ ) {
159 if ( dnIsSuffix( &op->o_req_ndn, &op->o_bd->be_replica[i]->ri_nsuffix[j] ) ) {
164 if ( !op->o_bd->be_replica[i]->ri_nsuffix[j].bv_val ) {
165 /* do not add "replica:" line */
169 /* See if we only want a subset of attributes */
170 if ( op->o_bd->be_replica[i]->ri_attrs != NULL &&
171 ( op->o_tag == LDAP_REQ_MODIFY || op->o_tag == LDAP_REQ_ADD || op->o_tag == LDAP_REQ_EXTENDED ) ) {
175 /* Do attribute subsets by themselves in a second pass */
179 fprintf( fp, "replica: %s\n", op->o_bd->be_replica[i]->ri_host );
180 #ifdef NO_LOG_WHEN_NO_REPLICAS
185 #ifdef NO_LOG_WHEN_NO_REPLICAS
186 if ( count == 0 && subsets == 0 ) {
187 /* if no replicas matched, drop the log
188 * (should we log it anyway?) */
189 lock_fclose( fp, lfp );
190 ldap_pvt_thread_mutex_unlock( &replog_mutex );
196 replog1( NULL, op, fp, now );
199 for ( i = subsets - 1; op->o_bd->be_replica[i] != NULL; i++ ) {
201 /* If no attrs, we already did this above */
202 if ( op->o_bd->be_replica[i]->ri_attrs == NULL ) {
206 /* check if dn's suffix matches legal suffixes, if any */
207 if ( op->o_bd->be_replica[i]->ri_nsuffix != NULL ) {
210 for ( j = 0; op->o_bd->be_replica[i]->ri_nsuffix[j].bv_val; j++ ) {
211 if ( dnIsSuffix( &op->o_req_ndn, &op->o_bd->be_replica[i]->ri_nsuffix[j] ) ) {
216 if ( !op->o_bd->be_replica[i]->ri_nsuffix[j].bv_val ) {
217 /* no matching suffix found, skip it */
221 switch( op->o_tag ) {
222 case LDAP_REQ_EXTENDED:
223 /* quick hack for extended operations */
224 /* assume change parameter is a Modifications* */
226 case LDAP_REQ_MODIFY:
230 /* Other operations were logged in the first pass */
233 replog1( op->o_bd->be_replica[i], op, fp, now );
237 lock_fclose( fp, lfp );
238 ldap_pvt_thread_mutex_unlock( &replog_mutex );
243 struct slap_replica_info *ri,
250 fprintf( fp, "replica: %s\n", ri->ri_host );
252 fprintf( fp, "time: %ld\n", now );
253 fprintf( fp, "dn: %s\n", op->o_req_dn.bv_val );
258 struct slap_replica_info *ri,
267 int dohdr = 1, ocs = -1;
268 struct berval vals[2];
270 vals[1].bv_val = NULL;
273 switch ( op->o_tag ) {
274 case LDAP_REQ_EXTENDED:
275 /* quick hack for extended operations */
276 /* assume change parameter is a Modifications* */
279 case LDAP_REQ_MODIFY:
280 for ( ml = op->orm_modlist; ml != NULL; ml = ml->sml_next ) {
281 char *did, *type = ml->sml_desc->ad_cname.bv_val;
282 switch ( ml->sml_op ) {
286 case LDAP_MOD_DELETE:
287 did = "delete"; break;
289 case LDAP_MOD_REPLACE:
290 did = "replace"; break;
292 case LDAP_MOD_INCREMENT:
293 did = "increment"; break;
295 if ( ri && ri->ri_attrs ) {
296 int is_in = ad_inlist( ml->sml_desc, ri->ri_attrs );
298 if ( ( !is_in && !ri->ri_exclude )
299 || ( is_in && ri->ri_exclude ) )
303 /* If this is objectClass, see if the value is included
304 * in any subset, otherwise drop it.
306 if ( ocs && ml->sml_desc == slap_schema.si_ad_objectClass
311 if ( ocs == -1 ) ocs = 0;
313 for ( i=0; ml->sml_values[i].bv_val; i++ ) {
315 for ( an = ri->ri_attrs; an->an_name.bv_val; an++ ) {
318 match |= an->an_oc_exclude;
319 if ( ml->sml_values[i].bv_len == an->an_name.bv_len
320 && !strcasecmp(ml->sml_values[i].bv_val,
321 an->an_name.bv_val ) ) {
322 match = !an->an_oc_exclude;
327 /* Objectclasses need no special treatment, drop into
332 match ^= ri->ri_exclude;
333 /* Found a match, log it */
336 rephdr( ri, op, fp, now );
337 fprintf( fp, "changetype: modify\n" );
341 fprintf( fp, "%s: %s\n", did, type );
344 vals[0] = an->an_name;
345 print_vals( fp, &ml->sml_desc->ad_cname, vals );
350 /* Explicit objectclasses have been handled already */
353 fprintf( fp, "-\n" );
360 rephdr( ri, op, fp, now );
361 fprintf( fp, "changetype: modify\n" );
364 fprintf( fp, "%s: %s\n", did, type );
365 if ( ml->sml_values ) {
366 print_vals( fp, &ml->sml_desc->ad_cname, ml->sml_values );
368 fprintf( fp, "-\n" );
373 for ( a = op->ora_e->e_attrs ; a != NULL; a=a->a_next ) {
374 if ( ri && ri->ri_attrs ) {
375 int is_in = ad_inlist( a->a_desc, ri->ri_attrs );
376 if ( ( !is_in && !ri->ri_exclude ) || ( is_in && ri->ri_exclude ) ) {
380 /* If the list includes objectClass names,
381 * only include those classes in the
382 * objectClass attribute
384 if ( ocs && a->a_desc == slap_schema.si_ad_objectClass ) {
387 if ( ocs == -1 ) ocs = 0;
389 for ( i=0; a->a_vals[i].bv_val; i++ ) {
391 for ( an = ri->ri_attrs; an->an_name.bv_val; an++ ) {
394 match |= an->an_oc_exclude;
395 if ( a->a_vals[i].bv_len == an->an_name.bv_len
396 && !strcasecmp(a->a_vals[i].bv_val,
397 an->an_name.bv_val ) ) {
398 match = !an->an_oc_exclude;
405 match ^= ri->ri_exclude;
408 rephdr( ri, op, fp, now );
409 fprintf( fp, "changetype: add\n" );
412 vals[0] = an->an_name;
413 print_vals( fp, &a->a_desc->ad_cname, vals );
420 rephdr( ri, op, fp, now );
421 fprintf( fp, "changetype: add\n" );
424 print_vals( fp, &a->a_desc->ad_cname, a->a_vals );
428 case LDAP_REQ_DELETE:
429 rephdr( ri, op, fp, now );
430 fprintf( fp, "changetype: delete\n" );
433 case LDAP_REQ_MODRDN:
434 rephdr( ri, op, fp, now );
435 fprintf( fp, "changetype: modrdn\n" );
436 fprintf( fp, "newrdn: %s\n", op->orr_newrdn.bv_val );
437 fprintf( fp, "deleteoldrdn: %d\n", op->orr_deleteoldrdn ? 1 : 0 );
438 if( op->orr_newSup != NULL ) {
439 fprintf( fp, "newsuperior: %s\n", op->orr_newSup->bv_val );
454 for ( i = 0, len = 0; bv && bv[i].bv_val; i++ ) {
455 if ( bv[i].bv_len > len )
459 len = LDIF_SIZE_NEEDED( type->bv_len, len ) + 1;
460 buf = (char *) ch_malloc( len );
462 for ( ; bv && bv->bv_val; bv++ ) {
464 ldif_sput( &bufp, LDIF_PUT_VALUE, type->bv_val,
465 bv->bv_val, bv->bv_len );