-
Notifications
You must be signed in to change notification settings - Fork 16
/
runsshd.c
executable file
·93 lines (80 loc) · 2.25 KB
/
runsshd.c
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
#include <windows.h>
#include "log.h"
#define SSHD "sshd.exe"
int dirname(char *program, char *srvcmd) {
int i;
for (i = strlen(program); i > 0; i--) {
if (program[i] == '\\')
break;
}
if (i == 0)
return 0;
strncpy(srvcmd, program, i+1);
srvcmd[i+1] = '\0';
return i;
}
HANDLE launch_command(char *module, char *command, char *rundir) {
STARTUPINFO si;
PROCESS_INFORMATION pi;
int ret;
memset(&si, 0, sizeof(si));
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
ret = CreateProcess(module, command, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, rundir, &si, &pi);
if (!ret) {
printf("Failed to run %s %s with cwd %s - %s\n", module, command ? command : "",
rundir, get_error_msg(GetLastError()));
return INVALID_HANDLE_VALUE;
}
printf("Process started with pid %d\n", pi.dwProcessId);
CloseHandle(pi.hThread);
return pi.hProcess;
}
int monitor_child(HANDLE child) {
DWORD exitcode;
if (WaitForSingleObject(child, INFINITE) != WAIT_OBJECT_0) {
printf("Unable to monitor child process - %s\n", get_error_msg(GetLastError()));
CloseHandle(child);
return 0;
}
GetExitCodeProcess(child, &exitcode);
if (exitcode == 42)
printf("Child Controlled Exit. Relaunching\n");
else
printf("Child Unexpected Exit %d. Relaunching\n");
CloseHandle(child);
return 1;
}
int main(int argc, char **argv) {
int i;
HANDLE cproc;
char program[MAX_PATH], srv_cmd[MAX_PATH], rundir[MAX_PATH], args[8192]; /* cmd.exe limit */
if (!GetModuleFileName(NULL, program, sizeof(program))) {
printf("Could not get module name - %s\n", get_error_msg(GetLastError()));
return 0;
}
/* we need the dir part of the name & I can't find a dirname equivalent that is available
on win server 2003 & 2008 */
if (!dirname(program, rundir)) {
printf("Could not extract dirname from module name (%s)\n", program);
return 0;
}
strcpy(srv_cmd, rundir);
strcat(srv_cmd, SSHD);
if (argc > 1) {
for (i = 1; i < argc; i++) {
strcat(args, argv[i]);
strcat(args, " ");
}
}
printf("Launching %s %s\n", srv_cmd, argc > 1 ? args : "");
while (1) {
cproc = launch_command(srv_cmd, argc > 1 ? args : NULL, rundir);
if (cproc == INVALID_HANDLE_VALUE)
return 0;
if (!monitor_child(cproc))
return 0;
}
return 1;
}