summaryrefslogtreecommitdiffstats
path: root/ldap/servers/slapd/tools/ldclt/scalab01.c
blob: 45974e024e46ddd033bdbad2a87498f53fd12b89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
#ident "ldclt @(#)scalab01.c	1.8 01/05/03"

/** BEGIN COPYRIGHT BLOCK
 * This Program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; version 2 of the License.
 * 
 * This Program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA.
 * 
 * In addition, as a special exception, Red Hat, Inc. gives You the additional
 * right to link the code of this Program with code not covered under the GNU
 * General Public License ("Non-GPL Code") and to distribute linked combinations
 * including the two, subject to the limitations in this paragraph. Non-GPL Code
 * permitted under this exception must only link to the code of this Program
 * through those well defined interfaces identified in the file named EXCEPTION
 * found in the source code files (the "Approved Interfaces"). The files of
 * Non-GPL Code may instantiate templates or use macros or inline functions from
 * the Approved Interfaces without causing the resulting work to be covered by
 * the GNU General Public License. Only Red Hat, Inc. may make changes or
 * additions to the list of Approved Interfaces. You must obey the GNU General
 * Public License in all respects for all of the Program code and other code used
 * in conjunction with the Program except the Non-GPL Code covered by this
 * exception. If you modify this file, you may extend this exception to your
 * version of the file, but you are not obligated to do so. If you do not wish to
 * provide this exception without modification, you must delete this exception
 * statement from your version and license this file solely under the GPL without
 * exception. 
 * 
 * 
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 * Copyright (C) 2006 Red Hat, Inc.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif


/*
	FILE :		scalab01.c
	AUTHOR :	Jean-Luc SCHWING
	VERSION :       1.0
	DATE :		08 January 2001
	DESCRIPTION :	
			This file contains the implmentation of the specific
			scenario scalab01 of ldclt.
			I implement this set of functions in a separate file to
			reduce the interconnection(s) between the main ldclt
			and the add-ons, keeping in mind the possibility to use
			a dynamic load of plugins for a future release.
	LOCAL :		None.
	HISTORY :
---------+--------------+------------------------------------------------------
dd/mm/yy | Author	| Comments
---------+--------------+------------------------------------------------------
08/01/01 | JL Schwing	| Creation
---------+--------------+------------------------------------------------------
12/01/01 | JL Schwing	| 1.2 : Second set of options for -e scalab01
---------+--------------+------------------------------------------------------
29/01/01 | B Kolics	| 1.3 : readAttrValue() uses filter of requested attr
---------+--------------+------------------------------------------------------
01/02/01 | JL Schwing	| 1.4 : Protect against multiple choice of same user.
---------+--------------+------------------------------------------------------
26/02/01 | JL Schwing	| 1.5 : Port on non-solaris platforms...
---------+--------------+------------------------------------------------------
14/03/01 | JL Schwing	| 1.6 : Lint cleanup.
			| Bug fix : forget to set ldap protocol version.
---------+--------------+------------------------------------------------------
26/04/01 | B Kolics     | 1.7 : in case of lock failure, thread is not aborted
---------+--------------+------------------------------------------------------
03/05/01 | B Kolics     | 1.8 : bug fix - forget to release more line.
---------+--------------+------------------------------------------------------
*/



#include <stdio.h>	/* printf(), etc... */
#include <stdlib.h>	/* malloc(), etc... */
#include <string.h>	/* strcpy(), etc... */
#include <errno.h>	/* perror(), etc... */
#ifndef _WIN32
#include <pthread.h>	/* pthreads(), etc... */
#endif

#include <lber.h>	/* ldap C-API BER declarations */
#include <ldap.h>	/* ldap C-API declarations */
#include <ldap_ssl.h>	/* ldapssl_init(), etc... */

#include "port.h"	/* Portability definitions */
#include "ldclt.h"	/* This tool's include file */
#include "utils.h"	/* Utilities functions */
#include "scalab01.h"	/* Scalab01 specific definitions */





/*
 * Private data structures.
 */
scalab01_context	 s1ctx;





/* ****************************************************************************
	FUNCTION :	scalab01_init
	PURPOSE :	Initiates the scalab01 scenario.
	INPUT :		None.
	OUTPUT :	None.
	RETURN :	-1 if error, 0 else.
	DESCRIPTION :
 *****************************************************************************/
