blob: eb99f7c5c5ab7381d9729e83f1d8818b1cd3d759 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* 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 */
Adam Tkac1d15e2d2010-04-23 14:06:38 +000018#include <assert.h>
19#include <stdlib.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000020#include <string.h>
21#ifdef _WIN32
22#define strcasecmp _stricmp
23#endif
Adam Tkacc210e8a2010-04-23 14:09:16 +000024#include <rfb/CSecurityNone.h>
Adam Tkacb10489b2010-04-23 14:16:04 +000025#include <rfb/CSecurityVeNCrypt.h>
Adam Tkacc210e8a2010-04-23 14:09:16 +000026#include <rfb/CSecurityVncAuth.h>
Adam Tkac1d15e2d2010-04-23 14:06:38 +000027#include <rdr/Exception.h>
28#include <rfb/LogWriter.h>
Adam Tkacb6eb3992010-04-23 14:05:00 +000029#include <rfb/Security.h>
Adam Tkac1d15e2d2010-04-23 14:06:38 +000030#include <rfb/SSecurityNone.h>
Adam Tkac1d15e2d2010-04-23 14:06:38 +000031#include <rfb/SSecurityVncAuth.h>
Adam Tkacdfe19cf2010-04-23 14:14:11 +000032#include <rfb/SSecurityVeNCrypt.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000033#include <rfb/util.h>
34
Adam Tkac1d15e2d2010-04-23 14:06:38 +000035using namespace rdr;
36using namespace rfb;
37using namespace std;
38
39static LogWriter vlog("Security");
40
Adam Tkacb10489b2010-04-23 14:16:04 +000041UserPasswdGetter *CSecurity::upg = NULL;
42
Adam Tkaca6578bf2010-04-23 14:07:41 +000043StringParameter Security::secTypes
44("SecurityTypes",
45 "Specify which security scheme to use (None, VncAuth)",
Adam Tkacb10489b2010-04-23 14:16:04 +000046 "VncAuth");
Adam Tkaca6578bf2010-04-23 14:07:41 +000047
Adam Tkacb10489b2010-04-23 14:16:04 +000048Security::Security(void)
Adam Tkac1d15e2d2010-04-23 14:06:38 +000049{
Adam Tkaca6578bf2010-04-23 14:07:41 +000050 char *secTypesStr = secTypes.getData();
Adam Tkac1d15e2d2010-04-23 14:06:38 +000051
52 enabledSecTypes = parseSecTypes(secTypesStr);
53
54 delete secTypesStr;
55}
56
57void Security::EnableSecType(U8 secType)
58{
59 list<U8>::iterator i;
60
61 for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++)
62 if (*i == secType)
63 return;
64
65 enabledSecTypes.push_back(secType);
66}
67
68bool Security::IsSupported(U8 secType)
69{
70 list<U8>::iterator i;
71
72 for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++)
73 if (*i == secType)
74 return true;
75
76 return false;
77}
78
79SSecurity* Security::GetSSecurity(U8 secType)
80{
81 if (!IsSupported(secType))
82 goto bail;
83
84 switch (secType) {
85 case secTypeNone: return new SSecurityNone();
86 case secTypeVncAuth: return new SSecurityVncAuth();
Adam Tkacdfe19cf2010-04-23 14:14:11 +000087 case secTypeVeNCrypt: return new SSecurityVeNCrypt();
Adam Tkacc210e8a2010-04-23 14:09:16 +000088 }
89
90bail:
91 throw Exception("Security type not supported");
92}
93
Adam Tkacb10489b2010-04-23 14:16:04 +000094CSecurity* Security::GetCSecurity(U8 secType)
Adam Tkacc210e8a2010-04-23 14:09:16 +000095{
Adam Tkacb10489b2010-04-23 14:16:04 +000096 assert (CSecurity::upg != NULL); /* (upg == NULL) means bug in the viewer */
Adam Tkacc210e8a2010-04-23 14:09:16 +000097
98 if (!IsSupported(secType))
99 goto bail;
100
101 switch (secType) {
102 case secTypeNone: return new CSecurityNone();
Adam Tkacb10489b2010-04-23 14:16:04 +0000103 case secTypeVncAuth: return new CSecurityVncAuth();
104 case secTypeVeNCrypt: return new CSecurityVeNCrypt();
Adam Tkac1d15e2d2010-04-23 14:06:38 +0000105 }
106
107bail:
108 throw Exception("Security type not supported");
109}
110
Adam Tkac94d88c12010-04-23 13:59:52 +0000111rdr::U8 rfb::secTypeNum(const char* name)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000112{
113 if (strcasecmp(name, "None") == 0) return secTypeNone;
114 if (strcasecmp(name, "VncAuth") == 0) return secTypeVncAuth;
115 if (strcasecmp(name, "Tight") == 0) return secTypeTight;
116 if (strcasecmp(name, "RA2") == 0) return secTypeRA2;
117 if (strcasecmp(name, "RA2ne") == 0) return secTypeRA2ne;
118 if (strcasecmp(name, "SSPI") == 0) return secTypeSSPI;
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000119 if (strcasecmp(name, "SSPIne") == 0) return secTypeSSPIne;
120 if (strcasecmp(name, "VeNCrypt") == 0) return secTypeVeNCrypt;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000121 return secTypeInvalid;
122}
123
Adam Tkac94d88c12010-04-23 13:59:52 +0000124const char* rfb::secTypeName(rdr::U8 num)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000125{
126 switch (num) {
127 case secTypeNone: return "None";
128 case secTypeVncAuth: return "VncAuth";
129 case secTypeTight: return "Tight";
130 case secTypeRA2: return "RA2";
131 case secTypeRA2ne: return "RA2ne";
132 case secTypeSSPI: return "SSPI";
133 case secTypeSSPIne: return "SSPIne";
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000134 case secTypeVeNCrypt: return "VeNCrypt";
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000135 default: return "[unknown secType]";
136 }
137}
138
Adam Tkac94d88c12010-04-23 13:59:52 +0000139std::list<rdr::U8> rfb::parseSecTypes(const char* types_)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000140{
Adam Tkac94d88c12010-04-23 13:59:52 +0000141 std::list<rdr::U8> result;
Adam Tkacd36b6262009-09-04 10:57:20 +0000142 CharArray types(strDup(types_)), type;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000143 while (types.buf) {
144 strSplit(types.buf, ',', &type.buf, &types.buf);
Adam Tkac94d88c12010-04-23 13:59:52 +0000145 rdr::U8 typeNum = secTypeNum(type.buf);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000146 if (typeNum != secTypeInvalid)
147 result.push_back(typeNum);
148 }
149 return result;
150}