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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #include <rfb/SSecurityVncAuth.h>
  24. #include <rdr/RandomStream.h>
  25. #include <rfb/SConnection.h>
  26. #include <rfb/Password.h>
  27. #include <rfb/Configuration.h>
  28. #include <rfb/LogWriter.h>
  29. #include <rfb/util.h>
  30. #include <rfb/Exception.h>
  31. #include <string.h>
  32. #include <stdio.h>
  33. extern "C" {
  34. #include <rfb/d3des.h>
  35. }
  36. using namespace rfb;
  37. static LogWriter vlog("SVncAuth");
  38. StringParameter SSecurityVncAuth::vncAuthPasswdFile
  39. ("PasswordFile", "Password file for VNC authentication", "", ConfServer);
  40. AliasParameter rfbauth("rfbauth", "Alias for PasswordFile",
  41. &SSecurityVncAuth::vncAuthPasswdFile, ConfServer);
  42. VncAuthPasswdParameter SSecurityVncAuth::vncAuthPasswd
  43. ("Password", "Obfuscated binary encoding of the password which clients must supply to "
  44. "access the server", &SSecurityVncAuth::vncAuthPasswdFile);
  45. SSecurityVncAuth::SSecurityVncAuth(SConnection* sc)
  46. : SSecurity(sc), sentChallenge(false),
  47. pg(&vncAuthPasswd), accessRights(0)
  48. {
  49. }
  50. bool SSecurityVncAuth::verifyResponse(const PlainPasswd &password)
  51. {
  52. rdr::U8 expectedResponse[vncAuthChallengeSize];
  53. // Calculate the expected response
  54. rdr::U8 key[8];
  55. int pwdLen = strlen(password.buf);
  56. for (int i=0; i<8; i++)
  57. key[i] = i<pwdLen ? password.buf[i] : 0;
  58. deskey(key, EN0);
  59. for (int j = 0; j < vncAuthChallengeSize; j += 8)
  60. des(challenge+j, expectedResponse+j);
  61. // Check the actual response
  62. return memcmp(response, expectedResponse, vncAuthChallengeSize) == 0;
  63. }
  64. bool SSecurityVncAuth::processMsg()
  65. {
  66. rdr::InStream* is = sc->getInStream();
  67. rdr::OutStream* os = sc->getOutStream();
  68. if (!sentChallenge) {
  69. rdr::RandomStream rs;
  70. if (!rs.hasData(vncAuthChallengeSize))
  71. throw Exception("Could not generate random data for VNC auth challenge");
  72. rs.readBytes(challenge, vncAuthChallengeSize);
  73. os->writeBytes(challenge, vncAuthChallengeSize);
  74. os->flush();
  75. sentChallenge = true;
  76. return false;
  77. }
  78. if (!is->hasData(vncAuthChallengeSize))
  79. return false;
  80. is->readBytes(response, vncAuthChallengeSize);
  81. PlainPasswd passwd, passwdReadOnly;
  82. pg->getVncAuthPasswd(&passwd, &passwdReadOnly);
  83. if (!passwd.buf)
  84. throw AuthFailureException("No password configured for VNC Auth");
  85. if (verifyResponse(passwd)) {
  86. accessRights = SConnection::AccessDefault;
  87. return true;
  88. }
  89. if (passwdReadOnly.buf && verifyResponse(passwdReadOnly)) {
  90. accessRights = SConnection::AccessView;
  91. return true;
  92. }
  93. throw AuthFailureException();
  94. }
  95. VncAuthPasswdParameter::VncAuthPasswdParameter(const char* name,
  96. const char* desc,
  97. StringParameter* passwdFile_)
  98. : BinaryParameter(name, desc, 0, 0, ConfServer), passwdFile(passwdFile_) {
  99. }
  100. void VncAuthPasswdParameter::getVncAuthPasswd(PlainPasswd *password, PlainPasswd *readOnlyPassword) {
  101. ObfuscatedPasswd obfuscated, obfuscatedReadOnly;
  102. getData((void**)&obfuscated.buf, &obfuscated.length);
  103. if (obfuscated.length == 0) {
  104. if (passwdFile) {
  105. CharArray fname(passwdFile->getData());
  106. if (!fname.buf[0]) {
  107. vlog.info("neither %s nor %s params set", getName(), passwdFile->getName());
  108. return;
  109. }
  110. FILE* fp = fopen(fname.buf, "r");
  111. if (!fp) {
  112. vlog.error("opening password file '%s' failed",fname.buf);
  113. return;
  114. }
  115. vlog.debug("reading password file");
  116. obfuscated.buf = new char[8];
  117. obfuscated.length = fread(obfuscated.buf, 1, 8, fp);
  118. obfuscatedReadOnly.buf = new char[8];
  119. obfuscatedReadOnly.length = fread(obfuscatedReadOnly.buf, 1, 8, fp);
  120. fclose(fp);
  121. } else {
  122. vlog.info("%s parameter not set", getName());
  123. }
  124. }
  125. try {
  126. PlainPasswd plainPassword(obfuscated);
  127. password->replaceBuf(plainPassword.takeBuf());
  128. PlainPasswd plainPasswordReadOnly(obfuscatedReadOnly);
  129. readOnlyPassword->replaceBuf(plainPasswordReadOnly.takeBuf());
  130. } catch (...) {
  131. }
  132. }