ua_services_session.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include "ua_services.h"
  2. #include "ua_server_internal.h"
  3. #include "ua_session_manager.h"
  4. #include "ua_statuscodes.h"
  5. #include "ua_util.h"
  6. void Service_CreateSession(UA_Server *server, UA_SecureChannel *channel,
  7. const UA_CreateSessionRequest *request,
  8. UA_CreateSessionResponse *response) {
  9. response->responseHeader.serviceResult =
  10. UA_Array_copy(server->endpointDescriptions, (void**)&response->serverEndpoints,
  11. &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION], server->endpointDescriptionsSize);
  12. if(response->responseHeader.serviceResult != UA_STATUSCODE_GOOD)
  13. return;
  14. response->serverEndpointsSize = server->endpointDescriptionsSize;
  15. UA_Session *newSession;
  16. response->responseHeader.serviceResult = UA_SessionManager_createSession(&server->sessionManager,
  17. channel, request, &newSession);
  18. if(response->responseHeader.serviceResult != UA_STATUSCODE_GOOD)
  19. return;
  20. //TODO get maxResponseMessageSize internally
  21. newSession->maxResponseMessageSize = request->maxResponseMessageSize;
  22. response->sessionId = newSession->sessionId;
  23. response->revisedSessionTimeout = newSession->timeout;
  24. response->authenticationToken = newSession->authenticationToken;
  25. response->responseHeader.serviceResult = UA_String_copy(&request->sessionName, &newSession->sessionName);
  26. if(server->endpointDescriptions)
  27. response->responseHeader.serviceResult |=
  28. UA_ByteString_copy(&server->endpointDescriptions->serverCertificate, &response->serverCertificate);
  29. if(response->responseHeader.serviceResult != UA_STATUSCODE_GOOD) {
  30. UA_SessionManager_removeSession(&server->sessionManager, &newSession->authenticationToken);
  31. return;
  32. }
  33. }
  34. void Service_ActivateSession(UA_Server *server, UA_SecureChannel *channel,
  35. const UA_ActivateSessionRequest *request,
  36. UA_ActivateSessionResponse *response) {
  37. // make the channel know about the session
  38. UA_Session *foundSession =
  39. UA_SessionManager_getSession(&server->sessionManager,
  40. (const UA_NodeId*)&request->requestHeader.authenticationToken);
  41. if(foundSession == UA_NULL) {
  42. response->responseHeader.serviceResult = UA_STATUSCODE_BADSESSIONIDINVALID;
  43. return;
  44. } else if(foundSession->validTill < UA_DateTime_now()) {
  45. response->responseHeader.serviceResult = UA_STATUSCODE_BADSESSIONIDINVALID;
  46. return;
  47. }
  48. UA_UserIdentityToken token;
  49. UA_UserIdentityToken_init(&token);
  50. size_t offset = 0;
  51. UA_UserIdentityToken_decodeBinary(&request->userIdentityToken.body, &offset, &token);
  52. UA_UserNameIdentityToken username_token;
  53. UA_UserNameIdentityToken_init(&username_token);
  54. if(token.policyId.data == UA_NULL) {
  55. /* 1) no policy defined */
  56. response->responseHeader.serviceResult = UA_STATUSCODE_BADIDENTITYTOKENINVALID;
  57. } else if(server->config.Login_enableAnonymous &&
  58. UA_String_equalchars(&token.policyId, ANONYMOUS_POLICY)) {
  59. /* 2) anonymous logins */
  60. if(foundSession->channel && foundSession->channel != channel)
  61. UA_SecureChannel_detachSession(foundSession->channel, foundSession);
  62. UA_SecureChannel_attachSession(channel, foundSession);
  63. foundSession->activated = UA_TRUE;
  64. UA_Session_updateLifetime(foundSession);
  65. } else if(server->config.Login_enableUsernamePassword &&
  66. UA_String_equalchars(&token.policyId, USERNAME_POLICY)) {
  67. /* 3) username logins */
  68. offset = 0;
  69. UA_UserNameIdentityToken_decodeBinary(&request->userIdentityToken.body, &offset, &username_token);
  70. if(username_token.encryptionAlgorithm.data != UA_NULL) {
  71. /* 3.1) we only support encryption */
  72. response->responseHeader.serviceResult = UA_STATUSCODE_BADIDENTITYTOKENINVALID;
  73. } else if(username_token.userName.length == -1 && username_token.password.length == -1){
  74. /* 3.2) empty username and password */
  75. response->responseHeader.serviceResult = UA_STATUSCODE_BADIDENTITYTOKENINVALID;
  76. } else {
  77. /* 3.3) ok, trying to match the username */
  78. UA_UInt32 i = 0;
  79. for(; i < server->config.Login_loginsCount; ++i) {
  80. if(UA_String_equalchars(&username_token.userName, server->config.Login_usernames[i])
  81. && UA_String_equalchars(&username_token.password, server->config.Login_passwords[i])) {
  82. /* success - activate */
  83. if(foundSession->channel && foundSession->channel != channel)
  84. UA_SecureChannel_detachSession(foundSession->channel, foundSession);
  85. UA_SecureChannel_attachSession(channel, foundSession);
  86. foundSession->activated = UA_TRUE;
  87. UA_Session_updateLifetime(foundSession);
  88. break;
  89. }
  90. }
  91. /* no username/pass matched */
  92. if(i >= server->config.Login_loginsCount)
  93. response->responseHeader.serviceResult = UA_STATUSCODE_BADUSERACCESSDENIED;
  94. }
  95. } else {
  96. response->responseHeader.serviceResult = UA_STATUSCODE_BADIDENTITYTOKENINVALID;
  97. }
  98. UA_UserIdentityToken_deleteMembers(&token);
  99. UA_UserNameIdentityToken_deleteMembers(&username_token);
  100. return;
  101. }
  102. void Service_CloseSession(UA_Server *server, UA_Session *session, const UA_CloseSessionRequest *request,
  103. UA_CloseSessionResponse *response) {
  104. UA_Session *foundSession =
  105. UA_SessionManager_getSession(&server->sessionManager,
  106. (const UA_NodeId*)&request->requestHeader.authenticationToken);
  107. if(foundSession == UA_NULL)
  108. response->responseHeader.serviceResult = UA_STATUSCODE_BADSESSIONIDINVALID;
  109. else
  110. response->responseHeader.serviceResult =
  111. UA_SessionManager_removeSession(&server->sessionManager, &session->authenticationToken);
  112. }