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