1 /* repl.c - log modifications for replication purposes */
4 * Copyright 1998-2002 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 + 1 ] = NULL;
56 struct berval dn, *ndn = NULL;
59 dn.bv_val = (char *) suffix;
60 dn.bv_len = strlen( dn.bv_val );
62 rc = dnNormalize( NULL, &dn, &ndn );
63 if( rc != LDAP_SUCCESS ) {
67 if ( select_backend( ndn, 0, 0 ) != be ) {
72 ber_bvecadd( &be->be_replica[nr]->ri_nsuffix, ndn );
83 be->be_replica[nr]->ri_attrs = str2anlist( be->be_replica[nr]->ri_attrs,
85 return ( be->be_replica[nr]->ri_attrs == NULL );
89 print_vals( FILE *fp, struct berval *type, struct berval *bv );
91 replog1( struct slap_replica_info *ri, Operation *op, void *change, FILE *fp, void *first);
102 Modifications *ml = NULL;
107 /* undef NO_LOG_WHEN_NO_REPLICAS */
108 #ifdef NO_LOG_WHEN_NO_REPLICAS
112 long now = slap_get_time();
114 if ( be->be_replogfile == NULL && replogfile == NULL ) {
118 ldap_pvt_thread_mutex_lock( &replog_mutex );
119 if ( (fp = lock_fopen( be->be_replogfile ? be->be_replogfile :
120 replogfile, "a", &lfp )) == NULL ) {
121 ldap_pvt_thread_mutex_unlock( &replog_mutex );
125 for ( i = 0; be->be_replica != NULL && be->be_replica[i] != NULL; i++ ) {
126 /* check if dn's suffix matches legal suffixes, if any */
127 if ( be->be_replica[i]->ri_nsuffix != NULL ) {
130 for ( j = 0; be->be_replica[i]->ri_nsuffix[j]; j++ ) {
131 if ( dnIsSuffix( ndn, be->be_replica[i]->ri_nsuffix[j] ) ) {
136 if ( !be->be_replica[i]->ri_nsuffix[j] ) {
137 /* do not add "replica:" line */
141 /* See if we only want a subset of attributes */
142 if ( be->be_replica[i]->ri_attrs != NULL &&
143 ( op->o_tag == LDAP_REQ_MODIFY || op->o_tag == LDAP_REQ_ADD || op->o_tag == LDAP_REQ_EXTENDED ) ) {
147 /* Do attribute subsets by themselves in a second pass */
151 fprintf( fp, "replica: %s\n", be->be_replica[i]->ri_host );
152 #ifdef NO_LOG_WHEN_NO_REPLICAS
157 #ifdef NO_LOG_WHEN_NO_REPLICAS
158 if ( count == 0 && subsets == 0 ) {
159 /* if no replicas matched, drop the log
160 * (should we log it anyway?) */
161 lock_fclose( fp, lfp );
162 ldap_pvt_thread_mutex_unlock( &replog_mutex );
168 fprintf( fp, "time: %ld\n", now );
169 fprintf( fp, "dn: %s\n", dn->bv_val );
171 replog1( NULL, op, change, fp, NULL );
175 for ( i = subsets - 1; be->be_replica != NULL && be->be_replica[i] != NULL; i++ ) {
177 /* If no attrs, we already did this above */
178 if ( be->be_replica[i]->ri_attrs == NULL ) {
182 /* check if dn's suffix matches legal suffixes, if any */
183 if ( be->be_replica[i]->ri_nsuffix != NULL ) {
186 for ( j = 0; be->be_replica[i]->ri_nsuffix[j]; j++ ) {
187 if ( dnIsSuffix( ndn, be->be_replica[i]->ri_nsuffix[j] ) ) {
192 if ( !be->be_replica[i]->ri_nsuffix[j] ) {
193 /* do not add "replica:" line */
199 switch( op->o_tag ) {
200 case LDAP_REQ_EXTENDED:
201 /* quick hack for extended operations */
202 /* assume change parameter is a Modfications* */
204 case LDAP_REQ_MODIFY:
205 for ( ml = change; ml != NULL; ml = ml->sml_next ) {
206 if ( ad_inlist( ml->sml_desc, be->be_replica[i]->ri_attrs ) ) {
218 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
219 if ( ad_inlist( a->a_desc, be->be_replica[i]->ri_attrs ) ) {
230 /* Other operations were logged in the first pass */
233 fprintf( fp, "replica: %s\n", be->be_replica[i]->ri_host );
234 fprintf( fp, "time: %ld\n", now );
235 fprintf( fp, "dn: %s\n", dn->bv_val );
236 replog1( be->be_replica[i], op, change, fp, first );
240 lock_fclose( fp, lfp );
241 ldap_pvt_thread_mutex_unlock( &replog_mutex );
247 struct slap_replica_info *ri,
257 struct slap_replog_moddn *moddn;
259 switch ( op->o_tag ) {
260 case LDAP_REQ_EXTENDED:
261 /* quick hack for extended operations */
262 /* assume change parameter is a Modfications* */
265 case LDAP_REQ_MODIFY:
266 fprintf( fp, "changetype: modify\n" );
267 ml = first ? first : change;
268 for ( ; ml != NULL; ml = ml->sml_next ) {
270 if ( ri && ri->ri_attrs && !ad_inlist( ml->sml_desc, ri->ri_attrs ) ) {
273 type = ml->sml_desc->ad_cname.bv_val;
274 switch ( ml->sml_op ) {
276 fprintf( fp, "add: %s\n", type );
279 case LDAP_MOD_DELETE:
280 fprintf( fp, "delete: %s\n", type );
283 case LDAP_MOD_REPLACE:
284 fprintf( fp, "replace: %s\n", type );
287 if ( ml->sml_bvalues )
288 print_vals( fp, &ml->sml_desc->ad_cname, ml->sml_bvalues );
289 fprintf( fp, "-\n" );
295 fprintf( fp, "changetype: add\n" );
296 a = first ? first : e->e_attrs;
297 for ( ; a != NULL; a=a->a_next ) {
298 if ( ri && ri->ri_attrs && !ad_inlist( a->a_desc, ri->ri_attrs ) ) {
301 print_vals( fp, &a->a_desc->ad_cname, a->a_vals );
305 case LDAP_REQ_DELETE:
306 fprintf( fp, "changetype: delete\n" );
309 case LDAP_REQ_MODRDN:
311 fprintf( fp, "changetype: modrdn\n" );
312 fprintf( fp, "newrdn: %s\n", moddn->newrdn->bv_val );
313 fprintf( fp, "deleteoldrdn: %d\n", moddn->deloldrdn ? 1 : 0 );
314 if( moddn->newsup != NULL ) {
315 fprintf( fp, "newsuperior: %s\n", moddn->newsup->bv_val );
330 for ( i = 0, len = 0; bv && bv[i].bv_val; i++ ) {
331 if ( bv[i].bv_len > len )
335 len = LDIF_SIZE_NEEDED( type->bv_len, len ) + 1;
336 buf = (char *) ch_malloc( len );
338 for ( ; bv && bv->bv_val; bv++ ) {
340 ldif_sput( &bufp, LDIF_PUT_VALUE, type->bv_val,
341 bv->bv_val, bv->bv_len );