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