-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathftpServer.c
438 lines (403 loc) · 13.8 KB
/
ftpServer.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
431
432
433
434
435
436
437
#include <string.h>
#include "virtualFileSystem/hiredis/hiredis.h"
#include "virtualFileSystem/vfs.h"
#include "virtualFileSystem/vfsPathParser.h"
#include "commonDefines.h"
#include "net/networking.h"
#include "net/connection.h"
#include "ftp/ftpCommon.h"
#include "ftp/ftp.h"
#include "ftp/ftpParser.h"
#include "httpProcessing/commonHTTP.h"//TODO: parser states
#include "httpProcessing/realtimePacketParser.h"//TODO: parser states
#include "httpProcessing/createHTTPHeader.h"
#include "google/googleAccessToken.h"
#include "google/googleUpload.h"
#include "crypto.h"
#include "fileTransfer.h"
#include "utils.h"
#define SERVER_LISTEN_PORT "25001"
#define FILE_SERVER_URL "localhost"
#define VALID_GREETING "220 Hey\r\n"
#define SEVER_IP_FTP "127,0,0,1"
#define STRING_BUFFER_LEN 2000
#define ENCRYPTED_BUFFER_LEN 2000
#define DECRYPTED_BUFFER_LEN 2000
#define AES_BLOCK_SIZE 16//fixme: get this from a config file
void flipBits(void* packetData, int size) {
char* b = packetData;
int i;
for (i = 0; i < size; ++i) {
b[i] = ~(b[i]);
}
}
void openDataConnection(ftpClientState_t *clientState) {
char strBuf1[1000]; //FIXME: hardcoded
int tempSock = getListeningSocket("0"); //FIXME: WHAT DO I DO WITH TEMPSOCK ?????
clientState->data_fd2 = tempSock;
int port = getPort(tempSock);
sprintf(strBuf1, "227 Entering Passive Mode (%s,%d,%d).\r\n", SEVER_IP_FTP,
port >> 8, port & 255);
sendFtpResponse(clientState, strBuf1);
socklen_t sin_size;
struct sockaddr_storage their_addr;
sin_size = sizeof their_addr;
clientState->data_fd = accept(tempSock, (struct sockaddr *) &their_addr,
&sin_size);
if (clientState->data_fd == -1) {
perror("accept");
}
clientState->isDataConnectionOpen = 1;
}
void ftp_handleFtpRequest( AccessTokenState_t *accessTokenState,
ftpHTTPParserState_t *parserState, ftpClientState_t *clientState ) {
int received, dataLength;
switch ( parserState->type ) {
case REQUEST_QUIT:
{
sendFtpResponse( clientState, "221 see ya.\r\n" );
break;
}
case REQUEST_USER:
{
strcpy( clientState->usernameBuffer, parserState->paramBuffer );
sendFtpResponse( clientState, "331 User name okay, need password.\r\n" );
clientState->loggedIn = 0;
break;
}
case REQUEST_PASS:
{
if ( isValidLogin( clientState->usernameBuffer,
parserState->paramBuffer ) ) {
sendFtpResponse( clientState,
"230 User logged in, proceed.\r\n" );
clientState->loggedIn = 1;
} else {
sendFtpResponse( clientState,
"430 Invalid username or password\r\n" );
}
break;
}
case REQUEST_SYST:
{
sendFtpResponse( clientState, "215 UNIX Type: L8\r\n" );
break;
}
case REQUEST_PWD:
{
char strBuf1[ STRING_BUFFER_LEN ], strBuf2[ STRING_BUFFER_LEN ];
printf( "PWD called, the CWD is %ld\n", clientState->ctx->cwd.id );
vfs_getCWDPath( clientState->ctx, strBuf1, STRING_BUFFER_LEN );
sprintf( strBuf2, "257 \"%s\"\r\n", strBuf1 );
sendFtpResponse( clientState, strBuf2 );
break;
}
case REQUEST_TYPE:
{
if ( strcmp( parserState->paramBuffer, "I" ) == 0 ) {
clientState->transferType = FTP_TRANSFER_TYPE_I;
sendFtpResponse( clientState, "200 Switching to Binary mode..\r\n" );
} else {
sendFtpResponse( clientState, "200 Switching to Binary mode..\r\n" );
//FIXME:
//sendFtpResponse( clientState, "504 Command not implemented for that parameter.\r\n" );
}
break;
}
case REQUEST_PASV:
{
if ( clientState->isDataConnectionOpen == 1 ) {
printf( "there was an open connection, it's closed\n" );
close( clientState->data_fd );
close( clientState->data_fd2 );
clientState->isDataConnectionOpen = 0;
}
openDataConnection( clientState );
break;
}
case REQUEST_SIZE:
{
char strBuf1[ STRING_BUFFER_LEN ], strBuf2[ STRING_BUFFER_LEN ];
vfsPathParserState_t vfsParserState;
long fileSize;
vfs_parsePath( clientState->ctx, &vfsParserState, parserState->paramBuffer,
strlen( parserState->paramBuffer ) );
if ( ( fileSize = vfs_getFileSize( clientState->ctx, &vfsParserState.fileObj ) ) == -1 ) {
sendFtpResponse( clientState, "550 Could not get file size.\r\n" );
} else {
sprintf( strBuf1, "213 %ld\r\n", fileSize );
sendFtpResponse( clientState, strBuf1 ); //TODO:
}
break;
}
case REQUEST_LIST:
{
sendFtpResponse( clientState,
"150 Here comes the directory listing.\r\n" );
int i = 0;
char *dirList = vfs_listUnixStyle( clientState->ctx, &clientState->ctx->cwd );
send( clientState->data_fd, dirList, strlen( dirList ), 0 );
if ( close( clientState->data_fd ) != 0 ) {
perror( "close:" );
}
if ( close( clientState->data_fd2 ) != 0 ) {
perror( "close2:" );
}
sendFtpResponse( clientState, "226 Directory send OK.\r\n" );
break;
}
case REQUEST_CWD:
{
vfsPathParserState_t vfsParserState;
vfs_parsePath( clientState->ctx, &vfsParserState, parserState->paramBuffer,
strlen(parserState->paramBuffer) );
if (vfsParserState.isExistingObject && vfsParserState.fileObj.isDir) {
vfs_set_cwd( clientState->ctx, &vfsParserState.fileObj );
sendFtpResponse(clientState,
"250 Directory successfully changed.\r\n"); //success
} else {
sendFtpResponse(clientState, "550 Failed to change directory.\r\n");
}
break;
}
case REQUEST_MKD:
{
vfs_mkdir(clientState->ctx, &clientState->ctx->cwd, parserState->paramBuffer);
sendFtpResponse(clientState, "257 MKDIR done\r\n");
break;
}
case REQUEST_STOR:
{
//we will under report the size of the buffers to functions when using them as input buffers
//so that we always meet the size+AES_BLOCK_SIZE requirements of the output buffer for the encrypt/decrypt functions
//see openssl docs for more info on ecrypted buffer/decrypted buffers size requirements
char encryptedDataBuffer[ ENCRYPTED_BUFFER_LEN + AES_BLOCK_SIZE ];
char decryptedDataBuffer[ DECRYPTED_BUFFER_LEN + AES_BLOCK_SIZE ];
CryptoState_t encryptionState;
int tempOutputSize;
Connection_t googleCon;
HTTPHeaderState_t hInfo;
char strBuf1[ STRING_BUFFER_LEN ], strBuf2[ STRING_BUFFER_LEN ];
GoogleUploadState_t fileState;
//get the file name
printf("trying to store file %s\n", parserState->paramBuffer);
//sprintf(strBuf1, "\"title\": \"%s\"", parserState->paramBuffer);
sprintf(strBuf1, "\"title\": \"%s\"", "nuffin.bin");
//check if the name is valid and get it's type
googleUpload_init(&googleCon, accessTokenState, strBuf1, "image/png");
//FIXME: make sure we have a connection open
sprintf(strBuf1, "150 FILE: /%s\r\n", parserState->paramBuffer);
sendFtpResponse(clientState, strBuf1);
//alright start reading in the file
startEncryption(&encryptionState, "phone");
long storFileSize = 0;
while ((received = recv(clientState->data_fd, decryptedDataBuffer,
DECRYPTED_BUFFER_LEN, 0)) > 0) {
storFileSize += received;
updateEncryption(&encryptionState, decryptedDataBuffer, received,
encryptedDataBuffer, &tempOutputSize);
googleUpload_update(&googleCon, encryptedDataBuffer,
tempOutputSize);
//printf("recv'd:--%.*s--\n", received, tempBuffer);
}
finishEncryption(&encryptionState, decryptedDataBuffer, received,
encryptedDataBuffer, &tempOutputSize);
googleUpload_update(&googleCon, encryptedDataBuffer, tempOutputSize);
googleUpload_end(&googleCon, &fileState);
vfs_createFile(clientState->ctx, &clientState->ctx->cwd, parserState->paramBuffer,
storFileSize, fileState.id, fileState.webUrl, fileState.apiUrl);
sendFtpResponse(clientState, "226 Transfer complete.\r\n");
if (close(clientState->data_fd) != 0) {
perror("close:");
}
if (close(clientState->data_fd2) != 0) {
perror("close:");
}
break;
}
case REQUEST_RETR:
{
//we will under report the size of the buffers to functions when using them as input buffers
//so that we always meet the size+AES_BLOCK_SIZE requirements of the output buffer for the encrypt/decrypt functions
//see openssl docs for more info on ecrypted buffer/decrypted buffers size requirements
char encryptedDataBuffer[ ENCRYPTED_BUFFER_LEN + AES_BLOCK_SIZE ];
char decryptedDataBuffer[ DECRYPTED_BUFFER_LEN + AES_BLOCK_SIZE ];
char strBuf1[ STRING_BUFFER_LEN ], strBuf2[ STRING_BUFFER_LEN ];
HTTPParserState_t googleParserState;
CryptoState_t decryptionState;
vfsPathParserState_t vfsParserState;
int tempOutputSize;
Connection_t googleCon;
HTTPHeaderState_t hInfo;
//FIXME: make sure we have a connection open
//send a get request to the and then continue the download
//take the download out to it's own file
vfs_parsePath( clientState->ctx, &vfsParserState, parserState->paramBuffer,
strlen(parserState->paramBuffer) );
//FIXME: handle if the file doesn't exist or if it's not a valid path
if( !vfsParserState.isFilePath ){
sendFtpResponse( clientState, "500 bad parameters, it's either not a file or it doesn't exist!\r\n" );
break;
}
vfs_getFileWebUrl( clientState->ctx, &vfsParserState.fileObj, strBuf1, 2000 );
printf( "the url of the file they're looking for is: %s\n", strBuf1 );
startFileDownload( strBuf1, 0, 0, 0, 0, &googleCon, &hInfo,
&googleParserState,
utils_shittyGetAccessTokenHeader( accessTokenState ) );
sendFtpResponse(clientState, "150 about to send file\r\n");
int encryptionStarted = 0;
while ( 1 ) {
received = updateFileDownload( &googleCon, &hInfo,
&googleParserState, encryptedDataBuffer,
ENCRYPTED_BUFFER_LEN, &dataLength, "" );
if ( !encryptionStarted ) {
startDecryption( &(decryptionState), "phone", NULL );
encryptionStarted = 1;
}
if ( received == 0 ) {
//finishDecryption(&decryptionState, tempBuffer, 0, tempBuffer2, &tempBuffer2OutputSize);
//send(clientState->data_fd, tempBuffer2, tempBuffer2OutputSize, 0);
break;
}
updateDecryption( &decryptionState, encryptedDataBuffer, dataLength,
decryptedDataBuffer, &tempOutputSize );
if( tempOutputSize>0 ){
send( clientState->data_fd, decryptedDataBuffer, tempOutputSize, 0 );
}
}
finishDecryption( &decryptionState, NULL, 0,
decryptedDataBuffer, &tempOutputSize );
if( tempOutputSize > 0 ){
send( clientState->data_fd, decryptedDataBuffer, tempOutputSize, 0 );
}
if ( close(clientState->data_fd2) != 0 ) {
perror( "close:" );
}
if ( close(clientState->data_fd) != 0 ) {
perror( "close:" );
}
sendFtpResponse(clientState, "226 Transfer complete.\r\n");
//FIXME: finishFileDownload();
break;
}
case REQUEST_RNFR:
{
//buffer the command
//check if the file exists
sprintf(clientState->fileNameChangeBuffer, "%s",
parserState->paramBuffer);
sendFtpResponse(clientState,
"350 RNFR accepted. Please supply new name for RNTO.\r\n");
break;
}
case REQUEST_RNTO:
{
//rename the actual file
//TODO: FIX THIS COMMAND TO SHOW THE FILENAMES
printf("mv %s %s\n", clientState->fileNameChangeBuffer,
parserState->paramBuffer);
vfs_mv(clientState->ctx, clientState->fileNameChangeBuffer, parserState->paramBuffer);
sendFtpResponse(clientState, "250 renamed\r\n");
break;
}
case REQUEST_DELE:
{
vfs_deleteObjectWithPath( clientState->ctx, parserState->paramBuffer );
sendFtpResponse( clientState,
"250 file was taken out back and shot.\r\n" );
break;
}
case REQUEST_CDUP:
{
vfsObject_t parent;
if( vfs_getDirParent( clientState->ctx, &clientState->ctx->cwd, &parent ) ) {
sendFtpResponse(clientState, "550 Failed to change directory.\r\n");
break;
}
clientState->ctx->cwd = parent;
sendFtpResponse( clientState, "250 Directory successfully changed.\r\n" );
break;
}
case REQUEST_RMD:
{
vfs_deleteObjectWithPath( clientState->ctx, parserState->paramBuffer );
sendFtpResponse( clientState, "250 directory killed.\r\n" ); //success
break;
}
default:
sendFtpResponse( clientState, "502 Command not implemented.\r\n" );
break;
}//switch
}
void handle_client(int client_fd, AccessTokenState_t *stateStruct) {
unsigned int j;
redisContext *c;
redisReply *reply;
const char *hostname = "127.0.0.1";
int port = 6379;
struct timeval timeout = { 1, 500000 }; // 1.5 seconds
c = redisConnectWithTimeout(hostname, port, timeout);
if (c == NULL || c->err) {
if (c) {
printf("REDIS Connection error: %s\n", c->errstr);
redisFree(c);
} else {
printf("REDIS Connection error: can't allocate redis context\n");
}
exit(1);
}
char buffer[MAX_PACKET_SIZE], pBuffer[MAX_PACKET_SIZE], usernameBuffer[100],
fileNameChangeBuffer[1000];
int sent, recieved;
ftpHTTPParserState_t parserState;
ftpClientState_t clientState;
ftp_newParserState(&parserState, pBuffer, MAX_PACKET_SIZE);
ftpClientState_init(&clientState, client_fd, usernameBuffer, 100,
fileNameChangeBuffer, 1000);
buildDatabaseIfRequired(clientState.ctx);
sent = send(client_fd, VALID_GREETING, strlen(VALID_GREETING), 0);
while (1) {
if ((recieved = recv(client_fd, buffer, MAX_PACKET_SIZE, 0)) == 0) {
break;
}
printf("buffer: %.*s--\n", recieved, buffer);
ftp_parsePacket(buffer, recieved, &parserState, &clientState);
ftp_handleFtpRequest(stateStruct, &parserState, &clientState);
if (parserState.type == REQUEST_QUIT) {
printf("got a quit\n");
break;
}
}
close(client_fd);
printf("connection closed\n");
}
int main(int argc, char *argv[]) {
AccessTokenState_t stateStruct;
gat_init_googleAccessToken(&stateStruct);
int sockfd = getListeningSocket(SERVER_LISTEN_PORT);
int client_fd;
socklen_t sin_size;
struct sockaddr_storage their_addr;
char s[INET6_ADDRSTRLEN];
printf("server: waiting for connections...\n");
while (1) { // main accept() loop
sin_size = sizeof their_addr;
client_fd = accept(sockfd, (struct sockaddr *) &their_addr, &sin_size);
if (client_fd == -1) {
perror("accept");
continue;
}
inet_ntop(their_addr.ss_family,
get_in_addr((struct sockaddr *) &their_addr), s, sizeof s);
printf("server: got connection from %s\n", s);
if (!fork()) { // this is the child process
close(sockfd); // child doesn't need the listener
handle_client(client_fd, &stateStruct);
close(client_fd);
//recv from client, now get a file from google
exit(0);
}
close(client_fd); // parent doesn't need this
}
return 0;
}