2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2005 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
40 get_add_entry( char *filename, LDAPMod ***mods );
43 do_addel( char *uri, char *host, int port, char *manager, char *passwd,
44 char *dn, LDAPMod **attrs, int maxloop, int maxretries );
49 fprintf( stderr, "usage: %s [-h <host>] -p port -D <managerDN> -w <passwd> -f <addfile> [-l <loops>]\n",
55 main( int argc, char **argv )
58 char *host = "localhost";
63 char *filename = NULL;
66 int retries = RETRIES;
67 LDAPMod **attrs = NULL;
69 while ( (i = getopt( argc, argv, "H:h:p:D:w:f:l:r:" )) != EOF ) {
71 case 'H': /* the server's URI */
72 uri = strdup( optarg );
75 case 'h': /* the servers host */
76 host = strdup( optarg );
79 case 'p': /* the servers port */
80 port = atoi( optarg );
83 case 'D': /* the servers manager */
84 manager = strdup( optarg );
87 case 'w': /* the server managers password */
88 passwd = strdup( optarg );
91 case 'f': /* file with entry search request */
92 filename = strdup( optarg );
95 case 'l': /* the number of loops */
96 loops = atoi( optarg );
100 retries = atoi( optarg );
109 if (( filename == NULL ) || ( port == -1 && uri == NULL ) ||
110 ( manager == NULL ) || ( passwd == NULL ))
113 entry = get_add_entry( filename, &attrs );
114 if (( entry == NULL ) || ( *entry == '\0' )) {
116 fprintf( stderr, "%s: invalid entry DN in file \"%s\".\n",
118 exit( EXIT_FAILURE );
122 if (( attrs == NULL ) || ( *attrs == '\0' )) {
124 fprintf( stderr, "%s: invalid attrs in file \"%s\".\n",
126 exit( EXIT_FAILURE );
130 do_addel( uri, host, port, manager, passwd, entry, attrs,
133 exit( EXIT_SUCCESS );
138 addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
145 modop |= LDAP_MOD_BVALUES;
148 if ( pmods != NULL ) {
149 for ( ; pmods[ i ] != NULL; ++i ) {
150 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
151 pmods[ i ]->mod_op == modop ) {
157 if ( pmods == NULL || pmods[ i ] == NULL ) {
158 if (( pmods = (LDAPMod **)realloc( pmods, (i + 2) *
159 sizeof( LDAPMod * ))) == NULL ) {
161 exit( EXIT_FAILURE );
164 pmods[ i + 1 ] = NULL;
165 if (( pmods[ i ] = (LDAPMod *)calloc( 1, sizeof( LDAPMod )))
168 exit( EXIT_FAILURE );
170 pmods[ i ]->mod_op = modop;
171 if (( pmods[ i ]->mod_type = strdup( attr )) == NULL ) {
173 exit( EXIT_FAILURE );
177 if ( value != NULL ) {
179 if ( pmods[ i ]->mod_bvalues != NULL ) {
180 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
184 if (( pmods[ i ]->mod_bvalues =
185 (struct berval **)ber_memrealloc( pmods[ i ]->mod_bvalues,
186 (j + 2) * sizeof( struct berval * ))) == NULL ) {
187 perror( "ber_realloc" );
188 exit( EXIT_FAILURE );
190 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
191 if (( bvp = (struct berval *)ber_memalloc( sizeof( struct berval )))
194 exit( EXIT_FAILURE );
196 pmods[ i ]->mod_bvalues[ j ] = bvp;
199 if (( bvp->bv_val = (char *)malloc( vlen + 1 )) == NULL ) {
201 exit( EXIT_FAILURE );
203 AC_MEMCPY( bvp->bv_val, value, vlen );
204 bvp->bv_val[ vlen ] = '\0';
210 get_add_entry( char *filename, LDAPMod ***mods )
215 if ( (fp = fopen( filename, "r" )) != NULL ) {
218 if ( fgets( line, BUFSIZ, fp )) {
221 if (( nl = strchr( line, '\r' )) || ( nl = strchr( line, '\n' )))
223 entry = strdup( line );
227 while ( fgets( line, BUFSIZ, fp )) {
231 if (( nl = strchr( line, '\r' )) || ( nl = strchr( line, '\n' )))
234 if ( *line == '\0' ) break;
235 if ( !( value = strchr( line, ':' ))) break;
238 while ( *value && isspace( (unsigned char) *value ))
241 addmodifyop( mods, LDAP_MOD_ADD, line, value, strlen( value ));
265 int i = 0, do_retry = maxretries;
266 pid_t pid = getpid();
267 int rc = LDAP_SUCCESS;
271 ldap_initialize( &ld, uri );
273 ld = ldap_init( host, port );
276 perror( "ldap_init" );
277 exit( EXIT_FAILURE );
281 int version = LDAP_VERSION3;
282 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
286 if ( do_retry == maxretries ) {
287 fprintf( stderr, "PID=%ld - Add/Delete(%d): entry=\"%s\".\n",
288 (long) pid, maxloop, entry );
291 rc = ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE );
292 if ( rc != LDAP_SUCCESS ) {
293 ldap_perror( ld, "ldap_bind" );
294 if ( rc == LDAP_BUSY && do_retry > 0 ) {
298 exit( EXIT_FAILURE );
301 for ( ; i < maxloop; i++ ) {
304 rc = ldap_add_s( ld, entry, attrs );
305 if ( rc != LDAP_SUCCESS ) {
306 ldap_perror( ld, "ldap_add" );
307 if ( rc == LDAP_BUSY && do_retry > 0 ) {
316 /* wait a second for the add to really complete */
317 /* This masks some race conditions though. */
321 /* now delete the entry again */
322 rc = ldap_delete_s( ld, entry );
323 if ( rc != LDAP_SUCCESS ) {
324 ldap_perror( ld, "ldap_delete" );
325 if ( rc == LDAP_BUSY && do_retry > 0 ) {
333 fprintf( stderr, " PID=%ld - Add/Delete done (%d).\n", (long) pid, rc );