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