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