3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
10 #include <ac/stdlib.h>
14 #include <ac/socket.h>
15 #include <ac/string.h>
16 #include <ac/unistd.h>
24 do_modrdn( char *uri, char *host, int port, char *manager, char *passwd, char *entry, int maxloop );
29 fprintf( stderr, "usage: %s [-h <host>] -p port -D <managerDN> -w <passwd> -e <entry> [-l <loops>]\n",
35 main( int argc, char **argv )
39 char *host = "localhost";
46 while ( (i = getopt( argc, argv, "H:h:p:D:w:e:l:" )) != EOF ) {
48 case 'H': /* the server uri */
49 uri = strdup( optarg );
51 case 'h': /* the servers host */
52 host = strdup( optarg );
55 case 'p': /* the servers port */
56 port = atoi( optarg );
58 case 'D': /* the servers manager */
59 manager = strdup( optarg );
62 case 'w': /* the server managers password */
63 passwd = strdup( optarg );
65 case 'e': /* entry to rename */
66 entry = strdup( optarg );
69 case 'l': /* the number of loops */
70 loops = atoi( optarg );
79 if (( entry == NULL ) || ( port == -1 && uri == NULL ))
82 if ( *entry == '\0' ) {
84 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
90 do_modrdn( uri, host, port, manager, passwd, entry, loops );
96 do_modrdn( char *uri, char *host, int port, char *manager,
97 char *passwd, char *entry, int maxloop )
101 pid_t pid = getpid();
106 DNs[1] = strdup( entry );
108 /* reverse the RDN, make new DN */
112 p1 = strchr( entry, '=' ) + 1;
113 p2 = strchr( p1, ',' );
116 rdns[1] = strdup( entry );
119 for (i = p1 - entry;p2 >= p1;)
123 rdns[0] = strdup( DNs[1] );
128 ldap_initialize( &ld, uri );
130 ld = ldap_init( host, port );
133 perror( "ldap_init" );
134 exit( EXIT_FAILURE );
138 int version = LDAP_VERSION3;
139 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
143 if ( ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE ) != LDAP_SUCCESS ) {
144 ldap_perror( ld, "ldap_bind" );
145 exit( EXIT_FAILURE );
149 fprintf( stderr, "PID=%ld - Modrdn(%d): entry=\"%s\".\n",
150 (long) pid, maxloop, entry );
152 for ( i = 0; i < maxloop; i++ ) {
155 if (( rc = ldap_modrdn2_s( ld, DNs[0], rdns[0], 0 ))
157 ldap_perror( ld, "ldap_modrdn" );
158 if ( rc != LDAP_NO_SUCH_OBJECT ) break;
161 if (( rc = ldap_modrdn2_s( ld, DNs[1], rdns[1], 1 ))
163 ldap_perror( ld, "ldap_modrdn" );
164 if ( rc != LDAP_NO_SUCH_OBJECT ) break;
170 fprintf( stderr, " PID=%ld - Modrdn done.\n", (long) pid );