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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 <rdr/InStream.h>
  26. #if !defined(WIN32) && !defined(__APPLE__)
  27. #include <rfb/UnixPasswordValidator.h>
  28. #endif
  29. #ifdef WIN32
  30. #include <rfb/WinPasswdValidator.h>
  31. #endif
  32. using namespace rfb;
  33. StringParameter PasswordValidator::plainUsers
  34. ("PlainUsers",
  35. "Users permitted to access via Plain security type (including TLSPlain, X509Plain etc.)",
  36. "");
  37. bool PasswordValidator::validUser(const char* username)
  38. {
  39. CharArray users(plainUsers.getValueStr()), user;
  40. while (users.buf) {
  41. strSplit(users.buf, ',', &user.buf, &users.buf);
  42. #ifdef WIN32
  43. if (0 == stricmp(user.buf, "*"))
  44. return true;
  45. if (0 == stricmp(user.buf, username))
  46. return true;
  47. #else
  48. if (!strcmp(user.buf, "*"))
  49. return true;
  50. if (!strcmp(user.buf, username))
  51. return true;
  52. #endif
  53. }
  54. return false;
  55. }
  56. SSecurityPlain::SSecurityPlain(SConnection* sc) : SSecurity(sc)
  57. {
  58. #ifdef WIN32
  59. valid = new WinPasswdValidator();
  60. #elif !defined(__APPLE__)
  61. valid = new UnixPasswordValidator();
  62. #else
  63. valid = NULL;
  64. #endif
  65. state = 0;
  66. }
  67. bool SSecurityPlain::processMsg()
  68. {
  69. rdr::InStream* is = sc->getInStream();
  70. char* pw;
  71. char *uname;
  72. CharArray password;
  73. if (!valid)
  74. throw AuthFailureException("No password validator configured");
  75. if (state == 0) {
  76. if (!is->hasData(8))
  77. return false;
  78. ulen = is->readU32();
  79. if (ulen > MaxSaneUsernameLength)
  80. throw AuthFailureException("Too long username");
  81. plen = is->readU32();
  82. if (plen > MaxSanePasswordLength)
  83. throw AuthFailureException("Too long password");
  84. state = 1;
  85. }
  86. if (state == 1) {
  87. if (!is->hasData(ulen + plen))
  88. return false;
  89. state = 2;
  90. pw = new char[plen + 1];
  91. uname = new char[ulen + 1];
  92. username.replaceBuf(uname);
  93. password.replaceBuf(pw);
  94. is->readBytes(uname, ulen);
  95. is->readBytes(pw, plen);
  96. pw[plen] = 0;
  97. uname[ulen] = 0;
  98. plen = 0;
  99. if (!valid->validate(sc, uname, pw))
  100. throw AuthFailureException("invalid password or username");
  101. }
  102. return true;
  103. }