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