1 /* repl.c - log modifications for replication purposes */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 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, long now);
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 replog1( NULL, op, fp, now );
195 for ( i = subsets - 1; op->o_bd->be_replica[i] != NULL; i++ ) {
197 /* If no attrs, we already did this above */
198 if ( op->o_bd->be_replica[i]->ri_attrs == NULL ) {
202 /* check if dn's suffix matches legal suffixes, if any */
203 if ( op->o_bd->be_replica[i]->ri_nsuffix != NULL ) {
206 for ( j = 0; op->o_bd->be_replica[i]->ri_nsuffix[j].bv_val; j++ ) {
207 if ( dnIsSuffix( &op->o_req_ndn, &op->o_bd->be_replica[i]->ri_nsuffix[j] ) ) {
212 if ( !op->o_bd->be_replica[i]->ri_nsuffix[j].bv_val ) {
213 /* no matching suffix found, skip it */
217 switch( op->o_tag ) {
218 case LDAP_REQ_EXTENDED:
219 /* quick hack for extended operations */
220 /* assume change parameter is a Modifications* */
222 case LDAP_REQ_MODIFY:
226 /* Other operations were logged in the first pass */
229 replog1( op->o_bd->be_replica[i], op, fp, now );
233 lock_fclose( fp, lfp );
234 ldap_pvt_thread_mutex_unlock( &replog_mutex );
239 struct slap_replica_info *ri,
246 fprintf( fp, "replica: %s\n", ri->ri_host );
248 fprintf( fp, "time: %ld\n", now );
249 fprintf( fp, "dn: %s\n", op->o_req_dn.bv_val );
254 struct slap_replica_info *ri,
263 int dohdr = 1, ocs = -1;
264 struct berval vals[2];
266 vals[1].bv_val = NULL;
269 switch ( op->o_tag ) {
270 case LDAP_REQ_EXTENDED:
271 /* quick hack for extended operations */
272 /* assume change parameter is a Modifications* */
275 case LDAP_REQ_MODIFY:
276 for ( ml = op->orm_modlist; ml != NULL; ml = ml->sml_next ) {
277 char *did, *type = ml->sml_desc->ad_cname.bv_val;
278 switch ( ml->sml_op ) {
282 case LDAP_MOD_DELETE:
283 did = "delete"; break;
285 case LDAP_MOD_REPLACE:
286 did = "replace"; break;
288 case LDAP_MOD_INCREMENT:
289 did = "increment"; break;
291 if ( ri && ri->ri_attrs ) {
292 int is_in = ad_inlist( ml->sml_desc, ri->ri_attrs );
294 if ( ( !is_in && !ri->ri_exclude )
295 || ( is_in && ri->ri_exclude ) )
299 /* If this is objectClass, see if the value is included
300 * in any subset, otherwise drop it.
302 if ( ocs && ml->sml_desc == slap_schema.si_ad_objectClass
307 if ( ocs == -1 ) ocs = 0;
309 for ( i=0; ml->sml_values[i].bv_val; i++ ) {
311 for ( an = ri->ri_attrs; an->an_name.bv_val; an++ ) {
314 match |= an->an_oc_exclude;
315 if ( ml->sml_values[i].bv_len == an->an_name.bv_len
316 && !strcasecmp(ml->sml_values[i].bv_val,
317 an->an_name.bv_val ) ) {
318 match = !an->an_oc_exclude;
323 /* Objectclasses need no special treatment, drop into
328 match ^= ri->ri_exclude;
329 /* Found a match, log it */
332 rephdr( ri, op, fp, now );
333 fprintf( fp, "changetype: modify\n" );
337 fprintf( fp, "%s: %s\n", did, type );
340 vals[0] = an->an_name;
341 print_vals( fp, &ml->sml_desc->ad_cname, vals );
346 /* Explicit objectclasses have been handled already */
349 fprintf( fp, "-\n" );
356 rephdr( ri, op, fp, now );
357 fprintf( fp, "changetype: modify\n" );
360 fprintf( fp, "%s: %s\n", did, type );
361 if ( ml->sml_values ) {
362 print_vals( fp, &ml->sml_desc->ad_cname, ml->sml_values );
364 fprintf( fp, "-\n" );
369 for ( a = op->ora_e->e_attrs ; a != NULL; a=a->a_next ) {
370 if ( ri && ri->ri_attrs ) {
371 int is_in = ad_inlist( a->a_desc, ri->ri_attrs );
372 if ( ( !is_in && !ri->ri_exclude ) || ( is_in && ri->ri_exclude ) ) {
376 /* If the list includes objectClass names,
377 * only include those classes in the
378 * objectClass attribute
380 if ( ocs && a->a_desc == slap_schema.si_ad_objectClass ) {
383 if ( ocs == -1 ) ocs = 0;
385 for ( i=0; a->a_vals[i].bv_val; i++ ) {
387 for ( an = ri->ri_attrs; an->an_name.bv_val; an++ ) {
390 match |= an->an_oc_exclude;
391 if ( a->a_vals[i].bv_len == an->an_name.bv_len
392 && !strcasecmp(a->a_vals[i].bv_val,
393 an->an_name.bv_val ) ) {
394 match = !an->an_oc_exclude;
401 match ^= ri->ri_exclude;
404 rephdr( ri, op, fp, now );
405 fprintf( fp, "changetype: add\n" );
408 vals[0] = an->an_name;
409 print_vals( fp, &a->a_desc->ad_cname, vals );
416 rephdr( ri, op, fp, now );
417 fprintf( fp, "changetype: add\n" );
420 print_vals( fp, &a->a_desc->ad_cname, a->a_vals );
424 case LDAP_REQ_DELETE:
425 rephdr( ri, op, fp, now );
426 fprintf( fp, "changetype: delete\n" );
429 case LDAP_REQ_MODRDN:
430 rephdr( ri, op, fp, now );
431 fprintf( fp, "changetype: modrdn\n" );
432 fprintf( fp, "newrdn: %s\n", op->orr_newrdn.bv_val );
433 fprintf( fp, "deleteoldrdn: %d\n", op->orr_deleteoldrdn ? 1 : 0 );
434 if( op->orr_newSup != NULL ) {
435 fprintf( fp, "newsuperior: %s\n", op->orr_newSup->bv_val );
450 for ( i = 0, len = 0; bv && bv[i].bv_val; i++ ) {
451 if ( bv[i].bv_len > len )
455 len = LDIF_SIZE_NEEDED( type->bv_len, len ) + 1;
456 buf = (char *) ch_malloc( len );
458 for ( ; bv && bv->bv_val; bv++ ) {
460 ldif_sput( &bufp, LDIF_PUT_VALUE, type->bv_val,
461 bv->bv_val, bv->bv_len );