register.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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. /*
  42. * LAS registration interface
  43. */
  44. #include <netsite.h>
  45. #include <plhash.h>
  46. #include <base/systems.h>
  47. #include <base/util.h>
  48. #include <prlog.h>
  49. #include "permhash.h"
  50. #include <libaccess/nserror.h>
  51. #include <libaccess/acl.h>
  52. #include "aclpriv.h"
  53. #include <libaccess/aclproto.h>
  54. #include <libaccess/aclglobal.h>
  55. #include "aclcache.h"
  56. #include <libaccess/aclerror.h>
  57. /* This is to force aclspace.o into ns-httpd30.dll */
  58. static ACLGlobal_p *link_ACLGlobal = &ACLGlobal;
  59. /* This forces oneeval.o into ns-httpd30.dll */
  60. static ACLDispatchVector_t **link_nsacl_table = &__nsacl_table;
  61. ACLMethod_t ACLMethodDefault = ACL_METHOD_INVALID;
  62. ACLDbType_t ACLDbTypeDefault = ACL_DBTYPE_INVALID;
  63. static char *ACLDatabaseDefault = 0;
  64. ACLDbType_t ACL_DbTypeLdap = ACL_DBTYPE_INVALID;
  65. DbParseFn_t ACLDbParseFnTable[ACL_MAX_DBTYPE];
  66. void
  67. ACL_LasHashInit()
  68. {
  69. int i;
  70. (void)(link_ACLGlobal); /* get rid of defined but not used compiler warning */
  71. (void)(link_nsacl_table); /* get rid of defined but not used compiler warning */
  72. ACLLasEvalHash = PR_NewHashTable(0,
  73. PR_HashString,
  74. PR_CompareStrings,
  75. PR_CompareValues,
  76. &ACLPermAllocOps,
  77. NULL);
  78. PR_ASSERT(ACLLasEvalHash);
  79. ACLLasFlushHash = PR_NewHashTable(0,
  80. PR_HashString,
  81. PR_CompareStrings,
  82. PR_CompareValues,
  83. &ACLPermAllocOps,
  84. NULL);
  85. PR_ASSERT(ACLLasFlushHash);
  86. ACLMethodHash = PR_NewHashTable(ACL_MAX_METHOD,
  87. PR_HashCaseString,
  88. PR_CompareCaseStrings,
  89. PR_CompareValues,
  90. &ACLPermAllocOps,
  91. NULL);
  92. PR_ASSERT(ACLMethodHash);
  93. ACLDbTypeHash = PR_NewHashTable(ACL_MAX_DBTYPE,
  94. PR_HashCaseString,
  95. PR_CompareCaseStrings,
  96. PR_CompareValues,
  97. &ACLPermAllocOps,
  98. NULL);
  99. PR_ASSERT(ACLDbTypeHash);
  100. for (i = 0; i < ACL_MAX_DBTYPE; i++)
  101. ACLDbParseFnTable[i] = 0;
  102. ACLAttrGetterHash = PR_NewHashTable(256,
  103. PR_HashCaseString,
  104. PR_CompareCaseStrings,
  105. PR_CompareValues,
  106. &ACLPermAllocOps,
  107. NULL);
  108. PR_ASSERT(ACLDbTypeHash);
  109. ACLDbNameHash = PR_NewHashTable(0,
  110. PR_HashCaseString,
  111. PR_CompareCaseStrings,
  112. PR_CompareValues,
  113. &ACLPermAllocOps,
  114. ACL_DATABASE_POOL);
  115. PR_ASSERT(ACLDbNameHash);
  116. ACLUserLdbHash = PR_NewHashTable(0,
  117. PR_HashCaseString,
  118. PR_CompareCaseStrings,
  119. PR_CompareValues,
  120. &ACLPermAllocOps,
  121. NULL);
  122. PR_ASSERT(ACLUserLdbHash);
  123. return;
  124. }
  125. void
  126. ACL_LasHashDestroy()
  127. {
  128. if (ACLLasEvalHash) {
  129. PR_HashTableDestroy(ACLLasEvalHash);
  130. ACLLasEvalHash=NULL;
  131. }
  132. if (ACLLasFlushHash) {
  133. PR_HashTableDestroy(ACLLasFlushHash);
  134. ACLLasFlushHash=NULL;
  135. }
  136. }
  137. /* ACL_LasRegister
  138. * INPUT
  139. * errp NSError structure
  140. * attr_name E.g. "ip" or "dns" etc.
  141. * eval_func E.g. LASIpEval
  142. * flush_func Optional - E.g. LASIpFlush or NULL
  143. * OUTPUT
  144. * 0 on success, non-zero on failure
  145. */
  146. NSAPI_PUBLIC int
  147. ACL_LasRegister(NSErr_t *errp, const char *attr_name, LASEvalFunc_t eval_func,
  148. LASFlushFunc_t flush_func)
  149. {
  150. if ((!attr_name) || (!eval_func)) return -1;
  151. ACL_CritEnter();
  152. /* See if the function is already registered. If so, report and
  153. * error, but go ahead and replace it.
  154. */
  155. if (PR_HashTableLookup(ACLLasEvalHash, attr_name) != NULL) {
  156. nserrGenerate(errp, ACLERRDUPSYM, ACLERR3900, ACL_Program, 1,
  157. attr_name);
  158. }
  159. /* Put it in the hash tables */
  160. if (NULL == PR_HashTableAdd(ACLLasEvalHash, attr_name, (void *)eval_func)) {
  161. ACL_CritExit();
  162. return -1;
  163. }
  164. if (NULL ==
  165. PR_HashTableAdd(ACLLasFlushHash, attr_name, (void *)flush_func)) {
  166. ACL_CritExit();
  167. return -1;
  168. }
  169. ACL_CritExit();
  170. return 0;
  171. }
  172. /* ACL_LasFindEval
  173. * INPUT
  174. * errp NSError pointer
  175. * attr_name E.g. "ip" or "user" etc.
  176. * eval_funcp Where the function pointer is returned. NULL if the
  177. * function isn't registered.
  178. * Must be called in a critical section as ACLEvalHash is a global
  179. * variable.
  180. * OUTPUT
  181. * 0 on success, non-zero on failure
  182. */
  183. NSAPI_PUBLIC int
  184. ACL_LasFindEval(NSErr_t *errp, char *attr_name, LASEvalFunc_t *eval_funcp)
  185. {
  186. PR_ASSERT(attr_name);
  187. if (!attr_name) return -1;
  188. *eval_funcp = (LASEvalFunc_t)PR_HashTableLookup(ACLLasEvalHash, attr_name);
  189. return 0;
  190. }
  191. /* ACL_LasFindFlush
  192. * INPUT
  193. * errp NSError pointer
  194. * attr_name E.g. "ip" or "user" etc.
  195. * eval_funcp Where the function pointer is returned. NULL if the
  196. * function isn't registered.
  197. * OUTPUT
  198. * 0 on success, non-zero on failure
  199. */
  200. NSAPI_PUBLIC int
  201. ACL_LasFindFlush(NSErr_t *errp, char *attr_name, LASFlushFunc_t *flush_funcp)
  202. {
  203. PR_ASSERT(attr_name);
  204. if (!attr_name) return -1;
  205. *flush_funcp = (LASFlushFunc_t)PR_HashTableLookup(ACLLasFlushHash, attr_name);
  206. return 0;
  207. }
  208. /* ACL_MethodRegister
  209. * INPUT
  210. * name Method name string. Can be freed after return.
  211. * OUTPUT
  212. * &t Place to return the Method_t (>0)
  213. * retcode 0 on success, non-zero otherwise
  214. */
  215. int cur_method = 0; /* Use a static counter to generate the numbers */
  216. NSAPI_PUBLIC int
  217. ACL_MethodRegister(NSErr_t *errp, const char *name, ACLMethod_t *t)
  218. {
  219. ACLMethod_t rv;
  220. ACL_CritEnter();
  221. /* See if this is already registered */
  222. rv = (ACLMethod_t) PR_HashTableLookup(ACLMethodHash, name);
  223. if (rv != NULL) {
  224. *t = rv;
  225. ACL_CritExit();
  226. return 0;
  227. }
  228. /* To prevent the hash table from resizing, don't get to 32 entries */
  229. if (cur_method >= (ACL_MAX_METHOD-1)) {
  230. ACL_CritExit();
  231. return -1;
  232. }
  233. /* Put it in the hash table */
  234. if (NULL == PR_HashTableAdd(ACLMethodHash, name, (void *)(intptr_t)++cur_method)) {
  235. ACL_CritExit();
  236. return -1;
  237. }
  238. *t = (ACLMethod_t) (intptr_t)cur_method;
  239. ACL_CritExit();
  240. return 0;
  241. }
  242. NSAPI_PUBLIC int
  243. ACL_MethodFind(NSErr_t *errp, const char *name, ACLMethod_t *t)
  244. {
  245. ACLMethod_t rv;
  246. /* Don't have to get the Critical Section lock 'cause the only danger
  247. * would be if the hash table had to be resized. We created it with
  248. * room for 32 entries before that happens.
  249. */
  250. rv = (ACLMethod_t) PR_HashTableLookup(ACLMethodHash, name);
  251. if (rv != NULL) {
  252. *t = rv;
  253. return 0;
  254. }
  255. return -1;
  256. }
  257. typedef struct HashEnumArg_s {
  258. char **names;
  259. int count;
  260. } HashEnumArg_t;
  261. typedef HashEnumArg_t *HashEnumArg_p;
  262. static int acl_hash_enumerator (PLHashEntry *he, PRIntn i, void *arg)
  263. {
  264. HashEnumArg_t *info = (HashEnumArg_t *)arg;
  265. char **names = info->names;
  266. names[info->count++] = STRDUP((const char *)he->key);
  267. return names[info->count-1] ? 0 : -1;
  268. }
  269. int acl_registered_names(PLHashTable *ht, int count, char ***names)
  270. {
  271. HashEnumArg_t arg;
  272. int rv;
  273. if (count == 0) {
  274. *names = 0;
  275. return 0;
  276. }
  277. arg.names = (char **)MALLOC(count * sizeof(char *));
  278. arg.count = 0;
  279. if (!arg.names) return -1;
  280. rv = PR_HashTableEnumerateEntries(ht, acl_hash_enumerator, &arg);
  281. if (rv >= 0) {
  282. /* success */
  283. *names = arg.names;
  284. }
  285. else {
  286. *names = 0;
  287. }
  288. return rv;
  289. }
  290. NSAPI_PUBLIC int
  291. ACL_MethodNamesGet(NSErr_t *errp, char ***names, int *count)
  292. {
  293. *count = cur_method;
  294. return acl_registered_names (ACLMethodHash, *count, names);
  295. }
  296. NSAPI_PUBLIC int
  297. ACL_MethodNamesFree(NSErr_t *errp, char **names, int count)
  298. {
  299. int i;
  300. if (!names) return 0;
  301. for (i = count-1; i; i--) FREE(names[i]);
  302. FREE(names);
  303. return 0;
  304. }
  305. NSAPI_PUBLIC int
  306. ACL_DbTypeFind(NSErr_t *errp, const char *name, ACLDbType_t *t)
  307. {
  308. ACLDbType_t rv;
  309. /* Don't have to get the Critical Section lock 'cause the only danger
  310. * would be if the hash table had to be resized. We created it with
  311. * room for 32 entries before that happens.
  312. */
  313. rv = (ACLDbType_t) PR_HashTableLookup(ACLDbTypeHash, name);
  314. if (rv != NULL) {
  315. *t = rv;
  316. return 0;
  317. }
  318. return -1;
  319. }
  320. /* ACL_DbTypeRegister
  321. * INPUT
  322. * name DbType name string. Can be freed after return.
  323. * OUTPUT
  324. * &t Place to return the DbType (>0)
  325. * retcode 0 on success, non-zero otherwise
  326. */
  327. int cur_dbtype = 0; /* Use a static counter to generate the numbers */
  328. NSAPI_PUBLIC int
  329. ACL_DbTypeRegister(NSErr_t *errp, const char *name, DbParseFn_t func, ACLDbType_t *t)
  330. {
  331. ACLDbType_t rv;
  332. ACL_CritEnter();
  333. /* See if this is already registered */
  334. rv = (ACLDbType_t) PR_HashTableLookup(ACLDbTypeHash, name);
  335. if (rv != NULL) {
  336. *t = rv;
  337. ACLDbParseFnTable[(int)(PRSize)rv] = func;
  338. ACL_CritExit();
  339. return 0;
  340. }
  341. /* To prevent the hash table from resizing, don't get to 32 entries */
  342. if (cur_dbtype >= (ACL_MAX_DBTYPE-1)) {
  343. ACL_CritExit();
  344. return -1;
  345. }
  346. /* Put it in the hash table */
  347. if (NULL == PR_HashTableAdd(ACLDbTypeHash, name, (void *)(intptr_t)++cur_dbtype)) {
  348. ACL_CritExit();
  349. return -1;
  350. }
  351. *t = (ACLDbType_t) (intptr_t)cur_dbtype;
  352. ACLDbParseFnTable[cur_dbtype] = func;
  353. ACL_CritExit();
  354. return 0;
  355. }
  356. NSAPI_PUBLIC int
  357. ACL_DbTypeIsRegistered (NSErr_t *errp, const ACLDbType_t t)
  358. {
  359. return (0 < ((int)(PRSize)t) && ((int)(PRSize)t) <= cur_dbtype);
  360. }
  361. /* ACL_MethodIsEqual
  362. * RETURNS non-zero if equal.
  363. */
  364. NSAPI_PUBLIC int
  365. ACL_MethodIsEqual(NSErr_t *errp, const ACLMethod_t t1, const ACLMethod_t t2)
  366. {
  367. return (t1 == t2);
  368. }
  369. /* ACL_DbTypeIsEqual
  370. * RETURNS non-zero if equal.
  371. */
  372. NSAPI_PUBLIC int
  373. ACL_DbTypeIsEqual(NSErr_t *errp, const ACLDbType_t t1, const ACLDbType_t t2)
  374. {
  375. return (t1 == t2);
  376. }
  377. /* ACL_MethodNameIsEqual
  378. * Takes a method type and a method name and sees if they match.
  379. * Returns non-zero on match.
  380. */
  381. NSAPI_PUBLIC int
  382. ACL_MethodNameIsEqual(NSErr_t *errp, const ACLMethod_t t1, const char *name)
  383. {
  384. int rv;
  385. ACLMethod_t t2;
  386. rv = ACL_MethodFind(errp, name, &t2);
  387. if (rv)
  388. return (rv);
  389. else
  390. return (t1 == t2);
  391. }
  392. /* ACL_DbTypeNameIsEqual
  393. * Takes a dbtype type and a dbtype name and sees if they match.
  394. * Returns non-zero on match.
  395. */
  396. NSAPI_PUBLIC int
  397. ACL_DbTypeNameIsEqual(NSErr_t *errp, const ACLDbType_t t1, const char *name)
  398. {
  399. int rv;
  400. ACLDbType_t t2;
  401. rv = ACL_DbTypeFind(errp, name, &t2);
  402. if (rv)
  403. return (rv);
  404. else
  405. return (t1 == t2);
  406. }
  407. /* ACL_MethodGetDefault
  408. */
  409. NSAPI_PUBLIC ACLMethod_t
  410. ACL_MethodGetDefault(NSErr_t *errp)
  411. {
  412. return (ACLMethodDefault);
  413. }
  414. /* ACL_MethodSetDefault
  415. */
  416. NSAPI_PUBLIC int
  417. ACL_MethodSetDefault(NSErr_t *errp, const ACLMethod_t t)
  418. {
  419. ACLMethodDefault = t;
  420. return 0;
  421. }
  422. /* ACL_DbTypeGetDefault
  423. */
  424. NSAPI_PUBLIC ACLDbType_t
  425. ACL_DbTypeGetDefault(NSErr_t *errp)
  426. {
  427. return (ACLDbTypeDefault);
  428. }
  429. /* ACL_DbTypeSetDefault
  430. */
  431. NSAPI_PUBLIC int
  432. ACL_DbTypeSetDefault(NSErr_t *errp, ACLDbType_t t)
  433. {
  434. ACLDbTypeDefault = t;
  435. return 0;
  436. }
  437. /* ACL_DatabaseGetDefault
  438. */
  439. NSAPI_PUBLIC const char *
  440. ACL_DatabaseGetDefault(NSErr_t *errp)
  441. {
  442. return (ACLDatabaseDefault);
  443. }
  444. /* ACL_DatabaseSetDefault
  445. */
  446. NSAPI_PUBLIC int
  447. ACL_DatabaseSetDefault(NSErr_t *errp, const char *dbname)
  448. {
  449. ACLDbType_t dbtype;
  450. int rv;
  451. void *db;
  452. if (!dbname || !*dbname) return LAS_EVAL_FAIL;
  453. rv = ACL_DatabaseFind(errp, dbname, &dbtype, &db);
  454. if (rv != LAS_EVAL_TRUE) return -1;
  455. if (ACLDatabaseDefault) pool_free(ACL_DATABASE_POOL, ACLDatabaseDefault);
  456. ACL_DbTypeSetDefault(errp, dbtype);
  457. ACLDatabaseDefault = pool_strdup(ACL_DATABASE_POOL, dbname);
  458. return ACLDatabaseDefault ? 0 : -1;
  459. }
  460. /* ACL_AuthInfoGetMethod
  461. * INPUT
  462. * auth_info A PList of the authentication name/value pairs as
  463. * provided by EvalTestRights to the LAS.
  464. * OUTPUT
  465. * *t The Method number. This can be the default method
  466. number if the auth_info PList doesn't explicitly have a Method entry.
  467. * retcode 0 on success.
  468. */
  469. NSAPI_PUBLIC int
  470. ACL_AuthInfoGetMethod(NSErr_t *errp, PList_t auth_info, ACLMethod_t *t)
  471. {
  472. ACLMethod_t *methodp;
  473. if (!auth_info ||
  474. PListGetValue(auth_info, ACL_ATTR_METHOD_INDEX, (void **)&methodp, NULL) < 0)
  475. {
  476. /* No entry for "method" */
  477. *t = ACLMethodDefault;
  478. } else {
  479. *t = *methodp;
  480. }
  481. return 0;
  482. }
  483. /* ACL_AuthInfoSetMethod
  484. * INPUT
  485. * auth_info A PList of the authentication name/value pairs as
  486. * provided by EvalTestRights to the LAS.
  487. * t The Method number.
  488. * OUTPUT
  489. * retcode 0 on success.
  490. */
  491. NSAPI_PUBLIC int
  492. ACL_AuthInfoSetMethod(NSErr_t *errp, PList_t auth_info, ACLMethod_t t)
  493. {
  494. ACLMethod_t *methodp;
  495. int rv;
  496. if (auth_info) {
  497. rv = PListGetValue(auth_info, ACL_ATTR_METHOD_INDEX, (void **)&methodp,
  498. NULL);
  499. if (rv < 0) {
  500. /* No entry for "method" */
  501. methodp = (ACLMethod_t *)PERM_MALLOC(sizeof(ACLMethod_t));
  502. if (!methodp) return -1;
  503. *methodp = t;
  504. PListInitProp(auth_info, ACL_ATTR_METHOD_INDEX, ACL_ATTR_METHOD, methodp, 0);
  505. }
  506. else {
  507. /* replace the old entry */
  508. if (!methodp) return -1;
  509. *methodp = t;
  510. }
  511. }
  512. else {
  513. return -1;
  514. }
  515. return 0;
  516. }
  517. /* ACL_AuthInfoSetDbname
  518. * INPUT
  519. * auth_info A PList of the authentication name/value pairs as
  520. * provided by EvalTestRights to the LAS.
  521. * dbname Name of the new auth_info database.
  522. * OUTPUT
  523. * retcode 0 on success.
  524. */
  525. NSAPI_PUBLIC int
  526. ACL_AuthInfoSetDbname(NSErr_t *errp, PList_t auth_info, const char *dbname)
  527. {
  528. ACLDbType_t *dbtype = NULL;
  529. ACLDbType_t *t2;
  530. char *copy;
  531. char *n2;
  532. void *db;
  533. int old1;
  534. int old2;
  535. int rv;
  536. if (auth_info) {
  537. dbtype = (ACLDbType_t *)PERM_MALLOC(sizeof(ACLDbType_t));
  538. if (!dbtype) {
  539. /* out of memory */
  540. return -1;
  541. }
  542. rv = ACL_DatabaseFind(errp, dbname, dbtype, (void **)&db);
  543. if (rv != LAS_EVAL_TRUE) {
  544. PERM_FREE(dbtype);
  545. return -1;
  546. }
  547. /* Check the existing entry */
  548. old1 = PListGetValue(auth_info, ACL_ATTR_DBTYPE_INDEX, (void **)&t2,
  549. NULL);
  550. old2 = PListGetValue(auth_info, ACL_ATTR_DATABASE_INDEX, (void **)&n2,
  551. NULL);
  552. if (old1 >= 0 && old2 >= 0) {
  553. /* check if the old entry is same */
  554. if (ACL_DbTypeIsEqual(errp, *dbtype, *t2)) {
  555. /* Nothing to do */
  556. PERM_FREE(dbtype);
  557. return 0;
  558. }
  559. }
  560. /* free the old entries */
  561. if (old1 >= 0) {
  562. PListDeleteProp(auth_info, ACL_ATTR_DBTYPE_INDEX, ACL_ATTR_DBTYPE);
  563. PERM_FREE(t2);
  564. }
  565. if (old2 >= 0) {
  566. PListDeleteProp(auth_info, ACL_ATTR_DATABASE_INDEX, ACL_ATTR_DATABASE);
  567. PERM_FREE(n2);
  568. }
  569. /* Create new entries for "dbtype" & "dbname" */
  570. copy = (char *)PERM_STRDUP(dbname);
  571. if (!copy) {
  572. PERM_FREE(dbtype);
  573. return -1;
  574. }
  575. PListInitProp(auth_info, ACL_ATTR_DATABASE_INDEX,
  576. ACL_ATTR_DATABASE, copy, 0);
  577. PListInitProp(auth_info, ACL_ATTR_DBTYPE_INDEX, ACL_ATTR_DBTYPE,
  578. dbtype, 0);
  579. }
  580. else {
  581. return -1;
  582. }
  583. return 0;
  584. }
  585. /* ACL_AuthInfoGetDbType
  586. * INPUT
  587. * auth_info A PList of the authentication name/value pairs as
  588. * provided by EvalTestRights to the LAS.
  589. * OUTPUT
  590. * *t The DbType number. This can be the default dbtype
  591. * number if the auth_info PList doesn't explicitly
  592. * have a DbType entry.
  593. * retcode 0 on success.
  594. */
  595. NSAPI_PUBLIC int
  596. ACL_AuthInfoGetDbType(NSErr_t *errp, PList_t auth_info, ACLDbType_t *t)
  597. {
  598. ACLDbType_t *dbtypep;
  599. if (!auth_info ||
  600. PListGetValue(auth_info, ACL_ATTR_DBTYPE_INDEX, (void **)&dbtypep, NULL) < 0)
  601. {
  602. /* No entry for "dbtype" */
  603. *t = ACLDbTypeDefault;
  604. } else {
  605. *t = *dbtypep;
  606. }
  607. return 0;
  608. }
  609. /* ACL_AuthInfoGetDbname
  610. * INPUT
  611. * auth_info A PList of the authentication name/value pairs as
  612. * provided by EvalTestRights to the LAS.
  613. * OUTPUT
  614. * dbname The database name. This can be the default database
  615. * name if the auth_info PList doesn't explicitly
  616. * have a database entry.
  617. * retcode 0 on success.
  618. */
  619. NSAPI_PUBLIC int
  620. ACL_AuthInfoGetDbname(PList_t auth_info, char **dbname)
  621. {
  622. char *dbstr;
  623. if (!auth_info ||
  624. PListGetValue(auth_info, ACL_ATTR_DATABASE_INDEX, (void **)&dbstr, NULL) < 0)
  625. {
  626. /* No entry for "database" */
  627. dbstr = ACLDatabaseDefault;
  628. }
  629. /* else the value was already set by the PListGetValue call */
  630. *dbname = dbstr;
  631. return 0;
  632. }
  633. NSAPI_PUBLIC DbParseFn_t
  634. ACL_DbTypeParseFn(NSErr_t *errp, const ACLDbType_t dbtype)
  635. {
  636. if (ACL_DbTypeIsRegistered(errp, dbtype))
  637. return ACLDbParseFnTable[(int)(PRSize)dbtype];
  638. else
  639. return 0;
  640. }
  641. /* The hash table is keyed by attribute name, and contains pointers to the
  642. * PRCList headers. These in turn, circularly link a set of AttrGetter_s
  643. * structures.
  644. */
  645. NSAPI_PUBLIC int
  646. ACL_AttrGetterRegister(NSErr_t *errp, const char *attr, ACLAttrGetterFn_t fn,
  647. ACLMethod_t m, ACLDbType_t d, int position, void *arg)
  648. {
  649. ACLAttrGetter_t *getter;
  650. PLHashEntry **hep;
  651. if (position != ACL_AT_FRONT && position != ACL_AT_END) {
  652. return -1;
  653. }
  654. ACL_CritEnter();
  655. hep = PR_HashTableRawLookup(ACLAttrGetterHash, PR_HashCaseString(attr), attr);
  656. /* Now, allocate the current entry */
  657. getter = (ACLAttrGetter_t *)CALLOC(sizeof(ACLAttrGetter_t));
  658. if (getter == NULL) {
  659. ACL_CritExit();
  660. return -1;
  661. }
  662. getter->method = m;
  663. getter->dbtype = d;
  664. getter->fn = fn;
  665. getter->arg = arg;
  666. if (*hep == 0) { /* New entry */
  667. PR_INIT_CLIST(&getter->list);
  668. if (NULL == PR_HashTableAdd(ACLAttrGetterHash, attr, (void *)getter)) {
  669. FREE(getter);
  670. ACL_CritExit();
  671. return -1;
  672. }
  673. }
  674. else {
  675. ACLAttrGetter_t *head = (ACLAttrGetter_t *)((*hep)->value);
  676. PR_INSERT_BEFORE(&getter->list, &head->list);
  677. if (position == ACL_AT_FRONT) {
  678. /* Set new head of list */
  679. (*hep)->value = (void *)getter;
  680. }
  681. }
  682. ACL_CritExit();
  683. return 0;
  684. }
  685. NSAPI_PUBLIC int
  686. ACL_AttrGetterFind(NSErr_t *errp, const char *attr,
  687. ACLAttrGetterList_t *getters)
  688. {
  689. *getters = PR_HashTableLookup(ACLAttrGetterHash, attr);
  690. if (*getters)
  691. return 0;
  692. else
  693. return -1;
  694. }
  695. NSAPI_PUBLIC
  696. ACLAttrGetter_t * ACL_AttrGetterFirst(ACLAttrGetterList_t *getters)
  697. {
  698. ACLAttrGetter_t * first = 0;
  699. if (getters && *getters) {
  700. first = (ACLAttrGetter_t *)(*getters);
  701. }
  702. return first;
  703. }
  704. NSAPI_PUBLIC ACLAttrGetter_t *
  705. ACL_AttrGetterNext(ACLAttrGetterList_t *getters, ACLAttrGetter_t *last)
  706. {
  707. ACLAttrGetter_t *head;
  708. ACLAttrGetter_t *next = 0;
  709. if (getters && *getters && last) {
  710. head = (ACLAttrGetter_t *)(*getters);
  711. if (head) {
  712. /* End of list? */
  713. if (last != (ACLAttrGetter_t *)PR_LIST_TAIL(&head->list)) {
  714. /* No, get next entry */
  715. next = (ACLAttrGetter_t *)PR_NEXT_LINK(&last->list);
  716. }
  717. }
  718. }
  719. return next;
  720. }