Constantin Kaplinsky | de179d4 | 2006-04-16 06:53:44 +0000 | [diff] [blame^] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
| 2 | * |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 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 | // |
| 19 | // SSecurityFactoryStandard |
| 20 | // |
| 21 | |
| 22 | #include <rfb/secTypes.h> |
| 23 | #include <rfb/SSecurityNone.h> |
| 24 | #include <rfb/Configuration.h> |
| 25 | #include <rfb/LogWriter.h> |
| 26 | #include <rfb/Exception.h> |
| 27 | #include <rfb/SSecurityFactoryStandard.h> |
Constantin Kaplinsky | de179d4 | 2006-04-16 06:53:44 +0000 | [diff] [blame^] | 28 | #include <rfb/Password.h> |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace rfb; |
| 31 | |
| 32 | static LogWriter vlog("SSecurityFactoryStandard"); |
| 33 | |
Constantin Kaplinsky | de179d4 | 2006-04-16 06:53:44 +0000 | [diff] [blame^] | 34 | StringParameter SSecurityFactoryStandard::sec_types |
| 35 | ("SecurityTypes", |
| 36 | "Specify which security scheme to use for incoming connections (None, VncAuth)", |
| 37 | "VncAuth"); |
| 38 | |
| 39 | StringParameter SSecurityFactoryStandard::rev_sec_types |
| 40 | ("ReverseSecurityTypes", |
| 41 | "Specify encryption scheme to use for reverse connections (None)", |
| 42 | "None"); |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 43 | |
| 44 | |
Constantin Kaplinsky | de179d4 | 2006-04-16 06:53:44 +0000 | [diff] [blame^] | 45 | StringParameter SSecurityFactoryStandard::vncAuthPasswdFile |
| 46 | ("PasswordFile", "Password file for VNC authentication", ""); |
| 47 | VncAuthPasswdParameter SSecurityFactoryStandard::vncAuthPasswd |
| 48 | ("Password", "Obfuscated binary encoding of the password which clients must supply to " |
| 49 | "access the server", &SSecurityFactoryStandard::vncAuthPasswdFile); |
| 50 | |
| 51 | |
| 52 | SSecurity* SSecurityFactoryStandard::getSSecurity(rdr::U8 secType, bool reverseConnection) { |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 53 | switch (secType) { |
Constantin Kaplinsky | de179d4 | 2006-04-16 06:53:44 +0000 | [diff] [blame^] | 54 | case secTypeNone: return new SSecurityNone(); |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 55 | case secTypeVncAuth: |
Constantin Kaplinsky | de179d4 | 2006-04-16 06:53:44 +0000 | [diff] [blame^] | 56 | return new SSecurityVncAuth(&vncAuthPasswd); |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 57 | default: |
Constantin Kaplinsky | de179d4 | 2006-04-16 06:53:44 +0000 | [diff] [blame^] | 58 | throw Exception("Security type not supported"); |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
Constantin Kaplinsky | de179d4 | 2006-04-16 06:53:44 +0000 | [diff] [blame^] | 62 | void SSecurityFactoryStandard::getSecTypes(std::list<rdr::U8>* secTypes, bool reverseConnection) { |
| 63 | CharArray secTypesStr; |
| 64 | if (reverseConnection) |
| 65 | secTypesStr.buf = rev_sec_types.getData(); |
| 66 | else |
| 67 | secTypesStr.buf = sec_types.getData(); |
| 68 | std::list<int> configured = parseSecTypes(secTypesStr.buf); |
| 69 | std::list<int>::iterator i; |
| 70 | for (i=configured.begin(); i!=configured.end(); i++) { |
| 71 | if (isSecTypeSupported(*i)) |
| 72 | secTypes->push_back(*i); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | bool SSecurityFactoryStandard::isSecTypeSupported(rdr::U8 secType) { |
| 77 | switch (secType) { |
| 78 | case secTypeNone: |
| 79 | case secTypeVncAuth: |
| 80 | return true; |
| 81 | default: |
| 82 | return false; |
| 83 | } |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | |
Constantin Kaplinsky | de179d4 | 2006-04-16 06:53:44 +0000 | [diff] [blame^] | 87 | VncAuthPasswdParameter::VncAuthPasswdParameter(const char* name, |
| 88 | const char* desc, |
| 89 | StringParameter* passwdFile_) |
| 90 | : BinaryParameter(name, desc, 0, 0), passwdFile(passwdFile_) { |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Constantin Kaplinsky | de179d4 | 2006-04-16 06:53:44 +0000 | [diff] [blame^] | 93 | char* VncAuthPasswdParameter::getVncAuthPasswd() { |
| 94 | ObfuscatedPasswd obfuscated; |
| 95 | getData((void**)&obfuscated.buf, &obfuscated.length); |
| 96 | |
| 97 | if (obfuscated.length == 0) { |
| 98 | if (passwdFile) { |
| 99 | CharArray fname(passwdFile->getData()); |
| 100 | if (!fname.buf[0]) { |
| 101 | vlog.info("neither %s nor %s params set", getName(), passwdFile->getName()); |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | FILE* fp = fopen(fname.buf, "r"); |
| 106 | if (!fp) { |
| 107 | vlog.error("opening password file '%s' failed",fname.buf); |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | vlog.debug("reading password file"); |
| 112 | obfuscated.buf = new char[128]; |
| 113 | obfuscated.length = fread(obfuscated.buf, 1, 128, fp); |
| 114 | fclose(fp); |
| 115 | } else { |
| 116 | vlog.info("%s parameter not set", getName()); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | try { |
| 121 | PlainPasswd password(obfuscated); |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 122 | return password.takeBuf(); |
Constantin Kaplinsky | de179d4 | 2006-04-16 06:53:44 +0000 | [diff] [blame^] | 123 | } catch (...) { |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 124 | return 0; |
| 125 | } |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Constantin Kaplinsky | de179d4 | 2006-04-16 06:53:44 +0000 | [diff] [blame^] | 128 | |