ua_services_session.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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,
  30. "Processing CreateSessionRequest failed");
  31. return;
  32. }
  33. /* Fill the session with more information */
  34. newSession->maxResponseMessageSize = request->maxResponseMessageSize;
  35. newSession->maxRequestMessageSize = channel->connection->localConf.maxMessageSize;
  36. response->responseHeader.serviceResult |=
  37. UA_ApplicationDescription_copy(&request->clientDescription, &newSession->clientDescription);
  38. /* Prepare the response */
  39. response->sessionId = newSession->sessionId;
  40. response->revisedSessionTimeout = (UA_Double)newSession->timeout;
  41. response->authenticationToken = newSession->authenticationToken;
  42. response->responseHeader.serviceResult = UA_String_copy(&request->sessionName, &newSession->sessionName);
  43. if(server->endpointDescriptionsSize > 0)
  44. response->responseHeader.serviceResult |=
  45. UA_ByteString_copy(&server->endpointDescriptions->serverCertificate,
  46. &response->serverCertificate);
  47. /* Failure -> remove the session */
  48. if(response->responseHeader.serviceResult != UA_STATUSCODE_GOOD) {
  49. UA_SessionManager_removeSession(&server->sessionManager, &newSession->authenticationToken);
  50. return;
  51. }
  52. UA_LOG_DEBUG_CHANNEL(server->config.logger, channel, "Session " UA_PRINTF_GUID_FORMAT " created",
  53. UA_PRINTF_GUID_DATA(newSession->sessionId.identifier.guid));
  54. }
  55. void
  56. Service_ActivateSession(UA_Server *server, UA_SecureChannel *channel,
  57. UA_Session *session, const UA_ActivateSessionRequest *request,
  58. UA_ActivateSessionResponse *response) {
  59. if(session->validTill < UA_DateTime_nowMonotonic()) {
  60. UA_LOG_INFO_SESSION(server->config.logger, session, "ActivateSession: SecureChannel %i wants "
  61. "to activate, but the session has timed out", channel->securityToken.channelId);
  62. response->responseHeader.serviceResult = UA_STATUSCODE_BADSESSIONIDINVALID;
  63. return;
  64. }
  65. /* Callback into userland access control */
  66. response->responseHeader.serviceResult =
  67. server->config.accessControl.activateSession(&session->sessionId, &request->userIdentityToken,
  68. &session->sessionHandle);
  69. if(response->responseHeader.serviceResult != UA_STATUSCODE_GOOD)
  70. return;
  71. /* Detach the old SecureChannel */
  72. if(session->channel && session->channel != channel) {
  73. UA_LOG_INFO_SESSION(server->config.logger, session,
  74. "ActivateSession: Detach from old channel");
  75. UA_SecureChannel_detachSession(session->channel, session);
  76. }
  77. /* Attach to the SecureChannel and activate */
  78. UA_SecureChannel_attachSession(channel, session);
  79. session->activated = true;
  80. UA_Session_updateLifetime(session);
  81. UA_LOG_INFO_SESSION(server->config.logger, session,
  82. "ActivateSession: Session activated");
  83. }
  84. void
  85. Service_CloseSession(UA_Server *server, UA_Session *session,
  86. const UA_CloseSessionRequest *request,
  87. UA_CloseSessionResponse *response) {
  88. UA_LOG_INFO_SESSION(server->config.logger, session, "CloseSession");
  89. /* Callback into userland access control */
  90. server->config.accessControl.closeSession(&session->sessionId, session->sessionHandle);
  91. response->responseHeader.serviceResult =
  92. UA_SessionManager_removeSession(&server->sessionManager, &session->authenticationToken);
  93. }