1 // SoftEther VPN Source Code - Stable Edition Repository
2 // Cedar Communication Module
3 //
4 // SoftEther VPN Server, Client and Bridge are free software under the Apache License, Version 2.0.
5 //
6 // Copyright (c) Daiyuu Nobori.
7 // Copyright (c) SoftEther VPN Project, University of Tsukuba, Japan.
8 // Copyright (c) SoftEther Corporation.
9 // Copyright (c) all contributors on SoftEther VPN project in GitHub.
10 //
11 // All Rights Reserved.
12 //
13 // http://www.softether.org/
14 //
15 // This stable branch is officially managed by Daiyuu Nobori, the owner of SoftEther VPN Project.
16 // Pull requests should be sent to the Developer Edition Master Repository on https://github.com/SoftEtherVPN/SoftEtherVPN
17 //
18 // License: The Apache License, Version 2.0
19 // https://www.apache.org/licenses/LICENSE-2.0
20 //
21 // DISCLAIMER
22 // ==========
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 // SOFTWARE.
31 //
32 // THIS SOFTWARE IS DEVELOPED IN JAPAN, AND DISTRIBUTED FROM JAPAN, UNDER
33 // JAPANESE LAWS. YOU MUST AGREE IN ADVANCE TO USE, COPY, MODIFY, MERGE, PUBLISH,
34 // DISTRIBUTE, SUBLICENSE, AND/OR SELL COPIES OF THIS SOFTWARE, THAT ANY
35 // JURIDICAL DISPUTES WHICH ARE CONCERNED TO THIS SOFTWARE OR ITS CONTENTS,
36 // AGAINST US (SOFTETHER PROJECT, SOFTETHER CORPORATION, DAIYUU NOBORI OR OTHER
37 // SUPPLIERS), OR ANY JURIDICAL DISPUTES AGAINST US WHICH ARE CAUSED BY ANY KIND
38 // OF USING, COPYING, MODIFYING, MERGING, PUBLISHING, DISTRIBUTING, SUBLICENSING,
39 // AND/OR SELLING COPIES OF THIS SOFTWARE SHALL BE REGARDED AS BE CONSTRUED AND
40 // CONTROLLED BY JAPANESE LAWS, AND YOU MUST FURTHER CONSENT TO EXCLUSIVE
41 // JURISDICTION AND VENUE IN THE COURTS SITTING IN TOKYO, JAPAN. YOU MUST WAIVE
42 // ALL DEFENSES OF LACK OF PERSONAL JURISDICTION AND FORUM NON CONVENIENS.
43 // PROCESS MAY BE SERVED ON EITHER PARTY IN THE MANNER AUTHORIZED BY APPLICABLE
44 // LAW OR COURT RULE.
45 //
46 // USE ONLY IN JAPAN. DO NOT USE THIS SOFTWARE IN ANOTHER COUNTRY UNLESS YOU HAVE
47 // A CONFIRMATION THAT THIS SOFTWARE DOES NOT VIOLATE ANY CRIMINAL LAWS OR CIVIL
48 // RIGHTS IN THAT PARTICULAR COUNTRY. USING THIS SOFTWARE IN OTHER COUNTRIES IS
49 // COMPLETELY AT YOUR OWN RISK. THE SOFTETHER VPN PROJECT HAS DEVELOPED AND
50 // DISTRIBUTED THIS SOFTWARE TO COMPLY ONLY WITH THE JAPANESE LAWS AND EXISTING
51 // CIVIL RIGHTS INCLUDING PATENTS WHICH ARE SUBJECTS APPLY IN JAPAN. OTHER
52 // COUNTRIES' LAWS OR CIVIL RIGHTS ARE NONE OF OUR CONCERNS NOR RESPONSIBILITIES.
53 // WE HAVE NEVER INVESTIGATED ANY CRIMINAL REGULATIONS, CIVIL LAWS OR
54 // INTELLECTUAL PROPERTY RIGHTS INCLUDING PATENTS IN ANY OF OTHER 200+ COUNTRIES
55 // AND TERRITORIES. BY NATURE, THERE ARE 200+ REGIONS IN THE WORLD, WITH
56 // DIFFERENT LAWS. IT IS IMPOSSIBLE TO VERIFY EVERY COUNTRIES' LAWS, REGULATIONS
57 // AND CIVIL RIGHTS TO MAKE THE SOFTWARE COMPLY WITH ALL COUNTRIES' LAWS BY THE
58 // PROJECT. EVEN IF YOU WILL BE SUED BY A PRIVATE ENTITY OR BE DAMAGED BY A
59 // PUBLIC SERVANT IN YOUR COUNTRY, THE DEVELOPERS OF THIS SOFTWARE WILL NEVER BE
60 // LIABLE TO RECOVER OR COMPENSATE SUCH DAMAGES, CRIMINAL OR CIVIL
61 // RESPONSIBILITIES. NOTE THAT THIS LINE IS NOT LICENSE RESTRICTION BUT JUST A
62 // STATEMENT FOR WARNING AND DISCLAIMER.
63 //
64 // READ AND UNDERSTAND THE 'WARNING.TXT' FILE BEFORE USING THIS SOFTWARE.
65 // SOME SOFTWARE PROGRAMS FROM THIRD PARTIES ARE INCLUDED ON THIS SOFTWARE WITH
66 // LICENSE CONDITIONS WHICH ARE DESCRIBED ON THE 'THIRD_PARTY.TXT' FILE.
67 //
68 //
69 // SOURCE CODE CONTRIBUTION
70 // ------------------------
71 //
72 // Your contribution to SoftEther VPN Project is much appreciated.
73 // Please send patches to us through GitHub.
74 // Read the SoftEther VPN Patch Acceptance Policy in advance:
75 // http://www.softether.org/5-download/src/9.patch
76 //
77 //
78 // DEAR SECURITY EXPERTS
79 // ---------------------
80 //
81 // If you find a bug or a security vulnerability please kindly inform us
82 // about the problem immediately so that we can fix the security problem
83 // to protect a lot of users around the world as soon as possible.
84 //
85 // Our e-mail address for security reports is:
86 // softether-vpn-security [at] softether.org
87 //
88 // Please note that the above e-mail address is not a technical support
89 // inquiry address. If you need technical assistance, please visit
90 // http://www.softether.org/ and ask your question on the users forum.
91 //
92 // Thank you for your cooperation.
93 //
94 //
95 // NO MEMORY OR RESOURCE LEAKS
96 // ---------------------------
97 //
98 // The memory-leaks and resource-leaks verification under the stress
99 // test has been passed before release this source code.
100 
101 
102 // SM.c
103 // VPN Server Manager for Win32
104 
105 #include <GlobalConst.h>
106 
107 #ifdef	WIN32
108 
109 #define	SM_C
110 #define	CM_C
111 #define	NM_C
112 
113 #define	_WIN32_WINNT		0x0502
114 #define	WINVER				0x0502
115 #include <winsock2.h>
116 #include <windows.h>
117 #include <wincrypt.h>
118 #include <wininet.h>
119 #include <shlobj.h>
120 #include <commctrl.h>
121 #include <Dbghelp.h>
122 #include <stdio.h>
123 #include <stdlib.h>
124 #include <string.h>
125 #include <wchar.h>
126 #include <stdarg.h>
127 #include <time.h>
128 #include <errno.h>
129 #include <Mayaqua/Mayaqua.h>
130 #include <Cedar/Cedar.h>
131 #include "CMInner.h"
132 #include "SMInner.h"
133 #include "NMInner.h"
134 #include "EMInner.h"
135 #include "../PenCore/resource.h"
136 
137 // Global variable
138 static SM *sm = NULL;
139 static bool link_create_now = false;
140 
141 
142 // Proxy Settings dialog initialization
SmProxyDlgInit(HWND hWnd,INTERNET_SETTING * t)143 void SmProxyDlgInit(HWND hWnd, INTERNET_SETTING *t)
144 {
145 	// Validate arguments
146 	if (hWnd == NULL || t == NULL)
147 	{
148 		return;
149 	}
150 
151 	Check(hWnd, R_DIRECT_TCP, t->ProxyType == PROXY_DIRECT);
152 	Check(hWnd, R_HTTPS, t->ProxyType == PROXY_HTTP);
153 	Check(hWnd, R_SOCKS, t->ProxyType == PROXY_SOCKS);
154 
155 	SmProxyDlgUpdate(hWnd, t);
156 }
157 
158 // Proxy Settings dialog update
SmProxyDlgUpdate(HWND hWnd,INTERNET_SETTING * t)159 void SmProxyDlgUpdate(HWND hWnd, INTERNET_SETTING *t)
160 {
161 	bool ok = false;
162 	// Validate arguments
163 	if (hWnd == NULL || t == NULL)
164 	{
165 		return;
166 	}
167 
168 	if (t->ProxyType == PROXY_DIRECT)
169 	{
170 		ok = true;
171 	}
172 	else
173 	{
174 		if (IsEmptyStr(t->ProxyHostName) == false &&
175 			t->ProxyPort != 0)
176 		{
177 			ok = true;
178 		}
179 	}
180 
181 	SetEnable(hWnd, IDOK, ok);
182 
183 	SetEnable(hWnd, B_PROXY_CONFIG, !IsChecked(hWnd, R_DIRECT_TCP));
184 }
185 
186 // Proxy settings generic dialog procedure
SmProxyDlg(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)187 UINT SmProxyDlg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
188 {
189 	INTERNET_SETTING *t = (INTERNET_SETTING *)param;
190 	CLIENT_OPTION a;
191 
192 	switch (msg)
193 	{
194 	case WM_INITDIALOG:
195 		SmProxyDlgInit(hWnd, t);
196 		break;
197 
198 	case WM_COMMAND:
199 		switch (wParam)
200 		{
201 		case IDOK:
202 			EndDialog(hWnd, 1);
203 			break;
204 
205 		case IDCANCEL:
206 			Close(hWnd);
207 			break;
208 
209 		case R_DIRECT_TCP:
210 		case R_HTTPS:
211 		case R_SOCKS:
212 			if (IsChecked(hWnd, R_HTTPS))
213 			{
214 				t->ProxyType = PROXY_HTTP;
215 			}
216 			else if (IsChecked(hWnd, R_SOCKS))
217 			{
218 				t->ProxyType = PROXY_SOCKS;
219 			}
220 			else
221 			{
222 				t->ProxyType = PROXY_DIRECT;
223 			}
224 
225 			SmProxyDlgUpdate(hWnd, t);
226 			break;
227 
228 		case B_PROXY_CONFIG:
229 			Zero(&a, sizeof(a));
230 
231 			a.ProxyType = t->ProxyType;
232 			StrCpy(a.ProxyName, sizeof(a.ProxyName), t->ProxyHostName);
233 			a.ProxyPort = t->ProxyPort;
234 			StrCpy(a.ProxyUsername, sizeof(a.ProxyUsername), t->ProxyUsername);
235 			StrCpy(a.ProxyPassword, sizeof(a.ProxyPassword), t->ProxyPassword);
236 
237 			if (CmProxyDlg(hWnd, &a))
238 			{
239 				t->ProxyType = a.ProxyType;
240 				StrCpy(t->ProxyHostName, sizeof(t->ProxyHostName), a.ProxyName);
241 				t->ProxyPort = a.ProxyPort;
242 				StrCpy(t->ProxyUsername, sizeof(t->ProxyUsername), a.ProxyUsername);
243 				StrCpy(t->ProxyPassword, sizeof(t->ProxyPassword), a.ProxyPassword);
244 			}
245 
246 			SmProxyDlgUpdate(hWnd, t);
247 
248 			break;
249 		}
250 		break;
251 
252 	case WM_CLOSE:
253 		EndDialog(hWnd, 0);
254 		break;
255 	}
256 
257 	return 0;
258 }
259 
260 // Proxy Settings generic dialog
SmProxy(HWND hWnd,INTERNET_SETTING * t)261 bool SmProxy(HWND hWnd, INTERNET_SETTING *t)
262 {
263 	// Validate arguments
264 	if (t == NULL)
265 	{
266 		return false;
267 	}
268 
269 	return Dialog(hWnd, D_SM_PROXY, SmProxyDlg, t);
270 }
271 
272 // VPN Azure dialog procedure
SmAzureDlg(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)273 UINT SmAzureDlg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
274 {
275 	SM_AZURE *a = (SM_AZURE *)param;
276 
277 	switch (msg)
278 	{
279 	case WM_INITDIALOG:
280 		SmAzureDlgOnInit(hWnd, a);
281 
282 		SetTimer(hWnd, 1, 1000, NULL);
283 		break;
284 
285 	case WM_COMMAND:
286 		switch (wParam)
287 		{
288 		case B_CHANGE:
289 			if (SmDDns(hWnd, a->s, false, true))
290 			{
291 				SmAzureDlgRefresh(hWnd, a);
292 			}
293 			break;
294 
295 		case B_WEB:
296 			MsExecute(_SS("SE_VPNAZURE_URL"), NULL);
297 			break;
298 
299 		case R_ENABLE:
300 		case R_DISABLE:
301 			if (IsChecked(hWnd, R_ENABLE) || IsChecked(hWnd, R_DISABLE))
302 			{
303 				Enable(hWnd, IDCANCEL);
304 				EnableClose(hWnd);
305 			}
306 
307 			SmAzureSetStatus(hWnd, a);
308 			break;
309 
310 		case IDOK:
311 		case IDCANCEL:
312 			Close(hWnd);
313 			break;
314 		}
315 		break;
316 
317 	case WM_TIMER:
318 		switch (wParam)
319 		{
320 		case 1:
321 			if (IsEnable(hWnd, 0))
322 			{
323 				KillTimer(hWnd, 1);
324 
325 				SmAzureDlgRefresh(hWnd, a);
326 
327 				SetTimer(hWnd, 1, 1000, NULL);
328 			}
329 			break;
330 		}
331 		break;
332 
333 	case WM_CLOSE:
334 		EndDialog(hWnd, 0);
335 		break;
336 	}
337 
338 	return 0;
339 }
340 
341 // Set the status
SmAzureSetStatus(HWND hWnd,SM_AZURE * a)342 void SmAzureSetStatus(HWND hWnd, SM_AZURE *a)
343 {
344 	RPC_AZURE_STATUS st;
345 	// Validate arguments
346 	if (hWnd == NULL || a == NULL)
347 	{
348 		return;
349 	}
350 
351 	Zero(&st, sizeof(st));
352 
353 	st.IsEnabled = IsChecked(hWnd, R_ENABLE);
354 
355 	if (CALL(hWnd, ScSetAzureStatus(a->s->Rpc, &st)) == false)
356 	{
357 		EndDialog(hWnd, 0);
358 		return;
359 	}
360 
361 	SmAzureDlgRefresh(hWnd, a);
362 }
363 
364 // Initialize the dialog
SmAzureDlgOnInit(HWND hWnd,SM_AZURE * a)365 void SmAzureDlgOnInit(HWND hWnd, SM_AZURE *a)
366 {
367 	RPC_AZURE_STATUS st;
368 	UINT current_lang_id;
369 	// Validate arguments
370 	if (hWnd == NULL || a == NULL)
371 	{
372 		return;
373 	}
374 
375 	SetIcon(hWnd, 0, ICO_AZURE);
376 
377 	DlgFont(hWnd, S_TITLE, 14, true);
378 	DlgFont(hWnd, R_ENABLE, 0, true);
379 
380 	SetFont(hWnd, E_HOST, GetFont("Verdana", 10, false, false, false, false));
381 
382 	current_lang_id = GetCurrentLangId();
383 
384 	// Japanese
385 	SetShow(hWnd, S_BMP_JA, current_lang_id == SE_LANG_JAPANESE);
386 
387 	// Chinese
388 	SetShow(hWnd, S_BMP_CN, current_lang_id == SE_LANG_CHINESE_ZH);
389 
390 	// Other languages
391 	SetShow(hWnd, S_BMP_EN, (current_lang_id != SE_LANG_JAPANESE) && (current_lang_id != SE_LANG_CHINESE_ZH));
392 
393 	// Apply the current settings
394 	Zero(&st, sizeof(st));
395 
396 	if (CALL(hWnd, ScGetAzureStatus(a->s->Rpc, &st)) == false)
397 	{
398 		EndDialog(hWnd, 0);
399 		return;
400 	}
401 
402 	if (a->OnSetup == false || st.IsEnabled)
403 	{
404 		Check(hWnd, R_ENABLE, st.IsEnabled);
405 		Check(hWnd, R_DISABLE, !st.IsEnabled);
406 	}
407 	else
408 	{
409 		Disable(hWnd, IDCANCEL);
410 		DisableClose(hWnd);
411 	}
412 
413 	SmAzureDlgRefresh(hWnd, a);
414 }
415 
416 // Update the dialog
SmAzureDlgRefresh(HWND hWnd,SM_AZURE * a)417 void SmAzureDlgRefresh(HWND hWnd, SM_AZURE *a)
418 {
419 	RPC_AZURE_STATUS st;
420 	DDNS_CLIENT_STATUS ddns;
421 	char tmp[MAX_SIZE];
422 	// Validate arguments
423 	if (hWnd == NULL || a == NULL)
424 	{
425 		return;
426 	}
427 
428 	Zero(&st, sizeof(st));
429 	Zero(&ddns, sizeof(ddns));
430 
431 	if (CALL(hWnd, ScGetAzureStatus(a->s->Rpc, &st)) == false)
432 	{
433 		EndDialog(hWnd, 0);
434 		return;
435 	}
436 
437 	if (CALL(hWnd, ScGetDDnsClientStatus(a->s->Rpc, &ddns)) == false)
438 	{
439 		EndDialog(hWnd, 0);
440 		return;
441 	}
442 
443 	if (st.IsEnabled == false)
444 	{
445 		SetText(hWnd, S_STATUS, _UU("SM_AZURE_STATUS_NOT_CONNECTED"));
446 		Disable(hWnd, S_STATUS);
447 	}
448 	else
449 	{
450 		SetText(hWnd, S_STATUS, (st.IsConnected ? _UU("SM_AZURE_STATUS_CONNECTED") : _UU("SM_AZURE_STATUS_NOT_CONNECTED")));
451 		Enable(hWnd, S_STATUS);
452 	}
453 
454 	SetShow(hWnd, S_HOSTNAME_BORDER, st.IsEnabled);
455 	SetShow(hWnd, S_HOSTNAME_INFO, st.IsEnabled);
456 	SetShow(hWnd, B_CHANGE, st.IsEnabled);
457 
458 	if (st.IsEnabled == false || IsEmptyStr(ddns.CurrentHostName))
459 	{
460 		Hide(hWnd, E_HOST);
461 	}
462 	else
463 	{
464 		StrCpy(tmp, sizeof(tmp), ddns.CurrentHostName);
465 		StrCat(tmp, sizeof(tmp), AZURE_DOMAIN_SUFFIX);
466 
467 		SetTextA(hWnd, E_HOST, tmp);
468 
469 		Show(hWnd, E_HOST);
470 	}
471 }
472 
473 // VPN Azure Setup screen
SmAzure(HWND hWnd,SM_SERVER * s,bool on_setup)474 void SmAzure(HWND hWnd, SM_SERVER *s, bool on_setup)
475 {
476 	SM_AZURE a;
477 	// Validate arguments
478 	if (s == NULL)
479 	{
480 		return;
481 	}
482 
483 	Zero(&a, sizeof(a));
484 
485 	a.s = s;
486 	a.OnSetup = on_setup;
487 
488 	Dialog(hWnd, D_SM_AZURE, SmAzureDlg, &a);
489 }
490 
491 // Notification screen about the bridge in VM
SmVmBridgeDlg(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)492 UINT SmVmBridgeDlg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
493 {
494 	switch (msg)
495 	{
496 	case WM_INITDIALOG:
497 		DlgFont(hWnd, S_TITLE, 14, true);
498 		SetIcon(hWnd, 0, ICO_NIC_ONLINE);
499 		break;
500 
501 	case WM_COMMAND:
502 		switch (wParam)
503 		{
504 		case IDOK:
505 		case IDCANCEL:
506 			Close(hWnd);
507 			break;
508 		}
509 		break;
510 
511 	case WM_CLOSE:
512 		EndDialog(hWnd, 0);
513 		break;
514 	}
515 
516 	return 0;
517 }
518 
519 // Setting screen of VPN over ICMP, etc.
SmSpecialListener(HWND hWnd,SM_SERVER * s)520 void SmSpecialListener(HWND hWnd, SM_SERVER *s)
521 {
522 	// Validate arguments
523 	if (s == NULL)
524 	{
525 		return;
526 	}
527 
528 	Dialog(hWnd, D_SM_SPECIALLISTENER, SmSpecialListenerDlg, s);
529 }
SmSpecialListenerDlg(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)530 UINT SmSpecialListenerDlg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
531 {
532 	SM_SERVER *s = (SM_SERVER *)param;
533 
534 	switch (msg)
535 	{
536 	case WM_INITDIALOG:
537 		SmSpecialListenerDlgInit(hWnd, s);
538 		break;
539 
540 	case WM_COMMAND:
541 		switch (wParam)
542 		{
543 		case IDOK:
544 			SmSpecialListenerDlgOnOk(hWnd, s);
545 			break;
546 
547 		case IDCANCEL:
548 			Close(hWnd);
549 			break;
550 		}
551 		break;
552 
553 	case WM_CLOSE:
554 		EndDialog(hWnd, 0);
555 		break;
556 	}
557 
558 	return 0;
559 }
SmSpecialListenerDlgInit(HWND hWnd,SM_SERVER * s)560 void SmSpecialListenerDlgInit(HWND hWnd, SM_SERVER *s)
561 {
562 	RPC_SPECIAL_LISTENER t;
563 	// Validate arguments
564 	if (hWnd == NULL || s == NULL)
565 	{
566 		return;
567 	}
568 
569 	SetIcon(hWnd, 0, ICO_SPECIALLISTENER);
570 
571 	DlgFont(hWnd, S_TITLE, 14, true);
572 	DlgFont(hWnd, S_1, 0, true);
573 	DlgFont(hWnd, R_OVER_ICMP, 0, true);
574 	DlgFont(hWnd, R_OVER_DNS, 0, true);
575 
576 	Zero(&t, sizeof(t));
577 
578 	if (CALL(hWnd, ScGetSpecialListener(s->Rpc, &t)) == false)
579 	{
580 		Close(hWnd);
581 		return;
582 	}
583 
584 	Check(hWnd, R_OVER_ICMP, t.VpnOverIcmpListener);
585 	Check(hWnd, R_OVER_DNS, t.VpnOverDnsListener);
586 }
SmSpecialListenerDlgOnOk(HWND hWnd,SM_SERVER * s)587 void SmSpecialListenerDlgOnOk(HWND hWnd, SM_SERVER *s)
588 {
589 	RPC_SPECIAL_LISTENER t;
590 	// Validate arguments
591 	if (hWnd == NULL || s == NULL)
592 	{
593 		return;
594 	}
595 
596 	Zero(&t, sizeof(t));
597 
598 	t.VpnOverIcmpListener = IsChecked(hWnd, R_OVER_ICMP);
599 	t.VpnOverDnsListener = IsChecked(hWnd, R_OVER_DNS);
600 
601 	if (CALL(hWnd, ScSetSpecialListener(s->Rpc, &t)) == false)
602 	{
603 		return;
604 	}
605 
606 	EndDialog(hWnd, 1);
607 }
608 
609 
610 // DDNS dialog
SmDDns(HWND hWnd,SM_SERVER * s,bool silent,bool no_change_cert)611 bool SmDDns(HWND hWnd, SM_SERVER *s, bool silent, bool no_change_cert)
612 {
613 	SM_DDNS d;
614 	// Validate arguments
615 	if (s == NULL)
616 	{
617 		return false;
618 	}
619 
620 	Zero(&d, sizeof(d));
621 	d.s = s;
622 	d.Silent = silent;
623 	d.NoChangeCert = no_change_cert;
624 
625 	Dialog(hWnd, D_SM_DDNS, SmDDnsDlg, &d);
626 
627 	return d.Changed;
628 }
SmDDnsDlg(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)629 UINT SmDDnsDlg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
630 {
631 	SM_DDNS *d = (SM_DDNS *)param;
632 	// Validate arguments
633 	if (hWnd == NULL)
634 	{
635 		return 0;
636 	}
637 
638 	switch (msg)
639 	{
640 	case WM_INITDIALOG:
641 		SmDDnsDlgInit(hWnd, d);
642 
643 		SetTimer(hWnd, 1, 1000, NULL);
644 		break;
645 
646 	case WM_COMMAND:
647 		switch (LOWORD(wParam))
648 		{
649 		case E_NEWHOST:
650 			SmDDnsDlgUpdate(hWnd, d);
651 			break;
652 		}
653 
654 		switch (wParam)
655 		{
656 		case IDOK:
657 			SmDDnsDlgOnOk(hWnd, d);
658 			break;
659 
660 		case B_RESTORE:
661 			// Restore to original
662 			if (d->Status.Err_IPv4 == ERR_NO_ERROR || d->Status.Err_IPv6 == ERR_NO_ERROR)
663 			{
664 				SetTextA(hWnd, E_NEWHOST, d->Status.CurrentHostName);
665 				SmDDnsDlgUpdate(hWnd, d);
666 				FocusEx(hWnd, E_NEWHOST);
667 			}
668 			break;
669 
670 		case IDCANCEL:
671 			Close(hWnd);
672 			break;
673 
674 		case B_DISABLE:
675 			d->DoNotPoll = true;
676 
677 			OnceMsg(hWnd, _UU("SM_DISABLE_DDNS_HINT_CAPTION"), _UU("SM_DISABLE_DDNS_HINT"), false, ICO_INFORMATION);
678 
679 			d->DoNotPoll = false;
680 			break;
681 
682 		case B_HINT:
683 			// Hint
684 			if (d->Status.Err_IPv4 == ERR_NO_ERROR || d->Status.Err_IPv6 == ERR_NO_ERROR)
685 			{
686 				wchar_t tmp[MAX_SIZE * 4];
687 				wchar_t ipv4[MAX_SIZE], ipv6[MAX_SIZE];
688 
689 				StrToUni(ipv4, sizeof(ipv4), d->Status.CurrentIPv4);
690 				StrToUni(ipv6, sizeof(ipv6), d->Status.CurrentIPv6);
691 
692 				if (UniIsEmptyStr(ipv4))
693 				{
694 					UniStrCpy(ipv4, sizeof(ipv4), _UU("SM_DDNS_FQDN_EMPTY"));
695 				}
696 
697 				if (UniIsEmptyStr(ipv6))
698 				{
699 					UniStrCpy(ipv6, sizeof(ipv6), _UU("SM_DDNS_FQDN_EMPTY"));
700 				}
701 
702 				UniFormat(tmp, sizeof(tmp),
703 					_UU("SM_DDNS_OK_MSG"),
704 					d->Status.CurrentHostName, d->Status.DnsSuffix,
705 					ipv4, ipv6,
706 					d->Status.CurrentHostName, d->Status.DnsSuffix,
707 					d->Status.CurrentHostName, d->Status.DnsSuffix);
708 
709 				d->DoNotPoll = true;
710 
711 				OnceMsg(hWnd, _UU("SM_DDNS_OK_TITLE"), tmp, false, ICO_DISPLAY);
712 
713 				d->DoNotPoll = false;
714 			}
715 			break;
716 
717 		case B_HINT2:
718 			// Hint2 (for DDNS key)
719 			{
720 				wchar_t tmp[MAX_SIZE * 4];
721 				wchar_t *keystr;
722 
723 				keystr = GetText(hWnd, E_KEY);
724 				UniFormat(tmp, sizeof(tmp), _UU("SM_DDNS_KEY_MSG"), keystr);
725 				Free(keystr);
726 				OnceMsg(hWnd, _UU("SM_DDNS_KEY_TITLE"), tmp, false, ICO_DISPLAY);
727 			}
728 			break;
729 
730 		case B_PROXY:
731 			// Proxy settings
732 			if (true)
733 			{
734 				INTERNET_SETTING t;
735 
736 				if (CALL(hWnd, ScGetDDnsInternetSetting(d->s->Rpc, &t)))
737 				{
738 					if (SmProxy(hWnd, &t))
739 					{
740 						if (CALL(hWnd, ScSetDDnsInternetSetting(d->s->Rpc, &t)))
741 						{
742 							SmDDnsRefresh(hWnd, d);
743 						}
744 					}
745 				}
746 			}
747 			break;
748 		}
749 		break;
750 
751 	case WM_TIMER:
752 		switch (wParam)
753 		{
754 		case 1:
755 			if (IsEnable(hWnd, 0))
756 			{
757 				KillTimer(hWnd, 1);
758 
759 				SmDDnsRefresh(hWnd, d);
760 
761 				SetTimer(hWnd, 1, 1000, NULL);
762 			}
763 			break;
764 		}
765 		break;
766 
767 	case WM_CLOSE:
768 		if (d->Changed || d->Silent)
769 		{
770 			// Check the server certificate if the host name has been changed
771 			RPC_KEY_PAIR t;
772 			char fqdn[MAX_SIZE];
773 			bool is_vgs_enabled = false;
774 
775 
776 			StrCpy(fqdn, sizeof(fqdn), d->Status.CurrentFqdn);
777 
778 			if (IsEmptyStr(fqdn) == false)
779 			{
780 				Zero(&t, sizeof(t));
781 				if (ScGetServerCert(d->s->Rpc, &t) == ERR_NO_ERROR)
782 				{
783 					if (t.Cert != NULL && t.Cert->root_cert && t.Cert->subject_name != NULL && is_vgs_enabled == false)
784 					{
785 						char cn[MAX_SIZE];
786 
787 						UniToStr(cn, sizeof(cn), t.Cert->subject_name->CommonName);
788 
789 						if ((StrCmpi(cn, fqdn) != 0) && (d->NoChangeCert == false))
790 						{
791 							// Confirm whether the user want to replace the server certificate
792 							if (d->Silent || (MsgBoxEx(hWnd, MB_ICONQUESTION | MB_YESNO,
793 								_UU("SM_DDNS_SERVER_CERT_MSG"),
794 								fqdn, fqdn) == IDYES))
795 							{
796 								// Re-generate the server certificate
797 								RPC_TEST tt;
798 
799 								Zero(&tt, sizeof(tt));
800 
801 								StrCpy(tt.StrValue, sizeof(tt.StrValue), fqdn);
802 
803 								SetText(hWnd, IDCANCEL, _UU("CM_VLAN_INSTALLING"));
804 								Refresh(DlgItem(hWnd, IDCANCEL));
805 								Refresh(hWnd);
806 								DoEvents(NULL);
807 
808 								if (CALL(hWnd, ScRegenerateServerCert(d->s->Rpc, &tt)))
809 								{
810 									// Confirm whether the user want to save the server certificate
811 									if ((d->Silent == false) && (
812 										MsgBoxEx(hWnd, MB_ICONINFORMATION | MB_YESNO,
813 										_UU("SM_DDNS_SERVER_CERT_OK"),
814 										fqdn) == IDYES))
815 									{
816 										// Get the server certificate
817 										RPC_KEY_PAIR t2;
818 
819 										Zero(&t2, sizeof(t2));
820 										if (CALL(hWnd, ScGetServerCert(d->s->Rpc, &t2)))
821 										{
822 											wchar_t *name;
823 											wchar_t defname[MAX_PATH];
824 
825 											StrToUni(defname, sizeof(defname), fqdn);
826 											UniStrCat(defname, sizeof(defname), L".cer");
827 
828 											name = SaveDlg(hWnd, _UU("DLG_CERT_FILES"), _UU("DLG_SAVE_CERT"), defname, L".cer");
829 
830 											if (name != NULL)
831 											{
832 												if (XToFileW(t2.Cert, name, true))
833 												{
834 													MsgBox(hWnd, MB_ICONINFORMATION, _UU("DLG_CERT_SAVE_OK"));
835 												}
836 												else
837 												{
838 													MsgBox(hWnd, MB_ICONSTOP, _UU("DLG_CERT_SAVE_ERROR"));
839 												}
840 
841 												Free(name);
842 											}
843 
844 											FreeRpcKeyPair(&t2);
845 										}
846 									}
847 								}
848 							}
849 						}
850 					}
851 
852 					FreeRpcKeyPair(&t);
853 				}
854 			}
855 		}
856 
857 		EndDialog(hWnd, 0);
858 		break;
859 	}
860 
861 	return 0;
862 }
863 
864 // Get the ddns key from the server configuration file
SmDdnsGetKey(char * key,SM_DDNS * d)865 static UINT SmDdnsGetKey(char *key, SM_DDNS *d){
866 	RPC_CONFIG config;
867 	UINT err;
868 	BUF *buf;
869 	FOLDER *root, *ddnsfolder;
870 	RPC *rpc;
871 
872 	// Validate arguments
873 	if(d == NULL || d->s == NULL || key == NULL){
874 		return ERR_INTERNAL_ERROR;
875 	}
876 
877 	rpc = d->s->Rpc;
878 
879 	Zero(&config, sizeof(config));
880 	err = ScGetConfig(d->s->Rpc, &config);
881 	if(err != ERR_NO_ERROR){
882 		return err;
883 	}
884 
885 	buf = NewBufFromMemory(config.FileData, StrLen(config.FileData));
886 	FreeRpcConfig(&config);
887 
888 	root = CfgBufTextToFolder(buf);
889 	FreeBuf(buf);
890 
891 	ddnsfolder = CfgGetFolder(root, "DDnsClient");
892 	err = CfgGetByte(ddnsfolder, "Key", key, 20);
893 
894 	CfgDeleteFolder(root);
895 
896 	return (err == 20) ? ERR_NO_ERROR : ERR_INTERNAL_ERROR;
897 }
898 
SmDDnsDlgInit(HWND hWnd,SM_DDNS * d)899 void SmDDnsDlgInit(HWND hWnd, SM_DDNS *d)
900 {
901 	char key[20];
902 	char encodedkey[20 * 4 + 32];
903 
904 	// Validate arguments
905 	if (hWnd == NULL || d == NULL)
906 	{
907 		return;
908 	}
909 
910 	d->DoNotPoll = false;
911 
912 	SetIcon(hWnd, 0, ICO_DISPLAY);
913 
914 	DlgFont(hWnd, S_TITLE, 14, true);
915 
916 	DlgFont(hWnd, S_BOLD, 0, true);
917 	DlgFont(hWnd, S_STATUS3, 0, true);
918 	DlgFont(hWnd, S_STATUS4, 0, true);
919 	DlgFont(hWnd, S_STATUS5, 0, true);
920 	DlgFont(hWnd, S_STATUS6, 0, true);
921 	DlgFont(hWnd, S_STATUS8, 0, true);
922 
923 	SetFont(hWnd, S_SUFFIX, GetFont("Verdana", 10, false, false, false, false));
924 	SetFont(hWnd, E_NEWHOST, GetFont("Verdana", 10, false, false, false, false));
925 
926 	SetFont(hWnd, E_HOST, GetFont((MsIsWinXPOrGreater() ? "Verdana" : NULL), 10, false, false, false, false));
927 	SetFont(hWnd, E_IPV4, GetFont((MsIsWinXPOrGreater() ? "Verdana" : NULL), 10, false, false, false, false));
928 	SetFont(hWnd, E_IPV6, GetFont((MsIsWinXPOrGreater() ? "Verdana" : NULL), 10, false, false, false, false));
929 	SetFont(hWnd, E_KEY, GetFont((MsIsWinXPOrGreater() ? "Verdana" : NULL), 8, false, false, false, false));
930 
931 	DlgFont(hWnd, IDOK, 0, true);
932 
933 	if (d->Silent)
934 	{
935 		Hide(hWnd, B_DISABLE);
936 	}
937 
938 	Hide(hWnd, B_PROXY);
939 
940 	if(SmDdnsGetKey(key, d) == ERR_NO_ERROR){
941 		encodedkey[ B64_Encode(encodedkey, key, 20) ] = 0;
942 		SetTextA(hWnd, E_KEY, encodedkey);
943 	}else{
944 		SetText(hWnd, E_KEY, _UU("SM_DDNS_KEY_ERR"));
945 	}
946 
947 	SmDDnsRefresh(hWnd, d);
948 }
949 
SmDDnsRefresh(HWND hWnd,SM_DDNS * d)950 void SmDDnsRefresh(HWND hWnd, SM_DDNS *d)
951 {
952 	DDNS_CLIENT_STATUS st;
953 	INTERNET_SETTING t;
954 
955 	// Validate arguments
956 	if (hWnd == NULL || d == NULL)
957 	{
958 		return;
959 	}
960 
961 	if (d->DoNotPoll)
962 	{
963 		return;
964 	}
965 
966 	Zero(&st, sizeof(st));
967 	Zero(&t, sizeof(t));
968 
969 	// Get the status
970 	if (CALL(hWnd, ScGetDDnsClientStatus(d->s->Rpc, &st)) == false)
971 	{
972 		Close(hWnd);
973 		return;
974 	}
975 
976 	ScGetDDnsInternetSetting(d->s->Rpc, &t);
977 
978 	Copy(&d->Status, &st, sizeof(st));
979 
980 	if (IsEmptyStr(st.CurrentFqdn) == false)
981 	{
982 		SetTextA(hWnd, E_HOST, st.CurrentFqdn);
983 	}
984 	else
985 	{
986 		SetText(hWnd, E_HOST, _UU("SM_DDNS_FQDN_EMPTY"));
987 	}
988 
989 	if (st.Err_IPv4 == ERR_NO_ERROR)
990 	{
991 		SetTextA(hWnd, E_IPV4, st.CurrentIPv4);
992 	}
993 	else
994 	{
995 		if (st.Err_IPv4 == ERR_CONNECT_FAILED)
996 		{
997 			SetText(hWnd, E_IPV4, _UU("SM_DDNS_IPV4_ERROR"));
998 		}
999 		else
1000 		{
1001 			SetText(hWnd, E_IPV4, _E(st.Err_IPv4));
1002 		}
1003 	}
1004 
1005 	if (st.Err_IPv6 == ERR_NO_ERROR)
1006 	{
1007 		SetTextA(hWnd, E_IPV6, st.CurrentIPv6);
1008 	}
1009 	else
1010 	{
1011 		if (st.Err_IPv6 == ERR_CONNECT_FAILED)
1012 		{
1013 			SetText(hWnd, E_IPV6, _UU("SM_DDNS_IPV6_ERROR"));
1014 		}
1015 		else
1016 		{
1017 			SetText(hWnd, E_IPV6, _E(st.Err_IPv6));
1018 		}
1019 	}
1020 
1021 	if (st.Err_IPv4 == ERR_NO_ERROR || st.Err_IPv6 == ERR_NO_ERROR)
1022 	{
1023 		if (IsEmptyStr(st.DnsSuffix) == false)
1024 		{
1025 			SetTextA(hWnd, S_SUFFIX, st.DnsSuffix);
1026 		}
1027 
1028 		Enable(hWnd, S_STATUS6);
1029 		Enable(hWnd, E_NEWHOST);
1030 		Enable(hWnd, S_SUFFIX);
1031 		Enable(hWnd, S_STATUS7);
1032 		Enable(hWnd, B_HINT);
1033 	}
1034 	else
1035 	{
1036 		SetTextA(hWnd, S_SUFFIX, "");
1037 
1038 		Disable(hWnd, S_STATUS6);
1039 		Disable(hWnd, E_NEWHOST);
1040 		Disable(hWnd, S_SUFFIX);
1041 		Disable(hWnd, S_STATUS7);
1042 		Disable(hWnd, B_HINT);
1043 	}
1044 
1045 	if (GetCapsBool(d->s->CapsList, "b_support_ddns_proxy"))
1046 	{
1047 		// Show the proxy button
1048 		Show(hWnd, B_PROXY);
1049 	}
1050 	else
1051 	{
1052 		// Hide the proxy button
1053 		Hide(hWnd, B_PROXY);
1054 	}
1055 
1056 	SmDDnsDlgUpdate(hWnd, d);
1057 
1058 	if (d->Flag == false)
1059 	{
1060 		d->Flag = true;
1061 	}
1062 
1063 	if (IsEmptyStr(st.CurrentHostName) == false)
1064 	{
1065 		if (d->HostnameSetFlag == false)
1066 		{
1067 			d->HostnameSetFlag = true;
1068 
1069 			SetTextA(hWnd, E_NEWHOST, st.CurrentHostName);
1070 
1071 			FocusEx(hWnd, E_NEWHOST);
1072 		}
1073 	}
1074 }
SmDDnsDlgUpdate(HWND hWnd,SM_DDNS * d)1075 void SmDDnsDlgUpdate(HWND hWnd, SM_DDNS *d)
1076 {
1077 	char tmp[MAX_SIZE];
1078 	bool b = false;
1079 
1080 	// Validate arguments
1081 	if (hWnd == NULL || d == NULL)
1082 	{
1083 		return;
1084 	}
1085 
1086 	if (GetTxtA(hWnd, E_NEWHOST, tmp, sizeof(tmp)))
1087 	{
1088 		Trim(tmp);
1089 
1090 		// Get whether the host name have changed
1091 		if (IsEmptyStr(tmp) == false)
1092 		{
1093 			if (StrCmpi(d->Status.CurrentHostName, tmp) != 0)
1094 			{
1095 				if (d->Status.Err_IPv4 == ERR_NO_ERROR || d->Status.Err_IPv6 == ERR_NO_ERROR)
1096 				{
1097 					b = true;
1098 				}
1099 			}
1100 		}
1101 	}
1102 
1103 	SetEnable(hWnd, IDOK, b);
1104 	SetEnable(hWnd, B_RESTORE, b);
1105 }
SmDDnsDlgOnOk(HWND hWnd,SM_DDNS * d)1106 void SmDDnsDlgOnOk(HWND hWnd, SM_DDNS *d)
1107 {
1108 	RPC_TEST t;
1109 	// Validate arguments
1110 	if (hWnd == NULL || d == NULL)
1111 	{
1112 		return;
1113 	}
1114 
1115 	Zero(&t, sizeof(t));
1116 	GetTxtA(hWnd, E_NEWHOST, t.StrValue, sizeof(t.StrValue));
1117 
1118 	if (CALL(hWnd, ScChangeDDnsClientHostname(d->s->Rpc, &t)) == false)
1119 	{
1120 		return;
1121 	}
1122 
1123 	d->Changed = true;
1124 
1125 	SmDDnsRefresh(hWnd, d);
1126 	FocusEx(hWnd, E_NEWHOST);
1127 
1128 	MsgBoxEx(hWnd, MB_ICONINFORMATION, _UU("SM_DDNS_OK_MSG2"), t.StrValue);
1129 	FocusEx(hWnd, E_NEWHOST);
1130 }
1131 
1132 // Open the OpenVPN dialog
SmOpenVpn(HWND hWnd,SM_SERVER * s)1133 void SmOpenVpn(HWND hWnd, SM_SERVER *s)
1134 {
1135 	// Validate arguments
1136 	if (s == NULL)
1137 	{
1138 		return;
1139 	}
1140 
1141 	Dialog(hWnd, D_SM_OPENVPN, SmOpenVpnDlg, s);
1142 }
1143 
1144 // OpenVPN dialog
SmOpenVpnDlg(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)1145 UINT SmOpenVpnDlg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
1146 {
1147 	SM_SERVER *s = (SM_SERVER *)param;
1148 	char tmp[MAX_SIZE];
1149 	// Validate arguments
1150 	if (hWnd == NULL)
1151 	{
1152 		return 0;
1153 	}
1154 
1155 	switch (msg)
1156 	{
1157 	case WM_INITDIALOG:
1158 		SmOpenVpnDlgInit(hWnd, s);
1159 		break;
1160 
1161 	case WM_COMMAND:
1162 		switch (LOWORD(wParam))
1163 		{
1164 		case R_OPENVPN:
1165 		case E_UDP:
1166 		case R_SSTP:
1167 			SmOpenVpnDlgUpdate(hWnd, s);
1168 			break;
1169 		}
1170 
1171 		switch (wParam)
1172 		{
1173 		case IDOK:
1174 			SmOpenVpnDlgOnOk(hWnd, s, false);
1175 			break;
1176 
1177 		case B_DEFAULT:
1178 			ToStr(tmp, OPENVPN_UDP_PORT);
1179 			SetTextA(hWnd, E_UDP, tmp);
1180 			FocusEx(hWnd, E_UDP);
1181 			break;
1182 
1183 		case B_CONFIG:
1184 			// Create an OpenVPN configuration
1185 			{
1186 				OPENVPN_SSTP_CONFIG t2;
1187 				RPC_READ_LOG_FILE t;
1188 
1189 				Zero(&t2, sizeof(t2));
1190 
1191 				if (CALL(hWnd, ScGetOpenVpnSstpConfig(s->Rpc, &t2)))
1192 				{
1193 					if (t2.EnableOpenVPN == false)
1194 					{
1195 						// Enable the OpenVPN first
1196 						SmOpenVpnDlgOnOk(hWnd, s, true);
1197 
1198 						Disable(hWnd, IDCANCEL);
1199 					}
1200 				}
1201 				else
1202 				{
1203 					break;
1204 				}
1205 
1206 				Zero(&t, sizeof(t));
1207 
1208 				if (CALL(hWnd, ScMakeOpenVpnConfigFile(s->Rpc, &t)))
1209 				{
1210 					// Generate a file name
1211 					wchar_t filename[MAX_SIZE];
1212 					char safe_hostname[MAX_SIZE];
1213 					SYSTEMTIME st;
1214 					wchar_t *dst;
1215 
1216 					MakeSafeFileName(safe_hostname, sizeof(safe_hostname), s->ServerName);
1217 
1218 					LocalTime(&st);
1219 
1220 					UniFormat(filename, sizeof(filename),
1221 						L"OpenVPN_Sample_Config_%S_%04u%02u%02u_%02u%02u%02u.zip",
1222 						safe_hostname,
1223 						st.wYear, st.wMonth, st.wDay,
1224 						st.wHour, st.wMinute, st.wSecond);
1225 
1226 					dst = SaveDlg(hWnd, _UU("DLG_ZIP_FILER"), _UU("DLG_SAVE_OPENVPN_CONFIG"),
1227 						filename, L".zip");
1228 
1229 					if (dst != NULL)
1230 					{
1231 						// Save
1232 						if (DumpBufW(t.Buffer, dst) == false)
1233 						{
1234 							// Failure
1235 							MsgBoxEx(hWnd, MB_ICONSTOP, _UU("SM_OPENVPN_CONFIG_SAVE_NG"), dst);
1236 						}
1237 						else
1238 						{
1239 							// Success
1240 							if (MsgBoxEx(hWnd, MB_ICONQUESTION | MB_YESNO, _UU("SM_OPENVPN_CONFIG_SAVE_OK"), dst) == IDYES)
1241 							{
1242 								if (MsExecuteW(dst, L"") == false)
1243 								{
1244 									MsgBoxEx(hWnd, MB_ICONINFORMATION, _UU("SM_OPENVPN_CONFIG_OPEN_NG"), dst);
1245 								}
1246 							}
1247 						}
1248 
1249 						Free(dst);
1250 					}
1251 
1252 					FreeRpcReadLogFile(&t);
1253 				}
1254 			}
1255 			break;
1256 
1257 		case B_IPSEC:
1258 			SmIPsec(hWnd, s);
1259 			break;
1260 
1261 		case IDCANCEL:
1262 			Close(hWnd);
1263 			break;
1264 		}
1265 		break;
1266 
1267 	case WM_CLOSE:
1268 		EndDialog(hWnd, 0);
1269 		break;
1270 	}
1271 
1272 	return 0;
1273 }
SmOpenVpnDlgInit(HWND hWnd,SM_SERVER * s)1274 void SmOpenVpnDlgInit(HWND hWnd, SM_SERVER *s)
1275 {
1276 	OPENVPN_SSTP_CONFIG t;
1277 	// Validate arguments
1278 	if (hWnd == NULL || s == NULL)
1279 	{
1280 		return;
1281 	}
1282 
1283 	Zero(&t, sizeof(t));
1284 	if (CALL(hWnd, ScGetOpenVpnSstpConfig(s->Rpc, &t)) == false)
1285 	{
1286 		Close(hWnd);
1287 		return;
1288 	}
1289 
1290 	Check(hWnd, R_OPENVPN, t.EnableOpenVPN);
1291 	SetTextA(hWnd, E_UDP, t.OpenVPNPortList);
1292 	Check(hWnd, R_SSTP, t.EnableSSTP);
1293 
1294 	SetIcon(hWnd, 0, ICO_OPENVPN);
1295 
1296 	DlgFont(hWnd, S_TITLE, 14, true);
1297 	SetFont(hWnd, E_UDP, GetFont("Verdana", 10, false, false, false, false));
1298 
1299 	DlgFont(hWnd, R_OPENVPN, 0, true);
1300 	DlgFont(hWnd, S_TOOL, 11, true);
1301 	DlgFont(hWnd, R_SSTP, 0, true);
1302 
1303 	SmOpenVpnDlgUpdate(hWnd, s);
1304 }
SmOpenVpnDlgUpdate(HWND hWnd,SM_SERVER * s)1305 void SmOpenVpnDlgUpdate(HWND hWnd, SM_SERVER *s)
1306 {
1307 	bool b1, b2;
1308 	// Validate arguments
1309 	if (hWnd == NULL || s == NULL)
1310 	{
1311 		return;
1312 	}
1313 
1314 	b1 = IsChecked(hWnd, R_OPENVPN);
1315 	b2 = IsChecked(hWnd, R_SSTP);
1316 
1317 	SetEnable(hWnd, S_UDP, b1);
1318 	SetEnable(hWnd, E_UDP, b1);
1319 	SetEnable(hWnd, B_DEFAULT, b1);
1320 	SetEnable(hWnd, S_UDP2, b1);
1321 	SetEnable(hWnd, S_TOOL, b1);
1322 	SetEnable(hWnd, S_TOOL2, b1);
1323 	SetEnable(hWnd, B_CONFIG, b1);
1324 
1325 	SetEnable(hWnd, S_SSTP, b2);
1326 }
SmOpenVpnDlgOnOk(HWND hWnd,SM_SERVER * s,bool no_close)1327 void SmOpenVpnDlgOnOk(HWND hWnd, SM_SERVER *s, bool no_close)
1328 {
1329 	OPENVPN_SSTP_CONFIG t;
1330 	// Validate arguments
1331 	if (hWnd == NULL || s == NULL)
1332 	{
1333 		return;
1334 	}
1335 
1336 	Zero(&t, sizeof(t));
1337 
1338 	t.EnableOpenVPN = IsChecked(hWnd, R_OPENVPN);
1339 	GetTxtA(hWnd, E_UDP, t.OpenVPNPortList, sizeof(t.OpenVPNPortList));
1340 	t.EnableSSTP = IsChecked(hWnd, R_SSTP);
1341 
1342 	if (CALL(hWnd, ScSetOpenVpnSstpConfig(s->Rpc, &t)) == false)
1343 	{
1344 		return;
1345 	}
1346 
1347 	if (no_close == false)
1348 	{
1349 		EndDialog(hWnd, 1);
1350 	}
1351 }
1352 
1353 // Open the EtherIP ID edit dialog
SmEtherIpId(HWND hWnd,SM_ETHERIP_ID * t)1354 bool SmEtherIpId(HWND hWnd, SM_ETHERIP_ID *t)
1355 {
1356 	// Validate arguments
1357 	if (t == NULL)
1358 	{
1359 		return false;
1360 	}
1361 
1362 	if (Dialog(hWnd, D_SM_ETHERIP_ID, SmEtherIpIdDlg, t) == 0)
1363 	{
1364 		return false;
1365 	}
1366 
1367 	return true;
1368 }
1369 
1370 // EtherIP ID edit dialog procedure
SmEtherIpIdDlg(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)1371 UINT SmEtherIpIdDlg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
1372 {
1373 	SM_ETHERIP_ID *t = (SM_ETHERIP_ID *)param;
1374 	// Validate arguments
1375 	if (hWnd == NULL)
1376 	{
1377 		return 0;
1378 	}
1379 
1380 	switch (msg)
1381 	{
1382 	case WM_INITDIALOG:
1383 		SmEtherIpIdDlgInit(hWnd, t);
1384 		break;
1385 
1386 	case WM_COMMAND:
1387 		switch (LOWORD(wParam))
1388 		{
1389 		case E_ID:
1390 		case L_HUBNAME:
1391 		case E_USERNAME:
1392 			SmEtherIpIdDlgUpdate(hWnd, t);
1393 			break;
1394 		}
1395 
1396 		switch (wParam)
1397 		{
1398 		case IDOK:
1399 			SmEtherIpIdDlgOnOk(hWnd, t);
1400 			break;
1401 
1402 		case IDCANCEL:
1403 			Close(hWnd);
1404 			break;
1405 		}
1406 		break;
1407 
1408 	case WM_CLOSE:
1409 		EndDialog(hWnd, 0);
1410 		break;
1411 	}
1412 
1413 	return 0;
1414 }
1415 
1416 // Initialize the EtherIP ID edit dialog
SmEtherIpIdDlgInit(HWND hWnd,SM_ETHERIP_ID * t)1417 void SmEtherIpIdDlgInit(HWND hWnd, SM_ETHERIP_ID *t)
1418 {
1419 	RPC_ENUM_HUB tt;
1420 	UINT sel_index;
1421 	UINT i;
1422 	// Validate arguments
1423 	if (hWnd == NULL || t == NULL)
1424 	{
1425 		return;
1426 	}
1427 
1428 	SetIcon(hWnd, 0, ICO_KEY);
1429 
1430 	// Get the current data in the case of edit mode
1431 	if (t->EditMode)
1432 	{
1433 		Zero(&t->Data, sizeof(t->Data));
1434 		StrCpy(t->Data.Id, sizeof(t->Data.Id), t->EditId);
1435 
1436 		if (CALL(hWnd, ScGetEtherIpId(t->s->Rpc, &t->Data)) == false)
1437 		{
1438 			EndDialog(hWnd, 0);
1439 			return;
1440 		}
1441 	}
1442 
1443 	// Enumerate the Virtual HUBs
1444 	Zero(&tt, sizeof(tt));
1445 	if (CALL(hWnd, ScEnumHub(t->s->Rpc, &tt)) == false)
1446 	{
1447 		EndDialog(hWnd, 0);
1448 		return;
1449 	}
1450 
1451 	CbReset(hWnd, L_HUBNAME);
1452 	CbSetHeight(hWnd, L_HUBNAME, 18);
1453 	sel_index = INFINITE;
1454 
1455 	for (i = 0;i < tt.NumHub;i++)
1456 	{
1457 		RPC_ENUM_HUB_ITEM *e = &tt.Hubs[i];
1458 		UINT index;
1459 
1460 		index = CbAddStrA(hWnd, L_HUBNAME, e->HubName, 0);
1461 		if (sel_index == INFINITE)
1462 		{
1463 			sel_index = index;
1464 		}
1465 
1466 		if (t->EditMode)
1467 		{
1468 			if (StrCmpi(e->HubName, t->Data.HubName) == 0)
1469 			{
1470 				sel_index = index;
1471 			}
1472 		}
1473 	}
1474 
1475 	if (sel_index != INFINITE)
1476 	{
1477 		CbSelectIndex(hWnd, L_HUBNAME, sel_index);
1478 	}
1479 
1480 	if (t->EditMode)
1481 	{
1482 		SetTextA(hWnd, E_ID, t->Data.Id);
1483 		SetTextA(hWnd, E_USERNAME, t->Data.UserName);
1484 		SetTextA(hWnd, E_PASSWORD, t->Data.Password);
1485 
1486 		FocusEx(hWnd, E_PASSWORD);
1487 	}
1488 	else
1489 	{
1490 		Focus(hWnd, E_ID);
1491 	}
1492 
1493 	FreeRpcEnumHub(&tt);
1494 
1495 	t->InitCompleted = true;
1496 	SmEtherIpIdDlgUpdate(hWnd, t);
1497 }
1498 
1499 // EtherIP ID edit dialog: Click the OK button
SmEtherIpIdDlgOnOk(HWND hWnd,SM_ETHERIP_ID * t)1500 void SmEtherIpIdDlgOnOk(HWND hWnd, SM_ETHERIP_ID *t)
1501 {
1502 	// Validate arguments
1503 	if (hWnd == NULL || t == NULL)
1504 	{
1505 		return;
1506 	}
1507 
1508 	SmEtherIpIdDlgGetSetting(hWnd, t);
1509 
1510 	if (t->EditMode)
1511 	{
1512 		ETHERIP_ID d;
1513 		// Delete old items
1514 		Zero(&d, sizeof(d));
1515 
1516 		StrCpy(d.Id, sizeof(d.Id), t->EditId);
1517 
1518 		ScDeleteEtherIpId(t->s->Rpc, &d);
1519 	}
1520 
1521 	if (CALL(hWnd, ScAddEtherIpId(t->s->Rpc, &t->Data)) == false)
1522 	{
1523 		return;
1524 	}
1525 
1526 	if (t->EditMode == false)
1527 	{
1528 		MsgBox(hWnd, MB_ICONINFORMATION, _UU("SM_ETHERIP_ADD_OK"));
1529 	}
1530 
1531 	EndDialog(hWnd, 1);
1532 }
1533 
1534 // EtherIP ID edit dialog: Update the controls
SmEtherIpIdDlgUpdate(HWND hWnd,SM_ETHERIP_ID * t)1535 void SmEtherIpIdDlgUpdate(HWND hWnd, SM_ETHERIP_ID *t)
1536 {
1537 	bool ok = true;
1538 	// Validate arguments
1539 	if (hWnd == NULL || t == NULL)
1540 	{
1541 		return;
1542 	}
1543 
1544 	if (t->InitCompleted == false)
1545 	{
1546 		return;
1547 	}
1548 
1549 	SmEtherIpIdDlgGetSetting(hWnd, t);
1550 
1551 	if (IsEmptyStr(t->Data.Id) ||
1552 		IsEmptyStr(t->Data.HubName) ||
1553 		IsEmptyStr(t->Data.UserName))
1554 	{
1555 		ok = false;
1556 	}
1557 
1558 	SetEnable(hWnd, IDOK, ok);
1559 }
1560 
1561 // EtherIP ID edit dialog: Get the current settings
SmEtherIpIdDlgGetSetting(HWND hWnd,SM_ETHERIP_ID * t)1562 void SmEtherIpIdDlgGetSetting(HWND hWnd, SM_ETHERIP_ID *t)
1563 {
1564 	wchar_t *ws;
1565 	// Validate arguments
1566 	if (hWnd == NULL || t == NULL)
1567 	{
1568 		return;
1569 	}
1570 
1571 	Zero(&t->Data, sizeof(t->Data));
1572 
1573 	GetTxtA(hWnd, E_ID, t->Data.Id, sizeof(t->Data.Id));
1574 	GetTxtA(hWnd, E_USERNAME, t->Data.UserName, sizeof(t->Data.UserName));
1575 	GetTxtA(hWnd, E_PASSWORD, t->Data.Password, sizeof(t->Data.Password));
1576 
1577 	ws = CbGetStr(hWnd, L_HUBNAME);
1578 
1579 	if (ws != NULL && IsEmptyUniStr(ws) == false)
1580 	{
1581 		UniToStr(t->Data.HubName, sizeof(t->Data.HubName), ws);
1582 	}
1583 
1584 	Free(ws);
1585 }
1586 
1587 
1588 // Open the EtherIP settings dialog
SmEtherIp(HWND hWnd,SM_SERVER * s)1589 void SmEtherIp(HWND hWnd, SM_SERVER *s)
1590 {
1591 	// Validate arguments
1592 	if (s == NULL)
1593 	{
1594 		return;
1595 	}
1596 
1597 	Dialog(hWnd, D_SM_ETHERIP, SmEtherIpDlg, s);
1598 }
1599 
1600 // EtherIP Setup dialog procedure
SmEtherIpDlg(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)1601 UINT SmEtherIpDlg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
1602 {
1603 	SM_SERVER *s = (SM_SERVER *)param;
1604 	NMHDR *n;
1605 	char *id;
1606 	SM_ETHERIP_ID t;
1607 	// Validate arguments
1608 	if (hWnd == NULL)
1609 	{
1610 		return 0;
1611 	}
1612 
1613 	switch (msg)
1614 	{
1615 	case WM_INITDIALOG:
1616 		SmEtherIpDlgInit(hWnd, s);
1617 		break;
1618 
1619 	case WM_COMMAND:
1620 		switch (wParam)
1621 		{
1622 		case IDOK:
1623 			// Edit
1624 			id = LvGetSelectedStrA(hWnd, L_LIST, 0);
1625 			if (id != NULL)
1626 			{
1627 				Zero(&t, sizeof(t));
1628 				StrCpy(t.EditId, sizeof(t.EditId), id);
1629 				t.EditMode = true;
1630 				t.s = s;
1631 
1632 				if (SmEtherIpId(hWnd, &t))
1633 				{
1634 					SmEtherIpDlgRefresh(hWnd, s);
1635 				}
1636 
1637 				Free(id);
1638 			}
1639 			break;
1640 
1641 		case B_ADD:
1642 			// Add
1643 			Zero(&t, sizeof(t));
1644 			t.s = s;
1645 			if (SmEtherIpId(hWnd, &t))
1646 			{
1647 				SmEtherIpDlgRefresh(hWnd, s);
1648 			}
1649 			break;
1650 
1651 		case B_DELETE:
1652 			// Delete
1653 			if (MsgBox(hWnd, MB_ICONQUESTION | MB_YESNO | MB_DEFBUTTON2, _UU("SM_CRL_DELETE_MSG")) == IDYES)
1654 			{
1655 				id = LvGetSelectedStrA(hWnd, L_LIST, 0);
1656 				if (id != NULL)
1657 				{
1658 					ETHERIP_ID d;
1659 
1660 					Zero(&d, sizeof(d));
1661 
1662 					StrCpy(d.Id, sizeof(d.Id), id);
1663 
1664 					if (CALL(hWnd, ScDeleteEtherIpId(s->Rpc, &d)))
1665 					{
1666 						SmEtherIpDlgRefresh(hWnd, s);
1667 					}
1668 
1669 					Free(id);
1670 				}
1671 			}
1672 			break;
1673 
1674 		case IDCANCEL:
1675 			// Close
1676 			Close(hWnd);
1677 			break;
1678 		}
1679 		break;
1680 
1681 	case WM_NOTIFY:
1682 		n = (NMHDR *)lParam;
1683 		switch (n->idFrom)
1684 		{
1685 		case L_LIST:
1686 			switch (n->code)
1687 			{
1688 			case LVN_ITEMCHANGED:
1689 				SmEtherIpDlgUpdate(hWnd, s);
1690 				break;
1691 
1692 			case NM_DBLCLK:
1693 				Command(hWnd, IDOK);
1694 				break;
1695 			}
1696 			break;
1697 		}
1698 		break;
1699 
1700 	case WM_CLOSE:
1701 		EndDialog(hWnd, 0);
1702 		break;
1703 	}
1704 
1705 	return 0;
1706 }
1707 
1708 // EtherIP Setup dialog data update
SmEtherIpDlgRefresh(HWND hWnd,SM_SERVER * s)1709 void SmEtherIpDlgRefresh(HWND hWnd, SM_SERVER *s)
1710 {
1711 	RPC_ENUM_ETHERIP_ID t;
1712 	UINT i;
1713 	LVB *b;
1714 	// Validate arguments
1715 	if (hWnd == NULL || s == NULL)
1716 	{
1717 		return;
1718 	}
1719 
1720 	// Data update
1721 	Zero(&t, sizeof(t));
1722 
1723 	if (CALL(hWnd, ScEnumEtherIpId(s->Rpc, &t)) == false)
1724 	{
1725 		EndDialog(hWnd, 0);
1726 		return;
1727 	}
1728 
1729 	b = LvInsertStart();
1730 
1731 	for (i = 0;i < t.NumItem;i++)
1732 	{
1733 		ETHERIP_ID *d = &t.IdList[i];
1734 		wchar_t id[MAX_SIZE], hubname[MAX_SIZE], username[MAX_SIZE];
1735 
1736 		StrToUni(id, sizeof(id), d->Id);
1737 		StrToUni(hubname, sizeof(hubname), d->HubName);
1738 		StrToUni(username, sizeof(username), d->UserName);
1739 
1740 		LvInsertAdd(b, ICO_CASCADE, NULL, 3, id, hubname, username);
1741 	}
1742 
1743 	LvInsertEnd(b, hWnd, L_LIST);
1744 
1745 	FreeRpcEnumEtherIpId(&t);
1746 
1747 	SmEtherIpDlgUpdate(hWnd, s);
1748 }
1749 
1750 // Initialize the EtherIP settings dialog
SmEtherIpDlgInit(HWND hWnd,SM_SERVER * s)1751 void SmEtherIpDlgInit(HWND hWnd, SM_SERVER *s)
1752 {
1753 	// Validate arguments
1754 	if (hWnd == NULL || s == NULL)
1755 	{
1756 		return;
1757 	}
1758 
1759 	SetIcon(hWnd, 0, ICO_MACHINE);
1760 	DlgFont(hWnd, S_TITLE, 14, true);
1761 	DlgFont(hWnd, S_BOLD, 0, true);
1762 
1763 	LvInit(hWnd, L_LIST);
1764 	LvInsertColumn(hWnd, L_LIST, 0, _UU("SM_ETHERIP_COLUMN_0"), 205);
1765 	LvInsertColumn(hWnd, L_LIST, 1, _UU("SM_ETHERIP_COLUMN_1"), 179);
1766 	LvInsertColumn(hWnd, L_LIST, 2, _UU("SM_ETHERIP_COLUMN_2"), 154);
1767 
1768 	SmEtherIpDlgRefresh(hWnd, s);
1769 }
1770 
1771 // EtherIP Settings dialog controls update
SmEtherIpDlgUpdate(HWND hWnd,SM_SERVER * s)1772 void SmEtherIpDlgUpdate(HWND hWnd, SM_SERVER *s)
1773 {
1774 	// Validate arguments
1775 	if (hWnd == NULL || s == NULL)
1776 	{
1777 		return;
1778 	}
1779 
1780 	SetEnable(hWnd, IDOK, LvIsSingleSelected(hWnd, L_LIST));
1781 	SetEnable(hWnd, B_DELETE, LvIsSingleSelected(hWnd, L_LIST));
1782 }
1783 
1784 // IPsec Settings dialog procedure
SmIPsecDlg(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)1785 UINT SmIPsecDlg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
1786 {
1787 	SM_SERVER *s = (SM_SERVER *)param;
1788 	// Validate arguments
1789 	if (hWnd == NULL)
1790 	{
1791 		return 0;
1792 	}
1793 
1794 	switch (msg)
1795 	{
1796 	case WM_INITDIALOG:
1797 		SmIPsecDlgInit(hWnd, s);
1798 		break;
1799 
1800 	case WM_COMMAND:
1801 		switch (LOWORD(wParam))
1802 		{
1803 		case R_L2TP_OVER_IPSEC:
1804 		case R_L2TP_RAW:
1805 		case R_ETHERIP:
1806 		case E_SECRET:
1807 		case L_HUBNAME:
1808 			SmIPsecDlgUpdate(hWnd, s);
1809 			break;
1810 		}
1811 
1812 		switch (wParam)
1813 		{
1814 		case IDOK:
1815 			SmIPsecDlgOnOk(hWnd, s);
1816 			break;
1817 
1818 		case IDCANCEL:
1819 			Close(hWnd);
1820 			break;
1821 
1822 		case B_DETAIL:
1823 			// Advanced Settings dialog for EtherIP function
1824 			SmEtherIp(hWnd, s);
1825 			break;
1826 		}
1827 		break;
1828 
1829 	case WM_CLOSE:
1830 		EndDialog(hWnd, 0);
1831 		break;
1832 	}
1833 
1834 	return 0;
1835 }
1836 
1837 // IPsec Settings dialog: controls update
SmIPsecDlgUpdate(HWND hWnd,SM_SERVER * s)1838 void SmIPsecDlgUpdate(HWND hWnd, SM_SERVER *s)
1839 {
1840 	IPSEC_SERVICES sl;
1841 	bool ok = true;
1842 	// Validate arguments
1843 	if (hWnd == NULL || s == NULL)
1844 	{
1845 		return;
1846 	}
1847 
1848 	SmIPsecDlgGetSetting(hWnd, &sl);
1849 
1850 	//SetEnable(hWnd, S_1, sl.L2TP_IPsec || sl.L2TP_Raw);
1851 	//SetEnable(hWnd, S_2, sl.L2TP_IPsec || sl.L2TP_Raw);
1852 	//SetEnable(hWnd, L_HUBNAME, sl.L2TP_IPsec || sl.L2TP_Raw);
1853 
1854 	SetEnable(hWnd, S_PSK, sl.L2TP_IPsec || sl.EtherIP_IPsec);
1855 	SetEnable(hWnd, S_PSK2, sl.L2TP_IPsec || sl.EtherIP_IPsec);
1856 	SetEnable(hWnd, E_SECRET, sl.L2TP_IPsec || sl.EtherIP_IPsec);
1857 
1858 	SetEnable(hWnd, B_DETAIL, sl.EtherIP_IPsec);
1859 
1860 	if ((sl.L2TP_IPsec || sl.EtherIP_IPsec) && IsEmptyStr(sl.IPsec_Secret))
1861 	{
1862 		ok = false;
1863 	}
1864 
1865 	SetEnable(hWnd, IDOK, ok);
1866 }
1867 
1868 // Get the IPsec configuration into the structure
SmIPsecDlgGetSetting(HWND hWnd,IPSEC_SERVICES * sl)1869 void SmIPsecDlgGetSetting(HWND hWnd, IPSEC_SERVICES *sl)
1870 {
1871 	wchar_t *ws;
1872 	// Validate arguments
1873 	if (hWnd == NULL || sl == NULL)
1874 	{
1875 		return;
1876 	}
1877 
1878 	Zero(sl, sizeof(IPSEC_SERVICES));
1879 
1880 	sl->L2TP_IPsec = IsChecked(hWnd, R_L2TP_OVER_IPSEC);
1881 	sl->L2TP_Raw = IsChecked(hWnd, R_L2TP_RAW);
1882 	sl->EtherIP_IPsec = IsChecked(hWnd, R_ETHERIP);
1883 
1884 	ws = CbGetStr(hWnd, L_HUBNAME);
1885 	if (ws != NULL && IsEmptyUniStr(ws) == false)
1886 	{
1887 		UniToStr(sl->L2TP_DefaultHub, sizeof(sl->L2TP_DefaultHub), ws);
1888 	}
1889 
1890 	Free(ws);
1891 
1892 	GetTxtA(hWnd, E_SECRET, sl->IPsec_Secret, sizeof(sl->IPsec_Secret));
1893 }
1894 
1895 // IPsec Settings dialog initialization
SmIPsecDlgInit(HWND hWnd,SM_SERVER * s)1896 void SmIPsecDlgInit(HWND hWnd, SM_SERVER *s)
1897 {
1898 	IPSEC_SERVICES sl;
1899 	RPC_ENUM_HUB t;
1900 	UINT i;
1901 	UINT sel_index;
1902 	// Validate arguments
1903 	if (hWnd == NULL || s == NULL)
1904 	{
1905 		return;
1906 	}
1907 
1908 	SetIcon(hWnd, 0, ICO_IPSEC);
1909 
1910 	DlgFont(hWnd, S_TITLE, 14, true);
1911 	SetFont(hWnd, E_SECRET, GetFont("Verdana", 10, false, false, false, false));
1912 
1913 	DlgFont(hWnd, R_L2TP_OVER_IPSEC, 0, true);
1914 	DlgFont(hWnd, R_L2TP_RAW, 0, true);
1915 	DlgFont(hWnd, R_ETHERIP, 0, true);
1916 
1917 	// Get the configuration
1918 	Zero(&sl, sizeof(sl));
1919 	if (CALL(hWnd, ScGetIPsecServices(s->Rpc, &sl)) == false)
1920 	{
1921 		EndDialog(hWnd, 0);
1922 		return;
1923 	}
1924 
1925 	SetTextA(hWnd, E_SECRET, sl.IPsec_Secret);
1926 
1927 	Check(hWnd, R_L2TP_OVER_IPSEC, sl.L2TP_IPsec);
1928 	Check(hWnd, R_L2TP_RAW, sl.L2TP_Raw);
1929 	Check(hWnd, R_ETHERIP, sl.EtherIP_IPsec);
1930 
1931 	// Enumerate the Virtual HUBs
1932 	Zero(&t, sizeof(t));
1933 	if (CALL(hWnd, ScEnumHub(s->Rpc, &t)) == false)
1934 	{
1935 		EndDialog(hWnd, 0);
1936 		return;
1937 	}
1938 
1939 	CbReset(hWnd, L_HUBNAME);
1940 	CbSetHeight(hWnd, L_HUBNAME, 18);
1941 
1942 	sel_index = INFINITE;
1943 	for (i = 0;i < t.NumHub;i++)
1944 	{
1945 		RPC_ENUM_HUB_ITEM *e = &t.Hubs[i];
1946 		UINT index;
1947 
1948 		index = CbAddStrA(hWnd, L_HUBNAME, e->HubName, 0);
1949 		if (sel_index == INFINITE)
1950 		{
1951 			sel_index = index;
1952 		}
1953 
1954 		if (StrCmpi(e->HubName, sl.L2TP_DefaultHub) == 0)
1955 		{
1956 			sel_index = index;
1957 		}
1958 	}
1959 
1960 	if (sel_index != INFINITE)
1961 	{
1962 		CbSelectIndex(hWnd, L_HUBNAME, sel_index);
1963 	}
1964 
1965 	FreeRpcEnumHub(&t);
1966 
1967 	SmIPsecDlgUpdate(hWnd, s);
1968 }
1969 
1970 // IPsec Settings dialog: on click the OK button
SmIPsecDlgOnOk(HWND hWnd,SM_SERVER * s)1971 void SmIPsecDlgOnOk(HWND hWnd, SM_SERVER *s)
1972 {
1973 	IPSEC_SERVICES sl;
1974 	// Validate arguments
1975 	if (hWnd == NULL || s == NULL)
1976 	{
1977 		return;
1978 	}
1979 
1980 	SmIPsecDlgGetSetting(hWnd, &sl);
1981 
1982 	// Confirm the length of the PSK
1983 	if (StrLen(sl.IPsec_Secret) >= 10)
1984 	{
1985 		IPSEC_SERVICES sl_old;
1986 		if (ScGetIPsecServices(s->Rpc, &sl_old) == ERR_NO_ERROR)
1987 		{
1988 			if (StrCmp(sl_old.IPsec_Secret, sl.IPsec_Secret) != 0 || ((sl_old.EtherIP_IPsec == false && sl_old.L2TP_IPsec == false)))
1989 			{
1990 				if (sl.EtherIP_IPsec || sl.L2TP_IPsec)
1991 				{
1992 					// Show a warning message if it exceeds 10 characters (Only if there is a change)
1993 					if (MsgBox(hWnd, MB_ICONEXCLAMATION | MB_YESNO, _UU("SM_IPSEC_PSK_TOO_LONG")) == IDYES)
1994 					{
1995 						FocusEx(hWnd, E_SECRET);
1996 						return;
1997 					}
1998 				}
1999 			}
2000 		}
2001 	}
2002 
2003 	if (CALL(hWnd, ScSetIPsecServices(s->Rpc, &sl)) == false)
2004 	{
2005 		return;
2006 	}
2007 
2008 	EndDialog(hWnd, 1);
2009 }
2010 
2011 // Start the IPsec Settings dialog
SmIPsec(HWND hWnd,SM_SERVER * s)2012 void SmIPsec(HWND hWnd, SM_SERVER *s)
2013 {
2014 	// Validate arguments
2015 	if (s == NULL)
2016 	{
2017 		return;
2018 	}
2019 
2020 	Dialog(hWnd, D_SM_IPSEC, SmIPsecDlg, s);
2021 }
2022 
2023 // Message Settings
SmHubMsg(HWND hWnd,SM_EDIT_HUB * s)2024 void SmHubMsg(HWND hWnd, SM_EDIT_HUB *s)
2025 {
2026 	// Validate arguments
2027 	if (s == NULL)
2028 	{
2029 		return;
2030 	}
2031 
2032 	Dialog(hWnd, D_SM_MSG, SmHubMsgDlg, s);
2033 }
2034 
2035 // Message dialog procedure
SmHubMsgDlg(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)2036 UINT SmHubMsgDlg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
2037 {
2038 	SM_EDIT_HUB *s = (SM_EDIT_HUB *)param;
2039 	// Validate arguments
2040 	if (hWnd == NULL)
2041 	{
2042 		return 0;
2043 	}
2044 
2045 	switch (msg)
2046 	{
2047 	case WM_INITDIALOG:
2048 		SmHubMsgDlgInit(hWnd, s);
2049 		break;
2050 
2051 	case WM_COMMAND:
2052 		switch (LOWORD(wParam))
2053 		{
2054 		case E_TEXT:
2055 			SmHubMsgDlgUpdate(hWnd, s);
2056 			break;
2057 		}
2058 
2059 		switch (wParam)
2060 		{
2061 		case IDOK:
2062 			SmHubMsgDlgOnOk(hWnd, s);
2063 			break;
2064 
2065 		case IDCANCEL:
2066 			Close(hWnd);
2067 			break;
2068 
2069 		case C_USEMSG:
2070 			SmHubMsgDlgUpdate(hWnd, s);
2071 
2072 			if (IsChecked(hWnd, C_USEMSG))
2073 			{
2074 				FocusEx(hWnd, E_TEXT);
2075 			}
2076 			break;
2077 		}
2078 
2079 		break;
2080 
2081 	case WM_CLOSE:
2082 		EndDialog(hWnd, 0);
2083 		break;
2084 	}
2085 
2086 	return 0;
2087 }
2088 
2089 // Message dialog initialization
SmHubMsgDlgInit(HWND hWnd,SM_EDIT_HUB * s)2090 void SmHubMsgDlgInit(HWND hWnd, SM_EDIT_HUB *s)
2091 {
2092 	RPC_MSG t;
2093 	// Validate arguments
2094 	if (hWnd == NULL || s == NULL)
2095 	{
2096 		return;
2097 	}
2098 
2099 	if (MsIsVista())
2100 	{
2101 		SetFont(hWnd, E_TEXT, GetMeiryoFont());
2102 	}
2103 	else
2104 	{
2105 		DlgFont(hWnd, E_TEXT, 11, false);
2106 	}
2107 
2108 	FormatText(hWnd, S_MSG_2, s->HubName);
2109 
2110 	LimitText(hWnd, E_TEXT, HUB_MAXMSG_LEN);
2111 
2112 	Zero(&t, sizeof(t));
2113 
2114 	StrCpy(t.HubName, sizeof(t.HubName), s->HubName);
2115 
2116 	if (CALL(hWnd, ScGetHubMsg(s->p->Rpc, &t)) == false)
2117 	{
2118 		Close(hWnd);
2119 		return;
2120 	}
2121 
2122 	if (UniIsEmptyStr(t.Msg) == false)
2123 	{
2124 		SetText(hWnd, E_TEXT, t.Msg);
2125 
2126 		Check(hWnd, C_USEMSG, true);
2127 	}
2128 	else
2129 	{
2130 		Check(hWnd, C_USEMSG, false);
2131 	}
2132 
2133 	FreeRpcMsg(&t);
2134 
2135 	SmHubMsgDlgUpdate(hWnd, s);
2136 }
2137 
2138 // [OK] button
SmHubMsgDlgOnOk(HWND hWnd,SM_EDIT_HUB * s)2139 void SmHubMsgDlgOnOk(HWND hWnd, SM_EDIT_HUB *s)
2140 {
2141 	RPC_MSG t;
2142 	// Validate arguments
2143 	if (hWnd == NULL || s == NULL)
2144 	{
2145 		return;
2146 	}
2147 
2148 	Zero(&t, sizeof(t));
2149 
2150 	StrCpy(t.HubName, sizeof(t.HubName), s->HubName);
2151 
2152 	if (IsChecked(hWnd, C_USEMSG) == false)
2153 	{
2154 		t.Msg = CopyUniStr(L"");
2155 	}
2156 	else
2157 	{
2158 		t.Msg = GetText(hWnd, E_TEXT);
2159 	}
2160 
2161 	if (CALL(hWnd, ScSetHubMsg(s->p->Rpc, &t)) == false)
2162 	{
2163 		return;
2164 	}
2165 
2166 	FreeRpcMsg(&t);
2167 
2168 	EndDialog(hWnd, 1);
2169 }
2170 
2171 // Message dialog update
SmHubMsgDlgUpdate(HWND hWnd,SM_EDIT_HUB * s)2172 void SmHubMsgDlgUpdate(HWND hWnd, SM_EDIT_HUB *s)
2173 {
2174 	bool b = true;
2175 	// Validate arguments
2176 	if (hWnd == NULL || s == NULL)
2177 	{
2178 		return;
2179 	}
2180 
2181 	SetEnable(hWnd, E_TEXT, IsChecked(hWnd, C_USEMSG));
2182 
2183 	if (IsChecked(hWnd, C_USEMSG))
2184 	{
2185 		wchar_t *s = GetText(hWnd, E_TEXT);
2186 
2187 		b = !IsEmptyUniStr(s);
2188 
2189 		Free(s);
2190 	}
2191 
2192 	SetEnable(hWnd, IDOK, b);
2193 }
2194 
2195 // VLAN utility
SmVLan(HWND hWnd,SM_SERVER * s)2196 void SmVLan(HWND hWnd, SM_SERVER *s)
2197 {
2198 	// Validate arguments
2199 	if (s == NULL)
2200 	{
2201 		return;
2202 	}
2203 
2204 	Dialog(hWnd, D_SM_VLAN, SmVLanDlg, s);
2205 }
2206 
2207 // VLAN dialog
SmVLanDlg(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)2208 UINT SmVLanDlg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
2209 {
2210 	SM_SERVER *s = (SM_SERVER *)param;
2211 	NMHDR *n;
2212 
2213 	// Validate arguments
2214 	if (hWnd == NULL)
2215 	{
2216 		return 0;
2217 	}
2218 
2219 	switch (msg)
2220 	{
2221 	case WM_INITDIALOG:
2222 		SmVLanDlgInit(hWnd, s);
2223 
2224 		if (LvNum(hWnd, L_LIST) == 0)
2225 		{
2226 			Disable(hWnd, L_LIST);
2227 			SetTimer(hWnd, 1, 100, NULL);
2228 		}
2229 		break;
2230 
2231 	case WM_TIMER:
2232 		switch (wParam)
2233 		{
2234 		case 1:
2235 			KillTimer(hWnd, 1);
2236 
2237 			MsgBoxEx(hWnd, MB_ICONINFORMATION, _UU("SM_VLAN_NOTHING"),
2238 				s->CurrentSetting->ClientOption.Hostname);
2239 			break;
2240 		}
2241 		break;
2242 
2243 	case WM_COMMAND:
2244 		switch (wParam)
2245 		{
2246 		case IDOK:
2247 		case IDCANCEL:
2248 			Close(hWnd);
2249 			break;
2250 
2251 		case B_ENABLE:
2252 		case B_DISABLE:
2253 			{
2254 				UINT i = LvGetSelected(hWnd, L_LIST);
2255 				if (i != INFINITE)
2256 				{
2257 					char *name = LvGetStrA(hWnd, L_LIST, i, 0);
2258 
2259 					if (IsEmptyStr(name) == false)
2260 					{
2261 						RPC_TEST t;
2262 
2263 						Zero(&t, sizeof(t));
2264 
2265 						StrCpy(t.StrValue, sizeof(t.StrValue), name);
2266 						t.IntValue = BOOL_TO_INT(wParam == B_ENABLE);
2267 
2268 						if (CALL(hWnd, ScSetEnableEthVLan(s->Rpc, &t)))
2269 						{
2270 							SmVLanDlgRefresh(hWnd, s);
2271 
2272 							if (wParam == B_ENABLE)
2273 							{
2274 								MsgBoxEx(hWnd, MB_ICONINFORMATION,
2275 									_UU("SM_VLAN_MSG_1"),
2276 									name, name, name);
2277 							}
2278 							else
2279 							{
2280 								MsgBoxEx(hWnd, MB_ICONINFORMATION,
2281 									_UU("SM_VLAN_MSG_2"),
2282 									name);
2283 							}
2284 						}
2285 					}
2286 
2287 					Free(name);
2288 				}
2289 				break;
2290 			}
2291 		}
2292 		break;
2293 
2294 	case WM_NOTIFY:
2295 		n = (NMHDR *)lParam;
2296 		switch (n->idFrom)
2297 		{
2298 		case L_LIST:
2299 			switch (n->code)
2300 			{
2301 			case LVN_ITEMCHANGED:
2302 				SmVLanDlgUpdate(hWnd, s);
2303 				break;
2304 			}
2305 			break;
2306 		}
2307 		break;
2308 
2309 	case WM_CLOSE:
2310 		EndDialog(hWnd, 0);
2311 		break;
2312 	}
2313 
2314 	return 0;
2315 }
2316 
2317 // VLAN dialog initialization
SmVLanDlgInit(HWND hWnd,SM_SERVER * s)2318 void SmVLanDlgInit(HWND hWnd, SM_SERVER *s)
2319 {
2320 	// Validate arguments
2321 	if (hWnd == NULL || s == NULL)
2322 	{
2323 		return;
2324 	}
2325 
2326 	LvInit(hWnd, L_LIST);
2327 	LvInsertColumn(hWnd, L_LIST, 0, _UU("SM_VLAN_COLUMN_0"), 245);
2328 	LvInsertColumn(hWnd, L_LIST, 1, _UU("SM_VLAN_COLUMN_1"), 75);
2329 	LvInsertColumn(hWnd, L_LIST, 2, _UU("SM_VLAN_COLUMN_2"), 100);
2330 	LvInsertColumn(hWnd, L_LIST, 3, _UU("SM_VLAN_COLUMN_3"), 100);
2331 	LvInsertColumn(hWnd, L_LIST, 4, _UU("SM_VLAN_COLUMN_4"), 290);
2332 	LvInsertColumn(hWnd, L_LIST, 5, _UU("SM_VLAN_COLUMN_5"), 430);
2333 
2334 	SmVLanDlgRefresh(hWnd, s);
2335 }
2336 
2337 // VLAN dialog content update
SmVLanDlgRefresh(HWND hWnd,SM_SERVER * s)2338 void SmVLanDlgRefresh(HWND hWnd, SM_SERVER *s)
2339 {
2340 	LVB *b;
2341 	RPC_ENUM_ETH_VLAN t;
2342 	UINT i;
2343 	// Validate arguments
2344 	if (hWnd == NULL || s == NULL)
2345 	{
2346 		return;
2347 	}
2348 
2349 	Zero(&t, sizeof(t));
2350 	if (CALL(hWnd, ScEnumEthVLan(s->Rpc, &t)) == false)
2351 	{
2352 		Close(hWnd);
2353 		return;
2354 	}
2355 
2356 	b = LvInsertStart();
2357 
2358 	for (i = 0;i < t.NumItem;i++)
2359 	{
2360 		RPC_ENUM_ETH_VLAN_ITEM *e = &t.Items[i];
2361 
2362 		if (e->Support)
2363 		{
2364 			wchar_t tmp0[MAX_SIZE];
2365 			wchar_t tmp1[MAX_SIZE];
2366 			wchar_t tmp2[MAX_SIZE];
2367 			wchar_t *tmp3;
2368 			wchar_t tmp4[MAX_SIZE];
2369 			wchar_t tmp5[MAX_SIZE];
2370 
2371 			StrToUni(tmp0, sizeof(tmp0), e->DeviceName);
2372 			StrToUni(tmp1, sizeof(tmp1), e->DriverType);
2373 			StrToUni(tmp2, sizeof(tmp2), e->DriverName);
2374 			tmp3 = (e->Enabled ? _UU("SM_VLAN_YES") : _UU("SM_VLAN_NO"));
2375 			StrToUni(tmp4, sizeof(tmp4), e->Guid);
2376 			StrToUni(tmp5, sizeof(tmp5), e->DeviceInstanceId);
2377 
2378 			LvInsertAdd(b,
2379 				e->Enabled ? ICO_NIC_ONLINE : ICO_NIC_OFFLINE, 0, 6,
2380 				tmp0, tmp1, tmp2, tmp3, tmp4, tmp5);
2381 		}
2382 	}
2383 
2384 	LvInsertEnd(b, hWnd, L_LIST);
2385 
2386 	FreeRpcEnumEthVLan(&t);
2387 
2388 	SmVLanDlgUpdate(hWnd, s);
2389 }
2390 
2391 // VLAN dialog control update
SmVLanDlgUpdate(HWND hWnd,SM_SERVER * s)2392 void SmVLanDlgUpdate(HWND hWnd, SM_SERVER *s)
2393 {
2394 	// Validate arguments
2395 	if (hWnd == NULL || s == NULL)
2396 	{
2397 		return;
2398 	}
2399 
2400 	if (LvIsSingleSelected(hWnd, L_LIST) == false)
2401 	{
2402 		Disable(hWnd, B_ENABLE);
2403 		Disable(hWnd, B_DISABLE);
2404 	}
2405 	else
2406 	{
2407 		UINT i = LvGetSelected(hWnd, L_LIST);
2408 		if (i != INFINITE)
2409 		{
2410 			wchar_t *s = LvGetStr(hWnd, L_LIST, i, 3);
2411 
2412 			if (UniStrCmpi(s, _UU("SM_VLAN_YES")) != 0)
2413 			{
2414 				Enable(hWnd, B_ENABLE);
2415 				Disable(hWnd, B_DISABLE);
2416 			}
2417 			else
2418 			{
2419 				Enable(hWnd, B_DISABLE);
2420 				Disable(hWnd, B_ENABLE);
2421 			}
2422 
2423 			Free(s);
2424 		}
2425 	}
2426 }
2427 
2428 // Examine whether the current state of VPN Server / VPN Bridge is the initial state
SmSetupIsNew(SM_SERVER * s)2429 bool SmSetupIsNew(SM_SERVER *s)
2430 {
2431 	RPC *rpc;
2432 	bool is_bridge;
2433 	char hubname[MAX_HUBNAME_LEN + 1];
2434 	bool check_hub = false;
2435 	// Validate arguments
2436 	if (s == NULL)
2437 	{
2438 		return false;
2439 	}
2440 
2441 	if (s->ServerAdminMode == false)
2442 	{
2443 		return false;
2444 	}
2445 
2446 	rpc = s->Rpc;
2447 	is_bridge =s->Bridge;
2448 
2449 	// Server type
2450 	if (is_bridge == false)
2451 	{
2452 		bool b = false;
2453 		RPC_SERVER_INFO t;
2454 
2455 		Zero(&t, sizeof(t));
2456 		if (ScGetServerInfo(rpc, &t) == ERR_NO_ERROR)
2457 		{
2458 			if (t.ServerType != SERVER_TYPE_STANDALONE)
2459 			{
2460 				b = true;
2461 			}
2462 
2463 			FreeRpcServerInfo(&t);
2464 		}
2465 		else
2466 		{
2467 			return false;
2468 		}
2469 
2470 		if (b)
2471 		{
2472 			return false;
2473 		}
2474 	}
2475 
2476 	// Local bridge
2477 	if (true)
2478 	{
2479 		RPC_ENUM_LOCALBRIDGE t;
2480 		bool b = false;
2481 
2482 		Zero(&t, sizeof(t));
2483 		if (ScEnumLocalBridge(rpc, &t) == ERR_NO_ERROR)
2484 		{
2485 			if (t.NumItem != 0)
2486 			{
2487 				b = true;
2488 			}
2489 			FreeRpcEnumLocalBridge(&t);
2490 		}
2491 
2492 		if (b)
2493 		{
2494 			return false;
2495 		}
2496 	}
2497 
2498 	// Virtual HUB
2499 
2500 	check_hub = false;
2501 
2502 	if (is_bridge == false)
2503 	{
2504 		RPC_ENUM_HUB t;
2505 		bool b = false;
2506 
2507 		Zero(&t, sizeof(t));
2508 		if (ScEnumHub(rpc, &t) == ERR_NO_ERROR)
2509 		{
2510 			if (t.NumHub >= 2)
2511 			{
2512 				b = true;
2513 			}
2514 			else if (t.NumHub == 1)
2515 			{
2516 				if (StrCmpi(t.Hubs[0].HubName, SERVER_DEFAULT_HUB_NAME) != 0)
2517 				{
2518 					b = true;
2519 				}
2520 				else
2521 				{
2522 					check_hub = true;
2523 				}
2524 			}
2525 
2526 			FreeRpcEnumHub(&t);
2527 		}
2528 
2529 		if (b)
2530 		{
2531 			return false;
2532 		}
2533 	}
2534 	else
2535 	{
2536 		check_hub = true;
2537 	}
2538 
2539 	// Status of the virtual HUB
2540 	if (is_bridge == false)
2541 	{
2542 		StrCpy(hubname, sizeof(hubname), SERVER_DEFAULT_HUB_NAME);
2543 	}
2544 	else
2545 	{
2546 		StrCpy(hubname, sizeof(hubname), SERVER_DEFAULT_BRIDGE_NAME);
2547 	}
2548 
2549 	if (check_hub)
2550 	{
2551 		if (true)
2552 		{
2553 			// Number of objects in the Virtual HUB
2554 			RPC_HUB_STATUS t;
2555 
2556 			Zero(&t, sizeof(t));
2557 			StrCpy(t.HubName, sizeof(t.HubName), hubname);
2558 
2559 			if (ScGetHubStatus(rpc, &t) == ERR_NO_ERROR)
2560 			{
2561 				if (t.NumSessions != 0 || t.NumAccessLists != 0 ||
2562 					t.NumUsers != 0 || t.NumGroups != 0 ||
2563 					t.NumMacTables != 0 || t.NumIpTables != 0 ||
2564 					t.SecureNATEnabled)
2565 				{
2566 					return false;
2567 				}
2568 			}
2569 			else
2570 			{
2571 				return false;
2572 			}
2573 		}
2574 
2575 		if (true)
2576 		{
2577 			// Cascade connection
2578 			RPC_ENUM_LINK t;
2579 
2580 			Zero(&t, sizeof(t));
2581 			StrCpy(t.HubName, sizeof(t.HubName), hubname);
2582 
2583 			if (ScEnumLink(rpc, &t) == ERR_NO_ERROR)
2584 			{
2585 				bool b = false;
2586 
2587 				if (t.NumLink != 0)
2588 				{
2589 					b = true;
2590 				}
2591 
2592 				FreeRpcEnumLink(&t);
2593 
2594 				if (b)
2595 				{
2596 					return false;
2597 				}
2598 			}
2599 			else
2600 			{
2601 				return false;
2602 			}
2603 		}
2604 
2605 		if (is_bridge == false)
2606 		{
2607 			// Certificate list to trust
2608 			RPC_HUB_ENUM_CA t;
2609 
2610 			Zero(&t, sizeof(t));
2611 			StrCpy(t.HubName, sizeof(t.HubName), hubname);
2612 
2613 			if (ScEnumCa(rpc, &t) == ERR_NO_ERROR)
2614 			{
2615 				bool b = false;
2616 
2617 				if (t.NumCa != 0)
2618 				{
2619 					b = true;
2620 				}
2621 
2622 				FreeRpcHubEnumCa(&t);
2623 
2624 				if (b)
2625 				{
2626 					return false;
2627 				}
2628 			}
2629 			else
2630 			{
2631 				return false;
2632 			}
2633 		}
2634 
2635 		if (is_bridge == false)
2636 		{
2637 			// Certificate revocation list
2638 			RPC_ENUM_CRL t;
2639 
2640 			Zero(&t, sizeof(t));
2641 			StrCpy(t.HubName, sizeof(t.HubName), hubname);
2642 
2643 			if (ScEnumCrl(rpc, &t) == ERR_NO_ERROR)
2644 			{
2645 				bool b = false;
2646 
2647 				if (t.NumItem != 0)
2648 				{
2649 					b = true;
2650 				}
2651 
2652 				FreeRpcEnumCrl(&t);
2653 
2654 				if (b)
2655 				{
2656 					return false;
2657 				}
2658 			}
2659 			else
2660 			{
2661 				return false;
2662 			}
2663 		}
2664 
2665 		if (is_bridge == false)
2666 		{
2667 			// Authentication server configuration
2668 			RPC_RADIUS t;
2669 
2670 			Zero(&t, sizeof(t));
2671 			StrCpy(t.HubName, sizeof(t.HubName), hubname);
2672 
2673 			if (ScGetHubRadius(rpc, &t) == ERR_NO_ERROR)
2674 			{
2675 				if (IsEmptyStr(t.RadiusServerName) == false)
2676 				{
2677 					return false;
2678 				}
2679 			}
2680 			else
2681 			{
2682 				return false;
2683 			}
2684 		}
2685 
2686 		if (is_bridge == false)
2687 		{
2688 			//  Virtual HUB configuration
2689 			RPC_CREATE_HUB t;
2690 
2691 			Zero(&t, sizeof(t));
2692 			StrCpy(t.HubName, sizeof(t.HubName), hubname);
2693 
2694 			if (ScGetHub(rpc, &t) == ERR_NO_ERROR)
2695 			{
2696 				if (t.HubOption.NoEnum || t.HubOption.MaxSession != 0 ||
2697 					t.Online == false)
2698 				{
2699 					return false;
2700 				}
2701 			}
2702 			else
2703 			{
2704 				return false;
2705 			}
2706 		}
2707 
2708 		if (is_bridge == false)
2709 		{
2710 			// IP access control list
2711 			RPC_AC_LIST t;
2712 
2713 			Zero(&t, sizeof(t));
2714 			StrCpy(t.HubName, sizeof(t.HubName), hubname);
2715 
2716 			if (ScGetAcList(rpc, &t) == ERR_NO_ERROR)
2717 			{
2718 				bool b = false;
2719 				if (LIST_NUM(t.o) != 0)
2720 				{
2721 					b = true;
2722 				}
2723 				FreeRpcAcList(&t);
2724 
2725 				if (b)
2726 				{
2727 					return false;
2728 				}
2729 			}
2730 			else
2731 			{
2732 				return false;
2733 			}
2734 		}
2735 
2736 		if (is_bridge == false)
2737 		{
2738 			// AO
2739 			RPC_ADMIN_OPTION t;
2740 
2741 			Zero(&t, sizeof(t));
2742 			StrCpy(t.HubName, sizeof(t.HubName), hubname);
2743 
2744 			if (ScGetHubAdminOptions(rpc, &t) == ERR_NO_ERROR)
2745 			{
2746 				bool b = false;
2747 				UINT i;
2748 
2749 				for (i = 0;i < t.NumItem;i++)
2750 				{
2751 					if (t.Items[i].Value != 0)
2752 					{
2753 						b = true;
2754 					}
2755 				}
2756 
2757 				FreeRpcAdminOption(&t);
2758 
2759 				if (b)
2760 				{
2761 					return false;
2762 				}
2763 			}
2764 			else
2765 			{
2766 				return false;
2767 			}
2768 		}
2769 	}
2770 
2771 	// Virtual layer 3 switch
2772 	if (is_bridge == false)
2773 	{
2774 		RPC_ENUM_L3SW t;
2775 		bool b = false;
2776 
2777 		Zero(&t, sizeof(t));
2778 		if (ScEnumL3Switch(rpc, &t) == ERR_NO_ERROR)
2779 		{
2780 			if (t.NumItem != 0)
2781 			{
2782 				b = true;
2783 			}
2784 
2785 			FreeRpcEnumL3Sw(&t);
2786 		}
2787 		else
2788 		{
2789 			return false;
2790 		}
2791 
2792 		if (b)
2793 		{
2794 			return false;
2795 		}
2796 	}
2797 
2798 	return true;
2799 }
2800 
2801 // Setup procedure dialog initialization
SmSetupStepDlgInit(HWND hWnd,SM_SETUP * s)2802 void SmSetupStepDlgInit(HWND hWnd, SM_SETUP *s)
2803 {
2804 	bool b;
2805 	RPC_ENUM_ETH t;
2806 	UINT i;
2807 	RPC_BRIDGE_SUPPORT bs;
2808 	// Validate arguments
2809 	if (hWnd == NULL || s == NULL)
2810 	{
2811 		return;
2812 	}
2813 
2814 	SetIcon(hWnd, 0, ICO_SETUP);
2815 
2816 	DlgFont(hWnd, S_1_1, 0, true);
2817 	DlgFont(hWnd, S_2_1, 0, true);
2818 	DlgFont(hWnd, S_3_1, 0, true);
2819 
2820 	b = false;
2821 	if (s->UseRemote)
2822 	{
2823 		b = true;
2824 	}
2825 	if (s->UseSite && s->UseSiteEdge == false)
2826 	{
2827 		b = true;
2828 	}
2829 
2830 	SetEnable(hWnd, S_1_1, b);
2831 	SetEnable(hWnd, S_1_2, b);
2832 	SetEnable(hWnd, B_USER, b);
2833 
2834 	b = false;
2835 	if (s->UseSiteEdge)
2836 	{
2837 		b = true;
2838 	}
2839 
2840 	SetEnable(hWnd, S_2_1, b);
2841 	SetEnable(hWnd, S_2_2, b);
2842 	SetEnable(hWnd, B_CASCADE, b);
2843 
2844 	CbReset(hWnd, C_DEVICE);
2845 	CbSetHeight(hWnd, C_DEVICE, 18);
2846 
2847 	Zero(&t, sizeof(t));
2848 
2849 	CbAddStr(hWnd, C_DEVICE, _UU("SM_SETUP_SELECT"), 0);
2850 
2851 	Zero(&bs, sizeof(bs));
2852 	if (CALL(hWnd, ScGetBridgeSupport(s->Rpc, &bs)) == false)
2853 	{
2854 		return;
2855 	}
2856 
2857 	if (bs.IsBridgeSupportedOs)
2858 	{
2859 		// Enumerate the local bridges
2860 		if (ScEnumEthernet(s->Rpc, &t) == ERR_NO_ERROR)
2861 		{
2862 			for (i = 0;i < t.NumItem;i++)
2863 			{
2864 				wchar_t tmp[MAX_PATH];
2865 				RPC_ENUM_ETH_ITEM *e = &t.Items[i];
2866 
2867 				if (GetCapsBool(s->s->CapsList, "b_support_network_connection_name"))
2868 				{
2869 					UniFormat(tmp, sizeof(tmp), BRIDGE_NETWORK_CONNECTION_STR, e->NetworkConnectionName, e->DeviceName);
2870 				}
2871 				else
2872 				{
2873 					StrToUni(tmp, sizeof(tmp), e->DeviceName);
2874 				}
2875 
2876 				CbAddStr(hWnd, C_DEVICE, tmp, 1);
2877 			}
2878 
2879 			FreeRpcEnumEth(&t);
2880 		}
2881 		Show(hWnd, C_DEVICE);
2882 		Hide(hWnd, B_SECURENAT);
2883 	}
2884 	else
2885 	{
2886 		RPC_HUB t;
2887 
2888 		// Enable the SecureNAT automatically if the local bridge does not work in this environment
2889 		SetText(hWnd, S_3_2, _UU("SM_SETUP_STEP_SECURENAT"));
2890 		SetText(hWnd, S_3_1, _UU("SM_SETUP_STEP_SECURENAT_TITLE"));
2891 		Hide(hWnd, C_DEVICE);
2892 		Show(hWnd, B_SECURENAT);
2893 		SetIcon(hWnd, S_ICON, ICO_ROUTER);
2894 
2895 		Zero(&t, sizeof(t));
2896 		StrCpy(t.HubName, sizeof(t.HubName), s->HubName);
2897 
2898 		ScEnableSecureNAT(s->Rpc, &t);
2899 	}
2900 
2901 	s->Flag1 = false;
2902 	s->Flag2 = false;
2903 }
2904 
2905 // Close
SmSetupOnClose(HWND hWnd,SM_SETUP * s)2906 void SmSetupOnClose(HWND hWnd, SM_SETUP *s)
2907 {
2908 	wchar_t *tmp;
2909 	char name[MAX_PATH];
2910 	RPC_BRIDGE_SUPPORT bs;
2911 	// Validate arguments
2912 	if (hWnd == NULL || s == NULL)
2913 	{
2914 		return;
2915 	}
2916 
2917 	Zero(&bs, sizeof(bs));
2918 	if (CALL(hWnd, ScGetBridgeSupport(s->Rpc, &bs)) == false)
2919 	{
2920 		return;
2921 	}
2922 
2923 	if (bs.IsBridgeSupportedOs)
2924 	{
2925 		// Add a Local Bridge
2926 		tmp = CbGetStr(hWnd, C_DEVICE);
2927 
2928 		if (tmp != NULL)
2929 		{
2930 			UniToStr(name, sizeof(name), tmp);
2931 
2932 			if (CbGetSelect(hWnd, C_DEVICE) != 0)
2933 			{
2934 				// Show a warning message if the VPN Server is running in a VM
2935 				if (GetCapsBool(s->s->CapsList, "b_is_in_vm"))
2936 				{
2937 					Dialog(hWnd, D_SM_VMBRIDGE, SmVmBridgeDlg, NULL);
2938 				}
2939 
2940 				if (GetCapsBool(s->s->CapsList, "b_support_network_connection_name") == false)
2941 				{
2942 					RPC_LOCALBRIDGE t;
2943 
2944 					Zero(&t, sizeof(t));
2945 					t.Active = true;
2946 					StrCpy(t.DeviceName, sizeof(t.DeviceName), name);
2947 					StrCpy(t.HubName, sizeof(t.HubName), s->HubName);
2948 					t.Online = true;
2949 					t.TapMode = false;
2950 
2951 					if (CALL(hWnd, ScAddLocalBridge(s->Rpc, &t)) == false)
2952 					{
2953 						Free(tmp);
2954 						return;
2955 					}
2956 				}
2957 				else
2958 				{
2959 					RPC_ENUM_ETH tt;
2960 					UINT i;
2961 
2962 					Zero(&tt, sizeof(tt));
2963 					if (CALL(hWnd, ScEnumEthernet(s->Rpc, &tt)) == false)
2964 					{
2965 						Free(tmp);
2966 						return;
2967 					}
2968 
2969 					for (i = 0;i < tt.NumItem;i++)
2970 					{
2971 						RPC_ENUM_ETH_ITEM *ti = &tt.Items[i];
2972 						wchar_t fullname[MAX_SIZE];
2973 
2974 						UniFormat(fullname, sizeof(fullname), BRIDGE_NETWORK_CONNECTION_STR, ti->NetworkConnectionName, ti->DeviceName);
2975 
2976 						if (UniStrCmpi(fullname, tmp) == 0)
2977 						{
2978 							RPC_LOCALBRIDGE t;
2979 
2980 							Zero(&t, sizeof(t));
2981 							t.Active = true;
2982 							StrCpy(t.DeviceName, sizeof(t.DeviceName), ti->DeviceName);
2983 							StrCpy(t.HubName, sizeof(t.HubName), s->HubName);
2984 							t.Online = true;
2985 							t.TapMode = false;
2986 
2987 							if (CALL(hWnd, ScAddLocalBridge(s->Rpc, &t)) == false)
2988 							{
2989 								FreeRpcEnumEth(&tt);
2990 								Free(tmp);
2991 								return;
2992 							}
2993 							break;
2994 						}
2995 					}
2996 
2997 					FreeRpcEnumEth(&tt);
2998 				}
2999 
3000 			}
3001 			Free(tmp);
3002 		}
3003 	}
3004 	else
3005 	{
3006 		// Enable the SecureNAT
3007 	}
3008 
3009 	EndDialog(hWnd, 0);
3010 }
3011 
3012 // Setup Procedure dialog procedure
SmSetupStepDlg(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)3013 UINT SmSetupStepDlg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
3014 {
3015 	SM_SETUP *s = (SM_SETUP *)param;
3016 	// Validate arguments
3017 	if (hWnd == NULL)
3018 	{
3019 		return 0;
3020 	}
3021 
3022 	switch (msg)
3023 	{
3024 	case WM_INITDIALOG:
3025 		// Initialize
3026 		SmSetupStepDlgInit(hWnd, s);
3027 		break;
3028 
3029 	case WM_COMMAND:
3030 		switch (wParam)
3031 		{
3032 		case B_USER:
3033 			// User creation
3034 			if (true)
3035 			{
3036 				SM_HUB h;
3037 
3038 				Zero(&h, sizeof(h));
3039 				h.HubName = s->HubName;
3040 				h.p = s->s;
3041 				h.Rpc = s->Rpc;
3042 
3043 				SmUserListDlgEx(hWnd, &h, NULL, s->Flag1 ? false : true);
3044 
3045 				s->Flag1 = true;
3046 			}
3047 			break;
3048 
3049 		case B_CASCADE:
3050 			// Create a cascade connection
3051 			if (true)
3052 			{
3053 				SM_HUB h;
3054 
3055 				Zero(&h, sizeof(h));
3056 				h.HubName = s->HubName;
3057 				h.p = s->s;
3058 				h.Rpc = s->Rpc;
3059 
3060 				SmLinkDlgEx(hWnd, &h, s->Flag2 ? false : true);
3061 				s->Flag2 = true;
3062 			}
3063 			break;
3064 
3065 		case B_SECURENAT:
3066 			// Setting the SecureNAT
3067 			if (true)
3068 			{
3069 				SM_HUB h;
3070 
3071 				Zero(&h, sizeof(h));
3072 				h.p = s->s;
3073 				h.Rpc = s->Rpc;
3074 				h.HubName = s->HubName;
3075 
3076 				Dialog(hWnd, D_SM_SNAT, SmSNATDlgProc, &h);
3077 			}
3078 			break;
3079 
3080 		case IDCANCEL:
3081 			// Close button
3082 			Close(hWnd);
3083 			break;
3084 		}
3085 		break;
3086 
3087 	case WM_CLOSE:
3088 		// Exit
3089 		SmSetupOnClose(hWnd, s);
3090 		break;
3091 	}
3092 
3093 	return 0;
3094 }
3095 
3096 // Setup procedure dialog
SmSetupStep(HWND hWnd,SM_SETUP * s)3097 void SmSetupStep(HWND hWnd, SM_SETUP *s)
3098 {
3099 	// Validate arguments
3100 	if (s == NULL)
3101 	{
3102 		return;
3103 	}
3104 
3105 	Dialog(hWnd, D_SM_SETUP_STEP, SmSetupStepDlg, s);
3106 }
3107 
3108 // Initialize by setup
SmSetupInit(HWND hWnd,SM_SETUP * s)3109 bool SmSetupInit(HWND hWnd, SM_SETUP *s)
3110 {
3111 	// Validate arguments
3112 	if (s == NULL)
3113 	{
3114 		return false;
3115 	}
3116 
3117 	if (s->IsBridge == false)
3118 	{
3119 		if (SmSetupDeleteAllLayer3(hWnd, s) == false)
3120 		{
3121 			return false;
3122 		}
3123 
3124 		if (SmSetupDeleteAllHub(hWnd, s) == false)
3125 		{
3126 			return false;
3127 		}
3128 	}
3129 	else
3130 	{
3131 		if (SmSetupDeleteAllObjectInBridgeHub(hWnd, s) == false)
3132 		{
3133 			return false;
3134 		}
3135 	}
3136 
3137 	SmSetupDeleteAllLocalBridge(hWnd, s);
3138 
3139 	if (s->IsBridge == false)
3140 	{
3141 		// Create a Virtual HUB
3142 		RPC_CREATE_HUB t;
3143 		char *password = "";
3144 
3145 		Zero(&t, sizeof(t));
3146 		Hash(t.HashedPassword, password, StrLen(password), true);
3147 		HashPassword(t.SecurePassword, ADMINISTRATOR_USERNAME, password);
3148 		StrCpy(t.HubName, sizeof(t.HubName), s->HubName);
3149 		t.HubType = HUB_TYPE_STANDALONE;
3150 		t.Online = true;
3151 
3152 		if (CALL(hWnd, ScCreateHub(s->Rpc, &t)) == false)
3153 		{
3154 			return false;
3155 		}
3156 	}
3157 
3158 	return true;
3159 }
3160 
3161 // Remove all objects in the Virtual HUB of the VPN Bridge
SmSetupDeleteAllObjectInBridgeHub(HWND hWnd,SM_SETUP * s)3162 bool SmSetupDeleteAllObjectInBridgeHub(HWND hWnd, SM_SETUP *s)
3163 {
3164 	char *hubname = SERVER_DEFAULT_BRIDGE_NAME;
3165 	// Validate arguments
3166 	if (hWnd == NULL || s == NULL)
3167 	{
3168 		return false;
3169 	}
3170 
3171 	if (true)
3172 	{
3173 		RPC_ENUM_LINK t;
3174 		UINT i;
3175 
3176 		Zero(&t, sizeof(t));
3177 		StrCpy(t.HubName, sizeof(t.HubName), hubname);
3178 
3179 		if (CALL(hWnd, ScEnumLink(s->Rpc, &t)) == false)
3180 		{
3181 			return false;
3182 		}
3183 
3184 		for (i = 0;i < t.NumLink;i++)
3185 		{
3186 			RPC_ENUM_LINK_ITEM *e = &t.Links[i];
3187 			RPC_LINK a;
3188 
3189 			Zero(&a, sizeof(a));
3190 			StrCpy(a.HubName, sizeof(a.HubName), hubname);
3191 			UniStrCpy(a.AccountName, sizeof(a.AccountName), e->AccountName);
3192 
3193 			if (CALL(hWnd, ScDeleteLink(s->Rpc, &a)) == false)
3194 			{
3195 				FreeRpcEnumLink(&t);
3196 				return false;
3197 			}
3198 		}
3199 
3200 		FreeRpcEnumLink(&t);
3201 	}
3202 
3203 	if (true)
3204 	{
3205 		RPC_HUB t;
3206 
3207 		Zero(&t, sizeof(t));
3208 		StrCpy(t.HubName, sizeof(t.HubName), hubname);
3209 
3210 		if (CALL(hWnd, ScDisableSecureNAT(s->Rpc, &t)) == false)
3211 		{
3212 			return false;
3213 		}
3214 	}
3215 
3216 	return true;
3217 }
3218 
3219 // Delete all Virtual Layer 3 Switches
SmSetupDeleteAllLayer3(HWND hWnd,SM_SETUP * s)3220 bool SmSetupDeleteAllLayer3(HWND hWnd, SM_SETUP *s)
3221 {
3222 	RPC_ENUM_L3SW t;
3223 	UINT i;
3224 	// Validate arguments
3225 	if (hWnd == NULL || s == NULL)
3226 	{
3227 		return false;
3228 	}
3229 
3230 	Zero(&t, sizeof(t));
3231 	if(CALL(hWnd, ScEnumL3Switch(s->Rpc, &t)) == false)
3232 	{
3233 		return false;
3234 	}
3235 
3236 	for (i = 0;i < t.NumItem;i++)
3237 	{
3238 		RPC_ENUM_L3SW_ITEM *e = &t.Items[i];
3239 		RPC_L3SW tt;
3240 
3241 		Zero(&tt, sizeof(tt));
3242 		StrCpy(tt.Name, sizeof(tt.Name), e->Name);
3243 
3244 		if (CALL(hWnd, ScDelL3Switch(s->Rpc, &tt)) == false)
3245 		{
3246 			FreeRpcEnumL3Sw(&t);
3247 			return false;
3248 		}
3249 	}
3250 
3251 	FreeRpcEnumL3Sw(&t);
3252 
3253 	return true;
3254 }
3255 
3256 // Delete all local bridges
SmSetupDeleteAllLocalBridge(HWND hWnd,SM_SETUP * s)3257 bool SmSetupDeleteAllLocalBridge(HWND hWnd, SM_SETUP *s)
3258 {
3259 	RPC_ENUM_LOCALBRIDGE t;
3260 	UINT i;
3261 	// Validate arguments
3262 	if (hWnd == NULL || s == NULL)
3263 	{
3264 		return false;
3265 	}
3266 
3267 	Zero(&t, sizeof(t));
3268 	if (ScEnumLocalBridge(s->Rpc, &t) != ERR_NO_ERROR)
3269 	{
3270 		return false;
3271 	}
3272 
3273 	for (i = 0;i < t.NumItem;i++)
3274 	{
3275 		RPC_LOCALBRIDGE *e = &t.Items[i];
3276 
3277 		if (CALL(hWnd, ScDeleteLocalBridge(s->Rpc, e)) == false)
3278 		{
3279 			FreeRpcEnumLocalBridge(&t);
3280 			return false;
3281 		}
3282 	}
3283 
3284 	FreeRpcEnumLocalBridge(&t);
3285 
3286 	return true;
3287 }
3288 
3289 // Delete all Virtual HUBs
SmSetupDeleteAllHub(HWND hWnd,SM_SETUP * s)3290 bool SmSetupDeleteAllHub(HWND hWnd, SM_SETUP *s)
3291 {
3292 	RPC_ENUM_HUB t;
3293 	UINT i;
3294 	// Validate arguments
3295 	if (hWnd == NULL || s == NULL)
3296 	{
3297 		return false;
3298 	}
3299 
3300 	Zero(&t, sizeof(t));
3301 	if (CALL(hWnd, ScEnumHub(s->Rpc, &t)) == false)
3302 	{
3303 		return false;
3304 	}
3305 
3306 	for (i = 0;i < t.NumHub;i++)
3307 	{
3308 		RPC_ENUM_HUB_ITEM *e = &t.Hubs[i];
3309 		RPC_DELETE_HUB tt;
3310 
3311 		Zero(&tt, sizeof(tt));
3312 		StrCpy(tt.HubName, sizeof(tt.HubName), e->HubName);
3313 
3314 		if (CALL(hWnd, ScDeleteHub(s->Rpc, &tt)) == false)
3315 		{
3316 			FreeRpcEnumHub(&t);
3317 			return false;
3318 		}
3319 	}
3320 
3321 	FreeRpcEnumHub(&t);
3322 
3323 	return true;
3324 }
3325 
3326 // Update the control of the Virtual HUB
SmSetupHubDlgUpdate(HWND hWnd,SM_SETUP * s)3327 void SmSetupHubDlgUpdate(HWND hWnd, SM_SETUP *s)
3328 {
3329 	bool ok = true;
3330 	char tmp[MAX_HUBNAME_LEN + 1];
3331 	// Validate arguments
3332 	if (hWnd == NULL || s == NULL)
3333 	{
3334 		return;
3335 	}
3336 
3337 	GetTxtA(hWnd, E_HUBNAME, tmp, sizeof(tmp));
3338 
3339 	if (IsEmptyStr(tmp) || IsSafeStr(tmp) == false)
3340 	{
3341 		ok = false;
3342 	}
3343 
3344 	SetEnable(hWnd, IDOK, ok);
3345 }
3346 
3347 // Virtual HUB creation dialog
SmSetupHubDlg(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)3348 UINT SmSetupHubDlg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
3349 {
3350 	SM_SETUP *s = (SM_SETUP *)param;
3351 	// Validate arguments
3352 	if (hWnd == NULL)
3353 	{
3354 		return 0;
3355 	}
3356 
3357 	switch (msg)
3358 	{
3359 	case WM_INITDIALOG:
3360 		SetTextA(hWnd, E_HUBNAME, "VPN");
3361 		FocusEx(hWnd, E_HUBNAME);
3362 		SmSetupHubDlgUpdate(hWnd, s);
3363 		break;
3364 
3365 	case WM_COMMAND:
3366 		SmSetupHubDlgUpdate(hWnd, s);
3367 
3368 		switch (wParam)
3369 		{
3370 		case IDOK:
3371 			GetTxtA(hWnd, E_HUBNAME, s->HubName, sizeof(s->HubName));
3372 			EndDialog(hWnd, true);
3373 			break;
3374 
3375 		case IDCANCEL:
3376 			Close(hWnd);
3377 			break;
3378 		}
3379 		break;
3380 
3381 	case WM_CLOSE:
3382 		EndDialog(hWnd, 0);
3383 		break;
3384 	}
3385 
3386 	return 0;
3387 }
3388 
3389 // Setup dialog: [Next] button
SmSetupDlgOnOk(HWND hWnd,SM_SETUP * s)3390 void SmSetupDlgOnOk(HWND hWnd, SM_SETUP *s)
3391 {
3392 	// Validate arguments
3393 	if (hWnd == NULL || s == NULL)
3394 	{
3395 		return;
3396 	}
3397 
3398 	if (MsgBox(hWnd, MB_YESNO | MB_ICONEXCLAMATION, _UU("SM_SETUP_WARNING")) == IDNO)
3399 	{
3400 		return;
3401 	}
3402 
3403 	s->UseRemote = IsChecked(hWnd, C_REMOTE);
3404 	s->UseSite = IsChecked(hWnd, C_SITE);
3405 	s->UseSiteEdge = IsChecked(hWnd, C_EDGE);
3406 
3407 	if (s->IsBridge)
3408 	{
3409 		StrCpy(s->HubName, sizeof(s->HubName), SERVER_DEFAULT_BRIDGE_NAME);
3410 	}
3411 	else
3412 	{
3413 		if (Dialog(hWnd, D_SM_SETUP_HUB, SmSetupHubDlg, s) == false)
3414 		{
3415 			return;
3416 		}
3417 	}
3418 
3419 	// Initialize (Wipe existing objects)
3420 	if (SmSetupInit(hWnd, s) == false)
3421 	{
3422 		return;
3423 	}
3424 
3425 	if (s->IsBridge == false)
3426 	{
3427 		if (GetCapsBool(s->s->CapsList, "b_support_ddns"))
3428 		{
3429 			if (s->UseRemote || (s->UseSite && s->UseSiteEdge == false))
3430 			{
3431 				DDNS_CLIENT_STATUS st;
3432 
3433 				Zero(&st, sizeof(st));
3434 
3435 				if (ScGetDDnsClientStatus(s->s->Rpc, &st) == ERR_NO_ERROR &&
3436 					IsEmptyStr(st.CurrentHostName) == false)
3437 				{
3438 					// Display the Dynamic DNS setting screen
3439 					SmDDns(hWnd, s->s, true, false);
3440 				}
3441 			}
3442 		}
3443 
3444 		// Configure the IPsec if the IPsec feature is available
3445 		if (GetCapsBool(s->s->CapsList, "b_support_ipsec") && s->s->IPsecMessageDisplayed == false)
3446 		{
3447 			// Display a message about IPsec
3448 			RPC_TEST flag;
3449 
3450 			if (s->UseRemote || (s->UseSite && s->UseSiteEdge == false))
3451 			{
3452 				SmIPsec(hWnd, s->s);
3453 			}
3454 
3455 			Zero(&flag, sizeof(flag));
3456 			flag.IntValue = 9;
3457 			ToStr(flag.StrValue, 1);
3458 
3459 			ScDebug(s->s->Rpc, &flag);
3460 
3461 			s->s->IPsecMessageDisplayed = true;
3462 		}
3463 
3464 		// Confgure the VPN Azure if VPN Azure feature is available
3465 		if (GetCapsBool(s->s->CapsList, "b_support_azure"))
3466 		{
3467 			SmAzure(hWnd, s->s, true);
3468 		}
3469 
3470 	}
3471 
3472 	// Execute the procedure
3473 	SmSetupStep(hWnd, s);
3474 
3475 	// Close the dialog
3476 	EndDialog(hWnd, true);
3477 }
3478 
3479 // Setup dialog: initialization
SmSetupDlgInit(HWND hWnd,SM_SETUP * s)3480 void SmSetupDlgInit(HWND hWnd, SM_SETUP *s)
3481 {
3482 	// Validate arguments
3483 	if (hWnd == NULL || s == NULL)
3484 	{
3485 		return;
3486 	}
3487 
3488 	SetIcon(hWnd, 0, ICO_SETUP);
3489 	DlgFont(hWnd, S_TITLE, 14, true);
3490 	DlgFont(hWnd, C_REMOTE, 0, true);
3491 	DlgFont(hWnd, C_SITE, 0, true);
3492 	DlgFont(hWnd, C_OTHER, 0, true);
3493 
3494 	if (s->IsBridge)
3495 	{
3496 		SetText(hWnd, B_BOLD, _UU("SM_SETUP_BRIDGE_ONLY"));
3497 		SetText(hWnd, C_EDGE, _UU("SM_SETUP_BRIDGE_EDGE"));
3498 
3499 		Check(hWnd, C_SITE, true);
3500 		Check(hWnd, C_EDGE, true);
3501 		Focus(hWnd, C_SITE);
3502 	}
3503 
3504 	SmSetupDlgUpdate(hWnd, s);
3505 }
3506 
3507 // Setup dialog: update
SmSetupDlgUpdate(HWND hWnd,SM_SETUP * s)3508 void SmSetupDlgUpdate(HWND hWnd, SM_SETUP *s)
3509 {
3510 	bool enable_remote = true;
3511 	bool enable_site = true;
3512 	bool enable_site_center = true;
3513 	bool enable_detail = true;
3514 	bool ok = true;
3515 	// Validate arguments
3516 	if (hWnd == NULL || s == NULL)
3517 	{
3518 		return;
3519 	}
3520 
3521 	if (s->IsBridge)
3522 	{
3523 		enable_remote = false;
3524 		enable_site_center = false;
3525 		enable_detail = false;
3526 	}
3527 
3528 	if (IsChecked(hWnd, C_OTHER))
3529 	{
3530 		ok = false;
3531 	}
3532 
3533 	SetEnable(hWnd, C_REMOTE, enable_remote && IsChecked(hWnd, C_OTHER) == false);
3534 	SetEnable(hWnd, S_REMOTE_1, enable_remote && IsChecked(hWnd, C_OTHER) == false);
3535 
3536 	SetEnable(hWnd, C_SITE, enable_site && IsChecked(hWnd, C_OTHER) == false);
3537 	SetEnable(hWnd, S_SITE_1, enable_site && IsChecked(hWnd, C_OTHER) == false);
3538 	SetEnable(hWnd, S_SITE_2, enable_site && IsChecked(hWnd, C_SITE) && IsChecked(hWnd, C_OTHER) == false);
3539 	SetEnable(hWnd, C_CENTER, enable_site && enable_site_center && IsChecked(hWnd, C_SITE) && IsChecked(hWnd, C_OTHER) == false);
3540 	SetEnable(hWnd, C_EDGE, enable_site && IsChecked(hWnd, C_SITE) && IsChecked(hWnd, C_OTHER) == false);
3541 
3542 	SetEnable(hWnd, C_OTHER, enable_detail);
3543 	SetEnable(hWnd, S_OTHER, enable_detail);
3544 
3545 	if (IsChecked(hWnd, C_REMOTE) == false && IsChecked(hWnd, C_SITE) == false)
3546 	{
3547 		ok = false;
3548 	}
3549 
3550 	if (IsChecked(hWnd, C_SITE))
3551 	{
3552 		if (IsChecked(hWnd, C_CENTER) == false && IsChecked(hWnd, C_EDGE) == false)
3553 		{
3554 			ok = false;
3555 		}
3556 	}
3557 
3558 	SetEnable(hWnd, IDOK, ok);
3559 
3560 	SetText(hWnd, S_INFO,
3561 		IsChecked(hWnd, C_OTHER) ? _UU("SM_SETUP_INFO_2") : _UU("SM_SETUP_INFO_1"));
3562 }
3563 
3564 // Setup dialog
SmSetupDlg(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)3565 UINT SmSetupDlg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
3566 {
3567 	SM_SETUP *s = (SM_SETUP *)param;
3568 	// Validate arguments
3569 	if (hWnd == NULL)
3570 	{
3571 		return 0;
3572 	}
3573 
3574 	switch (msg)
3575 	{
3576 	case WM_INITDIALOG:
3577 		SmSetupDlgInit(hWnd, s);
3578 		break;
3579 
3580 	case WM_COMMAND:
3581 		SmSetupDlgUpdate(hWnd, s);
3582 
3583 		switch (wParam)
3584 		{
3585 		case IDOK:
3586 			SmSetupDlgOnOk(hWnd, s);
3587 			break;
3588 
3589 		case IDCANCEL:
3590 			Close(hWnd);
3591 			break;
3592 		}
3593 		break;
3594 
3595 	case WM_CLOSE:
3596 		EndDialog(hWnd, 0);
3597 		break;
3598 	}
3599 
3600 	return 0;
3601 }
3602 
3603 // Setup
SmSetup(HWND hWnd,SM_SERVER * s)3604 bool SmSetup(HWND hWnd, SM_SERVER *s)
3605 {
3606 	SM_SETUP ss;
3607 	// Validate arguments
3608 	if (s == NULL)
3609 	{
3610 		return false;
3611 	}
3612 
3613 	Zero(&ss, sizeof(ss));
3614 	ss.s = s;
3615 	ss.IsBridge = ss.s->Bridge;
3616 	ss.Rpc = s->Rpc;
3617 
3618 	if (Dialog(hWnd, D_SM_SETUP, SmSetupDlg, &ss) == false)
3619 	{
3620 		return false;
3621 	}
3622 
3623 	return true;
3624 }
3625 
3626 // License registration process
SmLicenseAddDlgOnOk(HWND hWnd,SM_SERVER * s)3627 void SmLicenseAddDlgOnOk(HWND hWnd, SM_SERVER *s)
3628 {
3629 	char tmp[MAX_SIZE];
3630 	// Validate arguments
3631 	if (hWnd == NULL || s == NULL)
3632 	{
3633 		return;
3634 	}
3635 
3636 	SmLicenseAddDlgGetText(hWnd, tmp, sizeof(tmp));
3637 
3638 	if (LiIsLicenseKey(tmp))
3639 	{
3640 		RPC_TEST t;
3641 
3642 		if (s->LicenseWarnFlag == false)
3643 		{
3644 			if (MsgBoxEx(hWnd, MB_ICONINFORMATION | MB_OKCANCEL, _UU("SM_LICENSE_WARNING")) == IDCANCEL)
3645 			{
3646 				return;
3647 			}
3648 
3649 			s->LicenseWarnFlag = true;
3650 		}
3651 
3652 		Disable(hWnd, IDOK);
3653 		Disable(hWnd, IDCANCEL);
3654 
3655 		Zero(&t, sizeof(t));
3656 		StrCpy(t.StrValue, sizeof(t.StrValue), tmp);
3657 
3658 		if (CALL(hWnd, ScAddLicenseKey(s->Rpc, &t)) == false)
3659 		{
3660 			FocusEx(hWnd, B_KEY6);
3661 		}
3662 		else
3663 		{
3664 			EndDialog(hWnd, true);
3665 		}
3666 
3667 		Enable(hWnd, IDOK);
3668 		Enable(hWnd, IDCANCEL);
3669 	}
3670 }
3671 
3672 // Shift treatment of text input
SmLicenseAddDlgShiftTextItem(HWND hWnd,UINT id1,UINT id2,UINT * next_focus)3673 void SmLicenseAddDlgShiftTextItem(HWND hWnd, UINT id1, UINT id2, UINT *next_focus)
3674 {
3675 	char *s;
3676 	// Validate arguments
3677 	if (hWnd == NULL || next_focus == NULL)
3678 	{
3679 		return;
3680 	}
3681 
3682 	s = GetTextA(hWnd, id1);
3683 	if (StrLen(s) >= 6)
3684 	{
3685 		char *s2 = CopyStr(s);
3686 		char tmp[MAX_SIZE];
3687 		s2[6] = 0;
3688 		SetTextA(hWnd, id1, s2);
3689 		Free(s2);
3690 
3691 		if (id2 != 0)
3692 		{
3693 			GetTxtA(hWnd, id2, tmp, sizeof(tmp));
3694 
3695 			StrCat(tmp, sizeof(tmp), s + 6);
3696 			ReplaceStrEx(tmp, sizeof(tmp), tmp, "-", "", false);
3697 
3698 			SetTextA(hWnd, id2, tmp);
3699 
3700 			*next_focus = id2;
3701 		}
3702 		else
3703 		{
3704 			*next_focus = IDOK;
3705 		}
3706 	}
3707 
3708 	Free(s);
3709 }
3710 
3711 // Make a text from the input data
SmLicenseAddDlgGetText(HWND hWnd,char * str,UINT size)3712 void SmLicenseAddDlgGetText(HWND hWnd, char *str, UINT size)
3713 {
3714 	char *k1, *k2, *k3, *k4, *k5, *k6;
3715 	// Validate arguments
3716 	if (hWnd == NULL || str == NULL)
3717 	{
3718 		return;
3719 	}
3720 
3721 	k1 = GetTextA(hWnd, B_KEY1);
3722 	k2 = GetTextA(hWnd, B_KEY2);
3723 	k3 = GetTextA(hWnd, B_KEY3);
3724 	k4 = GetTextA(hWnd, B_KEY4);
3725 	k5 = GetTextA(hWnd, B_KEY5);
3726 	k6 = GetTextA(hWnd, B_KEY6);
3727 
3728 	Format(str, size, "%s-%s-%s-%s-%s-%s", k1, k2, k3, k4, k5, k6);
3729 
3730 	Free(k1);
3731 	Free(k2);
3732 	Free(k3);
3733 	Free(k4);
3734 	Free(k5);
3735 	Free(k6);
3736 }
3737 
3738 // License addition dialog update
SmLicenseAddDlgUpdate(HWND hWnd,SM_SERVER * s)3739 void SmLicenseAddDlgUpdate(HWND hWnd, SM_SERVER *s)
3740 {
3741 	UINT next_focus = 0;
3742 	char tmp[MAX_SIZE];
3743 	// Validate arguments
3744 	if (s == NULL || hWnd == NULL)
3745 	{
3746 		return;
3747 	}
3748 
3749 	SmLicenseAddDlgShiftTextItem(hWnd, B_KEY1, B_KEY2, &next_focus);
3750 	SmLicenseAddDlgShiftTextItem(hWnd, B_KEY2, B_KEY3, &next_focus);
3751 	SmLicenseAddDlgShiftTextItem(hWnd, B_KEY3, B_KEY4, &next_focus);
3752 	SmLicenseAddDlgShiftTextItem(hWnd, B_KEY4, B_KEY5, &next_focus);
3753 	SmLicenseAddDlgShiftTextItem(hWnd, B_KEY5, B_KEY6, &next_focus);
3754 	SmLicenseAddDlgShiftTextItem(hWnd, B_KEY6, 0, &next_focus);
3755 
3756 	if ((IsFocus(hWnd, B_KEY1) && GetTextLen(hWnd, B_KEY1, true) <= 5) ||
3757 		(IsFocus(hWnd, B_KEY2) && GetTextLen(hWnd, B_KEY2, true) <= 5) ||
3758 		(IsFocus(hWnd, B_KEY3) && GetTextLen(hWnd, B_KEY3, true) <= 5) ||
3759 		(IsFocus(hWnd, B_KEY4) && GetTextLen(hWnd, B_KEY4, true) <= 5) ||
3760 		(IsFocus(hWnd, B_KEY5) && GetTextLen(hWnd, B_KEY5, true) <= 5) ||
3761 		(IsFocus(hWnd, B_KEY6) && GetTextLen(hWnd, B_KEY6, true) <= 5))
3762 	{
3763 		next_focus = 0;
3764 	}
3765 
3766 	if (next_focus != 0)
3767 	{
3768 		Focus(hWnd, next_focus);
3769 	}
3770 
3771 	SmLicenseAddDlgGetText(hWnd, tmp, sizeof(tmp));
3772 
3773 	SetEnable(hWnd, IDOK, LiIsLicenseKey(tmp));
3774 }
3775 
3776 // License addition dialog initialization
SmLicenseAddDlgInit(HWND hWnd,SM_SERVER * s)3777 void SmLicenseAddDlgInit(HWND hWnd, SM_SERVER *s)
3778 {
3779 	HFONT h;
3780 	// Validate arguments
3781 	if (hWnd == NULL || s == NULL)
3782 	{
3783 		return;
3784 	}
3785 
3786 	h = GetFont("Arial", 10, true, false, false, false);
3787 	SetFont(hWnd, B_KEY1, h);
3788 	SetFont(hWnd, B_KEY2, h);
3789 	SetFont(hWnd, B_KEY3, h);
3790 	SetFont(hWnd, B_KEY4, h);
3791 	SetFont(hWnd, B_KEY5, h);
3792 	SetFont(hWnd, B_KEY6, h);
3793 
3794 	DlgFont(hWnd, S_INFO, 10, true);
3795 
3796 	SmLicenseAddDlgUpdate(hWnd, s);
3797 }
3798 
3799 // License addition dialog
SmLicenseAddDlg(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)3800 UINT SmLicenseAddDlg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
3801 {
3802 	SM_SERVER *s = (SM_SERVER *)param;
3803 	// Validate arguments
3804 	if (hWnd == NULL)
3805 	{
3806 		return 0;
3807 	}
3808 
3809 	switch (msg)
3810 	{
3811 	case WM_INITDIALOG:
3812 		SmLicenseAddDlgInit(hWnd, s);
3813 		break;
3814 
3815 	case WM_COMMAND:
3816 		switch (LOWORD(wParam))
3817 		{
3818 		case B_KEY1:
3819 		case B_KEY2:
3820 		case B_KEY3:
3821 		case B_KEY4:
3822 		case B_KEY5:
3823 		case B_KEY6:
3824 			switch (HIWORD(wParam))
3825 			{
3826 			case EN_CHANGE:
3827 				SmLicenseAddDlgUpdate(hWnd, s);
3828 
3829 				switch (LOWORD(wParam))
3830 				{
3831 				case B_KEY2:
3832 					if (GetTextLen(hWnd, B_KEY2, true) == 0)
3833 					{
3834 						FocusEx(hWnd, B_KEY1);
3835 					}
3836 					break;
3837 				case B_KEY3:
3838 					if (GetTextLen(hWnd, B_KEY3, true) == 0)
3839 					{
3840 						FocusEx(hWnd, B_KEY2);
3841 					}
3842 					break;
3843 				case B_KEY4:
3844 					if (GetTextLen(hWnd, B_KEY4, true) == 0)
3845 					{
3846 						FocusEx(hWnd, B_KEY3);
3847 					}
3848 					break;
3849 				case B_KEY5:
3850 					if (GetTextLen(hWnd, B_KEY5, true) == 0)
3851 					{
3852 						FocusEx(hWnd, B_KEY4);
3853 					}
3854 					break;
3855 				case B_KEY6:
3856 					if (GetTextLen(hWnd, B_KEY6, true) == 0)
3857 					{
3858 						FocusEx(hWnd, B_KEY5);
3859 					}
3860 					break;
3861 				}
3862 				break;
3863 			}
3864 			break;
3865 		}
3866 
3867 		switch (wParam)
3868 		{
3869 		case IDOK:
3870 			SmLicenseAddDlgOnOk(hWnd, s);
3871 			break;
3872 
3873 		case IDCANCEL:
3874 			Close(hWnd);
3875 			break;
3876 		}
3877 		break;
3878 
3879 	case WM_CLOSE:
3880 		EndDialog(hWnd, 0);
3881 		break;
3882 	}
3883 
3884 	return 0;
3885 }
3886 
3887 // Add a license
SmLicenseAdd(HWND hWnd,SM_SERVER * s)3888 bool SmLicenseAdd(HWND hWnd, SM_SERVER *s)
3889 {
3890 	// Validate arguments
3891 	if (s == NULL)
3892 	{
3893 		return false;
3894 	}
3895 
3896 	return Dialog(hWnd, D_SM_LICENSE_ADD, SmLicenseAddDlg, s);
3897 }
3898 
3899 // License dialog initialization
SmLicenseDlgInit(HWND hWnd,SM_SERVER * s)3900 void SmLicenseDlgInit(HWND hWnd, SM_SERVER *s)
3901 {
3902 	// Validate arguments
3903 	if (hWnd == NULL || s == NULL)
3904 	{
3905 		return;
3906 	}
3907 
3908 	SetIcon(hWnd, 0, ICO_CERT);
3909 
3910 	DlgFont(hWnd, S_BOLD, 0, true);
3911 	DlgFont(hWnd, S_BOLD2, 0, true);
3912 
3913 	LvInit(hWnd, L_LIST);
3914 	LvSetStyle(hWnd, L_LIST, LVS_EX_GRIDLINES);
3915 	LvInsertColumn(hWnd, L_LIST, 0, _UU("SM_LICENSE_COLUMN_1"), 50);
3916 	LvInsertColumn(hWnd, L_LIST, 1, _UU("SM_LICENSE_COLUMN_2"), 100);
3917 	LvInsertColumn(hWnd, L_LIST, 2, _UU("SM_LICENSE_COLUMN_3"), 290);
3918 	LvInsertColumn(hWnd, L_LIST, 3, _UU("SM_LICENSE_COLUMN_4"), 150);
3919 	LvInsertColumn(hWnd, L_LIST, 4, _UU("SM_LICENSE_COLUMN_5"), 120);
3920 	LvInsertColumn(hWnd, L_LIST, 5, _UU("SM_LICENSE_COLUMN_6"), 250);
3921 	LvInsertColumn(hWnd, L_LIST, 6, _UU("SM_LICENSE_COLUMN_7"), 100);
3922 	LvInsertColumn(hWnd, L_LIST, 7, _UU("SM_LICENSE_COLUMN_8"), 100);
3923 	LvInsertColumn(hWnd, L_LIST, 8, _UU("SM_LICENSE_COLUMN_9"), 100);
3924 
3925 	LvInitEx(hWnd, L_STATUS, true);
3926 	LvInsertColumn(hWnd, L_STATUS, 0, _UU("SM_STATUS_COLUMN_1"), 100);
3927 	LvInsertColumn(hWnd, L_STATUS, 1, _UU("SM_STATUS_COLUMN_2"), 100);
3928 
3929 	SmLicenseDlgRefresh(hWnd, s);
3930 }
3931 
3932 // License dialog update
SmLicenseDlgRefresh(HWND hWnd,SM_SERVER * s)3933 void SmLicenseDlgRefresh(HWND hWnd, SM_SERVER *s)
3934 {
3935 	RPC_ENUM_LICENSE_KEY t;
3936 	RPC_LICENSE_STATUS st;
3937 	UINT i;
3938 	wchar_t tmp[MAX_SIZE];
3939 	LVB *b;
3940 	// Validate arguments
3941 	if (hWnd == NULL || s == NULL)
3942 	{
3943 		return;
3944 	}
3945 
3946 	Zero(&t, sizeof(t));
3947 
3948 	if (CALL(hWnd, ScEnumLicenseKey(s->Rpc, &t)) == false)
3949 	{
3950 		Close(hWnd);
3951 		return;
3952 	}
3953 
3954 	b = LvInsertStart();
3955 
3956 	for (i = 0;i < t.NumItem;i++)
3957 	{
3958 		wchar_t tmp1[32], tmp2[LICENSE_KEYSTR_LEN + 1], tmp3[LICENSE_MAX_PRODUCT_NAME_LEN + 1],
3959 			*tmp4, tmp5[128], tmp6[LICENSE_LICENSEID_STR_LEN + 1], tmp7[64],
3960 			tmp8[64], tmp9[64];
3961 		RPC_ENUM_LICENSE_KEY_ITEM *e = &t.Items[i];
3962 
3963 		UniToStru(tmp1, e->Id);
3964 		StrToUni(tmp2, sizeof(tmp2), e->LicenseKey);
3965 		StrToUni(tmp3, sizeof(tmp3), e->LicenseName);
3966 		tmp4 = LiGetLicenseStatusStr(e->Status);
3967 		if (e->Expires == 0)
3968 		{
3969 			UniStrCpy(tmp5, sizeof(tmp5), _UU("SM_LICENSE_NO_EXPIRES"));
3970 		}
3971 		else
3972 		{
3973 			GetDateStrEx64(tmp5, sizeof(tmp5), e->Expires, NULL);
3974 		}
3975 		StrToUni(tmp6, sizeof(tmp6), e->LicenseId);
3976 		UniToStru(tmp7, e->ProductId);
3977 		UniFormat(tmp8, sizeof(tmp8), L"%I64u", e->SystemId);
3978 		UniToStru(tmp9, e->SerialId);
3979 
3980 		LvInsertAdd(b,
3981 			e->Status == LICENSE_STATUS_OK ? ICO_PASS : ICO_DISCARD,
3982 			(void *)e->Id, 9,
3983 			tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9);
3984 	}
3985 
3986 	LvInsertEnd(b, hWnd, L_LIST);
3987 
3988 	FreeRpcEnumLicenseKey(&t);
3989 
3990 	Zero(&st, sizeof(st));
3991 
3992 	if (CALL(hWnd, ScGetLicenseStatus(s->Rpc, &st)) == false)
3993 	{
3994 		Close(hWnd);
3995 		return;
3996 	}
3997 
3998 	b = LvInsertStart();
3999 
4000 	if (st.EditionId == LICENSE_EDITION_VPN3_NO_LICENSE)
4001 	{
4002 		LvInsertAdd(b, 0, NULL, 2, _UU("SM_NO_LICENSE_COLUMN"), _UU("SM_NO_LICENSE"));
4003 	}
4004 	else
4005 	{
4006 		// Product edition name
4007 		StrToUni(tmp, sizeof(tmp), st.EditionStr);
4008 		LvInsertAdd(b, 0, NULL, 2, _UU("SM_LICENSE_STATUS_EDITION"), tmp);
4009 
4010 		// Release date
4011 		if (st.ReleaseDate != 0)
4012 		{
4013 			GetDateStrEx64(tmp, sizeof(tmp), st.ReleaseDate, NULL);
4014 			LvInsertAdd(b, 0, NULL, 2, _UU("SM_LICENSE_STATUS_RELEASE"), tmp);
4015 		}
4016 
4017 		// Current system ID
4018 		UniFormat(tmp, sizeof(tmp), L"%I64u", st.SystemId);
4019 		LvInsertAdd(b, 0, NULL, 2, _UU("SM_LICENSE_STATUS_SYSTEM_ID"), tmp);
4020 
4021 		// Expiration date of the current product license
4022 		if (st.SystemExpires == 0)
4023 		{
4024 			UniStrCpy(tmp, sizeof(tmp), _UU("SM_LICENSE_NO_EXPIRES"));
4025 		}
4026 		else
4027 		{
4028 			GetDateStrEx64(tmp, sizeof(tmp), st.SystemExpires, NULL);
4029 		}
4030 		LvInsertAdd(b, 0, NULL, 2, _UU("SM_LICENSE_STATUS_EXPIRES"), tmp);
4031 
4032 		// Subscription (support) contract
4033 		if (st.NeedSubscription == false)
4034 		{
4035 			UniStrCpy(tmp, sizeof(tmp), _UU("SM_LICENSE_STATUS_SUBSCRIPTION_NONEED"));
4036 		}
4037 		else
4038 		{
4039 			if (st.SubscriptionExpires == 0)
4040 			{
4041 				UniStrCpy(tmp, sizeof(tmp), _UU("SM_LICENSE_STATUS_SUBSCRIPTION_NONE"));
4042 			}
4043 			else
4044 			{
4045 				wchar_t dtstr[MAX_PATH];
4046 
4047 				GetDateStrEx64(dtstr, sizeof(dtstr), st.SubscriptionExpires, NULL);
4048 
4049 				UniFormat(tmp, sizeof(tmp),
4050 					st.IsSubscriptionExpired ? _UU("SM_LICENSE_STATUS_SUBSCRIPTION_EXPIRED") :  _UU("SM_LICENSE_STATUS_SUBSCRIPTION_VALID"),
4051 					dtstr);
4052 			}
4053 		}
4054 		LvInsertAdd(b, 0, NULL, 2, _UU("SM_LICENSE_STATUS_SUBSCRIPTION"), tmp);
4055 
4056 		if (st.NeedSubscription == false && st.SubscriptionExpires != 0)
4057 		{
4058 			wchar_t dtstr[MAX_PATH];
4059 
4060 			GetDateStrEx64(dtstr, sizeof(dtstr), st.SubscriptionExpires, NULL);
4061 
4062 			LvInsertAdd(b, 0, NULL, 2, _UU("SM_LICENSE_STATUS_SUBSCRIPTION_BUILD_STR"), tmp);
4063 		}
4064 
4065 		if (st.NeedSubscription && st.SubscriptionExpires != 0)
4066 		{
4067 			wchar_t dtstr[MAX_PATH];
4068 
4069 			GetDateStrEx64(dtstr, sizeof(dtstr), st.SubscriptionExpires, NULL);
4070 
4071 			UniFormat(tmp, sizeof(tmp), _UU("SM_LICENSE_STATUS_SUBSCRIPTION_BUILD_STR"), dtstr);
4072 
4073 			LvInsertAdd(b, 0, NULL, 2, _UU("SM_LICENSE_STATUS_SUBSCRIPTION_BUILD"), tmp);
4074 		}
4075 
4076 		if (GetCapsBool(s->CapsList, "b_vpn3"))
4077 		{
4078 			// Maximum number of users
4079 			if (st.NumUserCreationLicense == INFINITE)
4080 			{
4081 				UniStrCpy(tmp, sizeof(tmp), _UU("SM_LICENSE_INFINITE"));
4082 			}
4083 			else
4084 			{
4085 				UniToStru(tmp, st.NumUserCreationLicense);
4086 			}
4087 			LvInsertAdd(b, 0, NULL, 2, _UU("SM_LICENSE_NUM_USER"), tmp);
4088 		}
4089 
4090 		// Available number of concurrent client connections
4091 		if (st.NumClientConnectLicense == INFINITE)
4092 		{
4093 			UniStrCpy(tmp, sizeof(tmp), _UU("SM_LICENSE_INFINITE"));
4094 		}
4095 		else
4096 		{
4097 			UniToStru(tmp, st.NumClientConnectLicense);
4098 		}
4099 		LvInsertAdd(b, 0, NULL, 2, _UU("SM_LICENSE_NUM_CLIENT"), tmp);
4100 
4101 		// Available number of concurrent Bridge connections
4102 		if (st.NumBridgeConnectLicense == INFINITE)
4103 		{
4104 			UniStrCpy(tmp, sizeof(tmp), _UU("SM_LICENSE_INFINITE"));
4105 		}
4106 		else
4107 		{
4108 			UniToStru(tmp, st.NumBridgeConnectLicense);
4109 		}
4110 		LvInsertAdd(b, 0, NULL, 2, _UU("SM_LICENSE_NUM_BRIDGE"), tmp);
4111 
4112 		// Availability of enterprise features
4113 		LvInsertAdd(b, 0, NULL, 2, _UU("SM_LICENSE_STATUS_ENTERPRISE"),
4114 			st.AllowEnterpriseFunction ? _UU("SM_LICENSE_STATUS_ENTERPRISE_YES") : _UU("SM_LICENSE_STATUS_ENTERPRISE_NO"));
4115 	}
4116 
4117 	LvInsertEnd(b, hWnd, L_STATUS);
4118 
4119 	if (LvNum(hWnd, L_STATUS) >= 1)
4120 	{
4121 		LvAutoSize(hWnd, L_STATUS);
4122 	}
4123 
4124 	SmLicenseDlgUpdate(hWnd, s);
4125 }
4126 
4127 // License dialog control update
SmLicenseDlgUpdate(HWND hWnd,SM_SERVER * s)4128 void SmLicenseDlgUpdate(HWND hWnd, SM_SERVER *s)
4129 {
4130 	bool b = false;
4131 	// Validate arguments
4132 	if (hWnd == NULL || s == NULL)
4133 	{
4134 		return;
4135 	}
4136 
4137 	b = LvIsSingleSelected(hWnd, L_LIST);
4138 
4139 	SetEnable(hWnd, B_DEL, b);
4140 	SetEnable(hWnd, IDOK, b);
4141 }
4142 
4143 // License dialog
SmLicenseDlg(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)4144 UINT SmLicenseDlg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
4145 {
4146 	SM_SERVER *s = (SM_SERVER *)param;
4147 	NMHDR *n;
4148 	// Validate arguments
4149 	if (hWnd == NULL)
4150 	{
4151 		return 0;
4152 	}
4153 
4154 	switch (msg)
4155 	{
4156 	case WM_INITDIALOG:
4157 		SmLicenseDlgInit(hWnd, s);
4158 		break;
4159 
4160 	case WM_NOTIFY:
4161 		n = (NMHDR *)lParam;
4162 		switch (n->code)
4163 		{
4164 		case LVN_ITEMCHANGED:
4165 			switch (n->idFrom)
4166 			{
4167 			case L_LIST:
4168 			case L_STATUS:
4169 				SmLicenseDlgUpdate(hWnd, s);
4170 				break;
4171 			}
4172 			break;
4173 		}
4174 		break;
4175 
4176 	case WM_COMMAND:
4177 		switch (wParam)
4178 		{
4179 		case IDOK:
4180 			if (IsEnable(hWnd, IDOK))
4181 			{
4182 				UINT i = LvGetSelected(hWnd, L_LIST);
4183 
4184 				if (i != INFINITE)
4185 				{
4186 					char *s = LvGetStrA(hWnd, L_LIST, i, 1);
4187 					char tmp[MAX_SIZE];
4188 
4189 					Format(tmp, sizeof(tmp), _SS("LICENSE_SUPPORT_URL"), s);
4190 					ShellExecute(hWnd, "open", tmp, NULL, NULL, SW_SHOW);
4191 
4192 					Free(s);
4193 				}
4194 			}
4195 			break;
4196 
4197 		case B_OBTAIN:
4198 			ShellExecute(hWnd, "open", _SS("LICENSE_INFO_URL"), NULL, NULL, SW_SHOW);
4199 			break;
4200 
4201 		case B_ADD:
4202 			if (SmLicenseAdd(hWnd, s))
4203 			{
4204 				SmLicenseDlgRefresh(hWnd, s);
4205 			}
4206 			break;
4207 
4208 		case B_DEL:
4209 			if (IsEnable(hWnd, B_DEL))
4210 			{
4211 				UINT id = (UINT)LvGetParam(hWnd, L_LIST, LvGetSelected(hWnd, L_LIST));
4212 
4213 				if (id != 0)
4214 				{
4215 					if (MsgBox(hWnd, MB_ICONQUESTION | MB_YESNO | MB_DEFBUTTON2, _UU("SM_LICENSE_DELETE_MSG")) == IDYES)
4216 					{
4217 						RPC_TEST t;
4218 
4219 						Zero(&t, sizeof(t));
4220 						t.IntValue = id;
4221 
4222 						if (CALL(hWnd, ScDelLicenseKey(s->Rpc, &t)))
4223 						{
4224 							SmLicenseDlgRefresh(hWnd, s);
4225 						}
4226 					}
4227 				}
4228 			}
4229 			break;
4230 
4231 		case IDCANCEL:
4232 			Close(hWnd);
4233 			break;
4234 		}
4235 		break;
4236 
4237 	case WM_CLOSE:
4238 		EndDialog(hWnd, 0);
4239 		break;
4240 	}
4241 
4242 	LvStandardHandler(hWnd, msg, wParam, lParam, L_LIST);
4243 
4244 	return 0;
4245 }
4246 
4247 // Add or Remove license
SmLicense(HWND hWnd,SM_SERVER * s)4248 void SmLicense(HWND hWnd, SM_SERVER *s)
4249 {
4250 	// Validate arguments
4251 	if (s == NULL)
4252 	{
4253 		return;
4254 	}
4255 
4256 	Dialog(hWnd, D_SM_LICENSE, SmLicenseDlg, s);
4257 
4258 	FreeCapsList(s->CapsList);
4259 	s->CapsList = ScGetCapsEx(s->Rpc);
4260 }
4261 
4262 // Log storing procedure
SmSaveLogProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)4263 UINT SmSaveLogProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
4264 {
4265 	SM_READ_LOG_FILE *p = (SM_READ_LOG_FILE *)param;
4266 	// Validate arguments
4267 	if (hWnd == NULL)
4268 	{
4269 		return 0;
4270 	}
4271 
4272 	switch (msg)
4273 	{
4274 	case WM_INITDIALOG:
4275 		FormatText(hWnd, S_INFO, p->filepath);
4276 		break;
4277 
4278 	case WM_COMMAND:
4279 		switch (wParam)
4280 		{
4281 		case IDOK:
4282 		case B_SAVE:
4283 			if (p->Buffer != NULL)
4284 			{
4285 				char filename[MAX_PATH];
4286 
4287 				Format(filename, sizeof(filename), "%s_%s", p->server_name, p->filepath);
4288 				ConvertSafeFileName(filename, sizeof(filename), filename);
4289 
4290 				if (wParam == IDOK)
4291 				{
4292 					// Open with an editor
4293 					char fullpath[MAX_PATH];
4294 
4295 					Format(fullpath, sizeof(fullpath), "%s\\%s",
4296 						MsGetMyTempDir(), filename);
4297 
4298 					if (DumpBuf(p->Buffer, fullpath) == false)
4299 					{
4300 						MsgBoxEx(hWnd, MB_ICONSTOP, _UU("SM_READ_SAVE_TMP_FAILED"),
4301 							fullpath);
4302 					}
4303 					else
4304 					{
4305 						if (((UINT)ShellExecute(hWnd, "open", fullpath, NULL, NULL, SW_SHOWNORMAL)) > 32)
4306 						{
4307 							EndDialog(hWnd, true);
4308 						}
4309 						else
4310 						{
4311 							MsgBoxEx(hWnd, MB_ICONEXCLAMATION, _UU("SM_READ_SAVE_OPEN_ERROR"), fullpath);
4312 						}
4313 					}
4314 				}
4315 				else
4316 				{
4317 					// Save to a file
4318 					wchar_t def[MAX_PATH];
4319 					wchar_t *uni_path;
4320 
4321 					StrToUni(def, sizeof(def), filename);
4322 
4323 					uni_path = SaveDlg(hWnd, _UU("SM_READ_SAVE_DLG_FILTER"), _UU("SM_READ_SAVE_DLG_TITLE"),
4324 						def, L".log");
4325 
4326 					if (uni_path != NULL)
4327 					{
4328 						char path[MAX_PATH];
4329 
4330 						UniToStr(path, sizeof(path), uni_path);
4331 						Free(uni_path);
4332 
4333 						if (DumpBuf(p->Buffer, path) == false)
4334 						{
4335 							MsgBox(hWnd, MB_ICONSTOP, _UU("SM_READ_SAVE_FAILED"));
4336 						}
4337 						else
4338 						{
4339 							EndDialog(hWnd, true);
4340 						}
4341 					}
4342 				}
4343 			}
4344 			break;
4345 
4346 		case IDCANCEL:
4347 			Close(hWnd);
4348 			break;
4349 		}
4350 		break;
4351 
4352 	case WM_CLOSE:
4353 		EndDialog(hWnd, false);
4354 		break;
4355 	}
4356 
4357 	return 0;
4358 }
4359 
4360 // Download callback procedure
SmReadLogFileProc(DOWNLOAD_PROGRESS * g)4361 bool SmReadLogFileProc(DOWNLOAD_PROGRESS *g)
4362 {
4363 	wchar_t tmp[MAX_SIZE];
4364 	char size1[64], size2[64];
4365 	SM_READ_LOG_FILE *p;
4366 	HWND hWnd;
4367 	// Validate arguments
4368 	if (g == NULL)
4369 	{
4370 		return false;
4371 	}
4372 
4373 	p = (SM_READ_LOG_FILE *)g->Param;
4374 	hWnd = p->hWnd;
4375 
4376 	SetPos(hWnd, P_PROGRESS, g->ProgressPercent);
4377 
4378 	ToStrByte(size1, sizeof(size1), g->CurrentSize);
4379 	ToStrByte(size2, sizeof(size2), g->TotalSize);
4380 	UniFormat(tmp, sizeof(tmp), _UU("SM_READ_LOG_FILE_INFO_2"), size2, size1);
4381 
4382 	SetText(hWnd, S_INFO, tmp);
4383 
4384 	DoEvents(hWnd);
4385 
4386 	return p->cancel_flag ? false : true;
4387 }
4388 
4389 // Log file download dialog procedure
SmReadLogFile(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)4390 UINT SmReadLogFile(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
4391 {
4392 	SM_READ_LOG_FILE *p = (SM_READ_LOG_FILE *)param;
4393 	BUF *buf;
4394 	// Validate arguments
4395 	if (hWnd == NULL)
4396 	{
4397 		return 0;
4398 	}
4399 
4400 	switch (msg)
4401 	{
4402 	case WM_INITDIALOG:
4403 		p->hWnd = hWnd;
4404 		SetFont(hWnd, S_INFO, Font(11, true));
4405 		SetText(hWnd, S_INFO, _UU("SM_READ_LOG_FILE_INFO_1"));
4406 		DisableClose(hWnd);
4407 		FormatText(hWnd, S_INFO2, p->filepath);
4408 		SetRange(hWnd, P_PROGRESS, 0, 100);
4409 
4410 		SetTimer(hWnd, 1, 100, NULL);
4411 		break;
4412 
4413 	case WM_TIMER:
4414 		switch (wParam)
4415 		{
4416 		case 1:
4417 			KillTimer(hWnd, 1);
4418 			buf = DownloadFileFromServer(p->s->Rpc, p->server_name, p->filepath, p->totalsize, SmReadLogFileProc, p);
4419 			if (buf == NULL)
4420 			{
4421 				if (p->cancel_flag == false)
4422 				{
4423 					// Download failure
4424 					MsgBox(hWnd, MB_ICONSTOP, _UU("SM_READ_LOG_FILE_ERROR"));
4425 				}
4426 				EndDialog(hWnd, false);
4427 			}
4428 			else
4429 			{
4430 				// Download success
4431 				p->Buffer = buf;
4432 				Dialog(hWnd, D_SM_SAVE_LOG, SmSaveLogProc, p);
4433 				FreeBuf(buf);
4434 				EndDialog(hWnd, true);
4435 			}
4436 			break;
4437 		}
4438 		break;
4439 
4440 	case WM_COMMAND:
4441 		switch (wParam)
4442 		{
4443 		case IDCANCEL:
4444 			p->cancel_flag = true;
4445 			break;
4446 		}
4447 		break;
4448 	}
4449 
4450 	return 0;
4451 }
4452 
4453 // Start the download of the log file
SmLogFileStartDownload(HWND hWnd,SM_SERVER * s,char * server_name,char * filepath,UINT totalsize)4454 void SmLogFileStartDownload(HWND hWnd, SM_SERVER *s, char *server_name, char *filepath, UINT totalsize)
4455 {
4456 	SM_READ_LOG_FILE p;
4457 	// Validate arguments
4458 	if (hWnd == NULL || server_name == NULL || filepath == NULL || totalsize == 0)
4459 	{
4460 		return;
4461 	}
4462 
4463 	Zero(&p, sizeof(p));
4464 	p.filepath = filepath;
4465 	p.s = s;
4466 	p.server_name = server_name;
4467 	p.totalsize = totalsize;
4468 
4469 	Dialog(hWnd, D_SM_READ_LOG_FILE, SmReadLogFile, &p);
4470 }
4471 
4472 // Initialize the dialog
SmLogFileDlgInit(HWND hWnd,SM_SERVER * p)4473 void SmLogFileDlgInit(HWND hWnd, SM_SERVER *p)
4474 {
4475 	// Validate arguments
4476 	if (hWnd == NULL || p == NULL)
4477 	{
4478 		return;
4479 	}
4480 
4481 	SetIcon(hWnd, 0, ICO_LOG2);
4482 
4483 	LvInit(hWnd, L_LIST);
4484 
4485 	LvInsertColumn(hWnd, L_LIST, 0, _UU("SM_LOG_FILE_COLUMN_1"), 250);
4486 	LvInsertColumn(hWnd, L_LIST, 1, _UU("SM_LOG_FILE_COLUMN_2"), 100);
4487 	LvInsertColumn(hWnd, L_LIST, 2, _UU("SM_LOG_FILE_COLUMN_3"), 130);
4488 	LvInsertColumn(hWnd, L_LIST, 3, _UU("SM_LOG_FILE_COLUMN_4"), 110);
4489 
4490 	SmLogFileDlgRefresh(hWnd, p);
4491 }
4492 
4493 // Dialog content update
SmLogFileDlgRefresh(HWND hWnd,SM_SERVER * p)4494 void SmLogFileDlgRefresh(HWND hWnd, SM_SERVER *p)
4495 {
4496 	UINT i;
4497 	LVB *v;
4498 	RPC_ENUM_LOG_FILE t;
4499 	// Validate arguments
4500 	if (hWnd == NULL || p == NULL)
4501 	{
4502 		return;
4503 	}
4504 
4505 	Zero(&t, sizeof(t));
4506 	if (CALL(hWnd, ScEnumLogFile(p->Rpc, &t)) == false)
4507 	{
4508 		Close(hWnd);
4509 		return;
4510 	}
4511 
4512 	v = LvInsertStart();
4513 
4514 	for (i = 0;i < t.NumItem;i++)
4515 	{
4516 		RPC_ENUM_LOG_FILE_ITEM *e = &t.Items[i];
4517 		wchar_t tmp1[MAX_PATH], tmp2[128], tmp3[128], tmp4[MAX_HOST_NAME_LEN + 1];
4518 		char tmp[MAX_SIZE];
4519 
4520 		StrToUni(tmp1, sizeof(tmp1), e->FilePath);
4521 
4522 		ToStrByte(tmp, sizeof(tmp), e->FileSize);
4523 		StrToUni(tmp2, sizeof(tmp2), tmp);
4524 
4525 		GetDateTimeStr64Uni(tmp3, sizeof(tmp3), SystemToLocal64(e->UpdatedTime));
4526 
4527 		StrToUni(tmp4, sizeof(tmp4), e->ServerName);
4528 
4529 		LvInsertAdd(v, ICO_LOG2, (void *)e->FileSize, 4, tmp1, tmp2, tmp3, tmp4);
4530 	}
4531 
4532 	LvInsertEndEx(v, hWnd, L_LIST, true);
4533 
4534 	if (t.NumItem != 0)
4535 	{
4536 		LvAutoSize(hWnd, L_LIST);
4537 	}
4538 
4539 	FreeRpcEnumLogFile(&t);
4540 
4541 	SmLogFileDlgUpdate(hWnd, p);
4542 }
4543 
4544 // Update the dialog control
SmLogFileDlgUpdate(HWND hWnd,SM_SERVER * p)4545 void SmLogFileDlgUpdate(HWND hWnd, SM_SERVER *p)
4546 {
4547 	// Validate arguments
4548 	if (hWnd == NULL || p == NULL)
4549 	{
4550 		return;
4551 	}
4552 
4553 	SetEnable(hWnd, IDOK, LvIsSingleSelected(hWnd, L_LIST));
4554 }
4555 
4556 // Log file dialog procedure
SmLogFileDlgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)4557 UINT SmLogFileDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
4558 {
4559 	NMHDR *n;
4560 	SM_SERVER *p = (SM_SERVER *)param;
4561 	// Validate arguments
4562 	if (hWnd == NULL)
4563 	{
4564 		return 0;
4565 	}
4566 
4567 	switch (msg)
4568 	{
4569 	case WM_INITDIALOG:
4570 		SmLogFileDlgInit(hWnd, p);
4571 		break;
4572 
4573 	case WM_COMMAND:
4574 		switch (wParam)
4575 		{
4576 		case IDOK:
4577 			if (IsEnable(hWnd, IDOK))
4578 			{
4579 				UINT i = LvGetSelected(hWnd, L_LIST);
4580 				if (i != INFINITE)
4581 				{
4582 					UINT size = (UINT)LvGetParam(hWnd, L_LIST, i);
4583 					char *server_name;
4584 					char *filepath;
4585 
4586 					server_name = LvGetStrA(hWnd, L_LIST, i, 3);
4587 					filepath = LvGetStrA(hWnd, L_LIST, i, 0);
4588 					SmLogFileStartDownload(hWnd, p, server_name, filepath, size);
4589 					Free(filepath);
4590 					Free(server_name);
4591 				}
4592 			}
4593 			break;
4594 
4595 		case IDCANCEL:
4596 			Close(hWnd);
4597 			break;
4598 
4599 		case B_REFRESH:
4600 			SmLogFileDlgRefresh(hWnd, p);
4601 			break;
4602 		}
4603 		break;
4604 
4605 	case WM_NOTIFY:
4606 		n = (NMHDR *)lParam;
4607 		switch (n->code)
4608 		{
4609 		case LVN_ITEMCHANGED:
4610 			switch (n->idFrom)
4611 			{
4612 			case L_LIST:
4613 				SmLogFileDlgUpdate(hWnd, p);
4614 				break;
4615 			}
4616 			break;
4617 		}
4618 		break;
4619 
4620 	case WM_CLOSE:
4621 		EndDialog(hWnd, false);
4622 		break;
4623 	}
4624 
4625 	LvStandardHandler(hWnd, msg, wParam, lParam, L_LIST);
4626 
4627 	return 0;
4628 }
4629 
4630 // Initialize the dialog
SmHubEditAcDlgInit(HWND hWnd,SM_EDIT_AC * p)4631 void SmHubEditAcDlgInit(HWND hWnd, SM_EDIT_AC *p)
4632 {
4633 	// Validate arguments
4634 	if (hWnd == NULL || p == NULL)
4635 	{
4636 		return;
4637 	}
4638 
4639 	SetEnable(hWnd, R_IPV6, GetCapsBool(p->e->s->p->CapsList, "b_support_ipv6_ac"));
4640 
4641 	if (p->id == 0)
4642 	{
4643 		UINT i, v;
4644 
4645 		Check(hWnd, R_SINGLE, true);
4646 		Check(hWnd, R_PASS, true);
4647 		Check(hWnd, R_IPV4, true);
4648 
4649 		v = 0;
4650 
4651 		for (i = 0;i < LIST_NUM(p->e->AcList);i++)
4652 		{
4653 			AC *ac = LIST_DATA(p->e->AcList, i);
4654 
4655 			v = MAX(v, ac->Priority);
4656 		}
4657 
4658 		v += 100;
4659 
4660 		SetInt(hWnd, E_PRIORITY, v);
4661 	}
4662 	else
4663 	{
4664 		AC *ac = GetAc(p->e->AcList, p->id);
4665 
4666 		if (ac == NULL)
4667 		{
4668 			EndDialog(hWnd, false);
4669 			return;
4670 		}
4671 
4672 		Check(hWnd, R_SINGLE, ac->Masked == false);
4673 		Check(hWnd, R_MASKED, ac->Masked);
4674 		Check(hWnd, R_IPV4, IsIP4(&ac->IpAddress));
4675 		Check(hWnd, R_IPV6, IsIP6(&ac->IpAddress));
4676 
4677 		if (IsIP4(&ac->IpAddress))
4678 		{
4679 			IpSet(hWnd, E_IP, IPToUINT(&ac->IpAddress));
4680 		}
4681 		else
4682 		{
4683 			char tmp[MAX_SIZE];
4684 
4685 			IPToStr(tmp, sizeof(tmp), &ac->IpAddress);
4686 			SetTextA(hWnd, E_IPV6, tmp);
4687 		}
4688 
4689 		if (ac->Masked)
4690 		{
4691 			if (IsIP4(&ac->IpAddress))
4692 			{
4693 				IpSet(hWnd, E_MASK, IPToUINT(&ac->SubnetMask));
4694 			}
4695 			else
4696 			{
4697 				char tmp[MAX_SIZE];
4698 
4699 				MaskToStrEx(tmp, sizeof(tmp), &ac->SubnetMask, false);
4700 
4701 				if (IsNum(tmp))
4702 				{
4703 					StrCatLeft(tmp, sizeof(tmp), "/");
4704 				}
4705 
4706 				SetTextA(hWnd, E_MASKV6, tmp);
4707 			}
4708 		}
4709 
4710 		Check(hWnd, R_PASS, ac->Deny == false);
4711 		Check(hWnd, R_DENY, ac->Deny);
4712 		SetInt(hWnd, E_PRIORITY, ac->Priority);
4713 
4714 		Free(ac);
4715 	}
4716 
4717 	Focus(hWnd, E_IP);
4718 
4719 	SmHubEditAcDlgUpdate(hWnd, p);
4720 }
4721 
4722 // Dialog update
SmHubEditAcDlgUpdate(HWND hWnd,SM_EDIT_AC * p)4723 void SmHubEditAcDlgUpdate(HWND hWnd, SM_EDIT_AC *p)
4724 {
4725 	bool b = true;
4726 	char tmp[MAX_SIZE];
4727 	// Validate arguments
4728 	if (hWnd == NULL || p == NULL)
4729 	{
4730 		return;
4731 	}
4732 
4733 	if (IsChecked(hWnd, R_SINGLE))
4734 	{
4735 		if (IsChecked(hWnd, R_IPV6) == false)
4736 		{
4737 			Show(hWnd, E_IP);
4738 			Hide(hWnd, E_IPV6);
4739 
4740 			if (IpIsFilled(hWnd, E_IP) == false)
4741 			{
4742 				b = false;
4743 			}
4744 
4745 			if (IpGet(hWnd, E_IP) == 0 || IpGet(hWnd, E_IP) == 0xffffffff)
4746 			{
4747 				b = false;
4748 			}
4749 		}
4750 		else
4751 		{
4752 			Show(hWnd, E_IPV6);
4753 			Hide(hWnd, E_IP);
4754 
4755 			GetTxtA(hWnd, E_IPV6, tmp, sizeof(tmp));
4756 
4757 			if (IsStrIPv6Address(tmp) == false)
4758 			{
4759 				b = false;
4760 			}
4761 		}
4762 
4763 		Hide(hWnd, S_MASK);
4764 		Hide(hWnd, E_MASK);
4765 		Hide(hWnd, E_MASKV6);
4766 	}
4767 	else
4768 	{
4769 		if (IsChecked(hWnd, R_IPV6) == false)
4770 		{
4771 			Show(hWnd, E_IP);
4772 			Hide(hWnd, E_IPV6);
4773 
4774 			if (IpIsFilled(hWnd, E_IP) == false || IpIsFilled(hWnd, E_MASK) == false)
4775 			{
4776 				b = false;
4777 			}
4778 
4779 			if (IpGet(hWnd, E_IP) == 0xffffffff)
4780 			{
4781 				b = false;
4782 			}
4783 		}
4784 		else
4785 		{
4786 			char tmp1[MAX_SIZE], tmp2[MAX_SIZE];
4787 
4788 			Show(hWnd, E_IPV6);
4789 			Hide(hWnd, E_IP);
4790 
4791 			GetTxtA(hWnd, E_IPV6, tmp1, sizeof(tmp1));
4792 			GetTxtA(hWnd, E_MASKV6, tmp2, sizeof(tmp2));
4793 
4794 			if (!(IsIpStr6(tmp1) && IsIpMask6(tmp2)))
4795 			{
4796 				b = false;
4797 			}
4798 		}
4799 
4800 		Show(hWnd, S_MASK);
4801 		SetShow(hWnd, E_MASK, !IsChecked(hWnd, R_IPV6));
4802 		SetShow(hWnd, E_MASKV6, IsChecked(hWnd, R_IPV6));
4803 	}
4804 
4805 	if (GetInt(hWnd, E_PRIORITY) == 0)
4806 	{
4807 		b = false;
4808 	}
4809 
4810 	SetIcon(hWnd, S_ICON, IsChecked(hWnd, R_PASS) ? ICO_INTERNET : ICO_INTERNET_X);
4811 
4812 	SetEnable(hWnd, IDOK, b);
4813 }
4814 
4815 // OK button is clicked in the dialog
SmHubEditAcDlgOnOk(HWND hWnd,SM_EDIT_AC * p)4816 void SmHubEditAcDlgOnOk(HWND hWnd, SM_EDIT_AC *p)
4817 {
4818 	AC ac;
4819 	char tmp[MAX_SIZE];
4820 	// Validate arguments
4821 	if (hWnd == NULL || p == NULL)
4822 	{
4823 		return;
4824 	}
4825 
4826 	Zero(&ac, sizeof(ac));
4827 	ac.Deny = IsChecked(hWnd, R_DENY);
4828 	ac.Priority = GetInt(hWnd, E_PRIORITY);
4829 
4830 	if (IsChecked(hWnd, R_IPV6) == false)
4831 	{
4832 		UINTToIP(&ac.IpAddress, IpGet(hWnd, E_IP));
4833 	}
4834 	else
4835 	{
4836 		GetTxtA(hWnd, E_IPV6, tmp, sizeof(tmp));
4837 
4838 		StrToIP6(&ac.IpAddress, tmp);
4839 	}
4840 
4841 	ac.Masked = IsChecked(hWnd, R_MASKED);
4842 
4843 	if (ac.Masked)
4844 	{
4845 		if (IsChecked(hWnd, R_IPV6) == false)
4846 		{
4847 			UINTToIP(&ac.SubnetMask, IpGet(hWnd, E_MASK));
4848 		}
4849 		else
4850 		{
4851 			GetTxtA(hWnd, E_MASKV6, tmp, sizeof(tmp));
4852 
4853 			StrToMask6(&ac.SubnetMask, tmp);
4854 		}
4855 	}
4856 
4857 	if (p->id != 0)
4858 	{
4859 		SetAc(p->e->AcList, p->id, &ac);
4860 	}
4861 	else
4862 	{
4863 		AddAc(p->e->AcList, &ac);
4864 	}
4865 
4866 	EndDialog(hWnd, true);
4867 }
4868 
4869 // AC edit dialog
SmHubEditAcDlgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)4870 UINT SmHubEditAcDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
4871 {
4872 	SM_EDIT_AC *p = (SM_EDIT_AC *)param;
4873 	// Validate arguments
4874 	if (hWnd == NULL)
4875 	{
4876 		return 0;
4877 	}
4878 
4879 	switch (msg)
4880 	{
4881 	case WM_INITDIALOG:
4882 		SmHubEditAcDlgInit(hWnd, p);
4883 		break;
4884 
4885 	case WM_COMMAND:
4886 		switch (LOWORD(wParam))
4887 		{
4888 		case R_SINGLE:
4889 		case R_MASKED:
4890 		case E_IP:
4891 		case E_MASK:
4892 		case R_PASS:
4893 		case R_DENY:
4894 		case E_PRIORITY:
4895 		case R_IPV4:
4896 		case R_IPV6:
4897 		case E_IPV6:
4898 		case E_MASKV6:
4899 			SmHubEditAcDlgUpdate(hWnd, p);
4900 			break;
4901 		}
4902 
4903 		switch (wParam)
4904 		{
4905 		case R_IPV4:
4906 		case R_IPV6:
4907 		case R_SINGLE:
4908 		case R_MASKED:
4909 			if (IsChecked(hWnd, R_IPV6) == false)
4910 			{
4911 				if (IpIsFilled(hWnd, E_IP))
4912 				{
4913 					Focus(hWnd, E_MASK);
4914 				}
4915 				else
4916 				{
4917 					Focus(hWnd, E_IP);
4918 				}
4919 			}
4920 			else
4921 			{
4922 				char tmp[MAX_SIZE];
4923 
4924 				GetTxtA(hWnd, E_IPV6, tmp, sizeof(tmp));
4925 
4926 				if (IsStrIPv6Address(tmp))
4927 				{
4928 					FocusEx(hWnd, E_MASKV6);
4929 				}
4930 				else
4931 				{
4932 					FocusEx(hWnd, E_IPV6);
4933 				}
4934 			}
4935 			break;
4936 
4937 		case IDOK:
4938 			SmHubEditAcDlgOnOk(hWnd, p);
4939 			break;
4940 
4941 		case IDCANCEL:
4942 			Close(hWnd);
4943 			break;
4944 		}
4945 		break;
4946 
4947 	case WM_CLOSE:
4948 		EndDialog(hWnd, false);
4949 		break;
4950 	}
4951 
4952 	return 0;
4953 }
4954 
4955 // Initialize the dialog
SmHubAcDlgInit(HWND hWnd,SM_EDIT_AC_LIST * p)4956 void SmHubAcDlgInit(HWND hWnd, SM_EDIT_AC_LIST *p)
4957 {
4958 	// Validate arguments
4959 	if (hWnd == NULL || p == NULL)
4960 	{
4961 		return;
4962 	}
4963 
4964 	SetIcon(hWnd, 0, ICO_INTERNET);
4965 
4966 	FormatText(hWnd, S_TITLE, p->s->HubName);
4967 
4968 	LvInit(hWnd, L_LIST);
4969 	LvInsertColumn(hWnd, L_LIST, 0, _UU("SM_AC_COLUMN_1"), 40);
4970 	LvInsertColumn(hWnd, L_LIST, 1, _UU("SM_AC_COLUMN_2"), 80);
4971 	LvInsertColumn(hWnd, L_LIST, 2, _UU("SM_AC_COLUMN_3"), 90);
4972 	LvInsertColumn(hWnd, L_LIST, 3, _UU("SM_AC_COLUMN_4"), 170);
4973 
4974 	SmHubAcDlgRefresh(hWnd, p);
4975 }
4976 
4977 // Update the dialog control
SmHubAcDlgUpdate(HWND hWnd,SM_EDIT_AC_LIST * p)4978 void SmHubAcDlgUpdate(HWND hWnd, SM_EDIT_AC_LIST *p)
4979 {
4980 	bool b;
4981 	// Validate arguments
4982 	if (hWnd == NULL || p == NULL)
4983 	{
4984 		return;
4985 	}
4986 
4987 	b = LvIsSingleSelected(hWnd, L_LIST);
4988 
4989 	SetEnable(hWnd, IDOK, b);
4990 	SetEnable(hWnd, B_DELETE, b);
4991 }
4992 
4993 // Dialog content update
SmHubAcDlgRefresh(HWND hWnd,SM_EDIT_AC_LIST * p)4994 void SmHubAcDlgRefresh(HWND hWnd, SM_EDIT_AC_LIST *p)
4995 {
4996 	UINT i;
4997 	LVB *v;
4998 	// Validate arguments
4999 	if (hWnd == NULL || p == NULL)
5000 	{
5001 		return;
5002 	}
5003 
5004 	v = LvInsertStart();
5005 
5006 	for (i = 0;i < LIST_NUM(p->AcList);i++)
5007 	{
5008 		wchar_t tmp1[32], *tmp2, tmp3[MAX_SIZE], tmp4[32];
5009 		char *tmp_str;
5010 		AC *ac = LIST_DATA(p->AcList, i);
5011 
5012 		UniToStru(tmp1, ac->Id);
5013 		tmp2 = ac->Deny ? _UU("SM_AC_DENY") : _UU("SM_AC_PASS");
5014 		tmp_str = GenerateAcStr(ac);
5015 		StrToUni(tmp3, sizeof(tmp3), tmp_str);
5016 
5017 		Free(tmp_str);
5018 
5019 		UniToStru(tmp4, ac->Priority);
5020 
5021 		LvInsertAdd(v, ac->Deny ? ICO_INTERNET_X : ICO_INTERNET,
5022 			(void *)ac->Id, 4, tmp1, tmp4, tmp2, tmp3);
5023 	}
5024 
5025 	LvInsertEnd(v, hWnd, L_LIST);
5026 	LvSortEx(hWnd, L_LIST, 0, false, true);
5027 
5028 
5029 	SmHubAcDlgUpdate(hWnd, p);
5030 }
5031 
5032 // Access control list editing dialog
SmHubAcDlgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)5033 UINT SmHubAcDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
5034 {
5035 	NMHDR *n;
5036 	SM_EDIT_AC_LIST *p = (SM_EDIT_AC_LIST *)param;
5037 	// Validate arguments
5038 	if (hWnd == NULL)
5039 	{
5040 		return 0;
5041 	}
5042 
5043 	switch (msg)
5044 	{
5045 	case WM_INITDIALOG:
5046 		SmHubAcDlgInit(hWnd, p);
5047 		break;
5048 
5049 	case WM_COMMAND:
5050 		switch (wParam)
5051 		{
5052 		case IDOK:
5053 			if (IsEnable(hWnd, IDOK))
5054 			{
5055 				SM_EDIT_AC s;
5056 				Zero(&s, sizeof(s));
5057 
5058 				s.e = p;
5059 				s.id = (UINT)LvGetParam(hWnd, L_LIST, LvGetSelected(hWnd, L_LIST));
5060 
5061 				if (Dialog(hWnd, D_SM_AC, SmHubEditAcDlgProc, &s))
5062 				{
5063 					SmHubAcDlgRefresh(hWnd, p);
5064 				}
5065 			}
5066 			break;
5067 
5068 		case B_ADD:
5069 			if (IsEnable(hWnd, B_ADD))
5070 			{
5071 				SM_EDIT_AC s;
5072 				Zero(&s, sizeof(s));
5073 
5074 				s.e = p;
5075 
5076 				if (Dialog(hWnd, D_SM_AC, SmHubEditAcDlgProc, &s))
5077 				{
5078 					SmHubAcDlgRefresh(hWnd, p);
5079 				}
5080 			}
5081 			break;
5082 
5083 		case B_DELETE:
5084 			if (IsEnable(hWnd, B_DELETE))
5085 			{
5086 				UINT id = (UINT)LvGetParam(hWnd, L_LIST, LvGetSelected(hWnd, L_LIST));
5087 
5088 				if (DelAc(p->AcList, id))
5089 				{
5090 					SmHubAcDlgRefresh(hWnd, p);
5091 				}
5092 			}
5093 			break;
5094 
5095 		case B_SAVE:
5096 			if (IsEnable(hWnd, B_SAVE))
5097 			{
5098 				RPC_AC_LIST t;
5099 
5100 				Zero(&t, sizeof(t));
5101 				StrCpy(t.HubName, sizeof(t.HubName), p->s->HubName);
5102 				t.o = CloneAcList(p->AcList);
5103 
5104 				if (CALL(hWnd, ScSetAcList(p->s->p->Rpc, &t)))
5105 				{
5106 					EndDialog(hWnd, true);
5107 				}
5108 
5109 				FreeRpcAcList(&t);
5110 			}
5111 			break;
5112 
5113 		case IDCANCEL:
5114 			Close(hWnd);
5115 			break;
5116 		}
5117 		break;
5118 
5119 	case WM_NOTIFY:
5120 		n = (NMHDR *)lParam;
5121 		switch (n->code)
5122 		{
5123 		case LVN_ITEMCHANGED:
5124 			switch (n->idFrom)
5125 			{
5126 			case L_LIST:
5127 				SmHubAcDlgUpdate(hWnd, p);
5128 				break;
5129 			}
5130 			break;
5131 		}
5132 		break;
5133 
5134 	case WM_CLOSE:
5135 		EndDialog(hWnd, false);
5136 		break;
5137 	}
5138 
5139 	LvStandardHandler(hWnd, msg, wParam, lParam, L_LIST);
5140 
5141 	return 0;
5142 }
5143 
5144 // Access control list editing
SmHubAc(HWND hWnd,SM_EDIT_HUB * s)5145 void SmHubAc(HWND hWnd, SM_EDIT_HUB *s)
5146 {
5147 	SM_EDIT_AC_LIST p;
5148 	RPC_AC_LIST t;
5149 	// Validate arguments
5150 	if (hWnd == NULL || s == NULL)
5151 	{
5152 		return;
5153 	}
5154 
5155 	Zero(&t, sizeof(t));
5156 	StrCpy(t.HubName, sizeof(t.HubName), s->HubName);
5157 
5158 	if (CALL(hWnd, ScGetAcList(s->p->Rpc, &t)) == false)
5159 	{
5160 		return;
5161 	}
5162 
5163 	Zero(&p, sizeof(p));
5164 	p.s = s;
5165 	p.AcList = CloneAcList(t.o);
5166 
5167 	FreeRpcAcList(&t);
5168 
5169 	Dialog(hWnd, D_SM_AC_LIST, SmHubAcDlgProc, &p);
5170 
5171 	FreeAcList(p.AcList);
5172 }
5173 
5174 // Initialize the dialog
SmEditCrlDlgInit(HWND hWnd,SM_EDIT_CRL * c)5175 void SmEditCrlDlgInit(HWND hWnd, SM_EDIT_CRL *c)
5176 {
5177 	// Validate arguments
5178 	if (hWnd == NULL || c == NULL)
5179 	{
5180 		return;
5181 	}
5182 
5183 	if (c->NewCrl == false)
5184 	{
5185 		RPC_CRL t;
5186 		CRL *crl;
5187 
5188 		Zero(&t, sizeof(t));
5189 		StrCpy(t.HubName, sizeof(t.HubName), c->s->HubName);
5190 		t.Key = c->Key;
5191 
5192 		if (CALL(hWnd, ScGetCrl(c->s->Rpc, &t)) == false)
5193 		{
5194 			EndDialog(hWnd, false);
5195 			return;
5196 		}
5197 
5198 		crl = t.Crl;
5199 
5200 		SmEditCrlDlgSetName(hWnd, crl->Name);
5201 		SmEditCrlDlgSetSerial(hWnd, crl->Serial);
5202 		SmEditCrlDlgSetHash(hWnd, crl->DigestMD5, crl->DigestSHA1);
5203 
5204 		FreeRpcCrl(&t);
5205 	}
5206 
5207 	SmEditCrlDlgUpdate(hWnd, c);
5208 }
5209 
5210 // Update the controls
SmEditCrlDlgUpdate(HWND hWnd,SM_EDIT_CRL * c)5211 void SmEditCrlDlgUpdate(HWND hWnd, SM_EDIT_CRL *c)
5212 {
5213 	bool b = true;
5214 	// Validate arguments
5215 	if (hWnd == NULL || c == NULL)
5216 	{
5217 		return;
5218 	}
5219 
5220 	SetEnable(hWnd, E_CN, IsChecked(hWnd, R_CN));
5221 	SetEnable(hWnd, E_O, IsChecked(hWnd, R_O));
5222 	SetEnable(hWnd, E_OU, IsChecked(hWnd, R_OU));
5223 	SetEnable(hWnd, E_C, IsChecked(hWnd, R_C));
5224 	SetEnable(hWnd, E_ST, IsChecked(hWnd, R_ST));
5225 	SetEnable(hWnd, E_L, IsChecked(hWnd, R_L));
5226 	SetEnable(hWnd, E_SERI, IsChecked(hWnd, R_SERI));
5227 	SetEnable(hWnd, E_MD5_HASH, IsChecked(hWnd, R_MD5_HASH));
5228 	SetEnable(hWnd, E_SHA1_HASH, IsChecked(hWnd, R_SHA1_HASH));
5229 
5230 	if (IsChecked(hWnd, R_CN))
5231 	{
5232 		if (IsEmpty(hWnd, E_CN))
5233 		{
5234 			b = false;
5235 		}
5236 	}
5237 
5238 	if (IsChecked(hWnd, R_O))
5239 	{
5240 		if (IsEmpty(hWnd, E_O))
5241 		{
5242 			b = false;
5243 		}
5244 	}
5245 
5246 	if (IsChecked(hWnd, R_OU))
5247 	{
5248 		if (IsEmpty(hWnd, E_OU))
5249 		{
5250 			b = false;
5251 		}
5252 	}
5253 
5254 	if (IsChecked(hWnd, R_C))
5255 	{
5256 		if (IsEmpty(hWnd, E_C))
5257 		{
5258 			b = false;
5259 		}
5260 	}
5261 
5262 	if (IsChecked(hWnd, R_ST))
5263 	{
5264 		if (IsEmpty(hWnd, E_ST))
5265 		{
5266 			b = false;
5267 		}
5268 	}
5269 
5270 	if (IsChecked(hWnd, R_L))
5271 	{
5272 		if (IsEmpty(hWnd, E_L))
5273 		{
5274 			b = false;
5275 		}
5276 	}
5277 
5278 	if (IsChecked(hWnd, R_SERI))
5279 	{
5280 		char tmp[MAX_SIZE];
5281 		BUF *buf;
5282 
5283 		GetTxtA(hWnd, E_SERI, tmp, sizeof(tmp));
5284 		buf = StrToBin(tmp);
5285 
5286 		if (buf->Size == 0)
5287 		{
5288 			b = false;
5289 		}
5290 
5291 		FreeBuf(buf);
5292 	}
5293 
5294 	if (IsChecked(hWnd, R_MD5_HASH))
5295 	{
5296 		char tmp[MAX_SIZE];
5297 		BUF *buf;
5298 
5299 		GetTxtA(hWnd, E_MD5_HASH, tmp, sizeof(tmp));
5300 		buf = StrToBin(tmp);
5301 
5302 		if (buf->Size != MD5_SIZE)
5303 		{
5304 			b = false;
5305 		}
5306 
5307 		FreeBuf(buf);
5308 	}
5309 
5310 	if (IsChecked(hWnd, R_SHA1_HASH))
5311 	{
5312 		char tmp[MAX_SIZE];
5313 		BUF *buf;
5314 
5315 		GetTxtA(hWnd, E_SHA1_HASH, tmp, sizeof(tmp));
5316 		buf = StrToBin(tmp);
5317 
5318 		if (buf->Size != SHA1_SIZE)
5319 		{
5320 			b = false;
5321 		}
5322 
5323 		FreeBuf(buf);
5324 	}
5325 
5326 	SetEnable(hWnd, IDOK, b);
5327 }
5328 
5329 // On click the OK button
SmEditCrlDlgOnOk(HWND hWnd,SM_EDIT_CRL * c)5330 void SmEditCrlDlgOnOk(HWND hWnd, SM_EDIT_CRL *c)
5331 {
5332 	CRL *crl;
5333 	NAME *n;
5334 	RPC_CRL t;
5335 	bool empty = true;
5336 	// Validate arguments
5337 	if (hWnd == NULL || c == NULL)
5338 	{
5339 		return;
5340 	}
5341 
5342 	crl = ZeroMalloc(sizeof(CRL));
5343 	crl->Name = ZeroMalloc(sizeof(NAME));
5344 	n = crl->Name;
5345 
5346 	if (IsChecked(hWnd, R_CN))
5347 	{
5348 		n->CommonName = GetText(hWnd, E_CN);
5349 		empty = false;
5350 	}
5351 
5352 	if (IsChecked(hWnd, R_O))
5353 	{
5354 		n->Organization = GetText(hWnd, E_O);
5355 		empty = false;
5356 	}
5357 
5358 	if (IsChecked(hWnd, R_OU))
5359 	{
5360 		n->Unit = GetText(hWnd, E_OU);
5361 		empty = false;
5362 	}
5363 
5364 	if (IsChecked(hWnd, R_C))
5365 	{
5366 		n->Country = GetText(hWnd, E_C);
5367 		empty = false;
5368 	}
5369 
5370 	if (IsChecked(hWnd, R_ST))
5371 	{
5372 		n->State = GetText(hWnd, E_ST);
5373 		empty = false;
5374 	}
5375 
5376 	if (IsChecked(hWnd, R_L))
5377 	{
5378 		n->Local = GetText(hWnd, E_L);
5379 		empty = false;
5380 	}
5381 
5382 	if (IsChecked(hWnd, R_SERI))
5383 	{
5384 		char tmp[MAX_SIZE];
5385 		BUF *b;
5386 
5387 		GetTxtA(hWnd, E_SERI, tmp, sizeof(tmp));
5388 		b = StrToBin(tmp);
5389 
5390 		if (b != NULL && b->Size >= 1)
5391 		{
5392 			crl->Serial = NewXSerial(b->Buf, b->Size);
5393 		}
5394 
5395 		FreeBuf(b);
5396 
5397 		empty = false;
5398 	}
5399 
5400 	if (IsChecked(hWnd, R_MD5_HASH))
5401 	{
5402 		char tmp[MAX_SIZE];
5403 		BUF *b;
5404 
5405 		GetTxtA(hWnd, E_MD5_HASH, tmp, sizeof(tmp));
5406 		b = StrToBin(tmp);
5407 
5408 		if (b != NULL && b->Size == MD5_SIZE)
5409 		{
5410 			Copy(crl->DigestMD5, b->Buf, MD5_SIZE);
5411 		}
5412 
5413 		FreeBuf(b);
5414 
5415 		empty = false;
5416 	}
5417 
5418 	if (IsChecked(hWnd, R_SHA1_HASH))
5419 	{
5420 		char tmp[MAX_SIZE];
5421 		BUF *b;
5422 
5423 		GetTxtA(hWnd, E_SHA1_HASH, tmp, sizeof(tmp));
5424 		b = StrToBin(tmp);
5425 
5426 		if (b != NULL && b->Size == SHA1_SIZE)
5427 		{
5428 			Copy(crl->DigestSHA1, b->Buf, SHA1_SIZE);
5429 		}
5430 
5431 		FreeBuf(b);
5432 
5433 		empty = false;
5434 	}
5435 
5436 	if (empty)
5437 	{
5438 		if (MsgBox(hWnd, MB_ICONEXCLAMATION | MB_YESNO | MB_DEFBUTTON2, _UU("SM_CRL_EMPTY_MSG")) == IDNO)
5439 		{
5440 			return;
5441 		}
5442 	}
5443 
5444 	if (c->NewCrl)
5445 	{
5446 		Zero(&t, sizeof(t));
5447 		StrCpy(t.HubName, sizeof(t.HubName), c->s->HubName);
5448 		t.Crl = crl;
5449 
5450 		if (CALL(hWnd, ScAddCrl(c->s->Rpc, &t)))
5451 		{
5452 			EndDialog(hWnd, true);
5453 		}
5454 
5455 		FreeRpcCrl(&t);
5456 	}
5457 	else
5458 	{
5459 		Zero(&t, sizeof(t));
5460 		StrCpy(t.HubName, sizeof(t.HubName), c->s->HubName);
5461 		t.Crl = crl;
5462 		t.Key = c->Key;
5463 
5464 		if (CALL(hWnd, ScSetCrl(c->s->Rpc, &t)))
5465 		{
5466 			EndDialog(hWnd, true);
5467 		}
5468 
5469 		FreeRpcCrl(&t);
5470 	}
5471 }
5472 
5473 // Read the certificate
SmEditCrlDlgOnLoad(HWND hWnd,SM_EDIT_CRL * c)5474 void SmEditCrlDlgOnLoad(HWND hWnd, SM_EDIT_CRL *c)
5475 {
5476 	X *x;
5477 	// Validate arguments
5478 	if (hWnd == NULL || c == NULL)
5479 	{
5480 		return;
5481 	}
5482 
5483 	if (CmLoadXFromFileOrSecureCard(hWnd, &x))
5484 	{
5485 		UCHAR md5[MD5_SIZE], sha1[SHA1_SIZE];
5486 
5487 		SmEditCrlDlgSetName(hWnd, x->subject_name);
5488 		SmEditCrlDlgSetSerial(hWnd, x->serial);
5489 		GetXDigest(x, md5, false);
5490 		GetXDigest(x, sha1, true);
5491 		SmEditCrlDlgSetHash(hWnd, md5, sha1);
5492 
5493 		FreeX(x);
5494 
5495 		SmEditCrlDlgUpdate(hWnd, c);
5496 	}
5497 }
5498 
5499 // Set the hash information to the dialog
SmEditCrlDlgSetHash(HWND hWnd,UCHAR * hash_md5,UCHAR * hash_sha1)5500 void SmEditCrlDlgSetHash(HWND hWnd, UCHAR *hash_md5, UCHAR *hash_sha1)
5501 {
5502 	char tmp[MAX_SIZE];
5503 	// Validate arguments
5504 	if (hWnd == NULL)
5505 	{
5506 		return;
5507 	}
5508 
5509 	if (hash_md5 != NULL && IsZero(hash_md5, MD5_SIZE) == false)
5510 	{
5511 		Check(hWnd, R_MD5_HASH, true);
5512 		BinToStrEx(tmp, sizeof(tmp), hash_md5, MD5_SIZE);
5513 		SetTextA(hWnd, E_MD5_HASH, tmp);
5514 	}
5515 	else
5516 	{
5517 		Check(hWnd, R_MD5_HASH, false);
5518 	}
5519 
5520 	if (hash_sha1 != NULL && IsZero(hash_sha1, SHA1_SIZE) == false)
5521 	{
5522 		Check(hWnd, R_SHA1_HASH, true);
5523 		BinToStrEx(tmp, sizeof(tmp), hash_sha1, SHA1_SIZE);
5524 		SetTextA(hWnd, E_SHA1_HASH, tmp);
5525 	}
5526 	else
5527 	{
5528 		Check(hWnd, R_SHA1_HASH, false);
5529 	}
5530 }
5531 
5532 // Set the serial number to the dialog
SmEditCrlDlgSetSerial(HWND hWnd,X_SERIAL * serial)5533 void SmEditCrlDlgSetSerial(HWND hWnd, X_SERIAL *serial)
5534 {
5535 	char tmp[MAX_SIZE];
5536 	// Validate arguments
5537 	if (hWnd == NULL || serial == NULL)
5538 	{
5539 		return;
5540 	}
5541 
5542 	BinToStrEx(tmp, sizeof(tmp), serial->data, serial->size);
5543 
5544 	Check(hWnd, R_SERI, true);
5545 
5546 	SetTextA(hWnd, E_SERI, tmp);
5547 }
5548 
5549 // Set the name situation to the dialog
SmEditCrlDlgSetName(HWND hWnd,NAME * name)5550 void SmEditCrlDlgSetName(HWND hWnd, NAME *name)
5551 {
5552 	// Validate arguments
5553 	if (hWnd == NULL || name == NULL)
5554 	{
5555 		return;
5556 	}
5557 
5558 	// CN
5559 	if (UniIsEmptyStr(name->CommonName))
5560 	{
5561 		Check(hWnd, R_CN, false);
5562 	}
5563 	else
5564 	{
5565 		Check(hWnd, R_CN, true);
5566 		SetText(hWnd, E_CN, name->CommonName);
5567 	}
5568 
5569 	// O
5570 	if (UniIsEmptyStr(name->Organization))
5571 	{
5572 		Check(hWnd, R_O, false);
5573 	}
5574 	else
5575 	{
5576 		Check(hWnd, R_O, true);
5577 		SetText(hWnd, E_O, name->Organization);
5578 	}
5579 
5580 	// OU
5581 	if (UniIsEmptyStr(name->Unit))
5582 	{
5583 		Check(hWnd, R_OU, false);
5584 	}
5585 	else
5586 	{
5587 		Check(hWnd, R_OU, true);
5588 		SetText(hWnd, E_OU, name->Unit);
5589 	}
5590 
5591 	// C
5592 	if (UniIsEmptyStr(name->Country))
5593 	{
5594 		Check(hWnd, R_C, false);
5595 	}
5596 	else
5597 	{
5598 		Check(hWnd, R_C, true);
5599 		SetText(hWnd, E_C, name->Country);
5600 	}
5601 
5602 	// ST
5603 	if (UniIsEmptyStr(name->State))
5604 	{
5605 		Check(hWnd, R_ST, false);
5606 	}
5607 	else
5608 	{
5609 		Check(hWnd, R_ST, true);
5610 		SetText(hWnd, E_ST, name->State);
5611 	}
5612 
5613 	// L
5614 	if (UniIsEmptyStr(name->Local))
5615 	{
5616 		Check(hWnd, R_L, false);
5617 	}
5618 	else
5619 	{
5620 		Check(hWnd, R_L, true);
5621 		SetText(hWnd, E_L, name->Local);
5622 	}
5623 }
5624 
5625 // CRL edit dialog procedure
SmEditCrlDlgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)5626 UINT SmEditCrlDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
5627 {
5628 	SM_EDIT_CRL *c = (SM_EDIT_CRL *)param;
5629 	// Validate arguments
5630 	if (hWnd == NULL)
5631 	{
5632 		return 0;
5633 	}
5634 
5635 	switch (msg)
5636 	{
5637 	case WM_INITDIALOG:
5638 		SmEditCrlDlgInit(hWnd, c);
5639 		break;
5640 
5641 	case WM_COMMAND:
5642 		switch (LOWORD(wParam))
5643 		{
5644 		case R_CN:
5645 		case E_CN:
5646 		case R_O:
5647 		case E_O:
5648 		case R_OU:
5649 		case E_OU:
5650 		case R_C:
5651 		case E_C:
5652 		case R_ST:
5653 		case E_ST:
5654 		case R_L:
5655 		case E_L:
5656 		case R_SERI:
5657 		case E_SERI:
5658 		case R_MD5_HASH:
5659 		case E_MD5_HASH:
5660 		case R_SHA1_HASH:
5661 		case E_SHA1_HASH:
5662 			SmEditCrlDlgUpdate(hWnd, c);
5663 			break;
5664 		}
5665 
5666 		switch (wParam)
5667 		{
5668 		case B_LOAD:
5669 			SmEditCrlDlgOnLoad(hWnd, c);
5670 			break;
5671 
5672 		case IDOK:
5673 			SmEditCrlDlgOnOk(hWnd, c);
5674 			break;
5675 
5676 		case IDCANCEL:
5677 			Close(hWnd);
5678 			break;
5679 
5680 		case R_CN:
5681 			FocusEx(hWnd, E_CN);
5682 			break;
5683 
5684 		case R_O:
5685 			FocusEx(hWnd, E_O);
5686 			break;
5687 
5688 		case R_OU:
5689 			FocusEx(hWnd, E_OU);
5690 			break;
5691 
5692 		case R_C:
5693 			FocusEx(hWnd, E_C);
5694 			break;
5695 
5696 		case R_ST:
5697 			FocusEx(hWnd, E_ST);
5698 			break;
5699 
5700 		case R_L:
5701 			FocusEx(hWnd, E_L);
5702 			break;
5703 
5704 		case R_SERI:
5705 			FocusEx(hWnd, E_SERI);
5706 			break;
5707 
5708 		case R_MD5_HASH:
5709 			FocusEx(hWnd, E_MD5_HASH);
5710 			break;
5711 
5712 		case R_SHA1_HASH:
5713 			FocusEx(hWnd, E_SHA1_HASH);
5714 			break;
5715 		}
5716 		break;
5717 
5718 	case WM_CLOSE:
5719 		EndDialog(hWnd, false);
5720 		break;
5721 	}
5722 
5723 	return 0;
5724 }
5725 
5726 // Initialize the dialog
SmCrlDlgInit(HWND hWnd,SM_HUB * s)5727 void SmCrlDlgInit(HWND hWnd, SM_HUB *s)
5728 {
5729 	// Validate arguments
5730 	if (hWnd == NULL || s == NULL)
5731 	{
5732 		return;
5733 	}
5734 
5735 	SetIcon(hWnd, 0, ICO_CERT_X);
5736 
5737 	LvInit(hWnd, L_LIST);
5738 	LvInsertColumn(hWnd, L_LIST, 0, _UU("SM_CRL_COLUMN_1"), 555);
5739 
5740 	SmCrlDlgRefresh(hWnd, s);
5741 }
5742 
5743 // Update the control
SmCrlDlgUpdate(HWND hWnd,SM_HUB * s)5744 void SmCrlDlgUpdate(HWND hWnd, SM_HUB *s)
5745 {
5746 	// Validate arguments
5747 	if (hWnd == NULL || s == NULL)
5748 	{
5749 		return;
5750 	}
5751 
5752 	SetEnable(hWnd, IDOK, LvIsSingleSelected(hWnd, L_LIST));
5753 	SetEnable(hWnd, B_DELETE, LvIsSingleSelected(hWnd, L_LIST));
5754 }
5755 
5756 // Content update
SmCrlDlgRefresh(HWND hWnd,SM_HUB * s)5757 void SmCrlDlgRefresh(HWND hWnd, SM_HUB *s)
5758 {
5759 	UINT i;
5760 	RPC_ENUM_CRL t;
5761 	LVB *v;
5762 	// Validate arguments
5763 	if (hWnd == NULL || s == NULL)
5764 	{
5765 		return;
5766 	}
5767 
5768 	Zero(&t, sizeof(t));
5769 	StrCpy(t.HubName, sizeof(t.HubName), s->HubName);
5770 
5771 	if (CALL(hWnd, ScEnumCrl(s->Rpc, &t)) == false)
5772 	{
5773 		EndDialog(hWnd, false);
5774 		return;
5775 	}
5776 
5777 	v = LvInsertStart();
5778 
5779 	for (i = 0;i < t.NumItem;i++)
5780 	{
5781 		RPC_ENUM_CRL_ITEM *e = &t.Items[i];
5782 		LvInsertAdd(v, ICO_CERT_X, (void *)e->Key, 1, e->CrlInfo);
5783 	}
5784 
5785 	LvInsertEndEx(v, hWnd, L_LIST, true);
5786 
5787 	if (t.NumItem >= 1)
5788 	{
5789 		LvAutoSize(hWnd, L_LIST);
5790 	}
5791 
5792 	FreeRpcEnumCrl(&t);
5793 
5794 	SmCrlDlgUpdate(hWnd, s);
5795 }
5796 
5797 // Certificate revocation list dialog procedure
SmCrlDlgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)5798 UINT SmCrlDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
5799 {
5800 	SM_EDIT_CRL c;
5801 	SM_HUB *s = (SM_HUB *)param;
5802 	NMHDR *n;
5803 	// Validate arguments
5804 	if (hWnd == NULL)
5805 	{
5806 		return 0;
5807 	}
5808 
5809 	switch (msg)
5810 	{
5811 	case WM_INITDIALOG:
5812 		SmCrlDlgInit(hWnd, s);
5813 		break;
5814 
5815 	case WM_COMMAND:
5816 		switch (wParam)
5817 		{
5818 		case B_ADD:
5819 			Zero(&c, sizeof(c));
5820 			c.NewCrl = true;
5821 			c.s = s;
5822 
5823 			if (Dialog(hWnd, D_SM_EDIT_CRL, SmEditCrlDlgProc, &c))
5824 			{
5825 				SmCrlDlgRefresh(hWnd, s);
5826 			}
5827 			break;
5828 
5829 		case B_DELETE:
5830 			if (IsEnable(hWnd, B_DELETE))
5831 			{
5832 				if (MsgBox(hWnd, MB_ICONQUESTION | MB_YESNO | MB_DEFBUTTON2, _UU("SM_CRL_DELETE_MSG")) == IDYES)
5833 				{
5834 					RPC_CRL t;
5835 
5836 					Zero(&t, sizeof(t));
5837 					StrCpy(t.HubName, sizeof(t.HubName), s->HubName);
5838 					t.Key = (UINT)LvGetParam(hWnd, L_LIST, LvGetSelected(hWnd, L_LIST));
5839 
5840 					if (CALL(hWnd, ScDelCrl(s->Rpc, &t)))
5841 					{
5842 						SmCrlDlgRefresh(hWnd, s);
5843 					}
5844 
5845 					FreeRpcCrl(&t);
5846 				}
5847 			}
5848 			break;
5849 
5850 		case IDOK:
5851 			if (IsEnable(hWnd, IDOK))
5852 			{
5853 				SM_EDIT_CRL c;
5854 
5855 				Zero(&c, sizeof(c));
5856 				c.NewCrl = false;
5857 				c.s = s;
5858 				c.Key = (UINT)LvGetParam(hWnd, L_LIST, LvGetSelected(hWnd, L_LIST));
5859 
5860 				if (Dialog(hWnd, D_SM_EDIT_CRL, SmEditCrlDlgProc, &c))
5861 				{
5862 					SmCrlDlgRefresh(hWnd, s);
5863 				}
5864 			}
5865 			break;
5866 
5867 		case IDCANCEL:
5868 			Close(hWnd);
5869 			break;
5870 		}
5871 		break;
5872 
5873 	case WM_CLOSE:
5874 		EndDialog(hWnd, 0);
5875 		break;
5876 
5877 	case WM_NOTIFY:
5878 		n = (NMHDR *)lParam;
5879 		switch (n->code)
5880 		{
5881 		case LVN_ITEMCHANGED:
5882 			switch (n->idFrom)
5883 			{
5884 			case L_LIST:
5885 				SmCrlDlgUpdate(hWnd, s);
5886 				break;
5887 			}
5888 			break;
5889 		}
5890 		break;
5891 	}
5892 
5893 	LvStandardHandler(hWnd, msg, wParam, lParam, L_LIST);
5894 
5895 	return 0;
5896 }
5897 
5898 // Smart Card Manager
SmSecureManager(HWND hWnd)5899 void SmSecureManager(HWND hWnd)
5900 {
5901 	UINT id = SmGetCurrentSecureIdFromReg();
5902 
5903 	if (id == 0)
5904 	{
5905 		id = SmSelectSecureId(hWnd);
5906 	}
5907 
5908 	if (id == 0)
5909 	{
5910 		return;
5911 	}
5912 
5913 	CmSecureManager(hWnd, id);
5914 }
5915 
5916 // Initialize the dialog
SmSelectKeyPairDlgInit(HWND hWnd,SM_SECURE_KEYPAIR * k)5917 void SmSelectKeyPairDlgInit(HWND hWnd, SM_SECURE_KEYPAIR *k)
5918 {
5919 	SECURE_DEVICE *dev;
5920 	// Validate arguments
5921 	if (hWnd == NULL || k == NULL)
5922 	{
5923 		return;
5924 	}
5925 
5926 	dev = GetSecureDevice(k->Id);
5927 	if (dev != NULL)
5928 	{
5929 		FormatText(hWnd, S_INFO, dev->DeviceName);
5930 	}
5931 
5932 	LvInit(hWnd, L_CERT);
5933 	LvInsertColumn(hWnd, L_CERT, 0, _UU("SEC_MGR_COLUMN1"), 200);
5934 	LvInsertColumn(hWnd, L_CERT, 1, _UU("SEC_MGR_COLUMN2"), 110);
5935 
5936 	LvInit(hWnd, L_KEY);
5937 	LvInsertColumn(hWnd, L_KEY, 0, _UU("SEC_MGR_COLUMN1"), 200);
5938 	LvInsertColumn(hWnd, L_KEY, 1, _UU("SEC_MGR_COLUMN2"), 110);
5939 
5940 	SetEnable(hWnd, L_CERT, k->UseCert);
5941 	SetEnable(hWnd, B_BOLD1, k->UseCert);
5942 	SetEnable(hWnd, L_KEY, k->UseKey);
5943 	SetEnable(hWnd, B_BOLD2, k->UseKey);
5944 
5945 	SetFont(hWnd, B_BOLD1, Font(0, true));
5946 	SetFont(hWnd, B_BOLD2, Font(0, true));
5947 
5948 	SmSelectKeyPairDlgUpdate(hWnd, k);
5949 }
5950 
5951 // Update the dialog control
SmSelectKeyPairDlgUpdate(HWND hWnd,SM_SECURE_KEYPAIR * k)5952 void SmSelectKeyPairDlgUpdate(HWND hWnd, SM_SECURE_KEYPAIR *k)
5953 {
5954 	bool ok = true;
5955 	// Validate arguments
5956 	if (hWnd == NULL || k == NULL)
5957 	{
5958 		return;
5959 	}
5960 
5961 	if (k->UseCert)
5962 	{
5963 		if (LvIsSingleSelected(hWnd, L_CERT) == false)
5964 		{
5965 			ok = false;
5966 		}
5967 		else
5968 		{
5969 			char *name = LvGetSelectedStrA(hWnd, L_CERT, 0);
5970 			if (name != NULL)
5971 			{
5972 				if (LvIsSingleSelected(hWnd, L_KEY) == false)
5973 				{
5974 					if ((k->Flag++) == 0)
5975 					{
5976 						LvSelect(hWnd, L_KEY, LvSearchStrA(hWnd, L_KEY, 0, name));
5977 					}
5978 				}
5979 				Free(name);
5980 			}
5981 		}
5982 	}
5983 
5984 	if (k->UseKey)
5985 	{
5986 		if (LvIsSingleSelected(hWnd, L_KEY) == false)
5987 		{
5988 			ok = false;
5989 		}
5990 		else
5991 		{
5992 			char *name = LvGetSelectedStrA(hWnd, L_KEY, 0);
5993 			if (name != NULL)
5994 			{
5995 				if (LvIsSingleSelected(hWnd, L_CERT) == false)
5996 				{
5997 					if ((k->Flag++) == 0)
5998 					{
5999 						LvSelect(hWnd, L_CERT, LvSearchStrA(hWnd, L_CERT, 0, name));
6000 					}
6001 				}
6002 				Free(name);
6003 			}
6004 		}
6005 	}
6006 
6007 	SetEnable(hWnd, IDOK, ok);
6008 }
6009 
6010 // Update the contents
SmSelectKeyPairDlgRefresh(HWND hWnd,SM_SECURE_KEYPAIR * k)6011 void SmSelectKeyPairDlgRefresh(HWND hWnd, SM_SECURE_KEYPAIR *k)
6012 {
6013 	bool ret;
6014 	LIST *o;
6015 	WINUI_SECURE_BATCH batch[] =
6016 	{
6017 		{WINUI_SECURE_ENUM_OBJECTS, NULL, false, NULL, NULL, NULL, NULL, NULL, NULL},
6018 	};
6019 	// Validate arguments
6020 	if (hWnd == NULL || k == NULL)
6021 	{
6022 		return;
6023 	}
6024 
6025 	ret = SecureDeviceWindow(hWnd, batch, sizeof(batch) / sizeof(batch[0]), k->Id, k->BitmapId);
6026 
6027 	if (ret == false)
6028 	{
6029 		Close(hWnd);
6030 		return;
6031 	}
6032 
6033 	o = batch[0].EnumList;
6034 	if (o != NULL)
6035 	{
6036 		if (k->UseCert)
6037 		{
6038 			CmSecureManagerDlgPrintListEx(hWnd, L_CERT, o, SEC_X);
6039 		}
6040 
6041 		if (k->UseKey)
6042 		{
6043 			CmSecureManagerDlgPrintListEx(hWnd, L_KEY, o, SEC_K);
6044 		}
6045 
6046 		FreeEnumSecObject(o);
6047 	}
6048 
6049 	// Update the control
6050 	SmSelectKeyPairDlgUpdate(hWnd, k);
6051 }
6052 
6053 // Key pair import dialog procedure
SmSelectKeyPairDlg(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)6054 UINT SmSelectKeyPairDlg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
6055 {
6056 	NMHDR *n;
6057 	SM_SECURE_KEYPAIR *k = (SM_SECURE_KEYPAIR *)param;
6058 	char *s1, *s2;
6059 	// Validate arguments
6060 	if (hWnd == NULL)
6061 	{
6062 		return 0;
6063 	}
6064 
6065 	switch (msg)
6066 	{
6067 	case WM_INITDIALOG:
6068 		SmSelectKeyPairDlgInit(hWnd, k);
6069 
6070 		SetTimer(hWnd, 1, 1, NULL);
6071 		SetTimer(hWnd, 2, 100, NULL);
6072 		break;
6073 
6074 	case WM_TIMER:
6075 		switch (wParam)
6076 		{
6077 		case 1:
6078 			KillTimer(hWnd, 1);
6079 			SmSelectKeyPairDlgRefresh(hWnd, k);
6080 			break;
6081 
6082 		case 2:
6083 			SmSelectKeyPairDlgUpdate(hWnd, k);
6084 			break;
6085 		}
6086 		break;
6087 
6088 	case WM_COMMAND:
6089 		switch (wParam)
6090 		{
6091 		case IDOK:
6092 			s1 = LvGetSelectedStrA(hWnd, L_CERT, 0);
6093 			s2 = LvGetSelectedStrA(hWnd, L_KEY, 0);
6094 			if (k->UseCert)
6095 			{
6096 				StrCpy(k->CertName, sizeof(k->CertName), s1);
6097 			}
6098 			if (k->UseKey)
6099 			{
6100 				StrCpy(k->KeyName, sizeof(k->KeyName), s2);
6101 			}
6102 			Free(s1);
6103 			Free(s2);
6104 			EndDialog(hWnd, true);
6105 			break;
6106 
6107 		case IDCANCEL:
6108 			Close(hWnd);
6109 			break;
6110 		}
6111 		break;
6112 
6113 	case WM_CLOSE:
6114 		EndDialog(hWnd, 0);
6115 		break;
6116 
6117 	case WM_NOTIFY:
6118 		n = (NMHDR *)lParam;
6119 		switch (n->idFrom)
6120 		{
6121 		case L_CERT:
6122 		case L_KEY:
6123 			switch (n->code)
6124 			{
6125 			case LVN_ITEMCHANGED:
6126 				SmSelectKeyPairDlgUpdate(hWnd, k);
6127 				break;
6128 			}
6129 			break;
6130 		}
6131 		break;
6132 	}
6133 
6134 	return 0;
6135 }
6136 
6137 // Read a key pair from the smart card
SmSelectKeyPair(HWND hWnd,char * cert_name,UINT cert_name_size,char * key_name,UINT key_name_size)6138 bool SmSelectKeyPair(HWND hWnd, char *cert_name, UINT cert_name_size, char *key_name, UINT key_name_size)
6139 {
6140 	return SmSelectKeyPairEx(hWnd, cert_name, cert_name_size, key_name, key_name_size, 0);
6141 }
SmSelectKeyPairEx(HWND hWnd,char * cert_name,UINT cert_name_size,char * key_name,UINT key_name_size,UINT bitmap_id)6142 bool SmSelectKeyPairEx(HWND hWnd, char *cert_name, UINT cert_name_size, char *key_name, UINT key_name_size, UINT bitmap_id)
6143 {
6144 	SM_SECURE_KEYPAIR p;
6145 	// Validate arguments
6146 	if (hWnd == NULL || (cert_name == NULL && key_name == NULL))
6147 	{
6148 		return false;
6149 	}
6150 
6151 	Zero(&p, sizeof(p));
6152 	p.Id = SmGetCurrentSecureId(hWnd);
6153 	if (p.Id == 0)
6154 	{
6155 		return false;
6156 	}
6157 
6158 	p.UseCert = (cert_name == NULL) ? false : true;
6159 	p.UseKey = (key_name == NULL) ? false : true;
6160 	p.BitmapId = bitmap_id;
6161 
6162 	if (Dialog(hWnd, D_SM_SELECT_KEYPAIR, SmSelectKeyPairDlg, &p) == false)
6163 	{
6164 		return false;
6165 	}
6166 
6167 	if (p.UseCert)
6168 	{
6169 		StrCpy(cert_name, cert_name_size, p.CertName);
6170 	}
6171 	if (p.UseKey)
6172 	{
6173 		StrCpy(key_name, key_name_size, p.KeyName);
6174 	}
6175 
6176 	return true;
6177 }
6178 
6179 // Make the user select the smart card number
SmSelectSecureId(HWND hWnd)6180 UINT SmSelectSecureId(HWND hWnd)
6181 {
6182 	UINT id = MsRegReadInt(REG_CURRENT_USER, SECURE_MANAGER_KEY, "DeviceId");
6183 	UINT ret;
6184 
6185 	if (id != 0 && CheckSecureDeviceId(id) == false)
6186 	{
6187 		id = 0;
6188 	}
6189 
6190 	ret = CmSelectSecure(hWnd, id);
6191 	if (ret == 0)
6192 	{
6193 		return 0;
6194 	}
6195 
6196 	SmWriteSelectSecureIdReg(ret);
6197 
6198 	return ret;
6199 }
6200 
6201 // Write the current smart card number to the registry
SmWriteSelectSecureIdReg(UINT id)6202 void SmWriteSelectSecureIdReg(UINT id)
6203 {
6204 	MsRegWriteInt(REG_CURRENT_USER, SECURE_MANAGER_KEY, "DeviceId", id);
6205 }
6206 
6207 // Get the current smart card number
SmGetCurrentSecureId(HWND hWnd)6208 UINT SmGetCurrentSecureId(HWND hWnd)
6209 {
6210 	// Load the current settings
6211 	UINT id = MsRegReadInt(REG_CURRENT_USER, SECURE_MANAGER_KEY, "DeviceId");
6212 
6213 	// Check whether it's valid
6214 	if (id == 0 || CheckSecureDeviceId(id) == false)
6215 	{
6216 		// Select a smart card device number if it's invalid
6217 		id = SmSelectSecureId(hWnd);
6218 	}
6219 
6220 	return id;
6221 }
6222 
6223 // Get the current smart card number from the registry
SmGetCurrentSecureIdFromReg()6224 UINT SmGetCurrentSecureIdFromReg()
6225 {
6226 	// Load the current settings
6227 	UINT id = MsRegReadInt(REG_CURRENT_USER, SECURE_MANAGER_KEY, "DeviceId");
6228 
6229 	// Check whether normal
6230 	if (id == 0 || CheckSecureDeviceId(id) == false)
6231 	{
6232 		id = 0;
6233 	}
6234 
6235 	return id;
6236 }
6237 
6238 // Get whether the specified L3 switch started
SmL3IsSwActive(SM_SERVER * s,char * name)6239 bool SmL3IsSwActive(SM_SERVER *s, char *name)
6240 {
6241 	bool ret = false;
6242 	UINT i;
6243 	RPC_ENUM_L3SW t;
6244 	// Validate arguments
6245 	if (s == NULL || name == NULL)
6246 	{
6247 		return false;
6248 	}
6249 
6250 	Zero(&t, sizeof(t));
6251 	if (ScEnumL3Switch(s->Rpc, &t) == ERR_NO_ERROR)
6252 	{
6253 		for (i = 0;i < t.NumItem;i++)
6254 		{
6255 			RPC_ENUM_L3SW_ITEM *e = &t.Items[i];
6256 			if (StrCmpi(e->Name, name) == 0)
6257 			{
6258 				if (e->Active)
6259 				{
6260 					ret = true;
6261 					break;
6262 				}
6263 			}
6264 		}
6265 		FreeRpcEnumL3Sw(&t);
6266 	}
6267 
6268 	return ret;
6269 }
6270 
6271 // Initialize the dialog
SmL3SwTableDlgInit(HWND hWnd,SM_L3SW * w)6272 void SmL3SwTableDlgInit(HWND hWnd, SM_L3SW *w)
6273 {
6274 	// Validate arguments
6275 	if (hWnd == NULL || w == NULL)
6276 	{
6277 		return;
6278 	}
6279 
6280 	SmL3SwTableDlgUpdate(hWnd, w);
6281 }
6282 
6283 // Update the control
SmL3SwTableDlgUpdate(HWND hWnd,SM_L3SW * w)6284 void SmL3SwTableDlgUpdate(HWND hWnd, SM_L3SW *w)
6285 {
6286 	bool b = true;
6287 	UINT ip;
6288 	// Validate arguments
6289 	if (hWnd == NULL || w == NULL)
6290 	{
6291 		return;
6292 	}
6293 
6294 	if (IpIsFilled(hWnd, E_NETWORK) == false ||
6295 		IpIsFilled(hWnd, E_MASK) == false ||
6296 		IpIsFilled(hWnd, E_GATEWAY) == false)
6297 	{
6298 		b = false;
6299 	}
6300 
6301 	ip = IpGet(hWnd, E_GATEWAY);
6302 	if (ip == 0 || ip == 0xffffffff)
6303 	{
6304 		b = false;
6305 	}
6306 
6307 	if (GetInt(hWnd, E_METRIC) == 0)
6308 	{
6309 		b = false;
6310 	}
6311 
6312 	if (IsNetworkAddress32(IpGet(hWnd, E_NETWORK), IpGet(hWnd, E_MASK)) == false)
6313 	{
6314 		b = false;
6315 	}
6316 
6317 	SetEnable(hWnd, IDOK, b);
6318 }
6319 
SmL3SwTableDlg(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)6320 UINT SmL3SwTableDlg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
6321 {
6322 	SM_L3SW *w = (SM_L3SW *)param;
6323 	RPC_L3TABLE t;
6324 
6325 	if (hWnd == NULL)
6326 	{
6327 		return 0;
6328 	}
6329 
6330 	switch (msg)
6331 	{
6332 	case WM_INITDIALOG:
6333 		SmL3SwTableDlgInit(hWnd, w);
6334 		break;
6335 
6336 	case WM_COMMAND:
6337 		switch (LOWORD(wParam))
6338 		{
6339 		case E_NETWORK:
6340 		case E_MASK:
6341 		case E_GATEWAY:
6342 		case E_METRIC:
6343 			SmL3SwTableDlgUpdate(hWnd, w);
6344 			break;
6345 		}
6346 
6347 		switch (wParam)
6348 		{
6349 		case IDOK:
6350 			Zero(&t, sizeof(t));
6351 			StrCpy(t.Name, sizeof(t.Name), w->SwitchName);
6352 			t.NetworkAddress = IpGet(hWnd, E_NETWORK);
6353 			t.SubnetMask = IpGet(hWnd, E_MASK);
6354 			t.GatewayAddress = IpGet(hWnd, E_GATEWAY);
6355 			t.Metric = GetInt(hWnd, E_METRIC);
6356 
6357 			if (CALL(hWnd, ScAddL3Table(w->s->Rpc, &t)))
6358 			{
6359 				EndDialog(hWnd, 1);
6360 			}
6361 			break;
6362 
6363 		case IDCANCEL:
6364 			Close(hWnd);
6365 			break;
6366 		}
6367 		break;
6368 
6369 	case WM_CLOSE:
6370 		EndDialog(hWnd, 0);
6371 		break;
6372 	}
6373 
6374 	return 0;
6375 }
6376 
6377 // Initialize the dialog
SmL3SwIfDlgInit(HWND hWnd,SM_L3SW * w)6378 void SmL3SwIfDlgInit(HWND hWnd, SM_L3SW *w)
6379 {
6380 	RPC_ENUM_HUB t;
6381 	UINT i;
6382 	// Validate arguments
6383 	if (hWnd == NULL || w == NULL)
6384 	{
6385 		return;
6386 	}
6387 
6388 	Zero(&t, sizeof(t));
6389 
6390 	if (CALL(hWnd, ScEnumHub(w->s->Rpc, &t)) == false)
6391 	{
6392 		Close(hWnd);
6393 		return;
6394 	}
6395 
6396 	CbReset(hWnd, E_HUBNAME);
6397 	CbSetHeight(hWnd, E_HUBNAME, 18);
6398 
6399 	for (i = 0;i < t.NumHub;i++)
6400 	{
6401 		RPC_ENUM_HUB_ITEM *e = &t.Hubs[i];
6402 
6403 		if (e->HubType != HUB_TYPE_FARM_DYNAMIC)
6404 		{
6405 			CbAddStrA(hWnd, E_HUBNAME, e->HubName, 0);
6406 		}
6407 	}
6408 
6409 	FreeRpcEnumHub(&t);
6410 
6411 	SetTextA(hWnd, E_HUBNAME, "");
6412 
6413 	SmL3SwIfDlgUpdate(hWnd, w);
6414 }
6415 
6416 // Update the control
SmL3SwIfDlgUpdate(HWND hWnd,SM_L3SW * w)6417 void SmL3SwIfDlgUpdate(HWND hWnd, SM_L3SW *w)
6418 {
6419 	bool b = true;
6420 	// Validate arguments
6421 	if (hWnd == NULL || w == NULL)
6422 	{
6423 		return;
6424 	}
6425 
6426 	if (IsEmpty(hWnd, E_HUBNAME))
6427 	{
6428 		b = false;
6429 	}
6430 
6431 	if (IpIsFilled(hWnd, E_IP) == false || IpIsFilled(hWnd, E_MASK) == false)
6432 	{
6433 		b = false;
6434 	}
6435 
6436 	if (IpGet(hWnd, E_IP) == 0 || IpGet(hWnd, E_IP) == 0xffffffff)
6437 	{
6438 		b = false;
6439 	}
6440 
6441 	if (IsSubnetMask32(IpGet(hWnd, E_MASK)) == false)
6442 	{
6443 		b = false;
6444 	}
6445 
6446 	SetEnable(hWnd, IDOK, b);
6447 }
6448 
6449 // Dialog for adding a virtual interface
SmL3SwIfDlg(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)6450 UINT SmL3SwIfDlg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
6451 {
6452 	SM_L3SW *w = (SM_L3SW *)param;
6453 	char *hubname;
6454 	// Validate arguments
6455 	if (hWnd == NULL)
6456 	{
6457 		return 0;
6458 	}
6459 
6460 	switch (msg)
6461 	{
6462 	case WM_INITDIALOG:
6463 		SmL3SwIfDlgInit(hWnd, w);
6464 
6465 		SetTimer(hWnd, 1, 250, NULL);
6466 		break;
6467 
6468 	case WM_TIMER:
6469 		switch (wParam)
6470 		{
6471 		case 1:
6472 			if (IsEnable(hWnd, 0))
6473 			{
6474 				SmL3SwIfDlgUpdate(hWnd, w);
6475 			}
6476 			break;
6477 		}
6478 		break;
6479 
6480 	case WM_COMMAND:
6481 		switch (LOWORD(wParam))
6482 		{
6483 		case E_HUBNAME:
6484 		case E_IP:
6485 		case E_MASK:
6486 			SmL3SwIfDlgUpdate(hWnd, w);
6487 			break;
6488 		}
6489 
6490 		switch (wParam)
6491 		{
6492 		case IDOK:
6493 			hubname = GetTextA(hWnd, E_HUBNAME);
6494 			if (hubname != NULL)
6495 			{
6496 				RPC_L3IF t;
6497 				Zero(&t, sizeof(t));
6498 				StrCpy(t.HubName, sizeof(t.HubName), hubname);
6499 				t.IpAddress = IpGet(hWnd, E_IP);
6500 				t.SubnetMask = IpGet(hWnd, E_MASK);
6501 				StrCpy(t.Name, sizeof(t.Name), w->SwitchName);
6502 
6503 				if (CALL(hWnd, ScAddL3If(w->s->Rpc, &t)))
6504 				{
6505 					EndDialog(hWnd, 1);
6506 				}
6507 
6508 				Free(hubname);
6509 			}
6510 			break;
6511 
6512 		case IDCANCEL:
6513 			Close(hWnd);
6514 			break;
6515 		}
6516 		break;
6517 
6518 	case WM_CLOSE:
6519 		EndDialog(hWnd, 0);
6520 		break;
6521 	}
6522 
6523 	return 0;
6524 }
6525 
6526 // Initialize
SmL3SwDlgInit(HWND hWnd,SM_L3SW * w)6527 void SmL3SwDlgInit(HWND hWnd, SM_L3SW *w)
6528 {
6529 	// Validate arguments
6530 	if (hWnd == NULL || w == NULL)
6531 	{
6532 		return;
6533 	}
6534 
6535 	SetIcon(hWnd, 0, ICO_SWITCH_OFFLINE);
6536 
6537 	FormatText(hWnd, 0, w->SwitchName);
6538 
6539 	SetFont(hWnd, S_BOLD1, Font(0, true));
6540 	SetFont(hWnd, S_BOLD2, Font(0, true));
6541 
6542 	LvInit(hWnd, L_IF);
6543 	LvInsertColumn(hWnd, L_IF, 0, _UU("SM_L3_SW_IF_COLUMN1"), 150);
6544 	LvInsertColumn(hWnd, L_IF, 1, _UU("SM_L3_SW_IF_COLUMN2"), 150);
6545 	LvInsertColumn(hWnd, L_IF, 2, _UU("SM_L3_SW_IF_COLUMN3"), 180);
6546 
6547 	LvInit(hWnd, L_TABLE);
6548 	LvInsertColumn(hWnd, L_TABLE, 0, _UU("SM_L3_SW_TABLE_COLUMN1"), 130);
6549 	LvInsertColumn(hWnd, L_TABLE, 1, _UU("SM_L3_SW_TABLE_COLUMN2"), 130);
6550 	LvInsertColumn(hWnd, L_TABLE, 2, _UU("SM_L3_SW_TABLE_COLUMN3"), 130);
6551 	LvInsertColumn(hWnd, L_TABLE, 3, _UU("SM_L3_SW_TABLE_COLUMN4"), 100);
6552 
6553 	w->Enable = SmL3IsSwActive(w->s, w->SwitchName) ? false : true;
6554 
6555 	SmL3SwDlgRefresh(hWnd, w);
6556 }
6557 
6558 // Update the control
SmL3SwDlgUpdate(HWND hWnd,SM_L3SW * w)6559 void SmL3SwDlgUpdate(HWND hWnd, SM_L3SW *w)
6560 {
6561 	// Validate arguments
6562 	if (hWnd == NULL || w == NULL)
6563 	{
6564 		return;
6565 	}
6566 
6567 	SetEnable(hWnd, B_ADD_IF, w->s->ServerAdminMode && w->Enable);
6568 	SetEnable(hWnd, B_ADD_TABLE, w->s->ServerAdminMode && w->Enable);
6569 	SetEnable(hWnd, B_DEL_IF, LvIsSingleSelected(hWnd, L_IF) && w->s->ServerAdminMode && w->Enable);
6570 	SetEnable(hWnd, B_DEL_TABLE, LvIsSingleSelected(hWnd, L_TABLE) && w->s->ServerAdminMode && w->Enable);
6571 	SetEnable(hWnd, B_START, w->s->ServerAdminMode && w->Enable);
6572 	SetEnable(hWnd, B_STOP, w->s->ServerAdminMode && (w->Enable == false));
6573 }
6574 
6575 // Content update
SmL3SwDlgRefresh(HWND hWnd,SM_L3SW * w)6576 void SmL3SwDlgRefresh(HWND hWnd, SM_L3SW *w)
6577 {
6578 	UINT i;
6579 	wchar_t tmp1[MAX_SIZE];
6580 	wchar_t tmp2[MAX_SIZE];
6581 	wchar_t tmp3[MAX_SIZE];
6582 	wchar_t tmp4[MAX_SIZE];
6583 	// Validate arguments
6584 	if (hWnd == NULL || w == NULL)
6585 	{
6586 		return;
6587 	}
6588 
6589 	// Virtual interface list
6590 	{
6591 		RPC_ENUM_L3IF t;
6592 		LVB *v;
6593 
6594 		Zero(&t, sizeof(t));
6595 		StrCpy(t.Name, sizeof(t.Name), w->SwitchName);
6596 
6597 		if (CALL(hWnd, ScEnumL3If(w->s->Rpc, &t)) == false)
6598 		{
6599 			Close(hWnd);
6600 			return;
6601 		}
6602 
6603 		v = LvInsertStart();
6604 
6605 		for (i = 0;i < t.NumItem;i++)
6606 		{
6607 			RPC_L3IF *e = &t.Items[i];
6608 
6609 			IPToUniStr32(tmp1, sizeof(tmp1), e->IpAddress);
6610 			IPToUniStr32(tmp2, sizeof(tmp2), e->SubnetMask);
6611 			StrToUni(tmp3, sizeof(tmp3), e->HubName);
6612 
6613 			LvInsertAdd(v, ICO_NIC_ONLINE, NULL, 3, tmp1, tmp2, tmp3);
6614 		}
6615 
6616 		LvReset(hWnd, L_IF);
6617 
6618 		LvInsertEnd(v, hWnd, L_IF);
6619 
6620 		FreeRpcEnumL3If(&t);
6621 	}
6622 
6623 	// Routing Table Entry List
6624 	{
6625 		RPC_ENUM_L3TABLE t;
6626 		LVB *v;
6627 
6628 		Zero(&t, sizeof(t));
6629 		StrCpy(t.Name, sizeof(t.Name), w->SwitchName);
6630 
6631 		if (CALL(hWnd, ScEnumL3Table(w->s->Rpc, &t)) == false)
6632 		{
6633 			Close(hWnd);
6634 			return;
6635 		}
6636 
6637 		v = LvInsertStart();
6638 
6639 		for (i = 0;i < t.NumItem;i++)
6640 		{
6641 			RPC_L3TABLE *e = &t.Items[i];
6642 
6643 			IPToUniStr32(tmp1, sizeof(tmp1), e->NetworkAddress);
6644 			IPToUniStr32(tmp2, sizeof(tmp2), e->SubnetMask);
6645 			IPToUniStr32(tmp3, sizeof(tmp3), e->GatewayAddress);
6646 			UniToStru(tmp4, e->Metric);
6647 
6648 			LvInsertAdd(v, ICO_PROTOCOL, NULL, 4, tmp1, tmp2, tmp3, tmp4);
6649 		}
6650 
6651 		LvReset(hWnd, L_TABLE);
6652 
6653 		LvInsertEnd(v, hWnd, L_TABLE);
6654 
6655 		FreeRpcEnumL3Table(&t);
6656 	}
6657 
6658 	SmL3SwDlgUpdate(hWnd, w);
6659 }
6660 
6661 // Edit dialog of L3 switch
SmL3SwDlg(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)6662 UINT SmL3SwDlg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
6663 {
6664 	SM_L3SW *w = (SM_L3SW *)param;
6665 	NMHDR *n;
6666 	// Validate arguments
6667 	if (hWnd == NULL)
6668 	{
6669 		return 0;
6670 	}
6671 
6672 	switch (msg)
6673 	{
6674 	case WM_INITDIALOG:
6675 		SmL3SwDlgInit(hWnd, w);
6676 
6677 		SetTimer(hWnd, 1, 1000, NULL);
6678 		break;
6679 
6680 	case WM_TIMER:
6681 		switch (wParam)
6682 		{
6683 		case 1:
6684 			if (IsEnable(hWnd, 0))
6685 			{
6686 				KillTimer(hWnd, 1);
6687 				w->Enable = SmL3IsSwActive(w->s, w->SwitchName) ? false : true;
6688 				SmL3SwDlgUpdate(hWnd, w);
6689 				SetTimer(hWnd, 1, 1000, NULL);
6690 			}
6691 			break;
6692 		}
6693 		break;
6694 
6695 	case WM_COMMAND:
6696 		switch (wParam)
6697 		{
6698 		case B_START:
6699 			if (IsEnable(hWnd, B_START))
6700 			{
6701 				RPC_L3SW t;
6702 
6703 				Zero(&t, sizeof(t));
6704 				StrCpy(t.Name, sizeof(t.Name), w->SwitchName);
6705 
6706 				if (CALL(hWnd, ScStartL3Switch(w->s->Rpc, &t)))
6707 				{
6708 					SmL3SwDlgUpdate(hWnd, w);
6709 				}
6710 			}
6711 			break;
6712 
6713 		case B_STOP:
6714 			if (IsEnable(hWnd, B_STOP))
6715 			{
6716 				RPC_L3SW t;
6717 
6718 				Zero(&t, sizeof(t));
6719 				StrCpy(t.Name, sizeof(t.Name), w->SwitchName);
6720 
6721 				if (CALL(hWnd, ScStopL3Switch(w->s->Rpc, &t)))
6722 				{
6723 					SmL3SwDlgUpdate(hWnd, w);
6724 				}
6725 			}
6726 			break;
6727 
6728 		case B_ADD_IF:
6729 			if (Dialog(hWnd, D_SM_L3_SW_IF, SmL3SwIfDlg, w))
6730 			{
6731 				SmL3SwDlgRefresh(hWnd, w);
6732 			}
6733 			break;
6734 
6735 		case B_DEL_IF:
6736 			if (LvIsSingleSelected(hWnd, L_IF))
6737 			{
6738 				RPC_L3IF t;
6739 				char *tmp1, *tmp2, *tmp3;
6740 
6741 				tmp1 = LvGetSelectedStrA(hWnd, L_IF, 0);
6742 				tmp2 = LvGetSelectedStrA(hWnd, L_IF, 1);
6743 				tmp3 = LvGetSelectedStrA(hWnd, L_IF, 2);
6744 
6745 				Zero(&t, sizeof(t));
6746 				StrCpy(t.Name, sizeof(t.Name), w->SwitchName);
6747 				t.IpAddress = StrToIP32(tmp1);
6748 				t.SubnetMask = StrToIP32(tmp2);
6749 				StrCpy(t.HubName, sizeof(t.HubName), tmp3);
6750 
6751 				if (CALL(hWnd, ScDelL3If(w->s->Rpc, &t)))
6752 				{
6753 					SmL3SwDlgRefresh(hWnd, w);
6754 				}
6755 
6756 				Free(tmp1);
6757 				Free(tmp2);
6758 				Free(tmp3);
6759 			}
6760 			break;
6761 
6762 		case B_ADD_TABLE:
6763 			if (Dialog(hWnd, D_SM_L3_SW_TABLE, SmL3SwTableDlg, w))
6764 			{
6765 				SmL3SwDlgRefresh(hWnd, w);
6766 			}
6767 			break;
6768 
6769 		case B_DEL_TABLE:
6770 			if (LvIsSingleSelected(hWnd, L_TABLE))
6771 			{
6772 				RPC_L3TABLE t;
6773 				char *tmp1, *tmp2, *tmp3, *tmp4;
6774 
6775 				tmp1 = LvGetSelectedStrA(hWnd, L_TABLE, 0);
6776 				tmp2 = LvGetSelectedStrA(hWnd, L_TABLE, 1);
6777 				tmp3 = LvGetSelectedStrA(hWnd, L_TABLE, 2);
6778 				tmp4 = LvGetSelectedStrA(hWnd, L_TABLE, 3);
6779 
6780 				Zero(&t, sizeof(t));
6781 				StrCpy(t.Name, sizeof(t.Name), w->SwitchName);
6782 				t.NetworkAddress = StrToIP32(tmp1);
6783 				t.SubnetMask = StrToIP32(tmp2);
6784 				t.GatewayAddress = StrToIP32(tmp3);
6785 				t.Metric = ToInt(tmp4);
6786 
6787 				if (CALL(hWnd, ScDelL3Table(w->s->Rpc, &t)))
6788 				{
6789 					SmL3SwDlgRefresh(hWnd, w);
6790 				}
6791 
6792 				Free(tmp1);
6793 				Free(tmp2);
6794 				Free(tmp3);
6795 				Free(tmp4);
6796 			}
6797 			break;
6798 
6799 		case IDCANCEL:
6800 			Close(hWnd);
6801 			break;
6802 		}
6803 		break;
6804 
6805 	case WM_CLOSE:
6806 		EndDialog(hWnd, 0);
6807 		break;
6808 
6809 	case WM_NOTIFY:
6810 		n = (NMHDR *)lParam;
6811 		switch (n->idFrom)
6812 		{
6813 		case L_IF:
6814 		case L_TABLE:
6815 			switch (n->code)
6816 			{
6817 			case LVN_ITEMCHANGED:
6818 				SmL3SwDlgUpdate(hWnd, w);
6819 				break;
6820 			}
6821 			break;
6822 		}
6823 		break;
6824 	}
6825 
6826 	return 0;
6827 }
6828 
6829 // Update the control
SmL3AddDlgUpdate(HWND hWnd,SM_SERVER * s)6830 void SmL3AddDlgUpdate(HWND hWnd, SM_SERVER *s)
6831 {
6832 	char *tmp;
6833 	// Validate arguments
6834 	if (hWnd == NULL || s == NULL)
6835 	{
6836 		return;
6837 	}
6838 
6839 	tmp = GetTextA(hWnd, E_NAME);
6840 
6841 	SetEnable(hWnd, IDOK, IsEmptyStr(tmp) == false && IsSafeStr(tmp));
6842 
6843 	Free(tmp);
6844 }
6845 
6846 // The dialog box to create a new L3 switch
SmL3AddDlg(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)6847 UINT SmL3AddDlg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
6848 {
6849 	SM_SERVER *s = (SM_SERVER *)param;
6850 	RPC_L3SW t;
6851 	// Validate arguments
6852 	if (hWnd == NULL)
6853 	{
6854 		return 0;
6855 	}
6856 
6857 	switch (msg)
6858 	{
6859 	case WM_INITDIALOG:
6860 		LimitText(hWnd, E_NAME, MAX_HUBNAME_LEN);
6861 		SmL3AddDlgUpdate(hWnd, s);
6862 		break;
6863 
6864 	case WM_COMMAND:
6865 		switch (LOWORD(wParam))
6866 		{
6867 		case E_NAME:
6868 			SmL3AddDlgUpdate(hWnd, s);
6869 			break;
6870 		}
6871 
6872 		switch (wParam)
6873 		{
6874 		case IDOK:
6875 			Zero(&t, sizeof(t));
6876 			GetTxtA(hWnd, E_NAME, t.Name, sizeof(t.Name));
6877 			if (CALL(hWnd, ScAddL3Switch(s->Rpc, &t)))
6878 			{
6879 				EndDialog(hWnd, 1);
6880 			}
6881 			break;
6882 
6883 		case IDCANCEL:
6884 			Close(hWnd);
6885 			break;
6886 		}
6887 		break;
6888 
6889 	case WM_CLOSE:
6890 		EndDialog(hWnd, 0);
6891 		break;
6892 	}
6893 
6894 	return 0;
6895 }
6896 
6897 // Initialize the dialog
SmL3DlgInit(HWND hWnd,SM_SERVER * s)6898 void SmL3DlgInit(HWND hWnd, SM_SERVER *s)
6899 {
6900 	// Validate arguments
6901 	if (hWnd == NULL || s == NULL)
6902 	{
6903 		return;
6904 	}
6905 
6906 	SetFont(hWnd, S_BOLD, Font(0, true));
6907 
6908 	SetIcon(hWnd, 0, ICO_SWITCH);
6909 
6910 	LvInit(hWnd, L_LIST);
6911 	LvInsertColumn(hWnd, L_LIST, 0, _UU("SM_L3_SW_COLUMN1"), 150);
6912 	LvInsertColumn(hWnd, L_LIST, 1, _UU("SM_L3_SW_COLUMN2"), 120);
6913 	LvInsertColumn(hWnd, L_LIST, 2, _UU("SM_L3_SW_COLUMN3"), 100);
6914 	LvInsertColumn(hWnd, L_LIST, 3, _UU("SM_L3_SW_COLUMN4"), 100);
6915 
6916 	SmL3DlgRefresh(hWnd, s);
6917 }
6918 
6919 // Update the dialog control
SmL3DlgUpdate(HWND hWnd,SM_SERVER * s)6920 void SmL3DlgUpdate(HWND hWnd, SM_SERVER *s)
6921 {
6922 	bool b = false;
6923 	bool active = false;
6924 	// Validate arguments
6925 	if (hWnd == NULL || s == NULL)
6926 	{
6927 		return;
6928 	}
6929 
6930 	if (LvIsSingleSelected(hWnd, L_LIST))
6931 	{
6932 		wchar_t *tmp;
6933 		UINT i;
6934 		b = true;
6935 		i = LvGetSelected(hWnd, L_LIST);
6936 		if (i != INFINITE)
6937 		{
6938 			tmp = LvGetStr(hWnd, L_LIST, i, 1);
6939 			if (UniStrCmpi(tmp, _UU("SM_L3_SW_ST_F_F")) != 0)
6940 			{
6941 				active = true;
6942 			}
6943 			Free(tmp);
6944 		}
6945 	}
6946 
6947 	SetEnable(hWnd, B_START, b && (active == false));
6948 	SetEnable(hWnd, B_STOP, b && (active != false));
6949 	SetEnable(hWnd, IDOK, b);
6950 	SetEnable(hWnd, B_DELETE, b);
6951 }
6952 
6953 // Dialog content update
SmL3DlgRefresh(HWND hWnd,SM_SERVER * s)6954 void SmL3DlgRefresh(HWND hWnd, SM_SERVER *s)
6955 {
6956 	RPC_ENUM_L3SW t;
6957 	UINT i;
6958 	LVB *v;
6959 	// Validate arguments
6960 	if (hWnd == NULL || s == NULL)
6961 	{
6962 		return;
6963 	}
6964 
6965 	Zero(&t, sizeof(t));
6966 	if (CALL(hWnd, ScEnumL3Switch(s->Rpc, &t)) == false)
6967 	{
6968 		Close(hWnd);
6969 		return;
6970 	}
6971 
6972 	v = LvInsertStart();
6973 
6974 	for (i = 0;i < t.NumItem;i++)
6975 	{
6976 		RPC_ENUM_L3SW_ITEM *e = &t.Items[i];
6977 		wchar_t tmp1[MAX_SIZE], *tmp2, tmp3[64], tmp4[64];
6978 
6979 		StrToUni(tmp1, sizeof(tmp1), e->Name);
6980 		if (e->Active == false)
6981 		{
6982 			tmp2 = _UU("SM_L3_SW_ST_F_F");
6983 		}
6984 		else if (e->Online == false)
6985 		{
6986 			tmp2 = _UU("SM_L3_SW_ST_T_F");
6987 		}
6988 		else
6989 		{
6990 			tmp2 = _UU("SM_L3_SW_ST_T_T");
6991 		}
6992 		UniToStru(tmp3, e->NumInterfaces);
6993 		UniToStru(tmp4, e->NumTables);
6994 
6995 		LvInsertAdd(v, e->Active ? ICO_SWITCH : ICO_SWITCH_OFFLINE, NULL,
6996 			4, tmp1, tmp2, tmp3, tmp4);
6997 	}
6998 
6999 	LvInsertEnd(v, hWnd, L_LIST);
7000 
7001 	FreeRpcEnumL3Sw(&t);
7002 
7003 	SmL3DlgUpdate(hWnd, s);
7004 }
7005 
7006 // L3 dialog procedure
SmL3Dlg(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)7007 UINT SmL3Dlg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
7008 {
7009 	NMHDR *n;
7010 	SM_SERVER *s = (SM_SERVER *)param;
7011 	RPC_L3SW t;
7012 	char *name;
7013 	// Validate arguments
7014 	if (hWnd == NULL)
7015 	{
7016 		return 0;
7017 	}
7018 
7019 	switch (msg)
7020 	{
7021 	case WM_INITDIALOG:
7022 		SmL3DlgInit(hWnd, s);
7023 
7024 		SetTimer(hWnd, 1, 1000, NULL);
7025 		break;
7026 
7027 	case WM_TIMER:
7028 		switch (wParam)
7029 		{
7030 		case 1:
7031 			if (IsEnable(hWnd, 0))
7032 			{
7033 				KillTimer(hWnd, 1);
7034 				SmL3DlgRefresh(hWnd, s);
7035 				SetTimer(hWnd, 1, 1000, NULL);
7036 			}
7037 			break;
7038 		}
7039 		break;
7040 
7041 	case WM_COMMAND:
7042 		switch (wParam)
7043 		{
7044 		case B_ADD:
7045 			// Add
7046 			if (Dialog(hWnd, D_SM_L3_ADD, SmL3AddDlg, s))
7047 			{
7048 				SmL3DlgRefresh(hWnd, s);
7049 			}
7050 			break;
7051 
7052 		case B_START:
7053 			// Operation start
7054 			name = LvGetSelectedStrA(hWnd, L_LIST, 0);
7055 			if (name != NULL)
7056 			{
7057 				Zero(&t, sizeof(t));
7058 				StrCpy(t.Name, sizeof(t.Name), name);
7059 
7060 				if (CALL(hWnd, ScStartL3Switch(s->Rpc, &t)))
7061 				{
7062 					SmL3DlgRefresh(hWnd, s);
7063 				}
7064 
7065 				Free(name);
7066 			}
7067 			break;
7068 
7069 		case B_STOP:
7070 			// Operation stop
7071 			name = LvGetSelectedStrA(hWnd, L_LIST, 0);
7072 			if (name != NULL)
7073 			{
7074 				Zero(&t, sizeof(t));
7075 				StrCpy(t.Name, sizeof(t.Name), name);
7076 
7077 				if (CALL(hWnd, ScStopL3Switch(s->Rpc, &t)))
7078 				{
7079 					SmL3DlgRefresh(hWnd, s);
7080 				}
7081 
7082 				Free(name);
7083 			}
7084 			break;
7085 
7086 		case IDOK:
7087 			// Edit
7088 			if (IsEnable(hWnd, IDOK))
7089 			{
7090 				name = LvGetSelectedStrA(hWnd, L_LIST, 0);
7091 				if (name != NULL)
7092 				{
7093 					SM_L3SW w;
7094 					Zero(&w, sizeof(w));
7095 					w.s = s;
7096 					w.SwitchName = name;
7097 
7098 					Dialog(hWnd, D_SM_L3_SW, SmL3SwDlg, &w);
7099 
7100 					Free(name);
7101 				}
7102 			}
7103 			break;
7104 
7105 		case B_DELETE:
7106 			// Delete
7107 			name = LvGetSelectedStrA(hWnd, L_LIST, 0);
7108 			if (name != NULL)
7109 			{
7110 				if (MsgBoxEx(hWnd, MB_ICONEXCLAMATION | MB_YESNO | MB_DEFBUTTON2,
7111 					_UU("SM_L3_SW_DEL_MSG"), name) == IDYES)
7112 				{
7113 					Zero(&t, sizeof(t));
7114 					StrCpy(t.Name, sizeof(t.Name), name);
7115 
7116 					if (CALL(hWnd, ScDelL3Switch(s->Rpc, &t)))
7117 					{
7118 						SmL3DlgRefresh(hWnd, s);
7119 					}
7120 				}
7121 
7122 				Free(name);
7123 			}
7124 			break;
7125 
7126 		case IDCANCEL:
7127 			// Close
7128 			Close(hWnd);
7129 			break;
7130 		}
7131 		break;
7132 
7133 	case WM_NOTIFY:
7134 		n = (NMHDR *)lParam;
7135 		switch (n->idFrom)
7136 		{
7137 		case L_LIST:
7138 			switch (n->code)
7139 			{
7140 			case LVN_ITEMCHANGED:
7141 				SmL3DlgUpdate(hWnd, s);
7142 				break;
7143 
7144 			case NM_DBLCLK:
7145 				Command(hWnd, IDOK);
7146 				break;
7147 			}
7148 			break;
7149 		}
7150 		break;
7151 
7152 	case WM_CLOSE:
7153 		EndDialog(hWnd, 0);
7154 		break;
7155 	}
7156 
7157 	return 0;
7158 }
7159 
7160 // L3 dialog
SmL3(HWND hWnd,SM_SERVER * s)7161 void SmL3(HWND hWnd, SM_SERVER *s)
7162 {
7163 	// Validate arguments
7164 	if (s == NULL)
7165 	{
7166 		return;
7167 	}
7168 
7169 	Dialog(hWnd, D_SM_L3, SmL3Dlg, s);
7170 }
7171 
7172 // Dialog for management option value
SmHubAdminOptionValueDlg(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)7173 UINT SmHubAdminOptionValueDlg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
7174 {
7175 	SM_EDIT_AO *a = (SM_EDIT_AO *)param;
7176 	UINT i;
7177 	char tmp[MAX_SIZE];
7178 	// Validate arguments
7179 	if (hWnd == NULL)
7180 	{
7181 		return 0;
7182 	}
7183 
7184 	switch (msg)
7185 	{
7186 	case WM_INITDIALOG:
7187 		CbReset(hWnd, C_NAME);
7188 		for (i = 0;i < a->DefaultOptions.NumItem;i++)
7189 		{
7190 			wchar_t tmp[MAX_PATH];
7191 			StrToUni(tmp, sizeof(tmp), a->DefaultOptions.Items[i].Name);
7192 			CbAddStr(hWnd, C_NAME, tmp, 0);
7193 		}
7194 		if (a->NewMode == false)
7195 		{
7196 			char tmp[MAX_SIZE];
7197 
7198 			SetTextA(hWnd, C_NAME, a->Name);
7199 			ToStr(tmp, a->Value);
7200 
7201 			SetTextA(hWnd, E_VALUE, tmp);
7202 		}
7203 		else
7204 		{
7205 			SetTextA(hWnd, C_NAME, "");
7206 		}
7207 		SmHubAdminOptionValueDlgUpdate(hWnd, a);
7208 		if (a->NewMode == false)
7209 		{
7210 			FocusEx(hWnd, E_VALUE);
7211 			Disable(hWnd, C_NAME);
7212 		}
7213 		else
7214 		{
7215 			FocusEx(hWnd, C_NAME);
7216 		}
7217 
7218 		SetTimer(hWnd, 1, 100, NULL);
7219 		break;
7220 
7221 	case WM_TIMER:
7222 		if (IsEnable(hWnd, 0))
7223 		{
7224 			SmHubAdminOptionValueDlgUpdate(hWnd, a);
7225 		}
7226 		break;
7227 
7228 	case WM_COMMAND:
7229 		switch (wParam)
7230 		{
7231 		case IDOK:
7232 			if (a->NewMode)
7233 			{
7234 				GetTxtA(hWnd, C_NAME, a->Name, sizeof(a->Name));
7235 			}
7236 
7237 			GetTxtA(hWnd, E_VALUE, tmp, sizeof(tmp));
7238 			a->Value = ToInt(tmp);
7239 
7240 			Trim(a->Name);
7241 
7242 			if (StartWith(a->Name, "no") || StartWith(a->Name, "allow") || StartWith(a->Name, "deny")
7243 				 || StartWith(a->Name, "filter") || StartWith(a->Name, "fix") || StartWith(a->Name, "force")
7244 				 || StartWith(a->Name, "use") || StartWith(a->Name, "b_") || StartWith(a->Name, "is")
7245 				 || StartWith(a->Name, "manage") || StartWith(a->Name, "yield")
7246 				 || StartWith(a->Name, "permit") || StartWith(a->Name, "yes") || StartWith(a->Name, "ok")
7247 				 || StartWith(a->Name, "do") || StartWith(a->Name, "only") || StartWith(a->Name, "disable"))
7248 			{
7249 				if (StrCmpi(tmp, "0") != 0 && StrCmpi(tmp, "1") != 0)
7250 				{
7251 					MsgBox(hWnd, MB_ICONEXCLAMATION, _UU("SM_TRUE_OR_FALSE"));
7252 					FocusEx(hWnd, E_VALUE);
7253 					break;
7254 				}
7255 			}
7256 
7257 			EndDialog(hWnd, true);
7258 			break;
7259 
7260 		case IDCANCEL:
7261 			Close(hWnd);
7262 			break;
7263 		}
7264 
7265 		SmHubAdminOptionValueDlgUpdate(hWnd, a);
7266 
7267 		break;
7268 
7269 	case WM_CLOSE:
7270 		EndDialog(hWnd, false);
7271 		break;
7272 	}
7273 
7274 	return 0;
7275 }
7276 
7277 // Update the dialog controls for management option value
SmHubAdminOptionValueDlgUpdate(HWND hWnd,SM_EDIT_AO * a)7278 void SmHubAdminOptionValueDlgUpdate(HWND hWnd, SM_EDIT_AO *a)
7279 {
7280 	char tmp[MAX_SIZE];
7281 	// Validate arguments
7282 	if (hWnd == NULL || a == NULL)
7283 	{
7284 		return;
7285 	}
7286 
7287 	GetTxtA(hWnd, C_NAME, tmp, sizeof(tmp));
7288 
7289 	SetEnable(hWnd, IDOK, IsEmpty(hWnd, C_NAME) == false && IsEmpty(hWnd, E_VALUE) == false &&
7290 		IsSafeStr(tmp));
7291 }
7292 
7293 // Initialize
SmHubAdminOptionDlgInit(HWND hWnd,SM_EDIT_AO * a)7294 void SmHubAdminOptionDlgInit(HWND hWnd, SM_EDIT_AO *a)
7295 {
7296 	UINT i;
7297 	// Validate arguments
7298 	if (hWnd == NULL || a == NULL)
7299 	{
7300 		return;
7301 	}
7302 
7303 	SetIcon(hWnd, 0, ICO_USER_ADMIN);
7304 
7305 	if (a->e->p->ServerAdminMode)
7306 	{
7307 		a->CanChange = true;
7308 	}
7309 	else
7310 	{
7311 		if (a->ExtOption == false)
7312 		{
7313 			for (i = 0;i < a->CurrentOptions.NumItem;i++)
7314 			{
7315 				if (StrCmpi(a->CurrentOptions.Items[i].Name, "allow_hub_admin_change_option") == 0)
7316 				{
7317 					if (a->CurrentOptions.Items[i].Value != 0)
7318 					{
7319 						a->CanChange = true;
7320 					}
7321 				}
7322 			}
7323 		}
7324 		else
7325 		{
7326 			a->CanChange = true;
7327 		}
7328 	}
7329 
7330 	FormatText(hWnd, S_INFO, a->e->HubName);
7331 
7332 	DlgFont(hWnd, S_BOLD, 0, true);
7333 
7334 	LvInit(hWnd, L_LIST);
7335 	LvInsertColumn(hWnd, L_LIST, 0, _UU("SM_AO_COLUMN_1"), 260);
7336 	LvInsertColumn(hWnd, L_LIST, 1, _UU("SM_AO_COLUMN_2"), 100);
7337 
7338 	for (i = 0;i < a->CurrentOptions.NumItem;i++)
7339 	{
7340 		ADMIN_OPTION *e = &a->CurrentOptions.Items[i];
7341 		wchar_t tmp1[MAX_SIZE];
7342 		wchar_t tmp2[MAX_SIZE];
7343 
7344 		StrToUni(tmp1, sizeof(tmp1), e->Name);
7345 		UniToStru(tmp2, e->Value);
7346 
7347 		LvInsert(hWnd, L_LIST, ICO_LOG, NULL, 2, tmp1, tmp2);
7348 
7349 	}
7350 
7351 	if (a->ExtOption)
7352 	{
7353 		SetIcon(hWnd, S_ICON, ICO_LINK2);
7354 		SetIcon(hWnd, 0, ICO_LINK2);
7355 
7356 		SetText(hWnd, 0, _UU("SM_HUBEXT_OPTION_TITLE"));
7357 		SetText(hWnd, S_STATIC1, _UU("SM_HUBEXT_OPTION_STATIC1"));
7358 		SetText(hWnd, S_STATIC2, _UU("SM_HUBEXT_OPTION_STATIC2"));
7359 	}
7360 
7361 	// Update the control
7362 	SmHubAdminOptionDlgUpdate(hWnd, a);
7363 }
7364 
7365 // Update the control
SmHubAdminOptionDlgUpdate(HWND hWnd,SM_EDIT_AO * a)7366 void SmHubAdminOptionDlgUpdate(HWND hWnd, SM_EDIT_AO *a)
7367 {
7368 	bool b = false;
7369 	wchar_t *helpstr;
7370 	// Validate arguments
7371 	if (hWnd == NULL || a == NULL)
7372 	{
7373 		return;
7374 	}
7375 
7376 	helpstr = _UU("HUB_AO_CLICK");
7377 
7378 	SetEnable(hWnd, IDOK, a->CanChange);
7379 	SetEnable(hWnd, B_ADD, a->CanChange);
7380 	SetEnable(hWnd, B_EDIT, a->CanChange && (LvIsMasked(hWnd, L_LIST) && LvIsMultiMasked(hWnd, L_LIST) == false));
7381 
7382 	if (LvIsMasked(hWnd, L_LIST) && LvIsMultiMasked(hWnd, L_LIST) == false)
7383 	{
7384 		UINT i;
7385 		i = LvGetSelected(hWnd, L_LIST);
7386 
7387 		if (a->CanChange)
7388 		{
7389 
7390 			b = true;
7391 
7392 			if (i != INFINITE)
7393 			{
7394 				char *name = LvGetStrA(hWnd, L_LIST, i, 0);
7395 				if (name != NULL)
7396 				{
7397 					UINT j;
7398 
7399 					for (j = 0;j < a->DefaultOptions.NumItem;j++)
7400 					{
7401 						if (StrCmpi(a->DefaultOptions.Items[j].Name, name) == 0)
7402 						{
7403 							b = false;
7404 						}
7405 					}
7406 					Free(name);
7407 				}
7408 			}
7409 		}
7410 
7411 		if (i != INFINITE)
7412 		{
7413 			char *name = LvGetStrA(hWnd, L_LIST, i, 0);
7414 			if (name != NULL)
7415 			{
7416 				helpstr = GetHubAdminOptionHelpString(name);
7417 			}
7418 			Free(name);
7419 		}
7420 	}
7421 	SetEnable(hWnd, B_DELETE, b);
7422 
7423 	SetText(hWnd, E_HELP, helpstr);
7424 }
7425 
7426 // Save
SmHubAdminOptionDlgOk(HWND hWnd,SM_EDIT_AO * a)7427 void SmHubAdminOptionDlgOk(HWND hWnd, SM_EDIT_AO *a)
7428 {
7429 	UINT i, num;
7430 	RPC_ADMIN_OPTION t;
7431 	// Validate arguments
7432 	if (hWnd == NULL || a == NULL)
7433 	{
7434 		return;
7435 	}
7436 
7437 	num = LvNum(hWnd, L_LIST);
7438 
7439 	Zero(&t, sizeof(t));
7440 
7441 	StrCpy(t.HubName, sizeof(t.HubName), a->e->HubName);
7442 	t.NumItem = num;
7443 	t.Items = ZeroMalloc(sizeof(ADMIN_OPTION) * num);
7444 
7445 	for (i = 0;i < num;i++)
7446 	{
7447 		char *name = LvGetStrA(hWnd, L_LIST, i, 0);
7448 		char *s_value = LvGetStrA(hWnd, L_LIST, i, 1);
7449 		ADMIN_OPTION *a = &t.Items[i];
7450 
7451 		StrCpy(a->Name, sizeof(a->Name), name);
7452 		a->Value = ToInt(s_value);
7453 
7454 		Free(name);
7455 		Free(s_value);
7456 	}
7457 
7458 	if (a->ExtOption == false)
7459 	{
7460 		if (CALL(hWnd, ScSetHubAdminOptions(a->e->p->Rpc, &t)))
7461 		{
7462 			MsgBox(hWnd, MB_ICONINFORMATION, _UU("SM_AO_SET_OK"));
7463 			EndDialog(hWnd, true);
7464 		}
7465 	}
7466 	else
7467 	{
7468 		if (CALL(hWnd, ScSetHubExtOptions(a->e->p->Rpc, &t)))
7469 		{
7470 			MsgBox(hWnd, MB_ICONINFORMATION, _UU("SM_EXT_OPTION_SET_OK"));
7471 			EndDialog(hWnd, true);
7472 		}
7473 	}
7474 
7475 	FreeRpcAdminOption(&t);
7476 }
7477 
7478 // Virtual HUB Management Options dialog
SmHubAdminOptionDlg(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)7479 UINT SmHubAdminOptionDlg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
7480 {
7481 	SM_EDIT_AO *a = (SM_EDIT_AO *)param;
7482 	NMHDR *n;
7483 	UINT i;
7484 	// Validate arguments
7485 	if (hWnd == NULL)
7486 	{
7487 		return 0;
7488 	}
7489 
7490 	switch (msg)
7491 	{
7492 	case WM_INITDIALOG:
7493 		SmHubAdminOptionDlgInit(hWnd, a);
7494 		break;
7495 
7496 	case WM_COMMAND:
7497 		switch (wParam)
7498 		{
7499 		case B_ADD:
7500 			a->NewMode = true;
7501 			StrCpy(a->Name, sizeof(a->Name), "");
7502 			a->Value = 0;
7503 			if (Dialog(hWnd, D_SM_AO_VALUE, SmHubAdminOptionValueDlg,
7504 				a))
7505 			{
7506 				wchar_t tmp1[MAX_SIZE];
7507 				wchar_t tmp2[MAX_SIZE];
7508 				StrToUni(tmp1, sizeof(tmp1), a->Name);
7509 				UniToStru(tmp2, a->Value);
7510 
7511 				LvInsert(hWnd, L_LIST, ICO_LOG, NULL, 2, tmp1, tmp2);
7512 			}
7513 			break;
7514 
7515 		case B_EDIT:
7516 			i = LvGetSelected(hWnd, L_LIST);
7517 			if (i != INFINITE && a->CanChange)
7518 			{
7519 				char *name, *value;
7520 				name = LvGetStrA(hWnd, L_LIST, i, 0);
7521 				value = LvGetStrA(hWnd, L_LIST, i, 1);
7522 				a->NewMode = false;
7523 				StrCpy(a->Name, sizeof(a->Name), name);
7524 				a->Value = ToInt(value);
7525 
7526 				if (Dialog(hWnd, D_SM_AO_VALUE, SmHubAdminOptionValueDlg,
7527 					a))
7528 				{
7529 					char tmp[MAX_PATH];
7530 					ToStr(tmp, a->Value);
7531 					LvSetItemA(hWnd, L_LIST, i, 1, tmp);
7532 				}
7533 
7534 				Free(name);
7535 				Free(value);
7536 			}
7537 			break;
7538 
7539 		case B_DELETE:
7540 			i = LvGetSelected(hWnd, L_LIST);
7541 			if (i != INFINITE)
7542 			{
7543 				LvDeleteItem(hWnd, L_LIST, i);
7544 			}
7545 			break;
7546 
7547 		case IDCANCEL:
7548 			Close(hWnd);
7549 			break;
7550 
7551 		case IDOK:
7552 			SmHubAdminOptionDlgOk(hWnd, a);
7553 			break;
7554 		}
7555 		break;
7556 
7557 	case WM_NOTIFY:
7558 		n = (NMHDR *)lParam;
7559 		switch (n->idFrom)
7560 		{
7561 		case L_LIST:
7562 			switch (n->code)
7563 			{
7564 			case LVN_ITEMCHANGED:
7565 				SmHubAdminOptionDlgUpdate(hWnd, a);
7566 				break;
7567 
7568 			case NM_DBLCLK:
7569 				Command(hWnd, B_EDIT);
7570 				break;
7571 			}
7572 			break;
7573 		}
7574 		break;
7575 
7576 	case WM_CLOSE:
7577 		EndDialog(hWnd, 0);
7578 		break;
7579 	}
7580 
7581 	return 0;
7582 }
7583 
7584 // Virtual HUB extended options
SmHubExtOption(HWND hWnd,SM_EDIT_HUB * e)7585 void SmHubExtOption(HWND hWnd, SM_EDIT_HUB *e)
7586 {
7587 	SM_EDIT_AO a;
7588 	// Validate arguments
7589 	if (hWnd == NULL || e == NULL)
7590 	{
7591 		return;
7592 	}
7593 
7594 	Zero(&a, sizeof(a));
7595 	a.e = e;
7596 	a.ExtOption = true;
7597 
7598 	StrCpy(a.CurrentOptions.HubName, sizeof(a.CurrentOptions.HubName), e->HubName);
7599 
7600 	// Get the current options on the server
7601 	if (CALL(hWnd, ScGetHubExtOptions(e->p->Rpc, &a.CurrentOptions)) == false)
7602 	{
7603 		return;
7604 	}
7605 
7606 	Dialog(hWnd, D_SM_ADMIN_OPTION, SmHubAdminOptionDlg, &a);
7607 
7608 	FreeRpcAdminOption(&a.CurrentOptions);
7609 	FreeRpcAdminOption(&a.DefaultOptions);
7610 }
7611 
7612 // Virtual HUB management options
SmHubAdminOption(HWND hWnd,SM_EDIT_HUB * e)7613 void SmHubAdminOption(HWND hWnd, SM_EDIT_HUB *e)
7614 {
7615 	SM_EDIT_AO a;
7616 	// Validate arguments
7617 	if (hWnd == NULL || e == NULL)
7618 	{
7619 		return;
7620 	}
7621 
7622 	Zero(&a, sizeof(a));
7623 	a.e = e;
7624 
7625 	StrCpy(a.CurrentOptions.HubName, sizeof(a.CurrentOptions.HubName), e->HubName);
7626 
7627 	// Get the current options on the server
7628 	if (CALL(hWnd, ScGetHubAdminOptions(e->p->Rpc, &a.CurrentOptions)) == false)
7629 	{
7630 		return;
7631 	}
7632 
7633 	ScGetDefaultHubAdminOptions(e->p->Rpc, &a.DefaultOptions);
7634 
7635 	Dialog(hWnd, D_SM_ADMIN_OPTION, SmHubAdminOptionDlg, &a);
7636 
7637 	FreeRpcAdminOption(&a.CurrentOptions);
7638 	FreeRpcAdminOption(&a.DefaultOptions);
7639 }
7640 
7641 // Initialize
SmConfigDlgInit(HWND hWnd,SM_CONFIG * c)7642 void SmConfigDlgInit(HWND hWnd, SM_CONFIG *c)
7643 {
7644 	wchar_t *tmp;
7645 	UINT tmp_size;
7646 	// Validate arguments
7647 	if (hWnd == NULL || c == NULL)
7648 	{
7649 		return;
7650 	}
7651 
7652 	Focus(hWnd, IDCANCEL);
7653 
7654 	SetIcon(hWnd, 0, ICO_MACHINE);
7655 
7656 	SetFont(hWnd, E_CONFIG, GetFont(_SS("DEFAULT_FONT_2"), 0, false, false,
7657 		false, false));
7658 
7659 	FormatText(hWnd, IDC_INFO, c->s->ServerName);
7660 
7661 	// Convert from UTF-8 to Unicode
7662 	tmp_size = CalcUtf8ToUni(c->Config.FileData, StrLen(c->Config.FileData)) + 1;
7663 	tmp = ZeroMalloc(tmp_size);
7664 	Utf8ToUni(tmp, tmp_size, c->Config.FileData, StrLen(c->Config.FileData));
7665 
7666 	SetText(hWnd, E_CONFIG, tmp);
7667 
7668 	Free(tmp);
7669 }
7670 
7671 // Config edit dialog
SmConfigDlg(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)7672 UINT SmConfigDlg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
7673 {
7674 	SM_CONFIG *c = (SM_CONFIG *)param;
7675 	char *filename;
7676 	wchar_t *filename_unicode;
7677 	wchar_t tmp[MAX_SIZE];
7678 	// Validate arguments
7679 	if (hWnd == NULL)
7680 	{
7681 		return 0;
7682 	}
7683 
7684 	switch (msg)
7685 	{
7686 	case WM_INITDIALOG:
7687 		SmConfigDlgInit(hWnd, c);
7688 		break;
7689 
7690 	case WM_COMMAND:
7691 		switch (wParam)
7692 		{
7693 		case B_EXPORT:
7694 			StrToUni(tmp, sizeof(tmp), c->Config.FileName);
7695 			filename_unicode = SaveDlg(hWnd, _UU("DLG_CONFIG_FILES"), _UU("DLG_SAVE_CONFIG"), tmp, L".config");
7696 			if (filename_unicode != NULL)
7697 			{
7698 				BUF *b = NewBuf();
7699 				filename = CopyUniToStr(filename_unicode);
7700 				WriteBuf(b, c->Config.FileData, StrLen(c->Config.FileData));
7701 				if (DumpBuf(b, filename))
7702 				{
7703 					MsgBox(hWnd, MB_ICONINFORMATION, _UU("SM_CONFIG_SAVED"));
7704 				}
7705 				else
7706 				{
7707 					MsgBox(hWnd, MB_ICONSTOP, _UU("SM_CONFIG_SAVE_FAILED"));
7708 				}
7709 				FreeBuf(b);
7710 				Free(filename);
7711 				Free(filename_unicode);
7712 			}
7713 			break;
7714 
7715 		case B_IMPORT:
7716 			filename_unicode = OpenDlg(hWnd, _UU("DLG_CONFIG_FILES"), _UU("DLG_OPEN_CONFIG"));
7717 			if (filename_unicode != NULL)
7718 			{
7719 				BUF *b;
7720 				filename = CopyUniToStr(filename_unicode);
7721 				b = ReadDump(filename);
7722 				if (b != NULL)
7723 				{
7724 					RPC_CONFIG t;
7725 
7726 					if (MsgBox(hWnd, MB_ICONQUESTION | MB_YESNO, _UU("SM_CONFIG_CONFIRM")) == IDYES)
7727 					{
7728 						Zero(&t, sizeof(t));
7729 						t.FileData = ZeroMalloc(b->Size + 1);
7730 						Copy(t.FileData, b->Buf, b->Size);
7731 
7732 						if (CALL(hWnd, ScSetConfig(c->s->Rpc, &t)))
7733 						{
7734 							// Success
7735 							MsgBox(hWnd, MB_ICONINFORMATION, _UU("SM_CONFIG_WRITE_OK"));
7736 							_exit(0);
7737 						}
7738 
7739 						FreeRpcConfig(&t);
7740 
7741 						FreeRpcConfig(&t);
7742 						FreeBuf(b);
7743 					}
7744 				}
7745 				else
7746 				{
7747 					MsgBox(hWnd, MB_ICONSTOP, _UU("SM_CONFIG_OPEN_FAILED"));
7748 				}
7749 				Free(filename);
7750 				Free(filename_unicode);
7751 			}
7752 			break;
7753 
7754 		case IDCANCEL:
7755 			Close(hWnd);
7756 			break;
7757 
7758 		case B_FACTORY:
7759 			if (MsgBox(hWnd, MB_ICONWARNING | MB_YESNO | MB_DEFBUTTON2, _UU("SM_FACTORY_DEFAULT_WARNING")) == IDYES)
7760 			{
7761 				RPC_TEST t;
7762 				UINT ret;
7763 
7764 				Zero(&t, sizeof(t));
7765 
7766 				t.IntValue = 1;
7767 				ret = ScRebootServer(c->s->Rpc, &t);
7768 
7769 				if (ret == ERR_DISCONNECTED || ret == ERR_NO_ERROR)
7770 				{
7771 					MsgBox(hWnd, MB_ICONINFORMATION, _UU("SM_FACTORY_DEFAULT_PERFORMED"));
7772 
7773 					exit(0);
7774 				}
7775 				else
7776 				{
7777 					CALL(hWnd, ret);
7778 				}
7779 			}
7780 			break;
7781 		}
7782 		break;
7783 
7784 	case WM_CLOSE:
7785 		EndDialog(hWnd, 0);
7786 		break;
7787 	}
7788 
7789 	return 0;
7790 }
7791 
7792 // Show the config edit dialog
SmConfig(HWND hWnd,SM_SERVER * s)7793 void SmConfig(HWND hWnd, SM_SERVER *s)
7794 {
7795 	SM_CONFIG c;
7796 	// Validate arguments
7797 	if (hWnd == NULL || s == NULL)
7798 	{
7799 		return;
7800 	}
7801 
7802 	Zero(&c, sizeof(c));
7803 
7804 	c.s = s;
7805 
7806 	// Get current config from the server
7807 	if (CALL(hWnd, ScGetConfig(s->Rpc, &c.Config)) == false)
7808 	{
7809 		return;
7810 	}
7811 
7812 	// Show the dialog
7813 	Dialog(hWnd, D_SM_CONFIG, SmConfigDlg, &c);
7814 
7815 	// Release
7816 	FreeRpcConfig(&c.Config);
7817 }
7818 
7819 // Bridge dialog initialization
SmBridgeDlgInit(HWND hWnd,SM_SERVER * s)7820 UINT SmBridgeDlgInit(HWND hWnd, SM_SERVER *s)
7821 {
7822 	UINT i;
7823 	RPC_ENUM_ETH t;
7824 	RPC_SERVER_INFO si;
7825 	UINT num = 0;
7826 	// Validate arguments
7827 	if (hWnd == NULL || s == NULL)
7828 	{
7829 		return 0;
7830 	}
7831 
7832 	LvInit(hWnd, L_LIST);
7833 	LvInsertColumn(hWnd, L_LIST, 0, _UU("SM_BRIDGE_COLUMN_1"), 50);
7834 	LvInsertColumn(hWnd, L_LIST, 1, _UU("SM_BRIDGE_COLUMN_2"), 145);
7835 	LvInsertColumn(hWnd, L_LIST, 2, _UU("SM_BRIDGE_COLUMN_3"), 300);
7836 	LvInsertColumn(hWnd, L_LIST, 3, _UU("SM_BRIDGE_COLUMN_4"), 100);
7837 
7838 	SmBridgeDlgRefresh(hWnd, s);
7839 
7840 	SetShow(hWnd, B_VLAN, GetCapsBool(s->CapsList, "b_support_eth_vlan"));
7841 
7842 	SetIcon(hWnd, 0, ICO_BRIDGE);
7843 
7844 	// Get the server information
7845 	Zero(&si, sizeof(si));
7846 	ScGetServerInfo(s->Rpc, &si);
7847 	if (GetCapsBool(s->CapsList, "b_tap_supported") == false)
7848 	{
7849 		// Tap does not supported
7850 		Hide(hWnd, R_TAP);
7851 		Hide(hWnd, S_TAP_1);
7852 		Hide(hWnd, E_TAPNAME);
7853 		Hide(hWnd, S_TAP_2);
7854 		Hide(hWnd, R_BRIDGE);
7855 		Hide(hWnd, S_STATIC5);
7856 	}
7857 	Check(hWnd, R_BRIDGE, true);
7858 	FreeRpcServerInfo(&si);
7859 
7860 	// Enumerate the Ethernet devices
7861 	Zero(&t, sizeof(t));
7862 	ScEnumEthernet(s->Rpc, &t);
7863 
7864 	CbReset(hWnd, E_NICNAME);
7865 	CbSetHeight(hWnd, E_NICNAME, 18);
7866 
7867 	num = t.NumItem;
7868 
7869 	for (i = 0;i < t.NumItem;i++)
7870 	{
7871 		RPC_ENUM_ETH_ITEM *e = &t.Items[i];
7872 		if (GetCapsBool(s->CapsList, "b_support_network_connection_name"))
7873 		{
7874 			wchar_t ncname[MAX_SIZE * 2];
7875 			UniFormat(ncname, sizeof(ncname), BRIDGE_NETWORK_CONNECTION_STR, e->NetworkConnectionName, e->DeviceName);
7876 			CbAddStr(hWnd, E_NICNAME, ncname, 0);
7877 		}
7878 		else
7879 		{
7880 			wchar_t *s = CopyStrToUni(e->DeviceName);
7881 			CbAddStr(hWnd, E_NICNAME, s, 0);
7882 			Free(s);
7883 		}
7884 	}
7885 
7886 	FreeRpcEnumEth(&t);
7887 
7888 	// Enumerate the Virtual HUBs
7889 	{
7890 		RPC_ENUM_HUB t;
7891 		Zero(&t, sizeof(t));
7892 
7893 		ScEnumHub(s->Rpc, &t);
7894 
7895 		CbReset(hWnd, E_HUBNAME);
7896 		CbSetHeight(hWnd, E_HUBNAME, 18);
7897 
7898 		for (i = 0;i < t.NumHub;i++)
7899 		{
7900 			RPC_ENUM_HUB_ITEM *e = &t.Hubs[i];
7901 			wchar_t *s = CopyStrToUni(e->HubName);
7902 
7903 			if (e->HubType != HUB_TYPE_FARM_DYNAMIC)
7904 			{
7905 				CbAddStr(hWnd, E_HUBNAME, s, 0);
7906 			}
7907 			Free(s);
7908 		}
7909 
7910 		SetText(hWnd, E_HUBNAME, L"");
7911 
7912 		FreeRpcEnumHub(&t);
7913 	}
7914 
7915 	if (s->Bridge)
7916 	{
7917 		SetTextA(hWnd, E_HUBNAME, "BRIDGE");
7918 	}
7919 
7920 	Focus(hWnd, E_HUBNAME);
7921 
7922 	SmBridgeDlgUpdate(hWnd, s);
7923 
7924 	SetTimer(hWnd, 1, 1000, NULL);
7925 
7926 	return num;
7927 }
7928 
7929 // Bridge dialog control update
SmBridgeDlgUpdate(HWND hWnd,SM_SERVER * s)7930 void SmBridgeDlgUpdate(HWND hWnd, SM_SERVER *s)
7931 {
7932 	bool ok = true;
7933 	// Validate arguments
7934 	if (hWnd == NULL || s == NULL)
7935 	{
7936 		return;
7937 	}
7938 
7939 	if (LvIsMasked(hWnd, L_LIST) && LvIsMultiMasked(hWnd, L_LIST) == false)
7940 	{
7941 		Enable(hWnd, B_DELETE);
7942 	}
7943 	else
7944 	{
7945 		Disable(hWnd, B_DELETE);
7946 	}
7947 
7948 	if (IsEmpty(hWnd, E_HUBNAME))
7949 	{
7950 		ok = false;
7951 	}
7952 
7953 	if (IsChecked(hWnd, R_TAP) == false)
7954 	{
7955 		// Bridge mode
7956 		Enable(hWnd, S_ETH_1);
7957 		Enable(hWnd, E_NICNAME);
7958 		Disable(hWnd, S_TAP_1);
7959 		Disable(hWnd, S_TAP_2);
7960 		Disable(hWnd, E_TAPNAME);
7961 		SetText(hWnd, S_INFO, _UU("SM_BRIDGE_INFO_1"));
7962 		SetIcon(hWnd, S_ICON, ICO_NIC_ONLINE);
7963 		if (IsEmpty(hWnd, E_NICNAME))
7964 		{
7965 			ok = false;
7966 		}
7967 	}
7968 	else
7969 	{
7970 		char tmp[MAX_SIZE];
7971 		// Tap mode
7972 		Disable(hWnd, S_ETH_1);
7973 		Disable(hWnd, E_NICNAME);
7974 		Enable(hWnd, S_TAP_1);
7975 		Enable(hWnd, S_TAP_2);
7976 		Enable(hWnd, E_TAPNAME);
7977 		SetText(hWnd, S_INFO, _UU("SM_BRIDGE_INFO_2"));
7978 		SetIcon(hWnd, S_ICON, ICO_PROTOCOL);
7979 		GetTxtA(hWnd, E_TAPNAME, tmp, sizeof(tmp));
7980 		if (IsEmptyStr(tmp))
7981 		{
7982 			ok = false;
7983 		}
7984 		else
7985 		{
7986 			if (IsSafeStr(tmp) == false)
7987 			{
7988 				ok = false;
7989 			}
7990 			if (StrLen(tmp) >= 12)
7991 			{
7992 				ok = false;
7993 			}
7994 		}
7995 	}
7996 
7997 	SetEnable(hWnd, IDOK, ok);
7998 }
7999 
8000 // Bridge dialog update
SmBridgeDlgRefresh(HWND hWnd,SM_SERVER * s)8001 void SmBridgeDlgRefresh(HWND hWnd, SM_SERVER *s)
8002 {
8003 	LVB *lvb;
8004 	RPC_ENUM_LOCALBRIDGE t;
8005 	UINT i;
8006 	// Validate arguments
8007 	if (hWnd == NULL || s == NULL)
8008 	{
8009 		return;
8010 	}
8011 
8012 	lvb = LvInsertStart();
8013 
8014 	Zero(&t, sizeof(t));
8015 
8016 	ScEnumLocalBridge(s->Rpc, &t);
8017 
8018 	for (i = 0;i < t.NumItem;i++)
8019 	{
8020 		RPC_LOCALBRIDGE *e = &t.Items[i];
8021 		wchar_t name[MAX_SIZE];
8022 		wchar_t nic[MAX_SIZE];
8023 		wchar_t hub[MAX_SIZE];
8024 		wchar_t *status = _UU("SM_BRIDGE_OFFLINE");
8025 
8026 		UniToStru(name, i + 1);
8027 		StrToUni(nic, sizeof(nic), e->DeviceName);
8028 		StrToUni(hub, sizeof(hub), e->HubName);
8029 
8030 		if (e->Online)
8031 		{
8032 			status = e->Active ? _UU("SM_BRIDGE_ONLINE") : _UU("SM_BRIDGE_ERROR");
8033 		}
8034 
8035 		LvInsertAdd(lvb, e->TapMode == false ? (e->Active ? ICO_NIC_ONLINE : ICO_NIC_OFFLINE) : ICO_PROTOCOL,
8036 			NULL, 4, name, hub, nic, status);
8037 	}
8038 
8039 	FreeRpcEnumLocalBridge(&t);
8040 
8041 	LvInsertEnd(lvb, hWnd, L_LIST);
8042 
8043 	SmBridgeDlgUpdate(hWnd, s);
8044 }
8045 
8046 // Add a Local Bridge
SmBridgeDlgOnOk(HWND hWnd,SM_SERVER * s)8047 void SmBridgeDlgOnOk(HWND hWnd, SM_SERVER *s)
8048 {
8049 	char nic[MAX_SIZE];
8050 	char hub[MAX_SIZE];
8051 	RPC_LOCALBRIDGE t;
8052 	bool tapmode = false;
8053 	// Validate arguments
8054 	if (hWnd == NULL || s == NULL)
8055 	{
8056 		return;
8057 	}
8058 
8059 	GetTxtA(hWnd, E_HUBNAME, hub, sizeof(hub));
8060 
8061 	Zero(nic, sizeof(nic));
8062 
8063 	if (IsChecked(hWnd, R_TAP) == false)
8064 	{
8065 		wchar_t nctmp[MAX_SIZE * 2];
8066 		if(GetCapsBool(s->CapsList, "b_support_network_connection_name") && GetTxt(hWnd, E_NICNAME, nctmp, sizeof(nctmp)))
8067 		{
8068 			RPC_ENUM_ETH et;
8069 			UINT i;
8070 			Zero(&et, sizeof(et));
8071 			ScEnumEthernet(s->Rpc, &et);
8072 			for(i = 0; i < et.NumItem; i++)
8073 			{
8074 				RPC_ENUM_ETH_ITEM *e = &et.Items[i];
8075 				if(UniIsEmptyStr(e->NetworkConnectionName) == false)
8076 				{
8077 					wchar_t ncname[MAX_SIZE * 2];
8078 					UniFormat(ncname, sizeof(ncname), BRIDGE_NETWORK_CONNECTION_STR, e->NetworkConnectionName, e->DeviceName);
8079 					if(UniStrCmp(ncname, nctmp) == 0)
8080 					{
8081 						StrCpy(nic, sizeof(nic), e->DeviceName);
8082 						break;
8083 					}
8084 				}
8085 			}
8086 			FreeRpcEnumEth(&et);
8087 
8088 			if (IsEmptyStr(nic))
8089 			{
8090 				GetTxtA(hWnd, E_NICNAME, nic, sizeof(nic));
8091 			}
8092 		}
8093 		else
8094 		{
8095 			GetTxtA(hWnd, E_NICNAME, nic, sizeof(nic));
8096 		}
8097 	}
8098 	else
8099 	{
8100 		tapmode = true;
8101 		GetTxtA(hWnd, E_TAPNAME, nic, sizeof(nic));
8102 	}
8103 
8104 	Trim(hub);
8105 	Trim(nic);
8106 
8107 	Zero(&t, sizeof(t));
8108 	StrCpy(t.DeviceName, sizeof(t.DeviceName), nic);
8109 	StrCpy(t.HubName, sizeof(t.HubName), hub);
8110 	t.TapMode = tapmode;
8111 
8112 	if (InStrEx(t.DeviceName, "vpn", false) || InStrEx(t.DeviceName, "tun", false)
8113 		|| InStrEx(t.DeviceName, "tap", false))
8114 	{
8115 		// Trying to make a local bridge to the VPN device
8116 		if (MsgBoxEx(hWnd, MB_ICONEXCLAMATION | MB_YESNO | MB_DEFBUTTON2,
8117 			_UU("SM_BRIDGE_VPN"),
8118 			t.DeviceName) == IDNO)
8119 		{
8120 			return;
8121 		}
8122 	}
8123 
8124 	// Show a warning message if the VPN Server is running in a VM
8125 	if (GetCapsBool(s->CapsList, "b_is_in_vm"))
8126 	{
8127 		Dialog(hWnd, D_SM_VMBRIDGE, SmVmBridgeDlg, NULL);
8128 	}
8129 
8130 	// Warning for such as Intel LAN cards
8131 	if (tapmode == false)
8132 	{
8133 		MsgBox(hWnd, MB_ICONINFORMATION, _UU("SM_BRIDGE_INTEL"));
8134 	}
8135 
8136 	if (CALL(hWnd, ScAddLocalBridge(s->Rpc, &t)) == false)
8137 	{
8138 		Focus(hWnd, E_HUBNAME);
8139 		return;
8140 	}
8141 
8142 	SetText(hWnd, E_HUBNAME, L"");
8143 	Focus(hWnd, E_HUBNAME);
8144 
8145 	if (tapmode)
8146 	{
8147 		SetTextA(hWnd, E_TAPNAME, "");
8148 	}
8149 
8150 	SmBridgeDlgRefresh(hWnd, s);
8151 
8152 	MsgBox(hWnd, MB_ICONINFORMATION, _UU("SM_BRIDGE_OK"));
8153 }
8154 
8155 // Bridge dialog procedure
SmBridgeDlgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)8156 UINT SmBridgeDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
8157 {
8158 	NMHDR *n;
8159 	SM_SERVER *s = (SM_SERVER *)param;
8160 	UINT i;
8161 	UINT num;
8162 	// Validate arguments
8163 	if (hWnd == NULL)
8164 	{
8165 		return 0;
8166 	}
8167 
8168 	switch (msg)
8169 	{
8170 	case WM_INITDIALOG:
8171 		num = SmBridgeDlgInit(hWnd, s);
8172 
8173 		if (num == 0)
8174 		{
8175 			SetTimer(hWnd, 2, 500, NULL);
8176 		}
8177 		break;
8178 
8179 	case WM_COMMAND:
8180 		switch (LOWORD(wParam))
8181 		{
8182 		case E_HUBNAME:
8183 		case E_NICNAME:
8184 		case R_BRIDGE:
8185 		case R_TAP:
8186 		case E_TAPNAME:
8187 			SmBridgeDlgUpdate(hWnd, s);
8188 			break;
8189 		}
8190 
8191 		switch (wParam)
8192 		{
8193 		case R_BRIDGE:
8194 			Focus(hWnd, E_NICNAME);
8195 			break;
8196 
8197 		case R_TAP:
8198 			FocusEx(hWnd, E_TAPNAME);
8199 			break;
8200 
8201 		case IDOK:
8202 			// Add
8203 			SmBridgeDlgOnOk(hWnd, s);
8204 			break;
8205 
8206 		case IDCANCEL:
8207 			// Close
8208 			Close(hWnd);
8209 			break;
8210 
8211 		case B_VLAN:
8212 			// VLAN utility
8213 			SmVLan(hWnd, s);
8214 			break;
8215 
8216 		case B_DELETE:
8217 			// Delete
8218 			i = LvGetSelected(hWnd, L_LIST);
8219 			if (i != INFINITE)
8220 			{
8221 				wchar_t *nic, *hub;
8222 				wchar_t tmp[MAX_SIZE];
8223 				RPC_LOCALBRIDGE t;
8224 
8225 				hub = LvGetStr(hWnd, L_LIST, i, 1);
8226 				nic = LvGetStr(hWnd, L_LIST, i, 2);
8227 
8228 				UniFormat(tmp, sizeof(tmp), _UU("SM_BRIDGE_DELETE"),
8229 					hub, nic);
8230 
8231 				if (MsgBox(hWnd, MB_ICONEXCLAMATION | MB_YESNO | MB_DEFBUTTON2, tmp) == IDYES)
8232 				{
8233 					Zero(&t, sizeof(t));
8234 					UniToStr(t.DeviceName, sizeof(t.DeviceName), nic);
8235 					UniToStr(t.HubName, sizeof(t.HubName), hub);
8236 
8237 					if (CALL(hWnd, ScDeleteLocalBridge(s->Rpc, &t)))
8238 					{
8239 						MsgBox(hWnd, MB_ICONINFORMATION, _UU("SM_BRIDGE_DELETE_OK"));
8240 						SmBridgeDlgRefresh(hWnd, s);
8241 					}
8242 				}
8243 
8244 				Free(hub);
8245 				Free(nic);
8246 			}
8247 			break;
8248 		}
8249 		break;
8250 
8251 	case WM_TIMER:
8252 		switch (wParam)
8253 		{
8254 		case 1:
8255 			if (IsEnable(hWnd, 0))
8256 			{
8257 				KillTimer(hWnd, 1);
8258 				SmBridgeDlgRefresh(hWnd, s);
8259 				SetTimer(hWnd, 1, 1000, NULL);
8260 			}
8261 			break;
8262 
8263 		case 2:
8264 			KillTimer(hWnd, 2);
8265 
8266 			MsgBox(hWnd, MB_ICONINFORMATION, _UU("SM_NO_BRIDGE_NICS"));
8267 			break;
8268 		}
8269 		break;
8270 
8271 	case WM_NOTIFY:
8272 		n = (NMHDR *)lParam;
8273 		switch (n->code)
8274 		{
8275 		case LVN_ITEMCHANGED:
8276 			switch (n->idFrom)
8277 			{
8278 			case L_LIST:
8279 				SmBridgeDlgUpdate(hWnd, s);
8280 				break;
8281 			}
8282 			break;
8283 		}
8284 		break;
8285 
8286 	case WM_CLOSE:
8287 		EndDialog(hWnd, false);
8288 		break;
8289 	}
8290 
8291 	return 0;
8292 }
8293 
8294 // Installation of WinPcap
SmInstallWinPcap(HWND hWnd,SM_SERVER * s)8295 void SmInstallWinPcap(HWND hWnd, SM_SERVER *s)
8296 {
8297 	wchar_t temp_name[MAX_SIZE];
8298 	IO *io;
8299 	BUF *buf;
8300 
8301 	// Ask whether the user want to start the installation
8302 	if (MsgBox(hWnd, MB_ICONQUESTION | MB_YESNO, _UU("SM_BRIDGE_WPCAP_INSTALL")) == IDNO)
8303 	{
8304 		return;
8305 	}
8306 
8307 	// Generate a temporary file name
8308 	UniFormat(temp_name, sizeof(temp_name), L"%s\\winpcap_installer.exe", MsGetTempDirW());
8309 
8310 	// Read from hamcore
8311 	buf = ReadDump(MsIsNt() ? "|winpcap_installer.exe" : "|winpcap_installer_win9x.exe");
8312 	if (buf == NULL)
8313 	{
8314 RES_ERROR:
8315 		MsgBox(hWnd, MB_ICONSTOP, _UU("SM_BRIDGE_RESOURCE"));
8316 		return;
8317 	}
8318 
8319 	// Write to a temporary file
8320 	io = FileCreateW(temp_name);
8321 	if (io == NULL)
8322 	{
8323 		FreeBuf(buf);
8324 		goto RES_ERROR;
8325 	}
8326 
8327 	FileWrite(io, buf->Buf, buf->Size);
8328 	FileClose(io);
8329 
8330 	FreeBuf(buf);
8331 
8332 	// Run
8333 	if (RunW(temp_name, NULL, false, true) == false)
8334 	{
8335 		// Failure
8336 		FileDeleteW(temp_name);
8337 		goto RES_ERROR;
8338 	}
8339 
8340 	FileDeleteW(temp_name);
8341 
8342 	if (s == NULL)
8343 	{
8344 		return;
8345 	}
8346 
8347 	// Message after completed
8348 	if (OS_IS_WINDOWS_NT(GetOsInfo()->OsType) == false)
8349 	{
8350 		// Need to restart the computer
8351 		MsgBox(hWnd, MB_ICONINFORMATION, _UU("SM_BRIDGE_WPCAP_REBOOT1"));
8352 	}
8353 	else
8354 	{
8355 		// Need to restart the service
8356 		if (MsgBox(hWnd, MB_ICONQUESTION | MB_YESNO, _UU("SM_BRIDGE_WPCAP_REBOOT2")) == IDNO)
8357 		{
8358 			// Not restart
8359 		}
8360 		else
8361 		{
8362 			// Restart
8363 			RPC_TEST t;
8364 			Zero(&t, sizeof(t));
8365 			ScRebootServer(s->Rpc, &t);
8366 
8367 			SleepThread(500);
8368 
8369 			Zero(&t, sizeof(t));
8370 			CALL(hWnd, ScTest(s->Rpc, &t));
8371 		}
8372 	}
8373 }
8374 
8375 // Bridge dialog
SmBridgeDlg(HWND hWnd,SM_SERVER * s)8376 void SmBridgeDlg(HWND hWnd, SM_SERVER *s)
8377 {
8378 	RPC_BRIDGE_SUPPORT t;
8379 	// Validate arguments
8380 	if (hWnd == NULL || s == NULL)
8381 	{
8382 		return;
8383 	}
8384 
8385 	// Examine the bridge support status of the server side first
8386 	Zero(&t, sizeof(t));
8387 	if (CALLEX(hWnd, ScGetBridgeSupport(s->Rpc, &t)) != ERR_NO_ERROR)
8388 	{
8389 		// Not supported because it is old version
8390 		MsgBox(hWnd, MB_ICONEXCLAMATION, _UU("SM_BRIDGE_TOO_OLD_VER"));
8391 		return;
8392 	}
8393 
8394 	if (t.IsBridgeSupportedOs == false)
8395 	{
8396 		// OS does not support the bridge
8397 		MsgBox(hWnd, MB_ICONEXCLAMATION, _UU("SM_BRIDGE_UNSUPPORTED"));
8398 		return;
8399 	}
8400 
8401 	if (t.IsWinPcapNeeded)
8402 	{
8403 		if (s->Rpc->Sock->RemoteIP.addr[0] != 127)
8404 		{
8405 			// WinPcap is required, but can not do anything because it is in remote control mode
8406 			MsgBox(hWnd, MB_ICONINFORMATION, _UU("SM_BRIDGE_WPCAP_REMOTE"));
8407 			return;
8408 		}
8409 		else
8410 		{
8411 			// WinPcap is required, and it's in local management mode
8412 			if (MsIsAdmin())
8413 			{
8414 				// The user is an Administrators
8415 				SmInstallWinPcap(hWnd, s);
8416 				return;
8417 			}
8418 			else
8419 			{
8420 				// The user is a non-Administrators
8421 				MsgBox(hWnd, MB_ICONINFORMATION, _UU("SM_BRIDGE_WPCAP_ROOT"));
8422 				return;
8423 			}
8424 		}
8425 	}
8426 
8427 	Dialog(hWnd, D_SM_BRIDGE, SmBridgeDlgProc, s);
8428 }
8429 
8430 // SecureNAT screen update
SmSNATDlgUpdate(HWND hWnd,SM_HUB * s)8431 void SmSNATDlgUpdate(HWND hWnd, SM_HUB *s)
8432 {
8433 	bool b;
8434 	RPC_HUB_STATUS t;
8435 	// Validate arguments
8436 	if (hWnd == NULL || s == NULL)
8437 	{
8438 		return;
8439 	}
8440 
8441 	Zero(&t, sizeof(t));
8442 	StrCpy(t.HubName, sizeof(t.HubName), s->HubName);
8443 	if (CALL(hWnd, ScGetHubStatus(s->Rpc, &t)) == false)
8444 	{
8445 		Close(hWnd);
8446 		return;
8447 	}
8448 
8449 	b = t.SecureNATEnabled;
8450 
8451 	if (b)
8452 	{
8453 		Disable(hWnd, B_ENABLE);
8454 		Enable(hWnd, B_DISABLE);
8455 		Enable(hWnd, B_NAT);
8456 		Enable(hWnd, B_DHCP);
8457 		Enable(hWnd, B_STATUS);
8458 	}
8459 	else
8460 	{
8461 		Enable(hWnd, B_ENABLE);
8462 		Disable(hWnd, B_DISABLE);
8463 		Disable(hWnd, B_NAT);
8464 		Disable(hWnd, B_DHCP);
8465 		Disable(hWnd, B_STATUS);
8466 	}
8467 }
8468 
8469 // SecureNAT configuration screen
SmSNATDlgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)8470 UINT SmSNATDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
8471 {
8472 	SM_HUB *s = (SM_HUB *)param;
8473 	RPC_HUB t;
8474 	// Validate arguments
8475 	if (hWnd == NULL)
8476 	{
8477 		return 0;
8478 	}
8479 
8480 	switch (msg)
8481 	{
8482 	case WM_INITDIALOG:
8483 		SetIcon(hWnd, 0, ICO_ROUTER);
8484 		DlgFont(hWnd, S_WARNING, (_GETLANG() == 0 || _GETLANG() == 2) ? 13 : 10, true);
8485 		FormatText(hWnd, S_TITLE, s->HubName);
8486 		SmSNATDlgUpdate(hWnd, s);
8487 
8488 		SetTimer(hWnd, 1, 1000, NULL);
8489 		break;
8490 
8491 	case WM_TIMER:
8492 		if (wParam == 1)
8493 		{
8494 			if (IsEnable(hWnd, 0))
8495 			{
8496 				KillTimer(hWnd, 1);
8497 
8498 				SmSNATDlgUpdate(hWnd, s);
8499 
8500 				SetTimer(hWnd, 1, 1000, NULL);
8501 			}
8502 		}
8503 		break;
8504 
8505 	case WM_COMMAND:
8506 		switch (wParam)
8507 		{
8508 		case IDCANCEL:
8509 			Close(hWnd);
8510 			break;
8511 
8512 		case B_ENABLE:
8513 			if (MsgBox(hWnd, MB_ICONEXCLAMATION | MB_OKCANCEL | MB_DEFBUTTON2,
8514 				_UU("SM_SECURE_NAT_MSG")) == IDOK)
8515 			{
8516 				Zero(&t, sizeof(t));
8517 				StrCpy(t.HubName, sizeof(t.HubName), s->HubName);
8518 				CALL(hWnd, ScEnableSecureNAT(s->Rpc, &t));
8519 				SmSNATDlgUpdate(hWnd, s);
8520 			}
8521 			break;
8522 
8523 		case B_DISABLE:
8524 			Zero(&t, sizeof(t));
8525 			StrCpy(t.HubName, sizeof(t.HubName), s->HubName);
8526 			CALL(hWnd, ScDisableSecureNAT(s->Rpc, &t));
8527 			SmSNATDlgUpdate(hWnd, s);
8528 			break;
8529 
8530 		case B_CONFIG:
8531 			NmEditVhOption(hWnd, s);
8532 			break;
8533 
8534 		case B_NAT:
8535 			NmNat(hWnd, s);
8536 			break;
8537 
8538 		case B_DHCP:
8539 			NmDhcp(hWnd, s);
8540 			break;
8541 
8542 		case B_STATUS:
8543 			SmStatusDlg(hWnd, s->p, s, false, true, _UU("SM_SNAT_STATUS"), ICO_ROUTER,
8544 				NULL, NmStatus);
8545 			break;
8546 		}
8547 		break;
8548 
8549 	case WM_CLOSE:
8550 		EndDialog(hWnd, 0);
8551 		break;
8552 	}
8553 
8554 	return 0;
8555 }
8556 
8557 // Initialize
SmCreateCertDlgInit(HWND hWnd,SM_CERT * s)8558 void SmCreateCertDlgInit(HWND hWnd, SM_CERT *s)
8559 {
8560 	UINT cert_sign;
8561 	UINT cert_days;
8562 	char *reg_o, *reg_ou, *reg_c, *reg_st, *reg_l;
8563 	UINT bits[] = {1024, 1536, 2048, 3072, 4096 };
8564 	UINT i;
8565 	UINT last_bit;
8566 	// Validate arguments
8567 	if (hWnd == NULL || s == NULL)
8568 	{
8569 		return;
8570 	}
8571 
8572 	SetTextA(hWnd, E_CN, s->default_cn);
8573 
8574 	last_bit = MsRegReadInt(REG_CURRENT_USER, SM_CERT_REG_KEY, "Bits");
8575 	if (last_bit == 0)
8576 	{
8577 		last_bit = 2048;
8578 	}
8579 
8580 	CbReset(hWnd, C_BITS);
8581 	for (i = 0;i < sizeof(bits) / sizeof(bits[0]);i++)
8582 	{
8583 		char tmp[MAX_PATH];
8584 		UINT index;
8585 
8586 		ToStr(tmp, bits[i]);
8587 
8588 		index = CbAddStrA(hWnd, C_BITS, tmp, bits[i]);
8589 	}
8590 
8591 	CbSelect(hWnd, C_BITS, 1024);
8592 	CbSelect(hWnd, C_BITS, last_bit);
8593 
8594 	reg_o = MsRegReadStr(REG_CURRENT_USER, SM_CERT_REG_KEY, "O");
8595 	reg_ou = MsRegReadStr(REG_CURRENT_USER, SM_CERT_REG_KEY, "OU");
8596 	reg_c = MsRegReadStr(REG_CURRENT_USER, SM_CERT_REG_KEY, "C");
8597 	reg_st = MsRegReadStr(REG_CURRENT_USER, SM_CERT_REG_KEY, "ST");
8598 	reg_l = MsRegReadStr(REG_CURRENT_USER, SM_CERT_REG_KEY, "L");
8599 	SetTextA(hWnd, E_O, reg_o);
8600 	SetTextA(hWnd, E_OU, reg_ou);
8601 	SetTextA(hWnd, E_C, reg_c);
8602 	SetTextA(hWnd, E_ST, reg_st);
8603 	SetTextA(hWnd, E_L, reg_l);
8604 	Free(reg_o);
8605 	Free(reg_ou);
8606 	Free(reg_c);
8607 	Free(reg_st);
8608 	Free(reg_l);
8609 
8610 	LimitText(hWnd, E_C, 2);
8611 
8612 	cert_sign = MsRegReadInt(REG_CURRENT_USER, SM_CERT_REG_KEY, "Sign");
8613 	cert_days = MsRegReadInt(REG_CURRENT_USER, SM_CERT_REG_KEY, "Days");
8614 
8615 	Check(hWnd, R_ROOT_CERT, cert_sign ? false : true);
8616 	Check(hWnd, R_SIGNED_CERT, cert_sign ? true : false);
8617 
8618 	if (cert_days == 0)
8619 	{
8620 		cert_days = 3650;
8621 	}
8622 
8623 	SetIntEx(hWnd, E_EXPIRE, cert_days);
8624 
8625 	SmCreateCertDlgUpdate(hWnd, s);
8626 
8627 	if (s->root_only)
8628 	{
8629 		Disable(hWnd, R_SIGNED_CERT);
8630 	}
8631 
8632 	// Font
8633 	SetFont(hWnd, E_CN, GetFont((MsIsWinXPOrGreater() ? "Verdana" : NULL), 0, false, false, false, false));
8634 	SetFont(hWnd, E_O, GetFont((MsIsWinXPOrGreater() ? "Verdana" : NULL), 0, false, false, false, false));
8635 	SetFont(hWnd, E_OU, GetFont((MsIsWinXPOrGreater() ? "Verdana" : NULL), 0, false, false, false, false));
8636 	SetFont(hWnd, E_C, GetFont((MsIsWinXPOrGreater() ? "Verdana" : NULL), 0, false, false, false, false));
8637 	SetFont(hWnd, E_ST, GetFont((MsIsWinXPOrGreater() ? "Verdana" : NULL), 0, false, false, false, false));
8638 	SetFont(hWnd, E_L, GetFont((MsIsWinXPOrGreater() ? "Verdana" : NULL), 0, false, false, false, false));
8639 	SetFont(hWnd, E_SERIAL, GetFont((MsIsWinXPOrGreater() ? "Verdana" : NULL), 0, false, false, false, false));
8640 	SetFont(hWnd, E_EXPIRE, GetFont((MsIsWinXPOrGreater() ? "Verdana" : NULL), 0, false, false, false, false));
8641 	SetFont(hWnd, C_BITS, GetFont("Verdana", 0, false, false, false, false));
8642 
8643 	FocusEx(hWnd, E_CN);
8644 }
8645 
8646 // Update
SmCreateCertDlgUpdate(HWND hWnd,SM_CERT * s)8647 void SmCreateCertDlgUpdate(HWND hWnd, SM_CERT *s)
8648 {
8649 	bool ok = true;
8650 	bool b;
8651 	UINT i;
8652 	// Validate arguments
8653 	if (hWnd == NULL || s == NULL)
8654 	{
8655 		return;
8656 	}
8657 
8658 	if (IsEmpty(hWnd, E_CN) && IsEmpty(hWnd, E_O) && IsEmpty(hWnd, E_OU) &&
8659 		IsEmpty(hWnd, E_ST) && IsEmpty(hWnd, E_L))
8660 	{
8661 		ok = false;
8662 	}
8663 
8664 	i = GetInt(hWnd, E_EXPIRE);
8665 	if (i == 0 || i >= (365 * 30))
8666 	{
8667 		ok = false;
8668 	}
8669 
8670 	b = IsChecked(hWnd, R_SIGNED_CERT);
8671 
8672 	SetEnable(hWnd, S_LOAD_1, b);
8673 	SetEnable(hWnd, B_LOAD, b);
8674 	SetEnable(hWnd, S_LOAD_2, b);
8675 
8676 	if (b && (s->root_k == NULL || s->root_x == NULL))
8677 	{
8678 		ok = false;
8679 	}
8680 
8681 	SetEnable(hWnd, IDOK, ok);
8682 }
8683 
8684 // [OK] button
SmCreateCertDlgOnOk(HWND hWnd,SM_CERT * s)8685 void SmCreateCertDlgOnOk(HWND hWnd, SM_CERT *s)
8686 {
8687 	wchar_t cn[MAX_SIZE], o[MAX_SIZE], ou[MAX_SIZE], c[MAX_SIZE], st[MAX_SIZE], l[MAX_SIZE];
8688 	char *reg_o, *reg_ou, *reg_c, *reg_st, *reg_l;
8689 	UINT days;
8690 	bool sign;
8691 	char serial[MAX_SIZE * 2];
8692 	X *x;
8693 	K *pub;
8694 	K *pri;
8695 	NAME *n;
8696 	X_SERIAL *x_serial;
8697 	BUF *buf;
8698 	UINT bits;
8699 	// Validate arguments
8700 	if (hWnd == NULL || s == NULL)
8701 	{
8702 		return;
8703 	}
8704 
8705 	GetTxt(hWnd, E_CN, cn, sizeof(cn));
8706 	GetTxt(hWnd, E_O, o, sizeof(o));
8707 	GetTxt(hWnd, E_OU, ou, sizeof(ou));
8708 	GetTxt(hWnd, E_C, c, sizeof(c));
8709 	GetTxt(hWnd, E_ST, st, sizeof(st));
8710 	GetTxt(hWnd, E_L, l, sizeof(l));
8711 	GetTxtA(hWnd, E_SERIAL, serial, sizeof(serial));
8712 
8713 	bits = CbGetSelect(hWnd, C_BITS);
8714 	if (bits == INFINITE)
8715 	{
8716 		bits = 1024;
8717 	}
8718 
8719 	buf = StrToBin(serial);
8720 	if (buf == NULL)
8721 	{
8722 		return;
8723 	}
8724 
8725 	if (buf->Size > 1)
8726 	{
8727 		x_serial = NewXSerial(buf->Buf, buf->Size);
8728 	}
8729 	else
8730 	{
8731 		x_serial = NULL;
8732 	}
8733 
8734 	FreeBuf(buf);
8735 
8736 	n = NewName(UniStrLen(cn) ? cn : NULL,
8737 		UniStrLen(o) ? o : NULL,
8738 		UniStrLen(ou) ? ou : NULL,
8739 		UniStrLen(c) ? c : NULL,
8740 		UniStrLen(st) ? st : NULL,
8741 		UniStrLen(l) ? l : NULL);
8742 
8743 	days = GetInt(hWnd, E_EXPIRE);
8744 
8745 	sign = IsChecked(hWnd, R_SIGNED_CERT);
8746 
8747 	MsRegWriteInt(REG_CURRENT_USER, SM_CERT_REG_KEY, "Sign", sign);
8748 	MsRegWriteInt(REG_CURRENT_USER, SM_CERT_REG_KEY, "Days", days);
8749 	MsRegWriteInt(REG_CURRENT_USER, SM_CERT_REG_KEY, "Bits", bits);
8750 
8751 	RsaGen(&pri, &pub, bits);
8752 
8753 	if (sign == false)
8754 	{
8755 		x = NewRootX(pub, pri, n, days, x_serial);
8756 	}
8757 	else
8758 	{
8759 		x = NewX(pub, s->root_k, s->root_x, n, days, x_serial);
8760 	}
8761 
8762 	FreeName(n);
8763 
8764 	FreeXSerial(x_serial);
8765 
8766 	if (x == NULL)
8767 	{
8768 		FreeX(x);
8769 		FreeK(pub);
8770 		FreeK(pri);
8771 		return;
8772 	}
8773 
8774 	if (s->do_not_save == false)
8775 	{
8776 		if (SmSaveKeyPairDlg(hWnd, x, pri) == false)
8777 		{
8778 			FreeX(x);
8779 			FreeK(pub);
8780 			FreeK(pri);
8781 			return;
8782 		}
8783 	}
8784 
8785 	s->x = x;
8786 	s->k = pri;
8787 	FreeK(pub);
8788 
8789 	reg_o = GetTextA(hWnd, E_O);
8790 	reg_ou = GetTextA(hWnd, E_OU);
8791 	reg_c = GetTextA(hWnd, E_C);
8792 	reg_st = GetTextA(hWnd, E_ST);
8793 	reg_l = GetTextA(hWnd, E_L);
8794 	MsRegWriteStr(REG_CURRENT_USER, SM_CERT_REG_KEY, "O", reg_o);
8795 	MsRegWriteStr(REG_CURRENT_USER, SM_CERT_REG_KEY, "OU", reg_ou);
8796 	MsRegWriteStr(REG_CURRENT_USER, SM_CERT_REG_KEY, "C", reg_c);
8797 	MsRegWriteStr(REG_CURRENT_USER, SM_CERT_REG_KEY, "ST", reg_st);
8798 	MsRegWriteStr(REG_CURRENT_USER, SM_CERT_REG_KEY, "L", reg_l);
8799 	Free(reg_o);
8800 	Free(reg_ou);
8801 	Free(reg_c);
8802 	Free(reg_st);
8803 	Free(reg_l);
8804 
8805 	EndDialog(hWnd, true);
8806 }
8807 
8808 // Certificate creation screen
SmCreateCertDlgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)8809 UINT SmCreateCertDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
8810 {
8811 	SM_CERT *s = (SM_CERT *)param;
8812 	// Validate arguments
8813 	if (hWnd == NULL)
8814 	{
8815 		return 0;
8816 	}
8817 
8818 	switch (msg)
8819 	{
8820 	case WM_INITDIALOG:
8821 		// Initialize
8822 		SmCreateCertDlgInit(hWnd, s);
8823 		break;
8824 
8825 	case WM_COMMAND:
8826 		switch (LOWORD(wParam))
8827 		{
8828 		case R_ROOT_CERT:
8829 		case R_SIGNED_CERT:
8830 		case B_LOAD:
8831 		case E_CN:
8832 		case E_O:
8833 		case E_OU:
8834 		case E_C:
8835 		case E_ST:
8836 		case E_L:
8837 		case E_EXPIRE:
8838 			SmCreateCertDlgUpdate(hWnd, s);
8839 			break;
8840 		}
8841 
8842 		switch (wParam)
8843 		{
8844 		case IDOK:
8845 			// [OK] button
8846 			SmCreateCertDlgOnOk(hWnd, s);
8847 			break;
8848 
8849 		case R_ROOT_CERT:
8850 			if (IsChecked(hWnd, R_ROOT_CERT))
8851 			{
8852 				FocusEx(hWnd, E_CN);
8853 			}
8854 			break;
8855 
8856 		case B_LOAD:
8857 			// Read a certificate
8858 			if (1)
8859 			{
8860 				X *x;
8861 				K *k;
8862 				if (CmLoadXAndK(hWnd, &x, &k))
8863 				{
8864 					wchar_t tmp[MAX_SIZE];
8865 					FreeX(s->root_x);
8866 					FreeK(s->root_k);
8867 					s->root_x = x;
8868 					s->root_k = k;
8869 
8870 					SmGetCertInfoStr(tmp, sizeof(tmp), x);
8871 					SetText(hWnd, S_LOAD_2, tmp);
8872 					SmCreateCertDlgUpdate(hWnd, s);
8873 				}
8874 			}
8875 			break;
8876 
8877 		case IDCANCEL:
8878 			// Cancel button
8879 			Close(hWnd);
8880 			break;
8881 		}
8882 		break;
8883 
8884 	case WM_CLOSE:
8885 		EndDialog(hWnd, false);
8886 		break;
8887 	}
8888 
8889 	return 0;
8890 }
8891 
8892 // Certificate tool
SmCreateCert(HWND hWnd,X ** x,K ** k,bool do_not_save,char * default_cn,bool root_only)8893 bool SmCreateCert(HWND hWnd, X **x, K **k, bool do_not_save, char *default_cn, bool root_only)
8894 {
8895 	bool ret;
8896 	SM_CERT s;
8897 	Zero(&s, sizeof(s));
8898 
8899 	if (default_cn == NULL)
8900 	{
8901 		default_cn = "";
8902 	}
8903 
8904 	s.default_cn = default_cn;
8905 
8906 	s.do_not_save = do_not_save;
8907 
8908 	s.root_only = root_only;
8909 
8910 	ret = Dialog(hWnd, D_SM_CREATE_CERT, SmCreateCertDlgProc, &s);
8911 
8912 	if (ret)
8913 	{
8914 		if (x != NULL)
8915 		{
8916 			*x = CloneX(s.x);
8917 		}
8918 
8919 		if (k != NULL)
8920 		{
8921 			*k = CloneK(s.k);
8922 		}
8923 	}
8924 
8925 	FreeX(s.x);
8926 	FreeK(s.k);
8927 	FreeX(s.root_x);
8928 	FreeK(s.root_k);
8929 
8930 	return ret;
8931 }
8932 
8933 // Initialize
SmIpTableDlgInit(HWND hWnd,SM_TABLE * s)8934 void SmIpTableDlgInit(HWND hWnd, SM_TABLE *s)
8935 {
8936 	// Validate arguments
8937 	if (hWnd == NULL || s == NULL)
8938 	{
8939 		return;
8940 	}
8941 
8942 	SetIcon(hWnd, 0, ICO_PROTOCOL);
8943 	FormatText(hWnd, S_TITLE, s->Hub->HubName);
8944 
8945 	if (s->SessionName != NULL)
8946 	{
8947 		wchar_t tmp[MAX_SIZE];
8948 		wchar_t tmp2[MAX_SIZE];
8949 		GetTxt(hWnd, S_TITLE, tmp, sizeof(tmp));
8950 		UniFormat(tmp2, sizeof(tmp2), _UU("SM_SESSION_FILTER"), s->SessionName);
8951 		UniStrCat(tmp, sizeof(tmp), tmp2);
8952 		SetText(hWnd, S_TITLE, tmp);
8953 	}
8954 
8955 	LvInit(hWnd, L_TABLE);
8956 	LvInsertColumn(hWnd, L_TABLE, 0, _UU("SM_IP_COLUMN_1"), 190);
8957 	LvInsertColumn(hWnd, L_TABLE, 1, _UU("SM_IP_COLUMN_2"), 140);
8958 	LvInsertColumn(hWnd, L_TABLE, 2, _UU("SM_IP_COLUMN_3"), 133);
8959 	LvInsertColumn(hWnd, L_TABLE, 3, _UU("SM_IP_COLUMN_4"), 133);
8960 	LvInsertColumn(hWnd, L_TABLE, 4, _UU("SM_IP_COLUMN_5"), 133);
8961 	LvSetStyle(hWnd, L_TABLE, LVS_EX_GRIDLINES);
8962 
8963 	SmIpTableDlgRefresh(hWnd, s);
8964 }
8965 
8966 // Update the control
SmIpTableDlgUpdate(HWND hWnd,SM_TABLE * s)8967 void SmIpTableDlgUpdate(HWND hWnd, SM_TABLE *s)
8968 {
8969 	bool ok = true;
8970 	// Validate arguments
8971 	if (hWnd == NULL || s == NULL)
8972 	{
8973 		return;
8974 	}
8975 
8976 	if (LvIsSelected(hWnd, L_TABLE) == false || LvIsMultiMasked(hWnd, L_TABLE))
8977 	{
8978 		ok = false;
8979 	}
8980 
8981 	SetEnable(hWnd, B_DELETE, ok);
8982 }
8983 
8984 // Content update
SmIpTableDlgRefresh(HWND hWnd,SM_TABLE * s)8985 void SmIpTableDlgRefresh(HWND hWnd, SM_TABLE *s)
8986 {
8987 	UINT i;
8988 	RPC_ENUM_IP_TABLE t;
8989 	UINT old_selected = 0;
8990 	// Validate arguments
8991 	if (hWnd == NULL || s == NULL)
8992 	{
8993 		return;
8994 	}
8995 
8996 	Zero(&t, sizeof(t));
8997 	StrCpy(t.HubName, sizeof(t.HubName), s->Hub->HubName);
8998 
8999 	if (CALL(hWnd, ScEnumIpTable(s->Rpc, &t)) == false)
9000 	{
9001 		EndDialog(hWnd, false);
9002 		return;
9003 	}
9004 
9005 	i = LvGetSelected(hWnd, L_TABLE);
9006 	if (i != INFINITE)
9007 	{
9008 		old_selected = (UINT)LvGetParam(hWnd, L_TABLE, i);
9009 	}
9010 
9011 	LvReset(hWnd, L_TABLE);
9012 
9013 	for (i = 0;i < t.NumIpTable;i++)
9014 	{
9015 		char str[MAX_SIZE];
9016 		wchar_t tmp1[MAX_SIZE];
9017 		wchar_t tmp2[MAX_SIZE];
9018 		wchar_t tmp3[MAX_SIZE];
9019 		wchar_t tmp4[MAX_SIZE];
9020 		wchar_t tmp5[MAX_SIZE];
9021 		RPC_ENUM_IP_TABLE_ITEM *e = &t.IpTables[i];
9022 
9023 		if (s->SessionName == NULL || StrCmpi(e->SessionName, s->SessionName) == 0)
9024 		{
9025 			StrToUni(tmp1, sizeof(tmp1), e->SessionName);
9026 
9027 			if (e->DhcpAllocated == false)
9028 			{
9029 				IPToStr(str, sizeof(str), &e->IpV6);
9030 				StrToUni(tmp2, sizeof(tmp2), str);
9031 			}
9032 			else
9033 			{
9034 				IPToStr(str, sizeof(str), &e->IpV6);
9035 				UniFormat(tmp2, sizeof(tmp2), _UU("SM_MAC_IP_DHCP"), str);
9036 			}
9037 
9038 			GetDateTimeStr64Uni(tmp3, sizeof(tmp3), SystemToLocal64(e->CreatedTime));
9039 
9040 			GetDateTimeStr64Uni(tmp4, sizeof(tmp4), SystemToLocal64(e->UpdatedTime));
9041 
9042 			if (StrLen(e->RemoteHostname) == 0)
9043 			{
9044 				UniStrCpy(tmp5, sizeof(tmp5), _UU("SM_MACIP_LOCAL"));
9045 			}
9046 			else
9047 			{
9048 				UniFormat(tmp5, sizeof(tmp5), _UU("SM_MACIP_SERVER"), e->RemoteHostname);
9049 			}
9050 
9051 			LvInsert(hWnd, L_TABLE, e->DhcpAllocated ? ICO_PROTOCOL_DHCP : ICO_PROTOCOL, (void *)e->Key, 5,
9052 				tmp1, tmp2, tmp3, tmp4, tmp5);
9053 		}
9054 	}
9055 
9056 	FreeRpcEnumIpTable(&t);
9057 
9058 	if (old_selected != 0)
9059 	{
9060 		LvSelect(hWnd, L_TABLE, LvSearchParam(hWnd, L_TABLE, (void *)old_selected));
9061 	}
9062 
9063 	SmIpTableDlgUpdate(hWnd, s);
9064 }
9065 
9066 // IP address table dialog procedure
SmIpTableDlgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)9067 UINT SmIpTableDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
9068 {
9069 	SM_TABLE *s = (SM_TABLE *)param;
9070 	NMHDR *n;
9071 	UINT i;
9072 	// Validate arguments
9073 	if (hWnd == NULL)
9074 	{
9075 		return 0;
9076 	}
9077 
9078 	switch (msg)
9079 	{
9080 	case WM_INITDIALOG:
9081 		// Initialize
9082 		SmIpTableDlgInit(hWnd, s);
9083 		break;
9084 
9085 	case WM_COMMAND:
9086 		switch (wParam)
9087 		{
9088 		case B_DELETE:
9089 			// Delete
9090 			i = LvGetSelected(hWnd, L_TABLE);
9091 			if (i != INFINITE)
9092 			{
9093 				RPC_DELETE_TABLE t;
9094 				UINT key = (UINT)LvGetParam(hWnd, L_TABLE, i);
9095 
9096 				Zero(&t, sizeof(t));
9097 				StrCpy(t.HubName, sizeof(t.HubName), s->Hub->HubName);
9098 				t.Key = key;
9099 				if (CALL(hWnd, ScDeleteIpTable(s->Rpc, &t)))
9100 				{
9101 					LvDeleteItem(hWnd, L_TABLE, i);
9102 				}
9103 			}
9104 			break;
9105 
9106 		case B_REFRESH:
9107 			// Update
9108 			SmIpTableDlgRefresh(hWnd, s);
9109 			break;
9110 
9111 		case IDCANCEL:
9112 			// Cancel button
9113 			Close(hWnd);
9114 			break;
9115 		}
9116 		break;
9117 
9118 	case WM_NOTIFY:
9119 		n = (NMHDR *)lParam;
9120 		switch (n->idFrom)
9121 		{
9122 		case L_TABLE:
9123 			switch (n->code)
9124 			{
9125 			case LVN_ITEMCHANGED:
9126 				SmIpTableDlgUpdate(hWnd, s);
9127 				break;
9128 			}
9129 			break;
9130 		}
9131 		break;
9132 
9133 	case WM_CLOSE:
9134 		EndDialog(hWnd, false);
9135 		break;
9136 	}
9137 
9138 	LvStandardHandler(hWnd, msg, wParam, lParam, L_TABLE);
9139 
9140 	return 0;
9141 }
9142 
9143 // IP address table dialog
SmIpTableDlg(HWND hWnd,SM_HUB * s,char * session_name)9144 void SmIpTableDlg(HWND hWnd, SM_HUB *s, char *session_name)
9145 {
9146 	SM_TABLE t;
9147 	// Validate arguments
9148 	if (hWnd == NULL || s == NULL)
9149 	{
9150 		return;
9151 	}
9152 
9153 	Zero(&t, sizeof(t));
9154 	t.Hub = s;
9155 	t.Rpc = s->Rpc;
9156 	t.SessionName = session_name;
9157 
9158 	Dialog(hWnd, D_SM_IP, SmIpTableDlgProc, &t);
9159 }
9160 
9161 
9162 // Initialize
SmMacTableDlgInit(HWND hWnd,SM_TABLE * s)9163 void SmMacTableDlgInit(HWND hWnd, SM_TABLE *s)
9164 {
9165 	UINT i = 0;
9166 	// Validate arguments
9167 	if (hWnd == NULL || s == NULL)
9168 	{
9169 		return;
9170 	}
9171 
9172 	SetIcon(hWnd, 0, ICO_NIC_ONLINE);
9173 	FormatText(hWnd, S_TITLE, s->Hub->HubName);
9174 
9175 	if (s->SessionName != NULL)
9176 	{
9177 		wchar_t tmp[MAX_SIZE];
9178 		wchar_t tmp2[MAX_SIZE];
9179 		GetTxt(hWnd, S_TITLE, tmp, sizeof(tmp));
9180 		UniFormat(tmp2, sizeof(tmp2), _UU("SM_SESSION_FILTER"), s->SessionName);
9181 		UniStrCat(tmp, sizeof(tmp), tmp2);
9182 		SetText(hWnd, S_TITLE, tmp);
9183 	}
9184 
9185 	LvInit(hWnd, L_TABLE);
9186 	LvInsertColumn(hWnd, L_TABLE, i++, _UU("SM_MAC_COLUMN_1"), 190);
9187 	if (GetCapsBool(s->Hub->p->CapsList, "b_support_vlan"))
9188 	{
9189 		LvInsertColumn(hWnd, L_TABLE, i++, _UU("SM_MAC_COLUMN_1A"), 65);
9190 	}
9191 	LvInsertColumn(hWnd, L_TABLE, i++, _UU("SM_MAC_COLUMN_2"), 140);
9192 	LvInsertColumn(hWnd, L_TABLE, i++, _UU("SM_MAC_COLUMN_3"), 133);
9193 	LvInsertColumn(hWnd, L_TABLE, i++, _UU("SM_MAC_COLUMN_4"), 133);
9194 	LvInsertColumn(hWnd, L_TABLE, i++, _UU("SM_MAC_COLUMN_5"), 133);
9195 	LvSetStyle(hWnd, L_TABLE, LVS_EX_GRIDLINES);
9196 
9197 	SmMacTableDlgRefresh(hWnd, s);
9198 }
9199 
9200 // Update the control
SmMacTableDlgUpdate(HWND hWnd,SM_TABLE * s)9201 void SmMacTableDlgUpdate(HWND hWnd, SM_TABLE *s)
9202 {
9203 	bool ok = true;
9204 	// Validate arguments
9205 	if (hWnd == NULL || s == NULL)
9206 	{
9207 		return;
9208 	}
9209 
9210 	if (LvIsSelected(hWnd, L_TABLE) == false || LvIsMultiMasked(hWnd, L_TABLE))
9211 	{
9212 		ok = false;
9213 	}
9214 
9215 	SetEnable(hWnd, B_DELETE, ok);
9216 }
9217 
9218 // Content update
SmMacTableDlgRefresh(HWND hWnd,SM_TABLE * s)9219 void SmMacTableDlgRefresh(HWND hWnd, SM_TABLE *s)
9220 {
9221 	UINT i;
9222 	RPC_ENUM_MAC_TABLE t;
9223 	UINT old_selected = 0;
9224 	// Validate arguments
9225 	if (hWnd == NULL || s == NULL)
9226 	{
9227 		return;
9228 	}
9229 
9230 	Zero(&t, sizeof(t));
9231 	StrCpy(t.HubName, sizeof(t.HubName), s->Hub->HubName);
9232 
9233 	if (CALL(hWnd, ScEnumMacTable(s->Rpc, &t)) == false)
9234 	{
9235 		EndDialog(hWnd, false);
9236 		return;
9237 	}
9238 
9239 	i = LvGetSelected(hWnd, L_TABLE);
9240 	if (i != INFINITE)
9241 	{
9242 		old_selected = (UINT)LvGetParam(hWnd, L_TABLE, i);
9243 	}
9244 
9245 	LvReset(hWnd, L_TABLE);
9246 
9247 	for (i = 0;i < t.NumMacTable;i++)
9248 	{
9249 		char str[MAX_SIZE];
9250 		wchar_t tmp1[MAX_SIZE];
9251 		wchar_t tmp2[MAX_SIZE];
9252 		wchar_t tmp3[MAX_SIZE];
9253 		wchar_t tmp4[MAX_SIZE];
9254 		wchar_t tmp5[MAX_SIZE];
9255 		wchar_t tmp6[MAX_SIZE];
9256 		RPC_ENUM_MAC_TABLE_ITEM *e = &t.MacTables[i];
9257 
9258 		if (s->SessionName == NULL || StrCmpi(e->SessionName, s->SessionName) == 0)
9259 		{
9260 			StrToUni(tmp1, sizeof(tmp1), e->SessionName);
9261 
9262 			MacToStr(str, sizeof(str), e->MacAddress);
9263 			StrToUni(tmp2, sizeof(tmp2), str);
9264 
9265 			GetDateTimeStr64Uni(tmp3, sizeof(tmp3), SystemToLocal64(e->CreatedTime));
9266 
9267 			GetDateTimeStr64Uni(tmp4, sizeof(tmp4), SystemToLocal64(e->UpdatedTime));
9268 
9269 			if (StrLen(e->RemoteHostname) == 0)
9270 			{
9271 				UniStrCpy(tmp5, sizeof(tmp5), _UU("SM_MACIP_LOCAL"));
9272 			}
9273 			else
9274 			{
9275 				UniFormat(tmp5, sizeof(tmp5), _UU("SM_MACIP_SERVER"), e->RemoteHostname);
9276 			}
9277 
9278 			UniToStru(tmp6, e->VlanId);
9279 			if (e->VlanId == 0)
9280 			{
9281 				UniStrCpy(tmp6, sizeof(tmp6), _UU("CM_ST_NONE"));
9282 			}
9283 
9284 			if (GetCapsBool(s->Hub->p->CapsList, "b_support_vlan"))
9285 			{
9286 				LvInsert(hWnd, L_TABLE, ICO_NIC_ONLINE, (void *)e->Key, 6,
9287 					tmp1, tmp6, tmp2, tmp3, tmp4, tmp5);
9288 			}
9289 			else
9290 			{
9291 				LvInsert(hWnd, L_TABLE, ICO_NIC_ONLINE, (void *)e->Key, 5,
9292 					tmp1, tmp2, tmp3, tmp4, tmp5);
9293 			}
9294 		}
9295 	}
9296 
9297 	FreeRpcEnumMacTable(&t);
9298 
9299 	if (old_selected != 0)
9300 	{
9301 		LvSelect(hWnd, L_TABLE, LvSearchParam(hWnd, L_TABLE, (void *)old_selected));
9302 	}
9303 
9304 	SmMacTableDlgUpdate(hWnd, s);
9305 }
9306 
9307 // MAC address table dialog procedure
SmMacTableDlgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)9308 UINT SmMacTableDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
9309 {
9310 	SM_TABLE *s = (SM_TABLE *)param;
9311 	NMHDR *n;
9312 	UINT i;
9313 	// Validate arguments
9314 	if (hWnd == NULL)
9315 	{
9316 		return 0;
9317 	}
9318 
9319 	switch (msg)
9320 	{
9321 	case WM_INITDIALOG:
9322 		// Initialize
9323 		SmMacTableDlgInit(hWnd, s);
9324 		break;
9325 
9326 	case WM_COMMAND:
9327 		switch (wParam)
9328 		{
9329 		case B_DELETE:
9330 			// Delete
9331 			i = LvGetSelected(hWnd, L_TABLE);
9332 			if (i != INFINITE)
9333 			{
9334 				RPC_DELETE_TABLE t;
9335 				UINT key = (UINT)LvGetParam(hWnd, L_TABLE, i);
9336 
9337 				Zero(&t, sizeof(t));
9338 				StrCpy(t.HubName, sizeof(t.HubName), s->Hub->HubName);
9339 				t.Key = key;
9340 				if (CALL(hWnd, ScDeleteMacTable(s->Rpc, &t)))
9341 				{
9342 					LvDeleteItem(hWnd, L_TABLE, i);
9343 				}
9344 			}
9345 			break;
9346 
9347 		case B_REFRESH:
9348 			// Update
9349 			SmMacTableDlgRefresh(hWnd, s);
9350 			break;
9351 
9352 		case IDCANCEL:
9353 			// Cancel button
9354 			Close(hWnd);
9355 			break;
9356 		}
9357 		break;
9358 
9359 	case WM_NOTIFY:
9360 		n = (NMHDR *)lParam;
9361 		switch (n->idFrom)
9362 		{
9363 		case L_TABLE:
9364 			switch (n->code)
9365 			{
9366 			case LVN_ITEMCHANGED:
9367 				SmMacTableDlgUpdate(hWnd, s);
9368 				break;
9369 			}
9370 			break;
9371 		}
9372 		break;
9373 
9374 	case WM_CLOSE:
9375 		EndDialog(hWnd, false);
9376 		break;
9377 	}
9378 
9379 	LvStandardHandler(hWnd, msg, wParam, lParam, L_TABLE);
9380 
9381 	return 0;
9382 }
9383 
9384 // MAC address table dialog
SmMacTableDlg(HWND hWnd,SM_HUB * s,char * session_name)9385 void SmMacTableDlg(HWND hWnd, SM_HUB *s, char *session_name)
9386 {
9387 	SM_TABLE t;
9388 	// Validate arguments
9389 	if (hWnd == NULL || s == NULL)
9390 	{
9391 		return;
9392 	}
9393 
9394 	Zero(&t, sizeof(t));
9395 	t.Hub = s;
9396 	t.Rpc = s->Rpc;
9397 	t.SessionName = session_name;
9398 
9399 	Dialog(hWnd, D_SM_MAC, SmMacTableDlgProc, &t);
9400 }
9401 
9402 // Initialize
SmSessionDlgInit(HWND hWnd,SM_HUB * s)9403 void SmSessionDlgInit(HWND hWnd, SM_HUB *s)
9404 {
9405 	// Validate arguments
9406 	if (hWnd == NULL || s == NULL)
9407 	{
9408 		return;
9409 	}
9410 
9411 	SetIcon(hWnd, 0, ICO_VPN);
9412 	FormatText(hWnd, 0, s->HubName);
9413 	FormatText(hWnd, S_TITLE, s->HubName);
9414 
9415 	LvInit(hWnd, L_LIST);
9416 	LvInsertColumn(hWnd, L_LIST, 0, _UU("SM_SESS_COLUMN_1"), 176);
9417 	LvInsertColumn(hWnd, L_LIST, 1, _UU("SM_SESS_COLUMN_8"), 58);
9418 	LvInsertColumn(hWnd, L_LIST, 2, _UU("SM_SESS_COLUMN_2"), 62);
9419 	LvInsertColumn(hWnd, L_LIST, 3, _UU("SM_SESS_COLUMN_3"), 78);
9420 	LvInsertColumn(hWnd, L_LIST, 4, _UU("SM_SESS_COLUMN_4"), 122);
9421 	LvInsertColumn(hWnd, L_LIST, 5, _UU("SM_SESS_COLUMN_5"), 68);
9422 	LvInsertColumn(hWnd, L_LIST, 6, _UU("SM_SESS_COLUMN_6"), 100);
9423 	LvInsertColumn(hWnd, L_LIST, 7, _UU("SM_SESS_COLUMN_7"), 100);
9424 	LvSetStyle(hWnd, L_LIST, LVS_EX_GRIDLINES);
9425 
9426 	if (s->p->ServerType == SERVER_TYPE_FARM_CONTROLLER && GetCapsBool(s->p->CapsList, "b_support_cluster_admin") == false)
9427 	{
9428 		Show(hWnd, S_FARM_INFO_1);
9429 		Show(hWnd, S_FARM_INFO_2);
9430 	}
9431 
9432 	SmSessionDlgRefresh(hWnd, s);
9433 }
9434 
9435 // Update the control
SmSessionDlgUpdate(HWND hWnd,SM_HUB * s)9436 void SmSessionDlgUpdate(HWND hWnd, SM_HUB *s)
9437 {
9438 	bool ok = true;
9439 	bool ok2 = true;
9440 	// Validate arguments
9441 	if (hWnd == NULL || s == NULL)
9442 	{
9443 		return;
9444 	}
9445 
9446 	if (LvIsSelected(hWnd, L_LIST) == false || LvIsMultiMasked(hWnd, L_LIST))
9447 	{
9448 		ok = false;
9449 		ok2 = false;
9450 	}
9451 	else
9452 	{
9453 		UINT i = LvGetSelected(hWnd, L_LIST);
9454 		if (i != INFINITE)
9455 		{
9456 			void *p = LvGetParam(hWnd, L_LIST, i);
9457 			if (((bool)p) != false)
9458 			{
9459 				if (GetCapsBool(s->p->CapsList, "b_support_cluster_admin") == false)
9460 				{
9461 					ok = false;
9462 				}
9463 			}
9464 		}
9465 	}
9466 
9467 	if (s->p->ServerInfo.ServerBuildInt < 2844)
9468 	{
9469 		// Old version doen't support for remote management of the sessions
9470 		ok2 = ok;
9471 	}
9472 
9473 	SetEnable(hWnd, IDOK, ok2);
9474 	SetEnable(hWnd, B_DISCONNECT, ok2);
9475 	SetEnable(hWnd, B_SESSION_IP_TABLE, ok);
9476 	SetEnable(hWnd, B_SESSION_MAC_TABLE, ok);
9477 }
9478 
9479 // Update the list
SmSessionDlgRefresh(HWND hWnd,SM_HUB * s)9480 void SmSessionDlgRefresh(HWND hWnd, SM_HUB *s)
9481 {
9482 	LVB *b;
9483 	UINT i;
9484 	wchar_t *old_select;
9485 	RPC_ENUM_SESSION t;
9486 	// Validate arguments
9487 	if (hWnd == NULL || s == NULL)
9488 	{
9489 		return;
9490 	}
9491 
9492 	Zero(&t, sizeof(t));
9493 	StrCpy(t.HubName, sizeof(t.HubName), s->HubName);
9494 
9495 	if (CALL(hWnd, ScEnumSession(s->Rpc, &t)) == false)
9496 	{
9497 		EndDialog(hWnd, false);
9498 		return;
9499 	}
9500 
9501 	old_select = LvGetSelectedStr(hWnd, L_LIST, 0);
9502 
9503 	LvReset(hWnd, L_LIST);
9504 
9505 	b = LvInsertStart();
9506 
9507 	for (i = 0;i < t.NumSession;i++)
9508 	{
9509 		RPC_ENUM_SESSION_ITEM *e = &t.Sessions[i];
9510 		wchar_t tmp1[MAX_SIZE];
9511 		wchar_t *tmp2;
9512 		wchar_t tmp3[MAX_SIZE];
9513 		wchar_t tmp4[MAX_SIZE];
9514 		wchar_t tmp5[MAX_SIZE];
9515 		wchar_t tmp6[MAX_SIZE];
9516 		wchar_t tmp7[MAX_SIZE];
9517 		wchar_t tmp8[MAX_SIZE];
9518 		bool free_tmp2 = false;
9519 		UINT icon;
9520 
9521 		StrToUni(tmp1, sizeof(tmp1), e->Name);
9522 
9523 		tmp2 = _UU("SM_SESS_NORMAL");
9524 		icon = ICO_VPN;
9525 		if (s->p->ServerType != SERVER_TYPE_STANDALONE)
9526 		{
9527 			if (e->RemoteSession)
9528 			{
9529 				tmp2 = ZeroMalloc(MAX_SIZE);
9530 				UniFormat(tmp2, MAX_SIZE, _UU("SM_SESS_REMOTE"), e->RemoteHostname);
9531 				icon = ICO_VPN;
9532 				free_tmp2 = true;
9533 			}
9534 			else
9535 			{
9536 				if (StrLen(e->RemoteHostname) == 0)
9537 				{
9538 					tmp2 = _UU("SM_SESS_LOCAL");
9539 				}
9540 				else
9541 				{
9542 					tmp2 = ZeroMalloc(MAX_SIZE);
9543 					UniFormat(tmp2, MAX_SIZE, _UU("SM_SESS_LOCAL_2"), e->RemoteHostname);
9544 					free_tmp2 = true;
9545 				}
9546 			}
9547 		}
9548 		if (e->LinkMode)
9549 		{
9550 			if (free_tmp2)
9551 			{
9552 				Free(tmp2);
9553 				free_tmp2 = false;
9554 			}
9555 			tmp2 = _UU("SM_SESS_LINK");
9556 			icon = ICO_CASCADE;
9557 		}
9558 		else if (e->SecureNATMode)
9559 		{
9560 			/*if (free_tmp2)
9561 			{
9562 				Free(tmp2);
9563 				free_tmp2 = false;
9564 			}
9565 			tmp2 = _UU("SM_SESS_SNAT");*/
9566 			icon = ICO_ROUTER;
9567 		}
9568 		else if (e->BridgeMode)
9569 		{
9570 			icon = ICO_BRIDGE;
9571 		}
9572 		else if (e->Layer3Mode)
9573 		{
9574 			icon = ICO_SWITCH;
9575 		}
9576 
9577 		StrToUni(tmp3, sizeof(tmp3), e->Username);
9578 
9579 		StrToUni(tmp4, sizeof(tmp4), e->Hostname);
9580 		if (e->LinkMode)
9581 		{
9582 			UniStrCpy(tmp4, sizeof(tmp4), _UU("SM_SESS_LINK_HOSTNAME"));
9583 		}
9584 		else if (e->SecureNATMode)
9585 		{
9586 			UniStrCpy(tmp4, sizeof(tmp4), _UU("SM_SESS_SNAT_HOSTNAME"));
9587 		}
9588 		else if (e->BridgeMode)
9589 		{
9590 			UniStrCpy(tmp4, sizeof(tmp4), _UU("SM_SESS_BRIDGE_HOSTNAME"));
9591 		}
9592 		else if (StartWith(e->Username, L3_USERNAME))
9593 		{
9594 			UniStrCpy(tmp4, sizeof(tmp4), _UU("SM_SESS_LAYER3_HOSTNAME"));
9595 		}
9596 
9597 		UniFormat(tmp5, sizeof(tmp5), L"%u / %u", e->CurrentNumTcp, e->MaxNumTcp);
9598 		if (e->LinkMode)
9599 		{
9600 			UniStrCpy(tmp5, sizeof(tmp5), _UU("SM_SESS_LINK_TCP"));
9601 		}
9602 		else if (e->SecureNATMode)
9603 		{
9604 			UniStrCpy(tmp5, sizeof(tmp5), _UU("SM_SESS_SNAT_TCP"));
9605 		}
9606 		else if (e->BridgeMode)
9607 		{
9608 			UniStrCpy(tmp5, sizeof(tmp5), _UU("SM_SESS_BRIDGE_TCP"));
9609 		}
9610 
9611 		if (e->VLanId == 0)
9612 		{
9613 			UniStrCpy(tmp8, sizeof(tmp8), _UU("CM_ST_NO_VLAN"));
9614 		}
9615 		else
9616 		{
9617 			UniToStru(tmp8, e->VLanId);
9618 		}
9619 
9620 		UniToStr3(tmp6, sizeof(tmp6), e->PacketSize);
9621 		UniToStr3(tmp7, sizeof(tmp7), e->PacketNum);
9622 
9623 		if (icon == ICO_VPN)
9624 		{
9625 			if (e->Client_BridgeMode)
9626 			{
9627 				icon = ICO_SESSION_BRIDGE;
9628 			}
9629 			else if (e->Client_MonitorMode)
9630 			{
9631 				icon = ICO_SESSION_MONITOR;
9632 			}
9633 		}
9634 
9635 		if (e->IsDormantEnabled && e->IsDormant)
9636 		{
9637 			icon = ICO_TRAY0;
9638 		}
9639 
9640 		LvInsertAdd(b, icon, (void *)(e->RemoteSession), 8, tmp1, tmp8, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7);
9641 
9642 		if (free_tmp2)
9643 		{
9644 			Free(tmp2);
9645 		}
9646 	}
9647 
9648 	LvInsertEnd(b, hWnd, L_LIST);
9649 
9650 	if (old_select != NULL && UniStrLen(old_select) != 0)
9651 	{
9652 		UINT i = LvSearchStr(hWnd, L_LIST, 0, old_select);
9653 		if (i != INFINITE)
9654 		{
9655 			LvSelect(hWnd, L_LIST, i);
9656 		}
9657 	}
9658 
9659 	Free(old_select);
9660 
9661 	FreeRpcEnumSession(&t);
9662 
9663 	SmSessionDlgUpdate(hWnd, s);
9664 }
9665 
9666 // Display the NODE_INFO
SmPrintNodeInfo(LVB * b,NODE_INFO * info)9667 void SmPrintNodeInfo(LVB *b, NODE_INFO *info)
9668 {
9669 	wchar_t tmp[MAX_SIZE];
9670 	char str[MAX_SIZE];
9671 	// Validate arguments
9672 	if (b == NULL || info == NULL)
9673 	{
9674 		return;
9675 	}
9676 
9677 	StrToUni(tmp, sizeof(tmp), info->ClientProductName);
9678 	LvInsertAdd(b, 0, NULL, 2, _UU("SM_NODE_CLIENT_NAME"), tmp);
9679 
9680 	UniFormat(tmp, sizeof(tmp), L"%u.%02u", Endian32(info->ClientProductVer) / 100, Endian32(info->ClientProductVer) % 100);
9681 	LvInsertAdd(b, 0, NULL, 2, _UU("SM_NODE_CLIENT_VER"), tmp);
9682 
9683 	UniFormat(tmp, sizeof(tmp), L"Build %u", Endian32(info->ClientProductBuild));
9684 	LvInsertAdd(b, 0, NULL, 2, _UU("SM_NODE_CLIENT_BUILD"), tmp);
9685 
9686 	StrToUni(tmp, sizeof(tmp), info->ClientOsName);
9687 	LvInsertAdd(b, 0, NULL, 2, _UU("SM_NODE_CLIENT_OS_NAME"), tmp);
9688 
9689 	StrToUni(tmp, sizeof(tmp), info->ClientOsVer);
9690 	LvInsertAdd(b, 0, NULL, 2, _UU("SM_NODE_CLIENT_OS_VER"), tmp);
9691 
9692 	StrToUni(tmp, sizeof(tmp), info->ClientOsProductId);
9693 	LvInsertAdd(b, 0, NULL, 2, _UU("SM_NODE_CLIENT_OS_PID"), tmp);
9694 
9695 	StrToUni(tmp, sizeof(tmp), info->ClientHostname);
9696 	LvInsertAdd(b, 0, NULL, 2, _UU("SM_NODE_CLIENT_HOST"), tmp);
9697 
9698 	IPToStr4or6(str, sizeof(str), info->ClientIpAddress, info->ClientIpAddress6);
9699 	StrToUni(tmp, sizeof(tmp), str);
9700 	LvInsertAdd(b, 0, NULL, 2, _UU("SM_NODE_CLIENT_IP"), tmp);
9701 
9702 	UniToStru(tmp, Endian32(info->ClientPort));
9703 	LvInsertAdd(b, 0, NULL, 2, _UU("SM_NODE_CLIENT_PORT"), tmp);
9704 
9705 	StrToUni(tmp, sizeof(tmp), info->ServerHostname);
9706 	LvInsertAdd(b, 0, NULL, 2, _UU("SM_NODE_SERVER_HOST"), tmp);
9707 
9708 	IPToStr4or6(str, sizeof(str), info->ServerIpAddress, info->ServerIpAddress6);
9709 	StrToUni(tmp, sizeof(tmp), str);
9710 	LvInsertAdd(b, 0, NULL, 2, _UU("SM_NODE_SERVER_IP"), tmp);
9711 
9712 	UniToStru(tmp, Endian32(info->ServerPort));
9713 	LvInsertAdd(b, 0, NULL, 2, _UU("SM_NODE_SERVER_PORT"), tmp);
9714 
9715 	if (StrLen(info->ProxyHostname) != 0)
9716 	{
9717 		StrToUni(tmp, sizeof(tmp), info->ProxyHostname);
9718 		LvInsertAdd(b, 0, NULL, 2, _UU("SM_NODE_PROXY_HOSTNAME"), tmp);
9719 
9720 		IPToStr4or6(str, sizeof(str), info->ProxyIpAddress, info->ProxyIpAddress6);
9721 		StrToUni(tmp, sizeof(tmp), str);
9722 		LvInsertAdd(b, 0, NULL, 2, _UU("SM_NODE_PROXY_IP"), tmp);
9723 
9724 		UniToStru(tmp, Endian32(info->ProxyPort));
9725 		LvInsertAdd(b, 0, NULL, 2, _UU("SM_NODE_PROXY_PORT"), tmp);
9726 	}
9727 }
9728 
9729 // Update the session status
SmRefreshSessionStatus(HWND hWnd,SM_SERVER * s,void * param)9730 bool SmRefreshSessionStatus(HWND hWnd, SM_SERVER *s, void *param)
9731 {
9732 	LVB *b;
9733 	SM_SESSION_STATUS *status = (SM_SESSION_STATUS *)param;
9734 	RPC_SESSION_STATUS t;
9735 	wchar_t tmp[MAX_SIZE];
9736 	char str[MAX_SIZE];
9737 	// Validate arguments
9738 	if (hWnd == NULL || s == NULL || param == NULL)
9739 	{
9740 		return false;
9741 	}
9742 
9743 	Zero(&t, sizeof(t));
9744 	StrCpy(t.HubName, sizeof(t.HubName), status->Hub->HubName);
9745 	StrCpy(t.Name, sizeof(t.Name), status->SessionName);
9746 
9747 	if (CALL(hWnd, ScGetSessionStatus(s->Rpc, &t)) == false)
9748 	{
9749 		return false;
9750 	}
9751 
9752 	b = LvInsertStart();
9753 
9754 	if (t.ClientIp != 0)
9755 	{
9756 		IPToStr4or6(str, sizeof(str), t.ClientIp, t.ClientIp6);
9757 		StrToUni(tmp, sizeof(tmp), str);
9758 		LvInsertAdd(b, 0, NULL, 2, _UU("SM_CLIENT_IP"), tmp);
9759 	}
9760 
9761 	if (StrLen(t.ClientHostName) != 0)
9762 	{
9763 		StrToUni(tmp, sizeof(tmp), t.ClientHostName);
9764 		LvInsertAdd(b, 0, NULL, 2, _UU("SM_CLIENT_HOSTNAME"), tmp);
9765 	}
9766 
9767 	StrToUni(tmp, sizeof(tmp), t.Username);
9768 	LvInsertAdd(b, 0, NULL, 2, _UU("SM_SESS_STATUS_USERNAME"), tmp);
9769 
9770 	if (StrCmpi(t.Username, LINK_USER_NAME_PRINT) != 0 && StrCmpi(t.Username, SNAT_USER_NAME_PRINT) != 0 && StrCmpi(t.Username, BRIDGE_USER_NAME_PRINT) != 0)
9771 	{
9772 		StrToUni(tmp, sizeof(tmp), t.RealUsername);
9773 		LvInsertAdd(b, 0, NULL, 2, _UU("SM_SESS_STATUS_REALUSER"), tmp);
9774 	}
9775 
9776 	if (IsEmptyStr(t.GroupName) == false)
9777 	{
9778 		StrToUni(tmp, sizeof(tmp), t.GroupName);
9779 		LvInsertAdd(b, 0, NULL, 2, _UU("SM_SESS_STATUS_GROUPNAME"), tmp);
9780 	}
9781 
9782 	CmPrintStatusToListViewEx(b, &t.Status, true);
9783 
9784 	if (StrCmpi(t.Username, LINK_USER_NAME_PRINT) != 0 && StrCmpi(t.Username, SNAT_USER_NAME_PRINT) != 0 && StrCmpi(t.Username, BRIDGE_USER_NAME_PRINT) != 0 &&
9785 		StartWith(t.Username, L3_USERNAME) == false)
9786 	{
9787 		SmPrintNodeInfo(b, &t.NodeInfo);
9788 	}
9789 
9790 	LvInsertEnd(b, hWnd, L_STATUS);
9791 
9792 	FreeRpcSessionStatus(&t);
9793 
9794 	return true;
9795 }
9796 
9797 // Session Management dialog procedure
SmSessionDlgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)9798 UINT SmSessionDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
9799 {
9800 	SM_HUB *s = (SM_HUB *)param;
9801 	wchar_t *tmp;
9802 	wchar_t tmp2[MAX_SIZE];
9803 	char name[MAX_SIZE];
9804 	NMHDR *n;
9805 	SM_SESSION_STATUS status;
9806 	// Validate arguments
9807 	if (hWnd == NULL)
9808 	{
9809 		return 0;
9810 	}
9811 
9812 	tmp = LvGetSelectedStr(hWnd, L_LIST, 0);
9813 	UniToStr(name, sizeof(name), tmp);
9814 
9815 	switch (msg)
9816 	{
9817 	case WM_INITDIALOG:
9818 		// Initialize
9819 		SmSessionDlgInit(hWnd, s);
9820 		break;
9821 
9822 	case WM_COMMAND:
9823 		switch (wParam)
9824 		{
9825 		case IDOK:
9826 			if (IsEnable(hWnd, IDOK))
9827 			{
9828 				// Session status display
9829 				UniFormat(tmp2, sizeof(tmp2), _UU("SM_SESS_STATUS_CAPTION"), name);
9830 				Zero(&status, sizeof(status));
9831 				status.Hub = s;
9832 				status.SessionName = name;
9833 				SmStatusDlg(hWnd, s->p, &status, true, true, tmp2, ICO_VPN,
9834 					NULL, SmRefreshSessionStatus);
9835 			}
9836 			break;
9837 
9838 		case B_DISCONNECT:
9839 			// Disconnect
9840 			if (MsgBoxEx(hWnd, MB_ICONQUESTION | MB_YESNO | MB_DEFBUTTON2,
9841 				_UU("SM_SESS_DISCONNECT_MSG"), name) == IDYES)
9842 			{
9843 				RPC_DELETE_SESSION t;
9844 				Zero(&t, sizeof(t));
9845 				StrCpy(t.HubName, sizeof(t.HubName), s->HubName);
9846 				StrCpy(t.Name, sizeof(t.Name), name);
9847 
9848 				if (CALL(hWnd, ScDeleteSession(s->Rpc, &t)))
9849 				{
9850 					SmSessionDlgRefresh(hWnd, s);
9851 				}
9852 			}
9853 			break;
9854 
9855 		case B_REFRESH:
9856 			// Update
9857 			SmSessionDlgRefresh(hWnd, s);
9858 			break;
9859 
9860 		case B_SESSION_IP_TABLE:
9861 			// IP table
9862 			SmIpTableDlg(hWnd, s, name);
9863 			break;
9864 
9865 		case B_SESSION_MAC_TABLE:
9866 			// MAC table
9867 			SmMacTableDlg(hWnd, s, name);
9868 			break;
9869 
9870 		case B_MAC_TABLE:
9871 			// MAC Table List
9872 			SmMacTableDlg(hWnd, s, NULL);
9873 			break;
9874 
9875 		case B_IP_TABLE:
9876 			// IP Table List
9877 			SmIpTableDlg(hWnd, s, NULL);
9878 			break;
9879 
9880 		case IDCANCEL:
9881 			// Cancel button
9882 			Close(hWnd);
9883 			break;
9884 		}
9885 		break;
9886 
9887 	case WM_NOTIFY:
9888 		n = (NMHDR *)lParam;
9889 		switch (n->code)
9890 		{
9891 		case LVN_ITEMCHANGED:
9892 			switch (n->idFrom)
9893 			{
9894 			case L_LIST:
9895 				SmSessionDlgUpdate(hWnd, s);
9896 				break;
9897 			}
9898 			break;
9899 		}
9900 		break;
9901 
9902 	case WM_CLOSE:
9903 		EndDialog(hWnd, false);
9904 		break;
9905 	}
9906 
9907 	Free(tmp);
9908 
9909 	LvStandardHandler(hWnd, msg, wParam, lParam, L_LIST);
9910 
9911 	return 0;
9912 }
9913 
9914 // Session Management dialog
SmSessionDlg(HWND hWnd,SM_HUB * s)9915 void SmSessionDlg(HWND hWnd, SM_HUB *s)
9916 {
9917 	// Validate arguments
9918 	if (hWnd == NULL || s == NULL)
9919 	{
9920 		return;
9921 	}
9922 
9923 	Dialog(hWnd, D_SM_SESSION, SmSessionDlgProc, s);
9924 }
9925 
9926 // Certificate List Update
SmCaDlgRefresh(HWND hWnd,SM_HUB * s)9927 void SmCaDlgRefresh(HWND hWnd, SM_HUB *s)
9928 {
9929 	LVB *b;
9930 	UINT i;
9931 	RPC_HUB_ENUM_CA t;
9932 	// Validate arguments
9933 	if (hWnd == NULL || s == NULL)
9934 	{
9935 		return;
9936 	}
9937 
9938 	Zero(&t, sizeof(t));
9939 	StrCpy(t.HubName, sizeof(t.HubName), s->HubName);
9940 	if (CALL(hWnd, ScEnumCa(s->Rpc, &t)) == false)
9941 	{
9942 		EndDialog(hWnd, false);
9943 		return;
9944 	}
9945 
9946 	b = LvInsertStart();
9947 
9948 	for (i = 0;i < t.NumCa;i++)
9949 	{
9950 		wchar_t tmp[MAX_SIZE];
9951 		RPC_HUB_ENUM_CA_ITEM *e = &t.Ca[i];
9952 
9953 		GetDateStrEx64(tmp, sizeof(tmp), SystemToLocal64(e->Expires), NULL);
9954 
9955 		LvInsertAdd(b, ICO_SERVER_CERT, (void *)e->Key, 3,
9956 			e->SubjectName, e->IssuerName, tmp);
9957 	}
9958 
9959 	LvInsertEnd(b, hWnd, L_CERT);
9960 
9961 	FreeRpcHubEnumCa(&t);
9962 
9963 	SmCaDlgUpdate(hWnd, s);
9964 }
9965 
9966 // Initialize
SmCaDlgInit(HWND hWnd,SM_HUB * s)9967 void SmCaDlgInit(HWND hWnd, SM_HUB *s)
9968 {
9969 	// Validate arguments
9970 	if (hWnd == NULL || s == NULL)
9971 	{
9972 		return;
9973 	}
9974 
9975 	SetIcon(hWnd, 0, ICO_SERVER_CERT);
9976 
9977 	LvInit(hWnd, L_CERT);
9978 	LvInsertColumn(hWnd, L_CERT, 0, _UU("CM_CERT_COLUMN_1"), 190);
9979 	LvInsertColumn(hWnd, L_CERT, 1, _UU("CM_CERT_COLUMN_2"), 190);
9980 	LvInsertColumn(hWnd, L_CERT, 2, _UU("CM_CERT_COLUMN_3"), 160);
9981 
9982 	SmCaDlgRefresh(hWnd, s);
9983 }
9984 
9985 // Update the control
SmCaDlgUpdate(HWND hWnd,SM_HUB * s)9986 void SmCaDlgUpdate(HWND hWnd, SM_HUB *s)
9987 {
9988 	// Validate arguments
9989 	if (hWnd == NULL || s == NULL)
9990 	{
9991 		return;
9992 	}
9993 
9994 	SetEnable(hWnd, B_DELETE, LvIsSelected(hWnd, L_CERT));
9995 	SetEnable(hWnd, IDOK, LvIsSelected(hWnd, L_CERT));
9996 }
9997 
9998 // OK
SmCaDlgOnOk(HWND hWnd,SM_HUB * s)9999 void SmCaDlgOnOk(HWND hWnd, SM_HUB *s)
10000 {
10001 	// Validate arguments
10002 	if (hWnd == NULL || s == NULL)
10003 	{
10004 		return;
10005 	}
10006 }
10007 
10008 // CA Adding dialog
SmCaDlgAdd(HWND hWnd,SM_HUB * s)10009 bool SmCaDlgAdd(HWND hWnd, SM_HUB *s)
10010 {
10011 	X *x;
10012 	RPC_HUB_ADD_CA t;
10013 	// Validate arguments
10014 	if (hWnd == NULL || s == NULL)
10015 	{
10016 		return false;
10017 	}
10018 
10019 	if (CmLoadXFromFileOrSecureCard(hWnd, &x) == false)
10020 	{
10021 		return false;
10022 	}
10023 
10024 	Zero(&t, sizeof(t));
10025 	StrCpy(t.HubName, sizeof(t.HubName), s->HubName);
10026 	t.Cert = x;
10027 
10028 	if (CALL(hWnd, ScAddCa(s->Rpc, &t)) == false)
10029 	{
10030 		return false;
10031 	}
10032 
10033 	FreeRpcHubAddCa(&t);
10034 
10035 	return true;
10036 }
10037 
10038 // CA List dialog procedure
SmCaDlgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)10039 UINT SmCaDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
10040 {
10041 	NMHDR *n;
10042 	SM_HUB *s = (SM_HUB *)param;
10043 	UINT i, key;
10044 	// Validate arguments
10045 	if (hWnd == NULL)
10046 	{
10047 		return 0;
10048 	}
10049 
10050 	switch (msg)
10051 	{
10052 	case WM_INITDIALOG:
10053 		// Initialize
10054 		SmCaDlgInit(hWnd, s);
10055 		break;
10056 
10057 	case WM_COMMAND:
10058 		switch (wParam)
10059 		{
10060 		case B_IMPORT:
10061 			// Add
10062 			if (SmCaDlgAdd(hWnd, s))
10063 			{
10064 				SmCaDlgRefresh(hWnd, s);
10065 			}
10066 			break;
10067 
10068 		case B_DELETE:
10069 			// Delete
10070 			i = LvGetSelected(hWnd, L_CERT);
10071 			if (i != INFINITE)
10072 			{
10073 				key = (UINT)LvGetParam(hWnd, L_CERT, i);
10074 				if (key != 0)
10075 				{
10076 					if (MsgBox(hWnd, MB_ICONQUESTION | MB_YESNO | MB_DEFBUTTON2,
10077 						_UU("CM_CERT_DELETE_MSG")) == IDYES)
10078 					{
10079 						RPC_HUB_DELETE_CA t;
10080 						Zero(&t, sizeof(t));
10081 						StrCpy(t.HubName, sizeof(t.HubName), s->HubName);
10082 						t.Key = key;
10083 
10084 						if (CALL(hWnd, ScDeleteCa(s->Rpc, &t)))
10085 						{
10086 							SmCaDlgRefresh(hWnd, s);
10087 						}
10088 					}
10089 				}
10090 			}
10091 			break;
10092 
10093 		case IDOK:
10094 			// Display
10095 			i = LvGetSelected(hWnd, L_CERT);
10096 			if (i != INFINITE)
10097 			{
10098 				key = (UINT)LvGetParam(hWnd, L_CERT, i);
10099 				if (key != 0)
10100 				{
10101 					RPC_HUB_GET_CA t;
10102 					Zero(&t, sizeof(t));
10103 					StrCpy(t.HubName, sizeof(t.HubName), s->HubName);
10104 					t.Key = key;
10105 
10106 					if (CALL(hWnd, ScGetCa(s->Rpc, &t)))
10107 					{
10108 						CertDlg(hWnd, t.Cert, NULL, true);
10109 						FreeRpcHubGetCa(&t);
10110 					}
10111 				}
10112 			}
10113 			break;
10114 
10115 		case IDCANCEL:
10116 			// Cancel button
10117 			Close(hWnd);
10118 			break;
10119 		}
10120 		break;
10121 
10122 	case WM_NOTIFY:
10123 		n = (NMHDR *)lParam;
10124 		switch (n->idFrom)
10125 		{
10126 		case L_CERT:
10127 			switch (n->code)
10128 			{
10129 			case LVN_ITEMCHANGED:
10130 				SmCaDlgUpdate(hWnd, s);
10131 				break;
10132 			}
10133 			break;
10134 		}
10135 		break;
10136 
10137 	case WM_CLOSE:
10138 		EndDialog(hWnd, false);
10139 		break;
10140 	}
10141 
10142 	LvStandardHandler(hWnd, msg, wParam, lParam, L_CERT);
10143 
10144 	return 0;
10145 }
10146 
10147 // CA List dialog box
SmCaDlg(HWND hWnd,SM_HUB * s)10148 void SmCaDlg(HWND hWnd, SM_HUB *s)
10149 {
10150 	// Validate arguments
10151 	if (hWnd == NULL || s == NULL)
10152 	{
10153 		return;
10154 	}
10155 
10156 	Dialog(hWnd, D_SM_CA, SmCaDlgProc, s);
10157 }
10158 
10159 // Initialize
SmLogDlgInit(HWND hWnd,SM_HUB * s)10160 void SmLogDlgInit(HWND hWnd, SM_HUB *s)
10161 {
10162 	RPC_HUB_LOG t;
10163 	// Validate arguments
10164 	if (hWnd == NULL || s == NULL)
10165 	{
10166 		return;
10167 	}
10168 
10169 	SetIcon(hWnd, 0, ICO_LOG2);
10170 
10171 	FormatText(hWnd, S_TITLE, s->HubName);
10172 
10173 	CbSetHeight(hWnd, C_SEC_SWITCH, 18);
10174 	CbSetHeight(hWnd, C_PACKET_SWITCH, 18);
10175 
10176 	// Initialize the control
10177 	CbAddStr(hWnd, C_SEC_SWITCH, _UU("SM_LOG_SWITCH_0"), 0);
10178 	CbAddStr(hWnd, C_SEC_SWITCH, _UU("SM_LOG_SWITCH_1"), 1);
10179 	CbAddStr(hWnd, C_SEC_SWITCH, _UU("SM_LOG_SWITCH_2"), 2);
10180 	CbAddStr(hWnd, C_SEC_SWITCH, _UU("SM_LOG_SWITCH_3"), 3);
10181 	CbAddStr(hWnd, C_SEC_SWITCH, _UU("SM_LOG_SWITCH_4"), 4);
10182 	CbAddStr(hWnd, C_SEC_SWITCH, _UU("SM_LOG_SWITCH_5"), 5);
10183 	CbAddStr(hWnd, C_PACKET_SWITCH, _UU("SM_LOG_SWITCH_0"), 0);
10184 	CbAddStr(hWnd, C_PACKET_SWITCH, _UU("SM_LOG_SWITCH_1"), 1);
10185 	CbAddStr(hWnd, C_PACKET_SWITCH, _UU("SM_LOG_SWITCH_2"), 2);
10186 	CbAddStr(hWnd, C_PACKET_SWITCH, _UU("SM_LOG_SWITCH_3"), 3);
10187 	CbAddStr(hWnd, C_PACKET_SWITCH, _UU("SM_LOG_SWITCH_4"), 4);
10188 	CbAddStr(hWnd, C_PACKET_SWITCH, _UU("SM_LOG_SWITCH_5"), 5);
10189 
10190 	// Get the log settings
10191 	Zero(&t, sizeof(t));
10192 	StrCpy(t.HubName, sizeof(t.HubName), s->HubName);
10193 	if (CALL(hWnd, ScGetHubLog(s->Rpc, &t)) == false)
10194 	{
10195 		EndDialog(hWnd, false);
10196 		return;
10197 	}
10198 
10199 	Check(hWnd, B_SEC, t.LogSetting.SaveSecurityLog);
10200 	CbSelect(hWnd, C_SEC_SWITCH, t.LogSetting.SecurityLogSwitchType);
10201 
10202 	Check(hWnd, B_PACKET, t.LogSetting.SavePacketLog);
10203 	CbSelect(hWnd, C_PACKET_SWITCH, t.LogSetting.PacketLogSwitchType);
10204 
10205 	Check(hWnd, B_PACKET_0_0, t.LogSetting.PacketLogConfig[0] == 0);
10206 	Check(hWnd, B_PACKET_0_1, t.LogSetting.PacketLogConfig[0] == 1);
10207 	Check(hWnd, B_PACKET_0_2, t.LogSetting.PacketLogConfig[0] == 2);
10208 
10209 	Check(hWnd, B_PACKET_1_0, t.LogSetting.PacketLogConfig[1] == 0);
10210 	Check(hWnd, B_PACKET_1_1, t.LogSetting.PacketLogConfig[1] == 1);
10211 	Check(hWnd, B_PACKET_1_2, t.LogSetting.PacketLogConfig[1] == 2);
10212 
10213 	Check(hWnd, B_PACKET_2_0, t.LogSetting.PacketLogConfig[2] == 0);
10214 	Check(hWnd, B_PACKET_2_1, t.LogSetting.PacketLogConfig[2] == 1);
10215 	Check(hWnd, B_PACKET_2_2, t.LogSetting.PacketLogConfig[2] == 2);
10216 
10217 	Check(hWnd, B_PACKET_3_0, t.LogSetting.PacketLogConfig[3] == 0);
10218 	Check(hWnd, B_PACKET_3_1, t.LogSetting.PacketLogConfig[3] == 1);
10219 	Check(hWnd, B_PACKET_3_2, t.LogSetting.PacketLogConfig[3] == 2);
10220 
10221 	Check(hWnd, B_PACKET_4_0, t.LogSetting.PacketLogConfig[4] == 0);
10222 	Check(hWnd, B_PACKET_4_1, t.LogSetting.PacketLogConfig[4] == 1);
10223 	Check(hWnd, B_PACKET_4_2, t.LogSetting.PacketLogConfig[4] == 2);
10224 
10225 	Check(hWnd, B_PACKET_5_0, t.LogSetting.PacketLogConfig[5] == 0);
10226 	Check(hWnd, B_PACKET_5_1, t.LogSetting.PacketLogConfig[5] == 1);
10227 	Check(hWnd, B_PACKET_5_2, t.LogSetting.PacketLogConfig[5] == 2);
10228 
10229 	Check(hWnd, B_PACKET_6_0, t.LogSetting.PacketLogConfig[6] == 0);
10230 	Check(hWnd, B_PACKET_6_1, t.LogSetting.PacketLogConfig[6] == 1);
10231 	Check(hWnd, B_PACKET_6_2, t.LogSetting.PacketLogConfig[6] == 2);
10232 
10233 	Check(hWnd, B_PACKET_7_0, t.LogSetting.PacketLogConfig[7] == 0);
10234 	Check(hWnd, B_PACKET_7_1, t.LogSetting.PacketLogConfig[7] == 1);
10235 	Check(hWnd, B_PACKET_7_2, t.LogSetting.PacketLogConfig[7] == 2);
10236 
10237 	SmLogDlgUpdate(hWnd, s);
10238 }
10239 
10240 // Update the control
SmLogDlgUpdate(HWND hWnd,SM_HUB * s)10241 void SmLogDlgUpdate(HWND hWnd, SM_HUB *s)
10242 {
10243 	bool b;
10244 	// Validate arguments
10245 	if (hWnd == NULL || s == NULL)
10246 	{
10247 		return;
10248 	}
10249 
10250 	b = IsChecked(hWnd, B_SEC);
10251 	SetEnable(hWnd, S_SEC, b);
10252 	SetEnable(hWnd, C_SEC_SWITCH, b);
10253 
10254 	b = IsChecked(hWnd, B_PACKET);
10255 	SetEnable(hWnd, S_PACKET, b);
10256 	SetEnable(hWnd, C_PACKET_SWITCH, b);
10257 	SetEnable(hWnd, S_PACKET_0, b);
10258 	SetEnable(hWnd, S_PACKET_1, b);
10259 	SetEnable(hWnd, S_PACKET_2, b);
10260 	SetEnable(hWnd, S_PACKET_3, b);
10261 	SetEnable(hWnd, S_PACKET_4, b);
10262 	SetEnable(hWnd, S_PACKET_5, b);
10263 	SetEnable(hWnd, S_PACKET_6, b);
10264 	SetEnable(hWnd, S_PACKET_7, b);
10265 	SetEnable(hWnd, B_PACKET_0_0, b); SetEnable(hWnd, B_PACKET_0_1, b); SetEnable(hWnd, B_PACKET_0_2, b);
10266 	SetEnable(hWnd, B_PACKET_1_0, b); SetEnable(hWnd, B_PACKET_1_1, b); SetEnable(hWnd, B_PACKET_1_2, b);
10267 	SetEnable(hWnd, B_PACKET_2_0, b); SetEnable(hWnd, B_PACKET_2_1, b); SetEnable(hWnd, B_PACKET_2_2, b);
10268 	SetEnable(hWnd, B_PACKET_3_0, b); SetEnable(hWnd, B_PACKET_3_1, b); SetEnable(hWnd, B_PACKET_3_2, b);
10269 	SetEnable(hWnd, B_PACKET_4_0, b); SetEnable(hWnd, B_PACKET_4_1, b); SetEnable(hWnd, B_PACKET_4_2, b);
10270 	SetEnable(hWnd, B_PACKET_5_0, b); SetEnable(hWnd, B_PACKET_5_1, b); SetEnable(hWnd, B_PACKET_5_2, b);
10271 	SetEnable(hWnd, B_PACKET_6_0, b); SetEnable(hWnd, B_PACKET_6_1, b); SetEnable(hWnd, B_PACKET_6_2, b);
10272 	SetEnable(hWnd, B_PACKET_7_0, b); SetEnable(hWnd, B_PACKET_7_1, b); SetEnable(hWnd, B_PACKET_7_2, b);
10273 }
10274 
10275 // OK
SmLogDlgOnOk(HWND hWnd,SM_HUB * s)10276 void SmLogDlgOnOk(HWND hWnd, SM_HUB *s)
10277 {
10278 	HUB_LOG g;
10279 	RPC_HUB_LOG t;
10280 	// Validate arguments
10281 	if (hWnd == NULL || s == NULL)
10282 	{
10283 		return;
10284 	}
10285 
10286 	Zero(&g, sizeof(g));
10287 	g.SaveSecurityLog = IsChecked(hWnd, B_SEC);
10288 	g.SavePacketLog = IsChecked(hWnd, B_PACKET);
10289 	g.SecurityLogSwitchType = CbGetSelect(hWnd, C_SEC_SWITCH);
10290 	g.PacketLogSwitchType = CbGetSelect(hWnd, C_PACKET_SWITCH);
10291 
10292 	g.PacketLogConfig[0] = IsChecked(hWnd, B_PACKET_0_0) ? 0 : IsChecked(hWnd, B_PACKET_0_1) ? 1 : 2;
10293 	g.PacketLogConfig[1] = IsChecked(hWnd, B_PACKET_1_0) ? 0 : IsChecked(hWnd, B_PACKET_1_1) ? 1 : 2;
10294 	g.PacketLogConfig[2] = IsChecked(hWnd, B_PACKET_2_0) ? 0 : IsChecked(hWnd, B_PACKET_2_1) ? 1 : 2;
10295 	g.PacketLogConfig[3] = IsChecked(hWnd, B_PACKET_3_0) ? 0 : IsChecked(hWnd, B_PACKET_3_1) ? 1 : 2;
10296 	g.PacketLogConfig[4] = IsChecked(hWnd, B_PACKET_4_0) ? 0 : IsChecked(hWnd, B_PACKET_4_1) ? 1 : 2;
10297 	g.PacketLogConfig[5] = IsChecked(hWnd, B_PACKET_5_0) ? 0 : IsChecked(hWnd, B_PACKET_5_1) ? 1 : 2;
10298 	g.PacketLogConfig[6] = IsChecked(hWnd, B_PACKET_6_0) ? 0 : IsChecked(hWnd, B_PACKET_6_1) ? 1 : 2;
10299 	g.PacketLogConfig[7] = IsChecked(hWnd, B_PACKET_7_0) ? 0 : IsChecked(hWnd, B_PACKET_7_1) ? 1 : 2;
10300 
10301 	Zero(&t, sizeof(t));
10302 	StrCpy(t.HubName, sizeof(t.HubName), s->HubName);
10303 	Copy(&t.LogSetting, &g, sizeof(HUB_LOG));
10304 
10305 	if (CALL(hWnd, ScSetHubLog(s->Rpc, &t)) == false)
10306 	{
10307 		return;
10308 	}
10309 
10310 	EndDialog(hWnd, true);
10311 }
10312 
10313 // Log storage settings dialog
SmLogDlg(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)10314 UINT SmLogDlg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
10315 {
10316 	SM_HUB *s = (SM_HUB *)param;
10317 	// Validate arguments
10318 	if (hWnd == NULL)
10319 	{
10320 		return 0;
10321 	}
10322 
10323 	switch (msg)
10324 	{
10325 	case WM_INITDIALOG:
10326 		// Initialize
10327 		SmLogDlgInit(hWnd, s);
10328 		break;
10329 
10330 	case WM_COMMAND:
10331 		switch (LOWORD(wParam))
10332 		{
10333 		case B_SEC:
10334 		case B_PACKET:
10335 			SmLogDlgUpdate(hWnd, s);
10336 			break;
10337 		}
10338 
10339 		switch (wParam)
10340 		{
10341 		case IDOK:
10342 			// [OK] button
10343 			SmLogDlgOnOk(hWnd, s);
10344 			break;
10345 
10346 		case IDCANCEL:
10347 			// Cancel button
10348 			Close(hWnd);
10349 			break;
10350 		}
10351 		break;
10352 
10353 	case WM_CLOSE:
10354 		EndDialog(hWnd, false);
10355 		break;
10356 	}
10357 
10358 	return 0;
10359 }
10360 
10361 // Show the status of the cascade connection
SmRefreshLinkStatus(HWND hWnd,SM_SERVER * s,void * param)10362 bool SmRefreshLinkStatus(HWND hWnd, SM_SERVER *s, void *param)
10363 {
10364 	SM_LINK *k = (SM_LINK *)param;
10365 	RPC_LINK_STATUS t;
10366 	LVB *b;
10367 	// Validate arguments
10368 	if (hWnd == NULL || s == NULL || param == NULL)
10369 	{
10370 		return false;
10371 	}
10372 
10373 	Zero(&t, sizeof(t));
10374 	StrCpy(t.HubName, sizeof(t.HubName), k->Hub->HubName);
10375 	UniStrCpy(t.AccountName, sizeof(t.AccountName), k->AccountName);
10376 
10377 	if (CALL(hWnd, ScGetLinkStatus(s->Rpc, &t)) == false)
10378 	{
10379 		return false;
10380 	}
10381 
10382 	b = LvInsertStart();
10383 
10384 	CmPrintStatusToListView(b, &t.Status);
10385 
10386 	LvInsertEnd(b, hWnd, L_STATUS);
10387 
10388 	FreeRpcLinkStatus(&t);
10389 
10390 	return true;
10391 }
10392 
10393 // Edit the link
SmLinkEdit(HWND hWnd,SM_HUB * s,wchar_t * name)10394 bool SmLinkEdit(HWND hWnd, SM_HUB *s, wchar_t *name)
10395 {
10396 	CM_ACCOUNT a;
10397 	RPC_CREATE_LINK t;
10398 	bool ret = false;
10399 	// Validate arguments
10400 	if (hWnd == NULL || s == NULL || name == NULL)
10401 	{
10402 		return false;
10403 	}
10404 
10405 	Zero(&a, sizeof(a));
10406 	Zero(&t, sizeof(t));
10407 
10408 	StrCpy(t.HubName, sizeof(t.HubName), s->HubName);
10409 	t.ClientOption = ZeroMalloc(sizeof(CLIENT_OPTION));
10410 	UniStrCpy(t.ClientOption->AccountName, sizeof(t.ClientOption->AccountName), name);
10411 
10412 	if (CALL(hWnd, ScGetLink(s->Rpc, &t)) == false)
10413 	{
10414 		return false;
10415 	}
10416 
10417 	a.Hub = s;
10418 	a.EditMode = true;
10419 	a.LinkMode = true;
10420 	a.ClientOption = ZeroMalloc(sizeof(CLIENT_OPTION));
10421 	a.OnlineFlag = t.Online;
10422 	Copy(a.ClientOption, t.ClientOption, sizeof(CLIENT_OPTION));
10423 	a.ClientAuth = CopyClientAuth(t.ClientAuth);
10424 	Copy(&a.Policy, &t.Policy, sizeof(POLICY));
10425 	a.CheckServerCert = t.CheckServerCert;
10426 	a.ServerCert = CloneX(t.ServerCert);
10427 	a.HideTrustCert = GetCapsBool(s->p->CapsList, "b_support_config_hub");
10428 	FreeRpcCreateLink(&t);
10429 
10430 	a.PolicyVer = s->p->PolicyVer;
10431 
10432 	if (GetCapsBool(s->p->CapsList, "b_support_cascade_client_cert") == false)
10433 	{
10434 		a.HideClientCertAuth = true;
10435 	}
10436 
10437 	a.HideSecureAuth = true;
10438 
10439 	ret = CmEditAccountDlg(hWnd, &a);
10440 
10441 	FreeX(a.ServerCert);
10442 	Free(a.ClientOption);
10443 	CiFreeClientAuth(a.ClientAuth);
10444 
10445 	return ret;
10446 }
10447 
10448 // Create a new link
SmLinkCreate(HWND hWnd,SM_HUB * s)10449 bool SmLinkCreate(HWND hWnd, SM_HUB *s)
10450 {
10451 	return SmLinkCreateEx(hWnd, s, false);
10452 }
SmLinkCreateEx(HWND hWnd,SM_HUB * s,bool connectNow)10453 bool SmLinkCreateEx(HWND hWnd, SM_HUB *s, bool connectNow)
10454 {
10455 	CM_ACCOUNT a;
10456 	bool ret = false;;
10457 	// Validate arguments
10458 	if (hWnd == NULL || s == NULL)
10459 	{
10460 		return false;
10461 	}
10462 
10463 	Zero(&a, sizeof(a));
10464 
10465 	a.Hub = s;
10466 	a.EditMode = false;
10467 	a.LinkMode = true;
10468 	a.ClientOption = ZeroMalloc(sizeof(CLIENT_OPTION));
10469 	a.OnlineFlag = false;
10470 	a.ClientAuth = ZeroMalloc(sizeof(CLIENT_AUTH));
10471 	a.ClientAuth->AuthType = CLIENT_AUTHTYPE_PASSWORD;
10472 	Copy(&a.Policy, GetDefaultPolicy(), sizeof(POLICY));
10473 	a.ClientOption->Port = 443;	// Default port number
10474 	a.ClientOption->NumRetry = INFINITE;
10475 	a.ClientOption->RetryInterval = 15;
10476 	a.ClientOption->MaxConnection = 8;
10477 	a.ClientOption->UseEncrypt = true;
10478 	a.ClientOption->HalfConnection = false;
10479 	a.ClientOption->AdditionalConnectionInterval = 1;
10480 	a.ClientOption->RequireBridgeRoutingMode = true;
10481 	a.Link_ConnectNow = connectNow;
10482 
10483 	a.PolicyVer = s->p->PolicyVer;
10484 
10485 	if (GetCapsBool(s->p->CapsList, "b_support_cascade_client_cert") == false)
10486 	{
10487 		a.HideClientCertAuth = true;
10488 	}
10489 
10490 	a.HideSecureAuth = true;
10491 
10492 	ret = CmEditAccountDlg(hWnd, &a);
10493 
10494 	FreeX(a.ServerCert);
10495 	Free(a.ClientOption);
10496 	CiFreeClientAuth(a.ClientAuth);
10497 
10498 	return ret;
10499 }
10500 
10501 // Initialize
SmLinkDlgInit(HWND hWnd,SM_HUB * s)10502 void SmLinkDlgInit(HWND hWnd, SM_HUB *s)
10503 {
10504 	// Validate arguments
10505 	if (hWnd == NULL || s == NULL)
10506 	{
10507 		return;
10508 	}
10509 
10510 	SetIcon(hWnd, 0, ICO_LINK);
10511 
10512 	FormatText(hWnd, 0, s->HubName);
10513 
10514 	LvInit(hWnd, L_LINK);
10515 
10516 	LvInsertColumn(hWnd, L_LINK, 0, _UU("SM_LINK_COLUMN_1"), 120);
10517 	LvInsertColumn(hWnd, L_LINK, 1, _UU("SM_LINK_COLUMN_2"), 150);
10518 	LvInsertColumn(hWnd, L_LINK, 2, _UU("SM_LINK_COLUMN_3"), 180);
10519 	LvInsertColumn(hWnd, L_LINK, 3, _UU("SM_LINK_COLUMN_4"), 130);
10520 	LvInsertColumn(hWnd, L_LINK, 4, _UU("SM_LINK_COLUMN_5"), 130);
10521 
10522 	LvSetStyle(hWnd, L_LINK, LVS_EX_GRIDLINES);
10523 
10524 	SmLinkDlgRefresh(hWnd, s);
10525 }
10526 
10527 // Update the controls
SmLinkDlgUpdate(HWND hWnd,SM_HUB * s)10528 void SmLinkDlgUpdate(HWND hWnd, SM_HUB *s)
10529 {
10530 	bool ok = true;
10531 	bool online = false;
10532 	// Validate arguments
10533 	if (hWnd == NULL || s == NULL)
10534 	{
10535 		return;
10536 	}
10537 
10538 	if (LvIsSelected(hWnd, L_LINK) == false || LvIsMultiMasked(hWnd, L_LINK))
10539 	{
10540 		ok = false;
10541 	}
10542 	else
10543 	{
10544 		online = (bool)LvGetParam(hWnd, L_LINK, LvGetSelected(hWnd, L_LINK));
10545 	}
10546 
10547 	SetEnable(hWnd, B_EDIT, ok);
10548 	SetEnable(hWnd, B_ONLINE, ok && (online == false));
10549 	SetEnable(hWnd, B_OFFLINE, ok && online);
10550 	SetEnable(hWnd, IDOK, ok && online);
10551 	SetEnable(hWnd, B_DELETE, ok);
10552 	SetEnable(hWnd, B_RENAME, ok);
10553 }
10554 
10555 // Content update
SmLinkDlgRefresh(HWND hWnd,SM_HUB * s)10556 void SmLinkDlgRefresh(HWND hWnd, SM_HUB *s)
10557 {
10558 	LVB *b;
10559 	RPC_ENUM_LINK t;
10560 	UINT i;
10561 	// Validate arguments
10562 	if (hWnd == NULL || s == NULL)
10563 	{
10564 		return;
10565 	}
10566 
10567 	Zero(&t, sizeof(t));
10568 	StrCpy(t.HubName, sizeof(t.HubName), s->HubName);
10569 	if (CALL(hWnd, ScEnumLink(s->Rpc, &t)) == false)
10570 	{
10571 		EndDialog(hWnd, false);
10572 		return;
10573 	}
10574 
10575 	b = LvInsertStart();
10576 
10577 	for (i = 0;i < t.NumLink;i++)
10578 	{
10579 		RPC_ENUM_LINK_ITEM *e = &t.Links[i];
10580 		wchar_t tmp1[MAX_SIZE];
10581 		wchar_t tmp2[MAX_SIZE];
10582 		wchar_t tmp3[MAX_SIZE];
10583 		wchar_t tmp4[MAX_SIZE];
10584 		UINT icon = ICO_CASCADE;
10585 
10586 		GetDateTimeStrEx64(tmp1, sizeof(tmp1), SystemToLocal64(e->ConnectedTime), NULL);
10587 		StrToUni(tmp2, sizeof(tmp2), e->Hostname);
10588 		StrToUni(tmp3, sizeof(tmp3), e->HubName);
10589 
10590 		if (e->Online == false)
10591 		{
10592 			UniStrCpy(tmp4, sizeof(tmp4), _UU("SM_LINK_STATUS_OFFLINE"));
10593 		}
10594 		else
10595 		{
10596 			if (e->Connected)
10597 			{
10598 				UniStrCpy(tmp4, sizeof(tmp4), _UU("SM_LINK_STATUS_ONLINE"));
10599 			}
10600 			else
10601 			{
10602 				if (e->LastError != 0)
10603 				{
10604 					UniFormat(tmp4, sizeof(tmp4), _UU("SM_LINK_STATUS_ERROR"), e->LastError, _E(e->LastError));
10605 				}
10606 				else
10607 				{
10608 					UniStrCpy(tmp4, sizeof(tmp4), _UU("SM_LINK_CONNECTING"));
10609 				}
10610 			}
10611 		}
10612 
10613 		if (e->Online == false)
10614 		{
10615 			icon = ICO_CASCADE_OFFLINE;
10616 		}
10617 		else
10618 		{
10619 			if (e->Connected == false && e->LastError != 0)
10620 			{
10621 				icon = ICO_CASCADE_ERROR;
10622 			}
10623 			else
10624 			{
10625 				icon = ICO_CASCADE;
10626 			}
10627 		}
10628 
10629 		LvInsertAdd(b,
10630 			icon, (void *)e->Online, 5,
10631 			e->AccountName, tmp4, tmp1, tmp2, tmp3);
10632 	}
10633 
10634 	LvInsertEnd(b, hWnd, L_LINK);
10635 
10636 	FreeRpcEnumLink(&t);
10637 
10638 	SmLinkDlgUpdate(hWnd, s);
10639 }
10640 
10641 
10642 // Link List dialog procedure
SmLinkDlgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)10643 UINT SmLinkDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
10644 {
10645 	SM_HUB *s = (SM_HUB *)param;
10646 	wchar_t *str;
10647 	NMHDR *n;
10648 	NMLVDISPINFOW *disp_info;
10649 	NMLVKEYDOWN *key;
10650 	// Validate arguments
10651 	if (hWnd == NULL)
10652 	{
10653 		return 0;
10654 	}
10655 
10656 	str = LvGetSelectedStr(hWnd, L_LINK, 0);
10657 
10658 	switch (msg)
10659 	{
10660 	case WM_INITDIALOG:
10661 		// Initialize
10662 		SmLinkDlgInit(hWnd, s);
10663 
10664 		if (link_create_now)
10665 		{
10666 			if (SmLinkCreateEx(hWnd, s, true))
10667 			{
10668 				SmLinkDlgRefresh(hWnd, s);
10669 			}
10670 		}
10671 
10672 		SetTimer(hWnd, 1, 1000, NULL);
10673 		break;
10674 
10675 	case WM_TIMER:
10676 		switch (wParam)
10677 		{
10678 		case 1:
10679 			if (IsEnable(hWnd, 0))
10680 			{
10681 				KillTimer(hWnd, 1);
10682 				SmLinkDlgRefresh(hWnd, s);
10683 				SetTimer(hWnd, 1, 1000, NULL);
10684 			}
10685 			break;
10686 		}
10687 		break;
10688 
10689 	case WM_COMMAND:
10690 		switch (wParam)
10691 		{
10692 		case B_CREATE:
10693 			// Create new
10694 			if (SmLinkCreate(hWnd, s))
10695 			{
10696 				SmLinkDlgRefresh(hWnd, s);
10697 			}
10698 			break;
10699 
10700 		case B_EDIT:
10701 			// Edit
10702 			if (str != NULL)
10703 			{
10704 				if (SmLinkEdit(hWnd, s, str))
10705 				{
10706 					SmLinkDlgRefresh(hWnd, s);
10707 				}
10708 			}
10709 			break;
10710 
10711 		case B_ONLINE:
10712 			// Online
10713 			if (str != NULL)
10714 			{
10715 				RPC_LINK t;
10716 				Zero(&t, sizeof(t));
10717 				UniStrCpy(t.AccountName, sizeof(t.AccountName), str);
10718 				StrCpy(t.HubName, sizeof(t.HubName), s->HubName);
10719 
10720 				if (CALL(hWnd, ScSetLinkOnline(s->Rpc, &t)))
10721 				{
10722 					SmLinkDlgRefresh(hWnd, s);
10723 				}
10724 			}
10725 			break;
10726 
10727 		case B_OFFLINE:
10728 			// Offline
10729 			if (str != NULL)
10730 			{
10731 				RPC_LINK t;
10732 				Zero(&t, sizeof(t));
10733 				UniStrCpy(t.AccountName, sizeof(t.AccountName), str);
10734 				StrCpy(t.HubName, sizeof(t.HubName), s->HubName);
10735 
10736 				if (MsgBoxEx(hWnd, MB_ICONEXCLAMATION | MB_YESNO | MB_DEFBUTTON2,
10737 					_UU("SM_LINK_OFFLINE_MSG"), t.AccountName) == IDYES)
10738 				{
10739 					if (CALL(hWnd, ScSetLinkOffline(s->Rpc, &t)))
10740 					{
10741 						SmLinkDlgRefresh(hWnd, s);
10742 					}
10743 				}
10744 			}
10745 			break;
10746 
10747 		case IDOK:
10748 			// Status
10749 			if (str != NULL)
10750 			{
10751 				wchar_t tmp[MAX_SIZE];
10752 				SM_LINK t;
10753 				Zero(&t, sizeof(t));
10754 				t.Hub = s;
10755 				t.AccountName = str;
10756 				UniFormat(tmp, sizeof(tmp), _UU("SM_LINK_STATUS_CAPTION"), str);
10757 				SmStatusDlg(hWnd, s->p, &t, true, true, tmp,
10758 					ICO_CASCADE, NULL, SmRefreshLinkStatus);
10759 			}
10760 			break;
10761 
10762 		case B_DELETE:
10763 			// Delete
10764 			if (str != NULL)
10765 			{
10766 				RPC_LINK t;
10767 				Zero(&t, sizeof(t));
10768 				UniStrCpy(t.AccountName, sizeof(t.AccountName), str);
10769 				StrCpy(t.HubName, sizeof(t.HubName), s->HubName);
10770 
10771 				if (MsgBoxEx(hWnd, MB_ICONEXCLAMATION | MB_YESNO | MB_DEFBUTTON2,
10772 					_UU("SM_LINK_DELETE_MSG"), t.AccountName) == IDYES)
10773 				{
10774 					if (CALL(hWnd, ScDeleteLink(s->Rpc, &t)))
10775 					{
10776 						SmLinkDlgRefresh(hWnd, s);
10777 					}
10778 				}
10779 			}
10780 			break;
10781 
10782 		case B_REFRESH:
10783 			// Update
10784 			SmLinkDlgRefresh(hWnd, s);
10785 			break;
10786 
10787 		case IDCANCEL:
10788 			// Cancel button
10789 			Close(hWnd);
10790 			break;
10791 
10792 		case B_RENAME:
10793 			// Change the name
10794 			Focus(hWnd, L_LINK);
10795 			LvRename(hWnd, L_LINK, LvGetSelected(hWnd, L_LINK));
10796 			break;
10797 		}
10798 		break;
10799 
10800 	case WM_NOTIFY:
10801 		n = (NMHDR *)lParam;
10802 		switch (n->idFrom)
10803 		{
10804 		case L_LINK:
10805 			switch (n->code)
10806 			{
10807 			case LVN_ITEMCHANGED:
10808 				// Change the selection state
10809 				SmLinkDlgUpdate(hWnd, s);
10810 				break;
10811 
10812 			case LVN_ENDLABELEDITW:
10813 				// Change the name
10814 				disp_info = (NMLVDISPINFOW *)n;
10815 				if (disp_info->item.pszText != NULL)
10816 				{
10817 					wchar_t *new_name = disp_info->item.pszText;
10818 					wchar_t *old_name = LvGetStr(hWnd, L_LINK, disp_info->item.iItem, 0);
10819 
10820 					if (old_name != NULL)
10821 					{
10822 						if (UniStrCmp(new_name, old_name) != 0 && UniIsEmptyStr(new_name) == false)
10823 						{
10824 							RPC_RENAME_LINK t;
10825 							Zero(&t, sizeof(t));
10826 							StrCpy(t.HubName, sizeof(t.HubName), s->HubName);
10827 							UniStrCpy(t.OldAccountName, sizeof(t.OldAccountName), old_name);
10828 							UniStrCpy(t.NewAccountName, sizeof(t.NewAccountName), new_name);
10829 							if (CALL(hWnd, ScRenameLink(s->Rpc, &t)))
10830 							{
10831 								SmLinkDlgRefresh(hWnd, s);
10832 							}
10833 						}
10834 
10835 						Free(old_name);
10836 					}
10837 				}
10838 				break;
10839 
10840 			case LVN_KEYDOWN:
10841 				// Keypress
10842 				key = (NMLVKEYDOWN *)n;
10843 				if (key != NULL)
10844 				{
10845 					bool ctrl, alt;
10846 					UINT code = key->wVKey;
10847 					ctrl = (GetKeyState(VK_CONTROL) & 0x8000) == 0 ? false : true;
10848 					alt = (GetKeyState(VK_MENU) & 0x8000) == 0 ? false : true;
10849 
10850 					if (code == VK_F2)
10851 					{
10852 						Command(hWnd, B_RENAME);
10853 					}
10854 				}
10855 				break;
10856 			}
10857 			break;
10858 		}
10859 		break;
10860 
10861 	case WM_CLOSE:
10862 		EndDialog(hWnd, false);
10863 		break;
10864 	}
10865 
10866 	Free(str);
10867 
10868 	LvStandardHandler(hWnd, msg, wParam, lParam, L_LINK);
10869 
10870 	return 0;
10871 }
10872 
10873 // Link List dialog
SmLinkDlg(HWND hWnd,SM_HUB * s)10874 void SmLinkDlg(HWND hWnd, SM_HUB *s)
10875 {
10876 	SmLinkDlgEx(hWnd, s, false);
10877 }
SmLinkDlgEx(HWND hWnd,SM_HUB * s,bool createNow)10878 void SmLinkDlgEx(HWND hWnd, SM_HUB *s, bool createNow)
10879 {
10880 	// Validate arguments
10881 	if (hWnd == NULL || s == NULL)
10882 	{
10883 		return;
10884 	}
10885 
10886 	link_create_now = createNow;
10887 
10888 	Dialog(hWnd, D_SM_LINK, SmLinkDlgProc, s);
10889 }
10890 
10891 // Initialize
SmRadiusDlgInit(HWND hWnd,SM_HUB * s)10892 void SmRadiusDlgInit(HWND hWnd, SM_HUB *s)
10893 {
10894 	RPC_RADIUS t;
10895 	// Validate arguments
10896 	if (hWnd == NULL || s == NULL)
10897 	{
10898 		return;
10899 	}
10900 
10901 	SetIcon(hWnd, 0, ICO_TOWER);
10902 
10903 	FormatText(hWnd, S_TITLE, s->HubName);
10904 	FormatText(hWnd, S_RADIUS_7, RADIUS_RETRY_INTERVAL, RADIUS_RETRY_TIMEOUT);
10905 
10906 	Zero(&t, sizeof(t));
10907 	StrCpy(t.HubName, sizeof(t.HubName), s->HubName);
10908 
10909 	if (CALL(hWnd, ScGetHubRadius(s->Rpc, &t)) == false)
10910 	{
10911 		EndDialog(hWnd, false);
10912 		return;
10913 	}
10914 
10915 	Check(hWnd, R_USE_RADIUS, StrLen(t.RadiusServerName) != 0);
10916 
10917 	if (StrLen(t.RadiusServerName) != 0)
10918 	{
10919 		SetTextA(hWnd, E_HOSTNAME, t.RadiusServerName);
10920 		SetIntEx(hWnd, E_PORT, t.RadiusPort);
10921 		SetTextA(hWnd, E_SECRET1, t.RadiusSecret);
10922 		SetTextA(hWnd, E_SECRET2, t.RadiusSecret);
10923 		SetIntEx(hWnd, E_RADIUS_RETRY_INTERVAL, t.RadiusRetryInterval);
10924 		FocusEx(hWnd, E_HOSTNAME);
10925 	}
10926 	else
10927 	{
10928 		SetInt(hWnd, E_PORT, RADIUS_DEFAULT_PORT);
10929 		SetInt(hWnd, E_RADIUS_RETRY_INTERVAL, RADIUS_RETRY_INTERVAL);
10930 	}
10931 
10932 	SmRadiusDlgUpdate(hWnd, s);
10933 }
10934 
10935 // Update the control
SmRadiusDlgUpdate(HWND hWnd,SM_HUB * s)10936 void SmRadiusDlgUpdate(HWND hWnd, SM_HUB *s)
10937 {
10938 	bool ok = true;
10939 	bool b, b1;
10940 	char tmp1[MAX_SIZE];
10941 	char tmp2[MAX_SIZE];
10942 	// Validate arguments
10943 	if (hWnd == NULL || s == NULL)
10944 	{
10945 		return;
10946 	}
10947 
10948 	b1 = GetCapsBool(s->p->CapsList, "b_support_radius_retry_interval_and_several_servers");
10949 	if(b1 == false)
10950 	{
10951 		Hide(hWnd, S_RADIUS_7);
10952 		Hide(hWnd, S_RADIUS_8);
10953 		Hide(hWnd, S_RADIUS_9);
10954 		Hide(hWnd, E_RADIUS_RETRY_INTERVAL);
10955 	}
10956 
10957 	b = IsChecked(hWnd, R_USE_RADIUS);
10958 
10959 	SetEnable(hWnd, S_RADIUS_1, b);
10960 	SetEnable(hWnd, S_RADIUS_2, b);
10961 	SetEnable(hWnd, S_RADIUS_3, b);
10962 	SetEnable(hWnd, S_RADIUS3, b);
10963 	SetEnable(hWnd, S_RADIUS_4, b);
10964 	SetEnable(hWnd, S_RADIUS_5, b);
10965 	SetEnable(hWnd, S_RADIUS_6, b);
10966 	SetEnable(hWnd, S_RADIUS_7, b);
10967 	SetEnable(hWnd, S_RADIUS_8, b);
10968 	SetEnable(hWnd, S_RADIUS_9, b);
10969 	SetEnable(hWnd, E_HOSTNAME, b);
10970 	SetEnable(hWnd, E_PORT, b);
10971 	SetEnable(hWnd, E_SECRET1, b);
10972 	SetEnable(hWnd, E_SECRET2, b);
10973 	SetEnable(hWnd, E_RADIUS_RETRY_INTERVAL, b);
10974 
10975 	if (b)
10976 	{
10977 		UINT p, m;
10978 		GetTxtA(hWnd, E_SECRET1, tmp1, sizeof(tmp1));
10979 		GetTxtA(hWnd, E_SECRET2, tmp2, sizeof(tmp2));
10980 
10981 		if (StrCmp(tmp1, tmp2) != 0)
10982 		{
10983 			ok = false;
10984 		}
10985 
10986 		if (IsEmpty(hWnd, E_HOSTNAME))
10987 		{
10988 			ok = false;
10989 		}
10990 
10991 		p = GetInt(hWnd, E_PORT);
10992 
10993 		if (p == 0 || p >= 65536)
10994 		{
10995 			ok = false;
10996 		}
10997 
10998 		m = GetInt(hWnd, E_RADIUS_RETRY_INTERVAL);
10999 		if (m > RADIUS_RETRY_TIMEOUT || m < RADIUS_RETRY_INTERVAL)
11000 		{
11001 			ok = false;
11002 		}
11003 	}
11004 
11005 	SetEnable(hWnd, IDOK, ok);
11006 }
11007 
11008 // [OK] button
SmRadiusDlgOnOk(HWND hWnd,SM_HUB * s)11009 void SmRadiusDlgOnOk(HWND hWnd, SM_HUB *s)
11010 {
11011 	RPC_RADIUS t;
11012 	// Validate arguments
11013 	if (hWnd == NULL || s == NULL)
11014 	{
11015 		return;
11016 	}
11017 
11018 	Zero(&t, sizeof(t));
11019 	StrCpy(t.HubName, sizeof(t.HubName), s->HubName);
11020 
11021 	if (IsChecked(hWnd, R_USE_RADIUS))
11022 	{
11023 		GetTxtA(hWnd, E_HOSTNAME, t.RadiusServerName, sizeof(t.RadiusServerName));
11024 		t.RadiusPort = GetInt(hWnd, E_PORT);
11025 		GetTxtA(hWnd, E_SECRET1,t.RadiusSecret, sizeof(t.RadiusSecret));
11026 		t.RadiusRetryInterval = GetInt(hWnd, E_RADIUS_RETRY_INTERVAL);
11027 	}
11028 
11029 	if (CALL(hWnd, ScSetHubRadius(s->Rpc, &t)) == false)
11030 	{
11031 		return;
11032 	}
11033 
11034 	EndDialog(hWnd, true);
11035 }
11036 
11037 
11038 // Radius dialog procedure
SmRadiusDlgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)11039 UINT SmRadiusDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
11040 {
11041 	SM_HUB *s = (SM_HUB *)param;
11042 	// Validate arguments
11043 	if (hWnd == NULL)
11044 	{
11045 		return 0;
11046 	}
11047 
11048 	switch (msg)
11049 	{
11050 	case WM_INITDIALOG:
11051 		// Initialize
11052 		SmRadiusDlgInit(hWnd, s);
11053 		break;
11054 
11055 	case WM_COMMAND:
11056 		switch (LOWORD(wParam))
11057 		{
11058 		case E_HOSTNAME:
11059 		case E_PORT:
11060 		case E_SECRET1:
11061 		case E_SECRET2:
11062 		case E_RADIUS_RETRY_INTERVAL:
11063 		case R_USE_RADIUS:
11064 			SmRadiusDlgUpdate(hWnd, s);
11065 			break;
11066 		}
11067 
11068 		switch (wParam)
11069 		{
11070 		case IDOK:
11071 			// [OK] button
11072 			SmRadiusDlgOnOk(hWnd, s);
11073 			break;
11074 
11075 		case IDCANCEL:
11076 			// Cancel button
11077 			Close(hWnd);
11078 			break;
11079 
11080 		case R_USE_RADIUS:
11081 			if (IsChecked(hWnd, R_USE_RADIUS))
11082 			{
11083 				FocusEx(hWnd, E_HOSTNAME);
11084 			}
11085 			break;
11086 		}
11087 		break;
11088 
11089 	case WM_CLOSE:
11090 		EndDialog(hWnd, false);
11091 		break;
11092 	}
11093 
11094 	return 0;
11095 }
11096 
11097 // Radius Setup dialog
SmRadiusDlg(HWND hWnd,SM_HUB * s)11098 void SmRadiusDlg(HWND hWnd, SM_HUB *s)
11099 {
11100 	// Validate arguments
11101 	if (hWnd == NULL || s == NULL)
11102 	{
11103 		return;
11104 	}
11105 
11106 	Dialog(hWnd, D_SM_RADIUS, SmRadiusDlgProc, s);
11107 }
11108 
11109 
11110 // Initialize
SmEditAccessInit(HWND hWnd,SM_EDIT_ACCESS * s)11111 void SmEditAccessInit(HWND hWnd, SM_EDIT_ACCESS *s)
11112 {
11113 	ACCESS *a;
11114 	wchar_t tmp[MAX_SIZE];
11115 	// Validate arguments
11116 	if (hWnd == NULL || s == NULL)
11117 	{
11118 		return;
11119 	}
11120 
11121 	SetIcon(hWnd, 0, ICO_PASS);
11122 
11123 	GetTxt(hWnd, 0, tmp, sizeof(tmp));
11124 
11125 	UniStrCat(tmp, sizeof(tmp), s->Access->IsIPv6 ? L" (IPv6)" : L" (IPv4)");
11126 
11127 	SetText(hWnd, 0, tmp);
11128 
11129 	s->Inited = false;
11130 	a = s->Access;
11131 
11132 	SetText(hWnd, E_NOTE, a->Note);
11133 
11134 	Check(hWnd, R_DISCARD, a->Discard);
11135 	Check(hWnd, R_PASS, a->Discard == false);
11136 	SetIntEx(hWnd, E_PRIORITY, a->Priority);
11137 
11138 	if (a->IsIPv6 == false)
11139 	{
11140 		// IPv4
11141 		if (a->SrcIpAddress == 0 && a->SrcSubnetMask == 0)
11142 		{
11143 			Check(hWnd, R_SRC_ALL, true);
11144 		}
11145 		else
11146 		{
11147 			IpSet(hWnd, E_SRC_IP, a->SrcIpAddress);
11148 			IpSet(hWnd, E_SRC_MASK, a->SrcSubnetMask);
11149 		}
11150 
11151 		if (a->DestIpAddress == 0 && a->DestSubnetMask == 0)
11152 		{
11153 			Check(hWnd, R_DST_ALL, true);
11154 		}
11155 		else
11156 		{
11157 			IpSet(hWnd, E_DST_IP, a->DestIpAddress);
11158 			IpSet(hWnd, E_DST_MASK, a->DestSubnetMask);
11159 		}
11160 	}
11161 	else
11162 	{
11163 		// IPv6
11164 		if (IsZeroIP6Addr(&a->SrcIpAddress6) && IsZeroIP6Addr(&a->SrcSubnetMask6))
11165 		{
11166 			Check(hWnd, R_SRC_ALL, true);
11167 		}
11168 		else
11169 		{
11170 			char tmp[MAX_SIZE];
11171 
11172 			IP6AddrToStr(tmp, sizeof(tmp), &a->SrcIpAddress6);
11173 			SetTextA(hWnd, E_SRC_IP_V6, tmp);
11174 
11175 			Mask6AddrToStrEx(tmp, sizeof(tmp), &a->SrcSubnetMask6, false);
11176 
11177 			if (IsNum(tmp))
11178 			{
11179 				StrCatLeft(tmp, sizeof(tmp), "/");
11180 			}
11181 
11182 			SetTextA(hWnd, E_SRC_MASK_V6, tmp);
11183 		}
11184 
11185 		if (IsZeroIP6Addr(&a->DestIpAddress6) && IsZeroIP6Addr(&a->DestSubnetMask6))
11186 		{
11187 			Check(hWnd, R_DST_ALL, true);
11188 		}
11189 		else
11190 		{
11191 			char tmp[MAX_SIZE];
11192 
11193 			IP6AddrToStr(tmp, sizeof(tmp), &a->DestIpAddress6);
11194 			SetTextA(hWnd, E_DST_IP_V6, tmp);
11195 
11196 			Mask6AddrToStrEx(tmp, sizeof(tmp), &a->DestSubnetMask6, false);
11197 
11198 			if (IsNum(tmp))
11199 			{
11200 				StrCatLeft(tmp, sizeof(tmp), "/");
11201 			}
11202 
11203 			SetTextA(hWnd, E_DST_MASK_V6, tmp);
11204 		}
11205 	}
11206 
11207 	CbSetHeight(hWnd, C_PROTOCOL, 18);
11208 	CbAddStr(hWnd, C_PROTOCOL, _UU("SM_ACCESS_PROTO_1"), 0);
11209 	CbAddStr(hWnd, C_PROTOCOL, _UU("SM_ACCESS_PROTO_2"), 0);
11210 	CbAddStr(hWnd, C_PROTOCOL, _UU("SM_ACCESS_PROTO_3"), 0);
11211 	CbAddStr(hWnd, C_PROTOCOL, _UU("SM_ACCESS_PROTO_4"), 0);
11212 	CbAddStr(hWnd, C_PROTOCOL, _UU("SM_ACCESS_PROTO_5"), 0);
11213 	CbAddStr(hWnd, C_PROTOCOL, _UU("SM_ACCESS_PROTO_6"), 0);
11214 
11215 	switch (a->Protocol)
11216 	{
11217 	case 0:
11218 		CbSelectIndex(hWnd, C_PROTOCOL, 0);
11219 		break;
11220 	case 6:
11221 		CbSelectIndex(hWnd, C_PROTOCOL, 1);
11222 		break;
11223 	case 17:
11224 		CbSelectIndex(hWnd, C_PROTOCOL, 2);
11225 		break;
11226 	case 1:
11227 		CbSelectIndex(hWnd, C_PROTOCOL, 3);
11228 		break;
11229 	case 58:
11230 		CbSelectIndex(hWnd, C_PROTOCOL, 4);
11231 		break;
11232 	default:
11233 		CbSelectIndex(hWnd, C_PROTOCOL, 5);
11234 		break;
11235 	}
11236 
11237 	SetIntEx(hWnd, E_IP_PROTO, a->Protocol);
11238 
11239 	SetIntEx(hWnd, E_SRC_PORT_1, a->SrcPortStart);
11240 	SetIntEx(hWnd, E_SRC_PORT_2, a->SrcPortEnd);
11241 	SetIntEx(hWnd, E_DST_PORT_1, a->DestPortStart);
11242 	SetIntEx(hWnd, E_DST_PORT_2, a->DestPortEnd);
11243 
11244 	SetTextA(hWnd, E_USERNAME1, a->SrcUsername);
11245 	SetTextA(hWnd, E_USERNAME2, a->DestUsername);
11246 
11247 	if(a->CheckSrcMac != false)
11248 	{
11249 		char mac[MAX_SIZE], mask[MAX_SIZE];
11250 		MacToStr(mac, sizeof(mac), a->SrcMacAddress);
11251 		MacToStr(mask, sizeof(mask), a->SrcMacMask);
11252 		SetTextA(hWnd, E_SRC_MAC, mac);
11253 		SetTextA(hWnd, E_SRC_MAC_MASK, mask);
11254 	}
11255 	if(a->CheckDstMac != false)
11256 	{
11257 		char mac[MAX_SIZE], mask[MAX_SIZE];
11258 		MacToStr(mac, sizeof(mac), a->DstMacAddress);
11259 		MacToStr(mask, sizeof(mask), a->DstMacMask);
11260 		SetTextA(hWnd, E_DST_MAC, mac);
11261 		SetTextA(hWnd, E_DST_MAC_MASK, mask);
11262 	}
11263 	Check(hWnd, R_CHECK_SRC_MAC, !a->CheckSrcMac);
11264 	Check(hWnd, R_CHECK_DST_MAC, !a->CheckDstMac);
11265 
11266 	Check(hWnd, R_CHECK_TCP_STATE, a->CheckTcpState);
11267 	if(a->CheckTcpState != false)
11268 	{
11269 		Check(hWnd, R_ESTABLISHED, a->Established);
11270 		Check(hWnd, R_UNESTABLISHED, !a->Established);
11271 	}
11272 
11273 	if (GetCapsBool(s->Hub->p->CapsList, "b_support_acl_group") == false)
11274 	{
11275 		SetText(hWnd, S_STATIC11, _UU("D_SM_EDIT_ACCESS@STATIC11_OLD"));
11276 		SetText(hWnd, S_STATIC12, _UU("D_SM_EDIT_ACCESS@STATIC12_OLD"));
11277 		SetText(hWnd, S_STATIC15, _UU("D_SM_EDIT_ACCESS@STATIC15_OLD"));
11278 	}
11279 
11280 	SetEnable(hWnd, R_REDIRECT, GetCapsBool(s->Hub->p->CapsList, "b_support_redirect_url_acl"));
11281 	Check(hWnd, R_REDIRECT, (IsEmptyStr(a->RedirectUrl) ? false : true));
11282 
11283 	s->Inited = true;
11284 
11285 	SmEditAccessUpdate(hWnd, s);
11286 }
11287 
11288 // HTTP Redirection Settings dialog
SmRedirect(HWND hWnd,SM_EDIT_ACCESS * s)11289 void SmRedirect(HWND hWnd, SM_EDIT_ACCESS *s)
11290 {
11291 	// Validate arguments
11292 	if (s == NULL)
11293 	{
11294 		return;
11295 	}
11296 
11297 	Dialog(hWnd, D_SM_REDIRECT, SmRedirectDlg, s);
11298 }
SmRedirectDlg(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)11299 UINT SmRedirectDlg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
11300 {
11301 	SM_EDIT_ACCESS *s = (SM_EDIT_ACCESS *)param;
11302 	char tmp[MAX_REDIRECT_URL_LEN + 1];
11303 
11304 	switch (msg)
11305 	{
11306 	case WM_INITDIALOG:
11307 		SmRedirectDlgInit(hWnd, s);
11308 		break;
11309 
11310 	case WM_COMMAND:
11311 		switch (LOWORD(wParam))
11312 		{
11313 		case E_URL:
11314 			SmRedirectDlgUpdate(hWnd, s);
11315 			break;
11316 		}
11317 
11318 		switch (wParam)
11319 		{
11320 		case IDOK:
11321 			GetTxtA(hWnd, E_URL, tmp, sizeof(tmp));
11322 
11323 			if (StartWith(tmp, "http://") == false &&
11324 				StartWith(tmp, "https://") == false)
11325 			{
11326 				MsgBox(hWnd, MB_ICONEXCLAMATION, _UU("SM_ADVANCED_REDIRECT_URL_MSG"));
11327 
11328 				FocusEx(hWnd, E_URL);
11329 				break;
11330 			}
11331 
11332 			StrCpy(s->Access->RedirectUrl, sizeof(s->Access->RedirectUrl), tmp);
11333 
11334 			EndDialog(hWnd, 1);
11335 			break;
11336 
11337 		case IDCANCEL:
11338 			Close(hWnd);
11339 			break;
11340 
11341 		case B_HINT:
11342 			OnceMsg(hWnd, _UU("SM_ADVANCED_REDIRECT_URL_HINT_TITLE"),
11343 				_UU("SM_ADVANCED_REDIRECT_URL_HINT"), false, ICO_INTERNET);
11344 			break;
11345 		}
11346 		break;
11347 
11348 	case WM_CLOSE:
11349 		EndDialog(hWnd, 0);
11350 		break;
11351 	}
11352 
11353 	return 0;
11354 }
SmRedirectDlgInit(HWND hWnd,SM_EDIT_ACCESS * s)11355 void SmRedirectDlgInit(HWND hWnd, SM_EDIT_ACCESS *s)
11356 {
11357 	ACCESS *a;
11358 	// Validate arguments
11359 	if (hWnd == NULL || s == NULL)
11360 	{
11361 		return;
11362 	}
11363 
11364 	a = s->Access;
11365 
11366 	DlgFont(hWnd, S_BOLD, 0, true);
11367 	DlgFont(hWnd, S_BOLD2, 0, true);
11368 
11369 	SetFont(hWnd, E_SAMPLE1, GetFont("Verdana", 0, false, false, false, false));
11370 	SetFont(hWnd, E_SAMPLE2, GetFont("Verdana", 0, false, false, false, false));
11371 	SetFont(hWnd, E_URL, GetFont("Verdana", 10, false, false, false, false));
11372 
11373 	SetTextA(hWnd, E_SAMPLE1, "http://www.google.com/about/");
11374 	SetTextA(hWnd, E_SAMPLE2, "http://www.google.com/search?q=<INFO>|secret");
11375 
11376 	SetTextA(hWnd, E_URL, s->Access->RedirectUrl);
11377 
11378 	if (IsEmpty(hWnd, E_URL))
11379 	{
11380 		SetTextA(hWnd, E_URL, "http://");
11381 
11382 		SetCursorOnRight(hWnd, E_URL);
11383 		Focus(hWnd, E_URL);
11384 	}
11385 	else
11386 	{
11387 		FocusEx(hWnd, E_URL);
11388 	}
11389 
11390 	SmRedirectDlgUpdate(hWnd, s);
11391 }
SmRedirectDlgUpdate(HWND hWnd,SM_EDIT_ACCESS * s)11392 void SmRedirectDlgUpdate(HWND hWnd, SM_EDIT_ACCESS *s)
11393 {
11394 	char tmp[MAX_REDIRECT_URL_LEN + 1];
11395 	bool ok = true;
11396 	// Validate arguments
11397 	if (hWnd == NULL || s == NULL)
11398 	{
11399 		return;
11400 	}
11401 
11402 	GetTxtA(hWnd, E_URL, tmp, sizeof(tmp));
11403 
11404 	if (IsEmptyStr(tmp))
11405 	{
11406 		ok = false;
11407 	}
11408 
11409 	SetEnable(hWnd, IDOK, ok);
11410 }
11411 
11412 // Update the control
SmEditAccessUpdate(HWND hWnd,SM_EDIT_ACCESS * s)11413 void SmEditAccessUpdate(HWND hWnd, SM_EDIT_ACCESS *s)
11414 {
11415 	bool ok = true;
11416 	bool tcp;
11417 	bool b;
11418 	bool check_srcmac, check_dstmac, support_mac;
11419 	bool check_state, support_check_state;
11420 	char srcmac[MAX_SIZE], srcmac_mask[MAX_SIZE], dstmac[MAX_SIZE], dstmac_mask[MAX_SIZE];
11421 	char tmp[MAX_SIZE];
11422 	wchar_t unitmp[MAX_SIZE];
11423 	ACCESS *a;
11424 	// Validate arguments
11425 	if (hWnd == NULL || s == NULL)
11426 	{
11427 		return;
11428 	}
11429 
11430 	if (s->Inited == false)
11431 	{
11432 		return;
11433 	}
11434 
11435 	a = s->Access;
11436 
11437 	GetTxt(hWnd, E_NOTE, a->Note, sizeof(a->Note));
11438 
11439 	a->Discard = IsChecked(hWnd, R_DISCARD);
11440 
11441 	a->Priority = GetInt(hWnd, E_PRIORITY);
11442 	if (a->Priority == 0)
11443 	{
11444 		ok = false;
11445 	}
11446 
11447 
11448 	b = IsChecked(hWnd, R_SRC_ALL) ? false : true;
11449 	if (b == false)
11450 	{
11451 		if (a->IsIPv6 == false)
11452 		{
11453 			a->SrcIpAddress = 0;
11454 			a->SrcSubnetMask = 0;
11455 		}
11456 		else
11457 		{
11458 			Zero(&a->SrcIpAddress6, sizeof(IPV6_ADDR));
11459 			Zero(&a->SrcSubnetMask6, sizeof(IPV6_ADDR));
11460 		}
11461 	}
11462 	else
11463 	{
11464 		if (a->IsIPv6 == false)
11465 		{
11466 			if (IpIsFilled(hWnd, E_SRC_IP) == false || IpIsFilled(hWnd, E_SRC_MASK) == false)
11467 			{
11468 				ok = false;
11469 			}
11470 			else
11471 			{
11472 				a->SrcIpAddress = IpGet(hWnd, E_SRC_IP);
11473 				a->SrcSubnetMask = IpGet(hWnd, E_SRC_MASK);
11474 			}
11475 		}
11476 		else
11477 		{
11478 			char tmp1[MAX_SIZE];
11479 			char tmp2[MAX_SIZE];
11480 
11481 			GetTxtA(hWnd, E_SRC_IP_V6, tmp1, sizeof(tmp1));
11482 			GetTxtA(hWnd, E_SRC_MASK_V6, tmp2, sizeof(tmp2));
11483 
11484 			if (StrToIP6Addr(&a->SrcIpAddress6, tmp1) == false ||
11485 				StrToMask6Addr(&a->SrcSubnetMask6, tmp2) == false)
11486 			{
11487 				ok = false;
11488 			}
11489 		}
11490 	}
11491 	SetEnable(hWnd, S_SRC_IP_1, b);
11492 	SetEnable(hWnd, S_SRC_IP_2, b);
11493 	SetEnable(hWnd, S_SRC_IP_3, b);
11494 	SetEnable(hWnd, E_SRC_IP, b);
11495 	SetEnable(hWnd, E_SRC_MASK, b);
11496 	SetEnable(hWnd, E_SRC_IP_V6, b);
11497 	SetEnable(hWnd, E_SRC_MASK_V6, b);
11498 
11499 	b = IsChecked(hWnd, R_DST_ALL) ? false : true;
11500 	if (b == false)
11501 	{
11502 		if (a->IsIPv6 == false)
11503 		{
11504 			a->DestIpAddress = 0;
11505 			a->DestSubnetMask = 0;
11506 		}
11507 		else
11508 		{
11509 			Zero(&a->DestIpAddress6, sizeof(IPV6_ADDR));
11510 			Zero(&a->DestSubnetMask6, sizeof(IPV6_ADDR));
11511 		}
11512 	}
11513 	else
11514 	{
11515 		if (a->IsIPv6 == false)
11516 		{
11517 			if (IpIsFilled(hWnd, E_DST_IP) == false || IpIsFilled(hWnd, E_DST_MASK) == false)
11518 			{
11519 				ok = false;
11520 			}
11521 			else
11522 			{
11523 				a->DestIpAddress = IpGet(hWnd, E_DST_IP);
11524 				a->DestSubnetMask = IpGet(hWnd, E_DST_MASK);
11525 			}
11526 		}
11527 		else
11528 		{
11529 			char tmp1[MAX_SIZE];
11530 			char tmp2[MAX_SIZE];
11531 
11532 			GetTxtA(hWnd, E_DST_IP_V6, tmp1, sizeof(tmp1));
11533 			GetTxtA(hWnd, E_DST_MASK_V6, tmp2, sizeof(tmp2));
11534 
11535 			if (StrToIP6Addr(&a->DestIpAddress6, tmp1) == false ||
11536 				StrToMask6Addr(&a->DestSubnetMask6, tmp2) == false)
11537 			{
11538 				ok = false;
11539 			}
11540 		}
11541 	}
11542 	SetEnable(hWnd, S_IP_DST_1, b);
11543 	SetEnable(hWnd, S_IP_DST_2, b);
11544 	SetEnable(hWnd, S_IP_DST_3, b);
11545 	SetEnable(hWnd, E_DST_IP, b);
11546 	SetEnable(hWnd, E_DST_MASK, b);
11547 	SetEnable(hWnd, E_DST_IP_V6, b);
11548 	SetEnable(hWnd, E_DST_MASK_V6, b);
11549 
11550 	a->Protocol = GetInt(hWnd, C_PROTOCOL);
11551 
11552 	GetTxtA(hWnd, C_PROTOCOL, tmp, sizeof(tmp));
11553 	GetTxt(hWnd, C_PROTOCOL, unitmp, sizeof(unitmp));
11554 
11555 	if (UniStrCmpi(unitmp, _UU("SM_ACCESS_PROTO_6")) == 0 || StrCmpi(tmp, _SS("SM_ACCESS_PROTO_6")) == 0)
11556 	{
11557 		a->Protocol = GetInt(hWnd, E_IP_PROTO);
11558 
11559 		if (IsEmpty(hWnd, E_IP_PROTO))
11560 		{
11561 			ok = false;
11562 		}
11563 
11564 		Enable(hWnd, S_PROTOID);
11565 		Enable(hWnd, E_IP_PROTO);
11566 	}
11567 	else
11568 	{
11569 		Disable(hWnd, E_IP_PROTO);
11570 		Disable(hWnd, S_PROTOID);
11571 	}
11572 
11573 	tcp = false;
11574 	if (a->Protocol == 17 || a->Protocol == 6)
11575 	{
11576 		tcp = true;
11577 	}
11578 
11579 	SetEnable(hWnd, S_TCP_1, tcp);
11580 	SetEnable(hWnd, S_TCP_2, tcp);
11581 	SetEnable(hWnd, S_TCP_3, tcp);
11582 	SetEnable(hWnd, S_TCP_4, tcp);
11583 	SetEnable(hWnd, S_TCP_5, tcp);
11584 	SetEnable(hWnd, S_TCP_6, tcp);
11585 	SetEnable(hWnd, S_TCP_7, tcp);
11586 	SetEnable(hWnd, E_SRC_PORT_1, tcp);
11587 	SetEnable(hWnd, E_SRC_PORT_2, tcp);
11588 	SetEnable(hWnd, E_DST_PORT_1, tcp);
11589 	SetEnable(hWnd, E_DST_PORT_2, tcp);
11590 
11591 	if (tcp == false)
11592 	{
11593 		a->SrcPortEnd = a->SrcPortStart = a->DestPortEnd = a->DestPortStart = 0;
11594 	}
11595 	else
11596 	{
11597 		a->SrcPortStart = GetInt(hWnd, E_SRC_PORT_1);
11598 		a->SrcPortEnd = GetInt(hWnd, E_SRC_PORT_2);
11599 		a->DestPortStart = GetInt(hWnd, E_DST_PORT_1);
11600 		a->DestPortEnd = GetInt(hWnd, E_DST_PORT_2);
11601 
11602 		if (a->SrcPortStart != 0)
11603 		{
11604 			if (a->SrcPortEnd != 0)
11605 			{
11606 				if (a->SrcPortStart > a->SrcPortEnd)
11607 				{
11608 					ok = false;
11609 				}
11610 			}
11611 		}
11612 		else
11613 		{
11614 			if (a->SrcPortEnd != 0)
11615 			{
11616 				ok = false;
11617 			}
11618 		}
11619 
11620 		if (a->DestPortStart != 0)
11621 		{
11622 			if (a->DestPortEnd != 0)
11623 			{
11624 				if (a->DestPortStart > a->DestPortEnd)
11625 				{
11626 					ok = false;
11627 				}
11628 			}
11629 		}
11630 		else
11631 		{
11632 			if (a->DestPortEnd != 0)
11633 			{
11634 				ok = false;
11635 			}
11636 		}
11637 
11638 		if (a->DestPortEnd < a->DestPortStart)
11639 		{
11640 			a->DestPortEnd = a->DestPortStart;
11641 		}
11642 
11643 		if (a->SrcPortEnd < a->SrcPortStart)
11644 		{
11645 			a->SrcPortEnd = a->SrcPortStart;
11646 		}
11647 	}
11648 
11649 	a->SrcUsernameHash = a->DestUsernameHash = 0;
11650 	GetTxtA(hWnd, E_USERNAME1, a->SrcUsername, sizeof(a->SrcUsername));
11651 	GetTxtA(hWnd, E_USERNAME2, a->DestUsername, sizeof(a->DestUsername));
11652 
11653 	if (StartWith(a->SrcUsername, ACCESS_LIST_INCLUDED_PREFIX) == false &&
11654 		StartWith(a->SrcUsername, ACCESS_LIST_EXCLUDED_PREFIX) == false)
11655 	{
11656 		MakeSimpleUsernameRemoveNtDomain(a->SrcUsername, sizeof(a->SrcUsername), a->SrcUsername);
11657 	}
11658 
11659 	if (StartWith(a->DestUsername, ACCESS_LIST_INCLUDED_PREFIX) == false &&
11660 		StartWith(a->DestUsername, ACCESS_LIST_EXCLUDED_PREFIX) == false)
11661 	{
11662 		MakeSimpleUsernameRemoveNtDomain(a->DestUsername, sizeof(a->DestUsername), a->DestUsername);
11663 	}
11664 
11665 	Trim(a->SrcUsername);
11666 	/*
11667 	if (StrLen(a->SrcUsername) != 0)
11668 	{
11669 		if (IsUserName(a->SrcUsername) == false)
11670 		{
11671 			ok = false;
11672 		}
11673 	}*/
11674 
11675 	Trim(a->DestUsername);
11676 	/*
11677 	if (StrLen(a->DestUsername) != 0)
11678 	{
11679 		if (IsUserName(a->DestUsername) == false)
11680 		{
11681 			ok = false;
11682 		}
11683 	}*/
11684 
11685 	support_mac = GetCapsBool(s->Hub->p->CapsList, "b_support_check_mac");
11686 
11687 	// Set the source MAC address
11688 	check_srcmac = a->CheckSrcMac = support_mac && (IsChecked(hWnd, R_CHECK_SRC_MAC) ? false : true);
11689 	if(check_srcmac == false)
11690 	{
11691 		Zero(a->SrcMacAddress, sizeof(a->SrcMacAddress));
11692 		Zero(a->SrcMacMask, sizeof(a->SrcMacMask));
11693 	}
11694 	else
11695 	{
11696 		GetTxtA(hWnd, E_SRC_MAC, srcmac, sizeof(srcmac));
11697 		GetTxtA(hWnd, E_SRC_MAC_MASK, srcmac_mask, sizeof(srcmac_mask));
11698 		Trim(srcmac);
11699 		Trim(srcmac_mask);
11700 		if(StrLen(srcmac) != 0 && StrLen(srcmac_mask) != 0)
11701 		{
11702 			UCHAR mac[6], mask[6];
11703 			if(StrToMac(mac, srcmac) && StrToMac(mask, srcmac_mask))
11704 			{
11705 				Copy(a->SrcMacAddress, mac, 6);
11706 				Copy(a->SrcMacMask, mask, 6);
11707 			}
11708 			else
11709 			{
11710 				ok = false;
11711 			}
11712 		}
11713 		else
11714 		{
11715 			ok = false;
11716 		}
11717 	}
11718 	SetEnable(hWnd, S_CHECK_SRC_MAC, support_mac);
11719 	SetEnable(hWnd, R_CHECK_SRC_MAC, support_mac);
11720 	SetEnable(hWnd, S_SRC_MAC, check_srcmac);
11721 	SetEnable(hWnd, S_SRC_MAC_MASK, check_srcmac);
11722 	SetEnable(hWnd, E_SRC_MAC, check_srcmac);
11723 	SetEnable(hWnd, E_SRC_MAC_MASK, check_srcmac);
11724 
11725 	// Set the destination MAC address
11726 	check_dstmac = a->CheckDstMac = support_mac && (IsChecked(hWnd, R_CHECK_DST_MAC) ? false : true);
11727 	if(check_dstmac == false)
11728 	{
11729 		Zero(a->DstMacAddress, sizeof(a->DstMacAddress));
11730 		Zero(a->DstMacMask, sizeof(a->DstMacMask));
11731 	}
11732 	else
11733 	{
11734 		GetTxtA(hWnd, E_DST_MAC, dstmac, sizeof(dstmac));
11735 		GetTxtA(hWnd, E_DST_MAC_MASK, dstmac_mask, sizeof(dstmac_mask));
11736 		Trim(dstmac);
11737 		Trim(dstmac_mask);
11738 		if(StrLen(dstmac) != 0 && StrLen(dstmac_mask) != 0)
11739 		{
11740 			UCHAR mac[6], mask[6];
11741 			if(StrToMac(mac, dstmac) && StrToMac(mask, dstmac_mask))
11742 			{
11743 				Copy(a->DstMacAddress, mac, 6);
11744 				Copy(a->DstMacMask, mask, 6);
11745 			}
11746 			else
11747 			{
11748 				ok = false;
11749 			}
11750 		}
11751 		else
11752 		{
11753 			ok = false;
11754 		}
11755 	}
11756 	SetEnable(hWnd, S_CHECK_DST_MAC, support_mac);
11757 	SetEnable(hWnd, R_CHECK_DST_MAC, support_mac);
11758 	SetEnable(hWnd, S_DST_MAC, check_dstmac);
11759 	SetEnable(hWnd, S_DST_MAC_MASK, check_dstmac);
11760 	SetEnable(hWnd, E_DST_MAC, check_dstmac);
11761 	SetEnable(hWnd, E_DST_MAC_MASK, check_dstmac);
11762 
11763 	SetEnable(hWnd, S_MAC_NOTE, check_srcmac || check_dstmac);
11764 
11765 	// Status of the TCP connection
11766 	support_check_state = GetCapsBool(s->Hub->p->CapsList, "b_support_check_tcp_state") && a->Protocol == 6;
11767 	SetEnable(hWnd, R_CHECK_TCP_STATE, support_check_state);
11768 	check_state = a->CheckTcpState = support_check_state && IsChecked(hWnd, R_CHECK_TCP_STATE);
11769 
11770 	a->Established = IsChecked(hWnd, R_ESTABLISHED) && check_state;
11771 	SetEnable(hWnd, R_ESTABLISHED, check_state);
11772 	SetEnable(hWnd, R_UNESTABLISHED, check_state);
11773 	if(check_state != false && IsChecked(hWnd, R_ESTABLISHED) == false && IsChecked(hWnd, R_UNESTABLISHED) == false)
11774 	{
11775 		ok = false;
11776 	}
11777 
11778 	// Settings button such as delay
11779 	SetEnable(hWnd, B_SIMULATION, a->Discard == false && GetCapsBool(s->Hub->p->CapsList, "b_support_ex_acl"));
11780 
11781 	// HTTP redirection settings button
11782 	SetEnable(hWnd, B_REDIRECT, IsChecked(hWnd, R_REDIRECT) && (a->Discard == false) && GetCapsBool(s->Hub->p->CapsList, "b_support_redirect_url_acl"));
11783 	SetEnable(hWnd, R_REDIRECT, (a->Discard == false) && GetCapsBool(s->Hub->p->CapsList, "b_support_redirect_url_acl"));
11784 
11785 	if (IsChecked(hWnd, R_REDIRECT) && (a->Discard == false) && GetCapsBool(s->Hub->p->CapsList, "b_support_redirect_url_acl"))
11786 	{
11787 		if (IsEmptyStr(a->RedirectUrl))
11788 		{
11789 			ok = false;
11790 		}
11791 	}
11792 
11793 	SetEnable(hWnd, IDOK, ok);
11794 }
11795 
11796 // OK Click
SmEditAccessOnOk(HWND hWnd,SM_EDIT_ACCESS * s)11797 void SmEditAccessOnOk(HWND hWnd, SM_EDIT_ACCESS *s)
11798 {
11799 	ACCESS *a;
11800 	// Validate arguments
11801 	if (hWnd == NULL || s == NULL)
11802 	{
11803 		return;
11804 	}
11805 
11806 	a = s->Access;
11807 
11808 	SmEditAccessUpdate(hWnd, s);
11809 
11810 	if (IsChecked(hWnd, R_REDIRECT) == false || (a->Discard) || GetCapsBool(s->Hub->p->CapsList, "b_support_redirect_url_acl") == false)
11811 	{
11812 		// Disable the HTTP redirection
11813 		ClearStr(a->RedirectUrl, sizeof(a->RedirectUrl));
11814 	}
11815 
11816 	EndDialog(hWnd, true);
11817 }
11818 
11819 
11820 // Access list editing dialog
SmEditAccessDlg(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)11821 UINT SmEditAccessDlg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
11822 {
11823 	SM_EDIT_ACCESS *s = (SM_EDIT_ACCESS *)param;
11824 	UINT ico;
11825 	ACCESS *a;
11826 	char tmp[MAX_SIZE];
11827 	// Validate arguments
11828 	if (hWnd == NULL)
11829 	{
11830 		return 0;
11831 	}
11832 
11833 	switch (msg)
11834 	{
11835 	case WM_INITDIALOG:
11836 		// Initialize
11837 		SmEditAccessInit(hWnd, s);
11838 
11839 		goto REFRESH_ICON;
11840 
11841 	case WM_COMMAND:
11842 		switch (LOWORD(wParam))
11843 		{
11844 		case R_PASS:
11845 		case R_DISCARD:
11846 		case E_PRIORITY:
11847 		case R_SRC_ALL:
11848 		case E_SRC_IP:
11849 		case E_SRC_MASK:
11850 		case R_DST_ALL:
11851 		case E_DST_MASK:
11852 		case E_SRC_IP_V6:
11853 		case E_SRC_MASK_V6:
11854 		case E_DST_MASK_V6:
11855 		case E_DST_IP_V6:
11856 		case C_PROTOCOL:
11857 		case E_SRC_PORT_1:
11858 		case E_SRC_PORT_2:
11859 		case E_DST_PORT_1:
11860 		case E_DST_PORT_2:
11861 		case E_USERNAME1:
11862 		case E_USERNAME2:
11863 		case E_IP_PROTO:
11864 		case R_CHECK_SRC_MAC:
11865 		case E_SRC_MAC:
11866 		case E_SRC_MAC_MASK:
11867 		case R_CHECK_DST_MAC:
11868 		case E_DST_MAC:
11869 		case E_DST_MAC_MASK:
11870 		case R_CHECK_TCP_STATE:
11871 		case R_ESTABLISHED:
11872 		case R_UNESTABLISHED:
11873 		case R_REDIRECT:
11874 			SmEditAccessUpdate(hWnd, s);
11875 			break;
11876 		}
11877 
11878 		switch (wParam)
11879 		{
11880 		case B_USER1:
11881 			if (GetTxtA(hWnd, E_USERNAME1, tmp, sizeof(tmp)))
11882 			{
11883 				char *ret = SmSelectUserDlgEx(hWnd, s->Hub, tmp, GetCapsBool(s->Hub->p->CapsList, "b_support_acl_group"));
11884 				if (ret == NULL)
11885 				{
11886 					SetTextA(hWnd, E_USERNAME1, "");
11887 				}
11888 				else
11889 				{
11890 					SetTextA(hWnd, E_USERNAME1, ret);
11891 					Free(ret);
11892 				}
11893 				FocusEx(hWnd, E_USERNAME1);
11894 			}
11895 			break;
11896 
11897 		case B_USER2:
11898 			if (GetTxtA(hWnd, E_USERNAME2, tmp, sizeof(tmp)))
11899 			{
11900 				char *ret = SmSelectUserDlgEx(hWnd, s->Hub, tmp, GetCapsBool(s->Hub->p->CapsList, "b_support_acl_group"));
11901 				if (ret == NULL)
11902 				{
11903 					SetTextA(hWnd, E_USERNAME2, "");
11904 				}
11905 				else
11906 				{
11907 					SetTextA(hWnd, E_USERNAME2, ret);
11908 					Free(ret);
11909 				}
11910 				FocusEx(hWnd, E_USERNAME2);
11911 			}
11912 			break;
11913 
11914 		case IDOK:
11915 			// [OK] button
11916 			SmEditAccessOnOk(hWnd, s);
11917 			break;
11918 
11919 		case IDCANCEL:
11920 			// Cancel button
11921 			Close(hWnd);
11922 			break;
11923 
11924 		case R_SRC_ALL:
11925 			if (IsChecked(hWnd, R_SRC_ALL) == false)
11926 			{
11927 				if (s->Access->IsIPv6)
11928 				{
11929 					FocusEx(hWnd, E_SRC_IP_V6);
11930 				}
11931 				else
11932 				{
11933 					Focus(hWnd, E_SRC_IP);
11934 				}
11935 			}
11936 			break;
11937 
11938 		case R_DST_ALL:
11939 			if (IsChecked(hWnd, R_DST_ALL) == false)
11940 			{
11941 				if (s->Access->IsIPv6)
11942 				{
11943 					FocusEx(hWnd, E_DST_IP_V6);
11944 				}
11945 				else
11946 				{
11947 					Focus(hWnd, E_DST_IP);
11948 				}
11949 			}
11950 			break;
11951 		case R_CHECK_SRC_MAC:
11952 			if(IsChecked(hWnd, R_CHECK_SRC_MAC) == false)
11953 			{
11954 				Focus(hWnd, E_SRC_MAC);
11955 			}
11956 			break;
11957 		case R_CHECK_DST_MAC:
11958 			if(IsChecked(hWnd, R_CHECK_DST_MAC) == false)
11959 			{
11960 				Focus(hWnd, E_DST_MAC);
11961 			}
11962 			break;
11963 
11964 		case R_PASS:
11965 		case R_DISCARD:
11966 REFRESH_ICON:
11967 			a = s->Access;
11968 			if (a->Discard == false && a->Active == false)
11969 			{
11970 				ico = ICO_PASS_DISABLE;
11971 			}
11972 			else if (a->Discard == false && a->Active)
11973 			{
11974 				ico = ICO_PASS;
11975 			}
11976 			else if (a->Discard && a->Active == false)
11977 			{
11978 				ico = ICO_DISCARD_DISABLE;
11979 			}
11980 			else
11981 			{
11982 				ico = ICO_DISCARD;
11983 			}
11984 
11985 			SetIcon(hWnd, S_ICON, ico);
11986 			break;
11987 
11988 		case B_SIMULATION:
11989 			// Simulation
11990 			Dialog(hWnd, D_SM_SIMULATION, SmSimulationDlg, s);
11991 			break;
11992 
11993 		case B_REDIRECT:
11994 			// Set the URL to redirect to
11995 			SmRedirect(hWnd, s);
11996 			SmEditAccessUpdate(hWnd, s);
11997 			break;
11998 		}
11999 		break;
12000 
12001 	case WM_CLOSE:
12002 		EndDialog(hWnd, false);
12003 		break;
12004 	}
12005 
12006 	return 0;
12007 }
12008 
12009 // Delay, jitter, packet-loss dialog
SmSimulationDlg(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)12010 UINT SmSimulationDlg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
12011 {
12012 	SM_EDIT_ACCESS *s = (SM_EDIT_ACCESS *)param;
12013 	// Validate arguments
12014 	if (hWnd == NULL)
12015 	{
12016 		return 0;
12017 	}
12018 
12019 	switch (msg)
12020 	{
12021 	case WM_INITDIALOG:
12022 		SmSimulationInit(hWnd, s);
12023 		break;
12024 
12025 	case WM_COMMAND:
12026 		switch (LOWORD(wParam))
12027 		{
12028 		case E_DELAY:
12029 		case E_JITTER:
12030 		case E_LOSS:
12031 			SmSimulationUpdate(hWnd, s);
12032 			break;
12033 		}
12034 
12035 		switch (wParam)
12036 		{
12037 		case IDOK:
12038 			SmSimulationOnOk(hWnd, s);
12039 			break;
12040 
12041 		case IDCANCEL:
12042 			Close(hWnd);
12043 			break;
12044 
12045 		case C_DELAY:
12046 			SmSimulationUpdate(hWnd, s);
12047 			if (IsChecked(hWnd, C_DELAY))
12048 			{
12049 				FocusEx(hWnd, E_DELAY);
12050 			}
12051 			break;
12052 
12053 		case C_JITTER:
12054 			SmSimulationUpdate(hWnd, s);
12055 			if (IsChecked(hWnd, C_JITTER))
12056 			{
12057 				FocusEx(hWnd, E_JITTER);
12058 			}
12059 			break;
12060 
12061 		case C_LOSS:
12062 			SmSimulationUpdate(hWnd, s);
12063 			if (IsChecked(hWnd, C_LOSS))
12064 			{
12065 				FocusEx(hWnd, E_LOSS);
12066 			}
12067 			break;
12068 		}
12069 		break;
12070 
12071 	case WM_CLOSE:
12072 		EndDialog(hWnd, false);
12073 		break;
12074 	}
12075 
12076 	return 0;
12077 }
12078 
12079 // Update of delay, jitter, packet-loss dialog
SmSimulationUpdate(HWND hWnd,SM_EDIT_ACCESS * s)12080 void SmSimulationUpdate(HWND hWnd, SM_EDIT_ACCESS *s)
12081 {
12082 	bool b1, b2, b3;
12083 	bool ok = true;
12084 	// Validate arguments
12085 	if (hWnd == NULL || s == NULL)
12086 	{
12087 		return;
12088 	}
12089 
12090 	b1 = IsChecked(hWnd, C_DELAY);
12091 	b2 = IsChecked(hWnd, C_JITTER);
12092 	b3 = IsChecked(hWnd, C_LOSS);
12093 
12094 	SetEnable(hWnd, S_DELAY, b1);
12095 	SetEnable(hWnd, S_DELAY2, b1);
12096 	SetEnable(hWnd, E_DELAY, b1);
12097 
12098 	SetEnable(hWnd, C_JITTER, b1);
12099 
12100 	if (b1 == false)
12101 	{
12102 		b2 = false;
12103 	}
12104 
12105 	SetEnable(hWnd, S_JITTER, b2);
12106 	SetEnable(hWnd, S_JITTER2, b2);
12107 	SetEnable(hWnd, E_JITTER, b2);
12108 
12109 	SetEnable(hWnd, S_LOSS, b3);
12110 	SetEnable(hWnd, S_LOSS2, b3);
12111 	SetEnable(hWnd, E_LOSS, b3);
12112 
12113 	if (b1)
12114 	{
12115 		UINT i = GetInt(hWnd, E_DELAY);
12116 		if (i == 0 || i > HUB_ACCESSLIST_DELAY_MAX)
12117 		{
12118 			ok = false;
12119 		}
12120 	}
12121 
12122 	if (b2)
12123 	{
12124 		UINT i = GetInt(hWnd, E_JITTER);
12125 		if (i == 0 || i > HUB_ACCESSLIST_JITTER_MAX)
12126 		{
12127 			ok = false;
12128 		}
12129 	}
12130 
12131 	if (b3)
12132 	{
12133 		UINT i = GetInt(hWnd, E_LOSS);
12134 		if (i == 0 || i > HUB_ACCESSLIST_LOSS_MAX)
12135 		{
12136 			ok = false;
12137 		}
12138 	}
12139 
12140 	SetEnable(hWnd, IDOK, ok);
12141 }
12142 
12143 // Initialization of delay, jitter, packet-loss dialog
SmSimulationInit(HWND hWnd,SM_EDIT_ACCESS * s)12144 void SmSimulationInit(HWND hWnd, SM_EDIT_ACCESS *s)
12145 {
12146 	ACCESS *a;
12147 	// Validate arguments
12148 	if (hWnd == NULL || s == NULL)
12149 	{
12150 		return;
12151 	}
12152 
12153 	a = s->Access;
12154 
12155 	Check(hWnd, C_DELAY, a->Delay != 0);
12156 	Check(hWnd, C_JITTER, a->Jitter != 0);
12157 	Check(hWnd, C_LOSS, a->Loss != 0);
12158 
12159 	SetIntEx(hWnd, E_DELAY, a->Delay);
12160 	if (a->Delay != 0)
12161 	{
12162 		SetIntEx(hWnd, E_JITTER, a->Jitter);
12163 	}
12164 	SetIntEx(hWnd, E_LOSS, a->Loss);
12165 
12166 	SmSimulationUpdate(hWnd, s);
12167 
12168 	if (a->Delay != 0)
12169 	{
12170 		FocusEx(hWnd, E_DELAY);
12171 	}
12172 	else
12173 	{
12174 		Focus(hWnd, C_DELAY);
12175 	}
12176 }
12177 
12178 // Saving of delay, jitter, packet-loss dialog
SmSimulationOnOk(HWND hWnd,SM_EDIT_ACCESS * s)12179 void SmSimulationOnOk(HWND hWnd, SM_EDIT_ACCESS *s)
12180 {
12181 	ACCESS *a;
12182 	// Validate arguments
12183 	if (hWnd == NULL || s == NULL)
12184 	{
12185 		return;
12186 	}
12187 
12188 	a = s->Access;
12189 
12190 	a->Jitter = a->Loss = a->Delay = 0;
12191 
12192 	if (IsChecked(hWnd, C_DELAY))
12193 	{
12194 		a->Delay = GetInt(hWnd, E_DELAY);
12195 	}
12196 
12197 	if (IsChecked(hWnd, C_JITTER))
12198 	{
12199 		a->Jitter = GetInt(hWnd, E_JITTER);
12200 	}
12201 
12202 	if (IsChecked(hWnd, C_LOSS))
12203 	{
12204 		a->Loss = GetInt(hWnd, E_LOSS);
12205 	}
12206 
12207 	EndDialog(hWnd, 1);
12208 }
12209 
12210 // Edit the access list
SmEditAccess(HWND hWnd,SM_ACCESS_LIST * s,ACCESS * a)12211 bool SmEditAccess(HWND hWnd, SM_ACCESS_LIST *s, ACCESS *a)
12212 {
12213 	SM_EDIT_ACCESS edit;
12214 	bool ret;
12215 	UINT i;
12216 	// Validate arguments
12217 	if (hWnd == NULL || s == NULL)
12218 	{
12219 		return false;
12220 	}
12221 
12222 	Zero(&edit, sizeof(edit));
12223 	edit.AccessList = s;
12224 	edit.EditMode = true;
12225 	edit.Access = ZeroMalloc(sizeof(ACCESS));
12226 	edit.Hub = s->Hub;
12227 	Copy(edit.Access, a, sizeof(ACCESS));
12228 
12229 	if (edit.Access->IsIPv6 == false)
12230 	{
12231 		ret = Dialog(hWnd, D_SM_EDIT_ACCESS, SmEditAccessDlg, &edit);
12232 	}
12233 	else
12234 	{
12235 		ret = Dialog(hWnd, D_SM_EDIT_ACCESS_V6, SmEditAccessDlg, &edit);
12236 	}
12237 
12238 	if (ret)
12239 	{
12240 		Copy(a, edit.Access, sizeof(ACCESS));
12241 		Free(edit.Access);
12242 		Sort(s->AccessList);
12243 
12244 		// Reassign the ID
12245 		for (i = 0;i < LIST_NUM(s->AccessList);i++)
12246 		{
12247 			ACCESS *a = LIST_DATA(s->AccessList, i);
12248 			a->Id = (i + 1);
12249 		}
12250 	}
12251 	else
12252 	{
12253 		Free(edit.Access);
12254 	}
12255 
12256 	return ret;
12257 }
12258 
12259 // Clone of the access list
SmCloneAccess(HWND hWnd,SM_ACCESS_LIST * s,ACCESS * t)12260 bool SmCloneAccess(HWND hWnd, SM_ACCESS_LIST *s, ACCESS *t)
12261 {
12262 	SM_EDIT_ACCESS edit;
12263 	bool ret;
12264 	UINT i;
12265 	// Validate arguments
12266 	if (hWnd == NULL || s == NULL || t == NULL)
12267 	{
12268 		return false;
12269 	}
12270 
12271 	Zero(&edit, sizeof(edit));
12272 	edit.AccessList = s;
12273 	edit.Access = Clone(t, sizeof(ACCESS));
12274 	edit.Access->Priority = 0;
12275 	edit.Hub = s->Hub;
12276 
12277 	// Generate a number that does not duplicate with other and is larger than the priority of the cloning original
12278 	for (edit.Access->Priority = t->Priority;edit.Access->Priority != INFINITE;edit.Access->Priority++)
12279 	{
12280 		bool exists = false;
12281 
12282 		for (i = 0;i < LIST_NUM(s->AccessList);i++)
12283 		{
12284 			ACCESS *a = LIST_DATA(s->AccessList, i);
12285 
12286 			if (a->Priority == edit.Access->Priority)
12287 			{
12288 				exists = true;
12289 				break;
12290 			}
12291 		}
12292 
12293 		if (exists == false)
12294 		{
12295 			break;
12296 		}
12297 	}
12298 
12299 	if (edit.Access->IsIPv6 == false)
12300 	{
12301 		ret = Dialog(hWnd, D_SM_EDIT_ACCESS, SmEditAccessDlg, &edit);
12302 	}
12303 	else
12304 	{
12305 		ret = Dialog(hWnd, D_SM_EDIT_ACCESS_V6, SmEditAccessDlg, &edit);
12306 	}
12307 
12308 	if (ret)
12309 	{
12310 		Insert(s->AccessList, edit.Access);
12311 
12312 		// Reassign the ID
12313 		for (i = 0;i < LIST_NUM(s->AccessList);i++)
12314 		{
12315 			ACCESS *a = LIST_DATA(s->AccessList, i);
12316 			a->Id = (i + 1);
12317 		}
12318 	}
12319 	else
12320 	{
12321 		Free(edit.Access);
12322 	}
12323 
12324 	return ret;
12325 }
12326 
12327 // Add to Access List
SmAddAccess(HWND hWnd,SM_ACCESS_LIST * s,bool ipv6)12328 bool SmAddAccess(HWND hWnd, SM_ACCESS_LIST *s, bool ipv6)
12329 {
12330 	SM_EDIT_ACCESS edit;
12331 	bool ret;
12332 	UINT i;
12333 	// Validate arguments
12334 	if (hWnd == NULL || s == NULL)
12335 	{
12336 		return false;
12337 	}
12338 
12339 	Zero(&edit, sizeof(edit));
12340 	edit.AccessList = s;
12341 	edit.Access = ZeroMalloc(sizeof(ACCESS));
12342 	edit.Access->Active = true;
12343 	edit.Access->Priority = 0;
12344 	edit.Access->IsIPv6 = ipv6;
12345 	edit.Hub = s->Hub;
12346 
12347 	// Get the new priority
12348 	for (i = 0;i < LIST_NUM(s->AccessList);i++)
12349 	{
12350 		ACCESS *a = LIST_DATA(s->AccessList, i);
12351 		edit.Access->Priority = MAX(edit.Access->Priority, a->Priority);
12352 	}
12353 
12354 	if (edit.Access->Priority == 0)
12355 	{
12356 		edit.Access->Priority = 900;
12357 	}
12358 
12359 	edit.Access->Priority += 100;
12360 
12361 	if (edit.Access->IsIPv6 == false)
12362 	{
12363 		ret = Dialog(hWnd, D_SM_EDIT_ACCESS, SmEditAccessDlg, &edit);
12364 	}
12365 	else
12366 	{
12367 		ret = Dialog(hWnd, D_SM_EDIT_ACCESS_V6, SmEditAccessDlg, &edit);
12368 	}
12369 
12370 	if (ret)
12371 	{
12372 		Insert(s->AccessList, edit.Access);
12373 
12374 		// Reassign the ID
12375 		for (i = 0;i < LIST_NUM(s->AccessList);i++)
12376 		{
12377 			ACCESS *a = LIST_DATA(s->AccessList, i);
12378 			a->Id = (i + 1);
12379 		}
12380 	}
12381 	else
12382 	{
12383 		Free(edit.Access);
12384 	}
12385 
12386 	return ret;
12387 }
12388 
12389 // Initialize
SmAccessListInit(HWND hWnd,SM_ACCESS_LIST * s)12390 void SmAccessListInit(HWND hWnd, SM_ACCESS_LIST *s)
12391 {
12392 	// Validate arguments
12393 	if (hWnd == NULL || s == NULL)
12394 	{
12395 		return;
12396 	}
12397 
12398 	SetIcon(hWnd, 0, ICO_PASS);
12399 	FormatText(hWnd, S_TITLE, s->Hub->HubName);
12400 
12401 	LvInit(hWnd, L_ACCESS_LIST);
12402 	LvInsertColumn(hWnd, L_ACCESS_LIST, 0, _UU("SM_ACCESS_COLUMN_0"), 60);
12403 	LvInsertColumn(hWnd, L_ACCESS_LIST, 1, _UU("SM_ACCESS_COLUMN_1"), 60);
12404 	LvInsertColumn(hWnd, L_ACCESS_LIST, 2, _UU("SM_ACCESS_COLUMN_2"), 60);
12405 	LvInsertColumn(hWnd, L_ACCESS_LIST, 3, _UU("SM_ACCESS_COLUMN_3"), 70);
12406 	LvInsertColumn(hWnd, L_ACCESS_LIST, 4, _UU("SM_ACCESS_COLUMN_4"), 150);
12407 	LvInsertColumn(hWnd, L_ACCESS_LIST, 5, _UU("SM_ACCESS_COLUMN_5"), 600);
12408 
12409 	LvSetStyle(hWnd, L_ACCESS_LIST, LVS_EX_GRIDLINES);
12410 
12411 	SetEnable(hWnd, B_ADD_V6, GetCapsBool(s->Hub->p->CapsList, "b_support_ipv6_acl"));
12412 
12413 	SmAccessListRefresh(hWnd, s);
12414 }
12415 
12416 // Update the control
SmAccessListUpdate(HWND hWnd,SM_ACCESS_LIST * s)12417 void SmAccessListUpdate(HWND hWnd, SM_ACCESS_LIST *s)
12418 {
12419 	bool ok = true;
12420 	UINT max_access_lists = 0;
12421 	// Validate arguments
12422 	if (hWnd == NULL || s == NULL)
12423 	{
12424 		return;
12425 	}
12426 
12427 	if (LvIsSelected(hWnd, L_ACCESS_LIST) == false || LvIsMultiMasked(hWnd, L_ACCESS_LIST))
12428 	{
12429 		ok = false;
12430 	}
12431 
12432 	SetEnable(hWnd, IDOK, ok);
12433 	SetEnable(hWnd, B_DELETE, ok);
12434 	SetEnable(hWnd, B_CLONE, ok);
12435 
12436 	if (ok == false)
12437 	{
12438 		SetEnable(hWnd, B_ENABLE, false);
12439 		SetEnable(hWnd, B_DISABLE, false);
12440 	}
12441 	else
12442 	{
12443 		ACCESS *a = LvGetParam(hWnd, L_ACCESS_LIST, LvGetSelected(hWnd, L_ACCESS_LIST));
12444 
12445 		if (a != NULL)
12446 		{
12447 			SetEnable(hWnd, B_ENABLE, (a->Active == false));
12448 			SetEnable(hWnd, B_DISABLE, (a->Active == true));
12449 		}
12450 		else
12451 		{
12452 			SetEnable(hWnd, B_ENABLE, false);
12453 			SetEnable(hWnd, B_DISABLE, false);
12454 		}
12455 	}
12456 
12457 	max_access_lists = GetCapsInt(s->Hub->p->CapsList, "i_max_access_lists");
12458 
12459 	SetEnable(hWnd, B_CREATE, LIST_NUM(s->AccessList) < max_access_lists);
12460 }
12461 
12462 // Content update
SmAccessListRefresh(HWND hWnd,SM_ACCESS_LIST * s)12463 void SmAccessListRefresh(HWND hWnd, SM_ACCESS_LIST *s)
12464 {
12465 	LVB *b;
12466 	UINT i;
12467 	// Validate arguments
12468 	if (hWnd == NULL || s == NULL)
12469 	{
12470 		return;
12471 	}
12472 
12473 	b = LvInsertStart();
12474 
12475 	Sort(s->AccessList);
12476 
12477 	for (i = 0;i < LIST_NUM(s->AccessList);i++)
12478 	{
12479 		ACCESS *a = LIST_DATA(s->AccessList, i);
12480 		char tmp[MAX_SIZE];
12481 		UINT ico = ICO_PASS;
12482 		wchar_t tmp3[MAX_SIZE];
12483 		wchar_t tmp1[MAX_SIZE];
12484 		wchar_t tmp2[MAX_SIZE];
12485 		GetAccessListStr(tmp, sizeof(tmp), a);
12486 		UniToStru(tmp1, a->Priority);
12487 		StrToUni(tmp2, sizeof(tmp2), tmp);
12488 
12489 		if (a->Discard == false && a->Active == false)
12490 		{
12491 			ico = ICO_PASS_DISABLE;
12492 		}
12493 		else if (a->Discard == false && a->Active)
12494 		{
12495 			ico = ICO_PASS;
12496 		}
12497 		else if (a->Discard && a->Active == false)
12498 		{
12499 			ico = ICO_DISCARD_DISABLE;
12500 		}
12501 		else
12502 		{
12503 			ico = ICO_DISCARD;
12504 		}
12505 
12506 		UniToStru(tmp3, a->Id);
12507 
12508 		LvInsertAdd(b, ico, (void *)a, 6,
12509 			tmp3,
12510 			a->Discard ? _UU("SM_ACCESS_DISCARD") : _UU("SM_ACCESS_PASS"),
12511 			a->Active ? _UU("SM_ACCESS_ENABLE") : _UU("SM_ACCESS_DISABLE"),
12512 			tmp1,
12513 			a->Note,
12514 			tmp2);
12515 	}
12516 
12517 	LvInsertEnd(b, hWnd, L_ACCESS_LIST);
12518 	LvSortEx(hWnd, L_ACCESS_LIST, 0, false, true);
12519 
12520 	SmAccessListUpdate(hWnd, s);
12521 }
12522 
12523 // Access List dialog procedure
SmAccessListProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)12524 UINT SmAccessListProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
12525 {
12526 	SM_ACCESS_LIST *s = (SM_ACCESS_LIST *)param;
12527 	NMHDR *n;
12528 	ACCESS *a;
12529 	// Validate arguments
12530 	if (hWnd == NULL)
12531 	{
12532 		return 0;
12533 	}
12534 
12535 	switch (msg)
12536 	{
12537 	case WM_INITDIALOG:
12538 		// Initialize
12539 		SmAccessListInit(hWnd, s);
12540 		break;
12541 
12542 	case WM_COMMAND:
12543 		switch (wParam)
12544 		{
12545 		case B_ADD:
12546 			// Add (IPv4)
12547 			if (SmAddAccess(hWnd, s, false))
12548 			{
12549 				SmAccessListRefresh(hWnd, s);
12550 			}
12551 			break;
12552 
12553 		case B_ADD_V6:
12554 			// Add (IPv6)
12555 			if (SmAddAccess(hWnd, s, true))
12556 			{
12557 				SmAccessListRefresh(hWnd, s);
12558 			}
12559 			break;
12560 
12561 		case IDOK:
12562 			// Edit
12563 			a = LvGetParam(hWnd, L_ACCESS_LIST, LvGetSelected(hWnd, L_ACCESS_LIST));
12564 			if (a != NULL)
12565 			{
12566 				if (SmEditAccess(hWnd, s, a))
12567 				{
12568 					SmAccessListRefresh(hWnd, s);
12569 				}
12570 			}
12571 			break;
12572 
12573 		case B_CLONE:
12574 			// Create by cloning
12575 			a = LvGetParam(hWnd, L_ACCESS_LIST, LvGetSelected(hWnd, L_ACCESS_LIST));
12576 			if (a != NULL)
12577 			{
12578 				if (SmCloneAccess(hWnd, s, a))
12579 				{
12580 					SmAccessListRefresh(hWnd, s);
12581 				}
12582 			}
12583 			break;
12584 
12585 		case B_ENABLE:
12586 			a = LvGetParam(hWnd, L_ACCESS_LIST, LvGetSelected(hWnd, L_ACCESS_LIST));
12587 			if (a != NULL)
12588 			{
12589 				a->Active = true;
12590 				SmAccessListRefresh(hWnd, s);
12591 			}
12592 			break;
12593 
12594 		case B_DISABLE:
12595 			a = LvGetParam(hWnd, L_ACCESS_LIST, LvGetSelected(hWnd, L_ACCESS_LIST));
12596 			if (a != NULL)
12597 			{
12598 				a->Active = false;
12599 				SmAccessListRefresh(hWnd, s);
12600 			}
12601 			break;
12602 
12603 		case B_DELETE:
12604 			// Delete
12605 			a = LvGetParam(hWnd, L_ACCESS_LIST, LvGetSelected(hWnd, L_ACCESS_LIST));
12606 			if (a != NULL)
12607 			{
12608 				UINT i;
12609 				if (IsInList(s->AccessList, a))
12610 				{
12611 					Delete(s->AccessList, a);
12612 					Free(a);
12613 					// Reassign the ID
12614 					for (i = 0;i < LIST_NUM(s->AccessList);i++)
12615 					{
12616 						ACCESS *a = LIST_DATA(s->AccessList, i);
12617 						a->Id = (i + 1);
12618 					}
12619 					SmAccessListRefresh(hWnd, s);
12620 				}
12621 			}
12622 			break;
12623 
12624 		case B_SAVE:
12625 			// Save
12626 			{
12627 				UINT i;
12628 				bool ok;
12629 				// Save the access list
12630 				RPC_ENUM_ACCESS_LIST t;
12631 				Zero(&t, sizeof(t));
12632 				StrCpy(t.HubName, sizeof(t.HubName), s->Hub->HubName);
12633 				t.NumAccess = LIST_NUM(s->AccessList);
12634 				t.Accesses = ZeroMalloc(sizeof(ACCESS) * t.NumAccess);
12635 				for (i = 0;i < LIST_NUM(s->AccessList);i++)
12636 				{
12637 					ACCESS *access = LIST_DATA(s->AccessList, i);
12638 					Copy(&t.Accesses[i], access, sizeof(ACCESS));
12639 				}
12640 
12641 				ok = CALL(hWnd, ScSetAccessList(s->Rpc, &t));
12642 				FreeRpcEnumAccessList(&t);
12643 				if (ok)
12644 				{
12645 					EndDialog(hWnd, true);
12646 				}
12647 			}
12648 			break;
12649 
12650 		case IDCANCEL:
12651 			// Cancel button
12652 			Close(hWnd);
12653 			break;
12654 		}
12655 		break;
12656 
12657 	case WM_NOTIFY:
12658 		n = (NMHDR *)lParam;
12659 		switch (n->idFrom)
12660 		{
12661 		case L_ACCESS_LIST:
12662 			switch (n->code)
12663 			{
12664 			case LVN_ITEMCHANGED:
12665 				SmAccessListUpdate(hWnd, s);
12666 				break;
12667 			}
12668 			break;
12669 		}
12670 		break;
12671 
12672 	case WM_CLOSE:
12673 		EndDialog(hWnd, false);
12674 		break;
12675 	}
12676 
12677 	LvStandardHandler(hWnd, msg, wParam, lParam, L_ACCESS_LIST);
12678 
12679 	return 0;
12680 }
12681 
12682 
12683 // Access List dialog
SmAccessListDlg(HWND hWnd,SM_HUB * s)12684 void SmAccessListDlg(HWND hWnd, SM_HUB *s)
12685 {
12686 	SM_ACCESS_LIST a;
12687 	UINT i;
12688 	RPC_ENUM_ACCESS_LIST t;
12689 	bool ret;
12690 	// Validate arguments
12691 	if (hWnd == NULL || s == NULL)
12692 	{
12693 		return;
12694 	}
12695 
12696 	Zero(&a, sizeof(a));
12697 	a.Hub = s;
12698 	a.Rpc = s->Rpc;
12699 
12700 	// Get the access list
12701 	Zero(&t, sizeof(t));
12702 	StrCpy(t.HubName, sizeof(t.HubName), s->HubName);
12703 	if (CALL(hWnd, ScEnumAccess(s->Rpc, &t)) == false)
12704 	{
12705 		return;
12706 	}
12707 
12708 	a.AccessList = NewListFast(CmpAccessList);
12709 	// Add to the list
12710 	for (i = 0;i < t.NumAccess;i++)
12711 	{
12712 		ACCESS *access = ZeroMalloc(sizeof(ACCESS));
12713 		Copy(access, &t.Accesses[i], sizeof(ACCESS));
12714 
12715 		Add(a.AccessList, access);
12716 	}
12717 
12718 	// Sort
12719 	Sort(a.AccessList);
12720 	FreeRpcEnumAccessList(&t);
12721 
12722 	// Show the dialog
12723 	ret = Dialog(hWnd, D_SM_ACCESS_LIST, SmAccessListProc, &a);
12724 
12725 	for (i = 0;i < LIST_NUM(a.AccessList);i++)
12726 	{
12727 		ACCESS *access = LIST_DATA(a.AccessList, i);
12728 		Free(access);
12729 	}
12730 	ReleaseList(a.AccessList);
12731 }
12732 
12733 // Initialize
SmEditGroupDlgInit(HWND hWnd,SM_EDIT_GROUP * g)12734 void SmEditGroupDlgInit(HWND hWnd, SM_EDIT_GROUP *g)
12735 {
12736 	RPC_SET_GROUP *group;
12737 	LVB *b;
12738 	// Validate arguments
12739 	if (hWnd == NULL || g == NULL)
12740 	{
12741 		return;
12742 	}
12743 
12744 	SetIcon(hWnd, 0, ICO_GROUP);
12745 
12746 	group = &g->SetGroup;
12747 
12748 	if (g->EditMode == false)
12749 	{
12750 		SetText(hWnd, 0, _UU("SM_EDIT_GROUP_CAPTION_1"));
12751 	}
12752 	else
12753 	{
12754 		wchar_t tmp[MAX_SIZE];
12755 		UniFormat(tmp, sizeof(tmp), _UU("SM_EDIT_GROUP_CAPTION_2"), group->Name);
12756 		SetText(hWnd, 0, tmp);
12757 	}
12758 
12759 	SetTextA(hWnd, E_GROUPNAME, group->Name);
12760 	SetText(hWnd, E_REALNAME, group->Realname);
12761 	SetText(hWnd, E_NOTE, group->Note);
12762 
12763 	g->Inited = true;
12764 
12765 	if (g->EditMode == false)
12766 	{
12767 		Disable(hWnd, L_STATUS);
12768 	}
12769 	else
12770 	{
12771 		LvInit(hWnd, L_STATUS);
12772 		LvInsertColumn(hWnd, L_STATUS, 0, _UU("SM_STATUS_COLUMN_1"), 0);
12773 		LvInsertColumn(hWnd, L_STATUS, 1, _UU("SM_STATUS_COLUMN_2"), 0);
12774 		LvSetStyle(hWnd, L_STATUS, LVS_EX_GRIDLINES);
12775 
12776 		b = LvInsertStart();
12777 
12778 		SmInsertTrafficInfo(b, &group->Traffic);
12779 
12780 		LvInsertEnd(b, hWnd, L_STATUS);
12781 
12782 		LvAutoSize(hWnd, L_STATUS);
12783 	}
12784 
12785 	Check(hWnd, R_POLICY, group->Policy != NULL);
12786 
12787 	if (g->EditMode)
12788 	{
12789 		Disable(hWnd, E_GROUPNAME);
12790 		FocusEx(hWnd, E_REALNAME);
12791 	}
12792 
12793 	SmEditGroupDlgUpdate(hWnd, g);
12794 }
12795 
12796 // Update
SmEditGroupDlgUpdate(HWND hWnd,SM_EDIT_GROUP * g)12797 void SmEditGroupDlgUpdate(HWND hWnd, SM_EDIT_GROUP *g)
12798 {
12799 	bool ok = true;
12800 	RPC_SET_GROUP *group;
12801 	// Validate arguments
12802 	if (hWnd == NULL || g == NULL)
12803 	{
12804 		return;
12805 	}
12806 
12807 	if (g->Inited == false)
12808 	{
12809 		return;
12810 	}
12811 
12812 	group = &g->SetGroup;
12813 
12814 	GetTxtA(hWnd, E_GROUPNAME, group->Name, sizeof(group->Name));
12815 	Trim(group->Name);
12816 
12817 	if (IsUserName(group->Name) == false)
12818 	{
12819 		ok = false;
12820 	}
12821 
12822 	GetTxt(hWnd, E_REALNAME, group->Realname, sizeof(group->Realname));
12823 	UniTrim(group->Realname);
12824 
12825 	GetTxt(hWnd, E_NOTE, group->Note, sizeof(group->Note));
12826 	UniTrim(group->Note);
12827 
12828 	SetEnable(hWnd, B_POLICY, IsChecked(hWnd, R_POLICY));
12829 
12830 	if (IsChecked(hWnd, R_POLICY))
12831 	{
12832 		if (group->Policy == NULL)
12833 		{
12834 			ok = false;
12835 		}
12836 	}
12837 
12838 	SetEnable(hWnd, IDOK, ok);
12839 }
12840 
12841 // OK
SmEditGroupDlgOnOk(HWND hWnd,SM_EDIT_GROUP * g)12842 void SmEditGroupDlgOnOk(HWND hWnd, SM_EDIT_GROUP *g)
12843 {
12844 	RPC_SET_GROUP *group;
12845 	RPC_SET_GROUP t;
12846 	// Validate arguments
12847 	if (hWnd == NULL || g == NULL)
12848 	{
12849 		return;
12850 	}
12851 
12852 	SmEditGroupDlgUpdate(hWnd, g);
12853 
12854 	group = &g->SetGroup;
12855 
12856 	if (IsChecked(hWnd, R_POLICY) == false)
12857 	{
12858 		if (group->Policy != NULL)
12859 		{
12860 			Free(group->Policy);
12861 			group->Policy = NULL;
12862 		}
12863 	}
12864 
12865 	Zero(&t, sizeof(t));
12866 	Copy(&t, group, sizeof(RPC_SET_GROUP));
12867 
12868 	t.Policy = ClonePolicy(group->Policy);
12869 
12870 	if (g->EditMode == false)
12871 	{
12872 		if (CALL(hWnd, ScCreateGroup(g->Rpc, &t)) == false)
12873 		{
12874 			FocusEx(hWnd, E_GROUPNAME);
12875 			return;
12876 		}
12877 	}
12878 	else
12879 	{
12880 		if (CALL(hWnd, ScSetGroup(g->Rpc, &t)) == false)
12881 		{
12882 			return;
12883 		}
12884 	}
12885 
12886 	FreeRpcSetGroup(&t);
12887 
12888 	if (g->EditMode == false)
12889 	{
12890 		MsgBoxEx(hWnd, MB_ICONINFORMATION, _UU("SM_GROUP_CREATED"), group->Name);
12891 	}
12892 
12893 	EndDialog(hWnd, true);
12894 }
12895 
12896 // Group editing dialog procedure
SmEditGroupDlgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)12897 UINT SmEditGroupDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
12898 {
12899 	SM_EDIT_GROUP *g = (SM_EDIT_GROUP *)param;
12900 	wchar_t tmp[MAX_SIZE];
12901 	NMHDR *n;
12902 	// Validate arguments
12903 	if (hWnd == NULL)
12904 	{
12905 		return 0;
12906 	}
12907 
12908 	switch (msg)
12909 	{
12910 	case WM_INITDIALOG:
12911 		// Initialize
12912 		SmEditGroupDlgInit(hWnd, g);
12913 		break;
12914 
12915 	case WM_COMMAND:
12916 		switch (LOWORD(wParam))
12917 		{
12918 		case E_GROUPNAME:
12919 		case E_REALNAME:
12920 		case E_NOTE:
12921 		case R_POLICY:
12922 			SmEditGroupDlgUpdate(hWnd, g);
12923 			break;
12924 		}
12925 
12926 		switch (wParam)
12927 		{
12928 		case IDOK:
12929 			// [OK] button
12930 			SmEditGroupDlgOnOk(hWnd, g);
12931 			break;
12932 
12933 		case IDCANCEL:
12934 			// Cancel button
12935 			Close(hWnd);
12936 			break;
12937 
12938 		case R_POLICY:
12939 			if (IsChecked(hWnd, R_POLICY))
12940 			{
12941 				Focus(hWnd, B_POLICY);
12942 			}
12943 			break;
12944 
12945 		case B_POLICY:
12946 			// Security policy
12947 			UniFormat(tmp, sizeof(tmp), _UU("SM_GROUP_POLICY_CAPTION"), g->SetGroup.Name);
12948 			if (g->SetGroup.Policy == NULL)
12949 			{
12950 				POLICY *p = ClonePolicy(GetDefaultPolicy());
12951 				if (SmPolicyDlgEx2(hWnd, p, tmp, false, g->p->PolicyVer))
12952 				{
12953 					g->SetGroup.Policy = p;
12954 					SmEditGroupDlgUpdate(hWnd, g);
12955 				}
12956 				else
12957 				{
12958 					Free(p);
12959 				}
12960 			}
12961 			else
12962 			{
12963 				SmPolicyDlgEx2(hWnd, g->SetGroup.Policy, tmp, false, g->p->PolicyVer);
12964 			}
12965 			break;
12966 		}
12967 		break;
12968 
12969 	case WM_NOTIFY:
12970 		n = (NMHDR *)lParam;
12971 		switch (n->idFrom)
12972 		{
12973 		case L_STATUS:
12974 			switch (n->code)
12975 			{
12976 			case LVN_ITEMCHANGED:
12977 				SmEditGroupDlgUpdate(hWnd, g);
12978 				break;
12979 			}
12980 			break;
12981 		}
12982 		break;
12983 
12984 	case WM_CLOSE:
12985 		EndDialog(hWnd, false);
12986 		break;
12987 	}
12988 
12989 	return 0;
12990 }
12991 
12992 // Group editing dialog
SmEditGroupDlg(HWND hWnd,SM_GROUP * s,char * name)12993 bool SmEditGroupDlg(HWND hWnd, SM_GROUP *s, char *name)
12994 {
12995 	SM_EDIT_GROUP g;
12996 	RPC_SET_GROUP *group;
12997 	bool ret;
12998 	// Validate arguments
12999 	if (hWnd == NULL || s == NULL)
13000 	{
13001 		return false;
13002 	}
13003 
13004 	Zero(&g, sizeof(g));
13005 	g.EditMode = true;
13006 	g.Hub = s->Hub;
13007 	g.p = s->p;
13008 	g.Rpc = s->Rpc;
13009 
13010 	group = &g.SetGroup;
13011 
13012 	StrCpy(group->Name, sizeof(group->Name), name);
13013 	StrCpy(group->HubName, sizeof(group->HubName), s->Hub->HubName);
13014 
13015 	if (CALL(hWnd, ScGetGroup(s->Rpc, group)) == false)
13016 	{
13017 		return false;
13018 	}
13019 
13020 	ret = Dialog(hWnd, D_SM_EDIT_GROUP, SmEditGroupDlgProc, &g);
13021 
13022 	FreeRpcSetGroup(group);
13023 
13024 	return ret;
13025 }
13026 
13027 // Group creation dialog
SmCreateGroupDlg(HWND hWnd,SM_GROUP * s)13028 bool SmCreateGroupDlg(HWND hWnd, SM_GROUP *s)
13029 {
13030 	SM_EDIT_GROUP g;
13031 	RPC_SET_GROUP *group;
13032 	bool ret;
13033 	// Validate arguments
13034 	if (hWnd == NULL || s == NULL)
13035 	{
13036 		return false;
13037 	}
13038 
13039 	Zero(&g, sizeof(g));
13040 	g.EditMode = false;
13041 	g.Hub = s->Hub;
13042 	g.p = s->p;
13043 	g.Rpc = s->Rpc;
13044 
13045 	group = &g.SetGroup;
13046 
13047 	StrCpy(group->HubName, sizeof(group->HubName), s->Hub->HubName);
13048 
13049 	ret = Dialog(hWnd, D_SM_EDIT_GROUP, SmEditGroupDlgProc, &g);
13050 
13051 	FreeRpcSetGroup(group);
13052 
13053 	return ret;
13054 }
13055 
13056 // Initialize
SmGroupListDlgInit(HWND hWnd,SM_GROUP * s)13057 void SmGroupListDlgInit(HWND hWnd, SM_GROUP *s)
13058 {
13059 	if (hWnd == NULL || s == NULL)
13060 	{
13061 		return;
13062 	}
13063 
13064 	SetIcon(hWnd, 0, ICO_GROUP);
13065 
13066 	// Initialize the column
13067 	LvInit(hWnd, L_GROUP);
13068 	LvInsertColumn(hWnd, L_GROUP, 0, _UU("SM_GROUPLIST_NAME"), 130);
13069 	LvInsertColumn(hWnd, L_GROUP, 1, _UU("SM_GROUPLIST_REALNAME"), 130);
13070 	LvInsertColumn(hWnd, L_GROUP, 2, _UU("SM_GROUPLIST_NOTE"), 170);
13071 	LvInsertColumn(hWnd, L_GROUP, 3, _UU("SM_GROUPLIST_NUMUSERS"), 80);
13072 	LvSetStyle(hWnd, L_GROUP, LVS_EX_GRIDLINES);
13073 
13074 	FormatText(hWnd, S_TITLE, s->Hub->HubName);
13075 
13076 	SmGroupListDlgRefresh(hWnd, s);
13077 
13078 	if (s->SelectMode)
13079 	{
13080 		SetStyle(hWnd, L_GROUP, LVS_SINGLESEL);
13081 	}
13082 
13083 	if (s->SelectMode)
13084 	{
13085 		wchar_t tmp[MAX_SIZE];
13086 		SetText(hWnd, IDOK, _UU("SM_SELECT_GROUP"));
13087 
13088 		if (s->SelectedGroupName != NULL)
13089 		{
13090 			UINT i;
13091 			StrToUni(tmp, sizeof(tmp), s->SelectedGroupName);
13092 			i = LvSearchStr(hWnd, L_GROUP, 0, tmp);
13093 			if (i != INFINITE)
13094 			{
13095 				LvSelect(hWnd, L_GROUP, i);
13096 			}
13097 		}
13098 	}
13099 }
13100 
13101 // Update the controls
SmGroupListDlgUpdate(HWND hWnd,SM_GROUP * s)13102 void SmGroupListDlgUpdate(HWND hWnd, SM_GROUP *s)
13103 {
13104 	bool ok = true;
13105 	if (hWnd == NULL || s == NULL)
13106 	{
13107 		return;
13108 	}
13109 
13110 	if (LvIsSelected(hWnd, L_GROUP) == false || LvIsMultiMasked(hWnd, L_GROUP))
13111 	{
13112 		ok = false;
13113 	}
13114 
13115 	SetEnable(hWnd, IDOK, ok);
13116 	SetEnable(hWnd, B_USER, ok);
13117 	SetEnable(hWnd, B_STATUS, ok);
13118 
13119 	if (s->SelectMode == false)
13120 	{
13121 		SetEnable(hWnd, B_DELETE, ok);
13122 	}
13123 	else
13124 	{
13125 		SetEnable(hWnd, B_DELETE, false);
13126 		SetEnable(hWnd, B_USER, false);
13127 		SetText(hWnd, IDCANCEL, _UU("SM_SELECT_NO_GROUP"));
13128 	}
13129 }
13130 
13131 // Content update
SmGroupListDlgRefresh(HWND hWnd,SM_GROUP * s)13132 void SmGroupListDlgRefresh(HWND hWnd, SM_GROUP *s)
13133 {
13134 	RPC_ENUM_GROUP t;
13135 	UINT i;
13136 	LVB *b;
13137 	if (hWnd == NULL || s == NULL)
13138 	{
13139 		return;
13140 	}
13141 
13142 	Zero(&t, sizeof(t));
13143 	StrCpy(t.HubName, sizeof(t.HubName), s->Hub->HubName);
13144 
13145 	if (CALL(hWnd, ScEnumGroup(s->Rpc, &t)) == false)
13146 	{
13147 		return;
13148 	}
13149 
13150 	b = LvInsertStart();
13151 
13152 	for (i = 0;i < t.NumGroup;i++)
13153 	{
13154 		wchar_t tmp1[MAX_SIZE];
13155 		wchar_t tmp2[MAX_SIZE];
13156 		RPC_ENUM_GROUP_ITEM *e = &t.Groups[i];
13157 
13158 		StrToUni(tmp1, sizeof(tmp1), e->Name);
13159 		UniToStru(tmp2, e->NumUsers);
13160 
13161 		LvInsertAdd(b, e->DenyAccess == false ? ICO_GROUP : ICO_GROUP_DENY,
13162 			NULL, 4, tmp1, e->Realname, e->Note, tmp2);
13163 	}
13164 
13165 	LvInsertEnd(b, hWnd, L_GROUP);
13166 
13167 	SmGroupListDlgUpdate(hWnd, s);
13168 
13169 	FreeRpcEnumGroup(&t);
13170 }
13171 
13172 // Group List dialog procedure
SmGroupListDlgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)13173 UINT SmGroupListDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
13174 {
13175 	SM_GROUP *s = (SM_GROUP *)param;
13176 	NMHDR *n;
13177 	wchar_t *tmp;
13178 	// Validate arguments
13179 	if (hWnd == NULL)
13180 	{
13181 		return 0;
13182 	}
13183 
13184 	switch (msg)
13185 	{
13186 	case WM_INITDIALOG:
13187 		// Initialize
13188 		SmGroupListDlgInit(hWnd, s);
13189 		break;
13190 
13191 	case WM_COMMAND:
13192 		switch (wParam)
13193 		{
13194 		case B_CREATE:
13195 			// Create new
13196 			if (SmCreateGroupDlg(hWnd, s))
13197 			{
13198 				SmGroupListDlgRefresh(hWnd, s);
13199 			}
13200 			break;
13201 
13202 		case IDOK:
13203 			// Edit
13204 			tmp = LvGetSelectedStr(hWnd, L_GROUP, 0);
13205 			if (tmp != NULL)
13206 			{
13207 				char name[MAX_SIZE];
13208 				UniToStr(name, sizeof(name), tmp);
13209 
13210 				if (s->SelectMode == false)
13211 				{
13212 					if (SmEditGroupDlg(hWnd, s, name))
13213 					{
13214 						SmGroupListDlgRefresh(hWnd, s);
13215 					}
13216 				}
13217 				else
13218 				{
13219 					s->SelectedGroupName = CopyStr(name);
13220 					EndDialog(hWnd, true);
13221 				}
13222 				Free(tmp);
13223 			}
13224 			break;
13225 
13226 		case B_DELETE:
13227 			// Delete
13228 			tmp = LvGetSelectedStr(hWnd, L_GROUP, 0);
13229 			if (tmp != NULL)
13230 			{
13231 				char name[MAX_SIZE];
13232 				RPC_DELETE_USER t;
13233 				UniToStr(name, sizeof(name), tmp);
13234 
13235 				if (MsgBoxEx(hWnd, MB_ICONQUESTION | MB_YESNO | MB_DEFBUTTON2,
13236 					_UU("SM_GROUP_DELETE_MSG"), name) == IDYES)
13237 				{
13238 					Zero(&t, sizeof(t));
13239 					StrCpy(t.HubName, sizeof(t.HubName), s->Hub->HubName);
13240 					StrCpy(t.Name, sizeof(t.Name), name);
13241 
13242 					if (CALL(hWnd, ScDeleteGroup(s->Rpc, &t)))
13243 					{
13244 						SmGroupListDlgRefresh(hWnd, s);
13245 					}
13246 				}
13247 
13248 				Free(tmp);
13249 			}
13250 			break;
13251 
13252 		case B_USER:
13253 			// Member List
13254 			tmp = LvGetSelectedStr(hWnd, L_GROUP, 0);
13255 			if (tmp != NULL)
13256 			{
13257 				char name[MAX_SIZE];
13258 				UniToStr(name, sizeof(name), tmp);
13259 				SmUserListDlgEx(hWnd, s->Hub, name, false);
13260 				Free(tmp);
13261 			}
13262 			break;
13263 
13264 		case B_REFRESH:
13265 			// Update to the latest information
13266 			SmGroupListDlgRefresh(hWnd, s);
13267 			break;
13268 
13269 		case IDCANCEL:
13270 			// Cancel button
13271 			Close(hWnd);
13272 			break;
13273 		}
13274 		break;
13275 
13276 	case WM_NOTIFY:
13277 		n = (NMHDR *)lParam;
13278 		switch (n->idFrom)
13279 		{
13280 		case L_GROUP:
13281 			switch (n->code)
13282 			{
13283 			case LVN_ITEMCHANGED:
13284 				SmGroupListDlgUpdate(hWnd, s);
13285 				break;
13286 			}
13287 			break;
13288 		}
13289 		break;
13290 
13291 	case WM_CLOSE:
13292 		EndDialog(hWnd, false);
13293 		break;
13294 	}
13295 
13296 	LvStandardHandler(hWnd, msg, wParam, lParam, L_GROUP);
13297 
13298 	return 0;
13299 }
13300 
13301 // Group List dialog (selection mode)
SmSelectGroupDlg(HWND hWnd,SM_HUB * s,char * default_name)13302 char *SmSelectGroupDlg(HWND hWnd, SM_HUB *s, char *default_name)
13303 {
13304 	SM_GROUP g;
13305 	// Validate arguments
13306 	if (hWnd == NULL || s == NULL)
13307 	{
13308 		return NULL;
13309 	}
13310 
13311 	Zero(&g, sizeof(g));
13312 	g.Hub = s;
13313 	g.p = s->p;
13314 	g.Rpc = s->Rpc;
13315 	g.SelectMode = true;
13316 	g.SelectedGroupName = default_name;
13317 
13318 	if (Dialog(hWnd, D_SM_GROUP, SmGroupListDlgProc, &g) == false)
13319 	{
13320 		return NULL;
13321 	}
13322 
13323 	return g.SelectedGroupName;
13324 }
13325 
13326 // Group List dialog
SmGroupListDlg(HWND hWnd,SM_HUB * s)13327 void SmGroupListDlg(HWND hWnd, SM_HUB *s)
13328 {
13329 	SM_GROUP g;
13330 	// Validate arguments
13331 	if (hWnd == NULL || s == NULL)
13332 	{
13333 		return;
13334 	}
13335 
13336 	Zero(&g, sizeof(g));
13337 	g.Hub = s;
13338 	g.p = s->p;
13339 	g.Rpc = s->Rpc;
13340 	g.SelectMode = false;
13341 
13342 	Dialog(hWnd, D_SM_GROUP, SmGroupListDlgProc, &g);
13343 }
13344 
13345 // Update the user information
SmRefreshUserInfo(HWND hWnd,SM_SERVER * s,void * param)13346 bool SmRefreshUserInfo(HWND hWnd, SM_SERVER *s, void *param)
13347 {
13348 	RPC_SET_USER t;
13349 	SM_USER_INFO *p = (SM_USER_INFO *)param;
13350 	LVB *b;
13351 	wchar_t tmp[MAX_SIZE];
13352 	char *username;
13353 
13354 	// Validate arguments
13355 	if (hWnd == NULL || s == NULL || param == NULL)
13356 	{
13357 		return false;
13358 	}
13359 
13360 	username = p->Username;
13361 
13362 	Zero(&t, sizeof(t));
13363 	StrCpy(t.HubName, sizeof(t.HubName), p->Hub->HubName);
13364 	StrCpy(t.Name, sizeof(t.Name), username);
13365 
13366 	if (CALL(hWnd, ScGetUser(s->Rpc, &t)) == false)
13367 	{
13368 		return false;
13369 	}
13370 
13371 	b = LvInsertStart();
13372 
13373 	StrToUni(tmp, sizeof(tmp), t.Name);
13374 	LvInsertAdd(b, ICO_USER, NULL, 2, _UU("SM_USERINFO_NAME"), tmp);
13375 
13376 	if (StrLen(t.GroupName) != 0)
13377 	{
13378 		StrToUni(tmp, sizeof(tmp), t.GroupName);
13379 		LvInsertAdd(b, ICO_GROUP, NULL, 2, _UU("SM_USERINFO_GROUP"), tmp);
13380 	}
13381 
13382 	GetDateTimeStrEx64(tmp, sizeof(tmp), SystemToLocal64(t.CreatedTime), NULL);
13383 	LvInsertAdd(b, ICO_USER_ADMIN, NULL, 2, _UU("SM_USERINFO_CREATE"), tmp);
13384 
13385 	GetDateTimeStrEx64(tmp, sizeof(tmp), SystemToLocal64(t.UpdatedTime), NULL);
13386 	LvInsertAdd(b, ICO_USER_ADMIN, NULL, 2, _UU("SM_USERINFO_UPDATE"), tmp);
13387 
13388 	if (t.ExpireTime != 0)
13389 	{
13390 		GetDateTimeStrEx64(tmp, sizeof(tmp), SystemToLocal64(t.ExpireTime), NULL);
13391 		LvInsertAdd(b, ICO_WARNING, NULL, 2, _UU("SM_USERINFO_EXPIRE"), tmp);
13392 	}
13393 
13394 	SmInsertTrafficInfo(b, &t.Traffic);
13395 
13396 	UniToStru(tmp, t.NumLogin);
13397 	LvInsertAdd(b, ICO_LINK, NULL, 2, _UU("SM_USERINFO_NUMLOGIN"), tmp);
13398 
13399 	LvInsertEnd(b, hWnd, L_STATUS);
13400 
13401 	FreeRpcSetUser(&t);
13402 
13403 	return true;
13404 }
13405 
13406 // Initialize
SmPolicyDlgInit(HWND hWnd,SM_POLICY * s)13407 void SmPolicyDlgInit(HWND hWnd, SM_POLICY *s)
13408 {
13409 	CM_POLICY cp;
13410 	// Validate arguments
13411 	if (hWnd == NULL || s == NULL)
13412 	{
13413 		return;
13414 	}
13415 
13416 	SetIcon(hWnd, 0, ICO_MACHINE);
13417 	SetText(hWnd, 0, s->Caption);
13418 	SetText(hWnd, S_TITLE, s->Caption);
13419 	DlgFont(hWnd, S_BOLD, 10, true);
13420 	DlgFont(hWnd, S_BOLD2, 10, true);
13421 
13422 	DlgFont(hWnd, S_POLICY_TITLE, 11, false);
13423 	DlgFont(hWnd, E_POLICY_DESCRIPTION, 10, false);
13424 
13425 	Zero(&cp, sizeof(cp));
13426 	cp.Policy = s->Policy;
13427 	cp.Extension = true;
13428 
13429 	LvInit(hWnd, L_POLICY);
13430 	LvInsertColumn(hWnd, L_POLICY, 0, _UU("POL_TITLE_STR"), 250);
13431 	LvInsertColumn(hWnd, L_POLICY, 1, _UU("POL_VALUE_STR"), 150);
13432 	LvSetStyle(hWnd, L_POLICY, LVS_EX_GRIDLINES);
13433 
13434 	CmPolicyDlgPrintEx2(hWnd, &cp, s->CascadeMode, s->Ver);
13435 
13436 	LvSelect(hWnd, L_POLICY, 0);
13437 
13438 	s->Inited = true;
13439 	SmPolicyDlgUpdate(hWnd, s);
13440 }
13441 
13442 // Update
SmPolicyDlgUpdate(HWND hWnd,SM_POLICY * s)13443 void SmPolicyDlgUpdate(HWND hWnd, SM_POLICY *s)
13444 {
13445 	bool ok = true;
13446 	bool value_changed = false;
13447 	UINT i;
13448 	// Validate arguments
13449 	if (hWnd == NULL || s == NULL)
13450 	{
13451 		return;
13452 	}
13453 
13454 	if (s->Inited == false)
13455 	{
13456 		return;
13457 	}
13458 
13459 	i = LvGetSelected(hWnd, L_POLICY);
13460 	if (i != INFINITE)
13461 	{
13462 		i = (UINT)LvGetParam(hWnd, L_POLICY, i);
13463 	}
13464 	if (i == INFINITE || i >= NUM_POLICY_ITEM)
13465 	{
13466 		SetText(hWnd, S_POLICY_TITLE, _UU("SM_POLICY_INIT_TITLE"));
13467 		SetText(hWnd, E_POLICY_DESCRIPTION, L"");
13468 		Disable(hWnd, S_POLICY_TITLE);
13469 		Disable(hWnd, S_BOLD);
13470 		Hide(hWnd, S_BOLD2);
13471 		Hide(hWnd, R_ENABLE);
13472 		Hide(hWnd, R_DISABLE);
13473 		Hide(hWnd, R_DEFINE);
13474 		Hide(hWnd, E_VALUE);
13475 		Hide(hWnd, S_TANI);
13476 		Hide(hWnd, S_LIMIT);
13477 	}
13478 	else
13479 	{
13480 		POLICY_ITEM *item = &policy_item[i];
13481 		bool changed = false;
13482 		wchar_t *tmp = GetText(hWnd, S_POLICY_TITLE);
13483 		if (UniStrCmp(tmp, GetPolicyTitle(i)) != 0)
13484 		{
13485 			changed = true;
13486 		}
13487 		Free(tmp);
13488 		SetText(hWnd, S_POLICY_TITLE, GetPolicyTitle(i));
13489 		SetText(hWnd, E_POLICY_DESCRIPTION, GetPolicyDescription(i));
13490 		Enable(hWnd, S_POLICY_TITLE);
13491 		Enable(hWnd, S_BOLD);
13492 		Show(hWnd, S_BOLD2);
13493 
13494 		if (item->TypeInt == false)
13495 		{
13496 			Show(hWnd, R_ENABLE);
13497 			Show(hWnd, R_DISABLE);
13498 			Hide(hWnd, R_DEFINE);
13499 			Hide(hWnd, E_VALUE);
13500 			Hide(hWnd, S_TANI);
13501 			Hide(hWnd, S_LIMIT);
13502 
13503 			if (changed)
13504 			{
13505 				if (POLICY_BOOL(s->Policy, i))
13506 				{
13507 					Check(hWnd, R_ENABLE, true);
13508 					Check(hWnd, R_DISABLE, false);
13509 				}
13510 				else
13511 				{
13512 					Check(hWnd, R_ENABLE, false);
13513 					Check(hWnd, R_DISABLE, true);
13514 				}
13515 			}
13516 
13517 			if ((!(POLICY_BOOL(s->Policy, i))) != (!(IsChecked(hWnd, R_ENABLE))))
13518 			{
13519 				POLICY_BOOL(s->Policy, i) = IsChecked(hWnd, R_ENABLE);
13520 				value_changed = true;
13521 			}
13522 		}
13523 		else
13524 		{
13525 			wchar_t tmp[MAX_SIZE];
13526 			UINT value;
13527 			if (item->AllowZero)
13528 			{
13529 				if (changed)
13530 				{
13531 					Check(hWnd, R_DEFINE, POLICY_INT(s->Policy, i) != 0);
13532 					Enable(hWnd, R_DEFINE);
13533 					SetIntEx(hWnd, E_VALUE, POLICY_INT(s->Policy, i));
13534 				}
13535 
13536 				SetEnable(hWnd, E_VALUE, IsChecked(hWnd, R_DEFINE));
13537 				SetEnable(hWnd, S_TANI, IsChecked(hWnd, R_DEFINE));
13538 				SetEnable(hWnd, S_LIMIT, IsChecked(hWnd, R_DEFINE));
13539 			}
13540 			else
13541 			{
13542 				if (changed)
13543 				{
13544 					Check(hWnd, R_DEFINE, true);
13545 					Disable(hWnd, R_DEFINE);
13546 					SetInt(hWnd, E_VALUE, POLICY_INT(s->Policy, i));
13547 				}
13548 
13549 				SetEnable(hWnd, E_VALUE, IsChecked(hWnd, R_DEFINE));
13550 				SetEnable(hWnd, S_TANI, IsChecked(hWnd, R_DEFINE));
13551 				SetEnable(hWnd, S_LIMIT, IsChecked(hWnd, R_DEFINE));
13552 			}
13553 
13554 			UniReplaceStrEx(tmp, sizeof(tmp), _UU(policy_item[i].FormatStr),
13555 				L"%u ", L"", false);
13556 			UniReplaceStrEx(tmp, sizeof(tmp), tmp,
13557 				L"%u", L"", false);
13558 
13559 			SetText(hWnd, S_TANI, tmp);
13560 
13561 			UniFormat(tmp, sizeof(tmp), _UU("SM_LIMIT_STR"), policy_item[i].MinValue, policy_item[i].MaxValue);
13562 			SetText(hWnd, S_LIMIT, tmp);
13563 
13564 			Hide(hWnd, R_ENABLE);
13565 			Hide(hWnd, R_DISABLE);
13566 			Show(hWnd, E_VALUE);
13567 			Show(hWnd, R_DEFINE);
13568 			Show(hWnd, S_TANI);
13569 			Show(hWnd, S_LIMIT);
13570 
13571 			value = GetInt(hWnd, E_VALUE);
13572 
13573 			if (item->AllowZero && (IsChecked(hWnd, R_DEFINE) == false))
13574 			{
13575 				value = 0;
13576 			}
13577 			else
13578 			{
13579 				if (value < policy_item[i].MinValue || value > policy_item[i].MaxValue)
13580 				{
13581 					ok = false;
13582 				}
13583 			}
13584 
13585 			if (ok)
13586 			{
13587 				if (POLICY_INT(s->Policy, i) != value)
13588 				{
13589 					POLICY_INT(s->Policy, i) = value;
13590 					value_changed = true;
13591 				}
13592 			}
13593 		}
13594 	}
13595 
13596 	SetEnable(hWnd, IDOK, ok);
13597 	SetEnable(hWnd, L_POLICY, ok);
13598 
13599 	if (value_changed)
13600 	{
13601 		CM_POLICY cp;
13602 		Zero(&cp, sizeof(cp));
13603 		cp.Policy = s->Policy;
13604 		cp.Extension = true;
13605 
13606 		CmPolicyDlgPrintEx(hWnd, &cp, s->CascadeMode);
13607 	}
13608 }
13609 
13610 // Confirmation
SmPolicyDlgOk(HWND hWnd,SM_POLICY * s)13611 void SmPolicyDlgOk(HWND hWnd, SM_POLICY *s)
13612 {
13613 	// Validate arguments
13614 	if (hWnd == NULL || s == NULL)
13615 	{
13616 		return;
13617 	}
13618 
13619 	EndDialog(hWnd, true);
13620 }
13621 
13622 // Policy dialog box procedure
SmPolicyDlgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)13623 UINT SmPolicyDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
13624 {
13625 	SM_POLICY *s = (SM_POLICY *)param;
13626 	NMHDR *n;
13627 	// Validate arguments
13628 	if (hWnd == NULL)
13629 	{
13630 		return 0;
13631 	}
13632 
13633 	switch (msg)
13634 	{
13635 	case WM_INITDIALOG:
13636 		// Initialize
13637 		SmPolicyDlgInit(hWnd, s);
13638 		break;
13639 
13640 	case WM_COMMAND:
13641 		switch (LOWORD(wParam))
13642 		{
13643 		case R_DEFINE:
13644 		case R_ENABLE:
13645 		case R_DISABLE:
13646 		case E_VALUE:
13647 			SmPolicyDlgUpdate(hWnd, s);
13648 			break;
13649 		}
13650 
13651 		switch (wParam)
13652 		{
13653 		case IDOK:
13654 			// [OK] button
13655 			SmPolicyDlgOk(hWnd, s);
13656 			break;
13657 
13658 		case IDCANCEL:
13659 			// Cancel button
13660 			Close(hWnd);
13661 			break;
13662 
13663 		case R_DEFINE:
13664 			if (IsChecked(hWnd, R_DEFINE))
13665 			{
13666 				FocusEx(hWnd, E_VALUE);
13667 			}
13668 			break;
13669 		}
13670 		break;
13671 
13672 	case WM_NOTIFY:
13673 		n = (NMHDR *)lParam;
13674 		switch (n->idFrom)
13675 		{
13676 		case L_POLICY:
13677 			switch (n->code)
13678 			{
13679 			case LVN_ITEMCHANGED:
13680 				SmPolicyDlgUpdate(hWnd, s);
13681 				break;
13682 			}
13683 			break;
13684 		}
13685 		break;
13686 
13687 	case WM_CLOSE:
13688 		EndDialog(hWnd, false);
13689 		break;
13690 	}
13691 
13692 	return 0;
13693 }
13694 
13695 // Show Policies dialog box
SmPolicyDlg(HWND hWnd,POLICY * p,wchar_t * caption)13696 bool SmPolicyDlg(HWND hWnd, POLICY *p, wchar_t *caption)
13697 {
13698 	return SmPolicyDlgEx(hWnd, p, caption, false);
13699 }
SmPolicyDlgEx(HWND hWnd,POLICY * p,wchar_t * caption,bool cascade_mode)13700 bool SmPolicyDlgEx(HWND hWnd, POLICY *p, wchar_t *caption, bool cascade_mode)
13701 {
13702 	return SmPolicyDlgEx2(hWnd, p, caption, cascade_mode, POLICY_CURRENT_VERSION);
13703 }
SmPolicyDlgEx2(HWND hWnd,POLICY * p,wchar_t * caption,bool cascade_mode,UINT ver)13704 bool SmPolicyDlgEx2(HWND hWnd, POLICY *p, wchar_t *caption, bool cascade_mode, UINT ver)
13705 {
13706 	SM_POLICY s;
13707 	bool ret;
13708 	// Validate arguments
13709 	if (hWnd == NULL || p == NULL)
13710 	{
13711 		return false;
13712 	}
13713 
13714 	if (caption == NULL)
13715 	{
13716 		caption = _UU("SM_POLICY_DEF_CAPTION");
13717 	}
13718 
13719 	Zero(&s, sizeof(s));
13720 	s.Caption = caption;
13721 	s.Policy = ClonePolicy(p);
13722 	s.CascadeMode = cascade_mode;
13723 	s.Ver = ver;
13724 
13725 	ret = Dialog(hWnd, D_SM_POLICY, SmPolicyDlgProc, &s);
13726 
13727 	if (ret)
13728 	{
13729 		Copy(p, s.Policy, sizeof(POLICY));
13730 	}
13731 
13732 	Free(s.Policy);
13733 
13734 	return ret;
13735 }
13736 
13737 // Edit user confirmed
SmEditUserDlgOk(HWND hWnd,SM_EDIT_USER * s)13738 void SmEditUserDlgOk(HWND hWnd, SM_EDIT_USER *s)
13739 {
13740 	RPC_SET_USER t;
13741 	RPC_SET_USER *u;
13742 	// Validate arguments
13743 	if (hWnd == NULL || s == NULL)
13744 	{
13745 		return;
13746 	}
13747 
13748 	SmEditUserDlgUpdate(hWnd, s);
13749 
13750 	Zero(&t, sizeof(t));
13751 	u = &s->SetUser;
13752 
13753 	StrCpy(t.HubName, sizeof(t.HubName), u->HubName);
13754 	StrCpy(t.Name, sizeof(t.Name), u->Name);
13755 	StrCpy(t.GroupName, sizeof(t.GroupName), u->GroupName);
13756 	UniStrCpy(t.Realname, sizeof(t.Realname), u->Realname);
13757 	UniStrCpy(t.Note, sizeof(t.Note), u->Note);
13758 	t.ExpireTime = u->ExpireTime;
13759 	t.AuthType = u->AuthType;
13760 	t.AuthData = CopyAuthData(u->AuthData, t.AuthType);
13761 
13762 	if (IsChecked(hWnd, R_POLICY))
13763 	{
13764 		t.Policy = ClonePolicy(u->Policy);
13765 	}
13766 	else
13767 	{
13768 		t.Policy = NULL;
13769 	}
13770 
13771 	if (s->EditMode == false)
13772 	{
13773 		if (CALL(hWnd, ScCreateUser(s->Rpc, &t)) == false)
13774 		{
13775 			FocusEx(hWnd, E_USERNAME);
13776 			return;
13777 		}
13778 		FreeRpcSetUser(&t);
13779 
13780 		MsgBoxEx(hWnd, MB_ICONINFORMATION, _UU("SM_USER_CREEATE_OK"), u->Name);
13781 	}
13782 	else
13783 	{
13784 		if (CALL(hWnd, ScSetUser(s->Rpc, &t)) == false)
13785 		{
13786 			FocusEx(hWnd, E_REALNAME);
13787 			return;
13788 		}
13789 		FreeRpcSetUser(&t);
13790 	}
13791 
13792 	EndDialog(hWnd, true);
13793 }
13794 
13795 // Edit user initialization
SmEditUserDlgInit(HWND hWnd,SM_EDIT_USER * s)13796 void SmEditUserDlgInit(HWND hWnd, SM_EDIT_USER *s)
13797 {
13798 	RPC_SET_USER *u;
13799 	wchar_t tmp[MAX_SIZE];
13800 	UINT i;
13801 	UINT icons[6] = {ICO_PASS, ICO_KEY, ICO_CERT, ICO_SERVER_CERT,
13802 		ICO_TOWER, ICO_LINK};
13803 	RECT rect;
13804 
13805 	// Validate arguments
13806 	if (hWnd == NULL || s == NULL)
13807 	{
13808 		return;
13809 	}
13810 
13811 	SetIcon(hWnd, 0, ICO_USER);
13812 
13813 	u = &s->SetUser;
13814 
13815 	// Initialize the column
13816 	LvInit(hWnd, L_AUTH);
13817 	LvSetStyle(hWnd, L_AUTH, LVS_EX_GRIDLINES);
13818 
13819 	GetClientRect(DlgItem(hWnd, L_AUTH), &rect);
13820 	LvInsertColumn(hWnd, L_AUTH, 0, L"Name", rect.right - rect.left);
13821 
13822 	for (i = 0;i < 6;i++)
13823 	{
13824 		LvInsert(hWnd, L_AUTH, icons[i], (void *)i, 1, SmGetAuthTypeStr(i));
13825 	}
13826 
13827 	// User name, etc.
13828 	SetTextA(hWnd, E_USERNAME, u->Name);
13829 	SetText(hWnd, E_REALNAME, u->Realname);
13830 	SetText(hWnd, E_NOTE, u->Note);
13831 
13832 
13833 	// Expiration date
13834 	if (u->ExpireTime == 0)
13835 	{
13836 		SYSTEMTIME st;
13837 		Check(hWnd, R_EXPIRES, false);
13838 		GetLocalTime(&st);
13839 		UINT64ToSystem(&st, SystemToUINT64(&st) + (60 * 60 * 24 * 1000));
13840 		st.wHour = st.wMinute = st.wSecond = st.wMilliseconds = 0;
13841 		DateTime_SetSystemtime(DlgItem(hWnd, E_EXPIRES_DATE), GDT_VALID, &st);
13842 		DateTime_SetSystemtime(DlgItem(hWnd, E_EXPIRES_TIME), GDT_VALID, &st);
13843 	}
13844 	else
13845 	{
13846 		SYSTEMTIME st;
13847 		UINT64ToSystem(&st, SystemToLocal64(u->ExpireTime));
13848 		Check(hWnd, R_EXPIRES, true);
13849 		DateTime_SetSystemtime(DlgItem(hWnd, E_EXPIRES_DATE), GDT_VALID, &st);
13850 		DateTime_SetSystemtime(DlgItem(hWnd, E_EXPIRES_TIME), GDT_VALID, &st);
13851 	}
13852 
13853 	if (GetCurrentOsLangId() == SE_LANG_JAPANESE || GetCurrentOsLangId() == SE_LANG_CHINESE_ZH)
13854 	{
13855 		SetStyle(hWnd, E_EXPIRES_DATE, DTS_LONGDATEFORMAT);
13856 	}
13857 	else
13858 	{
13859 		SetStyle(hWnd, E_EXPIRES_DATE, DTS_SHORTDATEFORMAT);
13860 	}
13861 
13862 	SetWindowLong(DlgItem(hWnd, E_EXPIRES_TIME), GWL_STYLE, WS_CHILDWINDOW | WS_VISIBLE | WS_TABSTOP | DTS_RIGHTALIGN | DTS_TIMEFORMAT | DTS_UPDOWN);
13863 
13864 	// Group name
13865 	SetTextA(hWnd, E_GROUP, u->GroupName);
13866 
13867 	// Authentication method
13868 	LvSelect(hWnd, L_AUTH, u->AuthType);
13869 
13870 	SetText(hWnd, S_CERT_INFO, _UU("SM_EDIT_USER_CERT_INFO"));
13871 
13872 	switch (u->AuthType)
13873 	{
13874 	case AUTHTYPE_PASSWORD:
13875 		if (s->EditMode)
13876 		{
13877 			SetTextA(hWnd, E_PASSWORD1, HIDDEN_PASSWORD);
13878 			SetTextA(hWnd, E_PASSWORD2, HIDDEN_PASSWORD);
13879 		}
13880 		break;
13881 
13882 	case AUTHTYPE_USERCERT:
13883 		SmGetCertInfoStr(tmp, sizeof(tmp), ((AUTHUSERCERT *)u->AuthData)->UserX);
13884 		break;
13885 
13886 	case AUTHTYPE_ROOTCERT:
13887 		if (u->AuthData != NULL)
13888 		{
13889 			AUTHROOTCERT *c = (AUTHROOTCERT *)u->AuthData;
13890 			if (c->CommonName != NULL && UniStrLen(c->CommonName) != 0)
13891 			{
13892 				Check(hWnd, R_CN, true);
13893 				SetText(hWnd, E_CN, c->CommonName);
13894 			}
13895 			else
13896 			{
13897 				Check(hWnd, R_CN, false);
13898 			}
13899 			if (c->Serial != NULL && c->Serial->size != 0)
13900 			{
13901 				X_SERIAL *s = c->Serial;
13902 				char *tmp;
13903 				UINT tmp_size = s->size * 3 + 1;
13904 				tmp = ZeroMalloc(tmp_size);
13905 				BinToStrEx(tmp, tmp_size, s->data, s->size);
13906 				SetTextA(hWnd, E_SERIAL, tmp);
13907 				Free(tmp);
13908 				Check(hWnd, R_SERIAL, true);
13909 			}
13910 			else
13911 			{
13912 				Check(hWnd, R_SERIAL, false);
13913 			}
13914 		}
13915 		break;
13916 
13917 	case AUTHTYPE_RADIUS:
13918 		if (u->AuthData != NULL)
13919 		{
13920 			AUTHRADIUS *r = (AUTHRADIUS *)u->AuthData;
13921 			if (UniStrLen(r->RadiusUsername) != 0)
13922 			{
13923 				Check(hWnd, R_SET_RADIUS_USERNAME, true);
13924 				SetText(hWnd, E_RADIUS_USERNAME, r->RadiusUsername);
13925 			}
13926 			else
13927 			{
13928 				Check(hWnd, R_SET_RADIUS_USERNAME, false);
13929 			}
13930 		}
13931 		break;
13932 
13933 	case AUTHTYPE_NT:
13934 		if (u->AuthData != NULL)
13935 		{
13936 			AUTHNT *n = (AUTHNT *)u->AuthData;
13937 			if (UniStrLen(n->NtUsername) != 0)
13938 			{
13939 				Check(hWnd, R_SET_RADIUS_USERNAME, true);
13940 				SetText(hWnd, E_RADIUS_USERNAME, n->NtUsername);
13941 			}
13942 			else
13943 			{
13944 				Check(hWnd, R_SET_RADIUS_USERNAME, false);
13945 			}
13946 		}
13947 		break;
13948 	}
13949 
13950 	if (u->Policy != NULL)
13951 	{
13952 		Check(hWnd, R_POLICY, true);
13953 	}
13954 
13955 	s->Inited = true;
13956 
13957 	SmEditUserDlgUpdate(hWnd, s);
13958 
13959 	if (s->EditMode == false)
13960 	{
13961 		Focus(hWnd, E_USERNAME);
13962 		SetText(hWnd, 0, _UU("SM_EDIT_USER_CAPTION_1"));
13963 	}
13964 	else
13965 	{
13966 		wchar_t tmp[MAX_SIZE];
13967 		UniFormat(tmp, sizeof(tmp), _UU("SM_EDIT_USER_CAPTION_2"), s->SetUser.Name);
13968 		SetText(hWnd, 0, tmp);
13969 
13970 		Disable(hWnd, E_USERNAME);
13971 		FocusEx(hWnd, E_REALNAME);
13972 	}
13973 
13974 	SetShow(hWnd, S_HINT, (s->EditMode ? false : true));
13975 }
13976 
13977 // User edit control update
SmEditUserDlgUpdate(HWND hWnd,SM_EDIT_USER * s)13978 void SmEditUserDlgUpdate(HWND hWnd, SM_EDIT_USER *s)
13979 {
13980 	RPC_SET_USER *u;
13981 	bool ok = true;
13982 	UINT old_authtype;
13983 	char tmp1[MAX_SIZE];
13984 	char tmp2[MAX_SIZE];
13985 	bool authtype_changed = false;
13986 	// Validate arguments
13987 	if (hWnd == NULL || s == NULL)
13988 	{
13989 		return;
13990 	}
13991 
13992 	if (s->Inited == false)
13993 	{
13994 		return;
13995 	}
13996 
13997 	u = &s->SetUser;
13998 
13999 	// User name
14000 	GetTxtA(hWnd, E_USERNAME, u->Name, sizeof(u->Name));
14001 	Trim(u->Name);
14002 	if (StrLen(u->Name) == 0 || IsUserName(u->Name) == false)
14003 	{
14004 		ok = false;
14005 	}
14006 
14007 	// Real name
14008 	GetTxt(hWnd, E_REALNAME, u->Realname, sizeof(u->Realname));
14009 	UniTrim(u->Realname);
14010 
14011 	// Note
14012 	GetTxt(hWnd, E_NOTE, u->Note, sizeof(u->Note));
14013 	UniTrim(u->Realname);
14014 
14015 	// Group
14016 	GetTxtA(hWnd, E_GROUP, u->GroupName, sizeof(u->GroupName));
14017 	Trim(u->GroupName);
14018 
14019 	// Expiration date
14020 	if (IsChecked(hWnd, R_EXPIRES) == false)
14021 	{
14022 		u->ExpireTime = 0;
14023 		Disable(hWnd, E_EXPIRES_DATE);
14024 		Disable(hWnd, E_EXPIRES_TIME);
14025 	}
14026 	else
14027 	{
14028 		SYSTEMTIME st1, st2;
14029 		Enable(hWnd, E_EXPIRES_DATE);
14030 		Enable(hWnd, E_EXPIRES_TIME);
14031 		DateTime_GetSystemtime(DlgItem(hWnd, E_EXPIRES_DATE), &st1);
14032 		DateTime_GetSystemtime(DlgItem(hWnd, E_EXPIRES_TIME), &st2);
14033 		st1.wHour = st2.wHour;
14034 		st1.wMinute = st2.wMinute;
14035 		st1.wSecond = st2.wSecond;
14036 		st1.wMilliseconds = st2.wMilliseconds;
14037 		u->ExpireTime = LocalToSystem64(SystemToUINT64(&st1));
14038 	}
14039 
14040 	// Authentication method
14041 	old_authtype = u->AuthType;
14042 	u->AuthType = LvGetSelected(hWnd, L_AUTH);
14043 
14044 	if (StrCmpi(u->Name, "*") == 0)
14045 	{
14046 		if (u->AuthType != AUTHTYPE_RADIUS && u->AuthType != AUTHTYPE_NT)
14047 		{
14048 			ok = false;
14049 		}
14050 	}
14051 
14052 	if (u->AuthType == INFINITE)
14053 	{
14054 		ok = false;
14055 		u->AuthType = 0;
14056 	}
14057 	if (old_authtype != u->AuthType)
14058 	{
14059 		authtype_changed = true;
14060 	}
14061 
14062 	if (authtype_changed)
14063 	{
14064 		FreeAuthData(old_authtype, u->AuthData);
14065 		u->AuthData = NULL;
14066 		switch (u->AuthType)
14067 		{
14068 		case AUTHTYPE_ANONYMOUS:
14069 			u->AuthData = NULL;
14070 			break;
14071 
14072 		case AUTHTYPE_PASSWORD:
14073 			u->AuthData = NewPasswordAuthData("", "");
14074 			GetTxtA(hWnd, E_PASSWORD1, tmp1, sizeof(tmp1));
14075 			if (StrCmp(tmp1, HIDDEN_PASSWORD) == 0)
14076 			{
14077 				SetTextA(hWnd, E_PASSWORD1, "");
14078 			}
14079 			GetTxtA(hWnd, E_PASSWORD2, tmp2, sizeof(tmp2));
14080 			if (StrCmp(tmp2, HIDDEN_PASSWORD) == 0)
14081 			{
14082 				SetTextA(hWnd, E_PASSWORD2, "");
14083 			}
14084 			break;
14085 
14086 		case AUTHTYPE_USERCERT:
14087 			u->AuthData = NewUserCertAuthData(NULL);
14088 			SetText(hWnd, S_CERT_INFO, _UU("SM_EDIT_USER_CERT_INFO"));
14089 			break;
14090 
14091 		case AUTHTYPE_ROOTCERT:
14092 			u->AuthData = NewRootCertAuthData(NULL, NULL);
14093 			break;
14094 
14095 		case AUTHTYPE_NT:
14096 			u->AuthData = NewNTAuthData(L"");
14097 			break;
14098 
14099 		case AUTHTYPE_RADIUS:
14100 			u->AuthData = NewRadiusAuthData(L"");
14101 			break;
14102 		}
14103 	}
14104 
14105 	SetEnable(hWnd, S_RADIUS_3, (u->AuthType == AUTHTYPE_RADIUS) || (u->AuthType == AUTHTYPE_NT));
14106 	SetEnable(hWnd, R_SET_RADIUS_USERNAME, (u->AuthType == AUTHTYPE_RADIUS) || (u->AuthType == AUTHTYPE_NT));
14107 	SetEnable(hWnd, S_RADIUS_1, (u->AuthType == AUTHTYPE_RADIUS) || (u->AuthType == AUTHTYPE_NT));
14108 
14109 	if (StrCmp(u->Name, "*") == 0)
14110 	{
14111 		Check(hWnd, R_SET_RADIUS_USERNAME, false);
14112 		Disable(hWnd, R_SET_RADIUS_USERNAME);
14113 	}
14114 
14115 	if ((u->AuthType == AUTHTYPE_RADIUS) || (u->AuthType == AUTHTYPE_NT))
14116 	{
14117 		SetEnable(hWnd, E_RADIUS_USERNAME, IsChecked(hWnd, R_SET_RADIUS_USERNAME));
14118 		SetEnable(hWnd, S_RADIUS_2, IsChecked(hWnd, R_SET_RADIUS_USERNAME));
14119 	}
14120 	else
14121 	{
14122 		SetEnable(hWnd, E_RADIUS_USERNAME, false);
14123 		SetEnable(hWnd, S_RADIUS_2, false);
14124 	}
14125 
14126 	SetEnable(hWnd, S_PASSWORD_1, u->AuthType == AUTHTYPE_PASSWORD);
14127 	SetEnable(hWnd, S_PASSWORD_2, u->AuthType == AUTHTYPE_PASSWORD);
14128 	SetEnable(hWnd, S_PASSWORD_3, u->AuthType == AUTHTYPE_PASSWORD);
14129 	SetEnable(hWnd, E_PASSWORD1, u->AuthType == AUTHTYPE_PASSWORD);
14130 	SetEnable(hWnd, E_PASSWORD2, u->AuthType == AUTHTYPE_PASSWORD);
14131 
14132 	SetEnable(hWnd, S_USER_CERT_1, u->AuthType == AUTHTYPE_USERCERT);
14133 	SetEnable(hWnd, S_CERT_INFO, u->AuthType == AUTHTYPE_USERCERT);
14134 	SetEnable(hWnd, B_LOAD_CERT, u->AuthType == AUTHTYPE_USERCERT);
14135 
14136 	if (u->AuthType == AUTHTYPE_USERCERT)
14137 	{
14138 		SetEnable(hWnd, B_VIEW_CERT, ((AUTHUSERCERT *)u->AuthData)->UserX != NULL);
14139 	}
14140 	else
14141 	{
14142 		SetEnable(hWnd, B_VIEW_CERT, false);
14143 	}
14144 
14145 	SetEnable(hWnd, S_ROOT_CERT_1, u->AuthType == AUTHTYPE_ROOTCERT);
14146 	SetEnable(hWnd, S_ROOT_CERT_2, u->AuthType == AUTHTYPE_ROOTCERT);
14147 	SetEnable(hWnd, S_ROOT_CERT_3, u->AuthType == AUTHTYPE_ROOTCERT);
14148 	SetEnable(hWnd, R_CN, u->AuthType == AUTHTYPE_ROOTCERT);
14149 	SetEnable(hWnd, R_SERIAL, u->AuthType == AUTHTYPE_ROOTCERT);
14150 
14151 	if (u->AuthType == AUTHTYPE_ROOTCERT)
14152 	{
14153 		SetEnable(hWnd, E_CN, IsChecked(hWnd, R_CN));
14154 		SetEnable(hWnd, E_SERIAL, IsChecked(hWnd, R_SERIAL));
14155 	}
14156 	else
14157 	{
14158 		Disable(hWnd, E_CN);
14159 		Disable(hWnd, E_SERIAL);
14160 	}
14161 
14162 	switch (u->AuthType)
14163 	{
14164 	case AUTHTYPE_PASSWORD:
14165 		GetTxtA(hWnd, E_PASSWORD1, tmp1, sizeof(tmp1));
14166 		GetTxtA(hWnd, E_PASSWORD2, tmp2, sizeof(tmp2));
14167 		if (StrCmp(tmp1, tmp2) != 0)
14168 		{
14169 			ok = false;
14170 		}
14171 		else
14172 		{
14173 			if (StrCmp(tmp1, HIDDEN_PASSWORD) != 0)
14174 			{
14175 				HashPassword(((AUTHPASSWORD *)u->AuthData)->HashedKey, u->Name, tmp1);
14176 				GenerateNtPasswordHash(((AUTHPASSWORD *)u->AuthData)->NtLmSecureHash, tmp1);
14177 			}
14178 		}
14179 		break;
14180 
14181 	case AUTHTYPE_USERCERT:
14182 		if (((AUTHUSERCERT *)u->AuthData)->UserX == NULL)
14183 		{
14184 			ok = false;
14185 		}
14186 		break;
14187 
14188 	case AUTHTYPE_ROOTCERT:
14189 		Free(((AUTHROOTCERT *)u->AuthData)->CommonName);
14190 		((AUTHROOTCERT *)u->AuthData)->CommonName = NULL;
14191 		if (IsChecked(hWnd, R_CN) && (IsEmpty(hWnd, E_CN) == false))
14192 		{
14193 			((AUTHROOTCERT *)u->AuthData)->CommonName = GetText(hWnd, E_CN);
14194 			UniTrim(((AUTHROOTCERT *)u->AuthData)->CommonName);
14195 		}
14196 		if (IsChecked(hWnd, R_CN) && ((AUTHROOTCERT *)u->AuthData)->CommonName == NULL)
14197 		{
14198 			ok = false;
14199 		}
14200 		FreeXSerial(((AUTHROOTCERT *)u->AuthData)->Serial);
14201 		((AUTHROOTCERT *)u->AuthData)->Serial = NULL;
14202 		if (IsChecked(hWnd, R_SERIAL))
14203 		{
14204 			char *serial_str = GetTextA(hWnd, E_SERIAL);
14205 			if (serial_str != NULL)
14206 			{
14207 				BUF *b = StrToBin(serial_str);
14208 				if (b->Size >= 1)
14209 				{
14210 					((AUTHROOTCERT *)u->AuthData)->Serial = NewXSerial(b->Buf, b->Size);
14211 				}
14212 				FreeBuf(b);
14213 				Free(serial_str);
14214 			}
14215 		}
14216 		if (IsChecked(hWnd, R_SERIAL) && ((AUTHROOTCERT *)u->AuthData)->Serial == NULL)
14217 		{
14218 			ok = false;
14219 		}
14220 		break;
14221 
14222 	case AUTHTYPE_RADIUS:
14223 		Free(((AUTHRADIUS *)u->AuthData)->RadiusUsername);
14224 		((AUTHRADIUS *)u->AuthData)->RadiusUsername = NULL;
14225 		if (IsChecked(hWnd, R_SET_RADIUS_USERNAME) && (IsEmpty(hWnd, E_RADIUS_USERNAME) == false))
14226 		{
14227 			((AUTHRADIUS *)u->AuthData)->RadiusUsername = GetText(hWnd, E_RADIUS_USERNAME);
14228 		}
14229 		if (IsChecked(hWnd, R_SET_RADIUS_USERNAME) && ((AUTHRADIUS *)u->AuthData)->RadiusUsername == NULL)
14230 		{
14231 			ok = false;
14232 		}
14233 		break;
14234 
14235 	case AUTHTYPE_NT:
14236 		Free(((AUTHNT *)u->AuthData)->NtUsername);
14237 		((AUTHNT *)u->AuthData)->NtUsername = NULL;
14238 		if (IsChecked(hWnd, R_SET_RADIUS_USERNAME) && (IsEmpty(hWnd, E_RADIUS_USERNAME) == false))
14239 		{
14240 			((AUTHNT *)u->AuthData)->NtUsername = GetText(hWnd, E_RADIUS_USERNAME);
14241 		}
14242 		if (IsChecked(hWnd, R_SET_RADIUS_USERNAME) && ((AUTHNT *)u->AuthData)->NtUsername == NULL)
14243 		{
14244 			ok = false;
14245 		}
14246 		break;
14247 	}
14248 
14249 	SetEnable(hWnd, B_POLICY, IsChecked(hWnd, R_POLICY));
14250 	if (IsChecked(hWnd, R_POLICY))
14251 	{
14252 		if (u->Policy == NULL)
14253 		{
14254 			ok = false;
14255 		}
14256 	}
14257 
14258 	SetEnable(hWnd, IDOK, ok);
14259 }
14260 
14261 // Edit User dialog procedure
SmEditUserDlgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)14262 UINT SmEditUserDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
14263 {
14264 	SM_EDIT_USER *s = (SM_EDIT_USER *)param;
14265 	NMHDR *n;
14266 	POLICY *policy;
14267 	X *x = NULL;
14268 	wchar_t tmp[MAX_SIZE];
14269 	char name[MAX_SIZE];
14270 	char *ret;
14271 	// Validate arguments
14272 	if (hWnd == NULL)
14273 	{
14274 		return 0;
14275 	}
14276 
14277 	switch (msg)
14278 	{
14279 	case WM_INITDIALOG:
14280 		// Initialize
14281 		SmEditUserDlgInit(hWnd, s);
14282 		break;
14283 
14284 	case WM_COMMAND:
14285 		switch (LOWORD(wParam))
14286 		{
14287 		case E_USERNAME:
14288 		case E_REALNAME:
14289 		case E_NOTE:
14290 		case R_EXPIRES:
14291 		case E_EXPIRES_DATE:
14292 		case E_EXPIRES_TIME:
14293 		case E_GROUP:
14294 		case L_AUTH:
14295 		case R_SET_RADIUS_USERNAME:
14296 		case E_RADIUS_USERNAME:
14297 		case R_POLICY:
14298 		case E_PASSWORD1:
14299 		case E_PASSWORD2:
14300 		case R_CN:
14301 		case E_CN:
14302 		case R_SERIAL:
14303 		case E_SERIAL:
14304 			SmEditUserDlgUpdate(hWnd, s);
14305 			break;
14306 		}
14307 
14308 		switch (wParam)
14309 		{
14310 		case IDOK:
14311 			// [OK] button
14312 			SmEditUserDlgOk(hWnd, s);
14313 			break;
14314 
14315 		case IDCANCEL:
14316 			// Cancel button
14317 			Close(hWnd);
14318 			break;
14319 
14320 		case B_POLICY:
14321 			UniFormat(tmp, sizeof(tmp), _UU("SM_EDIT_USER_POL_DLG"), s->SetUser.Name);
14322 			// Policy
14323 			if (s->SetUser.Policy == NULL)
14324 			{
14325 				policy = ClonePolicy(GetDefaultPolicy());
14326 				if (SmPolicyDlgEx2(hWnd, policy, tmp, false, s->p->PolicyVer))
14327 				{
14328 					s->SetUser.Policy = policy;
14329 					SmEditUserDlgUpdate(hWnd, s);
14330 				}
14331 				else
14332 				{
14333 					Free(policy);
14334 				}
14335 			}
14336 			else
14337 			{
14338 				SmPolicyDlgEx2(hWnd, s->SetUser.Policy, tmp, false, s->p->PolicyVer);
14339 			}
14340 			break;
14341 
14342 		case B_GROUP:
14343 			// Browse for a Group
14344 			GetTxtA(hWnd, E_GROUP, name, sizeof(name));
14345 			Trim(name);
14346 			ret = SmSelectGroupDlg(hWnd, s->Hub, StrLen(name) == 0 ? NULL : name);
14347 			if (ret != NULL)
14348 			{
14349 				SetTextA(hWnd, E_GROUP, ret);
14350 				Free(ret);
14351 			}
14352 			else
14353 			{
14354 				SetTextA(hWnd, E_GROUP, "");
14355 			}
14356 			FocusEx(hWnd, E_GROUP);
14357 			break;
14358 
14359 		case B_LOAD_CERT:
14360 			// Specify the certificate
14361 			if (CmLoadXFromFileOrSecureCard(hWnd, &x))
14362 			{
14363 UPDATE_CERT:
14364 				if (s->SetUser.AuthType == AUTHTYPE_USERCERT)
14365 				{
14366 					wchar_t tmp[MAX_SIZE];
14367 					FreeX(((AUTHUSERCERT *)s->SetUser.AuthData)->UserX);
14368 					((AUTHUSERCERT *)s->SetUser.AuthData)->UserX = x;
14369 					SmGetCertInfoStr(tmp, sizeof(tmp), x);
14370 					SetText(hWnd, S_CERT_INFO, tmp);
14371 					SmEditUserDlgUpdate(hWnd, s);
14372 				}
14373 				else
14374 				{
14375 					if (x != NULL)
14376 					{
14377 						FreeX(x);
14378 						x = NULL;
14379 					}
14380 				}
14381 			}
14382 			break;
14383 
14384 		case B_VIEW_CERT:
14385 			// Show the certificate
14386 			if (s->SetUser.AuthType == AUTHTYPE_USERCERT)
14387 			{
14388 				CertDlg(hWnd, ((AUTHUSERCERT *)s->SetUser.AuthData)->UserX, NULL, true);
14389 			}
14390 			break;
14391 
14392 		case B_CREATE:
14393 			// Create
14394 			GetTxtA(hWnd, E_USERNAME, name, sizeof(name));
14395 			Trim(name);
14396 			if (SmCreateCert(hWnd, &x, NULL, false, name, false))
14397 			{
14398 				if (s->SetUser.AuthType != AUTHTYPE_USERCERT)
14399 				{
14400 					LvSelect(hWnd, L_AUTH, 2);
14401 				}
14402 				goto UPDATE_CERT;
14403 			}
14404 			break;
14405 
14406 		case R_SET_RADIUS_USERNAME:
14407 			if (IsChecked(hWnd, R_SET_RADIUS_USERNAME))
14408 			{
14409 				FocusEx(hWnd, E_RADIUS_USERNAME);
14410 			}
14411 			break;
14412 
14413 		case R_EXPIRES:
14414 			if (IsChecked(hWnd, R_EXPIRES))
14415 			{
14416 				Focus(hWnd, E_EXPIRES_DATE);
14417 			}
14418 			break;
14419 
14420 		case R_POLICY:
14421 			if (IsChecked(hWnd, R_POLICY))
14422 			{
14423 				Focus(hWnd, B_POLICY);
14424 			}
14425 			break;
14426 
14427 		case R_CN:
14428 			if (IsChecked(hWnd, R_CN))
14429 			{
14430 				Focus(hWnd, E_CN);
14431 			}
14432 			break;
14433 
14434 		case R_SERIAL:
14435 			if (IsChecked(hWnd, R_SERIAL))
14436 			{
14437 				Focus(hWnd, E_SERIAL);
14438 			}
14439 			break;
14440 		}
14441 		break;
14442 
14443 	case WM_NOTIFY:
14444 		n = (NMHDR *)lParam;
14445 		switch (n->idFrom)
14446 		{
14447 		case L_AUTH:
14448 			switch (n->code)
14449 			{
14450 			case LVN_ITEMCHANGED:
14451 				SmEditUserDlgUpdate(hWnd, s);
14452 				break;
14453 			}
14454 			break;
14455 		}
14456 		break;
14457 
14458 	case WM_CLOSE:
14459 		EndDialog(hWnd, false);
14460 		break;
14461 	}
14462 
14463 	return 0;
14464 }
14465 
14466 // User editing dialog
SmEditUserDlg(HWND hWnd,SM_HUB * s,char * username)14467 bool SmEditUserDlg(HWND hWnd, SM_HUB *s, char *username)
14468 {
14469 	SM_EDIT_USER e;
14470 	bool ret;
14471 	// Validate arguments
14472 	if (hWnd == NULL || s == NULL || username == NULL)
14473 	{
14474 		return false;
14475 	}
14476 
14477 	Zero(&e, sizeof(e));
14478 	e.p = s->p;
14479 	e.Rpc = s->Rpc;
14480 	e.Hub = s;
14481 
14482 	// Get the User
14483 	StrCpy(e.SetUser.HubName, sizeof(e.SetUser.HubName), e.Hub->HubName);
14484 	StrCpy(e.SetUser.Name, sizeof(e.SetUser.Name), username);
14485 
14486 	if (CALL(hWnd, ScGetUser(s->Rpc, &e.SetUser)) == false)
14487 	{
14488 		return false;
14489 	}
14490 
14491 	e.EditMode = true;
14492 
14493 	ret = Dialog(hWnd, D_SM_EDIT_USER, SmEditUserDlgProc, &e);
14494 
14495 	FreeRpcSetUser(&e.SetUser);
14496 
14497 	return ret;
14498 }
14499 
14500 // New user creation dialog
SmCreateUserDlg(HWND hWnd,SM_HUB * s)14501 bool SmCreateUserDlg(HWND hWnd, SM_HUB *s)
14502 {
14503 	SM_EDIT_USER e;
14504 	bool ret;
14505 	// Validate arguments
14506 	if (hWnd == NULL || s == NULL)
14507 	{
14508 		return false;
14509 	}
14510 
14511 	Zero(&e, sizeof(e));
14512 	e.EditMode = false;
14513 	e.p = s->p;
14514 	e.Rpc = s->Rpc;
14515 	e.Hub = s;
14516 
14517 	// Set up a new user
14518 	StrCpy(e.SetUser.HubName, sizeof(e.SetUser.HubName), e.Hub->HubName);
14519 	e.SetUser.AuthType = CLIENT_AUTHTYPE_PASSWORD;
14520 	e.SetUser.AuthData = NewPasswordAuthData("", "");
14521 
14522 	ret = Dialog(hWnd, D_SM_EDIT_USER, SmEditUserDlgProc, &e);
14523 
14524 	FreeRpcSetUser(&e.SetUser);
14525 
14526 	return ret;
14527 }
14528 
14529 // Get a string of user authentication method
SmGetAuthTypeStr(UINT id)14530 wchar_t *SmGetAuthTypeStr(UINT id)
14531 {
14532 	return GetAuthTypeStr(id);
14533 }
14534 
14535 // User list initialization
SmUserListInit(HWND hWnd,SM_USER * s)14536 void SmUserListInit(HWND hWnd, SM_USER *s)
14537 {
14538 	wchar_t tmp1[MAX_SIZE];
14539 	wchar_t tmp2[MAX_SIZE];
14540 	// Validate arguments
14541 	if (hWnd == NULL || s == NULL)
14542 	{
14543 		return;
14544 	}
14545 
14546 	SetIcon(hWnd, 0, ICO_USER);
14547 
14548 	// Initialize the column
14549 	LvInit(hWnd, L_USER);
14550 	LvSetStyle(hWnd, L_USER, LVS_EX_GRIDLINES);
14551 	LvInsertColumn(hWnd, L_USER, 0, _UU("SM_USER_COLUMN_1"), 120);
14552 	LvInsertColumn(hWnd, L_USER, 1, _UU("SM_USER_COLUMN_2"), 100);
14553 	LvInsertColumn(hWnd, L_USER, 2, _UU("SM_USER_COLUMN_3"), 100);
14554 	LvInsertColumn(hWnd, L_USER, 3, _UU("SM_USER_COLUMN_4"), 130);
14555 	LvInsertColumn(hWnd, L_USER, 4, _UU("SM_USER_COLUMN_5"), 100);
14556 	LvInsertColumn(hWnd, L_USER, 5, _UU("SM_USER_COLUMN_6"), 90);
14557 	LvInsertColumn(hWnd, L_USER, 6, _UU("SM_USER_COLUMN_7"), 120);
14558 	LvInsertColumn(hWnd, L_USER, 7, _UU("SM_LICENSE_COLUMN_5"), 120);
14559 	LvInsertColumn(hWnd, L_USER, 8, _UU("SM_SESS_COLUMN_6"), 100);
14560 	LvInsertColumn(hWnd, L_USER, 9, _UU("SM_SESS_COLUMN_7"), 100);
14561 
14562 	FormatText(hWnd, S_TITLE, s->Hub->HubName);
14563 
14564 	if (s->GroupName != NULL)
14565 	{
14566 		GetTxt(hWnd, 0, tmp1, sizeof(tmp1));
14567 		UniFormat(tmp2, sizeof(tmp2), _UU("SM_GROUP_MEMBER_STR"), s->GroupName);
14568 		UniStrCat(tmp1, sizeof(tmp1), tmp2);
14569 		SetText(hWnd, S_TITLE, tmp1);
14570 		Disable(hWnd, B_CREATE);
14571 	}
14572 
14573 	if (s->SelectMode)
14574 	{
14575 		SetStyle(hWnd, L_USER, LVS_SINGLESEL);
14576 	}
14577 
14578 	SmUserListRefresh(hWnd, s);
14579 
14580 	if (s->SelectMode)
14581 	{
14582 		wchar_t tmp[MAX_SIZE];
14583 		UINT i;
14584 		StrToUni(tmp, sizeof(tmp), s->SelectedName);
14585 		i = LvSearchStr(hWnd, L_USER, 0, tmp);
14586 		if (i != INFINITE)
14587 		{
14588 			LvSelect(hWnd, L_USER, i);
14589 		}
14590 
14591 		if (s->AllowGroup)
14592 		{
14593 			SetText(hWnd, B_DELETE, _UU("SM_SELECT_ALT_GROUP"));
14594 		}
14595 	}
14596 }
14597 
14598 // User list update
SmUserListRefresh(HWND hWnd,SM_USER * s)14599 void SmUserListRefresh(HWND hWnd, SM_USER *s)
14600 {
14601 	LVB *b;
14602 	RPC_ENUM_USER t;
14603 	UINT i;
14604 	// Validate arguments
14605 	if (hWnd == NULL || s == NULL)
14606 	{
14607 		return;
14608 	}
14609 
14610 	Zero(&t, sizeof(t));
14611 
14612 	StrCpy(t.HubName, sizeof(t.HubName), s->Hub->HubName);
14613 	if (CALL(hWnd, ScEnumUser(s->Rpc, &t)) == false)
14614 	{
14615 		EndDialog(hWnd, false);
14616 		return;
14617 	}
14618 
14619 	b = LvInsertStart();
14620 
14621 	for (i = 0;i < t.NumUser;i++)
14622 	{
14623 		RPC_ENUM_USER_ITEM *e = &t.Users[i];
14624 		wchar_t name[MAX_SIZE];
14625 		wchar_t group[MAX_SIZE];
14626 		wchar_t num[MAX_SIZE];
14627 		wchar_t time[MAX_SIZE];
14628 		wchar_t exp[MAX_SIZE];
14629 		wchar_t num1[64], num2[64];
14630 
14631 		if (s->GroupName != NULL)
14632 		{
14633 			if (StrCmpi(s->GroupName, e->GroupName) != 0)
14634 			{
14635 				continue;
14636 			}
14637 		}
14638 
14639 		StrToUni(name, sizeof(name), e->Name);
14640 
14641 		if (StrLen(e->GroupName) != 0)
14642 		{
14643 			StrToUni(group, sizeof(group), e->GroupName);
14644 		}
14645 		else
14646 		{
14647 			UniStrCpy(group, sizeof(group), _UU("SM_NO_GROUP"));
14648 		}
14649 
14650 		UniToStru(num, e->NumLogin);
14651 
14652 		GetDateTimeStrEx64(time, sizeof(time), SystemToLocal64(e->LastLoginTime), NULL);
14653 
14654 		if (e->IsExpiresFilled == false)
14655 		{
14656 			UniStrCpy(exp, sizeof(exp), _UU("CM_ST_NONE"));
14657 		}
14658 		else
14659 		{
14660 			if (e->Expires == 0)
14661 			{
14662 				UniStrCpy(exp, sizeof(exp), _UU("SM_LICENSE_NO_EXPIRES"));
14663 			}
14664 			else
14665 			{
14666 				GetDateTimeStrEx64(exp, sizeof(exp), SystemToLocal64(e->Expires), NULL);
14667 			}
14668 		}
14669 
14670 		if (e->IsTrafficFilled == false)
14671 		{
14672 			UniStrCpy(num1, sizeof(num1), _UU("CM_ST_NONE"));
14673 			UniStrCpy(num2, sizeof(num2), _UU("CM_ST_NONE"));
14674 		}
14675 		else
14676 		{
14677 			UniToStr3(num1, sizeof(num1),
14678 				e->Traffic.Recv.BroadcastBytes + e->Traffic.Recv.UnicastBytes +
14679 				e->Traffic.Send.BroadcastBytes + e->Traffic.Send.UnicastBytes);
14680 
14681 			UniToStr3(num2, sizeof(num2),
14682 				e->Traffic.Recv.BroadcastCount + e->Traffic.Recv.UnicastCount +
14683 				e->Traffic.Send.BroadcastBytes + e->Traffic.Send.UnicastCount);
14684 		}
14685 
14686 		LvInsertAdd(b, e->DenyAccess ? ICO_USER_DENY : ICO_USER, NULL, 10,
14687 			name, e->Realname, group, e->Note, SmGetAuthTypeStr(e->AuthType),
14688 			num, time, exp, num1, num2);
14689 	}
14690 
14691 	LvInsertEnd(b, hWnd, L_USER);
14692 
14693 	FreeRpcEnumUser(&t);
14694 
14695 	SmUserListUpdate(hWnd, s);
14696 }
14697 
14698 // User list control update
SmUserListUpdate(HWND hWnd,SM_USER * s)14699 void SmUserListUpdate(HWND hWnd, SM_USER *s)
14700 {
14701 	bool b = true;
14702 	// Validate arguments
14703 	if (hWnd == NULL || s == NULL)
14704 	{
14705 		return;
14706 	}
14707 
14708 	if (LvIsSelected(hWnd, L_USER) == false || LvIsMultiMasked(hWnd, L_USER))
14709 	{
14710 		b = false;
14711 	}
14712 
14713 	if (s->SelectMode)
14714 	{
14715 		SetText(hWnd, IDOK, _UU("SM_SELECT_USER"));
14716 		SetText(hWnd, IDCANCEL, _UU("SM_SELECT_NO"));
14717 		SetText(hWnd, S_TITLE, _UU("SM_PLEASE_SELECT"));
14718 	}
14719 
14720 	SetEnable(hWnd, IDOK, b);
14721 
14722 	SetEnable(hWnd, B_STATUS, b);
14723 	SetEnable(hWnd, B_DELETE, (b && s->SelectedName == false) || s->AllowGroup);
14724 	SetEnable(hWnd, B_CREATE, s->SelectedName == false);
14725 }
14726 
14727 // User List dialog procedure
SmUserListProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)14728 UINT SmUserListProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
14729 {
14730 	SM_USER *s = (SM_USER *)param;
14731 	NMHDR *n;
14732 	wchar_t *str;
14733 	// Validate arguments
14734 	if (hWnd == NULL)
14735 	{
14736 		return 0;
14737 	}
14738 
14739 	switch (msg)
14740 	{
14741 	case WM_INITDIALOG:
14742 		// Initialize
14743 		SmUserListInit(hWnd, s);
14744 
14745 		if (s->CreateNow)
14746 		{
14747 			// Create instantly
14748 			if (IsEnable(hWnd, B_CREATE))
14749 			{
14750 				Command(hWnd, B_CREATE);
14751 			}
14752 		}
14753 		break;
14754 
14755 	case WM_COMMAND:
14756 		switch (wParam)
14757 		{
14758 		case IDOK:
14759 			if (s->SelectMode == false)
14760 			{
14761 				// Property
14762 				str = LvGetSelectedStr(hWnd, L_USER, 0);
14763 				if (str != NULL)
14764 				{
14765 					char name[MAX_SIZE];
14766 					UniToStr(name, sizeof(name), str);
14767 
14768 					if (SmEditUserDlg(hWnd, s->Hub, name))
14769 					{
14770 						SmUserListRefresh(hWnd, s);
14771 					}
14772 
14773 					Free(str);
14774 				}
14775 			}
14776 			else
14777 			{
14778 				// The user has been chosen
14779 				str = LvGetSelectedStr(hWnd, L_USER, 0);
14780 				if (str != NULL)
14781 				{
14782 					char name[MAX_SIZE];
14783 					UniToStr(name, sizeof(name), str);
14784 
14785 					s->SelectedName = CopyStr(name);
14786 
14787 					EndDialog(hWnd, true);
14788 
14789 					Free(str);
14790 				}
14791 			}
14792 			break;
14793 
14794 		case B_CREATE:
14795 			// Create new
14796 			if (SmCreateUserDlg(hWnd, s->Hub))
14797 			{
14798 				SmUserListRefresh(hWnd, s);
14799 			}
14800 			break;
14801 
14802 		case B_DELETE:
14803 			if (s->AllowGroup)
14804 			{
14805 				// Group selection
14806 				EndDialog(hWnd, INFINITE);
14807 			}
14808 			else
14809 			{
14810 				// Delete
14811 				str = LvGetSelectedStr(hWnd, L_USER, 0);
14812 				if (str != NULL)
14813 				{
14814 					RPC_DELETE_USER t;
14815 					char name[MAX_SIZE];
14816 					UniToStr(name, sizeof(name), str);
14817 
14818 					Zero(&t, sizeof(t));
14819 					StrCpy(t.HubName, sizeof(t.HubName), s->Hub->HubName);
14820 					StrCpy(t.Name, sizeof(t.Name), name);
14821 
14822 					if (MsgBoxEx(hWnd, MB_YESNO | MB_DEFBUTTON2 | MB_ICONQUESTION,
14823 						_UU("SM_USER_DELETE_MSG"), str) == IDYES)
14824 					{
14825 						if (CALL(hWnd, ScDeleteUser(s->Rpc, &t)))
14826 						{
14827 							SmUserListRefresh(hWnd, s);
14828 						}
14829 					}
14830 
14831 					Free(str);
14832 				}
14833 			}
14834 			break;
14835 
14836 		case B_STATUS:
14837 			// Display the User Information
14838 			str = LvGetSelectedStr(hWnd, L_USER, 0);
14839 			if (str != NULL)
14840 			{
14841 				char name[MAX_SIZE];
14842 				wchar_t tmp[MAX_SIZE];
14843 				SM_USER_INFO info;
14844 				UniToStr(name, sizeof(name), str);
14845 
14846 				UniFormat(tmp, sizeof(tmp), _UU("SM_USERINFO_CAPTION"), name);
14847 
14848 				Zero(&info, sizeof(info));
14849 				info.p = s->p;
14850 				info.Rpc = s->Rpc;
14851 				info.Hub = s->Hub;
14852 				info.Username = name;
14853 
14854 				SmStatusDlg(hWnd, s->p, &info, false, true, tmp, ICO_USER, NULL, SmRefreshUserInfo);
14855 
14856 				Free(str);
14857 			}
14858 			break;
14859 			break;
14860 
14861 		case B_REFRESH:
14862 			// Update
14863 			SmUserListRefresh(hWnd, s);
14864 			break;
14865 
14866 		case IDCANCEL:
14867 			// Cancel button
14868 			Close(hWnd);
14869 			break;
14870 		}
14871 		break;
14872 
14873 	case WM_NOTIFY:
14874 		n = (NMHDR *)lParam;
14875 		switch (n->idFrom)
14876 		{
14877 		case L_USER:
14878 			switch (n->code)
14879 			{
14880 			case LVN_ITEMCHANGED:
14881 				// Update the control
14882 				SmUserListUpdate(hWnd, s);
14883 				break;
14884 			}
14885 			break;
14886 		}
14887 		break;
14888 
14889 	case WM_CLOSE:
14890 		EndDialog(hWnd, false);
14891 		break;
14892 	}
14893 
14894 	LvStandardHandler(hWnd, msg, wParam, lParam, L_USER);
14895 
14896 	return 0;
14897 }
14898 
14899 // User List dialog (selection)
SmSelectUserDlg(HWND hWnd,SM_HUB * s,char * default_name)14900 char *SmSelectUserDlg(HWND hWnd, SM_HUB *s, char *default_name)
14901 {
14902 	return SmSelectUserDlgEx(hWnd, s, default_name, false);
14903 }
SmSelectUserDlgEx(HWND hWnd,SM_HUB * s,char * default_name,bool allow_group)14904 char *SmSelectUserDlgEx(HWND hWnd, SM_HUB *s, char *default_name, bool allow_group)
14905 {
14906 	UINT ret;
14907 	SM_USER user;
14908 	// Validate arguments
14909 	if (hWnd == NULL || s == NULL)
14910 	{
14911 		return NULL;
14912 	}
14913 
14914 	Zero(&user, sizeof(user));
14915 	user.Hub = s;
14916 	user.p = s->p;
14917 	user.Rpc = s->Rpc;
14918 	user.GroupName = NULL;
14919 	user.SelectedName = default_name;
14920 	user.SelectMode = true;
14921 	user.AllowGroup = allow_group;
14922 
14923 	ret = Dialog(hWnd, D_SM_USER, SmUserListProc, &user);
14924 
14925 	if (ret == 0)
14926 	{
14927 		return NULL;
14928 	}
14929 	else if (ret == INFINITE)
14930 	{
14931 		// Select a Group
14932 		return SmSelectGroupDlg(hWnd, s, default_name);
14933 	}
14934 	else
14935 	{
14936 		return user.SelectedName;
14937 	}
14938 }
14939 
14940 // User List dialog (filtered by group name)
SmUserListDlgEx(HWND hWnd,SM_HUB * s,char * groupname,bool create)14941 void SmUserListDlgEx(HWND hWnd, SM_HUB *s, char *groupname, bool create)
14942 {
14943 	SM_USER user;
14944 	// Validate arguments
14945 	if (hWnd == NULL || s == NULL)
14946 	{
14947 		return;
14948 	}
14949 
14950 	Zero(&user, sizeof(user));
14951 	user.Hub = s;
14952 	user.p = s->p;
14953 	user.Rpc = s->Rpc;
14954 	user.GroupName = groupname;
14955 	user.CreateNow = create;
14956 
14957 	Dialog(hWnd, D_SM_USER, SmUserListProc, &user);
14958 }
14959 
14960 // User List dialog
SmUserListDlg(HWND hWnd,SM_HUB * s)14961 void SmUserListDlg(HWND hWnd, SM_HUB *s)
14962 {
14963 	SmUserListDlgEx(hWnd, s, NULL, false);
14964 }
14965 
14966 // Initialize
SmHubDlgInit(HWND hWnd,SM_HUB * s)14967 void SmHubDlgInit(HWND hWnd, SM_HUB *s)
14968 {
14969 	CAPSLIST *caps;
14970 	bool support_user, support_group, support_accesslist, support_cascade,
14971 		support_log, support_config_hub, support_secure_nat, support_config_radius;
14972 	// Validate arguments
14973 	if (hWnd == NULL || s == NULL)
14974 	{
14975 		return;
14976 	}
14977 
14978 	FormatText(hWnd, 0, s->HubName);
14979 	FormatText(hWnd, S_TITLE, s->HubName);
14980 	SetIcon(hWnd, 0, ICO_HUB);
14981 	DlgFont(hWnd, S_TITLE, 15, true);
14982 
14983 	LvInit(hWnd, L_STATUS);
14984 	LvSetStyle(hWnd, L_STATUS, LVS_EX_GRIDLINES);
14985 	LvInsertColumn(hWnd, L_STATUS, 0, _UU("SM_STATUS_COLUMN_1"), 0);
14986 	LvInsertColumn(hWnd, L_STATUS, 1, _UU("SM_STATUS_COLUMN_2"), 0);
14987 
14988 	caps = s->p->CapsList;
14989 
14990 	support_user = GetCapsInt(caps, "i_max_users_per_hub") == 0 ? false : true;
14991 	support_group = GetCapsInt(caps, "i_max_groups_per_hub") == 0 ? false : true;
14992 	support_accesslist = GetCapsInt(caps, "i_max_access_lists") == 0 ? false : true;
14993 	support_cascade = GetCapsBool(caps, "b_support_cascade");
14994 	support_log = GetCapsBool(caps, "b_support_config_log");
14995 	support_config_hub = GetCapsBool(caps, "b_support_config_hub");
14996 	support_secure_nat = GetCapsBool(caps, "b_support_securenat");
14997 	support_config_radius = GetCapsBool(caps, "b_support_radius");
14998 
14999 	SetEnable(hWnd, B_USER, support_user);
15000 	SetEnable(hWnd, S_USER, support_user);
15001 
15002 	SetEnable(hWnd, B_GROUP, support_group);
15003 	SetEnable(hWnd, S_GROUP, support_group);
15004 
15005 	SetEnable(hWnd, B_ACCESS, support_accesslist);
15006 	SetEnable(hWnd, S_ACCESS, support_accesslist);
15007 
15008 	SetEnable(hWnd, B_PROPERTY, s->p->ServerType != SERVER_TYPE_FARM_MEMBER);
15009 	SetEnable(hWnd, S_PROPERTY, s->p->ServerType != SERVER_TYPE_FARM_MEMBER);
15010 
15011 	SetEnable(hWnd, B_RADIUS, support_config_radius);
15012 	SetEnable(hWnd, S_RADIUS, support_config_radius);
15013 
15014 	SetEnable(hWnd, B_LINK, support_cascade);
15015 	SetEnable(hWnd, S_LINK, support_cascade);
15016 
15017 	SetEnable(hWnd, B_LOG, support_log);
15018 	SetEnable(hWnd, S_LOG, support_log);
15019 
15020 	SetEnable(hWnd, B_CA, support_config_hub);
15021 	SetEnable(hWnd, S_CA, support_config_hub);
15022 
15023 	SetEnable(hWnd, B_SNAT, support_secure_nat);
15024 	SetEnable(hWnd, S_SNAT, support_secure_nat);
15025 
15026 	SetEnable(hWnd, B_CRL, GetCapsBool(caps, "b_support_crl"));
15027 
15028 	SetEnable(hWnd, B_LOG_FILE, GetCapsBool(caps, "b_support_read_log"));
15029 
15030 	SmHubDlgRefresh(hWnd, s);
15031 }
15032 
15033 // Update the control
SmHubDlgUpdate(HWND hWnd,SM_HUB * s)15034 void SmHubDlgUpdate(HWND hWnd, SM_HUB *s)
15035 {
15036 	// Validate arguments
15037 	if (hWnd == NULL || s == NULL)
15038 	{
15039 		return;
15040 	}
15041 }
15042 
15043 // Content update
SmHubDlgRefresh(HWND hWnd,SM_HUB * s)15044 void SmHubDlgRefresh(HWND hWnd, SM_HUB *s)
15045 {
15046 	// Validate arguments
15047 	if (hWnd == NULL || s == NULL)
15048 	{
15049 		return;
15050 	}
15051 
15052 	SmRefreshHubStatus(hWnd, s->p, (void *)s->HubName);
15053 	LvAutoSize(hWnd, L_STATUS);
15054 
15055 	SmHubDlgUpdate(hWnd, s);
15056 }
15057 
15058 // HUB management dialog
SmHubDlgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)15059 UINT SmHubDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
15060 {
15061 	SM_HUB *s = (SM_HUB *)param;
15062 	// Validate arguments
15063 	if (hWnd == NULL)
15064 	{
15065 		return 0;
15066 	}
15067 
15068 	switch (msg)
15069 	{
15070 	case WM_INITDIALOG:
15071 		// Initialize
15072 		SmHubDlgInit(hWnd, s);
15073 		break;
15074 
15075 	case WM_COMMAND:
15076 		switch (wParam)
15077 		{
15078 		case B_USER:
15079 			// User
15080 			SmUserListDlg(hWnd, s);
15081 			SmHubDlgRefresh(hWnd, s);
15082 			break;
15083 
15084 		case B_GROUP:
15085 			// Group
15086 			SmGroupListDlg(hWnd, s);
15087 			SmHubDlgRefresh(hWnd, s);
15088 			break;
15089 
15090 		case B_ACCESS:
15091 			// Access list
15092 			SmAccessListDlg(hWnd, s);
15093 			SmHubDlgRefresh(hWnd, s);
15094 			break;
15095 
15096 		case B_PROPERTY:
15097 			// Property
15098 			if (SmEditHubDlg(hWnd, s->p, s->HubName))
15099 			{
15100 				SmHubDlgRefresh(hWnd, s);
15101 			}
15102 			break;
15103 
15104 		case B_RADIUS:
15105 			// Radius
15106 			SmRadiusDlg(hWnd, s);
15107 			SmHubDlgRefresh(hWnd, s);
15108 			break;
15109 
15110 		case B_LINK:
15111 			// Cascade
15112 			SmLinkDlg(hWnd, s);
15113 			SmHubDlgRefresh(hWnd, s);
15114 			break;
15115 
15116 		case B_SESSION:
15117 			// Session
15118 			SmSessionDlg(hWnd, s);
15119 			SmHubDlgRefresh(hWnd, s);
15120 			break;
15121 
15122 		case B_LOG:
15123 			// Log
15124 			Dialog(hWnd, D_SM_LOG, SmLogDlg, s);
15125 			SmHubDlgRefresh(hWnd, s);
15126 			break;
15127 
15128 		case B_CA:
15129 			// CA
15130 			SmCaDlg(hWnd, s);
15131 			SmHubDlgRefresh(hWnd, s);
15132 			break;
15133 
15134 		case IDCANCEL:
15135 			// Cancel button
15136 			Close(hWnd);
15137 			break;
15138 
15139 		case B_REFRESH:
15140 			// Update
15141 			SmHubDlgRefresh(hWnd, s);
15142 			break;
15143 
15144 		case B_SNAT:
15145 			// SecureNAT
15146 			Dialog(hWnd, D_SM_SNAT, SmSNATDlgProc, s);
15147 			SmHubDlgRefresh(hWnd, s);
15148 			break;
15149 
15150 		case B_CRL:
15151 			// Certificate revocation list
15152 			Dialog(hWnd, D_SM_CRL, SmCrlDlgProc, s);
15153 			break;
15154 
15155 		case B_LOG_FILE:
15156 			// Log file
15157 			Dialog(hWnd, D_SM_LOG_FILE, SmLogFileDlgProc, s->p);
15158 			break;
15159 		}
15160 		break;
15161 
15162 	case WM_CLOSE:
15163 		EndDialog(hWnd, false);
15164 		break;
15165 	}
15166 
15167 	return 0;
15168 }
15169 
15170 // Management of HUB
SmHubDlg(HWND hWnd,SM_HUB * s)15171 void SmHubDlg(HWND hWnd, SM_HUB *s)
15172 {
15173 	// Validate arguments
15174 	if (hWnd == NULL || s == NULL)
15175 	{
15176 		return;
15177 	}
15178 
15179 	Dialog(hWnd, D_SM_HUB, SmHubDlgProc, s);
15180 }
15181 
15182 // Change the server password
SmChangeServerPasswordDlg(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)15183 UINT SmChangeServerPasswordDlg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
15184 {
15185 	SM_SERVER *p = (SM_SERVER *)param;
15186 	char tmp1[MAX_SIZE];
15187 	char tmp2[MAX_SIZE];
15188 	UCHAR hash[SHA1_SIZE];
15189 	RPC_SET_PASSWORD t;
15190 	SETTING *setting;
15191 	// Validate arguments
15192 	if (hWnd == NULL)
15193 	{
15194 		return 0;
15195 	}
15196 
15197 	switch (msg)
15198 	{
15199 	case WM_INITDIALOG:
15200 		// Initialize
15201 		SetIcon(hWnd, 0, ICO_USER_ADMIN);
15202 		FormatText(hWnd, 0, p->ServerName);
15203 		FormatText(hWnd, S_TITLE, p->ServerName);
15204 		Focus(hWnd, E_PASSWORD1);
15205 		break;
15206 
15207 	case WM_COMMAND:
15208 		switch (LOWORD(wParam))
15209 		{
15210 		case E_PASSWORD1:
15211 		case E_PASSWORD2:
15212 			GetTxtA(hWnd, E_PASSWORD1, tmp1, sizeof(tmp1));
15213 			GetTxtA(hWnd, E_PASSWORD2, tmp2, sizeof(tmp2));
15214 
15215 			if (StrLen(tmp1) == 0 || StrLen(tmp2) == 0)
15216 			{
15217 				Disable(hWnd, IDOK);
15218 			}
15219 			else
15220 			{
15221 				Enable(hWnd, IDOK);
15222 			}
15223 			break;
15224 		}
15225 
15226 		switch (wParam)
15227 		{
15228 		case IDOK:
15229 			// [OK] button
15230 			GetTxtA(hWnd, E_PASSWORD1, tmp1, sizeof(tmp1));
15231 			GetTxtA(hWnd, E_PASSWORD2, tmp2, sizeof(tmp2));
15232 			if (StrCmp(tmp1, tmp2) != 0)
15233 			{
15234 				MsgBox(hWnd, MB_ICONSTOP, _UU("SM_CHANGE_PASSWORD_1"));
15235 				FocusEx(hWnd, E_PASSWORD2);
15236 				break;
15237 			}
15238 			if (StrLen(tmp1) == 0)
15239 			{
15240 				if (MsgBox(hWnd, MB_ICONEXCLAMATION | MB_YESNO | MB_DEFBUTTON2, _UU("SM_CHANGE_PASSWORD_2")) == IDNO)
15241 				{
15242 					Focus(hWnd, E_PASSWORD1);
15243 					break;
15244 				}
15245 			}
15246 			Zero(&t, sizeof(t));
15247 			Hash(t.HashedPassword, tmp1, StrLen(tmp1), true);
15248 			Copy(hash, t.HashedPassword, sizeof(hash));
15249 			if (CALL(hWnd, ScSetServerPassword(p->Rpc, &t)) == false)
15250 			{
15251 				break;
15252 			}
15253 			MsgBox(hWnd, MB_ICONINFORMATION, _UU("SM_CHANGE_PASSWORD_3"));
15254 
15255 			// Change the password for the connection setting
15256 			setting = SmGetSetting(p->CurrentSetting->Title);
15257 			if (setting != NULL && sm->TempSetting == NULL)
15258 			{
15259 				if (IsZero(setting->HashedPassword, SHA1_SIZE) == false)
15260 				{
15261 					Copy(setting->HashedPassword, hash, SHA1_SIZE);
15262 					SmWriteSettingList();
15263 				}
15264 			}
15265 
15266 			EndDialog(hWnd, true);
15267 			break;
15268 
15269 		case IDCANCEL:
15270 			// Cancel button
15271 			Close(hWnd);
15272 			break;
15273 		}
15274 		break;
15275 
15276 	case WM_CLOSE:
15277 		EndDialog(hWnd, false);
15278 		break;
15279 	}
15280 
15281 	return 0;
15282 }
15283 
15284 // Update the status of the connection to the server farm controller
SmRefreshFarmConnectionInfo(HWND hWnd,SM_SERVER * p,void * param)15285 bool SmRefreshFarmConnectionInfo(HWND hWnd, SM_SERVER *p, void *param)
15286 {
15287 	RPC_FARM_CONNECTION_STATUS t;
15288 	LVB *b;
15289 	wchar_t tmp[MAX_SIZE];
15290 	char str[MAX_SIZE];
15291 	// Validate arguments
15292 	if (hWnd == NULL || p == NULL)
15293 	{
15294 		return false;
15295 	}
15296 
15297 	Zero(&t, sizeof(t));
15298 	if (CALL(hWnd, ScGetFarmConnectionStatus(p->Rpc, &t)) == false)
15299 	{
15300 		return false;
15301 	}
15302 
15303 	b = LvInsertStart();
15304 
15305 	if (t.Online == false)
15306 	{
15307 		LvInsertAdd(b, ICO_FARM, NULL, 2, _UU("SM_FC_IP"), _UU("SM_FC_NOT_CONNECTED"));
15308 
15309 		LvInsertAdd(b, ICO_PROTOCOL, NULL, 2, _UU("SM_FC_PORT"), _UU("SM_FC_NOT_CONNECTED"));
15310 	}
15311 	else
15312 	{
15313 		IPToStr32(str, sizeof(str), t.Ip);
15314 		StrToUni(tmp, sizeof(tmp), str);
15315 		LvInsertAdd(b, ICO_FARM, NULL, 2, _UU("SM_FC_IP"), tmp);
15316 
15317 		UniToStru(tmp, t.Port);
15318 		LvInsertAdd(b, ICO_PROTOCOL, NULL, 2, _UU("SM_FC_PORT"), tmp);
15319 	}
15320 
15321 	LvInsertAdd(b,
15322 		t.Online ? ICO_SERVER_ONLINE_EX : ICO_PROTOCOL_X, NULL, 2,
15323 		_UU("SM_FC_STATUS"),
15324 		t.Online ? _UU("SM_FC_ONLINE") : _UU("SM_FC_OFFLINE"));
15325 
15326 	if (t.Online == false)
15327 	{
15328 		UniFormat(tmp, sizeof(tmp), _UU("SM_FC_ERROR_TAG"), _E(t.LastError), t.LastError);
15329 		LvInsertAdd(b, ICO_STOP, NULL, 2,
15330 			_UU("SM_FC_LAST_ERROR"), tmp);
15331 	}
15332 
15333 	GetDateTimeStrEx64(tmp, sizeof(tmp), SystemToLocal64(t.StartedTime), NULL);
15334 	LvInsertAdd(b, ICO_INFORMATION, NULL, 2, _UU("SM_FC_START_TIME"), tmp);
15335 
15336 	GetDateTimeStrEx64(tmp, sizeof(tmp), SystemToLocal64(t.FirstConnectedTime), NULL);
15337 	LvInsertAdd(b, ICO_INFORMATION, NULL, 2, _UU("SM_FC_FIRST_TIME"), tmp);
15338 
15339 	//if (t.Online == false)
15340 	{
15341 		GetDateTimeStrEx64(tmp, sizeof(tmp), SystemToLocal64(t.CurrentConnectedTime), NULL);
15342 		LvInsertAdd(b, ICO_INFORMATION, NULL, 2, _UU("SM_FC_CURRENT_TIME"), tmp);
15343 	}
15344 
15345 	UniToStru(tmp, t.NumTry);
15346 	LvInsertAdd(b, ICO_PROTOCOL, NULL, 2, _UU("SM_FC_NUM_TRY"), tmp);
15347 
15348 	UniToStru(tmp, t.NumConnected);
15349 	LvInsertAdd(b, ICO_PROTOCOL, NULL, 2, _UU("SM_FC_NUM_CONNECTED"), tmp);
15350 
15351 	UniToStru(tmp, t.NumFailed);
15352 	LvInsertAdd(b, ICO_PROTOCOL, NULL, 2, _UU("SM_FC_NUM_FAILED"), tmp);
15353 
15354 	LvInsertEnd(b, hWnd, L_STATUS);
15355 
15356 	return true;
15357 }
15358 
15359 // Initialize
SmFarmMemberDlgInit(HWND hWnd,SM_SERVER * p)15360 void SmFarmMemberDlgInit(HWND hWnd, SM_SERVER *p)
15361 {
15362 	// Validate arguments
15363 	if (hWnd == NULL || p == NULL)
15364 	{
15365 		return;
15366 	}
15367 
15368 	SetIcon(hWnd, 0, ICO_FARM);
15369 
15370 	FormatText(hWnd, S_TITLE, p->ServerName);
15371 
15372 	// Initialize the column
15373 	LvInit(hWnd, L_FARM_MEMBER);
15374 	LvSetStyle(hWnd, L_FARM_MEMBER, LVS_EX_GRIDLINES);
15375 	LvInsertColumn(hWnd, L_FARM_MEMBER, 0, _UU("SM_FM_COLUMN_1"), 90);
15376 	LvInsertColumn(hWnd, L_FARM_MEMBER, 1, _UU("SM_FM_COLUMN_2"), 150);
15377 	LvInsertColumn(hWnd, L_FARM_MEMBER, 2, _UU("SM_FM_COLUMN_3"), 140);
15378 	LvInsertColumn(hWnd, L_FARM_MEMBER, 3, _UU("SM_FM_COLUMN_4"), 60);
15379 	LvInsertColumn(hWnd, L_FARM_MEMBER, 4, _UU("SM_FM_COLUMN_5"), 80);
15380 	LvInsertColumn(hWnd, L_FARM_MEMBER, 5, _UU("SM_FM_COLUMN_6"), 80);
15381 	LvInsertColumn(hWnd, L_FARM_MEMBER, 6, _UU("SM_FM_COLUMN_7"), 80);
15382 	LvInsertColumn(hWnd, L_FARM_MEMBER, 7, _UU("SM_FM_COLUMN_8"), 160);
15383 	LvInsertColumn(hWnd, L_FARM_MEMBER, 8, _UU("SM_FM_COLUMN_9"), 160);
15384 
15385 	SmFarmMemberDlgRefresh(hWnd, p);
15386 }
15387 
15388 // Update
SmFarmMemberDlgUpdate(HWND hWnd,SM_SERVER * p)15389 void SmFarmMemberDlgUpdate(HWND hWnd, SM_SERVER *p)
15390 {
15391 	// Validate arguments
15392 	if (hWnd == NULL || p == NULL)
15393 	{
15394 		return;
15395 	}
15396 
15397 	SetEnable(hWnd, IDOK, LvIsSelected(hWnd, L_FARM_MEMBER) && (LvIsMultiMasked(hWnd, L_FARM_MEMBER) == false));
15398 	SetEnable(hWnd, B_CERT, LvIsSelected(hWnd, L_FARM_MEMBER) && (LvIsMultiMasked(hWnd, L_FARM_MEMBER) == false));
15399 }
15400 
15401 // Content update
SmFarmMemberDlgRefresh(HWND hWnd,SM_SERVER * p)15402 void SmFarmMemberDlgRefresh(HWND hWnd, SM_SERVER *p)
15403 {
15404 	RPC_ENUM_FARM t;
15405 	UINT i;
15406 	// Validate arguments
15407 	if (hWnd == NULL || p == NULL)
15408 	{
15409 		return;
15410 	}
15411 
15412 	Zero(&t, sizeof(t));
15413 	if (CALL(hWnd, ScEnumFarmMember(p->Rpc, &t)) == false)
15414 	{
15415 		EndDialog(hWnd, false);
15416 		return;
15417 	}
15418 
15419 	LvReset(hWnd, L_FARM_MEMBER);
15420 
15421 	for (i = 0;i < t.NumFarm;i++)
15422 	{
15423 		RPC_ENUM_FARM_ITEM *e = &t.Farms[i];
15424 		wchar_t tmp1[MAX_SIZE];
15425 		wchar_t tmp2[MAX_SIZE];
15426 		wchar_t tmp3[64];
15427 		wchar_t tmp4[64];
15428 		wchar_t tmp5[64];
15429 		wchar_t tmp6[64];
15430 		wchar_t tmp7[64];
15431 		wchar_t tmp8[64];
15432 
15433 		GetDateTimeStrEx64(tmp1, sizeof(tmp1), SystemToLocal64(e->ConnectedTime), NULL);
15434 		StrToUni(tmp2, sizeof(tmp2), e->Hostname);
15435 		UniToStru(tmp3, e->Point);
15436 		UniToStru(tmp4, e->NumSessions);
15437 		UniToStru(tmp5, e->NumTcpConnections);
15438 		UniToStru(tmp6, e->NumHubs);
15439 		UniToStru(tmp7, e->AssignedClientLicense);
15440 		UniToStru(tmp8, e->AssignedBridgeLicense);
15441 
15442 		LvInsert(hWnd, L_FARM_MEMBER, e->Controller ? ICO_FARM : ICO_TOWER, (void *)e->Id, 9,
15443 			e->Controller ? _UU("SM_FM_CONTROLLER") : _UU("SM_FM_MEMBER"),
15444 			tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8);
15445 	}
15446 
15447 	FreeRpcEnumFarm(&t);
15448 
15449 	SmFarmMemberDlgUpdate(hWnd, p);
15450 }
15451 
15452 // [OK] button
SmFarmMemberDlgOnOk(HWND hWnd,SM_SERVER * p)15453 void SmFarmMemberDlgOnOk(HWND hWnd, SM_SERVER *p)
15454 {
15455 	// Validate arguments
15456 	if (hWnd == NULL || p == NULL)
15457 	{
15458 		return;
15459 	}
15460 
15461 }
15462 
15463 // Display the farm member certificate
SmFarmMemberCert(HWND hWnd,SM_SERVER * p,UINT id)15464 void SmFarmMemberCert(HWND hWnd, SM_SERVER *p, UINT id)
15465 {
15466 	RPC_FARM_INFO t;
15467 	// Validate arguments
15468 	if (hWnd == NULL || p == NULL || id == 0)
15469 	{
15470 		return;
15471 	}
15472 
15473 	Zero(&t, sizeof(t));
15474 	t.Id = id;
15475 
15476 	if (CALL(hWnd, ScGetFarmInfo(p->Rpc, &t)) == false)
15477 	{
15478 		return;
15479 	}
15480 
15481 	CertDlg(hWnd, t.ServerCert, NULL, true);
15482 
15483 	FreeRpcFarmInfo(&t);
15484 }
15485 
15486 // Update the farm member information
SmRefreshFarmMemberInfo(HWND hWnd,SM_SERVER * p,void * param)15487 bool SmRefreshFarmMemberInfo(HWND hWnd, SM_SERVER *p, void *param)
15488 {
15489 	RPC_FARM_INFO t;
15490 	UINT id = (UINT)param;
15491 	LVB *b;
15492 	UINT i;
15493 	wchar_t tmp[MAX_SIZE];
15494 	char str[MAX_SIZE];
15495 	// Validate arguments
15496 	if (hWnd == NULL || p == NULL || id == 0)
15497 	{
15498 		return false;
15499 	}
15500 
15501 	Zero(&t, sizeof(t));
15502 	t.Id = id;
15503 
15504 	if (CALL(hWnd, ScGetFarmInfo(p->Rpc, &t)) == false)
15505 	{
15506 		return false;
15507 	}
15508 
15509 	b = LvInsertStart();
15510 
15511 	LvInsertAdd(b, ICO_FARM, NULL, 2, _UU("SM_FMINFO_TYPE"),
15512 		t.Controller ? _UU("SM_FARM_CONTROLLER") : _UU("SM_FARM_MEMBER"));
15513 
15514 	GetDateTimeStrEx64(tmp, sizeof(tmp), SystemToLocal64(t.ConnectedTime), NULL);
15515 	LvInsertAdd(b, ICO_INFORMATION, NULL, 2, _UU("SM_FMINFO_CONNECT_TIME"), tmp);
15516 
15517 	IPToStr32(str, sizeof(str), t.Ip);
15518 	StrToUni(tmp, sizeof(tmp), str);
15519 	LvInsertAdd(b, ICO_PROTOCOL, NULL, 2, _UU("SM_FMINFO_IP"), tmp);
15520 
15521 	StrToUni(tmp, sizeof(tmp), t.Hostname);
15522 	LvInsertAdd(b, ICO_TOWER, NULL, 2, _UU("SM_FMINFO_HOSTNAME"), tmp);
15523 
15524 	UniToStru(tmp, t.Point);
15525 	LvInsertAdd(b, ICO_TEST, NULL, 2, _UU("SM_FMINFO_POINT"), tmp);
15526 
15527 	UniToStru(tmp, t.Weight);
15528 	LvInsertAdd(b, ICO_MACHINE, NULL, 2, _UU("SM_FMINFO_WEIGHT"), tmp);
15529 
15530 	UniToStru(tmp, t.NumPort);
15531 	LvInsertAdd(b, ICO_PROTOCOL, NULL, 2, _UU("SM_FMINFO_NUM_PORT"), tmp);
15532 
15533 	for (i = 0;i < t.NumPort;i++)
15534 	{
15535 		wchar_t tmp2[MAX_SIZE];
15536 		UniFormat(tmp, sizeof(tmp), _UU("SM_FMINFO_PORT"), i + 1);
15537 		UniToStru(tmp2, t.Ports[i]);
15538 		LvInsertAdd(b, ICO_PROTOCOL, NULL, 2, tmp, tmp2);
15539 	}
15540 
15541 	UniToStru(tmp, t.NumFarmHub);
15542 	LvInsertAdd(b, ICO_HUB_OFFLINE, NULL, 2, _UU("SM_FMINFO_NUM_HUB"), tmp);
15543 
15544 	for (i = 0;i < t.NumFarmHub;i++)
15545 	{
15546 		wchar_t tmp2[MAX_SIZE];
15547 		UniFormat(tmp, sizeof(tmp), _UU("SM_FMINFO_HUB"), i + 1);
15548 		UniFormat(tmp2, sizeof(tmp2),
15549 			t.FarmHubs[i].DynamicHub ? _UU("SM_FMINFO_HUB_TAG_2") : _UU("SM_FMINFO_HUB_TAG_1"),
15550 			t.FarmHubs[i].HubName);
15551 		LvInsertAdd(b, ICO_HUB, NULL, 2, tmp, tmp2);
15552 	}
15553 
15554 	UniToStru(tmp, t.NumSessions);
15555 	LvInsertAdd(b, ICO_VPN, NULL, 2, _UU("SM_FMINFO_NUM_SESSION"), tmp);
15556 
15557 	UniToStru(tmp, t.NumTcpConnections);
15558 	LvInsertAdd(b, ICO_PROTOCOL, NULL, 2, _UU("SM_FMINFO_NUN_CONNECTION"), tmp);
15559 
15560 	LvInsertEnd(b, hWnd, L_STATUS);
15561 
15562 	FreeRpcFarmInfo(&t);
15563 
15564 	return true;
15565 }
15566 
15567 // Farm Member List dialog
SmFarmMemberDlgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)15568 UINT SmFarmMemberDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
15569 {
15570 	SM_SERVER *p = (SM_SERVER *)param;
15571 	NMHDR *n;
15572 	UINT i;
15573 	// Validate arguments
15574 	if (hWnd == NULL)
15575 	{
15576 		return 0;
15577 	}
15578 
15579 	switch (msg)
15580 	{
15581 	case WM_INITDIALOG:
15582 		// Initialize
15583 		SmFarmMemberDlgInit(hWnd, p);
15584 		break;
15585 
15586 	case WM_COMMAND:
15587 		switch (wParam)
15588 		{
15589 		case IDOK:
15590 			// Display the information of farm members
15591 			i = LvGetSelected(hWnd, L_FARM_MEMBER);
15592 			if (i != INFINITE)
15593 			{
15594 				SmStatusDlg(hWnd, p, LvGetParam(hWnd, L_FARM_MEMBER, i), false, true,
15595 					_UU("SM_FMINFO_CAPTION"), ICO_FARM, NULL, SmRefreshFarmMemberInfo);
15596 			}
15597 			break;
15598 
15599 		case B_CERT:
15600 			// Show the Server Certificate
15601 			i = LvGetSelected(hWnd, L_FARM_MEMBER);
15602 			if (i != INFINITE)
15603 			{
15604 				SmFarmMemberCert(hWnd, p, (UINT)LvGetParam(hWnd, L_FARM_MEMBER, i));
15605 			}
15606 			break;
15607 
15608 		case IDCANCEL:
15609 			// Cancel button
15610 			Close(hWnd);
15611 			break;
15612 
15613 		case B_REFRESH:
15614 			// Update
15615 			SmFarmMemberDlgRefresh(hWnd, p);
15616 			break;
15617 		}
15618 		break;
15619 
15620 	case WM_NOTIFY:
15621 		n = (NMHDR *)lParam;
15622 		switch (n->code)
15623 		{
15624 		case LVN_ITEMCHANGED:
15625 			switch (n->idFrom)
15626 			{
15627 			case L_FARM_MEMBER:
15628 				SmFarmMemberDlgUpdate(hWnd, p);
15629 				break;
15630 			}
15631 			break;
15632 		}
15633 		break;
15634 
15635 	case WM_CLOSE:
15636 		EndDialog(hWnd, false);
15637 		break;
15638 	}
15639 
15640 	LvStandardHandler(hWnd, msg, wParam, lParam, L_FARM_MEMBER);
15641 
15642 	return 0;
15643 }
15644 
15645 // Convert the string to port list
SmStrToPortList(char * str)15646 LIST *SmStrToPortList(char *str)
15647 {
15648 	return StrToPortList(str);
15649 }
15650 
15651 // Initialize the dialog
SmFarmDlgInit(HWND hWnd,SM_SERVER * p)15652 void SmFarmDlgInit(HWND hWnd, SM_SERVER *p)
15653 {
15654 	RPC_FARM t;
15655 	// Validate arguments
15656 	if (hWnd == NULL || p == NULL)
15657 	{
15658 		return;
15659 	}
15660 
15661 	SetIcon(hWnd, 0, ICO_FARM);
15662 
15663 	// Get the current settings
15664 	Zero(&t, sizeof(t));
15665 	if (CALL(hWnd, ScGetFarmSetting(p->Rpc, &t)) == false)
15666 	{
15667 		EndDialog(hWnd, false);
15668 		return;
15669 	}
15670 
15671 	if (t.Weight == 0)
15672 	{
15673 		t.Weight = FARM_DEFAULT_WEIGHT;
15674 	}
15675 
15676 	FormatText(hWnd, S_TITLE, p->ServerName);
15677 	DlgFont(hWnd, S_CURRENT, 11, true);
15678 
15679 	SetText(hWnd, S_CURRENT, GetServerTypeStr(t.ServerType));
15680 
15681 	switch (t.ServerType)
15682 	{
15683 	case SERVER_TYPE_FARM_CONTROLLER:
15684 		Check(hWnd, R_CONTROLLER, true);
15685 		break;
15686 
15687 	case SERVER_TYPE_FARM_MEMBER:
15688 		Check(hWnd, R_MEMBER, true);
15689 		break;
15690 
15691 	default:
15692 		Check(hWnd, R_STANDALONE, true);
15693 		break;
15694 	}
15695 
15696 	SetInt(hWnd, E_WEIGHT, t.Weight);
15697 
15698 	if (t.ServerType == SERVER_TYPE_FARM_CONTROLLER)
15699 	{
15700 		Check(hWnd, R_CONTROLLER_ONLY, t.ControllerOnly);
15701 	}
15702 
15703 	if (t.ServerType == SERVER_TYPE_FARM_MEMBER)
15704 	{
15705 		char tmp[MAX_PUBLIC_PORT_NUM * 8];
15706 		UINT i;
15707 		if (t.PublicIp != 0)
15708 		{
15709 			IpSet(hWnd, E_IP, t.PublicIp);
15710 		}
15711 		StrCpy(tmp, sizeof(tmp), "");
15712 		if (t.NumPort != 0)
15713 		{
15714 			for (i = 0;i < t.NumPort;i++)
15715 			{
15716 				Format(tmp, sizeof(tmp), "%s%u", tmp, t.Ports[i]);
15717 				if (i != (t.NumPort - 1))
15718 				{
15719 					StrCat(tmp, sizeof(tmp), ", ");
15720 				}
15721 			}
15722 			SetTextA(hWnd, E_PORT, tmp);
15723 		}
15724 		SetTextA(hWnd, E_CONTROLLER, t.ControllerName);
15725 		SetIntEx(hWnd, E_CONTROLLER_PORT, t.ControllerPort);
15726 		SetTextA(hWnd, E_PASSWORD, HIDDEN_PASSWORD);
15727 	}
15728 	else
15729 	{
15730 		// Write the port list
15731 		RPC_LISTENER_LIST t;
15732 		char tmp[MAX_PUBLIC_PORT_NUM * 8];
15733 		Zero(&t, sizeof(t));
15734 		StrCpy(tmp, sizeof(tmp), "");
15735 		if (CALL(hWnd, ScEnumListener(p->Rpc, &t)))
15736 		{
15737 			UINT i;
15738 			if (t.NumPort != 0)
15739 			{
15740 				for (i = 0;i < t.NumPort;i++)
15741 				{
15742 					Format(tmp, sizeof(tmp), "%s%u", tmp, t.Ports[i]);
15743 					if (i != (t.NumPort - 1))
15744 					{
15745 						StrCat(tmp, sizeof(tmp), ", ");
15746 					}
15747 				}
15748 				SetTextA(hWnd, E_PORT, tmp);
15749 			}
15750 			FreeRpcListenerList(&t);
15751 		}
15752 	}
15753 
15754 	SmFarmDlgUpdate(hWnd, p);
15755 
15756 	FreeRpcFarm(&t);
15757 
15758 	Focus(hWnd, IDOK);
15759 }
15760 
15761 // Dialog update
SmFarmDlgUpdate(HWND hWnd,SM_SERVER * p)15762 void SmFarmDlgUpdate(HWND hWnd, SM_SERVER *p)
15763 {
15764 	bool ok = true;
15765 	bool farm_member_control = false;
15766 	char *s;
15767 	// Validate arguments
15768 	if (hWnd == NULL || p == NULL)
15769 	{
15770 		return;
15771 	}
15772 
15773 	if (IsChecked(hWnd, R_MEMBER))
15774 	{
15775 		LIST *o;
15776 		UINT i = IpGetFilledNum(hWnd, E_IP);
15777 		if (i != 0 && i != 4)
15778 		{
15779 			ok = false;
15780 		}
15781 
15782 		s = GetTextA(hWnd, E_PORT);
15783 		o = SmStrToPortList(s);
15784 		if (o == NULL)
15785 		{
15786 			ok = false;
15787 		}
15788 		else
15789 		{
15790 			ReleaseList(o);
15791 		}
15792 		Free(s);
15793 
15794 		if (IsEmpty(hWnd, E_CONTROLLER))
15795 		{
15796 			ok = false;
15797 		}
15798 
15799 		i = GetInt(hWnd, E_CONTROLLER_PORT);
15800 		if (i == 0 || i >= 65536)
15801 		{
15802 			ok = false;
15803 		}
15804 
15805 		farm_member_control = true;
15806 	}
15807 
15808 	if (IsChecked(hWnd, R_STANDALONE))
15809 	{
15810 		Disable(hWnd, S_1);
15811 		Disable(hWnd, S_2);
15812 		Disable(hWnd, E_WEIGHT);
15813 	}
15814 	else
15815 	{
15816 		Enable(hWnd, S_1);
15817 		Enable(hWnd, S_2);
15818 		Enable(hWnd, E_WEIGHT);
15819 	}
15820 
15821 	if (IsChecked(hWnd, R_CONTROLLER))
15822 	{
15823 		Enable(hWnd, R_CONTROLLER_ONLY);
15824 	}
15825 	else
15826 	{
15827 		Disable(hWnd, R_CONTROLLER_ONLY);
15828 	}
15829 
15830 	if (IsChecked(hWnd, R_CONTROLLER) || IsChecked(hWnd, R_MEMBER))
15831 	{
15832 		if (GetInt(hWnd, E_WEIGHT) == 0)
15833 		{
15834 			ok = false;
15835 		}
15836 	}
15837 
15838 	SetEnable(hWnd, S_IP_1, farm_member_control);
15839 	SetEnable(hWnd, E_IP, farm_member_control);
15840 	SetEnable(hWnd, S_IP_2, farm_member_control);
15841 	SetEnable(hWnd, S_PORT_1, farm_member_control);
15842 	SetEnable(hWnd, E_PORT, farm_member_control);
15843 	SetEnable(hWnd, S_PORT_2, farm_member_control);
15844 	SetEnable(hWnd, S_PORT_3, farm_member_control);
15845 	SetEnable(hWnd, E_CONTROLLER, farm_member_control);
15846 	SetEnable(hWnd, S_CONTROLLER, farm_member_control);
15847 	SetEnable(hWnd, E_CONTROLLER_PORT, farm_member_control);
15848 	SetEnable(hWnd, S_CONTROLLER_PORT, farm_member_control);
15849 	SetEnable(hWnd, S_PASSWORD, farm_member_control);
15850 	SetEnable(hWnd, E_PASSWORD, farm_member_control);
15851 	SetEnable(hWnd, IDOK, ok);
15852 }
15853 
15854 // [OK] button
SmFarmDlgOnOk(HWND hWnd,SM_SERVER * p)15855 void SmFarmDlgOnOk(HWND hWnd, SM_SERVER *p)
15856 {
15857 	// Validate arguments
15858 	if (hWnd == NULL || p == NULL)
15859 	{
15860 		return;
15861 	}
15862 
15863 	// Display the message
15864 	if (MsgBox(hWnd, MB_ICONEXCLAMATION | MB_OKCANCEL | MB_DEFBUTTON2,
15865 		_UU("SM_FARM_REBOOT_MSG")) == IDOK)
15866 	{
15867 		RPC_FARM t;
15868 		Zero(&t, sizeof(t));
15869 		t.ServerType = SERVER_TYPE_STANDALONE;
15870 		if (IsChecked(hWnd, R_CONTROLLER))
15871 		{
15872 			t.ServerType = SERVER_TYPE_FARM_CONTROLLER;
15873 		}
15874 		if (IsChecked(hWnd, R_MEMBER))
15875 		{
15876 			t.ServerType = SERVER_TYPE_FARM_MEMBER;
15877 		}
15878 
15879 		t.ControllerOnly = IsChecked(hWnd, R_CONTROLLER_ONLY);
15880 		t.Weight = GetInt(hWnd, E_WEIGHT);
15881 
15882 		if (t.ServerType == SERVER_TYPE_FARM_MEMBER)
15883 		{
15884 			char *s;
15885 			char pass[MAX_SIZE];
15886 			t.PublicIp = IpGet(hWnd, E_IP);
15887 			s = GetTextA(hWnd, E_PORT);
15888 			if (s != NULL)
15889 			{
15890 				LIST *o = SmStrToPortList(s);
15891 				if (o != NULL)
15892 				{
15893 					UINT i;
15894 					t.NumPort = LIST_NUM(o);
15895 					t.Ports = ZeroMalloc(sizeof(UINT) * t.NumPort);
15896 					for (i = 0;i < t.NumPort;i++)
15897 					{
15898 						t.Ports[i] = (UINT)LIST_DATA(o, i);
15899 					}
15900 					ReleaseList(o);
15901 				}
15902 				Free(s);
15903 			}
15904 			GetTxtA(hWnd, E_CONTROLLER, t.ControllerName, sizeof(t.ControllerName));
15905 			t.ControllerPort = GetInt(hWnd, E_CONTROLLER_PORT);
15906 			GetTxtA(hWnd, E_PASSWORD, pass, sizeof(pass));
15907 			if (StrCmp(pass, HIDDEN_PASSWORD) != 0)
15908 			{
15909 				Hash(t.MemberPassword, pass, StrLen(pass), true);
15910 			}
15911 		}
15912 
15913 		// Configuration update
15914 		if (CALL(hWnd, ScSetFarmSetting(p->Rpc, &t)) == false)
15915 		{
15916 			return;
15917 		}
15918 
15919 		FreeRpcFarm(&t);
15920 
15921 		EndDialog(hWnd, true);
15922 	}
15923 }
15924 
15925 // Server farm dialog procedure
SmFarmDlgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)15926 UINT SmFarmDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
15927 {
15928 	SM_SERVER *p = (SM_SERVER *)param;
15929 	// Validate arguments
15930 	if (hWnd == NULL)
15931 	{
15932 		return 0;
15933 	}
15934 
15935 	switch (msg)
15936 	{
15937 	case WM_INITDIALOG:
15938 		// Initialize
15939 		SmFarmDlgInit(hWnd, p);
15940 		break;
15941 
15942 	case WM_COMMAND:
15943 		switch (LOWORD(wParam))
15944 		{
15945 		case R_STANDALONE:
15946 		case R_CONTROLLER:
15947 		case R_MEMBER:
15948 		case E_IP:
15949 		case E_PORT:
15950 		case E_CONTROLLER:
15951 		case E_CONTROLLER_PORT:
15952 		case E_PASSWORD:
15953 		case R_CONTROLLER_ONLY:
15954 		case E_WEIGHT:
15955 			SmFarmDlgUpdate(hWnd, p);
15956 			break;
15957 		}
15958 
15959 		switch (wParam)
15960 		{
15961 		case IDOK:
15962 			// [OK] button
15963 			SmFarmDlgOnOk(hWnd, p);
15964 			break;
15965 
15966 		case IDCANCEL:
15967 			// Cancel button
15968 			Close(hWnd);
15969 			break;
15970 
15971 		case R_MEMBER:
15972 			if (IsChecked(hWnd, R_MEMBER))
15973 			{
15974 				Focus(hWnd, E_IP);
15975 			}
15976 			break;
15977 		}
15978 		break;
15979 
15980 	case WM_CLOSE:
15981 		EndDialog(hWnd, false);
15982 		break;
15983 	}
15984 
15985 	return 0;
15986 }
15987 
15988 // Server farm configuration
SmFarmDlg(HWND hWnd,SM_SERVER * p)15989 bool SmFarmDlg(HWND hWnd, SM_SERVER *p)
15990 {
15991 	// Validate arguments
15992 	if (hWnd == NULL || p == NULL)
15993 	{
15994 		return false;
15995 	}
15996 
15997 	return Dialog(hWnd, D_SM_FARM, SmFarmDlgProc, p);
15998 }
15999 
16000 // Update the connection information
SmRefreshConnectionStatus(HWND hWnd,SM_SERVER * p,void * param)16001 bool SmRefreshConnectionStatus(HWND hWnd, SM_SERVER *p, void *param)
16002 {
16003 	RPC_CONNECTION_INFO t;
16004 	SM_CONNECTION_INFO *info = (SM_CONNECTION_INFO *)param;
16005 	LVB *b;
16006 	wchar_t tmp[MAX_SIZE];
16007 	// Validate arguments
16008 	if (hWnd == NULL || p == NULL || param == NULL)
16009 	{
16010 		return false;
16011 	}
16012 
16013 	Zero(&t, sizeof(t));
16014 	StrCpy(t.Name, sizeof(t.Name), info->ConnectionName);
16015 	if (CALL(hWnd, ScGetConnectionInfo(p->Rpc, &t)) == false)
16016 	{
16017 		return false;
16018 	}
16019 
16020 	b = LvInsertStart();
16021 
16022 	StrToUni(tmp, sizeof(tmp), t.Name);
16023 	LvInsertAdd(b, ICO_PROTOCOL, NULL, 2, _UU("SM_CONNINFO_NAME"), tmp);
16024 
16025 	LvInsertAdd(b, ICO_MACHINE, NULL, 2, _UU("SM_CONNINFO_TYPE"), SmGetConnectionTypeStr(t.Type));
16026 
16027 	StrToUni(tmp, sizeof(tmp), t.Hostname);
16028 	LvInsertAdd(b, ICO_FARM, NULL, 2, _UU("SM_CONNINFO_HOSTNAME"), tmp);
16029 
16030 	UniToStru(tmp, t.Port);
16031 	LvInsertAdd(b, ICO_PROTOCOL, NULL, 2, _UU("SM_CONNINFO_PORT"), tmp);
16032 
16033 	GetDateTimeStrEx64(tmp, sizeof(tmp), SystemToLocal64(t.ConnectedTime), NULL);
16034 	LvInsertAdd(b, ICO_INFORMATION, NULL, 2, _UU("SM_CONNINFO_TIME"), tmp);
16035 
16036 	StrToUni(tmp, sizeof(tmp), t.ServerStr);
16037 	LvInsertAdd(b, ICO_VPNSERVER, NULL, 2, _UU("SM_CONNINFO_SERVER_STR"), tmp);
16038 
16039 	UniFormat(tmp, sizeof(tmp), L"%u.%02u", t.ServerVer / 100, t.ServerVer % 100);
16040 	LvInsertAdd(b, ICO_MACHINE, NULL, 2, _UU("SM_CONNINFO_SERVER_VER"), tmp);
16041 
16042 	UniToStru(tmp, t.ServerBuild);
16043 	LvInsertAdd(b, ICO_MACHINE, NULL, 2, _UU("SM_CONNINFO_SERVER_BUILD"), tmp);
16044 
16045 	if (StrLen(t.ClientStr) != 0)
16046 	{
16047 		StrToUni(tmp, sizeof(tmp), t.ClientStr);
16048 		LvInsertAdd(b, ICO_VPN, NULL, 2, _UU("SM_CONNINFO_CLIENT_STR"), tmp);
16049 
16050 		UniFormat(tmp, sizeof(tmp), L"%u.%02u", t.ClientVer / 100, t.ClientVer % 100);
16051 		LvInsertAdd(b, ICO_MACHINE, NULL, 2, _UU("SM_CONNINFO_CLIENT_VER"), tmp);
16052 
16053 		UniToStru(tmp, t.ClientBuild);
16054 		LvInsertAdd(b, ICO_MACHINE, NULL, 2, _UU("SM_CONNINFO_CLIENT_BUILD"), tmp);
16055 	}
16056 
16057 	LvInsertEnd(b, hWnd, L_STATUS);
16058 
16059 	return true;
16060 }
16061 
16062 // Initialize
SmConnectionDlgInit(HWND hWnd,SM_SERVER * p)16063 void SmConnectionDlgInit(HWND hWnd, SM_SERVER *p)
16064 {
16065 	// Validate arguments
16066 	if (hWnd == NULL || p == NULL)
16067 	{
16068 		return;
16069 	}
16070 
16071 	SetIcon(hWnd, 0, ICO_PROTOCOL);
16072 	FormatText(hWnd, S_TITLE, p->ServerName);
16073 
16074 	// Initialize the column
16075 	LvInit(hWnd, L_LIST);
16076 	LvSetStyle(hWnd, L_LIST, LVS_EX_GRIDLINES);
16077 	LvInsertColumn(hWnd, L_LIST, 0, _UU("SM_CONN_COLUMN_1"), 90);
16078 	LvInsertColumn(hWnd, L_LIST, 1, _UU("SM_CONN_COLUMN_2"), 150);
16079 	LvInsertColumn(hWnd, L_LIST, 2, _UU("SM_CONN_COLUMN_3"), 200);
16080 	LvInsertColumn(hWnd, L_LIST, 3, _UU("SM_CONN_COLUMN_4"), 80);
16081 
16082 	SmConnectionDlgRefresh(hWnd, p);
16083 	SmConnectionDlgUpdate(hWnd, p);
16084 }
16085 
16086 // Update
SmConnectionDlgRefresh(HWND hWnd,SM_SERVER * p)16087 void SmConnectionDlgRefresh(HWND hWnd, SM_SERVER *p)
16088 {
16089 	LVB *b;
16090 	UINT i;
16091 	RPC_ENUM_CONNECTION t;
16092 	// Validate arguments
16093 	if (hWnd == NULL || p == NULL)
16094 	{
16095 		return;
16096 	}
16097 
16098 	Zero(&t, sizeof(t));
16099 	if (CALL(hWnd, ScEnumConnection(p->Rpc, &t)) == false)
16100 	{
16101 		EndDialog(hWnd, false);
16102 		return;
16103 	}
16104 
16105 	b = LvInsertStart();
16106 
16107 	for (i = 0;i < t.NumConnection;i++)
16108 	{
16109 		wchar_t tmp[MAX_SIZE];
16110 		wchar_t name[MAX_SIZE];
16111 		wchar_t datetime[MAX_SIZE];
16112 		RPC_ENUM_CONNECTION_ITEM *e = &t.Connections[i];
16113 
16114 		StrToUni(name, sizeof(name), e->Name);
16115 		UniFormat(tmp, sizeof(tmp), _UU("SM_HOSTNAME_AND_PORT"), e->Hostname, e->Port);
16116 		GetDateTimeStrEx64(datetime, sizeof(datetime), SystemToLocal64(e->ConnectedTime), NULL);
16117 
16118 		LvInsertAdd(b, ICO_PROTOCOL, NULL, 4, name, tmp, datetime,
16119 			SmGetConnectionTypeStr(e->Type));
16120 	}
16121 
16122 	LvInsertEnd(b, hWnd, L_LIST);
16123 
16124 	FreeRpcEnumConnetion(&t);
16125 }
16126 
16127 // Update the control
SmConnectionDlgUpdate(HWND hWnd,SM_SERVER * p)16128 void SmConnectionDlgUpdate(HWND hWnd, SM_SERVER *p)
16129 {
16130 	bool b = false;
16131 	// Validate arguments
16132 	if (hWnd == NULL || p == NULL)
16133 	{
16134 		return;
16135 	}
16136 
16137 	if (LvIsSelected(hWnd, L_LIST) && (LvIsMultiMasked(hWnd, L_LIST) == false))
16138 	{
16139 		b = true;
16140 	}
16141 
16142 	SetEnable(hWnd, IDOK, b);
16143 	SetEnable(hWnd, B_DISCONNECT, b && p->ServerAdminMode);
16144 }
16145 
16146 // Connection List procedure
SmConnectionDlgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)16147 UINT SmConnectionDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
16148 {
16149 	SM_SERVER *p = (SM_SERVER *)param;
16150 	NMHDR *n;
16151 	wchar_t *s;
16152 	// Validate arguments
16153 	if (hWnd == NULL)
16154 	{
16155 		return 0;
16156 	}
16157 
16158 	switch (msg)
16159 	{
16160 	case WM_INITDIALOG:
16161 		// Initialize
16162 		SmConnectionDlgInit(hWnd, p);
16163 		break;
16164 
16165 	case WM_COMMAND:
16166 		switch (wParam)
16167 		{
16168 		case IDOK:
16169 			// Show the connection information
16170 			s = LvGetSelectedStr(hWnd, L_LIST, 0);
16171 			if (s != NULL)
16172 			{
16173 				wchar_t caption[MAX_SIZE];
16174 				SM_CONNECTION_INFO info;
16175 				UniFormat(caption, sizeof(caption), _UU("SM_CONNINFO_CAPTION"),
16176 					s);
16177 				Zero(&info, sizeof(info));
16178 				info.ConnectionName = CopyUniToStr(s);
16179 				info.p = p;
16180 				SmStatusDlg(hWnd, p, &info, false, false, caption, ICO_PROTOCOL,
16181 					NULL, SmRefreshConnectionStatus);
16182 				Free(info.ConnectionName);
16183 				Free(s);
16184 			}
16185 			break;
16186 
16187 		case B_DISCONNECT:
16188 			// Disconnect
16189 			s = LvGetSelectedStr(hWnd, L_LIST, 0);
16190 			if (s != NULL)
16191 			{
16192 				if (MsgBoxEx(hWnd, MB_ICONQUESTION | MB_YESNO | MB_DEFBUTTON2,
16193 					_UU("SM_CONN_DISCONNECT_MSG"), s) == IDYES)
16194 				{
16195 					char tmp[MAX_SIZE];
16196 					RPC_DISCONNECT_CONNECTION t;
16197 
16198 					UniToStr(tmp, sizeof(tmp), s);
16199 					Zero(&t, sizeof(t));
16200 					StrCpy(t.Name, sizeof(t.Name), tmp);
16201 
16202 					if (CALL(hWnd, ScDisconnectConnection(p->Rpc, &t)))
16203 					{
16204 						SmConnectionDlgRefresh(hWnd, p);
16205 					}
16206 				}
16207 				Free(s);
16208 			}
16209 			break;
16210 
16211 		case B_REFRESH:
16212 			// Update to the latest state
16213 			SmConnectionDlgRefresh(hWnd, p);
16214 			break;
16215 
16216 		case IDCANCEL:
16217 			// Cancel button
16218 			Close(hWnd);
16219 			break;
16220 		}
16221 		break;
16222 
16223 	case WM_NOTIFY:
16224 		n = (NMHDR *)lParam;
16225 		switch (n->idFrom)
16226 		{
16227 		case L_LIST:
16228 			switch (n->code)
16229 			{
16230 			case LVN_ITEMCHANGED:
16231 				SmConnectionDlgUpdate(hWnd, p);
16232 				break;
16233 			}
16234 			break;
16235 		}
16236 		break;
16237 
16238 	case WM_CLOSE:
16239 		EndDialog(hWnd, false);
16240 		break;
16241 	}
16242 
16243 	LvStandardHandler(hWnd, msg, wParam, lParam, L_LIST);
16244 
16245 	return 0;
16246 }
16247 
16248 // Display the connection list
SmConnectionDlg(HWND hWnd,SM_SERVER * p)16249 void SmConnectionDlg(HWND hWnd, SM_SERVER *p)
16250 {
16251 	// Validate arguments
16252 	if (hWnd == NULL || p == NULL)
16253 	{
16254 		return;
16255 	}
16256 
16257 	Dialog(hWnd, D_SM_CONNECTION, SmConnectionDlgProc, p);
16258 }
16259 
16260 // Get the connection type string
SmGetConnectionTypeStr(UINT type)16261 wchar_t *SmGetConnectionTypeStr(UINT type)
16262 {
16263 	return GetConnectionTypeStr(type);
16264 }
16265 
16266 // Update the server information
SmRefreshServerInfo(HWND hWnd,SM_SERVER * p,void * param)16267 bool SmRefreshServerInfo(HWND hWnd, SM_SERVER *p, void *param)
16268 {
16269 	RPC_SERVER_INFO t;
16270 	LVB *b;
16271 	wchar_t tmp[MAX_SIZE];
16272 	// Validate arguments
16273 	if (hWnd == NULL || p == NULL)
16274 	{
16275 		return false;
16276 	}
16277 
16278 	Zero(&t, sizeof(t));
16279 	if (CALL(hWnd, ScGetServerInfo(p->Rpc, &t)) == false)
16280 	{
16281 		return false;
16282 	}
16283 
16284 	b = LvInsertStart();
16285 
16286 	// Product name
16287 	StrToUni(tmp, sizeof(tmp), t.ServerProductName);
16288 	LvInsertAdd(b, ICO_VPNSERVER, NULL, 2, _UU("SM_INFO_PRODUCT_NAME"), tmp);
16289 
16290 	// Version
16291 	StrToUni(tmp, sizeof(tmp), t.ServerVersionString);
16292 	LvInsertAdd(b, ICO_INFORMATION, NULL, 2, _UU("SM_INFO_VERSION"), tmp);
16293 
16294 	// Build
16295 	StrToUni(tmp, sizeof(tmp), t.ServerBuildInfoString);
16296 	LvInsertAdd(b, ICO_MACHINE, NULL, 2, _UU("SM_INFO_BUILD"), tmp);
16297 
16298 	// Host name
16299 	StrToUni(tmp, sizeof(tmp), t.ServerHostName);
16300 	LvInsertAdd(b, ICO_TOWER, NULL, 2, _UU("SM_INFO_HOSTNAME"), tmp);
16301 
16302 	// Type
16303 	LvInsertAdd(b, t.ServerType == SERVER_TYPE_STANDALONE ? ICO_SERVER_ONLINE : ICO_FARM, 0,
16304 		2, _UU("SM_ST_SERVER_TYPE"),
16305 		GetServerTypeStr(t.ServerType));
16306 
16307 	// OS
16308 	StrToUni(tmp, sizeof(tmp), t.OsInfo.OsSystemName);
16309 	LvInsertAdd(b, ICO_MACHINE, NULL, 2, _UU("SM_OS_SYSTEM_NAME"), tmp);
16310 
16311 	StrToUni(tmp, sizeof(tmp), t.OsInfo.OsProductName);
16312 	LvInsertAdd(b, ICO_MACHINE, NULL, 2, _UU("SM_OS_PRODUCT_NAME"), tmp);
16313 
16314 	if (t.OsInfo.OsServicePack != 0)
16315 	{
16316 		UniFormat(tmp, sizeof(tmp), _UU("SM_OS_SP_TAG"), t.OsInfo.OsServicePack);
16317 		LvInsertAdd(b, ICO_MACHINE, NULL, 2, _UU("SM_OS_SERVICE_PACK"), tmp);
16318 	}
16319 
16320 	StrToUni(tmp, sizeof(tmp), t.OsInfo.OsVendorName);
16321 	LvInsertAdd(b, ICO_MACHINE, NULL, 2, _UU("SM_OS_VENDER_NAME"), tmp);
16322 
16323 	StrToUni(tmp, sizeof(tmp), t.OsInfo.OsVersion);
16324 	LvInsertAdd(b, ICO_MACHINE, NULL, 2, _UU("SM_OS_VERSION"), tmp);
16325 
16326 	StrToUni(tmp, sizeof(tmp), t.OsInfo.KernelName);
16327 	LvInsertAdd(b, ICO_MACHINE, NULL, 2, _UU("SM_OS_KERNEL_NAME"), tmp);
16328 
16329 	StrToUni(tmp, sizeof(tmp), t.OsInfo.KernelVersion);
16330 	LvInsertAdd(b, ICO_MACHINE, NULL, 2, _UU("SM_OS_KERNEL_VERSION"), tmp);
16331 
16332 	SmAddServerCaps(b, p->CapsList);
16333 
16334 	LvInsertEnd(b, hWnd, L_STATUS);
16335 
16336 	FreeRpcServerInfo(&t);
16337 
16338 	return true;
16339 }
16340 
16341 // Display the Caps of the server on the screen
SmAddServerCaps(LVB * b,CAPSLIST * t)16342 void SmAddServerCaps(LVB *b, CAPSLIST *t)
16343 {
16344 	UINT i;
16345 	// Validate arguments
16346 	if (b == NULL || t == NULL)
16347 	{
16348 		return;
16349 	}
16350 
16351 	for (i = 0;i < LIST_NUM(t->CapsList);i++)
16352 	{
16353 		CAPS *c = LIST_DATA(t->CapsList, i);
16354 		wchar_t title[MAX_SIZE];
16355 		char name[256];
16356 
16357 		Format(name, sizeof(name), "CT_%s", c->Name);
16358 
16359 		UniStrCpy(title, sizeof(title), _UU(name));
16360 
16361 		if (UniIsEmptyStr(title))
16362 		{
16363 			UniFormat(title, sizeof(title), L"%S", (StrLen(c->Name) >= 2) ? c->Name + 2 : c->Name);
16364 		}
16365 
16366 		if (StartWith(c->Name, "b_"))
16367 		{
16368 			bool icon_pass = c->Value == 0 ? false : true;
16369 			if (StrCmpi(c->Name, "b_must_install_pcap") == 0)
16370 			{
16371 				// Invert only the item of WinPcap
16372 				icon_pass = !icon_pass;
16373 			}
16374 			LvInsertAdd(b, icon_pass == false ? ICO_DISCARD : ICO_PASS,
16375 				NULL, 2, title, c->Value == 0 ? _UU("CAPS_NO") : _UU("CAPS_YES"));
16376 		}
16377 		else
16378 		{
16379 			wchar_t str[64];
16380 			UniToStru(str, c->Value);
16381 			LvInsertAdd(b, ICO_PROTOCOL, NULL, 2, title, str);
16382 		}
16383 	}
16384 }
16385 
16386 // Update the server state
SmRefreshServerStatus(HWND hWnd,SM_SERVER * p,void * param)16387 bool SmRefreshServerStatus(HWND hWnd, SM_SERVER *p, void *param)
16388 {
16389 	RPC_SERVER_STATUS t;
16390 	LVB *b;
16391 	wchar_t tmp[MAX_SIZE];
16392 	char str[MAX_SIZE];
16393 	// Validate arguments
16394 	if (hWnd == NULL || p == NULL)
16395 	{
16396 		return false;
16397 	}
16398 
16399 	Zero(&t, sizeof(t));
16400 	if (CALL(hWnd, ScGetServerStatus(p->Rpc, &t)) == false)
16401 	{
16402 		return false;
16403 	}
16404 
16405 	b = LvInsertStart();
16406 
16407 	// Type of server
16408 	LvInsertAdd(b, t.ServerType == SERVER_TYPE_STANDALONE ? ICO_SERVER_ONLINE : ICO_FARM, 0,
16409 		2, _UU("SM_ST_SERVER_TYPE"),
16410 		GetServerTypeStr(t.ServerType));
16411 
16412 	// Number of TCP connections
16413 	UniToStru(tmp, t.NumTcpConnections);
16414 	LvInsertAdd(b, ICO_PROTOCOL, NULL, 2, _UU("SM_ST_NUM_TCP"), tmp);
16415 
16416 	if (t.ServerType == SERVER_TYPE_FARM_CONTROLLER)
16417 	{
16418 		// Number of Local TCP connections
16419 		UniToStru(tmp, t.NumTcpConnectionsLocal);
16420 		LvInsertAdd(b, ICO_PROTOCOL, NULL, 2, _UU("SM_ST_NUM_TCP_LOCAL"), tmp);
16421 
16422 		// Number of remote TCP connections
16423 		UniToStru(tmp, t.NumTcpConnectionsRemote);
16424 		LvInsertAdd(b, ICO_PROTOCOL, NULL, 2, _UU("SM_ST_NUM_TCP_REMOTE"), tmp);
16425 	}
16426 
16427 	// Number of Virtual HUBs
16428 	UniToStru(tmp, t.NumHubTotal);
16429 	LvInsertAdd(b, ICO_HUB, NULL, 2, _UU("SM_ST_NUM_HUB_TOTAL"), tmp);
16430 
16431 	if (t.ServerType != SERVER_TYPE_STANDALONE)
16432 	{
16433 		// Number of static HUBs
16434 		UniToStru(tmp, t.NumHubStatic);
16435 		LvInsertAdd(b, ICO_HUB, NULL, 2, _UU("SM_ST_NUM_HUB_STATIC"), tmp);
16436 
16437 		// Number of Dynamic HUBs
16438 		UniToStru(tmp, t.NumHubDynamic);
16439 		LvInsertAdd(b, ICO_HUB, NULL, 2, _UU("SM_ST_NUM_HUB_DYNAMIC"), tmp);
16440 	}
16441 
16442 	// Number of sessions
16443 	UniToStru(tmp, t.NumSessionsTotal);
16444 	LvInsertAdd(b, ICO_VPN, NULL, 2, _UU("SM_ST_NUM_SESSION_TOTAL"), tmp);
16445 
16446 	if (t.ServerType == SERVER_TYPE_FARM_CONTROLLER)
16447 	{
16448 		// Number of local sessions
16449 		UniToStru(tmp, t.NumSessionsLocal);
16450 		LvInsertAdd(b, ICO_VPN, NULL, 2, _UU("SM_ST_NUM_SESSION_LOCAL"), tmp);
16451 
16452 		// Number of local sessions
16453 		UniToStru(tmp, t.NumSessionsRemote);
16454 		LvInsertAdd(b, ICO_VPN, NULL, 2, _UU("SM_ST_NUM_SESSION_REMOTE"), tmp);
16455 	}
16456 
16457 	// Number of MAC table entries
16458 	UniToStru(tmp, t.NumMacTables);
16459 	LvInsertAdd(b, ICO_MACHINE, NULL, 2, _UU("SM_ST_NUM_MAC_TABLE"), tmp);
16460 
16461 	// Number of IP table entries
16462 	UniToStru(tmp, t.NumIpTables);
16463 	LvInsertAdd(b, ICO_MACHINE, NULL, 2, _UU("SM_ST_NUM_IP_TABLE"), tmp);
16464 
16465 	// Number of users
16466 	UniToStru(tmp, t.NumUsers);
16467 	LvInsertAdd(b, ICO_USER, NULL, 2, _UU("SM_ST_NUM_USERS"), tmp);
16468 
16469 	// Number of groups
16470 	UniToStru(tmp, t.NumGroups);
16471 	LvInsertAdd(b, ICO_GROUP, NULL, 2, _UU("SM_ST_NUM_GROUPS"), tmp);
16472 
16473 	// Number of assigned licenses
16474 	UniToStru(tmp, t.AssignedClientLicenses);
16475 	LvInsertAdd(b, ICO_CERT, NULL, 2, _UU("SM_ST_CLIENT_LICENSE"), tmp);
16476 	UniToStru(tmp, t.AssignedBridgeLicenses);
16477 	LvInsertAdd(b, ICO_CERT, NULL, 2, _UU("SM_ST_BRIDGE_LICENSE"), tmp);
16478 
16479 	if (t.ServerType == SERVER_TYPE_FARM_CONTROLLER)
16480 	{
16481 		UniToStru(tmp, t.AssignedClientLicensesTotal);
16482 		LvInsertAdd(b, ICO_CERT, NULL, 2, _UU("SM_ST_CLIENT_LICENSE_EX"), tmp);
16483 		UniToStru(tmp, t.AssignedBridgeLicensesTotal);
16484 		LvInsertAdd(b, ICO_CERT, NULL, 2, _UU("SM_ST_BRIDGE_LICENSE_EX"), tmp);
16485 	}
16486 
16487 	// Traffic
16488 	SmInsertTrafficInfo(b, &t.Traffic);
16489 
16490 	// Server start-up time
16491 	GetDateTimeStrEx64(tmp, sizeof(tmp), SystemToLocal64(t.StartTime), NULL);
16492 	LvInsertAdd(b, ICO_NULL, NULL, 2, _UU("SM_ST_START_TIME"), tmp);
16493 
16494 	// Current time
16495 	GetDateTimeStrMilli64(str, sizeof(str), SystemToLocal64(t.CurrentTime));
16496 	StrToUni(tmp, sizeof(tmp), str);
16497 	LvInsertAdd(b, ICO_NULL, NULL, 2, _UU("SM_ST_CURRENT_TIME"), tmp);
16498 
16499 	// Tick value
16500 	UniFormat(tmp, sizeof(tmp), L"%I64u", t.CurrentTick);
16501 	LvInsertAdd(b, ICO_NULL, NULL, 2, _UU("SM_ST_CURRENT_TICK"), tmp);
16502 
16503 	// Memory information
16504 	if (t.MemInfo.TotalMemory != 0)
16505 	{
16506 		char vv[128];
16507 
16508 		ToStr3(vv, sizeof(vv), t.MemInfo.TotalMemory);
16509 		UniFormat(tmp, sizeof(tmp), _UU("SM_ST_RAM_SIZE_KB"), vv);
16510 		LvInsertAdd(b, ICO_MEMORY, NULL, 2, _UU("SM_ST_TOTAL_MEMORY"), tmp);
16511 
16512 		ToStr3(vv, sizeof(vv), t.MemInfo.UsedMemory);
16513 		UniFormat(tmp, sizeof(tmp), _UU("SM_ST_RAM_SIZE_KB"), vv);
16514 		LvInsertAdd(b, ICO_MEMORY, NULL, 2, _UU("SM_ST_USED_MEMORY"), tmp);
16515 
16516 		ToStr3(vv, sizeof(vv), t.MemInfo.FreeMemory);
16517 		UniFormat(tmp, sizeof(tmp), _UU("SM_ST_RAM_SIZE_KB"), vv);
16518 		LvInsertAdd(b, ICO_MEMORY, NULL, 2, _UU("SM_ST_FREE_MEMORY"), tmp);
16519 
16520 		ToStr3(vv, sizeof(vv), t.MemInfo.TotalPhys);
16521 		UniFormat(tmp, sizeof(tmp), _UU("SM_ST_RAM_SIZE_KB"), vv);
16522 		LvInsertAdd(b, ICO_MEMORY, NULL, 2, _UU("SM_ST_TOTAL_PHYS"), tmp);
16523 
16524 		ToStr3(vv, sizeof(vv), t.MemInfo.UsedPhys);
16525 		UniFormat(tmp, sizeof(tmp), _UU("SM_ST_RAM_SIZE_KB"), vv);
16526 		LvInsertAdd(b, ICO_MEMORY, NULL, 2, _UU("SM_ST_USED_PHYS"), tmp);
16527 
16528 		ToStr3(vv, sizeof(vv), t.MemInfo.FreePhys);
16529 		UniFormat(tmp, sizeof(tmp), _UU("SM_ST_RAM_SIZE_KB"), vv);
16530 		LvInsertAdd(b, ICO_MEMORY, NULL, 2, _UU("SM_ST_FREE_PHYS"), tmp);
16531 	}
16532 
16533 	LvInsertEnd(b, hWnd, L_STATUS);
16534 
16535 	return true;
16536 }
16537 
16538 // Initialize
SmSaveKeyPairDlgInit(HWND hWnd,SM_SAVE_KEY_PAIR * s)16539 void SmSaveKeyPairDlgInit(HWND hWnd, SM_SAVE_KEY_PAIR *s)
16540 {
16541 	UINT current;
16542 	// Validate arguments
16543 	if (hWnd == NULL || s == NULL)
16544 	{
16545 		return;
16546 	}
16547 
16548 	current = MsRegReadInt(REG_CURRENT_USER, SM_REG_KEY, "SavePkcs12");
16549 
16550 	if (current == 1)
16551 	{
16552 		Check(hWnd, R_PKCS12, true);
16553 	}
16554 	else if (current == 2)
16555 	{
16556 		Check(hWnd, R_SECURE, true);
16557 	}
16558 	else
16559 	{
16560 		Check(hWnd, R_X509_AND_KEY, true);
16561 	}
16562 
16563 	if (MsIsWine())
16564 	{
16565 		Disable(hWnd, R_SECURE);
16566 	}
16567 
16568 	SmSaveKeyPairDlgUpdate(hWnd, s);
16569 }
16570 
16571 // Update
SmSaveKeyPairDlgUpdate(HWND hWnd,SM_SAVE_KEY_PAIR * s)16572 void SmSaveKeyPairDlgUpdate(HWND hWnd, SM_SAVE_KEY_PAIR *s)
16573 {
16574 	SECURE_DEVICE *dev;
16575 	bool ok = true;
16576 	wchar_t tmp[MAX_SIZE];
16577 	// Validate arguments
16578 	if (hWnd == NULL || s == NULL)
16579 	{
16580 		return;
16581 	}
16582 
16583 	dev = GetSecureDevice(SmGetCurrentSecureIdFromReg());
16584 	if (dev == NULL)
16585 	{
16586 		UniStrCpy(tmp, sizeof(tmp), _UU("SEC_CURRENT_NO_DEVICE"));
16587 	}
16588 	else
16589 	{
16590 		UniFormat(tmp, sizeof(tmp), _UU("SEC_CURRENT_DEVICE"), dev->DeviceName);
16591 	}
16592 
16593 	SetText(hWnd, S_INFO, tmp);
16594 
16595 	if (IsChecked(hWnd, R_USE_PASS))
16596 	{
16597 		char *s1, *s2;
16598 		s1 = GetTextA(hWnd, E_PASS1);
16599 		s2 = GetTextA(hWnd, E_PASS2);
16600 		if (StrCmp(s1, s2) != 0)
16601 		{
16602 			ok = false;
16603 		}
16604 		Free(s1);
16605 		Free(s2);
16606 	}
16607 
16608 	if (IsChecked(hWnd, R_SECURE))
16609 	{
16610 		if (dev == NULL)
16611 		{
16612 			ok = false;
16613 		}
16614 	}
16615 
16616 	SetEnable(hWnd, B_SELECT, IsChecked(hWnd, R_SECURE));
16617 	SetEnable(hWnd, B_SECURE_MANAGER, IsChecked(hWnd, R_SECURE));
16618 	SetEnable(hWnd, S_INFO, IsChecked(hWnd, R_SECURE));
16619 
16620 	SetEnable(hWnd, E_PASS1, IsChecked(hWnd, R_USE_PASS) && (IsChecked(hWnd, R_SECURE) == false));
16621 	SetEnable(hWnd, E_PASS2, IsChecked(hWnd, R_USE_PASS) && (IsChecked(hWnd, R_SECURE) == false));
16622 	SetEnable(hWnd, S_PASS1, IsChecked(hWnd, R_USE_PASS) && (IsChecked(hWnd, R_SECURE) == false));
16623 	SetEnable(hWnd, S_PASS2, IsChecked(hWnd, R_USE_PASS) && (IsChecked(hWnd, R_SECURE) == false));
16624 	SetEnable(hWnd, R_USE_PASS, (IsChecked(hWnd, R_SECURE) == false));
16625 	SetEnable(hWnd, S_PASS3, (IsChecked(hWnd, R_SECURE) == false));
16626 	SetEnable(hWnd, S_PASS4, (IsChecked(hWnd, R_SECURE) == false));
16627 
16628 	SetEnable(hWnd, IDOK, ok);
16629 }
16630 
16631 // [OK] button
SmSaveKeyPairDlgOnOk(HWND hWnd,SM_SAVE_KEY_PAIR * s)16632 void SmSaveKeyPairDlgOnOk(HWND hWnd, SM_SAVE_KEY_PAIR *s)
16633 {
16634 	UINT pkcs12;
16635 	char pass[MAX_SIZE];
16636 	char *password;
16637 	// Validate arguments
16638 	if (hWnd == NULL || s == NULL)
16639 	{
16640 		return;
16641 	}
16642 
16643 	pkcs12 = 0;
16644 
16645 	if (IsChecked(hWnd, R_PKCS12))
16646 	{
16647 		pkcs12 = 1;
16648 	}
16649 	else if (IsChecked(hWnd, R_SECURE))
16650 	{
16651 		pkcs12 = 2;
16652 	}
16653 	MsRegWriteInt(REG_CURRENT_USER, SM_REG_KEY, "SavePkcs12", pkcs12);
16654 
16655 	if (pkcs12 != 2)
16656 	{
16657 		GetTxtA(hWnd, E_PASS1, pass, sizeof(pass));
16658 
16659 		if (StrLen(pass) != 0)
16660 		{
16661 			password = pass;
16662 		}
16663 		else
16664 		{
16665 			password = NULL;
16666 		}
16667 
16668 		if (pkcs12 == false)
16669 		{
16670 			// Write to the X509 and KEY
16671 			wchar_t *x509_name, *key_name;
16672 			x509_name = SaveDlg(hWnd, _UU("DLG_CERT_FILES"), _UU("DLG_SAVE_CERT"), NULL, L".cer");
16673 			if (x509_name == NULL)
16674 			{
16675 				// Cancel
16676 				return;
16677 			}
16678 			else
16679 			{
16680 				wchar_t default_key_name[MAX_SIZE];
16681 				UniReplaceStrEx(default_key_name, sizeof(default_key_name), x509_name,
16682 					L".cer", L"", false);
16683 				UniReplaceStrEx(default_key_name, sizeof(default_key_name), default_key_name,
16684 								L".crt", L"", false);
16685 				UniStrCat(default_key_name, sizeof(default_key_name), L".key");
16686 				key_name = SaveDlg(hWnd, _UU("DLG_KEY_FILTER"), _UU("DLG_SAVE_KEY"),
16687 					default_key_name, L".key");
16688 				if (key_name == NULL)
16689 				{
16690 					// Cancel
16691 					Free(x509_name);
16692 					return;
16693 				}
16694 				else
16695 				{
16696 					bool ok = true;
16697 					wchar_t filename1[MAX_SIZE];
16698 					wchar_t filename2[MAX_SIZE];
16699 
16700 					UniStrCpy(filename1, sizeof(filename1), x509_name);
16701 					UniStrCpy(filename2, sizeof(filename2), key_name);
16702 
16703 					// Save the certificate
16704 					if (XToFileW(s->Cert, filename1, true) == false)
16705 					{
16706 						MsgBox(hWnd, MB_ICONSTOP, _UU("DLG_CERT_SAVE_ERROR"));
16707 						ok = false;
16708 					}
16709 					else
16710 					{
16711 						if (KToFileW(s->Key, filename2, true, password) == false)
16712 						{
16713 							MsgBox(hWnd, MB_ICONSTOP, _UU("DLG_KEY_SAVE_ERROR"));
16714 							ok = false;
16715 						}
16716 					}
16717 
16718 					if (ok)
16719 					{
16720 						MsgBox(hWnd, MB_ICONINFORMATION, _UU("DLG_KEY_PAIR_SAVE_OK"));
16721 						EndDialog(hWnd, true);
16722 					}
16723 
16724 					Free(key_name);
16725 				}
16726 				Free(x509_name);
16727 			}
16728 		}
16729 		else
16730 		{
16731 			// Write to the PKCS#12
16732 			wchar_t *name = SaveDlg(hWnd, _UU("DLG_PKCS12_FILTER"), _UU("DLG_SAVE_P12"), NULL, L".p12");
16733 			if (name == NULL)
16734 			{
16735 				// Cancel
16736 				return;
16737 			}
16738 			else
16739 			{
16740 				P12 *p12;
16741 				wchar_t filename[MAX_SIZE];
16742 				UniStrCpy(filename, sizeof(filename), name);
16743 
16744 				// Convert to PKCS#12
16745 				p12 = NewP12(s->Cert, s->Key, pass);
16746 				if (p12 == NULL)
16747 				{
16748 					// Failure
16749 					MsgBox(hWnd, MB_ICONSTOP, _UU("DLG_KEY_PAIR_SAVE_ERROR"));
16750 				}
16751 				else
16752 				{
16753 					// Save
16754 					if (P12ToFileW(p12, filename) == false)
16755 					{
16756 						// Failure
16757 						MsgBox(hWnd, MB_ICONSTOP, _UU("DLG_KEY_PAIR_SAVE_ERROR"));
16758 					}
16759 					else
16760 					{
16761 						// Success
16762 						MsgBox(hWnd, MB_ICONINFORMATION, _UU("DLG_KEY_PAIR_SAVE_OK"));
16763 						EndDialog(hWnd, true);
16764 					}
16765 					FreeP12(p12);
16766 				}
16767 
16768 				Free(name);
16769 			}
16770 		}
16771 	}
16772 	else
16773 	{
16774 		char default_name[MAX_SIZE];
16775 		char *object_name;
16776 		bool ok = false;
16777 		X *x;
16778 		K *k;
16779 		WINUI_SECURE_BATCH batch[] =
16780 		{
16781 			{WINUI_SECURE_WRITE_CERT, NULL, true, NULL, NULL, NULL, NULL, NULL, NULL},
16782 			{WINUI_SECURE_WRITE_KEY, NULL, true, NULL, NULL, NULL, NULL, NULL, NULL},
16783 		};
16784 
16785 		x = s->Cert;
16786 		k = s->Key;
16787 
16788 		// Generate the default name
16789 		GetPrintNameFromXA(default_name, sizeof(default_name), x);
16790 		ConvertSafeFileName(default_name, sizeof(default_name), default_name);
16791 
16792 		object_name = StringDlgA(hWnd, _UU("SEC_OBJECT_NAME_TITLE"),
16793 			_UU("SEC_OBJECT_NAME_INFO"), default_name, ICO_CERT, false, false);
16794 
16795 		if (object_name != NULL)
16796 		{
16797 			// Write and enumerate
16798 			batch[0].InputX = x;
16799 			batch[0].Name = object_name;
16800 			batch[1].InputK = k;
16801 			batch[1].Name = object_name;
16802 
16803 			if (SecureDeviceWindow(hWnd, batch, sizeof(batch) / sizeof(batch[0]), SmGetCurrentSecureIdFromReg(), 0) == false)
16804 			{
16805 				// Failure
16806 			}
16807 			else
16808 			{
16809 				ok = true;
16810 			}
16811 
16812 			Free(object_name);
16813 		}
16814 
16815 		if (ok)
16816 		{
16817 			MsgBox(hWnd, MB_ICONINFORMATION, _UU("SEC_NEW_CERT_IMPORT_OK"));
16818 
16819 			EndDialog(hWnd, true);
16820 		}
16821 	}
16822 }
16823 
16824 // Saving dialog box of the certificate and private key
SmSaveKeyPairDlgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)16825 UINT SmSaveKeyPairDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
16826 {
16827 	SM_SAVE_KEY_PAIR *s = (SM_SAVE_KEY_PAIR *)param;
16828 	// Validate arguments
16829 	if (hWnd == NULL)
16830 	{
16831 		return 0;
16832 	}
16833 
16834 	switch (msg)
16835 	{
16836 	case WM_INITDIALOG:
16837 		// Initialize
16838 		SmSaveKeyPairDlgInit(hWnd, s);
16839 		break;
16840 
16841 	case WM_COMMAND:
16842 		switch (LOWORD(wParam))
16843 		{
16844 		case E_PASS1:
16845 		case E_PASS2:
16846 		case R_USE_PASS:
16847 		case R_SECURE:
16848 		case R_X509_AND_KEY:
16849 		case R_PKCS12:
16850 			SmSaveKeyPairDlgUpdate(hWnd, s);
16851 			break;
16852 		}
16853 
16854 		switch (wParam)
16855 		{
16856 		case IDOK:
16857 			// [OK] button
16858 			SmSaveKeyPairDlgOnOk(hWnd, s);
16859 			break;
16860 
16861 		case IDCANCEL:
16862 			// Cancel button
16863 			Close(hWnd);
16864 			break;
16865 
16866 		case R_USE_PASS:
16867 			if (IsChecked(hWnd, R_USE_PASS))
16868 			{
16869 				FocusEx(hWnd, E_PASS1);
16870 			}
16871 			break;
16872 
16873 		case B_SELECT:
16874 			SmSelectSecureId(hWnd);
16875 			SmSaveKeyPairDlgUpdate(hWnd, s);
16876 			break;
16877 
16878 		case B_SECURE_MANAGER:
16879 			CmSecureManagerEx(hWnd, SmGetCurrentSecureId(hWnd), true);
16880 			SmSaveKeyPairDlgUpdate(hWnd, s);
16881 			break;
16882 		}
16883 		break;
16884 
16885 	case WM_CLOSE:
16886 		EndDialog(hWnd, false);
16887 		break;
16888 	}
16889 
16890 	return 0;
16891 }
16892 
16893 // Save the certificate and private key
SmSaveKeyPairDlg(HWND hWnd,X * x,K * k)16894 bool SmSaveKeyPairDlg(HWND hWnd, X *x, K *k)
16895 {
16896 	SM_SAVE_KEY_PAIR s;
16897 	// Validate arguments
16898 	if (hWnd == NULL || x == NULL || k == NULL)
16899 	{
16900 		return false;
16901 	}
16902 
16903 	Zero(&s, sizeof(s));
16904 	s.Cert = x;
16905 	s.Key = k;
16906 
16907 	return Dialog(hWnd,	D_SM_SAVE_KEY_PAIR, SmSaveKeyPairDlgProc, &s);
16908 }
16909 
16910 // OK is clicked on the SSL related dialog
SmSslDlgOnOk(HWND hWnd,SM_SSL * s)16911 void SmSslDlgOnOk(HWND hWnd, SM_SSL *s)
16912 {
16913 	char *name;
16914 	RPC_KEEP t;
16915 	// Validate arguments
16916 	if (hWnd == NULL || s == NULL)
16917 	{
16918 		return;
16919 	}
16920 
16921 	if (s->p->ServerAdminMode == false)
16922 	{
16923 		EndDialog(hWnd, false);
16924 		return;
16925 	}
16926 
16927 	name = GetTextA(hWnd, C_CIPHER);
16928 	if (name == NULL)
16929 	{
16930 		return;
16931 	}
16932 	else
16933 	{
16934 		RPC_STR t;
16935 		Zero(&t, sizeof(t));
16936 		t.String = name;
16937 
16938 		// Set the encryption algorithm
16939 		if (CALL(hWnd, ScSetServerCipher(s->p->Rpc, &t)) == false)
16940 		{
16941 			Focus(hWnd, C_CIPHER);
16942 			return;
16943 		}
16944 		FreeRpcStr(&t);
16945 	}
16946 
16947 	if (s->SetCertAndKey)
16948 	{
16949 		// Set the certificate
16950 		RPC_KEY_PAIR t;
16951 		Zero(&t, sizeof(t));
16952 
16953 		t.Cert = CloneX(s->Cert);
16954 		t.Key = CloneK(s->Key);
16955 
16956 		if (CALL(hWnd, ScSetServerCert(s->p->Rpc, &t)) == false)
16957 		{
16958 			return;
16959 		}
16960 
16961 		if (t.Flag1 == 0)
16962 		{
16963 			// Show the warning message
16964 			MsgBox(hWnd, MB_ICONWARNING, _UU("SM_CERT_NEED_ROOT"));
16965 		}
16966 
16967 		FreeRpcKeyPair(&t);
16968 
16969 		MsgBox(hWnd, MB_ICONINFORMATION, _UU("CM_CERT_SET_MSG"));
16970 	}
16971 
16972 	Zero(&t, sizeof(t));
16973 	t.UseKeepConnect = IsChecked(hWnd, R_USE_KEEP_CONNECT);
16974 	GetTxtA(hWnd, E_HOSTNAME, t.KeepConnectHost, sizeof(t.KeepConnectHost));
16975 	t.KeepConnectPort = GetInt(hWnd, E_PORT);
16976 	t.KeepConnectInterval = GetInt(hWnd, E_INTERVAL);
16977 	t.KeepConnectProtocol = IsChecked(hWnd, R_UDP) ? 1 : 0;
16978 
16979 	CALL(hWnd, ScSetKeep(s->p->Rpc, &t));
16980 
16981 	if (GetCapsBool(s->p->CapsList, "b_support_syslog"))
16982 	{
16983 		if (s->p->ServerAdminMode)
16984 		{
16985 			SYSLOG_SETTING set;
16986 
16987 			Zero(&set, sizeof(set));
16988 			GetTxtA(hWnd, E_SYSLOG_HOSTNAME, set.Hostname, sizeof(set.Hostname));
16989 			set.Port = GetInt(hWnd, E_SYSLOG_PORT);
16990 			set.SaveType = CbGetSelect(hWnd, C_SYSLOG);
16991 
16992 			if (CALL(hWnd, ScSetSysLog(s->p->Rpc, &set)) == false)
16993 			{
16994 				return;
16995 			}
16996 		}
16997 	}
16998 
16999 	EndDialog(hWnd, true);
17000 }
17001 
17002 // SSL related dialog initialization
SmSslDlgInit(HWND hWnd,SM_SSL * s)17003 void SmSslDlgInit(HWND hWnd, SM_SSL *s)
17004 {
17005 	UINT i;
17006 	TOKEN_LIST *cipher_list;
17007 	RPC_KEEP t;
17008 	bool private_key_exportable = false;
17009 	// Validate arguments
17010 	if (hWnd == NULL || s == NULL)
17011 	{
17012 		return;
17013 	}
17014 
17015 	// Set the encryption algorithm list
17016 	cipher_list = GetCipherList();
17017 	SetFont(hWnd, C_CIPHER, GetFont("Tahoma", 8, false, false, false, false));
17018 	CbSetHeight(hWnd, C_CIPHER, 18);
17019 	for (i = 0;i < cipher_list->NumTokens;i++)
17020 	{
17021 		wchar_t tmp[MAX_SIZE];
17022 		char *name = cipher_list->Token[i];
17023 		StrToUni(tmp, sizeof(tmp), name);
17024 		CbAddStr(hWnd, C_CIPHER, tmp, 0);
17025 	}
17026 
17027 	if (s->p != NULL)
17028 	{
17029 		// Get the encryption algorithm name from the server
17030 		RPC_STR t;
17031 		Zero(&t, sizeof(t));
17032 		if (CALL(hWnd, ScGetServerCipher(s->p->Rpc, &t)))
17033 		{
17034 			wchar_t tmp[MAX_SIZE];
17035 			StrToUni(tmp, sizeof(tmp), t.String);
17036 			SetText(hWnd, C_CIPHER, tmp);
17037 			FreeRpcStr(&t);
17038 		}
17039 		else
17040 		{
17041 			EndDialog(hWnd, 0);
17042 			return;
17043 		}
17044 	}
17045 
17046 	if (s->p != NULL)
17047 	{
17048 		wchar_t tmp[MAX_SIZE];
17049 		// Get the SSL certificate and private key from the server
17050 		RPC_KEY_PAIR t;
17051 		s->SetCertAndKey = false;
17052 		Zero(&t, sizeof(t));
17053 		if (CALL(hWnd, ScGetServerCert(s->p->Rpc, &t)))
17054 		{
17055 			// Copy the certificate and key
17056 			s->Cert = CloneX(t.Cert);
17057 			s->Key = CloneK(t.Key);
17058 
17059 			if (t.Key != NULL)
17060 			{
17061 				private_key_exportable = true;
17062 			}
17063 
17064 			FreeRpcKeyPair(&t);
17065 		}
17066 		else
17067 		{
17068 			EndDialog(hWnd, 0);
17069 			return;
17070 		}
17071 
17072 		// Show the Certificate Information
17073 		SmGetCertInfoStr(tmp, sizeof(tmp), s->Cert);
17074 		SetText(hWnd, S_CERT_INFO, tmp);
17075 	}
17076 
17077 	// Password change
17078 	SetEnable(hWnd, B_PASSWORD, s->p->ServerAdminMode);
17079 	SetEnable(hWnd, S_INFO4, s->p->ServerAdminMode);
17080 
17081 	// Enable / disable the button
17082 	SetEnable(hWnd, B_IMPORT, s->p->ServerAdminMode);
17083 	SetEnable(hWnd, B_EXPORT, s->p->ServerAdminMode && private_key_exportable);
17084 	SetEnable(hWnd, B_REGENERATE, s->p->ServerAdminMode);
17085 	SetEnable(hWnd, R_USE_KEEP_CONNECT, s->p->ServerAdminMode);
17086 	SetEnable(hWnd, B_UPDATE_CONFIG, s->p->Update != NULL);
17087 
17088 	if (s->p->ServerAdminMode && GetCapsBool(s->p->CapsList, "b_support_special_listener"))
17089 	{
17090 		SetEnable(hWnd, B_SPECIALLISTENER, true);
17091 		SetEnable(hWnd, S_INFO5, true);
17092 	}
17093 	else
17094 	{
17095 		SetEnable(hWnd, B_SPECIALLISTENER, false);
17096 		SetEnable(hWnd, S_INFO5, false);
17097 	}
17098 
17099 	if (s->p->ServerAdminMode == false)
17100 	{
17101 		Disable(hWnd, C_CIPHER);
17102 	}
17103 
17104 	if (CALL(hWnd, ScGetKeep(s->p->Rpc, &t)))
17105 	{
17106 		Check(hWnd, R_USE_KEEP_CONNECT, t.UseKeepConnect);
17107 		SetTextA(hWnd, E_HOSTNAME, t.KeepConnectHost);
17108 		SetIntEx(hWnd, E_PORT, t.KeepConnectPort);
17109 		SetInt(hWnd, E_INTERVAL, t.KeepConnectInterval);
17110 		Check(hWnd, R_TCP, t.KeepConnectProtocol == 0);
17111 		Check(hWnd, R_UDP, t.KeepConnectProtocol != 0);
17112 	}
17113 
17114 	CbSetHeight(hWnd, C_SYSLOG, 18);
17115 	CbReset(hWnd, C_SYSLOG);
17116 	CbAddStr(hWnd, C_SYSLOG, _UU("SM_SYSLOG_0"), SYSLOG_NONE);
17117 	CbAddStr(hWnd, C_SYSLOG, _UU("SM_SYSLOG_1"), SYSLOG_SERVER_LOG);
17118 	CbAddStr(hWnd, C_SYSLOG, _UU("SM_SYSLOG_2"), SYSLOG_SERVER_AND_HUB_SECURITY_LOG);
17119 	CbAddStr(hWnd, C_SYSLOG, _UU("SM_SYSLOG_3"), SYSLOG_SERVER_AND_HUB_ALL_LOG);
17120 
17121 	if (GetCapsBool(s->p->CapsList, "b_support_syslog"))
17122 	{
17123 		SYSLOG_SETTING set;
17124 
17125 		SetEnable(hWnd, C_SYSLOG, s->p->ServerAdminMode);
17126 		SetEnable(hWnd, E_SYSLOG_HOSTNAME, s->p->ServerAdminMode);
17127 		SetEnable(hWnd, E_SYSLOG_PORT, s->p->ServerAdminMode);
17128 		SetEnable(hWnd, S_01, s->p->ServerAdminMode);
17129 		SetEnable(hWnd, S_02, s->p->ServerAdminMode);
17130 
17131 		Zero(&set, sizeof(set));
17132 
17133 		if (CALL(hWnd, ScGetSysLog(s->p->Rpc, &set)))
17134 		{
17135 			SetTextA(hWnd, E_SYSLOG_HOSTNAME, set.Hostname);
17136 			SetInt(hWnd, E_SYSLOG_PORT, set.Port == 0 ? SYSLOG_PORT : set.Port);
17137 			CbSelect(hWnd, C_SYSLOG, set.SaveType);
17138 		}
17139 	}
17140 	else
17141 	{
17142 		Disable(hWnd, C_SYSLOG);
17143 		Disable(hWnd, E_SYSLOG_HOSTNAME);
17144 		Disable(hWnd, E_SYSLOG_PORT);
17145 		Disable(hWnd, S_01);
17146 		Disable(hWnd, S_02);
17147 	}
17148 
17149 	SmSslDlgUpdate(hWnd, s);
17150 }
17151 
17152 // SSL related dialog control update
SmSslDlgUpdate(HWND hWnd,SM_SSL * s)17153 void SmSslDlgUpdate(HWND hWnd, SM_SSL *s)
17154 {
17155 	bool ok = true;
17156 	bool b;
17157 	// Validate arguments
17158 	if (hWnd == NULL || s == NULL)
17159 	{
17160 		return;
17161 	}
17162 
17163 	if (IsChecked(hWnd, R_USE_KEEP_CONNECT))
17164 	{
17165 		UINT i;
17166 		b = true;
17167 		if (IsEmpty(hWnd, E_HOSTNAME))
17168 		{
17169 			ok = false;
17170 		}
17171 		i = GetInt(hWnd, E_PORT);
17172 		if (i == 0 || i >= 65536)
17173 		{
17174 			ok = false;
17175 		}
17176 		i = GetInt(hWnd, E_INTERVAL);
17177 		if (i < 5 || i > 600)
17178 		{
17179 			ok = false;
17180 		}
17181 	}
17182 	else
17183 	{
17184 		b = false;
17185 	}
17186 
17187 	if (IsEnable(hWnd, C_SYSLOG))
17188 	{
17189 		UINT i = CbGetSelect(hWnd, C_SYSLOG);
17190 
17191 		SetEnable(hWnd, E_SYSLOG_HOSTNAME, i != SYSLOG_NONE);
17192 		SetEnable(hWnd, E_SYSLOG_PORT, i != SYSLOG_NONE);
17193 		SetEnable(hWnd, S_01, i != SYSLOG_NONE);
17194 		SetEnable(hWnd, S_02, i != SYSLOG_NONE);
17195 	}
17196 
17197 	SetEnable(hWnd, S_HOSTNAME, b);
17198 	SetEnable(hWnd, E_HOSTNAME, b);
17199 	SetEnable(hWnd, S_PORT, b);
17200 	SetEnable(hWnd, E_PORT, b);
17201 	SetEnable(hWnd, S_INTERVAL, b);
17202 	SetEnable(hWnd, E_INTERVAL, b);
17203 	SetEnable(hWnd, S_INTERVAL2, b);
17204 	SetEnable(hWnd, S_PROTOCOL, b);
17205 	SetEnable(hWnd, R_TCP, b);
17206 	SetEnable(hWnd, R_UDP, b);
17207 	SetEnable(hWnd, S_INFO, b);
17208 
17209 	SetEnable(hWnd, IDOK, ok);
17210 }
17211 
17212 // Get the certificate information string
SmGetCertInfoStr(wchar_t * str,UINT size,X * x)17213 void SmGetCertInfoStr(wchar_t *str, UINT size, X *x)
17214 {
17215 	wchar_t subject[MAX_SIZE];
17216 	wchar_t issuer[MAX_SIZE];
17217 	wchar_t date[MAX_SIZE];
17218 	// Validate arguments
17219 	if (x == NULL || str == NULL)
17220 	{
17221 		if (str != NULL)
17222 		{
17223 			str[0] = 0;
17224 		}
17225 		return;
17226 	}
17227 
17228 	GetPrintNameFromName(subject, sizeof(subject), x->subject_name);
17229 	GetPrintNameFromName(issuer, sizeof(issuer), x->issuer_name);
17230 	GetDateStrEx64(date, sizeof(date), x->notAfter, NULL);
17231 
17232 	UniFormat(str, size, _UU("CM_CERT_INFO"), subject, issuer, date);
17233 }
17234 
17235 // Regenerate the server certificate
SmRegenerateServerCert(HWND hWnd,SM_SERVER * server,char * default_cn,X ** x,K ** k,bool root_only)17236 bool SmRegenerateServerCert(HWND hWnd, SM_SERVER *server, char *default_cn, X **x, K **k, bool root_only)
17237 {
17238 	char defcn[MAX_SIZE];
17239 	// Validate arguments
17240 	if (server == NULL || x == NULL || k == NULL)
17241 	{
17242 		return false;
17243 	}
17244 
17245 	Zero(defcn, sizeof(defcn));
17246 	if (IsEmptyStr(default_cn) == false)
17247 	{
17248 		StrCpy(defcn, sizeof(defcn), default_cn);
17249 	}
17250 
17251 	if (IsEmptyStr(defcn))
17252 	{
17253 		// If default CN is not specified, copy from the setting of the DDNS server
17254 		DDNS_CLIENT_STATUS t;
17255 
17256 		Zero(&t, sizeof(t));
17257 
17258 		if (ScGetDDnsClientStatus(server->Rpc, &t) == ERR_NO_ERROR)
17259 		{
17260 			if (IsEmptyStr(t.CurrentFqdn) == false)
17261 			{
17262 				StrCpy(defcn, sizeof(defcn), t.CurrentFqdn);
17263 			}
17264 		}
17265 	}
17266 
17267 	if (IsEmptyStr(defcn))
17268 	{
17269 		// Copy from the certificate information of the current server
17270 		RPC_KEY_PAIR t;
17271 
17272 		Zero(&t, sizeof(t));
17273 
17274 		if (ScGetServerCert(server->Rpc, &t) == ERR_NO_ERROR)
17275 		{
17276 			if (t.Cert != NULL)
17277 			{
17278 				if (t.Cert->subject_name != NULL)
17279 				{
17280 					UniToStr(defcn, sizeof(defcn), t.Cert->subject_name->CommonName);
17281 				}
17282 			}
17283 
17284 			FreeRpcKeyPair(&t);
17285 		}
17286 	}
17287 
17288 	if (IsEmptyStr(defcn))
17289 	{
17290 		// Copy from the destination server name of the current connection settings
17291 		StrCpy(defcn, sizeof(defcn), server->ServerName);
17292 	}
17293 
17294 	// Create a new certificate in the Certificate Creation Tool
17295 	if (SmCreateCert(hWnd, x, k, true, defcn, root_only) == false)
17296 	{
17297 		return false;
17298 	}
17299 
17300 	return true;
17301 }
17302 
17303 // SSL related dialog procedure
SmSslDlgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)17304 UINT SmSslDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
17305 {
17306 	SM_SSL *s = (SM_SSL *)param;
17307 	X *x;
17308 	K *k;
17309 	// Validate arguments
17310 	if (hWnd == NULL)
17311 	{
17312 		return 0;
17313 	}
17314 
17315 	switch (msg)
17316 	{
17317 	case WM_INITDIALOG:
17318 		// Initialize
17319 		SmSslDlgInit(hWnd, s);
17320 		break;
17321 
17322 	case WM_COMMAND:
17323 		switch (LOWORD(wParam))
17324 		{
17325 		case R_USE_KEEP_CONNECT:
17326 		case E_HOSTNAME:
17327 		case E_PORT:
17328 		case E_INTERVAL:
17329 		case R_TCP:
17330 		case R_UDP:
17331 		case C_SYSLOG:
17332 		case E_SYSLOG_HOSTNAME:
17333 		case E_SYSLOG_PORT:
17334 			SmSslDlgUpdate(hWnd, s);
17335 			break;
17336 		}
17337 
17338 		switch (wParam)
17339 		{
17340 		case IDOK:
17341 			// [OK] button
17342 			SmSslDlgOnOk(hWnd, s);
17343 			break;
17344 
17345 		case IDCANCEL:
17346 			// Cancel button
17347 			Close(hWnd);
17348 			break;
17349 
17350 		case B_UPDATE_CONFIG:
17351 			// Update notification setting
17352 			ConfigUpdateUi(s->p->Update, hWnd);
17353 			break;
17354 
17355 		case B_IMPORT:
17356 			// Import
17357 			if (CmLoadXAndK(hWnd, &x, &k))
17358 			{
17359 				wchar_t tmp[MAX_SIZE];
17360 
17361 LABEL_APPLY_NEW_CERT:
17362 				FreeX(s->Cert);
17363 				FreeK(s->Key);
17364 				s->Cert = x;
17365 				s->Key = k;
17366 				s->SetCertAndKey = true;
17367 				// Show the Certificate Information
17368 				SmGetCertInfoStr(tmp, sizeof(tmp), s->Cert);
17369 				SetText(hWnd, S_CERT_INFO, tmp);
17370 			}
17371 			break;
17372 
17373 		case B_EXPORT:
17374 			// Export
17375 			SmSaveKeyPairDlg(hWnd, s->Cert, s->Key);
17376 			break;
17377 
17378 		case B_VIEW:
17379 			// Show the certificate
17380 			CertDlg(hWnd, s->Cert, NULL, true);
17381 			break;
17382 
17383 		case B_SPECIALLISTENER:
17384 			// Special listener configuration
17385 			SmSpecialListener(hWnd, s->p);
17386 			break;
17387 
17388 		case B_REGENERATE:
17389 			// Regenerating the certificate
17390 			if (SmRegenerateServerCert(hWnd, s->p, NULL, &x, &k, false))
17391 			{
17392 				// Confirmation message
17393 				if (MsgBox(hWnd, MB_ICONEXCLAMATION | MB_YESNO, _UU("SM_REGENERATE_CERT_MSG")) == IDYES)
17394 				{
17395 					goto LABEL_APPLY_NEW_CERT;
17396 				}
17397 				else
17398 				{
17399 					FreeX(x);
17400 					FreeK(k);
17401 				}
17402 			}
17403 			break;
17404 
17405 		case B_PASSWORD:
17406 			// Password change
17407 			Dialog(hWnd, D_SM_CHANGE_PASSWORD, SmChangeServerPasswordDlg, s->p);
17408 			break;
17409 
17410 		case R_USE_KEEP_CONNECT:
17411 			if (IsChecked(hWnd, R_USE_KEEP_CONNECT))
17412 			{
17413 				FocusEx(hWnd, E_HOSTNAME);
17414 			}
17415 			break;
17416 		}
17417 		break;
17418 
17419 	case WM_CLOSE:
17420 		EndDialog(hWnd, false);
17421 		break;
17422 	}
17423 
17424 	return 0;
17425 }
17426 
17427 // Display the SSL related dialog
SmSslDlg(HWND hWnd,SM_SERVER * p)17428 void SmSslDlg(HWND hWnd, SM_SERVER *p)
17429 {
17430 	SM_SSL s;
17431 	// Validate arguments
17432 	if (hWnd == NULL || p == NULL)
17433 	{
17434 		return;
17435 	}
17436 
17437 	Zero(&s, sizeof(s));
17438 	s.p = p;
17439 
17440 	Dialog(hWnd, D_SM_SSL, SmSslDlgProc, &s);
17441 
17442 	// Cleanup
17443 	FreeX(s.Cert);
17444 	FreeK(s.Key);
17445 }
17446 
17447 // Listener creation dialog procedure
SmCreateListenerDlgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)17448 UINT SmCreateListenerDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
17449 {
17450 	UINT port;
17451 	RPC_LISTENER t;
17452 	SM_SERVER *p = (SM_SERVER *)param;
17453 	// Validate arguments
17454 	if (hWnd == NULL)
17455 	{
17456 		return 0;
17457 	}
17458 
17459 	switch (msg)
17460 	{
17461 	case WM_INITDIALOG:
17462 		Focus(hWnd, E_PORT);
17463 		Disable(hWnd, IDOK);
17464 		break;
17465 
17466 	case WM_COMMAND:
17467 		switch (LOWORD(wParam))
17468 		{
17469 		case E_PORT:
17470 			port = GetInt(hWnd, E_PORT);
17471 			if (port == 0 || port >= 65536)
17472 			{
17473 				Disable(hWnd, IDOK);
17474 			}
17475 			else
17476 			{
17477 				Enable(hWnd, IDOK);
17478 			}
17479 			break;
17480 		}
17481 
17482 		switch (wParam)
17483 		{
17484 		case IDOK:
17485 			port = GetInt(hWnd, E_PORT);
17486 			Zero(&t, sizeof(t));
17487 			t.Enable = true;
17488 			t.Port = port;
17489 			if (CALL(hWnd, ScCreateListener(p->Rpc, &t)))
17490 			{
17491 				EndDialog(hWnd, true);
17492 			}
17493 			break;
17494 		case IDCANCEL:
17495 			Close(hWnd);
17496 		}
17497 		break;
17498 
17499 	case WM_CLOSE:
17500 		EndDialog(hWnd, false);
17501 		break;
17502 	}
17503 
17504 	return 0;
17505 }
17506 
17507 // Listener creation dialog
SmCreateListenerDlg(HWND hWnd,SM_SERVER * p)17508 bool SmCreateListenerDlg(HWND hWnd, SM_SERVER *p)
17509 {
17510 	// Validate arguments
17511 	if (hWnd == NULL || p == NULL)
17512 	{
17513 		return false;
17514 	}
17515 
17516 	return Dialog(hWnd, D_SM_CREATE_LISTENER, SmCreateListenerDlgProc, p);
17517 }
17518 
17519 // HUB edit OK button
SmEditHubOnOk(HWND hWnd,SM_EDIT_HUB * s)17520 void SmEditHubOnOk(HWND hWnd, SM_EDIT_HUB *s)
17521 {
17522 	RPC_CREATE_HUB t;
17523 	char pass1[MAX_SIZE];
17524 	char pass2[MAX_SIZE];
17525 	char hubname[MAX_HUBNAME_LEN + 1];
17526 	// Validate arguments
17527 	if (hWnd == NULL || s == NULL)
17528 	{
17529 		return;
17530 	}
17531 
17532 	Zero(&t, sizeof(t));
17533 	if (s->EditMode)
17534 	{
17535 		StrCpy(hubname, sizeof(hubname), s->HubName);
17536 		StrCpy(t.HubName, sizeof(t.HubName), s->HubName);
17537 	}
17538 	else
17539 	{
17540 		GetTxtA(hWnd, E_HUBNAME, t.HubName, sizeof(t.HubName));
17541 		StrCpy(hubname, sizeof(hubname), t.HubName);
17542 	}
17543 
17544 	GetTxtA(hWnd, E_PASSWORD1, pass1, sizeof(pass1));
17545 	GetTxtA(hWnd, E_PASSWORD2, pass2, sizeof(pass2));
17546 
17547 	if (s->EditMode == false || StrCmp(pass1, HIDDEN_PASSWORD) != 0)
17548 	{
17549 		Hash(t.HashedPassword, pass1, StrLen(pass1), true);
17550 		HashPassword(t.SecurePassword, ADMINISTRATOR_USERNAME, pass1);
17551 	}
17552 
17553 	if (IsChecked(hWnd, R_LIMIT_MAX_SESSION))
17554 	{
17555 		t.HubOption.MaxSession = GetInt(hWnd, E_MAX_SESSION);
17556 	}
17557 
17558 	t.Online = IsChecked(hWnd, R_ONLINE);
17559 
17560 	if (s->p->ServerType == SERVER_TYPE_FARM_CONTROLLER)
17561 	{
17562 		t.HubType = HUB_TYPE_FARM_STATIC;
17563 		if (IsChecked(hWnd, R_DYNAMIC))
17564 		{
17565 			t.HubType = HUB_TYPE_FARM_DYNAMIC;
17566 		}
17567 	}
17568 
17569 	t.HubOption.NoEnum = IsChecked(hWnd, R_NO_ENUM);
17570 
17571 	if (s->EditMode == false)
17572 	{
17573 		if (CALL(hWnd, ScCreateHub(s->p->Rpc, &t)))
17574 		{
17575 			MsgBoxEx(hWnd, MB_ICONINFORMATION, _UU("CM_EDIT_HUB_CREATER"), hubname);
17576 			EndDialog(hWnd, true);
17577 		}
17578 	}
17579 	else
17580 	{
17581 		if (CALL(hWnd, ScSetHub(s->p->Rpc, &t)))
17582 		{
17583 			EndDialog(hWnd, true);
17584 		}
17585 	}
17586 }
17587 
17588 // HUB editing update
SmEditHubUpdate(HWND hWnd,SM_EDIT_HUB * s)17589 void SmEditHubUpdate(HWND hWnd, SM_EDIT_HUB *s)
17590 {
17591 	bool ok = true;
17592 	char *s1, *s2;
17593 	char hubname[MAX_HUBNAME_LEN + 1];
17594 	// Validate arguments
17595 	if (hWnd == NULL || s == NULL)
17596 	{
17597 		return;
17598 	}
17599 
17600 	s1 = GetTextA(hWnd, E_PASSWORD1);
17601 	s2 = GetTextA(hWnd, E_PASSWORD2);
17602 	if (StrCmp(s1, s2) != 0)
17603 	{
17604 		ok = false;
17605 	}
17606 	Free(s1);
17607 	Free(s2);
17608 
17609 	GetTxtA(hWnd, E_HUBNAME, hubname, sizeof(hubname));
17610 	Trim(hubname);
17611 	if (StrLen(hubname) == 0 ||
17612 		IsSafeStr(hubname) == false)
17613 	{
17614 		ok = false;
17615 	}
17616 
17617 	if (IsChecked(hWnd, R_LIMIT_MAX_SESSION))
17618 	{
17619 		Enable(hWnd, E_MAX_SESSION);
17620 		Enable(hWnd, S_MAX_SESSION_1);
17621 		Enable(hWnd, S_MAX_SESSION_2);
17622 		if (GetInt(hWnd, E_MAX_SESSION) == 0)
17623 		{
17624 			ok = false;
17625 		}
17626 	}
17627 	else
17628 	{
17629 		Disable(hWnd, E_MAX_SESSION);
17630 		Disable(hWnd, S_MAX_SESSION_1);
17631 		Disable(hWnd, S_MAX_SESSION_2);
17632 	}
17633 
17634 	SetEnable(hWnd, IDOK, ok);
17635 }
17636 
17637 // HUB editing initialization
SmEditHubInit(HWND hWnd,SM_EDIT_HUB * s)17638 void SmEditHubInit(HWND hWnd, SM_EDIT_HUB *s)
17639 {
17640 	RPC_CREATE_HUB t;
17641 	bool b = false;
17642 	bool support_extoption = false;
17643 	// Validate arguments
17644 	if (hWnd == NULL || s == NULL)
17645 	{
17646 		return;
17647 	}
17648 
17649 	SetIcon(hWnd, 0, ICO_HUB);
17650 
17651 	Zero(&t, sizeof(t));
17652 
17653 	if (s->EditMode == false)
17654 	{
17655 		// Create new
17656 		SetText(hWnd, 0, _UU("CM_EDIT_HUB_1"));
17657 		FocusEx(hWnd, E_HUBNAME);
17658 
17659 		if (s->p->ServerType == SERVER_TYPE_STANDALONE)
17660 		{
17661 			// Stand-alone mode
17662 			Disable(hWnd, R_STATIC);
17663 			Disable(hWnd, R_DYNAMIC);
17664 			SetText(hWnd, S_FARM_INFO, _UU("CM_EDIT_HUB_STANDALONE"));
17665 		}
17666 		else
17667 		{
17668 			Check(hWnd, R_STATIC, true);
17669 		}
17670 
17671 		Check(hWnd, R_ONLINE, true);
17672 
17673 		Hide(hWnd, B_ACL);
17674 		Hide(hWnd, S_ACL);
17675 		Hide(hWnd, S_ACL_2);
17676 		Hide(hWnd, S_ACL_3);
17677 		Hide(hWnd, S_MSG_1);
17678 		Hide(hWnd, S_MSG_4);
17679 		Hide(hWnd, S_MSG_2);
17680 		Hide(hWnd, B_MSG);
17681 	}
17682 	else
17683 	{
17684 		// Edit
17685 		wchar_t tmp[MAX_SIZE];
17686 		UniFormat(tmp, sizeof(tmp), _UU("CM_EDIT_HUB_2"), s->HubName);
17687 		SetText(hWnd, 0, tmp);
17688 		SetTextA(hWnd, E_HUBNAME, s->HubName);
17689 		Disable(hWnd, E_HUBNAME);
17690 
17691 		if (s->p->ServerType == SERVER_TYPE_STANDALONE)
17692 		{
17693 			// Stand-alone mode
17694 			Disable(hWnd, R_STATIC);
17695 			Disable(hWnd, R_DYNAMIC);
17696 			SetText(hWnd, S_FARM_INFO, _UU("CM_EDIT_HUB_STANDALONE"));
17697 		}
17698 
17699 		if (s->p->ServerType == SERVER_TYPE_FARM_CONTROLLER)
17700 		{
17701 			// Controller
17702 			if (GetCapsBool(s->p->CapsList, "b_cluster_hub_type_fixed"))
17703 			{
17704 				Disable(hWnd, R_STATIC);
17705 				Disable(hWnd, R_DYNAMIC);
17706 				SetText(hWnd, S_FARM_INFO, _UU("CM_EDIT_HUB_TYPE_FIXED"));
17707 			}
17708 		}
17709 
17710 		// Get the HUB information
17711 		StrCpy(t.HubName, sizeof(t.HubName), s->HubName);
17712 		if (CALL(hWnd, ScGetHub(s->p->Rpc, &t)) == false)
17713 		{
17714 			EndDialog(hWnd, false);
17715 			return;
17716 		}
17717 
17718 		SetTextA(hWnd, E_PASSWORD1, HIDDEN_PASSWORD);
17719 		SetTextA(hWnd, E_PASSWORD2, HIDDEN_PASSWORD);
17720 
17721 		if (t.HubOption.MaxSession == 0)
17722 		{
17723 			Check(hWnd, R_LIMIT_MAX_SESSION, false);
17724 		}
17725 		else
17726 		{
17727 			Check(hWnd, R_LIMIT_MAX_SESSION, true);
17728 		}
17729 
17730 		Check(hWnd, R_NO_ENUM, t.HubOption.NoEnum);
17731 
17732 		SetIntEx(hWnd, E_MAX_SESSION, t.HubOption.MaxSession);
17733 
17734 		Check(hWnd, R_ONLINE, t.Online);
17735 		Check(hWnd, R_OFFLINE, t.Online ? false : true);
17736 
17737 		Check(hWnd, R_STATIC, t.HubType == HUB_TYPE_FARM_STATIC);
17738 		Check(hWnd, R_DYNAMIC, t.HubType == HUB_TYPE_FARM_DYNAMIC);
17739 
17740 		SetShow(hWnd, B_ACL, GetCapsBool(s->p->CapsList, "b_support_ac"));
17741 		SetShow(hWnd, S_ACL, GetCapsBool(s->p->CapsList, "b_support_ac"));
17742 		SetShow(hWnd, S_ACL_2, GetCapsBool(s->p->CapsList, "b_support_ac"));
17743 		SetShow(hWnd, S_ACL_3, GetCapsBool(s->p->CapsList, "b_support_ac"));
17744 
17745 		SetShow(hWnd, S_MSG_1, GetCapsBool(s->p->CapsList, "b_support_msg"));
17746 		SetShow(hWnd, S_MSG_4, GetCapsBool(s->p->CapsList, "b_support_msg"));
17747 		SetShow(hWnd, S_MSG_2, GetCapsBool(s->p->CapsList, "b_support_msg"));
17748 		SetShow(hWnd, B_MSG, GetCapsBool(s->p->CapsList, "b_support_msg"));
17749 	}
17750 
17751 	// Advanced options
17752 	if (s->EditMode)
17753 	{
17754 		support_extoption = GetCapsBool(s->p->CapsList, "b_support_hub_ext_options");
17755 	}
17756 
17757 	SetEnable(hWnd, S_STATIC, support_extoption);
17758 	SetEnable(hWnd, B_EXTOPTION, support_extoption);
17759 
17760 	SetEnable(hWnd, R_NO_ENUM, GetCapsBool(s->p->CapsList, "b_support_hide_hub"));
17761 
17762 	SmEditHubUpdate(hWnd, s);
17763 
17764 	if (s->EditMode)
17765 	{
17766 		Focus(hWnd, IDOK);
17767 	}
17768 
17769 	if (s->EditMode)
17770 	{
17771 		if (GetCapsBool(s->p->CapsList, "b_support_hub_admin_option"))
17772 		{
17773 			b = true;
17774 		}
17775 	}
17776 
17777 	SetShow(hWnd, S_AO_1, b);
17778 	SetShow(hWnd, S_AO_2, b);
17779 	SetShow(hWnd, S_AO_3, b);
17780 	SetShow(hWnd, B_ADMINOPTION, b);
17781 }
17782 
17783 // HUB edit procedure
SmEditHubProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)17784 UINT SmEditHubProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
17785 {
17786 	SM_EDIT_HUB *s = (SM_EDIT_HUB *)param;
17787 	// Validate arguments
17788 	if (hWnd == NULL)
17789 	{
17790 		return 0;
17791 	}
17792 
17793 	switch (msg)
17794 	{
17795 	case WM_INITDIALOG:
17796 		SmEditHubInit(hWnd, s);
17797 		break;
17798 
17799 	case WM_COMMAND:
17800 		switch (LOWORD(wParam))
17801 		{
17802 		case E_PASSWORD1:
17803 		case E_PASSWORD2:
17804 		case E_HUBNAME:
17805 		case R_LIMIT_MAX_SESSION:
17806 		case E_MAX_SESSION:
17807 			SmEditHubUpdate(hWnd, s);
17808 			break;
17809 		}
17810 
17811 		switch (wParam)
17812 		{
17813 		case IDOK:
17814 			SmEditHubOnOk(hWnd, s);
17815 			break;
17816 
17817 		case IDCANCEL:
17818 			Close(hWnd);
17819 			break;
17820 
17821 		case R_LIMIT_MAX_SESSION:
17822 			if (IsChecked(hWnd, R_LIMIT_MAX_SESSION))
17823 			{
17824 				FocusEx(hWnd, E_MAX_SESSION);
17825 			}
17826 			break;
17827 
17828 		case B_ADMINOPTION:
17829 			SmHubAdminOption(hWnd, s);
17830 			break;
17831 
17832 		case B_EXTOPTION:
17833 			SmHubExtOption(hWnd, s);
17834 			break;
17835 
17836 		case B_ACL:
17837 			SmHubAc(hWnd, s);
17838 			break;
17839 
17840 		case B_MSG:
17841 			SmHubMsg(hWnd, s);
17842 			break;
17843 		}
17844 		break;
17845 
17846 	case WM_CLOSE:
17847 		EndDialog(hWnd, false);
17848 		break;
17849 	}
17850 
17851 	return 0;
17852 }
17853 
17854 // HUB edit dialog
SmEditHubDlg(HWND hWnd,SM_SERVER * p,char * hubname)17855 bool SmEditHubDlg(HWND hWnd, SM_SERVER *p, char *hubname)
17856 {
17857 	SM_EDIT_HUB s;
17858 	// Validate arguments
17859 	if (hWnd == NULL || p == NULL)
17860 	{
17861 		return false;
17862 	}
17863 
17864 	Zero(&s, sizeof(s));
17865 	s.p = p;
17866 	s.EditMode = true;
17867 	StrCpy(s.HubName, sizeof(s.HubName), hubname);
17868 
17869 	if (p->Bridge == false)
17870 	{
17871 		return Dialog(hWnd, D_SM_EDIT_HUB, SmEditHubProc, &s);
17872 	}
17873 	else
17874 	{
17875 		SmHubExtOption(hWnd, &s);
17876 		return false;
17877 	}
17878 }
17879 
17880 // HUB creation dialog
SmCreateHubDlg(HWND hWnd,SM_SERVER * p)17881 bool SmCreateHubDlg(HWND hWnd, SM_SERVER *p)
17882 {
17883 	SM_EDIT_HUB s;
17884 	// Validate arguments
17885 	if (hWnd == NULL || p == NULL)
17886 	{
17887 		return false;
17888 	}
17889 
17890 	Zero(&s, sizeof(s));
17891 	s.p = p;
17892 	s.EditMode = false;
17893 
17894 	return Dialog(hWnd, D_SM_EDIT_HUB, SmEditHubProc, &s);
17895 }
17896 
17897 // Display the status of the virtual HUB
SmRefreshHubStatus(HWND hWnd,SM_SERVER * p,void * param)17898 bool SmRefreshHubStatus(HWND hWnd, SM_SERVER *p, void *param)
17899 {
17900 	RPC_HUB_STATUS t;
17901 	// Validate arguments
17902 	if (hWnd == NULL || p == NULL || param == NULL)
17903 	{
17904 		return false;
17905 	}
17906 
17907 	Zero(&t, sizeof(RPC_HUB_STATUS));
17908 	StrCpy(t.HubName, sizeof(t.HubName), (char *)param);
17909 	if (CALL(hWnd, ScGetHubStatus(p->Rpc, &t)))
17910 	{
17911 		wchar_t *s;
17912 		wchar_t tmp[MAX_SIZE];
17913 		LVB *b = LvInsertStart();
17914 
17915 		// HUB name
17916 		s = CopyStrToUni((char *)param);
17917 		LvInsertAdd(b, ICO_HUB, 0, 2, _UU("SM_HUB_STATUS_HUBNAME"), s);
17918 		Free(s);
17919 
17920 		// Online
17921 		LvInsertAdd(b, t.Online ? ICO_PROTOCOL : ICO_PROTOCOL_X, 0, 2, _UU("SM_HUB_STATUS_ONLINE"),
17922 			t.Online ? _UU("SM_HUB_ONLINE") : _UU("SM_HUB_OFFLINE"));
17923 
17924 		// Type of HUB
17925 		LvInsertAdd(b, t.HubType == HUB_TYPE_STANDALONE ? ICO_TOWER : ICO_FARM, 0, 2, _UU("SM_HUB_TYPE"),
17926 			GetHubTypeStr(t.HubType));
17927 
17928 		if (t.HubType == HUB_TYPE_STANDALONE)
17929 		{
17930 			// Enable / Disable the SecureNAT
17931 			LvInsertAdd(b, ICO_ROUTER, NULL, 2, _UU("SM_HUB_SECURE_NAT"),
17932 				t.SecureNATEnabled ? _UU("SM_HUB_SECURE_NAT_YES") : _UU("SM_HUB_SECURE_NAT_NO"));
17933 		}
17934 
17935 		// Other values
17936 		UniToStru(tmp, t.NumSessions);
17937 		LvInsertAdd(b, ICO_PROTOCOL, 0, 2, _UU("SM_HUB_NUM_SESSIONS"), tmp);
17938 		if (t.NumSessionsClient != 0 || t.NumSessionsBridge != 0)
17939 		{
17940 			UniToStru(tmp, t.NumSessionsClient);
17941 			LvInsertAdd(b, ICO_PROTOCOL, 0, 2, _UU("SM_HUB_NUM_SESSIONS_CLIENT"), tmp);
17942 			UniToStru(tmp, t.NumSessionsBridge);
17943 			LvInsertAdd(b, ICO_PROTOCOL, 0, 2, _UU("SM_HUB_NUM_SESSIONS_BRIDGE"), tmp);
17944 		}
17945 
17946 		UniToStru(tmp, t.NumAccessLists);
17947 		LvInsertAdd(b, ICO_DISCARD, 0, 2, _UU("SM_HUB_NUM_ACCESSES"), tmp);
17948 
17949 		if (p->ServerType != SERVER_TYPE_FARM_MEMBER)
17950 		{
17951 			UniToStru(tmp, t.NumUsers);
17952 			LvInsertAdd(b, ICO_USER, 0, 2, _UU("SM_HUB_NUM_USERS"), tmp);
17953 			UniToStru(tmp, t.NumGroups);
17954 			LvInsertAdd(b, ICO_GROUP, 0, 2, _UU("SM_HUB_NUM_GROUPS"), tmp);
17955 		}
17956 
17957 		UniToStru(tmp, t.NumMacTables);
17958 		LvInsertAdd(b, ICO_MACHINE, 0, 2, _UU("SM_HUB_NUM_MAC_TABLES"), tmp);
17959 		UniToStru(tmp, t.NumIpTables);
17960 		LvInsertAdd(b, ICO_MACHINE, 0, 2, _UU("SM_HUB_NUM_IP_TABLES"), tmp);
17961 
17962 		// Usage status
17963 		UniToStru(tmp, t.NumLogin);
17964 		LvInsertAdd(b, ICO_KEY, NULL, 2, _UU("SM_HUB_NUM_LOGIN"), tmp);
17965 
17966 		if (t.LastLoginTime != 0)
17967 		{
17968 			GetDateTimeStr64Uni(tmp, sizeof(tmp), SystemToLocal64(t.LastLoginTime));
17969 		}
17970 		else
17971 		{
17972 			UniStrCpy(tmp, sizeof(tmp), _UU("COMMON_UNKNOWN"));
17973 		}
17974 		LvInsertAdd(b, ICO_DATETIME, NULL, 2, _UU("SM_HUB_LAST_LOGIN_TIME"), tmp);
17975 
17976 		if (t.LastCommTime != 0)
17977 		{
17978 			GetDateTimeStr64Uni(tmp, sizeof(tmp), SystemToLocal64(t.LastCommTime));
17979 		}
17980 		else
17981 		{
17982 			UniStrCpy(tmp, sizeof(tmp), _UU("COMMON_UNKNOWN"));
17983 		}
17984 		LvInsertAdd(b, ICO_DATETIME, NULL, 2, _UU("SM_HUB_LAST_COMM_TIME"), tmp);
17985 
17986 		if (t.CreatedTime != 0)
17987 		{
17988 			GetDateTimeStr64Uni(tmp, sizeof(tmp), SystemToLocal64(t.CreatedTime));
17989 		}
17990 		else
17991 		{
17992 			UniStrCpy(tmp, sizeof(tmp), _UU("COMMON_UNKNOWN"));
17993 		}
17994 		LvInsertAdd(b, ICO_DATETIME, NULL, 2, _UU("SM_HUB_CREATED_TIME"), tmp);
17995 
17996 		// Traffic information
17997 		SmInsertTrafficInfo(b, &t.Traffic);
17998 
17999 		LvInsertEnd(b, hWnd, L_STATUS);
18000 	}
18001 	else
18002 	{
18003 		return false;
18004 	}
18005 
18006 	return true;
18007 }
18008 
18009 // Add a traffic information to LVB
SmInsertTrafficInfo(LVB * b,TRAFFIC * t)18010 void SmInsertTrafficInfo(LVB *b, TRAFFIC *t)
18011 {
18012 	wchar_t tmp[MAX_SIZE];
18013 	char vv[128];
18014 	// Validate arguments
18015 	if (b == NULL || t == NULL)
18016 	{
18017 		return;
18018 	}
18019 
18020 	// Transmission information
18021 	ToStr3(vv, sizeof(vv), t->Send.UnicastCount);
18022 	UniFormat(tmp, sizeof(tmp), _UU("SM_ST_NUM_PACKET_STR"), vv);
18023 	LvInsertAdd(b, ICO_INFORMATION, 0, 2, _UU("SM_ST_SEND_UCAST_NUM"), tmp);
18024 
18025 	ToStr3(vv, sizeof(vv), t->Send.UnicastBytes);
18026 	UniFormat(tmp, sizeof(tmp), _UU("SM_ST_SIZE_BYTE_STR"), vv);
18027 	LvInsertAdd(b, ICO_INFORMATION, 0, 2, _UU("SM_ST_SEND_UCAST_SIZE"), tmp);
18028 
18029 	ToStr3(vv, sizeof(vv), t->Send.BroadcastCount);
18030 	UniFormat(tmp, sizeof(tmp), _UU("SM_ST_NUM_PACKET_STR"), vv);
18031 	LvInsertAdd(b, ICO_INFORMATION, 0, 2, _UU("SM_ST_SEND_BCAST_NUM"), tmp);
18032 
18033 	ToStr3(vv, sizeof(vv), t->Send.BroadcastBytes);
18034 	UniFormat(tmp, sizeof(tmp), _UU("SM_ST_SIZE_BYTE_STR"), vv);
18035 	LvInsertAdd(b, ICO_INFORMATION, 0, 2, _UU("SM_ST_SEND_BCAST_SIZE"), tmp);
18036 
18037 	// Reception information
18038 	ToStr3(vv, sizeof(vv), t->Recv.UnicastCount);
18039 	UniFormat(tmp, sizeof(tmp), _UU("SM_ST_NUM_PACKET_STR"), vv);
18040 	LvInsertAdd(b, ICO_INFORMATION, 0, 2, _UU("SM_ST_RECV_UCAST_NUM"), tmp);
18041 
18042 	ToStr3(vv, sizeof(vv), t->Recv.UnicastBytes);
18043 	UniFormat(tmp, sizeof(tmp), _UU("SM_ST_SIZE_BYTE_STR"), vv);
18044 	LvInsertAdd(b, ICO_INFORMATION, 0, 2, _UU("SM_ST_RECV_UCAST_SIZE"), tmp);
18045 
18046 	ToStr3(vv, sizeof(vv), t->Recv.BroadcastCount);
18047 	UniFormat(tmp, sizeof(tmp), _UU("SM_ST_NUM_PACKET_STR"), vv);
18048 	LvInsertAdd(b, ICO_INFORMATION, 0, 2, _UU("SM_ST_RECV_BCAST_NUM"), tmp);
18049 
18050 	ToStr3(vv, sizeof(vv), t->Recv.BroadcastBytes);
18051 	UniFormat(tmp, sizeof(tmp), _UU("SM_ST_SIZE_BYTE_STR"), vv);
18052 	LvInsertAdd(b, ICO_INFORMATION, 0, 2, _UU("SM_ST_RECV_BCAST_SIZE"), tmp);
18053 }
18054 
18055 // Status display dialog procedure
SmStatusDlgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)18056 UINT SmStatusDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
18057 {
18058 	SM_STATUS *s = (SM_STATUS *)param;
18059 	// Validate arguments
18060 	if (hWnd == NULL)
18061 	{
18062 		return 0;
18063 	}
18064 
18065 	switch (msg)
18066 	{
18067 	case WM_INITDIALOG:
18068 		// Initialize
18069 		LvInitEx(hWnd, L_STATUS, s->NoImage);
18070 		LvSetStyle(hWnd, L_STATUS, LVS_EX_GRIDLINES);
18071 		SetIcon(hWnd, 0, s->Icon);
18072 		SetIcon(hWnd, S_ICON, s->Icon);
18073 		SetText(hWnd, 0, s->Caption);
18074 		SetText(hWnd, S_TITLE, s->Caption);
18075 		DlgFont(hWnd, S_TITLE, 15, true);
18076 		if (s->InitProc != NULL)
18077 		{
18078 			s->InitProc(hWnd, s->p, s->Param);
18079 		}
18080 		else
18081 		{
18082 			// Initialize the column
18083 			LvInsertColumn(hWnd, L_STATUS, 0, _UU("SM_STATUS_COLUMN_1"), 0);
18084 			LvInsertColumn(hWnd, L_STATUS, 1, _UU("SM_STATUS_COLUMN_2"), 0);
18085 		}
18086 		if (s->RefreshProc(hWnd, s->p, s->Param) == false)
18087 		{
18088 			Close(hWnd);
18089 		}
18090 		LvAutoSize(hWnd, L_STATUS);
18091 		Focus(hWnd, L_STATUS);
18092 
18093 		if (s->show_refresh_button == false)
18094 		{
18095 			Hide(hWnd, IDOK);
18096 		}
18097 
18098 		break;
18099 
18100 	case WM_COMMAND:
18101 		switch (wParam)
18102 		{
18103 		case IDOK:
18104 			// Update
18105 			if (s->RefreshProc(hWnd, s->p, s->Param) == false)
18106 			{
18107 				Close(hWnd);
18108 			}
18109 			LvAutoSize(hWnd, L_STATUS);
18110 			break;
18111 
18112 		case IDCANCEL:
18113 			Close(hWnd);
18114 			break;
18115 		}
18116 		break;
18117 
18118 	case WM_CLOSE:
18119 		EndDialog(hWnd, 0);
18120 		break;
18121 	}
18122 
18123 	LvStandardHandler(hWnd, msg, wParam, lParam, L_STATUS);
18124 
18125 	return 0;
18126 }
18127 
18128 // Status display dialog
SmStatusDlg(HWND hWnd,SM_SERVER * p,void * param,bool no_image,bool show_refresh_button,wchar_t * caption,UINT icon,SM_STATUS_INIT_PROC * init,SM_STATUS_REFRESH_PROC * refresh)18129 void SmStatusDlg(HWND hWnd, SM_SERVER *p, void *param, bool no_image, bool show_refresh_button, wchar_t *caption, UINT icon,
18130 				 SM_STATUS_INIT_PROC *init, SM_STATUS_REFRESH_PROC *refresh)
18131 {
18132 	SM_STATUS s;
18133 	// Validate arguments
18134 	if (hWnd == NULL || p == NULL || refresh == NULL)
18135 	{
18136 		return;
18137 	}
18138 
18139 	if (icon == 0)
18140 	{
18141 		icon = ICO_INFORMATION;
18142 	}
18143 	if (caption == NULL)
18144 	{
18145 		caption = _UU("SM_INFORMATION");
18146 	}
18147 
18148 	Zero(&s, sizeof(s));
18149 	s.show_refresh_button = show_refresh_button;
18150 	s.p = p;
18151 	s.NoImage = no_image;
18152 	s.Param = param;
18153 	s.Icon = icon;
18154 	s.Caption = caption;
18155 	s.InitProc = init;
18156 	s.RefreshProc = refresh;
18157 
18158 	Dialog(hWnd, D_SM_STATUS, SmStatusDlgProc, &s);
18159 }
18160 
18161 // Server management dialog update
SmServerDlgUpdate(HWND hWnd,SM_SERVER * p)18162 void SmServerDlgUpdate(HWND hWnd, SM_SERVER *p)
18163 {
18164 	bool hub_selected = false;
18165 	bool hub_selected_online = false;
18166 	bool hub_selected_offline = false;
18167 	bool hub_have_admin_right = false;
18168 	bool listener_selected = false;
18169 	bool listener_selected_enabled = false;
18170 	bool listener_selected_disabled = false;
18171 	bool two_or_more_listener = false;
18172 	bool bridge;
18173 	UINT i;
18174 	// Validate arguments
18175 	if (hWnd == NULL || p == NULL)
18176 	{
18177 		return;
18178 	}
18179 
18180 	bridge = GetCapsBool(p->CapsList, "b_bridge");
18181 
18182 	hub_selected = LvIsSelected(hWnd, L_HUB);
18183 
18184 	if (hub_selected)
18185 	{
18186 		if (p->ServerAdminMode)
18187 		{
18188 			hub_have_admin_right = true;
18189 		}
18190 		i = LvGetSelected(hWnd, L_HUB);
18191 		if (i != INFINITE)
18192 		{
18193 			wchar_t *s = LvGetStr(hWnd, L_HUB, i, 1);
18194 			if (p->ServerAdminMode == false)
18195 			{
18196 				char *hubname = LvGetStrA(hWnd, L_HUB, i, 0);
18197 				if (hubname != NULL)
18198 				{
18199 					if (StrCmpi(hubname, p->HubName) == 0)
18200 					{
18201 						hub_have_admin_right = true;
18202 					}
18203 					Free(hubname);
18204 				}
18205 			}
18206 			hub_selected_online = (UniStrCmpi(s, _UU("SM_HUB_ONLINE")) == 0);
18207 			hub_selected_offline = hub_selected_online ? false : true;
18208 			Free(s);
18209 		}
18210 	}
18211 
18212 	listener_selected = LvIsSelected(hWnd, L_LISTENER);
18213 	if (listener_selected)
18214 	{
18215 		wchar_t *s = LvGetSelectedStr(hWnd, L_LISTENER, 1);
18216 		if (UniStrCmpi(s, _UU("CM_LISTENER_OFFLINE")) == 0)
18217 		{
18218 			listener_selected_disabled = true;
18219 		}
18220 		else
18221 		{
18222 			listener_selected_enabled = true;
18223 		}
18224 		Free(s);
18225 	}
18226 
18227 	if (LvNum(hWnd, L_LISTENER) >= 2)
18228 	{
18229 		two_or_more_listener = true;
18230 	}
18231 
18232 	SetEnable(hWnd, IDOK, bridge || (hub_selected && hub_have_admin_right));
18233 	SetEnable(hWnd, B_ONLINE, bridge == false && hub_selected_offline && hub_have_admin_right && p->ServerType != SERVER_TYPE_FARM_MEMBER);
18234 	SetEnable(hWnd, B_OFFLINE, bridge == false && hub_selected_online && hub_have_admin_right && p->ServerType != SERVER_TYPE_FARM_MEMBER);
18235 	SetEnable(hWnd, B_HUB_STATUS, hub_selected && hub_have_admin_right);
18236 	SetEnable(hWnd, B_DELETE, bridge == false && hub_selected && p->ServerAdminMode && p->ServerType != SERVER_TYPE_FARM_MEMBER);
18237 	SetEnable(hWnd, B_EDIT, hub_selected && hub_have_admin_right && p->ServerType != SERVER_TYPE_FARM_MEMBER);
18238 	SetEnable(hWnd, B_CREATE, bridge == false && p->ServerAdminMode && p->ServerType != SERVER_TYPE_FARM_MEMBER);
18239 
18240 	SetEnable(hWnd, B_CREATE_LISTENER, p->ServerAdminMode);
18241 	SetEnable(hWnd, B_DELETE_LISTENER, p->ServerAdminMode && listener_selected && two_or_more_listener);
18242 	SetEnable(hWnd, B_START, p->ServerAdminMode && listener_selected_disabled);
18243 	SetEnable(hWnd, B_STOP, p->ServerAdminMode && listener_selected_enabled);
18244 	SetEnable(hWnd, B_FARM, GetCapsBool(p->CapsList, "b_support_cluster") && p->ServerAdminMode && GetCapsBool(p->CapsList, "b_support_vgs_in_client") == false);
18245 	SetEnable(hWnd, B_FARM_STATUS, GetCapsBool(p->CapsList, "b_support_cluster") && p->ServerType != SERVER_TYPE_STANDALONE);
18246 }
18247 
18248 // Server management dialog initialization
SmServerDlgInit(HWND hWnd,SM_SERVER * p)18249 void SmServerDlgInit(HWND hWnd, SM_SERVER *p)
18250 {
18251 	// Validate arguments
18252 	if (hWnd == NULL || p == NULL)
18253 	{
18254 		return;
18255 	}
18256 
18257 	// Initialize the column
18258 	LvInit(hWnd, L_HUB);
18259 	LvSetStyle(hWnd, L_HUB, LVS_EX_GRIDLINES);
18260 	LvInsertColumn(hWnd, L_HUB, 0, _UU("SM_HUB_COLUMN_1"), 150);
18261 	LvInsertColumn(hWnd, L_HUB, 1, _UU("SM_HUB_COLUMN_2"), 80);
18262 	LvInsertColumn(hWnd, L_HUB, 2, _UU("SM_HUB_COLUMN_3"), 80);
18263 	LvInsertColumn(hWnd, L_HUB, 3, _UU("SM_HUB_COLUMN_4"), 80);
18264 	LvInsertColumn(hWnd, L_HUB, 4, _UU("SM_HUB_COLUMN_5"), 80);
18265 	LvInsertColumn(hWnd, L_HUB, 5, _UU("SM_HUB_COLUMN_6"), 80);
18266 	LvInsertColumn(hWnd, L_HUB, 6, _UU("SM_HUB_COLUMN_7"), 80);
18267 	LvInsertColumn(hWnd, L_HUB, 7, _UU("SM_HUB_COLUMN_8"), 80);
18268 	LvInsertColumn(hWnd, L_HUB, 8, _UU("SM_HUB_COLUMN_9"), 80);
18269 	LvInsertColumn(hWnd, L_HUB, 9, _UU("SM_HUB_COLUMN_10"), 120);
18270 	LvInsertColumn(hWnd, L_HUB, 10, _UU("SM_HUB_COLUMN_11"), 120);
18271 	LvInsertColumn(hWnd, L_HUB, 11, _UU("SM_SESS_COLUMN_6"), 100);
18272 	LvInsertColumn(hWnd, L_HUB, 12, _UU("SM_SESS_COLUMN_7"), 100);
18273 
18274 	LvInit(hWnd, L_LISTENER);
18275 	LvSetStyle(hWnd, L_LISTENER, LVS_EX_GRIDLINES);
18276 	LvInsertColumn(hWnd, L_LISTENER, 0, _UU("CM_LISTENER_COLUMN_1"), 90);
18277 	LvInsertColumn(hWnd, L_LISTENER, 1, _UU("CM_LISTENER_COLUMN_2"), 80);
18278 
18279 	SmServerDlgRefresh(hWnd, p);
18280 
18281 	if (p->ServerAdminMode == false)
18282 	{
18283 		// Select the target HUB in the case of the Virtual HUB management mode
18284 		wchar_t *s = CopyStrToUni(p->HubName);
18285 		LvSelect(hWnd, L_HUB, LvSearchStr(hWnd, L_HUB, 0, s));
18286 		Free(s);
18287 	}
18288 	else
18289 	{
18290 		// In the case of whole server management mode
18291 		UINT num_hubs = LvNum(hWnd, L_HUB);
18292 
18293 		if (num_hubs == 1)
18294 		{
18295 			// Select the Virtual HUB if Virtual HUB exists only one
18296 			LvSelect(hWnd, L_HUB, 0);
18297 		}
18298 		else
18299 		{
18300 			// Select the Virtual HUB the last selected if there are some virtual HUBs
18301 			char tmp[MAX_SIZE];
18302 			char *hubname;
18303 
18304 			Format(tmp, sizeof(tmp), "%s:%u:%s", p->CurrentSetting->ClientOption.Hostname,
18305 				p->CurrentSetting->ClientOption.Port,
18306 				p->CurrentSetting->ServerAdminMode ? "" : p->CurrentSetting->HubName);
18307 
18308 			hubname = MsRegReadStr(REG_CURRENT_USER, SM_LASTHUB_REG_KEY, tmp);
18309 
18310 			if (IsEmptyStr(hubname) == false)
18311 			{
18312 				LvSelect(hWnd, L_HUB, LvSearchStrA(hWnd, L_HUB, 0, hubname));
18313 			}
18314 
18315 			Free(hubname);
18316 		}
18317 	}
18318 
18319 	Focus(hWnd, L_HUB);
18320 
18321 	SmServerDlgUpdate(hWnd, p);
18322 
18323 	if (GetCapsBool(p->CapsList, "b_bridge"))
18324 	{
18325 		Disable(hWnd, L_HUB);
18326 	}
18327 
18328 	// Enable Local bridge button, etc. in the case of the Admin of the Server
18329 	SetEnable(hWnd, B_BRIDGE, GetCapsBool(p->CapsList, "b_local_bridge") && p->ServerAdminMode && GetCapsBool(p->CapsList, "b_support_vgs_in_client") == false);
18330 	SetEnable(hWnd, B_CONNECTION, p->ServerAdminMode);
18331 
18332 	// Config R/W button
18333 	SetEnable(hWnd, B_CONFIG, GetCapsBool(p->CapsList, "b_support_config_rw") && p->ServerAdminMode && GetCapsBool(p->CapsList, "b_support_vgs_in_client") == false);
18334 
18335 	// Layer 3 button
18336 	SetEnable(hWnd, B_L3, GetCapsBool(p->CapsList, "b_support_layer3") && p->ServerAdminMode && GetCapsBool(p->CapsList, "b_support_vgs_in_client") == false);
18337 
18338 	// License button
18339 	SetShow(hWnd, B_LICENSE, GetCapsBool(p->CapsList, "b_support_license") && p->ServerAdminMode);
18340 	SetShow(hWnd, S_LICENSE, GetCapsBool(p->CapsList, "b_support_license") && p->ServerAdminMode);
18341 	SetShow(hWnd, S_BETA, GetCapsBool(p->CapsList, "b_beta_version") && (IsShow(hWnd, B_LICENSE) == false));
18342 
18343 	// IPsec button
18344 	SetEnable(hWnd, B_IPSEC, GetCapsBool(p->CapsList, "b_support_ipsec") && p->ServerAdminMode);
18345 
18346 	// OpenVPN, SSTP button
18347 	SetEnable(hWnd, B_OPENVPN, GetCapsBool(p->CapsList, "b_support_openvpn") && p->ServerAdminMode);
18348 
18349 	// DDNS button
18350 	SetEnable(hWnd, B_DDNS, GetCapsBool(p->CapsList, "b_support_ddns") && p->ServerAdminMode);
18351 
18352 	// VPN Azure button
18353 	SetEnable(hWnd, B_AZURE, GetCapsBool(p->CapsList, "b_support_azure") && p->ServerAdminMode);
18354 
18355 	DlgFont(hWnd, S_BETA, 12, false);
18356 	SetFont(hWnd, E_DDNS_HOST, GetFont("Verdana", 10, false, false, false, false));
18357 	SetFont(hWnd, E_AZURE_HOST, GetFont("Verdana", 10, false, false, false, false));
18358 
18359 	SetShow(hWnd, B_VPNGATE, false);
18360 	SetShow(hWnd, S_ICO_VPNGATE, false);
18361 
18362 	DlgFont(hWnd, IDOK, 0, true);
18363 }
18364 
18365 // Server management dialog update
SmServerDlgRefresh(HWND hWnd,SM_SERVER * p)18366 void SmServerDlgRefresh(HWND hWnd, SM_SERVER *p)
18367 {
18368 	RPC_ENUM_HUB t;
18369 	DDNS_CLIENT_STATUS st;
18370 	RPC_AZURE_STATUS sta;
18371 	UINT i;
18372 	// Validate arguments
18373 	if (hWnd == NULL || p == NULL)
18374 	{
18375 		return;
18376 	}
18377 
18378 	// Virtual HUB list update
18379 	Zero(&t, sizeof(t));
18380 	if (CALL(hWnd, ScEnumHub(p->Rpc, &t)))
18381 	{
18382 		LVB *b = LvInsertStart();
18383 		for (i = 0;i < t.NumHub;i++)
18384 		{
18385 			RPC_ENUM_HUB_ITEM *e = &t.Hubs[i];
18386 			wchar_t name[MAX_HUBNAME_LEN + 1];
18387 			wchar_t s1[64], s2[64], s3[64], s4[64], s5[64];
18388 			wchar_t s6[64], s7[128], s8[128];
18389 			wchar_t s9[64], s10[64];
18390 			UINT icon;
18391 
18392 			UniToStru(s1, e->NumUsers);
18393 			UniToStru(s2, e->NumGroups);
18394 			UniToStru(s3, e->NumSessions);
18395 			UniToStru(s4, e->NumMacTables);
18396 			UniToStru(s5, e->NumIpTables);
18397 
18398 			UniToStru(s6, e->NumLogin);
18399 
18400 			if (e->LastLoginTime != 0)
18401 			{
18402 				GetDateTimeStr64Uni(s7, sizeof(s7), SystemToLocal64(e->LastLoginTime));
18403 			}
18404 			else
18405 			{
18406 				UniStrCpy(s7, sizeof(s7), _UU("COMMON_UNKNOWN"));
18407 			}
18408 
18409 			if (e->LastCommTime != 0)
18410 			{
18411 				GetDateTimeStr64Uni(s8, sizeof(s8), SystemToLocal64(e->LastCommTime));
18412 			}
18413 			else
18414 			{
18415 				UniStrCpy(s8, sizeof(s8), _UU("COMMON_UNKNOWN"));
18416 			}
18417 
18418 			StrToUni(name, sizeof(name), e->HubName);
18419 
18420 			icon = ICO_HUB;
18421 			if (e->Online == false)
18422 			{
18423 				icon = ICO_HUB_OFFLINE;
18424 			}
18425 
18426 			if (e->IsTrafficFilled == false)
18427 			{
18428 				UniStrCpy(s9, sizeof(s9), _UU("CM_ST_NONE"));
18429 				UniStrCpy(s10, sizeof(s10), _UU("CM_ST_NONE"));
18430 			}
18431 			else
18432 			{
18433 				UniToStr3(s9, sizeof(s9),
18434 					e->Traffic.Recv.BroadcastBytes + e->Traffic.Recv.UnicastBytes +
18435 					e->Traffic.Send.BroadcastBytes + e->Traffic.Send.UnicastBytes);
18436 
18437 				UniToStr3(s10, sizeof(s10),
18438 					e->Traffic.Recv.BroadcastCount + e->Traffic.Recv.UnicastCount +
18439 					e->Traffic.Send.BroadcastCount + e->Traffic.Send.UnicastCount);
18440 			}
18441 
18442 			LvInsertAdd(b,
18443 				icon,
18444 				NULL,
18445 				13,
18446 				name,
18447 				e->Online ? _UU("SM_HUB_ONLINE") : _UU("SM_HUB_OFFLINE"),
18448 				GetHubTypeStr(e->HubType),
18449 				s1, s2, s3, s4, s5, s6, s7, s8, s9, s10);
18450 		}
18451 		LvInsertEnd(b, hWnd, L_HUB);
18452 		FreeRpcEnumHub(&t);
18453 	}
18454 
18455 	// Listener list update
18456 	if (p != NULL)
18457 	{
18458 		RPC_LISTENER_LIST t;
18459 		Zero(&t, sizeof(RPC_LISTENER_LIST));
18460 		if (CALL(hWnd, ScEnumListener(p->Rpc, &t)))
18461 		{
18462 			LVB *b = LvInsertStart();
18463 			for (i = 0;i < t.NumPort;i++)
18464 			{
18465 				wchar_t tmp[MAX_SIZE];
18466 				wchar_t *status;
18467 				UINT icon;
18468 				UniFormat(tmp, sizeof(tmp), _UU("CM_LISTENER_TCP_PORT"), t.Ports[i]);
18469 
18470 				status = _UU("CM_LISTENER_ONLINE");
18471 				icon = ICO_PROTOCOL;
18472 				if (t.Errors[i])
18473 				{
18474 					status = _UU("CM_LISTENER_ERROR");
18475 					icon = ICO_PROTOCOL_X;
18476 				}
18477 				else if (t.Enables[i] == false)
18478 				{
18479 					status = _UU("CM_LISTENER_OFFLINE");
18480 					icon = ICO_PROTOCOL_OFFLINE;
18481 				}
18482 
18483 				LvInsertAdd(b, icon, (void *)t.Ports[i], 2, tmp, status);
18484 			}
18485 			LvInsertEnd(b, hWnd, L_LISTENER);
18486 			FreeRpcListenerList(&t);
18487 		}
18488 	}
18489 
18490 	// Get the DDNS client state
18491 	Zero(&st, sizeof(st));
18492 	if (ScGetDDnsClientStatus(p->Rpc, &st) == ERR_NO_ERROR && IsEmptyStr(st.CurrentFqdn) == false)
18493 	{
18494 		SetTextA(hWnd, E_DDNS_HOST, st.CurrentFqdn);
18495 
18496 		Show(hWnd, S_DDNS);
18497 		Show(hWnd, E_DDNS_HOST);
18498 	}
18499 	else
18500 	{
18501 		Hide(hWnd, S_DDNS);
18502 		Hide(hWnd, E_DDNS_HOST);
18503 	}
18504 
18505 	// VPN Azure client state acquisition
18506 	Zero(&sta, sizeof(sta));
18507 	if (ScGetAzureStatus(p->Rpc, &sta) == ERR_NO_ERROR && sta.IsEnabled && IsEmptyStr(st.CurrentFqdn) == false)
18508 	{
18509 		char tmp[MAX_SIZE];
18510 
18511 		StrCpy(tmp, sizeof(tmp), st.CurrentHostName);
18512 		StrCat(tmp, sizeof(tmp), AZURE_DOMAIN_SUFFIX);
18513 
18514 		SetTextA(hWnd, E_AZURE_HOST, tmp);
18515 
18516 		Show(hWnd, S_AZURE);
18517 		Show(hWnd, E_AZURE_HOST);
18518 	}
18519 	else
18520 	{
18521 		Hide(hWnd, S_AZURE);
18522 		Hide(hWnd, E_AZURE_HOST);
18523 	}
18524 
18525 	SmServerDlgUpdate(hWnd, p);
18526 }
18527 
18528 // Server management dialog procedure
SmServerDlgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)18529 UINT SmServerDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
18530 {
18531 	SM_SERVER *p = (SM_SERVER *)param;
18532 	wchar_t *s;
18533 	wchar_t tmp[MAX_SIZE];
18534 	NMHDR *n;
18535 	UINT i;
18536 	// Validate arguments
18537 	if (hWnd == NULL)
18538 	{
18539 		return 0;
18540 	}
18541 
18542 	switch (msg)
18543 	{
18544 	case WM_INITDIALOG:
18545 		FormatText(hWnd, 0, p->Title);
18546 
18547 		if (p->Bridge == false)
18548 		{
18549 			FormatText(hWnd, S_TITLE, p->ServerName);
18550 		}
18551 		else
18552 		{
18553 			UniFormat(tmp, sizeof(tmp), _UU("SM_SERVER_BRIDGE_TITLE"), p->ServerName);
18554 			SetText(hWnd, S_TITLE, tmp);
18555 		}
18556 
18557 		DlgFont(hWnd, S_TITLE, 16, 1);
18558 
18559 		SetIcon(hWnd, 0, p->Bridge == false ? ICO_VPNSERVER : ICO_BRIDGE);
18560 
18561 		SmServerDlgInit(hWnd, p);
18562 
18563 		SetTimer(hWnd, 1, 50, NULL);
18564 
18565 		break;
18566 
18567 	case WM_COMMAND:
18568 		switch (wParam)
18569 		{
18570 		case IDOK:
18571 			// Management
18572 			if (IsEnable(hWnd, IDOK))
18573 			{
18574 				if (p->Bridge == false)
18575 				{
18576 					s = LvGetSelectedStr(hWnd, L_HUB, 0);
18577 				}
18578 				else
18579 				{
18580 					s = CopyUniStr(L"BRIDGE");
18581 				}
18582 				if (s != NULL)
18583 				{
18584 					char hubname[MAX_HUBNAME_LEN + 1];
18585 					SM_HUB hub;
18586 					Zero(&hub, sizeof(hub));
18587 					UniToStr(hubname, sizeof(hubname), s);
18588 					hub.p = p;
18589 					hub.Rpc = p->Rpc;
18590 					hub.HubName = hubname;
18591 					SmHubDlg(hWnd, &hub);
18592 					//SmServerDlgRefresh(hWnd, p);
18593 					Free(s);
18594 				}
18595 			}
18596 			break;
18597 
18598 		case B_ONLINE:
18599 			// Online
18600 			s = LvGetSelectedStr(hWnd, L_HUB, 0);
18601 			if (s != NULL)
18602 			{
18603 				RPC_SET_HUB_ONLINE t;
18604 				Zero(&t, sizeof(t));
18605 				UniToStr(t.HubName, sizeof(t.HubName), s);
18606 				t.Online = true;
18607 				if (CALL(hWnd, ScSetHubOnline(p->Rpc, &t)))
18608 				{
18609 					SmServerDlgRefresh(hWnd, p);
18610 				}
18611 				Free(s);
18612 			}
18613 			break;
18614 
18615 		case B_OFFLINE:
18616 			// Offline
18617 			s = LvGetSelectedStr(hWnd, L_HUB, 0);
18618 			if (s != NULL)
18619 			{
18620 				RPC_SET_HUB_ONLINE t;
18621 				Zero(&t, sizeof(t));
18622 				// Confirmation message
18623 				if (MsgBoxEx(hWnd, MB_ICONQUESTION | MB_YESNO | MB_DEFBUTTON2,
18624 					_UU("CM_OFFLINE_MSG"), s) == IDYES)
18625 				{
18626 					UniToStr(t.HubName, sizeof(t.HubName), s);
18627 					t.Online = false;
18628 					if (CALL(hWnd, ScSetHubOnline(p->Rpc, &t)))
18629 					{
18630 						SmServerDlgRefresh(hWnd, p);
18631 					}
18632 				}
18633 				Free(s);
18634 			}
18635 			break;
18636 
18637 		case B_HUB_STATUS:
18638 			// Status of HUB
18639 			s = LvGetSelectedStr(hWnd, L_HUB, 0);
18640 			if (s != NULL)
18641 			{
18642 				wchar_t tmp[MAX_SIZE];
18643 				char *hubname = CopyUniToStr(s);
18644 				UniFormat(tmp, sizeof(tmp), _UU("SM_HUB_STATUS_CAPTION"), s);
18645 				SmStatusDlg(hWnd, p, hubname, false, true, tmp, ICO_HUB,
18646 					NULL, SmRefreshHubStatus);
18647 				Free(hubname);
18648 				Free(s);
18649 			}
18650 			break;
18651 
18652 		case B_CREATE:
18653 			// Create a HUB
18654 			if (SmCreateHubDlg(hWnd, p))
18655 			{
18656 				SmServerDlgRefresh(hWnd, p);
18657 			}
18658 			break;
18659 
18660 		case B_EDIT:
18661 			// Edit the HUB
18662 			s = LvGetSelectedStr(hWnd, L_HUB, 0);
18663 			if (s != NULL)
18664 			{
18665 				char *name = CopyUniToStr(s);
18666 				if (SmEditHubDlg(hWnd, p, name))
18667 				{
18668 					SmServerDlgRefresh(hWnd, p);
18669 				}
18670 				Free(name);
18671 				Free(s);
18672 			}
18673 			break;
18674 
18675 		case B_DELETE:
18676 			// Delete the HUB
18677 			s = LvGetSelectedStr(hWnd, L_HUB, 0);
18678 			if (s != NULL)
18679 			{
18680 				char *name = CopyUniToStr(s);
18681 				RPC_DELETE_HUB t;
18682 				Zero(&t, sizeof(t));
18683 				StrCpy(t.HubName, sizeof(t.HubName), name);
18684 				if (MsgBoxEx(hWnd, MB_ICONEXCLAMATION | MB_YESNO | MB_DEFBUTTON2, _UU("CM_DELETE_HUB_MSG"), name) == IDYES)
18685 				{
18686 					if (CALL(hWnd, ScDeleteHub(p->Rpc, &t)))
18687 					{
18688 						SmServerDlgRefresh(hWnd, p);
18689 						MsgBoxEx(hWnd, MB_ICONINFORMATION, _UU("CM_HUB_DELETED_MSG"), name);
18690 					}
18691 				}
18692 				Free(name);
18693 				Free(s);
18694 			}
18695 			break;
18696 
18697 		case B_CREATE_LISTENER:
18698 			// Create a listener
18699 			if (SmCreateListenerDlg(hWnd, p))
18700 			{
18701 				SmServerDlgRefresh(hWnd, p);
18702 			}
18703 			break;
18704 
18705 		case B_DELETE_LISTENER:
18706 			// Remove the listener
18707 			i = LvGetSelected(hWnd, L_LISTENER);
18708 			if (i != INFINITE)
18709 			{
18710 				UINT port = (UINT)LvGetParam(hWnd, L_LISTENER, i);
18711 				if (MsgBoxEx(hWnd, MB_ICONEXCLAMATION | MB_YESNO | MB_DEFBUTTON2, _UU("CM_DELETE_LISTENER_MSG"), port) == IDYES)
18712 				{
18713 					RPC_LISTENER t;
18714 					Zero(&t, sizeof(t));
18715 					t.Enable = false;
18716 					t.Port = port;
18717 
18718 					if (CALL(hWnd, ScDeleteListener(p->Rpc, &t)))
18719 					{
18720 						SmServerDlgRefresh(hWnd, p);
18721 					}
18722 				}
18723 			}
18724 			break;
18725 
18726 		case B_START:
18727 			// Start
18728 			i = LvGetSelected(hWnd, L_LISTENER);
18729 			if (i != INFINITE)
18730 			{
18731 				UINT port = (UINT)LvGetParam(hWnd, L_LISTENER, i);
18732 				RPC_LISTENER t;
18733 				Zero(&t, sizeof(t));
18734 				t.Enable = true;
18735 				t.Port = port;
18736 
18737 				if (CALL(hWnd, ScEnableListener(p->Rpc, &t)))
18738 				{
18739 					SmServerDlgRefresh(hWnd, p);
18740 				}
18741 			}
18742 			break;
18743 
18744 		case B_STOP:
18745 			// Stop
18746 			i = LvGetSelected(hWnd, L_LISTENER);
18747 			if (i != INFINITE)
18748 			{
18749 				UINT port = (UINT)LvGetParam(hWnd, L_LISTENER, i);
18750 				if (MsgBoxEx(hWnd, MB_ICONQUESTION | MB_YESNO | MB_DEFBUTTON2, _UU("CM_STOP_LISTENER_MSG"), port) == IDYES)
18751 				{
18752 					RPC_LISTENER t;
18753 					Zero(&t, sizeof(t));
18754 					t.Enable = false;
18755 					t.Port = port;
18756 
18757 					if (CALL(hWnd, ScEnableListener(p->Rpc, &t)))
18758 					{
18759 						SmServerDlgRefresh(hWnd, p);
18760 					}
18761 				}
18762 			}
18763 			break;
18764 
18765 		case B_SSL:
18766 			// SSL related
18767 			SmSslDlg(hWnd, p);
18768 			break;
18769 
18770 		case B_STATUS:
18771 			// Server status
18772 			SmStatusDlg(hWnd, p, p, false, true, _UU("SM_SERVER_STATUS"), ICO_VPNSERVER,
18773 				NULL, SmRefreshServerStatus);
18774 			break;
18775 
18776 		case B_INFO:
18777 			// Server Information
18778 			SmStatusDlg(hWnd, p, p, false, false, _UU("SM_INFO_TITLE"), ICO_VPNSERVER,
18779 				NULL, SmRefreshServerInfo);
18780 			break;
18781 
18782 		case B_BRIDGE:
18783 			// Local bridge configuration
18784 			SmBridgeDlg(hWnd, p);
18785 			SmServerDlgRefresh(hWnd, p);
18786 			break;
18787 
18788 		case B_FARM:
18789 			// Server farm
18790 			if (SmFarmDlg(hWnd, p))
18791 			{
18792 				// Close the dialog if the server farm configuration has changed
18793 				Close(hWnd);
18794 			}
18795 			break;
18796 
18797 		case B_FARM_STATUS:
18798 			// Server farm status
18799 			if (p->ServerType == SERVER_TYPE_FARM_CONTROLLER)
18800 			{
18801 				Dialog(hWnd, D_SM_FARM_MEMBER, SmFarmMemberDlgProc, p);
18802 			}
18803 			else if (p->ServerType == SERVER_TYPE_FARM_MEMBER)
18804 			{
18805 				SmStatusDlg(hWnd, p, NULL, false, true, _UU("SM_FC_STATUS_CAPTION"),
18806 					ICO_FARM, NULL, SmRefreshFarmConnectionInfo);
18807 			}
18808 			break;
18809 
18810 		case B_CONNECTION:
18811 			// TCP connection list
18812 			SmConnectionDlg(hWnd, p);
18813 			break;
18814 
18815 		case B_REFRESH:
18816 			// Update to the latest state
18817 			SmServerDlgRefresh(hWnd, p);
18818 			break;
18819 
18820 		case B_CONFIG:
18821 			// Config edit
18822 			SmConfig(hWnd, p);
18823 			break;
18824 
18825 		case B_L3:
18826 			// L3 switch
18827 			SmL3(hWnd, p);
18828 			break;
18829 
18830 		case B_LICENSE:
18831 			// Add or Remove license
18832 			SmLicense(hWnd, p);
18833 			SmServerDlgUpdate(hWnd, p);
18834 			break;
18835 
18836 		case B_IPSEC:
18837 			// IPsec Settings
18838 			SmIPsec(hWnd, p);
18839 			break;
18840 
18841 		case B_OPENVPN:
18842 			// OpenVPN, SSTP setting
18843 			SmOpenVpn(hWnd, p);
18844 			break;
18845 
18846 		case B_DDNS:
18847 			// DDNS setting
18848 			if (SmDDns(hWnd, p, false, false))
18849 			{
18850 				SmServerDlgRefresh(hWnd, p);
18851 			}
18852 			break;
18853 
18854 		case B_AZURE:
18855 			// VPN Azure setting
18856 			SmAzure(hWnd, p, false);
18857 
18858 			SmServerDlgRefresh(hWnd, p);
18859 			break;
18860 
18861 
18862 		case IDCANCEL:
18863 			Close(hWnd);
18864 			break;
18865 		}
18866 		break;
18867 
18868 	case WM_CLOSE:
18869 		{
18870 			// Save the HUB that was selected last
18871 			char *hubname = NULL;
18872 			char tmp[MAX_SIZE];
18873 
18874 
18875 			Format(tmp, sizeof(tmp), "%s:%u:%s", p->CurrentSetting->ClientOption.Hostname,
18876 				p->CurrentSetting->ClientOption.Port,
18877 				p->CurrentSetting->ServerAdminMode ? "" : p->CurrentSetting->HubName);
18878 
18879 			if (LvIsSingleSelected(hWnd, L_HUB))
18880 			{
18881 				hubname = LvGetSelectedStrA(hWnd, L_HUB, 0);
18882 			}
18883 
18884 			if (IsEmptyStr(hubname) == false)
18885 			{
18886 				MsRegWriteStr(REG_CURRENT_USER, SM_LASTHUB_REG_KEY, tmp, hubname);
18887 			}
18888 			else
18889 			{
18890 				MsRegDeleteValue(REG_CURRENT_USER, SM_LASTHUB_REG_KEY, tmp);
18891 			}
18892 
18893 			Free(hubname);
18894 
18895 			EndDialog(hWnd, false);
18896 		}
18897 		break;
18898 
18899 	case WM_NOTIFY:
18900 		n = (NMHDR *)lParam;
18901 		switch (n->idFrom)
18902 		{
18903 		case L_HUB:
18904 			switch (n->code)
18905 			{
18906 			case LVN_ITEMCHANGED:
18907 				SmServerDlgUpdate(hWnd, p);
18908 				break;
18909 			}
18910 			break;
18911 		case L_LISTENER:
18912 			switch (n->code)
18913 			{
18914 			case LVN_ITEMCHANGED:
18915 				SmServerDlgUpdate(hWnd, p);
18916 				break;
18917 			}
18918 			break;
18919 		}
18920 		break;
18921 
18922 	case WM_TIMER:
18923 		switch (wParam)
18924 		{
18925 		case 1:
18926 			KillTimer(hWnd, 1);
18927 
18928 			if (p->ServerAdminMode)
18929 			{
18930 				// Prompt the registration if the license key is not registered
18931 				RPC_LICENSE_STATUS t;
18932 
18933 				Zero(&t, sizeof(t));
18934 				if (p->Bridge == false && GetCapsBool(p->CapsList, "b_support_license"))
18935 				{
18936 					if (ScGetLicenseStatus(p->Rpc, &t) == ERR_NO_ERROR)
18937 					{
18938 						if (t.EditionId == LICENSE_EDITION_VPN3_NO_LICENSE || (t.NeedSubscription && t.SubscriptionExpires == 0))
18939 						{
18940 							// Valid license key is not registered
18941 
18942 							if (MsgBox(hWnd, MB_YESNO | MB_ICONINFORMATION,
18943 								_UU("SM_SETUP_NO_LICENSE_KEY")) == IDYES)
18944 							{
18945 								SmLicense(hWnd, p);
18946 							}
18947 						}
18948 					}
18949 				}
18950 			}
18951 
18952 			SetTimer(hWnd, 2, 150, NULL);
18953 			break;
18954 
18955 		case 2:
18956 			// Setup
18957 			KillTimer(hWnd, 2);
18958 
18959 			if (SmSetupIsNew(p))
18960 			{
18961 				if (SmSetup(hWnd, p))
18962 				{
18963 					SmServerDlgRefresh(hWnd, p);
18964 				}
18965 			}
18966 
18967 			SmShowIPSecMessageIfNecessary(hWnd, p);
18968 
18969 			SmShowCertRegenerateMessageIfNecessary(hWnd, p);
18970 
18971 			SetTimer(hWnd, 3, 150, NULL);
18972 			break;
18973 
18974 		case 3:
18975 			// Message for Administrators
18976 			KillTimer(hWnd, 3);
18977 
18978 			if (UniIsEmptyStr(p->AdminMsg) == false)
18979 			{
18980 				wchar_t tmp[MAX_SIZE];
18981 
18982 				UniFormat(tmp, sizeof(tmp), _UU("SM_SERVER_ADMIN_MSG"), p->ServerName);
18983 				OnceMsg(hWnd, tmp, p->AdminMsg, true, ICO_VPNSERVER);
18984 			}
18985 			break;
18986 		}
18987 		break;
18988 	}
18989 
18990 	LvStandardHandler(hWnd, msg, wParam, lParam, L_HUB);
18991 
18992 	return 0;
18993 }
18994 
18995 // Display the message about the cert
SmShowCertRegenerateMessageIfNecessary(HWND hWnd,SM_SERVER * p)18996 void SmShowCertRegenerateMessageIfNecessary(HWND hWnd, SM_SERVER *p)
18997 {
18998 	// Validate arguments
18999 	if (p == NULL)
19000 	{
19001 		return;
19002 	}
19003 
19004 	if (p->ServerAdminMode && p->Bridge == false)
19005 	{
19006 		RPC_KEY_PAIR t;
19007 
19008 		Zero(&t, sizeof(t));
19009 
19010 		if (ScGetServerCert(p->Rpc, &t) == ERR_NO_ERROR)
19011 		{
19012 			if (t.Cert != NULL && t.Cert->has_basic_constraints == false)
19013 			{
19014 				if (t.Cert->root_cert)
19015 				{
19016 					if (MsRegReadInt(REG_CURRENT_USER, SM_HIDE_CERT_UPDATE_MSG_KEY, p->ServerName) == 0)
19017 					{
19018 						if (MsgBox(hWnd, MB_ICONQUESTION | MB_YESNO, _UU("SM_CERT_MESSAGE")) == IDYES)
19019 						{
19020 							X *x;
19021 							K *k;
19022 
19023 							// Regenerating the certificate
19024 							if (SmRegenerateServerCert(hWnd, p, NULL, &x, &k, false))
19025 							{
19026 								// Confirmation message
19027 								if (MsgBox(hWnd, MB_ICONEXCLAMATION | MB_YESNO, _UU("SM_REGENERATE_CERT_MSG")) == IDYES)
19028 								{
19029 									// Set the new certificate and private key
19030 									RPC_KEY_PAIR t2;
19031 
19032 									Zero(&t2, sizeof(t2));
19033 
19034 									t2.Cert = CloneX(x);
19035 									t2.Key = CloneK(k);
19036 
19037 									if (CALL(hWnd, ScSetServerCert(p->Rpc, &t2)))
19038 									{
19039 										FreeRpcKeyPair(&t2);
19040 
19041 										MsgBox(hWnd, MB_ICONINFORMATION, _UU("CM_CERT_SET_MSG"));
19042 									}
19043 								}
19044 
19045 								FreeX(x);
19046 								FreeK(k);
19047 							}
19048 						}
19049 						else
19050 						{
19051 							MsRegWriteInt(REG_CURRENT_USER, SM_HIDE_CERT_UPDATE_MSG_KEY, p->ServerName, 1);
19052 						}
19053 					}
19054 				}
19055 			}
19056 
19057 			FreeRpcKeyPair(&t);
19058 		}
19059 	}
19060 }
19061 
19062 // Display messages about IPsec, and prompt for the setting
SmShowIPSecMessageIfNecessary(HWND hWnd,SM_SERVER * p)19063 void SmShowIPSecMessageIfNecessary(HWND hWnd, SM_SERVER *p)
19064 {
19065 	// Validate arguments
19066 	if (p == NULL)
19067 	{
19068 		return;
19069 	}
19070 
19071 	if (GetCapsBool(p->CapsList, "b_support_vgs_in_client") == false)
19072 	{
19073 		if (GetCapsBool(p->CapsList, "b_support_ipsec") && p->IPsecMessageDisplayed == false)
19074 		{
19075 			// Display a message about IPsec
19076 			RPC_TEST flag;
19077 
19078 			if (MsgBox(hWnd, MB_ICONQUESTION | MB_YESNO, _UU("SM_IPSEC_SETUP_QUESTION")) == IDYES)
19079 			{
19080 				// Display the IPsec dialog
19081 				SmIPsec(hWnd, p);
19082 			}
19083 
19084 			Zero(&flag, sizeof(flag));
19085 			flag.IntValue = 9;
19086 			ToStr(flag.StrValue, 1);
19087 
19088 			ScDebug(p->Rpc, &flag);
19089 
19090 			p->IPsecMessageDisplayed = true;
19091 		}
19092 
19093 	}
19094 }
19095 
19096 // Connection
SmConnect(HWND hWnd,SETTING * s)19097 void SmConnect(HWND hWnd, SETTING *s)
19098 {
19099 	SmConnectEx(hWnd, s, false);
19100 }
SmConnectEx(HWND hWnd,SETTING * s,bool is_in_client)19101 void SmConnectEx(HWND hWnd, SETTING *s, bool is_in_client)
19102 {
19103 	bool ok;
19104 	RPC *rpc;
19105 	char *pass;
19106 	bool empty_password = false;
19107 	bool first_bad_password = false;
19108 	// Validate arguments
19109 	if (s == NULL)
19110 	{
19111 		return;
19112 	}
19113 
19114 	// Disable the control
19115 	Disable(hWnd, L_SETTING);
19116 	Disable(hWnd, B_NEW_SETTING);
19117 	Disable(hWnd, B_EDIT_SETTING);
19118 	Disable(hWnd, B_DELETE);
19119 	Disable(hWnd, IDOK);
19120 	Disable(hWnd, B_ABOUT);
19121 	Disable(hWnd, IDCANCEL);
19122 	Disable(hWnd, B_SECURE_MANAGER);
19123 	Disable(hWnd, B_SELECT_SECURE);
19124 	Disable(hWnd, B_CERT_TOOL);
19125 
19126 	ok = true;
19127 
19128 	if (IsZero(s->HashedPassword, SHA1_SIZE))
19129 	{
19130 		// Password input screen
19131 ENTER_PASSWORD:
19132 		pass = SmPassword(hWnd, s->ClientOption.Hostname);
19133 		if (pass != NULL)
19134 		{
19135 			Hash(s->HashedPassword, pass, StrLen(pass), true);
19136 			Free(pass);
19137 			ok = true;
19138 		}
19139 		else
19140 		{
19141 			ok = false;
19142 		}
19143 	}
19144 
19145 	if (ok)
19146 	{
19147 		UINT err = ERR_INTERNAL_ERROR;
19148 		// Connection
19149 		rpc = AdminConnectEx2(sm->Cedar, &s->ClientOption, s->ServerAdminMode ? "" : s->HubName, s->HashedPassword, &err, NULL,
19150 			hWnd);
19151 		if (rpc == NULL)
19152 		{
19153 			// An error has occured
19154 			if (err != ERR_ACCESS_DENIED || first_bad_password)
19155 			{
19156 				MsgBox(hWnd, MB_ICONSTOP, _E(err));
19157 			}
19158 			if (err == ERR_ACCESS_DENIED)
19159 			{
19160 				// Password incorrect
19161 				first_bad_password = true;
19162 				goto ENTER_PASSWORD;
19163 			}
19164 			else
19165 			{
19166 				// Other errors
19167 			}
19168 		}
19169 		else
19170 		{
19171 			UCHAR test[SHA1_SIZE];
19172 			SM_SERVER p;
19173 			RPC_SERVER_STATUS status;
19174 			RPC_SERVER_INFO info;
19175 			SETTING *setting;
19176 			RPC_MSG msg;
19177 			RPC_TEST flag;
19178 			bool cancel = false;
19179 
19180 			Hash(test, "", 0, true);
19181 
19182 			if (Cmp(test, s->HashedPassword, SHA1_SIZE) == 0 || Cmp(test, rpc->VpnServerHashedPassword, SHA1_SIZE) == 0)
19183 			{
19184 				empty_password = true;
19185 			}
19186 
19187 			if (sm->TempSetting == NULL)
19188 			{
19189 				setting = SmGetSetting(s->Title);
19190 				if (setting != NULL)
19191 				{
19192 					if (IsZero(setting->HashedPassword, SHA1_SIZE) == false)
19193 					{
19194 						Copy(setting->HashedPassword, s->HashedPassword, SHA1_SIZE);
19195 						SmWriteSettingList();
19196 					}
19197 				}
19198 			}
19199 
19200 			rpc->ServerAdminMode = s->ServerAdminMode;
19201 			if (s->ServerAdminMode == false)
19202 			{
19203 				StrCpy(rpc->HubName, sizeof(rpc->HubName), s->HubName);
19204 			}
19205 
19206 			Zero(&p, sizeof(p));
19207 			p.IsInClient = is_in_client;
19208 			p.CurrentSetting = s;
19209 			p.Rpc = rpc;
19210 			p.ServerAdminMode = rpc->ServerAdminMode;
19211 			StrCpy(p.ServerName, sizeof(p.ServerName), s->ClientOption.Hostname);
19212 			if (p.ServerAdminMode == false)
19213 			{
19214 				StrCpy(p.HubName, sizeof(p.HubName), rpc->HubName);
19215 			}
19216 			UniStrCpy(p.Title, sizeof(p.Title), s->Title);
19217 
19218 			// Get the type of server
19219 			Zero(&status, sizeof(status));
19220 			ScGetServerStatus(rpc, &status);
19221 
19222 			p.ServerType = status.ServerType;
19223 
19224 			Zero(&info, sizeof(info));
19225 			ScGetServerInfo(rpc, &info);
19226 
19227 			Copy(&p.ServerInfo, &info, sizeof(RPC_SERVER_INFO));
19228 			Copy(&p.ServerStatus, &status, sizeof(RPC_SERVER_STATUS));
19229 
19230 			// Get the Admin Msg
19231 			Zero(&msg, sizeof(msg));
19232 			if (ScGetAdminMsg(rpc, &msg) == ERR_NO_ERROR)
19233 			{
19234 				p.AdminMsg = UniCopyStr(msg.Msg);
19235 				FreeRpcMsg(&msg);
19236 			}
19237 
19238 			// IPsec related
19239 			Zero(&flag, sizeof(flag));
19240 			flag.IntValue = 8;
19241 			if (ScDebug(rpc, &flag) == ERR_NO_ERROR)
19242 			{
19243 				p.IPsecMessageDisplayed = ToInt(flag.StrValue);
19244 			}
19245 			else
19246 			{
19247 				p.IPsecMessageDisplayed = true;
19248 			}
19249 
19250 			// VGS related
19251 			Zero(&flag, sizeof(flag));
19252 			flag.IntValue = 10;
19253 			if (ScDebug(rpc, &flag) == ERR_NO_ERROR)
19254 			{
19255 				p.VgsMessageDisplayed = ToInt(flag.StrValue);
19256 			}
19257 			else
19258 			{
19259 				p.VgsMessageDisplayed = true;
19260 			}
19261 
19262 			// Get the Caps
19263 			p.CapsList = ScGetCapsEx(p.Rpc);
19264 
19265 			p.Bridge = GetCapsBool(p.CapsList, "b_bridge");
19266 
19267 			if (GetCapsBool(p.CapsList, "b_support_policy_ver_3"))
19268 			{
19269 				p.PolicyVer = 3;
19270 			}
19271 			else
19272 			{
19273 				p.PolicyVer = 2;
19274 			}
19275 
19276 			if (empty_password && s->ServerAdminMode)
19277 			{
19278 				// Make the user set a password when a password empty (In the case of server management mode)
19279 				if (Dialog(hWnd, D_SM_CHANGE_PASSWORD, SmChangeServerPasswordDlg, &p) == 0)
19280 				{
19281 					cancel = true;
19282 				}
19283 			}
19284 
19285 			// Server management screen
19286 			if (cancel == false)
19287 			{
19288 				// Update notification initialization
19289 				WINUI_UPDATE *update = NULL;
19290 
19291 				if (p.ServerAdminMode && is_in_client == false)
19292 				{
19293 					wchar_t update_software_title[MAX_SIZE];
19294 					char update_software_name[MAX_SIZE];
19295 					char server_name_safe[MAX_HOST_NAME_LEN + 1];
19296 					char family_name[128];
19297 
19298 					MakeSafeFileName(server_name_safe, sizeof(server_name_safe), p.ServerName);
19299 					Format(update_software_name, sizeof(update_software_name), (p.Bridge ? NAME_OF_VPN_BRIDGE_TARGET : NAME_OF_VPN_SERVER_TARGET), server_name_safe);
19300 					StrLower(update_software_name);
19301 					Trim(update_software_name);
19302 
19303 					Zero(family_name, sizeof(family_name));
19304 					StrCpy(family_name, sizeof(family_name), p.ServerInfo.ServerFamilyName);
19305 
19306 					if (IsEmptyStr(family_name))
19307 					{
19308 						if (InStr(p.ServerInfo.ServerProductName, "PacketiX"))
19309 						{
19310 							StrCpy(family_name, sizeof(family_name), "PacketiX");
19311 						}
19312 						else if (InStr(p.ServerInfo.ServerProductName, "UT-VPN") ||
19313 							InStr(p.ServerInfo.ServerProductName, "SoftEther"))
19314 						{
19315 							StrCpy(family_name, sizeof(family_name), "softether");
19316 						}
19317 					}
19318 
19319 					if (IsEmptyStr(family_name) == false)
19320 					{
19321 						UniFormat(update_software_title, sizeof(update_software_title), _UU(p.Bridge ? "SM_UPDATE_CHECK_TITLE_VPNBRIDGE" : "SM_UPDATE_CHECK_TITLE_VPNSERVER"),
19322 							family_name, p.ServerName);
19323 
19324 						update = InitUpdateUi(update_software_title, update_software_name, family_name, p.ServerInfo.ServerBuildDate,
19325 							p.ServerInfo.ServerBuildInt, p.ServerInfo.ServerVerInt, NULL, false);
19326 					}
19327 				}
19328 
19329 				p.Update = update;
19330 
19331 				// Main screen
19332 				Dialog(hWnd, D_SM_SERVER, SmServerDlgProc, &p);
19333 
19334 				if (p.Update != NULL)
19335 				{
19336 					FreeUpdateUi(p.Update);
19337 					p.Update = NULL;
19338 				}
19339 			}
19340 
19341 			// Disconnect
19342 			AdminDisconnect(rpc);
19343 
19344 			// Release the Caps
19345 			FreeCapsList(p.CapsList);
19346 
19347 			Free(p.AdminMsg);
19348 			p.AdminMsg = NULL;
19349 
19350 			FreeRpcServerInfo(&info);
19351 		}
19352 	}
19353 
19354 	// Enable the control
19355 	Enable(hWnd, L_SETTING);
19356 	Enable(hWnd, B_NEW_SETTING);
19357 	Enable(hWnd, B_EDIT_SETTING);
19358 	Enable(hWnd, B_DELETE);
19359 	Enable(hWnd, IDOK);
19360 	Enable(hWnd, B_ABOUT);
19361 	Enable(hWnd, IDCANCEL);
19362 
19363 	if (MsIsWine() == false)
19364 	{
19365 		Enable(hWnd, B_SECURE_MANAGER);
19366 		Enable(hWnd, B_SELECT_SECURE);
19367 	}
19368 
19369 	Enable(hWnd, B_CERT_TOOL);
19370 }
19371 
19372 // Password input dialog
SmPassword(HWND hWnd,char * server_name)19373 char *SmPassword(HWND hWnd, char *server_name)
19374 {
19375 	char *ret;
19376 	UI_PASSWORD_DLG p;
19377 	// Validate arguments
19378 	if (server_name == NULL)
19379 	{
19380 		return NULL;
19381 	}
19382 
19383 	Zero(&p, sizeof(p));
19384 	p.AdminMode = true;
19385 	StrCpy(p.ServerName, sizeof(p.ServerName), server_name);
19386 
19387 	if (PasswordDlg(hWnd, &p) == false)
19388 	{
19389 		return NULL;
19390 	}
19391 
19392 	ret = CopyStr(p.Password);
19393 
19394 	return ret;
19395 }
19396 
19397 // Configuration editing dialog initialization
SmEditSettingDlgInit(HWND hWnd,SM_EDIT_SETTING * p)19398 void SmEditSettingDlgInit(HWND hWnd, SM_EDIT_SETTING *p)
19399 {
19400 	SETTING *s;
19401 	// Validate arguments
19402 	if (hWnd == NULL || p == NULL)
19403 	{
19404 		return;
19405 	}
19406 
19407 	p->Inited = false;
19408 
19409 	s = p->Setting;
19410 
19411 	// Title
19412 	if (p->EditMode == false)
19413 	{
19414 		SetText(hWnd, 0, _UU("SM_EDIT_CAPTION_1"));
19415 	}
19416 	else
19417 	{
19418 		wchar_t tmp[MAX_SIZE];
19419 		UniFormat(tmp, sizeof(tmp), _UU("SM_EDIT_CAPTION_2"), s->Title);
19420 		SetText(hWnd, 0, tmp);
19421 	}
19422 
19423 	// Connection setting name
19424 	SetText(hWnd, E_ACCOUNT_NAME, s->Title);
19425 
19426 	// Host name
19427 	SetTextA(hWnd, E_HOSTNAME, s->ClientOption.Hostname);
19428 
19429 	// Port number
19430 	CbSetHeight(hWnd, C_PORT, 18);
19431 	CbAddStr(hWnd, C_PORT, _UU("CM_PORT_1"), 0);
19432 	CbAddStr(hWnd, C_PORT, _UU("CM_PORT_2"), 0);
19433 	CbAddStr(hWnd, C_PORT, _UU("CM_PORT_3"), 0);
19434 	CbAddStr(hWnd, C_PORT, _UU("CM_PORT_4"), 0);
19435 	SetIntEx(hWnd, C_PORT, s->ClientOption.Port);
19436 
19437 	// Proxy Settings
19438 	Check(hWnd, R_DIRECT_TCP, s->ClientOption.ProxyType == PROXY_DIRECT);
19439 	Check(hWnd, R_HTTPS, s->ClientOption.ProxyType == PROXY_HTTP);
19440 	Check(hWnd, R_SOCKS, s->ClientOption.ProxyType == PROXY_SOCKS);
19441 
19442 	// Management mode setting
19443 	Check(hWnd, R_SERVER_ADMIN, s->ServerAdminMode);
19444 	Check(hWnd, R_HUB_ADMIN, s->ServerAdminMode == false ? true : false);
19445 	CbSetHeight(hWnd, C_HUBNAME, 18);
19446 	SetTextA(hWnd, C_HUBNAME, s->HubName);
19447 
19448 	// Password
19449 	if (IsZero(s->HashedPassword, SHA1_SIZE))
19450 	{
19451 		Check(hWnd, R_NO_SAVE, true);
19452 	}
19453 	else
19454 	{
19455 		UCHAR test[SHA1_SIZE];
19456 
19457 		Hash(test, "", 0, true);
19458 		if (Cmp(test, s->HashedPassword, SHA1_SIZE) != 0)
19459 		{
19460 			SetTextA(hWnd, E_PASSWORD, HIDDEN_PASSWORD);
19461 		}
19462 	}
19463 
19464 	if (p->EditMode == false)
19465 	{
19466 		FocusEx(hWnd, E_ACCOUNT_NAME);
19467 	}
19468 	else
19469 	{
19470 		FocusEx(hWnd, E_HOSTNAME);
19471 	}
19472 
19473 	p->Inited = true;
19474 
19475 	// Start enumerating the Virtual HUBs
19476 	CmEnumHubStart(hWnd, &s->ClientOption);
19477 
19478 	SmEditSettingDlgUpdate(hWnd, p);
19479 }
19480 
19481 // Configuration editing dialog update
SmEditSettingDlgUpdate(HWND hWnd,SM_EDIT_SETTING * p)19482 void SmEditSettingDlgUpdate(HWND hWnd, SM_EDIT_SETTING *p)
19483 {
19484 	bool ok = true;
19485 	UINT delete_hub_list = 0;
19486 	SETTING *s;
19487 	char tmp[MAX_SIZE];
19488 	// Validate arguments
19489 	if (hWnd == NULL || p == NULL || p->Inited == false)
19490 	{
19491 		return;
19492 	}
19493 
19494 	s = p->Setting;
19495 
19496 	GetTxt(hWnd, E_ACCOUNT_NAME, s->Title, sizeof(s->Title));
19497 	UniTrim(s->Title);
19498 
19499 	if (UniStrLen(s->Title) == 0)
19500 	{
19501 		ok = false;
19502 	}
19503 
19504 	if (IsChecked(hWnd, R_LOCALHOST))
19505 	{
19506 		SetTextA(hWnd, E_HOSTNAME, "localhost");
19507 		Disable(hWnd, E_HOSTNAME);
19508 	}
19509 	else
19510 	{
19511 		Enable(hWnd, E_HOSTNAME);
19512 	}
19513 
19514 	GetTxtA(hWnd, E_HOSTNAME, tmp, sizeof(tmp));
19515 	Trim(tmp);
19516 
19517 	if (StrCmpi(tmp, s->ClientOption.Hostname) != 0)
19518 	{
19519 		delete_hub_list++;
19520 	}
19521 
19522 	StrCpy(s->ClientOption.Hostname, sizeof(s->ClientOption.Hostname), tmp);
19523 
19524 	if (StrLen(s->ClientOption.Hostname) == 0)
19525 	{
19526 		ok = false;
19527 	}
19528 
19529 	s->ClientOption.Port = GetInt(hWnd, C_PORT);
19530 	if (s->ClientOption.Port == 0)
19531 	{
19532 		ok = false;
19533 	}
19534 
19535 	if (IsChecked(hWnd, R_DIRECT_TCP))
19536 	{
19537 		s->ClientOption.ProxyType = PROXY_DIRECT;
19538 	}
19539 	else if (IsChecked(hWnd, R_HTTPS))
19540 	{
19541 		s->ClientOption.ProxyType = PROXY_HTTP;
19542 	}
19543 	else
19544 	{
19545 		s->ClientOption.ProxyType = PROXY_SOCKS;
19546 	}
19547 
19548 	SetEnable(hWnd, B_PROXY_CONFIG, s->ClientOption.ProxyType != PROXY_DIRECT);
19549 
19550 	if (s->ClientOption.ProxyType != PROXY_DIRECT)
19551 	{
19552 		if (StrLen(s->ClientOption.ProxyName) == 0)
19553 		{
19554 			ok = false;
19555 		}
19556 		if (s->ClientOption.ProxyPort == 0)
19557 		{
19558 			ok = false;
19559 		}
19560 	}
19561 
19562 	s->ServerAdminMode = IsChecked(hWnd, R_SERVER_ADMIN);
19563 
19564 	SetEnable(hWnd, C_HUBNAME, s->ServerAdminMode == false ? true : false);
19565 	SetEnable(hWnd, S_HUBNAME, s->ServerAdminMode == false ? true : false);
19566 
19567 	GetTxtA(hWnd, C_HUBNAME, s->HubName, sizeof(s->HubName));
19568 	Trim(s->HubName);
19569 	if (StrLen(s->HubName) == 0)
19570 	{
19571 		if (s->ServerAdminMode == false)
19572 		{
19573 			ok = false;
19574 		}
19575 	}
19576 
19577 	if (IsChecked(hWnd, R_NO_SAVE))
19578 	{
19579 		Zero(s->HashedPassword, SHA1_SIZE);
19580 		SetTextA(hWnd, E_PASSWORD, "");
19581 		Disable(hWnd, E_PASSWORD);
19582 		Disable(hWnd, S_PASSWORD);
19583 	}
19584 	else
19585 	{
19586 		char tmp[MAX_PASSWORD_LEN + 1];
19587 		Enable(hWnd, E_PASSWORD);
19588 		Enable(hWnd, S_PASSWORD);
19589 		GetTxtA(hWnd, E_PASSWORD, tmp, sizeof(tmp));
19590 		if (StrCmp(tmp, HIDDEN_PASSWORD) != 0)
19591 		{
19592 			Hash(s->HashedPassword, tmp, StrLen(tmp), true);
19593 		}
19594 	}
19595 
19596 	if (delete_hub_list)
19597 	{
19598 		CbReset(hWnd, C_HUBNAME);
19599 	}
19600 
19601 	SetEnable(hWnd, IDOK, ok);
19602 }
19603 
19604 // Configuration Edit dialog OK button
SmEditSettingDlgOnOk(HWND hWnd,SM_EDIT_SETTING * p)19605 void SmEditSettingDlgOnOk(HWND hWnd, SM_EDIT_SETTING *p)
19606 {
19607 	// Validate arguments
19608 	if (hWnd == NULL || p == NULL)
19609 	{
19610 		return;
19611 	}
19612 
19613 	if (p->EditMode == false)
19614 	{
19615 		// Register new
19616 		SETTING *s = ZeroMalloc(sizeof(SETTING));
19617 		Copy(s, p->Setting, sizeof(SETTING));
19618 		if (SmAddSetting(s) == false)
19619 		{
19620 			MsgBoxEx(hWnd, MB_ICONEXCLAMATION, _UU("SM_SETTING_EXISTS"), s->Title);
19621 			Free(s);
19622 			FocusEx(hWnd, E_ACCOUNT_NAME);
19623 			return;
19624 		}
19625 		EndDialog(hWnd, true);
19626 	}
19627 	else
19628 	{
19629 		// Update registration
19630 		SETTING *t = SmGetSetting(p->Setting->Title);
19631 		if (t != NULL && t != p->OldSetting)
19632 		{
19633 			MsgBoxEx(hWnd, MB_ICONEXCLAMATION, _UU("SM_SETTING_EXISTS"), p->Setting->Title);
19634 			FocusEx(hWnd, E_ACCOUNT_NAME);
19635 			return;
19636 		}
19637 
19638 		Copy(p->OldSetting, p->Setting, sizeof(SETTING));
19639 		Sort(sm->SettingList);
19640 		SmWriteSettingList();
19641 
19642 		EndDialog(hWnd, true);
19643 	}
19644 }
19645 
19646 // Settings add / edit dialog
SmEditSettingDlgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)19647 UINT SmEditSettingDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
19648 {
19649 	SM_EDIT_SETTING *p = (SM_EDIT_SETTING *)param;
19650 	// Validate arguments
19651 	if (hWnd == NULL)
19652 	{
19653 		return 0;
19654 	}
19655 
19656 	switch (msg)
19657 	{
19658 	case WM_INITDIALOG:
19659 		SmEditSettingDlgInit(hWnd, p);
19660 		break;
19661 
19662 	case WM_COMMAND:
19663 		switch (LOWORD(wParam))
19664 		{
19665 		case R_LOCALHOST:
19666 		case E_ACCOUNT_NAME:
19667 		case E_HOSTNAME:
19668 		case C_PORT:
19669 		case R_DIRECT_TCP:
19670 		case R_HTTPS:
19671 		case R_SOCKS:
19672 		case R_SERVER_ADMIN:
19673 		case R_HUB_ADMIN:
19674 		case C_HUBNAME:
19675 		case E_PASSWORD:
19676 		case R_NO_SAVE:
19677 			SmEditSettingDlgUpdate(hWnd, p);
19678 			break;
19679 		}
19680 
19681 		if (LOWORD(wParam) == R_LOCALHOST)
19682 		{
19683 			FocusEx(hWnd, E_HOSTNAME);
19684 		}
19685 
19686 		switch (LOWORD(wParam))
19687 		{
19688 		case E_HOSTNAME:
19689 			if (HIWORD(wParam) == EN_KILLFOCUS)
19690 			{
19691 				CmEnumHubStart(hWnd, &p->Setting->ClientOption);
19692 			}
19693 			break;
19694 		case C_PORT:
19695 			if (HIWORD(wParam) == CBN_KILLFOCUS)
19696 			{
19697 				CmEnumHubStart(hWnd, &p->Setting->ClientOption);
19698 			}
19699 			break;
19700 		case R_DIRECT_TCP:
19701 		case R_HTTPS:
19702 		case R_SOCKS:
19703 			if (HIWORD(wParam) == BN_CLICKED)
19704 			{
19705 				CmEnumHubStart(hWnd, &p->Setting->ClientOption);
19706 			}
19707 			break;
19708 		}
19709 
19710 		switch (wParam)
19711 		{
19712 		case IDOK:
19713 			SmEditSettingDlgOnOk(hWnd, p);
19714 			break;
19715 
19716 		case IDCANCEL:
19717 			Close(hWnd);
19718 			break;
19719 
19720 		case B_PROXY_CONFIG:
19721 			// Proxy Settings
19722 			if (CmProxyDlg(hWnd, &p->Setting->ClientOption))
19723 			{
19724 				UINT n = GetInt(hWnd, C_PORT);
19725 				if (p->Setting->ClientOption.ProxyType == PROXY_HTTP &&
19726 					n != 443)
19727 				{
19728 					// Show a warning message if the destination port is
19729 					// other than 443 in when HTTP proxy is used
19730 					if (MsgBoxEx(hWnd, MB_ICONQUESTION | MB_YESNO, _UU("CM_HTTP_PROXY_WARNING"), n) == IDYES)
19731 					{
19732 						// Change the port number to 443
19733 						SetText(hWnd, C_PORT, _UU("CM_PORT_2"));
19734 					}
19735 				}
19736 				SmEditSettingDlgUpdate(hWnd, p);
19737 				CmEnumHubStart(hWnd, &p->Setting->ClientOption);
19738 			}
19739 			break;
19740 
19741 		case R_NO_SAVE:
19742 			if (IsChecked(hWnd, R_NO_SAVE) == false)
19743 			{
19744 				FocusEx(hWnd, E_PASSWORD);
19745 			}
19746 			break;
19747 		}
19748 		break;
19749 
19750 	case WM_CLOSE:
19751 		EndDialog(hWnd, false);
19752 		break;
19753 	}
19754 
19755 	return 0;
19756 }
19757 
19758 // Show the Settings Adding dialog
SmAddSettingDlg(HWND hWnd,wchar_t * new_name,UINT new_name_size)19759 bool SmAddSettingDlg(HWND hWnd, wchar_t *new_name, UINT new_name_size)
19760 {
19761 	SM_EDIT_SETTING p;
19762 	SETTING s;
19763 	UINT i;
19764 	bool ret;
19765 	// Validate arguments
19766 	if (hWnd == NULL || new_name == NULL)
19767 	{
19768 		return false;
19769 	}
19770 
19771 	Zero(&p, sizeof(p));
19772 	Zero(&s, sizeof(s));
19773 
19774 	s.ClientOption.Port = 443;
19775 
19776 	p.EditMode = false;
19777 	p.Setting = &s;
19778 
19779 	for (i = 1;;i++)
19780 	{
19781 		wchar_t tmp[MAX_SIZE];
19782 		if (i == 1)
19783 		{
19784 			UniFormat(tmp, sizeof(tmp), _UU("CM_NEW_ACCOUNT_NAME_1"));
19785 		}
19786 		else
19787 		{
19788 			UniFormat(tmp, sizeof(tmp), _UU("CM_NEW_ACCOUNT_NAME_2"), i);
19789 		}
19790 
19791 		if (SmGetSetting(tmp) == NULL)
19792 		{
19793 			UniStrCpy(s.Title, sizeof(s.Title), tmp);
19794 			Hash(s.HashedPassword, "", 0, true);
19795 			s.ServerAdminMode = true;
19796 			break;
19797 		}
19798 	}
19799 
19800 	ret = Dialog(hWnd, D_SM_EDIT_SETTING, SmEditSettingDlgProc, &p);
19801 
19802 	if (ret)
19803 	{
19804 		UniStrCpy(new_name, new_name_size, s.Title);
19805 	}
19806 
19807 	return ret;
19808 }
19809 
19810 // Show the settings edit dialog
SmEditSettingDlg(HWND hWnd)19811 bool SmEditSettingDlg(HWND hWnd)
19812 {
19813 	SM_EDIT_SETTING p;
19814 	SETTING s, *setting;
19815 	UINT i;
19816 	wchar_t *name;
19817 	// Validate arguments
19818 	if (hWnd == NULL)
19819 	{
19820 		return false;
19821 	}
19822 
19823 	i = LvGetSelected(hWnd, L_SETTING);
19824 	if (i == INFINITE)
19825 	{
19826 		return false;
19827 	}
19828 
19829 	name = LvGetStr(hWnd, L_SETTING, i, 0);
19830 
19831 	setting = SmGetSetting(name);
19832 	if (setting == NULL)
19833 	{
19834 		Free(name);
19835 		return false;
19836 	}
19837 
19838 	Free(name);
19839 
19840 	Copy(&s, setting, sizeof(SETTING));
19841 
19842 	Zero(&p, sizeof(p));
19843 
19844 	p.EditMode = true;
19845 	p.OldSetting = setting;
19846 	p.Setting = &s;
19847 
19848 	return Dialog(hWnd, D_SM_EDIT_SETTING, SmEditSettingDlgProc, &p);
19849 }
19850 
19851 // Update the configuration
SmCheckNewName(SETTING * s,wchar_t * new_title)19852 bool SmCheckNewName(SETTING *s, wchar_t *new_title)
19853 {
19854 	UINT i;
19855 	// Validate arguments
19856 	if (new_title == NULL)
19857 	{
19858 		return false;
19859 	}
19860 	if (s != NULL)
19861 	{
19862 		if (IsInList(sm->SettingList, s) == false)
19863 		{
19864 			return false;
19865 		}
19866 	}
19867 
19868 	// Check whether there is the same name in other
19869 	for (i = 0;i < LIST_NUM(sm->SettingList);i++)
19870 	{
19871 		SETTING *t = LIST_DATA(sm->SettingList, i);
19872 
19873 		if (s != t)
19874 		{
19875 			if (UniStrCmpi(t->Title, new_title) == 0)
19876 			{
19877 				return false;
19878 			}
19879 		}
19880 	}
19881 
19882 	return true;
19883 }
19884 
19885 // Delete the configuration
SmDeleteSetting(wchar_t * title)19886 void SmDeleteSetting(wchar_t *title)
19887 {
19888 	SETTING *s;
19889 	// Validate arguments
19890 	if (title == NULL)
19891 	{
19892 		return;
19893 	}
19894 
19895 	s = SmGetSetting(title);
19896 	if (s == NULL)
19897 	{
19898 		return;
19899 	}
19900 
19901 	Delete(sm->SettingList, s);
19902 	Free(s);
19903 	Sort(sm->SettingList);
19904 
19905 	SmWriteSettingList();
19906 }
19907 
19908 // Add the settings
SmAddSetting(SETTING * s)19909 bool SmAddSetting(SETTING *s)
19910 {
19911 	// Validate arguments
19912 	if (s == NULL)
19913 	{
19914 		return false;
19915 	}
19916 
19917 	if (SmGetSetting(s->Title) != NULL)
19918 	{
19919 		return false;
19920 	}
19921 
19922 	Insert(sm->SettingList, s);
19923 
19924 	SmWriteSettingList();
19925 
19926 	return true;
19927 }
19928 
19929 // Get the configuration
SmGetSetting(wchar_t * title)19930 SETTING *SmGetSetting(wchar_t *title)
19931 {
19932 	SETTING s;
19933 	// Validate arguments
19934 	if (title == NULL)
19935 	{
19936 		return NULL;
19937 	}
19938 
19939 	Zero(&s, sizeof(SETTING));
19940 	UniStrCpy(s.Title, sizeof(s.Title), title);
19941 
19942 	return (SETTING *)Search(sm->SettingList, &s);
19943 }
19944 
19945 // Comparison of connection settings
SmCompareSetting(void * p1,void * p2)19946 int SmCompareSetting(void *p1, void *p2)
19947 {
19948 	SETTING *s1, *s2;
19949 	if (p1 == NULL || p2 == NULL)
19950 	{
19951 		return 0;
19952 	}
19953 	s1 = *(SETTING **)p1;
19954 	s2 = *(SETTING **)p2;
19955 	if (s1 == NULL || s2 == NULL)
19956 	{
19957 		return 0;
19958 	}
19959 
19960 	return UniStrCmpi(s1->Title, s2->Title);
19961 }
19962 
19963 // Initialize the configuration list
SmInitSettingList()19964 void SmInitSettingList()
19965 {
19966 	sm->SettingList = NewList(SmCompareSetting);
19967 
19968 	SmLoadSettingList();
19969 
19970 	SmInitDefaultSettingList();
19971 }
19972 
19973 // Release the configuration list
SmFreeSettingList()19974 void SmFreeSettingList()
19975 {
19976 	UINT i;
19977 
19978 	// Write
19979 	SmWriteSettingList();
19980 
19981 	for (i = 0;i < LIST_NUM(sm->SettingList);i++)
19982 	{
19983 		SETTING *s = LIST_DATA(sm->SettingList, i);
19984 		Free(s);
19985 	}
19986 	ReleaseList(sm->SettingList);
19987 
19988 	sm->SettingList = NULL;
19989 }
19990 
19991 // Write the configuration list
SmWriteSettingList()19992 void SmWriteSettingList()
19993 {
19994 	TOKEN_LIST *t;
19995 	UINT i;
19996 
19997 	t = MsRegEnumValue(REG_CURRENT_USER, SM_SETTING_REG_KEY);
19998 	if (t != NULL)
19999 	{
20000 		// Remove all existing values
20001 		for (i = 0;i < t->NumTokens;i++)
20002 		{
20003 			char *name = t->Token[i];
20004 			MsRegDeleteValue(REG_CURRENT_USER, SM_SETTING_REG_KEY, name);
20005 		}
20006 
20007 		FreeToken(t);
20008 	}
20009 
20010 	for (i = 0;i < LIST_NUM(sm->SettingList);i++)
20011 	{
20012 		char name[MAX_SIZE];
20013 		SETTING *s = LIST_DATA(sm->SettingList, i);
20014 
20015 		// Write
20016 		Format(name, sizeof(name), "Setting%u", i + 1);
20017 		MsRegWriteBin(REG_CURRENT_USER, SM_SETTING_REG_KEY, name, s, sizeof(SETTING));
20018 	}
20019 }
20020 
20021 // Load the connection list
SmLoadSettingList()20022 void SmLoadSettingList()
20023 {
20024 	TOKEN_LIST *t;
20025 	UINT i;
20026 	char *key_name = SM_SETTING_REG_KEY;
20027 
20028 	t = MsRegEnumValue(REG_CURRENT_USER, key_name);
20029 	if (t == NULL)
20030 	{
20031 		key_name = SM_SETTING_REG_KEY_OLD;
20032 		t = MsRegEnumValue(REG_CURRENT_USER, key_name);
20033 		if (t == NULL)
20034 		{
20035 			return;
20036 		}
20037 	}
20038 
20039 	for (i = 0;i < t->NumTokens;i++)
20040 	{
20041 		char *name = t->Token[i];
20042 		BUF *b = MsRegReadBin(REG_CURRENT_USER, key_name, name);
20043 		if (b != NULL)
20044 		{
20045 			if (b->Size == sizeof(SETTING))
20046 			{
20047 				SETTING *s = ZeroMalloc(sizeof(SETTING));
20048 				Copy(s, b->Buf, sizeof(SETTING));
20049 
20050 				Add(sm->SettingList, s);
20051 			}
20052 			FreeBuf(b);
20053 		}
20054 	}
20055 
20056 	FreeToken(t);
20057 
20058 	Sort(sm->SettingList);
20059 }
20060 
20061 // Initialize the default setting list
SmInitDefaultSettingList()20062 void SmInitDefaultSettingList()
20063 {
20064 	if (LIST_NUM(sm->SettingList) == 0)
20065 	{
20066 		bool b = false;
20067 		LIST *pl = MsGetProcessList();
20068 
20069 		if (pl != NULL)
20070 		{
20071 			UINT i;
20072 			for (i = 0;i < LIST_NUM(pl);i++)
20073 			{
20074 				MS_PROCESS *p = LIST_DATA(pl, i);
20075 
20076 				if (UniInStr(p->ExeFilenameW, L"vpnserver.exe") || UniInStr(p->ExeFilenameW, L"vpnserver_x64.exe") ||
20077 					UniInStr(p->ExeFilenameW, L"vpnserver_ia64.exe") ||
20078 					UniInStr(p->ExeFilenameW, L"vpnbridge.exe") || UniInStr(p->ExeFilenameW, L"vpnbridge_x64.exe") ||
20079 					UniInStr(p->ExeFilenameW, L"vpnbridge_ia64.exe"))
20080 				{
20081 					b = true;
20082 				}
20083 
20084 				if (UniInStr(p->ExeFilenameW, L"sevpnserver.exe") || UniInStr(p->ExeFilenameW, L"sevpnserver_x64.exe") ||
20085 					UniInStr(p->ExeFilenameW, L"sevpnserver_ia64.exe") ||
20086 					UniInStr(p->ExeFilenameW, L"sevpnbridge.exe") || UniInStr(p->ExeFilenameW, L"sevpnbridge_x64.exe") ||
20087 					UniInStr(p->ExeFilenameW, L"sevpnbridge_ia64.exe"))
20088 				{
20089 					b = true;
20090 				}
20091 
20092 				if (UniInStr(p->ExeFilenameW, L"utvpnserver.exe") || UniInStr(p->ExeFilenameW, L"utvpnserver_x64.exe") ||
20093 					UniInStr(p->ExeFilenameW, L"utvpnserver_ia64.exe") ||
20094 					UniInStr(p->ExeFilenameW, L"utvpnbridge.exe") || UniInStr(p->ExeFilenameW, L"utvpnbridge_x64.exe") ||
20095 					UniInStr(p->ExeFilenameW, L"utvpnbridge_ia64.exe"))
20096 				{
20097 					b = true;
20098 				}
20099 			}
20100 		}
20101 
20102 		MsFreeProcessList(pl);
20103 
20104 		if (b == false)
20105 		{
20106 			if (MsIsServiceRunning(GC_SVC_NAME_VPNSERVER) || MsIsServiceRunning(GC_SVC_NAME_VPNBRIDGE))
20107 			{
20108 				b = true;
20109 			}
20110 		}
20111 
20112 		if (b)
20113 		{
20114 			SETTING *s = ZeroMalloc(sizeof(SETTING));
20115 
20116 			UniStrCpy(s->Title, sizeof(s->Title), _UU("SM_LOCALHOST"));
20117 			s->ServerAdminMode = true;
20118 			Hash(s->HashedPassword, "", 0, true);
20119 			UniStrCpy(s->ClientOption.AccountName, sizeof(s->ClientOption.AccountName), s->Title);
20120 			StrCpy(s->ClientOption.Hostname, sizeof(s->ClientOption.Hostname), "localhost");
20121 			s->ClientOption.Port = GC_DEFAULT_PORT;
20122 
20123 			Add(sm->SettingList, s);
20124 		}
20125 	}
20126 }
20127 
20128 // Main dialog initialization
SmMainDlgInit(HWND hWnd)20129 void SmMainDlgInit(HWND hWnd)
20130 {
20131 	wchar_t *last_select;
20132 	UINT i = INFINITE;
20133 	// Validate arguments
20134 	if (hWnd == NULL)
20135 	{
20136 		return;
20137 	}
20138 
20139 	SetIcon(hWnd, 0, ICO_VPNSERVER);
20140 
20141 	LvInit(hWnd, L_SETTING);
20142 	LvSetStyle(hWnd, L_SETTING, LVS_EX_GRIDLINES);
20143 	LvInsertColumn(hWnd, L_SETTING, 0, _UU("SM_MAIN_COLUMN_1"), 145);
20144 	LvInsertColumn(hWnd, L_SETTING, 1, _UU("SM_MAIN_COLUMN_2"), 129);
20145 	LvInsertColumn(hWnd, L_SETTING, 2, _UU("SM_MAIN_COLUMN_3"), 125);
20146 
20147 	SmRefreshSetting(hWnd);
20148 
20149 	last_select = MsRegReadStrW(REG_CURRENT_USER, SM_REG_KEY, "Last Select");
20150 	if (UniIsEmptyStr(last_select) == false)
20151 	{
20152 		i = LvSearchStr(hWnd, L_SETTING, 0, last_select);
20153 	}
20154 	Free(last_select);
20155 
20156 	if (i == INFINITE)
20157 	{
20158 		LvSelect(hWnd, L_SETTING, 0);
20159 	}
20160 	else
20161 	{
20162 		LvSelect(hWnd, L_SETTING, i);
20163 	}
20164 
20165 	DlgFont(hWnd, IDOK, 10, true);
20166 
20167 	if (MsIsWine())
20168 	{
20169 		Disable(hWnd, B_SECURE_MANAGER);
20170 		Disable(hWnd, B_SELECT_SECURE);
20171 	}
20172 
20173 	Focus(hWnd, L_SETTING);
20174 
20175 	SmMainDlgUpdate(hWnd);
20176 }
20177 
20178 // Update the configuration list
SmRefreshSetting(HWND hWnd)20179 void SmRefreshSetting(HWND hWnd)
20180 {
20181 	SmRefreshSettingEx(hWnd, NULL);
20182 }
SmRefreshSettingEx(HWND hWnd,wchar_t * select_name)20183 void SmRefreshSettingEx(HWND hWnd, wchar_t *select_name)
20184 {
20185 	LVB *b;
20186 	UINT i;
20187 	// Validate arguments
20188 	if (hWnd == NULL)
20189 	{
20190 		return;
20191 	}
20192 
20193 	b = LvInsertStart();
20194 
20195 	for (i = 0;i < LIST_NUM(sm->SettingList);i++)
20196 	{
20197 		wchar_t tmp[MAX_SIZE];
20198 		wchar_t tmp2[MAX_SIZE];
20199 		SETTING *s = LIST_DATA(sm->SettingList, i);
20200 
20201 		if (s->ServerAdminMode)
20202 		{
20203 			UniStrCpy(tmp, sizeof(tmp), _UU("SM_MODE_SERVER"));
20204 		}
20205 		else
20206 		{
20207 			UniFormat(tmp, sizeof(tmp), _UU("SM_MODE_HUB"), s->HubName);
20208 		}
20209 
20210 		StrToUni(tmp2, sizeof(tmp2), s->ClientOption.Hostname);
20211 
20212 		LvInsertAdd(b,
20213 			(s->ServerAdminMode ? ICO_SERVER_ONLINE : ICO_HUB),
20214 			NULL,
20215 			3,
20216 			s->Title,
20217 			tmp2,
20218 			tmp);
20219 	}
20220 
20221 	LvInsertEnd(b, hWnd, L_SETTING);
20222 
20223 	if (UniIsEmptyStr(select_name) == false)
20224 	{
20225 		LvSelect(hWnd, L_SETTING, INFINITE);
20226 		LvSelect(hWnd, L_SETTING, LvSearchStr(hWnd, L_SETTING, 0, select_name));
20227 	}
20228 }
20229 
20230 // Main dialog update
SmMainDlgUpdate(HWND hWnd)20231 void SmMainDlgUpdate(HWND hWnd)
20232 {
20233 	bool ok = true;
20234 	// Validate arguments
20235 	if (hWnd == NULL)
20236 	{
20237 		return;
20238 	}
20239 
20240 	if (LvIsSelected(hWnd, L_SETTING) == false)
20241 	{
20242 		ok = false;
20243 	}
20244 	if (LvIsMultiMasked(hWnd, L_SETTING))
20245 	{
20246 		ok = false;
20247 	}
20248 
20249 	SetEnable(hWnd, IDOK, ok);
20250 	SetEnable(hWnd, B_EDIT_SETTING, ok);
20251 	SetEnable(hWnd, B_DELETE, ok);
20252 }
20253 
20254 // Main window procedure
SmMainDlgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam,void * param)20255 UINT SmMainDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, void *param)
20256 {
20257 	NMHDR *n;
20258 	NMLVDISPINFOW *info;
20259 	NMLVKEYDOWN *key;
20260 	wchar_t *tmp;
20261 	UINT i;
20262 	wchar_t new_name[MAX_SIZE];
20263 	// Validate arguments
20264 	if (hWnd == NULL)
20265 	{
20266 		return 0;
20267 	}
20268 
20269 	switch (msg)
20270 	{
20271 	case WM_INITDIALOG:
20272 		SmMainDlgInit(hWnd);
20273 		SetTimer(hWnd, 4, 100, NULL);
20274 
20275 		// Updater start
20276 		sm->Update = InitUpdateUi(_UU("PRODUCT_NAME_VPN_SMGR"), NAME_OF_VPN_SERVER_MANAGER, NULL, GetCurrentBuildDate(),
20277 			CEDAR_BUILD, CEDAR_VER, NULL, false);
20278 		break;
20279 
20280 	case WM_TIMER:
20281 		switch (wParam)
20282 		{
20283 		case 4:
20284 			KillTimer(hWnd, 4);
20285 			break;
20286 		}
20287 		break;
20288 
20289 	case WM_COMMAND:
20290 		switch (wParam)
20291 		{
20292 		case IDOK:
20293 			DisableUpdateUi(sm->Update);
20294 
20295 			// Connection
20296 			i = LvGetSelected(hWnd, L_SETTING);
20297 			if (i != INFINITE)
20298 			{
20299 				tmp = LvGetStr(hWnd, L_SETTING, i, 0);
20300 				if (tmp != NULL)
20301 				{
20302 					SETTING *setting = SmGetSetting(tmp);
20303 					if (setting != NULL)
20304 					{
20305 						SETTING s;
20306 
20307 						// Record in the registry as the last choice
20308 						MsRegWriteStrW(REG_CURRENT_USER, SM_REG_KEY, "Last Select", tmp);
20309 
20310 						// Copy the configuration
20311 						Copy(&s, setting, sizeof(SETTING));
20312 						SmConnect(hWnd, &s);
20313 					}
20314 					Free(tmp);
20315 				}
20316 			}
20317 			break;
20318 
20319 		case IDCANCEL:
20320 			Close(hWnd);
20321 			break;
20322 
20323 		case B_NEW_SETTING:
20324 			DisableUpdateUi(sm->Update);
20325 
20326 			// Add
20327 			if (SmAddSettingDlg(hWnd, new_name, sizeof(new_name)))
20328 			{
20329 				SmRefreshSettingEx(hWnd, new_name);
20330 			}
20331 			break;
20332 
20333 		case B_EDIT_SETTING:
20334 			DisableUpdateUi(sm->Update);
20335 
20336 			// Edit
20337 			if (SmEditSettingDlg(hWnd))
20338 			{
20339 				SmWriteSettingList();
20340 				SmRefreshSetting(hWnd);
20341 			}
20342 
20343 			break;
20344 
20345 		case B_DELETE:
20346 			DisableUpdateUi(sm->Update);
20347 
20348 			// Delete
20349 			i = LvGetSelected(hWnd, L_SETTING);
20350 			if (i != INFINITE)
20351 			{
20352 				tmp = LvGetStr(hWnd, L_SETTING, i, 0);
20353 				if (tmp != NULL)
20354 				{
20355 					if (MsgBoxEx(hWnd, MB_ICONQUESTION | MB_YESNO | MB_DEFBUTTON2,
20356 						_UU("SM_SETTING_DELETE_MSG"), tmp) == IDYES)
20357 					{
20358 						SmDeleteSetting(tmp);
20359 						SmWriteSettingList();
20360 						SmRefreshSetting(hWnd);
20361 					}
20362 					Free(tmp);
20363 				}
20364 			}
20365 			break;
20366 
20367 		case B_ABOUT:
20368 			// Version information
20369 			AboutEx(hWnd, sm->Cedar, _UU("PRODUCT_NAME_VPN_SMGR"), sm->Update);
20370 			break;
20371 
20372 		case B_SECURE_MANAGER:
20373 			DisableUpdateUi(sm->Update);
20374 
20375 			// Smart Card Manager
20376 			SmSecureManager(hWnd);
20377 			break;
20378 
20379 		case B_SELECT_SECURE:
20380 			DisableUpdateUi(sm->Update);
20381 
20382 			// Smart card selection
20383 			SmSelectSecureId(hWnd);
20384 			break;
20385 
20386 		case B_CERT_TOOL:
20387 			DisableUpdateUi(sm->Update);
20388 
20389 			// Certificate Creation Tool
20390 			SmCreateCert(hWnd, NULL, NULL, false, NULL, false);
20391 			break;
20392 		}
20393 
20394 		break;
20395 
20396 	case WM_CLOSE:
20397 		// Updater terminate
20398 		if (sm->Update != NULL)
20399 		{
20400 			FreeUpdateUi(sm->Update);
20401 			sm->Update = NULL;
20402 		}
20403 
20404 		EndDialog(hWnd, 0);
20405 		break;
20406 
20407 	case WM_NOTIFY:
20408 		n = (NMHDR *)lParam;
20409 		switch (n->idFrom)
20410 		{
20411 		case L_SETTING:
20412 			switch (n->code)
20413 			{
20414 			case NM_DBLCLK:
20415 				Command(hWnd, IDOK);
20416 				break;
20417 
20418 			case LVN_KEYDOWN:
20419 				key = (NMLVKEYDOWN *)n;
20420 				if (key != NULL)
20421 				{
20422 					UINT code = key->wVKey;
20423 					switch (code)
20424 					{
20425 					case VK_F2:
20426 						if (LvIsSelected(hWnd, L_SETTING))
20427 						{
20428 							LvRename(hWnd, L_SETTING, LvGetSelected(hWnd, L_SETTING));
20429 						}
20430 						break;
20431 
20432 					case VK_DELETE:
20433 						Command(hWnd, B_DELETE);
20434 						break;
20435 
20436 					case VK_RETURN:
20437 						Command(hWnd, IDOK);
20438 						break;
20439 					}
20440 				}
20441 				break;
20442 
20443 			case LVN_ENDLABELEDITW:
20444 				// Change the name
20445 				info = (NMLVDISPINFOW *)n;
20446 				if (info->item.pszText != NULL)
20447 				{
20448 					wchar_t *new_name = info->item.pszText;
20449 					wchar_t *old_name = LvGetStr(hWnd, L_SETTING, info->item.iItem, 0);
20450 
20451 					if (old_name != NULL)
20452 					{
20453 						if (UniStrCmp(new_name, old_name) != 0 && UniStrLen(new_name) != 0)
20454 						{
20455 							// Change the name
20456 							SETTING *s = SmGetSetting(old_name);
20457 							if (s != NULL)
20458 							{
20459 								if (SmGetSetting(new_name) != NULL)
20460 								{
20461 									MsgBoxEx(hWnd, MB_ICONEXCLAMATION, _UU("SM_SETTING_EXISTS"),
20462 										new_name);
20463 								}
20464 								else
20465 								{
20466 									UniStrCpy(s->Title, sizeof(s->Title), new_name);
20467 									Sort(sm->SettingList);
20468 									SmWriteSettingList();
20469 									LvSetItem(hWnd, L_SETTING, info->item.iItem, 0, new_name);
20470 								}
20471 							}
20472 						}
20473 
20474 						Free(old_name);
20475 					}
20476 				}
20477 				break;
20478 
20479 			case LVN_ITEMCHANGED:
20480 				SmMainDlgUpdate(hWnd);
20481 				break;
20482 			}
20483 			break;
20484 		}
20485 		break;
20486 	}
20487 
20488 	LvSortHander(hWnd, msg, wParam, lParam, L_SETTING);
20489 
20490 	return 0;
20491 }
20492 
20493 // Main window
SmMainDlg()20494 void SmMainDlg()
20495 {
20496 	Dialog(NULL, D_SM_MAIN, SmMainDlgProc, NULL);
20497 }
20498 
20499 // Server Manager main process
MainSM()20500 void MainSM()
20501 {
20502 //	MsgBoxEx(NULL, 0, L"MsIsWine: %u\n", MsIsWine());
20503 
20504 	if (sm->TempSetting == NULL)
20505 	{
20506 		// Open the main window
20507 		SmMainDlg();
20508 	}
20509 	else
20510 	{
20511 		SmConnect(sm->hParentWnd, sm->TempSetting);
20512 	}
20513 }
20514 
20515 // Initialize
InitSM()20516 void InitSM()
20517 {
20518 	InitSMEx(false);
20519 }
InitSMEx(bool from_cm)20520 void InitSMEx(bool from_cm)
20521 {
20522 	if (sm != NULL)
20523 	{
20524 		// Already initialized
20525 		return;
20526 	}
20527 
20528 	sm = ZeroMalloc(sizeof(SM));
20529 
20530 	if (from_cm == false)
20531 	{
20532 		InitWinUi(_UU("SM_TITLE"), _SS("DEFAULT_FONT"), _II("DEFAULT_FONT_SIZE"));
20533 	}
20534 
20535 	sm->Cedar = NewCedar(NULL, NULL);
20536 
20537 	if (from_cm == false)
20538 	{
20539 		SmInitSettingList();
20540 		InitCM(false);
20541 
20542 		// Interpret the command line
20543 		SmParseCommandLine();
20544 	}
20545 }
20546 
20547 // Interpret the command line
SmParseCommandLine()20548 void SmParseCommandLine()
20549 {
20550 	LIST *o;
20551 	CONSOLE *c = NewLocalConsole(NULL, NULL);
20552 	wchar_t *cmdline;
20553 	PARAM args[] =
20554 	{
20555 		{"[vpnserver]", NULL, NULL, NULL, NULL,},
20556 		{"HUB", NULL, NULL, NULL, NULL,},
20557 		{"PASSWORD", NULL, NULL, NULL, NULL,},
20558 		{"TITLE", NULL, NULL, NULL, NULL,},
20559 		{"HWND", NULL, NULL, NULL, NULL,},
20560 	};
20561 	if (c == NULL)
20562 	{
20563 		return;
20564 	}
20565 
20566 	cmdline = GetCommandLineUniStr();
20567 
20568 	if (UniIsEmptyStr(cmdline) == false)
20569 	{
20570 		o = ParseCommandList(c, "vpnsmgr", cmdline, args, sizeof(args) / sizeof(args[0]));
20571 		if (o != NULL)
20572 		{
20573 			char *host;
20574 			UINT port;
20575 
20576 			if (ParseHostPort(GetParamStr(o, "[vpnserver]"), &host, &port, 443))
20577 			{
20578 				char *hub = GetParamStr(o, "HUB");
20579 				char *password = GetParamStr(o, "PASSWORD");
20580 				char *title = GetParamStr(o, "TITLE");
20581 				char *hwndstr = GetParamStr(o, "HWND");
20582 
20583 				if (hub == NULL || StrCmpi(hub, "\"") == 0)
20584 				{
20585 					hub = CopyStr("");
20586 				}
20587 				if (password == NULL)
20588 				{
20589 					password = CopyStr("");
20590 				}
20591 				if (title == NULL)
20592 				{
20593 					title = CopyStr(host);
20594 				}
20595 
20596 				if (IsEmptyStr(host) == false)
20597 				{
20598 					SETTING *s = ZeroMalloc(sizeof(SETTING));
20599 					BUF *b;
20600 					CLIENT_OPTION *o;
20601 
20602 					StrToUni(s->Title, sizeof(s->Title), title);
20603 
20604 					if (IsEmptyStr(hub))
20605 					{
20606 						s->ServerAdminMode = true;
20607 					}
20608 					else
20609 					{
20610 						s->ServerAdminMode = false;
20611 						StrCpy(s->HubName, sizeof(s->HubName), hub);
20612 					}
20613 
20614 					b = StrToBin(password);
20615 					if (b == NULL || b->Size != SHA1_SIZE)
20616 					{
20617 						Hash(s->HashedPassword, password, StrLen(password), true);
20618 					}
20619 					else
20620 					{
20621 						Copy(s->HashedPassword, b->Buf, SHA1_SIZE);
20622 					}
20623 					FreeBuf(b);
20624 
20625 					o = &s->ClientOption;
20626 
20627 					UniStrCpy(o->AccountName, sizeof(o->AccountName), s->Title);
20628 					StrCpy(o->Hostname, sizeof(o->Hostname), host);
20629 					o->Port = port;
20630 					o->ProxyType = PROXY_DIRECT;
20631 					StrCpy(o->DeviceName, sizeof(o->DeviceName), "DUMMY");
20632 
20633 					sm->TempSetting = s;
20634 
20635 					if (IsEmptyStr(hwndstr) == false)
20636 					{
20637 						sm->hParentWnd = (HWND)ToInt64(hwndstr);
20638 					}
20639 				}
20640 
20641 				Free(hwndstr);
20642 				Free(title);
20643 				Free(hub);
20644 				Free(password);
20645 				Free(host);
20646 			}
20647 		}
20648 	}
20649 
20650 	Free(cmdline);
20651 
20652 	c->Free(c);
20653 }
20654 
20655 // Release
FreeSM()20656 void FreeSM()
20657 {
20658 	FreeSMEx(false);
20659 }
FreeSMEx(bool from_cm)20660 void FreeSMEx(bool from_cm)
20661 {
20662 	if (sm == NULL)
20663 	{
20664 		// Uninitialized
20665 		return;
20666 	}
20667 
20668 	if (from_cm == false)
20669 	{
20670 		FreeCM();
20671 
20672 		SmFreeSettingList();
20673 	}
20674 
20675 	ReleaseCedar(sm->Cedar);
20676 
20677 	if (from_cm == false)
20678 	{
20679 		FreeWinUi();
20680 	}
20681 
20682 	if (sm->TempSetting != NULL)
20683 	{
20684 		Free(sm->TempSetting);
20685 	}
20686 
20687 	Free(sm);
20688 	sm = NULL;
20689 }
20690 
20691 // Running the Server Manager
SMExec()20692 void SMExec()
20693 {
20694 	InitSM();
20695 	MainSM();
20696 	FreeSM();
20697 }
20698 
20699 #endif	// WIN32
20700 
20701 
20702