2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2003 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>
38 do_modrdn( char *uri, char *host, int port, char *manager, char *passwd, char *entry, int maxloop );
43 fprintf( stderr, "usage: %s [-h <host>] -p port -D <managerDN> -w <passwd> -e <entry> [-l <loops>]\n",
49 main( int argc, char **argv )
53 char *host = "localhost";
60 while ( (i = getopt( argc, argv, "H:h:p:D:w:e:l:" )) != EOF ) {
62 case 'H': /* the server uri */
63 uri = strdup( optarg );
65 case 'h': /* the servers host */
66 host = strdup( optarg );
69 case 'p': /* the servers port */
70 port = atoi( optarg );
72 case 'D': /* the servers manager */
73 manager = strdup( optarg );
76 case 'w': /* the server managers password */
77 passwd = strdup( optarg );
79 case 'e': /* entry to rename */
80 entry = strdup( optarg );
83 case 'l': /* the number of loops */
84 loops = atoi( optarg );
93 if (( entry == NULL ) || ( port == -1 && uri == NULL ))
96 if ( *entry == '\0' ) {
98 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
100 exit( EXIT_FAILURE );
104 do_modrdn( uri, host, port, manager, passwd, entry, loops );
105 exit( EXIT_SUCCESS );
110 do_modrdn( char *uri, char *host, int port, char *manager,
111 char *passwd, char *entry, int maxloop )
121 DNs[1] = strdup( entry );
123 /* reverse the RDN, make new DN */
127 p1 = strchr( entry, '=' ) + 1;
128 p2 = strchr( p1, ',' );
131 rdns[1] = strdup( entry );
134 for (i = p1 - entry;p2 >= p1;)
138 rdns[0] = strdup( DNs[1] );
143 ldap_initialize( &ld, uri );
145 ld = ldap_init( host, port );
148 perror( "ldap_init" );
149 exit( EXIT_FAILURE );
153 int version = LDAP_VERSION3;
154 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
158 if ( ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE ) != LDAP_SUCCESS ) {
159 ldap_perror( ld, "ldap_bind" );
160 exit( EXIT_FAILURE );
164 fprintf( stderr, "PID=%ld - Modrdn(%d): entry=\"%s\".\n",
165 (long) pid, maxloop, entry );
167 for ( i = 0; i < maxloop; i++ ) {
170 if (( rc = ldap_modrdn2_s( ld, DNs[0], rdns[0], 0 ))
172 ldap_perror( ld, "ldap_modrdn" );
173 if ( rc != LDAP_NO_SUCH_OBJECT ) break;
176 if (( rc = ldap_modrdn2_s( ld, DNs[1], rdns[1], 1 ))
178 ldap_perror( ld, "ldap_modrdn" );
179 if ( rc != LDAP_NO_SUCH_OBJECT ) break;
184 fprintf( stderr, " PID=%ld - Modrdn done.\n", (long) pid );