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