Added MDTM command, fixed some bugs

This commit is contained in:
Ugo Cirmignani
2019-02-12 21:09:12 +01:00
parent e52ba57b98
commit 2a2961e5c5
21 changed files with 148 additions and 24 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
@ -22,17 +22,14 @@ ENABLE_LARGE_FILE_SUPPORT=-D LARGE_FILE_SUPPORT_ENABLED -D _LARGEFILE64_SOURCE
ENABLE_OPENSSL_SUPPORT= ENABLE_OPENSSL_SUPPORT=
#TO ENABLE OPENSSL SUPPORT UNCOMMENT NEXT 2 LINES #TO ENABLE OPENSSL SUPPORT UNCOMMENT NEXT 2 LINES
#ENABLE_OPENSSL_SUPPORT=-D OPENSSL_ENABLED ENABLE_OPENSSL_SUPPORT=-D OPENSSL_ENABLED
#LIBS=-lpthread -lssl -lcrypto LIBS=-lpthread -lssl -lcrypto
ENABLE_PAM_SUPPORT= ENABLE_PAM_SUPPORT=
PAM_AUTH_LIB= PAM_AUTH_LIB=
#TO ENABLE PAM AUTH UNCOMMENT NEXT TWO LINES #TO ENABLE PAM AUTH UNCOMMENT NEXT TWO LINES
#ENABLE_PAM_SUPPORT= -D PAM_SUPPORT_ENABLED ENABLE_PAM_SUPPORT= -D PAM_SUPPORT_ENABLED
#PAM_AUTH_LIB= -lpam PAM_AUTH_LIB= -lpam
#USER PAM AUTH
#-lpam
CFLAGS=$(CFLAGSTEMP) $(ENABLE_LARGE_FILE_SUPPORT) $(ENABLE_OPENSSL_SUPPORT) $(ENABLE_PAM_SUPPORT) CFLAGS=$(CFLAGSTEMP) $(ENABLE_LARGE_FILE_SUPPORT) $(ENABLE_OPENSSL_SUPPORT) $(ENABLE_PAM_SUPPORT)

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

