]> git.sur5r.net Git - openldap/blob - servers/slapd/repl.c
Moved AttributeDescription caching into main code:
[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 #include "ldif.h"
22
23 int
24 add_replica_info(
25     Backend     *be,
26     const char  *host 
27 )
28 {
29         int i = 0;
30
31         assert( be );
32         assert( host );
33
34         if ( be->be_replica != NULL ) {
35                 for ( ; be->be_replica[ i ] != NULL; i++ );
36         }
37                 
38         be->be_replica = ch_realloc( be->be_replica, 
39                 sizeof( struct slap_replica_info * )*( i + 2 ) );
40
41         be->be_replica[ i ] 
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;
45
46         return( i );
47 }
48
49 void
50 replog(
51     Backend     *be,
52     Operation *op,
53     char        *dn,
54     void        *change
55 )
56 {
57         Modifications   *ml;
58         Entry   *e;
59         struct replog_moddn *moddn;
60         char *tmp;
61         FILE    *fp, *lfp;
62         int     len, i;
63 /* undef NO_LOG_WHEN_NO_REPLICAS */
64 #ifdef NO_LOG_WHEN_NO_REPLICAS
65         int     count = 0;
66 #endif
67
68         if ( be->be_replogfile == NULL && replogfile == NULL ) {
69                 return;
70         }
71
72         ldap_pvt_thread_mutex_lock( &replog_mutex );
73         if ( (fp = lock_fopen( be->be_replogfile ? be->be_replogfile :
74             replogfile, "a", &lfp )) == NULL ) {
75                 ldap_pvt_thread_mutex_unlock( &replog_mutex );
76                 return;
77         }
78
79         tmp = ch_strdup( dn );
80         if ( dn_normalize( tmp ) == NULL ) {
81                 /* something has gone really bad */
82                 ch_free( tmp );
83
84                 lock_fclose( fp, lfp );
85                 ldap_pvt_thread_mutex_unlock( &replog_mutex );
86                 return;
87         }
88
89         for ( i = 0; be->be_replica != NULL && be->be_replica[i] != NULL;
90             i++ ) {
91                 /* check if dn's suffix matches legal suffixes, if any */
92                 if ( be->be_replica[i]->ri_nsuffix != NULL ) {
93                         int j;
94
95                         for ( j = 0; be->be_replica[i]->ri_nsuffix[j]; j++ ) {
96                                 if ( dn_issuffix( tmp, be->be_replica[i]->ri_nsuffix[j] ) ) {
97                                         break;
98                                 }
99                         }
100
101                         if ( !be->be_replica[i]->ri_nsuffix[j] ) {
102                                 /* do not add "replica:" line */
103                                 continue;
104                         }
105                 }
106
107                 fprintf( fp, "replica: %s\n", be->be_replica[i]->ri_host );
108 #ifdef NO_LOG_WHEN_NO_REPLICAS
109                 ++count;
110 #endif
111         }
112
113         ch_free( tmp );
114 #ifdef NO_LOG_WHEN_NO_REPLICAS
115         if ( count == 0 ) {
116                 /* if no replicas matched, drop the log 
117                  * (should we log it anyway?) */
118                 lock_fclose( fp, lfp );
119                 ldap_pvt_thread_mutex_unlock( &replog_mutex );
120
121                 return;
122         }
123 #endif
124
125         fprintf( fp, "time: %ld\n", (long) slap_get_time() );
126         fprintf( fp, "dn: %s\n", dn );
127
128         switch ( op->o_tag ) {
129         case LDAP_REQ_EXTENDED:
130                 /* quick hack for extended operations */
131                 /* assume change parameter is a Modfications* */
132                 /* fall thru */
133
134         case LDAP_REQ_MODIFY:
135                 fprintf( fp, "changetype: modify\n" );
136                 ml = change;
137                 for ( ; ml != NULL; ml = ml->sml_next ) {
138                         char *type;
139                         type = ml->sml_desc->ad_cname.bv_val;
140                         switch ( ml->sml_op ) {
141                         case LDAP_MOD_ADD:
142                                 fprintf( fp, "add: %s\n", type );
143                                 break;
144
145                         case LDAP_MOD_DELETE:
146                                 fprintf( fp, "delete: %s\n", type );
147                                 break;
148
149                         case LDAP_MOD_REPLACE:
150                                 fprintf( fp, "replace: %s\n", type );
151                                 break;
152                         }
153
154                         for ( i = 0; ml->sml_bvalues != NULL &&
155                             ml->sml_bvalues[i] != NULL; i++ ) {
156                                 char    *buf, *bufp;
157
158                                 len = strlen( type );
159                                 len = LDIF_SIZE_NEEDED( len,
160                                     ml->sml_bvalues[i]->bv_len ) + 1;
161                                 buf = (char *) ch_malloc( len );
162
163                                 bufp = buf;
164                                 ldif_sput( &bufp, LDIF_PUT_VALUE,
165                                         type,
166                                     ml->sml_bvalues[i]->bv_val,
167                                     ml->sml_bvalues[i]->bv_len );
168                                 *bufp = '\0';
169
170                                 fputs( buf, fp );
171
172                                 free( buf );
173                         }
174                         fprintf( fp, "-\n" );
175                 }
176                 break;
177
178         case LDAP_REQ_ADD:
179                 e = change;
180                 fprintf( fp, "changetype: add\n" );
181                 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
182                 tmp = entry2str( e, &len );
183                 while ( (tmp = strchr( tmp, '\n' )) != NULL ) {
184                         tmp++;
185                         if ( ! isspace( (unsigned char) *tmp ) )
186                                 break;
187                 }
188                 fprintf( fp, "%s", tmp );
189                 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
190                 break;
191
192         case LDAP_REQ_DELETE:
193                 fprintf( fp, "changetype: delete\n" );
194                 break;
195
196         case LDAP_REQ_MODRDN:
197                 moddn = change;
198                 fprintf( fp, "changetype: modrdn\n" );
199                 fprintf( fp, "newrdn: %s\n", moddn->newrdn );
200                 fprintf( fp, "deleteoldrdn: %d\n", moddn->deloldrdn ? 1 : 0 );
201                 if( moddn->newsup != NULL ) {
202                         fprintf( fp, "newsuperior: %s\n", moddn->newsup );
203                 }
204         }
205         fprintf( fp, "\n" );
206
207         lock_fclose( fp, lfp );
208         ldap_pvt_thread_mutex_unlock( &replog_mutex );
209 }