3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
10 #include <ac/stdlib.h>
14 #include <ac/socket.h>
15 #include <ac/string.h>
16 #include <ac/unistd.h>
24 get_add_entry( char *filename, LDAPMod ***mods );
27 do_addel( char *uri, char *host, int port, char *manager, char *passwd,
28 char *dn, LDAPMod **attrs, int maxloop );
33 fprintf( stderr, "usage: %s [-h <host>] -p port -D <managerDN> -w <passwd> -f <addfile> [-l <loops>]\n",
39 main( int argc, char **argv )
42 char *host = "localhost";
47 char *filename = NULL;
50 LDAPMod **attrs = NULL;
52 while ( (i = getopt( argc, argv, "H:h:p:D:w:f:l:" )) != EOF ) {
54 case 'H': /* the server's URI */
55 uri = strdup( optarg );
57 case 'h': /* the servers host */
58 host = strdup( optarg );
61 case 'p': /* the servers port */
62 port = atoi( optarg );
65 case 'D': /* the servers manager */
66 manager = strdup( optarg );
69 case 'w': /* the server managers password */
70 passwd = strdup( optarg );
73 case 'f': /* file with entry search request */
74 filename = strdup( optarg );
77 case 'l': /* the number of loops */
78 loops = atoi( optarg );
87 if (( filename == NULL ) || ( port == -1 && uri == NULL ) ||
88 ( manager == NULL ) || ( passwd == NULL ))
91 entry = get_add_entry( filename, &attrs );
92 if (( entry == NULL ) || ( *entry == '\0' )) {
94 fprintf( stderr, "%s: invalid entry DN in file \"%s\".\n",
100 if (( attrs == NULL ) || ( *attrs == '\0' )) {
102 fprintf( stderr, "%s: invalid attrs in file \"%s\".\n",
104 exit( EXIT_FAILURE );
108 do_addel( uri, host, port, manager, passwd, entry, attrs, loops );
110 exit( EXIT_SUCCESS );
115 addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
122 modop |= LDAP_MOD_BVALUES;
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 ) {
134 if ( pmods == NULL || pmods[ i ] == NULL ) {
135 if (( pmods = (LDAPMod **)realloc( pmods, (i + 2) *
136 sizeof( LDAPMod * ))) == NULL ) {
138 exit( EXIT_FAILURE );
141 pmods[ i + 1 ] = NULL;
142 if (( pmods[ i ] = (LDAPMod *)calloc( 1, sizeof( LDAPMod )))
145 exit( EXIT_FAILURE );
147 pmods[ i ]->mod_op = modop;
148 if (( pmods[ i ]->mod_type = strdup( attr )) == NULL ) {
150 exit( EXIT_FAILURE );
154 if ( value != NULL ) {
156 if ( pmods[ i ]->mod_bvalues != NULL ) {
157 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
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 );
167 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
168 if (( bvp = (struct berval *)ber_memalloc( sizeof( struct berval )))
171 exit( EXIT_FAILURE );
173 pmods[ i ]->mod_bvalues[ j ] = bvp;
176 if (( bvp->bv_val = (char *)malloc( vlen + 1 )) == NULL ) {
178 exit( EXIT_FAILURE );
180 AC_MEMCPY( bvp->bv_val, value, vlen );
181 bvp->bv_val[ vlen ] = '\0';
187 get_add_entry( char *filename, LDAPMod ***mods )
192 if ( (fp = fopen( filename, "r" )) != NULL ) {
195 if ( fgets( line, BUFSIZ, fp )) {
198 if (( nl = strchr( line, '\r' )) || ( nl = strchr( line, '\n' )))
200 entry = strdup( line );
204 while ( fgets( line, BUFSIZ, fp )) {
208 if (( nl = strchr( line, '\r' )) || ( nl = strchr( line, '\n' )))
211 if ( *line == '\0' ) break;
212 if ( !( value = strchr( line, ':' ))) break;
215 while ( *value && isspace( (unsigned char) *value ))
218 addmodifyop( mods, LDAP_MOD_ADD, line, value, strlen( value ));
242 pid_t pid = getpid();
245 ldap_initialize( &ld, uri );
247 ld = ldap_init( host, port );
250 perror( "ldap_init" );
251 exit( EXIT_FAILURE );
255 int version = LDAP_VERSION3;
256 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
260 if ( ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE )
262 ldap_perror( ld, "ldap_bind" );
263 exit( EXIT_FAILURE );
267 fprintf( stderr, "PID=%ld - Add/Delete(%d): entry=\"%s\".\n",
268 (long) pid, maxloop, entry );
270 for ( i = 0; i < maxloop; i++ ) {
273 if ( ldap_add_s( ld, entry, attrs ) != LDAP_SUCCESS ) {
275 ldap_perror( ld, "ldap_add" );
280 /* wait a second for the add to really complete */
283 /* now delete the entry again */
284 if ( ldap_delete_s( ld, entry ) != LDAP_SUCCESS ) {
286 ldap_perror( ld, "ldap_delete" );
293 fprintf( stderr, " PID=%ld - Add/Delete done.\n", (long) pid );