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 );
119 memset( optarg, '*', passwd.bv_len );
122 case 'e': /* entry to modify */
123 entry = strdup( optarg );
127 ava = strdup( optarg );
130 case 'l': /* the number of loops */
131 if ( lutil_atoi( &loops, optarg ) != 0 ) {
136 case 'L': /* the number of outerloops */
137 if ( lutil_atoi( &outerloops, optarg ) != 0 ) {
142 case 'r': /* number of retries */
143 if ( lutil_atoi( &retries, optarg ) != 0 ) {
148 case 't': /* delay in seconds */
149 if ( lutil_atoi( &delay, optarg ) != 0 ) {
160 if (( entry == NULL ) || ( ava == NULL ) || ( port == -1 && uri == NULL ))
163 if ( *entry == '\0' ) {
165 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
167 exit( EXIT_FAILURE );
170 if ( *ava == '\0' ) {
171 fprintf( stderr, "%s: invalid EMPTY AVA.\n",
173 exit( EXIT_FAILURE );
176 if ( !( value = strchr( ava, ':' ))) {
177 fprintf( stderr, "%s: invalid AVA.\n",
179 exit( EXIT_FAILURE );
182 while ( *value && isspace( (unsigned char) *value ))
185 uri = tester_uri( uri, host, port );
187 for ( i = 0; i < outerloops; i++ ) {
188 do_modify( uri, manager, &passwd, entry, ava, value,
189 loops, retries, delay, friendly, chaserefs );
192 exit( EXIT_SUCCESS );
197 do_modify( char *uri, char *manager,
198 struct berval *passwd, char *entry, char* attr, char* value,
199 int maxloop, int maxretries, int delay, int friendly, int chaserefs )
202 int i = 0, do_retry = maxretries;
204 int rc = LDAP_SUCCESS;
207 struct ldapmod *mods[2];
209 int version = LDAP_VERSION3;
215 mod.mod_op = LDAP_MOD_ADD;
217 mod.mod_values = values;
222 ldap_initialize( &ld, uri );
224 tester_perror( "ldap_initialize", NULL );
225 exit( EXIT_FAILURE );
228 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
229 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
230 chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF );
232 if ( do_retry == maxretries ) {
233 fprintf( stderr, "PID=%ld - Modify(%d): entry=\"%s\".\n",
234 (long) pid, maxloop, entry );
237 rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
238 if ( rc != LDAP_SUCCESS ) {
239 tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
242 case LDAP_UNAVAILABLE:
243 if ( do_retry > 0 ) {
254 exit( EXIT_FAILURE );
257 for ( ; i < maxloop; i++ ) {
258 mod.mod_op = LDAP_MOD_ADD;
259 rc = ldap_modify_ext_s( ld, entry, mods, NULL, NULL );
260 if ( rc != LDAP_SUCCESS ) {
261 tester_ldap_error( ld, "ldap_modify_ext_s", NULL );
263 case LDAP_TYPE_OR_VALUE_EXISTS:
264 /* NOTE: this likely means
265 * the second modify failed
266 * during the previous round... */
273 case LDAP_UNAVAILABLE:
274 if ( do_retry > 0 ) {
285 mod.mod_op = LDAP_MOD_DELETE;
286 rc = ldap_modify_ext_s( ld, entry, mods, NULL, NULL );
287 if ( rc != LDAP_SUCCESS ) {
288 tester_ldap_error( ld, "ldap_modify_ext_s", NULL );
290 case LDAP_NO_SUCH_ATTRIBUTE:
291 /* NOTE: this likely means
292 * the first modify failed
293 * during the previous round... */
300 case LDAP_UNAVAILABLE:
301 if ( do_retry > 0 ) {
315 fprintf( stderr, " PID=%ld - Modify done (%d).\n", (long) pid, rc );
317 ldap_unbind_ext( ld, NULL, NULL );