1
0

method.cpp 6.7 KB

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