oneeval.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /*
  13. * Description (acleval.c)
  14. *
  15. * This module provides functions for evaluating Access Control List
  16. * (ACL) structures in memory.
  17. *
  18. */
  19. #include <string.h>
  20. #include <sys/types.h>
  21. #include <assert.h>
  22. #include <netsite.h>
  23. #include <base/systems.h>
  24. #include <base/crit.h>
  25. #include <libaccess/nserror.h>
  26. #include <libaccess/acl.h>
  27. #include "aclpriv.h"
  28. #include <libaccess/aclproto.h>
  29. #include <libaccess/las.h>
  30. #include <libaccess/symbols.h>
  31. #include <libaccess/aclerror.h>
  32. #include <libaccess/aclglobal.h>
  33. #include <libaccess/dbtlibaccess.h>
  34. #include <libaccess/aclerror.h>
  35. #include "access_plhash.h"
  36. #include "aclutil.h"
  37. #include "aclcache.h"
  38. #include "oneeval.h"
  39. #define NO_ACL_HASH_FUNCS
  40. #include "permhash.h"
  41. static ACLDispatchVector_t __nsacl_vector = {
  42. /* Error frame stack support */
  43. nserrDispose,
  44. nserrFAlloc,
  45. nserrFFree,
  46. nserrGenerate,
  47. /* Property list support */
  48. PListAssignValue,
  49. PListCreate,
  50. PListDefProp,
  51. PListDeleteProp,
  52. PListFindValue,
  53. PListInitProp,
  54. PListNew,
  55. PListDestroy,
  56. PListGetValue,
  57. PListNameProp,
  58. PListSetType,
  59. PListSetValue,
  60. PListEnumerate,
  61. PListDuplicate,
  62. PListGetPool,
  63. /* ACL attribute handling */
  64. ACL_LasRegister,
  65. /* method/dbtype registration routines */
  66. ACL_MethodRegister,
  67. ACL_MethodIsEqual,
  68. ACL_MethodNameIsEqual,
  69. ACL_MethodFind,
  70. ACL_MethodGetDefault,
  71. ACL_MethodSetDefault,
  72. ACL_AuthInfoGetMethod,
  73. ACL_DbTypeRegister,
  74. ACL_DbTypeIsEqual,
  75. ACL_DbTypeNameIsEqual,
  76. ACL_DbTypeFind,
  77. ACL_DbTypeGetDefault,
  78. ACL_AuthInfoGetDbType,
  79. ACL_DbTypeIsRegistered,
  80. ACL_DbTypeParseFn,
  81. ACL_AttrGetterRegister,
  82. ACL_ModuleRegister,
  83. ACL_GetAttribute,
  84. ACL_DatabaseRegister,
  85. ACL_DatabaseFind,
  86. ACL_DatabaseSetDefault,
  87. NULL,
  88. ACL_AuthInfoGetDbname,
  89. ACL_CacheFlushRegister,
  90. ACL_CacheFlush,
  91. /* ACL language and file interfaces */
  92. NULL /* ex ACL_ParseFile*/,
  93. ACL_ParseString,
  94. NULL /* ex ACL_WriteString*/,
  95. NULL /* ex ACL_WriteFile */,
  96. NULL /* ex ACL_FileRenameAcl */,
  97. NULL /* ex ACL_FileDeleteAcl */,
  98. NULL /* ex ACL_FileGetAcl */,
  99. NULL /* ex ACL_FileSetAcl */,
  100. /* ACL Expression construction interfaces */
  101. ACL_ExprNew,
  102. ACL_ExprDestroy,
  103. ACL_ExprSetPFlags,
  104. ACL_ExprClearPFlags,
  105. ACL_ExprTerm,
  106. ACL_ExprNot,
  107. ACL_ExprAnd,
  108. ACL_ExprOr,
  109. ACL_ExprAddAuthInfo,
  110. ACL_ExprAddArg,
  111. ACL_ExprSetDenyWith,
  112. ACL_ExprGetDenyWith,
  113. ACL_ExprAppend,
  114. /* ACL manipulation */
  115. ACL_AclNew,
  116. ACL_AclDestroy,
  117. /* ACL list manipulation */
  118. ACL_ListNew,
  119. ACL_ListConcat,
  120. ACL_ListAppend,
  121. ACL_ListDestroy,
  122. ACL_ListFind,
  123. ACL_ListAclDelete,
  124. ACL_ListGetNameList,
  125. ACL_NameListDestroy,
  126. /* ACL evaluation */
  127. ACL_EvalTestRights,
  128. ACL_EvalNew,
  129. ACL_EvalDestroy,
  130. ACL_EvalSetACL,
  131. ACL_EvalGetSubject,
  132. ACL_EvalSetSubject,
  133. ACL_EvalGetResource,
  134. ACL_EvalSetResource,
  135. /* Access to critical section for ACL cache */
  136. ACL_CritEnter,
  137. ACL_CritExit,
  138. /* Miscellaneous functions */
  139. ACL_AclGetTag,
  140. ACL_ListGetFirst,
  141. ACL_ListGetNext,
  142. /* Functions added after ES 3.0 release */
  143. ACL_DatabaseGetDefault,
  144. ACL_SetDefaultResult,
  145. ACL_GetDefaultResult
  146. };
  147. NSAPI_PUBLIC ACLDispatchVector_t *__nsacl_table = &__nsacl_vector;
  148. int ACLEvalAce(
  149. NSErr_t *errp,
  150. ACLEvalHandle_t *acleval,
  151. ACLExprHandle_t *ace,
  152. ACLCachable_t *cachable,
  153. PList_t autharray[],
  154. PList_t global_auth
  155. )
  156. {
  157. ACLCachable_t local_cachable;
  158. int result;
  159. ACLExprEntry_t *expr;
  160. int expr_index = 0;
  161. expr = &ace->expr_arry[0];
  162. *cachable = ACL_INDEF_CACHABLE;
  163. while (TRUE)
  164. {
  165. local_cachable = ACL_NOT_CACHABLE;
  166. /* Call the LAS driver */
  167. if (!expr->las_eval_func) {
  168. ACL_CritEnter();
  169. if (!expr->las_eval_func) { /* Must check again after locking */
  170. ACL_LasFindEval(errp, expr->attr_name, &expr->las_eval_func);
  171. if (!expr->las_eval_func) { /* Couldn't find it */
  172. ACL_CritExit();
  173. return LAS_EVAL_INVALID;
  174. }
  175. }
  176. ACL_CritExit();
  177. }
  178. result = (*expr->las_eval_func)(
  179. errp,
  180. expr->attr_name,
  181. expr->comparator,
  182. expr->attr_pattern,
  183. &local_cachable,
  184. &expr->las_cookie,
  185. acleval->subject,
  186. acleval->resource,
  187. autharray ? autharray[expr_index] : NULL,
  188. global_auth);
  189. /* Evaluate the cachable value */
  190. if (local_cachable < *cachable) {
  191. /* Take the minimum value */
  192. *cachable = local_cachable;
  193. }
  194. /* Evaluate the return code */
  195. switch (result) {
  196. case LAS_EVAL_TRUE:
  197. if (expr->true_idx < 0)
  198. return (expr->true_idx);
  199. else {
  200. expr_index = expr->true_idx;
  201. expr = &ace->expr_arry[expr->true_idx];
  202. }
  203. break;
  204. case LAS_EVAL_FALSE:
  205. if (expr->false_idx < 0)
  206. return (expr->false_idx);
  207. else {
  208. expr_index = expr->false_idx;
  209. expr = &ace->expr_arry[expr->false_idx];
  210. }
  211. break;
  212. default:
  213. return (result);
  214. }
  215. }
  216. }
  217. int
  218. ACL_EvalDestroyContext(ACLListCache_t *cache)
  219. {
  220. ACLAceEntry_t *cur_ace, *next_ace;
  221. ACLAceNumEntry_t *cur_num_p, *next_num_p;
  222. if (!cache)
  223. return 0;
  224. PR_HashTableDestroy(cache->Table);
  225. cache->Table = NULL;
  226. cur_ace = cache->acelist;
  227. cache->acelist = NULL;
  228. while (cur_ace) {
  229. if (cur_ace->autharray)
  230. PERM_FREE(cur_ace->autharray);
  231. if ((cur_ace->global_auth) &&
  232. (cur_ace->acep->expr_type == ACL_EXPR_TYPE_AUTH))
  233. PListDestroy(cur_ace->global_auth);
  234. next_ace = cur_ace->next;
  235. PERM_FREE(cur_ace);
  236. cur_ace = next_ace;
  237. }
  238. cur_num_p = cache->chain_head;
  239. cache->chain_head = NULL;
  240. while (cur_num_p) {
  241. next_num_p = cur_num_p->chain;
  242. PERM_FREE(cur_num_p);
  243. cur_num_p = next_num_p;
  244. }
  245. PERM_FREE(cache);
  246. return 0;
  247. }
  248. /* ACLEvalBuildContext
  249. * Builds three structures:
  250. * Table - A hash table of all access rights referenced by any ACE in any
  251. * of the ACLs in this list. Each hash entry then has a list of
  252. * the relevant ACEs, in the form of indexes to the ACE linked
  253. * list.
  254. * ACE List - A linked list of all the ACEs in the proper evaluation order.
  255. *
  256. * For concurrency control, the caller must call ACL_CritEnter()
  257. */
  258. int
  259. ACLEvalBuildContext(
  260. NSErr_t *errp,
  261. ACLEvalHandle_t *acleval)
  262. {
  263. ACLHandle_t *acl;
  264. ACLExprHandle_t *ace;
  265. int ace_cnt = -1;
  266. ACLAceEntry_t *acelast = NULL, *new_ace;
  267. ACLAceNumEntry_t *entry, *temp_entry;
  268. char **argp;
  269. ACLListCache_t *cache;
  270. ACLWrapper_t *wrapper;
  271. PList_t curauthplist=NULL, absauthplist=NULL;
  272. int i, rv;
  273. ACLExprEntry_t *expr;
  274. PList_t authplist;
  275. /* Allocate the cache context and link it into the ACLListHandle */
  276. cache = (ACLListCache_t *)PERM_CALLOC(sizeof(ACLListCache_t));
  277. if (cache == NULL) {
  278. nserrGenerate(errp, ACLERRNOMEM, ACLERR4010, ACL_Program, 0);
  279. goto error;
  280. }
  281. /* Allocate the access rights hash table */
  282. cache->Table = PR_NewHashTable(0,
  283. PR_HashString,
  284. PR_CompareStrings,
  285. PR_CompareValues,
  286. &ACLPermAllocOps,
  287. NULL);
  288. if (cache->Table == NULL) {
  289. nserrGenerate(errp, ACLERRNOMEM, ACLERR4000, ACL_Program, 1,
  290. XP_GetAdminStr(DBT_EvalBuildContextUnableToCreateHash));
  291. goto error;
  292. }
  293. wrapper = acleval->acllist->acl_list_head;
  294. /* Loop through all the ACLs in the list */
  295. while (wrapper)
  296. {
  297. acl = wrapper->acl;
  298. ace = acl->expr_list_head;
  299. while (ace) /* Loop through all the ACEs in this ACL */
  300. {
  301. /* allocate a new ace list entry and link it in to the ordered
  302. * list.
  303. */
  304. new_ace = (ACLAceEntry_t *)PERM_CALLOC(sizeof(ACLAceEntry_t));
  305. if (new_ace == (ACLAceEntry_t *)NULL) {
  306. nserrGenerate(errp, ACLERRNOMEM, ACLERR4020, ACL_Program, 1,
  307. XP_GetAdminStr(DBT_EvalBuildContextUnableToAllocAceEntry));
  308. goto error;
  309. }
  310. new_ace->acep = ace;
  311. ace_cnt++;
  312. if (cache->acelist == NULL)
  313. cache->acelist = acelast = new_ace;
  314. else {
  315. if(acelast)
  316. acelast->next = new_ace;
  317. acelast = new_ace;
  318. new_ace->acep = ace;
  319. }
  320. new_ace->next = NULL;
  321. argp = ace->expr_argv;
  322. switch (ace->expr_type)
  323. {
  324. case ACL_EXPR_TYPE_ALLOW:
  325. case ACL_EXPR_TYPE_DENY:
  326. /* Add this ACE to the appropriate entries in the access rights
  327. * hash table
  328. */
  329. while (*argp)
  330. {
  331. entry = (ACLAceNumEntry_t *)PERM_CALLOC(sizeof(ACLAceNumEntry_t));
  332. if (entry == (ACLAceNumEntry_t *)NULL) {
  333. nserrGenerate(errp, ACLERRNOMEM, ACLERR4030, ACL_Program, 1,
  334. XP_GetAdminStr(DBT_EvalBuildContextUnableToAllocAceEntry));
  335. goto error;
  336. }
  337. if (cache->chain_head == NULL)
  338. cache->chain_head = cache->chain_tail = entry;
  339. else {
  340. cache->chain_tail->chain = entry;
  341. cache->chain_tail = entry;
  342. }
  343. entry->acenum = ace_cnt;
  344. /*
  345. * OK to call PL_HasTableLookup() even though it mods
  346. * the Table as this routine is called in critical section.
  347. */
  348. temp_entry = (ACLAceNumEntry_t *)PL_HashTableLookup(cache->Table, *argp);
  349. /* the first ACE for this right? */
  350. if (temp_entry) {
  351. /* Link it in at the end */
  352. while (temp_entry->next) {
  353. temp_entry = temp_entry->next;
  354. }
  355. temp_entry->next = entry;
  356. } else /* just link it in */
  357. PR_HashTableAdd(cache->Table, *argp, entry);
  358. argp++;
  359. }
  360. /* See if any of the clauses require authentication. */
  361. if (curauthplist) {
  362. for (i = 0; i < ace->expr_term_index; i++) {
  363. expr = &ace->expr_arry[i];
  364. rv = PListFindValue(curauthplist, expr->attr_name,
  365. NULL, &authplist);
  366. if (rv > 0) {
  367. /* First one for this ACE? */
  368. if (!new_ace->autharray) {
  369. new_ace->autharray = (PList_t *)PERM_CALLOC(sizeof(PList_t) * ace->expr_term_index);
  370. if (!new_ace->autharray) {
  371. nserrGenerate(errp, ACLERRNOMEM, ACLERR4040, ACL_Program, 1,
  372. XP_GetAdminStr(DBT_EvalBuildContextUnableToAllocAuthPointerArray));
  373. goto error;
  374. }
  375. }
  376. new_ace->autharray[i] = authplist;
  377. }
  378. }
  379. }
  380. break;
  381. case ACL_EXPR_TYPE_AUTH:
  382. /* Allocate the running auth tables if none yet */
  383. if (!curauthplist) {
  384. curauthplist = PListNew(NULL);
  385. if (!curauthplist) {
  386. nserrGenerate(errp, ACLERRNOMEM, ACLERR4050, ACL_Program, 1,
  387. XP_GetAdminStr(DBT_EvalBuildContextUnableToAllocAuthPlist));
  388. goto error;
  389. }
  390. absauthplist = PListNew(NULL);
  391. if (!absauthplist) {
  392. nserrGenerate(errp, ACLERRNOMEM, ACLERR4050, ACL_Program, 1,
  393. XP_GetAdminStr(DBT_EvalBuildContextUnableToAllocAuthPlist));
  394. goto error;
  395. }
  396. } else { /* duplicate the existing auth table */
  397. curauthplist = PListDuplicate(curauthplist, NULL, 0);
  398. if (!curauthplist) {
  399. nserrGenerate(errp, ACLERRNOMEM, ACLERR4050, ACL_Program, 1,
  400. XP_GetAdminStr(DBT_EvalBuildContextUnableToAllocAuthPlist));
  401. goto error;
  402. }
  403. }
  404. /* For each listed attribute */
  405. while (*argp) {
  406. /* skip any attributes that were absoluted */
  407. if (PListFindValue(absauthplist, *argp, NULL, NULL) < 0){
  408. /* Save pointer to the property list */
  409. PListInitProp(curauthplist, 0, *argp, ace->expr_auth, ace->expr_auth);
  410. if (IS_ABSOLUTE(ace->expr_flags))
  411. PListInitProp(absauthplist, 0, *argp, NULL, NULL);
  412. }
  413. argp++;
  414. }
  415. break;
  416. case ACL_EXPR_TYPE_RESPONSE:
  417. (void) ACL_ExprGetDenyWith(NULL, ace, &cache->deny_type,
  418. &cache->deny_response);
  419. break;
  420. default:
  421. PR_ASSERT(0);
  422. } /* switch expr_type */
  423. new_ace->global_auth = curauthplist;
  424. ace = ace->expr_next;
  425. } /* while (ace) */
  426. /* Next ACL please */
  427. wrapper = wrapper->wrap_next;
  428. }
  429. if (absauthplist)
  430. PListDestroy(absauthplist);
  431. /* This must be done last to avoid a race in initialization */
  432. acleval->acllist->cache = (void *)cache;
  433. return 0;
  434. error:
  435. if (curauthplist)
  436. PListDestroy(curauthplist);
  437. if (absauthplist)
  438. PListDestroy(absauthplist);
  439. if (cache) {
  440. ACL_EvalDestroyContext(cache);
  441. }
  442. acleval->acllist->cache = NULL;
  443. return ACL_RES_ERROR;
  444. }
  445. /* ACL_InvalidateSubjectPList
  446. * Given a new authentication plist, enumerate the plist and for each
  447. * key in the plist, search for the matching key in the subject plist
  448. * and delete any matches. E.g. "user", "group".
  449. */
  450. void
  451. ACL_InvalidateSubjectPList(char *attr, const void *value, void *user_data)
  452. {
  453. PList_t subject = (PList_t)user_data;
  454. PListDeleteProp(subject, 0, attr);
  455. return;
  456. }
  457. NSAPI_PUBLIC int ACL_SetDefaultResult (NSErr_t *errp,
  458. ACLEvalHandle_t *acleval,
  459. int result)
  460. {
  461. int rv;
  462. switch(result) {
  463. case ACL_RES_ALLOW:
  464. case ACL_RES_DENY:
  465. case ACL_RES_FAIL:
  466. case ACL_RES_INVALID:
  467. acleval->default_result = result;
  468. rv = 0;
  469. break;
  470. default:
  471. rv = -1;
  472. }
  473. return rv;
  474. }
  475. NSAPI_PUBLIC int ACL_GetDefaultResult (ACLEvalHandle_t *acleval)
  476. {
  477. return acleval->default_result;
  478. }
  479. /* ACL_INTEvalTestRights
  480. * INPUT
  481. * *errp The usual error context stack
  482. * *acleval A list of ACLs
  483. * **rights An array of strings listing the requested rights
  484. * **map_generic An array of strings listing the specific rights
  485. * that map from the generic rights.
  486. * OUTPUT
  487. * **deny_type bong file type passed on the way back out
  488. * **deny_response bong file pathname passed on the way back out
  489. * **acl_tag Name of the ACL that denies access
  490. * *expr_num ACE number within the denying ACL
  491. * *cachable Is the result cachable?
  492. */
  493. static int
  494. ACL_INTEvalTestRights(
  495. NSErr_t *errp,
  496. ACLEvalHandle_t *acleval,
  497. const char **rights,
  498. const char **map_generic,
  499. char **deny_type,
  500. char **deny_response,
  501. char **acl_tag,
  502. int *expr_num,
  503. ACLCachable_t *cachable)
  504. {
  505. struct rights_ent {
  506. char right[64]; /* lowercase-ed rights string */
  507. int result; /* Interim result value */
  508. int absolute; /* ACE with absolute keyword */
  509. int count; /* # specific + generic rights */
  510. ACLAceNumEntry_t *acelist[ACL_MAX_GENERIC+1];
  511. /* List of relevant ACEs */
  512. };
  513. struct rights_ent *rarray_p;
  514. struct rights_ent rights_arry[ACL_MAX_TEST_RIGHTS];
  515. ACLAceNumEntry_t *alllist; /* List of ACEs for "all" rights */
  516. ACLAceEntry_t *cur_ace;
  517. ACLListCache_t *cache;
  518. int rights_cnt = 0;
  519. int prev_acenum, cur_acenum;
  520. int i, j, right_num, delta;
  521. ACLCachable_t ace_cachable;
  522. int result;
  523. int absolute = 0;
  524. int skipflag;
  525. int g_num; /* index into the generic rights array. */
  526. const char **g_rights;
  527. PList_t global_auth=NULL;
  528. int allow_error = 0;
  529. int allow_absolute = 0;
  530. char *allow_tag = NULL;
  531. int allow_num = 0;
  532. int default_result = ACL_GetDefaultResult(acleval);
  533. *acl_tag = NULL;
  534. *expr_num = 0;
  535. *cachable = ACL_INDEF_CACHABLE;
  536. /*
  537. * The acleval contains the list of acis we are asking about.
  538. * In our case it's always of length 1.
  539. * The acleval is a per aclpb structure but
  540. * the acllist is a global structure derived from the global
  541. * aci cache--so access to acllist is multi-threaded.
  542. * Hence, for example the use of the "read-only" hash
  543. * lookup routines in this function--ACL_EvalTestRights()
  544. * is called in a "reader only context" so this code is therefore
  545. * thread-safe.
  546. */
  547. if (acleval->acllist == ACL_LIST_NO_ACLS) return ACL_RES_ALLOW;
  548. /* Build up the access right - indexed structures */
  549. if (acleval->acllist->cache == NULL) {
  550. ACL_CritEnter();
  551. if (acleval->acllist->cache == NULL) { /* Check again */
  552. if (ACLEvalBuildContext(errp, acleval) == ACL_RES_ERROR) {
  553. nserrGenerate(errp, ACLERRINTERNAL, ACLERR4110, ACL_Program,
  554. 1, XP_GetAdminStr(DBT_EvalTestRightsEvalBuildContextFailed));
  555. ACL_CritExit();
  556. return ACL_RES_ERROR;
  557. }
  558. }
  559. ACL_CritExit();
  560. }
  561. cache = (ACLListCache_t *)acleval->acllist->cache;
  562. *deny_response = cache->deny_response;
  563. *deny_type = cache->deny_type;
  564. /* For the list of rights requested, get back the list of relevant
  565. * ACEs. If we want
  566. * to alter the precedence of allow/deny, this would be a good
  567. * place to do it.
  568. */
  569. while (*rights)
  570. {
  571. rarray_p = &rights_arry[rights_cnt];
  572. /* Initialize the rights array entry */
  573. strcpy(&rarray_p->right[0], *rights);
  574. makelower(&rarray_p->right[0]);
  575. rarray_p->result = default_result;
  576. rarray_p->absolute = 0;
  577. rarray_p->count = 1; // There's always the specific right
  578. /* Locate the list of ACEs that apply to the right */
  579. rarray_p->acelist[0] =
  580. (ACLAceNumEntry_t *)ACL_HashTableLookup_const(cache->Table, rarray_p->right);
  581. /* See if the requested right also maps back to a generic right and
  582. * if so, locate the acelist for it as well.
  583. */
  584. if (map_generic)
  585. {
  586. for (g_rights=map_generic, g_num=0; *g_rights; g_rights++, g_num++)
  587. {
  588. if (strstr(*g_rights, rarray_p->right)) {
  589. // Add it to our acelist, but skip 0 'cause that's the
  590. // specific right.
  591. rarray_p->acelist[rarray_p->count++] =
  592. (ACLAceNumEntry_t *)ACL_HashTableLookup_const(cache->Table,
  593. (char *)generic_rights[g_num]);
  594. PR_ASSERT (rarray_p->count < ACL_MAX_GENERIC);
  595. }
  596. }
  597. }
  598. rights_cnt++;
  599. rights++;
  600. PR_ASSERT (rights_cnt < ACL_MAX_TEST_RIGHTS);
  601. }
  602. /* Special case - look for an entry that applies to "all" rights */
  603. alllist = (ACLAceNumEntry_t *)ACL_HashTableLookup_const(cache->Table, "all");
  604. /* Ok, we've now got a list of relevant ACEs. Now evaluate things. */
  605. prev_acenum = -1;
  606. cur_ace = cache->acelist;
  607. /* Loop through the relevant ACEs for the requested rights */
  608. while (TRUE)
  609. {
  610. cur_acenum = 10000; /* Pick a really high num so we lose */
  611. /* Find the lowest ACE among the rights lists */
  612. for (i=0; i<rights_cnt; i++) {
  613. rarray_p = &rights_arry[i];
  614. if (rarray_p->absolute) continue; // This right doesn't matter
  615. for (j=0; j<rarray_p->count; j++) {
  616. if ((rarray_p->acelist[j] != NULL) &&
  617. (rarray_p->acelist[j]->acenum < cur_acenum)) {
  618. cur_acenum = rarray_p->acelist[j]->acenum;
  619. }
  620. }
  621. }
  622. /* Special case - look for the "all" rights ace list and see if its
  623. * the lowest of all.
  624. */
  625. if (alllist && (alllist->acenum < cur_acenum))
  626. cur_acenum = alllist->acenum;
  627. /* If no new ACEs then we're done - evaluate the rights list */
  628. if (cur_acenum == 10000)
  629. break;
  630. /* Locate that ACE and evaluate it. We have to step through the
  631. * linked list of ACEs to find it.
  632. */
  633. if (prev_acenum == -1)
  634. delta = cur_acenum;
  635. else
  636. delta = cur_acenum - prev_acenum;
  637. for (i=0; i<delta; i++)
  638. cur_ace = cur_ace->next;
  639. if (global_auth && global_auth != cur_ace->global_auth) {
  640. /* We must enumerate the auth_info plist and remove entries for
  641. * each attribute from the subject property list.
  642. */
  643. PListEnumerate(cur_ace->global_auth, ACL_InvalidateSubjectPList,
  644. acleval->subject);
  645. }
  646. global_auth = cur_ace->global_auth;
  647. result = ACLEvalAce(errp, acleval, cur_ace->acep, &ace_cachable,
  648. cur_ace->autharray, cur_ace->global_auth);
  649. /* Evaluate the cachable value */
  650. if (ace_cachable < *cachable) {
  651. /* Take the minimum value */
  652. *cachable = ace_cachable;
  653. }
  654. /* Under certain circumstances, no matter what happens later,
  655. * the current result is not gonna change.
  656. */
  657. if ((result != LAS_EVAL_TRUE) && (result != LAS_EVAL_FALSE)) {
  658. if (cur_ace->acep->expr_type != ACL_EXPR_TYPE_ALLOW) {
  659. if (allow_error) {
  660. *acl_tag = allow_tag;
  661. *expr_num = allow_num;
  662. return (allow_error);
  663. } else {
  664. *acl_tag = cur_ace->acep->acl_tag;
  665. *expr_num = cur_ace->acep->expr_number;
  666. return (EvalToRes(result));
  667. }
  668. } else {
  669. /* If the error is on an allow statement, continue processing
  670. * and see if a subsequent allow works. If not, remember the
  671. * error and return it.
  672. */
  673. if (!allow_error) {
  674. allow_error = EvalToRes(result);
  675. allow_tag = cur_ace->acep->acl_tag;
  676. allow_num = cur_ace->acep->expr_number;
  677. }
  678. if (IS_ABSOLUTE(cur_ace->acep->expr_flags)) {
  679. allow_absolute = 1;
  680. }
  681. }
  682. }
  683. /* Now apply the result to the rights array. Look to see which rights'
  684. * acelist include the current one, or if the current one is on the
  685. * "all" rights ace list.
  686. */
  687. for (right_num=0; right_num<rights_cnt; right_num++)
  688. {
  689. rarray_p = &rights_arry[right_num];
  690. /* Have we fixated on a prior result? */
  691. if (rarray_p->absolute)
  692. continue;
  693. skipflag = 1;
  694. // Did this ace apply to this right?
  695. for (i=0; i<rarray_p->count; i++) {
  696. if ((rarray_p->acelist[i]) &&
  697. (rarray_p->acelist[i]->acenum == cur_acenum)) {
  698. rarray_p->acelist[i] = rarray_p->acelist[i]->next;
  699. skipflag = 0;
  700. }
  701. }
  702. /* This ace was on the "all" rights queue */
  703. if ((alllist) && (alllist->acenum == cur_acenum)) {
  704. skipflag = 0;
  705. }
  706. if (skipflag)
  707. continue; /* doesn't apply to this right */
  708. if (IS_ABSOLUTE(cur_ace->acep->expr_flags) && (result ==
  709. LAS_EVAL_TRUE)) {
  710. rarray_p->absolute = 1;
  711. absolute = 1;
  712. } else
  713. absolute = 0;
  714. switch (cur_ace->acep->expr_type) {
  715. case ACL_EXPR_TYPE_ALLOW:
  716. if (result == LAS_EVAL_TRUE) {
  717. rarray_p->result = ACL_RES_ALLOW;
  718. if (!allow_absolute) {
  719. /* A previous ALLOW error was superceded */
  720. allow_error = 0;
  721. }
  722. }
  723. else if (!*acl_tag) {
  724. *acl_tag = cur_ace->acep->acl_tag;
  725. *expr_num = cur_ace->acep->expr_number;
  726. }
  727. break;
  728. case ACL_EXPR_TYPE_DENY:
  729. if (result == LAS_EVAL_TRUE) {
  730. *acl_tag = cur_ace->acep->acl_tag;
  731. *expr_num = cur_ace->acep->expr_number;
  732. if (absolute) {
  733. if (allow_error) {
  734. *acl_tag = allow_tag;
  735. *expr_num = allow_num;
  736. return (allow_error);
  737. }
  738. return (ACL_RES_DENY);
  739. }
  740. rarray_p->result = ACL_RES_DENY;
  741. }
  742. break;
  743. default:
  744. /* a non-authorization ACE, just ignore */
  745. break;
  746. }
  747. }
  748. /* This ace was on the "all" rights queue */
  749. if ((alllist) && (alllist->acenum == cur_acenum)) {
  750. alllist = alllist->next;
  751. }
  752. /* If this is an absolute, check to see if all the rights
  753. * have already been fixed by this or previous absolute
  754. * statements. If so, we can compute the response without
  755. * evaluating any more of the ACL list.
  756. */
  757. if (absolute) {
  758. for (i=0; i<rights_cnt; i++) {
  759. /* Non absolute right, so skip this section */
  760. if (rights_arry[i].absolute == 0)
  761. break;
  762. /* This shouldn't be possible, but check anyway.
  763. * Any absolute non-allow result should already
  764. * have been returned earlier.
  765. */
  766. if (rights_arry[i].result != ACL_RES_ALLOW) {
  767. char result_str[16];
  768. sprintf(result_str, "%d", rights_arry[i].result);
  769. nserrGenerate(errp, ACLERRINTERNAL, ACLERR4100, ACL_Program, 3, XP_GetAdminStr(DBT_EvalTestRightsInterimAbsoluteNonAllowValue), rights[i], result_str);
  770. break;
  771. }
  772. if (i == (rights_cnt - 1))
  773. return ACL_RES_ALLOW;
  774. }
  775. }
  776. prev_acenum = cur_acenum;
  777. } /* Next ACE */
  778. /* Do an AND on the results for the individual rights */
  779. for (right_num=0; right_num<rights_cnt; right_num++)
  780. if (rights_arry[right_num].result != ACL_RES_ALLOW) {
  781. if (allow_error) {
  782. *acl_tag = allow_tag;
  783. *expr_num = allow_num;
  784. return (allow_error);
  785. }
  786. return (rights_arry[right_num].result);
  787. }
  788. return (ACL_RES_ALLOW);
  789. }
  790. /* ACL_CachableAclList
  791. * Returns 1 if the ACL list will always evaluate to ALLOW for http_get.
  792. */
  793. NSAPI_PUBLIC int
  794. ACL_CachableAclList(ACLListHandle_t *acllist)
  795. {
  796. ACLEvalHandle_t *acleval;
  797. char *bong;
  798. char *bong_type;
  799. char *acl_tag;
  800. int expr_num;
  801. int rv;
  802. static const char *rights[] = { "http_get", NULL };
  803. ACLCachable_t cachable=ACL_INDEF_CACHABLE;
  804. if (!acllist || acllist == ACL_LIST_NO_ACLS) {
  805. return 1;
  806. }
  807. acleval = ACL_EvalNew(NULL, NULL);
  808. ACL_EvalSetACL(NULL, acleval, acllist);
  809. rv = ACL_INTEvalTestRights(NULL, acleval, rights, http_generic,
  810. &bong_type, &bong, &acl_tag, &expr_num,
  811. &cachable);
  812. ACL_EvalDestroyNoDecrement(NULL, NULL, acleval);
  813. if (rv == ACL_RES_ALLOW && cachable == ACL_INDEF_CACHABLE) {
  814. return 1;
  815. }
  816. return 0;
  817. }
  818. NSAPI_PUBLIC int
  819. ACL_EvalTestRights(
  820. NSErr_t *errp,
  821. ACLEvalHandle_t *acleval,
  822. const char **rights,
  823. const char **map_generic,
  824. char **deny_type,
  825. char **deny_response,
  826. char **acl_tag,
  827. int *expr_num)
  828. {
  829. ACLCachable_t cachable;
  830. return (ACL_INTEvalTestRights(errp, acleval, rights, map_generic,
  831. deny_type, deny_response,
  832. acl_tag, expr_num, &cachable));
  833. }
  834. NSAPI_PUBLIC ACLEvalHandle_t *
  835. ACL_EvalNew(NSErr_t *errp, pool_handle_t *pool)
  836. {
  837. ACLEvalHandle_t *rv = ((ACLEvalHandle_t *)pool_calloc(pool, sizeof(ACLEvalHandle_t), 1));
  838. rv->default_result = ACL_RES_DENY;
  839. return rv;
  840. }
  841. NSAPI_PUBLIC void
  842. ACL_EvalDestroy(NSErr_t *errp, pool_handle_t *pool, ACLEvalHandle_t *acleval)
  843. {
  844. if (!acleval->acllist || acleval->acllist == ACL_LIST_NO_ACLS)
  845. return;
  846. PR_ASSERT(acleval->acllist->ref_count > 0);
  847. ACL_CritEnter();
  848. PR_ASSERT(ACL_CritHeld());
  849. if (--acleval->acllist->ref_count == 0) {
  850. if (ACL_LIST_IS_STALE(acleval->acllist)) {
  851. ACL_ListDestroy(errp, acleval->acllist);
  852. }
  853. }
  854. ACL_CritExit();
  855. pool_free(pool, acleval);
  856. }
  857. NSAPI_PUBLIC void
  858. ACL_EvalDestroyNoDecrement(NSErr_t *errp, pool_handle_t *pool, ACLEvalHandle_t *acleval)
  859. {
  860. /*if (!acleval->acllist || acleval->acllist == ACL_LIST_NO_ACLS)
  861. return; */
  862. /* olga: we need to free acleval unconditionally to avoid memory leaks */
  863. if (acleval)
  864. pool_free(pool, acleval);
  865. }
  866. NSAPI_PUBLIC int
  867. ACL_ListDecrement(NSErr_t *errp, ACLListHandle_t *acllist)
  868. {
  869. if (!acllist || acllist == ACL_LIST_NO_ACLS)
  870. return 0;
  871. PR_ASSERT(ACL_AssertAcllist(acllist));
  872. ACL_CritEnter();
  873. PR_ASSERT(ACL_CritHeld());
  874. if (--acllist->ref_count == 0) {
  875. if (ACL_LIST_IS_STALE(acllist)) {
  876. ACL_ListDestroy(errp, acllist);
  877. }
  878. }
  879. ACL_CritExit();
  880. return 0;
  881. }
  882. NSAPI_PUBLIC int
  883. ACL_EvalSetACL(NSErr_t *errp, ACLEvalHandle_t *acleval, ACLListHandle_t *acllist)
  884. {
  885. PR_ASSERT(ACL_AssertAcllist(acllist));
  886. acleval->acllist = acllist;
  887. return(0);
  888. }
  889. NSAPI_PUBLIC int
  890. ACL_EvalSetSubject(NSErr_t *errp, ACLEvalHandle_t *acleval, PList_t subject)
  891. {
  892. acleval->subject = subject;
  893. return 0;
  894. }
  895. NSAPI_PUBLIC PList_t
  896. ACL_EvalGetSubject(NSErr_t *errp, ACLEvalHandle_t *acleval)
  897. {
  898. return (acleval->subject);
  899. }
  900. NSAPI_PUBLIC int
  901. ACL_EvalSetResource(NSErr_t *errp, ACLEvalHandle_t *acleval, PList_t resource)
  902. {
  903. acleval->resource = resource;
  904. return 0;
  905. }
  906. NSAPI_PUBLIC PList_t
  907. ACL_EvalGetResource(NSErr_t *errp, ACLEvalHandle_t *acleval)
  908. {
  909. return (acleval->resource);
  910. }