usrcache.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /* #define DBG_PRINT */
  42. #include <netsite.h>
  43. extern "C" {
  44. #include <secitem.h>
  45. }
  46. #include <base/crit.h>
  47. #include <ldaputil/errors.h>
  48. #include <libaccess/usrcache.h>
  49. #include <libaccess/las.h>
  50. #include <libaccess/authdb.h>
  51. #include "permhash.h"
  52. /* uid is unique within a database. The user cache tables are stored per
  53. * database. The following table maps a database name to the corresponding
  54. * user cache table. The user cache table is another hash table which stores
  55. * the UserCacheObj instances.
  56. */
  57. static PRHashTable *databaseUserCacheTable = 0;
  58. static time_t acl_usr_cache_lifetime = (time_t)120;
  59. static PRCList *usrobj_list = 0;
  60. static const int num_usrobj = 200;
  61. static CRITICAL usr_hash_crit = NULL; /* Controls user cache hash tables & */
  62. /* usrobj link list */
  63. static pool_handle_t *usrcache_pool = NULL;
  64. static PRHashTable *singleDbTable = 0;
  65. #define USEROBJ_PTR(l) \
  66. ((UserCacheObj*) ((char*) (l) - offsetof(UserCacheObj, list)))
  67. static void user_hash_crit_enter (void)
  68. {
  69. /* Caching may be disabled (usr_hash_crit will be NULL) */
  70. if (usr_hash_crit) crit_enter(usr_hash_crit);
  71. }
  72. static void user_hash_crit_exit (void)
  73. {
  74. /* Caching may be disabled (usr_hash_crit will be NULL) */
  75. if (usr_hash_crit) crit_exit(usr_hash_crit);
  76. }
  77. static void user_hash_crit_init (void)
  78. {
  79. usr_hash_crit = crit_init();
  80. }
  81. static PRHashNumber
  82. usr_cache_hash_cert(const void *key)
  83. {
  84. PRHashNumber h;
  85. const unsigned char *s;
  86. unsigned int i = 0;
  87. SECItem *derCert = (SECItem *)key;
  88. unsigned int len = derCert->len;
  89. h = 0;
  90. for (s = (const unsigned char *)derCert->data; i < len; s++, i++)
  91. h = (h >> 28) ^ (h << 4) ^ *s;
  92. return h;
  93. }
  94. static PRHashNumber
  95. usr_cache_hash_fn (const void *key)
  96. {
  97. UserCacheObj *usrObj = (UserCacheObj *)key;
  98. if (usrObj->derCert)
  99. return usr_cache_hash_cert(usrObj->derCert);
  100. else
  101. return PR_HashCaseString(usrObj->uid);
  102. }
  103. static int
  104. usr_cache_compare_certs(const void *v1, const void *v2)
  105. {
  106. const SECItem *c1 = (const SECItem *)v1;
  107. const SECItem *c2 = (const SECItem *)v2;
  108. return (c1->len == c2 ->len && !memcmp(c1->data, c2->data, c1->len));
  109. }
  110. static int
  111. usr_cache_compare_fn(const void *v1, const void *v2)
  112. {
  113. UserCacheObj *usrObj1 = (UserCacheObj *)v1;
  114. UserCacheObj *usrObj2 = (UserCacheObj *)v2;
  115. if (usrObj1->derCert && usrObj2->derCert)
  116. return usr_cache_compare_certs(usrObj1->derCert, usrObj2->derCert);
  117. else if (!usrObj1->derCert && !usrObj2->derCert)
  118. return PR_CompareCaseStrings(usrObj1->uid, usrObj1->uid);
  119. else
  120. return 0;
  121. }
  122. static PRHashTable *alloc_db2uid_table ()
  123. {
  124. return PR_NewHashTable(0,
  125. usr_cache_hash_fn,
  126. usr_cache_compare_fn,
  127. PR_CompareValues,
  128. &ACLPermAllocOps,
  129. usrcache_pool);
  130. }
  131. int acl_usr_cache_set_timeout (const int nsec)
  132. {
  133. acl_usr_cache_lifetime = (time_t)nsec;
  134. return 0;
  135. }
  136. int acl_usr_cache_enabled ()
  137. {
  138. return (acl_usr_cache_lifetime > 0);
  139. }
  140. int acl_usr_cache_init ()
  141. {
  142. UserCacheObj *usrobj;
  143. int i;
  144. if (acl_usr_cache_lifetime <= 0) {
  145. /* Caching is disabled */
  146. DBG_PRINT1("usrcache is disabled");
  147. return 0;
  148. }
  149. usrcache_pool = pool_create();
  150. user_hash_crit_init();
  151. if (acl_num_databases() == 0) {
  152. /* Something wrong -- No databases registered yet! */
  153. return -1;
  154. }
  155. else if (acl_num_databases() == 1) {
  156. /* Optimize for single database */
  157. DBG_PRINT1("Optimizing usrcache for single db");
  158. singleDbTable = alloc_db2uid_table();
  159. }
  160. else {
  161. singleDbTable = 0;
  162. databaseUserCacheTable = PR_NewHashTable(0,
  163. PR_HashCaseString,
  164. PR_CompareCaseStrings,
  165. PR_CompareValues,
  166. &ACLPermAllocOps,
  167. usrcache_pool);
  168. }
  169. /* Allocate first UserCacheObj and initialize the circular link list */
  170. usrobj = (UserCacheObj *)pool_malloc(usrcache_pool, sizeof(UserCacheObj));
  171. if (!usrobj) return -1;
  172. memset((void *)usrobj, 0, sizeof(UserCacheObj));
  173. usrobj_list = &usrobj->list;
  174. PR_INIT_CLIST(usrobj_list);
  175. /* Allocate rest of the UserCacheObj and put them in the link list */
  176. for(i = 0; i < num_usrobj; i++){
  177. usrobj = (UserCacheObj *)pool_malloc(usrcache_pool,
  178. sizeof(UserCacheObj));
  179. if (!usrobj) return -1;
  180. memset((void *)usrobj, 0, sizeof(UserCacheObj));
  181. PR_INSERT_AFTER(&usrobj->list, usrobj_list);
  182. }
  183. return (singleDbTable || databaseUserCacheTable) ? 0 : -1;
  184. }
  185. /* If the user hash table exists in the databaseUserCacheTable then return it.
  186. * Otherwise, create a new hash table, insert it in the databaseUserCacheTable
  187. * and then return it.
  188. */
  189. static int usr_cache_table_get (const char *dbname, PRHashTable **usrTable)
  190. {
  191. PRHashTable *table;
  192. if (singleDbTable) {
  193. *usrTable = singleDbTable;
  194. return LAS_EVAL_TRUE;
  195. }
  196. user_hash_crit_enter();
  197. table = (PRHashTable *)PR_HashTableLookup(databaseUserCacheTable,
  198. dbname);
  199. if (!table) {
  200. /* create a new table and insert it in the databaseUserCacheTable */
  201. table = alloc_db2uid_table();
  202. if (table) {
  203. PR_HashTableAdd(databaseUserCacheTable,
  204. pool_strdup(usrcache_pool, dbname),
  205. table);
  206. }
  207. }
  208. *usrTable = table;
  209. user_hash_crit_exit();
  210. return table ? LAS_EVAL_TRUE : LAS_EVAL_FAIL;
  211. }
  212. int acl_usr_cache_insert (const char *uid, const char *dbname,
  213. const char *userdn, const char *passwd,
  214. const char *group,
  215. const SECItem *derCert, const time_t time)
  216. {
  217. PRHashTable *usrTable;
  218. UserCacheObj *usrobj;
  219. UserCacheObj key;
  220. int rv;
  221. if (acl_usr_cache_lifetime <= 0) {
  222. /* Caching is disabled */
  223. return LAS_EVAL_TRUE;
  224. }
  225. rv = usr_cache_table_get (dbname, &usrTable);
  226. if (rv != LAS_EVAL_TRUE) return rv;
  227. user_hash_crit_enter();
  228. key.uid = (char *)uid;
  229. key.derCert = (SECItem *)derCert;
  230. usrobj = (UserCacheObj *)PR_HashTableLookup(usrTable, &key);
  231. if (usrobj) {
  232. time_t elapsed = time - usrobj->time;
  233. int expired = (elapsed >= acl_usr_cache_lifetime);
  234. /* Free & reset the old values in usrobj if -- there is an old value
  235. * and if the new value is given then it is different or the usrobj
  236. * has expired */
  237. /* Set the field if the new value is given and the field is not set */
  238. /* If the usrobj has not expired then we only want to update the field
  239. * whose new value is non-NULL and different */
  240. /* Work on the 'uid' field */
  241. if (usrobj->uid &&
  242. (uid ? strcmp(usrobj->uid, uid) : expired))
  243. {
  244. pool_free(usrcache_pool, usrobj->uid);
  245. usrobj->uid = 0;
  246. }
  247. if (uid && !usrobj->uid) {
  248. usrobj->uid = pool_strdup(usrcache_pool, uid);
  249. }
  250. /* Work on the 'userdn' field */
  251. if (usrobj->userdn &&
  252. (userdn ? strcmp(usrobj->userdn, userdn) : expired))
  253. {
  254. pool_free(usrcache_pool, usrobj->userdn);
  255. usrobj->userdn = 0;
  256. }
  257. if (userdn && !usrobj->userdn) {
  258. usrobj->userdn = pool_strdup(usrcache_pool, userdn);
  259. }
  260. /* Work on the 'passwd' field */
  261. if (usrobj->passwd &&
  262. (passwd ? strcmp(usrobj->passwd, passwd) : expired))
  263. {
  264. pool_free(usrcache_pool, usrobj->passwd);
  265. usrobj->passwd = 0;
  266. }
  267. if (passwd && !usrobj->passwd) {
  268. usrobj->passwd = pool_strdup(usrcache_pool, passwd);
  269. }
  270. /* Work on the 'group' field -- not replace a valid group */
  271. if (!expired && usrobj->group &&
  272. (group ? strcmp(usrobj->group, group) : expired))
  273. {
  274. pool_free(usrcache_pool, usrobj->group);
  275. usrobj->group = 0;
  276. }
  277. if (group && !usrobj->group) {
  278. usrobj->group = pool_strdup(usrcache_pool, group);
  279. }
  280. /* Work on the 'derCert' field */
  281. if (usrobj->derCert &&
  282. (derCert ? (derCert->len != usrobj->derCert->len ||
  283. memcmp(usrobj->derCert->data, derCert->data,
  284. derCert->len))
  285. : expired))
  286. {
  287. SECITEM_FreeItem(usrobj->derCert, PR_TRUE);
  288. usrobj->derCert = 0;
  289. }
  290. if (derCert && !usrobj->derCert) {
  291. usrobj->derCert = SECITEM_DupItem((SECItem *)derCert);
  292. }
  293. /* Reset the time only if the usrobj has expired */
  294. if (expired) {
  295. DBG_PRINT1("Replace ");
  296. usrobj->time = time;
  297. }
  298. else {
  299. DBG_PRINT1("Update ");
  300. }
  301. }
  302. else {
  303. /* Get the last usrobj from the link list, erase it and use it */
  304. /* Maybe the last usrobj is not invalid yet but we don't want to grow
  305. * the list of usrobjs. The last obj is the best candidate for being
  306. * not valid. We don't want to compare the time -- just use it.
  307. */
  308. PRCList *tail = PR_LIST_TAIL(usrobj_list);
  309. usrobj = USEROBJ_PTR(tail);
  310. /* If the removed usrobj is in the hashtable, remove it from there */
  311. if (usrobj->hashtable) {
  312. PR_HashTableRemove(usrobj->hashtable, usrobj);
  313. }
  314. /* Free the memory associated with the usrobj */
  315. if (usrobj->userdn) pool_free(usrcache_pool, usrobj->userdn);
  316. if (usrobj->passwd) pool_free(usrcache_pool, usrobj->passwd);
  317. if (usrobj->group) pool_free(usrcache_pool, usrobj->group);
  318. if (usrobj->derCert) SECITEM_FreeItem(usrobj->derCert, PR_TRUE);
  319. if (usrobj->uid) pool_free(usrcache_pool, usrobj->uid);
  320. /* Fill in the usrobj with the current data */
  321. usrobj->uid = pool_strdup(usrcache_pool, uid);
  322. usrobj->userdn = userdn ? pool_strdup(usrcache_pool, userdn) : 0;
  323. usrobj->passwd = passwd ? pool_strdup(usrcache_pool, passwd) : 0;
  324. usrobj->derCert = derCert ? SECITEM_DupItem((SECItem *)derCert) : 0;
  325. usrobj->group = group ? pool_strdup(usrcache_pool, group) : 0;
  326. usrobj->time = time;
  327. /* Add the usrobj to the user hash table */
  328. PR_HashTableAdd(usrTable, usrobj, usrobj);
  329. usrobj->hashtable = usrTable;
  330. DBG_PRINT1("Insert ");
  331. }
  332. /* Move the usrobj to the head of the list */
  333. PR_REMOVE_LINK(&usrobj->list);
  334. PR_INSERT_AFTER(&usrobj->list, usrobj_list);
  335. /* Set the time in the UserCacheObj */
  336. if (usrobj) {
  337. rv = LAS_EVAL_TRUE;
  338. }
  339. else {
  340. rv = LAS_EVAL_FAIL;
  341. }
  342. DBG_PRINT4("acl_usr_cache_insert: derCert = \"%s\" uid = \"%s\" at time = %ld\n",
  343. usrobj->derCert ? (char *)usrobj->derCert->data : "<NONE>",
  344. uid, time);
  345. user_hash_crit_exit();
  346. return rv;
  347. }
  348. static int acl_usr_cache_get_usrobj (const char *uid, const SECItem *derCert,
  349. const char *dbname, const time_t time,
  350. UserCacheObj **usrobj_out)
  351. {
  352. PRHashTable *usrtable;
  353. UserCacheObj *usrobj;
  354. UserCacheObj key;
  355. time_t elapsed;
  356. int rv;
  357. *usrobj_out = 0;
  358. if (acl_usr_cache_lifetime <= 0) {
  359. /* Caching is disabled */
  360. return LAS_EVAL_FALSE;
  361. }
  362. rv = usr_cache_table_get(dbname, &usrtable);
  363. if (!usrtable) return LAS_EVAL_FALSE;
  364. key.uid = (char *)uid;
  365. key.derCert = (SECItem *)derCert;
  366. usrobj = (UserCacheObj *)PR_HashTableLookup(usrtable, &key);
  367. if (!usrobj) return LAS_EVAL_FALSE;
  368. rv = LAS_EVAL_FALSE;
  369. elapsed = time - usrobj->time;
  370. /* If the cache is valid, return the usrobj */
  371. if (elapsed < acl_usr_cache_lifetime) {
  372. rv = LAS_EVAL_TRUE;
  373. *usrobj_out = usrobj;
  374. DBG_PRINT4("usr_cache found: derCert = \"%s\" uid = \"%s\" at time = %ld\n",
  375. usrobj->derCert ? (char *)usrobj->derCert->data : "<NONE>",
  376. usrobj->uid, time);
  377. }
  378. else {
  379. DBG_PRINT4("usr_cache expired: derCert = \"%s\" uid = \"%s\" at time = %ld\n",
  380. usrobj->derCert ? (char *)usrobj->derCert->data : "<NONE>",
  381. usrobj->uid, time);
  382. }
  383. return rv;
  384. }
  385. int acl_usr_cache_passwd_check (const char *uid, const char *dbname,
  386. const char *passwd,
  387. const time_t time, char **dn,
  388. pool_handle_t *pool)
  389. {
  390. UserCacheObj *usrobj;
  391. int rv;
  392. user_hash_crit_enter();
  393. rv = acl_usr_cache_get_usrobj(uid, 0, dbname, time, &usrobj);
  394. if (rv == LAS_EVAL_TRUE && usrobj->passwd && passwd &&
  395. !strcmp(usrobj->passwd, passwd))
  396. {
  397. /* extract dn from the usrobj */
  398. *dn = usrobj->userdn ? pool_strdup(pool, usrobj->userdn) : 0;
  399. rv = LAS_EVAL_TRUE;
  400. DBG_PRINT1("Success ");
  401. }
  402. else {
  403. rv = LAS_EVAL_FALSE;
  404. DBG_PRINT1("Failed ");
  405. }
  406. DBG_PRINT3("acl_usr_cache_passwd_check: uid = \"%s\" at time = %ld\n",
  407. uid, time);
  408. user_hash_crit_exit();
  409. return rv;
  410. }
  411. int acl_usr_cache_group_check (const char *uid, const char *dbname,
  412. const char *group, const time_t time)
  413. {
  414. UserCacheObj *usrobj;
  415. int rv;
  416. user_hash_crit_enter();
  417. rv = acl_usr_cache_get_usrobj(uid, 0, dbname, time, &usrobj);
  418. if (rv == LAS_EVAL_TRUE && usrobj->group && group &&
  419. !strcmp(usrobj->group, group))
  420. {
  421. DBG_PRINT1("Success ");
  422. }
  423. else {
  424. rv = LAS_EVAL_FALSE;
  425. DBG_PRINT1("Failed ");
  426. }
  427. DBG_PRINT3("acl_usr_cache_group_check: uid = \"%s\" group = \"%s\"\n",
  428. uid, group ? group : "<NONE>");
  429. user_hash_crit_exit();
  430. return rv;
  431. }
  432. int acl_usr_cache_group_len_check (const char *uid, const char *dbname,
  433. const char *group, const int len,
  434. const time_t time)
  435. {
  436. UserCacheObj *usrobj;
  437. int rv;
  438. user_hash_crit_enter();
  439. rv = acl_usr_cache_get_usrobj(uid, 0, dbname, time, &usrobj);
  440. if (rv == LAS_EVAL_TRUE && usrobj->group && group &&
  441. !strncmp(usrobj->group, group, len))
  442. {
  443. rv = LAS_EVAL_TRUE;
  444. DBG_PRINT1("Success ");
  445. }
  446. else {
  447. rv = LAS_EVAL_FALSE;
  448. DBG_PRINT1("Failed ");
  449. }
  450. DBG_PRINT3("acl_usr_cache_group_check: uid = \"%s\" group = \"%s\"\n",
  451. uid, group);
  452. user_hash_crit_exit();
  453. return rv;
  454. }
  455. int acl_usr_cache_get_userdn (const char *uid, const char *dbname,
  456. const time_t time, char **userdn,
  457. pool_handle_t *pool)
  458. {
  459. UserCacheObj *usrobj;
  460. int rv;
  461. *userdn = 0;
  462. user_hash_crit_enter();
  463. rv = acl_usr_cache_get_usrobj(uid, 0, dbname, time, &usrobj);
  464. if (rv == LAS_EVAL_TRUE) {
  465. *userdn = usrobj->userdn ? pool_strdup(pool, usrobj->userdn) : 0;
  466. DBG_PRINT1("Success ");
  467. }
  468. else {
  469. DBG_PRINT1("Failed ");
  470. }
  471. DBG_PRINT3("acl_usr_cache_get_userdn: uid = \"%s\" userdn = \"%s\"\n",
  472. uid, *userdn ? *userdn : "<NONE>");
  473. user_hash_crit_exit();
  474. return *userdn ? LAS_EVAL_TRUE : LAS_EVAL_FALSE;
  475. }
  476. int acl_usr_cache_userdn_check (const char *uid, const char *dbname,
  477. const char *userdn, const time_t time)
  478. {
  479. UserCacheObj *usrobj;
  480. int rv;
  481. user_hash_crit_enter();
  482. rv = acl_usr_cache_get_usrobj(uid, 0, dbname, time, &usrobj);
  483. if (rv == LAS_EVAL_TRUE && usrobj->userdn && userdn &&
  484. !strcmp(usrobj->userdn, userdn))
  485. {
  486. DBG_PRINT1("Success ");
  487. }
  488. else {
  489. rv = LAS_EVAL_FALSE;
  490. DBG_PRINT1("Failed ");
  491. }
  492. DBG_PRINT3("acl_usr_cache_userdn_check: uid = \"%s\" userdn = \"%s\"\n",
  493. uid, userdn ? userdn : "<NONE>");
  494. user_hash_crit_exit();
  495. return rv;
  496. }
  497. int acl_usr_cache_set_userdn (const char *uid, const char *dbname,
  498. const char *userdn, const time_t time)
  499. {
  500. int rv;
  501. /* acl_usr_cache_insert updates the existing un-expired entry or creates a
  502. * new one */
  503. rv = acl_usr_cache_insert(uid, dbname, userdn, 0, 0, 0, time);
  504. return rv;
  505. }
  506. int acl_usr_cache_get_group (const char *uid, const char *dbname,
  507. const time_t time, char **group,
  508. pool_handle_t *pool)
  509. {
  510. UserCacheObj *usrobj;
  511. int rv;
  512. *group = 0;
  513. user_hash_crit_enter();
  514. rv = acl_usr_cache_get_usrobj(uid, 0, dbname, time, &usrobj);
  515. if (rv == LAS_EVAL_TRUE) {
  516. *group = usrobj->group ? pool_strdup(pool, usrobj->group) : 0;
  517. DBG_PRINT1("Success ");
  518. }
  519. else {
  520. DBG_PRINT1("Failed ");
  521. }
  522. DBG_PRINT3("acl_usr_cache_get_group: uid = \"%s\" group = \"%s\"\n",
  523. uid, *group ? *group : "<NONE>");
  524. user_hash_crit_exit();
  525. return *group ? LAS_EVAL_TRUE : LAS_EVAL_FALSE;
  526. }
  527. int acl_usr_cache_set_group (const char *uid, const char *dbname,
  528. const char *group, const time_t time)
  529. {
  530. int rv;
  531. /* acl_usr_cache_insert updates the existing un-expired entry or creates a
  532. * new one */
  533. rv = acl_usr_cache_insert(uid, dbname, 0, 0, group, 0, time);
  534. return rv;
  535. }
  536. int acl_cert_cache_insert (void *cert_in, const char *dbname,
  537. const char *uid, const char *dn,
  538. const time_t time)
  539. {
  540. CERTCertificate *cert = (CERTCertificate *)cert_in;
  541. SECItem derCert = cert->derCert;
  542. int rv;
  543. rv = acl_usr_cache_insert(uid, dbname, dn, 0, 0, &derCert, time);
  544. return rv;
  545. }
  546. /* Returns LAS_EVAL_TRUE if the user's cache is valid and returns uid */
  547. int acl_cert_cache_get_uid (void *cert_in, const char *dbname,
  548. const time_t time, char **uid, char **dn,
  549. pool_handle_t *pool)
  550. {
  551. CERTCertificate *cert = (CERTCertificate *)cert_in;
  552. SECItem derCert = cert->derCert;
  553. UserCacheObj *usrobj = 0;
  554. int rv;
  555. rv = acl_usr_cache_get_usrobj(0, &derCert, dbname, time, &usrobj);
  556. if (rv == LAS_EVAL_TRUE && usrobj && usrobj->uid) {
  557. *uid = pool_strdup(pool, usrobj->uid);
  558. *dn = usrobj->userdn ? pool_strdup(pool, usrobj->userdn) : 0;
  559. }
  560. else {
  561. *uid = 0;
  562. *dn = 0;
  563. rv = LAS_EVAL_FALSE;
  564. }
  565. return rv;
  566. }