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>) "
65 main( int argc, char **argv )
68 char *host = "localhost";
73 char *filename = NULL;
76 int retries = RETRIES;
79 LDAPMod **attrs = NULL;
81 while ( (i = getopt( argc, argv, "FH:h:p:D:w:f:l:r:t:" )) != EOF ) {
87 case 'H': /* the server's URI */
88 uri = strdup( optarg );
91 case 'h': /* the servers host */
92 host = strdup( optarg );
95 case 'p': /* the servers port */
96 port = atoi( optarg );
99 case 'D': /* the servers manager */
100 manager = strdup( optarg );
103 case 'w': /* the server managers password */
104 passwd = strdup( optarg );
107 case 'f': /* file with entry search request */
108 filename = strdup( optarg );
111 case 'l': /* the number of loops */
112 loops = atoi( optarg );
115 case 'r': /* number of retries */
116 retries = atoi( optarg );
119 case 't': /* delay in seconds */
120 delay = atoi( optarg );
129 if (( filename == NULL ) || ( port == -1 && uri == NULL ) ||
130 ( manager == NULL ) || ( passwd == NULL ))
133 entry = get_add_entry( filename, &attrs );
134 if (( entry == NULL ) || ( *entry == '\0' )) {
136 fprintf( stderr, "%s: invalid entry DN in file \"%s\".\n",
138 exit( EXIT_FAILURE );
142 if (( attrs == NULL ) || ( *attrs == '\0' )) {
144 fprintf( stderr, "%s: invalid attrs in file \"%s\".\n",
146 exit( EXIT_FAILURE );
150 do_addel( uri, host, port, manager, passwd, entry, attrs,
151 loops, retries, delay, friendly );
153 exit( EXIT_SUCCESS );
158 addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
165 modop |= LDAP_MOD_BVALUES;
168 if ( pmods != NULL ) {
169 for ( ; pmods[ i ] != NULL; ++i ) {
170 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
171 pmods[ i ]->mod_op == modop ) {
177 if ( pmods == NULL || pmods[ i ] == NULL ) {
178 if (( pmods = (LDAPMod **)realloc( pmods, (i + 2) *
179 sizeof( LDAPMod * ))) == NULL ) {
181 exit( EXIT_FAILURE );
184 pmods[ i + 1 ] = NULL;
185 if (( pmods[ i ] = (LDAPMod *)calloc( 1, sizeof( LDAPMod )))
188 exit( EXIT_FAILURE );
190 pmods[ i ]->mod_op = modop;
191 if (( pmods[ i ]->mod_type = strdup( attr )) == NULL ) {
193 exit( EXIT_FAILURE );
197 if ( value != NULL ) {
199 if ( pmods[ i ]->mod_bvalues != NULL ) {
200 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
204 if (( pmods[ i ]->mod_bvalues =
205 (struct berval **)ber_memrealloc( pmods[ i ]->mod_bvalues,
206 (j + 2) * sizeof( struct berval * ))) == NULL ) {
207 perror( "ber_realloc" );
208 exit( EXIT_FAILURE );
210 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
211 if (( bvp = (struct berval *)ber_memalloc( sizeof( struct berval )))
214 exit( EXIT_FAILURE );
216 pmods[ i ]->mod_bvalues[ j ] = bvp;
219 if (( bvp->bv_val = (char *)malloc( vlen + 1 )) == NULL ) {
221 exit( EXIT_FAILURE );
223 AC_MEMCPY( bvp->bv_val, value, vlen );
224 bvp->bv_val[ vlen ] = '\0';
230 get_add_entry( char *filename, LDAPMod ***mods )
235 if ( (fp = fopen( filename, "r" )) != NULL ) {
238 if ( fgets( line, BUFSIZ, fp )) {
241 if (( nl = strchr( line, '\r' )) || ( nl = strchr( line, '\n' )))
243 entry = strdup( line );
247 while ( fgets( line, BUFSIZ, fp )) {
251 if (( nl = strchr( line, '\r' )) || ( nl = strchr( line, '\n' )))
254 if ( *line == '\0' ) break;
255 if ( !( value = strchr( line, ':' ))) break;
258 while ( *value && isspace( (unsigned char) *value ))
261 addmodifyop( mods, LDAP_MOD_ADD, line, value, strlen( value ));
287 int i = 0, do_retry = maxretries;
288 pid_t pid = getpid();
289 int rc = LDAP_SUCCESS;
293 ldap_initialize( &ld, uri );
295 ld = ldap_init( host, port );
298 perror( "ldap_init" );
299 exit( EXIT_FAILURE );
303 int version = LDAP_VERSION3;
304 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
308 if ( do_retry == maxretries ) {
309 fprintf( stderr, "PID=%ld - Add/Delete(%d): entry=\"%s\".\n",
310 (long) pid, maxloop, entry );
313 rc = ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE );
314 if ( rc != LDAP_SUCCESS ) {
315 ldap_perror( ld, "ldap_bind" );
318 case LDAP_UNAVAILABLE:
319 if ( do_retry > 0 ) {
330 exit( EXIT_FAILURE );
333 for ( ; i < maxloop; i++ ) {
336 rc = ldap_add_s( ld, entry, attrs );
337 if ( rc != LDAP_SUCCESS ) {
338 ldap_perror( ld, "ldap_add" );
340 case LDAP_ALREADY_EXISTS:
341 /* NOTE: this likely means
343 * during the previous round... */
350 case LDAP_UNAVAILABLE:
351 if ( do_retry > 0 ) {
363 /* wait a second for the add to really complete */
364 /* This masks some race conditions though. */
368 /* now delete the entry again */
369 rc = ldap_delete_s( ld, entry );
370 if ( rc != LDAP_SUCCESS ) {
371 ldap_perror( ld, "ldap_delete" );
373 case LDAP_NO_SUCH_OBJECT:
374 /* NOTE: this likely means
376 * during the previous round... */
383 case LDAP_UNAVAILABLE:
384 if ( do_retry > 0 ) {
397 fprintf( stderr, " PID=%ld - Add/Delete done (%d).\n", (long) pid, rc );