1999-09-09 03:06:24 +08:00
|
|
|
/* $OpenLDAP$ */
|
2003-11-27 02:19:00 +08:00
|
|
|
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
|
|
|
|
*
|
2004-01-02 03:15:16 +08:00
|
|
|
* Copyright 1998-2004 The OpenLDAP Foundation.
|
2003-11-27 02:19:00 +08:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted only as authorized by the OpenLDAP
|
|
|
|
* Public License.
|
|
|
|
*
|
|
|
|
* A copy of this license is available in file LICENSE in the
|
|
|
|
* top-level directory of the distribution or, alternatively, at
|
|
|
|
* <http://www.OpenLDAP.org/license.html>.
|
2001-01-21 01:49:05 +08:00
|
|
|
*/
|
2003-11-27 02:19:00 +08:00
|
|
|
/* Portions Copyright (c) 1996 Regents of the University of Michigan.
|
1998-08-09 08:43:13 +08:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms are permitted
|
|
|
|
* provided that this notice is preserved and that due credit is given
|
|
|
|
* to the University of Michigan at Ann Arbor. The name of the University
|
|
|
|
* may not be used to endorse or promote products derived from this
|
|
|
|
* software without specific prior written permission. This software
|
|
|
|
* is provided ``as is'' without express or implied warranty.
|
|
|
|
*/
|
2003-11-27 02:19:00 +08:00
|
|
|
/* ACKNOWLEDGEMENTS:
|
|
|
|
* This work was originally developed by the University of Michigan
|
|
|
|
* (as part of U-MICH LDAP).
|
|
|
|
*/
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ri.c - routines used to manipulate Ri structures. An Ri (Replica
|
|
|
|
* information) struct contains all information about one replica
|
|
|
|
* instance. The Ri struct is defined in slurp.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
1998-10-25 09:41:42 +08:00
|
|
|
#include "portable.h"
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
#include <stdio.h>
|
1999-06-03 08:37:44 +08:00
|
|
|
|
|
|
|
#include <ac/stdlib.h>
|
1999-09-01 13:28:52 +08:00
|
|
|
#include <ac/string.h>
|
1998-10-25 09:41:42 +08:00
|
|
|
#include <ac/signal.h>
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
#include "slurp.h"
|
|
|
|
#include "globals.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* Forward references */
|
1998-10-25 09:41:42 +08:00
|
|
|
static int ismine LDAP_P(( Ri *, Re * ));
|
|
|
|
static int isnew LDAP_P(( Ri *, Re * ));
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process any unhandled replication entries in the queue.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
Ri_process(
|
|
|
|
Ri *ri
|
|
|
|
)
|
|
|
|
{
|
|
|
|
Rq *rq = sglob->rq;
|
2002-04-08 17:43:22 +08:00
|
|
|
Re *re = NULL, *new_re = NULL;
|
1998-08-09 08:43:13 +08:00
|
|
|
int rc ;
|
|
|
|
char *errmsg;
|
2003-11-19 18:23:08 +08:00
|
|
|
int errfree;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1998-11-19 01:45:32 +08:00
|
|
|
(void) SIGNAL( LDAP_SIGUSR1, do_nothing );
|
2002-05-16 18:51:45 +08:00
|
|
|
#ifdef SIGPIPE
|
1998-08-09 08:43:13 +08:00
|
|
|
(void) SIGNAL( SIGPIPE, SIG_IGN );
|
2002-05-16 18:51:45 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( ri == NULL ) {
|
|
|
|
Debug( LDAP_DEBUG_ANY, "Error: Ri_process: ri == NULL!\n", 0, 0, 0 );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Startup code. See if there's any work to do. If not, wait on the
|
|
|
|
* rq->rq_more condition variable.
|
|
|
|
*/
|
|
|
|
rq->rq_lock( rq );
|
|
|
|
while ( !sglob->slurpd_shutdown &&
|
|
|
|
(( re = rq->rq_gethead( rq )) == NULL )) {
|
2000-10-04 02:50:42 +08:00
|
|
|
/* No work */
|
|
|
|
if ( sglob->one_shot_mode ) {
|
|
|
|
/* give up if in one shot mode */
|
|
|
|
rq->rq_unlock( rq );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* wait on condition variable */
|
1999-01-28 12:34:55 +08:00
|
|
|
ldap_pvt_thread_cond_wait( &rq->rq_more, &rq->rq_mutex );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When we get here, there's work in the queue, and we have the
|
|
|
|
* queue locked. re should be pointing to the head of the queue.
|
|
|
|
*/
|
|
|
|
rq->rq_unlock( rq );
|
|
|
|
while ( !sglob->slurpd_shutdown ) {
|
|
|
|
if ( re != NULL ) {
|
|
|
|
if ( !ismine( ri, re )) {
|
|
|
|
/* The Re doesn't list my host:port */
|
|
|
|
Debug( LDAP_DEBUG_TRACE,
|
|
|
|
"Replica %s:%d, skip repl record for %s (not mine)\n",
|
|
|
|
ri->ri_hostname, ri->ri_port, re->re_dn );
|
|
|
|
} else if ( !isnew( ri, re )) {
|
|
|
|
/* This Re is older than my saved status information */
|
|
|
|
Debug( LDAP_DEBUG_TRACE,
|
|
|
|
"Replica %s:%d, skip repl record for %s (old)\n",
|
|
|
|
ri->ri_hostname, ri->ri_port, re->re_dn );
|
|
|
|
} else {
|
2003-11-19 18:23:08 +08:00
|
|
|
rc = do_ldap( ri, re, &errmsg, &errfree );
|
1998-08-09 08:43:13 +08:00
|
|
|
switch ( rc ) {
|
|
|
|
case DO_LDAP_ERR_RETRYABLE:
|
1999-01-28 12:34:55 +08:00
|
|
|
ldap_pvt_thread_sleep( RETRY_SLEEP_TIME );
|
1998-08-09 08:43:13 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY,
|
|
|
|
"Retrying operation for DN %s on replica %s:%d\n",
|
|
|
|
re->re_dn, ri->ri_hostname, ri->ri_port );
|
|
|
|
continue;
|
|
|
|
break;
|
1998-11-04 09:41:00 +08:00
|
|
|
case DO_LDAP_ERR_FATAL: {
|
1998-08-09 08:43:13 +08:00
|
|
|
/* Non-retryable error. Write rejection log. */
|
1998-11-04 09:41:00 +08:00
|
|
|
int ld_errno = 0;
|
|
|
|
ldap_get_option(ri->ri_ldp, LDAP_OPT_ERROR_NUMBER, &ld_errno);
|
|
|
|
write_reject( ri, re, ld_errno, errmsg );
|
1998-08-09 08:43:13 +08:00
|
|
|
/* Update status ... */
|
|
|
|
(void) sglob->st->st_update( sglob->st, ri->ri_stel, re );
|
|
|
|
/* ... and write to disk */
|
|
|
|
(void) sglob->st->st_write( sglob->st );
|
1998-11-04 09:41:00 +08:00
|
|
|
} break;
|
1998-08-09 08:43:13 +08:00
|
|
|
default:
|
|
|
|
/* LDAP op completed ok - update status... */
|
|
|
|
(void) sglob->st->st_update( sglob->st, ri->ri_stel, re );
|
|
|
|
/* ... and write to disk */
|
|
|
|
(void) sglob->st->st_write( sglob->st );
|
|
|
|
break;
|
|
|
|
}
|
2003-11-19 18:23:08 +08:00
|
|
|
if ( errfree && errmsg ) {
|
|
|
|
ch_free( errmsg );
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Debug( LDAP_DEBUG_ANY, "Error: re is null in Ri_process\n",
|
|
|
|
0, 0, 0 );
|
|
|
|
}
|
|
|
|
rq->rq_lock( rq );
|
|
|
|
while ( !sglob->slurpd_shutdown &&
|
|
|
|
((new_re = re->re_getnext( re )) == NULL )) {
|
|
|
|
if ( sglob->one_shot_mode ) {
|
1998-11-12 03:17:25 +08:00
|
|
|
rq->rq_unlock( rq );
|
1998-08-09 08:43:13 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* No work - wait on condition variable */
|
1999-01-28 12:34:55 +08:00
|
|
|
ldap_pvt_thread_cond_wait( &rq->rq_more, &rq->rq_mutex );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
re->re_decrefcnt( re );
|
|
|
|
re = new_re;
|
|
|
|
rq->rq_unlock( rq );
|
|
|
|
if ( sglob->slurpd_shutdown ) {
|
2003-11-19 13:23:17 +08:00
|
|
|
if ( ri->ri_ldp ) {
|
|
|
|
ldap_unbind( ri->ri_ldp );
|
|
|
|
ri->ri_ldp = NULL;
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
1998-08-14 03:32:43 +08:00
|
|
|
* Wake a replication thread which may be sleeping.
|
1998-11-19 09:18:24 +08:00
|
|
|
* Send it a LDAP_SIGUSR1.
|
1998-08-09 08:43:13 +08:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
Ri_wake(
|
|
|
|
Ri *ri
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if ( ri == NULL ) {
|
|
|
|
return;
|
|
|
|
}
|
1999-01-28 12:34:55 +08:00
|
|
|
ldap_pvt_thread_kill( ri->ri_tid, LDAP_SIGUSR1 );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate and initialize an Ri struct.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
Ri_init(
|
|
|
|
Ri **ri
|
|
|
|
)
|
|
|
|
{
|
2000-09-23 06:24:30 +08:00
|
|
|
(*ri) = ( Ri * ) calloc( 1, sizeof( Ri ));
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( *ri == NULL ) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize member functions */
|
|
|
|
(*ri)->ri_process = Ri_process;
|
|
|
|
(*ri)->ri_wake = Ri_wake;
|
|
|
|
|
|
|
|
/* Initialize private data */
|
|
|
|
(*ri)->ri_hostname = NULL;
|
2003-06-14 08:06:36 +08:00
|
|
|
(*ri)->ri_uri = NULL;
|
1998-08-09 08:43:13 +08:00
|
|
|
(*ri)->ri_ldp = NULL;
|
|
|
|
(*ri)->ri_bind_dn = NULL;
|
|
|
|
(*ri)->ri_password = NULL;
|
2000-08-18 00:30:37 +08:00
|
|
|
(*ri)->ri_authcId = NULL;
|
1998-08-09 08:43:13 +08:00
|
|
|
(*ri)->ri_srvtab = NULL;
|
|
|
|
(*ri)->ri_curr = NULL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return 1 if the hostname and port in re match the hostname and port
|
|
|
|
* in ri, otherwise return zero.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ismine(
|
|
|
|
Ri *ri,
|
|
|
|
Re *re
|
|
|
|
)
|
|
|
|
{
|
|
|
|
Rh *rh;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if ( ri == NULL || re == NULL || ri->ri_hostname == NULL ||
|
|
|
|
re->re_replicas == NULL ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
rh = re->re_replicas;
|
|
|
|
for ( i = 0; rh[ i ].rh_hostname != NULL; i++ ) {
|
|
|
|
if ( !strcmp( rh[ i ].rh_hostname, ri->ri_hostname) &&
|
|
|
|
rh[ i ].rh_port == ri->ri_port ) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return 1 if the Re's timestamp/seq combination are greater than the
|
|
|
|
* timestamp and seq in the Ri's ri_stel member. In other words, if we
|
|
|
|
* find replication entries in the log which we've already processed,
|
|
|
|
* don't process them. If the re is "old," return 0.
|
|
|
|
* No check for NULL pointers is done.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
isnew(
|
|
|
|
Ri *ri,
|
|
|
|
Re *re
|
|
|
|
)
|
|
|
|
{
|
2001-09-10 05:16:17 +08:00
|
|
|
long x;
|
1998-08-09 08:43:13 +08:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Lock the St struct to avoid a race */
|
|
|
|
sglob->st->st_lock( sglob->st );
|
2001-09-10 05:16:17 +08:00
|
|
|
x = re->re_timestamp - ri->ri_stel->last;
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( x > 0 ) {
|
|
|
|
/* re timestamp is newer */
|
|
|
|
ret = 1;
|
|
|
|
} else if ( x < 0 ) {
|
|
|
|
ret = 0;
|
|
|
|
} else {
|
|
|
|
/* timestamps were equal */
|
|
|
|
if ( re->re_seq > ri->ri_stel->seq ) {
|
|
|
|
/* re seq is newer */
|
|
|
|
ret = 1;
|
|
|
|
} else {
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sglob->st->st_unlock( sglob->st );
|
|
|
|
return ret;
|
|
|
|
}
|