method.cpp 4.7 KB

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