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, int delay,
52 "-H <uri> | ([-h <host>] -p <port>) "
64 main( int argc, char **argv )
67 char *host = "localhost";
72 char *filename = NULL;
75 int retries = RETRIES;
78 LDAPMod **attrs = NULL;
80 while ( (i = getopt( argc, argv, "FH:h:p:D:w:f:l:r:t:" )) != EOF ) {
86 case 'H': /* the server's URI */
87 uri = strdup( optarg );
90 case 'h': /* the servers host */
91 host = strdup( optarg );
94 case 'p': /* the servers port */
95 port = atoi( optarg );
98 case 'D': /* the servers manager */
99 manager = strdup( optarg );
102 case 'w': /* the server managers password */
103 passwd = strdup( optarg );
106 case 'f': /* file with entry search request */
107 filename = strdup( optarg );
110 case 'l': /* the number of loops */
111 loops = atoi( optarg );
114 case 'r': /* number of retries */
115 retries = atoi( optarg );
118 case 't': /* delay in seconds */
119 delay = atoi( optarg );
128 if (( filename == NULL ) || ( port == -1 && uri == NULL ) ||
129 ( manager == NULL ) || ( passwd == NULL ))
132 entry = get_add_entry( filename, &attrs );
133 if (( entry == NULL ) || ( *entry == '\0' )) {
135 fprintf( stderr, "%s: invalid entry DN in file \"%s\".\n",
137 exit( EXIT_FAILURE );
141 if (( attrs == NULL ) || ( *attrs == '\0' )) {
143 fprintf( stderr, "%s: invalid attrs in file \"%s\".\n",
145 exit( EXIT_FAILURE );
149 do_addel( uri, host, port, manager, passwd, entry, attrs,
150 loops, retries, delay, friendly );
152 exit( EXIT_SUCCESS );
157 addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
164 modop |= LDAP_MOD_BVALUES;
167 if ( pmods != NULL ) {
168 for ( ; pmods[ i ] != NULL; ++i ) {
169 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
170 pmods[ i ]->mod_op == modop ) {
176 if ( pmods == NULL || pmods[ i ] == NULL ) {
177 if (( pmods = (LDAPMod **)realloc( pmods, (i + 2) *
178 sizeof( LDAPMod * ))) == NULL ) {
180 exit( EXIT_FAILURE );
183 pmods[ i + 1 ] = NULL;
184 if (( pmods[ i ] = (LDAPMod *)calloc( 1, sizeof( LDAPMod )))
187 exit( EXIT_FAILURE );
189 pmods[ i ]->mod_op = modop;
190 if (( pmods[ i ]->mod_type = strdup( attr )) == NULL ) {
192 exit( EXIT_FAILURE );
196 if ( value != NULL ) {
198 if ( pmods[ i ]->mod_bvalues != NULL ) {
199 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
203 if (( pmods[ i ]->mod_bvalues =
204 (struct berval **)ber_memrealloc( pmods[ i ]->mod_bvalues,
205 (j + 2) * sizeof( struct berval * ))) == NULL ) {
206 perror( "ber_realloc" );
207 exit( EXIT_FAILURE );
209 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
210 if (( bvp = (struct berval *)ber_memalloc( sizeof( struct berval )))
213 exit( EXIT_FAILURE );
215 pmods[ i ]->mod_bvalues[ j ] = bvp;
218 if (( bvp->bv_val = (char *)malloc( vlen + 1 )) == NULL ) {
220 exit( EXIT_FAILURE );
222 AC_MEMCPY( bvp->bv_val, value, vlen );
223 bvp->bv_val[ vlen ] = '\0';
229 get_add_entry( char *filename, LDAPMod ***mods )
234 if ( (fp = fopen( filename, "r" )) != NULL ) {
237 if ( fgets( line, BUFSIZ, fp )) {
240 if (( nl = strchr( line, '\r' )) || ( nl = strchr( line, '\n' )))
242 entry = strdup( line );
246 while ( fgets( line, BUFSIZ, fp )) {
250 if (( nl = strchr( line, '\r' )) || ( nl = strchr( line, '\n' )))
253 if ( *line == '\0' ) break;
254 if ( !( value = strchr( line, ':' ))) break;
257 while ( *value && isspace( (unsigned char) *value ))
260 addmodifyop( mods, LDAP_MOD_ADD, line, value, strlen( value ));
286 int i = 0, do_retry = maxretries;
287 pid_t pid = getpid();
288 int rc = LDAP_SUCCESS;
292 ldap_initialize( &ld, uri );
294 ld = ldap_init( host, port );
297 perror( "ldap_init" );
298 exit( EXIT_FAILURE );
302 int version = LDAP_VERSION3;
303 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
307 if ( do_retry == maxretries ) {
308 fprintf( stderr, "PID=%ld - Add/Delete(%d): entry=\"%s\".\n",
309 (long) pid, maxloop, entry );
312 rc = ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE );
313 if ( rc != LDAP_SUCCESS ) {
314 ldap_perror( ld, "ldap_bind" );
317 case LDAP_UNAVAILABLE:
318 if ( do_retry > 0 ) {
329 exit( EXIT_FAILURE );
332 for ( ; i < maxloop; i++ ) {
335 rc = ldap_add_s( ld, entry, attrs );
336 if ( rc != LDAP_SUCCESS ) {
337 ldap_perror( ld, "ldap_add" );
339 case LDAP_ALREADY_EXISTS:
340 /* NOTE: this likely means
342 * during the previous round... */
349 case LDAP_UNAVAILABLE:
350 if ( do_retry > 0 ) {
362 /* wait a second for the add to really complete */
363 /* This masks some race conditions though. */
367 /* now delete the entry again */
368 rc = ldap_delete_s( ld, entry );
369 if ( rc != LDAP_SUCCESS ) {
370 ldap_perror( ld, "ldap_delete" );
372 case LDAP_NO_SUCH_OBJECT:
373 /* NOTE: this likely means
375 * during the previous round... */
382 case LDAP_UNAVAILABLE:
383 if ( do_retry > 0 ) {
396 fprintf( stderr, " PID=%ld - Add/Delete done (%d).\n", (long) pid, rc );