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
52 assert( host != NULL );
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 )
131 /* undef NO_LOG_WHEN_NO_REPLICAS */
132 #ifdef NO_LOG_WHEN_NO_REPLICAS
136 long now = slap_get_time();
139 replogfile = op->o_bd->be_replogfile ? op->o_bd->be_replogfile :
140 frontendDB->be_replogfile;
145 ldap_pvt_thread_mutex_lock( &replog_mutex );
146 if ( (fp = lock_fopen( replogfile, "a", &lfp )) == NULL ) {
147 ldap_pvt_thread_mutex_unlock( &replog_mutex );
151 for ( i = 0; op->o_bd->be_replica != NULL && op->o_bd->be_replica[i] != NULL; i++ ) {
152 /* check if dn's suffix matches legal suffixes, if any */
153 if ( op->o_bd->be_replica[i]->ri_nsuffix != NULL ) {
156 for ( j = 0; op->o_bd->be_replica[i]->ri_nsuffix[j].bv_val; j++ ) {
157 if ( dnIsSuffix( &op->o_req_ndn, &op->o_bd->be_replica[i]->ri_nsuffix[j] ) ) {
162 if ( !op->o_bd->be_replica[i]->ri_nsuffix[j].bv_val ) {
163 /* do not add "replica:" line */
167 /* See if we only want a subset of attributes */
168 if ( op->o_bd->be_replica[i]->ri_attrs != NULL &&
169 ( op->o_tag == LDAP_REQ_MODIFY || op->o_tag == LDAP_REQ_ADD || op->o_tag == LDAP_REQ_EXTENDED ) ) {
173 /* Do attribute subsets by themselves in a second pass */
177 fprintf( fp, "replica: %s\n", op->o_bd->be_replica[i]->ri_host );
178 #ifdef NO_LOG_WHEN_NO_REPLICAS
183 #ifdef NO_LOG_WHEN_NO_REPLICAS
184 if ( count == 0 && subsets == 0 ) {
185 /* if no replicas matched, drop the log
186 * (should we log it anyway?) */
187 lock_fclose( fp, lfp );
188 ldap_pvt_thread_mutex_unlock( &replog_mutex );
194 replog1( NULL, op, fp, now );
197 for ( i = subsets - 1; op->o_bd->be_replica[i] != NULL; i++ ) {
199 /* If no attrs, we already did this above */
200 if ( op->o_bd->be_replica[i]->ri_attrs == NULL ) {
204 /* check if dn's suffix matches legal suffixes, if any */
205 if ( op->o_bd->be_replica[i]->ri_nsuffix != NULL ) {
208 for ( j = 0; op->o_bd->be_replica[i]->ri_nsuffix[j].bv_val; j++ ) {
209 if ( dnIsSuffix( &op->o_req_ndn, &op->o_bd->be_replica[i]->ri_nsuffix[j] ) ) {
214 if ( !op->o_bd->be_replica[i]->ri_nsuffix[j].bv_val ) {
215 /* no matching suffix found, skip it */
219 switch( op->o_tag ) {
220 case LDAP_REQ_EXTENDED:
221 /* quick hack for extended operations */
222 /* assume change parameter is a Modifications* */
224 case LDAP_REQ_MODIFY:
228 /* Other operations were logged in the first pass */
231 replog1( op->o_bd->be_replica[i], op, fp, now );
235 lock_fclose( fp, lfp );
236 ldap_pvt_thread_mutex_unlock( &replog_mutex );
241 struct slap_replica_info *ri,
248 fprintf( fp, "replica: %s\n", ri->ri_host );
250 fprintf( fp, "time: %ld\n", now );
251 fprintf( fp, "dn: %s\n", op->o_req_dn.bv_val );
256 struct slap_replica_info *ri,
265 int dohdr = 1, ocs = -1;
266 struct berval vals[2];
268 vals[1].bv_val = NULL;
271 switch ( op->o_tag ) {
272 case LDAP_REQ_EXTENDED:
273 /* quick hack for extended operations */
274 /* assume change parameter is a Modifications* */
277 case LDAP_REQ_MODIFY:
278 for ( ml = op->orm_modlist; ml != NULL; ml = ml->sml_next ) {
279 char *did = NULL, *type = ml->sml_desc->ad_cname.bv_val;
280 switch ( ml->sml_op ) {
284 case LDAP_MOD_DELETE:
285 did = "delete"; break;
287 case LDAP_MOD_REPLACE:
288 did = "replace"; break;
290 case LDAP_MOD_INCREMENT:
291 did = "increment"; break;
293 if ( ri && ri->ri_attrs ) {
294 int is_in = ad_inlist( ml->sml_desc, ri->ri_attrs );
296 if ( ( !is_in && !ri->ri_exclude )
297 || ( is_in && ri->ri_exclude ) )
301 /* If this is objectClass, see if the value is included
302 * in any subset, otherwise drop it.
304 if ( ocs && ml->sml_desc == slap_schema.si_ad_objectClass
309 if ( ocs == -1 ) ocs = 0;
311 for ( i=0; ml->sml_values[i].bv_val; i++ ) {
313 for ( an = ri->ri_attrs; an->an_name.bv_val; an++ ) {
316 match |= an->an_oc_exclude;
317 if ( ml->sml_values[i].bv_len == an->an_name.bv_len
318 && !strcasecmp(ml->sml_values[i].bv_val,
319 an->an_name.bv_val ) ) {
320 match = !an->an_oc_exclude;
325 /* Objectclasses need no special treatment, drop into
330 match ^= ri->ri_exclude;
331 /* Found a match, log it */
334 rephdr( ri, op, fp, now );
335 fprintf( fp, "changetype: modify\n" );
339 fprintf( fp, "%s: %s\n", did, type );
342 vals[0] = an->an_name;
343 print_vals( fp, &ml->sml_desc->ad_cname, vals );
348 /* Explicit objectclasses have been handled already */
351 fprintf( fp, "-\n" );
358 rephdr( ri, op, fp, now );
359 fprintf( fp, "changetype: modify\n" );
362 fprintf( fp, "%s: %s\n", did, type );
363 if ( ml->sml_values ) {
364 print_vals( fp, &ml->sml_desc->ad_cname, ml->sml_values );
366 fprintf( fp, "-\n" );
371 for ( a = op->ora_e->e_attrs ; a != NULL; a=a->a_next ) {
372 if ( ri && ri->ri_attrs ) {
373 int is_in = ad_inlist( a->a_desc, ri->ri_attrs );
374 if ( ( !is_in && !ri->ri_exclude ) || ( is_in && ri->ri_exclude ) ) {
378 /* If the list includes objectClass names,
379 * only include those classes in the
380 * objectClass attribute
382 if ( ocs && a->a_desc == slap_schema.si_ad_objectClass ) {
385 if ( ocs == -1 ) ocs = 0;
387 for ( i=0; a->a_vals[i].bv_val; i++ ) {
389 for ( an = ri->ri_attrs; an->an_name.bv_val; an++ ) {
392 match |= an->an_oc_exclude;
393 if ( a->a_vals[i].bv_len == an->an_name.bv_len
394 && !strcasecmp(a->a_vals[i].bv_val,
395 an->an_name.bv_val ) ) {
396 match = !an->an_oc_exclude;
403 match ^= ri->ri_exclude;
406 rephdr( ri, op, fp, now );
407 fprintf( fp, "changetype: add\n" );
410 vals[0] = an->an_name;
411 print_vals( fp, &a->a_desc->ad_cname, vals );
418 rephdr( ri, op, fp, now );
419 fprintf( fp, "changetype: add\n" );
422 print_vals( fp, &a->a_desc->ad_cname, a->a_vals );
426 case LDAP_REQ_DELETE:
427 rephdr( ri, op, fp, now );
428 fprintf( fp, "changetype: delete\n" );
431 case LDAP_REQ_MODRDN:
432 rephdr( ri, op, fp, now );
433 fprintf( fp, "changetype: modrdn\n" );
434 fprintf( fp, "newrdn: %s\n", op->orr_newrdn.bv_val );
435 fprintf( fp, "deleteoldrdn: %d\n", op->orr_deleteoldrdn ? 1 : 0 );
436 if( op->orr_newSup != NULL ) {
437 fprintf( fp, "newsuperior: %s\n", op->orr_newSup->bv_val );
452 for ( i = 0, len = 0; bv && bv[i].bv_val; i++ ) {
453 if ( bv[i].bv_len > len )
457 len = LDIF_SIZE_NEEDED( type->bv_len, len ) + 1;
458 buf = (char *) ch_malloc( len );
460 for ( ; bv && bv->bv_val; bv++ ) {
462 ldif_sput( &bufp, LDIF_PUT_VALUE, type->bv_val,
463 bv->bv_val, bv->bv_len );