]> git.sur5r.net Git - openldap/blob - servers/slapd/repl.c
line up comments and code
[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;
231
232                                         is_in = ad_inlist( ml->sml_desc, op->o_bd->be_replica[i]->ri_attrs );
233                                         exclude = op->o_bd->be_replica[i]->ri_exclude;
234                                         
235                                         /*
236                                          * there might be a more clever way to do this test,
237                                          * but this way, at least, is comprehensible :)
238                                          */
239                                         if ( ( is_in && !exclude ) || ( !is_in && exclude ) ) {
240                                                 subsets = 1;
241                                                 first = ml;
242                                                 break;
243                                         }
244                                 }
245                                 if ( !subsets ) {
246                                         continue;
247                                 }
248                                 break;
249                         case LDAP_REQ_ADD:
250                                 for ( a = op->ora_e->e_attrs; a != NULL; a = a->a_next ) {
251                                         int is_in, exclude;
252
253                                         is_in = ad_inlist( a->a_desc, op->o_bd->be_replica[i]->ri_attrs );
254                                         exclude = op->o_bd->be_replica[i]->ri_exclude;
255                                         
256                                         if ( ( is_in && !exclude ) || ( !is_in && exclude ) ) {
257                                                 subsets = 1;
258                                                 first = a;
259                                                 break;
260                                         }
261                                 }
262                                 if ( !subsets ) {
263                                         continue;
264                                 }
265                                 break;
266                         default:
267                                 /* Other operations were logged in the first pass */
268                                 continue;
269                         }
270                         fprintf( fp, "replica: %s\n", op->o_bd->be_replica[i]->ri_host );
271                         fprintf( fp, "time: %ld\n", now );
272                         fprintf( fp, "dn: %s\n", op->o_req_dn.bv_val );
273                         replog1( op->o_bd->be_replica[i], op, fp, first );
274                 }
275         }
276
277         lock_fclose( fp, lfp );
278         ldap_pvt_thread_mutex_unlock( &replog_mutex );
279 }
280
281
282 static void
283 replog1(
284     struct slap_replica_info *ri,
285     Operation *op,
286     FILE        *fp,
287         void    *first
288 )
289 {
290         Modifications   *ml;
291         Attribute       *a;
292
293         switch ( op->o_tag ) {
294         case LDAP_REQ_EXTENDED:
295                 /* quick hack for extended operations */
296                 /* assume change parameter is a Modfications* */
297                 /* fall thru */
298
299         case LDAP_REQ_MODIFY:
300                 fprintf( fp, "changetype: modify\n" );
301                 ml = first ? first : op->orm_modlist;
302                 for ( ; ml != NULL; ml = ml->sml_next ) {
303                         char *type;
304                         if ( ri && ri->ri_attrs ) {
305                                 int is_in = ad_inlist( ml->sml_desc, ri->ri_attrs );
306
307                                 if ( ( !is_in && !ri->ri_exclude )
308                                         || ( is_in && ri->ri_exclude ) )
309                                 {
310                                         continue;
311                                 }
312                         }
313                         type = ml->sml_desc->ad_cname.bv_val;
314                         switch ( ml->sml_op ) {
315                         case LDAP_MOD_ADD:
316                                 fprintf( fp, "add: %s\n", type );
317                                 break;
318
319                         case LDAP_MOD_DELETE:
320                                 fprintf( fp, "delete: %s\n", type );
321                                 break;
322
323                         case LDAP_MOD_REPLACE:
324                                 fprintf( fp, "replace: %s\n", type );
325                                 break;
326
327                         case LDAP_MOD_INCREMENT:
328                                 fprintf( fp, "increment: %s\n", type );
329                                 break;
330                         }
331                         if ( ml->sml_bvalues ) {
332                                 print_vals( fp, &ml->sml_desc->ad_cname, ml->sml_bvalues );
333                         }
334                         fprintf( fp, "-\n" );
335                 }
336                 break;
337
338         case LDAP_REQ_ADD:
339                 fprintf( fp, "changetype: add\n" );
340                 a = first ? first : op->ora_e->e_attrs;
341                 for ( ; a != NULL; a=a->a_next ) {
342                         if ( ri && ri->ri_attrs ) {
343                                 int is_in = ad_inlist( a->a_desc, ri->ri_attrs );
344                                 if ( ( !is_in && !ri->ri_exclude ) || ( is_in && ri->ri_exclude ) ) {
345                                         continue;
346                                 }
347
348                                 /* If the list includes objectClass names,
349                                  * only include those classes in the
350                                  * objectClass attribute
351                                  */
352                                 if ( a->a_desc == slap_schema.si_ad_objectClass ) {
353                                         int ocs = 0;
354                                         AttributeName *an;
355                                         struct berval vals[2];
356                                         vals[1].bv_val = NULL;
357                                         vals[1].bv_len = 0;
358                                         for ( an = ri->ri_attrs; an->an_name.bv_val; an++ ) {
359                                                 if ( an->an_oc ) {
360                                                         int i;
361
362                                                         for ( i=0; a->a_vals[i].bv_val; i++ ) {
363                                                                 if ( an->an_oc_exclude ) {
364                                                                         if ( a->a_vals[i].bv_len != an->an_name.bv_len
365                                                                                 || strcasecmp(a->a_vals[i].bv_val,
366                                                                                         an->an_name.bv_val ) ) {
367                                                                                 ocs = 1;
368                                                                         }
369
370                                                                 } else {
371                                                                         if ( a->a_vals[i].bv_len == an->an_name.bv_len
372                                                                                 && !strcasecmp(a->a_vals[i].bv_val,
373                                                                                         an->an_name.bv_val ) ) {
374                                                                                 ocs = 1;
375                                                                         }
376                                                                 }
377
378                                                                 if ( ocs )  {
379                                                                         vals[0] = an->an_name;
380                                                                         print_vals( fp, &a->a_desc->ad_cname, vals );
381                                                                         break;
382                                                                 }
383                                                         }
384                                                 }
385                                         }
386                                         if ( ocs ) continue;
387                                 }
388                         }
389                         print_vals( fp, &a->a_desc->ad_cname, a->a_vals );
390                 }
391                 break;
392
393         case LDAP_REQ_DELETE:
394                 fprintf( fp, "changetype: delete\n" );
395                 break;
396
397         case LDAP_REQ_MODRDN:
398                 fprintf( fp, "changetype: modrdn\n" );
399                 fprintf( fp, "newrdn: %s\n", op->orr_newrdn.bv_val );
400                 fprintf( fp, "deleteoldrdn: %d\n", op->orr_deleteoldrdn ? 1 : 0 );
401                 if( op->orr_newSup != NULL ) {
402                         fprintf( fp, "newsuperior: %s\n", op->orr_newSup->bv_val );
403                 }
404         }
405         fprintf( fp, "\n" );
406 }
407
408 static void
409 print_vals(
410         FILE *fp,
411         struct berval *type,
412         struct berval *bv )
413 {
414         ber_len_t i, len;
415         char    *buf, *bufp;
416
417         for ( i = 0, len = 0; bv && bv[i].bv_val; i++ ) {
418                 if ( bv[i].bv_len > len )
419                         len = bv[i].bv_len;
420         }
421
422         len = LDIF_SIZE_NEEDED( type->bv_len, len ) + 1;
423         buf = (char *) ch_malloc( len );
424
425         for ( ; bv && bv->bv_val; bv++ ) {
426                 bufp = buf;
427                 ldif_sput( &bufp, LDIF_PUT_VALUE, type->bv_val,
428                                     bv->bv_val, bv->bv_len );
429                 *bufp = '\0';
430
431                 fputs( buf, fp );
432
433         }
434         free( buf );
435 }