-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
280 lines (266 loc) · 9.28 KB
/
Copy pathserver.c
File metadata and controls
280 lines (266 loc) · 9.28 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include "common.h"
#include "config.h"
#include <errno.h>
#include <netdb.h>
#include <pty.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
enum { FRAME_DATA = 1, FRAME_EXIT = 2, FRAME_ERROR = 3 };
enum { FRAME_INPUT = 1, FRAME_TERMINATE = 2, FRAME_EOF = 3,
FRAME_RESIZE = 4 };
#define STREAM_CHUNK 4096U
static int constant_time_equal(const char *a, const char *b) {
size_t alen = strlen(a), blen = strlen(b), i, n = alen > blen ? alen : blen;
unsigned char difference = (unsigned char)(alen ^ blen);
for (i = 0; i < n; i++) {
unsigned char ac = i < alen ? (unsigned char)a[i] : 0;
unsigned char bc = i < blen ? (unsigned char)b[i] : 0;
difference |= ac ^ bc;
}
return difference == 0;
}
static int listen_socket(const char *address, const char *port) {
struct addrinfo hints, *list, *item;
int fd = -1, yes = 1;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if (getaddrinfo(address, port, &hints, &list) != 0) return -1;
for (item = list; item; item = item->ai_next) {
fd = socket(item->ai_family, item->ai_socktype, item->ai_protocol);
if (fd < 0) continue;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
if (bind(fd, item->ai_addr, item->ai_addrlen) == 0 && listen(fd, 16) == 0) break;
close(fd);
fd = -1;
}
freeaddrinfo(list);
return fd;
}
static int send_frame(int fd, uint32_t type, const void *data, uint32_t length) {
return send_u32(fd, type) < 0 ? -1 : send_blob(fd, data, length);
}
static int write_all_fd(int fd, const void *data, size_t length) {
const unsigned char *p = data;
while (length) {
ssize_t n = write(fd, p, length);
if (n < 0 && errno == EINTR) continue;
if (n <= 0) return -1;
p += (size_t)n;
length -= (size_t)n;
}
return 0;
}
static uint32_t status_code(int status) {
if (WIFEXITED(status)) return (uint32_t)WEXITSTATUS(status);
if (WIFSIGNALED(status)) return (uint32_t)(128 + WTERMSIG(status));
return 125;
}
static int buffered_command(int fd, const struct command *command) {
int pipefd[2], status;
pid_t child;
size_t used = 0, capacity = 4096;
char *buffer = malloc(capacity);
if (!buffer || pipe(pipefd) < 0) { free(buffer); return -1; }
child = fork();
if (child < 0) { close(pipefd[0]); close(pipefd[1]); free(buffer); return -1; }
if (child == 0) {
close(pipefd[0]);
if (dup2(pipefd[1], STDOUT_FILENO) < 0) _exit(126);
close(pipefd[1]);
execvp(command->argv[0], command->argv);
_exit(127);
}
close(pipefd[1]);
for (;;) {
ssize_t n;
if (used == capacity) {
char *larger;
if (capacity == 16U * 1024U * 1024U) { kill(child, SIGKILL); break; }
capacity *= 2;
if (capacity > 16U * 1024U * 1024U) capacity = 16U * 1024U * 1024U;
larger = realloc(buffer, capacity);
if (!larger) { kill(child, SIGKILL); break; }
buffer = larger;
}
n = read(pipefd[0], buffer + used, capacity - used);
if (n < 0 && errno == EINTR) continue;
if (n <= 0) break;
used += (size_t)n;
}
close(pipefd[0]);
while (waitpid(child, &status, 0) < 0 && errno == EINTR) {}
if (send_frame(fd, FRAME_DATA, buffer, (uint32_t)used) < 0) {
free(buffer);
return -1;
}
free(buffer);
{
uint32_t code = status_code(status);
return send_frame(fd, FRAME_EXIT, &code, sizeof(code));
}
}
static int stream_command(int fd, const struct command *command) {
struct winsize initial_size;
int master, status = 0, child_done = 0, terminating = 0;
memset(&initial_size, 0, sizeof(initial_size));
initial_size.ws_row = 24;
initial_size.ws_col = 80;
pid_t child = forkpty(&master, NULL, NULL, &initial_size);
if (child < 0) {
const char message[] = "server could not start command";
return send_frame(fd, FRAME_ERROR, message, sizeof(message) - 1);
}
if (child == 0) {
execvp(command->argv[0], command->argv);
_exit(127);
}
while (!child_done) {
fd_set reads;
int maximum = master > fd ? master : fd;
FD_ZERO(&reads);
FD_SET(master, &reads);
FD_SET(fd, &reads);
if (select(maximum + 1, &reads, NULL, NULL, NULL) < 0) {
if (errno == EINTR) continue;
terminating = 1;
}
if (!terminating && FD_ISSET(master, &reads)) {
char buffer[STREAM_CHUNK];
ssize_t n = read(master, buffer, sizeof(buffer));
if (n > 0) {
if (send_frame(fd, FRAME_DATA, buffer, (uint32_t)n) < 0)
terminating = 1;
} else if (n == 0 || errno == EIO) {
while (waitpid(child, &status, 0) < 0 && errno == EINTR) {}
child_done = 1;
}
}
if (!terminating && FD_ISSET(fd, &reads)) {
uint32_t type, length;
char *data = NULL;
if (recv_u32(fd, &type) < 0 ||
recv_blob(fd, &data, &length, STREAM_CHUNK) < 0) {
terminating = 1;
} else if (type == FRAME_INPUT) {
if (write_all_fd(master, data, length) < 0) terminating = 1;
} else if (type == FRAME_EOF) {
const char eot = 4;
if (write_all_fd(master, &eot, 1) < 0) terminating = 1;
} else if (type == FRAME_TERMINATE) {
terminating = 1;
} else if (type == FRAME_RESIZE &&
length == 2 * sizeof(uint32_t)) {
uint32_t dimensions[2];
struct winsize size;
memcpy(dimensions, data, sizeof(dimensions));
memset(&size, 0, sizeof(size));
size.ws_row = dimensions[0] > UINT16_MAX
? UINT16_MAX : (unsigned short)dimensions[0];
size.ws_col = dimensions[1] > UINT16_MAX
? UINT16_MAX : (unsigned short)dimensions[1];
if (size.ws_row == 0 || size.ws_col == 0 ||
ioctl(master, TIOCSWINSZ, &size) < 0)
terminating = 1;
} else {
terminating = 1;
}
free(data);
}
if (terminating && !child_done) {
kill(-child, SIGTERM);
while (waitpid(child, &status, 0) < 0 && errno == EINTR) {}
child_done = 1;
}
}
close(master);
{
uint32_t code = status_code(status);
return send_frame(fd, FRAME_EXIT, &code, sizeof(code));
}
}
static void reap_children(int signal_number) {
int saved_errno = errno;
(void)signal_number;
while (waitpid(-1, NULL, WNOHANG) > 0) {}
errno = saved_errno;
}
static void handle_client(int fd, const struct settings *settings,
const struct command_list *commands) {
char *password = NULL, *name = NULL;
const struct command *command;
if (recv_blob(fd, &password, NULL, 4096) < 0) goto done;
if (!constant_time_equal(password, settings->password)) {
send_u32(fd, 0);
send_string(fd, "authentication failed");
goto done;
}
if (send_u32(fd, 1) < 0 || recv_blob(fd, &name, NULL, 1024) < 0) goto done;
command = find_command(commands, name);
if (!command) {
send_u32(fd, 0);
send_string(fd, "command is not allowed by the server");
goto done;
}
if (send_u32(fd, 1) == 0 && send_u32(fd, (uint32_t)command->stream) == 0) {
if (command->stream) stream_command(fd, command);
else buffered_command(fd, command);
}
done:
free(password);
free(name);
close(fd);
}
int main(int argc, char **argv) {
struct settings settings;
struct command_list commands;
int listener;
const char *path = argc > 1 ? argv[1] : "server.conf";
signal(SIGPIPE, SIG_IGN);
signal(SIGCHLD, reap_children);
if (load_settings(path, &settings) < 0 ||
load_commands(settings.commands_file, &commands) < 0) {
fprintf(stderr, "failed to load server configuration\n");
return 1;
}
listener = listen_socket(settings.address, settings.port);
if (listener < 0) {
perror("listen");
return 1;
}
printf("listening on %s:%s\n", settings.address ? settings.address : "*", settings.port);
fflush(stdout);
for (;;) {
int client = accept(listener, NULL, NULL);
if (client < 0 && errno == EINTR) continue;
if (client < 0) {
perror("accept");
break;
}
{
pid_t handler = fork();
if (handler == 0) {
signal(SIGCHLD, SIG_DFL);
close(listener);
handle_client(client, &settings, &commands);
_exit(0);
}
close(client);
if (handler < 0) perror("fork");
}
}
close(listener);
free_commands(&commands);
free_settings(&settings);
return 1;
}