]> git.sur5r.net Git - openldap/blob - servers/slapd/repl.c
ITS#3353 consolidate slapd globals into a single struct
[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-2004 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 && frontendDB->be_replogfile == NULL ) {
139                 return;
140         }
141
142         ldap_pvt_thread_mutex_lock( &SLAPD_GLOBAL(replog_mutex) );
143         if ( (fp = lock_fopen( op->o_bd->be_replogfile ? op->o_bd->be_replogfile :
144             frontendDB->be_replogfile, "a", &lfp )) == NULL ) {
145                 ldap_pvt_thread_mutex_unlock( &SLAPD_GLOBAL(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( &SLAPD_GLOBAL(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( &SLAPD_GLOBAL(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, ocs = -1;
264         struct berval vals[2];
265
266         vals[1].bv_val = NULL;
267         vals[1].bv_len = 0;
268
269         switch ( op->o_tag ) {
270         case LDAP_REQ_EXTENDED:
271                 /* quick hack for extended operations */
272                 /* assume change parameter is a Modifications* */
273                 /* fall thru */
274
275         case LDAP_REQ_MODIFY:
276                 for ( ml = op->orm_modlist; ml != NULL; ml = ml->sml_next ) {
277                         char *did, *type = ml->sml_desc->ad_cname.bv_val;
278                         switch ( ml->sml_op ) {
279                         case LDAP_MOD_ADD:
280                                 did = "add"; break;
281
282                         case LDAP_MOD_DELETE:
283                                 did = "delete"; break;
284
285                         case LDAP_MOD_REPLACE:
286                                 did = "replace"; break;
287
288                         case LDAP_MOD_INCREMENT:
289                                 did = "increment"; break;
290                         }
291                         if ( ri && ri->ri_attrs ) {
292                                 int is_in = ad_inlist( ml->sml_desc, ri->ri_attrs );
293
294                                 if ( ( !is_in && !ri->ri_exclude )
295                                         || ( is_in && ri->ri_exclude ) )
296                                 {
297                                         continue;
298                                 }
299                                 /* If this is objectClass, see if the value is included
300                                  * in any subset, otherwise drop it.
301                                  */
302                                 if ( ocs && ml->sml_desc == slap_schema.si_ad_objectClass
303                                         && ml->sml_values )
304                                 {
305                                         int i, first = 1;
306
307                                         if ( ocs == -1 ) ocs = 0;
308
309                                         for ( i=0; ml->sml_values[i].bv_val; i++ ) {
310                                                 int match = 0;
311                                                 for ( an = ri->ri_attrs; an->an_name.bv_val; an++ ) {
312                                                         if ( an->an_oc ) {
313                                                                 ocs = 1;
314                                                                 match |= an->an_oc_exclude;
315                                                                 if ( ml->sml_values[i].bv_len == an->an_name.bv_len
316                                                                         && !strcasecmp(ml->sml_values[i].bv_val,
317                                                                                 an->an_name.bv_val ) ) {
318                                                                         match = !an->an_oc_exclude;
319                                                                         break;
320                                                                 }
321                                                         }
322                                                 }
323                                                 /* Objectclasses need no special treatment, drop into
324                                                  * regular processing
325                                                  */
326                                                 if ( !ocs ) break;
327
328                                                 match ^= ri->ri_exclude;
329                                                 /* Found a match, log it */
330                                                 if ( match ) {
331                                                         if ( dohdr ) {
332                                                                 rephdr( ri, op, fp, now );
333                                                                 fprintf( fp, "changetype: modify\n" );
334                                                                 dohdr = 0;
335                                                         }
336                                                         if ( first ) {
337                                                                 fprintf( fp, "%s: %s\n", did, type );
338                                                                 first = 0;
339                                                         }
340                                                         vals[0] = an->an_name;
341                                                         print_vals( fp, &ml->sml_desc->ad_cname, vals );
342                                                         ocs = 2;
343                                                 }
344
345                                         }
346                                         /* Explicit objectclasses have been handled already */
347                                         if ( ocs ) {
348                                                 if ( ocs == 2 ) {
349                                                         fprintf( fp, "-\n" );
350                                                 }
351                                                 continue;
352                                         }
353                                 }
354                         }
355                         if ( dohdr ) {
356                                 rephdr( ri, op, fp, now );
357                                 fprintf( fp, "changetype: modify\n" );
358                                 dohdr = 0;
359                         }
360                         fprintf( fp, "%s: %s\n", did, type );
361                         if ( ml->sml_values ) {
362                                 print_vals( fp, &ml->sml_desc->ad_cname, ml->sml_values );
363                         }
364                         fprintf( fp, "-\n" );
365                 }
366                 break;
367
368         case LDAP_REQ_ADD:
369                 for ( a = op->ora_e->e_attrs ; a != NULL; a=a->a_next ) {
370                         if ( ri && ri->ri_attrs ) {
371                                 int is_in = ad_inlist( a->a_desc, ri->ri_attrs );
372                                 if ( ( !is_in && !ri->ri_exclude ) || ( is_in && ri->ri_exclude ) ) {
373                                         continue;
374                                 }
375
376                                 /* If the list includes objectClass names,
377                                  * only include those classes in the
378                                  * objectClass attribute
379                                  */
380                                 if ( ocs && a->a_desc == slap_schema.si_ad_objectClass ) {
381                                         int i;
382
383                                         if ( ocs == -1 ) ocs = 0;
384
385                                         for ( i=0; a->a_vals[i].bv_val; i++ ) {
386                                                 int match = 0;
387                                                 for ( an = ri->ri_attrs; an->an_name.bv_val; an++ ) {
388                                                         if ( an->an_oc ) {
389                                                                 ocs = 1;
390                                                                 match |= an->an_oc_exclude;
391                                                                 if ( a->a_vals[i].bv_len == an->an_name.bv_len
392                                                                         && !strcasecmp(a->a_vals[i].bv_val,
393                                                                                 an->an_name.bv_val ) ) {
394                                                                         match = !an->an_oc_exclude;
395                                                                         break;
396                                                                 }
397                                                         }
398                                                 }
399                                                 if ( !ocs ) break;
400
401                                                 match ^= ri->ri_exclude;
402                                                 if ( match ) {
403                                                         if ( dohdr ) {
404                                                                 rephdr( ri, op, fp, now );
405                                                                 fprintf( fp, "changetype: add\n" );
406                                                                 dohdr = 0;
407                                                         }
408                                                         vals[0] = an->an_name;
409                                                         print_vals( fp, &a->a_desc->ad_cname, vals );
410                                                 }
411                                         }
412                                         if ( ocs ) continue;
413                                 }
414                         }
415                         if ( dohdr ) {
416                                 rephdr( ri, op, fp, now );
417                                 fprintf( fp, "changetype: add\n" );
418                                 dohdr = 0;
419                         }
420                         print_vals( fp, &a->a_desc->ad_cname, a->a_vals );
421                 }
422                 break;
423
424         case LDAP_REQ_DELETE:
425                 rephdr( ri, op, fp, now );
426                 fprintf( fp, "changetype: delete\n" );
427                 break;
428
429         case LDAP_REQ_MODRDN:
430                 rephdr( ri, op, fp, now );
431                 fprintf( fp, "changetype: modrdn\n" );
432                 fprintf( fp, "newrdn: %s\n", op->orr_newrdn.bv_val );
433                 fprintf( fp, "deleteoldrdn: %d\n", op->orr_deleteoldrdn ? 1 : 0 );
434                 if( op->orr_newSup != NULL ) {
435                         fprintf( fp, "newsuperior: %s\n", op->orr_newSup->bv_val );
436                 }
437         }
438         fprintf( fp, "\n" );
439 }
440
441 static void
442 print_vals(
443         FILE *fp,
444         struct berval *type,
445         struct berval *bv )
446 {
447         ber_len_t i, len;
448         char    *buf, *bufp;
449
450         for ( i = 0, len = 0; bv && bv[i].bv_val; i++ ) {
451                 if ( bv[i].bv_len > len )
452                         len = bv[i].bv_len;
453         }
454
455         len = LDIF_SIZE_NEEDED( type->bv_len, len ) + 1;
456         buf = (char *) ch_malloc( len );
457
458         for ( ; bv && bv->bv_val; bv++ ) {
459                 bufp = buf;
460                 ldif_sput( &bufp, LDIF_PUT_VALUE, type->bv_val,
461                                     bv->bv_val, bv->bv_len );
462                 *bufp = '\0';
463
464                 fputs( buf, fp );
465
466         }
467         free( buf );
468 }