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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #ifdef HAVE_NETTLE
  37. " or RSA-AES security types (RA2, RA2ne, RA2_256, RA2ne_256)"
  38. #endif
  39. ,
  40. "");
  41. bool PasswordValidator::validUser(const char* username)
  42. {
  43. CharArray users(plainUsers.getValueStr()), user;
  44. while (users.buf) {
  45. strSplit(users.buf, ',', &user.buf, &users.buf);
  46. #ifdef WIN32
  47. if (0 == stricmp(user.buf, "*"))
  48. return true;
  49. if (0 == stricmp(user.buf, username))
  50. return true;
  51. #else
  52. if (!strcmp(user.buf, "*"))
  53. return true;
  54. if (!strcmp(user.buf, username))
  55. return true;
  56. #endif
  57. }
  58. return false;
  59. }
  60. SSecurityPlain::SSecurityPlain(SConnection* sc) : SSecurity(sc)
  61. {
  62. #ifdef WIN32
  63. valid = new WinPasswdValidator();
  64. #elif !defined(__APPLE__)
  65. valid = new UnixPasswordValidator();
  66. #else
  67. valid = NULL;
  68. #endif
  69. state = 0;
  70. }
  71. bool SSecurityPlain::processMsg()
  72. {
  73. rdr::InStream* is = sc->getInStream();
  74. char* pw;
  75. char *uname;
  76. CharArray password;
  77. if (!valid)
  78. throw AuthFailureException("No password validator configured");
  79. if (state == 0) {
  80. if (!is->hasData(8))
  81. return false;
  82. ulen = is->readU32();
  83. if (ulen > MaxSaneUsernameLength)
  84. throw AuthFailureException("Too long username");
  85. plen = is->readU32();
  86. if (plen > MaxSanePasswordLength)
  87. throw AuthFailureException("Too long password");
  88. state = 1;
  89. }
  90. if (state == 1) {
  91. if (!is->hasData(ulen + plen))
  92. return false;
  93. state = 2;
  94. pw = new char[plen + 1];
  95. uname = new char[ulen + 1];
  96. username.replaceBuf(uname);
  97. password.replaceBuf(pw);
  98. is->readBytes(uname, ulen);
  99. is->readBytes(pw, plen);
  100. pw[plen] = 0;
  101. uname[ulen] = 0;
  102. plen = 0;
  103. if (!valid->validate(sc, uname, pw))
  104. throw AuthFailureException("invalid password or username");
  105. }
  106. return true;
  107. }