]> git.sur5r.net Git - openldap/blob - servers/slapd/repl.c
Add initial support for modify/increment. No over/under flow detection.
[openldap] / servers / slapd / repl.c
1 /* repl.c - log modifications for replication purposes */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 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 ]->ri_nsuffix = NULL;
45         be->be_replica[ i ]->ri_attrs = NULL;
46         be->be_replica[ i + 1 ] = NULL;
47
48         return( i );
49 }
50
51 int
52 add_replica_suffix(
53     Backend     *be,
54     int         nr,
55     const char  *suffix
56 )
57 {
58         struct berval dn, ndn;
59         int rc;
60
61         dn.bv_val = (char *) suffix;
62         dn.bv_len = strlen( dn.bv_val );
63
64         rc = dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL );
65         if( rc != LDAP_SUCCESS ) {
66                 return 2;
67         }
68
69         if ( select_backend( &ndn, 0, 0 ) != be ) {
70                 free( ndn.bv_val );
71                 return 1;
72         }
73
74         ber_bvarray_add( &be->be_replica[nr]->ri_nsuffix, &ndn );
75         return 0;
76 }
77
78 int
79 add_replica_attrs(
80         Backend *be,
81         int     nr,
82         char    *attrs,
83         int     exclude
84 )
85 {
86         if ( be->be_replica[nr]->ri_attrs != NULL ) {
87                 if ( be->be_replica[nr]->ri_exclude != exclude ) {
88                         fprintf( stderr, "attr selective replication directive '%s' conflicts with previous one (discarded)\n", attrs );
89                         ch_free( be->be_replica[nr]->ri_attrs );
90                         be->be_replica[nr]->ri_attrs = NULL;
91                 }
92         }
93
94         be->be_replica[nr]->ri_exclude = exclude;
95         be->be_replica[nr]->ri_attrs = str2anlist( be->be_replica[nr]->ri_attrs,
96                 attrs, "," );
97         return ( be->be_replica[nr]->ri_attrs == NULL );
98 }
99    
100 static void
101 print_vals( FILE *fp, struct berval *type, struct berval *bv );
102 static void
103 replog1( struct slap_replica_info *ri, Operation *op, FILE *fp, void *first);
104
105 void
106 replog( Operation *op )
107 {
108         Modifications   *ml = NULL;
109         Attribute       *a = NULL;
110         FILE    *fp, *lfp;
111         int     i;
112 /* undef NO_LOG_WHEN_NO_REPLICAS */
113 #ifdef NO_LOG_WHEN_NO_REPLICAS
114         int     count = 0;
115 #endif
116         int     subsets = 0;
117         long now = slap_get_time();
118
119         if ( op->o_bd->be_replogfile == NULL && replogfile == NULL ) {
120                 return;
121         }
122
123         ldap_pvt_thread_mutex_lock( &replog_mutex );
124         if ( (fp = lock_fopen( op->o_bd->be_replogfile ? op->o_bd->be_replogfile :
125             replogfile, "a", &lfp )) == NULL ) {
126                 ldap_pvt_thread_mutex_unlock( &replog_mutex );
127                 return;
128         }
129
130         for ( i = 0; op->o_bd->be_replica != NULL && op->o_bd->be_replica[i] != NULL; i++ ) {
131                 /* check if dn's suffix matches legal suffixes, if any */
132                 if ( op->o_bd->be_replica[i]->ri_nsuffix != NULL ) {
133                         int j;
134
135                         for ( j = 0; op->o_bd->be_replica[i]->ri_nsuffix[j].bv_val; j++ ) {
136                                 if ( dnIsSuffix( &op->o_req_ndn, &op->o_bd->be_replica[i]->ri_nsuffix[j] ) ) {
137                                         break;
138                                 }
139                         }
140
141                         if ( !op->o_bd->be_replica[i]->ri_nsuffix[j].bv_val ) {
142                                 /* do not add "replica:" line */
143                                 continue;
144                         }
145                 }
146                 /* See if we only want a subset of attributes */
147                 if ( op->o_bd->be_replica[i]->ri_attrs != NULL &&
148                         ( op->o_tag == LDAP_REQ_MODIFY || op->o_tag == LDAP_REQ_ADD || op->o_tag == LDAP_REQ_EXTENDED ) ) {
149                         if ( !subsets ) {
150                                 subsets = i + 1;
151                         }
152                         /* Do attribute subsets by themselves in a second pass */
153                         continue;
154                 }
155
156                 fprintf( fp, "replica: %s\n", op->o_bd->be_replica[i]->ri_host );
157 #ifdef NO_LOG_WHEN_NO_REPLICAS
158                 ++count;
159 #endif
160         }
161
162 #ifdef NO_LOG_WHEN_NO_REPLICAS
163         if ( count == 0 && subsets == 0 ) {
164                 /* if no replicas matched, drop the log 
165                  * (should we log it anyway?) */
166                 lock_fclose( fp, lfp );
167                 ldap_pvt_thread_mutex_unlock( &replog_mutex );
168
169                 return;
170         }
171 #endif
172
173         fprintf( fp, "time: %ld\n", now );
174         fprintf( fp, "dn: %s\n", op->o_req_dn.bv_val );
175
176         replog1( NULL, op, fp, NULL );
177
178         if ( subsets > 0 ) {
179                 void *first;
180                 for ( i = subsets - 1; op->o_bd->be_replica != NULL && op->o_bd->be_replica[i] != NULL; i++ ) {
181
182                         /* If no attrs, we already did this above */
183                         if ( op->o_bd->be_replica[i]->ri_attrs == NULL ) {
184                                 continue;
185                         }
186
187                         /* check if dn's suffix matches legal suffixes, if any */
188                         if ( op->o_bd->be_replica[i]->ri_nsuffix != NULL ) {
189                                 int j;
190
191                                 for ( j = 0; op->o_bd->be_replica[i]->ri_nsuffix[j].bv_val; j++ ) {
192                                         if ( dnIsSuffix( &op->o_req_ndn, &op->o_bd->be_replica[i]->ri_nsuffix[j] ) ) {
193                                                 break;
194                                         }
195                                 }
196
197                                 if ( !op->o_bd->be_replica[i]->ri_nsuffix[j].bv_val ) {
198                                         /* do not add "replica:" line */
199                                         continue;
200                                 }
201                         }
202                         subsets = 0;
203                         first = NULL;
204                         switch( op->o_tag ) {
205                         case LDAP_REQ_EXTENDED:
206                                 /* quick hack for extended operations */
207                                 /* assume change parameter is a Modfications* */
208                                 /* fall thru */
209                         case LDAP_REQ_MODIFY:
210                                 for ( ml = op->orm_modlist; ml != NULL; ml = ml->sml_next ) {
211                                         int is_in, exclude;
212
213                                         is_in = ad_inlist( ml->sml_desc, op->o_bd->be_replica[i]->ri_attrs );
214                                         exclude = op->o_bd->be_replica[i]->ri_exclude;
215                                         
216                                         /*
217                                          * there might be a more clever way to do this test,
218                                          * but this way, at least, is comprehensible :)
219                                          */
220                                         if ( ( is_in && !exclude ) || ( !is_in && exclude ) ) {
221                                                 subsets = 1;
222                                                 first = ml;
223                                                 break;
224                                         }
225                                 }
226                                 if ( !subsets ) {
227                                         continue;
228                                 }
229                                 break;
230                         case LDAP_REQ_ADD:
231                                 for ( a = op->ora_e->e_attrs; a != NULL; a = a->a_next ) {
232                                         int is_in, exclude;
233
234                                         is_in = ad_inlist( a->a_desc, op->o_bd->be_replica[i]->ri_attrs );
235                                         exclude = op->o_bd->be_replica[i]->ri_exclude;
236                                         
237                                         if ( ( is_in && !exclude ) || ( !is_in && exclude ) ) {
238                                                 subsets = 1;
239                                                 first = a;
240                                                 break;
241                                         }
242                                 }
243                                 if ( !subsets ) {
244                                         continue;
245                                 }
246                                 break;
247                         default:
248                                 /* Other operations were logged in the first pass */
249                                 continue;
250                         }
251                         fprintf( fp, "replica: %s\n", op->o_bd->be_replica[i]->ri_host );
252                         fprintf( fp, "time: %ld\n", now );
253                         fprintf( fp, "dn: %s\n", op->o_req_dn.bv_val );
254                         replog1( op->o_bd->be_replica[i], op, fp, first );
255                 }
256         }
257
258         lock_fclose( fp, lfp );
259         ldap_pvt_thread_mutex_unlock( &replog_mutex );
260 }
261
262
263 static void
264 replog1(
265     struct slap_replica_info *ri,
266     Operation *op,
267     FILE        *fp,
268         void    *first
269 )
270 {
271         Modifications   *ml;
272         Attribute       *a;
273
274         switch ( op->o_tag ) {
275         case LDAP_REQ_EXTENDED:
276                 /* quick hack for extended operations */
277                 /* assume change parameter is a Modfications* */
278                 /* fall thru */
279
280         case LDAP_REQ_MODIFY:
281                 fprintf( fp, "changetype: modify\n" );
282                 ml = first ? first : op->orm_modlist;
283                 for ( ; ml != NULL; ml = ml->sml_next ) {
284                         char *type;
285                         if ( ri && ri->ri_attrs ) {
286                                 int is_in = ad_inlist( ml->sml_desc, ri->ri_attrs );
287
288                                 if ( ( !is_in && !ri->ri_exclude )
289                                         || ( is_in && ri->ri_exclude ) )
290                                 {
291                                         continue;
292                                 }
293                         }
294                         type = ml->sml_desc->ad_cname.bv_val;
295                         switch ( ml->sml_op ) {
296                         case LDAP_MOD_ADD:
297                                 fprintf( fp, "add: %s\n", type );
298                                 break;
299
300                         case LDAP_MOD_DELETE:
301                                 fprintf( fp, "delete: %s\n", type );
302                                 break;
303
304                         case LDAP_MOD_REPLACE:
305                                 fprintf( fp, "replace: %s\n", type );
306                                 break;
307
308                         case LDAP_MOD_INCREMENT:
309                                 fprintf( fp, "increment: %s\n", type );
310                                 break;
311                         }
312                         if ( ml->sml_bvalues ) {
313                                 print_vals( fp, &ml->sml_desc->ad_cname, ml->sml_bvalues );
314                         }
315                         fprintf( fp, "-\n" );
316                 }
317                 break;
318
319         case LDAP_REQ_ADD:
320                 fprintf( fp, "changetype: add\n" );
321                 a = first ? first : op->ora_e->e_attrs;
322                 for ( ; a != NULL; a=a->a_next ) {
323                         if ( ri && ri->ri_attrs ) {
324                                 int is_in = ad_inlist( a->a_desc, ri->ri_attrs );
325                                 if ( ( !is_in && !ri->ri_exclude ) || ( is_in && ri->ri_exclude ) ) {
326                                         continue;
327                                 }
328                                 /* If the list includes objectClass names,
329                                  * only include those classes in the
330                                  * objectClass attribute
331                                  */
332                                 if ( a->a_desc == slap_schema.si_ad_objectClass ) {
333                                         int ocs = 0;
334                                         AttributeName *an;
335                                         struct berval vals[2];
336                                         vals[1].bv_val = NULL;
337                                         vals[1].bv_len = 0;
338                                         for ( an = ri->ri_attrs; an->an_name.bv_val; an++ ) {
339                                                 if ( an->an_oc ) {
340                                                         int i;
341                                                         for ( i=0; a->a_vals[i].bv_val; i++ ) {
342                                                                 if ( a->a_vals[i].bv_len == an->an_name.bv_len
343                                                                         && !strcasecmp(a->a_vals[i].bv_val,
344                                                                                 an->an_name.bv_val ) ) {
345                                                                         ocs = 1;
346                                                                         vals[0] = an->an_name;
347                                                                         print_vals( fp, &a->a_desc->ad_cname, vals );
348                                                                         break;
349                                                                 }
350                                                         }
351                                                 }
352                                         }
353                                         if ( ocs ) continue;
354                                 }
355                         }
356                         print_vals( fp, &a->a_desc->ad_cname, a->a_vals );
357                 }
358                 break;
359
360         case LDAP_REQ_DELETE:
361                 fprintf( fp, "changetype: delete\n" );
362                 break;
363
364         case LDAP_REQ_MODRDN:
365                 fprintf( fp, "changetype: modrdn\n" );
366                 fprintf( fp, "newrdn: %s\n", op->orr_newrdn.bv_val );
367                 fprintf( fp, "deleteoldrdn: %d\n", op->orr_deleteoldrdn ? 1 : 0 );
368                 if( op->orr_newSup != NULL ) {
369                         fprintf( fp, "newsuperior: %s\n", op->orr_newSup->bv_val );
370                 }
371         }
372         fprintf( fp, "\n" );
373 }
374
375 static void
376 print_vals(
377         FILE *fp,
378         struct berval *type,
379         struct berval *bv )
380 {
381         ber_len_t i, len;
382         char    *buf, *bufp;
383
384         for ( i = 0, len = 0; bv && bv[i].bv_val; i++ ) {
385                 if ( bv[i].bv_len > len )
386                         len = bv[i].bv_len;
387         }
388
389         len = LDIF_SIZE_NEEDED( type->bv_len, len ) + 1;
390         buf = (char *) ch_malloc( len );
391
392         for ( ; bv && bv->bv_val; bv++ ) {
393                 bufp = buf;
394                 ldif_sput( &bufp, LDIF_PUT_VALUE, type->bv_val,
395                                     bv->bv_val, bv->bv_len );
396                 *bufp = '\0';
397
398                 fputs( buf, fp );
399
400         }
401         free( buf );
402 }