]> git.sur5r.net Git - openldap/blob - servers/slapd/repl.c
logs changes even if no appropriate replica (or none at all) is defined (ITS#1335)
[openldap] / servers / slapd / repl.c
1 /* repl.c - log modifications for replication purposes */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/string.h>
13 #include <ac/ctype.h>
14 #include <ac/socket.h>
15
16 #ifdef HAVE_SYS_FILE_H
17 #include <sys/file.h>
18 #endif
19
20 #include "slap.h"
21
22 int
23 add_replica_info(
24     Backend     *be,
25     const char  *host 
26 )
27 {
28         int i = 0;
29
30         assert( be );
31         assert( host );
32
33         if ( be->be_replica != NULL ) {
34                 for ( ; be->be_replica[ i ] != NULL; i++ );
35         }
36                 
37         be->be_replica = ch_realloc( be->be_replica, 
38                 sizeof( struct slap_replica_info * )*( i + 2 ) );
39
40         be->be_replica[ i ] 
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;
44
45         return( i );
46 }
47
48 void
49 replog(
50     Backend     *be,
51     Operation *op,
52     char        *dn,
53     void        *change
54 )
55 {
56         Modifications   *ml;
57         Entry   *e;
58         struct replog_moddn *moddn;
59         char *tmp;
60         FILE    *fp, *lfp;
61         int     len, i;
62 /* undef NO_LOG_WHEN_NO_REPLICAS */
63 #ifdef NO_LOG_WHEN_NO_REPLICAS
64         int     count = 0;
65 #endif
66
67         if ( be->be_replogfile == NULL && replogfile == NULL ) {
68                 return;
69         }
70
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 );
75                 return;
76         }
77
78         tmp = ch_strdup( dn );
79         if ( dn_normalize( tmp ) == NULL ) {
80                 /* something has gone really bad */
81                 ch_free( tmp );
82
83                 lock_fclose( fp, lfp );
84                 ldap_pvt_thread_mutex_unlock( &replog_mutex );
85                 return;
86         }
87
88         for ( i = 0; be->be_replica != NULL && be->be_replica[i] != NULL;
89             i++ ) {
90                 /* check if dn's suffix matches legal suffixes, if any */
91                 if ( be->be_replica[i]->ri_nsuffix != NULL ) {
92                         int j;
93
94                         for ( j = 0; be->be_replica[i]->ri_nsuffix[j]; j++ ) {
95                                 if ( dn_issuffix( tmp, be->be_replica[i]->ri_nsuffix[j] ) ) {
96                                         break;
97                                 }
98                         }
99
100                         if ( !be->be_replica[i]->ri_nsuffix[j] ) {
101                                 /* do not add "replica:" line */
102                                 continue;
103                         }
104                 }
105
106                 fprintf( fp, "replica: %s\n", be->be_replica[i]->ri_host );
107 #ifdef NO_LOG_WHEN_NO_REPLICAS
108                 ++count;
109 #endif
110         }
111
112         ch_free( tmp );
113 #ifdef NO_LOG_WHEN_NO_REPLICAS
114         if ( count == 0 ) {
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 );
119
120                 return;
121         }
122 #endif
123
124         fprintf( fp, "time: %ld\n", (long) slap_get_time() );
125         fprintf( fp, "dn: %s\n", dn );
126
127         switch ( op->o_tag ) {
128         case LDAP_REQ_EXTENDED:
129                 /* quick hack for extended operations */
130                 /* assume change parameter is a Modfications* */
131                 /* fall thru */
132
133         case LDAP_REQ_MODIFY:
134                 fprintf( fp, "changetype: modify\n" );
135                 ml = change;
136                 for ( ; ml != NULL; ml = ml->sml_next ) {
137                         char *type;
138                         type = ml->sml_desc->ad_cname->bv_val;
139                         switch ( ml->sml_op ) {
140                         case LDAP_MOD_ADD:
141                                 fprintf( fp, "add: %s\n", type );
142                                 break;
143
144                         case LDAP_MOD_DELETE:
145                                 fprintf( fp, "delete: %s\n", type );
146                                 break;
147
148                         case LDAP_MOD_REPLACE:
149                                 fprintf( fp, "replace: %s\n", type );
150                                 break;
151                         }
152
153                         for ( i = 0; ml->sml_bvalues != NULL &&
154                             ml->sml_bvalues[i] != NULL; i++ ) {
155                                 char    *buf, *bufp;
156
157                                 len = strlen( type );
158                                 len = LDIF_SIZE_NEEDED( len,
159                                     ml->sml_bvalues[i]->bv_len ) + 1;
160                                 buf = (char *) ch_malloc( len );
161
162                                 bufp = buf;
163                                 ldif_sput( &bufp, LDIF_PUT_VALUE,
164                                         type,
165                                     ml->sml_bvalues[i]->bv_val,
166                                     ml->sml_bvalues[i]->bv_len );
167                                 *bufp = '\0';
168
169                                 fputs( buf, fp );
170
171                                 free( buf );
172                         }
173                         fprintf( fp, "-\n" );
174                 }
175                 break;
176
177         case LDAP_REQ_ADD:
178                 e = change;
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 ) {
183                         tmp++;
184                         if ( ! isspace( (unsigned char) *tmp ) )
185                                 break;
186                 }
187                 fprintf( fp, "%s", tmp );
188                 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
189                 break;
190
191         case LDAP_REQ_DELETE:
192                 fprintf( fp, "changetype: delete\n" );
193                 break;
194
195         case LDAP_REQ_MODRDN:
196                 moddn = change;
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 );
202                 }
203         }
204         fprintf( fp, "\n" );
205
206         lock_fclose( fp, lfp );
207         ldap_pvt_thread_mutex_unlock( &replog_mutex );
208 }