-
Notifications
You must be signed in to change notification settings - Fork 16
/
sshdservice.c
executable file
·67 lines (60 loc) · 1.83 KB
/
sshdservice.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
#include <windows.h>
#include <string.h>
#include "log.h"
#define SSHD_BANNER "Win32SSHD"
#define SSHD_BANNER_NICE "\"Win32 Secure Shell Server\""
void usage() {
fprintf(stderr, "Please specify one of -install, -uninstall");
exit(1);
}
int main(int argc, char **argv) {
if (argc < 2)
usage();
if (!strcmp(argv[1], "-install")) {
printf("%s will now be installed as a service\n", SSHD_BANNER_NICE);
if (!install_service()) {
fprintf(stderr, "Service installation failed !\n");
exit(1);
} else {
printf("Service installed\n");
exit(0);
}
} else if (!strcmp(argv[1], "-uninstall")) {
printf("%s will now be uninstalled\n", SSHD_BANNER_NICE);
if (!uninstall_service()) {
fprintf(stderr, "Service uninstall failed !\n");
exit(1);
} else {
printf("Service uninstalled\n");
exit(0);
}
} else
usage();
}
int uninstall_service() {
return 0;
}
int install_service() {
char program[MAX_PATH], srv_cmd[MAX_PATH + 256];
SC_HANDLE scmgr, service;
if (!GetModuleFileName(NULL, program, sizeof(program))) {
fprintf(stderr, "Could not get module name - %s\n", get_error_msg(GetLastError()));
return 0;
}
scmgr = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
if (!scmgr) {
fprintf(stderr, "Could not open service control manager - %s\n", get_error_msg(GetLastError()));
return 0;
}
sprintf(srv_cmd, "\"%s\" -start", program);
service = CreateService(scmgr, SSHD_BANNER, SSHD_BANNER_NICE, SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
srv_cmd, NULL, NULL, NULL, NULL, NULL);
if (!service) {
fprintf(stderr, "Could not register service for (%s) - %s\n", srv_cmd, get_error_msg(GetLastError()));
return 0;
}
CloseServiceHandle(service);
CloseServiceHandle(scmgr);
return 1;
}