diff --git a/Makefile b/Makefile index 499e403..85663b8 100644 --- a/Makefile +++ b/Makefile @@ -5,10 +5,10 @@ OUTPATH=./build/ SOURCE_MODULES_PATH=./library/ #FOR DEBUG PURPOSE -#CFLAGSTEMP=-c -Wall -I. -g -O0 +CFLAGSTEMP=-c -Wall -I. -g -O0 #FOR RELEASE -CFLAGSTEMP=-c -Wall -I. +#CFLAGSTEMP=-c -Wall -I. OPTIMIZATION=-O3 HEADERS=-I @@ -22,17 +22,14 @@ ENABLE_LARGE_FILE_SUPPORT=-D LARGE_FILE_SUPPORT_ENABLED -D _LARGEFILE64_SOURCE ENABLE_OPENSSL_SUPPORT= #TO ENABLE OPENSSL SUPPORT UNCOMMENT NEXT 2 LINES -#ENABLE_OPENSSL_SUPPORT=-D OPENSSL_ENABLED -#LIBS=-lpthread -lssl -lcrypto +ENABLE_OPENSSL_SUPPORT=-D OPENSSL_ENABLED +LIBS=-lpthread -lssl -lcrypto ENABLE_PAM_SUPPORT= PAM_AUTH_LIB= #TO ENABLE PAM AUTH UNCOMMENT NEXT TWO LINES -#ENABLE_PAM_SUPPORT= -D PAM_SUPPORT_ENABLED -#PAM_AUTH_LIB= -lpam - -#USER PAM AUTH -#-lpam +ENABLE_PAM_SUPPORT= -D PAM_SUPPORT_ENABLED +PAM_AUTH_LIB= -lpam CFLAGS=$(CFLAGSTEMP) $(ENABLE_LARGE_FILE_SUPPORT) $(ENABLE_OPENSSL_SUPPORT) $(ENABLE_PAM_SUPPORT) diff --git a/build/modules/configRead.o b/build/modules/configRead.o index 50c7402..2596735 100644 Binary files a/build/modules/configRead.o and b/build/modules/configRead.o differ diff --git a/build/modules/connection.o b/build/modules/connection.o index bf62d66..9e17366 100644 Binary files a/build/modules/connection.o and b/build/modules/connection.o differ diff --git a/build/modules/daemon.o b/build/modules/daemon.o index 1e60449..5336d71 100644 Binary files a/build/modules/daemon.o and b/build/modules/daemon.o differ diff --git a/build/modules/dynamicMemory.o b/build/modules/dynamicMemory.o index 0ea6505..52c7130 100644 Binary files a/build/modules/dynamicMemory.o and b/build/modules/dynamicMemory.o differ diff --git a/build/modules/dynamicVectors.o b/build/modules/dynamicVectors.o index 5a967df..6f7159d 100644 Binary files a/build/modules/dynamicVectors.o and b/build/modules/dynamicVectors.o differ diff --git a/build/modules/errorHandling.o b/build/modules/errorHandling.o index 46c51ee..965b9e4 100644 Binary files a/build/modules/errorHandling.o and b/build/modules/errorHandling.o differ diff --git a/build/modules/fileManagement.o b/build/modules/fileManagement.o index b4e78b4..c358c13 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 5dab624..16f0b1a 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 3d4de26..0f64fa7 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 0ebfedd..7c539cb 100644 Binary files a/build/modules/ftpServer.o and b/build/modules/ftpServer.o differ diff --git a/build/modules/logFunctions.o b/build/modules/logFunctions.o index ca9f615..28f97b6 100644 Binary files a/build/modules/logFunctions.o and b/build/modules/logFunctions.o differ diff --git a/build/modules/openSsl.o b/build/modules/openSsl.o index 2305cb7..b597125 100644 Binary files a/build/modules/openSsl.o and b/build/modules/openSsl.o differ diff --git a/build/modules/signals.o b/build/modules/signals.o index 66a36f9..a5f5a54 100644 Binary files a/build/modules/signals.o and b/build/modules/signals.o differ diff --git a/build/uFTP b/build/uFTP index 0eb1087..7fb44da 100755 Binary files a/build/uFTP and b/build/uFTP differ diff --git a/ftpCommandElaborate.c b/ftpCommandElaborate.c index 23836ce..d7917c8 100644 --- a/ftpCommandElaborate.c +++ b/ftpCommandElaborate.c @@ -1025,6 +1025,61 @@ int parseCommandDele(ftpDataType * data, int socketId) return functionReturnCode; } +int parseCommandMdtm(ftpDataType * data, int socketId) +{ + int functionReturnCode = 0; + int returnCode; + int isSafePath; + char *theFileToGetModificationDate; + + dynamicStringDataType mdtmFileName; + char theResponse[LIST_DATA_TYPE_MODIFIED_DATA_STR_SIZE]; + memset(theResponse, 0, LIST_DATA_TYPE_MODIFIED_DATA_STR_SIZE); + + theFileToGetModificationDate = getFtpCommandArg("MDTM", data->clients[socketId].theCommandReceived, 0); + + cleanDynamicStringDataType(&mdtmFileName, 1, &data->clients[socketId].memoryTable); + isSafePath = getSafePath(&mdtmFileName, theFileToGetModificationDate, &data->clients[socketId].login, &data->clients[socketId].memoryTable); + + + printf("\ntheFileToGetModificationDate = %s", theFileToGetModificationDate); + printf("\nmdtmFileName.text = %s", mdtmFileName.text); + printf("\nisSafePath = %d", isSafePath); + printf("\n Safe ok is file: %d, is directory: %d", FILE_IsFile(mdtmFileName.text), FILE_IsDirectory(mdtmFileName.text)); + if (isSafePath == 1) + { + printf("\n Safe ok is file: %d, is directory: %d", FILE_IsFile(mdtmFileName.text), FILE_IsDirectory(mdtmFileName.text)); + + if (FILE_IsFile(mdtmFileName.text) == 1 || + FILE_IsDirectory(mdtmFileName.text) == 1) + { + time_t theTime = FILE_GetLastModifiedData(mdtmFileName.text); + strftime(theResponse, LIST_DATA_TYPE_MODIFIED_DATA_STR_SIZE, "213 %Y%m%d%H%M%S\r\n", localtime(&theTime)); + returnCode = socketPrintf(data, socketId, "s", theResponse); + functionReturnCode = FTP_COMMAND_PROCESSED; + + if (returnCode <= 0) + functionReturnCode = FTP_COMMAND_PROCESSED_WRITE_ERROR; + + } + else + { + returnCode = socketPrintf(data, socketId, "s", "550 Can't check for file existence\r\n"); + functionReturnCode = FTP_COMMAND_PROCESSED; + + if (returnCode <= 0) + functionReturnCode = FTP_COMMAND_PROCESSED_WRITE_ERROR; + } + } + else + { + functionReturnCode = FTP_COMMAND_NOT_RECONIZED; + } + + cleanDynamicStringDataType(&mdtmFileName, 0, &data->clients[socketId].memoryTable); + return functionReturnCode; +} + int parseCommandNoop(ftpDataType * data, int socketId) { int returnCode; @@ -1343,6 +1398,8 @@ long long int writeRetrFile(ftpDataType * data, int theSocketId, long long int s if (writtenSize <= 0) { + + printf("\nError %d while writing retr file."); fclose(retrFP); retrFP = NULL; return -1; diff --git a/ftpCommandsElaborate.h b/ftpCommandsElaborate.h index 04247fd..ec2a4b6 100644 --- a/ftpCommandsElaborate.h +++ b/ftpCommandsElaborate.h @@ -78,6 +78,7 @@ int parseCommandCwd(ftpDataType * data, int socketId); int parseCommandRest(ftpDataType * data, int socketId); int parseCommandCdup(ftpDataType * data, int socketId); int parseCommandDele(ftpDataType * data, int socketId); +int parseCommandMdtm(ftpDataType * data, int socketId); int parseCommandOpts(ftpDataType * data, int socketId); int parseCommandRnfr(ftpDataType * data, int socketId); int parseCommandRnto(ftpDataType * data, int socketId); diff --git a/ftpData.c b/ftpData.c index 4c1e02b..7f579dd 100644 --- a/ftpData.c +++ b/ftpData.c @@ -94,7 +94,6 @@ void setDynamicStringDataType(dynamicStringDataType *dynamicString, char *theStr } - int getSafePath(dynamicStringDataType *safePath, char *theDirectoryName, loginDataType *loginData, DYNMEM_MemoryTable_DataType **memoryTable) { #define STRING_SIZE 4096 diff --git a/ftpServer.c b/ftpServer.c index 47ce49d..aa6d201 100644 --- a/ftpServer.c +++ b/ftpServer.c @@ -263,7 +263,7 @@ void *connectionWorkerHandle(void * socketId) if (ftpData.clients[theSocketId].workerData.socketIsConnected > 0) { //printf("\nWorker %d is waiting for commands!", theSocketId); - //Conditional lock on thread actions + //Conditional lock on tconditionVariablehread actions pthread_mutex_lock(&ftpData.clients[theSocketId].workerData.conditionMutex); while (ftpData.clients[theSocketId].workerData.commandReceived == 0) { @@ -461,7 +461,7 @@ void *connectionWorkerHandle(void * socketId) writenSize = writeRetrFile(&ftpData, theSocketId, ftpData.clients[theSocketId].workerData.retrRestartAtByte, ftpData.clients[theSocketId].workerData.theStorFile); ftpData.clients[theSocketId].workerData.retrRestartAtByte = 0; - if (writenSize == -1) + if (writenSize <= -1) { writeReturn = socketPrintf(&ftpData, theSocketId, "s", "550 unable to open the file for reading\r\n"); @@ -666,7 +666,7 @@ void runFtpServer(void) if (ftpData.clients[processingSock].buffer[i] == '\n') { ftpData.clients[processingSock].socketCommandReceived = 1; - //printf("\n Processing the command: %s", ftpData.clients[processingSock].theCommandReceived); + printf("\n Processing the command: %s", ftpData.clients[processingSock].theCommandReceived); commandProcessStatus = processCommand(processingSock); //Echo unrecognized commands if (commandProcessStatus == FTP_COMMAND_NOT_RECONIZED) @@ -865,7 +865,7 @@ static int processCommand(int processingElement) } else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "DELE", strlen("DELE")) == 1) { - //printf("\nDELE command received"); + //printf("\nDELE comman200 OKd received"); toReturn = parseCommandDele(&ftpData, processingElement); } else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "OPTS", strlen("OPTS")) == 1) @@ -873,10 +873,10 @@ static int processCommand(int processingElement) //printf("\nOPTS command received"); toReturn = parseCommandOpts(&ftpData, processingElement); } - else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "MTDM", strlen("MTDM")) == 1) + else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "MDTM", strlen("MDTM")) == 1) { - //printf("\nMTDM command received"); - //To implement + printf("\nMTDM command received"); + toReturn = parseCommandMdtm(&ftpData, processingElement); } else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "NLST", strlen("NLST")) == 1) { diff --git a/library/connection.c b/library/connection.c index 2e794a5..352c8e9 100644 --- a/library/connection.c +++ b/library/connection.c @@ -40,8 +40,6 @@ #include "../ftpData.h" #include "connection.h" - - int socketPrintf(ftpDataType * ftpData, int clientId, const char *__restrict __fmt, ...) { #define COMMAND_BUFFER 9600 @@ -54,7 +52,7 @@ int socketPrintf(ftpDataType * ftpData, int clientId, const char *__restrict __f memset(&commandBuffer, 0, COMMAND_BUFFER); //printf("\nWriting to socket id %d, TLS %d: ", clientId, ftpData->clients[clientId].tlsIsEnabled); - pthread_mutex_lock(&ftpData->clients[clientId].writeMutex); + //pthread_mutex_lock(&ftpData->clients[clientId].writeMutex); va_list args; va_start(args, __fmt); @@ -76,6 +74,72 @@ int socketPrintf(ftpDataType * ftpData, int clientId, const char *__restrict __f case 'c': case 'C': { + int i = 0; + theStringSize = 0; + switch(*__fmt) + { + case 'd': + case 'D': + { + int theInteger = va_arg(args, int); + memset(&theBuffer, 0, SOCKET_PRINTF_BUFFER); + theStringSize = snprintf(theBuffer, SOCKET_PRINTF_BUFFER, "%d", theInteger); + } + break; + + case 'c': + case 'C': + { + int theCharInteger = va_arg(args, int); + memset(&theBuffer, 0, SOCKET_PRINTF_BUFFER); + theStringSize = snprintf(theBuffer, SOCKET_PRINTF_BUFFER, "%c", theCharInteger); + } + break; + + case 'f': + case 'F': + { + float theDouble = va_arg(args, double); + memset(&theBuffer, 0, SOCKET_PRINTF_BUFFER); + theStringSize = snprintf(theBuffer, SOCKET_PRINTF_BUFFER, "%f", theDouble); + } + break; + + case 's': + case 'S': + { + char * theString = va_arg(args, char *); + memset(&theBuffer, 0, SOCKET_PRINTF_BUFFER); + theStringSize = snprintf(theBuffer, SOCKET_PRINTF_BUFFER, "%s", theString); + } + break; + + case 'l': + case 'L': + { + long long int theLongLongInt = va_arg(args, long long int); + memset(&theBuffer, 0, SOCKET_PRINTF_BUFFER); + theStringSize = snprintf(theBuffer, SOCKET_PRINTF_BUFFER, "%lld", theLongLongInt); + } + break; + + default: + { + printf("\n Switch is default (%c)", *__fmt); + } + break; + } + + for (i = 0; i clients[clientId].socketIsConnected != 1) + return 0; if (ftpData->clients[clientId].tlsIsEnabled != 1) { + //printf("\nwriting[%d] %s",theCommandSize, commandBuffer); + //fflush(0); bytesWritten = write(ftpData->clients[clientId].socketDescriptor, commandBuffer, theCommandSize); } else if (ftpData->clients[clientId].tlsIsEnabled == 1) @@ -142,7 +210,7 @@ int socketPrintf(ftpDataType * ftpData, int clientId, const char *__restrict __f //printf("\n%s", commandBuffer); - pthread_mutex_unlock(&ftpData->clients[clientId].writeMutex); + //pthread_mutex_unlock(&ftpData->clients[clientId].writeMutex); return bytesWritten; } @@ -228,7 +296,7 @@ int socketWorkerPrintf(ftpDataType * ftpData, int clientId, const char *__restri //Write the buffer if (theStringToWriteSize >= COMMAND_BUFFER) { - //printf("\nData to write theStringToWriteSize >= COMMAND_BUFFER: %s", writeBuffer); + int theReturnCode = 0; if (ftpData->clients[clientId].dataChannelIsTls != 1) { diff --git a/library/fileManagement.c b/library/fileManagement.c index d7acf1d..c557d7e 100644 --- a/library/fileManagement.c +++ b/library/fileManagement.c @@ -679,8 +679,10 @@ char * FILE_GetGroupOwner(char *fileName, DYNMEM_MemoryTable_DataType **memoryTa time_t FILE_GetLastModifiedData(char *path) { struct stat statbuf; - if (stat(path, &statbuf) == -1) { - + if (stat(path, &statbuf) == -1) + { + time_t theTime = 0; + return theTime; } return statbuf.st_mtime; }