2003-02-22 20:01:59 +08:00
|
|
|
/* $OpenLDAP$ */
|
2003-11-26 23:24:38 +08:00
|
|
|
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
|
|
|
|
*
|
2009-01-22 08:40:04 +08:00
|
|
|
* Copyright 1999-2009 The OpenLDAP Foundation.
|
2003-11-26 23:24:38 +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>.
|
2003-02-22 20:01:59 +08:00
|
|
|
*/
|
2003-11-26 23:24:38 +08:00
|
|
|
/* ACKNOWLEDGEMENTS:
|
|
|
|
* This work was initially developed by Howard Chu, based in part
|
|
|
|
* on other OpenLDAP test tools, for inclusion in OpenLDAP Software.
|
|
|
|
*/
|
|
|
|
|
2003-02-22 20:01:59 +08:00
|
|
|
#include "portable.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2007-08-14 00:20:24 +08:00
|
|
|
#include "ac/stdlib.h"
|
2003-02-22 20:01:59 +08:00
|
|
|
|
2007-08-14 00:20:24 +08:00
|
|
|
#include "ac/ctype.h"
|
|
|
|
#include "ac/param.h"
|
|
|
|
#include "ac/socket.h"
|
|
|
|
#include "ac/string.h"
|
|
|
|
#include "ac/unistd.h"
|
|
|
|
#include "ac/wait.h"
|
2003-02-22 20:01:59 +08:00
|
|
|
|
2007-08-14 00:20:24 +08:00
|
|
|
#include "ldap.h"
|
|
|
|
#include "lutil.h"
|
2003-02-22 20:01:59 +08:00
|
|
|
|
2006-01-12 21:01:38 +08:00
|
|
|
#include "slapd-common.h"
|
|
|
|
|
2003-02-22 20:01:59 +08:00
|
|
|
#define LOOPS 100
|
2005-04-21 00:16:39 +08:00
|
|
|
#define RETRIES 0
|
2003-02-22 20:01:59 +08:00
|
|
|
|
|
|
|
static void
|
2006-01-12 21:01:38 +08:00
|
|
|
do_modrdn( char *uri, char *manager, struct berval *passwd,
|
2005-11-05 00:17:32 +08:00
|
|
|
char *entry, int maxloop, int maxretries, int delay,
|
2006-03-25 18:13:30 +08:00
|
|
|
int friendly, int chaserefs );
|
2003-02-22 20:01:59 +08:00
|
|
|
|
|
|
|
static void
|
|
|
|
usage( char *name )
|
|
|
|
{
|
2005-08-19 15:27:32 +08:00
|
|
|
fprintf( stderr,
|
|
|
|
"usage: %s "
|
|
|
|
"-H <uri> | ([-h <host>] -p <port>) "
|
|
|
|
"-D <manager> "
|
|
|
|
"-w <passwd> "
|
|
|
|
"-e <entry> "
|
2006-06-09 17:30:36 +08:00
|
|
|
"[-i <ignore>] "
|
2005-08-19 15:27:32 +08:00
|
|
|
"[-l <loops>] "
|
2006-03-04 00:02:34 +08:00
|
|
|
"[-L <outerloops>] "
|
2005-08-19 15:27:32 +08:00
|
|
|
"[-r <maxretries>] "
|
2005-11-07 07:35:53 +08:00
|
|
|
"[-t <delay>] "
|
2006-03-25 18:13:30 +08:00
|
|
|
"[-F] "
|
|
|
|
"[-C]\n",
|
2003-02-22 20:01:59 +08:00
|
|
|
name );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main( int argc, char **argv )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char *uri = NULL;
|
2005-04-21 00:16:39 +08:00
|
|
|
char *host = "localhost";
|
|
|
|
int port = -1;
|
2003-02-22 20:01:59 +08:00
|
|
|
char *manager = NULL;
|
2006-01-12 21:01:38 +08:00
|
|
|
struct berval passwd = { 0, NULL };
|
2003-02-22 20:01:59 +08:00
|
|
|
char *entry = NULL;
|
2005-04-21 00:16:39 +08:00
|
|
|
int loops = LOOPS;
|
2006-03-04 00:02:34 +08:00
|
|
|
int outerloops = 1;
|
2005-04-21 00:16:39 +08:00
|
|
|
int retries = RETRIES;
|
2005-08-19 15:27:32 +08:00
|
|
|
int delay = 0;
|
2005-11-05 00:17:32 +08:00
|
|
|
int friendly = 0;
|
2006-03-25 18:13:30 +08:00
|
|
|
int chaserefs = 0;
|
2003-02-22 20:01:59 +08:00
|
|
|
|
2006-06-09 17:30:36 +08:00
|
|
|
tester_init( "slapd-modrdn", TESTER_MODRDN );
|
2006-01-12 21:01:38 +08:00
|
|
|
|
2006-12-04 05:34:24 +08:00
|
|
|
while ( ( i = getopt( argc, argv, "CD:e:FH:h:i:L:l:p:r:t:w:" ) ) != EOF )
|
|
|
|
{
|
|
|
|
switch ( i ) {
|
2006-03-25 18:13:30 +08:00
|
|
|
case 'C':
|
|
|
|
chaserefs++;
|
|
|
|
break;
|
|
|
|
|
2005-11-05 00:17:32 +08:00
|
|
|
case 'F':
|
|
|
|
friendly++;
|
|
|
|
break;
|
|
|
|
|
2005-04-21 00:16:39 +08:00
|
|
|
case 'H': /* the server uri */
|
|
|
|
uri = strdup( optarg );
|
2003-02-22 20:01:59 +08:00
|
|
|
break;
|
2005-04-21 00:16:39 +08:00
|
|
|
|
|
|
|
case 'h': /* the servers host */
|
|
|
|
host = strdup( optarg );
|
2003-02-22 20:01:59 +08:00
|
|
|
break;
|
|
|
|
|
2006-06-09 17:30:36 +08:00
|
|
|
case 'i':
|
|
|
|
/* ignored (!) by now */
|
|
|
|
break;
|
|
|
|
|
2005-04-21 00:16:39 +08:00
|
|
|
case 'p': /* the servers port */
|
2005-11-24 09:10:05 +08:00
|
|
|
if ( lutil_atoi( &port, optarg ) != 0 ) {
|
|
|
|
usage( argv[0] );
|
|
|
|
}
|
2003-02-22 20:01:59 +08:00
|
|
|
break;
|
|
|
|
|
2005-04-21 00:16:39 +08:00
|
|
|
case 'D': /* the servers manager */
|
|
|
|
manager = strdup( optarg );
|
2003-02-22 20:01:59 +08:00
|
|
|
break;
|
|
|
|
|
2005-04-21 00:16:39 +08:00
|
|
|
case 'w': /* the server managers password */
|
2006-01-12 21:01:38 +08:00
|
|
|
passwd.bv_val = strdup( optarg );
|
|
|
|
passwd.bv_len = strlen( optarg );
|
2006-07-02 02:22:43 +08:00
|
|
|
memset( optarg, '*', passwd.bv_len );
|
2005-04-21 00:16:39 +08:00
|
|
|
break;
|
2003-02-22 20:01:59 +08:00
|
|
|
|
2005-04-21 00:16:39 +08:00
|
|
|
case 'e': /* entry to rename */
|
|
|
|
entry = strdup( optarg );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'l': /* the number of loops */
|
2005-11-24 09:10:05 +08:00
|
|
|
if ( lutil_atoi( &loops, optarg ) != 0 ) {
|
|
|
|
usage( argv[0] );
|
|
|
|
}
|
2005-04-21 00:16:39 +08:00
|
|
|
break;
|
|
|
|
|
2006-03-04 00:02:34 +08:00
|
|
|
case 'L': /* the number of outerloops */
|
|
|
|
if ( lutil_atoi( &outerloops, optarg ) != 0 ) {
|
|
|
|
usage( argv[0] );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2005-04-21 00:16:39 +08:00
|
|
|
case 'r': /* the number of retries */
|
2005-11-24 09:10:05 +08:00
|
|
|
if ( lutil_atoi( &retries, optarg ) != 0 ) {
|
|
|
|
usage( argv[0] );
|
|
|
|
}
|
2005-04-21 00:16:39 +08:00
|
|
|
break;
|
|
|
|
|
2005-08-19 15:27:32 +08:00
|
|
|
case 't': /* delay in seconds */
|
2005-11-24 09:10:05 +08:00
|
|
|
if ( lutil_atoi( &delay, optarg ) != 0 ) {
|
|
|
|
usage( argv[0] );
|
|
|
|
}
|
2005-08-19 15:27:32 +08:00
|
|
|
break;
|
|
|
|
|
2005-04-21 00:16:39 +08:00
|
|
|
default:
|
|
|
|
usage( argv[0] );
|
|
|
|
break;
|
2003-02-22 20:01:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (( entry == NULL ) || ( port == -1 && uri == NULL ))
|
|
|
|
usage( argv[0] );
|
|
|
|
|
|
|
|
if ( *entry == '\0' ) {
|
|
|
|
|
|
|
|
fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
|
|
|
|
argv[0] );
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-01-12 21:01:38 +08:00
|
|
|
uri = tester_uri( uri, host, port );
|
|
|
|
|
2006-03-04 00:02:34 +08:00
|
|
|
for ( i = 0; i < outerloops; i++ ) {
|
|
|
|
do_modrdn( uri, manager, &passwd, entry,
|
2006-03-25 18:13:30 +08:00
|
|
|
loops, retries, delay, friendly, chaserefs );
|
2006-03-04 00:02:34 +08:00
|
|
|
}
|
|
|
|
|
2003-02-22 20:01:59 +08:00
|
|
|
exit( EXIT_SUCCESS );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2006-01-12 21:01:38 +08:00
|
|
|
do_modrdn( char *uri, char *manager,
|
2006-03-25 18:13:30 +08:00
|
|
|
struct berval *passwd, char *entry, int maxloop, int maxretries,
|
|
|
|
int delay, int friendly, int chaserefs )
|
2003-02-22 20:01:59 +08:00
|
|
|
{
|
|
|
|
LDAP *ld = NULL;
|
2009-03-06 03:26:35 +08:00
|
|
|
int i, do_retry = maxretries;
|
2006-01-12 21:01:38 +08:00
|
|
|
char *DNs[2];
|
|
|
|
char *rdns[2];
|
|
|
|
int rc = LDAP_SUCCESS;
|
|
|
|
char *p1, *p2;
|
|
|
|
int version = LDAP_VERSION3;
|
2003-02-22 20:01:59 +08:00
|
|
|
|
2003-03-05 05:52:23 +08:00
|
|
|
DNs[0] = entry;
|
2003-02-22 20:01:59 +08:00
|
|
|
DNs[1] = strdup( entry );
|
|
|
|
|
|
|
|
/* reverse the RDN, make new DN */
|
2006-01-12 21:01:38 +08:00
|
|
|
p1 = strchr( entry, '=' ) + 1;
|
|
|
|
p2 = strchr( p1, ',' );
|
|
|
|
|
|
|
|
*p2 = '\0';
|
|
|
|
rdns[1] = strdup( entry );
|
|
|
|
*p2-- = ',';
|
|
|
|
|
|
|
|
for (i = p1 - entry;p2 >= p1;)
|
|
|
|
DNs[1][i++] = *p2--;
|
|
|
|
|
|
|
|
DNs[1][i] = '\0';
|
|
|
|
rdns[0] = strdup( DNs[1] );
|
|
|
|
DNs[1][i] = ',';
|
2005-04-21 00:16:39 +08:00
|
|
|
|
2009-03-06 03:26:35 +08:00
|
|
|
i = 0;
|
|
|
|
|
2005-04-21 00:16:39 +08:00
|
|
|
retry:;
|
2006-01-12 21:01:38 +08:00
|
|
|
ldap_initialize( &ld, uri );
|
2003-02-22 20:01:59 +08:00
|
|
|
if ( ld == NULL ) {
|
2006-04-08 19:01:01 +08:00
|
|
|
tester_perror( "ldap_initialize", NULL );
|
2003-02-22 20:01:59 +08:00
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
|
|
|
|
2006-01-12 21:01:38 +08:00
|
|
|
(void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
|
2006-03-25 18:13:30 +08:00
|
|
|
(void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
|
|
|
|
chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF );
|
2003-02-22 20:01:59 +08:00
|
|
|
|
2005-04-21 00:16:39 +08:00
|
|
|
if ( do_retry == maxretries ) {
|
|
|
|
fprintf( stderr, "PID=%ld - Modrdn(%d): entry=\"%s\".\n",
|
|
|
|
(long) pid, maxloop, entry );
|
2003-02-22 20:01:59 +08:00
|
|
|
}
|
|
|
|
|
2006-01-12 21:01:38 +08:00
|
|
|
rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
|
2005-04-21 00:16:39 +08:00
|
|
|
if ( rc != LDAP_SUCCESS ) {
|
2006-04-08 19:01:01 +08:00
|
|
|
tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
|
2005-08-17 16:17:30 +08:00
|
|
|
switch ( rc ) {
|
|
|
|
case LDAP_BUSY:
|
|
|
|
case LDAP_UNAVAILABLE:
|
2005-08-19 15:27:32 +08:00
|
|
|
if ( do_retry > 0 ) {
|
|
|
|
do_retry--;
|
|
|
|
if ( delay > 0) {
|
|
|
|
sleep( delay );
|
|
|
|
}
|
2005-08-17 16:17:30 +08:00
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
/* fallthru */
|
|
|
|
default:
|
|
|
|
break;
|
2005-04-21 00:16:39 +08:00
|
|
|
}
|
|
|
|
exit( EXIT_FAILURE );
|
|
|
|
}
|
2003-02-22 20:01:59 +08:00
|
|
|
|
2005-04-21 02:57:36 +08:00
|
|
|
for ( ; i < maxloop; i++ ) {
|
2006-01-12 21:01:38 +08:00
|
|
|
rc = ldap_rename_s( ld, DNs[0], rdns[0], NULL, 0, NULL, NULL );
|
2005-04-21 00:16:39 +08:00
|
|
|
if ( rc != LDAP_SUCCESS ) {
|
2006-04-08 19:01:01 +08:00
|
|
|
tester_ldap_error( ld, "ldap_rename_s", NULL );
|
2005-11-04 22:49:59 +08:00
|
|
|
switch ( rc ) {
|
|
|
|
case LDAP_NO_SUCH_OBJECT:
|
|
|
|
/* NOTE: this likely means
|
|
|
|
* the second modrdn failed
|
|
|
|
* during the previous round... */
|
2005-11-05 00:17:32 +08:00
|
|
|
if ( !friendly ) {
|
|
|
|
goto done;
|
|
|
|
}
|
2005-11-04 22:49:59 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LDAP_BUSY:
|
|
|
|
case LDAP_UNAVAILABLE:
|
|
|
|
if ( do_retry > 0 ) {
|
|
|
|
do_retry--;
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
/* fall thru */
|
|
|
|
|
|
|
|
default:
|
|
|
|
goto done;
|
2005-04-21 00:16:39 +08:00
|
|
|
}
|
2003-02-22 20:01:59 +08:00
|
|
|
}
|
2006-01-12 21:01:38 +08:00
|
|
|
rc = ldap_rename_s( ld, DNs[1], rdns[1], NULL, 1, NULL, NULL );
|
2005-04-21 00:16:39 +08:00
|
|
|
if ( rc != LDAP_SUCCESS ) {
|
2006-04-08 19:01:01 +08:00
|
|
|
tester_ldap_error( ld, "ldap_rename_s", NULL );
|
2005-11-04 22:49:59 +08:00
|
|
|
switch ( rc ) {
|
|
|
|
case LDAP_NO_SUCH_OBJECT:
|
2005-11-04 23:51:19 +08:00
|
|
|
/* NOTE: this likely means
|
|
|
|
* the first modrdn failed
|
|
|
|
* during the previous round... */
|
2005-11-05 00:17:32 +08:00
|
|
|
if ( !friendly ) {
|
|
|
|
goto done;
|
|
|
|
}
|
2005-11-04 22:49:59 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LDAP_BUSY:
|
|
|
|
case LDAP_UNAVAILABLE:
|
|
|
|
if ( do_retry > 0 ) {
|
|
|
|
do_retry--;
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
/* fall thru */
|
|
|
|
|
|
|
|
default:
|
|
|
|
goto done;
|
2005-04-21 00:16:39 +08:00
|
|
|
}
|
2003-02-22 20:01:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-04 22:49:59 +08:00
|
|
|
done:;
|
2008-07-19 19:54:54 +08:00
|
|
|
fprintf( stderr, " PID=%ld - Modrdn done (%d).\n", (long) pid, rc );
|
2003-02-22 20:01:59 +08:00
|
|
|
|
2006-01-12 21:01:38 +08:00
|
|
|
ldap_unbind_ext( ld, NULL, NULL );
|
2003-02-22 20:01:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|