ua_services_session.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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
  6. Service_CreateSession(UA_Server *server, UA_Session *session, 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, (void**)&response->serverEndpoints,
  15. &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION], server->endpointDescriptionsSize);
  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, const UA_ActivateSessionRequest *request,
  48. UA_ActivateSessionResponse *response) {
  49. UA_SecureChannel *channel = session->channel;
  50. // make the channel know about the session
  51. UA_Session *foundSession =
  52. UA_SessionManager_getSession(&server->sessionManager,
  53. (const UA_NodeId*)&request->requestHeader.authenticationToken);
  54. if(foundSession == NULL) {
  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. } else if(foundSession->validTill < UA_DateTime_now()) {
  61. UA_LOG_DEBUG(server->logger, UA_LOGCATEGORY_SESSION,
  62. "Processing ActivateSessionRequest on SecureChannel %i, but the session has timed out",
  63. channel->securityToken.channelId);
  64. response->responseHeader.serviceResult = UA_STATUSCODE_BADSESSIONIDINVALID;
  65. return;
  66. }
  67. UA_LOG_DEBUG(server->logger, UA_LOGCATEGORY_SESSION,
  68. "Processing ActivateSessionRequest on SecureChannel %i for Session (ns=%i,i=%i)",
  69. channel->securityToken.channelId, foundSession->sessionId.namespaceIndex,
  70. foundSession->sessionId.identifier.numeric);
  71. UA_UserIdentityToken token;
  72. UA_UserIdentityToken_init(&token);
  73. size_t offset = 0;
  74. UA_UserIdentityToken_decodeBinary(&request->userIdentityToken.body, &offset, &token);
  75. UA_UserNameIdentityToken username_token;
  76. UA_UserNameIdentityToken_init(&username_token);
  77. UA_String ap = UA_STRING(ANONYMOUS_POLICY);
  78. UA_String up = UA_STRING(USERNAME_POLICY);
  79. //(Compatibility notice)
  80. //Siemens OPC Scout v10 provides an empty policyId, this is not okay
  81. //For compatibility we will assume that empty policyId == ANONYMOUS_POLICY
  82. //if(token.policyId.data == NULL) {
  83. // /* 1) no policy defined */
  84. // response->responseHeader.serviceResult = UA_STATUSCODE_BADIDENTITYTOKENINVALID;
  85. //} else
  86. //(End Compatibility notice)
  87. if(server->config.Login_enableAnonymous && (token.policyId.data == NULL || UA_String_equal(&token.policyId, &ap))) {
  88. /* 2) anonymous logins */
  89. if(foundSession->channel && foundSession->channel != channel)
  90. UA_SecureChannel_detachSession(foundSession->channel, foundSession);
  91. UA_SecureChannel_attachSession(channel, foundSession);
  92. foundSession->activated = UA_TRUE;
  93. UA_Session_updateLifetime(foundSession);
  94. } else if(server->config.Login_enableUsernamePassword && UA_String_equal(&token.policyId, &up)) {
  95. /* 3) username logins */
  96. offset = 0;
  97. UA_UserNameIdentityToken_decodeBinary(&request->userIdentityToken.body, &offset, &username_token);
  98. if(username_token.encryptionAlgorithm.data != NULL) {
  99. /* 3.1) we only support encryption */
  100. response->responseHeader.serviceResult = UA_STATUSCODE_BADIDENTITYTOKENINVALID;
  101. } else if(username_token.userName.length == -1 && username_token.password.length == -1){
  102. /* 3.2) empty username and password */
  103. response->responseHeader.serviceResult = UA_STATUSCODE_BADIDENTITYTOKENINVALID;
  104. } else {
  105. /* 3.3) ok, trying to match the username */
  106. UA_UInt32 i = 0;
  107. for(; i < server->config.Login_loginsCount; ++i) {
  108. UA_String user = UA_STRING(server->config.Login_usernames[i]);
  109. UA_String pw = UA_STRING(server->config.Login_passwords[i]);
  110. if(UA_String_equal(&username_token.userName, &user) &&
  111. UA_String_equal(&username_token.password, &pw)) {
  112. /* success - activate */
  113. if(foundSession->channel && foundSession->channel != channel)
  114. UA_SecureChannel_detachSession(foundSession->channel, foundSession);
  115. UA_SecureChannel_attachSession(channel, foundSession);
  116. foundSession->activated = UA_TRUE;
  117. UA_Session_updateLifetime(foundSession);
  118. break;
  119. }
  120. }
  121. /* no username/pass matched */
  122. if(i >= server->config.Login_loginsCount)
  123. response->responseHeader.serviceResult = UA_STATUSCODE_BADUSERACCESSDENIED;
  124. }
  125. } else {
  126. response->responseHeader.serviceResult = UA_STATUSCODE_BADIDENTITYTOKENINVALID;
  127. }
  128. UA_UserIdentityToken_deleteMembers(&token);
  129. UA_UserNameIdentityToken_deleteMembers(&username_token);
  130. return;
  131. }
  132. void Service_CloseSession(UA_Server *server, UA_Session *session, const UA_CloseSessionRequest *request,
  133. UA_CloseSessionResponse *response) {
  134. UA_LOG_DEBUG(server->logger, UA_LOGCATEGORY_SESSION,
  135. "Processing CloseSessionRequest for Session (ns=%i,i=%i)",
  136. session->sessionId.namespaceIndex, session->sessionId.identifier.numeric);
  137. response->responseHeader.serviceResult =
  138. UA_SessionManager_removeSession(&server->sessionManager, server, &session->authenticationToken);
  139. }