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.

SSecurityPlain.cxx 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* Copyright (C) 2005 Martin Koegler
  2. * Copyright (C) 2006 OCCAM Financial Technology
  3. *
  4. * This is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This software is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this software; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  17. * USA.
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. #include <config.h>
  21. #endif
  22. #include <rfb/SSecurityPlain.h>
  23. #include <rfb/SConnection.h>
  24. #include <rfb/Exception.h>
  25. #include <rfb/util.h>
  26. #include <rdr/InStream.h>
  27. #if !defined(WIN32) && !defined(__APPLE__)
  28. #include <rfb/UnixPasswordValidator.h>
  29. #endif
  30. #ifdef WIN32
  31. #include <rfb/WinPasswdValidator.h>
  32. #endif
  33. using namespace rfb;
  34. StringParameter PasswordValidator::plainUsers
  35. ("PlainUsers",
  36. "Users permitted to access via Plain security type (including TLSPlain, X509Plain etc.)"
  37. #ifdef HAVE_NETTLE
  38. " or RSA-AES security types (RA2, RA2ne, RA2_256, RA2ne_256)"
  39. #endif
  40. ,
  41. "");
  42. bool PasswordValidator::validUser(const char* username)
  43. {
  44. std::vector<std::string> users;
  45. users = split(plainUsers, ',');
  46. for (size_t i = 0; i < users.size(); i++) {
  47. if (users[i] == "*")
  48. return true;
  49. if (users[i] == username)
  50. return true;
  51. }
  52. return false;
  53. }
  54. SSecurityPlain::SSecurityPlain(SConnection* sc) : SSecurity(sc)
  55. {
  56. #ifdef WIN32
  57. valid = new WinPasswdValidator();
  58. #elif !defined(__APPLE__)
  59. valid = new UnixPasswordValidator();
  60. #else
  61. valid = NULL;
  62. #endif
  63. state = 0;
  64. }
  65. bool SSecurityPlain::processMsg()
  66. {
  67. rdr::InStream* is = sc->getInStream();
  68. char password[1024];
  69. if (!valid)
  70. throw AuthFailureException("No password validator configured");
  71. if (state == 0) {
  72. if (!is->hasData(8))
  73. return false;
  74. ulen = is->readU32();
  75. if (ulen >= sizeof(username))
  76. throw AuthFailureException("Too long username");
  77. plen = is->readU32();
  78. if (plen >= sizeof(password))
  79. throw AuthFailureException("Too long password");
  80. state = 1;
  81. }
  82. if (state == 1) {
  83. if (!is->hasData(ulen + plen))
  84. return false;
  85. state = 2;
  86. is->readBytes((uint8_t*)username, ulen);
  87. is->readBytes((uint8_t*)password, plen);
  88. password[plen] = 0;
  89. username[ulen] = 0;
  90. plen = 0;
  91. if (!valid->validate(sc, username, password))
  92. throw AuthFailureException("invalid password or username");
  93. }
  94. return true;
  95. }