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