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