oneeval.cpp 32 KB

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