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
33 if ( be->be_replica != NULL ) {
34 for ( ; be->be_replica[ i ] != NULL; i++ );
37 be->be_replica = ch_realloc( be->be_replica,
38 sizeof( struct slap_replica_info * )*( i + 2 ) );
41 = ch_calloc( sizeof( struct slap_replica_info ), 1 );
42 be->be_replica[ i ]->ri_host = ch_strdup( host );
43 be->be_replica[ i + 1 ] = NULL;
58 struct replog_moddn *moddn;
62 /* undef NO_LOG_WHEN_NO_REPLICAS */
63 #ifdef NO_LOG_WHEN_NO_REPLICAS
67 if ( be->be_replogfile == NULL && replogfile == NULL ) {
71 ldap_pvt_thread_mutex_lock( &replog_mutex );
72 if ( (fp = lock_fopen( be->be_replogfile ? be->be_replogfile :
73 replogfile, "a", &lfp )) == NULL ) {
74 ldap_pvt_thread_mutex_unlock( &replog_mutex );
78 tmp = ch_strdup( dn );
79 if ( dn_normalize( tmp ) == NULL ) {
80 /* something has gone really bad */
83 lock_fclose( fp, lfp );
84 ldap_pvt_thread_mutex_unlock( &replog_mutex );
88 for ( i = 0; be->be_replica != NULL && be->be_replica[i] != NULL;
90 /* check if dn's suffix matches legal suffixes, if any */
91 if ( be->be_replica[i]->ri_nsuffix != NULL ) {
94 for ( j = 0; be->be_replica[i]->ri_nsuffix[j]; j++ ) {
95 if ( dn_issuffix( tmp, be->be_replica[i]->ri_nsuffix[j] ) ) {
100 if ( !be->be_replica[i]->ri_nsuffix[j] ) {
101 /* do not add "replica:" line */
106 fprintf( fp, "replica: %s\n", be->be_replica[i]->ri_host );
107 #ifdef NO_LOG_WHEN_NO_REPLICAS
113 #ifdef NO_LOG_WHEN_NO_REPLICAS
115 /* if no replicas matched, drop the log
116 * (should we log it anyway?) */
117 lock_fclose( fp, lfp );
118 ldap_pvt_thread_mutex_unlock( &replog_mutex );
124 fprintf( fp, "time: %ld\n", (long) slap_get_time() );
125 fprintf( fp, "dn: %s\n", dn );
127 switch ( op->o_tag ) {
128 case LDAP_REQ_EXTENDED:
129 /* quick hack for extended operations */
130 /* assume change parameter is a Modfications* */
133 case LDAP_REQ_MODIFY:
134 fprintf( fp, "changetype: modify\n" );
136 for ( ; ml != NULL; ml = ml->sml_next ) {
138 type = ml->sml_desc->ad_cname->bv_val;
139 switch ( ml->sml_op ) {
141 fprintf( fp, "add: %s\n", type );
144 case LDAP_MOD_DELETE:
145 fprintf( fp, "delete: %s\n", type );
148 case LDAP_MOD_REPLACE:
149 fprintf( fp, "replace: %s\n", type );
153 for ( i = 0; ml->sml_bvalues != NULL &&
154 ml->sml_bvalues[i] != NULL; i++ ) {
157 len = strlen( type );
158 len = LDIF_SIZE_NEEDED( len,
159 ml->sml_bvalues[i]->bv_len ) + 1;
160 buf = (char *) ch_malloc( len );
163 ldif_sput( &bufp, LDIF_PUT_VALUE,
165 ml->sml_bvalues[i]->bv_val,
166 ml->sml_bvalues[i]->bv_len );
173 fprintf( fp, "-\n" );
179 fprintf( fp, "changetype: add\n" );
180 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
181 tmp = entry2str( e, &len );
182 while ( (tmp = strchr( tmp, '\n' )) != NULL ) {
184 if ( ! isspace( (unsigned char) *tmp ) )
187 fprintf( fp, "%s", tmp );
188 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
191 case LDAP_REQ_DELETE:
192 fprintf( fp, "changetype: delete\n" );
195 case LDAP_REQ_MODRDN:
197 fprintf( fp, "changetype: modrdn\n" );
198 fprintf( fp, "newrdn: %s\n", moddn->newrdn );
199 fprintf( fp, "deleteoldrdn: %d\n", moddn->deloldrdn ? 1 : 0 );
200 if( moddn->newsup != NULL ) {
201 fprintf( fp, "newsuperior: %s\n", moddn->newsup );
206 lock_fclose( fp, lfp );
207 ldap_pvt_thread_mutex_unlock( &replog_mutex );