oneeval.cpp 32 KB

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