ua_accesscontrol_default.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  2. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
  3. #include "ua_accesscontrol_default.h"
  4. /* Example access control management. Anonymous and username / password login.
  5. * The access rights are maximally permissive. */
  6. #define ANONYMOUS_POLICY "open62541-anonymous-policy"
  7. #define USERNAME_POLICY "open62541-username-policy"
  8. // TODO: There should be one definition of these strings in the endpoint.
  9. // Put the endpoint definition in the access control struct?
  10. #define UA_STRING_STATIC(s) {sizeof(s)-1, (UA_Byte*)s}
  11. const UA_String anonymous_policy = UA_STRING_STATIC(ANONYMOUS_POLICY);
  12. const UA_String username_policy = UA_STRING_STATIC(USERNAME_POLICY);
  13. const size_t usernamePasswordsSize = 2;
  14. UA_UsernamePasswordLogin usernamePasswords[2] = {
  15. { UA_STRING_STATIC("user1"), UA_STRING_STATIC("password") },
  16. { UA_STRING_STATIC("user2"), UA_STRING_STATIC("password1") } };
  17. UA_StatusCode
  18. activateSession_default(const UA_NodeId *sessionId,
  19. const UA_ExtensionObject *userIdentityToken,
  20. void **sessionHandle) {
  21. /* Could the token be decoded? */
  22. if(userIdentityToken->encoding < UA_EXTENSIONOBJECT_DECODED)
  23. return UA_STATUSCODE_BADIDENTITYTOKENINVALID;
  24. /* Anonymous login */
  25. if(userIdentityToken->content.decoded.type ==
  26. &UA_TYPES[UA_TYPES_ANONYMOUSIDENTITYTOKEN]) {
  27. const UA_AnonymousIdentityToken *token =
  28. (UA_AnonymousIdentityToken*)userIdentityToken->content.decoded.data;
  29. /* Compatibility notice: Siemens OPC Scout v10 provides an empty
  30. * policyId. This is not compliant. For compatibility, assume that empty
  31. * policyId == ANONYMOUS_POLICY */
  32. if(token->policyId.data &&
  33. !UA_String_equal(&token->policyId, &anonymous_policy))
  34. return UA_STATUSCODE_BADIDENTITYTOKENINVALID;
  35. /* No userdata atm */
  36. *sessionHandle = NULL;
  37. return UA_STATUSCODE_GOOD;
  38. }
  39. /* Username and password */
  40. if(userIdentityToken->content.decoded.type ==
  41. &UA_TYPES[UA_TYPES_USERNAMEIDENTITYTOKEN]) {
  42. const UA_UserNameIdentityToken *token =
  43. (UA_UserNameIdentityToken*)userIdentityToken->content.decoded.data;
  44. if(!UA_String_equal(&token->policyId, &username_policy))
  45. return UA_STATUSCODE_BADIDENTITYTOKENINVALID;
  46. /* Empty username and password */
  47. if(token->userName.length == 0 && token->password.length == 0)
  48. return UA_STATUSCODE_BADIDENTITYTOKENINVALID;
  49. /* Try to match username/pw */
  50. UA_Boolean match = false;
  51. for(size_t i = 0; i < usernamePasswordsSize; i++) {
  52. const UA_String *user = &usernamePasswords[i].username;
  53. const UA_String *pw = &usernamePasswords[i].password;
  54. if(UA_String_equal(&token->userName, user) &&
  55. UA_String_equal(&token->password, pw)) {
  56. match = true;
  57. break;
  58. }
  59. }
  60. if(!match)
  61. return UA_STATUSCODE_BADUSERACCESSDENIED;
  62. /* No userdata atm */
  63. *sessionHandle = NULL;
  64. return UA_STATUSCODE_GOOD;
  65. }
  66. /* Unsupported token type */
  67. return UA_STATUSCODE_BADIDENTITYTOKENINVALID;
  68. }
  69. void
  70. closeSession_default(const UA_NodeId *sessionId,
  71. void *sessionHandle) {
  72. /* no handle to clean up */
  73. }
  74. UA_UInt32
  75. getUserRightsMask_default(const UA_NodeId *sessionId,
  76. void *sessionHandle,
  77. const UA_NodeId *nodeId) {
  78. return 0xFFFFFFFF;
  79. }
  80. UA_Byte
  81. getUserAccessLevel_default(const UA_NodeId *sessionId,
  82. void *sessionHandle,
  83. const UA_NodeId *nodeId) {
  84. return 0xFF;
  85. }
  86. UA_Boolean
  87. getUserExecutable_default(const UA_NodeId *sessionId,
  88. void *sessionHandle,
  89. const UA_NodeId *nodeId) {
  90. return true;
  91. }
  92. UA_Boolean
  93. getUserExecutableOnObject_default(const UA_NodeId *sessionId,
  94. void *sessionHandle,
  95. const UA_NodeId *methodId,
  96. const UA_NodeId *objectId) {
  97. return true;
  98. }
  99. UA_Boolean
  100. allowAddNode_default(const UA_NodeId *sessionId,
  101. void *sessionHandle,
  102. const UA_AddNodesItem *item) {
  103. return true;
  104. }
  105. UA_Boolean
  106. allowAddReference_default(const UA_NodeId *sessionId,
  107. void *sessionHandle,
  108. const UA_AddReferencesItem *item) {
  109. return true;
  110. }
  111. UA_Boolean
  112. allowDeleteNode_default(const UA_NodeId *sessionId,
  113. void *sessionHandle,
  114. const UA_DeleteNodesItem *item) {
  115. return true;
  116. }
  117. UA_Boolean
  118. allowDeleteReference_default(const UA_NodeId *sessionId,
  119. void *sessionHandle,
  120. const UA_DeleteReferencesItem *item) {
  121. return true;
  122. }