You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SSecurityVncAuth.cxx 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. //
  19. // SSecurityVncAuth
  20. //
  21. // XXX not thread-safe, because d3des isn't - do we need to worry about this?
  22. //
  23. #ifdef HAVE_CONFIG_H
  24. #include <config.h>
  25. #endif
  26. #include <rfb/SSecurityVncAuth.h>
  27. #include <rdr/RandomStream.h>
  28. #include <rfb/SConnection.h>
  29. #include <rfb/Configuration.h>
  30. #include <rfb/LogWriter.h>
  31. #include <rfb/Exception.h>
  32. #include <rfb/obfuscate.h>
  33. #include <assert.h>
  34. #include <string.h>
  35. #include <stdio.h>
  36. extern "C" {
  37. #include <rfb/d3des.h>
  38. }
  39. using namespace rfb;
  40. static LogWriter vlog("SVncAuth");
  41. StringParameter SSecurityVncAuth::vncAuthPasswdFile
  42. ("PasswordFile", "Password file for VNC authentication", "", ConfServer);
  43. AliasParameter rfbauth("rfbauth", "Alias for PasswordFile",
  44. &SSecurityVncAuth::vncAuthPasswdFile, ConfServer);
  45. VncAuthPasswdParameter SSecurityVncAuth::vncAuthPasswd
  46. ("Password", "Obfuscated binary encoding of the password which clients must supply to "
  47. "access the server", &SSecurityVncAuth::vncAuthPasswdFile);
  48. SSecurityVncAuth::SSecurityVncAuth(SConnection* sc)
  49. : SSecurity(sc), sentChallenge(false),
  50. pg(&vncAuthPasswd), accessRights(0)
  51. {
  52. }
  53. bool SSecurityVncAuth::verifyResponse(const char* password)
  54. {
  55. uint8_t expectedResponse[vncAuthChallengeSize];
  56. // Calculate the expected response
  57. uint8_t key[8];
  58. int pwdLen = strlen(password);
  59. for (int i=0; i<8; i++)
  60. key[i] = i<pwdLen ? password[i] : 0;
  61. deskey(key, EN0);
  62. for (int j = 0; j < vncAuthChallengeSize; j += 8)
  63. des(challenge+j, expectedResponse+j);
  64. // Check the actual response
  65. return memcmp(response, expectedResponse, vncAuthChallengeSize) == 0;
  66. }
  67. bool SSecurityVncAuth::processMsg()
  68. {
  69. rdr::InStream* is = sc->getInStream();
  70. rdr::OutStream* os = sc->getOutStream();
  71. if (!sentChallenge) {
  72. rdr::RandomStream rs;
  73. if (!rs.hasData(vncAuthChallengeSize))
  74. throw Exception("Could not generate random data for VNC auth challenge");
  75. rs.readBytes(challenge, vncAuthChallengeSize);
  76. os->writeBytes(challenge, vncAuthChallengeSize);
  77. os->flush();
  78. sentChallenge = true;
  79. return false;
  80. }
  81. if (!is->hasData(vncAuthChallengeSize))
  82. return false;
  83. is->readBytes(response, vncAuthChallengeSize);
  84. std::string passwd, passwdReadOnly;
  85. pg->getVncAuthPasswd(&passwd, &passwdReadOnly);
  86. if (passwd.empty())
  87. throw AuthFailureException("No password configured for VNC Auth");
  88. if (verifyResponse(passwd.c_str())) {
  89. accessRights = SConnection::AccessDefault;
  90. return true;
  91. }
  92. if (!passwdReadOnly.empty() &&
  93. verifyResponse(passwdReadOnly.c_str())) {
  94. accessRights = SConnection::AccessView;
  95. return true;
  96. }
  97. throw AuthFailureException();
  98. }
  99. VncAuthPasswdParameter::VncAuthPasswdParameter(const char* name,
  100. const char* desc,
  101. StringParameter* passwdFile_)
  102. : BinaryParameter(name, desc, 0, 0, ConfServer), passwdFile(passwdFile_) {
  103. }
  104. void VncAuthPasswdParameter::getVncAuthPasswd(std::string *password, std::string *readOnlyPassword) {
  105. std::vector<uint8_t> obfuscated, obfuscatedReadOnly;
  106. obfuscated = getData();
  107. if (obfuscated.size() == 0) {
  108. if (passwdFile) {
  109. const char *fname = *passwdFile;
  110. if (!fname[0]) {
  111. vlog.info("neither %s nor %s params set", getName(), passwdFile->getName());
  112. return;
  113. }
  114. FILE* fp = fopen(fname, "r");
  115. if (!fp) {
  116. vlog.error("opening password file '%s' failed", fname);
  117. return;
  118. }
  119. vlog.debug("reading password file");
  120. obfuscated.resize(8);
  121. obfuscated.resize(fread(obfuscated.data(), 1, 8, fp));
  122. obfuscatedReadOnly.resize(8);
  123. obfuscatedReadOnly.resize(fread(obfuscatedReadOnly.data(), 1, 8, fp));
  124. fclose(fp);
  125. } else {
  126. vlog.info("%s parameter not set", getName());
  127. }
  128. }
  129. assert(password != NULL);
  130. assert(readOnlyPassword != NULL);
  131. try {
  132. *password = deobfuscate(obfuscated.data(), obfuscated.size());
  133. *readOnlyPassword = deobfuscate(obfuscatedReadOnly.data(), obfuscatedReadOnly.size());
  134. } catch (...) {
  135. }
  136. }