ua_services_session.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include "ua_services.h"
  2. #include "ua_server_internal.h"
  3. #include "ua_session_manager.h"
  4. #include "ua_types_generated_encoding_binary.h"
  5. void Service_CreateSession(UA_Server *server, UA_SecureChannel *channel,
  6. const UA_CreateSessionRequest *request, UA_CreateSessionResponse *response) {
  7. if(channel->securityToken.channelId == 0) {
  8. response->responseHeader.serviceResult = UA_STATUSCODE_BADSECURECHANNELIDINVALID;
  9. return;
  10. }
  11. response->responseHeader.serviceResult =
  12. UA_Array_copy(server->endpointDescriptions, server->endpointDescriptionsSize,
  13. (void**)&response->serverEndpoints, &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]);
  14. if(response->responseHeader.serviceResult != UA_STATUSCODE_GOOD)
  15. return;
  16. response->serverEndpointsSize = server->endpointDescriptionsSize;
  17. UA_Session *newSession;
  18. response->responseHeader.serviceResult =
  19. UA_SessionManager_createSession(&server->sessionManager, channel, request, &newSession);
  20. if(response->responseHeader.serviceResult != UA_STATUSCODE_GOOD) {
  21. UA_LOG_DEBUG_CHANNEL(server->config.logger, channel, "Processing CreateSessionRequest failed");
  22. return;
  23. }
  24. //TODO get maxResponseMessageSize internally
  25. newSession->maxResponseMessageSize = request->maxResponseMessageSize;
  26. response->sessionId = newSession->sessionId;
  27. response->revisedSessionTimeout = (UA_Double)newSession->timeout;
  28. response->authenticationToken = newSession->authenticationToken;
  29. response->responseHeader.serviceResult = UA_String_copy(&request->sessionName, &newSession->sessionName);
  30. if(server->endpointDescriptions)
  31. response->responseHeader.serviceResult |=
  32. UA_ByteString_copy(&server->endpointDescriptions->serverCertificate,
  33. &response->serverCertificate);
  34. if(response->responseHeader.serviceResult != UA_STATUSCODE_GOOD) {
  35. UA_SessionManager_removeSession(&server->sessionManager, &newSession->authenticationToken);
  36. return;
  37. }
  38. UA_LOG_DEBUG_CHANNEL(server->config.logger, channel, "Session %i created",
  39. newSession->sessionId.identifier.numeric);
  40. }
  41. void
  42. Service_ActivateSession(UA_Server *server, UA_SecureChannel *channel, UA_Session *session,
  43. const UA_ActivateSessionRequest *request, UA_ActivateSessionResponse *response) {
  44. if(session->validTill < UA_DateTime_now()) {
  45. UA_LOG_INFO_SESSION(server->config.logger, session, "ActivateSession: SecureChannel %i wants "
  46. "to activate, but the session has timed out", channel->securityToken.channelId);
  47. response->responseHeader.serviceResult = UA_STATUSCODE_BADSESSIONIDINVALID;
  48. return;
  49. }
  50. if(request->userIdentityToken.encoding < UA_EXTENSIONOBJECT_DECODED ||
  51. (request->userIdentityToken.content.decoded.type != &UA_TYPES[UA_TYPES_ANONYMOUSIDENTITYTOKEN] &&
  52. request->userIdentityToken.content.decoded.type != &UA_TYPES[UA_TYPES_USERNAMEIDENTITYTOKEN])) {
  53. UA_LOG_INFO_SESSION(server->config.logger, session, "ActivateSession: SecureChannel %i wants "
  54. "to activate, but the UserIdentify token is invalid",
  55. channel->securityToken.channelId);
  56. response->responseHeader.serviceResult = UA_STATUSCODE_BADINTERNALERROR;
  57. return;
  58. }
  59. UA_String ap = UA_STRING(ANONYMOUS_POLICY);
  60. UA_String up = UA_STRING(USERNAME_POLICY);
  61. /* Compatibility notice: Siemens OPC Scout v10 provides an empty policyId,
  62. this is not okay For compatibility we will assume that empty policyId ==
  63. ANONYMOUS_POLICY
  64. if(token.policyId->data == NULL)
  65. response->responseHeader.serviceResult = UA_STATUSCODE_BADIDENTITYTOKENINVALID;
  66. */
  67. /* anonymous login */
  68. if(server->config.enableAnonymousLogin &&
  69. request->userIdentityToken.content.decoded.type == &UA_TYPES[UA_TYPES_ANONYMOUSIDENTITYTOKEN]) {
  70. const UA_AnonymousIdentityToken *token = request->userIdentityToken.content.decoded.data;
  71. if(token->policyId.data && !UA_String_equal(&token->policyId, &ap)) {
  72. response->responseHeader.serviceResult = UA_STATUSCODE_BADIDENTITYTOKENINVALID;
  73. return;
  74. }
  75. if(session->channel && session->channel != channel) {
  76. /* Close the old SecureChannel (this also detaches it) */
  77. UA_LOG_INFO_SESSION(server->config.logger, session,
  78. "ActivateSession: Detach from old channel");
  79. UA_SecureChannelManager_close(&server->secureChannelManager,
  80. session->channel->securityToken.channelId);
  81. }
  82. UA_SecureChannel_attachSession(channel, session);
  83. session->activated = true;
  84. UA_Session_updateLifetime(session);
  85. UA_LOG_INFO_SESSION(server->config.logger, session, "ActivateSession: Session activated");
  86. return;
  87. }
  88. /* username login */
  89. if(server->config.enableUsernamePasswordLogin &&
  90. request->userIdentityToken.content.decoded.type == &UA_TYPES[UA_TYPES_USERNAMEIDENTITYTOKEN]) {
  91. const UA_UserNameIdentityToken *token = request->userIdentityToken.content.decoded.data;
  92. if(!UA_String_equal(&token->policyId, &up)) {
  93. response->responseHeader.serviceResult = UA_STATUSCODE_BADIDENTITYTOKENINVALID;
  94. return;
  95. }
  96. if(token->encryptionAlgorithm.length > 0) {
  97. /* we don't support encryption */
  98. response->responseHeader.serviceResult = UA_STATUSCODE_BADIDENTITYTOKENINVALID;
  99. return;
  100. }
  101. /* ok, trying to match the username */
  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. continue;
  107. /* success - activate */
  108. if(session->channel && session->channel != channel) {
  109. UA_LOG_INFO_SESSION(server->config.logger, session,
  110. "ActivateSession: Detach from old channel");
  111. UA_SecureChannel_detachSession(session->channel, session);
  112. }
  113. UA_SecureChannel_attachSession(channel, session);
  114. session->activated = true;
  115. UA_Session_updateLifetime(session);
  116. UA_LOG_INFO_SESSION(server->config.logger, session, "ActivateSession: Session activated");
  117. return;
  118. }
  119. /* no match */
  120. UA_LOG_INFO_SESSION(server->config.logger, session,
  121. "ActivateSession: Did not find matching username/password");
  122. response->responseHeader.serviceResult = UA_STATUSCODE_BADUSERACCESSDENIED;
  123. return;
  124. }
  125. response->responseHeader.serviceResult = UA_STATUSCODE_BADIDENTITYTOKENINVALID;
  126. }
  127. void
  128. Service_CloseSession(UA_Server *server, UA_Session *session, const UA_CloseSessionRequest *request,
  129. UA_CloseSessionResponse *response) {
  130. UA_LOG_INFO_SESSION(server->config.logger, session, "CloseSession");
  131. response->responseHeader.serviceResult =
  132. UA_SessionManager_removeSession(&server->sessionManager, &session->authenticationToken);
  133. }