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