1
1
/*
2
2
* Author: Manoj Ampalam <[email protected] >
3
3
* ssh-agent implementation on Windows
4
- *
4
+ *
5
5
* Copyright (c) 2015 Microsoft Corp.
6
6
* All rights reserved
7
7
*
@@ -49,16 +49,16 @@ static VOID ReportSvcStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD d
49
49
service_status .dwWin32ExitCode = dwWin32ExitCode ;
50
50
service_status .dwWaitHint = dwWaitHint ;
51
51
52
- if (dwCurrentState == SERVICE_START_PENDING )
52
+ if (dwCurrentState == SERVICE_START_PENDING )
53
53
service_status .dwControlsAccepted = 0 ;
54
54
else
55
55
service_status .dwControlsAccepted = SERVICE_ACCEPT_STOP ;
56
-
56
+
57
57
if ((dwCurrentState == SERVICE_RUNNING ) || (dwCurrentState == SERVICE_STOPPED ))
58
58
service_status .dwCheckPoint = 0 ;
59
59
else
60
60
service_status .dwCheckPoint = 1 ;
61
-
61
+
62
62
SetServiceStatus (service_status_handle , & service_status );
63
63
}
64
64
@@ -83,29 +83,34 @@ static VOID WINAPI service_handler(DWORD dwControl)
83
83
84
84
BOOL WINAPI ctrl_c_handler (
85
85
_In_ DWORD dwCtrlType
86
- ) {
86
+ ) {
87
87
/* for any Ctrl type, shutdown agent*/
88
88
debug ("Ctrl+C received" );
89
89
agent_shutdown ();
90
90
return TRUE;
91
91
}
92
92
93
93
int wmain (int argc , wchar_t * * argv ) {
94
-
94
+
95
95
w32posix_initialize ();
96
96
load_config ();
97
- if (!StartServiceCtrlDispatcherW (dispatch_table )) {
97
+ if (!StartServiceCtrlDispatcherW (dispatch_table )) {
98
98
if (GetLastError () == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT ) {
99
- /*todo - support debug mode*/
100
- /*
101
- if (debugMode) {
102
- SetConsoleCtrlHandler(ctrl_c_handler, TRUE);
103
- log_init("ssh-agent", 7, 1, 1);
104
- agent_start(TRUE, FALSE, 0);
105
- return 0;
106
- }
107
- */
99
+
108
100
if (argc == 2 ) {
101
+ if (wcsncmp (argv [1 ], L"-ddd" , 4 ) == 0 )
102
+ log_init ("ssh-agent" , 7 , 1 , 1 );
103
+ else if (wcsncmp (argv [1 ], L"-dd" , 3 ) == 0 )
104
+ log_init ("ssh-agent" , 6 , 1 , 1 );
105
+ else if (wcsncmp (argv [1 ], L"-d" , 2 ) == 0 )
106
+ log_init ("ssh-agent" , 5 , 1 , 1 );
107
+
108
+ if (wcsncmp (argv [1 ], L"-d" , 2 ) == 0 ) {
109
+ SetConsoleCtrlHandler (ctrl_c_handler , TRUE);
110
+ agent_start (TRUE, FALSE, 0 );
111
+ return 0 ;
112
+ }
113
+
109
114
/*agent process is likely a spawned child*/
110
115
char * h = 0 ;
111
116
h += _wtoi (* (argv + 1 ));
@@ -120,13 +125,13 @@ int wmain(int argc, wchar_t **argv) {
120
125
{
121
126
SC_HANDLE sc_handle , svc_handle ;
122
127
DWORD err ;
123
-
128
+
124
129
if ((sc_handle = OpenSCManagerW (NULL , NULL , SERVICE_START )) == NULL ||
125
- (svc_handle = OpenServiceW (sc_handle , L"ssh-agent" , SERVICE_START )) == NULL ){
130
+ (svc_handle = OpenServiceW (sc_handle , L"ssh-agent" , SERVICE_START )) == NULL ) {
126
131
fatal ("unable to open service handle" );
127
132
return -1 ;
128
133
}
129
-
134
+
130
135
if (StartService (svc_handle , 0 , NULL ) == FALSE && GetLastError () != ERROR_SERVICE_ALREADY_RUNNING ) {
131
136
fatal ("unable to start ssh-agent service, error :%d" , GetLastError ());
132
137
return -1 ;
@@ -146,7 +151,7 @@ int scm_start_service(DWORD num, LPWSTR* args) {
146
151
ZeroMemory (& service_status , sizeof (service_status ));
147
152
service_status .dwServiceType = SERVICE_WIN32_OWN_PROCESS ;
148
153
ReportSvcStatus (SERVICE_START_PENDING , NO_ERROR , 300 );
149
- ReportSvcStatus (SERVICE_RUNNING , NO_ERROR , 0 );
154
+ ReportSvcStatus (SERVICE_RUNNING , NO_ERROR , 0 );
150
155
log_init ("ssh-agent" , config_log_level (), 1 , 0 );
151
156
agent_start (FALSE, FALSE, 0 );
152
157
return 0 ;
0 commit comments