-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilerail_server.c
430 lines (398 loc) · 11.2 KB
/
filerail_server.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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <signal.h>
#include <errno.h>
#include "filerail/global.h"
#include "filerail/constants.h"
#include "filerail/protocol.h"
#include "filerail/socket.h"
#include "filerail/utils.h"
#include "filerail/crypto.h"
#include "filerail/operations.h"
// read the exit status to prevent zombies
static void handler(int signum) {
if (signum == SIGCHLD) {
while (waitpid(-1, NULL, WNOHANG) > 0) {
;
}
}
}
int main(int argc, char *argv[]) {
// arguemet parsing variables
int opt;
extern char *optarg;
extern int optopt;
bool should_resolve;
char *ip, *port, *key_path, *ckpt_path;
// logging related variables
extern int verbose;
extern int is_server;
// socket related variables and exit_status
int fd, clifd, exit_status;
pid_t pid, sid;
socklen_t addrlen;
struct sockaddr_in cliaddr;
// get/put related variables
filerail_command_header command;
filerail_resource_header resource;
filerail_response_header response;
filerail_AES_keys K;
struct stat stat_path;
char resource_path[MAX_PATH_LENGTH];
// signal related
struct sigaction act;
// register for SIGCHLD
memset(&act, 0, sizeof(act));
act.sa_handler = &handler;
if (sigaction(SIGCHLD, &act, NULL) < 0) {
LOG(LOG_ERR | LOG_USER, "sigaction");
goto parent_clean_up;
}
exit_status = 0;
should_resolve = false;
is_server = 1;
// parse command line arguement
ip = port = key_path = ckpt_path = NULL;
while ((opt = getopt(argc, argv, "uvqi:p:k:m:c:n")) != -1) {
switch(opt) {
case 'u' : {
printf(
"usage: -v [-i ipv4 address]"
" [-p port] [-k key file]"
" [-c checkpoint directory] [-n dns resolution]\n");
goto parent_clean_up;
}
case 'v': {
verbose = 1;
break;
}
case 'q': {
verbose = 0;
break;
}
case 'i': {
ip = optarg;
break;
}
case 'p': {
port = optarg;
break;
}
case 'k' : {
key_path = optarg;
break;
}
case 'c' : {
ckpt_path = optarg;
break;
}
case 'n' : {
should_resolve = true;
break;
}
case '?' : {
if (optopt == 'i' || optopt == 'p' || optopt == 'k' || optopt == 'c') {
printf("-%c option requires value\n", optopt);
goto parent_clean_up;
} else {
printf("-%c option is unknown\n", optopt);
}
break;
}
default: {
LOG(LOG_ERR | LOG_USER, "filerail_server getopt");
goto parent_clean_up;
}
}
}
// check important fields
if (ip == NULL || port == NULL || key_path == NULL || ckpt_path == NULL) {
printf("-i, -p, -k and -c are required options\n");
goto parent_clean_up;
}
// check if key file exists
if (!filerail_is_exists(key_path, &stat_path)) {
printf("Couldn't open key file\n");
goto parent_clean_up;
}
// if it exists, read keys
if (filerail_read_AES_keys(key_path, &K) == -1) {
exit_status = -1;
goto parent_clean_up;
}
// check if checkpoint directory exists, if not create one
if (!filerail_is_exists(ckpt_path, &stat_path)) {
if (filerail_mkdir(ckpt_path) == -1) {
printf("Failed to create checkpoints directory at %s\n", ckpt_path);
exit_status = -1;
goto parent_clean_up;
}
}
// dns resolution if option provided
if (should_resolve && (filerail_dns_resolve(ip) == -1)) {
goto parent_clean_up;
}
printf("Send SIGINT on this terminal, if the terminal is not released by process...\n");
// daemonize
pid = fork();
if (pid < 0) {
exit_status = -1;
goto parent_clean_up;
}
if (pid > 0) {
goto parent_clean_up;
}
umask(0);
openlog("filerail", LOG_PID, LOG_USER);
sid = setsid();
if (sid < 0) {
exit_status = -1;
goto parent_clean_up;
}
if (chdir("/") < 0) {
exit_status = -1;
goto parent_clean_up;
}
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
// start the server
if ((fd = filerail_create_tcp_server(ip, port)) == -1) {
exit_status = -1;
goto parent_clean_up;
}
while (true) {
memset(&cliaddr, 0, sizeof(cliaddr));
// accept client
clifd = filerail_accept(fd, (struct sockaddr*)&cliaddr, &addrlen);
if (clifd == -1) {
// if interrupted don't exit
if (errno != EINTR) {
exit_status = -1;
goto parent_clean_up;
}
continue;
}
// print about who joined
if (filerail_who(clifd, "joined") == -1) {
exit_status = -1;
goto parent_clean_up;
}
// fork and serve child process
pid = fork();
if (pid == -1) {
LOG(LOG_ERR | LOG_USER, "filerail_server fork\n");
exit_status = -1;
goto parent_clean_up;
} else if (pid == 0) {
close(fd);
// receive the command sent by client
if (filerail_recv_command_header(clifd, &command) == -1) {
exit_status = -1;
goto child_clean_up;
}
if (command.command_type == PUT) {
/*
Servers wait for meta data about resource which will be uploaded.
Server checks:
1. Check if there is sufficient stoarge
2. Duplicate condition (inform client about this and wait for response)
3. Check if resource is writeable
4. If there are no duplicates (resource with same resource name as sent by client), check if destination
directory is present and writeable.
If 2. is responded with OVERWRITE or 4. passes, upload process starts
*/
// receive information about resource which is about to be sent by client
if (filerail_recv_resource_header(clifd, &resource) == -1) {
exit_status = -1;
goto child_clean_up;
}
resource_path[0] = '\0';
strcpy(resource_path, resource.resource_dir);
strcat(resource_path, "/");
strcat(resource_path, resource.resource_name);
// check size feasibility
if (filerail_check_storage_size(resource.resource_size)) {
// check if resource already exists
if (filerail_is_exists(resource_path, &stat_path)) {
// check if it is writeable
if (filerail_is_writeable(resource_path)) {
// inform client about duplicate resource name, at resource dir
if (filerail_send_response_header(clifd, DUPLICATE_RESOURCE_NAME) == -1) {
exit_status = -1;
goto child_clean_up;
}
// receive command
if (filerail_recv_command_header(clifd, &command) == -1) {
exit_status = -1;
goto child_clean_up;
}
if (command.command_type == OVERWRITE) {
// if overwrite (remove old resource)
if (filerail_rm(resource_path) == -1) {
exit_status = -1;
goto child_clean_up;
}
// start transfer processs
put_file:
strcat(resource_path, ".zip");
if (
filerail_recvfile_handler(
clifd,
resource.resource_name,
resource.resource_dir,
resource_path,
ckpt_path,
&K
) == -1) {
exit_status = -1;
}
} else if (command.command_type == ABORT) {
// do nothing wait for clean up
} else {
LOG(LOG_INFO | LOG_USER, "PROTOCOL NOT FOLLOWED\n");
}
} else {
if (filerail_send_response_header(clifd, NO_ACCESS) == -1) {
exit_status = -1;
}
}
} else {
// if file doesnt exist, check if dir is accessible
if (stat(resource.resource_dir, &stat_path) == -1) {
exit_status = -1;
LOG(LOG_ERR | LOG_USER, "filerail_server main stat\n");
if (filerail_send_response_header(clifd, NOT_FOUND) == -1) {
exit_status = -1;
}
} else {
// if dir is accessible, check if dir is writeable
if (filerail_is_writeable(resource.resource_dir)) {
if (filerail_send_response_header(clifd, OK) == -1) {
exit_status = -1;
goto child_clean_up;
}
goto put_file;
} else {
if (filerail_send_response_header(clifd, NO_ACCESS) == -1) {
exit_status = -1;
}
}
}
}
} else {
if (filerail_send_response_header(clifd, INSUFFICIENT_SPACE) == -1) {
exit_status = -1;
}
}
} else if (command.command_type == PING) {
/*
Server responds with PONG, so that client can check connectivity.
*/
if (filerail_send_response_header(clifd, PONG) == -1) {
exit_status = -1;
}
} else if(command.command_type == GET) {
/*
Server receives meta data about the resource that client needs.
Server checks:
1. Check if resource requested exists
2. Check if resource requested is readable
3. Check if resource requested is file or directory
If all test passes server sends OK, so that client prepares to receive messages.
Server sends the size of file.
Client checks if file size is feasible, and sends ABORT/OK.
If client sends OK, download process starts.
*/
// receive the resource request from client
if (filerail_recv_resource_header(clifd, &resource) == -1) {
exit_status = -1;
goto child_clean_up;
}
resource_path[0] = '\0';
strcpy(resource_path, resource.resource_dir);
strcat(resource_path, "/");
strcat(resource_path, resource.resource_name);
// check if it exists
if (filerail_is_exists(resource_path, &stat_path)) {
// check if resource requested is readable
if (filerail_is_readable(resource_path)) {
// check if resource is file or dir
if (filerail_is_file(&stat_path) || filerail_is_dir(&stat_path)) {
// indicate server is ready
if (filerail_send_response_header(clifd, OK) == -1) {
exit_status = -1;
}
// advertize the resource size (unzipped)
if (filerail_send_resource_header(clifd, "\0", "\0", stat_path.st_size) == -1) {
exit_status = -1;
goto child_clean_up;
}
// wait for response from client
if (filerail_recv_response_header(clifd, &response) == -1) {
exit_status = -1;
goto child_clean_up;
}
if (response.response_type == ABORT) {
// do nothing wait for clean up
} else if (response.response_type == OK) {
// start transfer process
if (
filerail_sendfile_handler(
clifd,
resource.resource_dir,
resource.resource_name,
&stat_path,
ckpt_path,
&K) == -1) {
exit_status = -1;
}
} else {
LOG(LOG_INFO | LOG_USER, "PROTOCOL NOT FOLLOWED\n");
}
} else {
if (filerail_send_response_header(clifd, BAD_RESOURCE) == -1) {
exit_status = -1;
}
}
} else {
if (filerail_send_response_header(clifd, NO_ACCESS) == -1) {
exit_status = -1;
}
}
} else {
if (filerail_send_response_header(clifd, NOT_FOUND) == -1) {
exit_status = -1;
}
}
} else {
LOG(LOG_ERR | LOG_USER, "PROTOCOL NOT FOLLOWED\n");
}
child_clean_up:
filerail_close(clifd);
if (exit_status == -1) {
LOG(LOG_INFO | LOG_USER, "child process, FAILED\n");
} else {
LOG(LOG_INFO | LOG_USER, "child process, SUCCESS\n");
}
return exit_status;
} else {
// parent doesn't need the client socket
filerail_close(clifd);
}
}
parent_clean_up:
filerail_close(fd);
if (exit_status == -1) {
LOG(LOG_INFO | LOG_USER, "parent process, FAILED\n");
} else {
LOG(LOG_INFO | LOG_USER, "parent process, SUCCESS\n");
}
closelog();
return exit_status;
}