2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2006 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
15 /* Portions Copyright (c) 1996 Regents of the University of Michigan.
16 * All rights reserved.
18 * Redistribution and use in source and binary forms are permitted
19 * provided that this notice is preserved and that due credit is given
20 * to the University of Michigan at Ann Arbor. The name of the University
21 * may not be used to endorse or promote products derived from this
22 * software without specific prior written permission. This software
23 * is provided ``as is'' without express or implied warranty.
26 * This work was originally developed by the University of Michigan
27 * (as part of U-MICH LDAP).
31 * re.c - routines which deal with Re (Replication entry) structures.
32 * An Re struct is an in-core representation of one replication to
33 * be performed, along with member functions which are called by other
34 * routines. The Re struct is defined in slurp.h.
42 #include <ac/stdlib.h>
44 #include <ac/socket.h>
45 #include <ac/string.h>
48 #include "../slapd/slap.h"
54 /* Forward references */
55 static Rh *get_repl_hosts LDAP_P(( char *, int *, char ** ));
56 static int gettype LDAP_P(( char * ));
57 static int getchangetype LDAP_P(( char * ));
58 static int Re_parse LDAP_P(( Re *re, char *replbuf ));
59 static void Re_dump LDAP_P(( Re *re, FILE *fp ));
60 static void warn_unknown_replica LDAP_P(( char *, int port ));
62 /* Globals, scoped within this file */
63 static int nur = 0; /* Number of unknown replicas */
64 static Rh *ur = NULL; /* array of unknown replica names */
68 * Return the next Re in a linked list.
75 return(( re == NULL ) ? NULL : re->re_next );
83 * ??? Something should apparently return nonzero here, but I dont know what.
97 if ( re->re_refcnt > 0 ) {
98 Debug( LDAP_DEBUG_ANY,
99 "Warning: freeing re (dn: %s) with nonzero refcnt\n",
103 ldap_pvt_thread_mutex_destroy( &re->re_mutex );
105 if (( rh = re->re_replicas ) != NULL ) {
106 for ( i = 0; rh[ i ].rh_hostname != NULL; i++ ) {
107 free( rh[ i ].rh_hostname );
111 ch_free( re->re_dn );
112 if (( mi = re->re_mods ) != NULL ) {
113 for ( i = 0; mi[ i ].mi_type != NULL; i++ ) {
114 free( mi[ i ].mi_type );
115 ch_free( mi[ i ].mi_val );
127 * Read a buffer of data from a replication log file and fill in
128 * an (already allocated) Re.
134 #define GOT_CHANGETYPE 4
135 #define GOT_ALL ( GOT_DN | GOT_TIME | GOT_CHANGETYPE )
152 Debug( LDAP_DEBUG_ANY, "Re_parse: error: re is NULL\n", 0, 0, 0 );
155 if ( replbuf == NULL ) {
156 Debug( LDAP_DEBUG_ANY, "Re_parse: error: replbuf is NULL\n", 0, 0, 0 );
161 nml = 0; /* number of modification information entries */
164 re->re_replicas = get_repl_hosts( replbuf, &nreplicas, &rp );
165 re->re_refcnt = sglob->num_replicas;
168 if (( state == GOT_ALL ) || ( buf = ldif_getline( &rp )) == NULL ) {
172 * If we're processing a rejection log, then the first line
173 * of each replication record will begin with "ERROR" - just
176 if ( strncmp( buf, ERROR_STR, strlen( ERROR_STR )) == 0 ) {
179 buflen = strlen( buf );
180 if ( ldif_parse_line( buf, &type, &value, &len ) < 0 ) {
181 Debug( LDAP_DEBUG_ANY,
182 "Error: Re_parse: malformed replog file\n",
186 switch ( gettype( type )) {
188 re->re_changetype = getchangetype( value );
189 state |= GOT_CHANGETYPE;
194 if (( p = strchr( value, '.' )) != NULL ) {
195 /* there was a sequence number */
198 if ( lutil_atoul( &t, value ) != 0 ) {
199 Debug( LDAP_DEBUG_ANY,
200 "Error: Re_parse: unable to parse timestamp \"%s\"\n",
204 re->re_timestamp = (time_t)t;
205 if ( p != NULL && isdigit( (unsigned char) *p )
206 && lutil_atoi( &re->re_seq, p ) != 0 )
208 Debug( LDAP_DEBUG_ANY,
209 "Error: Re_parse: unable to parse sequence number \"%s\"\n",
216 re->re_dn = ch_malloc( len + 1 );
217 AC_MEMCPY( re->re_dn, value, len );
218 re->re_dn[ len ]='\0';
222 if ( !( state == GOT_ALL )) {
223 Debug( LDAP_DEBUG_ANY,
224 "Error: Re_parse: bad type <%s>\n",
235 if ( state != GOT_ALL ) {
236 Debug( LDAP_DEBUG_ANY,
237 "Error: Re_parse: malformed replog file\n",
243 char *const dash = "-";
245 if (( buf = ldif_getline( &rp )) == NULL ) {
248 buflen = strlen( buf );
249 if (( buflen == 1 ) && ( buf[ 0 ] == '-' )) {
253 if ( ldif_parse_line( buf, &type, &value, &len ) < 0 ) {
254 Debug( LDAP_DEBUG_ANY,
255 "Error: malformed replog line \"%s\"\n",
260 re->re_mods = ( Mi *) ch_realloc( (char *) re->re_mods,
261 sizeof( Mi ) * ( nml + 2 ));
262 re->re_mods[ nml ].mi_type = strdup( type );
263 if ( value != NULL ) {
264 re->re_mods[ nml ].mi_val = ch_malloc( len + 1 );
265 AC_MEMCPY( re->re_mods[ nml ].mi_val, value, len );
266 re->re_mods[ nml ].mi_val[ len ] = '\0';
267 re->re_mods[ nml ].mi_len = len;
269 re->re_mods[ nml ].mi_val = NULL;
270 re->re_mods[ nml ].mi_len = 0;
272 re->re_mods[ nml + 1 ].mi_type = NULL;
273 re->re_mods[ nml + 1 ].mi_val = NULL;
287 * Extract the replication hosts from a repl buf. Check to be sure that
288 * each replica host and port number are ones we know about (that is, they're
289 * in the slapd config file we read at startup). Without that information
290 * from the config file, we won't have the appropriate credentials to
291 * make modifications. If there are any unknown replica names, don't
292 * add them the the Re struct. Instead, log a warning message.
301 char *type, *value, *line, *p;
309 if ( replbuf == NULL ) {
316 * Get the host names of the replicas
321 /* If this is a reject log, we need to skip over the ERROR: line */
322 if ( !strncmp( *r_rp, ERROR_STR, strlen( ERROR_STR ))) {
323 line = ldif_getline( r_rp );
324 if ( line == NULL ) {
328 if ( strncasecmp( *r_rp, "replica:", 7 )) {
331 line = ldif_getline( r_rp );
332 if ( line == NULL ) {
335 if ( ldif_parse_line( line, &type, &value, &len ) < 0 ) {
339 if (( p = strchr( value, ':' )) != NULL ) {
342 if ( *p != '\0' && lutil_atoi( &port, p ) != 0 ) {
347 /* Verify that we've heard of this replica before */
349 for ( i = 0; i < sglob->num_replicas; i++ ) {
350 if ( strcmp( sglob->replicas[ i ]->ri_hostname, value )) {
353 if ( sglob->replicas[ i ]->ri_port == port ) {
360 warn_unknown_replica( value, port );
365 rh = (Rh *) ch_realloc((char *) rh, ( nreplicas + 2 ) * sizeof( Rh ));
367 Debug( LDAP_DEBUG_ANY, "Out of memory in get_repl_hosts\n",
371 rh[ nreplicas ].rh_hostname = strdup( value );
372 rh[ nreplicas ].rh_port = port;
378 if ( nreplicas == 0 ) {
382 rh[ nreplicas ].rh_hostname = NULL;
383 *r_nreplicas = nreplicas;
393 * Convert "type" to an int.
400 if ( !strcmp( type, T_CHANGETYPESTR )) {
401 return( T_CHANGETYPE );
403 if ( !strcmp( type, T_TIMESTR )) {
406 if ( !strcmp( type, T_DNSTR )) {
415 * Convert "changetype" to an int.
422 if ( !strcmp( changetype, T_ADDCTSTR )) {
425 if ( !strcmp( changetype, T_MODIFYCTSTR )) {
426 return( T_MODIFYCT );
428 if ( !strcmp( changetype, T_DELETECTSTR )) {
429 return( T_DELETECT );
431 if ( !strcmp( changetype, T_MODRDNCTSTR )) {
432 return( T_MODRDNCT );
442 * Find the first line which is not a "replica:" line in buf.
443 * Returns a pointer to the line. Returns NULL if there are
444 * only "replica:" lines in buf.
453 if ( strncasecmp( p, "replica:", 8 )) {
456 while (( *p != '\0' ) && ( *p != '\n' )) {
472 * For debugging purposes: dump the contents of a replication entry.
473 * to the given stream.
485 Debug( LDAP_DEBUG_TRACE, "Re_dump: re is NULL\n", 0, 0, 0 );
488 fprintf( fp, "Re_dump: ******\n" );
489 fprintf( fp, "re_refcnt: %d\n", re->re_refcnt );
490 fprintf( fp, "re_timestamp: %ld\n", (long) re->re_timestamp );
491 fprintf( fp, "re_seq: %d\n", re->re_seq );
492 for ( i = 0; re->re_replicas && re->re_replicas[ i ].rh_hostname != NULL;
494 fprintf( fp, "re_replicas[%d]: %s:%d\n",
495 i, re->re_replicas[ i ].rh_hostname,
496 re->re_replicas[ i ].rh_port );
498 fprintf( fp, "re_dn: %s\n", re->re_dn );
499 switch ( re->re_changetype ) {
501 fprintf( fp, "re_changetype: add\n" );
504 fprintf( fp, "re_changetype: modify\n" );
507 fprintf( fp, "re_changetype: delete\n" );
510 fprintf( fp, "re_changetype: modrdn\n" );
513 fprintf( fp, "re_changetype: (unknown, type = %d\n",
516 if ( re->re_mods == NULL ) {
517 fprintf( fp, "re_mods: (none)\n" );
520 fprintf( fp, "re_mods:\n" );
521 for ( i = 0; mi[ i ].mi_type != NULL; i++ ) {
522 fprintf( fp, " %s, \"%s\", (%d bytes)\n",
524 mi[ i ].mi_val == NULL ? "(null)" : mi[ i ].mi_val,
533 * Given an Ri, an Re, and a file pointer, write a replication record to
534 * the file pointer. If ri is NULL, then include all replicas in the
535 * output. If ri is non-NULL, then only include a single "replica:" line
536 * (used when writing rejection records). Returns 0 on success, -1
537 * on failure. Note that Re_write will not write anything out if the
550 if ( re == NULL || fp == NULL ) {
551 Debug( LDAP_DEBUG_ANY, "Internal error: Re_write: NULL argument\n",
556 if ( re->re_refcnt < 1 ) {
557 return 0; /* this is not an error */
560 if ( ri != NULL ) { /* write a single "replica:" line */
561 if ( ri->ri_port != 0 ) {
562 rc = fprintf( fp, "replica: %s:%d\n", ri->ri_hostname,
565 rc = fprintf( fp, "replica: %s\n", ri->ri_hostname );
573 } else { /* write multiple "replica:" lines */
574 for ( i = 0; re->re_replicas && re->re_replicas[ i ].rh_hostname != NULL; i++ ) {
575 if ( fprintf( fp, "replica: %s:%d\n",
576 re->re_replicas[ i ].rh_hostname,
577 re->re_replicas[ i ].rh_port ) < 0 ) {
583 if ( fprintf( fp, "time: %ld.%d\n", (long) re->re_timestamp, re->re_seq ) < 0 ) {
587 if ( fprintf( fp, "dn: %s\n", re->re_dn ) < 0 ) {
591 if ( fprintf( fp, "changetype: " ) < 0 ) {
595 switch ( re->re_changetype ) {
609 s = "IllegalModifyType!!!";
611 if ( fprintf( fp, "%s\n", s ) < 0 ) {
615 for ( i = 0; (( re->re_mods != NULL ) &&
616 ( re->re_mods[ i ].mi_type != NULL )); i++ ) {
617 if ( !strcmp( re->re_mods[ i ].mi_type, T_MODSEPSTR )) {
618 if ( fprintf( fp, "%s\n", T_MODSEPSTR ) < 0 ) {
624 obuf = ldif_put( LDIF_PUT_VALUE,
625 re->re_mods[ i ].mi_type,
626 re->re_mods[ i ].mi_val ? re->re_mods[ i ].mi_val : "",
627 re->re_mods[ i ].mi_len );
628 if ( fputs( obuf, fp ) < 0 ) {
637 if ( fprintf( fp, "\n" ) < 0 ) {
641 if ( fflush( fp ) != 0 ) {
647 Debug( LDAP_DEBUG_ANY, "Error while writing: %s\n",
648 sys_errlist[ errno ], 0, 0 );
657 * Decrement the refcnt. Locking handled internally.
673 * Get the refcnt. Locking handled internally.
693 * Lock this replication entry
700 return( ldap_pvt_thread_mutex_lock( &re->re_mutex ));
707 * Unlock this replication entry
714 return( ldap_pvt_thread_mutex_unlock( &re->re_mutex ));
721 * Instantiate and initialize an Re.
729 (*re) = (Re *) malloc( sizeof( Re ));
734 /* Fill in the member function pointers */
735 (*re)->re_free = Re_free;
736 (*re)->re_getnext = Re_getnext;
737 (*re)->re_parse = Re_parse;
738 (*re)->re_write = Re_write;
739 (*re)->re_dump = Re_dump;
740 (*re)->re_lock = Re_lock;
741 (*re)->re_unlock = Re_unlock;
742 (*re)->re_decrefcnt = Re_decrefcnt;
743 (*re)->re_getrefcnt = Re_getrefcnt;
745 /* Initialize private data */
746 (*re)->re_refcnt = sglob->num_replicas;
747 (*re)->re_timestamp = (time_t) 0L;
748 (*re)->re_replicas = NULL;
750 (*re)->re_changetype = 0;
752 (*re)->re_mods = NULL;
753 (*re)->re_next = NULL;
755 ldap_pvt_thread_mutex_init( &((*re)->re_mutex) );
763 * Given a host and port, generate a warning message iff we haven't already
764 * generated a message for this host:port combination.
767 warn_unknown_replica(
775 for ( i = 0; i < nur; i++ ) {
776 if ( strcmp( ur[ i ].rh_hostname, host )) {
779 if ( ur[ i ].rh_port == port ) {
785 Debug( LDAP_DEBUG_ANY,
786 "Warning: unknown replica %s:%d found in replication log\n",
789 ur = (Rh *) ch_realloc( (char *) ur, ( nur * sizeof( Rh )));
790 ur[ nur - 1 ].rh_hostname = strdup( host );
791 ur[ nur - 1 ].rh_port = port;