]> git.sur5r.net Git - openldap/blob - servers/slurpd/re.c
05fd60743c9479b217e8ae098ed159d6cc74b666
[openldap] / servers / slurpd / re.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2005 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* Portions Copyright (c) 1996 Regents of the University of Michigan.
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms are permitted
19  * provided that this notice is preserved and that due credit is given
20  * to the University of Michigan at Ann Arbor. The name of the University
21  * may not be used to endorse or promote products derived from this
22  * software without specific prior written permission. This software
23  * is provided ``as is'' without express or implied warranty.
24  */
25 /* ACKNOWLEDGEMENTS:
26  * This work was originally developed by the University of Michigan
27  * (as part of U-MICH LDAP).
28  */
29
30 /* 
31  * re.c - routines which deal with Re (Replication entry) structures.
32  * An Re struct is an in-core representation of one replication to
33  * be performed, along with member functions which are called by other
34  * routines.  The Re struct is defined in slurp.h.
35  */
36
37
38 #include "portable.h"
39
40 #include <stdio.h>
41
42 #include <ac/stdlib.h>
43 #include <ac/errno.h>
44 #include <ac/socket.h>
45 #include <ac/string.h>
46 #include <ac/ctype.h>
47
48 #include "../slapd/slap.h"
49
50 #include "slurp.h"
51 #include "globals.h"
52
53 /* Forward references */
54 static Rh       *get_repl_hosts LDAP_P(( char *, int *, char ** ));
55 static int      gettype LDAP_P(( char * ));
56 static int      getchangetype LDAP_P(( char * ));
57 static int      Re_parse LDAP_P(( Re *re, char *replbuf ));
58 static void     Re_dump LDAP_P(( Re *re, FILE *fp ));
59 static void     warn_unknown_replica LDAP_P(( char *, int port ));
60
61 /* Globals, scoped within this file */
62 static int      nur = 0;        /* Number of unknown replicas */
63 static Rh       *ur = NULL;     /* array of unknown replica names */
64
65
66 /*
67  * Return the next Re in a linked list.
68  */
69 static Re *
70 Re_getnext(
71     Re  *re
72 )
73 {
74     return(( re == NULL ) ? NULL :  re->re_next );
75 }
76
77
78
79
80 /*
81  * Free an Re
82  * ??? Something should apparently return nonzero here, but I dont know what.
83  */
84 static int
85 Re_free(
86     Re  *re
87 )
88 {
89     Rh  *rh;
90     Mi  *mi;
91     int i;
92
93     if ( re == NULL ) {
94         return 0;
95     }
96     if ( re->re_refcnt > 0 ) {
97         Debug( LDAP_DEBUG_ANY,
98                 "Warning: freeing re (dn: %s) with nonzero refcnt\n",
99                 re->re_dn, 0, 0 );
100     }
101
102     ldap_pvt_thread_mutex_destroy( &re->re_mutex );
103
104     if (( rh = re->re_replicas ) != NULL ) {
105         for ( i = 0; rh[ i ].rh_hostname != NULL; i++ ) {
106             free( rh[ i ].rh_hostname );
107         }
108         free( rh );
109     }
110     ch_free( re->re_dn );
111     if (( mi = re->re_mods ) != NULL ) {
112         for ( i = 0; mi[ i ].mi_type != NULL; i++ ) {
113             free( mi[ i ].mi_type );
114             ch_free( mi[ i ].mi_val );
115         }
116         free( mi );
117     }
118     free( re );
119     return 0;
120 }
121
122
123
124
125 /*
126  * Read a buffer of data from a replication log file and fill in
127  * an (already allocated) Re.
128  */
129
130 #define BEGIN           0
131 #define GOT_DN          1
132 #define GOT_TIME        2
133 #define GOT_CHANGETYPE  4
134 #define GOT_ALL         ( GOT_DN | GOT_TIME | GOT_CHANGETYPE )
135
136 static int
137 Re_parse(
138     Re          *re,
139     char        *replbuf
140 )
141 {
142     int                 state;
143     int                 nml;
144     char                *buf, *rp, *p;
145     size_t              buflen;
146     char                *type, *value;
147     ber_len_t   len;
148     int                 nreplicas;
149
150     if ( re == NULL ) {
151         Debug( LDAP_DEBUG_ANY, "Re_parse: error: re is NULL\n", 0, 0, 0 );
152         return -1;
153     }
154     if ( replbuf == NULL ) {
155         Debug( LDAP_DEBUG_ANY, "Re_parse: error: replbuf is NULL\n", 0, 0, 0 );
156         return -1;
157     }
158
159     state = BEGIN;
160     nml = 0;    /* number of modification information entries */
161     rp = replbuf;
162
163     re->re_replicas = get_repl_hosts( replbuf, &nreplicas, &rp );
164     re->re_refcnt = sglob->num_replicas;
165
166     for (;;) {
167         if (( state == GOT_ALL ) || ( buf = ldif_getline( &rp )) == NULL ) {
168             break;
169         }
170         /*
171          * If we're processing a rejection log, then the first line
172          * of each replication record will begin with "ERROR" - just
173          * ignore it.
174          */
175         if ( strncmp( buf, ERROR_STR, strlen( ERROR_STR )) == 0 ) {
176             continue;
177         }
178         buflen = strlen( buf );
179         if ( ldif_parse_line( buf, &type, &value, &len ) < 0 ) {
180             Debug( LDAP_DEBUG_ANY,
181                     "Error: Re_parse: malformed replog file\n",
182                     0, 0, 0 );
183             return -1;
184         }
185         switch ( gettype( type )) {
186         case T_CHANGETYPE:
187             re->re_changetype = getchangetype( value );
188             state |= GOT_CHANGETYPE;
189             break;
190         case T_TIME:
191             if (( p = strchr( value, '.' )) != NULL ) {
192                 /* there was a sequence number */
193                 *p++ = '\0';
194             }
195             re->re_timestamp = atol( value );
196             if ( p != NULL && isdigit( (unsigned char) *p )) {
197                 re->re_seq = atoi( p );
198             }
199             state |= GOT_TIME;
200             break;
201         case T_DN:
202             re->re_dn = ch_malloc( len + 1 );
203                 AC_MEMCPY( re->re_dn, value, len );
204                 re->re_dn[ len ]='\0';
205             state |= GOT_DN;
206             break;
207         default:
208             if ( !( state == GOT_ALL )) {
209                 Debug( LDAP_DEBUG_ANY,
210                         "Error: Re_parse: bad type <%s>\n",
211                         type, 0, 0 );
212                 free( type );
213                 free( value );
214                 return -1;
215             }
216         }
217         free( type );
218         free( value );
219     }
220
221     if ( state != GOT_ALL ) {
222         Debug( LDAP_DEBUG_ANY,
223                 "Error: Re_parse: malformed replog file\n",
224                 0, 0, 0 );
225         return -1;
226     }
227
228     for (;;) {
229         char *const dash = "-";
230
231         if (( buf = ldif_getline( &rp )) == NULL ) {
232             break;
233         }
234         buflen = strlen( buf );
235         if (( buflen == 1 ) && ( buf[ 0 ] == '-' )) {
236             type  = dash;
237             value = NULL;
238         } else {
239             if ( ldif_parse_line( buf, &type, &value, &len ) < 0 ) {
240                 Debug( LDAP_DEBUG_ANY,
241                         "Error: malformed replog line \"%s\"\n",
242                         buf, 0, 0 );
243                 return -1;
244             }
245         }
246         re->re_mods = ( Mi  *) ch_realloc( (char *) re->re_mods,
247             sizeof( Mi ) * ( nml + 2 ));
248         re->re_mods[ nml ].mi_type = strdup( type );
249         if ( value != NULL ) {
250             re->re_mods[ nml ].mi_val = ch_malloc( len + 1 );
251                 AC_MEMCPY( re->re_mods[ nml ].mi_val, value, len );
252                 re->re_mods[ nml ].mi_val[ len ] = '\0';
253             re->re_mods[ nml ].mi_len = len;
254         } else {
255             re->re_mods[ nml ].mi_val = NULL;
256             re->re_mods[ nml ].mi_len = 0;
257         }
258         re->re_mods[ nml + 1 ].mi_type = NULL;
259         re->re_mods[ nml + 1 ].mi_val = NULL;
260         nml++;
261
262         if ( type != dash )
263                 free( type );
264         if ( value != NULL )
265                 free( value );
266     }
267     return 0;
268 }
269
270
271
272 /*
273  * Extract the replication hosts from a repl buf.  Check to be sure that
274  * each replica host and port number are ones we know about (that is, they're
275  * in the slapd config file we read at startup).  Without that information
276  * from the config file, we won't have the appropriate credentials to
277  * make modifications.  If there are any unknown replica names, don't
278  * add them the the Re struct.  Instead, log a warning message.
279  */
280 static Rh *
281 get_repl_hosts(
282     char        *replbuf,
283     int         *r_nreplicas,
284     char        **r_rp
285 )
286 {
287     char                *type, *value, *line, *p;
288     Rh                  *rh = NULL;
289     int                 nreplicas;
290         ber_len_t       len;
291     int                 port;
292     int                 repl_ok;
293     int                 i;
294
295     if ( replbuf == NULL ) {
296         return( NULL );
297     }
298
299     nreplicas = 0;
300
301     /*
302      * Get the host names of the replicas
303      */
304     *r_nreplicas = 0;
305     *r_rp = replbuf;
306     for (;;) {
307         /* If this is a reject log, we need to skip over the ERROR: line */
308         if ( !strncmp( *r_rp, ERROR_STR, strlen( ERROR_STR ))) {
309             line = ldif_getline( r_rp );
310             if ( line == NULL ) {
311                 break;
312             }
313         }
314         if ( strncasecmp( *r_rp, "replica:", 7 )) {
315             break;
316         }
317         line = ldif_getline( r_rp );
318         if ( line == NULL ) {
319             break;
320         }
321         if ( ldif_parse_line( line, &type, &value, &len ) < 0 ) {
322             return( NULL );
323         }
324         port = 0;
325         if (( p = strchr( value, ':' )) != NULL ) {
326             *p = '\0';
327             p++;
328             if ( *p != '\0' ) {
329                 port = atoi( p );
330             }
331         }
332
333         /* Verify that we've heard of this replica before */
334         repl_ok = 0;
335         for ( i = 0; i < sglob->num_replicas; i++ ) {
336             if ( strcmp( sglob->replicas[ i ]->ri_hostname, value )) {
337                 continue;
338             }
339             if ( sglob->replicas[ i ]->ri_port == port ) {
340                 repl_ok = 1;
341                 break;
342             }
343         }
344         free( type );
345         if ( !repl_ok ) {
346             warn_unknown_replica( value, port );
347             free( value );
348             continue;
349         }
350
351         rh = (Rh *) ch_realloc((char *) rh, ( nreplicas + 2 ) * sizeof( Rh ));
352         if ( rh == NULL ) {
353             Debug( LDAP_DEBUG_ANY, "Out of memory in get_repl_hosts\n",
354                     0, 0, 0 );
355             return NULL;
356         }
357         rh[ nreplicas ].rh_hostname = strdup( value );
358         rh[ nreplicas ].rh_port = port;
359         nreplicas++;
360
361         free( value );
362     }
363
364     if ( nreplicas == 0 ) {
365         return( NULL );
366     }
367
368     rh[ nreplicas ].rh_hostname = NULL;
369     *r_nreplicas = nreplicas;
370
371     return( rh );
372 }
373
374
375
376
377
378 /*
379  * Convert "type" to an int.
380  */
381 static int
382 gettype(
383     char        *type
384 )
385 {
386     if ( !strcmp( type, T_CHANGETYPESTR )) {
387         return( T_CHANGETYPE );
388     }
389     if ( !strcmp( type, T_TIMESTR )) {
390         return( T_TIME );
391     }
392     if ( !strcmp( type, T_DNSTR )) {
393         return( T_DN );
394     }
395     return( T_ERR );
396 }
397
398
399
400 /*
401  * Convert "changetype" to an int.
402  */
403 static int
404 getchangetype(
405     char        *changetype
406 )
407 {
408     if ( !strcmp( changetype, T_ADDCTSTR )) {
409         return( T_ADDCT );
410     }
411     if ( !strcmp( changetype, T_MODIFYCTSTR )) {
412         return( T_MODIFYCT );
413     }
414     if ( !strcmp( changetype, T_DELETECTSTR )) {
415         return( T_DELETECT );
416     }
417     if ( !strcmp( changetype, T_MODRDNCTSTR )) {
418         return( T_MODRDNCT );
419     }
420     return( T_ERR );
421 }
422
423
424
425
426 /*
427  * Find the first line which is not a "replica:" line in buf.
428  * Returns a pointer to the line.  Returns NULL if there are
429  * only "replica:" lines in buf.
430  */
431 static char *
432 skip_replica_lines(
433     char        *buf
434 )
435 {
436     char *p = buf;
437     for (;;) {
438         if ( strncasecmp( p, "replica:", 8 )) {
439             return( p );
440         }
441         while (( *p != '\0' )  && ( *p != '\n' )) {
442             p++;
443         }
444         if ( *p == '\0' ) {
445             return ( NULL );
446         } else {
447             p++;
448         }
449     }
450 }
451
452
453
454
455 /*
456  * For debugging purposes: dump the contents of a replication entry.
457  * to the given stream.
458  */
459 static void
460 Re_dump(
461     Re          *re,
462     FILE        *fp
463 )
464 {
465     int i;
466     Mi *mi;
467
468     if ( re == NULL ) {
469         Debug( LDAP_DEBUG_TRACE, "Re_dump: re is NULL\n", 0, 0, 0 );
470         return;
471     }
472     fprintf( fp, "Re_dump: ******\n" );
473     fprintf( fp, "re_refcnt: %d\n", re->re_refcnt );
474     fprintf( fp, "re_timestamp: %ld\n", (long) re->re_timestamp );
475     fprintf( fp, "re_seq: %d\n", re->re_seq );
476     for ( i = 0; re->re_replicas && re->re_replicas[ i ].rh_hostname != NULL;
477                 i++ ) {
478         fprintf( fp, "re_replicas[%d]: %s:%d\n", 
479                 i, re->re_replicas[ i ].rh_hostname,
480                 re->re_replicas[ i ].rh_port );
481     }
482     fprintf( fp, "re_dn: %s\n", re->re_dn );
483     switch ( re->re_changetype ) {
484     case T_ADDCT:
485         fprintf( fp, "re_changetype: add\n" );
486         break;
487     case T_MODIFYCT:
488         fprintf( fp, "re_changetype: modify\n" );
489         break;
490     case T_DELETECT:
491         fprintf( fp, "re_changetype: delete\n" );
492         break;
493     case T_MODRDNCT:
494         fprintf( fp, "re_changetype: modrdn\n" );
495         break;
496     default:
497         fprintf( fp, "re_changetype: (unknown, type = %d\n",
498                 re->re_changetype );
499     }
500     if ( re->re_mods == NULL ) {
501         fprintf( fp, "re_mods: (none)\n" );
502     } else {
503         mi = re->re_mods;
504         fprintf( fp, "re_mods:\n" );
505         for ( i = 0; mi[ i ].mi_type != NULL; i++ ) {
506             fprintf( fp, "  %s, \"%s\", (%d bytes)\n",
507                     mi[ i ].mi_type,
508                     mi[ i ].mi_val == NULL ?  "(null)" : mi[ i ].mi_val,
509                     mi[ i ].mi_len );
510         }
511     }
512     return;
513 }
514
515
516 /* 
517  * Given an Ri, an Re, and a file pointer, write a replication record to
518  * the file pointer.  If ri is NULL, then include all replicas in the
519  * output.  If ri is non-NULL, then only include a single "replica:" line
520  * (used when writing rejection records).  Returns 0 on success, -1
521  * on failure.  Note that Re_write will not write anything out if the
522  * refcnt is zero.
523  */
524 static int
525 Re_write(
526     Ri          *ri,
527     Re          *re,
528     FILE        *fp )
529 {
530     int         i;
531     char        *s;
532     int         rc = 0;
533
534     if ( re == NULL || fp == NULL ) {
535         Debug( LDAP_DEBUG_ANY, "Internal error: Re_write: NULL argument\n",
536                 0, 0, 0 );
537         return -1;
538     }
539
540     if ( re->re_refcnt < 1 ) {
541         return 0;               /* this is not an error */
542     }
543
544     if ( ri != NULL ) {         /* write a single "replica:" line */
545         if ( fprintf( fp, "replica: %s:%d\n", ri->ri_hostname,
546                 ri->ri_port ) < 0 ) {
547             rc = -1;
548             goto bad;
549         }
550     } else {                    /* write multiple "replica:" lines */
551         for ( i = 0; re->re_replicas && re->re_replicas[ i ].rh_hostname != NULL; i++ ) {
552             if ( fprintf( fp, "replica: %s:%d\n",
553                     re->re_replicas[ i ].rh_hostname,
554                     re->re_replicas[ i ].rh_port ) < 0 ) {
555                 rc = -1;
556                 goto bad;
557             }
558         }
559     }
560     if ( fprintf( fp, "time: %ld.%d\n", (long) re->re_timestamp, re->re_seq ) < 0 ) {
561         rc = -1;
562         goto bad;
563     }
564     if ( fprintf( fp, "dn: %s\n", re->re_dn ) < 0 ) {
565         rc = -1;
566         goto bad;
567     }
568     if ( fprintf( fp, "changetype: " ) < 0 ) {
569         rc = -1;
570         goto bad;
571     }
572     switch ( re->re_changetype ) {
573     case T_ADDCT:
574         s = T_ADDCTSTR;
575         break;
576     case T_MODIFYCT:
577         s = T_MODIFYCTSTR;
578         break;
579     case T_DELETECT:
580         s = T_DELETECTSTR;
581         break;
582     case T_MODRDNCT:
583         s = T_MODRDNCTSTR;
584         break;
585     default:
586         s = "IllegalModifyType!!!";
587     }
588     if ( fprintf( fp, "%s\n", s ) < 0 ) {
589         rc = -1;
590         goto bad;
591     }
592     for ( i = 0; (( re->re_mods != NULL ) &&
593             ( re->re_mods[ i ].mi_type != NULL )); i++ ) {
594         if ( !strcmp( re->re_mods[ i ].mi_type, T_MODSEPSTR )) {
595             if ( fprintf( fp, "%s\n", T_MODSEPSTR ) < 0 ) {
596                 rc = -1;
597                 goto bad;
598             }
599         } else {
600             char *obuf;
601             obuf = ldif_put( LDIF_PUT_VALUE,
602                         re->re_mods[ i ].mi_type,
603                     re->re_mods[ i ].mi_val ? re->re_mods[ i ].mi_val : "",
604                     re->re_mods[ i ].mi_len );
605             if ( fputs( obuf, fp ) < 0 ) {
606                 rc = -1;
607                 free( obuf );
608                 goto bad;
609             } else {
610                 ber_memfree( obuf );
611             }
612         }
613     }
614     if ( fprintf( fp, "\n" ) < 0 ) {
615         rc = -1;
616         goto bad;
617     }
618     if ( fflush( fp ) != 0 ) {
619         rc = -1;
620         goto bad;
621     }
622 bad:
623     if ( rc != 0 ) {
624         Debug( LDAP_DEBUG_ANY, "Error while writing: %s\n",
625                 sys_errlist[ errno ], 0, 0 );
626     }
627     return rc;
628 }
629
630
631
632
633 /*
634  * Decrement the refcnt.  Locking handled internally.
635  */
636 static int
637 Re_decrefcnt(
638     Re  *re
639 )
640 {
641     re->re_lock( re );
642     re->re_refcnt--;
643     re->re_unlock( re );
644     return 0;
645 }
646
647
648
649 /*
650  * Get the refcnt.  Locking handled internally.
651  */
652 static int
653 Re_getrefcnt(
654     Re  *re
655 )
656 {
657     int ret;
658
659     re->re_lock( re );
660     ret = re->re_refcnt;
661     re->re_unlock( re );
662     return ret;
663 }
664     
665     
666
667
668
669 /*
670  * Lock this replication entry
671  */
672 static int
673 Re_lock(
674     Re  *re
675 )
676 {
677     return( ldap_pvt_thread_mutex_lock( &re->re_mutex ));
678 }
679
680
681
682
683 /*
684  * Unlock this replication entry
685  */
686 static int
687 Re_unlock(
688     Re  *re
689 )
690 {
691     return( ldap_pvt_thread_mutex_unlock( &re->re_mutex ));
692 }
693
694
695
696
697 /* 
698  * Instantiate and initialize an Re.
699  */
700 int
701 Re_init(
702     Re **re
703 )
704 {
705     /* Instantiate */
706     (*re) = (Re *) malloc( sizeof( Re ));
707     if ( *re == NULL ) {
708         return -1;
709     }
710
711     /* Fill in the member function pointers */
712     (*re)->re_free = Re_free;
713     (*re)->re_getnext = Re_getnext;
714     (*re)->re_parse = Re_parse;
715     (*re)->re_write = Re_write;
716     (*re)->re_dump = Re_dump;
717     (*re)->re_lock = Re_lock;
718     (*re)->re_unlock = Re_unlock;
719     (*re)->re_decrefcnt = Re_decrefcnt;
720     (*re)->re_getrefcnt = Re_getrefcnt;
721
722     /* Initialize private data */
723    (*re)->re_refcnt = sglob->num_replicas;
724    (*re)->re_timestamp = (time_t) 0L;
725    (*re)->re_replicas = NULL;
726    (*re)->re_dn = NULL;
727    (*re)->re_changetype = 0;
728    (*re)->re_seq = 0;
729    (*re)->re_mods = NULL;
730    (*re)->re_next = NULL;
731
732    ldap_pvt_thread_mutex_init( &((*re)->re_mutex) );
733    return 0;
734 }
735
736
737
738
739 /*
740  * Given a host and port, generate a warning message iff we haven't already
741  * generated a message for this host:port combination.
742  */
743 static void
744 warn_unknown_replica( 
745     char        *host,
746     int         port
747 )
748 {
749     int found = 0;
750     int i;
751
752     for ( i = 0; i < nur; i++ ) {
753         if ( strcmp( ur[ i ].rh_hostname, host )) {
754             continue;
755         }
756         if ( ur[ i ].rh_port == port ) {
757             found = 1;
758             break;
759         }
760     }
761     if ( !found ) {
762         Debug( LDAP_DEBUG_ANY,
763                 "Warning: unknown replica %s:%d found in replication log\n",
764                 host, port, 0 );
765         nur++;
766         ur = (Rh *) ch_realloc( (char *) ur, ( nur * sizeof( Rh )));
767         ur[ nur - 1 ].rh_hostname = strdup( host );
768         ur[ nur - 1 ].rh_port = port;
769     }
770 }