| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217 | 
							- #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 */
 
- #if !defined(USE_OPENLDAP)
 
- #include <ldap_ssl.h>	/* ldapssl_init(), etc... */
 
- #endif
 
- #include <prprf.h>
 
- #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 */
 
-   int		 rc = 0;
 
-   /*
 
-    * Create the new record.
 
-    */
 
-   new = (isp_user *) malloc (sizeof (isp_user));
 
-   if (NULL == new) {
 
-     fprintf (stderr, "ldclt[%d]: %s: cannot malloc(isp_user), error=%d (%s)\n",
 
-       mctx.pid, tttctx->thrdId, errno, strerror (errno));
 
-     fflush (stderr);
 
-     return -1;
 
-   }
 
-   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);
 
-     rc = -1;
 
-     goto error;
 
-   }
 
-   /*
 
-    * 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;
 
-       }
 
-     }
 
-   }
 
-   goto done;
 
- error:
 
-   if (new) free(new);
 
- done:
 
-   /*
 
-    * 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);
 
-     rc = -1;
 
-   }
 
-   return rc;
 
- }
 
- /* ****************************************************************************
 
- 	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] = {0};	/* To bind */
 
-   const char *mech = LDAP_SASL_SIMPLE;
 
-   struct berval cred = {0, NULL};
 
-   struct berval *servercredp = NULL;
 
- #if defined(USE_OPENLDAP)
 
-   char *ldapurl = NULL;
 
- #endif
 
- #if defined(USE_OPENLDAP)
 
-   ldapurl = PR_smprintf("ldap%s://%s:%d/",
 
- 			(mctx.mode & SSL) ? "s" : "",
 
- 			mctx.hostname, mctx.port);
 
-   if ((ret = ldap_initialize(&s1ctx.ldapCtx, ldapurl))) {
 
-     printf ("ldclt[%d]: ctrl: Cannot ldap_initialize (%s), errno=%d ldaperror=%d:%s\n",
 
- 	    mctx.pid, ldapurl, errno, ret, my_ldap_err2string(ret));
 
-     fflush (stdout);
 
-     PR_smprintf_free(ldapurl);
 
-     return (-1);
 
-   }
 
-   PR_smprintf_free(ldapurl);
 
-   ldapurl = NULL;
 
- #else /* !USE_OPENLDAP */
 
-   /*
 
-    * 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%p\n",
 
- 			mctx.pid, mctx.hostname, mctx.port, 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%p, %s, %s)",
 
- 	     mctx.pid, s1ctx.ldapCtx, mctx.keydbpin, mctx.cltcertname);
 
-       if (ret < 0)
 
-       {
 
- 	printf
 
- 	    ("ldclt[%d]: ctrl: Cannot ldapssl_enable_clientauth (ldapCtx=0x%p, %s, %s)",
 
- 	     mctx.pid, 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%p\n",
 
- 		mctx.pid, mctx.hostname, mctx.port, 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);
 
-     }
 
-   }
 
- #endif /* !USE_OPENLDAP */
 
-   if (mctx.mode & CLTAUTH) {
 
-     mech = "EXTERNAL";
 
-   } else {
 
-     strcpy (bindDN, SCALAB01_SUPER_USER_RDN);
 
-     strcat (bindDN, ",");
 
-     strcat (bindDN, mctx.baseDN);
 
-     cred.bv_val = SCALAB01_SUPER_USER_PASSWORD;
 
-     cred.bv_len = strlen(cred.bv_val);
 
-   }
 
-   /*
 
-    * 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*/
 
-   if (mctx.mode & VERY_VERBOSE)
 
-     printf ("ldclt[%d]: ctrl: Before bind mech %s (%s , %s)\n",
 
- 	    mctx.pid, mech ? mech : "SIMPLE", bindDN, SCALAB01_SUPER_USER_PASSWORD);
 
-   ret = ldap_sasl_bind_s (s1ctx.ldapCtx, bindDN, mech, &cred, NULL, NULL, &servercredp);
 
-   ber_bvfree(servercredp);
 
-   if (mctx.mode & VERY_VERBOSE)
 
-     printf ("ldclt[%d]: ctrl: After bind mech %s (%s, %s)\n",
 
- 	    mctx.pid, mech ? mech : "SIMPLE", bindDN, SCALAB01_SUPER_USER_PASSWORD);
 
-   if (ret != LDAP_SUCCESS)
 
-   {
 
-     printf("ldclt[%d]: ctrl: Cannot bind mech %s (%s, %s), error=%d (%s)\n",
 
- 	   mctx.pid, mech ? mech : "SIMPLE", 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		 *filter;	/* Filter used for searching */
 
-   /*
 
-    * First, ldap_search() the entry.
 
-    */
 
-   attrs[0] = attname;
 
-   attrs[1] = NULL;
 
-   filter = (char *)malloc((4+strlen(attname))*sizeof(char));
 
-   if (NULL == filter)
 
-   {
 
-     printf ("ldclt[%d]: %s: Out of memory\n", mctx.pid, ident);
 
-     fflush (stdout);
 
-     return (-1);
 
-   }
 
-   sprintf(filter, "(%s=*)", attname);
 
-   ret = ldap_search_ext_s (ldapCtx, dn, LDAP_SCOPE_BASE,
 
- 			   filter, attrs, 0, NULL, NULL, NULL, -1, &res);
 
-   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))
 
-       {
 
- 	struct berval **vals;
 
- 	vals = ldap_get_values_len (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);
 
- 	}
 
- 	strncpy (value, vals[0]->bv_val, vals[0]->bv_len);
 
- 	value[vals[0]->bv_len] = '\0';
 
- 	ldap_value_free_len (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)
 
-       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
 
-    */
 
- }
 
- /* ****************************************************************************
 
- 	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 */
 
 
  |