method.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. #include <netsite.h>
  39. #include <libaccess/las.h>
  40. #include <libaccess/acl.h>
  41. #include <libaccess/aclerror.h>
  42. #include <libaccess/dbtlibaccess.h>
  43. #include "aclpriv.h"
  44. NSAPI_PUBLIC int ACL_ModuleRegister (NSErr_t *errp, const char *module_name,
  45. AclModuleInitFunc func)
  46. {
  47. int rv;
  48. if (!module_name || !*module_name) {
  49. nserrGenerate(errp, ACLERRFAIL, ACLERR4200, ACL_Program, 1,
  50. XP_GetAdminStr(DBT_ModuleRegisterModuleNameMissing));
  51. return -1;
  52. }
  53. rv = (*func)(errp);
  54. if (rv < 0) {
  55. nserrGenerate(errp, ACLERRFAIL, ACLERR4210, ACL_Program, 2,
  56. XP_GetAdminStr(DBT_ModuleRegisterFailed), module_name);
  57. return rv;
  58. }
  59. return 0;
  60. }
  61. static int attr_getter_is_matching(NSErr_t *errp, ACLAttrGetter_t *getter,
  62. ACLMethod_t method, ACLDbType_t dbtype)
  63. {
  64. if ((ACL_MethodIsEqual(errp, getter->method, method) ||
  65. ACL_MethodIsEqual(errp, getter->method, ACL_METHOD_ANY)) &&
  66. (ACL_DbTypeIsEqual(errp, getter->dbtype, dbtype) ||
  67. ACL_DbTypeIsEqual(errp, getter->dbtype, ACL_DBTYPE_ANY)))
  68. {
  69. return 1;
  70. }
  71. else {
  72. return 0;
  73. }
  74. }
  75. NSAPI_PUBLIC int ACL_GetAttribute(NSErr_t *errp, const char *attr, void **val,
  76. PList_t subject, PList_t resource,
  77. PList_t auth_info, PList_t global_auth)
  78. {
  79. int rv;
  80. void *attrval;
  81. ACLAttrGetterFn_t func;
  82. ACLAttrGetterList_t getters;
  83. ACLAttrGetter_t *getter;
  84. ACLMethod_t method;
  85. ACLDbType_t dbtype;
  86. /* If subject PList is NULL, we will fail anyway */
  87. if (!subject) return LAS_EVAL_FAIL;
  88. /* Is the attribute already present in the subject property list? */
  89. rv = PListFindValue(subject, attr, &attrval, NULL);
  90. if (rv >= 0) {
  91. /* Yes, take it from there */
  92. *val = attrval;
  93. return LAS_EVAL_TRUE;
  94. }
  95. /* Get the authentication method and database type */
  96. rv = ACL_AuthInfoGetMethod(errp, auth_info, &method);
  97. if (rv < 0) {
  98. nserrGenerate(errp, ACLERRFAIL, ACLERR4300, ACL_Program, 2,
  99. XP_GetAdminStr(DBT_GetAttributeCouldntDetermineMethod), attr);
  100. return LAS_EVAL_FAIL;
  101. }
  102. rv = ACL_AuthInfoGetDbType (errp, auth_info, &dbtype);
  103. if (rv < 0) {
  104. nserrGenerate(errp, ACLERRFAIL, ACLERR4380, ACL_Program, 2,
  105. XP_GetAdminStr(DBT_ReadDbMapFileCouldntDetermineDbtype), attr);
  106. return LAS_EVAL_FAIL;
  107. }
  108. /* Get the list of attribute getters */
  109. rv = ACL_AttrGetterFind(errp, attr, &getters);
  110. if ((rv < 0) || (getters == 0)) {
  111. nserrGenerate(errp, ACLERRFAIL, ACLERR4310, ACL_Program, 2,
  112. XP_GetAdminStr(DBT_GetAttributeCouldntLocateGetter), attr);
  113. return LAS_EVAL_FAIL;
  114. }
  115. /* Iterate over each getter and see if it should be called
  116. * Call each matching getter until a getter which doesn't decline is
  117. * found.
  118. */
  119. for (getter = ACL_AttrGetterFirst(&getters);
  120. getter != 0;
  121. getter = ACL_AttrGetterNext(&getters, getter)) {
  122. /* Require matching method and database type */
  123. if (attr_getter_is_matching(errp, getter, method, dbtype)) {
  124. /* Call the getter function */
  125. func = getter->fn;
  126. rv = (*func)(errp, subject, resource, auth_info, global_auth,
  127. getter->arg);
  128. /* Did the getter succeed? */
  129. if (rv == LAS_EVAL_TRUE) {
  130. /*
  131. * Yes, it should leave the attribute on the subject
  132. * property list.
  133. */
  134. rv = PListFindValue(subject, attr, (void **)&attrval, NULL);
  135. if (rv < 0) {
  136. nserrGenerate(errp, ACLERRFAIL, ACLERR4320, ACL_Program, 2,
  137. XP_GetAdminStr(DBT_GetAttributeDidntSetAttr), attr);
  138. return LAS_EVAL_FAIL;
  139. }
  140. /* Got it */
  141. *val = attrval;
  142. return LAS_EVAL_TRUE;
  143. }
  144. /* Did the getter decline? */
  145. if (rv != LAS_EVAL_DECLINE) {
  146. /* No, did it fail to get the attribute */
  147. if (rv == LAS_EVAL_FAIL || rv == LAS_EVAL_INVALID) {
  148. nserrGenerate(errp, ACLERRFAIL, ACLERR4330, ACL_Program, 2,
  149. XP_GetAdminStr(DBT_GetAttributeDidntGetAttr), attr);
  150. }
  151. return rv;
  152. }
  153. }
  154. }
  155. /* If we fall out of the loop, all the getters declined */
  156. nserrGenerate(errp, ACLERRFAIL, ACLERR4340, ACL_Program, 2,
  157. XP_GetAdminStr(DBT_GetAttributeAllGettersDeclined), attr);
  158. return LAS_EVAL_FAIL;
  159. }