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,
49 "-H <uri> | ([-h <host>] -p <port>) "
62 main( int argc, char **argv )
66 char *host = "localhost";
72 int retries = RETRIES;
76 while ( (i = getopt( argc, argv, "FH:h:p:D:w:e:l:r:t:" )) != EOF ) {
82 case 'H': /* the server uri */
83 uri = strdup( optarg );
86 case 'h': /* the servers host */
87 host = strdup( optarg );
90 case 'p': /* the servers port */
91 port = atoi( optarg );
94 case 'D': /* the servers manager */
95 manager = strdup( optarg );
98 case 'w': /* the server managers password */
99 passwd = strdup( optarg );
102 case 'e': /* entry to rename */
103 entry = strdup( optarg );
106 case 'l': /* the number of loops */
107 loops = atoi( optarg );
110 case 'r': /* the number of retries */
111 retries = atoi( optarg );
114 case 't': /* delay in seconds */
115 delay = atoi( optarg );
124 if (( entry == NULL ) || ( port == -1 && uri == NULL ))
127 if ( *entry == '\0' ) {
129 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
131 exit( EXIT_FAILURE );
135 do_modrdn( uri, host, port, manager, passwd, entry,
136 loops, retries, delay, friendly );
137 exit( EXIT_SUCCESS );
142 do_modrdn( char *uri, char *host, int port, char *manager,
143 char *passwd, char *entry, int maxloop, int maxretries, int delay,
147 int i = 0, do_retry = maxretries;
151 int rc = LDAP_SUCCESS;
156 DNs[1] = strdup( entry );
158 /* reverse the RDN, make new DN */
162 p1 = strchr( entry, '=' ) + 1;
163 p2 = strchr( p1, ',' );
166 rdns[1] = strdup( entry );
169 for (i = p1 - entry;p2 >= p1;)
173 rdns[0] = strdup( DNs[1] );
179 ldap_initialize( &ld, uri );
181 ld = ldap_init( host, port );
184 perror( "ldap_init" );
185 exit( EXIT_FAILURE );
189 int version = LDAP_VERSION3;
190 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
194 if ( do_retry == maxretries ) {
195 fprintf( stderr, "PID=%ld - Modrdn(%d): entry=\"%s\".\n",
196 (long) pid, maxloop, entry );
199 rc = ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE );
200 if ( rc != LDAP_SUCCESS ) {
201 ldap_perror( ld, "ldap_bind" );
204 case LDAP_UNAVAILABLE:
205 if ( do_retry > 0 ) {
216 exit( EXIT_FAILURE );
219 for ( ; i < maxloop; i++ ) {
220 rc = ldap_modrdn2_s( ld, DNs[0], rdns[0], 0 );
221 if ( rc != LDAP_SUCCESS ) {
222 ldap_perror( ld, "ldap_modrdn" );
224 case LDAP_NO_SUCH_OBJECT:
225 /* NOTE: this likely means
226 * the second modrdn failed
227 * during the previous round... */
234 case LDAP_UNAVAILABLE:
235 if ( do_retry > 0 ) {
245 rc = ldap_modrdn2_s( ld, DNs[1], rdns[1], 1 );
246 if ( rc != LDAP_SUCCESS ) {
247 ldap_perror( ld, "ldap_modrdn" );
249 case LDAP_NO_SUCH_OBJECT:
250 /* NOTE: this likely means
251 * the first modrdn failed
252 * during the previous round... */
259 case LDAP_UNAVAILABLE:
260 if ( do_retry > 0 ) {
273 fprintf( stderr, " PID=%ld - Modrdn done (%d).\n", (long) pid, rc );