10 #include <ac/unistd.h>
13 #ifdef HAVE_SYS_PARAM_H
14 #include <sys/param.h>
23 get_add_entry( char *filename, LDAPMod ***mods );
26 do_addel( char *host, int port, char *manager, char *passwd,
27 char *dn, LDAPMod **attrs, int maxloop );
32 fprintf( stderr, "usage: %s [-h <host>] -p port -D <managerDN> -w <passwd> -f <addfile> [-l <loops>]\n",
38 main( int argc, char **argv )
41 char *host = "localhost";
45 char *filename = NULL;
48 LDAPMod **attrs = NULL;
50 while ( (i = getopt( argc, argv, "h:p:D:w:f:l:" )) != EOF ) {
52 case 'h': /* the servers host */
53 host = strdup( optarg );
56 case 'p': /* the servers port */
57 port = atoi( optarg );
60 case 'D': /* the servers manager */
61 manager = strdup( optarg );
64 case 'w': /* the server managers password */
65 passwd = strdup( optarg );
68 case 'f': /* file with entry search request */
69 filename = strdup( optarg );
72 case 'l': /* the number of loops */
73 loops = atoi( optarg );
82 if (( filename == NULL ) || ( port == -1 ) ||
83 ( manager == NULL ) || ( passwd == NULL ))
86 entry = get_add_entry( filename, &attrs );
87 if (( entry == NULL ) || ( *entry == '\0' )) {
89 fprintf( stderr, "%s: invalid entry DN in file \"%s\".\n",
95 if (( attrs == NULL ) || ( *attrs == '\0' )) {
97 fprintf( stderr, "%s: invalid attrs in file \"%s\".\n",
103 do_addel( host, port, manager, passwd, entry, attrs, loops );
110 addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
117 modop |= LDAP_MOD_BVALUES;
120 if ( pmods != NULL ) {
121 for ( ; pmods[ i ] != NULL; ++i ) {
122 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
123 pmods[ i ]->mod_op == modop ) {
129 if ( pmods == NULL || pmods[ i ] == NULL ) {
130 if (( pmods = (LDAPMod **)realloc( pmods, (i + 2) *
131 sizeof( LDAPMod * ))) == NULL ) {
136 pmods[ i + 1 ] = NULL;
137 if (( pmods[ i ] = (LDAPMod *)calloc( 1, sizeof( LDAPMod )))
142 pmods[ i ]->mod_op = modop;
143 if (( pmods[ i ]->mod_type = strdup( attr )) == NULL ) {
149 if ( value != NULL ) {
151 if ( pmods[ i ]->mod_bvalues != NULL ) {
152 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
156 if (( pmods[ i ]->mod_bvalues =
157 (struct berval **)ber_memrealloc( pmods[ i ]->mod_bvalues,
158 (j + 2) * sizeof( struct berval * ))) == NULL ) {
159 perror( "ber_realloc" );
162 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
163 if (( bvp = (struct berval *)ber_memalloc( sizeof( struct berval )))
168 pmods[ i ]->mod_bvalues[ j ] = bvp;
171 if (( bvp->bv_val = (char *)malloc( vlen + 1 )) == NULL ) {
175 SAFEMEMCPY( bvp->bv_val, value, vlen );
176 bvp->bv_val[ vlen ] = '\0';
182 get_add_entry( char *filename, LDAPMod ***mods )
187 if ( (fp = fopen( filename, "r" )) != NULL ) {
190 if ( fgets( line, BUFSIZ, fp )) {
193 if (( nl = strchr( line, '\r' )) || ( nl = strchr( line, '\n' )))
195 entry = strdup( line );
199 while ( fgets( line, BUFSIZ, fp )) {
203 if (( nl = strchr( line, '\r' )) || ( nl = strchr( line, '\n' )))
206 if ( *line == '\0' ) break;
207 if ( !( value = strchr( line, ':' ))) break;
210 while ( *value && isspace( (unsigned char) *value ))
213 addmodifyop( mods, LDAP_MOD_ADD, line, value, strlen( value ));
236 pid_t pid = getpid();
238 if (( ld = ldap_init( host, port )) == NULL ) {
239 perror( "ldap_init" );
243 if ( ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE )
245 ldap_perror( ld, "ldap_bind" );
250 fprintf( stderr, "PID=%ld - Add/Delete(%d): entry=\"%s\".\n",
251 (long) pid, maxloop, entry );
253 for ( i = 0; i < maxloop; i++ ) {
256 if ( ldap_add_s( ld, entry, attrs ) != LDAP_SUCCESS ) {
258 ldap_perror( ld, "ldap_add" );
263 /* wait a second for the add to really complete */
266 /* now delete the entry again */
267 if ( ldap_delete_s( ld, entry ) != LDAP_SUCCESS ) {
269 ldap_perror( ld, "ldap_delete" );
276 fprintf( stderr, " PID=%ld - Add/Delete done.\n", (long) pid );