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.
26 #include <ac/socket.h>
27 #include <ac/string.h>
33 #include "../slapd/slap.h"
35 /* Forward references */
36 static Rh *get_repl_hosts LDAP_P(( char *, int *, char ** ));
37 static int gettype LDAP_P(( char * ));
38 static int getchangetype LDAP_P(( char * ));
39 static int Re_parse LDAP_P(( Re *re, char *replbuf ));
40 static void Re_dump LDAP_P(( Re *re, FILE *fp ));
41 static void warn_unknown_replica LDAP_P(( char *, int port ));
43 /* Globals, scoped within this file */
44 static int nur = 0; /* Number of unknown replicas */
45 static Rh *ur = NULL; /* array of unknown replica names */
49 * Return the next Re in a linked list.
56 return(( re == NULL ) ? NULL : re->re_next );
64 * ??? Something should apparently return nonzero here, but I dont know what.
78 if ( re->re_refcnt > 0 ) {
79 Debug( LDAP_DEBUG_ANY,
80 "Warning: freeing re (dn: %s) with nonzero refcnt\n",
84 ldap_pvt_thread_mutex_destroy( &re->re_mutex );
86 ch_free( re->re_timestamp );
87 if (( rh = re->re_replicas ) != NULL ) {
88 for ( i = 0; rh[ i ].rh_hostname != NULL; i++ ) {
89 free( rh[ i ].rh_hostname );
94 if (( mi = re->re_mods ) != NULL ) {
95 for ( i = 0; mi[ i ].mi_type != NULL; i++ ) {
96 free( mi[ i ].mi_type );
97 ch_free( mi[ i ].mi_val );
109 * Read a buffer of data from a replication log file and fill in
110 * an (already allocated) Re.
116 #define GOT_CHANGETYPE 4
117 #define GOT_ALL ( GOT_DN | GOT_TIME | GOT_CHANGETYPE )
134 Debug( LDAP_DEBUG_ANY, "Re_parse: error: re is NULL\n", 0, 0, 0 );
137 if ( replbuf == NULL ) {
138 Debug( LDAP_DEBUG_ANY, "Re_parse: error: replbuf is NULL\n", 0, 0, 0 );
143 nml = 0; /* number of modification information entries */
146 re->re_replicas = get_repl_hosts( replbuf, &nreplicas, &rp );
147 re->re_refcnt = sglob->num_replicas;
150 if (( state == GOT_ALL ) || ( buf = ldif_getline( &rp )) == NULL ) {
154 * If we're processing a rejection log, then the first line
155 * of each replication record will begin with "ERROR" - just
158 if ( strncmp( buf, ERROR_STR, strlen( ERROR_STR )) == 0 ) {
161 buflen = strlen( buf );
162 if ( ldif_parse_line( buf, &type, &value, &len ) < 0 ) {
163 Debug( LDAP_DEBUG_ANY,
164 "Error: Re_parse: malformed replog file\n",
168 switch ( gettype( type )) {
170 re->re_changetype = getchangetype( value );
171 state |= GOT_CHANGETYPE;
174 if (( p = strchr( value, '.' )) != NULL ) {
175 /* there was a sequence number */
178 re->re_timestamp = strdup( value );
179 if ( p != NULL && isdigit( (unsigned char) *p )) {
180 re->re_seq = atoi( p );
185 re->re_dn = ch_malloc( len + 1 );
186 memcpy( re->re_dn, value, len );
187 re->re_dn[ len ]='\0';
191 if ( !( state == GOT_ALL )) {
192 Debug( LDAP_DEBUG_ANY,
193 "Error: Re_parse: bad type <%s>\n",
206 if ( state != GOT_ALL ) {
207 Debug( LDAP_DEBUG_ANY,
208 "Error: Re_parse: malformed replog file\n",
214 char *const dash = "-";
216 if (( buf = ldif_getline( &rp )) == NULL ) {
219 buflen = strlen( buf );
220 if (( buflen == 1 ) && ( buf[ 0 ] == '-' )) {
224 if ( ldif_parse_line( buf, &type, &value, &len ) < 0 ) {
225 Debug( LDAP_DEBUG_ANY,
226 "Error: malformed replog line \"%s\"\n",
231 re->re_mods = ( Mi *) ch_realloc( (char *) re->re_mods,
232 sizeof( Mi ) * ( nml + 2 ));
233 re->re_mods[ nml ].mi_type = strdup( type );
234 if ( value != NULL ) {
235 re->re_mods[ nml ].mi_val = ch_malloc( len + 1 );
236 memcpy( re->re_mods[ nml ].mi_val, value, len );
237 re->re_mods[ nml ].mi_val[ len ] = '\0';
238 re->re_mods[ nml ].mi_len = len;
240 re->re_mods[ nml ].mi_val = NULL;
241 re->re_mods[ nml ].mi_len = 0;
243 re->re_mods[ nml + 1 ].mi_type = NULL;
244 re->re_mods[ nml + 1 ].mi_val = NULL;
258 * Extract the replication hosts from a repl buf. Check to be sure that
259 * each replica host and port number are ones we know about (that is, they're
260 * in the slapd config file we read at startup). Without that information
261 * from the config file, we won't have the appropriate credentials to
262 * make modifications. If there are any unknown replica names, don't
263 * add them the the Re struct. Instead, log a warning message.
272 char *type, *value, *line, *p;
280 if ( replbuf == NULL ) {
287 * Get the host names of the replicas
292 /* If this is a reject log, we need to skip over the ERROR: line */
293 if ( !strncmp( *r_rp, ERROR_STR, strlen( ERROR_STR ))) {
294 line = ldif_getline( r_rp );
295 if ( line == NULL ) {
299 if ( strncasecmp( *r_rp, "replica:", 7 )) {
302 line = ldif_getline( r_rp );
303 if ( line == NULL ) {
306 if ( ldif_parse_line( line, &type, &value, &len ) < 0 ) {
310 if (( p = strchr( value, ':' )) != NULL ) {
318 /* Verify that we've heard of this replica before */
320 for ( i = 0; i < sglob->num_replicas; i++ ) {
321 if ( strcmp( sglob->replicas[ i ]->ri_hostname, value )) {
324 if ( sglob->replicas[ i ]->ri_port == port ) {
331 warn_unknown_replica( value, port );
337 rh = (Rh *) ch_realloc((char *) rh, ( nreplicas + 2 ) * sizeof( Rh ));
339 Debug( LDAP_DEBUG_ANY, "Out of memory in get_repl_hosts\n",
343 rh[ nreplicas ].rh_hostname = strdup( value );
344 rh[ nreplicas ].rh_port = port;
351 if ( nreplicas == 0 ) {
355 rh[ nreplicas ].rh_hostname = NULL;
356 *r_nreplicas = nreplicas;
366 * Convert "type" to an int.
373 if ( !strcmp( type, T_CHANGETYPESTR )) {
374 return( T_CHANGETYPE );
376 if ( !strcmp( type, T_TIMESTR )) {
379 if ( !strcmp( type, T_DNSTR )) {
388 * Convert "changetype" to an int.
395 if ( !strcmp( changetype, T_ADDCTSTR )) {
398 if ( !strcmp( changetype, T_MODIFYCTSTR )) {
399 return( T_MODIFYCT );
401 if ( !strcmp( changetype, T_DELETECTSTR )) {
402 return( T_DELETECT );
404 if ( !strcmp( changetype, T_MODRDNCTSTR )) {
405 return( T_MODRDNCT );
414 * Find the first line which is not a "replica:" line in buf.
415 * Returns a pointer to the line. Returns NULL if there are
416 * only "replica:" lines in buf.
425 if ( strncasecmp( p, "replica:", 8 )) {
428 while (( *p != '\0' ) && ( *p != '\n' )) {
443 * For debugging purposes: dump the contents of a replication entry.
444 * to the given stream.
456 Debug( LDAP_DEBUG_TRACE, "Re_dump: re is NULL\n", 0, 0, 0 );
459 fprintf( fp, "Re_dump: ******\n" );
460 fprintf( fp, "re_refcnt: %d\n", re->re_refcnt );
461 fprintf( fp, "re_timestamp: %s\n", re->re_timestamp );
462 fprintf( fp, "re_seq: %d\n", re->re_seq );
463 for ( i = 0; re->re_replicas && re->re_replicas[ i ].rh_hostname != NULL;
465 fprintf( fp, "re_replicas[%d]: %s:%d\n",
466 i, re->re_replicas[ i ].rh_hostname,
467 re->re_replicas[ i ].rh_port );
469 fprintf( fp, "re_dn: %s\n", re->re_dn );
470 switch ( re->re_changetype ) {
472 fprintf( fp, "re_changetype: add\n" );
475 fprintf( fp, "re_changetype: modify\n" );
478 fprintf( fp, "re_changetype: delete\n" );
481 fprintf( fp, "re_changetype: modrdn\n" );
484 fprintf( fp, "re_changetype: (unknown, type = %d\n",
487 if ( re->re_mods == NULL ) {
488 fprintf( fp, "re_mods: (none)\n" );
491 fprintf( fp, "re_mods:\n" );
492 for ( i = 0; mi[ i ].mi_type != NULL; i++ ) {
493 fprintf( fp, " %s, \"%s\", (%d bytes)\n",
495 mi[ i ].mi_val == NULL ? "(null)" : mi[ i ].mi_val,
504 * Given an Ri, an Re, and a file pointer, write a replication record to
505 * the file pointer. If ri is NULL, then include all replicas in the
506 * output. If ri is non-NULL, then only include a single "replica:" line
507 * (used when writing rejection records). Returns 0 on success, -1
508 * on failure. Note that Re_write will not write anything out if the
521 if ( re == NULL || fp == NULL ) {
522 Debug( LDAP_DEBUG_ANY, "Internal error: Re_write: NULL argument\n",
527 if ( re->re_refcnt < 1 ) {
528 return 0; /* this is not an error */
531 if ( ri != NULL ) { /* write a single "replica:" line */
532 if ( fprintf( fp, "replica: %s:%d\n", ri->ri_hostname,
533 ri->ri_port ) < 0 ) {
537 } else { /* write multiple "replica:" lines */
538 for ( i = 0; re->re_replicas[ i ].rh_hostname != NULL; i++ ) {
539 if ( fprintf( fp, "replica: %s:%d\n",
540 re->re_replicas[ i ].rh_hostname,
541 re->re_replicas[ i ].rh_port ) < 0 ) {
547 if ( fprintf( fp, "time: %s.%d\n", re->re_timestamp, re->re_seq ) < 0 ) {
551 if ( fprintf( fp, "dn: %s\n", re->re_dn ) < 0 ) {
555 if ( fprintf( fp, "changetype: " ) < 0 ) {
559 switch ( re->re_changetype ) {
573 s = "IllegalModifyType!!!";
575 if ( fprintf( fp, "%s\n", s ) < 0 ) {
579 for ( i = 0; (( re->re_mods != NULL ) &&
580 ( re->re_mods[ i ].mi_type != NULL )); i++ ) {
581 if ( !strcmp( re->re_mods[ i ].mi_type, T_MODSEPSTR )) {
582 if ( fprintf( fp, "%s\n", T_MODSEPSTR ) < 0 ) {
588 obuf = ldif_put( LDIF_PUT_VALUE,
589 re->re_mods[ i ].mi_type,
590 re->re_mods[ i ].mi_val ? re->re_mods[ i ].mi_val : "",
591 re->re_mods[ i ].mi_len );
592 if ( fputs( obuf, fp ) < 0 ) {
601 if ( fprintf( fp, "\n" ) < 0 ) {
605 if ( fflush( fp ) != 0 ) {
611 Debug( LDAP_DEBUG_ANY, "Error while writing: %s\n",
612 sys_errlist[ errno ], 0, 0 );
621 * Decrement the refcnt. Locking handled internally.
637 * Get the refcnt. Locking handled internally.
657 * Lock this replication entry
664 return( ldap_pvt_thread_mutex_lock( &re->re_mutex ));
671 * Unlock this replication entry
678 return( ldap_pvt_thread_mutex_unlock( &re->re_mutex ));
685 * Instantiate and initialize an Re.
693 (*re) = (Re *) malloc( sizeof( Re ));
698 /* Fill in the member function pointers */
699 (*re)->re_free = Re_free;
700 (*re)->re_getnext = Re_getnext;
701 (*re)->re_parse = Re_parse;
702 (*re)->re_write = Re_write;
703 (*re)->re_dump = Re_dump;
704 (*re)->re_lock = Re_lock;
705 (*re)->re_unlock = Re_unlock;
706 (*re)->re_decrefcnt = Re_decrefcnt;
707 (*re)->re_getrefcnt = Re_getrefcnt;
709 /* Initialize private data */
710 (*re)->re_refcnt = sglob->num_replicas;
711 (*re)->re_timestamp = NULL;
712 (*re)->re_replicas = NULL;
714 (*re)->re_changetype = 0;
716 (*re)->re_mods = NULL;
717 (*re)->re_next = NULL;
719 ldap_pvt_thread_mutex_init( &((*re)->re_mutex) );
727 * Given a host and port, generate a warning message iff we haven't already
728 * generated a message for this host:port combination.
731 warn_unknown_replica(
739 for ( i = 0; i < nur; i++ ) {
740 if ( strcmp( ur[ i ].rh_hostname, host )) {
743 if ( ur[ i ].rh_port == port ) {
749 Debug( LDAP_DEBUG_ANY,
750 "Warning: unknown replica %s:%d found in replication log\n",
753 ur = (Rh *) ch_realloc( (char *) ur, ( nur * sizeof( Rh )));
754 ur[ nur - 1 ].rh_hostname = strdup( host );
755 ur[ nur - 1 ].rh_port = port;