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>) "
61 main( int argc, char **argv )
65 char *host = "localhost";
71 int retries = RETRIES;
75 while ( (i = getopt( argc, argv, "FH:h:p:D:w:e:l:r:t:" )) != EOF ) {
81 case 'H': /* the server uri */
82 uri = strdup( optarg );
85 case 'h': /* the servers host */
86 host = strdup( optarg );
89 case 'p': /* the servers port */
90 port = atoi( optarg );
93 case 'D': /* the servers manager */
94 manager = strdup( optarg );
97 case 'w': /* the server managers password */
98 passwd = strdup( optarg );
101 case 'e': /* entry to rename */
102 entry = strdup( optarg );
105 case 'l': /* the number of loops */
106 loops = atoi( optarg );
109 case 'r': /* the number of retries */
110 retries = atoi( optarg );
113 case 't': /* delay in seconds */
114 delay = atoi( optarg );
123 if (( entry == NULL ) || ( port == -1 && uri == NULL ))
126 if ( *entry == '\0' ) {
128 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
130 exit( EXIT_FAILURE );
134 do_modrdn( uri, host, port, manager, passwd, entry,
135 loops, retries, delay, friendly );
136 exit( EXIT_SUCCESS );
141 do_modrdn( char *uri, char *host, int port, char *manager,
142 char *passwd, char *entry, int maxloop, int maxretries, int delay,
146 int i = 0, do_retry = maxretries;
150 int rc = LDAP_SUCCESS;
155 DNs[1] = strdup( entry );
157 /* reverse the RDN, make new DN */
161 p1 = strchr( entry, '=' ) + 1;
162 p2 = strchr( p1, ',' );
165 rdns[1] = strdup( entry );
168 for (i = p1 - entry;p2 >= p1;)
172 rdns[0] = strdup( DNs[1] );
178 ldap_initialize( &ld, uri );
180 ld = ldap_init( host, port );
183 perror( "ldap_init" );
184 exit( EXIT_FAILURE );
188 int version = LDAP_VERSION3;
189 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
193 if ( do_retry == maxretries ) {
194 fprintf( stderr, "PID=%ld - Modrdn(%d): entry=\"%s\".\n",
195 (long) pid, maxloop, entry );
198 rc = ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE );
199 if ( rc != LDAP_SUCCESS ) {
200 ldap_perror( ld, "ldap_bind" );
203 case LDAP_UNAVAILABLE:
204 if ( do_retry > 0 ) {
215 exit( EXIT_FAILURE );
218 for ( ; i < maxloop; i++ ) {
219 rc = ldap_modrdn2_s( ld, DNs[0], rdns[0], 0 );
220 if ( rc != LDAP_SUCCESS ) {
221 ldap_perror( ld, "ldap_modrdn" );
223 case LDAP_NO_SUCH_OBJECT:
224 /* NOTE: this likely means
225 * the second modrdn failed
226 * during the previous round... */
233 case LDAP_UNAVAILABLE:
234 if ( do_retry > 0 ) {
244 rc = ldap_modrdn2_s( ld, DNs[1], rdns[1], 1 );
245 if ( rc != LDAP_SUCCESS ) {
246 ldap_perror( ld, "ldap_modrdn" );
248 case LDAP_NO_SUCH_OBJECT:
249 /* NOTE: this likely means
250 * the first modrdn failed
251 * during the previous round... */
258 case LDAP_UNAVAILABLE:
259 if ( do_retry > 0 ) {
272 fprintf( stderr, " PID=%ld - Modrdn done (%d).\n", (long) pid, rc );