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