ua_services_session.c 5.0 KB

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