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