]> git.sur5r.net Git - openldap/blob - tests/progs/slapd-addel.c
74c9f89a5a74d3ffabf64f74156aea8fdbed5036
[openldap] / tests / progs / slapd-addel.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2017 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 #include "lutil.h"
35
36 #include "slapd-common.h"
37
38 static char *
39 get_add_entry( char *filename, LDAPMod ***mods );
40
41 static void
42 do_addel( struct tester_conn_args *config,
43         char *dn, LDAPMod **attrs, int friendly );
44
45 static void
46 usage( char *name, char opt )
47 {
48         if ( opt ) {
49                 fprintf( stderr, "%s: unable to handle option \'%c\'\n\n",
50                         name, opt );
51         }
52
53         fprintf( stderr, "usage: %s " TESTER_COMMON_HELP
54                 "-f <addfile> "
55                 "[-F]\n",
56                 name );
57         exit( EXIT_FAILURE );
58 }
59
60 int
61 main( int argc, char **argv )
62 {
63         int             i;
64         char            *filename = NULL;
65         char            *entry = NULL;
66         int             friendly = 0;
67         LDAPMod         **attrs = NULL;
68         struct tester_conn_args *config;
69
70         config = tester_init( "slapd-addel", TESTER_ADDEL );
71
72         while ( ( i = getopt( argc, argv, TESTER_COMMON_OPTS "Ff:" ) ) != EOF )
73         {
74                 switch ( i ) {
75                 case 'F':
76                         friendly++;
77                         break;
78                         
79                 case 'i':
80                         /* ignored (!) by now */
81                         break;
82
83                 case 'f':               /* file with entry search request */
84                         filename = strdup( optarg );
85                         break;
86
87                 default:
88                         if ( tester_config_opt( config, i, optarg ) == LDAP_SUCCESS ) {
89                                 break;
90                         }
91                         usage( argv[0], i );
92                         break;
93                 }
94         }
95
96         if ( filename == NULL )
97                 usage( argv[0], 0 );
98
99         entry = get_add_entry( filename, &attrs );
100         if (( entry == NULL ) || ( *entry == '\0' )) {
101
102                 fprintf( stderr, "%s: invalid entry DN in file \"%s\".\n",
103                                 argv[0], filename );
104                 exit( EXIT_FAILURE );
105
106         }
107
108         if (( attrs == NULL ) || ( *attrs == '\0' )) {
109
110                 fprintf( stderr, "%s: invalid attrs in file \"%s\".\n",
111                                 argv[0], filename );
112                 exit( EXIT_FAILURE );
113
114         }
115
116         tester_config_finish( config );
117
118         for ( i = 0; i < config->outerloops; i++ ) {
119                 do_addel( config, entry, attrs, friendly );
120         }
121
122         exit( EXIT_SUCCESS );
123 }
124
125
126 static void
127 addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
128 {
129     LDAPMod             **pmods;
130     int                 i, j;
131     struct berval       *bvp;
132
133     pmods = *pmodsp;
134     modop |= LDAP_MOD_BVALUES;
135
136     i = 0;
137     if ( pmods != NULL ) {
138                 for ( ; pmods[ i ] != NULL; ++i ) {
139                 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
140                         pmods[ i ]->mod_op == modop ) {
141                                 break;
142                 }
143                 }
144     }
145
146     if ( pmods == NULL || pmods[ i ] == NULL ) {
147                 if (( pmods = (LDAPMod **)realloc( pmods, (i + 2) *
148                         sizeof( LDAPMod * ))) == NULL ) {
149                         tester_perror( "realloc", NULL );
150                         exit( EXIT_FAILURE );
151                 }
152                 *pmodsp = pmods;
153                 pmods[ i + 1 ] = NULL;
154                 if (( pmods[ i ] = (LDAPMod *)calloc( 1, sizeof( LDAPMod )))
155                         == NULL ) {
156                         tester_perror( "calloc", NULL );
157                         exit( EXIT_FAILURE );
158                 }
159                 pmods[ i ]->mod_op = modop;
160                 if (( pmods[ i ]->mod_type = strdup( attr )) == NULL ) {
161                 tester_perror( "strdup", NULL );
162                 exit( EXIT_FAILURE );
163                 }
164     }
165
166     if ( value != NULL ) {
167                 j = 0;
168                 if ( pmods[ i ]->mod_bvalues != NULL ) {
169                 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
170                                 ;
171                 }
172                 }
173                 if (( pmods[ i ]->mod_bvalues =
174                         (struct berval **)ber_memrealloc( pmods[ i ]->mod_bvalues,
175                         (j + 2) * sizeof( struct berval * ))) == NULL ) {
176                         tester_perror( "ber_memrealloc", NULL );
177                         exit( EXIT_FAILURE );
178                 }
179                 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
180                 if (( bvp = (struct berval *)ber_memalloc( sizeof( struct berval )))
181                         == NULL ) {
182                         tester_perror( "ber_memalloc", NULL );
183                         exit( EXIT_FAILURE );
184                 }
185                 pmods[ i ]->mod_bvalues[ j ] = bvp;
186
187             bvp->bv_len = vlen;
188             if (( bvp->bv_val = (char *)malloc( vlen + 1 )) == NULL ) {
189                         tester_perror( "malloc", NULL );
190                         exit( EXIT_FAILURE );
191             }
192             AC_MEMCPY( bvp->bv_val, value, vlen );
193             bvp->bv_val[ vlen ] = '\0';
194     }
195 }
196
197
198 static char *
199 get_add_entry( char *filename, LDAPMod ***mods )
200 {
201         FILE    *fp;
202         char    *entry = NULL;
203
204         if ( (fp = fopen( filename, "r" )) != NULL ) {
205                 char  line[BUFSIZ];
206
207                 if ( fgets( line, BUFSIZ, fp )) {
208                         char *nl;
209
210                         if (( nl = strchr( line, '\r' )) || ( nl = strchr( line, '\n' )))
211                                 *nl = '\0';
212                         nl = line;
213                         if ( !strncasecmp( nl, "dn: ", 4 ))
214                                 nl += 4;
215                         entry = strdup( nl );
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         struct tester_conn_args *config,
246         char *entry,
247         LDAPMod **attrs,
248         int friendly )
249 {
250         LDAP    *ld = NULL;
251         int     i = 0, do_retry = config->retries;
252         int     rc = LDAP_SUCCESS;
253
254 retry:;
255         if ( ld == NULL ) {
256                 tester_init_ld( &ld, config, 0 );
257         }
258
259         if ( do_retry == config->retries ) {
260                 fprintf( stderr, "PID=%ld - Add/Delete(%d): entry=\"%s\".\n",
261                         (long) pid, config->loops, entry );
262         }
263
264         for ( ; i < config->loops; i++ ) {
265
266                 /* add the entry */
267                 rc = ldap_add_ext_s( ld, entry, attrs, NULL, NULL );
268                 if ( rc != LDAP_SUCCESS ) {
269                         tester_ldap_error( ld, "ldap_add_ext_s", NULL );
270                         switch ( rc ) {
271                         case LDAP_ALREADY_EXISTS:
272                                 /* NOTE: this likely means
273                                  * the delete failed
274                                  * during the previous round... */
275                                 if ( !friendly ) {
276                                         goto done;
277                                 }
278                                 break;
279
280                         case LDAP_BUSY:
281                         case LDAP_UNAVAILABLE:
282                                 if ( do_retry > 0 ) {
283                                         do_retry--;
284                                         goto retry;
285                                 }
286                                 /* fall thru */
287
288                         default:
289                                 goto done;
290                         }
291                 }
292
293 #if 0
294                 /* wait a second for the add to really complete */
295                 /* This masks some race conditions though. */
296                 sleep( 1 );
297 #endif
298
299                 /* now delete the entry again */
300                 rc = ldap_delete_ext_s( ld, entry, NULL, NULL );
301                 if ( rc != LDAP_SUCCESS ) {
302                         tester_ldap_error( ld, "ldap_delete_ext_s", NULL );
303                         switch ( rc ) {
304                         case LDAP_NO_SUCH_OBJECT:
305                                 /* NOTE: this likely means
306                                  * the add failed
307                                  * during the previous round... */
308                                 if ( !friendly ) {
309                                         goto done;
310                                 }
311                                 break;
312
313                         case LDAP_BUSY:
314                         case LDAP_UNAVAILABLE:
315                                 if ( do_retry > 0 ) {
316                                         do_retry--;
317                                         goto retry;
318                                 }
319                                 /* fall thru */
320
321                         default:
322                                 goto done;
323                         }
324                 }
325         }
326
327 done:;
328         fprintf( stderr, "  PID=%ld - Add/Delete done (%d).\n", (long) pid, rc );
329
330         ldap_unbind_ext( ld, NULL, NULL );
331 }
332
333