diff --git a/build/modules/connection.o b/build/modules/connection.o index 2e929eb..ce51d49 100644 Binary files a/build/modules/connection.o and b/build/modules/connection.o differ diff --git a/build/modules/fileManagement.o b/build/modules/fileManagement.o index 8bd8ca6..5971bab 100644 Binary files a/build/modules/fileManagement.o and b/build/modules/fileManagement.o differ diff --git a/build/modules/ftpCommandElaborate.o b/build/modules/ftpCommandElaborate.o index 0a9acd6..7fa09ca 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 9ad70bb..1160b8d 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 b76e10e..8b76a23 100644 Binary files a/build/modules/ftpServer.o and b/build/modules/ftpServer.o differ diff --git a/build/uFTP b/build/uFTP index 56af0a5..26cbc3c 100755 Binary files a/build/uFTP and b/build/uFTP differ diff --git a/ftpCommandElaborate.c b/ftpCommandElaborate.c index 900fccf..5bee2c8 100644 --- a/ftpCommandElaborate.c +++ b/ftpCommandElaborate.c @@ -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); diff --git a/ftpData.c b/ftpData.c index f4df1eb..f05c609 100644 --- a/ftpData.c +++ b/ftpData.c @@ -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); diff --git a/ftpServer.c b/ftpServer.c index d6ec03b..47530c0 100644 --- a/ftpServer.c +++ b/ftpServer.c @@ -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) diff --git a/library/connection.c b/library/connection.c index 3c4103d..14f45e0 100644 --- a/library/connection.c +++ b/library/connection.c @@ -22,7 +22,6 @@ * THE SOFTWARE. */ - #include #include #include @@ -36,12 +35,9 @@ #include #include - #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; diff --git a/library/fileManagement.c b/library/fileManagement.c index c9fb21a..093b66a 100644 --- a/library/fileManagement.c +++ b/library/fileManagement.c @@ -22,10 +22,6 @@ * THE SOFTWARE. */ - - - - #include #include #include @@ -37,6 +33,7 @@ #include #include #include +#include #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: can’t 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); +} diff --git a/library/fileManagement.h b/library/fileManagement.h index 9724dcb..c836c92 100644 --- a/library/fileManagement.h +++ b/library/fileManagement.h @@ -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 diff --git a/uftpd.cfg b/uftpd.cfg index 480467e..6046aeb 100644 --- a/uftpd.cfg +++ b/uftpd.cfg @@ -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