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 );
46 "-H <uri> | ([-h <host>] -p <port>) "
58 main( int argc, char **argv )
62 char *host = "localhost";
70 int retries = RETRIES;
73 while ( (i = getopt( argc, argv, "H:h:p:D:w:e:a:l:r:t:" )) != EOF ) {
75 case 'H': /* the server uri */
76 uri = strdup( optarg );
79 case 'h': /* the servers host */
80 host = strdup( optarg );
83 case 'p': /* the servers port */
84 port = atoi( optarg );
87 case 'D': /* the servers manager */
88 manager = strdup( optarg );
91 case 'w': /* the server managers password */
92 passwd = strdup( optarg );
95 case 'e': /* entry to modify */
96 entry = strdup( optarg );
100 ava = strdup( optarg );
103 case 'l': /* the number of loops */
104 loops = atoi( optarg );
107 case 'r': /* number of retries */
108 retries = atoi( optarg );
111 case 't': /* delay in seconds */
112 delay = atoi( optarg );
121 if (( entry == NULL ) || ( ava == NULL ) || ( port == -1 && uri == NULL ))
124 if ( *entry == '\0' ) {
126 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
128 exit( EXIT_FAILURE );
131 if ( *ava == '\0' ) {
132 fprintf( stderr, "%s: invalid EMPTY AVA.\n",
134 exit( EXIT_FAILURE );
137 if ( !( value = strchr( ava, ':' ))) {
138 fprintf( stderr, "%s: invalid AVA.\n",
140 exit( EXIT_FAILURE );
143 while ( *value && isspace( (unsigned char) *value ))
146 do_modify( uri, host, port, manager, passwd, entry, ava, value,
147 loops, retries, delay );
148 exit( EXIT_SUCCESS );
153 do_modify( char *uri, char *host, int port, char *manager,
154 char *passwd, char *entry, char* attr, char* value,
155 int maxloop, int maxretries, int delay )
158 int i = 0, do_retry = maxretries;
160 int rc = LDAP_SUCCESS;
163 struct ldapmod *mods[2];
170 mod.mod_op = LDAP_MOD_ADD;
172 mod.mod_values = values;
178 ldap_initialize( &ld, uri );
180 ld = ldap_init( host, port );
183 perror( "ldap_init" );
184 exit( EXIT_FAILURE );
188 int version = LDAP_VERSION3;
189 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
193 if ( do_retry == maxretries ) {
194 fprintf( stderr, "PID=%ld - Modify(%d): entry=\"%s\".\n",
195 (long) pid, maxloop, entry );
198 rc = ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE );
199 if ( rc != LDAP_SUCCESS ) {
200 ldap_perror( ld, "ldap_bind" );
203 case LDAP_UNAVAILABLE:
204 if ( do_retry > 0 ) {
215 exit( EXIT_FAILURE );
218 for ( ; i < maxloop; i++ ) {
219 mod.mod_op = LDAP_MOD_ADD;
220 rc = ldap_modify_s( ld, entry, mods );
221 if ( rc != LDAP_SUCCESS ) {
222 ldap_perror( ld, "ldap_modify" );
223 if ( rc == LDAP_BUSY && do_retry > 0 ) {
227 if ( rc != LDAP_NO_SUCH_OBJECT ) break;
231 mod.mod_op = LDAP_MOD_DELETE;
232 rc = ldap_modify_s( ld, entry, mods );
233 if ( rc != LDAP_SUCCESS ) {
234 ldap_perror( ld, "ldap_modify" );
235 if ( rc == LDAP_BUSY && do_retry > 0 ) {
239 if ( rc != LDAP_NO_SUCH_OBJECT ) break;
245 fprintf( stderr, " PID=%ld - Modify done (%d).\n", (long) pid, rc );