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