11/*
22 * Author: Manoj Ampalam <manoj.ampalam@microsoft.com>
33 * ssh-agent implementation on Windows
4- *
4+ *
55 * Copyright (c) 2015 Microsoft Corp.
66 * All rights reserved
77 *
@@ -49,16 +49,16 @@ static VOID ReportSvcStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD d
4949 service_status .dwWin32ExitCode = dwWin32ExitCode ;
5050 service_status .dwWaitHint = dwWaitHint ;
5151
52- if (dwCurrentState == SERVICE_START_PENDING )
52+ if (dwCurrentState == SERVICE_START_PENDING )
5353 service_status .dwControlsAccepted = 0 ;
5454 else
5555 service_status .dwControlsAccepted = SERVICE_ACCEPT_STOP ;
56-
56+
5757 if ((dwCurrentState == SERVICE_RUNNING ) || (dwCurrentState == SERVICE_STOPPED ))
5858 service_status .dwCheckPoint = 0 ;
5959 else
6060 service_status .dwCheckPoint = 1 ;
61-
61+
6262 SetServiceStatus (service_status_handle , & service_status );
6363}
6464
@@ -83,29 +83,34 @@ static VOID WINAPI service_handler(DWORD dwControl)
8383
8484BOOL WINAPI ctrl_c_handler (
8585 _In_ DWORD dwCtrlType
86- ) {
86+ ) {
8787 /* for any Ctrl type, shutdown agent*/
8888 debug ("Ctrl+C received" );
8989 agent_shutdown ();
9090 return TRUE;
9191}
9292
9393int wmain (int argc , wchar_t * * argv ) {
94-
94+
9595 w32posix_initialize ();
9696 load_config ();
97- if (!StartServiceCtrlDispatcherW (dispatch_table )) {
97+ if (!StartServiceCtrlDispatcherW (dispatch_table )) {
9898 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+
108100 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+
109114 /*agent process is likely a spawned child*/
110115 char * h = 0 ;
111116 h += _wtoi (* (argv + 1 ));
@@ -120,13 +125,13 @@ int wmain(int argc, wchar_t **argv) {
120125 {
121126 SC_HANDLE sc_handle , svc_handle ;
122127 DWORD err ;
123-
128+
124129 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 ) {
126131 fatal ("unable to open service handle" );
127132 return -1 ;
128133 }
129-
134+
130135 if (StartService (svc_handle , 0 , NULL ) == FALSE && GetLastError () != ERROR_SERVICE_ALREADY_RUNNING ) {
131136 fatal ("unable to start ssh-agent service, error :%d" , GetLastError ());
132137 return -1 ;
@@ -146,7 +151,7 @@ int scm_start_service(DWORD num, LPWSTR* args) {
146151 ZeroMemory (& service_status , sizeof (service_status ));
147152 service_status .dwServiceType = SERVICE_WIN32_OWN_PROCESS ;
148153 ReportSvcStatus (SERVICE_START_PENDING , NO_ERROR , 300 );
149- ReportSvcStatus (SERVICE_RUNNING , NO_ERROR , 0 );
154+ ReportSvcStatus (SERVICE_RUNNING , NO_ERROR , 0 );
150155 log_init ("ssh-agent" , config_log_level (), 1 , 0 );
151156 agent_start (FALSE, FALSE, 0 );
152157 return 0 ;
0 commit comments