1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
|
/*
Copyright (C) 2009 Red Hat, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <windows.h>
#include <winternl.h>
#include <wtsapi32.h>
#include <userenv.h>
#include <stdio.h>
#include <tlhelp32.h>
#include "vdcommon.h"
#include "vdi_port.h"
//#define DEBUG_VDSERVICE
#define VD_SERVICE_DISPLAY_NAME TEXT("RHEV Spice Agent")
#define VD_SERVICE_NAME TEXT("vdservice")
#define VD_SERVICE_DESCRIPTION TEXT("Enables Spice event injection and display configuration.")
#define VD_SERVICE_LOG_PATH TEXT("%svdservice.log")
#define VD_SERVICE_LOAD_ORDER_GROUP TEXT("Pointer Port")
#define VD_AGENT_PATH TEXT("%s\\vdagent.exe")
#define VD_AGENT_TIMEOUT 3000
#define VD_AGENT_MAX_RESTARTS 10
#define VD_AGENT_RESTART_INTERVAL 3000
#define VD_AGENT_RESTART_COUNT_RESET_INTERVAL 60000
#define WINLOGON_FILENAME TEXT("winlogon.exe")
#define CREATE_PROC_MAX_RETRIES 10
#define CREATE_PROC_INTERVAL_MS 500
enum {
VD_EVENT_PIPE_READ = 0,
VD_EVENT_PIPE_WRITE,
VD_EVENT_CONTROL,
VD_EVENT_READ,
VD_EVENT_WRITE,
VD_EVENT_LOGON,
VD_EVENT_AGENT, // Must be before last
VD_EVENTS_COUNT // Must be last
};
class VDService {
public:
static VDService* get();
~VDService();
bool run();
bool install();
bool uninstall();
private:
VDService();
bool execute();
void stop();
static DWORD WINAPI control_handler(DWORD control, DWORD event_type,
LPVOID event_data, LPVOID context);
static VOID WINAPI main(DWORD argc, TCHAR * argv[]);
void pipe_write_completion();
void write_agent_control(uint32_t type, uint32_t opaque);
void read_pipe();
void handle_pipe_data(DWORD bytes);
void handle_port_data();
bool handle_agent_control(VDPipeMessage* msg);
bool restart_agent(bool normal_restart);
bool launch_agent();
void send_logon();
bool kill_agent();
private:
static VDService* _singleton;
SERVICE_STATUS _status;
SERVICE_STATUS_HANDLE _status_handle;
PROCESS_INFORMATION _agent_proc_info;
HANDLE _control_event;
HANDLE _logon_event;
HANDLE _events[VD_EVENTS_COUNT];
TCHAR _agent_path[MAX_PATH];
VDIPort* _vdi_port;
VDPipeState _pipe_state;
uint32_t _connection_id;
mutex_t _agent_mutex;
DWORD _session_id;
DWORD _chunk_port;
DWORD _chunk_size;
DWORD _last_agent_restart_time;
int _agent_restarts;
int _system_version;
bool _pipe_connected;
bool _pending_reset;
bool _pending_write;
bool _pending_read;
bool _agent_alive;
bool _running;
VDLog* _log;
};
VDService* VDService::_singleton = NULL;
VDService* VDService::get()
{
if (!_singleton) {
_singleton = new VDService();
}
return (VDService*)_singleton;
}
enum SystemVersion {
SYS_VER_UNSUPPORTED,
SYS_VER_WIN_XP,
SYS_VER_WIN_7,
};
int supported_system_version()
{
OSVERSIONINFOEX osvi;
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
if (!GetVersionEx((OSVERSIONINFO*)&osvi)) {
vd_printf("GetVersionEx() failed: %u", GetLastError());
return 0;
}
if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1) {
return SYS_VER_WIN_XP;
} else if (osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 1 &&
osvi.wProductType == VER_NT_WORKSTATION) {
return SYS_VER_WIN_7;
}
return 0;
}
VDService::VDService()
: _status_handle (0)
, _vdi_port (NULL)
, _connection_id (0)
, _session_id (0)
, _chunk_port (0)
, _chunk_size (0)
, _last_agent_restart_time (0)
, _agent_restarts (0)
, _pipe_connected (false)
, _pending_reset (false)
, _pending_write (false)
, _pending_read (false)
, _agent_alive (false)
, _running (false)
, _log (NULL)
{
ZeroMemory(&_agent_proc_info, sizeof(_agent_proc_info));
ZeroMemory(&_pipe_state, sizeof(_pipe_state));
ZeroMemory(_events, sizeof(_events));
_system_version = supported_system_version();
_control_event = CreateEvent(NULL, FALSE, FALSE, NULL);
_pipe_state.write.overlap.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
_pipe_state.read.overlap.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
_logon_event = CreateEvent(NULL, FALSE, FALSE, NULL);
_agent_path[0] = wchar_t('\0');
MUTEX_INIT(_agent_mutex);
_singleton = this;
}
VDService::~VDService()
{
CloseHandle(_logon_event);
CloseHandle(_pipe_state.read.overlap.hEvent);
CloseHandle(_pipe_state.write.overlap.hEvent);
CloseHandle(_control_event);
delete _log;
}
bool VDService::run()
{
#ifndef DEBUG_VDSERVICE
SERVICE_TABLE_ENTRY service_table[] = {{VD_SERVICE_NAME, main}, {0, 0}};
return !!StartServiceCtrlDispatcher(service_table);
#else
main(0, NULL);
return true;
#endif
}
bool VDService::install()
{
bool ret = false;
SC_HANDLE service_control_manager = OpenSCManager(0, 0, SC_MANAGER_CREATE_SERVICE);
if (!service_control_manager) {
printf("OpenSCManager failed\n");
return false;
}
TCHAR path[_MAX_PATH + 1];
if (!GetModuleFileName(0, path, sizeof(path) / sizeof(path[0]))) {
printf("GetModuleFileName failed\n");
CloseServiceHandle(service_control_manager);
return false;
}
SC_HANDLE service = CreateService(service_control_manager, VD_SERVICE_NAME,
VD_SERVICE_DISPLAY_NAME, SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START,
SERVICE_ERROR_IGNORE, path, VD_SERVICE_LOAD_ORDER_GROUP,
0, 0, 0, 0);
if (service) {
SERVICE_DESCRIPTION descr;
descr.lpDescription = VD_SERVICE_DESCRIPTION;
if (!ChangeServiceConfig2(service, SERVICE_CONFIG_DESCRIPTION, &descr)) {
printf("ChangeServiceConfig2 failed\n");
}
CloseServiceHandle(service);
printf("Service installed successfully\n");
ret = true;
} else if (GetLastError() == ERROR_SERVICE_EXISTS) {
printf("Service already exists\n");
ret = true;
} else {
printf("Service not installed successfully, error %d\n", GetLastError());
}
CloseServiceHandle(service_control_manager);
return ret;
}
bool VDService::uninstall()
{
bool ret = false;
SC_HANDLE service_control_manager = OpenSCManager(0, 0, SC_MANAGER_CONNECT);
if (!service_control_manager) {
printf("OpenSCManager failed\n");
return false;
}
SC_HANDLE service = OpenService(service_control_manager, VD_SERVICE_NAME,
SERVICE_QUERY_STATUS | DELETE);
if (!service) {
printf("OpenService failed\n");
CloseServiceHandle(service_control_manager);
return false;
}
SERVICE_STATUS status;
if (!QueryServiceStatus(service, &status)) {
printf("QueryServiceStatus failed\n");
} else if (status.dwCurrentState != SERVICE_STOPPED) {
printf("Service is still running\n");
} else if (DeleteService(service)) {
printf("Service removed successfully\n");
ret = true;
} else {
switch (GetLastError()) {
case ERROR_ACCESS_DENIED:
printf("Access denied while trying to remove service\n");
break;
case ERROR_INVALID_HANDLE:
printf("Handle invalid while trying to remove service\n");
break;
case ERROR_SERVICE_MARKED_FOR_DELETE:
printf("Service already marked for deletion\n");
break;
}
}
CloseServiceHandle(service);
CloseServiceHandle(service_control_manager);
return ret;
}
const char* session_events[] = {
"INVALID", "CONNECT", "DISCONNECT", "REMOTE_CONNECT", "REMOTE_DISCONNECT", "LOGON", "LOGOFF",
"LOCK", "UNLOCK", "REMOTE_CONTROL"
};
DWORD WINAPI VDService::control_handler(DWORD control, DWORD event_type, LPVOID event_data,
LPVOID context)
{
VDService* s = _singleton;
DWORD ret = NO_ERROR;
ASSERT(s);
switch (control) {
case SERVICE_CONTROL_STOP:
case SERVICE_CONTROL_SHUTDOWN:
vd_printf("Stop service");
s->_status.dwCurrentState = SERVICE_STOP_PENDING;
SetServiceStatus(s->_status_handle, &s->_status);
s->stop();
break;
case SERVICE_CONTROL_INTERROGATE:
vd_printf("Interrogate service");
SetServiceStatus(s->_status_handle, &s->_status);
break;
case SERVICE_CONTROL_SESSIONCHANGE: {
DWORD session_id = ((WTSSESSION_NOTIFICATION*)event_data)->dwSessionId;
vd_printf("Session %u %s", session_id, session_events[event_type]);
SetServiceStatus(s->_status_handle, &s->_status);
if (s->_system_version != SYS_VER_UNSUPPORTED) {
if (event_type == WTS_CONSOLE_CONNECT) {
s->_session_id = session_id;
if (!s->restart_agent(true)) {
s->stop();
}
} else if (event_type == WTS_SESSION_LOGON) {
s->send_logon();
}
}
break;
}
default:
vd_printf("Unsupported control %u", control);
ret = ERROR_CALL_NOT_IMPLEMENTED;
}
return ret;
}
#define VDSERVICE_ACCEPTED_CONTROLS \
(SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_SESSIONCHANGE)
VOID WINAPI VDService::main(DWORD argc, TCHAR* argv[])
{
VDService* s = _singleton;
SERVICE_STATUS* status;
TCHAR log_path[MAX_PATH];
TCHAR full_path[MAX_PATH];
TCHAR temp_path[MAX_PATH];
TCHAR* slash;
ASSERT(s);
if (GetModuleFileName(NULL, full_path, MAX_PATH) && (slash = wcsrchr(full_path, TCHAR('\\'))) &&
GetTempPath(MAX_PATH, temp_path)) {
*slash = TCHAR('\0');
swprintf_s(s->_agent_path, MAX_PATH, VD_AGENT_PATH, full_path);
swprintf_s(log_path, MAX_PATH, VD_SERVICE_LOG_PATH, temp_path);
s->_log = VDLog::get(log_path);
}
vd_printf("***Service started***");
log_version();
if (!SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS)) {
vd_printf("SetPriorityClass failed %u", GetLastError());
}
status = &s->_status;
status->dwServiceType = SERVICE_WIN32;
status->dwCurrentState = SERVICE_STOPPED;
status->dwControlsAccepted = 0;
status->dwWin32ExitCode = NO_ERROR;
status->dwServiceSpecificExitCode = NO_ERROR;
status->dwCheckPoint = 0;
status->dwWaitHint = 0;
s->_status_handle = RegisterServiceCtrlHandlerEx(VD_SERVICE_NAME, &VDService::control_handler,
NULL);
if (!s->_status_handle) {
printf("RegisterServiceCtrlHandler failed\n");
#ifndef DEBUG_VDSERVICE
return;
#endif // DEBUG_VDSERVICE
}
// service is starting
status->dwCurrentState = SERVICE_START_PENDING;
SetServiceStatus(s->_status_handle, status);
// service running
status->dwControlsAccepted |= VDSERVICE_ACCEPTED_CONTROLS;
status->dwCurrentState = SERVICE_RUNNING;
SetServiceStatus(s->_status_handle, status);
s->_running = true;
s->execute();
// service was stopped
status->dwCurrentState = SERVICE_STOP_PENDING;
SetServiceStatus(s->_status_handle, status);
// service is stopped
status->dwControlsAccepted &= ~VDSERVICE_ACCEPTED_CONTROLS;
status->dwCurrentState = SERVICE_STOPPED;
#ifndef DEBUG_VDSERVICE
SetServiceStatus(s->_status_handle, status);
#endif //DEBUG_VDSERVICE
}
bool VDService::execute()
{
SECURITY_ATTRIBUTES sec_attr;
SECURITY_DESCRIPTOR* sec_desr;
HANDLE pipe;
sec_desr = (SECURITY_DESCRIPTOR*)LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
InitializeSecurityDescriptor(sec_desr, SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(sec_desr, TRUE, (PACL)NULL, FALSE);
sec_attr.nLength = sizeof(sec_attr);
sec_attr.bInheritHandle = TRUE;
sec_attr.lpSecurityDescriptor = sec_desr;
pipe = CreateNamedPipe(VD_SERVICE_PIPE_NAME, PIPE_ACCESS_DUPLEX |
FILE_FLAG_FIRST_PIPE_INSTANCE | FILE_FLAG_OVERLAPPED,
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
PIPE_UNLIMITED_INSTANCES, BUF_SIZE, BUF_SIZE,
VD_AGENT_TIMEOUT, &sec_attr);
LocalFree(sec_desr);
if (pipe == INVALID_HANDLE_VALUE) {
vd_printf("CreatePipe() failed: %u", GetLastError());
return false;
}
_pipe_state.pipe = pipe;
_session_id = WTSGetActiveConsoleSessionId();
if (_session_id == 0xFFFFFFFF) {
vd_printf("WTSGetActiveConsoleSessionId() failed");
CloseHandle(pipe);
return false;
}
if (!launch_agent()) {
CloseHandle(pipe);
return false;
}
_vdi_port = new VDIPort();
if (!_vdi_port->init()) {
delete _vdi_port;
CloseHandle(pipe);
return false;
}
vd_printf("Connected to server");
_events[VD_EVENT_PIPE_READ] = _pipe_state.read.overlap.hEvent;
_events[VD_EVENT_PIPE_WRITE] = _pipe_state.write.overlap.hEvent;
_events[VD_EVENT_CONTROL] = _control_event;
_events[VD_EVENT_READ] = _vdi_port->get_read_event();
_events[VD_EVENT_WRITE] = _vdi_port->get_write_event();
_events[VD_EVENT_LOGON] = _logon_event;
_events[VD_EVENT_AGENT] = _agent_proc_info.hProcess;
_chunk_size = _chunk_port = 0;
read_pipe();
while (_running) {
int cont_read = _vdi_port->read();
int cont_write = _vdi_port->write();
bool cont = false;
if (cont_read >= 0 && cont_write >= 0) {
cont = cont_read || cont_write;
} else if (cont_read == VDI_PORT_ERROR || cont_write == VDI_PORT_ERROR) {
vd_printf("VDI Port error, read %d write %d", cont_read, cont_write);
_running = false;
} else if (cont_read == VDI_PORT_RESET || cont_write == VDI_PORT_RESET) {
vd_printf("VDI Port reset, read %d write %d", cont_read, cont_write);
_chunk_size = _chunk_port = 0;
write_agent_control(VD_AGENT_RESET, ++_connection_id);
_pending_reset = true;
}
if (cont) {
handle_port_data();
}
if (cont_write) {
handle_pipe_data(0);
}
if (_running && (!cont || _pending_read || _pending_write)) {
DWORD events_count = _events[VD_EVENT_AGENT] ? VD_EVENTS_COUNT : VD_EVENTS_COUNT - 1;
DWORD wait_ret = WaitForMultipleObjects(events_count, _events, FALSE,
cont ? 0 : INFINITE);
switch (wait_ret) {
case WAIT_OBJECT_0 + VD_EVENT_PIPE_READ: {
DWORD bytes = 0;
if (_pipe_connected && _pending_read) {
_pending_read = false;
if (GetOverlappedResult(_pipe_state.pipe, &_pipe_state.read.overlap,
&bytes, FALSE) || GetLastError() == ERROR_MORE_DATA) {
handle_pipe_data(bytes);
read_pipe();
} else {
vd_printf("GetOverlappedResult failed %u", GetLastError());
_pipe_connected = false;
DisconnectNamedPipe(_pipe_state.pipe);
}
}
break;
}
case WAIT_OBJECT_0 + VD_EVENT_PIPE_WRITE:
pipe_write_completion();
break;
case WAIT_OBJECT_0 + VD_EVENT_CONTROL:
vd_printf("Control event");
break;
case WAIT_OBJECT_0 + VD_EVENT_READ:
_vdi_port->read_completion();
break;
case WAIT_OBJECT_0 + VD_EVENT_WRITE:
_vdi_port->write_completion();
break;
case WAIT_OBJECT_0 + VD_EVENT_LOGON:
vd_printf("logon event");
write_agent_control(VD_AGENT_SESSION_LOGON, 0);
break;
case WAIT_OBJECT_0 + VD_EVENT_AGENT:
vd_printf("Agent killed");
if (_system_version == SYS_VER_WIN_XP) {
restart_agent(false);
} else if (_system_version == SYS_VER_WIN_7) {
kill_agent();
}
break;
case WAIT_TIMEOUT:
break;
default:
vd_printf("WaitForMultipleObjects failed %u", GetLastError());
}
}
}
delete _vdi_port;
CloseHandle(pipe);
return true;
}
DWORD64 marshall_string(LPCWSTR str, DWORD max_size, LPBYTE* next_buf, DWORD* used_bytes)
{
DWORD offset = *used_bytes;
if (!str) {
return 0;
}
DWORD len = (DWORD)(wcslen(str) + 1) * sizeof(WCHAR);
if (*used_bytes + len > max_size) {
return 0;
}
memmove(*next_buf, str, len);
*used_bytes += len;
*next_buf += len;
return offset;
}
typedef struct CreateProcessParams {
DWORD size;
DWORD process_id;
BOOL use_default_token;
HANDLE token;
LPWSTR application_name;
LPWSTR command_line;
SECURITY_ATTRIBUTES process_attributes;
SECURITY_ATTRIBUTES thread_attributes;
BOOL inherit_handles;
DWORD creation_flags;
LPVOID environment;
LPWSTR current_directory;
STARTUPINFOW startup_info;
PROCESS_INFORMATION process_information;
BYTE data[0x2000];
} CreateProcessParams;
typedef struct CreateProcessRet {
DWORD size;
BOOL ret_value;
DWORD last_error;
PROCESS_INFORMATION process_information;
} CreateProcessRet;
BOOL create_session_process_as_user(IN DWORD session_id, IN BOOL use_default_token, IN HANDLE token,
IN LPCWSTR application_name, IN LPWSTR command_line,
IN LPSECURITY_ATTRIBUTES process_attributes,
IN LPSECURITY_ATTRIBUTES thread_attributes,
IN BOOL inherit_handles, IN DWORD creation_flags,
IN LPVOID environment, IN LPCWSTR current_directory,
IN LPSTARTUPINFOW startup_info,
OUT LPPROCESS_INFORMATION process_information)
{
WCHAR win_sta_path[MAX_PATH];
HINSTANCE win_sta_handle;
WCHAR pipe_name[MAX_PATH] = L"";
DWORD pipe_name_len;
BOOL got_pipe_name = FALSE;
HANDLE named_pipe;
CreateProcessRet proc_ret;
CreateProcessParams proc_params;
LPBYTE buffer = (LPBYTE)proc_params.data;
DWORD max_size = sizeof(proc_params);
DWORD bytes_used = offsetof(CreateProcessParams, data);
DWORD bytes_written;
DWORD bytes_read;
DWORD env_len = 0;
BOOL ret = FALSE;
GetSystemDirectoryW(win_sta_path, MAX_PATH);
lstrcatW(win_sta_path, L"\\winsta.dll");
win_sta_handle = LoadLibrary(win_sta_path);
if (win_sta_handle) {
PWINSTATIONQUERYINFORMATIONW win_sta_query_func =
(PWINSTATIONQUERYINFORMATIONW)GetProcAddress(win_sta_handle,
"WinStationQueryInformationW");
if (win_sta_query_func) {
got_pipe_name = win_sta_query_func(0, session_id, (WINSTATIONINFOCLASS)0x21,
pipe_name, sizeof(pipe_name), &pipe_name_len);
}
FreeLibrary(win_sta_handle);
}
if (!got_pipe_name || pipe_name[0] == '\0') {
swprintf_s(pipe_name, MAX_PATH, L"\\\\.\\Pipe\\TerminalServer\\SystemExecSrvr\\%d",
session_id);
}
do {
named_pipe = CreateFile(pipe_name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
0, 0);
if (named_pipe == INVALID_HANDLE_VALUE) {
if (GetLastError() == ERROR_PIPE_BUSY) {
if (!WaitNamedPipe(pipe_name, 3000)) {
return FALSE;
}
} else {
return FALSE;
}
}
} while (named_pipe == INVALID_HANDLE_VALUE);
memset(&proc_params, 0, sizeof(proc_params));
proc_params.process_id = GetCurrentProcessId();
proc_params.use_default_token = use_default_token;
proc_params.token = token;
proc_params.application_name = (LPWSTR)marshall_string(application_name, max_size, &buffer,
&bytes_used);
proc_params.command_line = (LPWSTR)marshall_string(command_line, max_size, &buffer,
&bytes_used);
if (process_attributes) {
proc_params.process_attributes = *process_attributes;
}
if (thread_attributes) {
proc_params.thread_attributes = *thread_attributes;
}
proc_params.inherit_handles = inherit_handles;
proc_params.creation_flags = creation_flags;
proc_params.current_directory = (LPWSTR)marshall_string(current_directory, max_size,
&buffer, &bytes_used);
if (startup_info) {
proc_params.startup_info = *startup_info;
proc_params.startup_info.lpDesktop = (LPWSTR)marshall_string(startup_info->lpDesktop,
max_size, &buffer,
&bytes_used);
proc_params.startup_info.lpTitle = (LPWSTR)marshall_string(startup_info->lpTitle,
max_size, &buffer, &bytes_used);
}
if (environment) {
if (creation_flags & CREATE_UNICODE_ENVIRONMENT) {
while ((env_len + bytes_used <= max_size)) {
if (((LPWSTR)environment)[env_len / 2] == '\0' &&
((LPWSTR)environment)[env_len / 2 + 1] == '\0') {
env_len += 2 * sizeof(WCHAR);
break;
}
env_len += sizeof(WCHAR);
}
} else {
while (env_len + bytes_used <= max_size) {
if (((LPSTR)environment)[env_len] == '\0' &&
((LPSTR)environment)[env_len + 1] == '\0') {
env_len += 2;
break;
}
env_len++;
}
}
if (env_len + bytes_used <= max_size) {
memmove(buffer, environment, env_len);
proc_params.environment = (LPVOID)(UINT64)bytes_used;
buffer += env_len;
bytes_used += env_len;
} else {
proc_params.environment = NULL;
}
} else {
proc_params.environment = NULL;
}
proc_params.size = bytes_used;
if (WriteFile(named_pipe, &proc_params, proc_params.size, &bytes_written, NULL) &&
ReadFile(named_pipe, &proc_ret, sizeof(proc_ret), &bytes_read, NULL)) {
ret = proc_ret.ret_value;
if (ret) {
*process_information = proc_ret.process_information;
} else {
SetLastError(proc_ret.last_error);
}
} else {
ret = FALSE;
}
CloseHandle(named_pipe);
return ret;
}
BOOL create_process_as_user(IN DWORD session_id, IN LPCWSTR application_name,
IN LPWSTR command_line, IN LPSECURITY_ATTRIBUTES process_attributes,
IN LPSECURITY_ATTRIBUTES thread_attributes, IN BOOL inherit_handles,
IN DWORD creation_flags, IN LPVOID environment,
IN LPCWSTR current_directory, IN LPSTARTUPINFOW startup_info,
OUT LPPROCESS_INFORMATION process_information)
{
PROCESSENTRY32 proc_entry;
DWORD winlogon_pid = 0;
HANDLE winlogon_proc;
HANDLE token = NULL;
HANDLE token_dup;
BOOL ret = FALSE;
HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (snap == INVALID_HANDLE_VALUE) {
vd_printf("CreateToolhelp32Snapshot() failed %u", GetLastError());
return false;
}
ZeroMemory(&proc_entry, sizeof(proc_entry));
proc_entry.dwSize = sizeof(PROCESSENTRY32);
if (!Process32First(snap, &proc_entry)) {
vd_printf("Process32First() failed %u", GetLastError());
CloseHandle(snap);
return false;
}
do {
if (_tcsicmp(proc_entry.szExeFile, WINLOGON_FILENAME) == 0) {
DWORD winlogon_session_id = 0;
if (ProcessIdToSessionId(proc_entry.th32ProcessID, &winlogon_session_id) &&
winlogon_session_id == session_id) {
winlogon_pid = proc_entry.th32ProcessID;
break;
}
}
} while (Process32Next(snap, &proc_entry));
CloseHandle(snap);
if (winlogon_pid == 0) {
vd_printf("Winlogon not found");
return false;
}
winlogon_proc = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, winlogon_pid);
if (!winlogon_proc) {
vd_printf("OpenProcess() failed %u", GetLastError());
return false;
}
ret = OpenProcessToken(winlogon_proc, TOKEN_DUPLICATE, &token);
CloseHandle(winlogon_proc);
if (!ret) {
vd_printf("OpenProcessToken() failed %u", GetLastError());
return false;
}
ret = DuplicateTokenEx(token, MAXIMUM_ALLOWED, NULL, SecurityIdentification, TokenPrimary,
&token_dup);
CloseHandle(token);
if (!ret) {
vd_printf("DuplicateTokenEx() failed %u", GetLastError());
return false;
}
ret = CreateProcessAsUser(token_dup, application_name, command_line, process_attributes,
thread_attributes, inherit_handles, creation_flags, environment,
current_directory, startup_info, process_information);
CloseHandle(token_dup);
return ret;
}
bool VDService::launch_agent()
{
STARTUPINFO startup_info;
BOOL ret = FALSE;
ZeroMemory(&startup_info, sizeof(startup_info));
startup_info.cb = sizeof(startup_info);
startup_info.lpDesktop = TEXT("Winsta0\\winlogon");
ZeroMemory(&_agent_proc_info, sizeof(_agent_proc_info));
if (_system_version == SYS_VER_WIN_XP) {
if (_session_id == 0) {
ret = CreateProcess(_agent_path, _agent_path, NULL, NULL, FALSE, 0, NULL, NULL,
&startup_info, &_agent_proc_info);
} else {
for (int i = 0; i < CREATE_PROC_MAX_RETRIES; i++) {
ret = create_session_process_as_user(_session_id, TRUE, NULL, NULL, _agent_path,
NULL, NULL, FALSE, 0, NULL, NULL,
&startup_info, &_agent_proc_info);
if (ret) {
vd_printf("create_session_process_as_user #%d", i);
break;
}
Sleep(CREATE_PROC_INTERVAL_MS);
}
}
} else if (_system_version == SYS_VER_WIN_7) {
startup_info.lpDesktop = TEXT("Winsta0\\default");
ret = create_process_as_user(_session_id, _agent_path, _agent_path, NULL, NULL, FALSE, 0,
NULL, NULL, &startup_info, &_agent_proc_info);
} else {
vd_printf("Not supported in this system version");
return false;
}
if (!ret) {
vd_printf("CreateProcess() failed: %u", GetLastError());
return false;
}
_agent_alive = true;
if (_pipe_connected) {
vd_printf("Pipe already connected");
return false;
}
vd_printf("Wait for vdagent to connect");
if (ConnectNamedPipe(_pipe_state.pipe, NULL) || GetLastError() == ERROR_PIPE_CONNECTED) {
_pipe_connected = true;
_pending_reset = false;
vd_printf("Pipe connected by vdagent");
} else {
vd_printf("ConnectNamedPipe() failed: %u", GetLastError());
return false;
}
_events[VD_EVENT_AGENT] = _agent_proc_info.hProcess;
return true;
}
bool VDService::kill_agent()
{
DWORD exit_code = 0;
DWORD wait_ret;
bool ret = true;
if (!_agent_alive) {
return true;
}
_events[VD_EVENT_AGENT] = 0;
_agent_alive = false;
if (_pipe_connected) {
_pipe_connected = false;
DisconnectNamedPipe(_pipe_state.pipe);
}
if (GetProcessId(_agent_proc_info.hProcess)) {
wait_ret = WaitForSingleObject(_agent_proc_info.hProcess, 3000);
switch (wait_ret) {
case WAIT_OBJECT_0:
if (GetExitCodeProcess(_agent_proc_info.hProcess, &exit_code)) {
vd_printf("vdagent exit code %u", exit_code);
} else if (exit_code == STILL_ACTIVE) {
vd_printf("Failed killing vdagent");
ret = false;
} else {
vd_printf("GetExitCodeProcess() failed: %u", GetLastError());
}
break;
case WAIT_TIMEOUT:
vd_printf("Wait timeout");
ret = false;
break;
case WAIT_FAILED:
default:
vd_printf("WaitForSingleObject() failed: %u", GetLastError());
break;
}
}
CloseHandle(_agent_proc_info.hProcess);
CloseHandle(_agent_proc_info.hThread);
ZeroMemory(&_agent_proc_info, sizeof(_agent_proc_info));
return ret;
}
bool VDService::restart_agent(bool normal_restart)
{
DWORD time = GetTickCount();
bool ret = true;
MUTEX_LOCK(_agent_mutex);
if (!normal_restart && ++_agent_restarts > VD_AGENT_MAX_RESTARTS) {
vd_printf("Agent restarted too many times");
ret = false;
stop();
}
if (ret && kill_agent() && launch_agent()) {
if (time - _last_agent_restart_time > VD_AGENT_RESTART_COUNT_RESET_INTERVAL) {
_agent_restarts = 0;
}
_last_agent_restart_time = time;
ret = true;
read_pipe();
}
MUTEX_UNLOCK(_agent_mutex);
return ret;
}
void VDService::stop()
{
vd_printf("Service stopped");
_running = false;
if (_control_event && !SetEvent(_control_event)) {
vd_printf("SetEvent() failed: %u", GetLastError());
}
}
void VDService::send_logon()
{
if (_logon_event && !SetEvent(_logon_event)) {
vd_printf("SetEvent() failed: %u", GetLastError());
}
}
void VDService::pipe_write_completion()
{
VDPipeState* ps = &this->_pipe_state;
DWORD bytes;
if (!_running) {
return;
}
if (_pending_write) {
if (GetOverlappedResult(_pipe_state.pipe, &_pipe_state.write.overlap, &bytes, FALSE)) {
ps->write.start += bytes;
if (ps->write.start == ps->write.end) {
ps->write.start = ps->write.end = 0;
}
} else if (GetLastError() == ERROR_IO_PENDING){
vd_printf("Overlapped write is pending");
return;
} else {
vd_printf("GetOverlappedResult() failed : %d", GetLastError());
}
_pending_write = false;
}
if (ps->write.start < ps->write.end) {
_pending_write = true;
if (!WriteFile(ps->pipe, ps->write.data + ps->write.start,
ps->write.end - ps->write.start, NULL, &_pipe_state.write.overlap)) {
vd_printf("WriteFile() failed: %u", GetLastError());
_pending_write = false;
_pipe_connected = false;
DisconnectNamedPipe(_pipe_state.pipe);
}
} else {
_pending_write = false;
}
}
void VDService::read_pipe()
{
VDPipeState* ps = &_pipe_state;
DWORD bytes;
if (ps->read.end < sizeof(ps->read.data)) {
_pending_read = true;
if (ReadFile(ps->pipe, ps->read.data + ps->read.end, sizeof(ps->read.data) - ps->read.end,
&bytes, &ps->read.overlap) || GetLastError() == ERROR_MORE_DATA) {
_pending_read = false;
vd_printf("ReadFile without pending %u", bytes);
handle_pipe_data(bytes);
read_pipe();
} else if (GetLastError() != ERROR_IO_PENDING) {
vd_printf("ReadFile() failed: %u", GetLastError());
_pending_read = false;
_pipe_connected = false;
DisconnectNamedPipe(_pipe_state.pipe);
}
} else {
_pending_read = false;
}
}
//FIXME: division to max size chunks should be here, not in the agent
void VDService::handle_pipe_data(DWORD bytes)
{
VDPipeState* ps = &_pipe_state;
DWORD read_size;
if (bytes) {
_pending_read = false;
}
if (!_running) {
return;
}
ps->read.end += bytes;
while (_running && (read_size = ps->read.end - ps->read.start) >= sizeof(VDPipeMessage)) {
VDPipeMessage* pipe_msg = (VDPipeMessage*)&ps->read.data[ps->read.start];
if (pipe_msg->type != VD_AGENT_COMMAND) {
handle_agent_control(pipe_msg);
ps->read.start += sizeof(VDPipeMessage);
continue;
}
if (read_size < sizeof(VDPipeMessage) + pipe_msg->size) {
break;
}
if (_vdi_port->write_ring_free_space() < sizeof(VDIChunkHeader) + pipe_msg->size) {
//vd_printf("DEBUG: no space in write ring %u", _vdi_port->write_ring_free_space());
break;
}
if (!_pending_reset) {
VDIChunkHeader chunk;
chunk.port = pipe_msg->opaque;
chunk.size = pipe_msg->size;
if (_vdi_port->ring_write(&chunk, sizeof(chunk)) != sizeof(chunk) ||
_vdi_port->ring_write(pipe_msg->data, chunk.size) != chunk.size) {
vd_printf("ring_write failed");
_running = false;
return;
}
}
ps->read.start += (sizeof(VDPipeMessage) + pipe_msg->size);
}
if (ps->read.start == ps->read.end && !_pending_read) {
DWORD prev_read_end = ps->read.end;
ps->read.start = ps->read.end = 0;
if (prev_read_end == sizeof(ps->read.data)) {
read_pipe();
}
}
}
void VDService::handle_port_data()
{
VDPipeMessage* pipe_msg;
VDIChunkHeader chunk;
int chunks_count = 0;
DWORD count = 0;
while (_running) {
if (!_chunk_size && _vdi_port->read_ring_size() >= sizeof(chunk)) {
if (_vdi_port->ring_read(&chunk, sizeof(chunk)) != sizeof(chunk)) {
vd_printf("ring_read of chunk header failed");
_running = false;
break;
}
count = sizeof(VDPipeMessage) + chunk.size;
if (_pipe_state.write.end + count > sizeof(_pipe_state.write.data)) {
vd_printf("chunk is too large, size %u port %u", chunk.size, chunk.port);
_running = false;
break;
}
_chunk_size = chunk.size;
_chunk_port = chunk.port;
}
if (_chunk_size && _vdi_port->read_ring_size() >= _chunk_size) {
count = sizeof(VDPipeMessage) + _chunk_size;
ASSERT(_pipe_state.write.end + count <= sizeof(_pipe_state.write.data));
pipe_msg = (VDPipeMessage*)&_pipe_state.write.data[_pipe_state.write.end];
if (_vdi_port->ring_read(pipe_msg->data, _chunk_size) != _chunk_size) {
vd_printf("ring_read of chunk data failed");
_running = false;
break;
}
if (_pipe_connected) {
pipe_msg->type = VD_AGENT_COMMAND;
pipe_msg->opaque = _chunk_port;
pipe_msg->size = _chunk_size;
_pipe_state.write.end += count;
chunks_count++;
} else {
_pipe_state.write.start = _pipe_state.write.end = 0;
}
_chunk_size = 0;
_chunk_port = 0;
} else {
break;
}
}
if (_pipe_connected && chunks_count && !_pending_write) {
pipe_write_completion();
}
}
bool VDService::handle_agent_control(VDPipeMessage* msg)
{
switch (msg->type) {
case VD_AGENT_RESET_ACK: {
if (msg->opaque != _connection_id) {
vd_printf("Agent reset ack mismatch %u %u", msg->opaque, _connection_id);
break;
}
vd_printf("Agent reset ack");
_pending_reset = false;
break;
}
default:
vd_printf("Unsupported control %u %u", msg->type, msg->opaque);
return false;
}
return true;
}
void VDService::write_agent_control(uint32_t type, uint32_t opaque)
{
if (!_pipe_connected) {
return;
}
if (_pipe_state.write.end + sizeof(VDPipeMessage) > sizeof(_pipe_state.write.data)) {
vd_printf("msg is too large");
_running = false;
return;
}
VDPipeMessage* msg = (VDPipeMessage*)&_pipe_state.write.data[_pipe_state.write.end];
msg->type = type;
msg->opaque = opaque;
_pipe_state.write.end += sizeof(VDPipeMessage);
if (!_pending_write) {
pipe_write_completion();
}
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
bool success = false;
if (!supported_system_version()) {
printf("vdservice is not supported in this system version\n");
return -1;
}
VDService* vdservice = VDService::get();
if (argc > 1) {
if (lstrcmpi(argv[1], TEXT("install")) == 0) {
success = vdservice->install();
} else if (lstrcmpi(argv[1], TEXT("uninstall")) == 0) {
success = vdservice->uninstall();
} else {
printf("Use: vdservice install / uninstall\n");
}
} else {
success = vdservice->run();
}
delete vdservice;
return (success ? 0 : -1);
}
|