int
scalab01_init (void)
{
  int	 ret;		/* Return value */

  s1ctx.nbcnx      = 0;		/* No connection yet */
  s1ctx.list       = NULL;	/* No record yet */
  s1ctx.lockingMax = 0;		/* No locking yet */

  /*
   * Initiates mutexes
   */
  if ((ret = ldclt_mutex_init (&(s1ctx.list_mutex))) != 0)
  {
    fprintf (stderr, "ldclt[%d]: %s\n", mctx.pid, strerror (ret));
    fprintf (stderr, "ldclt[%d]: Error: cannot initiate s1ctx.list_mutex\n", mctx.pid);
    fflush (stderr);
    return (-1);
  }
  if ((ret = ldclt_mutex_init (&(s1ctx.locking_mutex))) != 0)
  {
    fprintf (stderr, "ldclt[%d]: %s\n", mctx.pid, strerror (ret));
    fprintf (stderr, "ldclt[%d]: Error: cannot initiate s1ctx.locking_mutex\n", mctx.pid);
    fflush (stderr);
    return (-1);
  }
  if ((ret = ldclt_mutex_init (&(s1ctx.nbcnx_mutex))) != 0)
  {
    fprintf (stderr, "ldclt[%d]: %s\n", mctx.pid, strerror (ret));
    fprintf (stderr, "ldclt[%d]: Error: cannot initiate s1ctx.nbcnx_mutex\n", mctx.pid);
    fflush (stderr);
    return (-1);
  }

  /*
   * No error
   */
  return (0);
}







/* ****************************************************************************
	FUNCTION :	scalab01Lock
	PURPOSE :	Lock for single user trying to connect.
	INPUT :		tttctx	= thread context.
	OUTPUT :	None.
	RETURN :	-1 if error, 0 cannot lock, 1 if could lock.
	DESCRIPTION :
 *****************************************************************************/
int
scalab01Lock (
	thread_context	*tttctx)
{
  int	 i;	/* For the loop */
  int	 ret;	/* Return code */
  int	 res;	/* Result of this function */

  /*
   * Get secure access to the common data structure.
   */
  if ((ret = ldclt_mutex_lock (&(s1ctx.locking_mutex))) != 0)
  {
    fprintf (stderr, "ldclt[%d]: %s: cannot mutex_lock(), error=%d (%s)\n",
		mctx.pid, tttctx->thrdId, ret, strerror (ret));
    fflush (stderr);
    return (-1);
  }

  /*
   * Is it locked ?
   */
  res = 1;
  for (i=0 ; i<s1ctx.lockingMax ; i++)
    if ((s1ctx.locking[i] != NULL) && 
	(!strcmp (s1ctx.locking[i], tttctx->bufBindDN)))
    {
      res = 0;
      break;
    }
  if (res == 1)
  {
    for (i=0 ; (i<s1ctx.lockingMax) && (s1ctx.locking[i] != NULL) ; i++);
    if (i == s1ctx.lockingMax)
    {
      if (s1ctx.lockingMax == SCALAB01_MAX_LOCKING)
	res = 0;
      else
	s1ctx.lockingMax++;
    }
    if (res != 0)
      s1ctx.locking[i] = tttctx->bufBindDN;
  }

  /*
   * Free mutex
   */
  if ((ret = ldclt_mutex_unlock (&(s1ctx.locking_mutex))) != 0)
  {
    fprintf (stderr, "ldclt[%d]: %s: cannot mutex_unlock(), error=%d (%s)\n",
			mctx.pid, tttctx->thrdId, ret, strerror (ret));
    fflush (stderr);
    return (-1);
  }

  return (res);
}








/* ****************************************************************************
	FUNCTION :	scalab01Unlock
	PURPOSE :	Unlock for single user trying to connect.
	INPUT :		tttctx	= thread context.
	OUTPUT :	None.
	RETURN :	-1 if error, 0 else.
	DESCRIPTION :
 *****************************************************************************/
int
scalab01Unlock (
	thread_context	*tttctx)
{
  int	 i;	/* For the loop */
  int	 ret;	/* Return code */

  /*
   * Get secure access to the common data structure.
   */
  if ((ret = ldclt_mutex_lock (&(s1ctx.locking_mutex))) != 0)
  {
    fprintf (stderr, "ldclt[%d]: %s: cannot mutex_lock(), error=%d (%s)\n",
		mctx.pid, tttctx->thrdId, ret, strerror (ret));
    fflush (stderr);
    return (-1);
  }

  /*
   * Find the entry and unlock it.
   */
  for (i=0 ; i<s1ctx.lockingMax ; i++)
    if ((s1ctx.locking[i] != NULL) && 
	(!strcmp (s1ctx.locking[i], tttctx->bufBindDN)))
    {
      s1ctx.locking[i] = NULL;
      break;
    }

  /*
   * Free mutex
   */
  if ((ret = ldclt_mutex_unlock (&(s1ctx.locking_mutex))) != 0)
  {
    fprintf (stderr, "ldclt[%d]: %s: cannot mutex_unlock(), error=%d (%s)\n",
			mctx.pid, tttctx->thrdId, ret, strerror (ret));
    fflush (stderr);
    return (-1);
  }

  return (0);
}









