Skip to content

Commit

Permalink
parse command line arguments for Windows service
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobBarthelmeh committed Aug 17, 2023
1 parent 9c0db8f commit 7229bad
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
41 changes: 32 additions & 9 deletions apps/wolfsshd/wolfsshd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1734,6 +1734,7 @@ char* myoptarg = NULL;

#ifdef _WIN32
#include <tchar.h>
#include <shellapi.h>

SERVICE_STATUS serviceStatus = { 0 };
SERVICE_STATUS_HANDLE serviceStatusHandle = NULL;
Expand Down Expand Up @@ -1809,23 +1810,41 @@ static int StartSSHD(int argc, char** argv)
#ifdef _WIN32
char** argv = NULL;
DWORD i;
LPWSTR* cmdArgs = NULL;
LPWSTR cmdLn;
int cmdArgC = 0;

/* get what the command line was and parse arguments from it */
cmdLn = GetCommandLineW();
cmdArgs = CommandLineToArgvW(cmdLn, &cmdArgC);
if (cmdArgs == NULL) {
ret = WS_FATAL_ERROR;
}
argc = cmdArgC;

for (i = 0; i < argc; i++) {
if (WSTRCMP((char*)(wargv[i]), "-D") == 0) {
isDaemon = 0;
if (ret == WS_SUCCESS) {
for (i = 0; i < argc; i++) {
if (WSTRCMP((char*)(cmdArgs[i]), "-D") == 0) {
isDaemon = 0;
}
}
}

if (isDaemon) {
/* Set the logging to go to OutputDebugString */
wolfSSH_SetLoggingCb(ServiceDebugCb);

/* we want the arguments to be normal char strings not wchar_t */
argv = WMALLOC(argc * sizeof(char*), NULL, DYNTYPE_SSHD);
{
unsigned int z;
for (z = 0; z < argc; z++) {
argv[z] = _convertHelper(wargv[z], NULL);
if (ret == WS_SUCCESS) {
/* we want the arguments to be normal char strings not wchar_t */
argv = (char**)WMALLOC(argc * sizeof(char*), NULL, DYNTYPE_SSHD);
if (argv == NULL) {
ret = WS_MEMORY_E;
}
else {
unsigned int z;
for (z = 0; z < argc; z++) {
argv[z] = _convertHelper(cmdArgs[z], NULL);
}
}
}
}
Expand Down Expand Up @@ -2039,6 +2058,10 @@ static int StartSSHD(int argc, char** argv)
}
return;
}

if (cmdArgs != NULL) {
LocalFree(cmdArgs);
}
}
#endif

Expand Down
4 changes: 2 additions & 2 deletions ide/winvs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ SSHD Service
-----------

Creating a new servie
`sc.exe create wolfSSHd binpath=D:\work\wolfssh\ide\winvs\Debug\x64\wolfsshd.exe`
`sc.exe create wolfSSHd binpath="D:\work\wolfssh\ide\winvs\Debug\x64\wolfsshd.exe -f <sshd_config fils> -h <optionally load host key> -p <optional port number>"`

Starting wolfSSHd service run the following command in an adminstrator power shell session:
`sc.exe start wolfSSHd -f <sshd_config fils> -h <optionally load host key> -p <optional port number>`
`sc.exe start wolfSSHd`

To stop the service run the following in an adminstrator power shell session:
`sc.exe stop wolfSSHd`
Expand Down

0 comments on commit 7229bad

Please sign in to comment.