ua_services_session.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/.*/
  4. #include "ua_services.h"
  5. #include "ua_server_internal.h"
  6. #include "ua_session_manager.h"
  7. #include "ua_types_generated_encoding_binary.h"
  8. void Service_CreateSession(UA_Server *server, UA_SecureChannel *channel,
  9. const UA_CreateSessionRequest *request,
  10. UA_CreateSessionResponse *response) {
  11. if(channel->securityToken.channelId == 0) {
  12. response->responseHeader.serviceResult = UA_STATUSCODE_BADSECURECHANNELIDINVALID;
  13. return;
  14. }
  15. /* Copy the server's endpoint into the response */
  16. response->responseHeader.serviceResult =
  17. UA_Array_copy(server->endpointDescriptions, server->endpointDescriptionsSize,
  18. (void**)&response->serverEndpoints, &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]);
  19. if(response->responseHeader.serviceResult != UA_STATUSCODE_GOOD)
  20. return;
  21. response->serverEndpointsSize = server->endpointDescriptionsSize;
  22. /* Mirror back the endpointUrl */
  23. for(size_t i = 0; i < response->serverEndpointsSize; ++i)
  24. UA_String_copy(&request->endpointUrl, &response->serverEndpoints[i].endpointUrl);
  25. UA_Session *newSession;
  26. response->responseHeader.serviceResult =
  27. UA_SessionManager_createSession(&server->sessionManager, channel, request, &newSession);
  28. if(response->responseHeader.serviceResult != UA_STATUSCODE_GOOD) {
  29. UA_LOG_DEBUG_CHANNEL(server->config.logger, channel, "Processing CreateSessionRequest failed");
  30. return;
  31. }
  32. newSession->maxResponseMessageSize = request->maxResponseMessageSize;
  33. newSession->maxRequestMessageSize = channel->connection->localConf.maxMessageSize;
  34. response->sessionId = newSession->sessionId;
  35. response->revisedSessionTimeout = (UA_Double)newSession->timeout;
  36. response->authenticationToken = newSession->authenticationToken;
  37. response->responseHeader.serviceResult = UA_String_copy(&request->sessionName, &newSession->sessionName);
  38. if(server->endpointDescriptionsSize > 0)
  39. response->responseHeader.serviceResult |=
  40. UA_ByteString_copy(&server->endpointDescriptions->serverCertificate,
  41. &response->serverCertificate);
  42. if(response->responseHeader.serviceResult != UA_STATUSCODE_GOOD) {
  43. UA_SessionManager_removeSession(&server->sessionManager, &newSession->authenticationToken);
  44. return;
  45. }
  46. UA_LOG_DEBUG_CHANNEL(server->config.logger, channel, "Session " UA_PRINTF_GUID_FORMAT " created",
  47. UA_PRINTF_GUID_DATA(newSession->sessionId.identifier.guid));
  48. }
  49. void
  50. Service_ActivateSession(UA_Server *server, UA_SecureChannel *channel,
  51. UA_Session *session, const UA_ActivateSessionRequest *request,
  52. UA_ActivateSessionResponse *response) {
  53. if(session->validTill < UA_DateTime_nowMonotonic()) {
  54. UA_LOG_INFO_SESSION(server->config.logger, session, "ActivateSession: SecureChannel %i wants "
  55. "to activate, but the session has timed out", channel->securityToken.channelId);
  56. response->responseHeader.serviceResult = UA_STATUSCODE_BADSESSIONIDINVALID;
  57. return;
  58. }
  59. if(request->userIdentityToken.encoding < UA_EXTENSIONOBJECT_DECODED ||
  60. (request->userIdentityToken.content.decoded.type != &UA_TYPES[UA_TYPES_ANONYMOUSIDENTITYTOKEN] &&
  61. request->userIdentityToken.content.decoded.type != &UA_TYPES[UA_TYPES_USERNAMEIDENTITYTOKEN])) {
  62. UA_LOG_INFO_SESSION(server->config.logger, session, "ActivateSession: SecureChannel %i wants "
  63. "to activate, but the UserIdentify token is invalid", channel->securityToken.channelId);
  64. response->responseHeader.serviceResult = UA_STATUSCODE_BADIDENTITYTOKENINVALID;
  65. return;
  66. }
  67. UA_String ap = UA_STRING(ANONYMOUS_POLICY);
  68. UA_String up = UA_STRING(USERNAME_POLICY);
  69. /* Compatibility notice: Siemens OPC Scout v10 provides an empty policyId,
  70. this is not okay For compatibility we will assume that empty policyId == ANONYMOUS_POLICY
  71. if(token.policyId->data == NULL)
  72. response->responseHeader.serviceResult = UA_STATUSCODE_BADIDENTITYTOKENINVALID;
  73. */
  74. if(server->config.enableAnonymousLogin &&
  75. request->userIdentityToken.content.decoded.type == &UA_TYPES[UA_TYPES_ANONYMOUSIDENTITYTOKEN]) {
  76. /* anonymous login */
  77. const UA_AnonymousIdentityToken *token = request->userIdentityToken.content.decoded.data;
  78. if(token->policyId.data && !UA_String_equal(&token->policyId, &ap)) {
  79. response->responseHeader.serviceResult = UA_STATUSCODE_BADIDENTITYTOKENINVALID;
  80. return;
  81. }
  82. } else if(server->config.enableUsernamePasswordLogin &&
  83. request->userIdentityToken.content.decoded.type == &UA_TYPES[UA_TYPES_USERNAMEIDENTITYTOKEN]) {
  84. /* username login */
  85. const UA_UserNameIdentityToken *token = request->userIdentityToken.content.decoded.data;
  86. if(!UA_String_equal(&token->policyId, &up)) {
  87. response->responseHeader.serviceResult = UA_STATUSCODE_BADIDENTITYTOKENINVALID;
  88. return;
  89. }
  90. if(token->encryptionAlgorithm.length > 0) {
  91. /* we don't support encryption */
  92. response->responseHeader.serviceResult = UA_STATUSCODE_BADIDENTITYTOKENINVALID;
  93. return;
  94. }
  95. if(token->userName.length == 0 && token->password.length == 0) {
  96. /* empty username and password */
  97. response->responseHeader.serviceResult = UA_STATUSCODE_BADIDENTITYTOKENINVALID;
  98. return;
  99. }
  100. /* trying to match pw/username */
  101. UA_Boolean match = false;
  102. for(size_t i = 0; i < server->config.usernamePasswordLoginsSize; ++i) {
  103. UA_String *user = &server->config.usernamePasswordLogins[i].username;
  104. UA_String *pw = &server->config.usernamePasswordLogins[i].password;
  105. if(UA_String_equal(&token->userName, user) && UA_String_equal(&token->password, pw)) {
  106. match = true;
  107. break;
  108. }
  109. }
  110. if(!match) {
  111. UA_LOG_INFO_SESSION(server->config.logger, session,
  112. "ActivateSession: Did not find matching username/password");
  113. response->responseHeader.serviceResult = UA_STATUSCODE_BADUSERACCESSDENIED;
  114. return;
  115. }
  116. } else {
  117. /* Unsupported token type */
  118. response->responseHeader.serviceResult = UA_STATUSCODE_BADIDENTITYTOKENINVALID;
  119. return;
  120. }
  121. /* Detach the old SecureChannel */
  122. if(session->channel && session->channel != channel) {
  123. UA_LOG_INFO_SESSION(server->config.logger, session, "ActivateSession: Detach from old channel");
  124. UA_SecureChannel_detachSession(session->channel, session);
  125. }
  126. /* Attach to the SecureChannel and activate */
  127. UA_SecureChannel_attachSession(channel, session);
  128. session->activated = true;
  129. UA_Session_updateLifetime(session);
  130. UA_LOG_INFO_SESSION(server->config.logger, session, "ActivateSession: Session activated");
  131. }
  132. void
  133. Service_CloseSession(UA_Server *server, UA_Session *session, const UA_CloseSessionRequest *request,
  134. UA_CloseSessionResponse *response) {
  135. UA_LOG_INFO_SESSION(server->config.logger, session, "CloseSession");
  136. response->responseHeader.serviceResult =
  137. UA_SessionManager_removeSession(&server->sessionManager, &session->authenticationToken);
  138. }