blob: c3d67374d346474ad0b33078d32af05e58d5cc36 [file] [log] [blame]
Constantin Kaplinsky729598c2006-05-25 05:12:25 +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#ifndef WINVNCCONF_CONNECTIONS
19#define WINVNCCONF_CONNECTIONS
20
21#include <vector>
22
23#include <rfb_win32/Registry.h>
24#include <rfb_win32/Dialog.h>
25#include <rfb_win32/ModuleFileName.h>
26#include <rfb/Configuration.h>
27#include <rfb/Blacklist.h>
28#include <network/TcpSocket.h>
29
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000030static rfb::IntParameter port_number("PortNumber",
31 "TCP/IP port on which the server will accept connections", 5900);
32static rfb::StringParameter hosts("Hosts",
33 "Filter describing which hosts are allowed access to this server", "+");
34static rfb::BoolParameter localHost("LocalHost",
35 "Only accept connections from via the local loop-back network interface", false);
36
37namespace rfb {
38
39 namespace win32 {
40
41 class ConnHostDialog : public Dialog {
42 public:
43 ConnHostDialog() : Dialog(GetModuleHandle(0)) {}
44 bool showDialog(const TCHAR* pat) {
45 pattern.replaceBuf(tstrDup(pat));
46 return Dialog::showDialog(MAKEINTRESOURCE(IDD_CONN_HOST));
47 }
48 void initDialog() {
49 if (_tcslen(pattern.buf) == 0)
50 pattern.replaceBuf(tstrDup("+"));
51
52 if (pattern.buf[0] == _T('+'))
53 setItemChecked(IDC_ALLOW, true);
54 else if (pattern.buf[0] == _T('?'))
55 setItemChecked(IDC_QUERY, true);
56 else
57 setItemChecked(IDC_DENY, true);
58
59 setItemString(IDC_HOST_PATTERN, &pattern.buf[1]);
60 pattern.replaceBuf(0);
61 }
62 bool onOk() {
Adam Tkac934f63c2009-10-12 15:54:59 +000063 TCharArray host(getItemString(IDC_HOST_PATTERN));
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000064 TCharArray newPat(_tcslen(host.buf)+2);
65 if (isItemChecked(IDC_ALLOW))
66 newPat.buf[0] = _T('+');
67 else if (isItemChecked(IDC_QUERY))
68 newPat.buf[0] = _T('?');
69 else
70 newPat.buf[0] = _T('-');
71 newPat.buf[1] = 0;
72 _tcscat(newPat.buf, host.buf);
73
Pierre Ossmancd873172015-08-10 14:03:16 +020074 try {
75 network::TcpFilter::Pattern pat(network::TcpFilter::parsePattern(CStr(newPat.buf)));
76 pattern.replaceBuf(TCharArray(network::TcpFilter::patternToStr(pat)).takeBuf());
Pierre Ossman8ee522a2018-05-29 15:50:08 +020077 } catch(rdr::Exception& e) {
Pierre Ossmancd873172015-08-10 14:03:16 +020078 MsgBox(NULL, TStr(e.str()), MB_ICONEXCLAMATION | MB_OK);
79 return false;
80 }
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000081 return true;
82 }
83 const TCHAR* getPattern() {return pattern.buf;}
84 protected:
85 TCharArray pattern;
86 };
87
88 class ConnectionsPage : public PropSheetPage {
89 public:
90 ConnectionsPage(const RegKey& rk)
91 : PropSheetPage(GetModuleHandle(0), MAKEINTRESOURCE(IDD_CONNECTIONS)), regKey(rk) {}
92 void initDialog() {
93 vlog.debug("set IDC_PORT %d", (int)port_number);
94 setItemInt(IDC_PORT, port_number ? port_number : 5900);
95 setItemChecked(IDC_RFB_ENABLE, port_number != 0);
96 setItemInt(IDC_IDLE_TIMEOUT, rfb::Server::idleTimeout);
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000097 setItemChecked(IDC_LOCALHOST, localHost);
98
99 HWND listBox = GetDlgItem(handle, IDC_HOSTS);
100 while (SendMessage(listBox, LB_GETCOUNT, 0, 0))
101 SendMessage(listBox, LB_DELETESTRING, 0, 0);
102
103 CharArray tmp;
104 tmp.buf = hosts.getData();
105 while (tmp.buf) {
106 CharArray first;
107 strSplit(tmp.buf, ',', &first.buf, &tmp.buf);
108 if (strlen(first.buf))
109 SendMessage(listBox, LB_ADDSTRING, 0, (LPARAM)(const TCHAR*)TStr(first.buf));
110 }
111
112 onCommand(IDC_RFB_ENABLE, EN_CHANGE);
113 }
114 bool onCommand(int id, int cmd) {
115 switch (id) {
116 case IDC_HOSTS:
117 {
118 DWORD selected = SendMessage(GetDlgItem(handle, IDC_HOSTS), LB_GETCURSEL, 0, 0);
Pierre Ossman5c23b9e2015-03-03 16:26:03 +0100119 DWORD count = SendMessage(GetDlgItem(handle, IDC_HOSTS), LB_GETCOUNT, 0, 0);
120 bool enable = selected != (DWORD)LB_ERR;
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000121 enableItem(IDC_HOST_REMOVE, enable);
122 enableItem(IDC_HOST_UP, enable && (selected > 0));
Pierre Ossman5c23b9e2015-03-03 16:26:03 +0100123 enableItem(IDC_HOST_DOWN, enable && (selected+1 < count));
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000124 enableItem(IDC_HOST_EDIT, enable);
125 setChanged(isChanged());
126 }
127 return true;
128
129 case IDC_PORT:
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000130 case IDC_IDLE_TIMEOUT:
131 if (cmd == EN_CHANGE)
132 setChanged(isChanged());
133 return false;
134
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000135 case IDC_RFB_ENABLE:
136 case IDC_LOCALHOST:
137 {
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000138 // RFB port
139 enableItem(IDC_PORT, isItemChecked(IDC_RFB_ENABLE));
140
141 // Hosts
142 enableItem(IDC_LOCALHOST, isItemChecked(IDC_RFB_ENABLE));
143
144 bool enableHosts = !isItemChecked(IDC_LOCALHOST) && isItemChecked(IDC_RFB_ENABLE);
145 enableItem(IDC_HOSTS, enableHosts);
146 enableItem(IDC_HOST_ADD, enableHosts);
147 if (!enableHosts) {
148 enableItem(IDC_HOST_REMOVE, enableHosts);
149 enableItem(IDC_HOST_UP, enableHosts);
150 enableItem(IDC_HOST_DOWN, enableHosts);
151 enableItem(IDC_HOST_EDIT, enableHosts);
152 } else {
153 onCommand(IDC_HOSTS, EN_CHANGE);
154 }
155 setChanged(isChanged());
156 return false;
157 }
158
159 case IDC_HOST_ADD:
160 if (hostDialog.showDialog(_T("")))
161 {
162 const TCHAR* pattern = hostDialog.getPattern();
163 if (pattern)
164 SendMessage(GetDlgItem(handle, IDC_HOSTS), LB_ADDSTRING, 0, (LPARAM)pattern);
165 }
166 return true;
167
168 case IDC_HOST_EDIT:
169 {
170 HWND listBox = GetDlgItem(handle, IDC_HOSTS);
171 int item = SendMessage(listBox, LB_GETCURSEL, 0, 0);
172 TCharArray pattern(SendMessage(listBox, LB_GETTEXTLEN, item, 0)+1);
173 SendMessage(listBox, LB_GETTEXT, item, (LPARAM)pattern.buf);
174
175 if (hostDialog.showDialog(pattern.buf)) {
176 const TCHAR* newPat = hostDialog.getPattern();
177 if (newPat) {
178 item = SendMessage(listBox, LB_FINDSTRINGEXACT, item, (LPARAM)pattern.buf);
179 if (item != LB_ERR) {
180 SendMessage(listBox, LB_DELETESTRING, item, 0);
181 SendMessage(listBox, LB_INSERTSTRING, item, (LPARAM)newPat);
182 SendMessage(listBox, LB_SETCURSEL, item, 0);
183 onCommand(IDC_HOSTS, EN_CHANGE);
184 }
185 }
186 }
187 }
188 return true;
189
190 case IDC_HOST_UP:
191 {
192 HWND listBox = GetDlgItem(handle, IDC_HOSTS);
193 int item = SendMessage(listBox, LB_GETCURSEL, 0, 0);
194 TCharArray pattern(SendMessage(listBox, LB_GETTEXTLEN, item, 0)+1);
195 SendMessage(listBox, LB_GETTEXT, item, (LPARAM)pattern.buf);
196 SendMessage(listBox, LB_DELETESTRING, item, 0);
197 SendMessage(listBox, LB_INSERTSTRING, item-1, (LPARAM)pattern.buf);
198 SendMessage(listBox, LB_SETCURSEL, item-1, 0);
199 onCommand(IDC_HOSTS, EN_CHANGE);
200 }
201 return true;
202
203 case IDC_HOST_DOWN:
204 {
205 HWND listBox = GetDlgItem(handle, IDC_HOSTS);
206 int item = SendMessage(listBox, LB_GETCURSEL, 0, 0);
207 TCharArray pattern(SendMessage(listBox, LB_GETTEXTLEN, item, 0)+1);
208 SendMessage(listBox, LB_GETTEXT, item, (LPARAM)pattern.buf);
209 SendMessage(listBox, LB_DELETESTRING, item, 0);
210 SendMessage(listBox, LB_INSERTSTRING, item+1, (LPARAM)pattern.buf);
211 SendMessage(listBox, LB_SETCURSEL, item+1, 0);
212 onCommand(IDC_HOSTS, EN_CHANGE);
213 }
214 return true;
215
216 case IDC_HOST_REMOVE:
217 {
218 HWND listBox = GetDlgItem(handle, IDC_HOSTS);
219 int item = SendMessage(listBox, LB_GETCURSEL, 0, 0);
220 SendMessage(listBox, LB_DELETESTRING, item, 0);
221 onCommand(IDC_HOSTS, EN_CHANGE);
222 }
223
224 }
225 return false;
226 }
227 bool onOk() {
228 regKey.setInt(_T("PortNumber"), isItemChecked(IDC_RFB_ENABLE) ? getItemInt(IDC_PORT) : 0);
229 regKey.setInt(_T("IdleTimeout"), getItemInt(IDC_IDLE_TIMEOUT));
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000230 regKey.setInt(_T("LocalHost"), isItemChecked(IDC_LOCALHOST));
231 regKey.setString(_T("Hosts"), TCharArray(getHosts()).buf);
232 return true;
233 }
234 bool isChanged() {
235 try {
Adam Tkac934f63c2009-10-12 15:54:59 +0000236 CharArray new_hosts(getHosts());
237 CharArray old_hosts(hosts.getData());
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000238 return (strcmp(new_hosts.buf, old_hosts.buf) != 0) ||
239 (localHost != isItemChecked(IDC_LOCALHOST)) ||
240 (port_number != getItemInt(IDC_PORT)) ||
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000241 (rfb::Server::idleTimeout != getItemInt(IDC_IDLE_TIMEOUT));
Pierre Ossman8ee522a2018-05-29 15:50:08 +0200242 } catch (rdr::Exception&) {
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000243 return false;
244 }
245 }
246 char* getHosts() {
247 int bufLen = 1, i;
248 HWND listBox = GetDlgItem(handle, IDC_HOSTS);
249 for (i=0; i<SendMessage(listBox, LB_GETCOUNT, 0, 0); i++)
250 bufLen+=SendMessage(listBox, LB_GETTEXTLEN, i, 0)+1;
251 TCharArray hosts_str(bufLen);
252 hosts_str.buf[0] = 0;
253 TCHAR* outPos = hosts_str.buf;
254 for (i=0; i<SendMessage(listBox, LB_GETCOUNT, 0, 0); i++) {
255 outPos += SendMessage(listBox, LB_GETTEXT, i, (LPARAM)outPos);
256 outPos[0] = ',';
257 outPos[1] = 0;
258 outPos++;
259 }
260 return strDup(hosts_str.buf);
261 }
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000262
263 protected:
264 RegKey regKey;
265 ConnHostDialog hostDialog;
266 };
267
268 };
269
270};
271
272#endif