1*549b59edSchristos /* $NetBSD: slapd-auth.c,v 1.2 2021/08/14 16:15:03 christos Exp $ */
2e670fd5cSchristos
3e670fd5cSchristos /* $OpenLDAP$ */
4e670fd5cSchristos /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5e670fd5cSchristos *
6e670fd5cSchristos * Copyright 2006-2021 The OpenLDAP Foundation.
7e670fd5cSchristos * All rights reserved.
8e670fd5cSchristos *
9e670fd5cSchristos * Redistribution and use in source and binary forms, with or without
10e670fd5cSchristos * modification, are permitted only as authorized by the OpenLDAP
11e670fd5cSchristos * Public License.
12e670fd5cSchristos *
13e670fd5cSchristos * A copy of this license is available in file LICENSE in the
14e670fd5cSchristos * top-level directory of the distribution or, alternatively, at
15e670fd5cSchristos * <http://www.OpenLDAP.org/license.html>.
16e670fd5cSchristos */
17e670fd5cSchristos /* ACKNOWLEDGEMENTS:
18e670fd5cSchristos * This work was initially developed by Howard Chu for inclusion
19e670fd5cSchristos * in OpenLDAP Software.
20e670fd5cSchristos */
21e670fd5cSchristos
22e670fd5cSchristos #include <sys/cdefs.h>
23*549b59edSchristos __RCSID("$NetBSD: slapd-auth.c,v 1.2 2021/08/14 16:15:03 christos Exp $");
24e670fd5cSchristos
25e670fd5cSchristos #include "portable.h"
26e670fd5cSchristos
27e670fd5cSchristos #include <stdio.h>
28e670fd5cSchristos
29e670fd5cSchristos #include <ac/stdlib.h>
30e670fd5cSchristos
31e670fd5cSchristos #include <ac/ctype.h>
32e670fd5cSchristos #include <ac/param.h>
33e670fd5cSchristos #include <ac/socket.h>
34e670fd5cSchristos #include <ac/string.h>
35e670fd5cSchristos #include <ac/unistd.h>
36e670fd5cSchristos #include <ac/wait.h>
37e670fd5cSchristos #include <ac/time.h>
38e670fd5cSchristos #include <ac/signal.h>
39e670fd5cSchristos
40e670fd5cSchristos #include <ldap.h>
41e670fd5cSchristos #include <ldap_pvt_thread.h>
42e670fd5cSchristos #include <lutil.h>
43e670fd5cSchristos
44e670fd5cSchristos static int
45e670fd5cSchristos do_time( );
46e670fd5cSchristos
47e670fd5cSchristos /* This program is a simplified version of SLAMD's WeightedAuthRate jobclass.
48e670fd5cSchristos * It doesn't offer as much configurability, but it's a good starting point.
49e670fd5cSchristos * When run without the -R option it will behave as a Standard AuthRate job.
50e670fd5cSchristos * Eventually this will grow into a set of C-based load generators for the SLAMD
51e670fd5cSchristos * framework. This code is anywhere from 2 to 10 times more efficient than the
52e670fd5cSchristos * original Java code, allowing servers to be fully loaded without requiring
53e670fd5cSchristos * anywhere near as much load-generation hardware.
54e670fd5cSchristos */
55e670fd5cSchristos static void
usage(char * name)56e670fd5cSchristos usage( char *name )
57e670fd5cSchristos {
58e670fd5cSchristos fprintf( stderr, "usage: %s -H <uri> -b <baseDN> -w <passwd> -t <seconds> -r lo:hi\n\t"
59e670fd5cSchristos "[-R %:lo:hi] [-f <filter-template>] [-n <threads>] [-D <bindDN>] [-i <seconds>]\n",
60e670fd5cSchristos name );
61e670fd5cSchristos exit( EXIT_FAILURE );
62e670fd5cSchristos }
63e670fd5cSchristos
64e670fd5cSchristos static char *filter = "(uid=user.%d)";
65e670fd5cSchristos
66e670fd5cSchristos static char hname[1024];
67e670fd5cSchristos static char *uri = "ldap:///";
68e670fd5cSchristos static char *base;
69e670fd5cSchristos static char *pass;
70e670fd5cSchristos static char *binder;
71e670fd5cSchristos
72e670fd5cSchristos static int tdur, r1per, r1lo, r1hi, r2per, r2lo, r2hi;
73e670fd5cSchristos static int threads = 1;
74e670fd5cSchristos
75e670fd5cSchristos static int interval = 30;
76e670fd5cSchristos
77e670fd5cSchristos static volatile int *r1binds, *r2binds;
78e670fd5cSchristos static int *r1old, *r2old;
79e670fd5cSchristos static volatile int finish;
80e670fd5cSchristos
81e670fd5cSchristos int
main(int argc,char ** argv)82e670fd5cSchristos main( int argc, char **argv )
83e670fd5cSchristos {
84e670fd5cSchristos int i;
85e670fd5cSchristos
86e670fd5cSchristos while ( (i = getopt( argc, argv, "b:D:H:w:f:n:i:t:r:R:" )) != EOF ) {
87e670fd5cSchristos switch( i ) {
88e670fd5cSchristos case 'b': /* base DN of a tree of user DNs */
89e670fd5cSchristos base = optarg;
90e670fd5cSchristos break;
91e670fd5cSchristos
92e670fd5cSchristos case 'D':
93e670fd5cSchristos binder = optarg;
94e670fd5cSchristos break;
95e670fd5cSchristos
96e670fd5cSchristos case 'H': /* the server uri */
97e670fd5cSchristos uri = optarg;
98e670fd5cSchristos break;
99e670fd5cSchristos
100e670fd5cSchristos case 'w':
101e670fd5cSchristos pass = strdup( optarg );
102e670fd5cSchristos break;
103e670fd5cSchristos
104e670fd5cSchristos case 't': /* the duration to run */
105e670fd5cSchristos if ( lutil_atoi( &tdur, optarg ) != 0 ) {
106e670fd5cSchristos usage( argv[0] );
107e670fd5cSchristos }
108e670fd5cSchristos break;
109e670fd5cSchristos
110e670fd5cSchristos case 'i': /* the time interval */
111e670fd5cSchristos if ( lutil_atoi( &interval, optarg ) != 0 ) {
112e670fd5cSchristos usage( argv[0] );
113e670fd5cSchristos }
114e670fd5cSchristos break;
115e670fd5cSchristos
116e670fd5cSchristos case 'r': /* the uid range */
117e670fd5cSchristos if ( sscanf(optarg, "%d:%d", &r1lo, &r1hi) != 2 ) {
118e670fd5cSchristos usage( argv[0] );
119e670fd5cSchristos }
120e670fd5cSchristos break;
121e670fd5cSchristos
122e670fd5cSchristos case 'R': /* percentage:2nd uid range */
123e670fd5cSchristos if ( sscanf(optarg, "%d:%d:%d", &r2per, &r2lo, &r2hi) != 3 ) {
124e670fd5cSchristos usage( argv[0] );
125e670fd5cSchristos }
126e670fd5cSchristos break;
127e670fd5cSchristos
128e670fd5cSchristos case 'f':
129e670fd5cSchristos filter = optarg;
130e670fd5cSchristos break;
131e670fd5cSchristos
132e670fd5cSchristos case 'n':
133e670fd5cSchristos if ( lutil_atoi( &threads, optarg ) != 0 || threads < 1 ) {
134e670fd5cSchristos usage( argv[0] );
135e670fd5cSchristos }
136e670fd5cSchristos break;
137e670fd5cSchristos
138e670fd5cSchristos default:
139e670fd5cSchristos usage( argv[0] );
140e670fd5cSchristos break;
141e670fd5cSchristos }
142e670fd5cSchristos }
143e670fd5cSchristos
144e670fd5cSchristos if ( tdur == 0 || r1hi <= r1lo )
145e670fd5cSchristos usage( argv[0] );
146e670fd5cSchristos
147e670fd5cSchristos r1per = 100 - r2per;
148e670fd5cSchristos if ( r1per < 1 )
149e670fd5cSchristos usage( argv[0] );
150e670fd5cSchristos
151e670fd5cSchristos r1binds = calloc( threads*4, sizeof( int ));
152e670fd5cSchristos r2binds = r1binds + threads;
153e670fd5cSchristos r1old = (int *)r2binds + threads;
154e670fd5cSchristos r2old = r1old + threads;
155e670fd5cSchristos
156e670fd5cSchristos do_time( );
157e670fd5cSchristos
158e670fd5cSchristos exit( EXIT_SUCCESS );
159e670fd5cSchristos }
160e670fd5cSchristos
161e670fd5cSchristos static void *
my_task(void * my_num)162e670fd5cSchristos my_task( void *my_num )
163e670fd5cSchristos {
164e670fd5cSchristos LDAP *ld = NULL, *sld = NULL;
165e670fd5cSchristos ber_int_t msgid;
166e670fd5cSchristos LDAPMessage *res, *msg;
167e670fd5cSchristos char *attrs[] = { "1.1", NULL };
168e670fd5cSchristos int rc = LDAP_SUCCESS;
169e670fd5cSchristos int tid = *(int *)my_num;
170e670fd5cSchristos
171e670fd5cSchristos ldap_initialize( &ld, uri );
172e670fd5cSchristos if ( ld == NULL ) {
173e670fd5cSchristos perror( "ldap_initialize" );
174e670fd5cSchristos return NULL;
175e670fd5cSchristos }
176e670fd5cSchristos
177e670fd5cSchristos {
178e670fd5cSchristos int version = LDAP_VERSION3;
179e670fd5cSchristos (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
180e670fd5cSchristos &version );
181e670fd5cSchristos }
182e670fd5cSchristos (void) ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
183e670fd5cSchristos
184e670fd5cSchristos ldap_initialize( &sld, uri );
185e670fd5cSchristos if ( sld == NULL ) {
186e670fd5cSchristos perror( "ldap_initialize" );
187e670fd5cSchristos return NULL;
188e670fd5cSchristos }
189e670fd5cSchristos
190e670fd5cSchristos {
191e670fd5cSchristos int version = LDAP_VERSION3;
192e670fd5cSchristos (void) ldap_set_option( sld, LDAP_OPT_PROTOCOL_VERSION,
193e670fd5cSchristos &version );
194e670fd5cSchristos }
195e670fd5cSchristos (void) ldap_set_option( sld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
196e670fd5cSchristos if ( binder ) {
197e670fd5cSchristos rc = ldap_bind_s( sld, binder, pass, LDAP_AUTH_SIMPLE );
198e670fd5cSchristos if ( rc != LDAP_SUCCESS ) {
199e670fd5cSchristos ldap_perror( sld, "ldap_bind" );
200e670fd5cSchristos }
201e670fd5cSchristos }
202e670fd5cSchristos
203e670fd5cSchristos r1binds[tid] = 0;
204e670fd5cSchristos
205e670fd5cSchristos for (;;) {
206e670fd5cSchristos char dn[BUFSIZ], *ptr, fstr[256];
207e670fd5cSchristos int j, isr1;
208e670fd5cSchristos
209e670fd5cSchristos if ( finish )
210e670fd5cSchristos break;
211e670fd5cSchristos
212e670fd5cSchristos j = rand() % 100;
213e670fd5cSchristos if ( j < r1per ) {
214e670fd5cSchristos j = rand() % r1hi;
215e670fd5cSchristos isr1 = 1;
216e670fd5cSchristos } else {
217e670fd5cSchristos j = rand() % (r2hi - r2lo + 1 );
218e670fd5cSchristos j += r2lo;
219e670fd5cSchristos isr1 = 0;
220e670fd5cSchristos }
221e670fd5cSchristos sprintf(fstr, filter, j);
222e670fd5cSchristos
223e670fd5cSchristos rc = ldap_search_ext( sld, base, LDAP_SCOPE_SUB,
224e670fd5cSchristos fstr, attrs, 0, NULL, NULL, 0, 0, &msgid );
225e670fd5cSchristos if ( rc != LDAP_SUCCESS ) {
226e670fd5cSchristos ldap_perror( sld, "ldap_search_ex" );
227e670fd5cSchristos return NULL;
228e670fd5cSchristos }
229e670fd5cSchristos
230e670fd5cSchristos while (( rc=ldap_result( sld, LDAP_RES_ANY, LDAP_MSG_ONE, NULL, &res )) >0){
231e670fd5cSchristos BerElement *ber;
232e670fd5cSchristos struct berval bv;
233e670fd5cSchristos char *ptr;
234e670fd5cSchristos int done = 0;
235e670fd5cSchristos
236e670fd5cSchristos for (msg = ldap_first_message( sld, res ); msg;
237e670fd5cSchristos msg = ldap_next_message( sld, msg )) {
238e670fd5cSchristos switch ( ldap_msgtype( msg )) {
239e670fd5cSchristos case LDAP_RES_SEARCH_ENTRY:
240e670fd5cSchristos rc = ldap_get_dn_ber( sld, msg, &ber, &bv );
241e670fd5cSchristos strcpy(dn, bv.bv_val );
242e670fd5cSchristos ber_free( ber, 0 );
243e670fd5cSchristos break;
244e670fd5cSchristos case LDAP_RES_SEARCH_RESULT:
245e670fd5cSchristos done = 1;
246e670fd5cSchristos break;
247e670fd5cSchristos }
248e670fd5cSchristos if ( done )
249e670fd5cSchristos break;
250e670fd5cSchristos }
251e670fd5cSchristos ldap_msgfree( res );
252e670fd5cSchristos if ( done ) break;
253e670fd5cSchristos }
254e670fd5cSchristos
255e670fd5cSchristos rc = ldap_bind_s( ld, dn, pass, LDAP_AUTH_SIMPLE );
256e670fd5cSchristos if ( rc != LDAP_SUCCESS ) {
257e670fd5cSchristos ldap_perror( ld, "ldap_bind" );
258e670fd5cSchristos }
259e670fd5cSchristos if ( isr1 )
260e670fd5cSchristos r1binds[tid]++;
261e670fd5cSchristos else
262e670fd5cSchristos r2binds[tid]++;
263e670fd5cSchristos }
264e670fd5cSchristos
265e670fd5cSchristos ldap_unbind( sld );
266e670fd5cSchristos ldap_unbind( ld );
267e670fd5cSchristos
268e670fd5cSchristos return NULL;
269e670fd5cSchristos }
270e670fd5cSchristos
271e670fd5cSchristos static int
do_time()272e670fd5cSchristos do_time( )
273e670fd5cSchristos {
274e670fd5cSchristos struct timeval tv;
275e670fd5cSchristos time_t now, prevt, start;
276e670fd5cSchristos
277e670fd5cSchristos int r1new, r2new;
278e670fd5cSchristos int dt, dr1, dr2, rr1, rr2;
279e670fd5cSchristos int dr10, dr20;
280e670fd5cSchristos int i;
281e670fd5cSchristos
282e670fd5cSchristos gethostname(hname, sizeof(hname));
283e670fd5cSchristos printf("%s(tid)\tdeltaT\tauth1\tauth2\trate1\trate2\tRate1+2\n", hname);
284e670fd5cSchristos srand(getpid());
285e670fd5cSchristos
286e670fd5cSchristos prevt = start = time(0L);
287e670fd5cSchristos
288e670fd5cSchristos for ( i = 0; i<threads; i++ ) {
289e670fd5cSchristos ldap_pvt_thread_t thr;
290e670fd5cSchristos r1binds[i] = i;
291e670fd5cSchristos ldap_pvt_thread_create( &thr, 1, my_task, (void *)&r1binds[i] );
292e670fd5cSchristos }
293e670fd5cSchristos
294e670fd5cSchristos for (;;) {
295e670fd5cSchristos tv.tv_sec = interval;
296e670fd5cSchristos tv.tv_usec = 0;
297e670fd5cSchristos
298e670fd5cSchristos select(0, NULL, NULL, NULL, &tv);
299e670fd5cSchristos
300e670fd5cSchristos now = time(0L);
301e670fd5cSchristos
302e670fd5cSchristos dt = now - prevt;
303e670fd5cSchristos prevt = now;
304e670fd5cSchristos
305e670fd5cSchristos dr10 = 0;
306e670fd5cSchristos dr20 = 0;
307e670fd5cSchristos
308e670fd5cSchristos for ( i = 0; i < threads; i++ ) {
309e670fd5cSchristos r1new = r1binds[i];
310e670fd5cSchristos r2new = r2binds[i];
311e670fd5cSchristos
312e670fd5cSchristos dr1 = r1new - r1old[i];
313e670fd5cSchristos dr2 = r2new - r2old[i];
314e670fd5cSchristos rr1 = dr1 / dt;
315e670fd5cSchristos rr2 = dr2 / dt;
316e670fd5cSchristos
317e670fd5cSchristos printf("%s(%d)\t%d\t%d\t%d\t%d\t%d\t%d\n",
318e670fd5cSchristos hname, i, dt, dr1, dr2, rr1, rr2, rr1 + rr2);
319e670fd5cSchristos
320e670fd5cSchristos dr10 += dr1;
321e670fd5cSchristos dr20 += dr2;
322e670fd5cSchristos
323e670fd5cSchristos r1old[i] = r1new;
324e670fd5cSchristos r2old[i] = r2new;
325e670fd5cSchristos }
326e670fd5cSchristos if ( i > 1 ) {
327e670fd5cSchristos rr1 = dr10 / dt;
328e670fd5cSchristos rr2 = dr20 / dt;
329e670fd5cSchristos
330e670fd5cSchristos printf("%s(sum)\t%d\t%d\t%d\t%d\t%d\t%d\n",
331e670fd5cSchristos hname, 0, dr10, dr20, rr1, rr2, rr1 + rr2);
332e670fd5cSchristos }
333e670fd5cSchristos
334e670fd5cSchristos if ( now - start >= tdur ) {
335e670fd5cSchristos finish = 1;
336e670fd5cSchristos break;
337e670fd5cSchristos }
338e670fd5cSchristos }
339e670fd5cSchristos return 0;
340e670fd5cSchristos }
341