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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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(void)
  46. : sentChallenge(false), responsePos(0), pg(&vncAuthPasswd)
  47. {
  48. }
  49. bool SSecurityVncAuth::processMsg(SConnection* sc)
  50. {
  51. rdr::InStream* is = sc->getInStream();
  52. rdr::OutStream* os = sc->getOutStream();
  53. if (!sentChallenge) {
  54. rdr::RandomStream rs;
  55. rs.readBytes(challenge, vncAuthChallengeSize);
  56. os->writeBytes(challenge, vncAuthChallengeSize);
  57. os->flush();
  58. sentChallenge = true;
  59. return false;
  60. }
  61. while (responsePos < vncAuthChallengeSize && is->checkNoWait(1))
  62. response[responsePos++] = is->readU8();
  63. if (responsePos < vncAuthChallengeSize) return false;
  64. PlainPasswd passwd(pg->getVncAuthPasswd());
  65. if (!passwd.buf)
  66. throw AuthFailureException("No password configured for VNC Auth");
  67. // Calculate the expected response
  68. rdr::U8 key[8];
  69. int pwdLen = strlen(passwd.buf);
  70. for (int i=0; i<8; i++)
  71. key[i] = i<pwdLen ? passwd.buf[i] : 0;
  72. deskey(key, EN0);
  73. for (int j = 0; j < vncAuthChallengeSize; j += 8)
  74. des(challenge+j, challenge+j);
  75. // Check the actual response
  76. if (memcmp(challenge, response, vncAuthChallengeSize) != 0)
  77. throw AuthFailureException();
  78. return true;
  79. }
  80. VncAuthPasswdParameter::VncAuthPasswdParameter(const char* name,
  81. const char* desc,
  82. StringParameter* passwdFile_)
  83. : BinaryParameter(name, desc, 0, 0, ConfServer), passwdFile(passwdFile_) {
  84. }
  85. char* VncAuthPasswdParameter::getVncAuthPasswd() {
  86. ObfuscatedPasswd obfuscated;
  87. getData((void**)&obfuscated.buf, &obfuscated.length);
  88. if (obfuscated.length == 0) {
  89. if (passwdFile) {
  90. CharArray fname(passwdFile->getData());
  91. if (!fname.buf[0]) {
  92. vlog.info("neither %s nor %s params set", getName(), passwdFile->getName());
  93. return 0;
  94. }
  95. FILE* fp = fopen(fname.buf, "r");
  96. if (!fp) {
  97. vlog.error("opening password file '%s' failed",fname.buf);
  98. return 0;
  99. }
  100. vlog.debug("reading password file");
  101. obfuscated.buf = new char[128];
  102. obfuscated.length = fread(obfuscated.buf, 1, 128, fp);
  103. fclose(fp);
  104. } else {
  105. vlog.info("%s parameter not set", getName());
  106. }
  107. }
  108. try {
  109. PlainPasswd password(obfuscated);
  110. return password.takeBuf();
  111. } catch (...) {
  112. return 0;
  113. }
  114. }