Fixed security issues, upgrade! Fixed many instability issues on slow connections

This commit is contained in:
Ugo Cirmignani
2019-04-06 22:42:51 +02:00
parent 2a7ca60243
commit ace9448ad2
22 changed files with 106 additions and 47 deletions

View File

@ -5,10 +5,10 @@ OUTPATH=./build/
SOURCE_MODULES_PATH=./library/ SOURCE_MODULES_PATH=./library/
#FOR DEBUG PURPOSE #FOR DEBUG PURPOSE
CFLAGSTEMP=-c -Wall -I. -g -O0 #CFLAGSTEMP=-c -Wall -I. -g -O0
#FOR RELEASE #FOR RELEASE
#CFLAGSTEMP=-c -Wall -I. CFLAGSTEMP=-c -Wall -I.
OPTIMIZATION=-O3 OPTIMIZATION=-O3
HEADERS=-I HEADERS=-I

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -495,19 +495,25 @@ int parseCommandPasv(ftpDataType * data, int socketId)
//printf("\n data->clients[%d].workerData.threadHasBeenCreated = %d", socketId, data->clients[socketId].workerData.threadHasBeenCreated); //printf("\n data->clients[%d].workerData.threadHasBeenCreated = %d", socketId, data->clients[socketId].workerData.threadHasBeenCreated);
if (data->clients[socketId].workerData.threadIsAlive == 1) if (data->clients[socketId].workerData.threadIsAlive == 1)
{ {
returnCode = pthread_cancel(data->clients[socketId].workerData.workerThread); printf("\nPASV PTHREAD CANCEL");
//printf("\npasv pthread_cancel = %d", returnCode); int returnCode = pthread_cancel(data->clients[socketId].workerData.workerThread);
do printf ("\npthread_cancel return code: %d", returnCode);
{
printf("\nQuit command received the Pasv Thread has been cancelled!!!"); returnCode = pthread_join(data->clients[socketId].workerData.workerThread, &pReturn);
usleep(10000); //fflush(0);
} while (data->clients[socketId].workerData.threadIsAlive == 1); data->clients[socketId].workerData.threadHasBeenCreated = 0;
printf("\nparseCommand PASV JOIN RETURN STATUS %d", returnCode);
printf("\nPASV THREAD CANCELLED!!!");
printf("\nftpData->clients[processingSocket].workerData.threadIsAlive = %d", data->clients[socketId].workerData.threadIsAlive);
} }
if (data->clients[socketId].workerData.threadHasBeenCreated == 1) if (data->clients[socketId].workerData.threadHasBeenCreated == 1)
{ {
returnCode = pthread_join(data->clients[socketId].workerData.workerThread, &pReturn); returnCode = pthread_join(data->clients[socketId].workerData.workerThread, &pReturn);
//printf("\nPasv join ok %d", returnCode); printf("\nPASV JOIN RETURN STATUS %d", returnCode);
} }
data->clients[socketId].workerData.passiveModeOn = 1; data->clients[socketId].workerData.passiveModeOn = 1;
@ -537,16 +543,23 @@ int parseCommandPort(ftpDataType * data, int socketId)
void *pReturn; void *pReturn;
if (data->clients[socketId].workerData.threadIsAlive == 1) if (data->clients[socketId].workerData.threadIsAlive == 1)
{ {
returnCode = pthread_cancel(data->clients[socketId].workerData.workerThread); printf("\nPORT PTHREAD CANCEL");
do int returnCode = pthread_cancel(data->clients[socketId].workerData.workerThread);
{ printf ("\npthread_cancel return code: %d", returnCode);
printf("\nQuit command received the Pasv Thread has been cancelled!!!"); //fflush(0);
usleep(10000);
} while (data->clients[socketId].workerData.threadIsAlive == 1); returnCode = pthread_join(data->clients[socketId].workerData.workerThread, &pReturn);
data->clients[socketId].workerData.threadHasBeenCreated = 0;
printf("\nparseCommand PORT JOIN RETURN STATUS %d", returnCode);
printf("\nPORT Thread cancelled!!!");
printf("\nftpData->clients[processingSocket].workerData.threadIsAlive = %d", data->clients[socketId].workerData.threadIsAlive);
} }
if (data->clients[socketId].workerData.threadHasBeenCreated == 1) if (data->clients[socketId].workerData.threadHasBeenCreated == 1)
{ {
returnCode = pthread_join(data->clients[socketId].workerData.workerThread, &pReturn); returnCode = pthread_join(data->clients[socketId].workerData.workerThread, &pReturn);
printf("\nPORT JOIN RETURN STATUS %d", returnCode);
} }
data->clients[socketId].workerData.passiveModeOn = 0; data->clients[socketId].workerData.passiveModeOn = 0;
data->clients[socketId].workerData.activeModeOn = 1; data->clients[socketId].workerData.activeModeOn = 1;
@ -575,12 +588,20 @@ int parseCommandAbor(ftpDataType * data, int socketId)
{ {
if (data->clients[socketId].workerData.threadIsAlive == 1) if (data->clients[socketId].workerData.threadIsAlive == 1)
{ {
pthread_cancel(data->clients[socketId].workerData.workerThread); void *pReturn;
do
{ printf("\nABOR PTHREAD CANCEL");
printf("\nQuit command received the Pasv Thread has been cancelled!!!"); int returnCode = pthread_cancel(data->clients[socketId].workerData.workerThread);
usleep(10000); printf ("\npthread_cancel return code: %d", returnCode);
} while (data->clients[socketId].workerData.threadIsAlive == 1); //fflush(0);
returnCode = pthread_join(data->clients[socketId].workerData.workerThread, &pReturn);
data->clients[socketId].workerData.threadHasBeenCreated = 0;
printf("\nparseCommand ABOR JOIN RETURN STATUS %d", returnCode);
printf("\nAbor thread cancelled!!!");
printf("\nftpData->clients[processingSocket].workerData.threadIsAlive = %d", data->clients[socketId].workerData.threadIsAlive);
} }
returnCode = socketPrintf(data, socketId, "s", "426 ABORT\r\n"); returnCode = socketPrintf(data, socketId, "s", "426 ABORT\r\n");

View File

@ -600,8 +600,21 @@ void resetWorkerData(ftpDataType *data, int clientId, int isInitialization)
} }
#ifdef OPENSSL_ENABLED #ifdef OPENSSL_ENABLED
SSL_free(data->clients[clientId].workerData.serverSsl);
SSL_free(data->clients[clientId].workerData.clientSsl); if (data->clients[clientId].workerData.serverSsl != NULL)
{
SSL_free(data->clients[clientId].workerData.serverSsl);
data->clients[clientId].workerData.serverSsl = NULL;
}
if (data->clients[clientId].workerData.clientSsl != NULL)
{
SSL_free(data->clients[clientId].workerData.clientSsl);
data->clients[clientId].workerData.clientSsl = NULL;
}
#endif #endif
} }
else else
@ -633,13 +646,22 @@ void resetClientData(ftpDataType *data, int clientId, int isInitialization)
if (isInitialization != 1) if (isInitialization != 1)
{ {
if (data->clients[clientId].workerData.threadIsAlive == 1) { if (data->clients[clientId].workerData.threadIsAlive == 1) {
pthread_cancel(data->clients[clientId].workerData.workerThread); void *pReturn;
do
{
printf("\nQuit command received the Pasv Thread has been cancelled!!!");
usleep(10000);
} while (data->clients[clientId].workerData.threadIsAlive == 1);
printf("\nRESET CLIENT PTHREAD CANCEL");
int returnCode = pthread_cancel(data->clients[clientId].workerData.workerThread);
printf ("\npthread_cancel return code: %d", returnCode);
//fflush(0);
returnCode = pthread_join(data->clients[clientId].workerData.workerThread, &pReturn);
data->clients[clientId].workerData.threadHasBeenCreated = 0;
printf("\nReset client data JOIN RETURN STATUS %d", returnCode);
printf("\nReset client data thread cancelled!!!");
printf("\nftpData->clients[processingSocket].workerData.threadIsAlive = %d", data->clients[clientId].workerData.threadIsAlive);
} }
pthread_mutex_destroy(&data->clients[clientId].conditionMutex); pthread_mutex_destroy(&data->clients[clientId].conditionMutex);
pthread_cond_destroy(&data->clients[clientId].conditionVariable); pthread_cond_destroy(&data->clients[clientId].conditionVariable);
@ -647,8 +669,11 @@ void resetClientData(ftpDataType *data, int clientId, int isInitialization)
pthread_mutex_destroy(&data->clients[clientId].writeMutex); pthread_mutex_destroy(&data->clients[clientId].writeMutex);
#ifdef OPENSSL_ENABLED #ifdef OPENSSL_ENABLED
SSL_free(data->clients[clientId].ssl); if (data->clients[clientId].ssl != NULL)
//SSL_free(data->clients[clientId].workerData.ssl); {
SSL_free(data->clients[clientId].ssl);
data->clients[clientId].ssl = NULL;
}
#endif #endif
} }
else else

