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.

Security.cxx 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #include <assert.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #ifdef _WIN32
  22. #define strcasecmp _stricmp
  23. #endif
  24. #include <rfb/CSecurityNone.h>
  25. #include <rfb/CSecurityVncAuth.h>
  26. #include <rdr/Exception.h>
  27. #include <rfb/LogWriter.h>
  28. #include <rfb/Security.h>
  29. #include <rfb/SSecurityNone.h>
  30. #include <rfb/SSecurityVncAuth.h>
  31. #include <rfb/util.h>
  32. using namespace rdr;
  33. using namespace rfb;
  34. using namespace std;
  35. static LogWriter vlog("Security");
  36. StringParameter Security::secTypes
  37. ("SecurityTypes",
  38. "Specify which security scheme to use (None, VncAuth)",
  39. "VncAuth", ConfServer);
  40. Security::Security(void) : upg(NULL)
  41. {
  42. char *secTypesStr = secTypes.getData();
  43. enabledSecTypes = parseSecTypes(secTypesStr);
  44. delete secTypesStr;
  45. }
  46. void Security::EnableSecType(U8 secType)
  47. {
  48. list<U8>::iterator i;
  49. for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++)
  50. if (*i == secType)
  51. return;
  52. enabledSecTypes.push_back(secType);
  53. }
  54. bool Security::IsSupported(U8 secType)
  55. {
  56. list<U8>::iterator i;
  57. for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++)
  58. if (*i == secType)
  59. return true;
  60. return false;
  61. }
  62. SSecurity* Security::GetSSecurity(U8 secType)
  63. {
  64. if (!IsSupported(secType))
  65. goto bail;
  66. switch (secType) {
  67. case secTypeNone: return new SSecurityNone();
  68. case secTypeVncAuth: return new SSecurityVncAuth();
  69. }
  70. bail:
  71. throw Exception("Security type not supported");
  72. }
  73. CSecurity* Security::GetCSecurity(rdr::U8 secType)
  74. {
  75. assert (upg != NULL); /* (upg == NULL) means bug in the viewer */
  76. if (!IsSupported(secType))
  77. goto bail;
  78. switch (secType) {
  79. case secTypeNone: return new CSecurityNone();
  80. case secTypeVncAuth: return new CSecurityVncAuth(upg);
  81. }
  82. bail:
  83. throw Exception("Security type not supported");
  84. }
  85. rdr::U8 rfb::secTypeNum(const char* name)
  86. {
  87. if (strcasecmp(name, "None") == 0) return secTypeNone;
  88. if (strcasecmp(name, "VncAuth") == 0) return secTypeVncAuth;
  89. if (strcasecmp(name, "Tight") == 0) return secTypeTight;
  90. if (strcasecmp(name, "RA2") == 0) return secTypeRA2;
  91. if (strcasecmp(name, "RA2ne") == 0) return secTypeRA2ne;
  92. if (strcasecmp(name, "SSPI") == 0) return secTypeSSPI;
  93. if (strcasecmp(name, "SSPIne") == 0) return secTypeSSPIne;
  94. return secTypeInvalid;
  95. }
  96. const char* rfb::secTypeName(rdr::U8 num)
  97. {
  98. switch (num) {
  99. case secTypeNone: return "None";
  100. case secTypeVncAuth: return "VncAuth";
  101. case secTypeTight: return "Tight";
  102. case secTypeRA2: return "RA2";
  103. case secTypeRA2ne: return "RA2ne";
  104. case secTypeSSPI: return "SSPI";
  105. case secTypeSSPIne: return "SSPIne";
  106. default: return "[unknown secType]";
  107. }
  108. }
  109. std::list<rdr::U8> rfb::parseSecTypes(const char* types_)
  110. {
  111. std::list<rdr::U8> result;
  112. CharArray types(strDup(types_)), type;
  113. while (types.buf) {
  114. strSplit(types.buf, ',', &type.buf, &types.buf);
  115. rdr::U8 typeNum = secTypeNum(type.buf);
  116. if (typeNum != secTypeInvalid)
  117. result.push_back(typeNum);
  118. }
  119. return result;
  120. }