]> git.sur5r.net Git - openldap/blob - servers/slapd/repl.c
Clean up prev commit
[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, long now);
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         replog1( NULL, op, fp, now );
193
194         if ( subsets > 0 ) {
195                 for ( i = subsets - 1; op->o_bd->be_replica[i] != NULL; i++ ) {
196
197                         /* If no attrs, we already did this above */
198                         if ( op->o_bd->be_replica[i]->ri_attrs == NULL ) {
199                                 continue;
200                         }
201
202                         /* check if dn's suffix matches legal suffixes, if any */
203                         if ( op->o_bd->be_replica[i]->ri_nsuffix != NULL ) {
204                                 int j;
205
206                                 for ( j = 0; op->o_bd->be_replica[i]->ri_nsuffix[j].bv_val; j++ ) {
207                                         if ( dnIsSuffix( &op->o_req_ndn, &op->o_bd->be_replica[i]->ri_nsuffix[j] ) ) {
208                                                 break;
209                                         }
210                                 }
211
212                                 if ( !op->o_bd->be_replica[i]->ri_nsuffix[j].bv_val ) {
213                                         /* no matching suffix found, skip it */
214                                         continue;
215                                 }
216                         }
217                         switch( op->o_tag ) {
218                         case LDAP_REQ_EXTENDED:
219                                 /* quick hack for extended operations */
220                                 /* assume change parameter is a Modifications* */
221                                 /* fall thru */
222                         case LDAP_REQ_MODIFY:
223                         case LDAP_REQ_ADD:
224                                 break;
225                         default:
226                                 /* Other operations were logged in the first pass */
227                                 continue;
228                         }
229                         replog1( op->o_bd->be_replica[i], op, fp, now );
230                 }
231         }
232
233         lock_fclose( fp, lfp );
234         ldap_pvt_thread_mutex_unlock( &replog_mutex );
235 }
236
237 static void
238 rephdr(
239         struct slap_replica_info *ri,
240         Operation *op,
241         FILE *fp,
242         long now
243 )
244 {
245         if ( ri ) {
246                 fprintf( fp, "replica: %s\n", ri->ri_host );
247         }
248         fprintf( fp, "time: %ld\n", now );
249         fprintf( fp, "dn: %s\n", op->o_req_dn.bv_val );
250 }
251
252 static void
253 replog1(
254         struct slap_replica_info *ri,
255         Operation *op,
256         FILE    *fp,
257         long    now
258 )
259 {
260         Modifications   *ml;
261         Attribute       *a;
262         AttributeName   *an;
263         int             dohdr = 1;
264         struct berval vals[2];
265         vals[1].bv_val = NULL;
266         vals[1].bv_len = 0;
267
268         switch ( op->o_tag ) {
269         case LDAP_REQ_EXTENDED:
270                 /* quick hack for extended operations */
271                 /* assume change parameter is a Modifications* */
272                 /* fall thru */
273
274         case LDAP_REQ_MODIFY:
275                 for ( ml = op->orm_modlist; ml != NULL; ml = ml->sml_next ) {
276                         char *did, *type = ml->sml_desc->ad_cname.bv_val;
277                         switch ( ml->sml_op ) {
278                         case LDAP_MOD_ADD:
279                                 did = "add"; break;
280
281                         case LDAP_MOD_DELETE:
282                                 did = "delete"; break;
283
284                         case LDAP_MOD_REPLACE:
285                                 did = "replace"; break;
286
287                         case LDAP_MOD_INCREMENT:
288                                 did = "increment"; break;
289                         }
290                         if ( ri && ri->ri_attrs ) {
291                                 int is_in = ad_inlist( ml->sml_desc, ri->ri_attrs );
292
293                                 if ( ( !is_in && !ri->ri_exclude )
294                                         || ( is_in && ri->ri_exclude ) )
295                                 {
296                                         continue;
297                                 }
298                                 /* If this is objectClass, see if the value is included
299                                  * in any subset, otherwise drop it.
300                                  */
301                                 if ( ml->sml_desc == slap_schema.si_ad_objectClass
302                                         && ml->sml_bvalues ) {
303                                         int i, ocs = 0, first = 1;
304                                         for ( i=0; ml->sml_bvalues[i].bv_val; i++ ) {
305                                                 int match = 0;
306                                                 for ( an = ri->ri_attrs; an->an_name.bv_val; an++ ) {
307                                                         if ( an->an_oc ) {
308                                                                 ocs = 1;
309                                                                 if ( ml->sml_bvalues[i].bv_len == an->an_name.bv_len
310                                                                         && !strcasecmp(ml->sml_bvalues[i].bv_val,
311                                                                                 an->an_name.bv_val ) ) {
312                                                                         match = 1 ^ an->an_oc_exclude;
313                                                                         break;
314                                                                 }
315                                                         }
316                                                 }
317                                                 if ( ocs ) {
318                                                         match ^= ri->ri_exclude;
319                                                         /* Found a match, log it */
320                                                         if ( match ) {
321                                                                 if ( dohdr ) {
322                                                                         rephdr( ri, op, fp, now );
323                                                                         fprintf( fp, "changetype: modify\n" );
324                                                                         dohdr = 0;
325                                                                 }
326                                                                 if ( first ) {
327                                                                         fprintf( fp, "%s: %s\n", did, type );
328                                                                         first = 0;
329                                                                 }
330                                                                 vals[0] = an->an_name;
331                                                                 print_vals( fp, &ml->sml_desc->ad_cname, vals );
332                                                                 ocs = 2;
333                                                         }
334                                                 }
335                                         }
336                                         /* Explicit objectclasses have been handled already */
337                                         if ( ocs ) {
338                                                 if ( ocs == 2 ) {
339                                                         fprintf( fp, "-\n" );
340                                                 }
341                                                 continue;
342                                         }
343                                 }
344                         }
345                         if ( dohdr ) {
346                                 rephdr( ri, op, fp, now );
347                                 fprintf( fp, "changetype: modify\n" );
348                                 dohdr = 0;
349                         }
350                         fprintf( fp, "%s: %s\n", did, type );
351                         if ( ml->sml_bvalues ) {
352                                 print_vals( fp, &ml->sml_desc->ad_cname, ml->sml_bvalues );
353                         }
354                         fprintf( fp, "-\n" );
355                 }
356                 break;
357
358         case LDAP_REQ_ADD:
359                 for ( a = op->ora_e->e_attrs ; a != NULL; a=a->a_next ) {
360                         if ( ri && ri->ri_attrs ) {
361                                 int is_in = ad_inlist( a->a_desc, ri->ri_attrs );
362                                 if ( ( !is_in && !ri->ri_exclude ) || ( is_in && ri->ri_exclude ) ) {
363                                         continue;
364                                 }
365
366                                 /* If the list includes objectClass names,
367                                  * only include those classes in the
368                                  * objectClass attribute
369                                  */
370                                 if ( a->a_desc == slap_schema.si_ad_objectClass ) {
371                                         int i, ocs = 0;
372                                         for ( i=0; a->a_vals[i].bv_val; i++ ) {
373                                                 int match = 0;
374                                                 for ( an = ri->ri_attrs; an->an_name.bv_val; an++ ) {
375                                                         if ( an->an_oc ) {
376                                                                 ocs = 1;
377                                                                 if ( a->a_vals[i].bv_len == an->an_name.bv_len
378                                                                         && !strcasecmp(a->a_vals[i].bv_val,
379                                                                                 an->an_name.bv_val ) ) {
380                                                                         match = 1 ^ an->an_oc_exclude;
381                                                                         break;
382                                                                 }
383                                                         }
384                                                 }
385                                                 match ^= ri->ri_exclude;
386                                                 if ( ocs && match ) {
387                                                         if ( dohdr ) {
388                                                                 rephdr( ri, op, fp, now );
389                                                                 fprintf( fp, "changetype: add\n" );
390                                                                 dohdr = 0;
391                                                         }
392                                                         vals[0] = an->an_name;
393                                                         print_vals( fp, &a->a_desc->ad_cname, vals );
394                                                 }
395                                         }
396                                         if ( ocs ) continue;
397                                 }
398                         }
399                         if ( dohdr ) {
400                                 rephdr( ri, op, fp, now );
401                                 fprintf( fp, "changetype: add\n" );
402                                 dohdr = 0;
403                         }
404                         print_vals( fp, &a->a_desc->ad_cname, a->a_vals );
405                 }
406                 break;
407
408         case LDAP_REQ_DELETE:
409                 rephdr( ri, op, fp, now );
410                 fprintf( fp, "changetype: delete\n" );
411                 break;
412
413         case LDAP_REQ_MODRDN:
414                 rephdr( ri, op, fp, now );
415                 fprintf( fp, "changetype: modrdn\n" );
416                 fprintf( fp, "newrdn: %s\n", op->orr_newrdn.bv_val );
417                 fprintf( fp, "deleteoldrdn: %d\n", op->orr_deleteoldrdn ? 1 : 0 );
418                 if( op->orr_newSup != NULL ) {
419                         fprintf( fp, "newsuperior: %s\n", op->orr_newSup->bv_val );
420                 }
421         }
422         fprintf( fp, "\n" );
423 }
424
425 static void
426 print_vals(
427         FILE *fp,
428         struct berval *type,
429         struct berval *bv )
430 {
431         ber_len_t i, len;
432         char    *buf, *bufp;
433
434         for ( i = 0, len = 0; bv && bv[i].bv_val; i++ ) {
435                 if ( bv[i].bv_len > len )
436                         len = bv[i].bv_len;
437         }
438
439         len = LDIF_SIZE_NEEDED( type->bv_len, len ) + 1;
440         buf = (char *) ch_malloc( len );
441
442         for ( ; bv && bv->bv_val; bv++ ) {
443                 bufp = buf;
444                 ldif_sput( &bufp, LDIF_PUT_VALUE, type->bv_val,
445                                     bv->bv_val, bv->bv_len );
446                 *bufp = '\0';
447
448                 fputs( buf, fp );
449
450         }
451         free( buf );
452 }