1 /* repl.c - log modifications for replication purposes */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2003 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
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_host = ch_strdup( host );
63 be->be_replica[ i ]->ri_nsuffix = NULL;
64 be->be_replica[ i ]->ri_attrs = NULL;
65 be->be_replica[ i + 1 ] = NULL;
77 struct berval dn, ndn;
80 dn.bv_val = (char *) suffix;
81 dn.bv_len = strlen( dn.bv_val );
83 rc = dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL );
84 if( rc != LDAP_SUCCESS ) {
88 if ( select_backend( &ndn, 0, 0 ) != be ) {
93 ber_bvarray_add( &be->be_replica[nr]->ri_nsuffix, &ndn );
105 if ( be->be_replica[nr]->ri_attrs != NULL ) {
106 if ( be->be_replica[nr]->ri_exclude != exclude ) {
107 fprintf( stderr, "attr selective replication directive '%s' conflicts with previous one (discarded)\n", attrs );
108 ch_free( be->be_replica[nr]->ri_attrs );
109 be->be_replica[nr]->ri_attrs = NULL;
113 be->be_replica[nr]->ri_exclude = exclude;
114 be->be_replica[nr]->ri_attrs = str2anlist( be->be_replica[nr]->ri_attrs,
116 return ( be->be_replica[nr]->ri_attrs == NULL );
120 print_vals( FILE *fp, struct berval *type, struct berval *bv );
122 replog1( struct slap_replica_info *ri, Operation *op, FILE *fp, void *first);
125 replog( Operation *op )
127 Modifications *ml = NULL;
131 /* undef NO_LOG_WHEN_NO_REPLICAS */
132 #ifdef NO_LOG_WHEN_NO_REPLICAS
136 long now = slap_get_time();
138 if ( op->o_bd->be_replogfile == NULL && replogfile == NULL ) {
142 ldap_pvt_thread_mutex_lock( &replog_mutex );
143 if ( (fp = lock_fopen( op->o_bd->be_replogfile ? op->o_bd->be_replogfile :
144 replogfile, "a", &lfp )) == NULL ) {
145 ldap_pvt_thread_mutex_unlock( &replog_mutex );
149 for ( i = 0; op->o_bd->be_replica != NULL && op->o_bd->be_replica[i] != NULL; i++ ) {
150 /* check if dn's suffix matches legal suffixes, if any */
151 if ( op->o_bd->be_replica[i]->ri_nsuffix != NULL ) {
154 for ( j = 0; op->o_bd->be_replica[i]->ri_nsuffix[j].bv_val; j++ ) {
155 if ( dnIsSuffix( &op->o_req_ndn, &op->o_bd->be_replica[i]->ri_nsuffix[j] ) ) {
160 if ( !op->o_bd->be_replica[i]->ri_nsuffix[j].bv_val ) {
161 /* do not add "replica:" line */
165 /* See if we only want a subset of attributes */
166 if ( op->o_bd->be_replica[i]->ri_attrs != NULL &&
167 ( op->o_tag == LDAP_REQ_MODIFY || op->o_tag == LDAP_REQ_ADD || op->o_tag == LDAP_REQ_EXTENDED ) ) {
171 /* Do attribute subsets by themselves in a second pass */
175 fprintf( fp, "replica: %s\n", op->o_bd->be_replica[i]->ri_host );
176 #ifdef NO_LOG_WHEN_NO_REPLICAS
181 #ifdef NO_LOG_WHEN_NO_REPLICAS
182 if ( count == 0 && subsets == 0 ) {
183 /* if no replicas matched, drop the log
184 * (should we log it anyway?) */
185 lock_fclose( fp, lfp );
186 ldap_pvt_thread_mutex_unlock( &replog_mutex );
192 fprintf( fp, "time: %ld\n", now );
193 fprintf( fp, "dn: %s\n", op->o_req_dn.bv_val );
195 replog1( NULL, op, fp, NULL );
199 for ( i = subsets - 1; op->o_bd->be_replica != NULL && 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 /* do not add "replica:" line */
223 switch( op->o_tag ) {
224 case LDAP_REQ_EXTENDED:
225 /* quick hack for extended operations */
226 /* assume change parameter is a Modfications* */
228 case LDAP_REQ_MODIFY:
229 for ( ml = op->orm_modlist; ml != NULL; ml = ml->sml_next ) {
230 int is_in, exclude, ocs = 0;
232 exclude = op->o_bd->be_replica[i]->ri_exclude;
234 if ( ml->sml_desc == slap_schema.si_ad_objectClass
235 && ml->sml_bvalues ) {
237 for ( an = op->o_bd->be_replica[i]->ri_attrs; an->an_name.bv_val; an++ ) {
241 for ( i=0; ml->sml_bvalues[i].bv_val; i++ ) {
242 if ( ml->sml_bvalues[i].bv_len == an->an_name.bv_len
243 && !strcasecmp(ml->sml_bvalues[i].bv_val,
244 an->an_name.bv_val ) ) {
250 match ^= an->an_oc_exclude;
251 /* Found a match, stop looking */
258 if ( ocs == 1 ) continue;
261 is_in = ad_inlist( ml->sml_desc, op->o_bd->be_replica[i]->ri_attrs );
264 * there might be a more clever way to do this test,
265 * but this way, at least, is comprehensible :)
267 if ( ( is_in && !exclude ) || ( !is_in && exclude ) ) {
278 for ( a = op->ora_e->e_attrs; a != NULL; a = a->a_next ) {
281 is_in = ad_inlist( a->a_desc, op->o_bd->be_replica[i]->ri_attrs );
282 exclude = op->o_bd->be_replica[i]->ri_exclude;
284 if ( ( is_in && !exclude ) || ( !is_in && exclude ) ) {
295 /* Other operations were logged in the first pass */
298 fprintf( fp, "replica: %s\n", op->o_bd->be_replica[i]->ri_host );
299 fprintf( fp, "time: %ld\n", now );
300 fprintf( fp, "dn: %s\n", op->o_req_dn.bv_val );
301 replog1( op->o_bd->be_replica[i], op, fp, first );
305 lock_fclose( fp, lfp );
306 ldap_pvt_thread_mutex_unlock( &replog_mutex );
312 struct slap_replica_info *ri,
323 switch ( op->o_tag ) {
324 case LDAP_REQ_EXTENDED:
325 /* quick hack for extended operations */
326 /* assume change parameter is a Modfications* */
329 case LDAP_REQ_MODIFY:
330 ml = first ? first : op->orm_modlist;
331 for ( ; ml != NULL; ml = ml->sml_next ) {
334 if ( ri && ri->ri_attrs ) {
335 int is_in = ad_inlist( ml->sml_desc, ri->ri_attrs );
337 if ( ( !is_in && !ri->ri_exclude )
338 || ( is_in && ri->ri_exclude ) )
342 /* If this is objectClass, see if the value is included
343 * in any subset, otherwise drop it.
345 if ( ml->sml_desc == slap_schema.si_ad_objectClass
346 && ml->sml_bvalues ) {
347 for ( an = ri->ri_attrs; an->an_name.bv_val; an++ ) {
351 for ( i=0; ml->sml_bvalues[i].bv_val; i++ ) {
352 if ( ml->sml_bvalues[i].bv_len == an->an_name.bv_len
353 && !strcasecmp(ml->sml_bvalues[i].bv_val,
354 an->an_name.bv_val ) ) {
359 match ^= ri->ri_exclude;
360 match ^= an->an_oc_exclude;
361 /* Found a match, stop looking */
369 /* Have explicit objectClasses, but none matched - forget it */
370 if ( ocs == 1 ) continue;
373 fprintf( fp, "changetype: modify\n" );
376 type = ml->sml_desc->ad_cname.bv_val;
377 switch ( ml->sml_op ) {
379 fprintf( fp, "add: %s\n", type );
382 case LDAP_MOD_DELETE:
383 fprintf( fp, "delete: %s\n", type );
386 case LDAP_MOD_REPLACE:
387 fprintf( fp, "replace: %s\n", type );
390 case LDAP_MOD_INCREMENT:
391 fprintf( fp, "increment: %s\n", type );
396 struct berval vals[2];
397 vals[1].bv_val = NULL;
399 for ( i=0; ml->sml_bvalues[i].bv_val; i++ ) {
401 for ( an = ri->ri_attrs; an->an_name.bv_val; an++ ) {
403 && ml->sml_bvalues[i].bv_len == an->an_name.bv_len
404 && !strcasecmp(ml->sml_bvalues[i].bv_val,
405 an->an_name.bv_val ) ) {
406 match = 1 ^ an->an_oc_exclude;
410 match ^= ri->ri_exclude;
412 vals[0] = an->an_name;
413 print_vals( fp, &ml->sml_desc->ad_cname, vals );
416 } else if ( ml->sml_bvalues ) {
417 print_vals( fp, &ml->sml_desc->ad_cname, ml->sml_bvalues );
419 fprintf( fp, "-\n" );
424 fprintf( fp, "changetype: add\n" );
425 a = first ? first : op->ora_e->e_attrs;
426 for ( ; a != NULL; a=a->a_next ) {
427 if ( ri && ri->ri_attrs ) {
428 int is_in = ad_inlist( a->a_desc, ri->ri_attrs );
429 if ( ( !is_in && !ri->ri_exclude ) || ( is_in && ri->ri_exclude ) ) {
433 /* If the list includes objectClass names,
434 * only include those classes in the
435 * objectClass attribute
437 if ( a->a_desc == slap_schema.si_ad_objectClass ) {
439 struct berval vals[2];
440 vals[1].bv_val = NULL;
442 for ( i=0; a->a_vals[i].bv_val; i++ ) {
444 for ( an = ri->ri_attrs; an->an_name.bv_val; an++ ) {
447 if ( a->a_vals[i].bv_len == an->an_name.bv_len
448 && !strcasecmp(a->a_vals[i].bv_val,
449 an->an_name.bv_val ) ) {
450 match = 1 ^ an->an_oc_exclude;
455 match ^= ri->ri_exclude;
456 if ( ocs && match ) {
457 vals[0] = an->an_name;
458 print_vals( fp, &a->a_desc->ad_cname, vals );
464 print_vals( fp, &a->a_desc->ad_cname, a->a_vals );
468 case LDAP_REQ_DELETE:
469 fprintf( fp, "changetype: delete\n" );
472 case LDAP_REQ_MODRDN:
473 fprintf( fp, "changetype: modrdn\n" );
474 fprintf( fp, "newrdn: %s\n", op->orr_newrdn.bv_val );
475 fprintf( fp, "deleteoldrdn: %d\n", op->orr_deleteoldrdn ? 1 : 0 );
476 if( op->orr_newSup != NULL ) {
477 fprintf( fp, "newsuperior: %s\n", op->orr_newSup->bv_val );
492 for ( i = 0, len = 0; bv && bv[i].bv_val; i++ ) {
493 if ( bv[i].bv_len > len )
497 len = LDIF_SIZE_NEEDED( type->bv_len, len ) + 1;
498 buf = (char *) ch_malloc( len );
500 for ( ; bv && bv->bv_val; bv++ ) {
502 ldif_sput( &bufp, LDIF_PUT_VALUE, type->bv_val,
503 bv->bv_val, bv->bv_len );