/* ****************************************************************************
	FUNCTION :	scalab01_modemIncr
	PURPOSE :	Increments the modem nb of cnx
	INPUT :		ident	= thread identifier
	OUTPUT :	None.
	RETURN :	-1 if error
			 0 if no modem available
			 1 if modem available
	DESCRIPTION :
 *****************************************************************************/
int
scalab01_modemIncr (
	char	*ident)
{
  int	 ret;	/* Return value */
  int	 res;	/* Result of this function */

  /*
   * Get secure access to the common data structure.
   */
  if ((ret = ldclt_mutex_lock (&(s1ctx.nbcnx_mutex))) != 0)
  {
    fprintf (stderr, "ldclt[%d]: %s: cannot mutex_lock(), error=%d (%s)\n",
		mctx.pid, ident, ret, strerror (ret));
    fflush (stderr);
    return (-1);
  }

  if (s1ctx.nbcnx >= s1ctx.maxcnxnb)
    res = 0;
  else
  {
    res = 1;
    s1ctx.nbcnx++;
  }

  /*
   * Free mutex
   */
  if ((ret = ldclt_mutex_unlock (&(s1ctx.nbcnx_mutex))) != 0)
  {
    fprintf (stderr, "ldclt[%d]: %s: cannot mutex_unlock(), error=%d (%s)\n",
			mctx.pid, ident, ret, strerror (ret));
    fflush (stderr);
    return (-1);
  }

  return (res);
}


/* ****************************************************************************
	FUNCTION :	scalab01_modemDecr
	PURPOSE :	Decrements the modem nb of cnx
	INPUT :		ident	= thread identifier
	OUTPUT :	None.
	RETURN :	-1 if error, 0 else.
	DESCRIPTION :
 *****************************************************************************/
int
scalab01_modemDecr (
	char	*ident)
{
  int	 ret;	/* Return value */

  /*
   * Get secure access to the common data structure.
   */
  if ((ret = ldclt_mutex_lock (&(s1ctx.nbcnx_mutex))) != 0)
  {
    fprintf (stderr, "ldclt[%d]: %s: cannot mutex_lock(), error=%d (%s)\n",
		mctx.pid, ident, ret, strerror (ret));
    fflush (stderr);
    return (-1);
  }

  s1ctx.nbcnx--;

  /*
   * Free mutex
   */
  if ((ret = ldclt_mutex_unlock (&(s1ctx.nbcnx_mutex))) != 0)
  {
    fprintf (stderr, "ldclt[%d]: %s: cannot mutex_unlock(), error=%d (%s)\n",
			mctx.pid, ident, ret, strerror (ret));
    fflush (stderr);
    return (-1);
  }

  return (0);
}







/* ****************************************************************************
	FUNCTION :	scalab01_addLogin
	PURPOSE :	Add a new user login to the s1ctx structure.
	INPUT :		tttctx	 = thread context.
			dn	 = user's dn.
			duration = duration of the connection.
	OUTPUT :	None.
	RETURN :	-1 if error, 0 else.
	DESCRIPTION :
 *****************************************************************************/
int
scalab01_addLogin (
	thread_context	*tttctx,
	char		*dn,
	int		 duration)
{
  int		 ret;	/* Return value */
  isp_user	*new;	/* New entry */
  isp_user	*cur;	/* Current entry */

  /*
   * Create the new record.
   */
  new = (isp_user *) malloc (sizeof (isp_user));
  strcpy (new->dn, dn);
  new->cost = new->counter = duration;
  new->next = NULL;

  /*
   * Get secure access to the common data structure.
   * Note : it should be possible to reduce the "size" of this critical
   *        section but I am not 100% certain this won't mess up all things.
   */
  if ((ret = ldclt_mutex_lock (&(s1ctx.list_mutex))) != 0)
  {
    fprintf (stderr, "ldclt[%d]: %s: cannot mutex_lock(), error=%d (%s)\n",
		mctx.pid, tttctx->thrdId, ret, strerror (ret));
    fflush (stderr);
    return (-1);
  }

  /*
   * Maybe this is the first entry of the list ?
   */
  if (s1ctx.list == NULL)
    s1ctx.list = new;
  else
  {
    /*
     * Check with the list's head
     */
    if (s1ctx.list->counter >= duration)
    {
      new->next  = s1ctx.list;
      s1ctx.list = new;
    }
    else
    {
      cur = s1ctx.list;
      while (cur != NULL)
      {
	if (cur->next == NULL)
	{
	  cur->next = new;
	  cur       = NULL; /* Exit loop */
	}
	else
	  if (cur->next->counter >= duration)
	  {
	    new->next = cur->next;
	    cur->next = new;
	    cur       = NULL; /* Exit loop */
	  }
	  else
	    cur = cur->next;
      }
    }
  }

  /*
   * Free mutex
   */
  if ((ret = ldclt_mutex_unlock (&(s1ctx.list_mutex))) != 0)
  {
    fprintf (stderr, "ldclt[%d]: %s: cannot mutex_unlock(), error=%d (%s)\n",
			mctx.pid, tttctx->thrdId, ret, strerror (ret));
    fflush (stderr);
    return (-1);
  }

  return (0);
}









