2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2006 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
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>.
20 #include <ac/stdlib.h>
24 #include <ac/socket.h>
25 #include <ac/string.h>
26 #include <ac/unistd.h>
32 #include "slapd-common.h"
38 do_modify( char *uri, char *manager, struct berval *passwd,
39 char *entry, char *attr, char *value, int maxloop,
40 int maxretries, int delay, int friendly, int chaserefs );
48 "-H <uri> | ([-h <host>] -p <port>) "
64 main( int argc, char **argv )
68 char *host = "localhost";
71 struct berval passwd = { 0, NULL };
77 int retries = RETRIES;
82 tester_init( "slapd-modify", TESTER_MODIFY );
84 while ( (i = getopt( argc, argv, "CFH:h:i:p:D:w:e:a:l:L:r:t:" )) != EOF ) {
94 case 'H': /* the server uri */
95 uri = strdup( optarg );
98 case 'h': /* the servers host */
99 host = strdup( optarg );
103 /* ignored (!) by now */
106 case 'p': /* the servers port */
107 if ( lutil_atoi( &port, optarg ) != 0 ) {
112 case 'D': /* the servers manager */
113 manager = strdup( optarg );
116 case 'w': /* the server managers password */
117 passwd.bv_val = strdup( optarg );
118 passwd.bv_len = strlen( optarg );
121 case 'e': /* entry to modify */
122 entry = strdup( optarg );
126 ava = strdup( optarg );
129 case 'l': /* the number of loops */
130 if ( lutil_atoi( &loops, optarg ) != 0 ) {
135 case 'L': /* the number of outerloops */
136 if ( lutil_atoi( &outerloops, optarg ) != 0 ) {
141 case 'r': /* number of retries */
142 if ( lutil_atoi( &retries, optarg ) != 0 ) {
147 case 't': /* delay in seconds */
148 if ( lutil_atoi( &delay, optarg ) != 0 ) {
159 if (( entry == NULL ) || ( ava == NULL ) || ( port == -1 && uri == NULL ))
162 if ( *entry == '\0' ) {
164 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
166 exit( EXIT_FAILURE );
169 if ( *ava == '\0' ) {
170 fprintf( stderr, "%s: invalid EMPTY AVA.\n",
172 exit( EXIT_FAILURE );
175 if ( !( value = strchr( ava, ':' ))) {
176 fprintf( stderr, "%s: invalid AVA.\n",
178 exit( EXIT_FAILURE );
181 while ( *value && isspace( (unsigned char) *value ))
184 uri = tester_uri( uri, host, port );
186 for ( i = 0; i < outerloops; i++ ) {
187 do_modify( uri, manager, &passwd, entry, ava, value,
188 loops, retries, delay, friendly, chaserefs );
191 exit( EXIT_SUCCESS );
196 do_modify( char *uri, char *manager,
197 struct berval *passwd, char *entry, char* attr, char* value,
198 int maxloop, int maxretries, int delay, int friendly, int chaserefs )
201 int i = 0, do_retry = maxretries;
203 int rc = LDAP_SUCCESS;
206 struct ldapmod *mods[2];
208 int version = LDAP_VERSION3;
214 mod.mod_op = LDAP_MOD_ADD;
216 mod.mod_values = values;
221 ldap_initialize( &ld, uri );
223 tester_perror( "ldap_initialize", NULL );
224 exit( EXIT_FAILURE );
227 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
228 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
229 chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF );
231 if ( do_retry == maxretries ) {
232 fprintf( stderr, "PID=%ld - Modify(%d): entry=\"%s\".\n",
233 (long) pid, maxloop, entry );
236 rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
237 if ( rc != LDAP_SUCCESS ) {
238 tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
241 case LDAP_UNAVAILABLE:
242 if ( do_retry > 0 ) {
253 exit( EXIT_FAILURE );
256 for ( ; i < maxloop; i++ ) {
257 mod.mod_op = LDAP_MOD_ADD;
258 rc = ldap_modify_ext_s( ld, entry, mods, NULL, NULL );
259 if ( rc != LDAP_SUCCESS ) {
260 tester_ldap_error( ld, "ldap_modify_ext_s", NULL );
262 case LDAP_TYPE_OR_VALUE_EXISTS:
263 /* NOTE: this likely means
264 * the second modify failed
265 * during the previous round... */
272 case LDAP_UNAVAILABLE:
273 if ( do_retry > 0 ) {
284 mod.mod_op = LDAP_MOD_DELETE;
285 rc = ldap_modify_ext_s( ld, entry, mods, NULL, NULL );
286 if ( rc != LDAP_SUCCESS ) {
287 tester_ldap_error( ld, "ldap_modify_ext_s", NULL );
289 case LDAP_NO_SUCH_ATTRIBUTE:
290 /* NOTE: this likely means
291 * the first modify failed
292 * during the previous round... */
299 case LDAP_UNAVAILABLE:
300 if ( do_retry > 0 ) {
314 fprintf( stderr, " PID=%ld - Modify done (%d).\n", (long) pid, rc );
316 ldap_unbind_ext( ld, NULL, NULL );