1 /* $OpenLDAP: pkg/ldap/tests/progs/slapd-modify.c,v 1.19.2.5 2008/02/11 23:26:50 kurt Exp $ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2008 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
, "a:CD:e:FH:h:i:L:l:p:r:t:w:" ) ) != EOF
)
95 case 'H': /* the server uri */
96 uri
= strdup( optarg
);
99 case 'h': /* the servers host */
100 host
= strdup( optarg
);
104 /* ignored (!) by now */
107 case 'p': /* the servers port */
108 if ( lutil_atoi( &port
, optarg
) != 0 ) {
113 case 'D': /* the servers manager */
114 manager
= strdup( optarg
);
117 case 'w': /* the server managers password */
118 passwd
.bv_val
= strdup( optarg
);
119 passwd
.bv_len
= strlen( optarg
);
120 memset( optarg
, '*', passwd
.bv_len
);
123 case 'e': /* entry to modify */
124 entry
= strdup( optarg
);
128 ava
= strdup( optarg
);
131 case 'l': /* the number of loops */
132 if ( lutil_atoi( &loops
, optarg
) != 0 ) {
137 case 'L': /* the number of outerloops */
138 if ( lutil_atoi( &outerloops
, optarg
) != 0 ) {
143 case 'r': /* number of retries */
144 if ( lutil_atoi( &retries
, optarg
) != 0 ) {
149 case 't': /* delay in seconds */
150 if ( lutil_atoi( &delay
, optarg
) != 0 ) {
161 if (( entry
== NULL
) || ( ava
== NULL
) || ( port
== -1 && uri
== NULL
))
164 if ( *entry
== '\0' ) {
166 fprintf( stderr
, "%s: invalid EMPTY entry DN.\n",
168 exit( EXIT_FAILURE
);
171 if ( *ava
== '\0' ) {
172 fprintf( stderr
, "%s: invalid EMPTY AVA.\n",
174 exit( EXIT_FAILURE
);
177 if ( !( value
= strchr( ava
, ':' ))) {
178 fprintf( stderr
, "%s: invalid AVA.\n",
180 exit( EXIT_FAILURE
);
183 while ( *value
&& isspace( (unsigned char) *value
))
186 uri
= tester_uri( uri
, host
, port
);
188 for ( i
= 0; i
< outerloops
; i
++ ) {
189 do_modify( uri
, manager
, &passwd
, entry
, ava
, value
,
190 loops
, retries
, delay
, friendly
, chaserefs
);
193 exit( EXIT_SUCCESS
);
198 do_modify( char *uri
, char *manager
,
199 struct berval
*passwd
, char *entry
, char* attr
, char* value
,
200 int maxloop
, int maxretries
, int delay
, int friendly
, int chaserefs
)
203 int i
= 0, do_retry
= maxretries
;
204 int rc
= LDAP_SUCCESS
;
207 struct ldapmod
*mods
[2];
209 int version
= LDAP_VERSION3
;
213 mod
.mod_op
= LDAP_MOD_ADD
;
215 mod
.mod_values
= values
;
220 ldap_initialize( &ld
, uri
);
222 tester_perror( "ldap_initialize", NULL
);
223 exit( EXIT_FAILURE
);
226 (void) ldap_set_option( ld
, LDAP_OPT_PROTOCOL_VERSION
, &version
);
227 (void) ldap_set_option( ld
, LDAP_OPT_REFERRALS
,
228 chaserefs
? LDAP_OPT_ON
: LDAP_OPT_OFF
);
230 if ( do_retry
== maxretries
) {
231 fprintf( stderr
, "PID=%ld - Modify(%d): entry=\"%s\".\n",
232 (long) pid
, maxloop
, entry
);
235 rc
= ldap_sasl_bind_s( ld
, manager
, LDAP_SASL_SIMPLE
, passwd
, NULL
, NULL
, NULL
);
236 if ( rc
!= LDAP_SUCCESS
) {
237 tester_ldap_error( ld
, "ldap_sasl_bind_s", NULL
);
240 case LDAP_UNAVAILABLE
:
241 if ( do_retry
> 0 ) {
252 exit( EXIT_FAILURE
);
255 for ( ; i
< maxloop
; i
++ ) {
256 mod
.mod_op
= LDAP_MOD_ADD
;
257 rc
= ldap_modify_ext_s( ld
, entry
, mods
, NULL
, NULL
);
258 if ( rc
!= LDAP_SUCCESS
) {
259 tester_ldap_error( ld
, "ldap_modify_ext_s", NULL
);
261 case LDAP_TYPE_OR_VALUE_EXISTS
:
262 /* NOTE: this likely means
263 * the second modify failed
264 * during the previous round... */
271 case LDAP_UNAVAILABLE
:
272 if ( do_retry
> 0 ) {
283 mod
.mod_op
= LDAP_MOD_DELETE
;
284 rc
= ldap_modify_ext_s( ld
, entry
, mods
, NULL
, NULL
);
285 if ( rc
!= LDAP_SUCCESS
) {
286 tester_ldap_error( ld
, "ldap_modify_ext_s", NULL
);
288 case LDAP_NO_SUCH_ATTRIBUTE
:
289 /* NOTE: this likely means
290 * the first modify failed
291 * during the previous round... */
298 case LDAP_UNAVAILABLE
:
299 if ( do_retry
> 0 ) {
313 fprintf( stderr
, " PID=%ld - Modify done (%d).\n", (long) pid
, rc
);
315 ldap_unbind_ext( ld
, NULL
, NULL
);