Xmass patch

This commit is contained in:
Ugo Cirmignani
2018-12-25 11:35:04 +01:00
parent e43eb85153
commit aa915b8522
13 changed files with 163 additions and 30 deletions

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

@ -378,8 +378,10 @@ int parseCommandProt(ftpDataType * data, int socketId)
int parseCommandCcc(ftpDataType * data, int socketId)
{
int returnCode;
#ifdef OPENSSL_ENABLED
int returnCode;
returnCode = socketPrintf(data, socketId, "s", "200 TLS connection aborted\r\n");
SSL_set_shutdown(data->clients[socketId].ssl, SSL_SENT_SHUTDOWN);
data->clients[socketId].tlsIsEnabled = 0;
@ -388,7 +390,7 @@ int parseCommandCcc(ftpDataType * data, int socketId)
return FTP_COMMAND_PROCESSED_WRITE_ERROR;
#endif
#ifdef OPENSSL_ENABLED
#ifndef OPENSSL_ENABLED
returnCode = socketPrintf(data, socketId, "s", "500 command not supported\r\n");
if (returnCode <= 0)
@ -532,7 +534,6 @@ int parseCommandAbor(ftpDataType * data, int socketId)
int returnCode;
if (data->clients[socketId].workerData.threadIsAlive == 1)
{
void *pReturn;
if (data->clients[socketId].workerData.threadIsAlive == 1)
{
pthread_cancel(data->clients[socketId].workerData.workerThread);

View File

@ -405,10 +405,7 @@ int searchInLoginFailsVector(void * loginFailsVector, void *element)
return i;
}
}
void cleanup_openssl()
{
EVP_cleanup();
}
return -1;
}
@ -584,12 +581,16 @@ void resetWorkerData(ftpDataType *data, int clientId, int isInitialization)
/* wait main for action */
if (isInitialization != 1)
{
printf("\n Operation is not an initialization! ");
pthread_mutex_destroy(&data->clients[clientId].workerData.conditionMutex);
pthread_cond_destroy(&data->clients[clientId].workerData.conditionVariable);
if (data->clients[clientId].workerData.theStorFile != NULL)
{
fclose(data->clients[clientId].workerData.theStorFile);
int returnCode = 0;
returnCode = fclose(data->clients[clientId].workerData.theStorFile);
data->clients[clientId].workerData.theStorFile = NULL;
}
@ -639,7 +640,6 @@ void resetClientData(ftpDataType *data, int clientId, int isInitialization)
if (isInitialization != 1)
{
void *pReturn;
if (data->clients[clientId].workerData.threadIsAlive == 1)
pthread_cancel(data->clients[clientId].workerData.workerThread);

View File

@ -108,8 +108,8 @@ void workerCleanup(void *socketId)
shutdown(ftpData.clients[theSocketId].workerData.socketConnection, SHUT_RDWR);
shutdown(ftpData.clients[theSocketId].workerData.passiveListeningSocket, SHUT_RDWR);
close(ftpData.clients[theSocketId].workerData.socketConnection);
close(ftpData.clients[theSocketId].workerData.passiveListeningSocket);
returnCode = close(ftpData.clients[theSocketId].workerData.socketConnection);
returnCode = close(ftpData.clients[theSocketId].workerData.passiveListeningSocket);
resetWorkerData(&ftpData, theSocketId, 0);
printf("\nWorker cleaned!");
}
@ -339,7 +339,9 @@ void *connectionWorkerHandle(void * socketId)
break;
}
}
fclose(ftpData.clients[theSocketId].workerData.theStorFile);
int theReturnCode;
theReturnCode = fclose(ftpData.clients[theSocketId].workerData.theStorFile);
ftpData.clients[theSocketId].workerData.theStorFile = NULL;
if (ftpData.clients[theSocketId].login.ownerShip.ownerShipSet == 1)

View File

