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,
44 fprintf( stderr, "usage: %s [-h <host>] -p port -D <managerDN> -w <passwd> -e <entry> [-l <loops>]\n",
50 main( int argc, char **argv )
54 char *host = "localhost";
62 int retries = RETRIES;
64 while ( (i = getopt( argc, argv, "H:h:p:D:w:e:a:l:r:" )) != EOF ) {
66 case 'H': /* the server uri */
67 uri = strdup( optarg );
70 case 'h': /* the servers host */
71 host = strdup( optarg );
74 case 'p': /* the servers port */
75 port = atoi( optarg );
78 case 'D': /* the servers manager */
79 manager = strdup( optarg );
82 case 'w': /* the server managers password */
83 passwd = strdup( optarg );
86 case 'e': /* entry to modify */
87 entry = strdup( optarg );
91 ava = strdup( optarg );
94 case 'l': /* the number of loops */
95 loops = atoi( optarg );
99 retries = atoi( optarg );
108 if (( entry == NULL ) || ( ava == NULL ) || ( port == -1 && uri == NULL ))
111 if ( *entry == '\0' ) {
113 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
115 exit( EXIT_FAILURE );
118 if ( *ava == '\0' ) {
119 fprintf( stderr, "%s: invalid EMPTY AVA.\n",
121 exit( EXIT_FAILURE );
124 if ( !( value = strchr( ava, ':' ))) {
125 fprintf( stderr, "%s: invalid AVA.\n",
127 exit( EXIT_FAILURE );
130 while ( *value && isspace( (unsigned char) *value ))
133 do_modify( uri, host, port, manager, passwd, entry, ava, value,
135 exit( EXIT_SUCCESS );
140 do_modify( char *uri, char *host, int port, char *manager,
141 char *passwd, char *entry, char* attr, char* value,
142 int maxloop, int maxretries )
145 int i = 0, do_retry = maxretries;
147 int rc = LDAP_SUCCESS;
150 struct ldapmod *mods[2];
157 mod.mod_op = LDAP_MOD_ADD;
159 mod.mod_values = values;
165 ldap_initialize( &ld, uri );
167 ld = ldap_init( host, port );
170 perror( "ldap_init" );
171 exit( EXIT_FAILURE );
175 int version = LDAP_VERSION3;
176 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
180 if ( do_retry == maxretries ) {
181 fprintf( stderr, "PID=%ld - Modify(%d): entry=\"%s\".\n",
182 (long) pid, maxloop, entry );
185 rc = ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE );
186 if ( rc != LDAP_SUCCESS ) {
187 ldap_perror( ld, "ldap_bind" );
188 if ( rc == LDAP_BUSY && do_retry > 0 ) {
192 exit( EXIT_FAILURE );
195 for ( ; i < maxloop; i++ ) {
196 mod.mod_op = LDAP_MOD_ADD;
197 rc = ldap_modify_s( ld, entry, mods );
198 if ( rc != LDAP_SUCCESS ) {
199 ldap_perror( ld, "ldap_modify" );
200 if ( rc == LDAP_BUSY && do_retry > 0 ) {
204 if ( rc != LDAP_NO_SUCH_OBJECT ) break;
208 mod.mod_op = LDAP_MOD_DELETE;
209 rc = ldap_modify_s( ld, entry, mods );
210 if ( rc != LDAP_SUCCESS ) {
211 ldap_perror( ld, "ldap_modify" );
212 if ( rc == LDAP_BUSY && do_retry > 0 ) {
216 if ( rc != LDAP_NO_SUCH_OBJECT ) break;
222 fprintf( stderr, " PID=%ld - Modify done (%d).\n", (long) pid, rc );