diff --git a/build/modules/configRead.o b/build/modules/configRead.o index 6540daa..ee50065 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 f4a3196..aa58a57 100644 Binary files a/build/modules/connection.o and b/build/modules/connection.o differ diff --git a/build/modules/ftpCommandElaborate.o b/build/modules/ftpCommandElaborate.o index aa9be8f..ec3621f 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 33c1b0f..418802a 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 a6cf01e..e5b3025 100644 Binary files a/build/modules/ftpServer.o and b/build/modules/ftpServer.o differ diff --git a/build/uFTP b/build/uFTP index 7e52b62..624c885 100755 Binary files a/build/uFTP and b/build/uFTP differ diff --git a/ftpCommandElaborate.c b/ftpCommandElaborate.c index 7796aaa..b5e20db 100644 --- a/ftpCommandElaborate.c +++ b/ftpCommandElaborate.c @@ -41,20 +41,21 @@ #include "library/fileManagement.h" #include "library/configRead.h" #include "library/openSsl.h" +#include "library/connection.h" #include "ftpCommandsElaborate.h" /* Elaborate the User login command */ -int parseCommandUser(clientDataType *theClientData) +int parseCommandUser(ftpDataType * data, int socketId) { int returnCode; char *theUserName; - theUserName = getFtpCommandArg("USER", theClientData->theCommandReceived, 0); + theUserName = getFtpCommandArg("USER", data->clients[socketId].theCommandReceived, 0); if (strlen(theUserName) >= 1) { - setDynamicStringDataType(&theClientData->login.name, theUserName, strlen(theUserName)); - returnCode = dprintf(theClientData->socketDescriptor, "331 User ok, Waiting for the password.\r\n"); + setDynamicStringDataType(&data->clients[socketId].login.name, theUserName, strlen(theUserName)); + returnCode = socketPrintf(data, socketId, "s", "331 User ok, Waiting for the password.\r\n"); if (returnCode <= 0) return FTP_COMMAND_PROCESSED_WRITE_ERROR; @@ -63,9 +64,9 @@ int parseCommandUser(clientDataType *theClientData) } else { - returnCode = dprintf(theClientData->socketDescriptor, "430 Invalid username.\r\n"); - - if (returnCode <= 0) + returnCode = socketPrintf(data, socketId, "s", "430 Invalid username.\r\n"); + + if (returnCode <= 0) return FTP_COMMAND_PROCESSED_WRITE_ERROR; return FTP_COMMAND_PROCESSED; @@ -73,33 +74,33 @@ int parseCommandUser(clientDataType *theClientData) } /* Elaborate the User login command */ -int parseCommandSite(clientDataType *theClientData) +int parseCommandSite(ftpDataType * data, int socketId) { int returnCode, setPermissionsReturnCode; char *theCommand; - theCommand = getFtpCommandArg("SITE", theClientData->theCommandReceived, 0); + theCommand = getFtpCommandArg("SITE", data->clients[socketId].theCommandReceived, 0); if(compareStringCaseInsensitive(theCommand, "CHMOD", strlen("CHMOD")) == 1) { - setPermissionsReturnCode = setPermissions(theCommand, theClientData->login.absolutePath.text, theClientData->login.ownerShip); + setPermissionsReturnCode = setPermissions(theCommand, data->clients[socketId].login.absolutePath.text, data->clients[socketId].login.ownerShip); switch (setPermissionsReturnCode) { case FTP_CHMODE_COMMAND_RETURN_CODE_OK: { - returnCode = dprintf(theClientData->socketDescriptor, "200 Permissions changed\r\n"); + returnCode = socketPrintf(data, socketId, "s", "200 Permissions changed\r\n"); } break; case FTP_CHMODE_COMMAND_RETURN_CODE_NO_FILE: { - returnCode = dprintf(theClientData->socketDescriptor, "550 chmod no such file\r\n"); + returnCode = socketPrintf(data, socketId, "s", "550 chmod no such file\r\n"); } break; case FTP_CHMODE_COMMAND_RETURN_CODE_NO_PERMISSIONS: { - returnCode = dprintf(theClientData->socketDescriptor, "550 Some errors occoured while changing file permissions.\r\n"); + returnCode = socketPrintf(data, socketId, "s", "550 Some errors occurred while changing file permissions.\r\n"); } break; @@ -113,7 +114,7 @@ int parseCommandSite(clientDataType *theClientData) } else { - returnCode = dprintf(theClientData->socketDescriptor, "500 unknown extension\r\n"); + returnCode = socketPrintf(data, socketId, "s", "500 unknown extension\r\n"); if (returnCode <= 0) return FTP_COMMAND_PROCESSED_WRITE_ERROR; @@ -145,7 +146,7 @@ int parseCommandPass(ftpDataType * data, int socketId) { printf("\n TOO MANY LOGIN FAILS! \n"); data->clients[socketId].closeTheClient = 1; - returnCode = dprintf(data->clients[socketId].socketDescriptor, "430 Too many login failure detected, your ip will be blacklisted for 5 minutes\r\n"); + returnCode = socketPrintf(data, socketId, "s", "430 Too many login failure detected, your ip will be blacklisted for 5 minutes\r\n"); if (returnCode <= 0) return FTP_COMMAND_PROCESSED_WRITE_ERROR; return FTP_COMMAND_PROCESSED; } @@ -172,9 +173,9 @@ int parseCommandPass(ftpDataType * data, int socketId) ((loginFailsDataType *) data->loginFailsVector.Data[searchPosition])->failureNumbers++; ((loginFailsDataType *) data->loginFailsVector.Data[searchPosition])->failTimeStamp = time(NULL); } - - returnCode = dprintf(data->clients[socketId].socketDescriptor, "430 Invalid username or password\r\n"); - if (returnCode <= 0) return FTP_COMMAND_PROCESSED_WRITE_ERROR; + returnCode = socketPrintf(data, socketId, "s", "430 Invalid username or password\r\n"); + if (returnCode <= 0) + return FTP_COMMAND_PROCESSED_WRITE_ERROR; } else { @@ -188,8 +189,9 @@ int parseCommandPass(ftpDataType * data, int socketId) data->clients[socketId].login.ownerShip.uid = ((usersParameters_DataType *) data->ftpParameters.usersVector.Data[searchUserNameIndex])->ownerShip.uid; data->clients[socketId].login.userLoggedIn = 1; - returnCode = dprintf(data->clients[socketId].socketDescriptor, "230 Login Ok.\r\n"); - if (returnCode <= 0) return FTP_COMMAND_PROCESSED_WRITE_ERROR; + returnCode = socketPrintf(data, socketId, "s", "230 Login Ok.\r\n"); + if (returnCode <= 0) + return FTP_COMMAND_PROCESSED_WRITE_ERROR; } return FTP_COMMAND_PROCESSED; @@ -209,8 +211,9 @@ int parseCommandPass(ftpDataType * data, int socketId) ((loginFailsDataType *) data->loginFailsVector.Data[searchPosition])->failTimeStamp = time(NULL); } - returnCode = dprintf(data->clients[socketId].socketDescriptor, "430 Invalid username or password\r\n"); - if (returnCode <= 0) return FTP_COMMAND_PROCESSED_WRITE_ERROR; + returnCode = socketPrintf(data, socketId, "s", "430 Invalid username or password\r\n"); + if (returnCode <= 0) + return FTP_COMMAND_PROCESSED_WRITE_ERROR; return 1; } } @@ -218,8 +221,7 @@ int parseCommandPass(ftpDataType * data, int socketId) int parseCommandAuth(clientDataType *theClientData, SSL_CTX *ctx) { int returnCode; - //returnCode = dprintf(theClientData->socketDescriptor, "502 Security extensions not implemented.\r\n"); - returnCode = dprintf(theClientData->socketDescriptor, "234 AUTH TLS OK..\r\n"); + //returnCode = socketPrintf(data, socketId, "s", "234 AUTH TLS OK..\r\n"); if (returnCode <= 0) return FTP_COMMAND_PROCESSED_WRITE_ERROR; @@ -244,27 +246,28 @@ int parseCommandAuth(clientDataType *theClientData, SSL_CTX *ctx) return FTP_COMMAND_PROCESSED; } -int parseCommandPwd(clientDataType *theClientData) +int parseCommandPwd(ftpDataType * data, int socketId) { int returnCode; - returnCode = dprintf(theClientData->socketDescriptor, "257 \"%s\" is your current location\r\n", theClientData->login.ftpPath.text); + returnCode = socketPrintf(data, socketId, "sss", "257 \"", data->clients[socketId].login.ftpPath.text ,"\" is your current location\r\n"); + if (returnCode <= 0) return FTP_COMMAND_PROCESSED_WRITE_ERROR; return FTP_COMMAND_PROCESSED; } -int parseCommandSyst(clientDataType *theClientData) +int parseCommandSyst(ftpDataType * data, int socketId) { int returnCode; - returnCode = dprintf(theClientData->socketDescriptor, "215 UNIX Type: L8\r\n"); + returnCode = socketPrintf(data, socketId, "s", "215 UNIX Type: L8\r\n"); if (returnCode <= 0) return FTP_COMMAND_PROCESSED_WRITE_ERROR; return FTP_COMMAND_PROCESSED; } -int parseCommandFeat(clientDataType *theClientData) +int parseCommandFeat(ftpDataType * data, int socketId) { /* 211-Extensions supported: @@ -289,27 +292,28 @@ int parseCommandFeat(clientDataType *theClientData) 211 End. */ int returnCode; - returnCode = dprintf(theClientData->socketDescriptor, "211-Extensions supported:\r\n PASV\r\nUTF8\r\nAUTH TLS\r\n211 End.\r\n"); + returnCode = socketPrintf(data, socketId, "s", "211-Extensions supported:\r\n PASV\r\nUTF8\r\nAUTH TLS\r\n211 End.\r\n"); if (returnCode <= 0) return FTP_COMMAND_PROCESSED_WRITE_ERROR; return FTP_COMMAND_PROCESSED; } -int parseCommandTypeA(clientDataType *theClientData) +int parseCommandTypeA(ftpDataType * data, int socketId) { int returnCode; - returnCode = dprintf(theClientData->socketDescriptor, "200 TYPE is now 8-bit binary\r\n"); + returnCode = socketPrintf(data, socketId, "s", "200 TYPE is now 8-bit binary\r\n"); + if (returnCode <= 0) return FTP_COMMAND_PROCESSED_WRITE_ERROR; return FTP_COMMAND_PROCESSED; } -int parseCommandTypeI(clientDataType *theClientData) +int parseCommandTypeI(ftpDataType * data, int socketId) { int returnCode; - returnCode = dprintf(theClientData->socketDescriptor, "200 TYPE is now 8-bit binary\r\n"); + returnCode = socketPrintf(data, socketId, "s", "200 TYPE is now 8-bit binary\r\n"); if (returnCode <= 0) return FTP_COMMAND_PROCESSED_WRITE_ERROR; @@ -317,21 +321,21 @@ int parseCommandTypeI(clientDataType *theClientData) return FTP_COMMAND_PROCESSED; } -int parseCommandStruF(clientDataType *theClientData) +int parseCommandStruF(ftpDataType * data, int socketId) { int returnCode; - returnCode = dprintf(theClientData->socketDescriptor, "200 TYPE is now 8-bit binary\r\n"); + returnCode = socketPrintf(data, socketId, "s", "200 TYPE is now 8-bit binary\r\n"); if (returnCode <= 0) return FTP_COMMAND_PROCESSED_WRITE_ERROR; return FTP_COMMAND_PROCESSED; } -int parseCommandModeS(clientDataType *theClientData) +int parseCommandModeS(ftpDataType * data, int socketId) { int returnCode; - returnCode = dprintf(theClientData->socketDescriptor, "200 TYPE is now 8-bit binary\r\n"); - + returnCode = socketPrintf(data, socketId, "s", "200 TYPE is now 8-bit binary\r\n"); + if (returnCode <= 0) return FTP_COMMAND_PROCESSED_WRITE_ERROR; @@ -396,17 +400,17 @@ int parseCommandAbor(ftpDataType * data, int socketId) } pthread_join(data->clients[socketId].workerData.workerThread, &pReturn); - returnCode = dprintf(data->clients[socketId].socketDescriptor, "426 ABORT\r\n"); + returnCode = socketPrintf(data, socketId, "s", "426 ABORT\r\n"); if (returnCode <= 0) return FTP_COMMAND_PROCESSED_WRITE_ERROR; - - returnCode = dprintf(data->clients[socketId].socketDescriptor, "226 Transfer aborted\r\n"); + + returnCode = socketPrintf(data, socketId, "s", "226 Transfer aborted\r\n"); if (returnCode <= 0) return FTP_COMMAND_PROCESSED_WRITE_ERROR; } else { - returnCode = dprintf(data->clients[socketId].socketDescriptor, "226 Since you see this ABOR must've succeeded\r\n"); + returnCode = socketPrintf(data, socketId, "s", "226 Since you see this ABOR must've succeeded\r\n"); if (returnCode <= 0) return FTP_COMMAND_PROCESSED_WRITE_ERROR; } @@ -559,7 +563,7 @@ int parseCommandStor(ftpDataType * data, int socketId) return FTP_COMMAND_PROCESSED; } -int parseCommandCwd(clientDataType *theClientData) +int parseCommandCwd(ftpDataType * data, int socketId) { dynamicStringDataType absolutePathPrevious, ftpPathPrevious, theSafePath; int isSafePath; @@ -570,62 +574,62 @@ int parseCommandCwd(clientDataType *theClientData) cleanDynamicStringDataType(&ftpPathPrevious, 1); cleanDynamicStringDataType(&theSafePath, 1); - thePath = getFtpCommandArg("CWD", theClientData->theCommandReceived, 0); + thePath = getFtpCommandArg("CWD", data->clients[socketId].theCommandReceived, 0); if (strlen(thePath) > 0) { - isSafePath = getSafePath(&theSafePath, thePath, &theClientData->login); + isSafePath = getSafePath(&theSafePath, thePath, &data->clients[socketId].login); } if (isSafePath == 1) { printf("\n The Path requested for CWD IS:%s", theSafePath.text); - setDynamicStringDataType(&absolutePathPrevious, theClientData->login.absolutePath.text, theClientData->login.absolutePath.textLen); - setDynamicStringDataType(&ftpPathPrevious, theClientData->login.ftpPath.text, theClientData->login.ftpPath.textLen); + setDynamicStringDataType(&absolutePathPrevious, data->clients[socketId].login.absolutePath.text, data->clients[socketId].login.absolutePath.textLen); + setDynamicStringDataType(&ftpPathPrevious, data->clients[socketId].login.ftpPath.text, data->clients[socketId].login.ftpPath.textLen); if (theSafePath.text[0] != '/') { - if (theClientData->login.absolutePath.text[theClientData->login.absolutePath.textLen-1] != '/') - appendToDynamicStringDataType(&theClientData->login.absolutePath, "/", 1); + if (data->clients[socketId].login.absolutePath.text[data->clients[socketId].login.absolutePath.textLen-1] != '/') + appendToDynamicStringDataType(&data->clients[socketId].login.absolutePath, "/", 1); - if (theClientData->login.ftpPath.text[theClientData->login.ftpPath.textLen-1] != '/') - appendToDynamicStringDataType(&theClientData->login.ftpPath, "/", 1); + if (data->clients[socketId].login.ftpPath.text[data->clients[socketId].login.ftpPath.textLen-1] != '/') + appendToDynamicStringDataType(&data->clients[socketId].login.ftpPath, "/", 1); - appendToDynamicStringDataType(&theClientData->login.absolutePath, theSafePath.text, theSafePath.textLen); - appendToDynamicStringDataType(&theClientData->login.ftpPath, theSafePath.text, theSafePath.textLen); + appendToDynamicStringDataType(&data->clients[socketId].login.absolutePath, theSafePath.text, theSafePath.textLen); + appendToDynamicStringDataType(&data->clients[socketId].login.ftpPath, theSafePath.text, theSafePath.textLen); } else if (theSafePath.text[0] == '/') { - cleanDynamicStringDataType(&theClientData->login.ftpPath, 0); - cleanDynamicStringDataType(&theClientData->login.absolutePath, 0); + cleanDynamicStringDataType(&data->clients[socketId].login.ftpPath, 0); + cleanDynamicStringDataType(&data->clients[socketId].login.absolutePath, 0); - setDynamicStringDataType(&theClientData->login.ftpPath, theSafePath.text, theSafePath.textLen); - setDynamicStringDataType(&theClientData->login.absolutePath, theClientData->login.homePath.text, theClientData->login.homePath.textLen); + setDynamicStringDataType(&data->clients[socketId].login.ftpPath, theSafePath.text, theSafePath.textLen); + setDynamicStringDataType(&data->clients[socketId].login.absolutePath, data->clients[socketId].login.homePath.text, data->clients[socketId].login.homePath.textLen); if (strlen(theSafePath.text)> 0) { char *theDirPointer = theSafePath.text; - if (theClientData->login.absolutePath.text[theClientData->login.absolutePath.textLen-1] == '/') + if (data->clients[socketId].login.absolutePath.text[data->clients[socketId].login.absolutePath.textLen-1] == '/') { while(theDirPointer[0] == '/') theDirPointer++; } if (strlen(theDirPointer) > 0) - appendToDynamicStringDataType(&theClientData->login.absolutePath, theDirPointer, strlen(theDirPointer)); + appendToDynamicStringDataType(&data->clients[socketId].login.absolutePath, theDirPointer, strlen(theDirPointer)); } } - if (FILE_IsDirectory(theClientData->login.absolutePath.text) == 1) + if (FILE_IsDirectory(data->clients[socketId].login.absolutePath.text) == 1) { - returnCode = dprintf(theClientData->socketDescriptor, "250 OK. Current directory is %s\r\n", theClientData->login.ftpPath.text); + returnCode = socketPrintf(data, socketId, "sss", "250 OK. Current directory is ", data->clients[socketId].login.ftpPath.text, "\r\n"); } else { - setDynamicStringDataType(&theClientData->login.absolutePath, absolutePathPrevious.text, absolutePathPrevious.textLen); - setDynamicStringDataType(&theClientData->login.ftpPath, ftpPathPrevious.text, ftpPathPrevious.textLen); - returnCode = dprintf(theClientData->socketDescriptor, "530 Can't change directory to %s: No such file or directory\r\n", theClientData->login.absolutePath.text); + setDynamicStringDataType(&data->clients[socketId].login.absolutePath, absolutePathPrevious.text, absolutePathPrevious.textLen); + setDynamicStringDataType(&data->clients[socketId].login.ftpPath, ftpPathPrevious.text, ftpPathPrevious.textLen); + returnCode = socketPrintf(data, socketId, "sss", "530 Can't change directory to ", data->clients[socketId].login.absolutePath.text, ": No such file or directory\r\n"); } cleanDynamicStringDataType(&absolutePathPrevious, 0); @@ -647,7 +651,7 @@ int parseCommandCwd(clientDataType *theClientData) } } -int parseCommandRest(clientDataType *theClientData) +int parseCommandRest(ftpDataType * data, int socketId) { int returnCode; int i, theSizeIndex; @@ -655,33 +659,33 @@ int parseCommandRest(clientDataType *theClientData) memset(theSize, 0, FTP_COMMAND_ELABORATE_CHAR_BUFFER); theSizeIndex = 0; - for (i = strlen("REST")+1; i < theClientData->commandIndex; i++) + for (i = strlen("REST")+1; i < data->clients[socketId].commandIndex; i++) { - if (theClientData->theCommandReceived[i] == '\r' || - theClientData->theCommandReceived[i] == '\0' || - theClientData->theCommandReceived[i] == '\n') + if (data->clients[socketId].theCommandReceived[i] == '\r' || + data->clients[socketId].theCommandReceived[i] == '\0' || + data->clients[socketId].theCommandReceived[i] == '\n') { break; } if (theSizeIndex < FTP_COMMAND_ELABORATE_CHAR_BUFFER && - (theClientData->theCommandReceived[i] == '0' || - theClientData->theCommandReceived[i] == '1' || - theClientData->theCommandReceived[i] == '2' || - theClientData->theCommandReceived[i] == '3' || - theClientData->theCommandReceived[i] == '4' || - theClientData->theCommandReceived[i] == '5' || - theClientData->theCommandReceived[i] == '6' || - theClientData->theCommandReceived[i] == '7' || - theClientData->theCommandReceived[i] == '8' || - theClientData->theCommandReceived[i] == '9' )) + (data->clients[socketId].theCommandReceived[i] == '0' || + data->clients[socketId].theCommandReceived[i] == '1' || + data->clients[socketId].theCommandReceived[i] == '2' || + data->clients[socketId].theCommandReceived[i] == '3' || + data->clients[socketId].theCommandReceived[i] == '4' || + data->clients[socketId].theCommandReceived[i] == '5' || + data->clients[socketId].theCommandReceived[i] == '6' || + data->clients[socketId].theCommandReceived[i] == '7' || + data->clients[socketId].theCommandReceived[i] == '8' || + data->clients[socketId].theCommandReceived[i] == '9' )) { - theSize[theSizeIndex++] = theClientData->theCommandReceived[i]; + theSize[theSizeIndex++] = data->clients[socketId].theCommandReceived[i]; } } - theClientData->workerData.retrRestartAtByte = atoll(theSize); - returnCode = dprintf(theClientData->socketDescriptor, "350 Restarting at %s\r\n", theSize); + data->clients[socketId].workerData.retrRestartAtByte = atoll(theSize); + returnCode = socketPrintf(data, socketId, "sss", "350 Restarting at ", theSize, "\r\n"); if (returnCode <= 0) return FTP_COMMAND_PROCESSED_WRITE_ERROR; @@ -689,7 +693,7 @@ int parseCommandRest(clientDataType *theClientData) return FTP_COMMAND_PROCESSED; } -int parseCommandMkd(clientDataType *theClientData) +int parseCommandMkd(ftpDataType * data, int socketId) { int returnCode; int functionReturnCode = FTP_COMMAND_NOT_RECONIZED; @@ -697,11 +701,11 @@ int parseCommandMkd(clientDataType *theClientData) char *theDirectoryFilename; dynamicStringDataType mkdFileName; - theDirectoryFilename = getFtpCommandArg("MKD", theClientData->theCommandReceived, 0); + theDirectoryFilename = getFtpCommandArg("MKD", data->clients[socketId].theCommandReceived, 0); cleanDynamicStringDataType(&mkdFileName, 1); - isSafePath = getSafePath(&mkdFileName, theDirectoryFilename, &theClientData->login); + isSafePath = getSafePath(&mkdFileName, theDirectoryFilename, &data->clients[socketId].login); if (isSafePath == 1) { @@ -710,8 +714,7 @@ int parseCommandMkd(clientDataType *theClientData) if (returnStatus == -1) { - returnCode = dprintf(theClientData->socketDescriptor, "550 error while creating directory %s\r\n", theDirectoryFilename); - + returnCode = socketPrintf(data, socketId, "sss", "550 error while creating directory ", theDirectoryFilename, "\r\n"); if (returnCode <= 0) { functionReturnCode = FTP_COMMAND_PROCESSED_WRITE_ERROR; @@ -723,12 +726,12 @@ int parseCommandMkd(clientDataType *theClientData) } else { - if (theClientData->login.ownerShip.ownerShipSet == 1) + if (data->clients[socketId].login.ownerShip.ownerShipSet == 1) { - returnStatus = FILE_doChownFromUidGid(mkdFileName.text, theClientData->login.ownerShip.uid, theClientData->login.ownerShip.gid); + returnStatus = FILE_doChownFromUidGid(mkdFileName.text, data->clients[socketId].login.ownerShip.uid, data->clients[socketId].login.ownerShip.gid); } - returnCode = dprintf(theClientData->socketDescriptor, "257 \"%s\" : The directory was successfully created\r\n", theDirectoryFilename); + returnCode = socketPrintf(data, socketId, "sss", "257 \"", theDirectoryFilename, "\" : The directory was successfully created\r\n"); if (returnCode <= 0) { @@ -750,10 +753,10 @@ int parseCommandMkd(clientDataType *theClientData) return functionReturnCode; } -int parseCommandOpts(clientDataType *theClientData) +int parseCommandOpts(ftpDataType * data, int socketId) { int returnCode; - returnCode = dprintf(theClientData->socketDescriptor, "200 OK\r\n"); + returnCode = socketPrintf(data, socketId, "s", "200 OK\r\n"); if (returnCode <= 0) return FTP_COMMAND_PROCESSED_WRITE_ERROR; @@ -761,7 +764,7 @@ int parseCommandOpts(clientDataType *theClientData) return FTP_COMMAND_PROCESSED; } -int parseCommandDele(clientDataType *theClientData) +int parseCommandDele(ftpDataType * data, int socketId) { int functionReturnCode = 0; int returnCode; @@ -770,10 +773,10 @@ int parseCommandDele(clientDataType *theClientData) char *theFileToDelete; dynamicStringDataType deleFileName; - theFileToDelete = getFtpCommandArg("DELE", theClientData->theCommandReceived, 0); + theFileToDelete = getFtpCommandArg("DELE", data->clients[socketId].theCommandReceived, 0); cleanDynamicStringDataType(&deleFileName, 1); - isSafePath = getSafePath(&deleFileName, theFileToDelete, &theClientData->login); + isSafePath = getSafePath(&deleFileName, theFileToDelete, &data->clients[socketId].login); if (isSafePath == 1) { @@ -784,11 +787,11 @@ int parseCommandDele(clientDataType *theClientData) if (returnStatus == -1) { - returnCode = dprintf(theClientData->socketDescriptor, "550 Could not delete the file: %s some errors occoured\r\n", theFileToDelete); + returnCode = socketPrintf(data, socketId, "sss", "550 Could not delete the file: ", theFileToDelete, " some errors occurred\r\n"); } else { - returnCode = dprintf(theClientData->socketDescriptor, "250 Deleted %s\r\n", theFileToDelete); + returnCode = socketPrintf(data, socketId, "sss", "250 Deleted ", theFileToDelete, "\r\n"); } functionReturnCode = FTP_COMMAND_PROCESSED; @@ -798,7 +801,7 @@ int parseCommandDele(clientDataType *theClientData) } else { - returnCode = dprintf(theClientData->socketDescriptor, "550 Could not delete the file: No such file or file is a directory\r\n"); + returnCode = socketPrintf(data, socketId, "s", "550 Could not delete the file: No such file or file is a directory\r\n"); functionReturnCode = FTP_COMMAND_PROCESSED; if (returnCode <= 0) @@ -814,11 +817,11 @@ int parseCommandDele(clientDataType *theClientData) return functionReturnCode; } -int parseCommandNoop(clientDataType *theClientData) +int parseCommandNoop(ftpDataType * data, int socketId) { int returnCode; - returnCode = dprintf(theClientData->socketDescriptor, "200 Zzz...\r\n"); - + + returnCode = socketPrintf(data, socketId, "s", "200 Zzz...\r\n"); if (returnCode <= 0) { return FTP_COMMAND_PROCESSED_WRITE_ERROR; } @@ -826,10 +829,11 @@ int parseCommandNoop(clientDataType *theClientData) return FTP_COMMAND_PROCESSED; } -int notLoggedInMessage(clientDataType *theClientData) +int notLoggedInMessage(ftpDataType * data, int socketId) { int returnCode; - returnCode = dprintf(theClientData->socketDescriptor, "530 You aren't logged in\r\n"); + returnCode = socketPrintf(data, socketId, "s", "530 You aren't logged in\r\n"); + if (returnCode <= 0) return FTP_COMMAND_PROCESSED_WRITE_ERROR; @@ -839,7 +843,8 @@ int notLoggedInMessage(clientDataType *theClientData) int parseCommandQuit(ftpDataType * data, int socketId) { int returnCode; - returnCode = dprintf(data->clients[socketId].socketDescriptor, "221 Logout.\r\n"); + returnCode = socketPrintf(data, socketId, "s", "221 Logout.\r\n"); + if (returnCode <= 0) return FTP_COMMAND_PROCESSED_WRITE_ERROR; @@ -848,7 +853,7 @@ int parseCommandQuit(ftpDataType * data, int socketId) return FTP_COMMAND_PROCESSED; } -int parseCommandRmd(clientDataType *theClientData) +int parseCommandRmd(ftpDataType * data, int socketId) { int functionReturnCode = 0; int returnCode; @@ -857,11 +862,9 @@ int parseCommandRmd(clientDataType *theClientData) char *theDirectoryFilename; dynamicStringDataType rmdFileName; - theDirectoryFilename = getFtpCommandArg("RMD", theClientData->theCommandReceived, 0); - + theDirectoryFilename = getFtpCommandArg("RMD", data->clients[socketId].theCommandReceived, 0); cleanDynamicStringDataType(&rmdFileName, 1); - - isSafePath = getSafePath(&rmdFileName, theDirectoryFilename, &theClientData->login); + isSafePath = getSafePath(&rmdFileName, theDirectoryFilename, &data->clients[socketId].login); if (isSafePath == 1) { @@ -871,11 +874,11 @@ int parseCommandRmd(clientDataType *theClientData) if (returnStatus == -1) { - returnCode = dprintf(theClientData->socketDescriptor, "550 Could not remove the directory, some errors occoured.\r\n"); + returnCode = socketPrintf(data, socketId, "s", "550 Could not remove the directory, some errors occurred.\r\n"); } else { - returnCode = dprintf(theClientData->socketDescriptor, "250 The directory was successfully removed\r\n"); + returnCode = socketPrintf(data, socketId, "s", "250 The directory was successfully removed\r\n"); } functionReturnCode = FTP_COMMAND_PROCESSED; @@ -885,7 +888,7 @@ int parseCommandRmd(clientDataType *theClientData) } else { - returnCode = dprintf(theClientData->socketDescriptor, "550 Could not delete the directory:No such directory\r\n"); + returnCode = socketPrintf(data, socketId, "s", "550 Could not delete the directory:No such directory\r\n"); functionReturnCode = FTP_COMMAND_PROCESSED; if (returnCode <= 0) @@ -902,7 +905,7 @@ int parseCommandRmd(clientDataType *theClientData) return functionReturnCode; } -int parseCommandSize(clientDataType *theClientData) +int parseCommandSize(ftpDataType * data, int socketId) { int returnCode; int isSafePath; @@ -910,11 +913,11 @@ int parseCommandSize(clientDataType *theClientData) char *theFileName; dynamicStringDataType getSizeFromFileName; - theFileName = getFtpCommandArg("SIZE", theClientData->theCommandReceived, 0); + theFileName = getFtpCommandArg("SIZE", data->clients[socketId].theCommandReceived, 0); cleanDynamicStringDataType(&getSizeFromFileName, 1); - isSafePath = getSafePath(&getSizeFromFileName, theFileName, &theClientData->login); + isSafePath = getSafePath(&getSizeFromFileName, theFileName, &data->clients[socketId].login); if (isSafePath == 1) { @@ -922,16 +925,16 @@ int parseCommandSize(clientDataType *theClientData) if (FILE_IsFile(getSizeFromFileName.text)== 1) { theSize = FILE_GetFileSizeFromPath(getSizeFromFileName.text); - returnCode = dprintf(theClientData->socketDescriptor, "213 %lld\r\n", theSize); + returnCode = socketPrintf(data, socketId, "sls", "213 ", theSize, "\r\n"); } else { - returnCode = dprintf(theClientData->socketDescriptor, "550 Can't check for file existence\r\n"); + returnCode = socketPrintf(data, socketId, "s", "550 Can't check for file existence\r\n"); } } else { - returnCode = dprintf(theClientData->socketDescriptor, "550 Can't check for file existence\r\n"); + returnCode = socketPrintf(data, socketId, "s", "550 Can't check for file existence\r\n"); } cleanDynamicStringDataType(&getSizeFromFileName, 0); @@ -941,34 +944,34 @@ int parseCommandSize(clientDataType *theClientData) return FTP_COMMAND_PROCESSED; } -int parseCommandRnfr(clientDataType *theClientData) +int parseCommandRnfr(ftpDataType * data, int socketId) { int returnCode; int isSafePath; char *theRnfrFileName; - theRnfrFileName = getFtpCommandArg("RNFR", theClientData->theCommandReceived, 0); - cleanDynamicStringDataType(&theClientData->renameFromFile, 0); + theRnfrFileName = getFtpCommandArg("RNFR", data->clients[socketId].theCommandReceived, 0); + cleanDynamicStringDataType(&data->clients[socketId].renameFromFile, 0); - isSafePath = getSafePath(&theClientData->renameFromFile, theRnfrFileName, &theClientData->login); + isSafePath = getSafePath(&data->clients[socketId].renameFromFile, theRnfrFileName, &data->clients[socketId].login); if (isSafePath == 1&& - (FILE_IsFile(theClientData->renameFromFile.text) == 1 || - FILE_IsDirectory(theClientData->renameFromFile.text) == 1)) + (FILE_IsFile(data->clients[socketId].renameFromFile.text) == 1 || + FILE_IsDirectory(data->clients[socketId].renameFromFile.text) == 1)) { - if (FILE_IsFile(theClientData->renameFromFile.text) == 1 || - FILE_IsDirectory(theClientData->renameFromFile.text) == 1) + if (FILE_IsFile(data->clients[socketId].renameFromFile.text) == 1 || + FILE_IsDirectory(data->clients[socketId].renameFromFile.text) == 1) { - returnCode = dprintf(theClientData->socketDescriptor, "350 RNFR accepted - file exists, ready for destination\r\n"); + returnCode = socketPrintf(data, socketId, "s", "350 RNFR accepted - file exists, ready for destination\r\n"); } else { - returnCode = dprintf(theClientData->socketDescriptor, "550 Sorry, but that file doesn't exist\r\n"); + returnCode = socketPrintf(data, socketId, "s", "550 Sorry, but that file doesn't exist\r\n"); } } else { - returnCode = dprintf(theClientData->socketDescriptor, "550 Sorry, but that file doesn't exist\r\n"); + returnCode = socketPrintf(data, socketId, "s", "550 Sorry, but that file doesn't exist\r\n"); } @@ -976,74 +979,70 @@ int parseCommandRnfr(clientDataType *theClientData) return FTP_COMMAND_PROCESSED; } -int parseCommandRnto(clientDataType *theClientData) +int parseCommandRnto(ftpDataType * data, int socketId) { int returnCode = 0; int isSafePath; char *theRntoFileName; - theRntoFileName = getFtpCommandArg("RNTO", theClientData->theCommandReceived, 0); - cleanDynamicStringDataType(&theClientData->renameToFile, 0); + theRntoFileName = getFtpCommandArg("RNTO", data->clients[socketId].theCommandReceived, 0); + cleanDynamicStringDataType(&data->clients[socketId].renameToFile, 0); - isSafePath = getSafePath(&theClientData->renameToFile, theRntoFileName, &theClientData->login); + isSafePath = getSafePath(&data->clients[socketId].renameToFile, theRntoFileName, &data->clients[socketId].login); if (isSafePath == 1 && - theClientData->renameFromFile.textLen > 0) + data->clients[socketId].renameFromFile.textLen > 0) { - if (FILE_IsFile(theClientData->renameFromFile.text) == 1 || - FILE_IsDirectory(theClientData->renameFromFile.text) == 1) + if (FILE_IsFile(data->clients[socketId].renameFromFile.text) == 1 || + FILE_IsDirectory(data->clients[socketId].renameFromFile.text) == 1) { - returnCode = rename (theClientData->renameFromFile.text, theClientData->renameToFile.text); + returnCode = rename (data->clients[socketId].renameFromFile.text, data->clients[socketId].renameToFile.text); if (returnCode == 0) { - printf("\n250 File successfully renamed or moved"); - returnCode = dprintf(theClientData->socketDescriptor, "250 File successfully renamed or moved\r\n"); + returnCode = socketPrintf(data, socketId, "s", "250 File successfully renamed or moved\r\n"); } else { - printf("\n503 Error Renaming the file"); - returnCode = dprintf(theClientData->socketDescriptor, "503 Error Renaming the file\r\n"); + returnCode = socketPrintf(data, socketId, "s", "503 Error Renaming the file\r\n"); } } else { - printf("\n503 Need RNFR before RNTO"); - returnCode = dprintf(theClientData->socketDescriptor, "503 Need RNFR before RNTO\r\n"); + returnCode = socketPrintf(data, socketId, "s", "503 Need RNFR before RNTO\r\n"); } } else { - printf("\n503 Error Renaming the file"); - returnCode = dprintf(theClientData->socketDescriptor, "503 Error Renaming the file\r\n"); + returnCode = socketPrintf(data, socketId, "s", "503 Error Renaming the file\r\n"); } if (returnCode <= 0) { - printf("\, parseCommandRnto return code: %d", returnCode); return FTP_COMMAND_PROCESSED_WRITE_ERROR; } - else { + else + { return FTP_COMMAND_PROCESSED; } } -int parseCommandCdup(clientDataType *theClientData) +int parseCommandCdup(ftpDataType * data, int socketId) { int returnCode; - FILE_DirectoryToParent(&theClientData->login.absolutePath.text); - FILE_DirectoryToParent(&theClientData->login.ftpPath.text); - theClientData->login.absolutePath.textLen = strlen(theClientData->login.absolutePath.text); - theClientData->login.ftpPath.textLen = strlen(theClientData->login.ftpPath.text); + FILE_DirectoryToParent(&data->clients[socketId].login.absolutePath.text); + FILE_DirectoryToParent(&data->clients[socketId].login.ftpPath.text); + data->clients[socketId].login.absolutePath.textLen = strlen(data->clients[socketId].login.absolutePath.text); + data->clients[socketId].login.ftpPath.textLen = strlen(data->clients[socketId].login.ftpPath.text); - if(strncmp(theClientData->login.absolutePath.text, theClientData->login.homePath.text, theClientData->login.homePath.textLen) != 0) + if(strncmp(data->clients[socketId].login.absolutePath.text, data->clients[socketId].login.homePath.text, data->clients[socketId].login.homePath.textLen) != 0) { - setDynamicStringDataType(&theClientData->login.absolutePath, theClientData->login.homePath.text, theClientData->login.homePath.textLen); + setDynamicStringDataType(&data->clients[socketId].login.absolutePath, data->clients[socketId].login.homePath.text, data->clients[socketId].login.homePath.textLen); } - returnCode = dprintf(theClientData->socketDescriptor, "250 OK. Current directory is %s\r\n", theClientData->login.ftpPath.text); + returnCode = socketPrintf(data, socketId, "sss", "250 OK. Current directory is ", data->clients[socketId].login.ftpPath.text, "\r\n"); if (returnCode <= 0) return FTP_COMMAND_PROCESSED_WRITE_ERROR; diff --git a/ftpCommandsElaborate.h b/ftpCommandsElaborate.h index 83926ef..d3be4d1 100644 --- a/ftpCommandsElaborate.h +++ b/ftpCommandsElaborate.h @@ -47,37 +47,39 @@ extern "C" { /* Elaborate the User login command */ -int parseCommandUser(clientDataType *theClientData); -int parseCommandSite(clientDataType *theClientData); +int parseCommandUser(ftpDataType * data, int socketId); +int parseCommandSite(ftpDataType * data, int socketId); int parseCommandPass(ftpDataType * data, int socketId); + int parseCommandAuth(clientDataType *theClientData, SSL_CTX *); -int parseCommandPwd(clientDataType *theClientData); -int parseCommandSyst(clientDataType *theClientData); -int parseCommandFeat(clientDataType *theClientData); -int parseCommandStruF(clientDataType *theClientData); -int parseCommandTypeI(clientDataType *theClientData); -int parseCommandModeS(clientDataType *theClientData); -int parseCommandTypeA(clientDataType *theClientData); + +int parseCommandPwd(ftpDataType * data, int socketId); +int parseCommandSyst(ftpDataType * data, int socketId); +int parseCommandFeat(ftpDataType * data, int socketId); +int parseCommandStruF(ftpDataType * data, int socketId); +int parseCommandTypeI(ftpDataType * data, int socketId); +int parseCommandModeS(ftpDataType * data, int socketId); +int parseCommandTypeA(ftpDataType * data, int socketId); int parseCommandAbor(ftpDataType * data, int socketId); int parseCommandPasv(ftpDataType * data, int socketId); int parseCommandPort(ftpDataType * data, int socketId); int parseCommandList(ftpDataType * data, int socketId); int parseCommandNlst(ftpDataType * data, int socketId); int parseCommandRetr(ftpDataType * data, int socketId); -int parseCommandMkd(clientDataType *theClientData); -int parseCommandNoop(clientDataType *theClientData); -int notLoggedInMessage(clientDataType *theClientData); -int parseCommandRmd(clientDataType *theClientData); +int parseCommandMkd(ftpDataType * data, int socketId); +int parseCommandNoop(ftpDataType * data, int socketId); +int notLoggedInMessage(ftpDataType * data, int socketId); +int parseCommandRmd(ftpDataType * data, int socketId); int parseCommandQuit(ftpDataType * data, int socketId); -int parseCommandSize(clientDataType *theClientData); +int parseCommandSize(ftpDataType * data, int socketId); int parseCommandStor(ftpDataType * data, int socketId); -int parseCommandCwd(clientDataType *theClientData); -int parseCommandRest(clientDataType *theClientData); -int parseCommandCdup(clientDataType *theClientData); -int parseCommandDele(clientDataType *theClientData); -int parseCommandOpts(clientDataType *theClientData); -int parseCommandRnfr(clientDataType *theClientData); -int parseCommandRnto(clientDataType *theClientData); +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 parseCommandOpts(ftpDataType * data, int socketId); +int parseCommandRnfr(ftpDataType * data, int socketId); +int parseCommandRnto(ftpDataType * data, int socketId); long long int writeRetrFile(char * theFilename, int thePasvSocketConnection, long long int startFrom, FILE *retrFP); char *getFtpCommandArg(char * theCommand, char *theCommandString, int skipArgs); diff --git a/ftpData.c b/ftpData.c index 4daf72f..5e04644 100644 --- a/ftpData.c +++ b/ftpData.c @@ -241,14 +241,10 @@ int writeListDataInfoToSocket(char * thePath, int theSocket, int *filesNumber, i data.fileNameWithPath = NULL; data.finalStringPath = NULL; data.linkPath = NULL; - - data.isFile = 0; data.isDirectory = 0; - - + //printf("\nPROCESSING: %s", fileList[i]); - //fflush(0); if (FILE_IsDirectory(fileList[i]) == 1) { @@ -329,7 +325,7 @@ int writeListDataInfoToSocket(char * thePath, int theSocket, int *filesNumber, i case COMMAND_TYPE_NLST: { - returnCode = dprintf(theSocket, "%s\r\n",data.fileNameNoPath); + returnCode = dprintf(theSocket, "%s\r\n",data.fileNameNoPath); } break; @@ -339,7 +335,6 @@ int writeListDataInfoToSocket(char * thePath, int theSocket, int *filesNumber, i printf("\nWarning switch default in function writeListDataInfoToSocket (%d)", commandType); } break; - } @@ -370,12 +365,12 @@ int writeListDataInfoToSocket(char * thePath, int theSocket, int *filesNumber, i } } - - if (fileList != NULL) - { - free (fileList); - } - + + if (fileList != NULL) + { + free (fileList); + } + return 1; } @@ -584,9 +579,19 @@ void resetWorkerData(workerDataType *workerData, int isInitialization) workerData->workerThread = 0; } - pthread_mutex_init(&workerData->conditionMutex, NULL); - pthread_cond_init(&workerData->conditionVariable, NULL); - + if (pthread_mutex_init(&workerData->conditionMutex, NULL) != 0) + { + printf("\nworkerData->conditionMutex init failed\n"); + exit(0); + } + + + if (pthread_cond_init(&workerData->conditionVariable, NULL) != 0) + { + printf("\nworkerData->conditionVariable init failed\n"); + exit(0); + } + //Clear the dynamic vector structure int theSize = workerData->directoryInfo.Size; char ** lastToDestroy = NULL; @@ -613,8 +618,16 @@ void resetClientData(clientDataType *clientData, int isInitialization) void *pReturn = NULL; pthread_join(clientData->workerData.workerThread, &pReturn); } + + pthread_mutex_destroy(&clientData->writeMutex); } + if (pthread_mutex_init(&clientData->writeMutex, NULL) != 0) + { + printf("\nclientData->writeMutex init failed\n"); + exit(0); + } + clientData->socketDescriptor = -1; clientData->socketCommandReceived = 0; clientData->socketIsConnected = 0; diff --git a/ftpData.h b/ftpData.h index 5588dd8..7905ad8 100644 --- a/ftpData.h +++ b/ftpData.h @@ -117,8 +117,6 @@ struct ipData struct workerData { - - int threadIsAlive; int connectionPort; int passiveModeOn; @@ -152,6 +150,7 @@ struct clientData { SSL *ssl; int tlsIsEnabled; + pthread_mutex_t writeMutex; int clientProgressiveNumber; int socketDescriptor; diff --git a/ftpServer.c b/ftpServer.c index 57b2fe5..8126b10 100644 --- a/ftpServer.c +++ b/ftpServer.c @@ -22,8 +22,6 @@ * THE SOFTWARE. */ - - #include #include #include @@ -39,19 +37,19 @@ #include /* FTP LIBS */ -#include "ftpServer.h" -#include "ftpData.h" -#include "ftpCommandsElaborate.h" - #include "library/fileManagement.h" #include "library/logFunctions.h" #include "library/configRead.h" #include "library/signals.h" #include "library/openSsl.h" #include "library/connection.h" +#include "ftpServer.h" +#include "ftpData.h" +#include "ftpCommandsElaborate.h" ftpDataType ftpData; SSL_CTX *ctx; + static int processCommand(int processingElement); void workerCleanup(void *socketId) @@ -85,7 +83,7 @@ void *connectionWorkerHandle(void * socketId) { break; } - + tries--; } if (ftpData.clients[theSocketId].workerData.passiveListeningSocket == -1) @@ -98,7 +96,7 @@ void *connectionWorkerHandle(void * socketId) if (ftpData.clients[theSocketId].workerData.socketIsConnected == 0) { - returnCode = dprintf(ftpData.clients[theSocketId].socketDescriptor, "227 Entering Passive Mode (%d,%d,%d,%d,%d,%d)\r\n", ftpData.clients[theSocketId].serverIpAddressInteger[0], ftpData.clients[theSocketId].serverIpAddressInteger[1], ftpData.clients[theSocketId].serverIpAddressInteger[2], ftpData.clients[theSocketId].serverIpAddressInteger[3], (ftpData.clients[theSocketId].workerData.connectionPort / 256), (ftpData.clients[theSocketId].workerData.connectionPort % 256)); + returnCode = socketPrintf(&ftpData, theSocketId, "sdsdsdsdsdsds", "227 Entering Passive Mode (", ftpData.clients[theSocketId].serverIpAddressInteger[0], ",", ftpData.clients[theSocketId].serverIpAddressInteger[1], ",", ftpData.clients[theSocketId].serverIpAddressInteger[2], ",", ftpData.clients[theSocketId].serverIpAddressInteger[3], ",", (ftpData.clients[theSocketId].workerData.connectionPort / 256), ",", (ftpData.clients[theSocketId].workerData.connectionPort % 256), ")\r\n"); if (returnCode <= 0) { ftpData.clients[theSocketId].closeTheClient = 1; @@ -130,7 +128,7 @@ void *connectionWorkerHandle(void * socketId) pthread_exit(NULL); } - returnCode = dprintf(ftpData.clients[theSocketId].socketDescriptor, "200 connection accepted\r\n"); + returnCode = socketPrintf(&ftpData, theSocketId, "s", "200 connection accepted\r\n"); if (returnCode <= 0) { @@ -172,8 +170,8 @@ void *connectionWorkerHandle(void * socketId) if (ftpData.clients[theSocketId].workerData.theStorFile == NULL) - { - returnCode = dprintf(ftpData.clients[theSocketId].socketDescriptor, "553 Unable to write the file\r\n"); + { + returnCode = socketPrintf(&ftpData, theSocketId, "s", "553 Unable to write the file\r\n"); if (returnCode <= 0) { @@ -185,7 +183,7 @@ void *connectionWorkerHandle(void * socketId) break; } - returnCode = dprintf(ftpData.clients[theSocketId].socketDescriptor, "150 Accepted data connection\r\n"); + returnCode = socketPrintf(&ftpData, theSocketId, "s", "150 Accepted data connection\r\n"); if (returnCode <= 0) { @@ -221,7 +219,7 @@ void *connectionWorkerHandle(void * socketId) FILE_doChownFromUidGid(ftpData.clients[theSocketId].fileToStor.text, ftpData.clients[theSocketId].login.ownerShip.uid, ftpData.clients[theSocketId].login.ownerShip.gid); } - returnCode = dprintf(ftpData.clients[theSocketId].socketDescriptor, "226 file stor ok\r\n"); + returnCode = socketPrintf(&ftpData, theSocketId, "s", "226 file stor ok\r\n"); if (returnCode <= 0) { ftpData.clients[theSocketId].closeTheClient = 1; @@ -243,7 +241,7 @@ void *connectionWorkerHandle(void * socketId) else if (compareStringCaseInsensitive(ftpData.clients[theSocketId].workerData.theCommandReceived, "NLST", strlen("NLST")) == 1) theCommandType = COMMAND_TYPE_NLST; - returnCode = dprintf(ftpData.clients[theSocketId].socketDescriptor, "150 Accepted data connection\r\n"); + returnCode = socketPrintf(&ftpData, theSocketId, "s", "150 Accepted data connection\r\n"); if (returnCode <= 0) { ftpData.clients[theSocketId].closeTheClient = 1; @@ -259,7 +257,7 @@ void *connectionWorkerHandle(void * socketId) pthread_exit(NULL); } - returnCode = dprintf(ftpData.clients[theSocketId].socketDescriptor, "226 %d matches total\r\n", theFiles); + returnCode = socketPrintf(&ftpData, theSocketId, "sds", "226 ", theFiles, " matches total\r\n"); if (returnCode <= 0) { ftpData.clients[theSocketId].closeTheClient = 1; @@ -273,8 +271,7 @@ void *connectionWorkerHandle(void * socketId) compareStringCaseInsensitive(ftpData.clients[theSocketId].workerData.theCommandReceived, "RETR", strlen("RETR")) == 1) { long long int writenSize = 0, writeReturn = 0; - - writeReturn = dprintf(ftpData.clients[theSocketId].socketDescriptor, "150 Accepted data connection\r\n"); + writeReturn = socketPrintf(&ftpData, theSocketId, "s", "150 Accepted data connection\r\n"); if (writeReturn <= 0) { ftpData.clients[theSocketId].closeTheClient = 1; @@ -288,7 +285,7 @@ void *connectionWorkerHandle(void * socketId) if (writenSize == -1) { - writeReturn = dprintf(ftpData.clients[theSocketId].socketDescriptor, "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"); if (writeReturn <= 0) { @@ -299,7 +296,7 @@ void *connectionWorkerHandle(void * socketId) break; } - writeReturn = dprintf(ftpData.clients[theSocketId].socketDescriptor, "226-File successfully transferred\r\n226 done\r\n"); + writeReturn = socketPrintf(&ftpData, theSocketId, "s", "226-File successfully transferred\r\n226 done\r\n"); if (writeReturn <= 0) { @@ -328,6 +325,7 @@ void runFtpServer(void) { printf("\nHello uFTP server v%s starting..\n", UFTP_SERVER_VERSION); + /* Needed for Select*/ static int processingSock = 0, returnCode = 0; @@ -434,7 +432,7 @@ void runFtpServer(void) if (commandProcessStatus == FTP_COMMAND_NOT_RECONIZED) { int returnCode = 0; - returnCode = dprintf(ftpData.clients[processingSock].socketDescriptor, "500 Unknown command\r\n"); + returnCode = socketPrintf(&ftpData, processingSock, "s", "500 Unknown command\r\n"); if (returnCode < 0) { ftpData.clients[processingSock].closeTheClient = 1; @@ -459,7 +457,7 @@ void runFtpServer(void) int returnCode; ftpData.clients[processingSock].commandIndex = 0; memset(ftpData.clients[processingSock].theCommandReceived, 0, CLIENT_COMMAND_STRING_SIZE); - returnCode = dprintf(ftpData.clients[processingSock].socketDescriptor, "500 Unknown command\r\n"); + returnCode = socketPrintf(&ftpData, processingSock, "s", "500 Unknown command\r\n"); if (returnCode <= 0) ftpData.clients[processingSock].closeTheClient = 1; @@ -496,7 +494,7 @@ static int processCommand(int processingElement) compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "QUIT", strlen("QUIT")) != 1 && compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "AUTH", strlen("AUTH")) != 1)) { - toReturn = notLoggedInMessage(&ftpData.clients[processingElement]); + toReturn = notLoggedInMessage(&ftpData, processingElement); ftpData.clients[processingElement].commandIndex = 0; memset(ftpData.clients[processingElement].theCommandReceived, 0, CLIENT_COMMAND_STRING_SIZE); return 1; @@ -506,7 +504,7 @@ static int processCommand(int processingElement) if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "USER", strlen("USER")) == 1) { printf("\nUSER COMMAND RECEIVED"); - toReturn = parseCommandUser(&ftpData.clients[processingElement]); + toReturn = parseCommandUser(&ftpData, processingElement); } else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "PASS", strlen("PASS")) == 1) { @@ -516,7 +514,7 @@ static int processCommand(int processingElement) else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "SITE", strlen("SITE")) == 1) { printf("\nSITE COMMAND RECEIVED"); - toReturn = parseCommandSite(&ftpData.clients[processingElement]); + toReturn = parseCommandSite(&ftpData, processingElement); } else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "AUTH", strlen("AUTH")) == 1) { @@ -567,37 +565,37 @@ static int processCommand(int processingElement) else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "PWD", strlen("PWD")) == 1) { printf("\nPWD COMMAND RECEIVED"); - toReturn = parseCommandPwd(&ftpData.clients[processingElement]); + toReturn = parseCommandPwd(&ftpData, processingElement); } else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "SYST", strlen("SYST")) == 1) { printf("\nSYST COMMAND RECEIVED"); - toReturn = parseCommandSyst(&ftpData.clients[processingElement]); + toReturn = parseCommandSyst(&ftpData, processingElement); } else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "FEAT", strlen("FEAT")) == 1) { printf("\nFEAT COMMAND RECEIVED"); - toReturn = parseCommandFeat(&ftpData.clients[processingElement]); + toReturn = parseCommandFeat(&ftpData, processingElement); } else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "TYPE I", strlen("TYPE I")) == 1) { printf("\nTYPE I COMMAND RECEIVED"); - toReturn = parseCommandTypeI(&ftpData.clients[processingElement]); + toReturn = parseCommandTypeI(&ftpData, processingElement); } else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "STRU F", strlen("STRU F")) == 1) { printf("\nTYPE I COMMAND RECEIVED"); - toReturn = parseCommandStruF(&ftpData.clients[processingElement]); + toReturn = parseCommandStruF(&ftpData, processingElement); } else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "MODE S", strlen("MODE S")) == 1) { printf("\nMODE S COMMAND RECEIVED"); - toReturn = parseCommandModeS(&ftpData.clients[processingElement]); + toReturn = parseCommandModeS(&ftpData, processingElement); } else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "TYPE A", strlen("TYPE A")) == 1) { printf("\nTYPE A COMMAND RECEIVED"); - toReturn = parseCommandTypeI(&ftpData.clients[processingElement]); + toReturn = parseCommandTypeI(&ftpData, processingElement); } else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "PASV", strlen("PASV")) == 1) { @@ -617,17 +615,17 @@ static int processCommand(int processingElement) else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "CWD", strlen("CWD")) == 1) { printf("\nCWD COMMAND RECEIVED"); - toReturn = parseCommandCwd(&ftpData.clients[processingElement]); + toReturn = parseCommandCwd(&ftpData, processingElement); } else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "CDUP", strlen("CDUP")) == 1) { printf("\nCDUP COMMAND RECEIVED"); - toReturn = parseCommandCdup(&ftpData.clients[processingElement]); + toReturn = parseCommandCdup(&ftpData, processingElement); } else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "REST", strlen("REST")) == 1) { printf("\nREST COMMAND RECEIVED"); - toReturn = parseCommandRest(&ftpData.clients[processingElement]); + toReturn = parseCommandRest(&ftpData, processingElement); } else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "RETR", strlen("RETR")) == 1) { @@ -642,7 +640,7 @@ static int processCommand(int processingElement) else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "MKD", strlen("MKD")) == 1) { printf("\nMKD command received"); - toReturn = parseCommandMkd(&ftpData.clients[processingElement]); + toReturn = parseCommandMkd(&ftpData, processingElement); } else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "ABOR", strlen("ABOR")) == 1) { @@ -652,12 +650,12 @@ static int processCommand(int processingElement) else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "DELE", strlen("DELE")) == 1) { printf("\nDELE command received"); - toReturn = parseCommandDele(&ftpData.clients[processingElement]); + toReturn = parseCommandDele(&ftpData, processingElement); } else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "OPTS", strlen("OPTS")) == 1) { printf("\nOPTS command received"); - toReturn = parseCommandOpts(&ftpData.clients[processingElement]); + toReturn = parseCommandOpts(&ftpData, processingElement); } else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "MTDM", strlen("MTDM")) == 1) { @@ -682,23 +680,23 @@ static int processCommand(int processingElement) else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "RMD", strlen("RMD")) == 1) { printf("\nRMD command received"); - toReturn = parseCommandRmd(&ftpData.clients[processingElement]); + toReturn = parseCommandRmd(&ftpData, processingElement); } else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "RNFR", strlen("RNFR")) == 1) { printf("\nRNFR command received"); - toReturn = parseCommandRnfr(&ftpData.clients[processingElement]); + toReturn = parseCommandRnfr(&ftpData, processingElement); } else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "RNTO", strlen("RNTO")) == 1) { printf("\nRNTO command received"); - toReturn = parseCommandRnto(&ftpData.clients[processingElement]); + toReturn = parseCommandRnto(&ftpData, processingElement); } else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "SIZE", strlen("SIZE")) == 1) { printf("\nSIZE command received"); - toReturn = parseCommandSize(&ftpData.clients[processingElement]); + toReturn = parseCommandSize(&ftpData, processingElement); } else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "APPE", strlen("APPE")) == 1) { @@ -708,7 +706,7 @@ static int processCommand(int processingElement) else if(compareStringCaseInsensitive(ftpData.clients[processingElement].theCommandReceived, "NOOP", strlen("NOOP")) == 1) { printf("\nNOOP command received"); - toReturn = parseCommandNoop(&ftpData.clients[processingElement]); + toReturn = parseCommandNoop(&ftpData, processingElement); } else { @@ -720,7 +718,6 @@ static int processCommand(int processingElement) return toReturn; } - void deallocateMemory(void) { printf("\n Deallocating the memory.. "); diff --git a/key.pem b/key.pem index 4192fd0..e4666a6 100644 --- a/key.pem +++ b/key.pem @@ -1,54 +1,51 @@ ------BEGIN ENCRYPTED PRIVATE KEY----- -MIIJnDBOBgkqhkiG9w0BBQ0wQTApBgkqhkiG9w0BBQwwHAQIk9UFKRwEjw8CAggA -MAwGCCqGSIb3DQIJBQAwFAYIKoZIhvcNAwcECGutkv5IqpwYBIIJSOqHgV8C9ZRY -okfTbCG9TxaCwX8Lxy0+7/FK2hpRDv3WwdNN5YYyr7iTUl5EiL2MsBpWshtIpT0H -P4ODORNWA1ZbQAYdPvdwsyo+Q1+A9j9Cl02Us9t8D2OSHWjxKzaJJrWgROtHicYP -t1V3tgLpQikG8idL9/eIy4os/i3HMZPTRrzs7XfsH5YWQKEcHS0hjJkeZy5uVAKz -J2UEiQJBS/DP+bvS0dTyU8JwbXpVjSXFHNWy0a5AhdddXL5etWWaZn+VJaWjvYI6 -IkOP6bk0V9YcbvLm5rmXoWkQ3ZufFP+T/HU1bFFEpy1H1aV9aE91UBXxQm9ShOIU -0f9itc19JUg1P7Z1IK7J3f332Wy2/tjVX/XQ5rQqMrC/qsH9llfBeZshb+2SmNzC -vs88hSH4KZ2dAehyUazW4z6RGzH26V+CK2MXc1UV3ocTRvhiMux2XFlni1yyd0hy -2uSFT6a2rRPMoVzW0mLzu4qTgFf0vDaihGrJGFZH/WBcSiSvKzSDg86LfO5ZVAWQ -JHAdP4il9IraMQo9ugAK/EmkDDGN+d0VnLAmtuwkxAI/QhUGYkGfGIn8FEx8xLpL -lGcQqwJQBmd3FL6oOpISvaba+ZeawK7QtUjGiWj7IjahJLdmrnI5W8dLf7l+rQRY -VYfv4qvoB5oZLrS7/DGBQs/zNv9uY6pKv4dwoilGkv+hTbAtfMp/VlqhM49mSgdA -nNXnRGy0dfG3R8nymcaCXz5gU3VLB7J8Gcmo93L9XKz9LCUjourmCW2u9hDMXBXh -La/6F88wOiwhi1ZfLQjMr+eaM0QjVWhrt9GnRl9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gmc9HbxquT7BOMQrMtXcCJZ3d8S/xaMqUdgtJcJvY -HLJ2pchHH/14crDeE2eUobRt50LEC8aoqn+LeCtJtM8AW1qnPoVUR8ng/jL5ABqw -+bEdTualgsLaBay+4VwoaypxAFh1WZKew+2mkoANLFtWWfOB/HazNh442iOsv5g2 -ptRDth1SZsFhKa0ovi88avQji3zBQWoz7lp9L4W7VR2Y8bR33KHG2T9sTdv/+rxG -K2EdyPkehQ1J4kxpT5WkMQ== ------END ENCRYPTED PRIVATE KEY----- +-----BEGIN RSA PRIVATE KEY----- +MIIJKQIBAAKCAgEAxtffV5tVrEF9pXAVaDmA2ObGchVThV+8P78W0bJFOBNqDlIr +mDtSgNdA/MUOndLGgit2VeQo35w5YCxHpX8aR/hm3LNwK7MOy8Vq0xtafasSzdZA +uv1Qfq8QyVNOc4lZXOHURAmULooj4tM5a0jbQjGhB85FWyhlZDMLM+d4/1C17C6H +OS9jpFgyoCPADsUmqHDJ4gKn6ly8lW5ulDtDwjIXFJSFq+NqQ7DnZburpxVxbJzy +2vdySatxEbfVoQNUyInJWQ5sxlIk4uFmfeixmponX+IWfv6bQ2x4oH93o0av1wb4 +/bJj8C92wmxO2a6sH8gQjOR7jDoXBuRAu8ghPzEXRDMfmWUcdHIQDymgWOb63JPF ++9MLZK/Q8WB1xGYdZ98qvvVmOmTqTeuAzGhQDMfxA4OSeLmdD//aI/6ciGqxUrGN +JVf6jyNtu72bCd/R553/STp+X9CIGRlBKlHL523YBkVEo6UFGtCqfMIQOtIS/frg +fq48UpYICyVBkPv6bvHkJjGJYswLs//saEnfv4bunMAgzkn/GXVMxMMnEQXwHNtT +qinLkFltsEc8kr9KHUx/T+QIZrqzDJmS7HLWuneMyBstSvY4XP83TjiuzVm5R7Ut +qmEOwx5kdVFH7/2x03Hm+GsC59xPDnb77l2h64FaLnizvgn01JMSE7+jHeMCAwEA +AQKCAgBGXzwB602kywDGviY1XrSFDxeIZJfMihc7kEtpJ2hr8iMEsnwPQl9ujb/I +N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m8aRbcAL7e +AcP9gXCFUq6bndk0XF7LfhvmavLOBlAbOiBozQsUKG0eLH24zBkkIShRGxtLyA8+ +r98RRylaPsDPfegLBb45QW1DzeC6ZEoJ1plTWJonh65+l6ADtA9iF5ZxixXV6LUD +ID4+KlRnj7ycAo++329VdvtCbRrozrrV0SNWzOlin4mUq6UKcugGvwgV8rZSIADP +sh1d6YA3nFICcNgY8qhSTv8iN6QYBpQRoVonCbIksfgd3vR5hT6d75Ivq1iq +-----END RSA PRIVATE KEY----- diff --git a/key.pem.cfr b/key.pem.cfr new file mode 100644 index 0000000..4192fd0 --- /dev/null +++ b/key.pem.cfr @@ -0,0 +1,54 @@ +-----BEGIN ENCRYPTED PRIVATE KEY----- +MIIJnDBOBgkqhkiG9w0BBQ0wQTApBgkqhkiG9w0BBQwwHAQIk9UFKRwEjw8CAggA +MAwGCCqGSIb3DQIJBQAwFAYIKoZIhvcNAwcECGutkv5IqpwYBIIJSOqHgV8C9ZRY +okfTbCG9TxaCwX8Lxy0+7/FK2hpRDv3WwdNN5YYyr7iTUl5EiL2MsBpWshtIpT0H +P4ODORNWA1ZbQAYdPvdwsyo+Q1+A9j9Cl02Us9t8D2OSHWjxKzaJJrWgROtHicYP +t1V3tgLpQikG8idL9/eIy4os/i3HMZPTRrzs7XfsH5YWQKEcHS0hjJkeZy5uVAKz +J2UEiQJBS/DP+bvS0dTyU8JwbXpVjSXFHNWy0a5AhdddXL5etWWaZn+VJaWjvYI6 +IkOP6bk0V9YcbvLm5rmXoWkQ3ZufFP+T/HU1bFFEpy1H1aV9aE91UBXxQm9ShOIU +0f9itc19JUg1P7Z1IK7J3f332Wy2/tjVX/XQ5rQqMrC/qsH9llfBeZshb+2SmNzC +vs88hSH4KZ2dAehyUazW4z6RGzH26V+CK2MXc1UV3ocTRvhiMux2XFlni1yyd0hy +2uSFT6a2rRPMoVzW0mLzu4qTgFf0vDaihGrJGFZH/WBcSiSvKzSDg86LfO5ZVAWQ +JHAdP4il9IraMQo9ugA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+5bHbPaNGr3Uj/r2wrrvAdjbe2uIyAmW8kP65l5Aw1cHh3GNbc23oSfE7yVBeoEHo +QVc4GX5waiqtgfjhbb8u+KD+26y3W6wq0clTseTq1tw0qWU3uCJbG/4//nbtf6/e +hoR7gux+YRliEqAkVhIQFMtMQNSViAx5liPIwklZ68qKmHQ779C1OUqBwoYSHRUg +JzWCLmcAjd1v16na+CRjpyPi7zdEeC2JJq7Aq6Kd6z8mxbHAwd03ZkFaf8NRukh6 +B1b4G2AwQVFMBKCHV/bourQgmc9HbxquT7BOMQrMtXcCJZ3d8S/xaMqUdgtJcJvY +HLJ2pchHH/14crDeE2eUobRt50LEC8aoqn+LeCtJtM8AW1qnPoVUR8ng/jL5ABqw ++bEdTualgsLaBay+4VwoaypxAFh1WZKew+2mkoANLFtWWfOB/HazNh442iOsv5g2 +ptRDth1SZsFhKa0ovi88avQji3zBQWoz7lp9L4W7VR2Y8bR33KHG2T9sTdv/+rxG +K2EdyPkehQ1J4kxpT5WkMQ== +-----END ENCRYPTED PRIVATE KEY----- diff --git a/library/connection.c b/library/connection.c index 2219009..4ae2964 100644 --- a/library/connection.c +++ b/library/connection.c @@ -34,10 +34,119 @@ #include #include #include +#include + #include "../ftpData.h" #include "connection.h" + + +int socketPrintf(ftpDataType * ftpData, int clientId, const char *__restrict __fmt, ...) +{ + #define SOCKET_PRINTF_BUFFER 2048 + + int bytesWritten = 0; + char theBuffer[2048]; + int theStringSize = 0; + memset(&theBuffer, 0, SOCKET_PRINTF_BUFFER); + printf("\nWriting to socket id %d: ", clientId); + + va_list args; + va_start(args, __fmt); + //pthread_mutex_lock(&ftpData->clients[clientId].writeMutex); + + while (*__fmt != '\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; + } + + if (theStringSize >= SOCKET_PRINTF_BUFFER) { + printf("\n String buffer is full!"); + } + else if (theStringSize < SOCKET_PRINTF_BUFFER && + theStringSize > 0) + { + int theReturnCode = 0; + theReturnCode = write(ftpData->clients[clientId].socketDescriptor, theBuffer, theStringSize); + printf("%s", theBuffer); + + if (theReturnCode > 0) + { + bytesWritten += theReturnCode; + } + else + { + bytesWritten = theReturnCode; + break; + } + } + else if(theStringSize == 0) + { + printf("\n Nothing to write.. "); + } + + ++__fmt; + } + + //pthread_mutex_unlock(&ftpData->clients[clientId].writeMutex); + va_end(args); + return bytesWritten; +} + + /* Return the higher socket available*/ int getMaximumSocketFd(int mainSocket, ftpDataType * ftpData) { @@ -381,12 +490,16 @@ int evaluateClientSocketConnection(ftpDataType * ftpData) if (ftpData->ftpParameters.maximumConnectionsPerIp > 0 && numberOfConnectionFromSameIp >= ftpData->ftpParameters.maximumConnectionsPerIp) { - dprintf(ftpData->clients[availableSocketIndex].socketDescriptor, "530 too many connection from your ip address %s \r\n", ftpData->clients[availableSocketIndex].clientIpAddress); + int theReturnCode = socketPrintf(ftpData, availableSocketIndex, "sss", "530 too many connection from your ip address ", ftpData->clients[availableSocketIndex].clientIpAddress, " \r\n"); ftpData->clients[availableSocketIndex].closeTheClient = 1; } else { - write(ftpData->clients[availableSocketIndex].socketDescriptor, ftpData->welcomeMessage, strlen(ftpData->welcomeMessage)); + int returnCode = socketPrintf(&ftpData, availableSocketIndex, "s", ftpData->welcomeMessage); + if (returnCode <= 0) + { + ftpData->clients[availableSocketIndex].closeTheClient = 1; + } } return 1; diff --git a/library/connection.h b/library/connection.h index 2382ce7..c66deb4 100644 --- a/library/connection.h +++ b/library/connection.h @@ -48,7 +48,7 @@ int selectWait(ftpDataType * ftpData); int isClientConnected(ftpDataType * ftpData, int cliendId); int getAvailableClientSocketIndex(ftpDataType * ftpData); int evaluateClientSocketConnection(ftpDataType * ftpData); - +int socketPrintf(ftpDataType * ftpData, int clientId, const char *__restrict __fmt, ...); #ifdef __cplusplus diff --git a/todo.txt b/todo.txt index e69de29..0a37e9c 100644 --- a/todo.txt +++ b/todo.txt @@ -0,0 +1,3 @@ +To remove key files: +openssl rsa -in key.pem -out newkey.pem +