#include <ldap.h>
#define LOOPS 100
+#define RETRIES 0
static void
-do_modrdn( char *uri, char *host, int port, char *manager, char *passwd, char *entry, int maxloop );
+do_modrdn( char *uri, char *host, int port, char *manager, char *passwd,
+ char *entry, int maxloop, int maxretries, int delay );
static void
usage( char *name )
{
- fprintf( stderr, "usage: %s [-h <host>] -p port -D <managerDN> -w <passwd> -e <entry> [-l <loops>]\n",
+ fprintf( stderr,
+ "usage: %s "
+ "-H <uri> | ([-h <host>] -p <port>) "
+ "-D <manager> "
+ "-w <passwd> "
+ "-e <entry> "
+ "[-l <loops>] "
+ "[-r <maxretries>] "
+ "[-t <delay>]\n",
name );
exit( EXIT_FAILURE );
}
{
int i;
char *uri = NULL;
- char *host = "localhost";
- int port = -1;
+ char *host = "localhost";
+ int port = -1;
char *manager = NULL;
char *passwd = NULL;
char *entry = NULL;
- int loops = LOOPS;
+ int loops = LOOPS;
+ int retries = RETRIES;
+ int delay = 0;
- while ( (i = getopt( argc, argv, "H:h:p:D:w:e:l:" )) != EOF ) {
+ while ( (i = getopt( argc, argv, "H:h:p:D:w:e:l:r:t:" )) != EOF ) {
switch( i ) {
- case 'H': /* the server uri */
- uri = strdup( optarg );
+ case 'H': /* the server uri */
+ uri = strdup( optarg );
break;
- case 'h': /* the servers host */
- host = strdup( optarg );
+
+ case 'h': /* the servers host */
+ host = strdup( optarg );
+ break;
+
+ case 'p': /* the servers port */
+ port = atoi( optarg );
+ break;
+
+ case 'D': /* the servers manager */
+ manager = strdup( optarg );
break;
- case 'p': /* the servers port */
- port = atoi( optarg );
- break;
- case 'D': /* the servers manager */
- manager = strdup( optarg );
+ case 'w': /* the server managers password */
+ passwd = strdup( optarg );
break;
- case 'w': /* the server managers password */
- passwd = strdup( optarg );
+ case 'e': /* entry to rename */
+ entry = strdup( optarg );
break;
- case 'e': /* entry to rename */
- entry = strdup( optarg );
- break;
- case 'l': /* the number of loops */
- loops = atoi( optarg );
- break;
+ case 'l': /* the number of loops */
+ loops = atoi( optarg );
+ break;
+
+ case 'r': /* the number of retries */
+ retries = atoi( optarg );
+ break;
+
+ case 't': /* delay in seconds */
+ delay = atoi( optarg );
+ break;
- default:
- usage( argv[0] );
- break;
+ default:
+ usage( argv[0] );
+ break;
}
}
}
- do_modrdn( uri, host, port, manager, passwd, entry, loops );
+ do_modrdn( uri, host, port, manager, passwd, entry, loops, retries, delay );
exit( EXIT_SUCCESS );
}
static void
do_modrdn( char *uri, char *host, int port, char *manager,
- char *passwd, char *entry, int maxloop )
+ char *passwd, char *entry, int maxloop, int maxretries, int delay )
{
LDAP *ld = NULL;
- int i;
+ int i = 0, do_retry = maxretries;
pid_t pid;
char *DNs[2];
char *rdns[2];
rdns[0] = strdup( DNs[1] );
DNs[1][i] = ',';
}
-
+
+retry:;
if ( uri ) {
ldap_initialize( &ld, uri );
} else {
&version );
}
- if ( ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE ) != LDAP_SUCCESS ) {
- ldap_perror( ld, "ldap_bind" );
- exit( EXIT_FAILURE );
+ if ( do_retry == maxretries ) {
+ fprintf( stderr, "PID=%ld - Modrdn(%d): entry=\"%s\".\n",
+ (long) pid, maxloop, entry );
}
+ rc = ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE );
+ if ( rc != LDAP_SUCCESS ) {
+ ldap_perror( ld, "ldap_bind" );
+ switch ( rc ) {
+ case LDAP_BUSY:
+ case LDAP_UNAVAILABLE:
+ if ( do_retry > 0 ) {
+ do_retry--;
+ if ( delay > 0) {
+ sleep( delay );
+ }
+ goto retry;
+ }
+ /* fallthru */
+ default:
+ break;
+ }
+ exit( EXIT_FAILURE );
+ }
- fprintf( stderr, "PID=%ld - Modrdn(%d): entry=\"%s\".\n",
- (long) pid, maxloop, entry );
-
- for ( i = 0; i < maxloop; i++ ) {
- if (( rc = ldap_modrdn2_s( ld, DNs[0], rdns[0], 0 ))
- != LDAP_SUCCESS ) {
+ for ( ; i < maxloop; i++ ) {
+ rc = ldap_modrdn2_s( ld, DNs[0], rdns[0], 0 );
+ if ( rc != LDAP_SUCCESS ) {
ldap_perror( ld, "ldap_modrdn" );
+ if ( rc == LDAP_BUSY && do_retry > 0 ) {
+ do_retry--;
+ goto retry;
+ }
if ( rc != LDAP_NO_SUCH_OBJECT ) break;
continue;
}
- if (( rc = ldap_modrdn2_s( ld, DNs[1], rdns[1], 1 ))
- != LDAP_SUCCESS ) {
+ rc = ldap_modrdn2_s( ld, DNs[1], rdns[1], 1 );
+ if ( rc != LDAP_SUCCESS ) {
ldap_perror( ld, "ldap_modrdn" );
+ if ( rc == LDAP_BUSY && do_retry > 0 ) {
+ do_retry--;
+ goto retry;
+ }
if ( rc != LDAP_NO_SUCH_OBJECT ) break;
continue;
}