]> git.sur5r.net Git - openldap/blob - servers/slurpd/re.c
s/SAFEMEMCPY/AC_MEMCPY/
[openldap] / servers / slurpd / re.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright (c) 1996 Regents of the University of Michigan.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms are permitted
7  * provided that this notice is preserved and that due credit is given
8  * to the University of Michigan at Ann Arbor. The name of the University
9  * may not be used to endorse or promote products derived from this
10  * software without specific prior written permission. This software
11  * is provided ``as is'' without express or implied warranty.
12  */
13
14 /* 
15  * re.c - routines which deal with Re (Replication entry) structures.
16  * An Re struct is an in-core representation of one replication to
17  * be performed, along with member functions which are called by other
18  * routines.  The Re struct is defined in slurp.h.
19  */
20
21
22 #include "portable.h"
23
24 #include <stdio.h>
25
26 #include <ac/errno.h>
27 #include <ac/socket.h>
28 #include <ac/string.h>
29 #include <ac/ctype.h>
30
31 #include "slurp.h"
32 #include "globals.h"
33
34 #include "../slapd/slap.h"
35
36 /* Forward references */
37 static Rh       *get_repl_hosts LDAP_P(( char *, int *, char ** ));
38 static int      gettype LDAP_P(( char * ));
39 static int      getchangetype LDAP_P(( char * ));
40 static int      Re_parse LDAP_P(( Re *re, char *replbuf ));
41 static void     Re_dump LDAP_P(( Re *re, FILE *fp ));
42 static void     warn_unknown_replica LDAP_P(( char *, int port ));
43
44 /* Globals, scoped within this file */
45 static int      nur = 0;        /* Number of unknown replicas */
46 static Rh       *ur = NULL;     /* array of unknown replica names */
47
48
49 /*
50  * Return the next Re in a linked list.
51  */
52 static Re *
53 Re_getnext(
54     Re  *re
55 )
56 {
57     return(( re == NULL ) ? NULL :  re->re_next );
58 }
59
60
61
62
63 /*
64  * Free an Re
65  * ??? Something should apparently return nonzero here, but I dont know what.
66  */
67 static int
68 Re_free(
69     Re  *re
70 )
71 {
72     Rh  *rh;
73     Mi  *mi;
74     int i;
75
76     if ( re == NULL ) {
77         return 0;
78     }
79     if ( re->re_refcnt > 0 ) {
80         Debug( LDAP_DEBUG_ANY,
81                 "Warning: freeing re (dn: %s) with nonzero refcnt\n",
82                 re->re_dn, 0, 0 );
83     }
84
85     ldap_pvt_thread_mutex_destroy( &re->re_mutex );
86
87     ch_free( re->re_timestamp );
88     if (( rh = re->re_replicas ) != NULL ) {
89         for ( i = 0; rh[ i ].rh_hostname != NULL; i++ ) {
90             free( rh[ i ].rh_hostname );
91         }
92         free( rh );
93     }
94     ch_free( re->re_dn );
95     if (( mi = re->re_mods ) != NULL ) {
96         for ( i = 0; mi[ i ].mi_type != NULL; i++ ) {
97             free( mi[ i ].mi_type );
98             ch_free( mi[ i ].mi_val );
99         }
100         free( mi );
101     }
102     free( re );
103     return 0;
104 }
105
106
107
108
109 /*
110  * Read a buffer of data from a replication log file and fill in
111  * an (already allocated) Re.
112  */
113
114 #define BEGIN           0
115 #define GOT_DN          1
116 #define GOT_TIME        2
117 #define GOT_CHANGETYPE  4
118 #define GOT_ALL         ( GOT_DN | GOT_TIME | GOT_CHANGETYPE )
119
120 static int
121 Re_parse(
122     Re          *re,
123     char        *replbuf
124 )
125 {
126     int                 state;
127     int                 nml;
128     char                *buf, *rp, *p;
129     size_t              buflen;
130     char                *type, *value;
131     ber_len_t   len;
132     int                 nreplicas;
133
134     if ( re == NULL ) {
135         Debug( LDAP_DEBUG_ANY, "Re_parse: error: re is NULL\n", 0, 0, 0 );
136         return -1;
137     }
138     if ( replbuf == NULL ) {
139         Debug( LDAP_DEBUG_ANY, "Re_parse: error: replbuf is NULL\n", 0, 0, 0 );
140         return -1;
141     }
142
143     state = BEGIN;
144     nml = 0;    /* number of modification information entries */
145     rp = replbuf;
146
147     re->re_replicas = get_repl_hosts( replbuf, &nreplicas, &rp );
148     re->re_refcnt = sglob->num_replicas;
149
150     for (;;) {
151         if (( state == GOT_ALL ) || ( buf = ldif_getline( &rp )) == NULL ) {
152             break;
153         }
154         /*
155          * If we're processing a rejection log, then the first line
156          * of each replication record will begin with "ERROR" - just
157          * ignore it.
158          */
159         if ( strncmp( buf, ERROR_STR, strlen( ERROR_STR )) == 0 ) {
160             continue;
161         }
162         buflen = strlen( buf );
163         if ( ldif_parse_line( buf, &type, &value, &len ) < 0 ) {
164             Debug( LDAP_DEBUG_ANY,
165                     "Error: Re_parse: malformed replog file\n",
166                     0, 0, 0 );
167             return -1;
168         }
169         switch ( gettype( type )) {
170         case T_CHANGETYPE:
171             re->re_changetype = getchangetype( value );
172             state |= GOT_CHANGETYPE;
173             break;
174         case T_TIME:
175             if (( p = strchr( value, '.' )) != NULL ) {
176                 /* there was a sequence number */
177                 *p++ = '\0';
178             }
179             re->re_timestamp = strdup( value );
180             if ( p != NULL && isdigit( (unsigned char) *p )) {
181                 re->re_seq = atoi( p );
182             }
183             state |= GOT_TIME;
184             break;
185         case T_DN:
186             re->re_dn = ch_malloc( len + 1 );
187                 AC_MEMCPY( re->re_dn, value, len );
188                 re->re_dn[ len ]='\0';
189             state |= GOT_DN;
190             break;
191         default:
192             if ( !( state == GOT_ALL )) {
193                 Debug( LDAP_DEBUG_ANY,
194                         "Error: Re_parse: bad type <%s>\n",
195                         type, 0, 0 );
196                 free( type );
197                 if ( value != NULL )
198                         free( value );
199                 return -1;
200             }
201         }
202         free( type );
203         if ( value != NULL )
204                 free( value );
205     }
206
207     if ( state != GOT_ALL ) {
208         Debug( LDAP_DEBUG_ANY,
209                 "Error: Re_parse: malformed replog file\n",
210                 0, 0, 0 );
211         return -1;
212     }
213
214     for (;;) {
215         char *const dash = "-";
216
217         if (( buf = ldif_getline( &rp )) == NULL ) {
218             break;
219         }
220         buflen = strlen( buf );
221         if (( buflen == 1 ) && ( buf[ 0 ] == '-' )) {
222             type  = dash;
223             value = NULL;
224         } else {
225             if ( ldif_parse_line( buf, &type, &value, &len ) < 0 ) {
226                 Debug( LDAP_DEBUG_ANY,
227                         "Error: malformed replog line \"%s\"\n",
228                         buf, 0, 0 );
229                 return -1;
230             }
231         }
232         re->re_mods = ( Mi  *) ch_realloc( (char *) re->re_mods,
233             sizeof( Mi ) * ( nml + 2 ));
234         re->re_mods[ nml ].mi_type = strdup( type );
235         if ( value != NULL ) {
236             re->re_mods[ nml ].mi_val = ch_malloc( len + 1 );
237                 AC_MEMCPY( re->re_mods[ nml ].mi_val, value, len );
238                 re->re_mods[ nml ].mi_val[ len ] = '\0';
239             re->re_mods[ nml ].mi_len = len;
240         } else {
241             re->re_mods[ nml ].mi_val = NULL;
242             re->re_mods[ nml ].mi_len = 0;
243         }
244         re->re_mods[ nml + 1 ].mi_type = NULL;
245         re->re_mods[ nml + 1 ].mi_val = NULL;
246         nml++;
247
248         if ( type != dash )
249                 free( type );
250         if ( value != NULL )
251                 free( value );
252     }
253     return 0;
254 }
255
256
257
258 /*
259  * Extract the replication hosts from a repl buf.  Check to be sure that
260  * each replica host and port number are ones we know about (that is, they're
261  * in the slapd config file we read at startup).  Without that information
262  * from the config file, we won't have the appropriate credentials to
263  * make modifications.  If there are any unknown replica names, don't
264  * add them the the Re struct.  Instead, log a warning message.
265  */
266 static Rh *
267 get_repl_hosts(
268     char        *replbuf,
269     int         *r_nreplicas,
270     char        **r_rp
271 )
272 {
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 }