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 );
51 "-H <uri> | ([-h <host>] -p <port>) "
63 main( int argc, char **argv )
66 char *host = "localhost";
71 char *filename = NULL;
74 int retries = RETRIES;
76 LDAPMod **attrs = NULL;
78 while ( (i = getopt( argc, argv, "H:h:p:D:w:f:l:r:t:" )) != EOF ) {
80 case 'H': /* the server's URI */
81 uri = strdup( optarg );
84 case 'h': /* the servers host */
85 host = strdup( optarg );
88 case 'p': /* the servers port */
89 port = atoi( optarg );
92 case 'D': /* the servers manager */
93 manager = strdup( optarg );
96 case 'w': /* the server managers password */
97 passwd = strdup( optarg );
100 case 'f': /* file with entry search request */
101 filename = strdup( optarg );
104 case 'l': /* the number of loops */
105 loops = atoi( optarg );
108 case 'r': /* number of retries */
109 retries = atoi( optarg );
112 case 't': /* delay in seconds */
113 delay = atoi( optarg );
122 if (( filename == NULL ) || ( port == -1 && uri == NULL ) ||
123 ( manager == NULL ) || ( passwd == NULL ))
126 entry = get_add_entry( filename, &attrs );
127 if (( entry == NULL ) || ( *entry == '\0' )) {
129 fprintf( stderr, "%s: invalid entry DN in file \"%s\".\n",
131 exit( EXIT_FAILURE );
135 if (( attrs == NULL ) || ( *attrs == '\0' )) {
137 fprintf( stderr, "%s: invalid attrs in file \"%s\".\n",
139 exit( EXIT_FAILURE );
143 do_addel( uri, host, port, manager, passwd, entry, attrs,
144 loops, retries, delay );
146 exit( EXIT_SUCCESS );
151 addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
158 modop |= LDAP_MOD_BVALUES;
161 if ( pmods != NULL ) {
162 for ( ; pmods[ i ] != NULL; ++i ) {
163 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
164 pmods[ i ]->mod_op == modop ) {
170 if ( pmods == NULL || pmods[ i ] == NULL ) {
171 if (( pmods = (LDAPMod **)realloc( pmods, (i + 2) *
172 sizeof( LDAPMod * ))) == NULL ) {
174 exit( EXIT_FAILURE );
177 pmods[ i + 1 ] = NULL;
178 if (( pmods[ i ] = (LDAPMod *)calloc( 1, sizeof( LDAPMod )))
181 exit( EXIT_FAILURE );
183 pmods[ i ]->mod_op = modop;
184 if (( pmods[ i ]->mod_type = strdup( attr )) == NULL ) {
186 exit( EXIT_FAILURE );
190 if ( value != NULL ) {
192 if ( pmods[ i ]->mod_bvalues != NULL ) {
193 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
197 if (( pmods[ i ]->mod_bvalues =
198 (struct berval **)ber_memrealloc( pmods[ i ]->mod_bvalues,
199 (j + 2) * sizeof( struct berval * ))) == NULL ) {
200 perror( "ber_realloc" );
201 exit( EXIT_FAILURE );
203 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
204 if (( bvp = (struct berval *)ber_memalloc( sizeof( struct berval )))
207 exit( EXIT_FAILURE );
209 pmods[ i ]->mod_bvalues[ j ] = bvp;
212 if (( bvp->bv_val = (char *)malloc( vlen + 1 )) == NULL ) {
214 exit( EXIT_FAILURE );
216 AC_MEMCPY( bvp->bv_val, value, vlen );
217 bvp->bv_val[ vlen ] = '\0';
223 get_add_entry( char *filename, LDAPMod ***mods )
228 if ( (fp = fopen( filename, "r" )) != NULL ) {
231 if ( fgets( line, BUFSIZ, fp )) {
234 if (( nl = strchr( line, '\r' )) || ( nl = strchr( line, '\n' )))
236 entry = strdup( line );
240 while ( fgets( line, BUFSIZ, fp )) {
244 if (( nl = strchr( line, '\r' )) || ( nl = strchr( line, '\n' )))
247 if ( *line == '\0' ) break;
248 if ( !( value = strchr( line, ':' ))) break;
251 while ( *value && isspace( (unsigned char) *value ))
254 addmodifyop( mods, LDAP_MOD_ADD, line, value, strlen( value ));
279 int i = 0, do_retry = maxretries;
280 pid_t pid = getpid();
281 int rc = LDAP_SUCCESS;
285 ldap_initialize( &ld, uri );
287 ld = ldap_init( host, port );
290 perror( "ldap_init" );
291 exit( EXIT_FAILURE );
295 int version = LDAP_VERSION3;
296 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
300 if ( do_retry == maxretries ) {
301 fprintf( stderr, "PID=%ld - Add/Delete(%d): entry=\"%s\".\n",
302 (long) pid, maxloop, entry );
305 rc = ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE );
306 if ( rc != LDAP_SUCCESS ) {
307 ldap_perror( ld, "ldap_bind" );
310 case LDAP_UNAVAILABLE:
311 if ( do_retry > 0 ) {
322 exit( EXIT_FAILURE );
325 for ( ; i < maxloop; i++ ) {
328 rc = ldap_add_s( ld, entry, attrs );
329 if ( rc != LDAP_SUCCESS ) {
330 ldap_perror( ld, "ldap_add" );
331 if ( rc == LDAP_BUSY && do_retry > 0 ) {
340 /* wait a second for the add to really complete */
341 /* This masks some race conditions though. */
345 /* now delete the entry again */
346 rc = ldap_delete_s( ld, entry );
347 if ( rc != LDAP_SUCCESS ) {
348 ldap_perror( ld, "ldap_delete" );
349 if ( rc == LDAP_BUSY && do_retry > 0 ) {
357 fprintf( stderr, " PID=%ld - Add/Delete done (%d).\n", (long) pid, rc );