Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame^] | 1 | /* Copyright (C) 2002-2004 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 | // 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> |
| 28 | |
| 29 | using namespace rfb; |
| 30 | |
| 31 | static LogWriter vlog("SSecurityFactoryStandard"); |
| 32 | |
| 33 | VncAuthPasswdParameter* SSecurityFactoryStandard::vncAuthPasswd = 0; |
| 34 | |
| 35 | |
| 36 | SSecurity* SSecurityFactoryStandard::getSSecurity(int secType, bool noAuth) { |
| 37 | switch (secType) { |
| 38 | case secTypeNone: return new SSecurityNone(); |
| 39 | case secTypeVncAuth: |
| 40 | if (!vncAuthPasswd) |
| 41 | throw rdr::Exception("No VncAuthPasswdParameter defined!"); |
| 42 | return new SSecurityVncAuth(vncAuthPasswd); |
| 43 | default: |
| 44 | throw Exception("Unsupported secType?"); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | VncAuthPasswdParameter::VncAuthPasswdParameter() { |
| 49 | if (SSecurityFactoryStandard::vncAuthPasswd) |
| 50 | throw rdr::Exception("duplicate VncAuthPasswdParameter!"); |
| 51 | SSecurityFactoryStandard::vncAuthPasswd = this; |
| 52 | } |
| 53 | |
| 54 | |
| 55 | VncAuthPasswdConfigParameter::VncAuthPasswdConfigParameter() |
| 56 | : passwdParam("Password", |
| 57 | "Obfuscated binary encoding of the password which clients must supply to " |
| 58 | "access the server", 0, 0) { |
| 59 | } |
| 60 | |
| 61 | char* VncAuthPasswdConfigParameter::getVncAuthPasswd() { |
| 62 | CharArray obfuscated; |
| 63 | int len; |
| 64 | passwdParam.getData((void**)&obfuscated.buf, &len); |
| 65 | printf("vnc password len=%d\n", len); // *** |
| 66 | if (len == 8) { |
| 67 | CharArray password(9); |
| 68 | memcpy(password.buf, obfuscated.buf, 8); |
| 69 | vncAuthUnobfuscatePasswd(password.buf); |
| 70 | return password.takeBuf(); |
| 71 | } |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | |
| 76 | VncAuthPasswdFileParameter::VncAuthPasswdFileParameter() |
| 77 | : param("PasswordFile", "Password file for VNC authentication", "") { |
| 78 | } |
| 79 | |
| 80 | char* VncAuthPasswdFileParameter::getVncAuthPasswd() { |
| 81 | CharArray fname(param.getData()); |
| 82 | if (!fname.buf[0]) { |
| 83 | vlog.error("passwordFile parameter not set"); |
| 84 | return 0; |
| 85 | } |
| 86 | FILE* fp = fopen(fname.buf, "r"); |
| 87 | if (!fp) { |
| 88 | vlog.error("opening password file '%s' failed",fname.buf); |
| 89 | return 0; |
| 90 | } |
| 91 | CharArray passwd(9); |
| 92 | int len = fread(passwd.buf, 1, 9, fp); |
| 93 | fclose(fp); |
| 94 | if (len != 8) { |
| 95 | vlog.error("password file '%s' is the wrong length",fname.buf); |
| 96 | return 0; |
| 97 | } |
| 98 | vncAuthUnobfuscatePasswd(passwd.buf); |
| 99 | return passwd.takeBuf(); |
| 100 | } |
| 101 | |