1 // SoftEther VPN Source Code - Developer Edition Master Branch
2 // Cedar Communication Module
3 
4 
5 // EtherLog.c
6 // EtherLogger program
7 
8 #include "EtherLog.h"
9 
10 #include "Admin.h"
11 #include "BridgeUnix.h"
12 #include "BridgeWin32.h"
13 #include "Listener.h"
14 #include "Remote.h"
15 #include "Sam.h"
16 #include "Server.h"
17 #include "WinUi.h"
18 
19 #include "Mayaqua/Cfg.h"
20 #include "Mayaqua/Encrypt.h"
21 #include "Mayaqua/Memory.h"
22 #include "Mayaqua/Object.h"
23 #include "Mayaqua/OS.h"
24 #include "Mayaqua/Pack.h"
25 #include "Mayaqua/Str.h"
26 
27 static LOCK *el_lock = NULL;
28 static EL *el = NULL;
29 
30 // RPC functional related macro
31 #define	DECLARE_RPC_EX(rpc_name, data_type, function, in_rpc, out_rpc, free_rpc)		\
32 	else if (StrCmpi(name, rpc_name) == 0)								\
33 	{																	\
34 		data_type t;													\
35 		Zero(&t, sizeof(t));											\
36 		in_rpc(&t, p);													\
37 		err = function(e, &t);											\
38 		if (err == ERR_NO_ERROR)										\
39 		{																\
40 			out_rpc(ret, &t);											\
41 		}																\
42 		free_rpc(&t);													\
43 		ok = true;														\
44 	}
45 #define	DECLARE_RPC(rpc_name, data_type, function, in_rpc, out_rpc)		\
46 	else if (StrCmpi(name, rpc_name) == 0)								\
47 	{																	\
48 		data_type t;													\
49 		Zero(&t, sizeof(t));											\
50 		in_rpc(&t, p);													\
51 		err = function(e, &t);											\
52 		if (err == ERR_NO_ERROR)										\
53 		{																\
54 			out_rpc(ret, &t);											\
55 		}																\
56 		ok = true;														\
57 	}
58 #define	DECLARE_SC_EX(rpc_name, data_type, function, in_rpc, out_rpc, free_rpc)	\
59 	UINT function(RPC *r, data_type *t)									\
60 	{																	\
61 		PACK *p, *ret;													\
62 		UINT err;														\
63 		if (r == NULL || t == NULL)										\
64 		{																\
65 			return ERR_INTERNAL_ERROR;									\
66 		}																\
67 		p = NewPack();													\
68 		out_rpc(p, t);													\
69 		free_rpc(t);													\
70 		Zero(t, sizeof(data_type));										\
71 		ret = AdminCall(r, rpc_name, p);								\
72 		err = GetErrorFromPack(ret);									\
73 		if (err == ERR_NO_ERROR)										\
74 		{																\
75 			in_rpc(t, ret);												\
76 		}																\
77 		FreePack(ret);													\
78 		return err;														\
79 	}
80 #define	DECLARE_SC(rpc_name, data_type, function, in_rpc, out_rpc)		\
81 	UINT function(RPC *r, data_type *t)									\
82 	{																	\
83 		PACK *p, *ret;													\
84 		UINT err;														\
85 		if (r == NULL || t == NULL)										\
86 		{																\
87 			return ERR_INTERNAL_ERROR;									\
88 		}																\
89 		p = NewPack();													\
90 		out_rpc(p, t);													\
91 		ret = AdminCall(r, rpc_name, p);								\
92 		err = GetErrorFromPack(ret);									\
93 		if (err == ERR_NO_ERROR)										\
94 		{																\
95 			in_rpc(t, ret);												\
96 		}																\
97 		FreePack(ret);													\
98 		return err;														\
99 	}
100 
101 // RPC client disconnect
EcDisconnect(RPC * rpc)102 void EcDisconnect(RPC *rpc)
103 {
104 	// Validate arguments
105 	if (rpc == NULL)
106 	{
107 		return;
108 	}
109 
110 	RpcFree(rpc);
111 }
112 
113 // RPC client connect
EcConnect(char * host,UINT port,char * password,RPC ** rpc)114 UINT EcConnect(char *host, UINT port, char *password, RPC **rpc)
115 {
116 	SOCK *s;
117 	UCHAR password_hash[SHA1_SIZE];
118 	UCHAR rand[SHA1_SIZE];
119 	UCHAR response[SHA1_SIZE];
120 	bool retcode;
121 	// Validate arguments
122 	if (host == NULL)
123 	{
124 		host = "localhost";
125 	}
126 	if (port == 0)
127 	{
128 		port = EL_ADMIN_PORT;
129 	}
130 	if (password == NULL)
131 	{
132 		password = "";
133 	}
134 	if (rpc == NULL)
135 	{
136 		return ERR_INTERNAL_ERROR;
137 	}
138 
139 	// Connect to the server
140 	s = Connect(host, port);
141 	if (s == NULL)
142 	{
143 		// Connection failure
144 		return ERR_CONNECT_FAILED;
145 	}
146 
147 	SetTimeout(s, 5000);
148 
149 	// Hash the password
150 	Sha0(password_hash, password, StrLen(password));
151 
152 	// Receive the random number
153 	Zero(rand, sizeof(rand));
154 	(void)RecvAll(s, rand, sizeof(rand), false);
155 	SecurePassword(response, password_hash, rand);
156 
157 	// Send a response
158 	SendAll(s, response, sizeof(response), false);
159 
160 	// Receive results
161 	retcode = false;
162 	if (RecvAll(s, &retcode, sizeof(retcode), false) == false)
163 	{
164 		// Disconnect
165 		ReleaseSock(s);
166 		return ERR_PROTOCOL_ERROR;
167 	}
168 	retcode = Endian32(retcode);
169 
170 	if (retcode == false)
171 	{
172 		// Password incorrect
173 		ReleaseSock(s);
174 		return ERR_AUTH_FAILED;
175 	}
176 
177 	// Successful connection
178 	SetTimeout(s, INFINITE);
179 
180 	*rpc = StartRpcClient(s, NULL);
181 
182 	ReleaseSock(s);
183 
184 	return ERR_NO_ERROR;
185 }
186 
187 // RPC server function
ElRpcServer(RPC * r,char * name,PACK * p)188 PACK *ElRpcServer(RPC *r, char *name, PACK *p)
189 {
190 	EL *e;
191 	PACK *ret;
192 	UINT err;
193 	bool ok;
194 	// Validate arguments
195 	if (r == NULL || name == NULL || p == NULL || r->Param == NULL)
196 	{
197 		return NULL;
198 	}
199 
200 	e = (EL *)r->Param;
201 	ret = NewPack();
202 	err = ERR_NO_ERROR;
203 	ok = false;
204 
205 	if (0) {}
206 
207 	DECLARE_RPC("AddDevice", RPC_ADD_DEVICE, EtAddDevice, InRpcAddDevice, OutRpcAddDevice)
208 	DECLARE_RPC("DelDevice", RPC_DELETE_DEVICE, EtDelDevice, InRpcDeleteDevice, OutRpcDeleteDevice)
209 	DECLARE_RPC("SetDevice", RPC_ADD_DEVICE, EtSetDevice, InRpcAddDevice, OutRpcAddDevice)
210 	DECLARE_RPC("GetDevice", RPC_ADD_DEVICE, EtGetDevice, InRpcAddDevice, OutRpcAddDevice)
211 	DECLARE_RPC_EX("EnumDevice", RPC_ENUM_DEVICE, EtEnumDevice, InRpcEnumDevice, OutRpcEnumDevice, FreeRpcEnumDevice)
212 	DECLARE_RPC("SetPassword", RPC_SET_PASSWORD, EtSetPassword, InRpcSetPassword, OutRpcSetPassword)
213 	DECLARE_RPC_EX("EnumAllDevice", RPC_ENUM_DEVICE, EtEnumAllDevice, InRpcEnumDevice, OutRpcEnumDevice, FreeRpcEnumDevice)
214 	DECLARE_RPC("AddLicenseKey", RPC_TEST, EtAddLicenseKey, InRpcTest, OutRpcTest)
215 	DECLARE_RPC("DelLicenseKey", RPC_TEST, EtDelLicenseKey, InRpcTest, OutRpcTest)
216 	DECLARE_RPC_EX("EnumLicenseKey", RPC_ENUM_LICENSE_KEY, EtEnumLicenseKey, InRpcEnumLicenseKey, OutRpcEnumLicenseKey, FreeRpcEnumLicenseKey)
217 	DECLARE_RPC("GetLicenseStatus", RPC_EL_LICENSE_STATUS, EtGetLicenseStatus, InRpcElLicenseStatus, OutRpcElLicenseStatus)
218 	DECLARE_RPC("GetBridgeSupport", RPC_BRIDGE_SUPPORT, EtGetBridgeSupport, InRpcBridgeSupport, OutRpcBridgeSupport)
219 	DECLARE_RPC("RebootServer", RPC_TEST, EtRebootServer, InRpcTest, OutRpcTest)
220 
221 	if (ok == false)
222 	{
223 		err = ERR_NOT_SUPPORTED;
224 	}
225 
226 	PackAddInt(ret, "error", err);
227 
228 	return ret;
229 }
230 
231 DECLARE_SC("AddDevice", RPC_ADD_DEVICE, EcAddDevice, InRpcAddDevice, OutRpcAddDevice)
232 DECLARE_SC("DelDevice", RPC_DELETE_DEVICE, EcDelDevice, InRpcDeleteDevice, OutRpcDeleteDevice)
233 DECLARE_SC("SetDevice", RPC_ADD_DEVICE, EcSetDevice, InRpcAddDevice, OutRpcAddDevice)
234 DECLARE_SC("GetDevice", RPC_ADD_DEVICE, EcGetDevice, InRpcAddDevice, OutRpcAddDevice)
235 DECLARE_SC_EX("EnumDevice", RPC_ENUM_DEVICE, EcEnumDevice, InRpcEnumDevice, OutRpcEnumDevice, FreeRpcEnumDevice)
236 DECLARE_SC("SetPassword", RPC_SET_PASSWORD, EcSetPassword, InRpcSetPassword, OutRpcSetPassword)
237 DECLARE_SC_EX("EnumAllDevice", RPC_ENUM_DEVICE, EcEnumAllDevice, InRpcEnumDevice, OutRpcEnumDevice, FreeRpcEnumDevice)
238 DECLARE_SC("DelLicenseKey", RPC_TEST, EcDelLicenseKey, InRpcTest, OutRpcTest)
239 DECLARE_SC_EX("EnumLicenseKey", RPC_ENUM_LICENSE_KEY, EcEnumLicenseKey, InRpcEnumLicenseKey, OutRpcEnumLicenseKey, FreeRpcEnumLicenseKey)
240 DECLARE_SC("GetLicenseStatus", RPC_EL_LICENSE_STATUS, EcGetLicenseStatus, InRpcElLicenseStatus, OutRpcElLicenseStatus)
241 DECLARE_SC("GetBridgeSupport", RPC_BRIDGE_SUPPORT, EcGetBridgeSupport, InRpcBridgeSupport, OutRpcBridgeSupport)
242 DECLARE_SC("RebootServer", RPC_TEST, EcRebootServer, InRpcTest, OutRpcTest)
243 
244 // Thread to restart the server
EiRebootServerThread(THREAD * thread,void * param)245 void EiRebootServerThread(THREAD *thread, void *param)
246 {
247 	// Validate arguments
248 	if (thread == NULL)
249 	{
250 		return;
251 	}
252 
253 	if (el == NULL)
254 	{
255 		return;
256 	}
257 
258 	// Stopping the server
259 	ElStop();
260 
261 	// Starting the server
262 	ElStart();
263 }
264 
265 // Restarting the server
EiRebootServer()266 void EiRebootServer()
267 {
268 	THREAD *t;
269 
270 	t = NewThread(EiRebootServerThread, NULL);
271 	ReleaseThread(t);
272 }
273 
274 // RPC to restart server
EtRebootServer(EL * a,RPC_TEST * t)275 UINT EtRebootServer(EL *a, RPC_TEST *t)
276 {
277 
278 	EiRebootServer();
279 
280 	return ERR_NO_ERROR;
281 }
282 
283 // Get support information for the local bridge
EtGetBridgeSupport(EL * a,RPC_BRIDGE_SUPPORT * t)284 UINT EtGetBridgeSupport(EL *a, RPC_BRIDGE_SUPPORT *t)
285 {
286 	Zero(t, sizeof(RPC_BRIDGE_SUPPORT));
287 
288 	t->IsBridgeSupportedOs = IsBridgeSupported();
289 	t->IsWinPcapNeeded = IsNeedWinPcap();
290 
291 	return ERR_NO_ERROR;
292 }
293 
294 // Save by analyzing the status of the current license
ElParseCurrentLicenseStatus(LICENSE_SYSTEM * s,EL_LICENSE_STATUS * st)295 void ElParseCurrentLicenseStatus(LICENSE_SYSTEM *s, EL_LICENSE_STATUS *st)
296 {
297 }
298 
299 // Get a license status
EtGetLicenseStatus(EL * e,RPC_EL_LICENSE_STATUS * t)300 UINT EtGetLicenseStatus(EL *e, RPC_EL_LICENSE_STATUS *t)
301 {
302 	UINT ret = ERR_NO_ERROR;
303 	LICENSE_SYSTEM *ls = e->LicenseSystem;
304 
305 	if (ls == NULL)
306 	{
307 		return ERR_NOT_SUPPORTED;
308 	}
309 
310 	Zero(t, sizeof(RPC_EL_LICENSE_STATUS));
311 
312 	// Get the current license status
313 	ElParseCurrentLicenseStatus(ls, e->LicenseStatus);
314 
315 	t->Valid = e->LicenseStatus->Valid;
316 	t->SystemId = e->LicenseStatus->SystemId;
317 	t->SystemExpires = e->LicenseStatus->Expires;
318 
319 	return ret;
320 }
321 
322 // Enumerate the license keys
EtEnumLicenseKey(EL * el,RPC_ENUM_LICENSE_KEY * t)323 UINT EtEnumLicenseKey(EL *el, RPC_ENUM_LICENSE_KEY *t)
324 {
325 	return ERR_NOT_SUPPORTED;
326 }
327 
328 // Add a license key
EtAddLicenseKey(EL * e,RPC_TEST * t)329 UINT EtAddLicenseKey(EL *e, RPC_TEST *t)
330 {
331 	return ERR_NOT_SUPPORTED;
332 }
333 
334 // Delete the license key
EtDelLicenseKey(EL * e,RPC_TEST * t)335 UINT EtDelLicenseKey(EL *e, RPC_TEST *t)
336 {
337 	return ERR_NOT_SUPPORTED;
338 }
339 
340 // Password setting
EtSetPassword(EL * e,RPC_SET_PASSWORD * t)341 UINT EtSetPassword(EL *e, RPC_SET_PASSWORD *t)
342 {
343 	Copy(e->HashedPassword, t->HashedPassword, SHA1_SIZE);
344 
345 	ElSaveConfig(e);
346 
347 	return ERR_NO_ERROR;
348 }
349 
350 // Add a device
EtAddDevice(EL * e,RPC_ADD_DEVICE * t)351 UINT EtAddDevice(EL *e, RPC_ADD_DEVICE *t)
352 {
353 	if (ElAddCaptureDevice(e, t->DeviceName, &t->LogSetting, t->NoPromiscuous) == false)
354 	{
355 		return ERR_CAPTURE_DEVICE_ADD_ERROR;
356 	}
357 
358 	ElSaveConfig(e);
359 
360 	return ERR_NO_ERROR;
361 }
362 
363 // Remove the device
EtDelDevice(EL * e,RPC_DELETE_DEVICE * t)364 UINT EtDelDevice(EL *e, RPC_DELETE_DEVICE *t)
365 {
366 	if (ElDeleteCaptureDevice(e, t->DeviceName) == false)
367 	{
368 		return ERR_CAPTURE_NOT_FOUND;
369 	}
370 
371 	ElSaveConfig(e);
372 
373 	return ERR_NO_ERROR;
374 }
375 
376 // Get the device
EtGetDevice(EL * e,RPC_ADD_DEVICE * t)377 UINT EtGetDevice(EL *e, RPC_ADD_DEVICE *t)
378 {
379 	UINT ret = ERR_CAPTURE_NOT_FOUND;
380 
381 	LockList(e->DeviceList);
382 	{
383 		EL_DEVICE *d, a;
384 		Zero(&a, sizeof(a));
385 		StrCpy(a.DeviceName, sizeof(a.DeviceName), t->DeviceName);
386 
387 		d = Search(e->DeviceList, &a);
388 
389 		if (d != NULL)
390 		{
391 			ret = ERR_NO_ERROR;
392 
393 			Copy(&t->LogSetting, &d->LogSetting, sizeof(HUB_LOG));
394 			t->NoPromiscuous = d->NoPromiscuous;
395 		}
396 	}
397 	UnlockList(e->DeviceList);
398 
399 	return ret;
400 }
401 
402 // Device Setting
EtSetDevice(EL * e,RPC_ADD_DEVICE * t)403 UINT EtSetDevice(EL *e, RPC_ADD_DEVICE *t)
404 {
405 	if (ElSetCaptureDeviceLogSetting(e, t->DeviceName, &t->LogSetting) == false)
406 	{
407 		return ERR_CAPTURE_NOT_FOUND;
408 	}
409 
410 	ElSaveConfig(e);
411 
412 	return ERR_NO_ERROR;
413 }
414 
415 // Enumerate all devices
EtEnumAllDevice(EL * e,RPC_ENUM_DEVICE * t)416 UINT EtEnumAllDevice(EL *e, RPC_ENUM_DEVICE *t)
417 {
418 	TOKEN_LIST *eth;
419 	UINT i;
420 	if (IsEthSupported() == false)
421 	{
422 		return ERR_NOT_SUPPORTED;
423 	}
424 
425 	FreeRpcEnumDevice(t);
426 	Zero(t, sizeof(RPC_ENUM_DEVICE));
427 
428 	eth = GetEthList();
429 
430 	t->NumItem = eth->NumTokens;
431 	t->Items = ZeroMalloc(sizeof(RPC_ENUM_DEVICE_ITEM) * t->NumItem);
432 
433 	for (i = 0;i < eth->NumTokens;i++)
434 	{
435 		char *name = eth->Token[i];
436 		RPC_ENUM_DEVICE_ITEM *item = &t->Items[i];
437 
438 		StrCpy(item->DeviceName, sizeof(item->DeviceName), name);
439 	}
440 
441 	FreeToken(eth);
442 
443 	return ERR_NO_ERROR;
444 }
445 
446 // Device enumeration
EtEnumDevice(EL * e,RPC_ENUM_DEVICE * t)447 UINT EtEnumDevice(EL *e, RPC_ENUM_DEVICE *t)
448 {
449 	bool is_beta_expired = ElIsBetaExpired();
450 
451 	if (is_beta_expired)
452 	{
453 		// The beta version has expired
454 		return ERR_BETA_EXPIRES;
455 	}
456 
457 	FreeRpcEnumDevice(t);
458 	Zero(t, sizeof(RPC_ENUM_DEVICE));
459 
460 	LockList(e->DeviceList);
461 	{
462 		UINT i;
463 
464 		t->NumItem = LIST_NUM(e->DeviceList);
465 		t->Items = ZeroMalloc(sizeof(RPC_ENUM_DEVICE_ITEM) * t->NumItem);
466 
467 		for (i = 0;i < t->NumItem;i++)
468 		{
469 			RPC_ENUM_DEVICE_ITEM *d = &t->Items[i];
470 			EL_DEVICE *eld = LIST_DATA(e->DeviceList, i);
471 
472 			StrCpy(d->DeviceName, sizeof(d->DeviceName), eld->DeviceName);
473 			d->Active = eld->Active && ((ELOG_IS_BETA || e->LicenseStatus->Valid) ? true : false);
474 		}
475 	}
476 	UnlockList(e->DeviceList);
477 
478 	return ERR_NO_ERROR;
479 }
480 
InRpcAddDevice(RPC_ADD_DEVICE * t,PACK * p)481 void InRpcAddDevice(RPC_ADD_DEVICE *t, PACK *p)
482 {
483 	UINT i;
484 	// Validate arguments
485 	if (t == NULL || p == NULL)
486 	{
487 		return;
488 	}
489 
490 	Zero(t, sizeof(RPC_ADD_DEVICE));
491 	PackGetStr(p, "DeviceName", t->DeviceName, sizeof(t->DeviceName));
492 	t->NoPromiscuous = PackGetInt(p, "NoPromiscuous");
493 	t->LogSetting.PacketLogSwitchType = PackGetInt(p, "PacketLogSwitchType");
494 
495 	for (i = 0;i < NUM_PACKET_LOG;i++)
496 	{
497 		t->LogSetting.PacketLogConfig[i] = PackGetIntEx(p, "PacketLogConfig", i);
498 	}
499 }
500 
OutRpcAddDevice(PACK * p,RPC_ADD_DEVICE * t)501 void OutRpcAddDevice(PACK *p, RPC_ADD_DEVICE *t)
502 {
503 	UINT i;
504 	// Validate arguments
505 	if (t == NULL || p == NULL)
506 	{
507 		return;
508 	}
509 
510 	PackAddStr(p, "DeviceName", t->DeviceName);
511 	PackAddInt(p, "NoPromiscuous", t->NoPromiscuous);
512 	PackAddInt(p, "PacketLogSwitchType", t->LogSetting.PacketLogSwitchType);
513 
514 	for (i = 0;i < NUM_PACKET_LOG;i++)
515 	{
516 		PackAddIntEx(p, "PacketLogConfig", t->LogSetting.PacketLogConfig[i], i, NUM_PACKET_LOG);
517 	}
518 }
519 
InRpcDeleteDevice(RPC_DELETE_DEVICE * t,PACK * p)520 void InRpcDeleteDevice(RPC_DELETE_DEVICE *t, PACK *p)
521 {
522 	// Validate arguments
523 	if (t == NULL || p == NULL)
524 	{
525 		return;
526 	}
527 
528 	Zero(t, sizeof(RPC_DELETE_DEVICE));
529 	PackGetStr(p, "DeviceName", t->DeviceName, sizeof(t->DeviceName));
530 }
531 
OutRpcDeleteDevice(PACK * p,RPC_DELETE_DEVICE * t)532 void OutRpcDeleteDevice(PACK *p, RPC_DELETE_DEVICE *t)
533 {
534 	// Validate arguments
535 	if (p == NULL || t == NULL)
536 	{
537 		return;
538 	}
539 
540 	PackAddStr(p, "DeviceName", t->DeviceName);
541 }
542 
InRpcEnumDevice(RPC_ENUM_DEVICE * t,PACK * p)543 void InRpcEnumDevice(RPC_ENUM_DEVICE *t, PACK *p)
544 {
545 	UINT i;
546 	// Validate arguments
547 	if (t == NULL || p == NULL)
548 	{
549 		return;
550 	}
551 
552 	Zero(t, sizeof(RPC_ENUM_DEVICE));
553 	t->NumItem = PackGetInt(p, "NumItem");
554 	t->Items = ZeroMalloc(sizeof(RPC_ENUM_DEVICE_ITEM) * t->NumItem);
555 
556 	for (i = 0;i < t->NumItem;i++)
557 	{
558 		RPC_ENUM_DEVICE_ITEM *d = &t->Items[i];
559 
560 		PackGetStrEx(p, "DeviceName", d->DeviceName, sizeof(d->DeviceName), i);
561 		d->Active = PackGetBoolEx(p, "Active", i);
562 	}
563 
564 	t->IsLicenseSupported = PackGetBool(p, "IsLicenseSupported");
565 }
566 
OutRpcEnumDevice(PACK * p,RPC_ENUM_DEVICE * t)567 void OutRpcEnumDevice(PACK *p, RPC_ENUM_DEVICE *t)
568 {
569 	UINT i;
570 	// Validate arguments
571 	if (t == NULL || p == NULL)
572 	{
573 		return;
574 	}
575 
576 	PackAddInt(p, "NumItem", t->NumItem);
577 
578 	PackSetCurrentJsonGroupName(p, "DeviceList");
579 	for (i = 0;i < t->NumItem;i++)
580 	{
581 		RPC_ENUM_DEVICE_ITEM *d = &t->Items[i];
582 
583 		PackAddStrEx(p, "DeviceName", d->DeviceName, i, t->NumItem);
584 		PackAddBoolEx(p, "Active", d->Active, i, t->NumItem);
585 	}
586 	PackSetCurrentJsonGroupName(p, NULL);
587 
588 	PackAddBool(p, "IsLicenseSupported", t->IsLicenseSupported);
589 }
590 
FreeRpcEnumDevice(RPC_ENUM_DEVICE * t)591 void FreeRpcEnumDevice(RPC_ENUM_DEVICE *t)
592 {
593 	// Validate arguments
594 	if (t == NULL)
595 	{
596 		return;
597 	}
598 
599 	Free(t->Items);
600 }
601 
602 // RPC_LICENSE_STATUS
InRpcElLicenseStatus(RPC_EL_LICENSE_STATUS * t,PACK * p)603 void InRpcElLicenseStatus(RPC_EL_LICENSE_STATUS *t, PACK *p)
604 {
605 	// Validate arguments
606 	if (t == NULL || p == NULL)
607 	{
608 		return;
609 	}
610 
611 	Zero(t, sizeof(RPC_EL_LICENSE_STATUS));
612 
613 	t->Valid = PackGetBool(p, "Valid");
614 	t->SystemId = PackGetInt64(p, "SystemId");
615 	t->SystemExpires = PackGetInt64(p, "SystemExpires");
616 }
OutRpcElLicenseStatus(PACK * p,RPC_EL_LICENSE_STATUS * t)617 void OutRpcElLicenseStatus(PACK *p, RPC_EL_LICENSE_STATUS *t)
618 {
619 	// Validate arguments
620 	if (t == NULL || p == NULL)
621 	{
622 		return;
623 	}
624 
625 	PackAddBool(p, "Valid", t->Valid);
626 	PackAddInt64(p, "SystemId", t->SystemId);
627 	PackAddTime64(p, "SystemExpires", t->SystemExpires);
628 }
629 
630 // Listener thread
ElListenerProc(THREAD * thread,void * param)631 void ElListenerProc(THREAD *thread, void *param)
632 {
633 	TCP_ACCEPTED_PARAM *data = (TCP_ACCEPTED_PARAM *)param;
634 	EL *e;
635 	SOCK *s;
636 	UCHAR rand[SHA1_SIZE];
637 	UCHAR pass1[SHA1_SIZE], pass2[SHA1_SIZE];
638 	// Validate arguments
639 	if (data == NULL || thread == NULL)
640 	{
641 		return;
642 	}
643 
644 	e = (EL *)data->r->ThreadParam;
645 	s = data->s;
646 	AddRef(s->ref);
647 	SetTimeout(s, 5000);
648 	LockList(e->AdminThreadList);
649 	{
650 		AddRef(thread->ref);
651 		AddRef(s->ref);
652 		Insert(e->AdminThreadList, thread);
653 		Insert(e->AdminSockList, s);
654 	}
655 	UnlockList(e->AdminThreadList);
656 	NoticeThreadInit(thread);
657 
658 	// Submit a challenge
659 	Rand(rand, sizeof(rand));
660 	SendAll(s, rand, sizeof(rand), false);
661 
662 	// Receive a response
663 	SecurePassword(pass1, e->HashedPassword, rand);
664 	Zero(pass2, sizeof(pass2));
665 	(void)RecvAll(s, pass2, sizeof(pass2), false);
666 
667 	if (Cmp(pass1, pass2, SHA1_SIZE) != 0)
668 	{
669 		// Password incorrect
670 		bool code = false;
671 		code = Endian32(code);
672 		SendAll(s, &code, sizeof(code), false);
673 	}
674 	else
675 	{
676 		// Password match
677 		bool code = true;
678 		RPC *r;
679 
680 		code = Endian32(code);
681 		SendAll(s, &code, sizeof(code), false);
682 
683 		SetTimeout(s, INFINITE);
684 
685 		// Start operation as a RPC server
686 		r = StartRpcServer(s, ElRpcServer, e);
687 		RpcServer(r);
688 		RpcFree(r);
689 	}
690 
691 	Disconnect(s);
692 	ReleaseSock(s);
693 
694 	LockList(e->AdminThreadList);
695 	{
696 		if (Delete(e->AdminThreadList, thread))
697 		{
698 			ReleaseThread(thread);
699 		}
700 		if (Delete(e->AdminSockList, s))
701 		{
702 			ReleaseSock(s);
703 		}
704 	}
705 	UnlockList(e->AdminThreadList);
706 }
707 
708 // Listener start
ElStartListener(EL * e)709 void ElStartListener(EL *e)
710 {
711 	// Validate arguments
712 	if (e == NULL)
713 	{
714 		return;
715 	}
716 
717 	e->AdminThreadList = NewList(NULL);
718 	e->AdminSockList = NewList(NULL);
719 
720 	e->Listener = NewListenerEx(e->Cedar, LISTENER_TCP, e->Port == 0 ? EL_ADMIN_PORT : e->Port,
721 		ElListenerProc, e);
722 }
723 
724 // Listener stop
ElStopListener(EL * e)725 void ElStopListener(EL *e)
726 {
727 	UINT i;
728 	THREAD **threads;
729 	SOCK **socks;
730 	UINT num_threads, num_socks;
731 	// Validate arguments
732 	if (e == NULL)
733 	{
734 		return;
735 	}
736 
737 	StopAllListener(e->Cedar);
738 
739 	LockList(e->AdminThreadList);
740 	{
741 		threads = ToArray(e->AdminThreadList);
742 		num_threads = LIST_NUM(e->AdminThreadList);
743 		DeleteAll(e->AdminThreadList);
744 
745 		socks = ToArray(e->AdminSockList);
746 		num_socks = LIST_NUM(e->AdminSockList);
747 		DeleteAll(e->AdminSockList);
748 	}
749 	UnlockList(e->AdminThreadList);
750 
751 	for (i = 0;i < num_socks;i++)
752 	{
753 		Disconnect(socks[i]);
754 		ReleaseSock(socks[i]);
755 	}
756 
757 	for (i = 0;i < num_threads;i++)
758 	{
759 		WaitThread(threads[i], INFINITE);
760 		ReleaseThread(threads[i]);
761 	}
762 
763 	Free(threads);
764 	Free(socks);
765 
766 	ReleaseList(e->AdminSockList);
767 	ReleaseList(e->AdminThreadList);
768 
769 	ReleaseListener(e->Listener);
770 }
771 
772 // Update the log configuration of the capture device
ElSetCaptureDeviceLogSetting(EL * e,char * name,HUB_LOG * log)773 bool ElSetCaptureDeviceLogSetting(EL *e, char *name, HUB_LOG *log)
774 {
775 	EL_DEVICE *d;
776 	bool ret = false;
777 	// Validate arguments
778 	if (e == NULL || log == NULL || name == NULL)
779 	{
780 		return false;
781 	}
782 
783 	LockList(e->DeviceList);
784 	{
785 		EL_DEVICE t;
786 
787 		Zero(&t, sizeof(t));
788 		StrCpy(t.DeviceName, sizeof(t.DeviceName), name);
789 
790 		d = Search(e->DeviceList, &t);
791 
792 		if (d != NULL)
793 		{
794 			Copy(&d->LogSetting, log, sizeof(HUB_LOG));
795 
796 			SetLogSwitchType(d->Logger, log->PacketLogSwitchType);
797 
798 			ret = true;
799 		}
800 	}
801 	UnlockList(e->DeviceList);
802 
803 	return ret;
804 }
805 
806 // Confirm whether the beta version has expired
ElIsBetaExpired()807 bool ElIsBetaExpired()
808 {
809 	SYSTEMTIME st;
810 	UINT64 expires64;
811 	UINT64 now64;
812 	if (ELOG_IS_BETA == false)
813 	{
814 		return false;
815 	}
816 
817 	Zero(&st, sizeof(st));
818 
819 	st.wYear = ELOG_BETA_EXPIRES_YEAR;
820 	st.wMonth = ELOG_BETA_EXPIRES_MONTH;
821 	st.wDay = ELOG_BETA_EXPIRES_DAY;
822 
823 	expires64 = SystemToUINT64(&st);
824 	now64 = LocalTime64();
825 
826 	if (now64 >= expires64)
827 	{
828 		return true;
829 	}
830 
831 	return false;
832 }
833 
834 // Capture thread
ElCaptureThread(THREAD * thread,void * param)835 void ElCaptureThread(THREAD *thread, void *param)
836 {
837 }
838 
839 // Delete the capture device
ElDeleteCaptureDevice(EL * e,char * name)840 bool ElDeleteCaptureDevice(EL *e, char *name)
841 {
842 	bool ret = false;
843 	EL_DEVICE *d, t;
844 	// Validate arguments
845 	if (e == NULL || name == NULL)
846 	{
847 		return false;
848 	}
849 
850 	LockList(e->DeviceList);
851 	{
852 		Zero(&t, sizeof(t));
853 		StrCpy(t.DeviceName, sizeof(t.DeviceName), name);
854 
855 		d = Search(e->DeviceList, &t);
856 
857 		if (d != NULL)
858 		{
859 			// Stop capture
860 			d->Halt = true;
861 			Cancel(d->Cancel1);
862 
863 			// Wait for thread stop
864 			WaitThread(d->Thread, INFINITE);
865 			ReleaseThread(d->Thread);
866 
867 			// Release the memory
868 			Delete(e->DeviceList, d);
869 			Free(d);
870 
871 			ret = true;
872 		}
873 	}
874 	UnlockList(e->DeviceList);
875 
876 	return ret;
877 }
878 
879 // Add a capture device
ElAddCaptureDevice(EL * e,char * name,HUB_LOG * log,bool no_promiscuous)880 bool ElAddCaptureDevice(EL *e, char *name, HUB_LOG *log, bool no_promiscuous)
881 {
882 	EL_DEVICE *d, t;
883 	// Validate arguments
884 	if (e == NULL || name == NULL || log == NULL)
885 	{
886 		return false;
887 	}
888 
889 	Zero(&t, sizeof(t));
890 	StrCpy(t.DeviceName, sizeof(t.DeviceName), name);
891 
892 	LockList(e->DeviceList);
893 	{
894 		d = Search(e->DeviceList, &t);
895 		if (d != NULL)
896 		{
897 			// Capture settings with the same name already exists
898 			UnlockList(e->DeviceList);
899 			return false;
900 		}
901 
902 		// Add a device
903 		d = ZeroMalloc(sizeof(EL_DEVICE));
904 		StrCpy(d->DeviceName, sizeof(d->DeviceName), name);
905 		Copy(&d->LogSetting, log, sizeof(HUB_LOG));
906 		d->NoPromiscuous = no_promiscuous;
907 		d->el = e;
908 		Insert(e->DeviceList, d);
909 
910 		// Start the thread
911 		d->Thread = NewThread(ElCaptureThread, d);
912 		WaitThreadInit(d->Thread);
913 	}
914 	UnlockList(e->DeviceList);
915 
916 	ElSaveConfig(e);
917 
918 	return true;
919 }
920 
921 // Write the license List
EiWriteLicenseManager(FOLDER * f,EL * s)922 void EiWriteLicenseManager(FOLDER *f, EL *s)
923 {
924 }
925 
926 // Read the license list
EiLoadLicenseManager(EL * s,FOLDER * f)927 void EiLoadLicenseManager(EL *s, FOLDER *f)
928 {
929 }
930 
931 // Configuration initialization
ElInitConfig(EL * e)932 void ElInitConfig(EL *e)
933 {
934 	// Validate arguments
935 	if (e == NULL)
936 	{
937 		return;
938 	}
939 
940 	// Device list initialization
941 	e->DeviceList = NewList(ElCompareDevice);
942 
943 	// Read configuration file
944 	ElLoadConfig(e);
945 
946 	// Write configuration file
947 	ElSaveConfig(e);
948 }
949 
950 // Write the configuration
ElSaveConfig(EL * e)951 void ElSaveConfig(EL *e)
952 {
953 	FOLDER *root;
954 	// Validate arguments
955 	if (e == NULL)
956 	{
957 		return;
958 	}
959 
960 	root = CfgCreateFolder(NULL, TAG_ROOT);
961 
962 	ElSaveConfigToFolder(e, root);
963 
964 	SaveCfgRw(e->CfgRw, root);
965 
966 	CfgDeleteFolder(root);
967 }
968 
969 // Write the configuration to the folder
ElSaveConfigToFolder(EL * e,FOLDER * root)970 void ElSaveConfigToFolder(EL *e, FOLDER *root)
971 {
972 	UINT i;
973 	FOLDER *devices;
974 	// Validate arguments
975 	if (e == NULL || root == NULL)
976 	{
977 		return;
978 	}
979 
980 	CfgAddInt64(root, "AutoDeleteCheckDiskFreeSpaceMin", e->AutoDeleteCheckDiskFreeSpaceMin);
981 
982 	CfgAddInt(root, "AdminPort", e->Port);
983 
984 	CfgAddByte(root, "AdminPassword", e->HashedPassword, sizeof(e->HashedPassword));
985 
986 	if (ELOG_IS_BETA == false)
987 	{
988 		EiWriteLicenseManager(CfgCreateFolder(root, "LicenseManager"), e);
989 	}
990 
991 	devices = CfgCreateFolder(root,"Devices");
992 
993 	LockList(e->DeviceList);
994 	{
995 		for (i = 0;i < LIST_NUM(e->DeviceList);i++)
996 		{
997 			FOLDER *f;
998 			EL_DEVICE *d = LIST_DATA(e->DeviceList, i);
999 
1000 			f = CfgCreateFolder(devices, d->DeviceName);
1001 			SiWriteHubLogCfgEx(f, &d->LogSetting, true);
1002 			CfgAddBool(f, "NoPromiscuousMode", d->NoPromiscuous);
1003 		}
1004 	}
1005 	UnlockList(e->DeviceList);
1006 }
1007 
1008 // Read the configuration from the folder
ElLoadConfigFromFolder(EL * e,FOLDER * root)1009 void ElLoadConfigFromFolder(EL *e, FOLDER *root)
1010 {
1011 	UINT i;
1012 	TOKEN_LIST *t;
1013 	FOLDER *devices;
1014 
1015 	// Validate arguments
1016 	if (e == NULL || root == NULL)
1017 	{
1018 		return;
1019 	}
1020 
1021 	i = CfgGetInt(root, "AdminPort");
1022 	if (i >= 1 && i <= 65535)
1023 	{
1024 		e->Port = i;
1025 	}
1026 
1027 	e->AutoDeleteCheckDiskFreeSpaceMin = CfgGetInt64(root, "AutoDeleteCheckDiskFreeSpaceMin");
1028 	if (CfgIsItem(root, "AutoDeleteCheckDiskFreeSpaceMin") == false && e->AutoDeleteCheckDiskFreeSpaceMin == 0)
1029 	{
1030 		e->AutoDeleteCheckDiskFreeSpaceMin = DISK_FREE_SPACE_DEFAULT;
1031 	}
1032 
1033 	if (e->AutoDeleteCheckDiskFreeSpaceMin != 0)
1034 	{
1035 		if (e->AutoDeleteCheckDiskFreeSpaceMin < DISK_FREE_SPACE_MIN)
1036 		{
1037 			e->AutoDeleteCheckDiskFreeSpaceMin = DISK_FREE_SPACE_MIN;
1038 		}
1039 	}
1040 
1041 	if (CfgGetByte(root, "AdminPassword", e->HashedPassword, sizeof(e->HashedPassword)) != sizeof(e->HashedPassword))
1042 	{
1043 		Sha0(e->HashedPassword, "", 0);
1044 	}
1045 
1046 	if (ELOG_IS_BETA == false)
1047 	{
1048 		EiLoadLicenseManager(e,	CfgGetFolder(root, "LicenseManager"));
1049 	}
1050 
1051 	devices = CfgGetFolder(root, "Devices");
1052 	if(devices != NULL)
1053 	{
1054 		LockList(e->DeviceList);
1055 		{
1056 			t = CfgEnumFolderToTokenList(devices);
1057 			for (i = 0;i < t->NumTokens;i++)
1058 			{
1059 				char *name = t->Token[i];
1060 				FOLDER *f = CfgGetFolder(devices, name);
1061 
1062 				if (f != NULL)
1063 				{
1064 					HUB_LOG g;
1065 
1066 					Zero(&g, sizeof(g));
1067 					SiLoadHubLogCfg(&g, f);
1068 					ElAddCaptureDevice(e, name, &g, CfgGetBool(f, "NoPromiscuousMode"));
1069 				}
1070 			}
1071 			FreeToken(t);
1072 		}
1073 		UnlockList(e->DeviceList);
1074 	}
1075 }
1076 
1077 // Reading configuration
ElLoadConfig(EL * e)1078 bool ElLoadConfig(EL *e)
1079 {
1080 	FOLDER *root;
1081 	bool ret = false;
1082 	// Validate arguments
1083 	if (e == NULL)
1084 	{
1085 		return false;
1086 	}
1087 
1088 	e->Port = EL_ADMIN_PORT;
1089 
1090 	e->CfgRw = NewCfgRw(&root, EL_CONFIG_FILENAME);
1091 
1092 	if (root != NULL)
1093 	{
1094 		ElLoadConfigFromFolder(e, root);
1095 
1096 		CfgDeleteFolder(root);
1097 	}
1098 	else
1099 	{
1100 		char *pass = "";
1101 		Sha0(e->HashedPassword, pass, StrLen(pass));
1102 		e->AutoDeleteCheckDiskFreeSpaceMin = DISK_FREE_SPACE_DEFAULT;
1103 	}
1104 
1105 	return ret;
1106 }
1107 
1108 // Configuration release
ElFreeConfig(EL * e)1109 void ElFreeConfig(EL *e)
1110 {
1111 	UINT i;
1112 	LIST *o;
1113 	// Validate arguments
1114 	if (e == NULL)
1115 	{
1116 		return;
1117 	}
1118 
1119 	// Write the configuration file
1120 	ElSaveConfig(e);
1121 	FreeCfgRw(e->CfgRw);
1122 
1123 	// Stop all capture
1124 	o = NewList(NULL);
1125 	LockList(e->DeviceList);
1126 	{
1127 		for (i = 0;i < LIST_NUM(e->DeviceList);i++)
1128 		{
1129 			EL_DEVICE *d = LIST_DATA(e->DeviceList, i);
1130 			Insert(o, CopyStr(d->DeviceName));
1131 		}
1132 		for (i = 0;i < LIST_NUM(o);i++)
1133 		{
1134 			char *name = LIST_DATA(o, i);
1135 			ElDeleteCaptureDevice(e, name);
1136 			Free(name);
1137 		}
1138 		ReleaseList(o);
1139 	}
1140 	UnlockList(e->DeviceList);
1141 
1142 	ReleaseList(e->DeviceList);
1143 }
1144 
1145 // Comparison function of the device
ElCompareDevice(void * p1,void * p2)1146 int ElCompareDevice(void *p1, void *p2)
1147 {
1148 	EL_DEVICE *d1, *d2;
1149 	// Validate arguments
1150 	if (p1 == NULL || p2 == NULL)
1151 	{
1152 		return 0;
1153 	}
1154 	d1 = *(EL_DEVICE **)p1;
1155 	d2 = *(EL_DEVICE **)p2;
1156 	if (d1 == NULL || d2 == NULL)
1157 	{
1158 		return 0;
1159 	}
1160 
1161 	return StrCmpi(d1->DeviceName, d2->DeviceName);
1162 }
1163 
1164 // Clean-up the EL
CleanupEl(EL * e)1165 void CleanupEl(EL *e)
1166 {
1167 	// Validate arguments
1168 	if (e == NULL)
1169 	{
1170 		return;
1171 	}
1172 
1173 	// Stop Eraser
1174 	FreeEraser(e->Eraser);
1175 
1176 	// Stop Listener
1177 	ElStopListener(e);
1178 
1179 	// Setting release
1180 	ElFreeConfig(e);
1181 
1182 	// Free the license system
1183 	if(e->LicenseSystem != NULL)
1184 	{
1185 	}
1186 
1187 	// Free the license status
1188 	if(e->LicenseStatus != NULL)
1189 	{
1190 		Free(e->LicenseStatus);
1191 	}
1192 
1193 	// Ethernet release
1194 	FreeEth();
1195 
1196 	ReleaseCedar(e->Cedar);
1197 
1198 	DeleteLock(e->lock);
1199 
1200 	Free(e);
1201 }
1202 
1203 // Release the EL
ReleaseEl(EL * e)1204 void ReleaseEl(EL *e)
1205 {
1206 	// Validate arguments
1207 	if (e == NULL)
1208 	{
1209 		return;
1210 	}
1211 
1212 	if (Release(e->ref) == 0)
1213 	{
1214 		CleanupEl(e);
1215 	}
1216 }
1217 
1218 // Create the EL
NewEl()1219 EL *NewEl()
1220 {
1221 	EL *e;
1222 
1223 #ifdef OS_WIN32
1224 	RegistWindowsFirewallAll();
1225 #endif
1226 
1227 	e = ZeroMalloc(sizeof(EL));
1228 	e->lock = NewLock();
1229 	e->ref = NewRef();
1230 
1231 	e->Cedar = NewCedar(NULL, NULL);
1232 
1233 
1234 	// Ethernet initialization
1235 	InitEth();
1236 
1237 	// Setting initialization
1238 	ElInitConfig(e);
1239 
1240 	// Listener start
1241 	ElStartListener(e);
1242 
1243 	// Initialize the license status
1244 	ElParseCurrentLicenseStatus(e->LicenseSystem, e->LicenseStatus);
1245 
1246 	// Eraser start
1247 	e->Eraser = NewEraser(NULL, e->AutoDeleteCheckDiskFreeSpaceMin);
1248 
1249 	return e;
1250 }
1251 
1252 // EL start
ElStart()1253 void ElStart()
1254 {
1255 	// Raise the priority
1256 	OSSetHighPriority();
1257 
1258 	Lock(el_lock);
1259 	{
1260 		el = NewEl();
1261 	}
1262 	Unlock(el_lock);
1263 }
1264 
1265 // EL stop
ElStop()1266 void ElStop()
1267 {
1268 	Lock(el_lock);
1269 	{
1270 		ReleaseEl(el);
1271 		el = NULL;
1272 	}
1273 	Unlock(el_lock);
1274 }
1275 
1276