]> git.sur5r.net Git - openldap/blob - tests/progs/slapd-modify.c
Better fix for req_pwdexop_s
[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 #define RETRIES 0
34
35 static void
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 );
39
40
41 static void
42 usage( char *name )
43 {
44         fprintf( stderr,
45                 "usage: %s "
46                 "-H <uri> | ([-h <host>] -p <port>) "
47                 "-D <manager> "
48                 "-w <passwd> "
49                 "-e <entry> "
50                 "[-l <loops>] "
51                 "[-r <maxretries>] "
52                 "[-t <delay>]\n",
53                         name );
54         exit( EXIT_FAILURE );
55 }
56
57 int
58 main( int argc, char **argv )
59 {
60         int             i;
61         char            *uri = NULL;
62         char            *host = "localhost";
63         int             port = -1;
64         char            *manager = NULL;
65         char            *passwd = NULL;
66         char            *entry = NULL;
67         char            *ava = NULL;
68         char            *value = NULL;
69         int             loops = LOOPS;
70         int             retries = RETRIES;
71         int             delay = 0;
72
73         while ( (i = getopt( argc, argv, "H:h:p:D:w:e:a:l:r:t:" )) != EOF ) {
74                 switch( i ) {
75                 case 'H':               /* the server uri */
76                         uri = strdup( optarg );
77                         break;
78
79                 case 'h':               /* the servers host */
80                         host = strdup( optarg );
81                         break;
82
83                 case 'p':               /* the servers port */
84                         port = atoi( optarg );
85                         break;
86
87                 case 'D':               /* the servers manager */
88                         manager = strdup( optarg );
89                         break;
90
91                 case 'w':               /* the server managers password */
92                         passwd = strdup( optarg );
93                         break;
94
95                 case 'e':               /* entry to modify */
96                         entry = strdup( optarg );
97                         break;
98
99                 case 'a':
100                         ava = strdup( optarg );
101                         break;
102
103                 case 'l':               /* the number of loops */
104                         loops = atoi( optarg );
105                         break;
106
107                 case 'r':               /* number of retries */
108                         retries = atoi( optarg );
109                         break;
110
111                 case 't':               /* delay in seconds */
112                         delay = atoi( optarg );
113                         break;
114
115                 default:
116                         usage( argv[0] );
117                         break;
118                 }
119         }
120
121         if (( entry == NULL ) || ( ava == NULL ) || ( port == -1 && uri == NULL ))
122                 usage( argv[0] );
123
124         if ( *entry == '\0' ) {
125
126                 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
127                                 argv[0] );
128                 exit( EXIT_FAILURE );
129
130         }
131         if ( *ava  == '\0' ) {
132                 fprintf( stderr, "%s: invalid EMPTY AVA.\n",
133                                 argv[0] );
134                 exit( EXIT_FAILURE );
135         }
136         
137         if ( !( value = strchr( ava, ':' ))) {
138                 fprintf( stderr, "%s: invalid AVA.\n",
139                                 argv[0] );
140                 exit( EXIT_FAILURE );
141         }
142         *value++ = '\0'; 
143         while ( *value && isspace( (unsigned char) *value ))
144                 value++;
145
146         do_modify( uri, host, port, manager, passwd, entry, ava, value,
147                         loops, retries, delay );
148         exit( EXIT_SUCCESS );
149 }
150
151
152 static void
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 )
156 {
157         LDAP    *ld = NULL;
158         int     i = 0, do_retry = maxretries;
159         pid_t   pid;
160         int     rc = LDAP_SUCCESS;
161
162         struct ldapmod mod;
163         struct ldapmod *mods[2];
164         char *values[2];
165
166         pid = getpid();
167         
168         values[0] = value;
169         values[1] = NULL;
170         mod.mod_op = LDAP_MOD_ADD;
171         mod.mod_type = attr;
172         mod.mod_values = values;
173         mods[0] = &mod;
174         mods[1] = NULL;
175
176 retry:;
177         if ( uri ) {
178                 ldap_initialize( &ld, uri );
179         } else {
180                 ld = ldap_init( host, port );
181         }
182         if ( ld == NULL ) {
183                 perror( "ldap_init" );
184                 exit( EXIT_FAILURE );
185         }
186
187         {
188                 int version = LDAP_VERSION3;
189                 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
190                         &version ); 
191         }
192
193         if ( do_retry == maxretries ) {
194                 fprintf( stderr, "PID=%ld - Modify(%d): entry=\"%s\".\n",
195                         (long) pid, maxloop, entry );
196         }
197
198         rc = ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE );
199         if ( rc != LDAP_SUCCESS ) {
200                 ldap_perror( ld, "ldap_bind" );
201                 switch ( rc ) {
202                 case LDAP_BUSY:
203                 case LDAP_UNAVAILABLE:
204                         if ( do_retry > 0 ) {
205                                 do_retry--;
206                                 if ( delay > 0 ) {
207                                     sleep( delay );
208                                 }
209                                 goto retry;
210                         }
211                 /* fallthru */
212                 default:
213                         break;
214                 }
215                 exit( EXIT_FAILURE );
216         }
217
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 ) {
224                                 do_retry--;
225                                 goto retry;
226                         }
227                         if ( rc != LDAP_NO_SUCH_OBJECT ) break;
228                         continue;
229                 }
230                 
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 ) {
236                                 do_retry--;
237                                 goto retry;
238                         }
239                         if ( rc != LDAP_NO_SUCH_OBJECT ) break;
240                         continue;
241                 }
242
243         }
244
245         fprintf( stderr, " PID=%ld - Modify done (%d).\n", (long) pid, rc );
246
247         ldap_unbind( ld );
248 }
249
250