ua_services_session.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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_Session *session,
  6. const UA_CreateSessionRequest *request,
  7. UA_CreateSessionResponse *response) {
  8. UA_SecureChannel *channel = session->channel;
  9. if(channel->securityToken.channelId == 0) {
  10. response->responseHeader.serviceResult = UA_STATUSCODE_BADSECURECHANNELIDINVALID;
  11. return;
  12. }
  13. response->responseHeader.serviceResult =
  14. UA_Array_copy(server->endpointDescriptions, server->endpointDescriptionsSize,
  15. (void**)&response->serverEndpoints, &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]);
  16. if(response->responseHeader.serviceResult != UA_STATUSCODE_GOOD)
  17. return;
  18. response->serverEndpointsSize = server->endpointDescriptionsSize;
  19. UA_Session *newSession;
  20. response->responseHeader.serviceResult =
  21. UA_SessionManager_createSession(&server->sessionManager, channel, request, &newSession);
  22. if(response->responseHeader.serviceResult != UA_STATUSCODE_GOOD) {
  23. UA_LOG_DEBUG(server->logger, UA_LOGCATEGORY_SESSION,
  24. "Processing CreateSessionRequest on SecureChannel %i failed",
  25. channel->securityToken.channelId);
  26. return;
  27. }
  28. //TODO get maxResponseMessageSize internally
  29. newSession->maxResponseMessageSize = request->maxResponseMessageSize;
  30. response->sessionId = newSession->sessionId;
  31. response->revisedSessionTimeout = (UA_Double)newSession->timeout;
  32. response->authenticationToken = newSession->authenticationToken;
  33. response->responseHeader.serviceResult = UA_String_copy(&request->sessionName, &newSession->sessionName);
  34. if(server->endpointDescriptions)
  35. response->responseHeader.serviceResult |=
  36. UA_ByteString_copy(&server->endpointDescriptions->serverCertificate, &response->serverCertificate);
  37. if(response->responseHeader.serviceResult != UA_STATUSCODE_GOOD) {
  38. UA_SessionManager_removeSession(&server->sessionManager, server, &newSession->authenticationToken);
  39. return;
  40. }
  41. UA_LOG_DEBUG(server->logger, UA_LOGCATEGORY_SESSION,
  42. "Processing CreateSessionRequest on SecureChannel %i succeeded, created Session (ns=%i,i=%i)",
  43. channel->securityToken.channelId, response->sessionId.namespaceIndex,
  44. response->sessionId.identifier.numeric);
  45. }
  46. void
  47. Service_ActivateSession(UA_Server *server, UA_Session *session,
  48. const UA_ActivateSessionRequest *request,
  49. UA_ActivateSessionResponse *response) {
  50. UA_SecureChannel *channel = session->channel;
  51. // make the channel know about the session
  52. UA_Session *foundSession =
  53. UA_SessionManager_getSession(&server->sessionManager, &request->requestHeader.authenticationToken);
  54. if(!foundSession) {
  55. response->responseHeader.serviceResult = UA_STATUSCODE_BADSESSIONIDINVALID;
  56. UA_LOG_DEBUG(server->logger, UA_LOGCATEGORY_SESSION,
  57. "Processing ActivateSessionRequest on SecureChannel %i, but no session found for the authentication token",
  58. channel->securityToken.channelId);
  59. return;
  60. }
  61. if(foundSession->validTill < UA_DateTime_now()) {
  62. UA_LOG_DEBUG(server->logger, UA_LOGCATEGORY_SESSION,
  63. "Processing ActivateSessionRequest on SecureChannel %i, but the session has timed out",
  64. channel->securityToken.channelId);
  65. response->responseHeader.serviceResult = UA_STATUSCODE_BADSESSIONIDINVALID;
  66. return;
  67. }
  68. if(request->userIdentityToken.encoding < UA_EXTENSIONOBJECT_DECODED ||
  69. (request->userIdentityToken.content.decoded.type != &UA_TYPES[UA_TYPES_ANONYMOUSIDENTITYTOKEN] &&
  70. request->userIdentityToken.content.decoded.type != &UA_TYPES[UA_TYPES_USERNAMEIDENTITYTOKEN])) {
  71. UA_LOG_DEBUG(server->logger, UA_LOGCATEGORY_SESSION,
  72. "Invalided UserIdentityToken on SecureChannel %i for Session (ns=%i,i=%i)",
  73. channel->securityToken.channelId, foundSession->sessionId.namespaceIndex,
  74. foundSession->sessionId.identifier.numeric);
  75. response->responseHeader.serviceResult = UA_STATUSCODE_BADINTERNALERROR;
  76. return;
  77. }
  78. UA_LOG_DEBUG(server->logger, UA_LOGCATEGORY_SESSION,
  79. "Processing ActivateSessionRequest on SecureChannel %i for Session (ns=%i,i=%i)",
  80. channel->securityToken.channelId, foundSession->sessionId.namespaceIndex,
  81. foundSession->sessionId.identifier.numeric);
  82. UA_String ap = UA_STRING(ANONYMOUS_POLICY);
  83. UA_String up = UA_STRING(USERNAME_POLICY);
  84. /* Compatibility notice: Siemens OPC Scout v10 provides an empty policyId,
  85. this is not okay For compatibility we will assume that empty policyId ==
  86. ANONYMOUS_POLICY
  87. if(token.policyId->data == NULL)
  88. response->responseHeader.serviceResult = UA_STATUSCODE_BADIDENTITYTOKENINVALID;
  89. */
  90. /* anonymous login */
  91. if(server->config.Login_enableAnonymous &&
  92. request->userIdentityToken.content.decoded.type == &UA_TYPES[UA_TYPES_ANONYMOUSIDENTITYTOKEN]) {
  93. const UA_AnonymousIdentityToken *token = request->userIdentityToken.content.decoded.data;
  94. if(token->policyId.data && !UA_String_equal(&token->policyId, &ap)) {
  95. response->responseHeader.serviceResult = UA_STATUSCODE_BADIDENTITYTOKENINVALID;
  96. return;
  97. }
  98. if(foundSession->channel && foundSession->channel != channel)
  99. UA_SecureChannel_detachSession(foundSession->channel, foundSession);
  100. UA_SecureChannel_attachSession(channel, foundSession);
  101. foundSession->activated = UA_TRUE;
  102. UA_Session_updateLifetime(foundSession);
  103. return;
  104. }
  105. /* username login */
  106. if(server->config.Login_enableUsernamePassword &&
  107. request->userIdentityToken.content.decoded.type == &UA_TYPES[UA_TYPES_USERNAMEIDENTITYTOKEN]) {
  108. const UA_UserNameIdentityToken *token = request->userIdentityToken.content.decoded.data;
  109. if(!UA_String_equal(&token->policyId, &up)) {
  110. response->responseHeader.serviceResult = UA_STATUSCODE_BADIDENTITYTOKENINVALID;
  111. return;
  112. }
  113. if(token->encryptionAlgorithm.length > 0) {
  114. /* we don't support encryption */
  115. response->responseHeader.serviceResult = UA_STATUSCODE_BADIDENTITYTOKENINVALID;
  116. return;
  117. }
  118. /* ok, trying to match the username */
  119. for(size_t i = 0; i < server->config.Login_loginsCount; ++i) {
  120. UA_String user = UA_STRING(server->config.Login_usernames[i]);
  121. UA_String pw = UA_STRING(server->config.Login_passwords[i]);
  122. if(!UA_String_equal(&token->userName, &user) || !UA_String_equal(&token->password, &pw))
  123. continue;
  124. /* success - activate */
  125. if(foundSession->channel && foundSession->channel != channel)
  126. UA_SecureChannel_detachSession(foundSession->channel, foundSession);
  127. UA_SecureChannel_attachSession(channel, foundSession);
  128. foundSession->activated = UA_TRUE;
  129. UA_Session_updateLifetime(foundSession);
  130. return;
  131. }
  132. /* no match */
  133. response->responseHeader.serviceResult = UA_STATUSCODE_BADUSERACCESSDENIED;
  134. return;
  135. }
  136. response->responseHeader.serviceResult = UA_STATUSCODE_BADIDENTITYTOKENINVALID;
  137. }
  138. void
  139. Service_CloseSession(UA_Server *server, UA_Session *session,
  140. const UA_CloseSessionRequest *request,
  141. UA_CloseSessionResponse *response) {
  142. UA_LOG_DEBUG(server->logger, UA_LOGCATEGORY_SESSION,
  143. "Processing CloseSessionRequest for Session (ns=%i,i=%i)",
  144. session->sessionId.namespaceIndex, session->sessionId.identifier.numeric);
  145. response->responseHeader.serviceResult =
  146. UA_SessionManager_removeSession(&server->sessionManager, server, &session->authenticationToken);
  147. }