scalab01.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  1. #ident "ldclt @(#)scalab01.c 1.8 01/05/03"
  2. /** BEGIN COPYRIGHT BLOCK
  3. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  4. * Copyright (C) 2006 Red Hat, Inc.
  5. * All rights reserved.
  6. *
  7. * License: GPL (version 3 or any later version).
  8. * See LICENSE for details.
  9. * END COPYRIGHT BLOCK **/
  10. #ifdef HAVE_CONFIG_H
  11. # include <config.h>
  12. #endif
  13. /*
  14. FILE : scalab01.c
  15. AUTHOR : Jean-Luc SCHWING
  16. VERSION : 1.0
  17. DATE : 08 January 2001
  18. DESCRIPTION :
  19. This file contains the implmentation of the specific
  20. scenario scalab01 of ldclt.
  21. I implement this set of functions in a separate file to
  22. reduce the interconnection(s) between the main ldclt
  23. and the add-ons, keeping in mind the possibility to use
  24. a dynamic load of plugins for a future release.
  25. LOCAL : None.
  26. HISTORY :
  27. ---------+--------------+------------------------------------------------------
  28. dd/mm/yy | Author | Comments
  29. ---------+--------------+------------------------------------------------------
  30. 08/01/01 | JL Schwing | Creation
  31. ---------+--------------+------------------------------------------------------
  32. 12/01/01 | JL Schwing | 1.2 : Second set of options for -e scalab01
  33. ---------+--------------+------------------------------------------------------
  34. 29/01/01 | B Kolics | 1.3 : readAttrValue() uses filter of requested attr
  35. ---------+--------------+------------------------------------------------------
  36. 01/02/01 | JL Schwing | 1.4 : Protect against multiple choice of same user.
  37. ---------+--------------+------------------------------------------------------
  38. 26/02/01 | JL Schwing | 1.5 : Port on non-solaris platforms...
  39. ---------+--------------+------------------------------------------------------
  40. 14/03/01 | JL Schwing | 1.6 : Lint cleanup.
  41. | Bug fix : forget to set ldap protocol version.
  42. ---------+--------------+------------------------------------------------------
  43. 26/04/01 | B Kolics | 1.7 : in case of lock failure, thread is not aborted
  44. ---------+--------------+------------------------------------------------------
  45. 03/05/01 | B Kolics | 1.8 : bug fix - forget to release more line.
  46. ---------+--------------+------------------------------------------------------
  47. */
  48. #include <stdio.h> /* printf(), etc... */
  49. #include <stdlib.h> /* malloc(), etc... */
  50. #include <string.h> /* strcpy(), etc... */
  51. #include <errno.h> /* perror(), etc... */
  52. #include <pthread.h> /* pthreads(), etc... */
  53. #include <lber.h> /* ldap C-API BER declarations */
  54. #include <ldap.h> /* ldap C-API declarations */
  55. #if !defined(USE_OPENLDAP)
  56. #include <ldap_ssl.h> /* ldapssl_init(), etc... */
  57. #endif
  58. #include <prprf.h>
  59. #include "port.h" /* Portability definitions */
  60. #include "ldclt.h" /* This tool's include file */
  61. #include "utils.h" /* Utilities functions */
  62. #include "scalab01.h" /* Scalab01 specific definitions */
  63. /*
  64. * Private data structures.
  65. */
  66. scalab01_context s1ctx;
  67. /* ****************************************************************************
  68. FUNCTION : scalab01_init
  69. PURPOSE : Initiates the scalab01 scenario.
  70. INPUT : None.
  71. OUTPUT : None.
  72. RETURN : -1 if error, 0 else.
  73. DESCRIPTION :
  74. *****************************************************************************/
  75. int
  76. scalab01_init (void)
  77. {
  78. int ret; /* Return value */
  79. s1ctx.nbcnx = 0; /* No connection yet */
  80. s1ctx.list = NULL; /* No record yet */
  81. s1ctx.lockingMax = 0; /* No locking yet */
  82. /*
  83. * Initiates mutexes
  84. */
  85. if ((ret = ldclt_mutex_init (&(s1ctx.list_mutex))) != 0)
  86. {
  87. fprintf (stderr, "ldclt[%d]: %s\n", mctx.pid, strerror (ret));
  88. fprintf (stderr, "ldclt[%d]: Error: cannot initiate s1ctx.list_mutex\n", mctx.pid);
  89. fflush (stderr);
  90. return (-1);
  91. }
  92. if ((ret = ldclt_mutex_init (&(s1ctx.locking_mutex))) != 0)
  93. {
  94. fprintf (stderr, "ldclt[%d]: %s\n", mctx.pid, strerror (ret));
  95. fprintf (stderr, "ldclt[%d]: Error: cannot initiate s1ctx.locking_mutex\n", mctx.pid);
  96. fflush (stderr);
  97. return (-1);
  98. }
  99. if ((ret = ldclt_mutex_init (&(s1ctx.nbcnx_mutex))) != 0)
  100. {
  101. fprintf (stderr, "ldclt[%d]: %s\n", mctx.pid, strerror (ret));
  102. fprintf (stderr, "ldclt[%d]: Error: cannot initiate s1ctx.nbcnx_mutex\n", mctx.pid);
  103. fflush (stderr);
  104. return (-1);
  105. }
  106. /*
  107. * No error
  108. */
  109. return (0);
  110. }
  111. /* ****************************************************************************
  112. FUNCTION : scalab01Lock
  113. PURPOSE : Lock for single user trying to connect.
  114. INPUT : tttctx = thread context.
  115. OUTPUT : None.
  116. RETURN : -1 if error, 0 cannot lock, 1 if could lock.
  117. DESCRIPTION :
  118. *****************************************************************************/
  119. int
  120. scalab01Lock (
  121. thread_context *tttctx)
  122. {
  123. int i; /* For the loop */
  124. int ret; /* Return code */
  125. int res; /* Result of this function */
  126. /*
  127. * Get secure access to the common data structure.
  128. */
  129. if ((ret = ldclt_mutex_lock (&(s1ctx.locking_mutex))) != 0)
  130. {
  131. fprintf (stderr, "ldclt[%d]: %s: cannot mutex_lock(), error=%d (%s)\n",
  132. mctx.pid, tttctx->thrdId, ret, strerror (ret));
  133. fflush (stderr);
  134. return (-1);
  135. }
  136. /*
  137. * Is it locked ?
  138. */
  139. res = 1;
  140. for (i=0 ; i<s1ctx.lockingMax ; i++)
  141. if ((s1ctx.locking[i] != NULL) &&
  142. (!strcmp (s1ctx.locking[i], tttctx->bufBindDN)))
  143. {
  144. res = 0;
  145. break;
  146. }
  147. if (res == 1)
  148. {
  149. for (i=0 ; (i<s1ctx.lockingMax) && (s1ctx.locking[i] != NULL) ; i++);
  150. if (i == s1ctx.lockingMax)
  151. {
  152. if (s1ctx.lockingMax == SCALAB01_MAX_LOCKING)
  153. res = 0;
  154. else
  155. s1ctx.lockingMax++;
  156. }
  157. if (res != 0)
  158. s1ctx.locking[i] = tttctx->bufBindDN;
  159. }
  160. /*
  161. * Free mutex
  162. */
  163. if ((ret = ldclt_mutex_unlock (&(s1ctx.locking_mutex))) != 0)
  164. {
  165. fprintf (stderr, "ldclt[%d]: %s: cannot mutex_unlock(), error=%d (%s)\n",
  166. mctx.pid, tttctx->thrdId, ret, strerror (ret));
  167. fflush (stderr);
  168. return (-1);
  169. }
  170. return (res);
  171. }
  172. /* ****************************************************************************
  173. FUNCTION : scalab01Unlock
  174. PURPOSE : Unlock for single user trying to connect.
  175. INPUT : tttctx = thread context.
  176. OUTPUT : None.
  177. RETURN : -1 if error, 0 else.
  178. DESCRIPTION :
  179. *****************************************************************************/
  180. int
  181. scalab01Unlock (
  182. thread_context *tttctx)
  183. {
  184. int i; /* For the loop */
  185. int ret; /* Return code */
  186. /*
  187. * Get secure access to the common data structure.
  188. */
  189. if ((ret = ldclt_mutex_lock (&(s1ctx.locking_mutex))) != 0)
  190. {
  191. fprintf (stderr, "ldclt[%d]: %s: cannot mutex_lock(), error=%d (%s)\n",
  192. mctx.pid, tttctx->thrdId, ret, strerror (ret));
  193. fflush (stderr);
  194. return (-1);
  195. }
  196. /*
  197. * Find the entry and unlock it.
  198. */
  199. for (i=0 ; i<s1ctx.lockingMax ; i++)
  200. if ((s1ctx.locking[i] != NULL) &&
  201. (!strcmp (s1ctx.locking[i], tttctx->bufBindDN)))
  202. {
  203. s1ctx.locking[i] = NULL;
  204. break;
  205. }
  206. /*
  207. * Free mutex
  208. */
  209. if ((ret = ldclt_mutex_unlock (&(s1ctx.locking_mutex))) != 0)
  210. {
  211. fprintf (stderr, "ldclt[%d]: %s: cannot mutex_unlock(), error=%d (%s)\n",
  212. mctx.pid, tttctx->thrdId, ret, strerror (ret));
  213. fflush (stderr);
  214. return (-1);
  215. }
  216. return (0);
  217. }
  218. /* ****************************************************************************
  219. FUNCTION : scalab01_modemIncr
  220. PURPOSE : Increments the modem nb of cnx
  221. INPUT : ident = thread identifier
  222. OUTPUT : None.
  223. RETURN : -1 if error
  224. 0 if no modem available
  225. 1 if modem available
  226. DESCRIPTION :
  227. *****************************************************************************/
  228. int
  229. scalab01_modemIncr (
  230. char *ident)
  231. {
  232. int ret; /* Return value */
  233. int res; /* Result of this function */
  234. /*
  235. * Get secure access to the common data structure.
  236. */
  237. if ((ret = ldclt_mutex_lock (&(s1ctx.nbcnx_mutex))) != 0)
  238. {
  239. fprintf (stderr, "ldclt[%d]: %s: cannot mutex_lock(), error=%d (%s)\n",
  240. mctx.pid, ident, ret, strerror (ret));
  241. fflush (stderr);
  242. return (-1);
  243. }
  244. if (s1ctx.nbcnx >= s1ctx.maxcnxnb)
  245. res = 0;
  246. else
  247. {
  248. res = 1;
  249. s1ctx.nbcnx++;
  250. }
  251. /*
  252. * Free mutex
  253. */
  254. if ((ret = ldclt_mutex_unlock (&(s1ctx.nbcnx_mutex))) != 0)
  255. {
  256. fprintf (stderr, "ldclt[%d]: %s: cannot mutex_unlock(), error=%d (%s)\n",
  257. mctx.pid, ident, ret, strerror (ret));
  258. fflush (stderr);
  259. return (-1);
  260. }
  261. return (res);
  262. }
  263. /* ****************************************************************************
  264. FUNCTION : scalab01_modemDecr
  265. PURPOSE : Decrements the modem nb of cnx
  266. INPUT : ident = thread identifier
  267. OUTPUT : None.
  268. RETURN : -1 if error, 0 else.
  269. DESCRIPTION :
  270. *****************************************************************************/
  271. int
  272. scalab01_modemDecr (
  273. char *ident)
  274. {
  275. int ret; /* Return value */
  276. /*
  277. * Get secure access to the common data structure.
  278. */
  279. if ((ret = ldclt_mutex_lock (&(s1ctx.nbcnx_mutex))) != 0)
  280. {
  281. fprintf (stderr, "ldclt[%d]: %s: cannot mutex_lock(), error=%d (%s)\n",
  282. mctx.pid, ident, ret, strerror (ret));
  283. fflush (stderr);
  284. return (-1);
  285. }
  286. s1ctx.nbcnx--;
  287. /*
  288. * Free mutex
  289. */
  290. if ((ret = ldclt_mutex_unlock (&(s1ctx.nbcnx_mutex))) != 0)
  291. {
  292. fprintf (stderr, "ldclt[%d]: %s: cannot mutex_unlock(), error=%d (%s)\n",
  293. mctx.pid, ident, ret, strerror (ret));
  294. fflush (stderr);
  295. return (-1);
  296. }
  297. return (0);
  298. }
  299. /* ****************************************************************************
  300. FUNCTION : scalab01_addLogin
  301. PURPOSE : Add a new user login to the s1ctx structure.
  302. INPUT : tttctx = thread context.
  303. dn = user's dn.
  304. duration = duration of the connection.
  305. OUTPUT : None.
  306. RETURN : -1 if error, 0 else.
  307. DESCRIPTION :
  308. *****************************************************************************/
  309. int
  310. scalab01_addLogin (
  311. thread_context *tttctx,
  312. char *dn,
  313. int duration)
  314. {
  315. int ret; /* Return value */
  316. isp_user *new; /* New entry */
  317. isp_user *cur; /* Current entry */
  318. int rc = 0;
  319. /*
  320. * Create the new record.
  321. */
  322. new = (isp_user *) malloc (sizeof (isp_user));
  323. if (NULL == new) {
  324. fprintf (stderr, "ldclt[%d]: %s: cannot malloc(isp_user), error=%d (%s)\n",
  325. mctx.pid, tttctx->thrdId, errno, strerror (errno));
  326. fflush (stderr);
  327. return -1;
  328. }
  329. strncpy (new->dn, dn, sizeof(new->dn));
  330. new->dn[sizeof(new->dn)-1] = '\0';
  331. new->cost = new->counter = duration;
  332. new->next = NULL;
  333. /*
  334. * Get secure access to the common data structure.
  335. * Note : it should be possible to reduce the "size" of this critical
  336. * section but I am not 100% certain this won't mess up all things.
  337. */
  338. if ((ret = ldclt_mutex_lock (&(s1ctx.list_mutex))) != 0)
  339. {
  340. fprintf (stderr, "ldclt[%d]: %s: cannot mutex_lock(), error=%d (%s)\n",
  341. mctx.pid, tttctx->thrdId, ret, strerror (ret));
  342. fflush (stderr);
  343. rc = -1;
  344. goto error;
  345. }
  346. /*
  347. * Maybe this is the first entry of the list ?
  348. */
  349. if (s1ctx.list == NULL)
  350. s1ctx.list = new;
  351. else
  352. {
  353. /*
  354. * Check with the list's head
  355. */
  356. if (s1ctx.list->counter >= duration)
  357. {
  358. new->next = s1ctx.list;
  359. s1ctx.list = new;
  360. }
  361. else
  362. {
  363. cur = s1ctx.list;
  364. /* If cur is NULL, we should just bail and free new. */
  365. if (cur == NULL)
  366. {
  367. goto error;
  368. }
  369. while (cur != NULL)
  370. {
  371. if (cur->next == NULL)
  372. {
  373. cur->next = new;
  374. cur = NULL; /* Exit loop */
  375. }
  376. else if (cur->next->counter >= duration)
  377. {
  378. new->next = cur->next;
  379. cur->next = new;
  380. cur = NULL; /* Exit loop */
  381. }
  382. else
  383. {
  384. cur = cur->next;
  385. }
  386. }
  387. }
  388. }
  389. goto done;
  390. error:
  391. if (new) free(new);
  392. done:
  393. /*
  394. * Free mutex
  395. */
  396. if ((ret = ldclt_mutex_unlock (&(s1ctx.list_mutex))) != 0)
  397. {
  398. fprintf (stderr, "ldclt[%d]: %s: cannot mutex_unlock(), error=%d (%s)\n",
  399. mctx.pid, tttctx->thrdId, ret, strerror (ret));
  400. fflush (stderr);
  401. rc = -1;
  402. }
  403. return rc;
  404. }
  405. /* ****************************************************************************
  406. FUNCTION : scalab01_connectSuperuser
  407. PURPOSE : Purpose of the fct
  408. INPUT : None.
  409. OUTPUT : None.
  410. RETURN : -1 if error, 0 else.
  411. DESCRIPTION :
  412. *****************************************************************************/
  413. int
  414. scalab01_connectSuperuser (void)
  415. {
  416. char bindDN [MAX_DN_LENGTH] = {0}; /* To bind */
  417. unsigned int mode = mctx.mode;
  418. unsigned int mod2 = mctx.mod2;
  419. if (!(mode & CLTAUTH)) {
  420. snprintf (bindDN, sizeof(bindDN), "%s,%s", SCALAB01_SUPER_USER_RDN, mctx.baseDN);
  421. bindDN[sizeof(bindDN)-1] = '\0';
  422. }
  423. /* clear bits not applicable to this mode */
  424. mod2 &= ~M2_RNDBINDFILE;
  425. mod2 &= ~M2_SASLAUTH;
  426. mod2 &= ~M2_RANDOM_SASLAUTHID;
  427. /* force bind to happen */
  428. mode |= BIND_EACH_OPER;
  429. s1ctx.ldapCtx = connectToLDAP(NULL, bindDN, SCALAB01_SUPER_USER_PASSWORD, mode, mod2);
  430. if (!s1ctx.ldapCtx) {
  431. return (-1);
  432. }
  433. /*
  434. * Normal end...
  435. */
  436. return (0);
  437. }
  438. /* ****************************************************************************
  439. FUNCTION : readAttrValue
  440. PURPOSE : This function will ldap_search the given entry for the
  441. value of the given attribute.
  442. INPUT : ident = thread identifier
  443. ldapCtx = LDAP context
  444. dn = dn of the entry to process
  445. attname = attribute name
  446. OUTPUT : value = attribute value. This buffer must be
  447. initiated with enough memory.
  448. value[0] == '\0' if not find.
  449. RETURN : -1 if error, 0 else.
  450. DESCRIPTION :
  451. *****************************************************************************/
  452. int
  453. readAttrValue (
  454. LDAP *ldapCtx,
  455. char *ident,
  456. char *dn,
  457. char *attname,
  458. char *value)
  459. {
  460. int ret; /* Return value */
  461. char *attrs[2]; /* Attribute to retrieve */
  462. LDAPMessage *res; /* LDAP responses */
  463. LDAPMessage *cur; /* Current message */
  464. BerElement *ber; /* To decode the response */
  465. char *aname; /* Current attribute name */
  466. char *filter; /* Filter used for searching */
  467. /*
  468. * First, ldap_search() the entry.
  469. */
  470. attrs[0] = attname;
  471. attrs[1] = NULL;
  472. filter = (char *)malloc((4+strlen(attname))*sizeof(char));
  473. if (NULL == filter)
  474. {
  475. printf ("ldclt[%d]: %s: Out of memory\n", mctx.pid, ident);
  476. fflush (stdout);
  477. return (-1);
  478. }
  479. sprintf(filter, "(%s=*)", attname);
  480. ret = ldap_search_ext_s (ldapCtx, dn, LDAP_SCOPE_BASE,
  481. filter, attrs, 0, NULL, NULL, NULL, -1, &res);
  482. free(filter);
  483. if (ret != LDAP_SUCCESS)
  484. {
  485. printf ("ldclt[%d]: %s: Cannot ldap_search (%s in %s), error=%d (%s)\n",
  486. mctx.pid, ident, attname, dn, ret, my_ldap_err2string (ret));
  487. fflush (stdout);
  488. return (-1);
  489. }
  490. /*
  491. * Decode the response
  492. */
  493. value[0] = '\0'; /* Not find yet */
  494. cur = ldap_first_entry (ldapCtx, res);
  495. while ((!value[0]) && (cur != NULL))
  496. {
  497. aname = ldap_first_attribute (ldapCtx, cur, &ber);
  498. while ((!value[0]) && (aname != NULL))
  499. {
  500. /*
  501. * We expect this attribute to be single-valued.
  502. */
  503. if (!strcmp (aname, attname))
  504. {
  505. struct berval **vals;
  506. vals = ldap_get_values_len (ldapCtx, cur, aname);
  507. if (vals == NULL)
  508. {
  509. printf ("ldclt[%d]: %s: no value for %s in %s\n",
  510. mctx.pid, ident, dn, attname);
  511. fflush (stdout);
  512. return (-1);
  513. }
  514. strncpy (value, vals[0]->bv_val, vals[0]->bv_len);
  515. value[vals[0]->bv_len] = '\0';
  516. ldap_value_free_len (vals);
  517. }
  518. /*
  519. * Next attribute
  520. */
  521. ldap_memfree (aname);
  522. if (!value[0])
  523. aname = ldap_next_attribute (ldapCtx, cur, ber);
  524. }
  525. /*
  526. * Next entry - shouldn't happen in theory
  527. */
  528. if (ber != NULL)
  529. ber_free (ber, 0);
  530. cur = ldap_next_entry (ldapCtx, cur);
  531. }
  532. ldap_msgfree (res); /* Free the response */
  533. return (0);
  534. }
  535. /* ****************************************************************************
  536. FUNCTION : writeAttrValue
  537. PURPOSE : This function will ldap_modify the given entry to
  538. replace the value of the given attribute.
  539. INPUT : ident = thread identifier
  540. ldapCtx = LDAP context
  541. dn = dn of the entry to process
  542. attname = attribute name
  543. value = attribute value
  544. OUTPUT : None.
  545. RETURN : -1 if error, 0 else.
  546. DESCRIPTION :
  547. *****************************************************************************/
  548. int
  549. writeAttrValue (
  550. LDAP *ldapCtx,
  551. char *ident,
  552. char *dn,
  553. char *attname,
  554. char *value)
  555. {
  556. int ret; /* Return value */
  557. LDAPMod attribute; /* To build the attributes */
  558. LDAPMod *attrsmod[2]; /* Modify attributes */
  559. char *pvalues[2]; /* To build the values list */
  560. /*
  561. * Prepear the data to be written
  562. */
  563. pvalues[0] = value;
  564. pvalues[1] = NULL;
  565. attribute.mod_op = LDAP_MOD_REPLACE;
  566. attribute.mod_type = attname;
  567. attribute.mod_values = pvalues;
  568. attrsmod[0] = &attribute;
  569. attrsmod[1] = NULL;
  570. /*
  571. * Store the data in the directory.
  572. */
  573. ret = ldap_modify_ext_s (ldapCtx, dn, attrsmod, NULL, NULL);
  574. if (ret != LDAP_SUCCESS)
  575. {
  576. printf ("ldclt[%d]: %s: Cannot ldap_modify_ext_s (%s in %s), error=%d (%s)\n",
  577. mctx.pid, ident, attname, dn, ret, my_ldap_err2string (ret));
  578. fflush (stdout);
  579. return (-1);
  580. }
  581. return (0);
  582. }
  583. /* ****************************************************************************
  584. FUNCTION : scalab01_unlock
  585. PURPOSE : Unlock the user given in argument.
  586. INPUT : entry = entry to unlock.
  587. OUTPUT : None.
  588. RETURN : -1 if error, 0 else.
  589. DESCRIPTION :
  590. *****************************************************************************/
  591. int
  592. scalab01_unlock (
  593. isp_user *user)
  594. {
  595. int account; /* Accounting value */
  596. char buf[20]; /* To read/write attribute */
  597. /*
  598. * Increment accounting counters
  599. * First, read the current value.
  600. */
  601. if (readAttrValue (s1ctx.ldapCtx,"ctrl",user->dn,SCALAB01_ACC_ATTRIB,buf) < 0)
  602. {
  603. printf ("ldclt[%d]: ctrl: Cannot read accounting attribute of %s\n",
  604. mctx.pid, user->dn);
  605. fflush (stdout);
  606. return (-1);
  607. }
  608. /*
  609. * If this attribute has no value (doesn't exist) we assume it is 0.
  610. */
  611. if (buf[0] != '\0')
  612. account = atoi (buf);
  613. else
  614. {
  615. printf ("ldclt[%d]: ctrl: No accounting attribute for %s - assume it is 0\n",
  616. mctx.pid, user->dn);
  617. fflush (stdout);
  618. account = 0;
  619. }
  620. /*
  621. * Compute the new value and store it in the directory.
  622. */
  623. sprintf (buf, "%d", account + user->cost);
  624. if (writeAttrValue (s1ctx.ldapCtx,"ctrl",user->dn,SCALAB01_ACC_ATTRIB,buf) <0)
  625. {
  626. printf ("ldclt[%d]: ctrl: Cannot write accounting attribute of %s\n",
  627. mctx.pid, user->dn);
  628. fflush (stdout);
  629. return (-1);
  630. }
  631. /*
  632. * Unlock the user
  633. */
  634. if (writeAttrValue (s1ctx.ldapCtx, "ctrl", user->dn,
  635. SCALAB01_LOCK_ATTRIB, SCALAB01_VAL_UNLOCKED) < 0)
  636. {
  637. printf ("ldclt[%d]: ctrl: Cannot write lock (unlock) attribute of %s\n",
  638. mctx.pid, user->dn);
  639. fflush (stdout);
  640. return (-1);
  641. }
  642. if (mctx.mode & VERY_VERBOSE)
  643. printf ("ldclt[%d]: ctrl: entry %s unlocked\n",
  644. mctx.pid, user->dn);
  645. /*
  646. * Decrement modem pool usage...
  647. */
  648. if (scalab01_modemDecr ("ctrl") < 0)
  649. return (-1);
  650. /*
  651. * Normal end
  652. */
  653. return (0);
  654. }
  655. /* ****************************************************************************
  656. FUNCTION : scalab01_control
  657. PURPOSE : This function implements the control loop/thread of
  658. the scalab01 scenario. Its main target is to manage
  659. the counters of each "connection" and to unlock the
  660. entry when time is reached.
  661. INPUT : None.
  662. OUTPUT : None.
  663. RETURN : -1 if error, 0 else.
  664. DESCRIPTION :
  665. *****************************************************************************/
  666. void *
  667. scalab01_control (
  668. void *arg)
  669. {
  670. isp_user *cur; /* Current entry */
  671. isp_user *head; /* Head of entries to process */
  672. int ret; /* Return value */
  673. int nbTot; /* Total nb entries locked */
  674. int nbU; /* Number unlocked */
  675. /*
  676. * Initialization
  677. * Failure to connect is a critical error...
  678. */
  679. if (scalab01_connectSuperuser () < 0)
  680. ldcltExit (EXIT_NOBIND);
  681. /*
  682. * Main loop
  683. */
  684. while (1 /*CONSTCOND*/) /*JLS 14-03-01*/
  685. {
  686. ldclt_sleep (1); /* Poll the connections every second */
  687. nbTot = nbU = 0; /* No entries processed yet */
  688. /*
  689. * Get protected access to the entries
  690. */
  691. if ((ret = ldclt_mutex_lock (&(s1ctx.list_mutex))) != 0)
  692. {
  693. fprintf (stderr, "ldclt[%d]: ctrl: cannot mutex_lock(), error=%d (%s)\n",
  694. mctx.pid, ret, strerror (ret));
  695. fflush (stderr);
  696. ldcltExit (EXIT_OTHER);
  697. }
  698. /*
  699. * Decrement all counters
  700. */
  701. for (cur=s1ctx.list ; cur!=NULL ; cur=cur->next)
  702. {
  703. cur->counter--;
  704. nbTot++;
  705. }
  706. /*
  707. * Find the entries to process.
  708. */
  709. if ((s1ctx.list == NULL) || (s1ctx.list->counter > 0))
  710. head = NULL;
  711. else
  712. {
  713. head = cur = s1ctx.list;
  714. while ((cur != NULL) && (cur->counter == 0))
  715. cur = cur->next;
  716. s1ctx.list = cur;
  717. }
  718. /*
  719. * Release mutex
  720. */
  721. if ((ret = ldclt_mutex_unlock (&(s1ctx.list_mutex))) != 0)
  722. {
  723. fprintf (stderr, "ldclt[%d]: ctrl: cannot mutex_unlock(), error=%d (%s)\n",
  724. mctx.pid, ret, strerror (ret));
  725. fflush (stderr);
  726. ldcltExit (EXIT_OTHER);
  727. }
  728. /*
  729. * Now, we have "head" that points either to NULL or to a list of
  730. * entries to process.
  731. * Attention, this list of entries is not terminated by NULL, but
  732. * we must rather check the field head->next->counter" for the last
  733. * entry...
  734. *
  735. * NOTE : implements this section as a separate thread to keep the
  736. * general timer working...
  737. */
  738. while (head != NULL)
  739. {
  740. if (scalab01_unlock (head) < 0)
  741. {
  742. printf ("ldclt[%d]: ctrl: cannot unlock %s\n", mctx.pid, head->dn);
  743. ldcltExit (EXIT_OTHER);
  744. }
  745. nbU++; /* One more entry unlocked */
  746. /*
  747. * Next entry...
  748. */
  749. cur = head;
  750. if (head->next == NULL)
  751. head = NULL;
  752. else
  753. if (head->next->counter != 0)
  754. head = NULL;
  755. else
  756. head = head->next;
  757. free (cur);
  758. } /* while (head =! NULL) */
  759. /*
  760. * Print some stats...
  761. */
  762. if (mctx.mode & VERBOSE)
  763. printf ("ldclt[%d]: ctrl: nb entries unlocked / total : %3d / %5d\n",
  764. mctx.pid, nbU, nbTot);
  765. } /* Main loop */
  766. /*
  767. * End of thread
  768. */
  769. }
  770. /* ****************************************************************************
  771. FUNCTION : doScalab01
  772. PURPOSE : Implements the client part of the scalab01 scenario.
  773. INPUT : tttctx = this thread context
  774. OUTPUT : None.
  775. RETURN : -1 if error, 0 else.
  776. DESCRIPTION :
  777. *****************************************************************************/
  778. int
  779. doScalab01 (
  780. thread_context *tttctx)
  781. {
  782. char buf[32]; /* To read attributes value */
  783. int duration; /* Use a variable for trace purpose */
  784. int res; /* Result of cnx to modem pool */
  785. int doloop; /* To know if we should loop */
  786. /*
  787. * Simulate connection to the modem pool.
  788. */
  789. while ((res = scalab01_modemIncr(tttctx->thrdId)) != 1)
  790. switch (res)
  791. {
  792. case 0:
  793. ldclt_sleep (s1ctx.wait==0?SCALAB01_DEF_WAIT_TIME:rndlim(0,s1ctx.wait));
  794. break;
  795. case -1:
  796. return (-1);
  797. break;
  798. }
  799. /*
  800. * Connection to the server
  801. * The function connectToServer() will take care of the various connection/
  802. * disconnection, bind/unbind/close etc... requested by the user.
  803. * The cost is one more function call in this application, but the
  804. * resulting source code will be much more easiest to maintain.
  805. */
  806. if (connectToServer (tttctx) < 0)
  807. return (-1);
  808. if (!(tttctx->binded))
  809. return (0);
  810. /*
  811. * Check that no other thread is using the same identity...
  812. */
  813. doloop = 1;
  814. while (doloop)
  815. {
  816. switch (scalab01Lock (tttctx))
  817. {
  818. case 0:
  819. ldclt_sleep (1);
  820. break;
  821. case 1:
  822. doloop = 0;
  823. break;
  824. case -1:
  825. return (-1);
  826. break;
  827. }
  828. }
  829. /*
  830. * Ok, we are now binded. Great ;-)
  831. * The DN we used to bind is available in tttctx->bufBindDN
  832. * Read lock attribute
  833. */
  834. if (readAttrValue (tttctx->ldapCtx, tttctx->thrdId, tttctx->bufBindDN,
  835. SCALAB01_LOCK_ATTRIB, buf) < 0)
  836. {
  837. printf ("ldclt[%d]: %s: Cannot read lock attribute of %s\n",
  838. mctx.pid, tttctx->thrdId, tttctx->bufBindDN);
  839. fflush (stdout);
  840. (void) scalab01_modemDecr (tttctx->thrdId);
  841. return (-1);
  842. }
  843. if (mctx.mode & VERY_VERBOSE)
  844. printf ("ldclt[%d]: %s: entry %s lock read\n",
  845. mctx.pid, tttctx->thrdId, tttctx->bufBindDN);
  846. /*
  847. * If locked, then we cannot login now...
  848. */
  849. if (!strcmp (buf, SCALAB01_VAL_LOCKED))
  850. {
  851. if (scalab01_modemDecr (tttctx->thrdId) < 0)
  852. return (-1);
  853. return (0);
  854. }
  855. /*
  856. * If not locked :
  857. * - lock the user
  858. * - decide how many times will be connected
  859. * - add information to the list of connected
  860. */
  861. if (writeAttrValue (tttctx->ldapCtx, tttctx->thrdId, tttctx->bufBindDN,
  862. SCALAB01_LOCK_ATTRIB, SCALAB01_VAL_LOCKED) < 0)
  863. {
  864. printf ("ldclt[%d]: %s: Cannot write lock attribute of %s\n",
  865. mctx.pid, tttctx->thrdId, tttctx->bufBindDN);
  866. fflush (stdout);
  867. /*
  868. * It can still happen that two threads write this attribute at the same
  869. * time, so there can be failure in one of the threads
  870. * in this case just return
  871. */
  872. if (scalab01_modemDecr (tttctx->thrdId) < 0) /*JLS 03-05-01*/
  873. return (-1); /*JLS 03-05-01*/
  874. return (0); /*BK 26-04-01*/
  875. }
  876. if (mctx.mode & VERY_VERBOSE)
  877. printf ("ldclt[%d]: %s: entry %s lock written\n",
  878. mctx.pid, tttctx->thrdId, tttctx->bufBindDN);
  879. if (scalab01Unlock (tttctx) < 0)
  880. return (-1);
  881. duration = rndlim (1, s1ctx.cnxduration);
  882. if (scalab01_addLogin (tttctx, tttctx->bufBindDN, duration) < 0)
  883. {
  884. printf ("ldclt[%d]: %s: Cannot memorize new login of %s\n",
  885. mctx.pid, tttctx->thrdId, tttctx->bufBindDN);
  886. fflush (stdout);
  887. return (-1);
  888. }
  889. if (mctx.mode & VERY_VERBOSE)
  890. printf ("ldclt[%d]: %s: entry %s login added duration %6d\n",
  891. mctx.pid, tttctx->thrdId, tttctx->bufBindDN, duration);
  892. /*
  893. * Memorize the operation
  894. */
  895. if (incrementNbOpers (tttctx) < 0)
  896. return (-1);
  897. /*
  898. * Wait before next operation...
  899. */
  900. if (s1ctx.wait > 0)
  901. ldclt_sleep (rndlim (0,s1ctx.wait));
  902. /*
  903. * Unbind
  904. */
  905. /*
  906. TBC - this is done in the next loop... - cf connectToServer()
  907. */
  908. return (0);
  909. }