2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2004 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
39 do_modrdn( char *uri, char *host, int port, char *manager, char *passwd, char *entry, int maxloop );
44 fprintf( stderr, "usage: %s [-h <host>] -p port -D <managerDN> -w <passwd> -e <entry> [-l <loops>]\n",
50 main( int argc, char **argv )
54 char *host = "localhost";
61 while ( (i = getopt( argc, argv, "H:h:p:D:w:e:l:" )) != EOF ) {
63 case 'H': /* the server uri */
64 uri = strdup( optarg );
66 case 'h': /* the servers host */
67 host = strdup( optarg );
70 case 'p': /* the servers port */
71 port = atoi( optarg );
73 case 'D': /* the servers manager */
74 manager = strdup( optarg );
77 case 'w': /* the server managers password */
78 passwd = strdup( optarg );
80 case 'e': /* entry to rename */
81 entry = strdup( optarg );
84 case 'l': /* the number of loops */
85 loops = atoi( optarg );
94 if (( entry == NULL ) || ( port == -1 && uri == NULL ))
97 if ( *entry == '\0' ) {
99 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
101 exit( EXIT_FAILURE );
105 do_modrdn( uri, host, port, manager, passwd, entry, loops );
106 exit( EXIT_SUCCESS );
111 do_modrdn( char *uri, char *host, int port, char *manager,
112 char *passwd, char *entry, int maxloop )
122 DNs[1] = strdup( entry );
124 /* reverse the RDN, make new DN */
128 p1 = strchr( entry, '=' ) + 1;
129 p2 = strchr( p1, ',' );
132 rdns[1] = strdup( entry );
135 for (i = p1 - entry;p2 >= p1;)
139 rdns[0] = strdup( DNs[1] );
144 ldap_initialize( &ld, uri );
146 ld = ldap_init( host, port );
149 perror( "ldap_init" );
150 exit( EXIT_FAILURE );
154 int version = LDAP_VERSION3;
155 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
159 if ( ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE ) != LDAP_SUCCESS ) {
160 ldap_perror( ld, "ldap_bind" );
161 exit( EXIT_FAILURE );
165 fprintf( stderr, "PID=%ld - Modrdn(%d): entry=\"%s\".\n",
166 (long) pid, maxloop, entry );
168 for ( i = 0; i < maxloop; i++ ) {
171 if (( rc = ldap_modrdn2_s( ld, DNs[0], rdns[0], 0 ))
173 ldap_perror( ld, "ldap_modrdn" );
174 if ( rc != LDAP_NO_SUCH_OBJECT ) break;
177 if (( rc = ldap_modrdn2_s( ld, DNs[1], rdns[1], 1 ))
179 ldap_perror( ld, "ldap_modrdn" );
180 if ( rc != LDAP_NO_SUCH_OBJECT ) break;
185 fprintf( stderr, " PID=%ld - Modrdn done.\n", (long) pid );