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