blob: 05488f677af00bbeecf0c6f9a9c87ddd6794bbb7 [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 */
18//
19// SSecurityVncAuth
20//
21// XXX not thread-safe, because d3des isn't - do we need to worry about this?
22//
23
24#include <rfb/SSecurityVncAuth.h>
25#include <rdr/RandomStream.h>
26#include <rfb/SConnection.h>
27#include <rfb/Password.h>
28#include <rfb/Configuration.h>
29#include <rfb/LogWriter.h>
30#include <rfb/util.h>
31#include <rfb/Exception.h>
32#include <string.h>
33#include <stdio.h>
34extern "C" {
35#include <rfb/d3des.h>
36}
37
38
39using namespace rfb;
40
41static LogWriter vlog("SVncAuth");
42
Adam Tkac162ac352010-04-23 14:02:43 +000043StringParameter SSecurityVncAuth::vncAuthPasswdFile
44("PasswordFile", "Password file for VNC authentication", "", ConfServer);
45AliasParameter rfbauth("rfbauth", "Alias for PasswordFile",
46 &SSecurityVncAuth::vncAuthPasswdFile, ConfServer);
47VncAuthPasswdParameter SSecurityVncAuth::vncAuthPasswd
48("Password", "Obfuscated binary encoding of the password which clients must supply to "
49 "access the server", &SSecurityVncAuth::vncAuthPasswdFile);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000050
Adam Tkac162ac352010-04-23 14:02:43 +000051SSecurityVncAuth::SSecurityVncAuth(void)
Michal Srb270a31c2014-11-10 15:32:00 +020052 : sentChallenge(false), responsePos(0), pg(&vncAuthPasswd), accessRights(0)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000053{
54}
55
Michal Srb270a31c2014-11-10 15:32:00 +020056bool SSecurityVncAuth::verifyResponse(const PlainPasswd &password)
57{
58 rdr::U8 expectedResponse[vncAuthChallengeSize];
59
60 // Calculate the expected response
61 rdr::U8 key[8];
62 int pwdLen = strlen(password.buf);
63 for (int i=0; i<8; i++)
64 key[i] = i<pwdLen ? password.buf[i] : 0;
65 deskey(key, EN0);
66 for (int j = 0; j < vncAuthChallengeSize; j += 8)
67 des(challenge+j, expectedResponse+j);
68
69 // Check the actual response
70 return memcmp(response, expectedResponse, vncAuthChallengeSize) == 0;
71}
72
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000073bool SSecurityVncAuth::processMsg(SConnection* sc)
74{
75 rdr::InStream* is = sc->getInStream();
76 rdr::OutStream* os = sc->getOutStream();
77
78 if (!sentChallenge) {
79 rdr::RandomStream rs;
80 rs.readBytes(challenge, vncAuthChallengeSize);
81 os->writeBytes(challenge, vncAuthChallengeSize);
82 os->flush();
83 sentChallenge = true;
84 return false;
85 }
86
87 while (responsePos < vncAuthChallengeSize && is->checkNoWait(1))
88 response[responsePos++] = is->readU8();
89
90 if (responsePos < vncAuthChallengeSize) return false;
91
Michal Srb270a31c2014-11-10 15:32:00 +020092 PlainPasswd passwd, passwdReadOnly;
93 pg->getVncAuthPasswd(&passwd, &passwdReadOnly);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000094
95 if (!passwd.buf)
96 throw AuthFailureException("No password configured for VNC Auth");
97
Michal Srb270a31c2014-11-10 15:32:00 +020098 if (verifyResponse(passwd)) {
99 accessRights = SConnection::AccessDefault;
100 return true;
101 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000102
Michal Srb270a31c2014-11-10 15:32:00 +0200103 if (passwdReadOnly.buf && verifyResponse(passwdReadOnly)) {
104 accessRights = SConnection::AccessView;
105 return true;
106 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000107
Michal Srb270a31c2014-11-10 15:32:00 +0200108 throw AuthFailureException();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000109}
Adam Tkac162ac352010-04-23 14:02:43 +0000110
111VncAuthPasswdParameter::VncAuthPasswdParameter(const char* name,
112 const char* desc,
113 StringParameter* passwdFile_)
114: BinaryParameter(name, desc, 0, 0, ConfServer), passwdFile(passwdFile_) {
115}
116
Michal Srb270a31c2014-11-10 15:32:00 +0200117void VncAuthPasswdParameter::getVncAuthPasswd(PlainPasswd *password, PlainPasswd *readOnlyPassword) {
118 ObfuscatedPasswd obfuscated, obfuscatedReadOnly;
Adam Tkac162ac352010-04-23 14:02:43 +0000119 getData((void**)&obfuscated.buf, &obfuscated.length);
120
121 if (obfuscated.length == 0) {
122 if (passwdFile) {
123 CharArray fname(passwdFile->getData());
124 if (!fname.buf[0]) {
125 vlog.info("neither %s nor %s params set", getName(), passwdFile->getName());
Michal Srb270a31c2014-11-10 15:32:00 +0200126 return;
Adam Tkac162ac352010-04-23 14:02:43 +0000127 }
128
129 FILE* fp = fopen(fname.buf, "r");
130 if (!fp) {
131 vlog.error("opening password file '%s' failed",fname.buf);
Michal Srb270a31c2014-11-10 15:32:00 +0200132 return;
Adam Tkac162ac352010-04-23 14:02:43 +0000133 }
134
135 vlog.debug("reading password file");
Michal Srb270a31c2014-11-10 15:32:00 +0200136 obfuscated.buf = new char[8];
137 obfuscated.length = fread(obfuscated.buf, 1, 8, fp);
138 obfuscatedReadOnly.buf = new char[8];
139 obfuscatedReadOnly.length = fread(obfuscatedReadOnly.buf, 1, 8, fp);
Adam Tkac162ac352010-04-23 14:02:43 +0000140 fclose(fp);
141 } else {
142 vlog.info("%s parameter not set", getName());
143 }
144 }
145
146 try {
Michal Srb270a31c2014-11-10 15:32:00 +0200147 PlainPasswd plainPassword(obfuscated);
148 password->replaceBuf(plainPassword.takeBuf());
149 PlainPasswd plainPasswordReadOnly(obfuscatedReadOnly);
150 readOnlyPassword->replaceBuf(plainPasswordReadOnly.takeBuf());
Adam Tkac162ac352010-04-23 14:02:43 +0000151 } catch (...) {
Adam Tkac162ac352010-04-23 14:02:43 +0000152 }
153}
154