]> git.sur5r.net Git - openldap/blob - servers/slapd/repl.c
ITS#3773 additional fix
[openldap] / servers / slapd / repl.c
1 /* repl.c - log modifications for replication purposes */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2005 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms are permitted
20  * provided that this notice is preserved and that due credit is given
21  * to the University of Michigan at Ann Arbor. The name of the University
22  * may not be used to endorse or promote products derived from this
23  * software without specific prior written permission. This software
24  * is provided ``as is'' without express or implied warranty.
25  */
26
27 #include "portable.h"
28
29 #include <stdio.h>
30
31 #include <ac/string.h>
32 #include <ac/ctype.h>
33 #include <ac/socket.h>
34
35 #ifdef HAVE_SYS_FILE_H
36 #include <sys/file.h>
37 #endif
38
39 #include "slap.h"
40 #include "ldif.h"
41
42 int
43 add_replica_info(
44     Backend     *be,
45     const char  *uri, 
46     const char  *host 
47 )
48 {
49         int i = 0;
50
51         assert( be );
52         assert( host );
53
54         if ( be->be_replica != NULL ) {
55                 for ( ; be->be_replica[ i ] != NULL; i++ );
56         }
57                 
58         be->be_replica = ch_realloc( be->be_replica, 
59                 sizeof( struct slap_replica_info * )*( i + 2 ) );
60
61         be->be_replica[ i ] 
62                 = ch_calloc( sizeof( struct slap_replica_info ), 1 );
63         be->be_replica[ i ]->ri_uri = uri;
64         be->be_replica[ i ]->ri_host = host;
65         be->be_replica[ i ]->ri_nsuffix = NULL;
66         be->be_replica[ i ]->ri_attrs = NULL;
67         be->be_replica[ i + 1 ] = NULL;
68
69         return( i );
70 }
71
72 int
73 add_replica_suffix(
74     Backend     *be,
75     int         nr,
76     const char  *suffix
77 )
78 {
79         struct berval dn, ndn;
80         int rc;
81
82         dn.bv_val = (char *) suffix;
83         dn.bv_len = strlen( dn.bv_val );
84
85         rc = dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL );
86         if( rc != LDAP_SUCCESS ) {
87                 return 2;
88         }
89
90         if ( select_backend( &ndn, 0, 0 ) != be ) {
91                 free( ndn.bv_val );
92                 return 1;
93         }
94
95         ber_bvarray_add( &be->be_replica[nr]->ri_nsuffix, &ndn );
96         return 0;
97 }
98
99 int
100 add_replica_attrs(
101         Backend *be,
102         int     nr,
103         char    *attrs,
104         int     exclude
105 )
106 {
107         if ( be->be_replica[nr]->ri_attrs != NULL ) {
108                 if ( be->be_replica[nr]->ri_exclude != exclude ) {
109                         fprintf( stderr, "attr selective replication directive '%s' conflicts with previous one (discarded)\n", attrs );
110                         ch_free( be->be_replica[nr]->ri_attrs );
111                         be->be_replica[nr]->ri_attrs = NULL;
112                 }
113         }
114
115         be->be_replica[nr]->ri_exclude = exclude;
116         be->be_replica[nr]->ri_attrs = str2anlist( be->be_replica[nr]->ri_attrs,
117                 attrs, "," );
118         return ( be->be_replica[nr]->ri_attrs == NULL );
119 }
120    
121 static void
122 print_vals( FILE *fp, struct berval *type, struct berval *bv );
123 static void
124 replog1( struct slap_replica_info *ri, Operation *op, FILE *fp, long now);
125
126 void
127 replog( Operation *op )
128 {
129         Modifications   *ml = NULL;
130         Attribute       *a = NULL;
131         FILE    *fp, *lfp;
132         int     i;
133 /* undef NO_LOG_WHEN_NO_REPLICAS */
134 #ifdef NO_LOG_WHEN_NO_REPLICAS
135         int     count = 0;
136 #endif
137         int     subsets = 0;
138         long    now = slap_get_time();
139         char    *replogfile;
140
141         replogfile = op->o_bd->be_replogfile ? op->o_bd->be_replogfile :
142                 frontendDB->be_replogfile;
143         if ( !replogfile ) {
144                 return;
145         }
146
147         ldap_pvt_thread_mutex_lock( &replog_mutex );
148         if ( (fp = lock_fopen( replogfile, "a", &lfp )) == NULL ) {
149                 ldap_pvt_thread_mutex_unlock( &replog_mutex );
150                 return;
151         }
152
153         for ( i = 0; op->o_bd->be_replica != NULL && op->o_bd->be_replica[i] != NULL; i++ ) {
154                 /* check if dn's suffix matches legal suffixes, if any */
155                 if ( op->o_bd->be_replica[i]->ri_nsuffix != NULL ) {
156                         int j;
157
158                         for ( j = 0; op->o_bd->be_replica[i]->ri_nsuffix[j].bv_val; j++ ) {
159                                 if ( dnIsSuffix( &op->o_req_ndn, &op->o_bd->be_replica[i]->ri_nsuffix[j] ) ) {
160                                         break;
161                                 }
162                         }
163
164                         if ( !op->o_bd->be_replica[i]->ri_nsuffix[j].bv_val ) {
165                                 /* do not add "replica:" line */
166                                 continue;
167                         }
168                 }
169                 /* See if we only want a subset of attributes */
170                 if ( op->o_bd->be_replica[i]->ri_attrs != NULL &&
171                         ( op->o_tag == LDAP_REQ_MODIFY || op->o_tag == LDAP_REQ_ADD || op->o_tag == LDAP_REQ_EXTENDED ) ) {
172                         if ( !subsets ) {
173                                 subsets = i + 1;
174                         }
175                         /* Do attribute subsets by themselves in a second pass */
176                         continue;
177                 }
178
179                 fprintf( fp, "replica: %s\n", op->o_bd->be_replica[i]->ri_host );
180 #ifdef NO_LOG_WHEN_NO_REPLICAS
181                 ++count;
182 #endif
183         }
184
185 #ifdef NO_LOG_WHEN_NO_REPLICAS
186         if ( count == 0 && subsets == 0 ) {
187                 /* if no replicas matched, drop the log 
188                  * (should we log it anyway?) */
189                 lock_fclose( fp, lfp );
190                 ldap_pvt_thread_mutex_unlock( &replog_mutex );
191
192                 return;
193         }
194 #endif
195
196         replog1( NULL, op, fp, now );
197
198         if ( subsets > 0 ) {
199                 for ( i = subsets - 1; op->o_bd->be_replica[i] != NULL; i++ ) {
200
201                         /* If no attrs, we already did this above */
202                         if ( op->o_bd->be_replica[i]->ri_attrs == NULL ) {
203                                 continue;
204                         }
205
206                         /* check if dn's suffix matches legal suffixes, if any */
207                         if ( op->o_bd->be_replica[i]->ri_nsuffix != NULL ) {
208                                 int j;
209
210                                 for ( j = 0; op->o_bd->be_replica[i]->ri_nsuffix[j].bv_val; j++ ) {
211                                         if ( dnIsSuffix( &op->o_req_ndn, &op->o_bd->be_replica[i]->ri_nsuffix[j] ) ) {
212                                                 break;
213                                         }
214                                 }
215
216                                 if ( !op->o_bd->be_replica[i]->ri_nsuffix[j].bv_val ) {
217                                         /* no matching suffix found, skip it */
218                                         continue;
219                                 }
220                         }
221                         switch( op->o_tag ) {
222                         case LDAP_REQ_EXTENDED:
223                                 /* quick hack for extended operations */
224                                 /* assume change parameter is a Modifications* */
225                                 /* fall thru */
226                         case LDAP_REQ_MODIFY:
227                         case LDAP_REQ_ADD:
228                                 break;
229                         default:
230                                 /* Other operations were logged in the first pass */
231                                 continue;
232                         }
233                         replog1( op->o_bd->be_replica[i], op, fp, now );
234                 }
235         }
236
237         lock_fclose( fp, lfp );
238         ldap_pvt_thread_mutex_unlock( &replog_mutex );
239 }
240
241 static void
242 rephdr(
243         struct slap_replica_info *ri,
244         Operation *op,
245         FILE *fp,
246         long now
247 )
248 {
249         if ( ri ) {
250                 fprintf( fp, "replica: %s\n", ri->ri_host );
251         }
252         fprintf( fp, "time: %ld\n", now );
253         fprintf( fp, "dn: %s\n", op->o_req_dn.bv_val );
254 }
255
256 static void
257 replog1(
258         struct slap_replica_info *ri,
259         Operation *op,
260         FILE    *fp,
261         long    now
262 )
263 {
264         Modifications   *ml;
265         Attribute       *a;
266         AttributeName   *an;
267         int             dohdr = 1, ocs = -1;
268         struct berval vals[2];
269
270         vals[1].bv_val = NULL;
271         vals[1].bv_len = 0;
272
273         switch ( op->o_tag ) {
274         case LDAP_REQ_EXTENDED:
275                 /* quick hack for extended operations */
276                 /* assume change parameter is a Modifications* */
277                 /* fall thru */
278
279         case LDAP_REQ_MODIFY:
280                 for ( ml = op->orm_modlist; ml != NULL; ml = ml->sml_next ) {
281                         char *did, *type = ml->sml_desc->ad_cname.bv_val;
282                         switch ( ml->sml_op ) {
283                         case LDAP_MOD_ADD:
284                                 did = "add"; break;
285
286                         case LDAP_MOD_DELETE:
287                                 did = "delete"; break;
288
289                         case LDAP_MOD_REPLACE:
290                                 did = "replace"; break;
291
292                         case LDAP_MOD_INCREMENT:
293                                 did = "increment"; break;
294                         }
295                         if ( ri && ri->ri_attrs ) {
296                                 int is_in = ad_inlist( ml->sml_desc, ri->ri_attrs );
297
298                                 if ( ( !is_in && !ri->ri_exclude )
299                                         || ( is_in && ri->ri_exclude ) )
300                                 {
301                                         continue;
302                                 }
303                                 /* If this is objectClass, see if the value is included
304                                  * in any subset, otherwise drop it.
305                                  */
306                                 if ( ocs && ml->sml_desc == slap_schema.si_ad_objectClass
307                                         && ml->sml_values )
308                                 {
309                                         int i, first = 1;
310
311                                         if ( ocs == -1 ) ocs = 0;
312
313                                         for ( i=0; ml->sml_values[i].bv_val; i++ ) {
314                                                 int match = 0;
315                                                 for ( an = ri->ri_attrs; an->an_name.bv_val; an++ ) {
316                                                         if ( an->an_oc ) {
317                                                                 ocs = 1;
318                                                                 match |= an->an_oc_exclude;
319                                                                 if ( ml->sml_values[i].bv_len == an->an_name.bv_len
320                                                                         && !strcasecmp(ml->sml_values[i].bv_val,
321                                                                                 an->an_name.bv_val ) ) {
322                                                                         match = !an->an_oc_exclude;
323                                                                         break;
324                                                                 }
325                                                         }
326                                                 }
327                                                 /* Objectclasses need no special treatment, drop into
328                                                  * regular processing
329                                                  */
330                                                 if ( !ocs ) break;
331
332                                                 match ^= ri->ri_exclude;
333                                                 /* Found a match, log it */
334                                                 if ( match ) {
335                                                         if ( dohdr ) {
336                                                                 rephdr( ri, op, fp, now );
337                                                                 fprintf( fp, "changetype: modify\n" );
338                                                                 dohdr = 0;
339                                                         }
340                                                         if ( first ) {
341                                                                 fprintf( fp, "%s: %s\n", did, type );
342                                                                 first = 0;
343                                                         }
344                                                         vals[0] = an->an_name;
345                                                         print_vals( fp, &ml->sml_desc->ad_cname, vals );
346                                                         ocs = 2;
347                                                 }
348
349                                         }
350                                         /* Explicit objectclasses have been handled already */
351                                         if ( ocs ) {
352                                                 if ( ocs == 2 ) {
353                                                         fprintf( fp, "-\n" );
354                                                 }
355                                                 continue;
356                                         }
357                                 }
358                         }
359                         if ( dohdr ) {
360                                 rephdr( ri, op, fp, now );
361                                 fprintf( fp, "changetype: modify\n" );
362                                 dohdr = 0;
363                         }
364                         fprintf( fp, "%s: %s\n", did, type );
365                         if ( ml->sml_values ) {
366                                 print_vals( fp, &ml->sml_desc->ad_cname, ml->sml_values );
367                         }
368                         fprintf( fp, "-\n" );
369                 }
370                 break;
371
372         case LDAP_REQ_ADD:
373                 for ( a = op->ora_e->e_attrs ; a != NULL; a=a->a_next ) {
374                         if ( ri && ri->ri_attrs ) {
375                                 int is_in = ad_inlist( a->a_desc, ri->ri_attrs );
376                                 if ( ( !is_in && !ri->ri_exclude ) || ( is_in && ri->ri_exclude ) ) {
377                                         continue;
378                                 }
379
380                                 /* If the list includes objectClass names,
381                                  * only include those classes in the
382                                  * objectClass attribute
383                                  */
384                                 if ( ocs && a->a_desc == slap_schema.si_ad_objectClass ) {
385                                         int i;
386
387                                         if ( ocs == -1 ) ocs = 0;
388
389                                         for ( i=0; a->a_vals[i].bv_val; i++ ) {
390                                                 int match = 0;
391                                                 for ( an = ri->ri_attrs; an->an_name.bv_val; an++ ) {
392                                                         if ( an->an_oc ) {
393                                                                 ocs = 1;
394                                                                 match |= an->an_oc_exclude;
395                                                                 if ( a->a_vals[i].bv_len == an->an_name.bv_len
396                                                                         && !strcasecmp(a->a_vals[i].bv_val,
397                                                                                 an->an_name.bv_val ) ) {
398                                                                         match = !an->an_oc_exclude;
399                                                                         break;
400                                                                 }
401                                                         }
402                                                 }
403                                                 if ( !ocs ) break;
404
405                                                 match ^= ri->ri_exclude;
406                                                 if ( match ) {
407                                                         if ( dohdr ) {
408                                                                 rephdr( ri, op, fp, now );
409                                                                 fprintf( fp, "changetype: add\n" );
410                                                                 dohdr = 0;
411                                                         }
412                                                         vals[0] = an->an_name;
413                                                         print_vals( fp, &a->a_desc->ad_cname, vals );
414                                                 }
415                                         }
416                                         if ( ocs ) continue;
417                                 }
418                         }
419                         if ( dohdr ) {
420                                 rephdr( ri, op, fp, now );
421                                 fprintf( fp, "changetype: add\n" );
422                                 dohdr = 0;
423                         }
424                         print_vals( fp, &a->a_desc->ad_cname, a->a_vals );
425                 }
426                 break;
427
428         case LDAP_REQ_DELETE:
429                 rephdr( ri, op, fp, now );
430                 fprintf( fp, "changetype: delete\n" );
431                 break;
432
433         case LDAP_REQ_MODRDN:
434                 rephdr( ri, op, fp, now );
435                 fprintf( fp, "changetype: modrdn\n" );
436                 fprintf( fp, "newrdn: %s\n", op->orr_newrdn.bv_val );
437                 fprintf( fp, "deleteoldrdn: %d\n", op->orr_deleteoldrdn ? 1 : 0 );
438                 if( op->orr_newSup != NULL ) {
439                         fprintf( fp, "newsuperior: %s\n", op->orr_newSup->bv_val );
440                 }
441         }
442         fprintf( fp, "\n" );
443 }
444
445 static void
446 print_vals(
447         FILE *fp,
448         struct berval *type,
449         struct berval *bv )
450 {
451         ber_len_t i, len;
452         char    *buf, *bufp;
453
454         for ( i = 0, len = 0; bv && bv[i].bv_val; i++ ) {
455                 if ( bv[i].bv_len > len )
456                         len = bv[i].bv_len;
457         }
458
459         len = LDIF_SIZE_NEEDED( type->bv_len, len ) + 1;
460         buf = (char *) ch_malloc( len );
461
462         for ( ; bv && bv->bv_val; bv++ ) {
463                 bufp = buf;
464                 ldif_sput( &bufp, LDIF_PUT_VALUE, type->bv_val,
465                                     bv->bv_val, bv->bv_len );
466                 *bufp = '\0';
467
468                 fputs( buf, fp );
469
470         }
471         free( buf );
472 }