method.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. #include <netsite.h>
  13. #include <libaccess/las.h>
  14. #include <libaccess/acl.h>
  15. #include <libaccess/aclerror.h>
  16. #include <libaccess/dbtlibaccess.h>
  17. #include "aclpriv.h"
  18. NSAPI_PUBLIC int ACL_ModuleRegister (NSErr_t *errp, const char *module_name,
  19. AclModuleInitFunc func)
  20. {
  21. int rv;
  22. if (!module_name || !*module_name) {
  23. nserrGenerate(errp, ACLERRFAIL, ACLERR4200, ACL_Program, 1,
  24. XP_GetAdminStr(DBT_ModuleRegisterModuleNameMissing));
  25. return -1;
  26. }
  27. rv = (*func)(errp);
  28. if (rv < 0) {
  29. nserrGenerate(errp, ACLERRFAIL, ACLERR4210, ACL_Program, 2,
  30. XP_GetAdminStr(DBT_ModuleRegisterFailed), module_name);
  31. return rv;
  32. }
  33. return 0;
  34. }
  35. static int attr_getter_is_matching(NSErr_t *errp, ACLAttrGetter_t *getter,
  36. ACLMethod_t method, ACLDbType_t dbtype)
  37. {
  38. if ((ACL_MethodIsEqual(errp, getter->method, method) ||
  39. ACL_MethodIsEqual(errp, getter->method, ACL_METHOD_ANY)) &&
  40. (ACL_DbTypeIsEqual(errp, getter->dbtype, dbtype) ||
  41. ACL_DbTypeIsEqual(errp, getter->dbtype, ACL_DBTYPE_ANY)))
  42. {
  43. return 1;
  44. }
  45. else {
  46. return 0;
  47. }
  48. }
  49. NSAPI_PUBLIC int ACL_GetAttribute(NSErr_t *errp, const char *attr, void **val,
  50. PList_t subject, PList_t resource,
  51. PList_t auth_info, PList_t global_auth)
  52. {
  53. int rv;
  54. void *attrval;
  55. ACLAttrGetterFn_t func;
  56. ACLAttrGetterList_t getters;
  57. ACLAttrGetter_t *getter;
  58. ACLMethod_t method;
  59. ACLDbType_t dbtype;
  60. /* If subject PList is NULL, we will fail anyway */
  61. if (!subject) return LAS_EVAL_FAIL;
  62. /* Is the attribute already present in the subject property list? */
  63. rv = PListFindValue(subject, attr, &attrval, NULL);
  64. if (rv >= 0) {
  65. /* Yes, take it from there */
  66. *val = attrval;
  67. return LAS_EVAL_TRUE;
  68. }
  69. /* Get the authentication method and database type */
  70. rv = ACL_AuthInfoGetMethod(errp, auth_info, &method);
  71. if (rv < 0) {
  72. nserrGenerate(errp, ACLERRFAIL, ACLERR4300, ACL_Program, 2,
  73. XP_GetAdminStr(DBT_GetAttributeCouldntDetermineMethod), attr);
  74. return LAS_EVAL_FAIL;
  75. }
  76. rv = ACL_AuthInfoGetDbType (errp, auth_info, &dbtype);
  77. if (rv < 0) {
  78. nserrGenerate(errp, ACLERRFAIL, ACLERR4380, ACL_Program, 2,
  79. XP_GetAdminStr(DBT_ReadDbMapFileCouldntDetermineDbtype), attr);
  80. return LAS_EVAL_FAIL;
  81. }
  82. /* Get the list of attribute getters */
  83. rv = ACL_AttrGetterFind(errp, attr, &getters);
  84. if ((rv < 0) || (getters == 0)) {
  85. nserrGenerate(errp, ACLERRFAIL, ACLERR4310, ACL_Program, 2,
  86. XP_GetAdminStr(DBT_GetAttributeCouldntLocateGetter), attr);
  87. return LAS_EVAL_FAIL;
  88. }
  89. /* Iterate over each getter and see if it should be called
  90. * Call each matching getter until a getter which doesn't decline is
  91. * found.
  92. */
  93. for (getter = ACL_AttrGetterFirst(&getters);
  94. getter != 0;
  95. getter = ACL_AttrGetterNext(&getters, getter)) {
  96. /* Require matching method and database type */
  97. if (attr_getter_is_matching(errp, getter, method, dbtype)) {
  98. /* Call the getter function */
  99. func = getter->fn;
  100. rv = (*func)(errp, subject, resource, auth_info, global_auth,
  101. getter->arg);
  102. /* Did the getter succeed? */
  103. if (rv == LAS_EVAL_TRUE) {
  104. /*
  105. * Yes, it should leave the attribute on the subject
  106. * property list.
  107. */
  108. rv = PListFindValue(subject, attr, (void **)&attrval, NULL);
  109. if (rv < 0) {
  110. nserrGenerate(errp, ACLERRFAIL, ACLERR4320, ACL_Program, 2,
  111. XP_GetAdminStr(DBT_GetAttributeDidntSetAttr), attr);
  112. return LAS_EVAL_FAIL;
  113. }
  114. /* Got it */
  115. *val = attrval;
  116. return LAS_EVAL_TRUE;
  117. }
  118. /* Did the getter decline? */
  119. if (rv != LAS_EVAL_DECLINE) {
  120. /* No, did it fail to get the attribute */
  121. if (rv == LAS_EVAL_FAIL || rv == LAS_EVAL_INVALID) {
  122. nserrGenerate(errp, ACLERRFAIL, ACLERR4330, ACL_Program, 2,
  123. XP_GetAdminStr(DBT_GetAttributeDidntGetAttr), attr);
  124. }
  125. return rv;
  126. }
  127. }
  128. }
  129. /* If we fall out of the loop, all the getters declined */
  130. nserrGenerate(errp, ACLERRFAIL, ACLERR4340, ACL_Program, 2,
  131. XP_GetAdminStr(DBT_GetAttributeAllGettersDeclined), attr);
  132. return LAS_EVAL_FAIL;
  133. }