2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 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 ) {
97 Debug( LDAP_DEBUG_ANY,
98 "Warning: freeing re (dn: %s) with nonzero refcnt\n",
102 ldap_pvt_thread_mutex_destroy( &re->re_mutex );
104 if (( rh = re->re_replicas ) != NULL ) {
105 for ( i = 0; rh[ i ].rh_hostname != NULL; i++ ) {
106 free( rh[ i ].rh_hostname );
110 ch_free( re->re_dn );
111 if (( mi = re->re_mods ) != NULL ) {
112 for ( i = 0; mi[ i ].mi_type != NULL; i++ ) {
113 free( mi[ i ].mi_type );
114 ch_free( mi[ i ].mi_val );
126 * Read a buffer of data from a replication log file and fill in
127 * an (already allocated) Re.
133 #define GOT_CHANGETYPE 4
134 #define GOT_ALL ( GOT_DN | GOT_TIME | GOT_CHANGETYPE )
151 Debug( LDAP_DEBUG_ANY, "Re_parse: error: re is NULL\n", 0, 0, 0 );
154 if ( replbuf == NULL ) {
155 Debug( LDAP_DEBUG_ANY, "Re_parse: error: replbuf is NULL\n", 0, 0, 0 );
160 nml = 0; /* number of modification information entries */
163 re->re_replicas = get_repl_hosts( replbuf, &nreplicas, &rp );
164 re->re_refcnt = sglob->num_replicas;
167 if (( state == GOT_ALL ) || ( buf = ldif_getline( &rp )) == NULL ) {
171 * If we're processing a rejection log, then the first line
172 * of each replication record will begin with "ERROR" - just
175 if ( strncmp( buf, ERROR_STR, strlen( ERROR_STR )) == 0 ) {
178 buflen = strlen( buf );
179 if ( ldif_parse_line( buf, &type, &value, &len ) < 0 ) {
180 Debug( LDAP_DEBUG_ANY,
181 "Error: Re_parse: malformed replog file\n",
185 switch ( gettype( type )) {
187 re->re_changetype = getchangetype( value );
188 state |= GOT_CHANGETYPE;
191 if (( p = strchr( value, '.' )) != NULL ) {
192 /* there was a sequence number */
195 re->re_timestamp = atol( value );
196 if ( p != NULL && isdigit( (unsigned char) *p )) {
197 re->re_seq = atoi( p );
202 re->re_dn = ch_malloc( len + 1 );
203 AC_MEMCPY( re->re_dn, value, len );
204 re->re_dn[ len ]='\0';
208 if ( !( state == GOT_ALL )) {
209 Debug( LDAP_DEBUG_ANY,
210 "Error: Re_parse: bad type <%s>\n",
221 if ( state != GOT_ALL ) {
222 Debug( LDAP_DEBUG_ANY,
223 "Error: Re_parse: malformed replog file\n",
229 char *const dash = "-";
231 if (( buf = ldif_getline( &rp )) == NULL ) {
234 buflen = strlen( buf );
235 if (( buflen == 1 ) && ( buf[ 0 ] == '-' )) {
239 if ( ldif_parse_line( buf, &type, &value, &len ) < 0 ) {
240 Debug( LDAP_DEBUG_ANY,
241 "Error: malformed replog line \"%s\"\n",
246 re->re_mods = ( Mi *) ch_realloc( (char *) re->re_mods,
247 sizeof( Mi ) * ( nml + 2 ));
248 re->re_mods[ nml ].mi_type = strdup( type );
249 if ( value != NULL ) {
250 re->re_mods[ nml ].mi_val = ch_malloc( len + 1 );
251 AC_MEMCPY( re->re_mods[ nml ].mi_val, value, len );
252 re->re_mods[ nml ].mi_val[ len ] = '\0';
253 re->re_mods[ nml ].mi_len = len;
255 re->re_mods[ nml ].mi_val = NULL;
256 re->re_mods[ nml ].mi_len = 0;
258 re->re_mods[ nml + 1 ].mi_type = NULL;
259 re->re_mods[ nml + 1 ].mi_val = NULL;
273 * Extract the replication hosts from a repl buf. Check to be sure that
274 * each replica host and port number are ones we know about (that is, they're
275 * in the slapd config file we read at startup). Without that information
276 * from the config file, we won't have the appropriate credentials to
277 * make modifications. If there are any unknown replica names, don't
278 * add them the the Re struct. Instead, log a warning message.
287 char *type, *value, *line, *p;
295 if ( replbuf == NULL ) {
302 * Get the host names of the replicas
307 /* If this is a reject log, we need to skip over the ERROR: line */
308 if ( !strncmp( *r_rp, ERROR_STR, strlen( ERROR_STR ))) {
309 line = ldif_getline( r_rp );
310 if ( line == NULL ) {
314 if ( strncasecmp( *r_rp, "replica:", 7 )) {
317 line = ldif_getline( r_rp );
318 if ( line == NULL ) {
321 if ( ldif_parse_line( line, &type, &value, &len ) < 0 ) {
325 if (( p = strchr( value, ':' )) != NULL ) {
333 /* Verify that we've heard of this replica before */
335 for ( i = 0; i < sglob->num_replicas; i++ ) {
336 if ( strcmp( sglob->replicas[ i ]->ri_hostname, value )) {
339 if ( sglob->replicas[ i ]->ri_port == port ) {
346 warn_unknown_replica( value, port );
351 rh = (Rh *) ch_realloc((char *) rh, ( nreplicas + 2 ) * sizeof( Rh ));
353 Debug( LDAP_DEBUG_ANY, "Out of memory in get_repl_hosts\n",
357 rh[ nreplicas ].rh_hostname = strdup( value );
358 rh[ nreplicas ].rh_port = port;
364 if ( nreplicas == 0 ) {
368 rh[ nreplicas ].rh_hostname = NULL;
369 *r_nreplicas = nreplicas;
379 * Convert "type" to an int.
386 if ( !strcmp( type, T_CHANGETYPESTR )) {
387 return( T_CHANGETYPE );
389 if ( !strcmp( type, T_TIMESTR )) {
392 if ( !strcmp( type, T_DNSTR )) {
401 * Convert "changetype" to an int.
408 if ( !strcmp( changetype, T_ADDCTSTR )) {
411 if ( !strcmp( changetype, T_MODIFYCTSTR )) {
412 return( T_MODIFYCT );
414 if ( !strcmp( changetype, T_DELETECTSTR )) {
415 return( T_DELETECT );
417 if ( !strcmp( changetype, T_MODRDNCTSTR )) {
418 return( T_MODRDNCT );
428 * Find the first line which is not a "replica:" line in buf.
429 * Returns a pointer to the line. Returns NULL if there are
430 * only "replica:" lines in buf.
439 if ( strncasecmp( p, "replica:", 8 )) {
442 while (( *p != '\0' ) && ( *p != '\n' )) {
458 * For debugging purposes: dump the contents of a replication entry.
459 * to the given stream.
471 Debug( LDAP_DEBUG_TRACE, "Re_dump: re is NULL\n", 0, 0, 0 );
474 fprintf( fp, "Re_dump: ******\n" );
475 fprintf( fp, "re_refcnt: %d\n", re->re_refcnt );
476 fprintf( fp, "re_timestamp: %ld\n", (long) re->re_timestamp );
477 fprintf( fp, "re_seq: %d\n", re->re_seq );
478 for ( i = 0; re->re_replicas && re->re_replicas[ i ].rh_hostname != NULL;
480 fprintf( fp, "re_replicas[%d]: %s:%d\n",
481 i, re->re_replicas[ i ].rh_hostname,
482 re->re_replicas[ i ].rh_port );
484 fprintf( fp, "re_dn: %s\n", re->re_dn );
485 switch ( re->re_changetype ) {
487 fprintf( fp, "re_changetype: add\n" );
490 fprintf( fp, "re_changetype: modify\n" );
493 fprintf( fp, "re_changetype: delete\n" );
496 fprintf( fp, "re_changetype: modrdn\n" );
499 fprintf( fp, "re_changetype: (unknown, type = %d\n",
502 if ( re->re_mods == NULL ) {
503 fprintf( fp, "re_mods: (none)\n" );
506 fprintf( fp, "re_mods:\n" );
507 for ( i = 0; mi[ i ].mi_type != NULL; i++ ) {
508 fprintf( fp, " %s, \"%s\", (%d bytes)\n",
510 mi[ i ].mi_val == NULL ? "(null)" : mi[ i ].mi_val,
519 * Given an Ri, an Re, and a file pointer, write a replication record to
520 * the file pointer. If ri is NULL, then include all replicas in the
521 * output. If ri is non-NULL, then only include a single "replica:" line
522 * (used when writing rejection records). Returns 0 on success, -1
523 * on failure. Note that Re_write will not write anything out if the
536 if ( re == NULL || fp == NULL ) {
537 Debug( LDAP_DEBUG_ANY, "Internal error: Re_write: NULL argument\n",
542 if ( re->re_refcnt < 1 ) {
543 return 0; /* this is not an error */
546 if ( ri != NULL ) { /* write a single "replica:" line */
547 if ( fprintf( fp, "replica: %s:%d\n", ri->ri_hostname,
548 ri->ri_port ) < 0 ) {
552 } else { /* write multiple "replica:" lines */
553 for ( i = 0; re->re_replicas && re->re_replicas[ i ].rh_hostname != NULL; i++ ) {
554 if ( fprintf( fp, "replica: %s:%d\n",
555 re->re_replicas[ i ].rh_hostname,
556 re->re_replicas[ i ].rh_port ) < 0 ) {
562 if ( fprintf( fp, "time: %ld.%d\n", (long) re->re_timestamp, re->re_seq ) < 0 ) {
566 if ( fprintf( fp, "dn: %s\n", re->re_dn ) < 0 ) {
570 if ( fprintf( fp, "changetype: " ) < 0 ) {
574 switch ( re->re_changetype ) {
588 s = "IllegalModifyType!!!";
590 if ( fprintf( fp, "%s\n", s ) < 0 ) {
594 for ( i = 0; (( re->re_mods != NULL ) &&
595 ( re->re_mods[ i ].mi_type != NULL )); i++ ) {
596 if ( !strcmp( re->re_mods[ i ].mi_type, T_MODSEPSTR )) {
597 if ( fprintf( fp, "%s\n", T_MODSEPSTR ) < 0 ) {
603 obuf = ldif_put( LDIF_PUT_VALUE,
604 re->re_mods[ i ].mi_type,
605 re->re_mods[ i ].mi_val ? re->re_mods[ i ].mi_val : "",
606 re->re_mods[ i ].mi_len );
607 if ( fputs( obuf, fp ) < 0 ) {
616 if ( fprintf( fp, "\n" ) < 0 ) {
620 if ( fflush( fp ) != 0 ) {
626 Debug( LDAP_DEBUG_ANY, "Error while writing: %s\n",
627 sys_errlist[ errno ], 0, 0 );
636 * Decrement the refcnt. Locking handled internally.
652 * Get the refcnt. Locking handled internally.
672 * Lock this replication entry
679 return( ldap_pvt_thread_mutex_lock( &re->re_mutex ));
686 * Unlock this replication entry
693 return( ldap_pvt_thread_mutex_unlock( &re->re_mutex ));
700 * Instantiate and initialize an Re.
708 (*re) = (Re *) malloc( sizeof( Re ));
713 /* Fill in the member function pointers */
714 (*re)->re_free = Re_free;
715 (*re)->re_getnext = Re_getnext;
716 (*re)->re_parse = Re_parse;
717 (*re)->re_write = Re_write;
718 (*re)->re_dump = Re_dump;
719 (*re)->re_lock = Re_lock;
720 (*re)->re_unlock = Re_unlock;
721 (*re)->re_decrefcnt = Re_decrefcnt;
722 (*re)->re_getrefcnt = Re_getrefcnt;
724 /* Initialize private data */
725 (*re)->re_refcnt = sglob->num_replicas;
726 (*re)->re_timestamp = (time_t) 0L;
727 (*re)->re_replicas = NULL;
729 (*re)->re_changetype = 0;
731 (*re)->re_mods = NULL;
732 (*re)->re_next = NULL;
734 ldap_pvt_thread_mutex_init( &((*re)->re_mutex) );
742 * Given a host and port, generate a warning message iff we haven't already
743 * generated a message for this host:port combination.
746 warn_unknown_replica(
754 for ( i = 0; i < nur; i++ ) {
755 if ( strcmp( ur[ i ].rh_hostname, host )) {
758 if ( ur[ i ].rh_port == port ) {
764 Debug( LDAP_DEBUG_ANY,
765 "Warning: unknown replica %s:%d found in replication log\n",
768 ur = (Rh *) ch_realloc( (char *) ur, ( nur * sizeof( Rh )));
769 ur[ nur - 1 ].rh_hostname = strdup( host );
770 ur[ nur - 1 ].rh_port = port;