-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInit.c
More file actions
105 lines (94 loc) · 3.26 KB
/
Init.c
File metadata and controls
105 lines (94 loc) · 3.26 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#define likely(x) __builtin_expect((x), 1)
#define unlikely(x) __builtin_expect((x), 0)
void runService_SSHServer(void) {
// Allocate Space for Variables
char *SSH_PUBLIC_KEY = NULL;
// Get SSH_PUBLIC_KEY from Environment
SSH_PUBLIC_KEY = getenv("SSH_PUBLIC_KEY");
if (unlikely(SSH_PUBLIC_KEY == NULL)) {
printf("\033[0;31m%s\033[0m%s\n",
"ERROR: ", "Failed to get \"SSH_PUBLIC_KEY\" from the Environment");
exit(EXIT_FAILURE);
}
// Start openSSH-Server Only When SSH_PUBLIC_KEY is Set
if (unlikely(strcmp(SSH_PUBLIC_KEY, "YOUR_SSH_PUBLIC_KEY") != 0)) {
int fileDescriptor = open("/home/coder/.ssh/authorized_keys", O_WRONLY | O_CREAT);
if (unlikely(fileDescriptor == -1)) {
printf("\033[0;31m%s\033[0m%s\n",
"ERROR: ", "Failed Write SSH Public Key to File");
exit(EXIT_FAILURE);
}
int bytesWritten =
write(fileDescriptor, SSH_PUBLIC_KEY, strlen(SSH_PUBLIC_KEY));
if (unlikely(bytesWritten != strlen(SSH_PUBLIC_KEY))) {
printf("\033[0;31m%s\033[0m%s\n",
"ERROR: ", "Failed Write SSH Public Key to File");
exit(EXIT_FAILURE);
}
close(fileDescriptor);
printf("\033[0;32m%s\033[0m%s\n", "INFO: ", "Starting openSSH Server");
fflush(stdout);
pid_t pidSSH = fork();
if (unlikely(pidSSH == -1)) {
printf("\033[0;31m%s\033[0m%s\n", "ERROR: ", "Failed to Fork");
exit(EXIT_FAILURE);
} else if (pidSSH == 0) {
fclose(stdin);
freopen("/var/log/Shigure/ssh/out", "w", stdout);
freopen("/var/log/Shigure/ssh/err", "w", stderr);
seteuid(1000);
execl("/usr/sbin/service", "service", "ssh", "start", NULL);
printf("\033[0;31m%s\033[0m%s\n", "ERROR: ", "Failed to Launch openSSH Server");
exit(EXIT_FAILURE);
}
waitpid(pidSSH, NULL, 0);
} else {
printf("\033[0;33m%s\033[0m%s\n", "WARNING: ",
"\"SSH_PUBLIC_KEY\" not Set, not Starting openSSH Server");
}
}
void runService_CodeServer(void) {
// Start Code-Server
printf("\033[0;32m%s\033[0m%s\n", "INFO: ", "Starting Code-Server");
fflush(stdout);
pid_t pidCodeServer = fork();
if (unlikely(pidCodeServer == -1)) {
printf("\033[0;31m%s\033[0m%s\n", "ERROR: ", "Failed to Fork");
exit(EXIT_FAILURE);
} else if (pidCodeServer == 0) {
fflush(stdout);
fclose(stdin);
freopen("/var/log/Shigure/code-server/out", "w", stdout);
freopen("/var/log/Shigure/code-server/err", "w", stderr);
setuid(1000);
execl("/usr/bin/code-server", "code-server", NULL);
printf("\033[0;31m%s\033[0m%s\n", "ERROR: ", "Failed to Launch Code-Server");
exit(EXIT_FAILURE);
}
waitpid(pidCodeServer, NULL, 0);
}
int main(void) {
// Refuse to Start as Non-Pid=1 Program
if (getpid() != 1) {
printf("\033[0;31m%s\033[0m%s\n", "ERROR: ", "Must be Run as PID 1");
exit(EXIT_FAILURE);
}
// Refuse to Start as Non-Uid=1 Program
// if (geteuid() != 0) {
// printf("\033[0;31m%s\033[0m%s\n", "ERROR: ", "Must be Run as UID 1");
// exit(EXIT_FAILURE);
// }
runService_SSHServer();
runService_CodeServer();
// Collect Zombine Process
while (1) {
waitpid(-1, NULL, 0);
}
return EXIT_SUCCESS;
}