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