]> git.sur5r.net Git - openldap/blob - servers/slurpd/re.c
Merge in all -devel changes made since branch was created.
[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 "slurp.h"
31 #include "globals.h"
32
33 #include "../slapd/slap.h"
34
35 /* Forward references */
36 static Rh       *get_repl_hosts LDAP_P(( char *, int *, char ** ));
37 static int      gettype LDAP_P(( char * ));
38 static int      getchangetype LDAP_P(( char * ));
39 static int      Re_parse LDAP_P(( Re *re, char *replbuf ));
40 static void     Re_dump LDAP_P(( Re *re, FILE *fp ));
41 static void     warn_unknown_replica LDAP_P(( char *, int port ));
42
43 /* Globals, scoped within this file */
44 static int      nur = 0;        /* Number of unknown replicas */
45 static Rh       *ur = NULL;     /* array of unknown replica names */
46
47
48 /*
49  * Return the next Re in a linked list.
50  */
51 static Re *
52 Re_getnext(
53     Re  *re
54 )
55 {
56     return(( re == NULL ) ? NULL :  re->re_next );
57 }
58
59
60
61
62 /*
63  * Free an Re
64  * ??? Something should apparently return nonzero here, but I dont know what.
65  */
66 static int
67 Re_free(
68     Re  *re
69 )
70 {
71     Rh  *rh;
72     Mi  *mi;
73     int i;
74
75     if ( re == NULL ) {
76         return 0;
77     }
78     if ( re->re_refcnt > 0 ) {
79         Debug( LDAP_DEBUG_ANY,
80                 "Warning: freeing re (dn: %s) with nonzero refcnt\n",
81                 re->re_dn, 0, 0 );
82     }
83
84     ldap_pvt_thread_mutex_destroy( &re->re_mutex );
85
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     size_t              buflen;
129     char                *type, *value;
130     ber_len_t   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 = ldif_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 = strlen( buf );
162         if ( ldif_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 = ldif_getline( &rp )) == NULL ) {
209             break;
210         }
211         buflen = strlen( buf );
212         if (( buflen == 1 ) && ( buf[ 0 ] == '-' )) {
213             type = "-";
214             value = NULL;
215         } else {
216             if ( ldif_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[ LDIF_LINE_WIDTH + 1 ];
260     char                *type, *value, *line, *p;
261     Rh                  *rh = NULL;
262     int                 nreplicas;
263         ber_len_t       len;
264     int                 port;
265     int                 repl_ok;
266     int                 i;
267
268     if ( replbuf == NULL ) {
269         return( NULL );
270     }
271
272     nreplicas = 0;
273
274     /*
275      * Get the host names of the replicas
276      */
277     *r_nreplicas = 0;
278     *r_rp = replbuf;
279     for (;;) {
280         /* If this is a reject log, we need to skip over the ERROR: line */
281         if ( !strncmp( *r_rp, ERROR_STR, strlen( ERROR_STR ))) {
282             line = ldif_getline( r_rp );
283             if ( line == NULL ) {
284                 break;
285             }
286         }
287         if ( strncasecmp( *r_rp, "replica:", 7 )) {
288             break;
289         }
290         line = ldif_getline( r_rp );
291         if ( line == NULL ) {
292             break;
293         }
294         if ( ldif_parse_line( line, &type, &value, &len ) < 0 ) {
295             return( NULL );
296         }
297         port = 0;
298         if (( p = strchr( value, ':' )) != NULL ) {
299             *p = '\0';
300             p++;
301             if ( *p != '\0' ) {
302                 port = atoi( p );
303             }
304         }
305
306         /* Verify that we've heard of this replica before */
307         repl_ok = 0;
308         for ( i = 0; i < sglob->num_replicas; i++ ) {
309             if ( strcmp( sglob->replicas[ i ]->ri_hostname, value )) {
310                 continue;
311             }
312             if ( sglob->replicas[ i ]->ri_port == port ) {
313                 repl_ok = 1;
314                 break;
315             }
316         }
317         if ( !repl_ok ) {
318             warn_unknown_replica( value, port );
319             continue;
320         }
321
322         rh = (Rh *) ch_realloc((char *) rh, ( nreplicas + 2 ) * sizeof( Rh ));
323         if ( rh == NULL ) {
324             Debug( LDAP_DEBUG_ANY, "Out of memory in get_repl_hosts\n",
325                     0, 0, 0 );
326             return NULL;
327         }
328         rh[ nreplicas ].rh_hostname = strdup( value );
329         rh[ nreplicas ].rh_port = port;
330         nreplicas++;
331     }
332
333     if ( nreplicas == 0 ) {
334         return( NULL );
335     }
336
337     rh[ nreplicas ].rh_hostname = NULL;
338     *r_nreplicas = nreplicas;
339
340     return( rh );
341 }
342
343
344
345
346
347 /*
348  * Convert "type" to an int.
349  */
350 static int
351 gettype(
352     char        *type
353 )
354 {
355     if ( !strcmp( type, T_CHANGETYPESTR )) {
356         return( T_CHANGETYPE );
357     }
358     if ( !strcmp( type, T_TIMESTR )) {
359         return( T_TIME );
360     }
361     if ( !strcmp( type, T_DNSTR )) {
362         return( T_DN );
363     }
364     return( T_ERR );
365 }
366
367
368
369 /*
370  * Convert "changetype" to an int.
371  */
372 static int
373 getchangetype(
374     char        *changetype
375 )
376 {
377     if ( !strcmp( changetype, T_ADDCTSTR )) {
378         return( T_ADDCT );
379     }
380     if ( !strcmp( changetype, T_MODIFYCTSTR )) {
381         return( T_MODIFYCT );
382     }
383     if ( !strcmp( changetype, T_DELETECTSTR )) {
384         return( T_DELETECT );
385     }
386     if ( !strcmp( changetype, T_MODRDNCTSTR )) {
387         return( T_MODRDNCT );
388     }
389     return( T_ERR );
390 }
391
392
393
394
395 /*
396  * Find the first line which is not a "replica:" line in buf.
397  * Returns a pointer to the line.  Returns NULL if there are
398  * only "replica:" lines in buf.
399  */
400 static char *
401 skip_replica_lines(
402     char        *buf
403 )
404 {
405     char *p = buf;
406     for (;;) {
407         if ( strncasecmp( p, "replica:", 8 )) {
408             return( p );
409         }
410         while (( *p != '\0' )  && ( *p != '\n' )) {
411             p++;
412         }
413         if ( *p == '\0' ) {
414             return ( NULL );
415         } else {
416             p++;
417         }
418     }
419 }
420
421
422
423
424 /*
425  * For debugging purposes: dump the contents of a replication entry.
426  * to the given stream.
427  */
428 static void
429 Re_dump(
430     Re          *re,
431     FILE        *fp
432 )
433 {
434     int i;
435     Mi *mi;
436
437     if ( re == NULL ) {
438         Debug( LDAP_DEBUG_TRACE, "Re_dump: re is NULL\n", 0, 0, 0 );
439         return;
440     }
441     fprintf( fp, "Re_dump: ******\n" );
442     fprintf( fp, "re_refcnt: %d\n", re->re_refcnt );
443     fprintf( fp, "re_timestamp: %s\n", re->re_timestamp );
444     fprintf( fp, "re_seq: %d\n", re->re_seq );
445     for ( i = 0; re->re_replicas && re->re_replicas[ i ].rh_hostname != NULL;
446                 i++ ) {
447         fprintf( fp, "re_replicas[%d]: %s:%d\n", 
448                 i, re->re_replicas[ i ].rh_hostname,
449                 re->re_replicas[ i ].rh_port );
450     }
451     fprintf( fp, "re_dn: %s\n", re->re_dn );
452     switch ( re->re_changetype ) {
453     case T_ADDCT:
454         fprintf( fp, "re_changetype: add\n" );
455         break;
456     case T_MODIFYCT:
457         fprintf( fp, "re_changetype: modify\n" );
458         break;
459     case T_DELETECT:
460         fprintf( fp, "re_changetype: delete\n" );
461         break;
462     case T_MODRDNCT:
463         fprintf( fp, "re_changetype: modrdn\n" );
464         break;
465     default:
466         fprintf( fp, "re_changetype: (unknown, type = %d\n",
467                 re->re_changetype );
468     }
469     if ( re->re_mods == NULL ) {
470         fprintf( fp, "re_mods: (none)\n" );
471     } else {
472         mi = re->re_mods;
473         fprintf( fp, "re_mods:\n" );
474         for ( i = 0; mi[ i ].mi_type != NULL; i++ ) {
475             fprintf( fp, "  %s, \"%s\", (%d bytes)\n",
476                     mi[ i ].mi_type,
477                     mi[ i ].mi_val == NULL ?  "(null)" : mi[ i ].mi_val,
478                     mi[ i ].mi_len );
479         }
480     }
481     return;
482 }
483
484
485 /* 
486  * Given an Ri, an Re, and a file pointer, write a replication record to
487  * the file pointer.  If ri is NULL, then include all replicas in the
488  * output.  If ri is non-NULL, then only include a single "replica:" line
489  * (used when writing rejection records).  Returns 0 on success, -1
490  * on failure.  Note that Re_write will not write anything out if the
491  * refcnt is zero.
492  */
493 static int
494 Re_write(
495     Ri          *ri,
496     Re          *re,
497     FILE        *fp )
498 {
499     int         i;
500     char        *s;
501     int         rc = 0;
502
503     if ( re == NULL || fp == NULL ) {
504         Debug( LDAP_DEBUG_ANY, "Internal error: Re_write: NULL argument\n",
505                 0, 0, 0 );
506         return -1;
507     }
508
509     if ( re->re_refcnt < 1 ) {
510         return 0;               /* this is not an error */
511     }
512
513     if ( ri != NULL ) {         /* write a single "replica:" line */
514         if ( fprintf( fp, "replica: %s:%d\n", ri->ri_hostname,
515                 ri->ri_port ) < 0 ) {
516             rc = -1;
517             goto bad;
518         }
519     } else {                    /* write multiple "replica:" lines */
520         for ( i = 0; re->re_replicas[ i ].rh_hostname != NULL; i++ ) {
521             if ( fprintf( fp, "replica: %s:%d\n",
522                     re->re_replicas[ i ].rh_hostname,
523                     re->re_replicas[ i ].rh_port ) < 0 ) {
524                 rc = -1;
525                 goto bad;
526             }
527         }
528     }
529     if ( fprintf( fp, "time: %s.%d\n", re->re_timestamp, re->re_seq ) < 0 ) {
530         rc = -1;
531         goto bad;
532     }
533     if ( fprintf( fp, "dn: %s\n", re->re_dn ) < 0 ) {
534         rc = -1;
535         goto bad;
536     }
537     if ( fprintf( fp, "changetype: " ) < 0 ) {
538         rc = -1;
539         goto bad;
540     }
541     switch ( re->re_changetype ) {
542     case T_ADDCT:
543         s = T_ADDCTSTR;
544         break;
545     case T_MODIFYCT:
546         s = T_MODIFYCTSTR;
547         break;
548     case T_DELETECT:
549         s = T_DELETECTSTR;
550         break;
551     case T_MODRDNCT:
552         s = T_MODRDNCTSTR;
553         break;
554     default:
555         s = "IllegalModifyType!!!";
556     }
557     if ( fprintf( fp, "%s\n", s ) < 0 ) {
558         rc = -1;
559         goto bad;
560     }
561     for ( i = 0; (( re->re_mods != NULL ) &&
562             ( re->re_mods[ i ].mi_type != NULL )); i++ ) {
563         if ( !strcmp( re->re_mods[ i ].mi_type, T_MODSEPSTR )) {
564             if ( fprintf( fp, "%s\n", T_MODSEPSTR ) < 0 ) {
565                 rc = -1;
566                 goto bad;
567             }
568         } else {
569             char *obuf;
570             obuf = ldif_put( LDIF_PUT_VALUE,
571                         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                 ber_memfree( 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( ldap_pvt_thread_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( ldap_pvt_thread_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    ldap_pvt_thread_mutex_init( &((*re)->re_mutex) );
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 }