/* ****************************************************************************
	FUNCTION :	scalab01_connectSuperuser
	PURPOSE :	Purpose of the fct
	INPUT :		None.
	OUTPUT :	None.
	RETURN :	-1 if error, 0 else.
	DESCRIPTION :
 *****************************************************************************/
int
scalab01_connectSuperuser (void)
{
  int	 ret;				/* Return value */
  int	 v2v3;				/* LDAP version used */
  char	 bindDN [MAX_DN_LENGTH];	/* To bind */

  /*
   * Create the LDAP context
   */
  /*
   * SSL is enabled ?
   */
  if (mctx.mode & SSL)
  {
    /*
     * LDAP session initialization in SSL mode
     */
    s1ctx.ldapCtx = ldapssl_init(mctx.hostname, mctx.port, 1);
    if (mctx.mode & VERY_VERBOSE)
      printf ("ldclt[%d]: ctrl: ldapssl_init (%s, %d), ldapCtx=0x%08x\n",
			mctx.pid, mctx.hostname, mctx.port, (unsigned int)s1ctx.ldapCtx);
    if (s1ctx.ldapCtx == NULL)
    {
      printf ("ldclt[%d]: ctrl: Cannot ldapssl_init (%s, %d), errno=%d\n",
			mctx.pid, mctx.hostname, mctx.port, errno);
      fflush (stdout);
      return (-1);
    }
    /*
     * Client authentication is used ?
     */
    if (mctx.mode & CLTAUTH)
    {
      ret = ldapssl_enable_clientauth(s1ctx.ldapCtx, "", mctx.keydbpin, mctx.cltcertname);
      if (mctx.mode & VERY_VERBOSE)
	printf
	    ("ldclt[%d]: ctrl: After ldapssl_enable_clientauth (ldapCtx=0x%08x, %s, %s)",
	     mctx.pid, (unsigned int)s1ctx.ldapCtx, mctx.keydbpin, mctx.cltcertname);
      if (ret < 0)
      {
	printf
	    ("ldclt[%d]: ctrl: Cannot ldapssl_enable_clientauth (ldapCtx=0x%08x, %s, %s)",
	     mctx.pid, (unsigned int)s1ctx.ldapCtx, mctx.keydbpin, mctx.cltcertname);
	ldap_perror(s1ctx.ldapCtx, "ldapssl_enable_clientauth");
	fflush (stdout);
	return (-1);
      }
    }
  }
  else
  {
    /*
     * Connection initialization in normal, unencrypted mode
     */
    s1ctx.ldapCtx = ldap_init (mctx.hostname, mctx.port);
    if (mctx.mode & VERY_VERBOSE)
      printf ("ldclt[%d]: ctrl: After ldap_init (%s, %d), ldapCtx=0x%08x\n",
		mctx.pid, mctx.hostname, mctx.port, (unsigned int)s1ctx.ldapCtx);
    if (s1ctx.ldapCtx == NULL)
    {
      printf ("ldclt[%d]: ctrl: Cannot ldap_init (%s, %d), errno=%d\n",
		mctx.pid, mctx.hostname, mctx.port, errno);
      fflush (stdout);
      return (-1);
    }
  }

  /*
   * Set the LDAP version and other options...
   */
  if (mctx.mode & LDAP_V2)
    v2v3 = LDAP_VERSION2;
  else
    v2v3 = LDAP_VERSION3;

  ret = ldap_set_option (s1ctx.ldapCtx, LDAP_OPT_PROTOCOL_VERSION, &v2v3);
  if (ret < 0)							/*JLS 14-03-01*/
  {								/*JLS 14-03-01*/
    printf ("ldclt[%d]: ctrl: Cannot ldap_set_option(LDAP_OPT_PROTOCOL_VERSION)\n",
             mctx.pid);
    fflush (stdout);						/*JLS 14-03-01*/
    return (-1);						/*JLS 14-03-01*/
  }								/*JLS 14-03-01*/


  /*
   * Now we could bind
   */
  /*
   * for SSL client authentication, SASL BIND is used
   */
  if (mctx.mode & CLTAUTH)
  {
    if (mctx.mode & VERY_VERBOSE)
      printf ("ldclt[%d]: ctrl: Before ldap_sasl_bind_s\n", mctx.pid);
    ret = ldap_sasl_bind_s (s1ctx.ldapCtx, "", "EXTERNAL", NULL, NULL, NULL,
			    NULL);
    if (mctx.mode & VERY_VERBOSE)
      printf ("ldclt[%d]: ctrl: After ldap_sasl_bind_s\n", mctx.pid);
    if (ret != LDAP_SUCCESS)
    {
      printf ("ldclt[%d]: ctrl: Cannot ldap_sasl_bind_s, error=%d (%s)\n",
		mctx.pid, ret, my_ldap_err2string (ret));
      fflush (stdout);
      return (-1);
    }
  }
  else
  {
    strcpy (bindDN, SCALAB01_SUPER_USER_RDN);
    strcat (bindDN, ",");
    strcat (bindDN, mctx.baseDN);
    if (mctx.mode & VERY_VERBOSE)
      printf ("ldclt[%d]: ctrl: Before ldap_simple_bind_s (%s , %s)\n",
		mctx.pid, bindDN, SCALAB01_SUPER_USER_PASSWORD);
    ret = ldap_simple_bind_s (s1ctx.ldapCtx,
		bindDN, SCALAB01_SUPER_USER_PASSWORD);
    if (mctx.mode & VERY_VERBOSE)
      printf ("ldclt[%d]: ctrl: After ldap_simple_bind_s (%s, %s)\n",
		mctx.pid, bindDN, SCALAB01_SUPER_USER_PASSWORD);
    if (ret != LDAP_SUCCESS)
    {
      printf("ldclt[%d]: ctrl: Cannot ldap_simple_bind_s (%s, %s), error=%d (%s)\n",
		mctx.pid, bindDN, SCALAB01_SUPER_USER_PASSWORD,
		ret, my_ldap_err2string (ret));
      fflush (stdout);
      return (-1);
    }
  }

  /*
   * Normal end...
   */
  return (0);
}








