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>
33 #define LDAP_DEPRECATED 1
39 get_add_entry( char *filename, LDAPMod ***mods );
42 do_addel( char *uri, char *host, int port, char *manager, char *passwd,
43 char *dn, LDAPMod **attrs, int maxloop );
48 fprintf( stderr, "usage: %s [-h <host>] -p port -D <managerDN> -w <passwd> -f <addfile> [-l <loops>]\n",
54 main( int argc, char **argv )
57 char *host = "localhost";
62 char *filename = NULL;
65 LDAPMod **attrs = NULL;
67 while ( (i = getopt( argc, argv, "H:h:p:D:w:f:l:" )) != EOF ) {
69 case 'H': /* the server's URI */
70 uri = strdup( optarg );
72 case 'h': /* the servers host */
73 host = strdup( optarg );
76 case 'p': /* the servers port */
77 port = atoi( optarg );
80 case 'D': /* the servers manager */
81 manager = strdup( optarg );
84 case 'w': /* the server managers password */
85 passwd = strdup( optarg );
88 case 'f': /* file with entry search request */
89 filename = strdup( optarg );
92 case 'l': /* the number of loops */
93 loops = atoi( optarg );
102 if (( filename == NULL ) || ( port == -1 && uri == NULL ) ||
103 ( manager == NULL ) || ( passwd == NULL ))
106 entry = get_add_entry( filename, &attrs );
107 if (( entry == NULL ) || ( *entry == '\0' )) {
109 fprintf( stderr, "%s: invalid entry DN in file \"%s\".\n",
111 exit( EXIT_FAILURE );
115 if (( attrs == NULL ) || ( *attrs == '\0' )) {
117 fprintf( stderr, "%s: invalid attrs in file \"%s\".\n",
119 exit( EXIT_FAILURE );
123 do_addel( uri, host, port, manager, passwd, entry, attrs, loops );
125 exit( EXIT_SUCCESS );
130 addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
137 modop |= LDAP_MOD_BVALUES;
140 if ( pmods != NULL ) {
141 for ( ; pmods[ i ] != NULL; ++i ) {
142 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
143 pmods[ i ]->mod_op == modop ) {
149 if ( pmods == NULL || pmods[ i ] == NULL ) {
150 if (( pmods = (LDAPMod **)realloc( pmods, (i + 2) *
151 sizeof( LDAPMod * ))) == NULL ) {
153 exit( EXIT_FAILURE );
156 pmods[ i + 1 ] = NULL;
157 if (( pmods[ i ] = (LDAPMod *)calloc( 1, sizeof( LDAPMod )))
160 exit( EXIT_FAILURE );
162 pmods[ i ]->mod_op = modop;
163 if (( pmods[ i ]->mod_type = strdup( attr )) == NULL ) {
165 exit( EXIT_FAILURE );
169 if ( value != NULL ) {
171 if ( pmods[ i ]->mod_bvalues != NULL ) {
172 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
176 if (( pmods[ i ]->mod_bvalues =
177 (struct berval **)ber_memrealloc( pmods[ i ]->mod_bvalues,
178 (j + 2) * sizeof( struct berval * ))) == NULL ) {
179 perror( "ber_realloc" );
180 exit( EXIT_FAILURE );
182 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
183 if (( bvp = (struct berval *)ber_memalloc( sizeof( struct berval )))
186 exit( EXIT_FAILURE );
188 pmods[ i ]->mod_bvalues[ j ] = bvp;
191 if (( bvp->bv_val = (char *)malloc( vlen + 1 )) == NULL ) {
193 exit( EXIT_FAILURE );
195 AC_MEMCPY( bvp->bv_val, value, vlen );
196 bvp->bv_val[ vlen ] = '\0';
202 get_add_entry( char *filename, LDAPMod ***mods )
207 if ( (fp = fopen( filename, "r" )) != NULL ) {
210 if ( fgets( line, BUFSIZ, fp )) {
213 if (( nl = strchr( line, '\r' )) || ( nl = strchr( line, '\n' )))
215 entry = strdup( line );
219 while ( fgets( line, BUFSIZ, fp )) {
223 if (( nl = strchr( line, '\r' )) || ( nl = strchr( line, '\n' )))
226 if ( *line == '\0' ) break;
227 if ( !( value = strchr( line, ':' ))) break;
230 while ( *value && isspace( (unsigned char) *value ))
233 addmodifyop( mods, LDAP_MOD_ADD, line, value, strlen( value ));
257 pid_t pid = getpid();
260 ldap_initialize( &ld, uri );
262 ld = ldap_init( host, port );
265 perror( "ldap_init" );
266 exit( EXIT_FAILURE );
270 int version = LDAP_VERSION3;
271 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
275 if ( ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE )
277 ldap_perror( ld, "ldap_bind" );
278 exit( EXIT_FAILURE );
282 fprintf( stderr, "PID=%ld - Add/Delete(%d): entry=\"%s\".\n",
283 (long) pid, maxloop, entry );
285 for ( i = 0; i < maxloop; i++ ) {
288 if ( ldap_add_s( ld, entry, attrs ) != LDAP_SUCCESS ) {
290 ldap_perror( ld, "ldap_add" );
295 /* wait a second for the add to really complete */
298 /* now delete the entry again */
299 if ( ldap_delete_s( ld, entry ) != LDAP_SUCCESS ) {
301 ldap_perror( ld, "ldap_delete" );
308 fprintf( stderr, " PID=%ld - Add/Delete done.\n", (long) pid );