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