1 /* repl.c - log modifications for replication purposes */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/string.h>
14 #include <ac/socket.h>
16 #ifdef HAVE_SYS_FILE_H
34 if ( be->be_replica != NULL ) {
35 for ( ; be->be_replica[ i ] != NULL; i++ );
38 be->be_replica = ch_realloc( be->be_replica,
39 sizeof( struct slap_replica_info * )*( i + 2 ) );
42 = ch_calloc( sizeof( struct slap_replica_info ), 1 );
43 be->be_replica[ i ]->ri_host = ch_strdup( host );
44 be->be_replica[ i ]->ri_nsuffix = NULL;
45 be->be_replica[ i ]->ri_attrs = NULL;
46 be->be_replica[ i + 1 ] = NULL;
58 struct berval dn, ndn;
61 dn.bv_val = (char *) suffix;
62 dn.bv_len = strlen( dn.bv_val );
64 rc = dnNormalize2( NULL, &dn, &ndn );
65 if( rc != LDAP_SUCCESS ) {
69 if ( select_backend( &ndn, 0, 0 ) != be ) {
74 ber_bvarray_add( &be->be_replica[nr]->ri_nsuffix, &ndn );
86 if ( be->be_replica[nr]->ri_attrs != NULL ) {
87 if ( be->be_replica[nr]->ri_exclude != exclude ) {
88 fprintf( stderr, "attr selective replication directive '%s' conflicts with previous one (discarded)\n", attrs );
89 ch_free( be->be_replica[nr]->ri_attrs );
90 be->be_replica[nr]->ri_attrs = NULL;
94 be->be_replica[nr]->ri_exclude = exclude;
95 be->be_replica[nr]->ri_attrs = str2anlist( be->be_replica[nr]->ri_attrs,
97 return ( be->be_replica[nr]->ri_attrs == NULL );
101 print_vals( FILE *fp, struct berval *type, struct berval *bv );
103 replog1( struct slap_replica_info *ri, Operation *op, void *change, FILE *fp, void *first);
114 Modifications *ml = NULL;
119 /* undef NO_LOG_WHEN_NO_REPLICAS */
120 #ifdef NO_LOG_WHEN_NO_REPLICAS
124 long now = slap_get_time();
126 if ( be->be_replogfile == NULL && replogfile == NULL ) {
130 ldap_pvt_thread_mutex_lock( &replog_mutex );
131 if ( (fp = lock_fopen( be->be_replogfile ? be->be_replogfile :
132 replogfile, "a", &lfp )) == NULL ) {
133 ldap_pvt_thread_mutex_unlock( &replog_mutex );
137 for ( i = 0; be->be_replica != NULL && be->be_replica[i] != NULL; i++ ) {
138 /* check if dn's suffix matches legal suffixes, if any */
139 if ( be->be_replica[i]->ri_nsuffix != NULL ) {
142 for ( j = 0; be->be_replica[i]->ri_nsuffix[j].bv_val; j++ ) {
143 if ( dnIsSuffix( ndn, &be->be_replica[i]->ri_nsuffix[j] ) ) {
148 if ( !be->be_replica[i]->ri_nsuffix[j].bv_val ) {
149 /* do not add "replica:" line */
153 /* See if we only want a subset of attributes */
154 if ( be->be_replica[i]->ri_attrs != NULL &&
155 ( op->o_tag == LDAP_REQ_MODIFY || op->o_tag == LDAP_REQ_ADD || op->o_tag == LDAP_REQ_EXTENDED ) ) {
159 /* Do attribute subsets by themselves in a second pass */
163 fprintf( fp, "replica: %s\n", be->be_replica[i]->ri_host );
164 #ifdef NO_LOG_WHEN_NO_REPLICAS
169 #ifdef NO_LOG_WHEN_NO_REPLICAS
170 if ( count == 0 && subsets == 0 ) {
171 /* if no replicas matched, drop the log
172 * (should we log it anyway?) */
173 lock_fclose( fp, lfp );
174 ldap_pvt_thread_mutex_unlock( &replog_mutex );
180 fprintf( fp, "time: %ld\n", now );
181 fprintf( fp, "dn: %s\n", dn->bv_val );
183 replog1( NULL, op, change, fp, NULL );
187 for ( i = subsets - 1; be->be_replica != NULL && be->be_replica[i] != NULL; i++ ) {
189 /* If no attrs, we already did this above */
190 if ( be->be_replica[i]->ri_attrs == NULL ) {
194 /* check if dn's suffix matches legal suffixes, if any */
195 if ( be->be_replica[i]->ri_nsuffix != NULL ) {
198 for ( j = 0; be->be_replica[i]->ri_nsuffix[j].bv_val; j++ ) {
199 if ( dnIsSuffix( ndn, &be->be_replica[i]->ri_nsuffix[j] ) ) {
204 if ( !be->be_replica[i]->ri_nsuffix[j].bv_val ) {
205 /* do not add "replica:" line */
211 switch( op->o_tag ) {
212 case LDAP_REQ_EXTENDED:
213 /* quick hack for extended operations */
214 /* assume change parameter is a Modfications* */
216 case LDAP_REQ_MODIFY:
217 for ( ml = change; ml != NULL; ml = ml->sml_next ) {
220 is_in = ad_inlist( ml->sml_desc, be->be_replica[i]->ri_attrs );
221 exclude = be->be_replica[i]->ri_exclude;
224 * there might be a more clever way to do this test,
225 * but this way, at least, is comprehensible :)
227 if ( ( is_in && !exclude ) || ( !is_in && exclude ) ) {
239 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
242 is_in = ad_inlist( a->a_desc, be->be_replica[i]->ri_attrs );
243 exclude = be->be_replica[i]->ri_exclude;
245 if ( ( is_in && !exclude ) || ( !is_in && exclude ) ) {
256 /* Other operations were logged in the first pass */
259 fprintf( fp, "replica: %s\n", be->be_replica[i]->ri_host );
260 fprintf( fp, "time: %ld\n", now );
261 fprintf( fp, "dn: %s\n", dn->bv_val );
262 replog1( be->be_replica[i], op, change, fp, first );
266 lock_fclose( fp, lfp );
267 ldap_pvt_thread_mutex_unlock( &replog_mutex );
273 struct slap_replica_info *ri,
283 struct slap_replog_moddn *moddn;
285 switch ( op->o_tag ) {
286 case LDAP_REQ_EXTENDED:
287 /* quick hack for extended operations */
288 /* assume change parameter is a Modfications* */
291 case LDAP_REQ_MODIFY:
292 fprintf( fp, "changetype: modify\n" );
293 ml = first ? first : change;
294 for ( ; ml != NULL; ml = ml->sml_next ) {
296 if ( ri && ri->ri_attrs ) {
297 int is_in = ad_inlist( ml->sml_desc, ri->ri_attrs );
299 if ( ( !is_in && !ri->ri_exclude ) || ( is_in && ri->ri_exclude ) ) {
303 type = ml->sml_desc->ad_cname.bv_val;
304 switch ( ml->sml_op ) {
306 fprintf( fp, "add: %s\n", type );
309 case LDAP_MOD_DELETE:
310 fprintf( fp, "delete: %s\n", type );
313 case LDAP_MOD_REPLACE:
314 fprintf( fp, "replace: %s\n", type );
317 if ( ml->sml_bvalues )
318 print_vals( fp, &ml->sml_desc->ad_cname, ml->sml_bvalues );
319 fprintf( fp, "-\n" );
325 fprintf( fp, "changetype: add\n" );
326 a = first ? first : e->e_attrs;
327 for ( ; a != NULL; a=a->a_next ) {
328 if ( ri && ri->ri_attrs ) {
329 int is_in = ad_inlist( a->a_desc, ri->ri_attrs );
330 if ( ( !is_in && !ri->ri_exclude ) || ( is_in && ri->ri_exclude ) ) {
333 /* If the list includes objectClass names,
334 * only include those classes in the
335 * objectClass attribute
337 if ( a->a_desc == slap_schema.si_ad_objectClass ) {
340 struct berval vals[2];
341 vals[1].bv_val = NULL;
343 for ( an = ri->ri_attrs; an->an_name.bv_val; an++ ) {
346 for ( i=0; a->a_vals[i].bv_val; i++ ) {
347 if ( a->a_vals[i].bv_len == an->an_name.bv_len
348 && !strcasecmp(a->a_vals[i].bv_val,
349 an->an_name.bv_val ) ) {
351 vals[0] = an->an_name;
352 print_vals( fp, &a->a_desc->ad_cname, vals );
361 print_vals( fp, &a->a_desc->ad_cname, a->a_vals );
365 case LDAP_REQ_DELETE:
366 fprintf( fp, "changetype: delete\n" );
369 case LDAP_REQ_MODRDN:
371 fprintf( fp, "changetype: modrdn\n" );
372 fprintf( fp, "newrdn: %s\n", moddn->newrdn->bv_val );
373 fprintf( fp, "deleteoldrdn: %d\n", moddn->deloldrdn ? 1 : 0 );
374 /* moddn->newsup is never NULL, see modrdn.c */
375 if( moddn->newsup->bv_val != NULL ) {
376 fprintf( fp, "newsuperior: %s\n", moddn->newsup->bv_val );
391 for ( i = 0, len = 0; bv && bv[i].bv_val; i++ ) {
392 if ( bv[i].bv_len > len )
396 len = LDIF_SIZE_NEEDED( type->bv_len, len ) + 1;
397 buf = (char *) ch_malloc( len );
399 for ( ; bv && bv->bv_val; bv++ ) {
401 ldif_sput( &bufp, LDIF_PUT_VALUE, type->bv_val,
402 bv->bv_val, bv->bv_len );