@ -1025,6 +1025,61 @@ int parseCommandDele(ftpDataType * data, int socketId)
return functionReturnCode; 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 parseCommandNoop(ftpDataType * data, int socketId)
{ {
int returnCode; int returnCode;
@ -1343,6 +1398,8 @@ long long int writeRetrFile(ftpDataType * data, int theSocketId, long long int s
if (writtenSize <= 0) if (writtenSize <= 0)
{ {
printf("\nError %d while writing retr file.");
fclose(retrFP); fclose(retrFP);
retrFP = NULL; retrFP = NULL;
return -1; return -1;

View File

@ -78,6 +78,7 @@ int parseCommandCwd(ftpDataType * data, int socketId);
int parseCommandRest(ftpDataType * data, int socketId); int parseCommandRest(ftpDataType * data, int socketId);
int parseCommandCdup(ftpDataType * data, int socketId); int parseCommandCdup(ftpDataType * data, int socketId);
int parseCommandDele(ftpDataType * data, int socketId); int parseCommandDele(ftpDataType * data, int socketId);
int parseCommandMdtm(ftpDataType * data, int socketId);
int parseCommandOpts(ftpDataType * data, int socketId); int parseCommandOpts(ftpDataType * data, int socketId);
int parseCommandRnfr(ftpDataType * data, int socketId); int parseCommandRnfr(ftpDataType * data, int socketId);
int parseCommandRnto(ftpDataType * data, int socketId); int parseCommandRnto(ftpDataType * data, int socketId);

View File

@ -94,7 +94,6 @@ void setDynamicStringDataType(dynamicStringDataType *dynamicString, char *theStr
} }
int getSafePath(dynamicStringDataType *safePath, char *theDirectoryName, loginDataType *loginData, DYNMEM_MemoryTable_DataType **memoryTable) int getSafePath(dynamicStringDataType *safePath, char *theDirectoryName, loginDataType *loginData, DYNMEM_MemoryTable_DataType **memoryTable)
{ {
#define STRING_SIZE 4096 #define STRING_SIZE 4096

View File

@ -263,7 +263,7 @@ void *connectionWorkerHandle(void * socketId)
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 thread actions //Conditional lock on tconditionVariablehread actions
pthread_mutex_lock(&ftpData.clients[theSocketId].workerData.conditionMutex); pthread_mutex_lock(&ftpData.clients[theSocketId].workerData.conditionMutex);
while (ftpData.clients[theSocketId].workerData.commandReceived == 0) 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); writenSize = writeRetrFile(&ftpData, theSocketId, ftpData.clients[theSocketId].workerData.retrRestartAtByte, ftpData.clients[theSocketId].workerData.theStorFile);
ftpData.clients[theSocketId].workerData.retrRestartAtByte = 0; 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"); 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') if (ftpData.clients[processingSock].buffer[i] == '\n')
{ {
ftpData.clients[processingSock].socketCommandReceived = 1; 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); commandProcessStatus = processCommand(processingSock);
//Echo unrecognized commands //Echo unrecognized commands
if (commandProcessStatus == FTP_COMMAND_NOT_RECONIZED) 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) else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "DELE", strlen("DELE")) == 1)
{ {
//printf("\nDELE command received"); //printf("\nDELE comman200 OKd received");
toReturn = parseCommandDele(&ftpData, processingElement); toReturn = parseCommandDele(&ftpData, processingElement);
} }
else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "OPTS", strlen("OPTS")) == 1) else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "OPTS", strlen("OPTS")) == 1)
@ -873,10 +873,10 @@ static int processCommand(int processingElement)
//printf("\nOPTS command received"); //printf("\nOPTS command received");
toReturn = parseCommandOpts(&ftpData, processingElement); 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"); printf("\nMTDM command received");
//To implement toReturn = parseCommandMdtm(&ftpData, processingElement);
} }
else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "NLST", strlen("NLST")) == 1) else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "NLST", strlen("NLST")) == 1)
{ {

View File

@ -40,8 +40,6 @@
#include "../ftpData.h" #include "../ftpData.h"
#include "connection.h" #include "connection.h"
int socketPrintf(ftpDataType * ftpData, int clientId, const char *__restrict __fmt, ...) int socketPrintf(ftpDataType * ftpData, int clientId, const char *__restrict __fmt, ...)
{ {
#define COMMAND_BUFFER 9600 #define COMMAND_BUFFER 9600
@ -54,7 +52,7 @@ int socketPrintf(ftpDataType * ftpData, int clientId, const char *__restrict __f
memset(&commandBuffer, 0, COMMAND_BUFFER); memset(&commandBuffer, 0, COMMAND_BUFFER);
//printf("\nWriting to socket id %d, TLS %d: ", clientId, ftpData->clients[clientId].tlsIsEnabled); //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_list args;
va_start(args, __fmt); va_start(args, __fmt);
@ -73,6 +71,22 @@ int socketPrintf(ftpDataType * ftpData, int clientId, const char *__restrict __f
} }
break; break;
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':
case 'C': case 'C':
{ {
@ -125,12 +139,66 @@ int socketPrintf(ftpDataType * ftpData, int clientId, const char *__restrict __f
} }
++__fmt; ++__fmt;
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 <theStringSize; i++)
{
if (theCommandSize < COMMAND_BUFFER)
{
commandBuffer[theCommandSize++] = theBuffer[i];
}
}
++__fmt;
} }
va_end(args); va_end(args);
if (ftpData->clients[clientId].socketIsConnected != 1)
return 0;
if (ftpData->clients[clientId].tlsIsEnabled != 1) if (ftpData->clients[clientId].tlsIsEnabled != 1)
{ {
//printf("\nwriting[%d] %s",theCommandSize, commandBuffer);
//fflush(0);
bytesWritten = write(ftpData->clients[clientId].socketDescriptor, commandBuffer, theCommandSize); bytesWritten = write(ftpData->clients[clientId].socketDescriptor, commandBuffer, theCommandSize);
} }
else if (ftpData->clients[clientId].tlsIsEnabled == 1) 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); //printf("\n%s", commandBuffer);
pthread_mutex_unlock(&ftpData->clients[clientId].writeMutex); //pthread_mutex_unlock(&ftpData->clients[clientId].writeMutex);
return bytesWritten; return bytesWritten;
} }
@ -228,7 +296,7 @@ int socketWorkerPrintf(ftpDataType * ftpData, int clientId, const char *__restri
//Write the buffer //Write the buffer
if (theStringToWriteSize >= COMMAND_BUFFER) if (theStringToWriteSize >= COMMAND_BUFFER)
{ {
//printf("\nData to write theStringToWriteSize >= COMMAND_BUFFER: %s", writeBuffer);
int theReturnCode = 0; int theReturnCode = 0;
if (ftpData->clients[clientId].dataChannelIsTls != 1) if (ftpData->clients[clientId].dataChannelIsTls != 1)
{ {

View File

@ -679,8 +679,10 @@ char * FILE_GetGroupOwner(char *fileName, DYNMEM_MemoryTable_DataType **memoryTa
time_t FILE_GetLastModifiedData(char *path) time_t FILE_GetLastModifiedData(char *path)
{ {
struct stat statbuf; struct stat statbuf;
if (stat(path, &statbuf) == -1) { if (stat(path, &statbuf) == -1)
{
time_t theTime = 0;
return theTime;
} }
return statbuf.st_mtime; return statbuf.st_mtime;
} }