]> git.sur5r.net Git - openldap/blob - tests/progs/slapd-modrdn.c
add concurrency test for back-meta; cleanup previous commit
[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 );
42
43 static void
44 usage( char *name )
45 {
46         fprintf( stderr, "usage: %s [-h <host>] -p port -D <managerDN> -w <passwd> -e <entry> [-l <loops>]\n",
47                         name );
48         exit( EXIT_FAILURE );
49 }
50
51 int
52 main( int argc, char **argv )
53 {
54         int             i;
55         char            *uri = NULL;
56         char            *host = "localhost";
57         int             port = -1;
58         char            *manager = NULL;
59         char            *passwd = NULL;
60         char            *entry = NULL;
61         int             loops = LOOPS;
62         int             retries = RETRIES;
63
64         while ( (i = getopt( argc, argv, "H:h:p:D:w:e:l:r:" )) != EOF ) {
65                 switch( i ) {
66                 case 'H':               /* the server uri */
67                         uri = strdup( optarg );
68                         break;
69
70                 case 'h':               /* the servers host */
71                         host = strdup( optarg );
72                         break;
73
74                 case 'p':               /* the servers port */
75                         port = atoi( optarg );
76                         break;
77
78                 case 'D':               /* the servers manager */
79                         manager = strdup( optarg );
80                         break;
81
82                 case 'w':               /* the server managers password */
83                         passwd = strdup( optarg );
84                         break;
85
86                 case 'e':               /* entry to rename */
87                         entry = strdup( optarg );
88                         break;
89
90                 case 'l':               /* the number of loops */
91                         loops = atoi( optarg );
92                         break;
93
94                 case 'r':               /* the number of retries */
95                         retries = atoi( optarg );
96                         break;
97
98                 default:
99                         usage( argv[0] );
100                         break;
101                 }
102         }
103
104         if (( entry == NULL ) || ( port == -1 && uri == NULL ))
105                 usage( argv[0] );
106
107         if ( *entry == '\0' ) {
108
109                 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
110                                 argv[0] );
111                 exit( EXIT_FAILURE );
112
113         }
114
115         do_modrdn( uri, host, port, manager, passwd, entry, loops, retries );
116         exit( EXIT_SUCCESS );
117 }
118
119
120 static void
121 do_modrdn( char *uri, char *host, int port, char *manager,
122         char *passwd, char *entry, int maxloop, int maxretries )
123 {
124         LDAP    *ld = NULL;
125         int     i = 0, do_retry = maxretries;
126         pid_t   pid;
127         char *DNs[2];
128         char *rdns[2];
129         int         rc = LDAP_SUCCESS;
130
131
132         pid = getpid();
133         DNs[0] = entry;
134         DNs[1] = strdup( entry );
135
136         /* reverse the RDN, make new DN */
137         {
138                 char *p1, *p2;
139
140                 p1 = strchr( entry, '=' ) + 1;
141                 p2 = strchr( p1, ',' );
142
143                 *p2 = '\0';
144                 rdns[1] = strdup( entry );
145                 *p2-- = ',';
146
147                 for (i = p1 - entry;p2 >= p1;)
148                         DNs[1][i++] = *p2--;
149                 
150                 DNs[1][i] = '\0';
151                 rdns[0] = strdup( DNs[1] );
152                 DNs[1][i] = ',';
153         }
154
155 retry:;
156         if ( uri ) {
157                 ldap_initialize( &ld, uri );
158         } else {
159                 ld = ldap_init( host, port );
160         }
161         if ( ld == NULL ) {
162                 perror( "ldap_init" );
163                 exit( EXIT_FAILURE );
164         }
165
166         {
167                 int version = LDAP_VERSION3;
168                 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
169                         &version ); 
170         }
171
172         if ( do_retry == maxretries ) {
173                 fprintf( stderr, "PID=%ld - Modrdn(%d): entry=\"%s\".\n",
174                         (long) pid, maxloop, entry );
175         }
176
177         rc = ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE );
178         if ( rc != LDAP_SUCCESS ) {
179                 ldap_perror( ld, "ldap_bind" );
180                 if ( rc == LDAP_BUSY && do_retry > 0 ) {
181                         do_retry--;
182                         goto retry;
183                 }
184                 exit( EXIT_FAILURE );
185         }
186
187         for ( ; i < maxloop; i++ ) {
188                 rc = ldap_modrdn2_s( ld, DNs[0], rdns[0], 0 );
189                 if ( rc != LDAP_SUCCESS ) {
190                         ldap_perror( ld, "ldap_modrdn" );
191                         if ( rc == LDAP_BUSY && do_retry > 0 ) {
192                                 do_retry--;
193                                 goto retry;
194                         }
195                         if ( rc != LDAP_NO_SUCH_OBJECT ) break;
196                         continue;
197                 }
198                 rc = ldap_modrdn2_s( ld, DNs[1], rdns[1], 1 );
199                 if ( rc != LDAP_SUCCESS ) {
200                         ldap_perror( ld, "ldap_modrdn" );
201                         if ( rc == LDAP_BUSY && do_retry > 0 ) {
202                                 do_retry--;
203                                 goto retry;
204                         }
205                         if ( rc != LDAP_NO_SUCH_OBJECT ) break;
206                         continue;
207                 }
208         }
209
210         fprintf( stderr, " PID=%ld - Modrdn done (%d).\n", (long) pid, rc );
211
212         ldap_unbind( ld );
213 }
214
215