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