]> git.sur5r.net Git - openldap/blob - tests/progs/slapd-addel.c
Initial round of changes for 2.3 beta
[openldap] / tests / progs / slapd-addel.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 /* ACKNOWLEDGEMENTS:
16  * This work was initially developed by Kurt Spanier for inclusion
17  * in OpenLDAP Software.
18  */
19  
20 #include "portable.h"
21
22 #include <stdio.h>
23
24 #include <ac/stdlib.h>
25
26 #include <ac/ctype.h>
27 #include <ac/param.h>
28 #include <ac/socket.h>
29 #include <ac/string.h>
30 #include <ac/unistd.h>
31 #include <ac/wait.h>
32
33 #define LDAP_DEPRECATED 1
34 #include <ldap.h>
35
36 #define LOOPS   100
37 #define RETRIES 0
38
39 static char *
40 get_add_entry( char *filename, LDAPMod ***mods );
41
42 static void
43 do_addel( char *uri, char *host, int port, char *manager, char *passwd,
44         char *dn, LDAPMod **attrs, int maxloop, int maxretries );
45
46 static void
47 usage( char *name )
48 {
49         fprintf( stderr, "usage: %s [-h <host>] -p port -D <managerDN> -w <passwd> -f <addfile> [-l <loops>]\n",
50                         name );
51         exit( EXIT_FAILURE );
52 }
53
54 int
55 main( int argc, char **argv )
56 {
57         int             i;
58         char            *host = "localhost";
59         char            *uri = NULL;
60         int             port = -1;
61         char            *manager = NULL;
62         char            *passwd = NULL;
63         char            *filename = NULL;
64         char            *entry = NULL;
65         int             loops = LOOPS;
66         int             retries = RETRIES;
67         LDAPMod         **attrs = NULL;
68
69         while ( (i = getopt( argc, argv, "H:h:p:D:w:f:l:r:" )) != EOF ) {
70                 switch( i ) {
71                 case 'H':               /* the server's URI */
72                         uri = strdup( optarg );
73                         break;
74
75                 case 'h':               /* the servers host */
76                         host = strdup( optarg );
77                         break;
78
79                 case 'p':               /* the servers port */
80                         port = atoi( optarg );
81                         break;
82
83                 case 'D':               /* the servers manager */
84                         manager = strdup( optarg );
85                         break;
86
87                 case 'w':               /* the server managers password */
88                         passwd = strdup( optarg );
89                         break;
90
91                 case 'f':               /* file with entry search request */
92                         filename = strdup( optarg );
93                         break;
94
95                 case 'l':               /* the number of loops */
96                         loops = atoi( optarg );
97                         break;
98
99                 case 'r':
100                         retries = atoi( optarg );
101                         break;
102
103                 default:
104                         usage( argv[0] );
105                         break;
106                 }
107         }
108
109         if (( filename == NULL ) || ( port == -1 && uri == NULL ) ||
110                                 ( manager == NULL ) || ( passwd == NULL ))
111                 usage( argv[0] );
112
113         entry = get_add_entry( filename, &attrs );
114         if (( entry == NULL ) || ( *entry == '\0' )) {
115
116                 fprintf( stderr, "%s: invalid entry DN in file \"%s\".\n",
117                                 argv[0], filename );
118                 exit( EXIT_FAILURE );
119
120         }
121
122         if (( attrs == NULL ) || ( *attrs == '\0' )) {
123
124                 fprintf( stderr, "%s: invalid attrs in file \"%s\".\n",
125                                 argv[0], filename );
126                 exit( EXIT_FAILURE );
127
128         }
129
130         do_addel( uri, host, port, manager, passwd, entry, attrs,
131                         loops, retries );
132
133         exit( EXIT_SUCCESS );
134 }
135
136
137 static void
138 addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
139 {
140     LDAPMod             **pmods;
141     int                 i, j;
142     struct berval       *bvp;
143
144     pmods = *pmodsp;
145     modop |= LDAP_MOD_BVALUES;
146
147     i = 0;
148     if ( pmods != NULL ) {
149                 for ( ; pmods[ i ] != NULL; ++i ) {
150                 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
151                         pmods[ i ]->mod_op == modop ) {
152                                 break;
153                 }
154                 }
155     }
156
157     if ( pmods == NULL || pmods[ i ] == NULL ) {
158                 if (( pmods = (LDAPMod **)realloc( pmods, (i + 2) *
159                         sizeof( LDAPMod * ))) == NULL ) {
160                         perror( "realloc" );
161                         exit( EXIT_FAILURE );
162                 }
163                 *pmodsp = pmods;
164                 pmods[ i + 1 ] = NULL;
165                 if (( pmods[ i ] = (LDAPMod *)calloc( 1, sizeof( LDAPMod )))
166                         == NULL ) {
167                         perror( "calloc" );
168                         exit( EXIT_FAILURE );
169                 }
170                 pmods[ i ]->mod_op = modop;
171                 if (( pmods[ i ]->mod_type = strdup( attr )) == NULL ) {
172                 perror( "strdup" );
173                 exit( EXIT_FAILURE );
174                 }
175     }
176
177     if ( value != NULL ) {
178                 j = 0;
179                 if ( pmods[ i ]->mod_bvalues != NULL ) {
180                 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
181                                 ;
182                 }
183                 }
184                 if (( pmods[ i ]->mod_bvalues =
185                         (struct berval **)ber_memrealloc( pmods[ i ]->mod_bvalues,
186                         (j + 2) * sizeof( struct berval * ))) == NULL ) {
187                         perror( "ber_realloc" );
188                         exit( EXIT_FAILURE );
189                 }
190                 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
191                 if (( bvp = (struct berval *)ber_memalloc( sizeof( struct berval )))
192                         == NULL ) {
193                         perror( "malloc" );
194                         exit( EXIT_FAILURE );
195                 }
196                 pmods[ i ]->mod_bvalues[ j ] = bvp;
197
198             bvp->bv_len = vlen;
199             if (( bvp->bv_val = (char *)malloc( vlen + 1 )) == NULL ) {
200                         perror( "malloc" );
201                         exit( EXIT_FAILURE );
202             }
203             AC_MEMCPY( bvp->bv_val, value, vlen );
204             bvp->bv_val[ vlen ] = '\0';
205     }
206 }
207
208
209 static char *
210 get_add_entry( char *filename, LDAPMod ***mods )
211 {
212         FILE    *fp;
213         char    *entry = NULL;
214
215         if ( (fp = fopen( filename, "r" )) != NULL ) {
216                 char  line[BUFSIZ];
217
218                 if ( fgets( line, BUFSIZ, fp )) {
219                         char *nl;
220
221                         if (( nl = strchr( line, '\r' )) || ( nl = strchr( line, '\n' )))
222                                 *nl = '\0';
223                         entry = strdup( line );
224
225                 }
226
227                 while ( fgets( line, BUFSIZ, fp )) {
228                         char    *nl;
229                         char    *value;
230
231                         if (( nl = strchr( line, '\r' )) || ( nl = strchr( line, '\n' )))
232                                 *nl = '\0';
233
234                         if ( *line == '\0' ) break;
235                         if ( !( value = strchr( line, ':' ))) break;
236
237                         *value++ = '\0'; 
238                         while ( *value && isspace( (unsigned char) *value ))
239                                 value++;
240
241                         addmodifyop( mods, LDAP_MOD_ADD, line, value, strlen( value ));
242
243                 }
244                 fclose( fp );
245         }
246
247         return( entry );
248 }
249
250
251 static void
252 do_addel(
253         char *uri,
254         char *host,
255         int port,
256         char *manager,
257         char *passwd,
258         char *entry,
259         LDAPMod **attrs,
260         int maxloop,
261         int maxretries
262 )
263 {
264         LDAP    *ld = NULL;
265         int     i = 0, do_retry = maxretries;
266         pid_t   pid = getpid();
267         int     rc = LDAP_SUCCESS;
268
269 retry:;
270         if ( uri ) {
271                 ldap_initialize( &ld, uri );
272         } else {
273                 ld = ldap_init( host, port );
274         }
275         if ( ld == NULL ) {
276                 perror( "ldap_init" );
277                 exit( EXIT_FAILURE );
278         }
279
280         {
281                 int version = LDAP_VERSION3;
282                 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
283                         &version ); 
284         }
285
286         if ( do_retry == maxretries ) {
287                 fprintf( stderr, "PID=%ld - Add/Delete(%d): entry=\"%s\".\n",
288                         (long) pid, maxloop, entry );
289         }
290
291         rc = ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE );
292         if ( rc != LDAP_SUCCESS ) {
293                 ldap_perror( ld, "ldap_bind" );
294                 if ( rc == LDAP_BUSY && do_retry > 0 ) {
295                         do_retry--;
296                         goto retry;
297                 }
298                 exit( EXIT_FAILURE );
299         }
300
301         for ( ; i < maxloop; i++ ) {
302
303                 /* add the entry */
304                 rc = ldap_add_s( ld, entry, attrs );
305                 if ( rc != LDAP_SUCCESS ) {
306                         ldap_perror( ld, "ldap_add" );
307                         if ( rc == LDAP_BUSY && do_retry > 0 ) {
308                                 do_retry--;
309                                 goto retry;
310                         }
311                         break;
312
313                 }
314
315 #if 0
316                 /* wait a second for the add to really complete */
317                 /* This masks some race conditions though. */
318                 sleep( 1 );
319 #endif
320
321                 /* now delete the entry again */
322                 rc = ldap_delete_s( ld, entry );
323                 if ( rc != LDAP_SUCCESS ) {
324                         ldap_perror( ld, "ldap_delete" );
325                         if ( rc == LDAP_BUSY && do_retry > 0 ) {
326                                 do_retry--;
327                                 goto retry;
328                         }
329                         break;
330                 }
331         }
332
333         fprintf( stderr, " PID=%ld - Add/Delete done (%d).\n", (long) pid, rc );
334
335         ldap_unbind( ld );
336 }
337
338