oneeval.cpp 32 KB

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