2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2005 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
16 * This work was initially developed by Howard Chu, based in part
17 * on other OpenLDAP test tools, for inclusion in OpenLDAP Software.
24 #include <ac/stdlib.h>
28 #include <ac/socket.h>
29 #include <ac/string.h>
30 #include <ac/unistd.h>
33 #define LDAP_DEPRECATED 1
40 do_modrdn( char *uri, char *host, int port, char *manager, char *passwd,
41 char *entry, int maxloop, int maxretries, int delay );
48 "-H <uri> | ([-h <host>] -p <port>) "
60 main( int argc, char **argv )
64 char *host = "localhost";
70 int retries = RETRIES;
73 while ( (i = getopt( argc, argv, "H:h:p:D:w:e:l:r:t:" )) != EOF ) {
75 case 'H': /* the server uri */
76 uri = strdup( optarg );
79 case 'h': /* the servers host */
80 host = strdup( optarg );
83 case 'p': /* the servers port */
84 port = atoi( optarg );
87 case 'D': /* the servers manager */
88 manager = strdup( optarg );
91 case 'w': /* the server managers password */
92 passwd = strdup( optarg );
95 case 'e': /* entry to rename */
96 entry = strdup( optarg );
99 case 'l': /* the number of loops */
100 loops = atoi( optarg );
103 case 'r': /* the number of retries */
104 retries = atoi( optarg );
107 case 't': /* delay in seconds */
108 delay = atoi( optarg );
117 if (( entry == NULL ) || ( port == -1 && uri == NULL ))
120 if ( *entry == '\0' ) {
122 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
124 exit( EXIT_FAILURE );
128 do_modrdn( uri, host, port, manager, passwd, entry, loops, retries, delay );
129 exit( EXIT_SUCCESS );
134 do_modrdn( char *uri, char *host, int port, char *manager,
135 char *passwd, char *entry, int maxloop, int maxretries, int delay )
138 int i = 0, do_retry = maxretries;
142 int rc = LDAP_SUCCESS;
147 DNs[1] = strdup( entry );
149 /* reverse the RDN, make new DN */
153 p1 = strchr( entry, '=' ) + 1;
154 p2 = strchr( p1, ',' );
157 rdns[1] = strdup( entry );
160 for (i = p1 - entry;p2 >= p1;)
164 rdns[0] = strdup( DNs[1] );
170 ldap_initialize( &ld, uri );
172 ld = ldap_init( host, port );
175 perror( "ldap_init" );
176 exit( EXIT_FAILURE );
180 int version = LDAP_VERSION3;
181 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
185 if ( do_retry == maxretries ) {
186 fprintf( stderr, "PID=%ld - Modrdn(%d): entry=\"%s\".\n",
187 (long) pid, maxloop, entry );
190 rc = ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE );
191 if ( rc != LDAP_SUCCESS ) {
192 ldap_perror( ld, "ldap_bind" );
195 case LDAP_UNAVAILABLE:
196 if ( do_retry > 0 ) {
207 exit( EXIT_FAILURE );
210 for ( ; i < maxloop; i++ ) {
211 rc = ldap_modrdn2_s( ld, DNs[0], rdns[0], 0 );
212 if ( rc != LDAP_SUCCESS ) {
213 ldap_perror( ld, "ldap_modrdn" );
215 case LDAP_NO_SUCH_OBJECT:
216 /* NOTE: this likely means
217 * the second modrdn failed
218 * during the previous round... */
222 case LDAP_UNAVAILABLE:
223 if ( do_retry > 0 ) {
233 rc = ldap_modrdn2_s( ld, DNs[1], rdns[1], 1 );
234 if ( rc != LDAP_SUCCESS ) {
235 ldap_perror( ld, "ldap_modrdn" );
237 case LDAP_NO_SUCH_OBJECT:
238 /* NOTE: this likely means
239 * the first modrdn failed
240 * during the previous round... */
244 case LDAP_UNAVAILABLE:
245 if ( do_retry > 0 ) {
258 fprintf( stderr, " PID=%ld - Modrdn done (%d).\n", (long) pid, rc );