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