/* ****************************************************************************
	FUNCTION :	readAttrValue
	PURPOSE :	This function will ldap_search the given entry for the
			value of the given attribute.
	INPUT :		ident	= thread identifier
			ldapCtx	= LDAP context
			dn	= dn of the entry to process
			attname	= attribute name
	OUTPUT :	value	= attribute value. This buffer must be
				  initiated with enough memory.
				  value[0] == '\0' if not find.
	RETURN :	-1 if error, 0 else.
	DESCRIPTION :
 *****************************************************************************/
int
readAttrValue (
	LDAP	*ldapCtx,
	char	*ident,
	char	*dn,
	char	*attname,
	char	*value)
{
  int		  ret;		/* Return value */
  char		 *attrs[2];	/* Attribute to retrieve */
  LDAPMessage	 *res;		/* LDAP responses */
  LDAPMessage	 *cur;		/* Current message */
  BerElement	 *ber;		/* To decode the response */
  char		 *aname;	/* Current attribute name */
  char		**vals;		/* Attribute value returned */
  char		 *filter;	/* Filter used for searching */

  /*
   * First, ldap_search() the entry.
   */
  attrs[0] = attname;
  attrs[1] = NULL;
  filter = (char *)malloc((4+strlen(attname))*sizeof(char));
  sprintf(filter, "(%s=*)", attname);
  ret = ldap_search_s (ldapCtx, dn, LDAP_SCOPE_BASE,
	filter, attrs, 0, &res);
  if (filter != NULL) free(filter);
  if (ret != LDAP_SUCCESS)
  {
    printf ("ldclt[%d]: %s: Cannot ldap_search (%s in %s), error=%d (%s)\n",
		mctx.pid, ident, attname, dn, ret, my_ldap_err2string (ret));
    fflush (stdout);
    return (-1);
  }

  /*
   * Decode the response
   */
  value[0] = '\0';	/* Not find yet */
  cur  = ldap_first_entry (ldapCtx, res);
  while ((!value[0]) && (cur != NULL))
  {
    aname = ldap_first_attribute (ldapCtx, cur, &ber);
    while ((!value[0]) && (aname != NULL))
    {
      /*
       * We expect this attribute to be single-valued.
       */
      if (!strcmp (aname, attname))
      {
	vals = ldap_get_values (ldapCtx, cur, aname);
	if (vals == NULL)
	{
	  printf ("ldclt[%d]: %s: no value for %s in %s\n",
                   mctx.pid, ident, dn, attname);
	  fflush (stdout);
	  return (-1);
	}
	strcpy (value, vals[0]);
	ldap_value_free (vals);
      }

      /*
       * Next attribute
       */
      ldap_memfree (aname);
      if (!value[0])
	aname = ldap_next_attribute (ldapCtx, cur, ber);
    }

    /*
     * Next entry - shouldn't happen in theory
     */
    if (ber != NULL)
      ldap_ber_free (ber, 0);
    cur = ldap_next_entry (ldapCtx, cur);
  }
  ldap_msgfree (res);	/* Free the response */

  return (0);
}







