diff --git a/build/modules/connection.o b/build/modules/connection.o index 7066c7b..62c0fac 100644 Binary files a/build/modules/connection.o and b/build/modules/connection.o differ diff --git a/build/modules/ftpCommandElaborate.o b/build/modules/ftpCommandElaborate.o index f135fad..43f07d5 100644 Binary files a/build/modules/ftpCommandElaborate.o and b/build/modules/ftpCommandElaborate.o differ diff --git a/build/modules/ftpData.o b/build/modules/ftpData.o index 9794131..7e77937 100644 Binary files a/build/modules/ftpData.o and b/build/modules/ftpData.o differ diff --git a/build/modules/ftpServer.o b/build/modules/ftpServer.o index 1c71db4..46125a5 100644 Binary files a/build/modules/ftpServer.o and b/build/modules/ftpServer.o differ diff --git a/build/uFTP b/build/uFTP index f6864a0..e611b60 100755 Binary files a/build/uFTP and b/build/uFTP differ diff --git a/ftpCommandElaborate.c b/ftpCommandElaborate.c index b42d4fd..f49f7c6 100644 --- a/ftpCommandElaborate.c +++ b/ftpCommandElaborate.c @@ -1181,7 +1181,7 @@ int parseCommandCdup(ftpDataType * data, int socketId) return FTP_COMMAND_PROCESSED; } -long long int writeRetrFile(char * theFilename, int thePasvSocketConnection, long long int startFrom, FILE *retrFP) +long long int writeRetrFile(ftpDataType * data, int theSocketId, long long int startFrom, FILE *retrFP) { long long int readen = 0; long long int toReturn = 0, writtenSize = 0; @@ -1191,12 +1191,12 @@ long long int writeRetrFile(char * theFilename, int thePasvSocketConnection, lon #ifdef LARGE_FILE_SUPPORT_ENABLED //#warning LARGE FILE SUPPORT IS ENABLED! - retrFP = fopen64(theFilename, "rb"); + retrFP = fopen64(data->clients[theSocketId].fileToRetr.text, "rb"); #endif #ifndef LARGE_FILE_SUPPORT_ENABLED #warning LARGE FILE SUPPORT IS NOT ENABLED! - retrFP = fopen(theFilename, "rb"); + retrFP = fopen(data->clients[theSocketId].fileToRetr.text, "rb"); #endif if (retrFP == NULL) @@ -1208,7 +1208,6 @@ long long int writeRetrFile(char * theFilename, int thePasvSocketConnection, lon if (startFrom > 0) { - #ifdef LARGE_FILE_SUPPORT_ENABLED //#warning LARGE FILE SUPPORT IS ENABLED! currentPosition = (long long int) lseek64(fileno(retrFP), startFrom, SEEK_SET); @@ -1229,8 +1228,16 @@ long long int writeRetrFile(char * theFilename, int thePasvSocketConnection, lon while ((readen = (long long int) fread(buffer, sizeof(char), FTP_COMMAND_ELABORATE_CHAR_BUFFER, retrFP)) > 0) { - writtenSize = write(thePasvSocketConnection, buffer, readen); - + + if (data->clients[theSocketId].dataChannelIsTls != 1) + { + writtenSize = write(data->clients[theSocketId].workerData.socketConnection, buffer, readen); + } + else + { + writtenSize = SSL_write(data->clients[theSocketId].workerData.ssl, buffer, readen); + } + if (writtenSize <= 0) { fclose(retrFP); diff --git a/ftpCommandsElaborate.h b/ftpCommandsElaborate.h index 1bb997c..1268617 100644 --- a/ftpCommandsElaborate.h +++ b/ftpCommandsElaborate.h @@ -85,7 +85,7 @@ int parseCommandOpts(ftpDataType * data, int socketId); int parseCommandRnfr(ftpDataType * data, int socketId); int parseCommandRnto(ftpDataType * data, int socketId); -long long int writeRetrFile(char * theFilename, int thePasvSocketConnection, long long int startFrom, FILE *retrFP); +long long int writeRetrFile(ftpDataType * data, int theSocketId, long long int startFrom, FILE *retrFP); char *getFtpCommandArg(char * theCommand, char *theCommandString, int skipArgs); int getFtpCommandArgWithOptions(char * theCommand, char *theCommandString, ftpCommandDataType *ftpCommand); int setPermissions(char * permissionsCommand, char * basePath, ownerShip_DataType ownerShip); diff --git a/ftpData.c b/ftpData.c index a4d85d0..b8b7f6d 100644 --- a/ftpData.c +++ b/ftpData.c @@ -592,7 +592,6 @@ void resetWorkerData(ftpDataType *data, int clientId, int isInitialization) #ifdef OPENSSL_ENABLED SSL_free(data->clients[clientId].workerData.ssl); #endif - } else { @@ -623,11 +622,9 @@ void resetWorkerData(ftpDataType *data, int clientId, int isInitialization) data->clients[clientId].workerData.directoryInfo.Destroy(&data->clients[clientId].workerData.directoryInfo, deleteListDataInfoVector); free(lastToDestroy); } - #ifdef OPENSSL_ENABLED - data->clients[clientId].ssl = SSL_new(data->ctx); + data->clients[clientId].workerData.ssl = SSL_new(data->ctx); #endif - } void resetClientData(ftpDataType *data, int clientId, int isInitialization) @@ -651,6 +648,7 @@ void resetClientData(ftpDataType *data, int clientId, int isInitialization) #ifdef OPENSSL_ENABLED SSL_free(data->clients[clientId].ssl); + //SSL_free(data->clients[clientId].workerData.ssl); #endif } @@ -699,7 +697,8 @@ void resetClientData(ftpDataType *data, int clientId, int isInitialization) data->clients[clientId].lastActivityTimeStamp = 0; #ifdef OPENSSL_ENABLED - data->clients[clientId].workerData.ssl = SSL_new(data->ctx); + //data->clients[clientId].workerData.ssl = SSL_new(data->ctx); + data->clients[clientId].ssl = SSL_new(data->ctx); #endif } diff --git a/ftpServer.c b/ftpServer.c index a5277b2..2ecc256 100644 --- a/ftpServer.c +++ b/ftpServer.c @@ -53,14 +53,30 @@ static int processCommand(int processingElement); void workerCleanup(void *socketId) { - int theSocketId = *(int *)socketId; - //printf("\nClosing pasv socket (%d) ok!", theSocketId); + int theSocketId = *(int *)socketId; + int returnCode = 0; + #ifdef OPENSSL_ENABLED - if (ftpData.clients[theSocketId].dataChannelIsTls == 1) - { - printf("\nSSL worker Shutdown!"); - SSL_shutdown(ftpData.clients[theSocketId].workerData.ssl); - } + if (ftpData.clients[theSocketId].dataChannelIsTls == 1) + { + printf("\nSSL worker Shutdown 1"); + returnCode = SSL_shutdown(ftpData.clients[theSocketId].workerData.ssl); + printf(" return code : %d", returnCode); + + if (returnCode < 0) + { + printf("SSL_shutdown failed return code %d", returnCode); + } + else if (returnCode == 0) + { + returnCode = SSL_shutdown(ftpData.clients[theSocketId].workerData.ssl); + + if (returnCode <= 0) + { + printf("SSL_shutdown (2nd time) failed"); + } + } + } #endif shutdown(ftpData.clients[theSocketId].workerData.socketConnection, SHUT_RDWR); @@ -190,7 +206,6 @@ void *connectionWorkerHandle(void * socketId) ftpData.clients[theSocketId].workerData.theStorFile = fopen(ftpData.clients[theSocketId].fileToStor.text, "wb"); #endif - if (ftpData.clients[theSocketId].workerData.theStorFile == NULL) { returnCode = socketPrintf(&ftpData, theSocketId, "s", "553 Unable to write the file\r\n"); @@ -216,8 +231,19 @@ void *connectionWorkerHandle(void * socketId) while(1) { - ftpData.clients[theSocketId].workerData.bufferIndex = read(ftpData.clients[theSocketId].workerData.socketConnection, ftpData.clients[theSocketId].workerData.buffer, CLIENT_BUFFER_STRING_SIZE); - + if (ftpData.clients[theSocketId].dataChannelIsTls != 1) + { + ftpData.clients[theSocketId].workerData.bufferIndex = read(ftpData.clients[theSocketId].workerData.socketConnection, ftpData.clients[theSocketId].workerData.buffer, CLIENT_BUFFER_STRING_SIZE); + } + else if (ftpData.clients[theSocketId].dataChannelIsTls == 1) + { + ftpData.clients[theSocketId].workerData.bufferIndex = SSL_read(ftpData.clients[theSocketId].workerData.ssl, ftpData.clients[theSocketId].workerData.buffer, CLIENT_BUFFER_STRING_SIZE); + } + else + { + printf("\nError state"); + } + if (ftpData.clients[theSocketId].workerData.bufferIndex == 0) { break; @@ -234,8 +260,7 @@ void *connectionWorkerHandle(void * socketId) } fclose(ftpData.clients[theSocketId].workerData.theStorFile); ftpData.clients[theSocketId].workerData.theStorFile = NULL; - - + if (ftpData.clients[theSocketId].login.ownerShip.ownerShipSet == 1) { FILE_doChownFromUidGid(ftpData.clients[theSocketId].fileToStor.text, ftpData.clients[theSocketId].login.ownerShip.uid, ftpData.clients[theSocketId].login.ownerShip.gid); @@ -303,7 +328,7 @@ void *connectionWorkerHandle(void * socketId) } - writenSize = writeRetrFile(ftpData.clients[theSocketId].fileToRetr.text, ftpData.clients[theSocketId].workerData.socketConnection, ftpData.clients[theSocketId].workerData.retrRestartAtByte, ftpData.clients[theSocketId].workerData.theStorFile); + writenSize = writeRetrFile(&ftpData, theSocketId, ftpData.clients[theSocketId].workerData.retrRestartAtByte, ftpData.clients[theSocketId].workerData.theStorFile); ftpData.clients[theSocketId].workerData.retrRestartAtByte = 0; if (writenSize == -1) diff --git a/library/connection.c b/library/connection.c index 81a3182..08212b3 100644 --- a/library/connection.c +++ b/library/connection.c @@ -50,7 +50,7 @@ int socketPrintf(ftpDataType * ftpData, int clientId, const char *__restrict __f char theBuffer[2048]; int theStringSize = 0; memset(&theBuffer, 0, SOCKET_PRINTF_BUFFER); - printf("\nWriting to socket id %d: ", clientId); + printf("\nWriting to socket id %d, TLS %d: ", clientId, ftpData->clients[clientId].tlsIsEnabled); va_list args; va_start(args, __fmt); @@ -165,8 +165,7 @@ int socketWorkerPrintf(ftpDataType * ftpData, int clientId, const char *__restri char theBuffer[2048]; int theStringSize = 0; memset(&theBuffer, 0, SOCKET_PRINTF_BUFFER); - printf("\nWriting to worker socket id %d: ", clientId); - + printf("\nWriting to worker socket id %dd, TLS %d: ", clientId, ftpData->clients[clientId].dataChannelIsTls); va_list args; va_start(args, __fmt); while (*__fmt != '\0')