3 * Copyright (c) 1996 Regents of the University of Michigan.
6 * Redistribution and use in source and binary forms are permitted
7 * provided that this notice is preserved and that due credit is given
8 * to the University of Michigan at Ann Arbor. The name of the University
9 * may not be used to endorse or promote products derived from this
10 * software without specific prior written permission. This software
11 * is provided ``as is'' without express or implied warranty.
15 * re.c - routines which deal with Re (Replication entry) structures.
16 * An Re struct is an in-core representation of one replication to
17 * be performed, along with member functions which are called by other
18 * routines. The Re struct is defined in slurp.h.
27 #include <ac/socket.h>
28 #include <ac/string.h>
34 #include "../slapd/slap.h"
36 /* Forward references */
37 static Rh *get_repl_hosts LDAP_P(( char *, int *, char ** ));
38 static int gettype LDAP_P(( char * ));
39 static int getchangetype LDAP_P(( char * ));
40 static int Re_parse LDAP_P(( Re *re, char *replbuf ));
41 static void Re_dump LDAP_P(( Re *re, FILE *fp ));
42 static void warn_unknown_replica LDAP_P(( char *, int port ));
44 /* Globals, scoped within this file */
45 static int nur = 0; /* Number of unknown replicas */
46 static Rh *ur = NULL; /* array of unknown replica names */
50 * Return the next Re in a linked list.
57 return(( re == NULL ) ? NULL : re->re_next );
65 * ??? Something should apparently return nonzero here, but I dont know what.
79 if ( re->re_refcnt > 0 ) {
80 Debug( LDAP_DEBUG_ANY,
81 "Warning: freeing re (dn: %s) with nonzero refcnt\n",
85 ldap_pvt_thread_mutex_destroy( &re->re_mutex );
87 ch_free( re->re_timestamp );
88 if (( rh = re->re_replicas ) != NULL ) {
89 for ( i = 0; rh[ i ].rh_hostname != NULL; i++ ) {
90 free( rh[ i ].rh_hostname );
95 if (( mi = re->re_mods ) != NULL ) {
96 for ( i = 0; mi[ i ].mi_type != NULL; i++ ) {
97 free( mi[ i ].mi_type );
98 ch_free( mi[ i ].mi_val );
110 * Read a buffer of data from a replication log file and fill in
111 * an (already allocated) Re.
117 #define GOT_CHANGETYPE 4
118 #define GOT_ALL ( GOT_DN | GOT_TIME | GOT_CHANGETYPE )
135 Debug( LDAP_DEBUG_ANY, "Re_parse: error: re is NULL\n", 0, 0, 0 );
138 if ( replbuf == NULL ) {
139 Debug( LDAP_DEBUG_ANY, "Re_parse: error: replbuf is NULL\n", 0, 0, 0 );
144 nml = 0; /* number of modification information entries */
147 re->re_replicas = get_repl_hosts( replbuf, &nreplicas, &rp );
148 re->re_refcnt = sglob->num_replicas;
151 if (( state == GOT_ALL ) || ( buf = ldif_getline( &rp )) == NULL ) {
155 * If we're processing a rejection log, then the first line
156 * of each replication record will begin with "ERROR" - just
159 if ( strncmp( buf, ERROR_STR, strlen( ERROR_STR )) == 0 ) {
162 buflen = strlen( buf );
163 if ( ldif_parse_line( buf, &type, &value, &len ) < 0 ) {
164 Debug( LDAP_DEBUG_ANY,
165 "Error: Re_parse: malformed replog file\n",
169 switch ( gettype( type )) {
171 re->re_changetype = getchangetype( value );
172 state |= GOT_CHANGETYPE;
175 if (( p = strchr( value, '.' )) != NULL ) {
176 /* there was a sequence number */
179 re->re_timestamp = strdup( value );
180 if ( p != NULL && isdigit( (unsigned char) *p )) {
181 re->re_seq = atoi( p );
186 re->re_dn = ch_malloc( len + 1 );
187 memcpy( re->re_dn, value, len );
188 re->re_dn[ len ]='\0';
192 if ( !( state == GOT_ALL )) {
193 Debug( LDAP_DEBUG_ANY,
194 "Error: Re_parse: bad type <%s>\n",
207 if ( state != GOT_ALL ) {
208 Debug( LDAP_DEBUG_ANY,
209 "Error: Re_parse: malformed replog file\n",
215 char *const dash = "-";
217 if (( buf = ldif_getline( &rp )) == NULL ) {
220 buflen = strlen( buf );
221 if (( buflen == 1 ) && ( buf[ 0 ] == '-' )) {
225 if ( ldif_parse_line( buf, &type, &value, &len ) < 0 ) {
226 Debug( LDAP_DEBUG_ANY,
227 "Error: malformed replog line \"%s\"\n",
232 re->re_mods = ( Mi *) ch_realloc( (char *) re->re_mods,
233 sizeof( Mi ) * ( nml + 2 ));
234 re->re_mods[ nml ].mi_type = strdup( type );
235 if ( value != NULL ) {
236 re->re_mods[ nml ].mi_val = ch_malloc( len + 1 );
237 memcpy( re->re_mods[ nml ].mi_val, value, len );
238 re->re_mods[ nml ].mi_val[ len ] = '\0';
239 re->re_mods[ nml ].mi_len = len;
241 re->re_mods[ nml ].mi_val = NULL;
242 re->re_mods[ nml ].mi_len = 0;
244 re->re_mods[ nml + 1 ].mi_type = NULL;
245 re->re_mods[ nml + 1 ].mi_val = NULL;
259 * Extract the replication hosts from a repl buf. Check to be sure that
260 * each replica host and port number are ones we know about (that is, they're
261 * in the slapd config file we read at startup). Without that information
262 * from the config file, we won't have the appropriate credentials to
263 * make modifications. If there are any unknown replica names, don't
264 * add them the the Re struct. Instead, log a warning message.
273 char *type, *value, *line, *p;
281 if ( replbuf == NULL ) {
288 * Get the host names of the replicas
293 /* If this is a reject log, we need to skip over the ERROR: line */
294 if ( !strncmp( *r_rp, ERROR_STR, strlen( ERROR_STR ))) {
295 line = ldif_getline( r_rp );
296 if ( line == NULL ) {
300 if ( strncasecmp( *r_rp, "replica:", 7 )) {
303 line = ldif_getline( r_rp );
304 if ( line == NULL ) {
307 if ( ldif_parse_line( line, &type, &value, &len ) < 0 ) {
311 if (( p = strchr( value, ':' )) != NULL ) {
319 /* Verify that we've heard of this replica before */
321 for ( i = 0; i < sglob->num_replicas; i++ ) {
322 if ( strcmp( sglob->replicas[ i ]->ri_hostname, value )) {
325 if ( sglob->replicas[ i ]->ri_port == port ) {
332 warn_unknown_replica( value, port );
338 rh = (Rh *) ch_realloc((char *) rh, ( nreplicas + 2 ) * sizeof( Rh ));
340 Debug( LDAP_DEBUG_ANY, "Out of memory in get_repl_hosts\n",
344 rh[ nreplicas ].rh_hostname = strdup( value );
345 rh[ nreplicas ].rh_port = port;
352 if ( nreplicas == 0 ) {
356 rh[ nreplicas ].rh_hostname = NULL;
357 *r_nreplicas = nreplicas;
367 * Convert "type" to an int.
374 if ( !strcmp( type, T_CHANGETYPESTR )) {
375 return( T_CHANGETYPE );
377 if ( !strcmp( type, T_TIMESTR )) {
380 if ( !strcmp( type, T_DNSTR )) {
389 * Convert "changetype" to an int.
396 if ( !strcmp( changetype, T_ADDCTSTR )) {
399 if ( !strcmp( changetype, T_MODIFYCTSTR )) {
400 return( T_MODIFYCT );
402 if ( !strcmp( changetype, T_DELETECTSTR )) {
403 return( T_DELETECT );
405 if ( !strcmp( changetype, T_MODRDNCTSTR )) {
406 return( T_MODRDNCT );
415 * Find the first line which is not a "replica:" line in buf.
416 * Returns a pointer to the line. Returns NULL if there are
417 * only "replica:" lines in buf.
426 if ( strncasecmp( p, "replica:", 8 )) {
429 while (( *p != '\0' ) && ( *p != '\n' )) {
444 * For debugging purposes: dump the contents of a replication entry.
445 * to the given stream.
457 Debug( LDAP_DEBUG_TRACE, "Re_dump: re is NULL\n", 0, 0, 0 );
460 fprintf( fp, "Re_dump: ******\n" );
461 fprintf( fp, "re_refcnt: %d\n", re->re_refcnt );
462 fprintf( fp, "re_timestamp: %s\n", re->re_timestamp );
463 fprintf( fp, "re_seq: %d\n", re->re_seq );
464 for ( i = 0; re->re_replicas && re->re_replicas[ i ].rh_hostname != NULL;
466 fprintf( fp, "re_replicas[%d]: %s:%d\n",
467 i, re->re_replicas[ i ].rh_hostname,
468 re->re_replicas[ i ].rh_port );
470 fprintf( fp, "re_dn: %s\n", re->re_dn );
471 switch ( re->re_changetype ) {
473 fprintf( fp, "re_changetype: add\n" );
476 fprintf( fp, "re_changetype: modify\n" );
479 fprintf( fp, "re_changetype: delete\n" );
482 fprintf( fp, "re_changetype: modrdn\n" );
485 fprintf( fp, "re_changetype: (unknown, type = %d\n",
488 if ( re->re_mods == NULL ) {
489 fprintf( fp, "re_mods: (none)\n" );
492 fprintf( fp, "re_mods:\n" );
493 for ( i = 0; mi[ i ].mi_type != NULL; i++ ) {
494 fprintf( fp, " %s, \"%s\", (%d bytes)\n",
496 mi[ i ].mi_val == NULL ? "(null)" : mi[ i ].mi_val,
505 * Given an Ri, an Re, and a file pointer, write a replication record to
506 * the file pointer. If ri is NULL, then include all replicas in the
507 * output. If ri is non-NULL, then only include a single "replica:" line
508 * (used when writing rejection records). Returns 0 on success, -1
509 * on failure. Note that Re_write will not write anything out if the
522 if ( re == NULL || fp == NULL ) {
523 Debug( LDAP_DEBUG_ANY, "Internal error: Re_write: NULL argument\n",
528 if ( re->re_refcnt < 1 ) {
529 return 0; /* this is not an error */
532 if ( ri != NULL ) { /* write a single "replica:" line */
533 if ( fprintf( fp, "replica: %s:%d\n", ri->ri_hostname,
534 ri->ri_port ) < 0 ) {
538 } else { /* write multiple "replica:" lines */
539 for ( i = 0; re->re_replicas[ i ].rh_hostname != NULL; i++ ) {
540 if ( fprintf( fp, "replica: %s:%d\n",
541 re->re_replicas[ i ].rh_hostname,
542 re->re_replicas[ i ].rh_port ) < 0 ) {
548 if ( fprintf( fp, "time: %s.%d\n", re->re_timestamp, re->re_seq ) < 0 ) {
552 if ( fprintf( fp, "dn: %s\n", re->re_dn ) < 0 ) {
556 if ( fprintf( fp, "changetype: " ) < 0 ) {
560 switch ( re->re_changetype ) {
574 s = "IllegalModifyType!!!";
576 if ( fprintf( fp, "%s\n", s ) < 0 ) {
580 for ( i = 0; (( re->re_mods != NULL ) &&
581 ( re->re_mods[ i ].mi_type != NULL )); i++ ) {
582 if ( !strcmp( re->re_mods[ i ].mi_type, T_MODSEPSTR )) {
583 if ( fprintf( fp, "%s\n", T_MODSEPSTR ) < 0 ) {
589 obuf = ldif_put( LDIF_PUT_VALUE,
590 re->re_mods[ i ].mi_type,
591 re->re_mods[ i ].mi_val ? re->re_mods[ i ].mi_val : "",
592 re->re_mods[ i ].mi_len );
593 if ( fputs( obuf, fp ) < 0 ) {
602 if ( fprintf( fp, "\n" ) < 0 ) {
606 if ( fflush( fp ) != 0 ) {
612 Debug( LDAP_DEBUG_ANY, "Error while writing: %s\n",
613 sys_errlist[ errno ], 0, 0 );
622 * Decrement the refcnt. Locking handled internally.
638 * Get the refcnt. Locking handled internally.
658 * Lock this replication entry
665 return( ldap_pvt_thread_mutex_lock( &re->re_mutex ));
672 * Unlock this replication entry
679 return( ldap_pvt_thread_mutex_unlock( &re->re_mutex ));
686 * Instantiate and initialize an Re.
694 (*re) = (Re *) malloc( sizeof( Re ));
699 /* Fill in the member function pointers */
700 (*re)->re_free = Re_free;
701 (*re)->re_getnext = Re_getnext;
702 (*re)->re_parse = Re_parse;
703 (*re)->re_write = Re_write;
704 (*re)->re_dump = Re_dump;
705 (*re)->re_lock = Re_lock;
706 (*re)->re_unlock = Re_unlock;
707 (*re)->re_decrefcnt = Re_decrefcnt;
708 (*re)->re_getrefcnt = Re_getrefcnt;
710 /* Initialize private data */
711 (*re)->re_refcnt = sglob->num_replicas;
712 (*re)->re_timestamp = NULL;
713 (*re)->re_replicas = NULL;
715 (*re)->re_changetype = 0;
717 (*re)->re_mods = NULL;
718 (*re)->re_next = NULL;
720 ldap_pvt_thread_mutex_init( &((*re)->re_mutex) );
728 * Given a host and port, generate a warning message iff we haven't already
729 * generated a message for this host:port combination.
732 warn_unknown_replica(
740 for ( i = 0; i < nur; i++ ) {
741 if ( strcmp( ur[ i ].rh_hostname, host )) {
744 if ( ur[ i ].rh_port == port ) {
750 Debug( LDAP_DEBUG_ANY,
751 "Warning: unknown replica %s:%d found in replication log\n",
754 ur = (Rh *) ch_realloc( (char *) ur, ( nur * sizeof( Rh )));
755 ur[ nur - 1 ].rh_hostname = strdup( host );
756 ur[ nur - 1 ].rh_port = port;