/* ****************************************************************************
	FUNCTION :	writeAttrValue
	PURPOSE :	This function will ldap_modify the given entry to
			replace the value of the given attribute.
	INPUT :		ident	= thread identifier
			ldapCtx	= LDAP context
			dn	= dn of the entry to process
			attname	= attribute name
			value	= attribute value
	OUTPUT :	None.
	RETURN :	-1 if error, 0 else.
	DESCRIPTION :
 *****************************************************************************/
int
writeAttrValue (
	LDAP	*ldapCtx,
	char	*ident,
	char	*dn,
	char	*attname,
	char	*value)
{
  int		 ret;		/* Return value */
  LDAPMod	 attribute;	/* To build the attributes */
  LDAPMod	*attrsmod[2];	/* Modify attributes */
  char		*pvalues[2];	/* To build the values list */

  /*
   * Prepear the data to be written
   */
  pvalues[0]           = value;
  pvalues[1]           = NULL;
  attribute.mod_op     = LDAP_MOD_REPLACE;
  attribute.mod_type   = attname;
  attribute.mod_values = pvalues;
  attrsmod[0]          = &attribute;
  attrsmod[1]          = NULL;

  /*
   * Store the data in the directory.
   */
  ret = ldap_modify_ext_s (ldapCtx, dn, attrsmod, NULL, NULL);
  if (ret != LDAP_SUCCESS)
  {
    printf ("ldclt[%d]: %s: Cannot ldap_modify_ext_s (%s in %s), error=%d (%s)\n",
			mctx.pid, ident, attname, dn, ret, my_ldap_err2string (ret));
    fflush (stdout);
    return (-1);
  }

  return (0);
}







/* ****************************************************************************
	FUNCTION :	scalab01_unlock
	PURPOSE :	Unlock the user given in argument.
	INPUT :		entry	= entry to unlock.
	OUTPUT :	None.
	RETURN :	-1 if error, 0 else.
	DESCRIPTION :
 *****************************************************************************/
int
scalab01_unlock (
	isp_user	*user)
{
  int	 account;	/* Accounting value */
  char	 buf[20];	/* To read/write attribute */

  /*
   * Increment accounting counters
   * First, read the current value.
   */
  if (readAttrValue (s1ctx.ldapCtx,"ctrl",user->dn,SCALAB01_ACC_ATTRIB,buf) < 0)
  {
    printf ("ldclt[%d]: ctrl: Cannot read accounting attribute of %s\n",
             mctx.pid, user->dn);
    fflush (stdout);
    return (-1);
  }

  /*
   * If this attribute has no value (doesn't exist) we assume it is 0.
   */
  if (buf[0] != '\0')
    account = atoi (buf);
  else
  {
    printf ("ldclt[%d]: ctrl: No accounting attribute for %s - assume it is 0\n",
             mctx.pid, user->dn);
    fflush (stdout);
    account = 0;
  }

  /*
   * Compute the new value and store it in the directory.
   */
  sprintf (buf, "%d", account + user->cost);
  if (writeAttrValue (s1ctx.ldapCtx,"ctrl",user->dn,SCALAB01_ACC_ATTRIB,buf) <0)
  {
    printf ("ldclt[%d]: ctrl: Cannot write accounting attribute of %s\n",
             mctx.pid, user->dn);
    fflush (stdout);
    return (-1);
  }

  /*
   * Unlock the user
   */
  if (writeAttrValue (s1ctx.ldapCtx, "ctrl", user->dn, 
			SCALAB01_LOCK_ATTRIB, SCALAB01_VAL_UNLOCKED) < 0)
  {
    printf ("ldclt[%d]: ctrl: Cannot write lock (unlock) attribute of %s\n",
             mctx.pid, user->dn);
    fflush (stdout);
    return (-1);
  }
  if (mctx.mode & VERY_VERBOSE)
    printf ("ldclt[%d]: ctrl: entry %s unlocked\n",
             mctx.pid, user->dn);

  /*
   * Decrement modem pool usage...
   */
  if (scalab01_modemDecr ("ctrl") < 0)
    return (-1);

  /*
   * Normal end
   */
  return (0);
}








