]> git.sur5r.net Git - openldap/blob - tests/progs/slapd-modrdn.c
Sync with HEAD
[openldap] / tests / progs / slapd-modrdn.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2005 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 #define RETRIES 0
38
39 static void
40 do_modrdn( char *uri, char *host, int port, char *manager, char *passwd,
41                 char *entry, int maxloop, int maxretries, int delay,
42                 int friendly );
43
44 static void
45 usage( char *name )
46 {
47         fprintf( stderr,
48                 "usage: %s "
49                 "-H <uri> | ([-h <host>] -p <port>) "
50                 "-D <manager> "
51                 "-w <passwd> "
52                 "-e <entry> "
53                 "[-l <loops>] "
54                 "[-r <maxretries>] "
55                 "[-t <delay>]\n",
56                         name );
57         exit( EXIT_FAILURE );
58 }
59
60 int
61 main( int argc, char **argv )
62 {
63         int             i;
64         char            *uri = NULL;
65         char            *host = "localhost";
66         int             port = -1;
67         char            *manager = NULL;
68         char            *passwd = NULL;
69         char            *entry = NULL;
70         int             loops = LOOPS;
71         int             retries = RETRIES;
72         int             delay = 0;
73         int             friendly = 0;
74
75         while ( (i = getopt( argc, argv, "FH:h:p:D:w:e:l:r:t:" )) != EOF ) {
76                 switch( i ) {
77                 case 'F':
78                         friendly++;
79                         break;
80
81                 case 'H':               /* the server uri */
82                         uri = strdup( optarg );
83                         break;
84
85                 case 'h':               /* the servers host */
86                         host = strdup( optarg );
87                         break;
88
89                 case 'p':               /* the servers port */
90                         port = atoi( optarg );
91                         break;
92
93                 case 'D':               /* the servers manager */
94                         manager = strdup( optarg );
95                         break;
96
97                 case 'w':               /* the server managers password */
98                         passwd = strdup( optarg );
99                         break;
100
101                 case 'e':               /* entry to rename */
102                         entry = strdup( optarg );
103                         break;
104
105                 case 'l':               /* the number of loops */
106                         loops = atoi( optarg );
107                         break;
108
109                 case 'r':               /* the number of retries */
110                         retries = atoi( optarg );
111                         break;
112
113                 case 't':               /* delay in seconds */
114                         delay = atoi( optarg );
115                         break;
116
117                 default:
118                         usage( argv[0] );
119                         break;
120                 }
121         }
122
123         if (( entry == NULL ) || ( port == -1 && uri == NULL ))
124                 usage( argv[0] );
125
126         if ( *entry == '\0' ) {
127
128                 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
129                                 argv[0] );
130                 exit( EXIT_FAILURE );
131
132         }
133
134         do_modrdn( uri, host, port, manager, passwd, entry,
135                         loops, retries, delay, friendly );
136         exit( EXIT_SUCCESS );
137 }
138
139
140 static void
141 do_modrdn( char *uri, char *host, int port, char *manager,
142         char *passwd, char *entry, int maxloop, int maxretries, int delay,
143         int friendly )
144 {
145         LDAP    *ld = NULL;
146         int     i = 0, do_retry = maxretries;
147         pid_t   pid;
148         char *DNs[2];
149         char *rdns[2];
150         int         rc = LDAP_SUCCESS;
151
152
153         pid = getpid();
154         DNs[0] = entry;
155         DNs[1] = strdup( entry );
156
157         /* reverse the RDN, make new DN */
158         {
159                 char *p1, *p2;
160
161                 p1 = strchr( entry, '=' ) + 1;
162                 p2 = strchr( p1, ',' );
163
164                 *p2 = '\0';
165                 rdns[1] = strdup( entry );
166                 *p2-- = ',';
167
168                 for (i = p1 - entry;p2 >= p1;)
169                         DNs[1][i++] = *p2--;
170                 
171                 DNs[1][i] = '\0';
172                 rdns[0] = strdup( DNs[1] );
173                 DNs[1][i] = ',';
174         }
175
176 retry:;
177         if ( uri ) {
178                 ldap_initialize( &ld, uri );
179         } else {
180                 ld = ldap_init( host, port );
181         }
182         if ( ld == NULL ) {
183                 perror( "ldap_init" );
184                 exit( EXIT_FAILURE );
185         }
186
187         {
188                 int version = LDAP_VERSION3;
189                 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
190                         &version ); 
191         }
192
193         if ( do_retry == maxretries ) {
194                 fprintf( stderr, "PID=%ld - Modrdn(%d): entry=\"%s\".\n",
195                         (long) pid, maxloop, entry );
196         }
197
198         rc = ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE );
199         if ( rc != LDAP_SUCCESS ) {
200                 ldap_perror( ld, "ldap_bind" );
201                 switch ( rc ) {
202                 case LDAP_BUSY:
203                 case LDAP_UNAVAILABLE:
204                         if ( do_retry > 0 ) {
205                                 do_retry--;
206                                 if ( delay > 0) {
207                                     sleep( delay );
208                                 }
209                                 goto retry;
210                         }
211                 /* fallthru */
212                 default:
213                         break;
214                 }
215                 exit( EXIT_FAILURE );
216         }
217
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" );
222                         switch ( rc ) {
223                         case LDAP_NO_SUCH_OBJECT:
224                                 /* NOTE: this likely means
225                                  * the second modrdn failed
226                                  * during the previous round... */
227                                 if ( !friendly ) {
228                                         goto done;
229                                 }
230                                 break;
231
232                         case LDAP_BUSY:
233                         case LDAP_UNAVAILABLE:
234                                 if ( do_retry > 0 ) {
235                                         do_retry--;
236                                         goto retry;
237                                 }
238                                 /* fall thru */
239
240                         default:
241                                 goto done;
242                         }
243                 }
244                 rc = ldap_modrdn2_s( ld, DNs[1], rdns[1], 1 );
245                 if ( rc != LDAP_SUCCESS ) {
246                         ldap_perror( ld, "ldap_modrdn" );
247                         switch ( rc ) {
248                         case LDAP_NO_SUCH_OBJECT:
249                                 /* NOTE: this likely means
250                                  * the first modrdn failed
251                                  * during the previous round... */
252                                 if ( !friendly ) {
253                                         goto done;
254                                 }
255                                 break;
256
257                         case LDAP_BUSY:
258                         case LDAP_UNAVAILABLE:
259                                 if ( do_retry > 0 ) {
260                                         do_retry--;
261                                         goto retry;
262                                 }
263                                 /* fall thru */
264
265                         default:
266                                 goto done;
267                         }
268                 }
269         }
270
271 done:;
272         fprintf( stderr, " PID=%ld - Modrdn done (%d).\n", (long) pid, rc );
273
274         ldap_unbind( ld );
275 }
276
277