@ -22,7 +22,6 @@
* THE SOFTWARE.
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
@ -36,12 +35,9 @@
#include <pthread.h>
#include <stdarg.h>
#include "../ftpData.h"
#include "connection.h"
int socketPrintf(ftpDataType * ftpData, int clientId, const char *__restrict __fmt, ...)
{
#define COMMAND_BUFFER 9600
@ -291,6 +287,10 @@ int createSocket(ftpDataType * ftpData)
//Socket creation
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1)
{
return -1;
}
temp.sin_family = AF_INET;
temp.sin_addr.s_addr = INADDR_ANY;
temp.sin_port = htons(ftpData->ftpParameters.port);
@ -311,9 +311,25 @@ int createSocket(ftpDataType * ftpData)
#endif
//Bind socket
errorCode = bind(sock,(struct sockaddr*) &temp,sizeof(temp));
if (errorCode == -1)
{
if (sock != -1)
{
close(sock);
}
return -1;
}
//Number of client allowed
errorCode = listen(sock, ftpData->ftpParameters.maxClients + 1);
if (errorCode == -1)
{
if (sock != -1)
{
close(sock);
}
return -1;
}
return sock;
}
@ -325,6 +341,11 @@ int createPassiveSocket(int port)
//Socket creation
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1)
{
return -1;
}
temp.sin_family = AF_INET;
temp.sin_addr.s_addr = INADDR_ANY;
temp.sin_port = htons(port);
@ -345,13 +366,28 @@ int createPassiveSocket(int port)
returnCode = bind(sock,(struct sockaddr*) &temp,sizeof(temp));
if (returnCode == -1)
return returnCode;
{
printf("\n Could not bind %d errno = %d", sock, errno);
if (sock != -1)
{
close(sock);
}
return returnCode;
}
//Number of client allowed
returnCode = listen(sock, 1);
if (returnCode == -1)
return returnCode;
{
printf("\n Could not listen %d errno = %d", sock, errno);
if (sock != -1)
{
close(sock);
}
return returnCode;
}
return sock;
}
@ -379,7 +415,7 @@ int createActiveSocket(int port, char *ipAddress)
}
else
{
printf("\n sockfd = %d \n", sockfd);
printf("\ncreateActiveSocket created socket = %d \n", sockfd);
}
@ -398,6 +434,12 @@ int createActiveSocket(int port, char *ipAddress)
if(connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\n3 Error : Connect Failed \n");
if (sockfd != -1)
{
close(sockfd);
}
return -1;
}
@ -437,9 +479,41 @@ void fdRemove(ftpDataType * ftpData, int index)
void closeSocket(ftpDataType * ftpData, int processingSocket)
{
int theReturnCode = 0;
#ifdef OPENSSL_ENABLED
/*
if (ftpData->clients[processingSocket].dataChannelIsTls == 1)
{
if(ftpData->clients[processingSocket].workerData.passiveModeOn == 1)
{
printf("\nSSL worker Shutdown 1");
returnCode = SSL_shutdown(ftpData->clients[processingSocket].ssl);
printf("\nnSSL worker Shutdown 1 return code : %d", returnCode);
if (returnCode < 0)
{
printf("SSL_shutdown failed return code %d", returnCode);
}
else if (returnCode == 0)
{
printf("\nSSL worker Shutdown 2");
returnCode = SSL_shutdown(ftpData->clients[processingSocket].ssl);
printf("\nnSSL worker Shutdown 2 return code : %d", returnCode);
if (returnCode <= 0)
{
printf("SSL_shutdown (2nd time) failed");
}
}
}
}*/
#endif
//Close the socket
shutdown(ftpData->clients[processingSocket].socketDescriptor, SHUT_RDWR);
close(ftpData->clients[processingSocket].socketDescriptor);
theReturnCode = close(ftpData->clients[processingSocket].socketDescriptor);
resetClientData(ftpData, processingSocket, 0);
resetWorkerData(ftpData, processingSocket, 0);
@ -462,9 +536,7 @@ void closeClient(ftpDataType * ftpData, int processingSocket)
if (ftpData->clients[processingSocket].workerData.threadIsAlive == 1)
{
void *pReturn;
pthread_cancel(ftpData->clients[processingSocket].workerData.workerThread);
//pthread_join(ftpData->clients[processingSocket].workerData.workerThread, &pReturn);
printf("\nQuit command received the Pasv Thread has been cancelled.");
}
@ -522,7 +594,6 @@ int selectWait(ftpDataType * ftpData)
struct timeval selectMaximumLockTime;
selectMaximumLockTime.tv_sec = 10;
selectMaximumLockTime.tv_usec = 0;
ftpData->connectionData.rset = ftpData->connectionData.rsetAll;
ftpData->connectionData.wset = ftpData->connectionData.wsetAll;
ftpData->connectionData.eset = ftpData->connectionData.esetAll;
@ -640,10 +711,11 @@ int evaluateClientSocketConnection(ftpDataType * ftpData)
struct sockaddr_in socketRefuse_sockaddr_in;
if ((socketRefuseFd = accept(ftpData->connectionData.theMainSocket, (struct sockaddr *)&socketRefuse_sockaddr_in, (socklen_t*)&socketRefuse_in_size))!=-1)
{
int theReturnCode = 0;
char *messageToWrite = "10068 Server reached the maximum number of connection, please try later.\r\n";
write(socketRefuseFd, messageToWrite, strlen(messageToWrite));
shutdown(socketRefuseFd, SHUT_RDWR);
close(socketRefuseFd);
theReturnCode = close(socketRefuseFd);
}
return 0;

View File

@ -22,10 +22,6 @@
* THE SOFTWARE.
*/
#include <pwd.h>
#include <grp.h>
#include <stdio.h>
@ -37,6 +33,7 @@
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/resource.h>
#include "fileManagement.h"
#include "dynamicVectors.h"
@ -58,6 +55,11 @@ static int FILE_CompareStringParameter(const void * a, const void * b)
return strcmp(typeA->Name, typeB->Name);
}
int FILE_fdIsValid(int fd)
{
return fcntl(fd, F_GETFD);
}
/* Check if inode is a directory */
int FILE_IsDirectory(char *DirectoryPath)
{
@ -718,3 +720,57 @@ gid_t FILE_getGID(const char *group_name)
return grp->gr_gid;
}
void FILE_checkAllOpenedFD(void)
{
int openedFd = 0, i,ret;
struct rlimit rl;
if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
printf("%s: cant get file limit", "");
if (rl.rlim_max == RLIM_INFINITY)
rl.rlim_max = 1024;
for (i = 0; i < rl.rlim_max; i++)
{
ret = FILE_fdIsValid(i);
//printf("\nret = %d", ret);
if (ret != -1)
{
struct stat statbuf;
fstat(i, &statbuf);
if (S_ISSOCK(statbuf.st_mode))
{
printf("\n fd %d is socket", i);
}
else if (S_ISDIR(statbuf.st_mode))
{
printf("\n fd %d is dir", i);
}
/*
else if (S_ISSOCK(statbuf.st_mode))
{
printf("\n fd %d is socket", fd);
}
else if (S_ISSOCK(statbuf.st_mode))
{
printf("\n fd %d is socket", fd);
}
else if (S_ISSOCK(statbuf.st_mode))
{
printf("\n fd %d is socket", fd);
}
else if (S_ISSOCK(statbuf.st_mode))
{
printf("\n fd %d is socket", fd);
}
*/
openedFd++;
}
}
printf("\n\nOpened fd : %d", openedFd);
}

View File

@ -71,5 +71,7 @@
int FILE_doChownFromUidGid(const char *file_path, uid_t uid, gid_t gid);
uid_t FILE_getUID(const char *user_name);
gid_t FILE_getGID(const char *group_name);
void FILE_checkAllOpenedFD(void);
int fd_is_valid(int fd);
#define GEN_FILE_MANAGEMENT_TYPES
#endif

View File

@ -10,10 +10,10 @@ MAXIMUM_ALLOWED_FTP_CONNECTION = 30
FTP_PORT = 21
#TCP/IP PORT SETTINGS (DEFAULT 21)
SINGLE_INSTANCE = true
SINGLE_INSTANCE = false
#Allow only one server instance (true or false)
DAEMON_MODE = true
DAEMON_MODE = false
#Run in background, daemon mode ok
IDLE_MAX_TIMEOUT = 3600