2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2005 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>
29 #define LDAP_DEPRECATED 1
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, int delay, int friendly );
46 "-H <uri> | ([-h <host>] -p <port>) "
58 main( int argc, char **argv )
62 char *host = "localhost";
70 int retries = RETRIES;
74 while ( (i = getopt( argc, argv, "FH:h:p:D:w:e:a:l:r:t:" )) != EOF ) {
80 case 'H': /* the server uri */
81 uri = strdup( optarg );
84 case 'h': /* the servers host */
85 host = strdup( optarg );
88 case 'p': /* the servers port */
89 port = atoi( optarg );
92 case 'D': /* the servers manager */
93 manager = strdup( optarg );
96 case 'w': /* the server managers password */
97 passwd = strdup( optarg );
100 case 'e': /* entry to modify */
101 entry = strdup( optarg );
105 ava = strdup( optarg );
108 case 'l': /* the number of loops */
109 loops = atoi( optarg );
112 case 'r': /* number of retries */
113 retries = atoi( optarg );
116 case 't': /* delay in seconds */
117 delay = atoi( optarg );
126 if (( entry == NULL ) || ( ava == NULL ) || ( port == -1 && uri == NULL ))
129 if ( *entry == '\0' ) {
131 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
133 exit( EXIT_FAILURE );
136 if ( *ava == '\0' ) {
137 fprintf( stderr, "%s: invalid EMPTY AVA.\n",
139 exit( EXIT_FAILURE );
142 if ( !( value = strchr( ava, ':' ))) {
143 fprintf( stderr, "%s: invalid AVA.\n",
145 exit( EXIT_FAILURE );
148 while ( *value && isspace( (unsigned char) *value ))
151 do_modify( uri, host, port, manager, passwd, entry, ava, value,
152 loops, retries, delay, friendly );
153 exit( EXIT_SUCCESS );
158 do_modify( char *uri, char *host, int port, char *manager,
159 char *passwd, char *entry, char* attr, char* value,
160 int maxloop, int maxretries, int delay, int friendly )
163 int i = 0, do_retry = maxretries;
165 int rc = LDAP_SUCCESS;
168 struct ldapmod *mods[2];
175 mod.mod_op = LDAP_MOD_ADD;
177 mod.mod_values = values;
183 ldap_initialize( &ld, uri );
185 ld = ldap_init( host, port );
188 perror( "ldap_init" );
189 exit( EXIT_FAILURE );
193 int version = LDAP_VERSION3;
194 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
198 if ( do_retry == maxretries ) {
199 fprintf( stderr, "PID=%ld - Modify(%d): entry=\"%s\".\n",
200 (long) pid, maxloop, entry );
203 rc = ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE );
204 if ( rc != LDAP_SUCCESS ) {
205 ldap_perror( ld, "ldap_bind" );
208 case LDAP_UNAVAILABLE:
209 if ( do_retry > 0 ) {
220 exit( EXIT_FAILURE );
223 for ( ; i < maxloop; i++ ) {
224 mod.mod_op = LDAP_MOD_ADD;
225 rc = ldap_modify_s( ld, entry, mods );
226 if ( rc != LDAP_SUCCESS ) {
227 ldap_perror( ld, "ldap_modify" );
229 case LDAP_TYPE_OR_VALUE_EXISTS:
230 /* NOTE: this likely means
231 * the second modify failed
232 * during the previous round... */
239 case LDAP_UNAVAILABLE:
240 if ( do_retry > 0 ) {
251 mod.mod_op = LDAP_MOD_DELETE;
252 rc = ldap_modify_s( ld, entry, mods );
253 if ( rc != LDAP_SUCCESS ) {
254 ldap_perror( ld, "ldap_modify" );
256 case LDAP_NO_SUCH_ATTRIBUTE:
257 /* NOTE: this likely means
258 * the first modify failed
259 * during the previous round... */
266 case LDAP_UNAVAILABLE:
267 if ( do_retry > 0 ) {
281 fprintf( stderr, " PID=%ld - Modify done (%d).\n", (long) pid, rc );