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