1 /* repl.c - log modifications for replication purposes */
4 * Copyright 1998-2000 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 char *nsuffix = ch_strdup( suffix );
59 if ( dn_normalize( nsuffix ) != NULL ) {
60 if ( select_backend( nsuffix, 0 ) == be ) {
61 charray_add( &be->be_replica[nr]->ri_nsuffix, nsuffix );
83 struct replog_moddn *moddn;
87 /* undef NO_LOG_WHEN_NO_REPLICAS */
88 #ifdef NO_LOG_WHEN_NO_REPLICAS
92 if ( be->be_replogfile == NULL && replogfile == NULL ) {
96 ldap_pvt_thread_mutex_lock( &replog_mutex );
97 if ( (fp = lock_fopen( be->be_replogfile ? be->be_replogfile :
98 replogfile, "a", &lfp )) == NULL ) {
99 ldap_pvt_thread_mutex_unlock( &replog_mutex );
103 tmp = ch_strdup( dn );
104 if ( dn_normalize( tmp ) == NULL ) {
105 /* something has gone really bad */
108 lock_fclose( fp, lfp );
109 ldap_pvt_thread_mutex_unlock( &replog_mutex );
113 for ( i = 0; be->be_replica != NULL && be->be_replica[i] != NULL;
115 /* check if dn's suffix matches legal suffixes, if any */
116 if ( be->be_replica[i]->ri_nsuffix != NULL ) {
119 for ( j = 0; be->be_replica[i]->ri_nsuffix[j]; j++ ) {
120 if ( dn_issuffix( tmp, be->be_replica[i]->ri_nsuffix[j] ) ) {
125 if ( !be->be_replica[i]->ri_nsuffix[j] ) {
126 /* do not add "replica:" line */
131 fprintf( fp, "replica: %s\n", be->be_replica[i]->ri_host );
132 #ifdef NO_LOG_WHEN_NO_REPLICAS
138 #ifdef NO_LOG_WHEN_NO_REPLICAS
140 /* if no replicas matched, drop the log
141 * (should we log it anyway?) */
142 lock_fclose( fp, lfp );
143 ldap_pvt_thread_mutex_unlock( &replog_mutex );
149 fprintf( fp, "time: %ld\n", (long) slap_get_time() );
150 fprintf( fp, "dn: %s\n", dn );
152 switch ( op->o_tag ) {
153 case LDAP_REQ_EXTENDED:
154 /* quick hack for extended operations */
155 /* assume change parameter is a Modfications* */
158 case LDAP_REQ_MODIFY:
159 fprintf( fp, "changetype: modify\n" );
161 for ( ; ml != NULL; ml = ml->sml_next ) {
163 type = ml->sml_desc->ad_cname.bv_val;
164 switch ( ml->sml_op ) {
166 fprintf( fp, "add: %s\n", type );
169 case LDAP_MOD_DELETE:
170 fprintf( fp, "delete: %s\n", type );
173 case LDAP_MOD_REPLACE:
174 fprintf( fp, "replace: %s\n", type );
178 for ( i = 0; ml->sml_bvalues != NULL &&
179 ml->sml_bvalues[i] != NULL; i++ ) {
182 len = strlen( type );
183 len = LDIF_SIZE_NEEDED( len,
184 ml->sml_bvalues[i]->bv_len ) + 1;
185 buf = (char *) ch_malloc( len );
188 ldif_sput( &bufp, LDIF_PUT_VALUE,
190 ml->sml_bvalues[i]->bv_val,
191 ml->sml_bvalues[i]->bv_len );
198 fprintf( fp, "-\n" );
204 fprintf( fp, "changetype: add\n" );
205 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
206 tmp = entry2str( e, &len );
207 while ( (tmp = strchr( tmp, '\n' )) != NULL ) {
209 if ( ! isspace( (unsigned char) *tmp ) )
212 fprintf( fp, "%s", tmp );
213 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
216 case LDAP_REQ_DELETE:
217 fprintf( fp, "changetype: delete\n" );
220 case LDAP_REQ_MODRDN:
222 fprintf( fp, "changetype: modrdn\n" );
223 fprintf( fp, "newrdn: %s\n", moddn->newrdn );
224 fprintf( fp, "deleteoldrdn: %d\n", moddn->deloldrdn ? 1 : 0 );
225 if( moddn->newsup != NULL ) {
226 fprintf( fp, "newsuperior: %s\n", moddn->newsup );
231 lock_fclose( fp, lfp );
232 ldap_pvt_thread_mutex_unlock( &replog_mutex );