/* ****************************************************************************
	FUNCTION :	scalab01_control
	PURPOSE :	This function implements the control loop/thread of
			the scalab01 scenario. Its main target is to manage
			the counters of each "connection" and to unlock the
			entry when time is reached.
	INPUT :		None.
	OUTPUT :	None.
	RETURN :	-1 if error, 0 else.
	DESCRIPTION :
 *****************************************************************************/
void *
scalab01_control (
	void	*arg)
{
  isp_user	*cur;	/* Current entry */
  isp_user	*head;	/* Head of entries to process */
  int		 ret;	/* Return value */
  int		 nbTot;	/* Total nb entries locked */
  int		 nbU;	/* Number unlocked */

  /*
   * Initialization
   * Failure to connect is a critical error...
   */
  if (scalab01_connectSuperuser () < 0)
    ldcltExit (EXIT_NOBIND);

  /*
   * Main loop
   */
  while (1 /*CONSTCOND*/)					/*JLS 14-03-01*/
  {
    ldclt_sleep (1);	/* Poll the connections every second */
    nbTot = nbU = 0;	/* No entries processed yet */

    /*
     * Get protected access to the entries
     */
    if ((ret = ldclt_mutex_lock (&(s1ctx.list_mutex))) != 0)
    {
      fprintf (stderr, "ldclt[%d]: ctrl: cannot mutex_lock(), error=%d (%s)\n",
		mctx.pid, ret, strerror (ret));
      fflush (stderr);
      ldcltExit (EXIT_OTHER);
    }

    /*
     * Decrement all counters
     */
    for (cur=s1ctx.list ; cur!=NULL ; cur=cur->next)
    {
      cur->counter--;
      nbTot++;
    }

    /*
     * Find the entries to process.
     */
    if ((s1ctx.list == NULL) || (s1ctx.list->counter > 0))
      head = NULL;
    else
    {
      head = cur = s1ctx.list;
      while ((cur != NULL) && (cur->counter == 0))
	cur = cur->next;
      s1ctx.list = cur;
    }

    /*
     * Release mutex
     */
    if ((ret = ldclt_mutex_unlock (&(s1ctx.list_mutex))) != 0)
    {
      fprintf (stderr, "ldclt[%d]: ctrl: cannot mutex_unlock(), error=%d (%s)\n",
		mctx.pid, ret, strerror (ret));
      fflush (stderr);
      ldcltExit (EXIT_OTHER);
    }

    /*
     * Now, we have "head" that points either to NULL or to a list of
     * entries to process.
     * Attention, this list of entries is not terminated by NULL, but
     * we must rather check the field head->next->counter" for the last
     * entry...
     *
     * NOTE : implements this section as a separate thread to keep the
     *        general timer working...
     */
    while (head != NULL)
    {
      if (scalab01_unlock (head) < 0)
      {
	printf ("ldclt[%d]: ctrl: cannot unlock %s\n", mctx.pid, head->dn);
	ldcltExit (EXIT_OTHER);
      }
      nbU++;	/* One more entry unlocked */

      /*
       * Next entry...
       */
      cur = head;
      if (head->next == NULL)
	head = NULL;
      else
	if (head->next->counter != 0)
	  head = NULL;
	else
	  head = head->next;

      free (cur);
    } /* while (head =! NULL) */

    /*
     * Print some stats...
     */
    if (mctx.mode & VERBOSE)
      printf ("ldclt[%d]: ctrl: nb entries unlocked / total : %3d / %5d\n",
               mctx.pid, nbU, nbTot);
  } /* Main loop */

  /*
   * End of thread
   */
  return (arg);
}







/* ****************************************************************************
	FUNCTION :	doScalab01
	PURPOSE :	Implements the client part of the scalab01 scenario.
	INPUT :		tttctx	= this thread context
	OUTPUT :	None.
	RETURN :	-1 if error, 0 else.
	DESCRIPTION :
 *****************************************************************************/