View File

@ -58,9 +58,15 @@ void workerCleanup(void *socketId)
int theSocketId = *(int *)socketId; int theSocketId = *(int *)socketId;
int returnCode = 0; int returnCode = 0;
//printf("\nWorker %d cleanup", theSocketId); //printf("\nWorker %d cleanup", theSocketId);
#ifdef OPENSSL_ENABLED #ifdef OPENSSL_ENABLED
int error;
error = fcntl(ftpData.clients[theSocketId].workerData.socketConnection, F_SETFL, O_NONBLOCK);
if (ftpData.clients[theSocketId].dataChannelIsTls == 1) if (ftpData.clients[theSocketId].dataChannelIsTls == 1)
{ {
if(ftpData.clients[theSocketId].workerData.passiveModeOn == 1) if(ftpData.clients[theSocketId].workerData.passiveModeOn == 1)
@ -258,15 +264,20 @@ void *connectionWorkerHandle(void * socketId)
//Endless loop ftp process //Endless loop ftp process
while (1) while (1)
{ {
usleep(1000);
if (ftpData.clients[theSocketId].workerData.socketIsConnected > 0) if (ftpData.clients[theSocketId].workerData.socketIsConnected > 0)
{ {
//printf("\nWorker %d is waiting for commands!", theSocketId); printf("\nWorker %d is waiting for commands!", theSocketId);
//Conditional lock on tconditionVariablehread actions //Conditional lock on tconditionVariablehread actions
pthread_mutex_lock(&ftpData.clients[theSocketId].conditionMutex); pthread_mutex_lock(&ftpData.clients[theSocketId].conditionMutex);
//int sleepTime = 1000;
while (ftpData.clients[theSocketId].workerData.commandReceived == 0) while (ftpData.clients[theSocketId].workerData.commandReceived == 0)
{ {
//usleep(sleepTime);
//if (sleepTime < 200000)
//{
//sleepTime+= 1000;
//}
pthread_cond_wait(&ftpData.clients[theSocketId].conditionVariable, &ftpData.clients[theSocketId].conditionMutex); pthread_cond_wait(&ftpData.clients[theSocketId].conditionVariable, &ftpData.clients[theSocketId].conditionMutex);
} }
pthread_mutex_unlock(&ftpData.clients[theSocketId].conditionMutex); pthread_mutex_unlock(&ftpData.clients[theSocketId].conditionMutex);

View File

@ -33,7 +33,7 @@ int authenticateSystem(const char *username, const char *password)
pam_handle_t *local_auth_handle = NULL; // this gets set by pam_start pam_handle_t *local_auth_handle = NULL; // this gets set by pam_start
int retval; int retval;
retval = pam_start("su", username, &local_conversation, &local_auth_handle); retval = pam_start("sudo", username, &local_conversation, &local_auth_handle);
if (retval != PAM_SUCCESS) if (retval != PAM_SUCCESS)
{ {
@ -118,10 +118,7 @@ void loginCheck(char *name, char *password, loginDataType *login, DYNMEM_MemoryT
// printf("\nlogin->absolutePath.text: %s", login->absolutePath.text); // printf("\nlogin->absolutePath.text: %s", login->absolutePath.text);
} }
} }
else
{
cleanLoginData(login, 0, &*memoryTable);
}
} }

View File

@ -127,7 +127,7 @@ int socketPrintf(ftpDataType * ftpData, int clientId, const char *__restrict __f
va_end(args); va_end(args);
if (ftpData->clients[clientId].socketIsConnected != 1 || if (ftpData->clients[clientId].socketIsConnected != 1 ||
ftpData->clients[clientId].socketDescriptor == 0) ftpData->clients[clientId].socketDescriptor < 0)
{ {
printf("\n Client is not connected!"); printf("\n Client is not connected!");
return -1; return -1;
@ -459,7 +459,6 @@ int createActiveSocket(int port, char *ipAddress)
struct sockaddr_in serv_addr; struct sockaddr_in serv_addr;
//printf("\n Connection socket is going to start ip: %s:%d \n", ipAddress, port); //printf("\n Connection socket is going to start ip: %s:%d \n", ipAddress, port);
//sleep(100);
memset(&serv_addr, 0, sizeof(struct sockaddr_in)); memset(&serv_addr, 0, sizeof(struct sockaddr_in));
serv_addr.sin_family = AF_INET; serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port); serv_addr.sin_port = htons(port);
@ -596,14 +595,18 @@ void closeClient(ftpDataType * ftpData, int processingSocket)
if (ftpData->clients[processingSocket].workerData.threadIsAlive == 1) if (ftpData->clients[processingSocket].workerData.threadIsAlive == 1)
{ {
printf("\nCLOSE THE CLIENT PTHREAD CANCEL");
void *pReturn;
int returnCode = pthread_cancel(ftpData->clients[processingSocket].workerData.workerThread);
printf ("\npthread_cancel return code: %d", returnCode);
//fflush(0);
pthread_cancel(ftpData->clients[processingSocket].workerData.workerThread); returnCode = pthread_join(ftpData->clients[processingSocket].workerData.workerThread, &pReturn);
ftpData->clients[processingSocket].workerData.threadHasBeenCreated = 0;
printf("\nCLOSE THE CLIENT JOIN RETURN STATUS %d", returnCode);
do printf("\nClose client thread cancelled!!!");
{ printf("\nftpData->clients[processingSocket].workerData.threadIsAlive = %d", ftpData->clients[processingSocket].workerData.threadIsAlive);
printf("\nQuit command received the Pasv Thread has been cancelled!!!");
usleep(10000);
} while (ftpData->clients[processingSocket].workerData.threadIsAlive == 1);
} }

View File

@ -10,6 +10,8 @@ sudo apt-get install libpam0g-dev
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365
openssl rsa -in key.pem -out newkey.pem openssl rsa -in key.pem -out newkey.pem
handle SIGPIPE nostop noprint pass handle SIGPIPE nostop noprint pass
thread apply all where
#Testing ssl #Testing ssl