]> git.sur5r.net Git - openldap/blob - servers/slapd/repl.c
603634735d3853f4a4ab4966765a3a9557db29ee
[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-2003 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  *host 
46 )
47 {
48         int i = 0;
49
50         assert( be );
51         assert( host );
52
53         if ( be->be_replica != NULL ) {
54                 for ( ; be->be_replica[ i ] != NULL; i++ );
55         }
56                 
57         be->be_replica = ch_realloc( be->be_replica, 
58                 sizeof( struct slap_replica_info * )*( i + 2 ) );
59
60         be->be_replica[ i ] 
61                 = ch_calloc( sizeof( struct slap_replica_info ), 1 );
62         be->be_replica[ i ]->ri_host = ch_strdup( host );
63         be->be_replica[ i ]->ri_nsuffix = NULL;
64         be->be_replica[ i ]->ri_attrs = NULL;
65         be->be_replica[ i + 1 ] = NULL;
66
67         return( i );
68 }
69
70 int
71 add_replica_suffix(
72     Backend     *be,
73     int         nr,
74     const char  *suffix
75 )
76 {
77         struct berval dn, ndn;
78         int rc;
79
80         dn.bv_val = (char *) suffix;
81         dn.bv_len = strlen( dn.bv_val );
82
83         rc = dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL );
84         if( rc != LDAP_SUCCESS ) {
85                 return 2;
86         }
87
88         if ( select_backend( &ndn, 0, 0 ) != be ) {
89                 free( ndn.bv_val );
90                 return 1;
91         }
92
93         ber_bvarray_add( &be->be_replica[nr]->ri_nsuffix, &ndn );
94         return 0;
95 }
96
97 int
98 add_replica_attrs(
99         Backend *be,
100         int     nr,
101         char    *attrs,
102         int     exclude
103 )
104 {
105         if ( be->be_replica[nr]->ri_attrs != NULL ) {
106                 if ( be->be_replica[nr]->ri_exclude != exclude ) {
107                         fprintf( stderr, "attr selective replication directive '%s' conflicts with previous one (discarded)\n", attrs );
108                         ch_free( be->be_replica[nr]->ri_attrs );
109                         be->be_replica[nr]->ri_attrs = NULL;
110                 }
111         }
112
113         be->be_replica[nr]->ri_exclude = exclude;
114         be->be_replica[nr]->ri_attrs = str2anlist( be->be_replica[nr]->ri_attrs,
115                 attrs, "," );
116         return ( be->be_replica[nr]->ri_attrs == NULL );
117 }
118    
119 static void
120 print_vals( FILE *fp, struct berval *type, struct berval *bv );
121 static void
122 replog1( struct slap_replica_info *ri, Operation *op, FILE *fp, void *first);
123
124 void
125 replog( Operation *op )
126 {
127         Modifications   *ml = NULL;
128         Attribute       *a = NULL;
129         FILE    *fp, *lfp;
130         int     i;
131 /* undef NO_LOG_WHEN_NO_REPLICAS */
132 #ifdef NO_LOG_WHEN_NO_REPLICAS
133         int     count = 0;
134 #endif
135         int     subsets = 0;
136         long now = slap_get_time();
137
138         if ( op->o_bd->be_replogfile == NULL && replogfile == NULL ) {
139                 return;
140         }
141
142         ldap_pvt_thread_mutex_lock( &replog_mutex );
143         if ( (fp = lock_fopen( op->o_bd->be_replogfile ? op->o_bd->be_replogfile :
144             replogfile, "a", &lfp )) == NULL ) {
145                 ldap_pvt_thread_mutex_unlock( &replog_mutex );
146                 return;
147         }
148
149         for ( i = 0; op->o_bd->be_replica != NULL && op->o_bd->be_replica[i] != NULL; i++ ) {
150                 /* check if dn's suffix matches legal suffixes, if any */
151                 if ( op->o_bd->be_replica[i]->ri_nsuffix != NULL ) {
152                         int j;
153
154                         for ( j = 0; op->o_bd->be_replica[i]->ri_nsuffix[j].bv_val; j++ ) {
155                                 if ( dnIsSuffix( &op->o_req_ndn, &op->o_bd->be_replica[i]->ri_nsuffix[j] ) ) {
156                                         break;
157                                 }
158                         }
159
160                         if ( !op->o_bd->be_replica[i]->ri_nsuffix[j].bv_val ) {
161                                 /* do not add "replica:" line */
162                                 continue;
163                         }
164                 }
165                 /* See if we only want a subset of attributes */
166                 if ( op->o_bd->be_replica[i]->ri_attrs != NULL &&
167                         ( op->o_tag == LDAP_REQ_MODIFY || op->o_tag == LDAP_REQ_ADD || op->o_tag == LDAP_REQ_EXTENDED ) ) {
168                         if ( !subsets ) {
169                                 subsets = i + 1;
170                         }
171                         /* Do attribute subsets by themselves in a second pass */
172                         continue;
173                 }
174
175                 fprintf( fp, "replica: %s\n", op->o_bd->be_replica[i]->ri_host );
176 #ifdef NO_LOG_WHEN_NO_REPLICAS
177                 ++count;
178 #endif
179         }
180
181 #ifdef NO_LOG_WHEN_NO_REPLICAS
182         if ( count == 0 && subsets == 0 ) {
183                 /* if no replicas matched, drop the log 
184                  * (should we log it anyway?) */
185                 lock_fclose( fp, lfp );
186                 ldap_pvt_thread_mutex_unlock( &replog_mutex );
187
188                 return;
189         }
190 #endif
191
192         fprintf( fp, "time: %ld\n", now );
193         fprintf( fp, "dn: %s\n", op->o_req_dn.bv_val );
194
195         replog1( NULL, op, fp, NULL );
196
197         if ( subsets > 0 ) {
198                 void *first;
199                 for ( i = subsets - 1; op->o_bd->be_replica != NULL && 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                                         /* do not add "replica:" line */
218                                         continue;
219                                 }
220                         }
221                         subsets = 0;
222                         first = NULL;
223                         switch( op->o_tag ) {
224                         case LDAP_REQ_EXTENDED:
225                                 /* quick hack for extended operations */
226                                 /* assume change parameter is a Modfications* */
227                                 /* fall thru */
228                         case LDAP_REQ_MODIFY:
229                                 for ( ml = op->orm_modlist; ml != NULL; ml = ml->sml_next ) {
230                                         int is_in, exclude, ocs = 0;
231  
232                                         exclude = op->o_bd->be_replica[i]->ri_exclude;
233
234                                         if ( ml->sml_desc == slap_schema.si_ad_objectClass
235                                                 && ml->sml_bvalues ) {
236                                                 AttributeName *an;
237                                                 for ( an = op->o_bd->be_replica[i]->ri_attrs; an->an_name.bv_val; an++ ) {
238                                                         if ( an->an_oc ) {
239                                                                 int i, match = 0;
240                                                                 ocs = 1;
241                                                                 for ( i=0; ml->sml_bvalues[i].bv_val; i++ ) {
242                                                                         if ( ml->sml_bvalues[i].bv_len == an->an_name.bv_len
243                                                                                 && !strcasecmp(ml->sml_bvalues[i].bv_val,
244                                                                                         an->an_name.bv_val ) ) {
245                                                                                 match = 1;
246                                                                                 break;
247                                                                         }
248                                                                 }
249                                                                 match ^= exclude;
250                                                                 match ^= an->an_oc_exclude;
251                                                                 /* Found a match, stop looking */
252                                                                 if ( match ) {
253                                                                         ocs = 2;
254                                                                         break;
255                                                                 }
256                                                         }
257                                                 }
258                                                 if ( ocs == 1 ) continue;
259                                         }
260
261                                         is_in = ad_inlist( ml->sml_desc, op->o_bd->be_replica[i]->ri_attrs );
262                                         
263                                         /*
264                                          * there might be a more clever way to do this test,
265                                          * but this way, at least, is comprehensible :)
266                                          */
267                                         if ( ( is_in && !exclude ) || ( !is_in && exclude ) ) {
268                                                 subsets = 1;
269                                                 first = ml;
270                                                 break;
271                                         }
272                                 }
273                                 if ( !subsets ) {
274                                         continue;
275                                 }
276                                 break;
277                         case LDAP_REQ_ADD:
278                                 for ( a = op->ora_e->e_attrs; a != NULL; a = a->a_next ) {
279                                         int is_in, exclude;
280
281                                         is_in = ad_inlist( a->a_desc, op->o_bd->be_replica[i]->ri_attrs );
282                                         exclude = op->o_bd->be_replica[i]->ri_exclude;
283                                         
284                                         if ( ( is_in && !exclude ) || ( !is_in && exclude ) ) {
285                                                 subsets = 1;
286                                                 first = a;
287                                                 break;
288                                         }
289                                 }
290                                 if ( !subsets ) {
291                                         continue;
292                                 }
293                                 break;
294                         default:
295                                 /* Other operations were logged in the first pass */
296                                 continue;
297                         }
298                         fprintf( fp, "replica: %s\n", op->o_bd->be_replica[i]->ri_host );
299                         fprintf( fp, "time: %ld\n", now );
300                         fprintf( fp, "dn: %s\n", op->o_req_dn.bv_val );
301                         replog1( op->o_bd->be_replica[i], op, fp, first );
302                 }
303         }
304
305         lock_fclose( fp, lfp );
306         ldap_pvt_thread_mutex_unlock( &replog_mutex );
307 }
308
309
310 static void
311 replog1(
312     struct slap_replica_info *ri,
313     Operation *op,
314     FILE        *fp,
315         void    *first
316 )
317 {
318         Modifications   *ml;
319         Attribute       *a;
320         AttributeName   *an;
321         int             domod = 1;
322
323         switch ( op->o_tag ) {
324         case LDAP_REQ_EXTENDED:
325                 /* quick hack for extended operations */
326                 /* assume change parameter is a Modfications* */
327                 /* fall thru */
328
329         case LDAP_REQ_MODIFY:
330                 ml = first ? first : op->orm_modlist;
331                 for ( ; ml != NULL; ml = ml->sml_next ) {
332                         char *type;
333                         int ocs = 0;
334                         if ( ri && ri->ri_attrs ) {
335                                 int is_in = ad_inlist( ml->sml_desc, ri->ri_attrs );
336
337                                 if ( ( !is_in && !ri->ri_exclude )
338                                         || ( is_in && ri->ri_exclude ) )
339                                 {
340                                         continue;
341                                 }
342                                 /* If this is objectClass, see if the value is included
343                                  * in any subset, otherwise drop it.
344                                  */
345                                 if ( ml->sml_desc == slap_schema.si_ad_objectClass
346                                         && ml->sml_bvalues ) {
347                                         for ( an = ri->ri_attrs; an->an_name.bv_val; an++ ) {
348                                                 if ( an->an_oc ) {
349                                                         int i, match = 0;
350                                                         ocs = 1;
351                                                         for ( i=0; ml->sml_bvalues[i].bv_val; i++ ) {
352                                                                 if ( ml->sml_bvalues[i].bv_len == an->an_name.bv_len
353                                                                         && !strcasecmp(ml->sml_bvalues[i].bv_val,
354                                                                                 an->an_name.bv_val ) ) {
355                                                                         match = 1;
356                                                                         break;
357                                                                 }
358                                                         }
359                                                         match ^= ri->ri_exclude;
360                                                         match ^= an->an_oc_exclude;
361                                                         /* Found a match, stop looking */
362                                                         if ( match ) {
363                                                                 ocs = 2;
364                                                                 break;
365                                                         }
366                                                 }
367                                         }
368                                 }
369                                 /* Have explicit objectClasses, but none matched - forget it */
370                                 if ( ocs == 1 ) continue;
371                         }
372                         if ( domod ) {
373                                 fprintf( fp, "changetype: modify\n" );
374                                 domod = 0;
375                         }
376                         type = ml->sml_desc->ad_cname.bv_val;
377                         switch ( ml->sml_op ) {
378                         case LDAP_MOD_ADD:
379                                 fprintf( fp, "add: %s\n", type );
380                                 break;
381
382                         case LDAP_MOD_DELETE:
383                                 fprintf( fp, "delete: %s\n", type );
384                                 break;
385
386                         case LDAP_MOD_REPLACE:
387                                 fprintf( fp, "replace: %s\n", type );
388                                 break;
389
390                         case LDAP_MOD_INCREMENT:
391                                 fprintf( fp, "increment: %s\n", type );
392                                 break;
393                         }
394                         if ( ocs == 2 ) {
395                                 int i;
396                                 struct berval vals[2];
397                                 vals[1].bv_val = NULL;
398                                 vals[1].bv_len = 0;
399                                 for ( i=0; ml->sml_bvalues[i].bv_val; i++ ) {
400                                         int match = 0;
401                                         for ( an = ri->ri_attrs; an->an_name.bv_val; an++ ) {
402                                                 if ( an->an_oc
403                                                         && ml->sml_bvalues[i].bv_len == an->an_name.bv_len
404                                                         && !strcasecmp(ml->sml_bvalues[i].bv_val,
405                                                                 an->an_name.bv_val ) ) {
406                                                         match = 1 ^ an->an_oc_exclude;
407                                                         break;
408                                                 }
409                                         }
410                                         match ^= ri->ri_exclude;
411                                         if ( match ) {
412                                                 vals[0] = an->an_name;
413                                                 print_vals( fp, &ml->sml_desc->ad_cname, vals );
414                                         }
415                                 }
416                         } else if ( ml->sml_bvalues ) {
417                                 print_vals( fp, &ml->sml_desc->ad_cname, ml->sml_bvalues );
418                         }
419                         fprintf( fp, "-\n" );
420                 }
421                 break;
422
423         case LDAP_REQ_ADD:
424                 fprintf( fp, "changetype: add\n" );
425                 a = first ? first : op->ora_e->e_attrs;
426                 for ( ; a != NULL; a=a->a_next ) {
427                         if ( ri && ri->ri_attrs ) {
428                                 int is_in = ad_inlist( a->a_desc, ri->ri_attrs );
429                                 if ( ( !is_in && !ri->ri_exclude ) || ( is_in && ri->ri_exclude ) ) {
430                                         continue;
431                                 }
432
433                                 /* If the list includes objectClass names,
434                                  * only include those classes in the
435                                  * objectClass attribute
436                                  */
437                                 if ( a->a_desc == slap_schema.si_ad_objectClass ) {
438                                         int i, ocs = 0;
439                                         struct berval vals[2];
440                                         vals[1].bv_val = NULL;
441                                         vals[1].bv_len = 0;
442                                         for ( i=0; a->a_vals[i].bv_val; i++ ) {
443                                                 int match = 0;
444                                                 for ( an = ri->ri_attrs; an->an_name.bv_val; an++ ) {
445                                                         if ( an->an_oc ) {
446                                                                 ocs = 1;
447                                                                 if ( a->a_vals[i].bv_len == an->an_name.bv_len
448                                                                         && !strcasecmp(a->a_vals[i].bv_val,
449                                                                                 an->an_name.bv_val ) ) {
450                                                                         match = 1 ^ an->an_oc_exclude;
451                                                                         break;
452                                                                 }
453                                                         }
454                                                 }
455                                                 match ^= ri->ri_exclude;
456                                                 if ( ocs && match ) {
457                                                         vals[0] = an->an_name;
458                                                         print_vals( fp, &a->a_desc->ad_cname, vals );
459                                                 }
460                                         }
461                                         if ( ocs ) continue;
462                                 }
463                         }
464                         print_vals( fp, &a->a_desc->ad_cname, a->a_vals );
465                 }
466                 break;
467
468         case LDAP_REQ_DELETE:
469                 fprintf( fp, "changetype: delete\n" );
470                 break;
471
472         case LDAP_REQ_MODRDN:
473                 fprintf( fp, "changetype: modrdn\n" );
474                 fprintf( fp, "newrdn: %s\n", op->orr_newrdn.bv_val );
475                 fprintf( fp, "deleteoldrdn: %d\n", op->orr_deleteoldrdn ? 1 : 0 );
476                 if( op->orr_newSup != NULL ) {
477                         fprintf( fp, "newsuperior: %s\n", op->orr_newSup->bv_val );
478                 }
479         }
480         fprintf( fp, "\n" );
481 }
482
483 static void
484 print_vals(
485         FILE *fp,
486         struct berval *type,
487         struct berval *bv )
488 {
489         ber_len_t i, len;
490         char    *buf, *bufp;
491
492         for ( i = 0, len = 0; bv && bv[i].bv_val; i++ ) {
493                 if ( bv[i].bv_len > len )
494                         len = bv[i].bv_len;
495         }
496
497         len = LDIF_SIZE_NEEDED( type->bv_len, len ) + 1;
498         buf = (char *) ch_malloc( len );
499
500         for ( ; bv && bv->bv_val; bv++ ) {
501                 bufp = buf;
502                 ldif_sput( &bufp, LDIF_PUT_VALUE, type->bv_val,
503                                     bv->bv_val, bv->bv_len );
504                 *bufp = '\0';
505
506                 fputs( buf, fp );
507
508         }
509         free( buf );
510 }