]> git.sur5r.net Git - openldap/blob - tests/progs/slapd-modrdn.c
92e3970b93b39a9b5761cc1b3b3e0ede8b18af6d
[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,
97         char *passwd, char *entry, int maxloop )
98 {
99         LDAP    *ld = NULL;
100         int     i;
101         pid_t   pid;
102         char *DNs[2];
103         char *rdns[2];
104
105         pid = getpid();
106         DNs[0] = entry;
107         DNs[1] = strdup( entry );
108
109         /* reverse the RDN, make new DN */
110         {
111                 char *p1, *p2;
112
113                 p1 = strchr( entry, '=' ) + 1;
114                 p2 = strchr( p1, ',' );
115
116                 *p2 = '\0';
117                 rdns[1] = strdup( entry );
118                 *p2-- = ',';
119
120                 for (i = p1 - entry;p2 >= p1;)
121                         DNs[1][i++] = *p2--;
122                 
123                 DNs[1][i] = '\0';
124                 rdns[0] = strdup( DNs[1] );
125                 DNs[1][i] = ',';
126         }
127                 
128         if ( uri ) {
129                 ldap_initialize( &ld, uri );
130         } else {
131                 ld = ldap_init( host, port );
132         }
133         if ( ld == NULL ) {
134                 perror( "ldap_init" );
135                 exit( EXIT_FAILURE );
136         }
137
138         {
139                 int version = LDAP_VERSION3;
140                 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
141                         &version ); 
142         }
143
144         if ( ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE ) != LDAP_SUCCESS ) {
145                 ldap_perror( ld, "ldap_bind" );
146                  exit( EXIT_FAILURE );
147         }
148
149
150         fprintf( stderr, "PID=%ld - Modrdn(%d): entry=\"%s\".\n",
151                  (long) pid, maxloop, entry );
152
153         for ( i = 0; i < maxloop; i++ ) {
154                 int         rc;
155
156                 if (( rc = ldap_modrdn2_s( ld, DNs[0], rdns[0], 0 ))
157                         != LDAP_SUCCESS ) {
158                         ldap_perror( ld, "ldap_modrdn" );
159                         if ( rc != LDAP_NO_SUCH_OBJECT ) break;
160                         continue;
161                 }
162                 if (( rc = ldap_modrdn2_s( ld, DNs[1], rdns[1], 1 ))
163                         != LDAP_SUCCESS ) {
164                         ldap_perror( ld, "ldap_modrdn" );
165                         if ( rc != LDAP_NO_SUCH_OBJECT ) break;
166                         continue;
167                 }
168         }
169
170         fprintf( stderr, " PID=%ld - Modrdn done.\n", (long) pid );
171
172         ldap_unbind( ld );
173 }
174
175