Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 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 | */ |
Adam Tkac | 1d15e2d | 2010-04-23 14:06:38 +0000 | [diff] [blame^] | 18 | #include <assert.h> |
| 19 | #include <stdlib.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 20 | #include <string.h> |
| 21 | #ifdef _WIN32 |
| 22 | #define strcasecmp _stricmp |
| 23 | #endif |
Adam Tkac | 1d15e2d | 2010-04-23 14:06:38 +0000 | [diff] [blame^] | 24 | #include <rdr/Exception.h> |
| 25 | #include <rfb/LogWriter.h> |
Adam Tkac | b6eb399 | 2010-04-23 14:05:00 +0000 | [diff] [blame] | 26 | #include <rfb/Security.h> |
Adam Tkac | 1d15e2d | 2010-04-23 14:06:38 +0000 | [diff] [blame^] | 27 | #include <rfb/SSecurityNone.h> |
| 28 | #include <rfb/SSecurityFactoryStandard.h> |
| 29 | #include <rfb/SSecurityVncAuth.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 30 | #include <rfb/util.h> |
| 31 | |
Adam Tkac | 1d15e2d | 2010-04-23 14:06:38 +0000 | [diff] [blame^] | 32 | using namespace rdr; |
| 33 | using namespace rfb; |
| 34 | using namespace std; |
| 35 | |
| 36 | static LogWriter vlog("Security"); |
| 37 | |
| 38 | Security::Security(void) |
| 39 | { |
| 40 | char *secTypesStr = SSecurityFactoryStandard::sec_types.getData(); |
| 41 | |
| 42 | enabledSecTypes = parseSecTypes(secTypesStr); |
| 43 | |
| 44 | delete secTypesStr; |
| 45 | } |
| 46 | |
| 47 | void Security::EnableSecType(U8 secType) |
| 48 | { |
| 49 | list<U8>::iterator i; |
| 50 | |
| 51 | for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++) |
| 52 | if (*i == secType) |
| 53 | return; |
| 54 | |
| 55 | enabledSecTypes.push_back(secType); |
| 56 | } |
| 57 | |
| 58 | bool Security::IsSupported(U8 secType) |
| 59 | { |
| 60 | list<U8>::iterator i; |
| 61 | |
| 62 | for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++) |
| 63 | if (*i == secType) |
| 64 | return true; |
| 65 | |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | SSecurity* Security::GetSSecurity(U8 secType) |
| 70 | { |
| 71 | if (!IsSupported(secType)) |
| 72 | goto bail; |
| 73 | |
| 74 | switch (secType) { |
| 75 | case secTypeNone: return new SSecurityNone(); |
| 76 | case secTypeVncAuth: return new SSecurityVncAuth(); |
| 77 | default: |
| 78 | vlog.error("Undefined security type %d, aborting"); |
| 79 | abort(); |
| 80 | } |
| 81 | |
| 82 | bail: |
| 83 | throw Exception("Security type not supported"); |
| 84 | } |
| 85 | |
Adam Tkac | 94d88c1 | 2010-04-23 13:59:52 +0000 | [diff] [blame] | 86 | rdr::U8 rfb::secTypeNum(const char* name) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 87 | { |
| 88 | if (strcasecmp(name, "None") == 0) return secTypeNone; |
| 89 | if (strcasecmp(name, "VncAuth") == 0) return secTypeVncAuth; |
| 90 | if (strcasecmp(name, "Tight") == 0) return secTypeTight; |
| 91 | if (strcasecmp(name, "RA2") == 0) return secTypeRA2; |
| 92 | if (strcasecmp(name, "RA2ne") == 0) return secTypeRA2ne; |
| 93 | if (strcasecmp(name, "SSPI") == 0) return secTypeSSPI; |
| 94 | if (strcasecmp(name, "SSPIne") == 0) return secTypeSSPIne; |
| 95 | return secTypeInvalid; |
| 96 | } |
| 97 | |
Adam Tkac | 94d88c1 | 2010-04-23 13:59:52 +0000 | [diff] [blame] | 98 | const char* rfb::secTypeName(rdr::U8 num) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 99 | { |
| 100 | switch (num) { |
| 101 | case secTypeNone: return "None"; |
| 102 | case secTypeVncAuth: return "VncAuth"; |
| 103 | case secTypeTight: return "Tight"; |
| 104 | case secTypeRA2: return "RA2"; |
| 105 | case secTypeRA2ne: return "RA2ne"; |
| 106 | case secTypeSSPI: return "SSPI"; |
| 107 | case secTypeSSPIne: return "SSPIne"; |
| 108 | default: return "[unknown secType]"; |
| 109 | } |
| 110 | } |
| 111 | |
Adam Tkac | 94d88c1 | 2010-04-23 13:59:52 +0000 | [diff] [blame] | 112 | std::list<rdr::U8> rfb::parseSecTypes(const char* types_) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 113 | { |
Adam Tkac | 94d88c1 | 2010-04-23 13:59:52 +0000 | [diff] [blame] | 114 | std::list<rdr::U8> result; |
Adam Tkac | d36b626 | 2009-09-04 10:57:20 +0000 | [diff] [blame] | 115 | CharArray types(strDup(types_)), type; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 116 | while (types.buf) { |
| 117 | strSplit(types.buf, ',', &type.buf, &types.buf); |
Adam Tkac | 94d88c1 | 2010-04-23 13:59:52 +0000 | [diff] [blame] | 118 | rdr::U8 typeNum = secTypeNum(type.buf); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 119 | if (typeNum != secTypeInvalid) |
| 120 | result.push_back(typeNum); |
| 121 | } |
| 122 | return result; |
| 123 | } |