2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2003 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
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>.
16 * This work was initially developed by Kurt Spanier for inclusion
17 * in OpenLDAP Software.
24 #include <ac/stdlib.h>
28 #include <ac/socket.h>
29 #include <ac/string.h>
30 #include <ac/unistd.h>
38 get_add_entry( char *filename, LDAPMod ***mods );
41 do_addel( char *uri, char *host, int port, char *manager, char *passwd,
42 char *dn, LDAPMod **attrs, int maxloop );
47 fprintf( stderr, "usage: %s [-h <host>] -p port -D <managerDN> -w <passwd> -f <addfile> [-l <loops>]\n",
53 main( int argc, char **argv )
56 char *host = "localhost";
61 char *filename = NULL;
64 LDAPMod **attrs = NULL;
66 while ( (i = getopt( argc, argv, "H:h:p:D:w:f:l:" )) != EOF ) {
68 case 'H': /* the server's URI */
69 uri = strdup( optarg );
71 case 'h': /* the servers host */
72 host = strdup( optarg );
75 case 'p': /* the servers port */
76 port = atoi( optarg );
79 case 'D': /* the servers manager */
80 manager = strdup( optarg );
83 case 'w': /* the server managers password */
84 passwd = strdup( optarg );
87 case 'f': /* file with entry search request */
88 filename = strdup( optarg );
91 case 'l': /* the number of loops */
92 loops = atoi( optarg );
101 if (( filename == NULL ) || ( port == -1 && uri == NULL ) ||
102 ( manager == NULL ) || ( passwd == NULL ))
105 entry = get_add_entry( filename, &attrs );
106 if (( entry == NULL ) || ( *entry == '\0' )) {
108 fprintf( stderr, "%s: invalid entry DN in file \"%s\".\n",
110 exit( EXIT_FAILURE );
114 if (( attrs == NULL ) || ( *attrs == '\0' )) {
116 fprintf( stderr, "%s: invalid attrs in file \"%s\".\n",
118 exit( EXIT_FAILURE );
122 do_addel( uri, host, port, manager, passwd, entry, attrs, loops );
124 exit( EXIT_SUCCESS );
129 addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
136 modop |= LDAP_MOD_BVALUES;
139 if ( pmods != NULL ) {
140 for ( ; pmods[ i ] != NULL; ++i ) {
141 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
142 pmods[ i ]->mod_op == modop ) {
148 if ( pmods == NULL || pmods[ i ] == NULL ) {
149 if (( pmods = (LDAPMod **)realloc( pmods, (i + 2) *
150 sizeof( LDAPMod * ))) == NULL ) {
152 exit( EXIT_FAILURE );
155 pmods[ i + 1 ] = NULL;
156 if (( pmods[ i ] = (LDAPMod *)calloc( 1, sizeof( LDAPMod )))
159 exit( EXIT_FAILURE );
161 pmods[ i ]->mod_op = modop;
162 if (( pmods[ i ]->mod_type = strdup( attr )) == NULL ) {
164 exit( EXIT_FAILURE );
168 if ( value != NULL ) {
170 if ( pmods[ i ]->mod_bvalues != NULL ) {
171 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
175 if (( pmods[ i ]->mod_bvalues =
176 (struct berval **)ber_memrealloc( pmods[ i ]->mod_bvalues,
177 (j + 2) * sizeof( struct berval * ))) == NULL ) {
178 perror( "ber_realloc" );
179 exit( EXIT_FAILURE );
181 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
182 if (( bvp = (struct berval *)ber_memalloc( sizeof( struct berval )))
185 exit( EXIT_FAILURE );
187 pmods[ i ]->mod_bvalues[ j ] = bvp;
190 if (( bvp->bv_val = (char *)malloc( vlen + 1 )) == NULL ) {
192 exit( EXIT_FAILURE );
194 AC_MEMCPY( bvp->bv_val, value, vlen );
195 bvp->bv_val[ vlen ] = '\0';
201 get_add_entry( char *filename, LDAPMod ***mods )
206 if ( (fp = fopen( filename, "r" )) != NULL ) {
209 if ( fgets( line, BUFSIZ, fp )) {
212 if (( nl = strchr( line, '\r' )) || ( nl = strchr( line, '\n' )))
214 entry = strdup( line );
218 while ( fgets( line, BUFSIZ, fp )) {
222 if (( nl = strchr( line, '\r' )) || ( nl = strchr( line, '\n' )))
225 if ( *line == '\0' ) break;
226 if ( !( value = strchr( line, ':' ))) break;
229 while ( *value && isspace( (unsigned char) *value ))
232 addmodifyop( mods, LDAP_MOD_ADD, line, value, strlen( value ));
256 pid_t pid = getpid();
259 ldap_initialize( &ld, uri );
261 ld = ldap_init( host, port );
264 perror( "ldap_init" );
265 exit( EXIT_FAILURE );
269 int version = LDAP_VERSION3;
270 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
274 if ( ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE )
276 ldap_perror( ld, "ldap_bind" );
277 exit( EXIT_FAILURE );
281 fprintf( stderr, "PID=%ld - Add/Delete(%d): entry=\"%s\".\n",
282 (long) pid, maxloop, entry );
284 for ( i = 0; i < maxloop; i++ ) {
287 if ( ldap_add_s( ld, entry, attrs ) != LDAP_SUCCESS ) {
289 ldap_perror( ld, "ldap_add" );
294 /* wait a second for the add to really complete */
297 /* now delete the entry again */
298 if ( ldap_delete_s( ld, entry ) != LDAP_SUCCESS ) {
300 ldap_perror( ld, "ldap_delete" );
307 fprintf( stderr, " PID=%ld - Add/Delete done.\n", (long) pid );