3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
10 #include <ac/stdlib.h>
13 #include <ac/socket.h>
14 #include <ac/string.h>
15 #include <ac/unistd.h>
18 #ifdef HAVE_SYS_PARAM_H
19 #include <sys/param.h>
27 get_add_entry( char *filename, LDAPMod ***mods );
30 do_addel( char *host, int port, char *manager, char *passwd,
31 char *dn, LDAPMod **attrs, int maxloop );
36 fprintf( stderr, "usage: %s [-h <host>] -p port -D <managerDN> -w <passwd> -f <addfile> [-l <loops>]\n",
42 main( int argc, char **argv )
45 char *host = "localhost";
49 char *filename = NULL;
52 LDAPMod **attrs = NULL;
54 while ( (i = getopt( argc, argv, "h:p:D:w:f:l:" )) != EOF ) {
56 case 'h': /* the servers host */
57 host = strdup( optarg );
60 case 'p': /* the servers port */
61 port = atoi( optarg );
64 case 'D': /* the servers manager */
65 manager = strdup( optarg );
68 case 'w': /* the server managers password */
69 passwd = strdup( optarg );
72 case 'f': /* file with entry search request */
73 filename = strdup( optarg );
76 case 'l': /* the number of loops */
77 loops = atoi( optarg );
86 if (( filename == NULL ) || ( port == -1 ) ||
87 ( manager == NULL ) || ( passwd == NULL ))
90 entry = get_add_entry( filename, &attrs );
91 if (( entry == NULL ) || ( *entry == '\0' )) {
93 fprintf( stderr, "%s: invalid entry DN in file \"%s\".\n",
99 if (( attrs == NULL ) || ( *attrs == '\0' )) {
101 fprintf( stderr, "%s: invalid attrs in file \"%s\".\n",
103 exit( EXIT_FAILURE );
107 do_addel( host, port, manager, passwd, entry, attrs, loops );
109 exit( EXIT_SUCCESS );
114 addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
121 modop |= LDAP_MOD_BVALUES;
124 if ( pmods != NULL ) {
125 for ( ; pmods[ i ] != NULL; ++i ) {
126 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
127 pmods[ i ]->mod_op == modop ) {
133 if ( pmods == NULL || pmods[ i ] == NULL ) {
134 if (( pmods = (LDAPMod **)realloc( pmods, (i + 2) *
135 sizeof( LDAPMod * ))) == NULL ) {
137 exit( EXIT_FAILURE );
140 pmods[ i + 1 ] = NULL;
141 if (( pmods[ i ] = (LDAPMod *)calloc( 1, sizeof( LDAPMod )))
144 exit( EXIT_FAILURE );
146 pmods[ i ]->mod_op = modop;
147 if (( pmods[ i ]->mod_type = strdup( attr )) == NULL ) {
149 exit( EXIT_FAILURE );
153 if ( value != NULL ) {
155 if ( pmods[ i ]->mod_bvalues != NULL ) {
156 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
160 if (( pmods[ i ]->mod_bvalues =
161 (struct berval **)ber_memrealloc( pmods[ i ]->mod_bvalues,
162 (j + 2) * sizeof( struct berval * ))) == NULL ) {
163 perror( "ber_realloc" );
164 exit( EXIT_FAILURE );
166 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
167 if (( bvp = (struct berval *)ber_memalloc( sizeof( struct berval )))
170 exit( EXIT_FAILURE );
172 pmods[ i ]->mod_bvalues[ j ] = bvp;
175 if (( bvp->bv_val = (char *)malloc( vlen + 1 )) == NULL ) {
177 exit( EXIT_FAILURE );
179 SAFEMEMCPY( bvp->bv_val, value, vlen );
180 bvp->bv_val[ vlen ] = '\0';
186 get_add_entry( char *filename, LDAPMod ***mods )
191 if ( (fp = fopen( filename, "r" )) != NULL ) {
194 if ( fgets( line, BUFSIZ, fp )) {
197 if (( nl = strchr( line, '\r' )) || ( nl = strchr( line, '\n' )))
199 entry = strdup( line );
203 while ( fgets( line, BUFSIZ, fp )) {
207 if (( nl = strchr( line, '\r' )) || ( nl = strchr( line, '\n' )))
210 if ( *line == '\0' ) break;
211 if ( !( value = strchr( line, ':' ))) break;
214 while ( *value && isspace( (unsigned char) *value ))
217 addmodifyop( mods, LDAP_MOD_ADD, line, value, strlen( value ));
240 pid_t pid = getpid();
242 if (( ld = ldap_init( host, port )) == NULL ) {
243 perror( "ldap_init" );
244 exit( EXIT_FAILURE );
247 if ( ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE )
249 ldap_perror( ld, "ldap_bind" );
250 exit( EXIT_FAILURE );
254 fprintf( stderr, "PID=%ld - Add/Delete(%d): entry=\"%s\".\n",
255 (long) pid, maxloop, entry );
257 for ( i = 0; i < maxloop; i++ ) {
260 if ( ldap_add_s( ld, entry, attrs ) != LDAP_SUCCESS ) {
262 ldap_perror( ld, "ldap_add" );
267 /* wait a second for the add to really complete */
270 /* now delete the entry again */
271 if ( ldap_delete_s( ld, entry ) != LDAP_SUCCESS ) {
273 ldap_perror( ld, "ldap_delete" );
280 fprintf( stderr, " PID=%ld - Add/Delete done.\n", (long) pid );