3 * Copyright 1998-1999 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 *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";
46 char *filename = NULL;
49 LDAPMod **attrs = NULL;
51 while ( (i = getopt( argc, argv, "h:p:D:w:f:l:" )) != EOF ) {
53 case 'h': /* the servers host */
54 host = strdup( optarg );
57 case 'p': /* the servers port */
58 port = atoi( optarg );
61 case 'D': /* the servers manager */
62 manager = strdup( optarg );
65 case 'w': /* the server managers password */
66 passwd = strdup( optarg );
69 case 'f': /* file with entry search request */
70 filename = strdup( optarg );
73 case 'l': /* the number of loops */
74 loops = atoi( optarg );
83 if (( filename == NULL ) || ( port == -1 ) ||
84 ( manager == NULL ) || ( passwd == NULL ))
87 entry = get_add_entry( filename, &attrs );
88 if (( entry == NULL ) || ( *entry == '\0' )) {
90 fprintf( stderr, "%s: invalid entry DN in file \"%s\".\n",
96 if (( attrs == NULL ) || ( *attrs == '\0' )) {
98 fprintf( stderr, "%s: invalid attrs in file \"%s\".\n",
100 exit( EXIT_FAILURE );
104 do_addel( host, port, manager, passwd, entry, attrs, loops );
106 exit( EXIT_SUCCESS );
111 addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
118 modop |= LDAP_MOD_BVALUES;
121 if ( pmods != NULL ) {
122 for ( ; pmods[ i ] != NULL; ++i ) {
123 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
124 pmods[ i ]->mod_op == modop ) {
130 if ( pmods == NULL || pmods[ i ] == NULL ) {
131 if (( pmods = (LDAPMod **)realloc( pmods, (i + 2) *
132 sizeof( LDAPMod * ))) == NULL ) {
134 exit( EXIT_FAILURE );
137 pmods[ i + 1 ] = NULL;
138 if (( pmods[ i ] = (LDAPMod *)calloc( 1, sizeof( LDAPMod )))
141 exit( EXIT_FAILURE );
143 pmods[ i ]->mod_op = modop;
144 if (( pmods[ i ]->mod_type = strdup( attr )) == NULL ) {
146 exit( EXIT_FAILURE );
150 if ( value != NULL ) {
152 if ( pmods[ i ]->mod_bvalues != NULL ) {
153 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
157 if (( pmods[ i ]->mod_bvalues =
158 (struct berval **)ber_memrealloc( pmods[ i ]->mod_bvalues,
159 (j + 2) * sizeof( struct berval * ))) == NULL ) {
160 perror( "ber_realloc" );
161 exit( EXIT_FAILURE );
163 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
164 if (( bvp = (struct berval *)ber_memalloc( sizeof( struct berval )))
167 exit( EXIT_FAILURE );
169 pmods[ i ]->mod_bvalues[ j ] = bvp;
172 if (( bvp->bv_val = (char *)malloc( vlen + 1 )) == NULL ) {
174 exit( EXIT_FAILURE );
176 SAFEMEMCPY( bvp->bv_val, value, vlen );
177 bvp->bv_val[ vlen ] = '\0';
183 get_add_entry( char *filename, LDAPMod ***mods )
188 if ( (fp = fopen( filename, "r" )) != NULL ) {
191 if ( fgets( line, BUFSIZ, fp )) {
194 if (( nl = strchr( line, '\r' )) || ( nl = strchr( line, '\n' )))
196 entry = strdup( line );
200 while ( fgets( line, BUFSIZ, fp )) {
204 if (( nl = strchr( line, '\r' )) || ( nl = strchr( line, '\n' )))
207 if ( *line == '\0' ) break;
208 if ( !( value = strchr( line, ':' ))) break;
211 while ( *value && isspace( (unsigned char) *value ))
214 addmodifyop( mods, LDAP_MOD_ADD, line, value, strlen( value ));
237 pid_t pid = getpid();
239 if (( ld = ldap_init( host, port )) == NULL ) {
240 perror( "ldap_init" );
241 exit( EXIT_FAILURE );
244 if ( ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE )
246 ldap_perror( ld, "ldap_bind" );
247 exit( EXIT_FAILURE );
251 fprintf( stderr, "PID=%ld - Add/Delete(%d): entry=\"%s\".\n",
252 (long) pid, maxloop, entry );
254 for ( i = 0; i < maxloop; i++ ) {
257 if ( ldap_add_s( ld, entry, attrs ) != LDAP_SUCCESS ) {
259 ldap_perror( ld, "ldap_add" );
264 /* wait a second for the add to really complete */
267 /* now delete the entry again */
268 if ( ldap_delete_s( ld, entry ) != LDAP_SUCCESS ) {
270 ldap_perror( ld, "ldap_delete" );
277 fprintf( stderr, " PID=%ld - Add/Delete done.\n", (long) pid );