blob: a0726986aba9fc63e87ece8490ff7f901854ff4f [file] [log] [blame]
Constantin Kaplinskyde179d42006-04-16 06:53:44 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +00003 * 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 Kaplinskyde179d42006-04-16 06:53:44 +000028#include <rfb/Password.h>
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000029
30using namespace rfb;
31
32static LogWriter vlog("SSecurityFactoryStandard");
33
Constantin Kaplinskyde179d42006-04-16 06:53:44 +000034StringParameter SSecurityFactoryStandard::sec_types
35("SecurityTypes",
36 "Specify which security scheme to use for incoming connections (None, VncAuth)",
37 "VncAuth");
38
39StringParameter SSecurityFactoryStandard::rev_sec_types
40("ReverseSecurityTypes",
41 "Specify encryption scheme to use for reverse connections (None)",
42 "None");
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000043
44
Constantin Kaplinskyde179d42006-04-16 06:53:44 +000045StringParameter SSecurityFactoryStandard::vncAuthPasswdFile
46("PasswordFile", "Password file for VNC authentication", "");
47VncAuthPasswdParameter SSecurityFactoryStandard::vncAuthPasswd
48("Password", "Obfuscated binary encoding of the password which clients must supply to "
49 "access the server", &SSecurityFactoryStandard::vncAuthPasswdFile);
50
51
52SSecurity* SSecurityFactoryStandard::getSSecurity(rdr::U8 secType, bool reverseConnection) {
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000053 switch (secType) {
Constantin Kaplinskyde179d42006-04-16 06:53:44 +000054 case secTypeNone: return new SSecurityNone();
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000055 case secTypeVncAuth:
Constantin Kaplinskyde179d42006-04-16 06:53:44 +000056 return new SSecurityVncAuth(&vncAuthPasswd);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000057 default:
Constantin Kaplinskyde179d42006-04-16 06:53:44 +000058 throw Exception("Security type not supported");
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000059 }
60}
61
Constantin Kaplinskyde179d42006-04-16 06:53:44 +000062void 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
76bool SSecurityFactoryStandard::isSecTypeSupported(rdr::U8 secType) {
77 switch (secType) {
78 case secTypeNone:
79 case secTypeVncAuth:
80 return true;
81 default:
82 return false;
83 }
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000084}
85
86
Constantin Kaplinskyde179d42006-04-16 06:53:44 +000087VncAuthPasswdParameter::VncAuthPasswdParameter(const char* name,
88 const char* desc,
89 StringParameter* passwdFile_)
90: BinaryParameter(name, desc, 0, 0), passwdFile(passwdFile_) {
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000091}
92
Constantin Kaplinskyde179d42006-04-16 06:53:44 +000093char* 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 Kaplinsky47ed8d32004-10-08 09:43:57 +0000122 return password.takeBuf();
Constantin Kaplinskyde179d42006-04-16 06:53:44 +0000123 } catch (...) {
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000124 return 0;
125 }
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000126}
127
Constantin Kaplinskyde179d42006-04-16 06:53:44 +0000128