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