-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathshsrv.c
More file actions
242 lines (194 loc) · 4.93 KB
/
Copy pathshsrv.c
File metadata and controls
242 lines (194 loc) · 4.93 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* Copyright (C) 2024 John Törnblom
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3, or (at your option) any
later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, see
<http://www.gnu.org/licenses/>. */
#include <arpa/inet.h>
#include <fcntl.h>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#include <sys/sysctl.h>
#include <unistd.h>
#include <ps5/kernel.h>
#include <ps5/klog.h>
#include "elfldr.h"
#include "notify.h"
#include "sh.elf.inc"
/**
* Serve access to sh.elf.
**/
static int
serve_sh(uint16_t port, int notify_user) {
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
char ip[INET_ADDRSTRLEN];
char* argv[] = {"sh", 0};
struct ifaddrs *ifaddr;
int ifaddr_wait = 1;
socklen_t addr_len;
int optval;
int connfd;
int srvfd;
if(getifaddrs(&ifaddr) == -1) {
klog_perror("getifaddrs");
exit(EXIT_FAILURE);
}
// Enumerate all AF_INET IPs
for(struct ifaddrs *ifa=ifaddr; ifa!=NULL; ifa=ifa->ifa_next) {
if(ifa->ifa_addr == NULL) {
continue;
}
if(ifa->ifa_addr->sa_family != AF_INET) {
continue;
}
// skip localhost
if(!strncmp("lo", ifa->ifa_name, 2)) {
continue;
}
struct sockaddr_in *in = (struct sockaddr_in*)ifa->ifa_addr;
inet_ntop(AF_INET, &(in->sin_addr), ip, sizeof(ip));
// skip interfaces without an ip
if(!strncmp("0.", ip, 2)) {
continue;
}
ifaddr_wait = 0;
if(notify_user) {
notify("Serving shell on %s:%d (%s)", ip, port, ifa->ifa_name);
}
klog_printf("Serving shell on %s:%d (%s)\n", ip, port, ifa->ifa_name);
}
freeifaddrs(ifaddr);
if(ifaddr_wait) {
return 0;
}
if((srvfd=socket(AF_INET, SOCK_STREAM, 0)) < 0) {
klog_perror("socket");
return -1;
}
if(setsockopt(srvfd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int)) < 0) {
klog_perror("setsockopt");
return -1;
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(port);
if(bind(srvfd, (struct sockaddr*)&server_addr, sizeof(server_addr)) != 0) {
klog_perror("bind");
return -1;
}
if(listen(srvfd, 5) != 0) {
klog_perror("listen");
return -1;
}
while(1) {
addr_len = sizeof(client_addr);
if((connfd=accept(srvfd, (struct sockaddr*)&client_addr, &addr_len)) < 0) {
klog_perror("accept");
break;
}
optval = 1;
if(setsockopt(connfd, SOL_SOCKET, SO_KEEPALIVE, (char *)&optval,
sizeof (optval))) {
klog_perror("setsockopt");
break;
}
optval = 1;
if(setsockopt(connfd, IPPROTO_TCP, TCP_NODELAY, (char *)&optval,
sizeof (optval))) {
klog_perror("setsockopt");
break;
}
elfldr_spawn(connfd, connfd, -1, sh_elf, argv);
close(connfd);
}
return close(srvfd);
}
/**
* Get the pid of a process with the given name.
**/
static pid_t
find_pid(const char* name) {
int mib[4] = {1, 14, 8, 0};
pid_t mypid = getpid();
pid_t pid = -1;
size_t buf_size;
uint8_t *buf;
if(sysctl(mib, 4, 0, &buf_size, 0, 0)) {
klog_perror("sysctl");
return -1;
}
if(!(buf=malloc(buf_size))) {
klog_perror("malloc");
return -1;
}
if(sysctl(mib, 4, buf, &buf_size, 0, 0)) {
klog_perror("sysctl");
return -1;
}
for(uint8_t *ptr=buf; ptr<(buf+buf_size);) {
int ki_structsize = *(int*)ptr;
pid_t ki_pid = *(pid_t*)&ptr[72];
char *ki_tdname = (char*)&ptr[447];
ptr += ki_structsize;
if(!strcmp(name, ki_tdname) && mypid != ki_pid) {
pid = ki_pid;
}
}
free(buf);
return pid;
}
/**
* Initialize stdio to /dev/console
**/
static void
init_stdio(void) {
int fd = open("/dev/console", O_RDWR);
close(STDERR_FILENO);
close(STDOUT_FILENO);
close(STDIN_FILENO);
dup2(fd, STDIN_FILENO);
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
close(fd);
}
/**
* Launch shsrv.elf.
**/
int
main(void) {
int notify_user = 1;
int port = 2323;
pid_t pid;
syscall(SYS_thr_set_name, -1, "shsrv.elf");
init_stdio();
klog_printf("Shell server was compiled at %s %s\n", __DATE__, __TIME__);
while((pid=find_pid("shsrv.elf")) > 0) {
if(kill(pid, SIGKILL)) {
klog_perror("kill");
}
sleep(1);
}
signal(SIGCHLD, SIG_IGN);
while(1) {
serve_sh(port, notify_user);
notify_user = 0;
sleep(3);
}
return 0;
}