TLS almost ok

This commit is contained in:
Ugo Cirmignani
2018-12-04 21:50:28 +01:00
parent 26157b8c44
commit fb7917a711
10 changed files with 58 additions and 28 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -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);

View File

@ -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);

View File

@ -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
}

View File

@ -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)

View File

@ -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')