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