]> git.sur5r.net Git - openldap/blob - tests/progs/slapd-modify.c
add concurrency test for back-meta; cleanup previous commit
[openldap] / tests / progs / slapd-modify.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
16 #include "portable.h"
17
18 #include <stdio.h>
19
20 #include <ac/stdlib.h>
21
22 #include <ac/ctype.h>
23 #include <ac/param.h>
24 #include <ac/socket.h>
25 #include <ac/string.h>
26 #include <ac/unistd.h>
27 #include <ac/wait.h>
28
29 #define LDAP_DEPRECATED 1
30 #include <ldap.h>
31
32 #define LOOPS   100
33 #define RETRIES 0
34
35 static void
36 do_modify( char *uri, char *host, int port, char *manager, char *passwd,
37                 char *entry, char *attr, char *value, int maxloop,
38                 int maxretries );
39
40
41 static void
42 usage( char *name )
43 {
44         fprintf( stderr, "usage: %s [-h <host>] -p port -D <managerDN> -w <passwd> -e <entry> [-l <loops>]\n",
45                         name );
46         exit( EXIT_FAILURE );
47 }
48
49 int
50 main( int argc, char **argv )
51 {
52         int             i;
53         char            *uri = NULL;
54         char            *host = "localhost";
55         int             port = -1;
56         char            *manager = NULL;
57         char            *passwd = NULL;
58         char            *entry = NULL;
59         char            *ava = NULL;
60         char            *value = NULL;
61         int             loops = LOOPS;
62         int             retries = RETRIES;
63
64         while ( (i = getopt( argc, argv, "H:h:p:D:w:e:a: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 modify */
87                         entry = strdup( optarg );
88                         break;
89
90                 case 'a':
91                         ava = strdup( optarg );
92                         break;
93
94                 case 'l':               /* the number of loops */
95                         loops = atoi( optarg );
96                         break;
97
98                 case 'r':
99                         retries = atoi( optarg );
100                         break;
101
102                 default:
103                         usage( argv[0] );
104                         break;
105                 }
106         }
107
108         if (( entry == NULL ) || ( ava == NULL ) || ( port == -1 && uri == NULL ))
109                 usage( argv[0] );
110
111         if ( *entry == '\0' ) {
112
113                 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
114                                 argv[0] );
115                 exit( EXIT_FAILURE );
116
117         }
118         if ( *ava  == '\0' ) {
119                 fprintf( stderr, "%s: invalid EMPTY AVA.\n",
120                                 argv[0] );
121                 exit( EXIT_FAILURE );
122         }
123         
124         if ( !( value = strchr( ava, ':' ))) {
125                 fprintf( stderr, "%s: invalid AVA.\n",
126                                 argv[0] );
127                 exit( EXIT_FAILURE );
128         }
129         *value++ = '\0'; 
130         while ( *value && isspace( (unsigned char) *value ))
131                 value++;
132
133         do_modify( uri, host, port, manager, passwd, entry, ava, value,
134                         loops, retries );
135         exit( EXIT_SUCCESS );
136 }
137
138
139 static void
140 do_modify( char *uri, char *host, int port, char *manager,
141         char *passwd, char *entry, char* attr, char* value,
142         int maxloop, int maxretries )
143 {
144         LDAP    *ld = NULL;
145         int     i = 0, do_retry = maxretries;
146         pid_t   pid;
147         int     rc = LDAP_SUCCESS;
148
149         struct ldapmod mod;
150         struct ldapmod *mods[2];
151         char *values[2] = { value, NULL };
152
153         pid = getpid();
154         
155         mod.mod_op = LDAP_MOD_ADD;
156         mod.mod_type = attr;
157         mod.mod_values = values;
158         mods[0] = &mod;
159         mods[1] = NULL;
160
161 retry:;
162         if ( uri ) {
163                 ldap_initialize( &ld, uri );
164         } else {
165                 ld = ldap_init( host, port );
166         }
167         if ( ld == NULL ) {
168                 perror( "ldap_init" );
169                 exit( EXIT_FAILURE );
170         }
171
172         {
173                 int version = LDAP_VERSION3;
174                 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
175                         &version ); 
176         }
177
178         if ( do_retry == maxretries ) {
179                 fprintf( stderr, "PID=%ld - Modify(%d): entry=\"%s\".\n",
180                         (long) pid, maxloop, entry );
181         }
182
183         rc = ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE );
184         if ( rc != LDAP_SUCCESS ) {
185                 ldap_perror( ld, "ldap_bind" );
186                 if ( rc == LDAP_BUSY && do_retry > 0 ) {
187                         do_retry--;
188                         goto retry;
189                 }
190                 exit( EXIT_FAILURE );
191         }
192
193         for ( ; i < maxloop; i++ ) {
194                 mod.mod_op = LDAP_MOD_ADD;
195                 rc = ldap_modify_s( ld, entry, mods );
196                 if ( rc != LDAP_SUCCESS ) {
197                         ldap_perror( ld, "ldap_modify" );
198                         if ( rc == LDAP_BUSY && do_retry > 0 ) {
199                                 do_retry--;
200                                 goto retry;
201                         }
202                         if ( rc != LDAP_NO_SUCH_OBJECT ) break;
203                         continue;
204                 }
205                 
206                 mod.mod_op = LDAP_MOD_DELETE;
207                 rc = ldap_modify_s( ld, entry, mods );
208                 if ( rc != LDAP_SUCCESS ) {
209                         ldap_perror( ld, "ldap_modify" );
210                         if ( rc == LDAP_BUSY && do_retry > 0 ) {
211                                 do_retry--;
212                                 goto retry;
213                         }
214                         if ( rc != LDAP_NO_SUCH_OBJECT ) break;
215                         continue;
216                 }
217
218         }
219
220         fprintf( stderr, " PID=%ld - Modify done (%d).\n", (long) pid, rc );
221
222         ldap_unbind( ld );
223 }
224
225