12 #ifdef HAVE_SYS_PARAM_H
13 #include <sys/param.h>
22 get_add_entry( char *filename, LDAPMod ***mods );
25 do_addel( char *host, int port, char *manager, char *passwd,
26 char *dn, LDAPMod **attrs, int maxloop );
31 fprintf( stderr, "usage: %s [-h <host>] -p port -D <managerDN> -w <passwd> -f <addfile> [-l <loops>]\n",
37 main( int argc, char **argv )
40 char *host = "localhost";
44 char *filename = NULL;
47 LDAPMod **attrs = NULL;
49 while ( (i = getopt( argc, argv, "h:p:D:w:f:l:" )) != EOF ) {
51 case 'h': /* the servers host */
52 host = strdup( optarg );
55 case 'p': /* the servers port */
56 port = atoi( optarg );
59 case 'D': /* the servers manager */
60 manager = strdup( optarg );
63 case 'w': /* the server managers password */
64 passwd = strdup( optarg );
67 case 'f': /* file with entry search request */
68 filename = strdup( optarg );
71 case 'l': /* the number of loops */
72 loops = atoi( optarg );
81 if (( filename == NULL ) || ( port == -1 ) ||
82 ( manager == NULL ) || ( passwd == NULL ))
85 entry = get_add_entry( filename, &attrs );
86 if (( entry == NULL ) || ( *entry == '\0' )) {
88 fprintf( stderr, "%s: invalid entry DN in file \"%s\".\n",
94 if (( attrs == NULL ) || ( *attrs == '\0' )) {
96 fprintf( stderr, "%s: invalid attrs in file \"%s\".\n",
102 do_addel( host, port, manager, passwd, entry, attrs, loops );
109 addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
116 modop |= LDAP_MOD_BVALUES;
119 if ( pmods != NULL ) {
120 for ( ; pmods[ i ] != NULL; ++i ) {
121 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
122 pmods[ i ]->mod_op == modop ) {
128 if ( pmods == NULL || pmods[ i ] == NULL ) {
129 if (( pmods = (LDAPMod **)realloc( pmods, (i + 2) *
130 sizeof( LDAPMod * ))) == NULL ) {
135 pmods[ i + 1 ] = NULL;
136 if (( pmods[ i ] = (LDAPMod *)calloc( 1, sizeof( LDAPMod )))
141 pmods[ i ]->mod_op = modop;
142 if (( pmods[ i ]->mod_type = strdup( attr )) == NULL ) {
148 if ( value != NULL ) {
150 if ( pmods[ i ]->mod_bvalues != NULL ) {
151 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
155 if (( pmods[ i ]->mod_bvalues =
156 (struct berval **)ber_realloc( pmods[ i ]->mod_bvalues,
157 (j + 2) * sizeof( struct berval * ))) == NULL ) {
158 perror( "ber_realloc" );
161 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
162 if (( bvp = (struct berval *)ber_malloc( sizeof( struct berval )))
167 pmods[ i ]->mod_bvalues[ j ] = bvp;
170 if (( bvp->bv_val = (char *)malloc( vlen + 1 )) == NULL ) {
174 SAFEMEMCPY( bvp->bv_val, value, vlen );
175 bvp->bv_val[ vlen ] = '\0';
181 get_add_entry( char *filename, LDAPMod ***mods )
186 if ( (fp = fopen( filename, "r" )) != NULL ) {
189 if ( fgets( line, BUFSIZ, fp )) {
192 if (( nl = strchr( line, '\r' )) || ( nl = strchr( line, '\n' )))
194 entry = strdup( line );
198 while ( fgets( line, BUFSIZ, fp )) {
202 if (( nl = strchr( line, '\r' )) || ( nl = strchr( line, '\n' )))
205 if ( *line == '\0' ) break;
206 if ( !( value = strchr( line, ':' ))) break;
209 while ( *value && isspace( (unsigned char) *value ))
212 addmodifyop( mods, LDAP_MOD_ADD, line, value, strlen( value ));
235 pid_t pid = getpid();
237 if (( ld = ldap_init( host, port )) == NULL ) {
238 perror( "ldap_init" );
242 if ( ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE )
244 ldap_perror( ld, "ldap_bind" );
249 fprintf( stderr, "PID=%ld - Add/Delete(%d): entry=\"%s\".\n",
250 (long) pid, maxloop, entry );
252 for ( i = 0; i < maxloop; i++ ) {
255 if ( ldap_add_s( ld, entry, attrs ) != LDAP_SUCCESS ) {
257 ldap_perror( ld, "ldap_add" );
262 /* wait a second for the add to really complete */
265 /* now delete the entry again */
266 if ( ldap_delete_s( ld, entry ) != LDAP_SUCCESS ) {
268 ldap_perror( ld, "ldap_delete" );
275 fprintf( stderr, " PID=%ld - Add/Delete done.\n", (long) pid );