2 * Copyright (c) 1996 Regents of the University of Michigan.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of Michigan at Ann Arbor. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
14 * re.c - routines which deal with Re (Replication entry) structures.
15 * An Re struct is an in-core representation of one replication to
16 * be performed, along with member functions which are called by other
17 * routines. The Re struct is defined in slurp.h.
23 #include <sys/types.h>
24 #include <sys/socket.h>
26 #include "../slapd/slap.h"
31 extern char *str_getline( char **next );
32 extern void ch_free( char *p );
34 #ifndef SYSERRLIST_IN_STDIO
35 extern char *sys_errlist[];
36 #endif /* SYSERRLIST_IN_STDIO */
38 /* Forward references */
39 static Rh *get_repl_hosts( char *, int *, char ** );
40 static int gettype( char * );
41 static int getchangetype( char *);
42 static int Re_parse( Re *re, char *replbuf );
43 static void Re_dump( Re *re, FILE *fp );
44 static void warn_unknown_replica( char *, int port );
46 /* Globals, scoped within this file */
47 static int nur = 0; /* Number of unknown replicas */
48 static Rh *ur = NULL; /* array of unknown replica names */
52 * Return the next Re in a linked list.
59 return(( re == NULL ) ? NULL : re->re_next );
80 if ( re->re_refcnt > 0 ) {
81 Debug( LDAP_DEBUG_ANY,
82 "Warning: freeing re (dn: %s) with nonzero refcnt\n",
85 #if !defined( THREAD_SUNOS4_LWP )
86 /* This seems to have problems under SunOS lwp */
87 pthread_mutex_destroy( &re->re_mutex );
88 #endif /* THREAD_SUNOS4_LWP */
89 ch_free( re->re_timestamp );
90 if (( rh = re->re_replicas ) != NULL ) {
91 for ( i = 0; rh[ i ].rh_hostname != NULL; i++ ) {
92 free( rh[ i ].rh_hostname );
97 if (( mi = re->re_mods ) != NULL ) {
98 for ( i = 0; mi[ i ].mi_type != NULL; i++ ) {
99 free( mi[ i ].mi_type );
100 ch_free( mi[ i ].mi_val );
111 * Read a buffer of data from a replication log file and fill in
112 * an (already allocated) Re.
118 #define GOT_CHANGETYPE 4
119 #define GOT_ALL ( GOT_DN | GOT_TIME | GOT_CHANGETYPE )
136 Debug( LDAP_DEBUG_ANY, "Re_parse: error: re is NULL\n", 0, 0, 0 );
139 if ( replbuf == NULL ) {
140 Debug( LDAP_DEBUG_ANY, "Re_parse: error: replbuf is NULL\n", 0, 0, 0 );
145 nml = 0; /* number of modification information entries */
148 re->re_replicas = get_repl_hosts( replbuf, &nreplicas, &rp );
149 re->re_refcnt = sglob->num_replicas;
152 if (( state == GOT_ALL ) || ( buf = str_getline( &rp )) == NULL ) {
156 * If we're processing a rejection log, then the first line
157 * of each replication record will begin with "ERROR" - just
160 if ( strncmp( buf, ERROR_STR, strlen( ERROR_STR )) == 0 ) {
163 buflen = ( long ) strlen( buf );
164 if ( str_parse_line( buf, &type, &value, &len ) < 0 ) {
165 Debug( LDAP_DEBUG_ANY,
166 "Error: Re_parse: malformed replog file\n",
170 switch ( gettype( type )) {
172 re->re_changetype = getchangetype( value );
173 state |= GOT_CHANGETYPE;
176 if (( p = strchr( value, '.' )) != NULL ) {
177 /* there was a sequence number */
180 re->re_timestamp = strdup( value );
181 if ( p != NULL && isdigit( *p )) {
182 re->re_seq = atoi( p );
187 re->re_dn = calloc( 1, len + 1 );
188 memcpy( re->re_dn, value, len );
192 if ( !( state == GOT_ALL )) {
193 Debug( LDAP_DEBUG_ANY,
194 "Error: Re_parse: bad type <%s>\n",
201 if ( state != GOT_ALL ) {
202 Debug( LDAP_DEBUG_ANY,
203 "Error: Re_parse: malformed replog file\n",
209 if (( buf = str_getline( &rp )) == NULL ) {
212 buflen = ( long ) strlen( buf );
213 if (( buflen == 1 ) && ( buf[ 0 ] == '-' )) {
217 if ( str_parse_line( buf, &type, &value, &len ) < 0 ) {
218 Debug( LDAP_DEBUG_ANY,
219 "Error: malformed replog line \"%s\"\n",
224 re->re_mods = ( Mi *) ch_realloc( (char *) re->re_mods,
225 sizeof( Mi ) * ( nml + 2 ));
226 re->re_mods[ nml ].mi_type = strdup( type );
227 if ( value != NULL ) {
228 re->re_mods[ nml ].mi_val = calloc( 1, len + 1 );
229 memcpy( re->re_mods[ nml ].mi_val, value, len );
230 re->re_mods[ nml ].mi_len = len;
232 re->re_mods[ nml ].mi_val = NULL;
233 re->re_mods[ nml ].mi_len = 0;
235 re->re_mods[ nml + 1 ].mi_type = NULL;
236 re->re_mods[ nml + 1 ].mi_val = NULL;
245 * Extract the replication hosts from a repl buf. Check to be sure that
246 * each replica host and port number are ones we know about (that is, they're
247 * in the slapd config file we read at startup). Without that information
248 * from the config file, we won't have the appropriate credentials to
249 * make modifications. If there are any unknown replica names, don't
250 * add them the the Re struct. Instead, log a warning message.
259 char buf[ LINE_WIDTH + 1 ];
260 char *type, *value, *line, *p;
267 if ( replbuf == NULL ) {
274 * Get the host names of the replicas
279 /* If this is a reject log, we need to skip over the ERROR: line */
280 if ( !strncmp( *r_rp, ERROR_STR, strlen( ERROR_STR ))) {
281 line = str_getline( r_rp );
282 if ( line == NULL ) {
286 if ( strncasecmp( *r_rp, "replica:", 7 )) {
289 line = str_getline( r_rp );
290 if ( line == NULL ) {
293 if ( str_parse_line( line, &type, &value, &len ) < 0 ) {
297 if (( p = strchr( value, ':' )) != NULL ) {
305 /* Verify that we've heard of this replica before */
307 for ( i = 0; i < sglob->num_replicas; i++ ) {
308 if ( strcmp( sglob->replicas[ i ]->ri_hostname, value )) {
311 if ( sglob->replicas[ i ]->ri_port == port ) {
317 warn_unknown_replica( value, port );
321 rh = (Rh *) ch_realloc((char *) rh, ( nreplicas + 2 ) * sizeof( Rh ));
323 Debug( LDAP_DEBUG_ANY, "Out of memory in get_repl_hosts\n",
327 rh[ nreplicas ].rh_hostname = strdup( value );
328 rh[ nreplicas ].rh_port = port;
332 if ( nreplicas == 0 ) {
336 rh[ nreplicas ].rh_hostname = NULL;
337 *r_nreplicas = nreplicas;
347 * Convert "type" to an int.
354 if ( !strcmp( type, T_CHANGETYPESTR )) {
355 return( T_CHANGETYPE );
357 if ( !strcmp( type, T_TIMESTR )) {
360 if ( !strcmp( type, T_DNSTR )) {
369 * Convert "changetype" to an int.
376 if ( !strcmp( changetype, T_ADDCTSTR )) {
379 if ( !strcmp( changetype, T_MODIFYCTSTR )) {
380 return( T_MODIFYCT );
382 if ( !strcmp( changetype, T_DELETECTSTR )) {
383 return( T_DELETECT );
385 if ( !strcmp( changetype, T_MODRDNCTSTR )) {
386 return( T_MODRDNCT );
395 * Find the first line which is not a "replica:" line in buf.
396 * Returns a pointer to the line. Returns NULL if there are
397 * only "replica:" lines in buf.
406 if ( strncasecmp( p, "replica:", 8 )) {
409 while (( *p != '\0' ) && ( *p != '\n' )) {
424 * For debugging purposes: dump the contents of a replication entry.
425 * to the given stream.
437 Debug( LDAP_DEBUG_TRACE, "Re_dump: re is NULL\n", 0, 0, 0 );
440 fprintf( fp, "Re_dump: ******\n" );
441 fprintf( fp, "re_refcnt: %d\n", re->re_refcnt );
442 fprintf( fp, "re_timestamp: %s\n", re->re_timestamp );
443 fprintf( fp, "re_seq: %d\n", re->re_seq );
444 for ( i = 0; re->re_replicas && re->re_replicas[ i ].rh_hostname != NULL;
446 fprintf( fp, "re_replicas[%d]: %s:%d\n",
447 i, re->re_replicas[ i ].rh_hostname,
448 re->re_replicas[ i ].rh_port );
450 fprintf( fp, "re_dn: %s\n", re->re_dn );
451 switch ( re->re_changetype ) {
453 fprintf( fp, "re_changetype: add\n" );
456 fprintf( fp, "re_changetype: modify\n" );
459 fprintf( fp, "re_changetype: delete\n" );
462 fprintf( fp, "re_changetype: modrdn\n" );
465 fprintf( fp, "re_changetype: (unknown, type = %d\n",
468 if ( re->re_mods == NULL ) {
469 fprintf( fp, "re_mods: (none)\n" );
472 fprintf( fp, "re_mods:\n" );
473 for ( i = 0; mi[ i ].mi_type != NULL; i++ ) {
474 fprintf( fp, " %s, \"%s\", (%d bytes)\n",
476 mi[ i ].mi_val == NULL ? "(null)" : mi[ i ].mi_val,
485 * Given an Ri, an Re, and a file pointer, write a replication record to
486 * the file pointer. If ri is NULL, then include all replicas in the
487 * output. If ri is non-NULL, then only include a single "replica:" line
488 * (used when writing rejection records). Returns 0 on success, -1
489 * on failure. Note that Re_write will not write anything out if the
503 if ( re == NULL || fp == NULL ) {
504 Debug( LDAP_DEBUG_ANY, "Internal error: Re_write: NULL argument\n",
509 if ( re->re_refcnt < 1 ) {
510 return 0; /* this is not an error */
513 if ( ri != NULL ) { /* write a single "replica:" line */
514 if ( fprintf( fp, "replica: %s:%d\n", ri->ri_hostname,
515 ri->ri_port ) < 0 ) {
519 } else { /* write multiple "replica:" lines */
520 for ( i = 0; re->re_replicas[ i ].rh_hostname != NULL; i++ ) {
521 if ( fprintf( fp, "replica: %s:%d\n",
522 re->re_replicas[ i ].rh_hostname,
523 re->re_replicas[ i ].rh_port ) < 0 ) {
529 if ( fprintf( fp, "time: %s.%d\n", re->re_timestamp, re->re_seq ) < 0 ) {
533 if ( fprintf( fp, "dn: %s\n", re->re_dn ) < 0 ) {
537 if ( fprintf( fp, "changetype: " ) < 0 ) {
541 switch ( re->re_changetype ) {
555 s = "IllegalModifyType!!!";
557 if ( fprintf( fp, "%s\n", s ) < 0 ) {
561 for ( i = 0; (( re->re_mods != NULL ) &&
562 ( re->re_mods[ i ].mi_type != NULL )); i++ ) {
563 if ( !strcmp( re->re_mods[ i ].mi_type, T_MODSEPSTR )) {
564 if ( fprintf( fp, "%s\n", T_MODSEPSTR ) < 0 ) {
570 obuf = ldif_type_and_value( re->re_mods[ i ].mi_type,
571 re->re_mods[ i ].mi_val ? re->re_mods[ i ].mi_val : "",
572 re->re_mods[ i ].mi_len );
573 if ( fputs( obuf, fp ) < 0 ) {
582 if ( fprintf( fp, "\n" ) < 0 ) {
586 if ( fflush( fp ) != 0 ) {
592 Debug( LDAP_DEBUG_ANY, "Error while writing: %s\n",
593 sys_errlist[ errno ], 0, 0 );
602 * Decrement the refcnt. Locking handled internally.
618 * Get the refcnt. Locking handled internally.
638 * Lock this replication entry
645 return( pthread_mutex_lock( &re->re_mutex ));
652 * Unlock this replication entry
659 return( pthread_mutex_unlock( &re->re_mutex ));
666 * Instantiate and initialize an Re.
674 (*re) = (Re *) malloc( sizeof( Re ));
679 /* Fill in the member function pointers */
680 (*re)->re_free = Re_free;
681 (*re)->re_getnext = Re_getnext;
682 (*re)->re_parse = Re_parse;
683 (*re)->re_write = Re_write;
684 (*re)->re_dump = Re_dump;
685 (*re)->re_lock = Re_lock;
686 (*re)->re_unlock = Re_unlock;
687 (*re)->re_decrefcnt = Re_decrefcnt;
688 (*re)->re_getrefcnt = Re_getrefcnt;
690 /* Initialize private data */
691 (*re)->re_refcnt = sglob->num_replicas;
692 (*re)->re_timestamp = NULL;
693 (*re)->re_replicas = NULL;
695 (*re)->re_changetype = 0;
697 (*re)->re_mods = NULL;
698 (*re)->re_next = NULL;
700 pthread_mutex_init( &((*re)->re_mutex), pthread_mutexattr_default );
708 * Given a host and port, generate a warning message iff we haven't already
709 * generated a message for this host:port combination.
712 warn_unknown_replica(
720 for ( i = 0; i < nur; i++ ) {
721 if ( strcmp( ur[ i ].rh_hostname, host )) {
724 if ( ur[ i ].rh_port == port ) {
730 Debug( LDAP_DEBUG_ANY,
731 "Warning: unknown replica %s:%d found in replication log\n",
734 ur = (Rh *) ch_realloc( (char *) ur, ( nur * sizeof( Rh )));
735 ur[ nur - 1 ].rh_hostname = strdup( host );
736 ur[ nur - 1 ].rh_port = port;