int
doScalab01 (
	thread_context	*tttctx)
{
  char	 buf[32];	/* To read attributes value */
  int	 duration;	/* Use a variable for trace purpose */
  int	 res;		/* Result of cnx to modem pool */
  int	 doloop;	/* To know if we should loop */

  /*
   * Simulate connection to the modem pool.
   */
  while ((res = scalab01_modemIncr(tttctx->thrdId)) != 1)
    switch (res)
    {
      case 0:
	ldclt_sleep (s1ctx.wait==0?SCALAB01_DEF_WAIT_TIME:rndlim(0,s1ctx.wait));
	break;
      case -1:
	return (-1);
	break;
    }

  /*
   * Connection to the server
   * The function connectToServer() will take care of the various connection/
   * disconnection, bind/unbind/close etc... requested by the user.
   * The cost is one more function call in this application, but the
   * resulting source code will be much more easiest to maintain.
   */
  if (connectToServer (tttctx) < 0)
    return (-1);
  if (!(tttctx->binded))
    return (0);

  /*
   * Check that no other thread is using the same identity...
   */
  doloop = 1;
  while (doloop)
  {
    switch (scalab01Lock (tttctx))
    {
      case 0:
	ldclt_sleep (1);
	break;
      case 1:
	doloop = 0;
	break;
      case -1:
	return (-1);
	break;
    }
  }

  /*
   * Ok, we are now binded. Great ;-)
   * The DN we used to bind is available in tttctx->bufBindDN
   * Read lock attribute
   */
  if (readAttrValue (tttctx->ldapCtx, tttctx->thrdId, tttctx->bufBindDN,
		SCALAB01_LOCK_ATTRIB, buf) < 0)
  {
    printf ("ldclt[%d]: %s: Cannot read lock attribute of %s\n", 
				mctx.pid, tttctx->thrdId, tttctx->bufBindDN);
    fflush (stdout);
    (void) scalab01_modemDecr (tttctx->thrdId);
    return (-1);
  }
  if (mctx.mode & VERY_VERBOSE)
    printf ("ldclt[%d]: %s: entry %s lock read\n",
             mctx.pid, tttctx->thrdId, tttctx->bufBindDN);

  /*
   * If locked, then we cannot login now...
   */
  if (!strcmp (buf, SCALAB01_VAL_LOCKED))
  {
    if (scalab01_modemDecr (tttctx->thrdId) < 0)
      return (-1);
    return (0);
  }

  /*
   * If not locked :
   *  - lock the user
   *  - decide how many times will be connected
   *  - add information to the list of connected
   */
  if (writeAttrValue (tttctx->ldapCtx, tttctx->thrdId, tttctx->bufBindDN,
		SCALAB01_LOCK_ATTRIB, SCALAB01_VAL_LOCKED) < 0)
  {
    printf ("ldclt[%d]: %s: Cannot write lock attribute of %s\n", 
				mctx.pid, tttctx->thrdId, tttctx->bufBindDN);
    fflush (stdout);
    /*
     * It can still happen that two threads write this attribute at the same
     * time, so there can be failure in one of the threads
     * in this case just return
     */
    if (scalab01_modemDecr (tttctx->thrdId) < 0)		/*JLS 03-05-01*/
      return (-1);						/*JLS 03-05-01*/
    return (0);							/*BK  26-04-01*/
  }
  if (mctx.mode & VERY_VERBOSE)
    printf ("ldclt[%d]: %s: entry %s lock written\n",
             mctx.pid, tttctx->thrdId, tttctx->bufBindDN);

  if (scalab01Unlock (tttctx) < 0)
    return (-1);

  duration = rndlim (1, s1ctx.cnxduration);
  if (scalab01_addLogin (tttctx, tttctx->bufBindDN, duration) < 0)
  {
    printf ("ldclt[%d]: %s: Cannot memorize new login of %s\n", 
				mctx.pid, tttctx->thrdId, tttctx->bufBindDN);
    fflush (stdout);
    return (-1);
  }
  if (mctx.mode & VERY_VERBOSE)
    printf ("ldclt[%d]: %s: entry %s login added duration %6d\n",
		mctx.pid, tttctx->thrdId, tttctx->bufBindDN, duration);

  /*
   * Memorize the operation
   */
  if (incrementNbOpers (tttctx) < 0)
    return (-1);

  /*
   * Wait before next operation...
   */
  if (s1ctx.wait > 0)
    ldclt_sleep (rndlim (0,s1ctx.wait));

  /*
   * Unbind
   */
/*
TBC - this is done in the next loop... - cf connectToServer()
*/

  return (0);
}






/* End of file */