3 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1996 Regents of the University of Michigan.
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.
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.
30 #include <ac/stdlib.h>
32 #include <ac/socket.h>
33 #include <ac/string.h>
39 #include "../slapd/slap.h"
41 /* Forward references */
42 static Rh *get_repl_hosts LDAP_P(( char *, int *, char ** ));
43 static int gettype LDAP_P(( char * ));
44 static int getchangetype LDAP_P(( char * ));
45 static int Re_parse LDAP_P(( Re *re, char *replbuf ));
46 static void Re_dump LDAP_P(( Re *re, FILE *fp ));
47 static void warn_unknown_replica LDAP_P(( char *, int port ));
49 /* Globals, scoped within this file */
50 static int nur = 0; /* Number of unknown replicas */
51 static Rh *ur = NULL; /* array of unknown replica names */
55 * Return the next Re in a linked list.
62 return(( re == NULL ) ? NULL : re->re_next );
70 * ??? Something should apparently return nonzero here, but I dont know what.
84 if ( re->re_refcnt > 0 ) {
86 LDAP_LOG ( SLURPD, WARNING, "Re_free: "
87 "Warning: freeing re (dn: %s) with nonzero refcnt\n", re->re_dn, 0, 0 );
89 Debug( LDAP_DEBUG_ANY,
90 "Warning: freeing re (dn: %s) with nonzero refcnt\n",
95 ldap_pvt_thread_mutex_destroy( &re->re_mutex );
97 if (( rh = re->re_replicas ) != NULL ) {
98 for ( i = 0; rh[ i ].rh_hostname != NULL; i++ ) {
99 free( rh[ i ].rh_hostname );
103 ch_free( re->re_dn );
104 if (( mi = re->re_mods ) != NULL ) {
105 for ( i = 0; mi[ i ].mi_type != NULL; i++ ) {
106 free( mi[ i ].mi_type );
107 ch_free( mi[ i ].mi_val );
119 * Read a buffer of data from a replication log file and fill in
120 * an (already allocated) Re.
126 #define GOT_CHANGETYPE 4
127 #define GOT_ALL ( GOT_DN | GOT_TIME | GOT_CHANGETYPE )
145 LDAP_LOG ( SLURPD, ERR, "Re_parse: Error: re is NULL\n", 0, 0, 0 );
147 Debug( LDAP_DEBUG_ANY, "Re_parse: error: re is NULL\n", 0, 0, 0 );
151 if ( replbuf == NULL ) {
153 LDAP_LOG ( SLURPD, ERR, "Re_parse: Error: replbuf is NULL\n", 0, 0, 0 );
155 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 ) {
182 LDAP_LOG ( SLURPD, ERR,
183 "Re_parse: Error: malformed replog file\n", 0, 0, 0 );
185 Debug( LDAP_DEBUG_ANY,
186 "Error: Re_parse: malformed replog file\n",
191 switch ( gettype( type )) {
193 re->re_changetype = getchangetype( value );
194 state |= GOT_CHANGETYPE;
197 if (( p = strchr( value, '.' )) != NULL ) {
198 /* there was a sequence number */
201 re->re_timestamp = atol( value );
202 if ( p != NULL && isdigit( (unsigned char) *p )) {
203 re->re_seq = atoi( p );
208 re->re_dn = ch_malloc( len + 1 );
209 AC_MEMCPY( re->re_dn, value, len );
210 re->re_dn[ len ]='\0';
214 if ( !( state == GOT_ALL )) {
216 LDAP_LOG ( SLURPD, ERR,
217 "Re_parse: Error: bad type <%s>\n", type, 0, 0 );
219 Debug( LDAP_DEBUG_ANY,
220 "Error: Re_parse: bad type <%s>\n",
234 if ( state != GOT_ALL ) {
236 LDAP_LOG ( SLURPD, ERR,
237 "Re_parse: Error: malformed replog file\n", 0, 0, 0 );
239 Debug( LDAP_DEBUG_ANY,
240 "Error: Re_parse: malformed replog file\n",
247 char *const dash = "-";
249 if (( buf = ldif_getline( &rp )) == NULL ) {
252 buflen = strlen( buf );
253 if (( buflen == 1 ) && ( buf[ 0 ] == '-' )) {
257 if ( ldif_parse_line( buf, &type, &value, &len ) < 0 ) {
259 LDAP_LOG ( SLURPD, ERR,
260 "Re_parse: Error: malformed replog line \"%s\"\n", buf, 0, 0 );
262 Debug( LDAP_DEBUG_ANY,
263 "Error: malformed replog line \"%s\"\n",
269 re->re_mods = ( Mi *) ch_realloc( (char *) re->re_mods,
270 sizeof( Mi ) * ( nml + 2 ));
271 re->re_mods[ nml ].mi_type = strdup( type );
272 if ( value != NULL ) {
273 re->re_mods[ nml ].mi_val = ch_malloc( len + 1 );
274 AC_MEMCPY( re->re_mods[ nml ].mi_val, value, len );
275 re->re_mods[ nml ].mi_val[ len ] = '\0';
276 re->re_mods[ nml ].mi_len = len;
278 re->re_mods[ nml ].mi_val = NULL;
279 re->re_mods[ nml ].mi_len = 0;
281 re->re_mods[ nml + 1 ].mi_type = NULL;
282 re->re_mods[ nml + 1 ].mi_val = NULL;
296 * Extract the replication hosts from a repl buf. Check to be sure that
297 * each replica host and port number are ones we know about (that is, they're
298 * in the slapd config file we read at startup). Without that information
299 * from the config file, we won't have the appropriate credentials to
300 * make modifications. If there are any unknown replica names, don't
301 * add them the the Re struct. Instead, log a warning message.
310 char *type, *value, *line, *p;
318 if ( replbuf == NULL ) {
325 * Get the host names of the replicas
330 /* If this is a reject log, we need to skip over the ERROR: line */
331 if ( !strncmp( *r_rp, ERROR_STR, strlen( ERROR_STR ))) {
332 line = ldif_getline( r_rp );
333 if ( line == NULL ) {
337 if ( strncasecmp( *r_rp, "replica:", 7 )) {
340 line = ldif_getline( r_rp );
341 if ( line == NULL ) {
344 if ( ldif_parse_line( line, &type, &value, &len ) < 0 ) {
348 if (( p = strchr( value, ':' )) != NULL ) {
356 /* Verify that we've heard of this replica before */
358 for ( i = 0; i < sglob->num_replicas; i++ ) {
359 if ( strcmp( sglob->replicas[ i ]->ri_hostname, value )) {
362 if ( sglob->replicas[ i ]->ri_port == port ) {
369 warn_unknown_replica( value, port );
375 rh = (Rh *) ch_realloc((char *) rh, ( nreplicas + 2 ) * sizeof( Rh ));
378 LDAP_LOG ( SLURPD, ERR,
379 "get_repl_hosts: Out of memory\n", 0, 0, 0 );
381 Debug( LDAP_DEBUG_ANY, "Out of memory in get_repl_hosts\n",
386 rh[ nreplicas ].rh_hostname = strdup( value );
387 rh[ nreplicas ].rh_port = port;
394 if ( nreplicas == 0 ) {
398 rh[ nreplicas ].rh_hostname = NULL;
399 *r_nreplicas = nreplicas;
409 * Convert "type" to an int.
416 if ( !strcmp( type, T_CHANGETYPESTR )) {
417 return( T_CHANGETYPE );
419 if ( !strcmp( type, T_TIMESTR )) {
422 if ( !strcmp( type, T_DNSTR )) {
431 * Convert "changetype" to an int.
438 if ( !strcmp( changetype, T_ADDCTSTR )) {
441 if ( !strcmp( changetype, T_MODIFYCTSTR )) {
442 return( T_MODIFYCT );
444 if ( !strcmp( changetype, T_DELETECTSTR )) {
445 return( T_DELETECT );
447 if ( !strcmp( changetype, T_MODRDNCTSTR )) {
448 return( T_MODRDNCT );
457 * Find the first line which is not a "replica:" line in buf.
458 * Returns a pointer to the line. Returns NULL if there are
459 * only "replica:" lines in buf.
468 if ( strncasecmp( p, "replica:", 8 )) {
471 while (( *p != '\0' ) && ( *p != '\n' )) {
486 * For debugging purposes: dump the contents of a replication entry.
487 * to the given stream.
500 LDAP_LOG ( SLURPD, ERR, "Re_dump: re is NULL\n", 0, 0, 0 );
502 Debug( LDAP_DEBUG_TRACE, "Re_dump: re is NULL\n", 0, 0, 0 );
506 fprintf( fp, "Re_dump: ******\n" );
507 fprintf( fp, "re_refcnt: %d\n", re->re_refcnt );
508 fprintf( fp, "re_timestamp: %ld\n", (long) re->re_timestamp );
509 fprintf( fp, "re_seq: %d\n", re->re_seq );
510 for ( i = 0; re->re_replicas && re->re_replicas[ i ].rh_hostname != NULL;
512 fprintf( fp, "re_replicas[%d]: %s:%d\n",
513 i, re->re_replicas[ i ].rh_hostname,
514 re->re_replicas[ i ].rh_port );
516 fprintf( fp, "re_dn: %s\n", re->re_dn );
517 switch ( re->re_changetype ) {
519 fprintf( fp, "re_changetype: add\n" );
522 fprintf( fp, "re_changetype: modify\n" );
525 fprintf( fp, "re_changetype: delete\n" );
528 fprintf( fp, "re_changetype: modrdn\n" );
531 fprintf( fp, "re_changetype: (unknown, type = %d\n",
534 if ( re->re_mods == NULL ) {
535 fprintf( fp, "re_mods: (none)\n" );
538 fprintf( fp, "re_mods:\n" );
539 for ( i = 0; mi[ i ].mi_type != NULL; i++ ) {
540 fprintf( fp, " %s, \"%s\", (%d bytes)\n",
542 mi[ i ].mi_val == NULL ? "(null)" : mi[ i ].mi_val,
551 * Given an Ri, an Re, and a file pointer, write a replication record to
552 * the file pointer. If ri is NULL, then include all replicas in the
553 * output. If ri is non-NULL, then only include a single "replica:" line
554 * (used when writing rejection records). Returns 0 on success, -1
555 * on failure. Note that Re_write will not write anything out if the
568 if ( re == NULL || fp == NULL ) {
570 LDAP_LOG ( SLURPD, ERR,
571 "Re_write: Internal error: NULL argument\n", 0, 0, 0 );
573 Debug( LDAP_DEBUG_ANY, "Internal error: Re_write: NULL argument\n",
579 if ( re->re_refcnt < 1 ) {
580 return 0; /* this is not an error */
583 if ( ri != NULL ) { /* write a single "replica:" line */
584 if ( fprintf( fp, "replica: %s:%d\n", ri->ri_hostname,
585 ri->ri_port ) < 0 ) {
589 } else { /* write multiple "replica:" lines */
590 for ( i = 0; re->re_replicas[ i ].rh_hostname != NULL; i++ ) {
591 if ( fprintf( fp, "replica: %s:%d\n",
592 re->re_replicas[ i ].rh_hostname,
593 re->re_replicas[ i ].rh_port ) < 0 ) {
599 if ( fprintf( fp, "time: %ld.%d\n", (long) re->re_timestamp, re->re_seq ) < 0 ) {
603 if ( fprintf( fp, "dn: %s\n", re->re_dn ) < 0 ) {
607 if ( fprintf( fp, "changetype: " ) < 0 ) {
611 switch ( re->re_changetype ) {
625 s = "IllegalModifyType!!!";
627 if ( fprintf( fp, "%s\n", s ) < 0 ) {
631 for ( i = 0; (( re->re_mods != NULL ) &&
632 ( re->re_mods[ i ].mi_type != NULL )); i++ ) {
633 if ( !strcmp( re->re_mods[ i ].mi_type, T_MODSEPSTR )) {
634 if ( fprintf( fp, "%s\n", T_MODSEPSTR ) < 0 ) {
640 obuf = ldif_put( LDIF_PUT_VALUE,
641 re->re_mods[ i ].mi_type,
642 re->re_mods[ i ].mi_val ? re->re_mods[ i ].mi_val : "",
643 re->re_mods[ i ].mi_len );
644 if ( fputs( obuf, fp ) < 0 ) {
653 if ( fprintf( fp, "\n" ) < 0 ) {
657 if ( fflush( fp ) != 0 ) {
664 LDAP_LOG ( SLURPD, ERR,
665 "Re_write: Error while writing: %s\n", sys_errlist[ errno ], 0, 0 );
667 Debug( LDAP_DEBUG_ANY, "Error while writing: %s\n",
668 sys_errlist[ errno ], 0, 0 );
678 * Decrement the refcnt. Locking handled internally.
694 * Get the refcnt. Locking handled internally.
714 * Lock this replication entry
721 return( ldap_pvt_thread_mutex_lock( &re->re_mutex ));
728 * Unlock this replication entry
735 return( ldap_pvt_thread_mutex_unlock( &re->re_mutex ));
742 * Instantiate and initialize an Re.
750 (*re) = (Re *) malloc( sizeof( Re ));
755 /* Fill in the member function pointers */
756 (*re)->re_free = Re_free;
757 (*re)->re_getnext = Re_getnext;
758 (*re)->re_parse = Re_parse;
759 (*re)->re_write = Re_write;
760 (*re)->re_dump = Re_dump;
761 (*re)->re_lock = Re_lock;
762 (*re)->re_unlock = Re_unlock;
763 (*re)->re_decrefcnt = Re_decrefcnt;
764 (*re)->re_getrefcnt = Re_getrefcnt;
766 /* Initialize private data */
767 (*re)->re_refcnt = sglob->num_replicas;
768 (*re)->re_timestamp = (time_t) 0L;
769 (*re)->re_replicas = NULL;
771 (*re)->re_changetype = 0;
773 (*re)->re_mods = NULL;
774 (*re)->re_next = NULL;
776 ldap_pvt_thread_mutex_init( &((*re)->re_mutex) );
784 * Given a host and port, generate a warning message iff we haven't already
785 * generated a message for this host:port combination.
788 warn_unknown_replica(
796 for ( i = 0; i < nur; i++ ) {
797 if ( strcmp( ur[ i ].rh_hostname, host )) {
800 if ( ur[ i ].rh_port == port ) {
807 LDAP_LOG ( SLURPD, WARNING, "warn_unknown_replica: "
808 "Warning: unknown replica %s:%d found in replication log\n",
811 Debug( LDAP_DEBUG_ANY,
812 "Warning: unknown replica %s:%d found in replication log\n",
816 ur = (Rh *) ch_realloc( (char *) ur, ( nur * sizeof( Rh )));
817 ur[ nur - 1 ].rh_hostname = strdup( host );
818 ur[ nur - 1 ].rh_port = port;