register.cpp 22 KB

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