]> git.sur5r.net Git - openldap/blob - tests/progs/slapd-modrdn.c
54f4a2d1476879d48fd4469116e0d738a9ea718b
[openldap] / tests / progs / slapd-modrdn.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2004 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
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>.
14  */
15 /* ACKNOWLEDGEMENTS:
16  * This work was initially developed by Howard Chu, based in part
17  * on other OpenLDAP test tools, for inclusion in OpenLDAP Software.
18  */
19
20 #include "portable.h"
21
22 #include <stdio.h>
23
24 #include <ac/stdlib.h>
25
26 #include <ac/ctype.h>
27 #include <ac/param.h>
28 #include <ac/socket.h>
29 #include <ac/string.h>
30 #include <ac/unistd.h>
31 #include <ac/wait.h>
32
33 #define LDAP_DEPRECATED 1
34 #include <ldap.h>
35
36 #define LOOPS   100
37
38 static void
39 do_modrdn( char *uri, char *host, int port, char *manager, char *passwd, char *entry, int maxloop );
40
41 static void
42 usage( char *name )
43 {
44         fprintf( stderr, "usage: %s [-h <host>] -p port -D <managerDN> -w <passwd> -e <entry> [-l <loops>]\n",
45                         name );
46         exit( EXIT_FAILURE );
47 }
48
49 int
50 main( int argc, char **argv )
51 {
52         int             i;
53         char            *uri = NULL;
54         char        *host = "localhost";
55         int                     port = -1;
56         char            *manager = NULL;
57         char            *passwd = NULL;
58         char            *entry = NULL;
59         int                     loops = LOOPS;
60
61         while ( (i = getopt( argc, argv, "H:h:p:D:w:e:l:" )) != EOF ) {
62                 switch( i ) {
63                         case 'H':               /* the server uri */
64                                 uri = strdup( optarg );
65                         break;
66                         case 'h':               /* the servers host */
67                                 host = strdup( optarg );
68                         break;
69
70                         case 'p':               /* the servers port */
71                                 port = atoi( optarg );
72                                 break;
73                         case 'D':               /* the servers manager */
74                                 manager = strdup( optarg );
75                         break;
76
77                         case 'w':               /* the server managers password */
78                                 passwd = strdup( optarg );
79                         break;
80                         case 'e':               /* entry to rename */
81                                 entry = strdup( optarg );
82                                 break;
83
84                         case 'l':               /* the number of loops */
85                                 loops = atoi( optarg );
86                                 break;
87
88                         default:
89                                 usage( argv[0] );
90                                 break;
91                 }
92         }
93
94         if (( entry == NULL ) || ( port == -1 && uri == NULL ))
95                 usage( argv[0] );
96
97         if ( *entry == '\0' ) {
98
99                 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
100                                 argv[0] );
101                 exit( EXIT_FAILURE );
102
103         }
104
105         do_modrdn( uri, host, port, manager, passwd, entry, loops );
106         exit( EXIT_SUCCESS );
107 }
108
109
110 static void
111 do_modrdn( char *uri, char *host, int port, char *manager,
112         char *passwd, char *entry, int maxloop )
113 {
114         LDAP    *ld = NULL;
115         int     i;
116         pid_t   pid;
117         char *DNs[2];
118         char *rdns[2];
119         int         rc = LDAP_SUCCESS;
120
121
122         pid = getpid();
123         DNs[0] = entry;
124         DNs[1] = strdup( entry );
125
126         /* reverse the RDN, make new DN */
127         {
128                 char *p1, *p2;
129
130                 p1 = strchr( entry, '=' ) + 1;
131                 p2 = strchr( p1, ',' );
132
133                 *p2 = '\0';
134                 rdns[1] = strdup( entry );
135                 *p2-- = ',';
136
137                 for (i = p1 - entry;p2 >= p1;)
138                         DNs[1][i++] = *p2--;
139                 
140                 DNs[1][i] = '\0';
141                 rdns[0] = strdup( DNs[1] );
142                 DNs[1][i] = ',';
143         }
144                 
145         if ( uri ) {
146                 ldap_initialize( &ld, uri );
147         } else {
148                 ld = ldap_init( host, port );
149         }
150         if ( ld == NULL ) {
151                 perror( "ldap_init" );
152                 exit( EXIT_FAILURE );
153         }
154
155         {
156                 int version = LDAP_VERSION3;
157                 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
158                         &version ); 
159         }
160
161         if ( ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE ) != LDAP_SUCCESS ) {
162                 ldap_perror( ld, "ldap_bind" );
163                  exit( EXIT_FAILURE );
164         }
165
166
167         fprintf( stderr, "PID=%ld - Modrdn(%d): entry=\"%s\".\n",
168                  (long) pid, maxloop, entry );
169
170         for ( i = 0; i < maxloop; i++ ) {
171                 if (( rc = ldap_modrdn2_s( ld, DNs[0], rdns[0], 0 ))
172                         != LDAP_SUCCESS ) {
173                         ldap_perror( ld, "ldap_modrdn" );
174                         if ( rc != LDAP_NO_SUCH_OBJECT ) break;
175                         continue;
176                 }
177                 if (( rc = ldap_modrdn2_s( ld, DNs[1], rdns[1], 1 ))
178                         != LDAP_SUCCESS ) {
179                         ldap_perror( ld, "ldap_modrdn" );
180                         if ( rc != LDAP_NO_SUCH_OBJECT ) break;
181                         continue;
182                 }
183         }
184
185         fprintf( stderr, " PID=%ld - Modrdn done (%d).\n", (long) pid, rc );
186
187         ldap_unbind( ld );
188 }
189
190