2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2004 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"
53 /* Forward references */
54 static Rh *get_repl_hosts LDAP_P(( char *, int *, char ** ));
55 static int gettype LDAP_P(( char * ));
56 static int getchangetype LDAP_P(( char * ));
57 static int Re_parse LDAP_P(( Re *re, char *replbuf ));
58 static void Re_dump LDAP_P(( Re *re, FILE *fp ));
59 static void warn_unknown_replica LDAP_P(( char *, int port ));
61 /* Globals, scoped within this file */
62 static int nur = 0; /* Number of unknown replicas */
63 static Rh *ur = NULL; /* array of unknown replica names */
67 * Return the next Re in a linked list.
74 return(( re == NULL ) ? NULL : re->re_next );
82 * ??? Something should apparently return nonzero here, but I dont know what.
96 if ( re->re_refcnt > 0 ) {
98 LDAP_LOG ( SLURPD, WARNING, "Re_free: "
99 "Warning: freeing re (dn: %s) with nonzero refcnt\n", re->re_dn, 0, 0 );
101 Debug( LDAP_DEBUG_ANY,
102 "Warning: freeing re (dn: %s) with nonzero refcnt\n",
107 ldap_pvt_thread_mutex_destroy( &re->re_mutex );
109 if (( rh = re->re_replicas ) != NULL ) {
110 for ( i = 0; rh[ i ].rh_hostname != NULL; i++ ) {
111 free( rh[ i ].rh_hostname );
115 ch_free( re->re_dn );
116 if (( mi = re->re_mods ) != NULL ) {
117 for ( i = 0; mi[ i ].mi_type != NULL; i++ ) {
118 free( mi[ i ].mi_type );
119 ch_free( mi[ i ].mi_val );
131 * Read a buffer of data from a replication log file and fill in
132 * an (already allocated) Re.
138 #define GOT_CHANGETYPE 4
139 #define GOT_ALL ( GOT_DN | GOT_TIME | GOT_CHANGETYPE )
157 LDAP_LOG ( SLURPD, ERR, "Re_parse: Error: re is NULL\n", 0, 0, 0 );
159 Debug( LDAP_DEBUG_ANY, "Re_parse: error: re is NULL\n", 0, 0, 0 );
163 if ( replbuf == NULL ) {
165 LDAP_LOG ( SLURPD, ERR, "Re_parse: Error: replbuf is NULL\n", 0, 0, 0 );
167 Debug( LDAP_DEBUG_ANY, "Re_parse: error: replbuf is NULL\n", 0, 0, 0 );
173 nml = 0; /* number of modification information entries */
176 re->re_replicas = get_repl_hosts( replbuf, &nreplicas, &rp );
177 re->re_refcnt = sglob->num_replicas;
180 if (( state == GOT_ALL ) || ( buf = ldif_getline( &rp )) == NULL ) {
184 * If we're processing a rejection log, then the first line
185 * of each replication record will begin with "ERROR" - just
188 if ( strncmp( buf, ERROR_STR, strlen( ERROR_STR )) == 0 ) {
191 buflen = strlen( buf );
192 if ( ldif_parse_line( buf, &type, &value, &len ) < 0 ) {
194 LDAP_LOG ( SLURPD, ERR,
195 "Re_parse: Error: malformed replog file\n", 0, 0, 0 );
197 Debug( LDAP_DEBUG_ANY,
198 "Error: Re_parse: malformed replog file\n",
203 switch ( gettype( type )) {
205 re->re_changetype = getchangetype( value );
206 state |= GOT_CHANGETYPE;
209 if (( p = strchr( value, '.' )) != NULL ) {
210 /* there was a sequence number */
213 re->re_timestamp = atol( value );
214 if ( p != NULL && isdigit( (unsigned char) *p )) {
215 re->re_seq = atoi( p );
220 re->re_dn = ch_malloc( len + 1 );
221 AC_MEMCPY( re->re_dn, value, len );
222 re->re_dn[ len ]='\0';
226 if ( !( state == GOT_ALL )) {
228 LDAP_LOG ( SLURPD, ERR,
229 "Re_parse: Error: bad type <%s>\n", type, 0, 0 );
231 Debug( LDAP_DEBUG_ANY,
232 "Error: Re_parse: bad type <%s>\n",
244 if ( state != GOT_ALL ) {
246 LDAP_LOG ( SLURPD, ERR,
247 "Re_parse: Error: malformed replog file\n", 0, 0, 0 );
249 Debug( LDAP_DEBUG_ANY,
250 "Error: Re_parse: malformed replog file\n",
257 char *const dash = "-";
259 if (( buf = ldif_getline( &rp )) == NULL ) {
262 buflen = strlen( buf );
263 if (( buflen == 1 ) && ( buf[ 0 ] == '-' )) {
267 if ( ldif_parse_line( buf, &type, &value, &len ) < 0 ) {
269 LDAP_LOG ( SLURPD, ERR,
270 "Re_parse: Error: malformed replog line \"%s\"\n", buf, 0, 0 );
272 Debug( LDAP_DEBUG_ANY,
273 "Error: malformed replog line \"%s\"\n",
279 re->re_mods = ( Mi *) ch_realloc( (char *) re->re_mods,
280 sizeof( Mi ) * ( nml + 2 ));
281 re->re_mods[ nml ].mi_type = strdup( type );
282 if ( value != NULL ) {
283 re->re_mods[ nml ].mi_val = ch_malloc( len + 1 );
284 AC_MEMCPY( re->re_mods[ nml ].mi_val, value, len );
285 re->re_mods[ nml ].mi_val[ len ] = '\0';
286 re->re_mods[ nml ].mi_len = len;
288 re->re_mods[ nml ].mi_val = NULL;
289 re->re_mods[ nml ].mi_len = 0;
291 re->re_mods[ nml + 1 ].mi_type = NULL;
292 re->re_mods[ nml + 1 ].mi_val = NULL;
306 * Extract the replication hosts from a repl buf. Check to be sure that
307 * each replica host and port number are ones we know about (that is, they're
308 * in the slapd config file we read at startup). Without that information
309 * from the config file, we won't have the appropriate credentials to
310 * make modifications. If there are any unknown replica names, don't
311 * add them the the Re struct. Instead, log a warning message.
320 char *type, *value, *line, *p;
328 if ( replbuf == NULL ) {
335 * Get the host names of the replicas
340 /* If this is a reject log, we need to skip over the ERROR: line */
341 if ( !strncmp( *r_rp, ERROR_STR, strlen( ERROR_STR ))) {
342 line = ldif_getline( r_rp );
343 if ( line == NULL ) {
347 if ( strncasecmp( *r_rp, "replica:", 7 )) {
350 line = ldif_getline( r_rp );
351 if ( line == NULL ) {
354 if ( ldif_parse_line( line, &type, &value, &len ) < 0 ) {
358 if (( p = strchr( value, ':' )) != NULL ) {
366 /* Verify that we've heard of this replica before */
368 for ( i = 0; i < sglob->num_replicas; i++ ) {
369 if ( strcmp( sglob->replicas[ i ]->ri_hostname, value )) {
372 if ( sglob->replicas[ i ]->ri_port == port ) {
379 warn_unknown_replica( value, port );
384 rh = (Rh *) ch_realloc((char *) rh, ( nreplicas + 2 ) * sizeof( Rh ));
387 LDAP_LOG ( SLURPD, ERR,
388 "get_repl_hosts: Out of memory\n", 0, 0, 0 );
390 Debug( LDAP_DEBUG_ANY, "Out of memory in get_repl_hosts\n",
395 rh[ nreplicas ].rh_hostname = strdup( value );
396 rh[ nreplicas ].rh_port = port;
402 if ( nreplicas == 0 ) {
406 rh[ nreplicas ].rh_hostname = NULL;
407 *r_nreplicas = nreplicas;
417 * Convert "type" to an int.
424 if ( !strcmp( type, T_CHANGETYPESTR )) {
425 return( T_CHANGETYPE );
427 if ( !strcmp( type, T_TIMESTR )) {
430 if ( !strcmp( type, T_DNSTR )) {
439 * Convert "changetype" to an int.
446 if ( !strcmp( changetype, T_ADDCTSTR )) {
449 if ( !strcmp( changetype, T_MODIFYCTSTR )) {
450 return( T_MODIFYCT );
452 if ( !strcmp( changetype, T_DELETECTSTR )) {
453 return( T_DELETECT );
455 if ( !strcmp( changetype, T_MODRDNCTSTR )) {
456 return( T_MODRDNCT );
465 * Find the first line which is not a "replica:" line in buf.
466 * Returns a pointer to the line. Returns NULL if there are
467 * only "replica:" lines in buf.
476 if ( strncasecmp( p, "replica:", 8 )) {
479 while (( *p != '\0' ) && ( *p != '\n' )) {
494 * For debugging purposes: dump the contents of a replication entry.
495 * to the given stream.
508 LDAP_LOG ( SLURPD, ERR, "Re_dump: re is NULL\n", 0, 0, 0 );
510 Debug( LDAP_DEBUG_TRACE, "Re_dump: re is NULL\n", 0, 0, 0 );
514 fprintf( fp, "Re_dump: ******\n" );
515 fprintf( fp, "re_refcnt: %d\n", re->re_refcnt );
516 fprintf( fp, "re_timestamp: %ld\n", (long) re->re_timestamp );
517 fprintf( fp, "re_seq: %d\n", re->re_seq );
518 for ( i = 0; re->re_replicas && re->re_replicas[ i ].rh_hostname != NULL;
520 fprintf( fp, "re_replicas[%d]: %s:%d\n",
521 i, re->re_replicas[ i ].rh_hostname,
522 re->re_replicas[ i ].rh_port );
524 fprintf( fp, "re_dn: %s\n", re->re_dn );
525 switch ( re->re_changetype ) {
527 fprintf( fp, "re_changetype: add\n" );
530 fprintf( fp, "re_changetype: modify\n" );
533 fprintf( fp, "re_changetype: delete\n" );
536 fprintf( fp, "re_changetype: modrdn\n" );
539 fprintf( fp, "re_changetype: (unknown, type = %d\n",
542 if ( re->re_mods == NULL ) {
543 fprintf( fp, "re_mods: (none)\n" );
546 fprintf( fp, "re_mods:\n" );
547 for ( i = 0; mi[ i ].mi_type != NULL; i++ ) {
548 fprintf( fp, " %s, \"%s\", (%d bytes)\n",
550 mi[ i ].mi_val == NULL ? "(null)" : mi[ i ].mi_val,
559 * Given an Ri, an Re, and a file pointer, write a replication record to
560 * the file pointer. If ri is NULL, then include all replicas in the
561 * output. If ri is non-NULL, then only include a single "replica:" line
562 * (used when writing rejection records). Returns 0 on success, -1
563 * on failure. Note that Re_write will not write anything out if the
576 if ( re == NULL || fp == NULL ) {
578 LDAP_LOG ( SLURPD, ERR,
579 "Re_write: Internal error: NULL argument\n", 0, 0, 0 );
581 Debug( LDAP_DEBUG_ANY, "Internal error: Re_write: NULL argument\n",
587 if ( re->re_refcnt < 1 ) {
588 return 0; /* this is not an error */
591 if ( ri != NULL ) { /* write a single "replica:" line */
592 if ( fprintf( fp, "replica: %s:%d\n", ri->ri_hostname,
593 ri->ri_port ) < 0 ) {
597 } else { /* write multiple "replica:" lines */
598 for ( i = 0; re->re_replicas && re->re_replicas[ i ].rh_hostname != NULL; i++ ) {
599 if ( fprintf( fp, "replica: %s:%d\n",
600 re->re_replicas[ i ].rh_hostname,
601 re->re_replicas[ i ].rh_port ) < 0 ) {
607 if ( fprintf( fp, "time: %ld.%d\n", (long) re->re_timestamp, re->re_seq ) < 0 ) {
611 if ( fprintf( fp, "dn: %s\n", re->re_dn ) < 0 ) {
615 if ( fprintf( fp, "changetype: " ) < 0 ) {
619 switch ( re->re_changetype ) {
633 s = "IllegalModifyType!!!";
635 if ( fprintf( fp, "%s\n", s ) < 0 ) {
639 for ( i = 0; (( re->re_mods != NULL ) &&
640 ( re->re_mods[ i ].mi_type != NULL )); i++ ) {
641 if ( !strcmp( re->re_mods[ i ].mi_type, T_MODSEPSTR )) {
642 if ( fprintf( fp, "%s\n", T_MODSEPSTR ) < 0 ) {
648 obuf = ldif_put( LDIF_PUT_VALUE,
649 re->re_mods[ i ].mi_type,
650 re->re_mods[ i ].mi_val ? re->re_mods[ i ].mi_val : "",
651 re->re_mods[ i ].mi_len );
652 if ( fputs( obuf, fp ) < 0 ) {
661 if ( fprintf( fp, "\n" ) < 0 ) {
665 if ( fflush( fp ) != 0 ) {
672 LDAP_LOG ( SLURPD, ERR,
673 "Re_write: Error while writing: %s\n", sys_errlist[ errno ], 0, 0 );
675 Debug( LDAP_DEBUG_ANY, "Error while writing: %s\n",
676 sys_errlist[ errno ], 0, 0 );
686 * Decrement the refcnt. Locking handled internally.
702 * Get the refcnt. Locking handled internally.
722 * Lock this replication entry
729 return( ldap_pvt_thread_mutex_lock( &re->re_mutex ));
736 * Unlock this replication entry
743 return( ldap_pvt_thread_mutex_unlock( &re->re_mutex ));
750 * Instantiate and initialize an Re.
758 (*re) = (Re *) malloc( sizeof( Re ));
763 /* Fill in the member function pointers */
764 (*re)->re_free = Re_free;
765 (*re)->re_getnext = Re_getnext;
766 (*re)->re_parse = Re_parse;
767 (*re)->re_write = Re_write;
768 (*re)->re_dump = Re_dump;
769 (*re)->re_lock = Re_lock;
770 (*re)->re_unlock = Re_unlock;
771 (*re)->re_decrefcnt = Re_decrefcnt;
772 (*re)->re_getrefcnt = Re_getrefcnt;
774 /* Initialize private data */
775 (*re)->re_refcnt = sglob->num_replicas;
776 (*re)->re_timestamp = (time_t) 0L;
777 (*re)->re_replicas = NULL;
779 (*re)->re_changetype = 0;
781 (*re)->re_mods = NULL;
782 (*re)->re_next = NULL;
784 ldap_pvt_thread_mutex_init( &((*re)->re_mutex) );
792 * Given a host and port, generate a warning message iff we haven't already
793 * generated a message for this host:port combination.
796 warn_unknown_replica(
804 for ( i = 0; i < nur; i++ ) {
805 if ( strcmp( ur[ i ].rh_hostname, host )) {
808 if ( ur[ i ].rh_port == port ) {
815 LDAP_LOG ( SLURPD, WARNING, "warn_unknown_replica: "
816 "Warning: unknown replica %s:%d found in replication log\n",
819 Debug( LDAP_DEBUG_ANY,
820 "Warning: unknown replica %s:%d found in replication log\n",
824 ur = (Rh *) ch_realloc( (char *) ur, ( nur * sizeof( Rh )));
825 ur[ nur - 1 ].rh_hostname = strdup( host );
826 ur[ nur - 1 ].rh_port = port;