oneeval.cpp 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  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. NULL,
  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. NULL /* ex ACL_FileRenameAcl */,
  125. NULL /* ex ACL_FileDeleteAcl */,
  126. NULL /* ex ACL_FileGetAcl */,
  127. NULL /* ex 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 (curauthplist)
  464. PListDestroy(curauthplist);
  465. if (absauthplist)
  466. PListDestroy(absauthplist);
  467. if (cache) {
  468. ACL_EvalDestroyContext(cache);
  469. }
  470. acleval->acllist->cache = NULL;
  471. return ACL_RES_ERROR;
  472. }
  473. /* ACL_InvalidateSubjectPList
  474. * Given a new authentication plist, enumerate the plist and for each
  475. * key in the plist, search for the matching key in the subject plist
  476. * and delete any matches. E.g. "user", "group".
  477. */
  478. void
  479. ACL_InvalidateSubjectPList(char *attr, const void *value, void *user_data)
  480. {
  481. PList_t subject = (PList_t)user_data;
  482. PListDeleteProp(subject, 0, attr);
  483. return;
  484. }
  485. NSAPI_PUBLIC int ACL_SetDefaultResult (NSErr_t *errp,
  486. ACLEvalHandle_t *acleval,
  487. int result)
  488. {
  489. int rv;
  490. switch(result) {
  491. case ACL_RES_ALLOW:
  492. case ACL_RES_DENY:
  493. case ACL_RES_FAIL:
  494. case ACL_RES_INVALID:
  495. acleval->default_result = result;
  496. rv = 0;
  497. break;
  498. default:
  499. rv = -1;
  500. }
  501. return rv;
  502. }
  503. NSAPI_PUBLIC int ACL_GetDefaultResult (ACLEvalHandle_t *acleval)
  504. {
  505. return acleval->default_result;
  506. }
  507. /* ACL_INTEvalTestRights
  508. * INPUT
  509. * *errp The usual error context stack
  510. * *acleval A list of ACLs
  511. * **rights An array of strings listing the requested rights
  512. * **map_generic An array of strings listing the specific rights
  513. * that map from the generic rights.
  514. * OUTPUT
  515. * **deny_type bong file type passed on the way back out
  516. * **deny_response bong file pathname passed on the way back out
  517. * **acl_tag Name of the ACL that denies access
  518. * *expr_num ACE number within the denying ACL
  519. * *cachable Is the result cachable?
  520. */
  521. static int
  522. ACL_INTEvalTestRights(
  523. NSErr_t *errp,
  524. ACLEvalHandle_t *acleval,
  525. char **rights,
  526. char **map_generic,
  527. char **deny_type,
  528. char **deny_response,
  529. char **acl_tag,
  530. int *expr_num,
  531. ACLCachable_t *cachable)
  532. {
  533. struct rights_ent {
  534. char right[64]; /* lowercase-ed rights string */
  535. int result; /* Interim result value */
  536. int absolute; /* ACE with absolute keyword */
  537. int count; /* # specific + generic rights */
  538. ACLAceNumEntry_t *acelist[ACL_MAX_GENERIC+1];
  539. /* List of relevant ACEs */
  540. };
  541. struct rights_ent *rarray_p;
  542. struct rights_ent rights_arry[ACL_MAX_TEST_RIGHTS];
  543. ACLAceNumEntry_t *alllist; /* List of ACEs for "all" rights */
  544. ACLAceEntry_t *cur_ace;
  545. ACLListCache_t *cache;
  546. int rights_cnt = 0;
  547. int prev_acenum, cur_acenum;
  548. int i, j, right_num, delta;
  549. ACLCachable_t ace_cachable;
  550. int result;
  551. int absolute = 0;
  552. int skipflag;
  553. int g_num; /* index into the generic rights array. */
  554. char **g_rights;
  555. PList_t global_auth=NULL;
  556. int allow_error = 0;
  557. int allow_absolute = 0;
  558. char *allow_tag = NULL;
  559. int allow_num = 0;
  560. int default_result = ACL_GetDefaultResult(acleval);
  561. *acl_tag = NULL;
  562. *expr_num = 0;
  563. *cachable = ACL_INDEF_CACHABLE;
  564. /*
  565. * The acleval contains the list of acis we are asking about.
  566. * In our case it's always of length 1.
  567. * The acleval is a per aclpb structure but
  568. * the acllist is a global structure derived from the global
  569. * aci cache--so access to acllist is multi-threaded.
  570. * Hence, for example the use of the "read-only" hash
  571. * lookup routines in this function--ACL_EvalTestRights()
  572. * is called in a "reader only context" so this code is therefore
  573. * thread-safe.
  574. */
  575. if (acleval->acllist == ACL_LIST_NO_ACLS) return ACL_RES_ALLOW;
  576. /* Build up the access right - indexed structures */
  577. if (acleval->acllist->cache == NULL) {
  578. ACL_CritEnter();
  579. if (acleval->acllist->cache == NULL) { /* Check again */
  580. if (ACLEvalBuildContext(errp, acleval) == ACL_RES_ERROR) {
  581. nserrGenerate(errp, ACLERRINTERNAL, ACLERR4110, ACL_Program,
  582. 1, XP_GetAdminStr(DBT_EvalTestRightsEvalBuildContextFailed));
  583. ACL_CritExit();
  584. return ACL_RES_ERROR;
  585. }
  586. }
  587. ACL_CritExit();
  588. }
  589. cache = (ACLListCache_t *)acleval->acllist->cache;
  590. *deny_response = cache->deny_response;
  591. *deny_type = cache->deny_type;
  592. /* For the list of rights requested, get back the list of relevant
  593. * ACEs. If we want
  594. * to alter the precedence of allow/deny, this would be a good
  595. * place to do it.
  596. */
  597. while (*rights)
  598. {
  599. rarray_p = &rights_arry[rights_cnt];
  600. /* Initialize the rights array entry */
  601. strcpy(&rarray_p->right[0], *rights);
  602. makelower(&rarray_p->right[0]);
  603. rarray_p->result = default_result;
  604. rarray_p->absolute = 0;
  605. rarray_p->count = 1; // There's always the specific right
  606. /* Locate the list of ACEs that apply to the right */
  607. rarray_p->acelist[0] =
  608. (ACLAceNumEntry_t *)ACL_HashTableLookup_const(cache->Table, rarray_p->right);
  609. /* See if the requested right also maps back to a generic right and
  610. * if so, locate the acelist for it as well.
  611. */
  612. if (map_generic)
  613. {
  614. for (g_rights=map_generic, g_num=0; *g_rights; g_rights++, g_num++)
  615. {
  616. if (strstr(*g_rights, rarray_p->right)) {
  617. // Add it to our acelist, but skip 0 'cause that's the
  618. // specific right.
  619. rarray_p->acelist[rarray_p->count++] =
  620. (ACLAceNumEntry_t *)ACL_HashTableLookup_const(cache->Table,
  621. (char *)generic_rights[g_num]);
  622. PR_ASSERT (rarray_p->count < ACL_MAX_GENERIC);
  623. }
  624. }
  625. }
  626. rights_cnt++;
  627. rights++;
  628. PR_ASSERT (rights_cnt < ACL_MAX_TEST_RIGHTS);
  629. }
  630. /* Special case - look for an entry that applies to "all" rights */
  631. alllist = (ACLAceNumEntry_t *)ACL_HashTableLookup_const(cache->Table, "all");
  632. /* Ok, we've now got a list of relevant ACEs. Now evaluate things. */
  633. prev_acenum = -1;
  634. cur_ace = cache->acelist;
  635. /* Loop through the relevant ACEs for the requested rights */
  636. while (TRUE)
  637. {
  638. cur_acenum = 10000; /* Pick a really high num so we lose */
  639. /* Find the lowest ACE among the rights lists */
  640. for (i=0; i<rights_cnt; i++) {
  641. rarray_p = &rights_arry[i];
  642. if (rarray_p->absolute) continue; // This right doesn't matter
  643. for (j=0; j<rarray_p->count; j++) {
  644. if ((rarray_p->acelist[j] != NULL) &&
  645. (rarray_p->acelist[j]->acenum < cur_acenum)) {
  646. cur_acenum = rarray_p->acelist[j]->acenum;
  647. }
  648. }
  649. }
  650. /* Special case - look for the "all" rights ace list and see if its
  651. * the lowest of all.
  652. */
  653. if (alllist && (alllist->acenum < cur_acenum))
  654. cur_acenum = alllist->acenum;
  655. /* If no new ACEs then we're done - evaluate the rights list */
  656. if (cur_acenum == 10000)
  657. break;
  658. /* Locate that ACE and evaluate it. We have to step through the
  659. * linked list of ACEs to find it.
  660. */
  661. if (prev_acenum == -1)
  662. delta = cur_acenum;
  663. else
  664. delta = cur_acenum - prev_acenum;
  665. for (i=0; i<delta; i++)
  666. cur_ace = cur_ace->next;
  667. if (global_auth && global_auth != cur_ace->global_auth) {
  668. /* We must enumerate the auth_info plist and remove entries for
  669. * each attribute from the subject property list.
  670. */
  671. PListEnumerate(cur_ace->global_auth, ACL_InvalidateSubjectPList,
  672. acleval->subject);
  673. }
  674. global_auth = cur_ace->global_auth;
  675. result = ACLEvalAce(errp, acleval, cur_ace->acep, &ace_cachable,
  676. cur_ace->autharray, cur_ace->global_auth);
  677. /* Evaluate the cachable value */
  678. if (ace_cachable < *cachable) {
  679. /* Take the minimum value */
  680. *cachable = ace_cachable;
  681. }
  682. /* Under certain circumstances, no matter what happens later,
  683. * the current result is not gonna change.
  684. */
  685. if ((result != LAS_EVAL_TRUE) && (result != LAS_EVAL_FALSE)) {
  686. if (cur_ace->acep->expr_type != ACL_EXPR_TYPE_ALLOW) {
  687. if (allow_error) {
  688. *acl_tag = allow_tag;
  689. *expr_num = allow_num;
  690. return (allow_error);
  691. } else {
  692. *acl_tag = cur_ace->acep->acl_tag;
  693. *expr_num = cur_ace->acep->expr_number;
  694. return (EvalToRes(result));
  695. }
  696. } else {
  697. /* If the error is on an allow statement, continue processing
  698. * and see if a subsequent allow works. If not, remember the
  699. * error and return it.
  700. */
  701. if (!allow_error) {
  702. allow_error = EvalToRes(result);
  703. allow_tag = cur_ace->acep->acl_tag;
  704. allow_num = cur_ace->acep->expr_number;
  705. }
  706. if (IS_ABSOLUTE(cur_ace->acep->expr_flags)) {
  707. allow_absolute = 1;
  708. }
  709. }
  710. }
  711. /* Now apply the result to the rights array. Look to see which rights'
  712. * acelist include the current one, or if the current one is on the
  713. * "all" rights ace list.
  714. */
  715. for (right_num=0; right_num<rights_cnt; right_num++)
  716. {
  717. rarray_p = &rights_arry[right_num];
  718. /* Have we fixated on a prior result? */
  719. if (rarray_p->absolute)
  720. continue;
  721. skipflag = 1;
  722. // Did this ace apply to this right?
  723. for (i=0; i<rarray_p->count; i++) {
  724. if ((rarray_p->acelist[i]) &&
  725. (rarray_p->acelist[i]->acenum == cur_acenum)) {
  726. rarray_p->acelist[i] = rarray_p->acelist[i]->next;
  727. skipflag = 0;
  728. }
  729. }
  730. /* This ace was on the "all" rights queue */
  731. if ((alllist) && (alllist->acenum == cur_acenum)) {
  732. skipflag = 0;
  733. }
  734. if (skipflag)
  735. continue; /* doesn't apply to this right */
  736. if (IS_ABSOLUTE(cur_ace->acep->expr_flags) && (result ==
  737. LAS_EVAL_TRUE)) {
  738. rarray_p->absolute = 1;
  739. absolute = 1;
  740. } else
  741. absolute = 0;
  742. switch (cur_ace->acep->expr_type) {
  743. case ACL_EXPR_TYPE_ALLOW:
  744. if (result == LAS_EVAL_TRUE) {
  745. rarray_p->result = ACL_RES_ALLOW;
  746. if (!allow_absolute) {
  747. /* A previous ALLOW error was superceded */
  748. allow_error = 0;
  749. }
  750. }
  751. else if (!*acl_tag) {
  752. *acl_tag = cur_ace->acep->acl_tag;
  753. *expr_num = cur_ace->acep->expr_number;
  754. }
  755. break;
  756. case ACL_EXPR_TYPE_DENY:
  757. if (result == LAS_EVAL_TRUE) {
  758. *acl_tag = cur_ace->acep->acl_tag;
  759. *expr_num = cur_ace->acep->expr_number;
  760. if (absolute) {
  761. if (allow_error) {
  762. *acl_tag = allow_tag;
  763. *expr_num = allow_num;
  764. return (allow_error);
  765. }
  766. return (ACL_RES_DENY);
  767. }
  768. rarray_p->result = ACL_RES_DENY;
  769. }
  770. break;
  771. default:
  772. /* a non-authorization ACE, just ignore */
  773. break;
  774. }
  775. }
  776. /* This ace was on the "all" rights queue */
  777. if ((alllist) && (alllist->acenum == cur_acenum)) {
  778. alllist = alllist->next;
  779. }
  780. /* If this is an absolute, check to see if all the rights
  781. * have already been fixed by this or previous absolute
  782. * statements. If so, we can compute the response without
  783. * evaluating any more of the ACL list.
  784. */
  785. if (absolute) {
  786. for (i=0; i<rights_cnt; i++) {
  787. /* Non absolute right, so skip this section */
  788. if (rights_arry[i].absolute == 0)
  789. break;
  790. /* This shouldn't be possible, but check anyway.
  791. * Any absolute non-allow result should already
  792. * have been returned earlier.
  793. */
  794. if (rights_arry[i].result != ACL_RES_ALLOW) {
  795. char result_str[16];
  796. sprintf(result_str, "%d", rights_arry[i].result);
  797. nserrGenerate(errp, ACLERRINTERNAL, ACLERR4100, ACL_Program, 3, XP_GetAdminStr(DBT_EvalTestRightsInterimAbsoluteNonAllowValue), rights[i], result_str);
  798. break;
  799. }
  800. if (i == (rights_cnt - 1))
  801. return ACL_RES_ALLOW;
  802. }
  803. }
  804. prev_acenum = cur_acenum;
  805. } /* Next ACE */
  806. /* Do an AND on the results for the individual rights */
  807. for (right_num=0; right_num<rights_cnt; right_num++)
  808. if (rights_arry[right_num].result != ACL_RES_ALLOW) {
  809. if (allow_error) {
  810. *acl_tag = allow_tag;
  811. *expr_num = allow_num;
  812. return (allow_error);
  813. }
  814. return (rights_arry[right_num].result);
  815. }
  816. return (ACL_RES_ALLOW);
  817. }
  818. /* ACL_CachableAclList
  819. * Returns 1 if the ACL list will always evaluate to ALLOW for http_get.
  820. */
  821. NSAPI_PUBLIC int
  822. ACL_CachableAclList(ACLListHandle_t *acllist)
  823. {
  824. ACLEvalHandle_t *acleval;
  825. char *bong;
  826. char *bong_type;
  827. char *acl_tag;
  828. int expr_num;
  829. int rv;
  830. static char *rights[] = { "http_get", NULL };
  831. ACLCachable_t cachable=ACL_INDEF_CACHABLE;
  832. if (!acllist || acllist == ACL_LIST_NO_ACLS) {
  833. return 1;
  834. }
  835. acleval = ACL_EvalNew(NULL, NULL);
  836. ACL_EvalSetACL(NULL, acleval, acllist);
  837. rv = ACL_INTEvalTestRights(NULL, acleval, rights, http_generic,
  838. &bong_type, &bong, &acl_tag, &expr_num,
  839. &cachable);
  840. ACL_EvalDestroyNoDecrement(NULL, NULL, acleval);
  841. if (rv == ACL_RES_ALLOW && cachable == ACL_INDEF_CACHABLE) {
  842. return 1;
  843. }
  844. return 0;
  845. }
  846. NSAPI_PUBLIC int
  847. ACL_EvalTestRights(
  848. NSErr_t *errp,
  849. ACLEvalHandle_t *acleval,
  850. char **rights,
  851. char **map_generic,
  852. char **deny_type,
  853. char **deny_response,
  854. char **acl_tag,
  855. int *expr_num)
  856. {
  857. ACLCachable_t cachable;
  858. return (ACL_INTEvalTestRights(errp, acleval, rights, map_generic,
  859. deny_type, deny_response,
  860. acl_tag, expr_num, &cachable));
  861. }
  862. NSAPI_PUBLIC ACLEvalHandle_t *
  863. ACL_EvalNew(NSErr_t *errp, pool_handle_t *pool)
  864. {
  865. ACLEvalHandle_t *rv = ((ACLEvalHandle_t *)pool_calloc(pool, sizeof(ACLEvalHandle_t), 1));
  866. rv->default_result = ACL_RES_DENY;
  867. return rv;
  868. }
  869. NSAPI_PUBLIC void
  870. ACL_EvalDestroy(NSErr_t *errp, pool_handle_t *pool, ACLEvalHandle_t *acleval)
  871. {
  872. if (!acleval->acllist || acleval->acllist == ACL_LIST_NO_ACLS)
  873. return;
  874. PR_ASSERT(acleval->acllist->ref_count > 0);
  875. ACL_CritEnter();
  876. PR_ASSERT(ACL_CritHeld());
  877. if (--acleval->acllist->ref_count == 0) {
  878. if (ACL_LIST_IS_STALE(acleval->acllist)) {
  879. ACL_ListDestroy(errp, acleval->acllist);
  880. }
  881. }
  882. ACL_CritExit();
  883. pool_free(pool, acleval);
  884. }
  885. NSAPI_PUBLIC void
  886. ACL_EvalDestroyNoDecrement(NSErr_t *errp, pool_handle_t *pool, ACLEvalHandle_t *acleval)
  887. {
  888. /*if (!acleval->acllist || acleval->acllist == ACL_LIST_NO_ACLS)
  889. return; */
  890. /* olga: we need to free acleval unconditionally to avoid memory leaks */
  891. if (acleval)
  892. pool_free(pool, acleval);
  893. }
  894. NSAPI_PUBLIC int
  895. ACL_ListDecrement(NSErr_t *errp, ACLListHandle_t *acllist)
  896. {
  897. if (!acllist || acllist == ACL_LIST_NO_ACLS)
  898. return 0;
  899. PR_ASSERT(ACL_AssertAcllist(acllist));
  900. ACL_CritEnter();
  901. PR_ASSERT(ACL_CritHeld());
  902. if (--acllist->ref_count == 0) {
  903. if (ACL_LIST_IS_STALE(acllist)) {
  904. ACL_ListDestroy(errp, acllist);
  905. }
  906. }
  907. ACL_CritExit();
  908. return 0;
  909. }
  910. NSAPI_PUBLIC int
  911. ACL_EvalSetACL(NSErr_t *errp, ACLEvalHandle_t *acleval, ACLListHandle_t *acllist)
  912. {
  913. PR_ASSERT(ACL_AssertAcllist(acllist));
  914. acleval->acllist = acllist;
  915. return(0);
  916. }
  917. NSAPI_PUBLIC int
  918. ACL_EvalSetSubject(NSErr_t *errp, ACLEvalHandle_t *acleval, PList_t subject)
  919. {
  920. acleval->subject = subject;
  921. return 0;
  922. }
  923. NSAPI_PUBLIC PList_t
  924. ACL_EvalGetSubject(NSErr_t *errp, ACLEvalHandle_t *acleval)
  925. {
  926. return (acleval->subject);
  927. }
  928. NSAPI_PUBLIC int
  929. ACL_EvalSetResource(NSErr_t *errp, ACLEvalHandle_t *acleval, PList_t resource)
  930. {
  931. acleval->resource = resource;
  932. return 0;
  933. }
  934. NSAPI_PUBLIC PList_t
  935. ACL_EvalGetResource(NSErr_t *errp, ACLEvalHandle_t *acleval)
  936. {
  937. return (acleval->resource);
  938. }