2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2004 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
35 do_modify( char *uri, char *host, int port, char *manager, char *passwd, char *entry,
36 char *attr, char *value, int maxloop );
42 fprintf( stderr, "usage: %s [-h <host>] -p port -D <managerDN> -w <passwd> -e <entry> [-l <loops>]\n",
48 main( int argc, char **argv )
52 char *host = "localhost";
61 while ( (i = getopt( argc, argv, "H:h:p:D:w:e:a:l:" )) != EOF ) {
63 case 'H': /* the server uri */
64 uri = strdup( optarg );
66 case 'h': /* the servers host */
67 host = strdup( optarg );
70 case 'p': /* the servers port */
71 port = atoi( optarg );
73 case 'D': /* the servers manager */
74 manager = strdup( optarg );
77 case 'w': /* the server managers password */
78 passwd = strdup( optarg );
80 case 'e': /* entry to modify */
81 entry = strdup( optarg );
84 ava = strdup( optarg );
86 case 'l': /* the number of loops */
87 loops = atoi( optarg );
96 if (( entry == NULL ) || ( ava == NULL ) || ( port == -1 && uri == NULL ))
99 if ( *entry == '\0' ) {
101 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
103 exit( EXIT_FAILURE );
106 if ( *ava == '\0' ) {
107 fprintf( stderr, "%s: invalid EMPTY AVA.\n",
109 exit( EXIT_FAILURE );
112 if ( !( value = strchr( ava, ':' ))) {
113 fprintf( stderr, "%s: invalid AVA.\n",
115 exit( EXIT_FAILURE );
118 while ( *value && isspace( (unsigned char) *value ))
121 do_modify( uri, host, port, manager, passwd, entry, ava, value, loops );
122 exit( EXIT_SUCCESS );
127 do_modify( char *uri, char *host, int port, char *manager,
128 char *passwd, char *entry, char* attr, char* value, int maxloop )
135 struct ldapmod *mods[2];
136 char *values[2] = { value, NULL };
140 mod.mod_op = LDAP_MOD_ADD;
142 mod.mod_values = values;
148 ldap_initialize( &ld, uri );
150 ld = ldap_init( host, port );
153 perror( "ldap_init" );
154 exit( EXIT_FAILURE );
158 int version = LDAP_VERSION3;
159 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
163 if ( ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE ) != LDAP_SUCCESS ) {
164 ldap_perror( ld, "ldap_bind" );
165 exit( EXIT_FAILURE );
169 fprintf( stderr, "PID=%ld - Modify(%d): entry=\"%s\".\n",
170 (long) pid, maxloop, entry );
172 for ( i = 0; i < maxloop; i++ ) {
175 mod.mod_op = LDAP_MOD_ADD;
176 if (( rc = ldap_modify_s( ld, entry, mods )) != LDAP_SUCCESS ) {
177 ldap_perror( ld, "ldap_modify" );
178 if ( rc != LDAP_NO_SUCH_OBJECT ) break;
182 mod.mod_op = LDAP_MOD_DELETE;
183 if (( rc = ldap_modify_s( ld, entry, mods )) != LDAP_SUCCESS ) {
184 ldap_perror( ld, "ldap_modify" );
185 if ( rc != LDAP_NO_SUCH_OBJECT ) break;
191 fprintf( stderr, " PID=%ld - Modify done.\n", (long) pid );