]> git.sur5r.net Git - openldap/blob - tests/progs/slapd-modify.c
847785947ab07194e16ebe1344a11a9244657b50
[openldap] / tests / progs / slapd-modify.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2005 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
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>.
14  */
15
16 #include "portable.h"
17
18 #include <stdio.h>
19
20 #include <ac/stdlib.h>
21
22 #include <ac/ctype.h>
23 #include <ac/param.h>
24 #include <ac/socket.h>
25 #include <ac/string.h>
26 #include <ac/unistd.h>
27 #include <ac/wait.h>
28
29 #define LDAP_DEPRECATED 1
30 #include <ldap.h>
31
32 #define LOOPS   100
33
34 static void
35 do_modify( char *uri, char *host, int port, char *manager, char *passwd, char *entry, 
36                 char *attr, char *value, int maxloop );
37
38
39 static void
40 usage( char *name )
41 {
42         fprintf( stderr, "usage: %s [-h <host>] -p port -D <managerDN> -w <passwd> -e <entry> [-l <loops>]\n",
43                         name );
44         exit( EXIT_FAILURE );
45 }
46
47 int
48 main( int argc, char **argv )
49 {
50         int             i;
51         char            *uri = NULL;
52         char        *host = "localhost";
53         int                     port = -1;
54         char            *manager = NULL;
55         char            *passwd = NULL;
56         char            *entry = NULL;
57         char            *ava = NULL;
58         char            *value = NULL;
59         int                     loops = LOOPS;
60
61         while ( (i = getopt( argc, argv, "H:h:p:D:w:e:a:l:" )) != EOF ) {
62                 switch( i ) {
63                         case 'H':               /* the server uri */
64                                 uri = strdup( optarg );
65                         break;
66                         case 'h':               /* the servers host */
67                                 host = strdup( optarg );
68                         break;
69
70                         case 'p':               /* the servers port */
71                                 port = atoi( optarg );
72                         break;
73                         case 'D':               /* the servers manager */
74                                 manager = strdup( optarg );
75                         break;
76
77                         case 'w':               /* the server managers password */
78                                 passwd = strdup( optarg );
79                         break;
80                         case 'e':               /* entry to modify */
81                                 entry = strdup( optarg );
82                         break;
83                         case 'a':
84                                 ava = strdup( optarg );
85                         break;
86                         case 'l':               /* the number of loops */
87                                 loops = atoi( optarg );
88                         break;
89
90                         default:
91                                 usage( argv[0] );
92                         break;
93                 }
94         }
95
96         if (( entry == NULL ) || ( ava == NULL ) || ( port == -1 && uri == NULL ))
97                 usage( argv[0] );
98
99         if ( *entry == '\0' ) {
100
101                 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
102                                 argv[0] );
103                 exit( EXIT_FAILURE );
104
105         }
106         if ( *ava  == '\0' ) {
107                 fprintf( stderr, "%s: invalid EMPTY AVA.\n",
108                                 argv[0] );
109                 exit( EXIT_FAILURE );
110         }
111         
112         if ( !( value = strchr( ava, ':' ))) {
113                 fprintf( stderr, "%s: invalid AVA.\n",
114                                 argv[0] );
115                 exit( EXIT_FAILURE );
116         }
117         *value++ = '\0'; 
118         while ( *value && isspace( (unsigned char) *value ))
119                 value++;
120
121         do_modify( uri, host, port, manager, passwd, entry, ava, value, loops );
122         exit( EXIT_SUCCESS );
123 }
124
125
126 static void
127 do_modify( char *uri, char *host, int port, char *manager,
128         char *passwd, char *entry, char* attr, char* value, int maxloop )
129 {
130         LDAP    *ld = NULL;
131         int     i;
132         pid_t   pid;
133         int     rc = LDAP_SUCCESS;
134
135         struct ldapmod mod;
136         struct ldapmod *mods[2];
137         char *values[2] = { value, NULL };
138
139         pid = getpid();
140         
141         mod.mod_op = LDAP_MOD_ADD;
142         mod.mod_type = attr;
143         mod.mod_values = values;
144         mods[0] = &mod;
145         mods[1] = NULL;
146
147         
148         if ( uri ) {
149                 ldap_initialize( &ld, uri );
150         } else {
151                 ld = ldap_init( host, port );
152         }
153         if ( ld == NULL ) {
154                 perror( "ldap_init" );
155                 exit( EXIT_FAILURE );
156         }
157
158         {
159                 int version = LDAP_VERSION3;
160                 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
161                         &version ); 
162         }
163
164         if ( ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE ) != LDAP_SUCCESS ) {
165                 ldap_perror( ld, "ldap_bind" );
166                  exit( EXIT_FAILURE );
167         }
168
169
170         fprintf( stderr, "PID=%ld - Modify(%d): entry=\"%s\".\n",
171                  (long) pid, maxloop, entry );
172
173         for ( i = 0; i < maxloop; i++ ) {
174                 mod.mod_op = LDAP_MOD_ADD;
175                 if (( rc = ldap_modify_s( ld, entry, mods )) != LDAP_SUCCESS ) {
176                         ldap_perror( ld, "ldap_modify" );
177                         if ( rc != LDAP_NO_SUCH_OBJECT ) break;
178                         continue;
179                 }
180                 
181                 mod.mod_op = LDAP_MOD_DELETE;
182                 if (( rc = ldap_modify_s( ld, entry, mods )) != LDAP_SUCCESS ) {
183                         ldap_perror( ld, "ldap_modify" );
184                         if ( rc != LDAP_NO_SUCH_OBJECT ) break;
185                         continue;
186                 }
187
188         }
189
190         fprintf( stderr, " PID=%ld - Modify done (%d).\n", (long) pid, rc );
191
192         ldap_unbind( ld );
193 }
194
195