2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2007 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
41 get_add_entry( char *filename, LDAPMod ***mods );
44 do_addel( char *uri, char *host, int port, char *manager, char *passwd,
45 char *dn, LDAPMod **attrs, int maxloop, int maxretries, int delay,
53 "-H <uri> | ([-h <host>] -p <port>) "
66 main( int argc, char **argv )
69 char *host = "localhost";
74 char *filename = NULL;
77 int retries = RETRIES;
80 LDAPMod **attrs = NULL;
82 while ( (i = getopt( argc, argv, "FH:h:p:D:w:f:l:r:t:" )) != EOF ) {
88 case 'H': /* the server's URI */
89 uri = strdup( optarg );
92 case 'h': /* the servers host */
93 host = strdup( optarg );
96 case 'p': /* the servers port */
97 if ( lutil_atoi( &port, optarg ) != 0 ) {
102 case 'D': /* the servers manager */
103 manager = strdup( optarg );
106 case 'w': /* the server managers password */
107 passwd = strdup( optarg );
110 case 'f': /* file with entry search request */
111 filename = strdup( optarg );
114 case 'l': /* the number of loops */
115 if ( lutil_atoi( &loops, optarg ) != 0 ) {
120 case 'r': /* number of retries */
121 if ( lutil_atoi( &retries, optarg ) != 0 ) {
126 case 't': /* delay in seconds */
127 if ( lutil_atoi( &delay, optarg ) != 0 ) {
138 if (( filename == NULL ) || ( port == -1 && uri == NULL ) ||
139 ( manager == NULL ) || ( passwd == NULL ))
142 entry = get_add_entry( filename, &attrs );
143 if (( entry == NULL ) || ( *entry == '\0' )) {
145 fprintf( stderr, "%s: invalid entry DN in file \"%s\".\n",
147 exit( EXIT_FAILURE );
151 if (( attrs == NULL ) || ( *attrs == '\0' )) {
153 fprintf( stderr, "%s: invalid attrs in file \"%s\".\n",
155 exit( EXIT_FAILURE );
159 do_addel( uri, host, port, manager, passwd, entry, attrs,
160 loops, retries, delay, friendly );
162 exit( EXIT_SUCCESS );
167 addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
174 modop |= LDAP_MOD_BVALUES;
177 if ( pmods != NULL ) {
178 for ( ; pmods[ i ] != NULL; ++i ) {
179 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
180 pmods[ i ]->mod_op == modop ) {
186 if ( pmods == NULL || pmods[ i ] == NULL ) {
187 if (( pmods = (LDAPMod **)realloc( pmods, (i + 2) *
188 sizeof( LDAPMod * ))) == NULL ) {
190 exit( EXIT_FAILURE );
193 pmods[ i + 1 ] = NULL;
194 if (( pmods[ i ] = (LDAPMod *)calloc( 1, sizeof( LDAPMod )))
197 exit( EXIT_FAILURE );
199 pmods[ i ]->mod_op = modop;
200 if (( pmods[ i ]->mod_type = strdup( attr )) == NULL ) {
202 exit( EXIT_FAILURE );
206 if ( value != NULL ) {
208 if ( pmods[ i ]->mod_bvalues != NULL ) {
209 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
213 if (( pmods[ i ]->mod_bvalues =
214 (struct berval **)ber_memrealloc( pmods[ i ]->mod_bvalues,
215 (j + 2) * sizeof( struct berval * ))) == NULL ) {
216 perror( "ber_realloc" );
217 exit( EXIT_FAILURE );
219 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
220 if (( bvp = (struct berval *)ber_memalloc( sizeof( struct berval )))
223 exit( EXIT_FAILURE );
225 pmods[ i ]->mod_bvalues[ j ] = bvp;
228 if (( bvp->bv_val = (char *)malloc( vlen + 1 )) == NULL ) {
230 exit( EXIT_FAILURE );
232 AC_MEMCPY( bvp->bv_val, value, vlen );
233 bvp->bv_val[ vlen ] = '\0';
239 get_add_entry( char *filename, LDAPMod ***mods )
244 if ( (fp = fopen( filename, "r" )) != NULL ) {
247 if ( fgets( line, BUFSIZ, fp )) {
250 if (( nl = strchr( line, '\r' )) || ( nl = strchr( line, '\n' )))
252 entry = strdup( line );
256 while ( fgets( line, BUFSIZ, fp )) {
260 if (( nl = strchr( line, '\r' )) || ( nl = strchr( line, '\n' )))
263 if ( *line == '\0' ) break;
264 if ( !( value = strchr( line, ':' ))) break;
267 while ( *value && isspace( (unsigned char) *value ))
270 addmodifyop( mods, LDAP_MOD_ADD, line, value, strlen( value ));
296 int i = 0, do_retry = maxretries;
297 pid_t pid = getpid();
298 int rc = LDAP_SUCCESS;
302 ldap_initialize( &ld, uri );
304 ld = ldap_init( host, port );
307 perror( "ldap_init" );
308 exit( EXIT_FAILURE );
312 int version = LDAP_VERSION3;
313 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
317 if ( do_retry == maxretries ) {
318 fprintf( stderr, "PID=%ld - Add/Delete(%d): entry=\"%s\".\n",
319 (long) pid, maxloop, entry );
322 rc = ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE );
323 if ( rc != LDAP_SUCCESS ) {
324 ldap_perror( ld, "ldap_bind" );
327 case LDAP_UNAVAILABLE:
328 if ( do_retry > 0 ) {
339 exit( EXIT_FAILURE );
342 for ( ; i < maxloop; i++ ) {
345 rc = ldap_add_s( ld, entry, attrs );
346 if ( rc != LDAP_SUCCESS ) {
347 ldap_perror( ld, "ldap_add" );
349 case LDAP_ALREADY_EXISTS:
350 /* NOTE: this likely means
352 * during the previous round... */
359 case LDAP_UNAVAILABLE:
360 if ( do_retry > 0 ) {
372 /* wait a second for the add to really complete */
373 /* This masks some race conditions though. */
377 /* now delete the entry again */
378 rc = ldap_delete_s( ld, entry );
379 if ( rc != LDAP_SUCCESS ) {
380 ldap_perror( ld, "ldap_delete" );
382 case LDAP_NO_SUCH_OBJECT:
383 /* NOTE: this likely means
385 * during the previous round... */
392 case LDAP_UNAVAILABLE:
393 if ( do_retry > 0 ) {
406 fprintf( stderr, " PID=%ld - Add/Delete done (%d).\n", (long) pid, rc );