]> git.sur5r.net Git - openldap/blob - servers/slurpd/re.c
d95da212eddea23d9b9666eae097200700ff86db
[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                *type, *value, *line, *p;
273     Rh                  *rh = NULL;
274     int                 nreplicas;
275         ber_len_t       len;
276     int                 port;
277     int                 repl_ok;
278     int                 i;
279
280     if ( replbuf == NULL ) {
281         return( NULL );
282     }
283
284     nreplicas = 0;
285
286     /*
287      * Get the host names of the replicas
288      */
289     *r_nreplicas = 0;
290     *r_rp = replbuf;
291     for (;;) {
292         /* If this is a reject log, we need to skip over the ERROR: line */
293         if ( !strncmp( *r_rp, ERROR_STR, strlen( ERROR_STR ))) {
294             line = ldif_getline( r_rp );
295             if ( line == NULL ) {
296                 break;
297             }
298         }
299         if ( strncasecmp( *r_rp, "replica:", 7 )) {
300             break;
301         }
302         line = ldif_getline( r_rp );
303         if ( line == NULL ) {
304             break;
305         }
306         if ( ldif_parse_line( line, &type, &value, &len ) < 0 ) {
307             return( NULL );
308         }
309         port = 0;
310         if (( p = strchr( value, ':' )) != NULL ) {
311             *p = '\0';
312             p++;
313             if ( *p != '\0' ) {
314                 port = atoi( p );
315             }
316         }
317
318         /* Verify that we've heard of this replica before */
319         repl_ok = 0;
320         for ( i = 0; i < sglob->num_replicas; i++ ) {
321             if ( strcmp( sglob->replicas[ i ]->ri_hostname, value )) {
322                 continue;
323             }
324             if ( sglob->replicas[ i ]->ri_port == port ) {
325                 repl_ok = 1;
326                 break;
327             }
328         }
329         free( type );
330         if ( !repl_ok ) {
331             warn_unknown_replica( value, port );
332             if ( value != NULL )
333                 free( value );
334             continue;
335         }
336
337         rh = (Rh *) ch_realloc((char *) rh, ( nreplicas + 2 ) * sizeof( Rh ));
338         if ( rh == NULL ) {
339             Debug( LDAP_DEBUG_ANY, "Out of memory in get_repl_hosts\n",
340                     0, 0, 0 );
341             return NULL;
342         }
343         rh[ nreplicas ].rh_hostname = strdup( value );
344         rh[ nreplicas ].rh_port = port;
345         nreplicas++;
346
347         if ( value != NULL )
348                 free( value );
349     }
350
351     if ( nreplicas == 0 ) {
352         return( NULL );
353     }
354
355     rh[ nreplicas ].rh_hostname = NULL;
356     *r_nreplicas = nreplicas;
357
358     return( rh );
359 }
360
361
362
363
364
365 /*
366  * Convert "type" to an int.
367  */
368 static int
369 gettype(
370     char        *type
371 )
372 {
373     if ( !strcmp( type, T_CHANGETYPESTR )) {
374         return( T_CHANGETYPE );
375     }
376     if ( !strcmp( type, T_TIMESTR )) {
377         return( T_TIME );
378     }
379     if ( !strcmp( type, T_DNSTR )) {
380         return( T_DN );
381     }
382     return( T_ERR );
383 }
384
385
386
387 /*
388  * Convert "changetype" to an int.
389  */
390 static int
391 getchangetype(
392     char        *changetype
393 )
394 {
395     if ( !strcmp( changetype, T_ADDCTSTR )) {
396         return( T_ADDCT );
397     }
398     if ( !strcmp( changetype, T_MODIFYCTSTR )) {
399         return( T_MODIFYCT );
400     }
401     if ( !strcmp( changetype, T_DELETECTSTR )) {
402         return( T_DELETECT );
403     }
404     if ( !strcmp( changetype, T_MODRDNCTSTR )) {
405         return( T_MODRDNCT );
406     }
407     return( T_ERR );
408 }
409
410
411
412
413 /*
414  * Find the first line which is not a "replica:" line in buf.
415  * Returns a pointer to the line.  Returns NULL if there are
416  * only "replica:" lines in buf.
417  */
418 static char *
419 skip_replica_lines(
420     char        *buf
421 )
422 {
423     char *p = buf;
424     for (;;) {
425         if ( strncasecmp( p, "replica:", 8 )) {
426             return( p );
427         }
428         while (( *p != '\0' )  && ( *p != '\n' )) {
429             p++;
430         }
431         if ( *p == '\0' ) {
432             return ( NULL );
433         } else {
434             p++;
435         }
436     }
437 }
438
439
440
441
442 /*
443  * For debugging purposes: dump the contents of a replication entry.
444  * to the given stream.
445  */
446 static void
447 Re_dump(
448     Re          *re,
449     FILE        *fp
450 )
451 {
452     int i;
453     Mi *mi;
454
455     if ( re == NULL ) {
456         Debug( LDAP_DEBUG_TRACE, "Re_dump: re is NULL\n", 0, 0, 0 );
457         return;
458     }
459     fprintf( fp, "Re_dump: ******\n" );
460     fprintf( fp, "re_refcnt: %d\n", re->re_refcnt );
461     fprintf( fp, "re_timestamp: %s\n", re->re_timestamp );
462     fprintf( fp, "re_seq: %d\n", re->re_seq );
463     for ( i = 0; re->re_replicas && re->re_replicas[ i ].rh_hostname != NULL;
464                 i++ ) {
465         fprintf( fp, "re_replicas[%d]: %s:%d\n", 
466                 i, re->re_replicas[ i ].rh_hostname,
467                 re->re_replicas[ i ].rh_port );
468     }
469     fprintf( fp, "re_dn: %s\n", re->re_dn );
470     switch ( re->re_changetype ) {
471     case T_ADDCT:
472         fprintf( fp, "re_changetype: add\n" );
473         break;
474     case T_MODIFYCT:
475         fprintf( fp, "re_changetype: modify\n" );
476         break;
477     case T_DELETECT:
478         fprintf( fp, "re_changetype: delete\n" );
479         break;
480     case T_MODRDNCT:
481         fprintf( fp, "re_changetype: modrdn\n" );
482         break;
483     default:
484         fprintf( fp, "re_changetype: (unknown, type = %d\n",
485                 re->re_changetype );
486     }
487     if ( re->re_mods == NULL ) {
488         fprintf( fp, "re_mods: (none)\n" );
489     } else {
490         mi = re->re_mods;
491         fprintf( fp, "re_mods:\n" );
492         for ( i = 0; mi[ i ].mi_type != NULL; i++ ) {
493             fprintf( fp, "  %s, \"%s\", (%d bytes)\n",
494                     mi[ i ].mi_type,
495                     mi[ i ].mi_val == NULL ?  "(null)" : mi[ i ].mi_val,
496                     mi[ i ].mi_len );
497         }
498     }
499     return;
500 }
501
502
503 /* 
504  * Given an Ri, an Re, and a file pointer, write a replication record to
505  * the file pointer.  If ri is NULL, then include all replicas in the
506  * output.  If ri is non-NULL, then only include a single "replica:" line
507  * (used when writing rejection records).  Returns 0 on success, -1
508  * on failure.  Note that Re_write will not write anything out if the
509  * refcnt is zero.
510  */
511 static int
512 Re_write(
513     Ri          *ri,
514     Re          *re,
515     FILE        *fp )
516 {
517     int         i;
518     char        *s;
519     int         rc = 0;
520
521     if ( re == NULL || fp == NULL ) {
522         Debug( LDAP_DEBUG_ANY, "Internal error: Re_write: NULL argument\n",
523                 0, 0, 0 );
524         return -1;
525     }
526
527     if ( re->re_refcnt < 1 ) {
528         return 0;               /* this is not an error */
529     }
530
531     if ( ri != NULL ) {         /* write a single "replica:" line */
532         if ( fprintf( fp, "replica: %s:%d\n", ri->ri_hostname,
533                 ri->ri_port ) < 0 ) {
534             rc = -1;
535             goto bad;
536         }
537     } else {                    /* write multiple "replica:" lines */
538         for ( i = 0; re->re_replicas[ i ].rh_hostname != NULL; i++ ) {
539             if ( fprintf( fp, "replica: %s:%d\n",
540                     re->re_replicas[ i ].rh_hostname,
541                     re->re_replicas[ i ].rh_port ) < 0 ) {
542                 rc = -1;
543                 goto bad;
544             }
545         }
546     }
547     if ( fprintf( fp, "time: %s.%d\n", re->re_timestamp, re->re_seq ) < 0 ) {
548         rc = -1;
549         goto bad;
550     }
551     if ( fprintf( fp, "dn: %s\n", re->re_dn ) < 0 ) {
552         rc = -1;
553         goto bad;
554     }
555     if ( fprintf( fp, "changetype: " ) < 0 ) {
556         rc = -1;
557         goto bad;
558     }
559     switch ( re->re_changetype ) {
560     case T_ADDCT:
561         s = T_ADDCTSTR;
562         break;
563     case T_MODIFYCT:
564         s = T_MODIFYCTSTR;
565         break;
566     case T_DELETECT:
567         s = T_DELETECTSTR;
568         break;
569     case T_MODRDNCT:
570         s = T_MODRDNCTSTR;
571         break;
572     default:
573         s = "IllegalModifyType!!!";
574     }
575     if ( fprintf( fp, "%s\n", s ) < 0 ) {
576         rc = -1;
577         goto bad;
578     }
579     for ( i = 0; (( re->re_mods != NULL ) &&
580             ( re->re_mods[ i ].mi_type != NULL )); i++ ) {
581         if ( !strcmp( re->re_mods[ i ].mi_type, T_MODSEPSTR )) {
582             if ( fprintf( fp, "%s\n", T_MODSEPSTR ) < 0 ) {
583                 rc = -1;
584                 goto bad;
585             }
586         } else {
587             char *obuf;
588             obuf = ldif_put( LDIF_PUT_VALUE,
589                         re->re_mods[ i ].mi_type,
590                     re->re_mods[ i ].mi_val ? re->re_mods[ i ].mi_val : "",
591                     re->re_mods[ i ].mi_len );
592             if ( fputs( obuf, fp ) < 0 ) {
593                 rc = -1;
594                 free( obuf );
595                 goto bad;
596             } else {
597                 ber_memfree( obuf );
598             }
599         }
600     }
601     if ( fprintf( fp, "\n" ) < 0 ) {
602         rc = -1;
603         goto bad;
604     }
605     if ( fflush( fp ) != 0 ) {
606         rc = -1;
607         goto bad;
608     }
609 bad:
610     if ( rc != 0 ) {
611         Debug( LDAP_DEBUG_ANY, "Error while writing: %s\n",
612                 sys_errlist[ errno ], 0, 0 );
613     }
614     return rc;
615 }
616
617
618
619
620 /*
621  * Decrement the refcnt.  Locking handled internally.
622  */
623 static int
624 Re_decrefcnt(
625     Re  *re
626 )
627 {
628     re->re_lock( re );
629     re->re_refcnt--;
630     re->re_unlock( re );
631     return 0;
632 }
633
634
635
636 /*
637  * Get the refcnt.  Locking handled internally.
638  */
639 static int
640 Re_getrefcnt(
641     Re  *re
642 )
643 {
644     int ret;
645
646     re->re_lock( re );
647     ret = re->re_refcnt;
648     re->re_unlock( re );
649     return ret;
650 }
651     
652     
653
654
655
656 /*
657  * Lock this replication entry
658  */
659 static int
660 Re_lock(
661     Re  *re
662 )
663 {
664     return( ldap_pvt_thread_mutex_lock( &re->re_mutex ));
665 }
666
667
668
669
670 /*
671  * Unlock this replication entry
672  */
673 static int
674 Re_unlock(
675     Re  *re
676 )
677 {
678     return( ldap_pvt_thread_mutex_unlock( &re->re_mutex ));
679 }
680
681
682
683
684 /* 
685  * Instantiate and initialize an Re.
686  */
687 int
688 Re_init(
689     Re **re
690 )
691 {
692     /* Instantiate */
693     (*re) = (Re *) malloc( sizeof( Re ));
694     if ( *re == NULL ) {
695         return -1;
696     }
697
698     /* Fill in the member function pointers */
699     (*re)->re_free = Re_free;
700     (*re)->re_getnext = Re_getnext;
701     (*re)->re_parse = Re_parse;
702     (*re)->re_write = Re_write;
703     (*re)->re_dump = Re_dump;
704     (*re)->re_lock = Re_lock;
705     (*re)->re_unlock = Re_unlock;
706     (*re)->re_decrefcnt = Re_decrefcnt;
707     (*re)->re_getrefcnt = Re_getrefcnt;
708
709     /* Initialize private data */
710    (*re)->re_refcnt = sglob->num_replicas;
711    (*re)->re_timestamp = NULL;
712    (*re)->re_replicas = NULL;
713    (*re)->re_dn = NULL;
714    (*re)->re_changetype = 0;
715    (*re)->re_seq = 0;
716    (*re)->re_mods = NULL;
717    (*re)->re_next = NULL;
718
719    ldap_pvt_thread_mutex_init( &((*re)->re_mutex) );
720    return 0;
721 }
722
723
724
725
726 /*
727  * Given a host and port, generate a warning message iff we haven't already
728  * generated a message for this host:port combination.
729  */
730 static void
731 warn_unknown_replica( 
732     char        *host,
733     int         port
734 )
735 {
736     int found = 0;
737     int i;
738
739     for ( i = 0; i < nur; i++ ) {
740         if ( strcmp( ur[ i ].rh_hostname, host )) {
741             continue;
742         }
743         if ( ur[ i ].rh_port == port ) {
744             found = 1;
745             break;
746         }
747     }
748     if ( !found ) {
749         Debug( LDAP_DEBUG_ANY,
750                 "Warning: unknown replica %s:%d found in replication log\n",
751                 host, port, 0 );
752         nur++;
753         ur = (Rh *) ch_realloc( (char *) ur, ( nur * sizeof( Rh )));
754         ur[ nur - 1 ].rh_hostname = strdup( host );
755         ur[ nur - 1 ].rh_port = port;
756     }
757 }