]> git.sur5r.net Git - openldap/blob - tests/progs/slapd-addel.c
d4a98e9a72e984da7bd617759e62e12900804d77
[openldap] / tests / progs / slapd-addel.c
1 #include "portable.h"
2
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 #include <ac/ctype.h>
7 #include <ac/socket.h>
8 #include <ac/string.h>
9 #include <ac/unistd.h>
10 #include <ac/wait.h>
11
12 #ifdef HAVE_SYS_PARAM_H
13 #include <sys/param.h>
14 #endif
15
16 #include "lber.h"
17 #include "ldap.h"
18
19 #define LOOPS   100
20
21 static char *
22 get_add_entry( char *filename, LDAPMod ***mods );
23
24 static void
25 do_addel( char *host, int port, char *manager, char *passwd,
26         char *dn, LDAPMod **attrs, int maxloop );
27
28 static void
29 usage( char *name )
30 {
31         fprintf( stderr, "usage: %s [-h <host>] -p port -D <managerDN> -w <passwd> -f <addfile> [-l <loops>]\n",
32                         name );
33         exit( 1 );
34 }
35
36 int
37 main( int argc, char **argv )
38 {
39         int             i;
40         char        *host = "localhost";
41         int                     port = -1;
42         char            *manager = NULL;
43         char            *passwd = NULL;
44         char            *filename = NULL;
45         char            *entry = NULL;
46         int                     loops = LOOPS;
47         LDAPMod     **attrs = NULL;
48
49         while ( (i = getopt( argc, argv, "h:p:D:w:f:l:" )) != EOF ) {
50                 switch( i ) {
51                         case 'h':               /* the servers host */
52                                 host = strdup( optarg );
53                         break;
54
55                         case 'p':               /* the servers port */
56                                 port = atoi( optarg );
57                                 break;
58
59                         case 'D':               /* the servers manager */
60                                 manager = strdup( optarg );
61                         break;
62
63                         case 'w':               /* the server managers password */
64                                 passwd = strdup( optarg );
65                         break;
66
67                         case 'f':               /* file with entry search request */
68                                 filename = strdup( optarg );
69                                 break;
70
71                         case 'l':               /* the number of loops */
72                                 loops = atoi( optarg );
73                                 break;
74
75                         default:
76                                 usage( argv[0] );
77                                 break;
78                 }
79         }
80
81         if (( filename == NULL ) || ( port == -1 ) ||
82                                 ( manager == NULL ) || ( passwd == NULL ))
83                 usage( argv[0] );
84
85         entry = get_add_entry( filename, &attrs );
86         if (( entry == NULL ) || ( *entry == '\0' )) {
87
88                 fprintf( stderr, "%s: invalid entry DN in file \"%s\".\n",
89                                 argv[0], filename );
90                 exit( 1 );
91
92         }
93
94         if (( attrs == NULL ) || ( *attrs == '\0' )) {
95
96                 fprintf( stderr, "%s: invalid attrs in file \"%s\".\n",
97                                 argv[0], filename );
98                 exit( 1 );
99
100         }
101
102         do_addel( host, port, manager, passwd, entry, attrs, loops );
103
104         exit( 0 );
105 }
106
107
108 static void
109 addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
110 {
111     LDAPMod             **pmods;
112     int                 i, j;
113     struct berval       *bvp;
114
115     pmods = *pmodsp;
116     modop |= LDAP_MOD_BVALUES;
117
118     i = 0;
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 ) {
123                                 break;
124                 }
125                 }
126     }
127
128     if ( pmods == NULL || pmods[ i ] == NULL ) {
129                 if (( pmods = (LDAPMod **)realloc( pmods, (i + 2) *
130                         sizeof( LDAPMod * ))) == NULL ) {
131                         perror( "realloc" );
132                         exit( 1 );
133                 }
134                 *pmodsp = pmods;
135                 pmods[ i + 1 ] = NULL;
136                 if (( pmods[ i ] = (LDAPMod *)calloc( 1, sizeof( LDAPMod )))
137                         == NULL ) {
138                         perror( "calloc" );
139                         exit( 1 );
140                 }
141                 pmods[ i ]->mod_op = modop;
142                 if (( pmods[ i ]->mod_type = strdup( attr )) == NULL ) {
143                 perror( "strdup" );
144                 exit( 1 );
145                 }
146     }
147
148     if ( value != NULL ) {
149                 j = 0;
150                 if ( pmods[ i ]->mod_bvalues != NULL ) {
151                 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
152                                 ;
153                 }
154                 }
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" );
159                         exit( 1 );
160                 }
161                 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
162                 if (( bvp = (struct berval *)ber_malloc( sizeof( struct berval )))
163                         == NULL ) {
164                         perror( "malloc" );
165                         exit( 1 );
166                 }
167                 pmods[ i ]->mod_bvalues[ j ] = bvp;
168
169             bvp->bv_len = vlen;
170             if (( bvp->bv_val = (char *)malloc( vlen + 1 )) == NULL ) {
171                         perror( "malloc" );
172                         exit( 1 );
173             }
174             SAFEMEMCPY( bvp->bv_val, value, vlen );
175             bvp->bv_val[ vlen ] = '\0';
176     }
177 }
178
179
180 static char *
181 get_add_entry( char *filename, LDAPMod ***mods )
182 {
183         FILE    *fp;
184         char    *entry = NULL;
185
186         if ( (fp = fopen( filename, "r" )) != NULL ) {
187                 char  line[BUFSIZ];
188
189                 if ( fgets( line, BUFSIZ, fp )) {
190                         char *nl;
191
192                         if (( nl = strchr( line, '\r' )) || ( nl = strchr( line, '\n' )))
193                                 *nl = '\0';
194                         entry = strdup( line );
195
196                 }
197
198                 while ( fgets( line, BUFSIZ, fp )) {
199                         char    *nl;
200                         char    *value;
201
202                         if (( nl = strchr( line, '\r' )) || ( nl = strchr( line, '\n' )))
203                                 *nl = '\0';
204
205                         if ( *line == '\0' ) break;
206                         if ( !( value = strchr( line, ':' ))) break;
207
208                         *value++ = '\0'; 
209                         while ( *value && isspace( (unsigned char) *value ))
210                                 value++;
211
212                         addmodifyop( mods, LDAP_MOD_ADD, line, value, strlen( value ));
213
214                 }
215                 fclose( fp );
216         }
217
218         return( entry );
219 }
220
221
222 static void
223 do_addel(
224         char *host,
225         int port,
226         char *manager,
227         char *passwd,
228         char *entry,
229         LDAPMod **attrs,
230         int maxloop
231 )
232 {
233         LDAP    *ld;
234         int     i;
235         pid_t   pid = getpid();
236
237         if (( ld = ldap_init( host, port )) == NULL ) {
238                 perror( "ldap_init" );
239                 exit( 1 );
240         }
241
242         if ( ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE )
243                                 != LDAP_SUCCESS ) {
244                 ldap_perror( ld, "ldap_bind" );
245                  exit( 1 );
246         }
247
248
249         fprintf( stderr, "PID=%ld - Add/Delete(%d): entry=\"%s\".\n",
250                                         (long) pid, maxloop, entry );
251
252         for ( i = 0; i < maxloop; i++ ) {
253
254                 /* add the entry */
255                 if ( ldap_add_s( ld, entry, attrs ) != LDAP_SUCCESS ) {
256
257                         ldap_perror( ld, "ldap_add" );
258                         break;
259
260                 }
261
262                 /* wait a second for the add to really complete */
263                 sleep( 1 );
264
265                 /* now delete the entry again */
266                 if ( ldap_delete_s( ld, entry ) != LDAP_SUCCESS ) {
267
268                         ldap_perror( ld, "ldap_delete" );
269                         break;
270
271                 }
272
273         }
274
275         fprintf( stderr, " PID=%ld - Add/Delete done.\n", (long) pid );
276
277         ldap_unbind( ld );
278 }
279
280