Daemon mode, single instance check, site chmod command added

This commit is contained in:
Ugo Cirmignani
2018-02-11 16:22:00 +01:00
parent f73003c1fe
commit 63b9916fe7
35 changed files with 791 additions and 70 deletions

Binary file not shown.

Binary file not shown.

View File

@ -1,7 +1,8 @@
build/Debug/GNU-Linux/ftpServer.o: ftpServer.c library/dynamicVectors.h \
library/fileManagement.h library/dynamicVectors.h ftpServer.h ftpData.h \
library/dynamicVectors.h library/configRead.h library/dynamicVectors.h \
ftpCommandsElaborate.h library/fileManagement.h library/logFunctions.h
ftpCommandsElaborate.h library/fileManagement.h library/logFunctions.h \
library/daemon.h
library/dynamicVectors.h:
@ -24,3 +25,5 @@ ftpCommandsElaborate.h:
library/fileManagement.h:
library/logFunctions.h:
library/daemon.h:

Binary file not shown.

View File

@ -0,0 +1,11 @@
build/Debug/GNU-Linux/library/daemon.o: library/daemon.c \
library/dynamicVectors.h library/fileManagement.h \
library/dynamicVectors.h library/fileManagement.h
library/dynamicVectors.h:
library/fileManagement.h:
library/dynamicVectors.h:
library/fileManagement.h:

Binary file not shown.

View File

@ -4,16 +4,24 @@
# SERVER SETTINGS #
#######################################################
MAXIMUM_ALLOWED_FTP_CONNECTION = 10
MAXIMUM_ALLOWED_FTP_CONNECTION = 30
#SET THE MAXIMUM CONCURRENT ALLOWED CONNECTIONS ON THE SERVER
FTP_PORT = 21
#SET THE SERVER PORT DEFAULT 21
FTP_SERVER_IP = 192.168.1.237
#BIND THE SERVER TO SPECIFIC ADDRESS
#NOT YET IMPLEMENTED
#FTP_SERVER_IP = 192.168.1.237
#Allow only one server instance
SINGLE_INSTANCE = false
#Run in background, daemon mode ok
DAEMON_MODE = false
#USERS
#START FROM ZERO TO XXX
#START FROM USER 0 TO XXX
USER_0 = ugo
PASSWORD_0 = pass
HOME_0 = /

Binary file not shown.

View File

@ -1,9 +1,29 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
* The MIT License
*
* Copyright 2018 Ugo Cirmignani.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
@ -47,6 +67,35 @@ int parseCommandUser(clientDataType *theClientData)
}
}
/* Elaborate the User login command */
int parseCommandSite(clientDataType *theClientData)
{
char *theCommand;
theCommand = getFtpCommandArg("SITE", theClientData->theCommandReceived);
printTimeStamp();
printf("\ntheCommand:%s", theCommand);
if(strncmp(theCommand, "CHMOD", strlen("CHMOD")) == 0 ||
strncmp(theCommand, "chmod", strlen("chmod")) == 0)
{
setPermissions(theCommand, theClientData->login.absolutePath.text);
char *theResponse = "200 Permissions changed\r\n";
write(theClientData->socketDescriptor, theResponse, strlen(theResponse));
}
else
{
char *theResponse = "500 unknown extension\r\n";
write(theClientData->socketDescriptor, theResponse, strlen(theResponse));
}
//site chmod 777 test
//200 Permissions changed on test
//500 SITE ciao is an unknown extension
}
int parseCommandPass(ftpDataType * data, int socketId)
{
char *thePass;
@ -969,4 +1018,68 @@ int getFtpCommandArgWithOptions(char * theCommand, char *theCommandString, ftpCo
return 1;
}
int setPermissions(char * permissionsCommand, char * basePath)
{
#define STATUS_INCREASE 0
#define STATUS_PERMISSIONS 1
#define STATUS_LOCAL_PATH 2
int permissionsCommandCursor = 0;
int status = STATUS_INCREASE;
char thePermissionString[1024];
char theLocalPath[1024];
char theFinalCommand[2048];
memset(theLocalPath, 0, 1024);
memset(thePermissionString, 0, 1024);
memset(theFinalCommand, 0, 2048);
int thePermissionStringCursor = 0, theLocalPathCursor = 0;
while (permissionsCommand[permissionsCommandCursor] != '\r' &&
permissionsCommand[permissionsCommandCursor] != '\n' &&
permissionsCommand[permissionsCommandCursor] != '\0')
{
switch (status)
{
case STATUS_INCREASE:
if (permissionsCommandCursor == strlen("chmod"))
{
status = STATUS_PERMISSIONS;
}
break;
case STATUS_PERMISSIONS:
if (permissionsCommand[permissionsCommandCursor] == ' ')
{
status = STATUS_LOCAL_PATH;
break;
}
if (thePermissionStringCursor < 1024 )
thePermissionString[thePermissionStringCursor++] = permissionsCommand[permissionsCommandCursor];
break;
case STATUS_LOCAL_PATH:
if (theLocalPathCursor < 1024)
theLocalPath[theLocalPathCursor++] = permissionsCommand[permissionsCommandCursor];
break;
}
permissionsCommandCursor++;
}
printf("\n thePermissionString = %s ", thePermissionString);
printf("\n theLocalPathCursor = %s ", theLocalPath);
if (basePath[strlen(basePath)-1] != '/')
sprintf(theFinalCommand, "chmod %s %s/%s", thePermissionString, basePath, theLocalPath);
else
sprintf(theFinalCommand, "chmod %s %s%s", thePermissionString, basePath, theLocalPath);
printf("\n theFinalCommand = %s ", theFinalCommand);
system(theFinalCommand);
return 1;
}

View File

@ -1,3 +1,29 @@
/*
* The MIT License
*
* Copyright 2018 Ugo Cirmignani.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
@ -24,6 +50,7 @@ extern "C" {
/* Elaborate the User login command */
int parseCommandUser(clientDataType *theClientData);
int parseCommandSite(clientDataType *theClientData);
int parseCommandPass(ftpDataType * data, int socketId);
int parseCommandAuth(clientDataType *theClientData);
int parseCommandPwd(clientDataType *theClientData);
@ -56,6 +83,7 @@ int parseCommandRnto(clientDataType *theClientData);
int writeRetrFile(char * theFilename, int thePasvSocketConnection, int startFrom);
char *getFtpCommandArg(char * theCommand, char *theCommandString);
int getFtpCommandArgWithOptions(char * theCommand, char *theCommandString, ftpCommandDataType *ftpCommand);
int setPermissions(char * permissionsCommand, char * basePath);
#ifdef __cplusplus
}

View File

@ -1,9 +1,29 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
* The MIT License
*
* Copyright 2018 Ugo Cirmignani.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

View File

@ -1,3 +1,29 @@
/*
* The MIT License
*
* Copyright 2018 Ugo Cirmignani.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
@ -124,6 +150,7 @@ struct clientData
struct ftpData
{
int connectedClients;
char welcomeMessage[1024];
int theSocket;
clientDataType *clients;

View File

@ -1,9 +1,29 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
* The MIT License
*
* Copyright 2018 Ugo Cirmignani.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
@ -26,7 +46,7 @@
#include "library/fileManagement.h"
#include "library/logFunctions.h"
#include "library/configRead.h"
#include "library/daemon.h"
/* Catch Signal Handler functio */
void signal_callback_handler(int signum){
@ -317,35 +337,56 @@ void *pasvThreadHandler(void * socketId)
void runFtpServer(void)
{
DYNV_VectorGenericDataType configParameters;
fd_set rset, wset, eset, rsetAll, wsetAll, esetAll;
static int processingSock = 0;
static int maxSocketFD = 0;
DYNV_VectorGeneric_Init(&configParameters);
readConfigurationFile("./config.cfg", &configParameters);
parseConfigurationFile(&ftpData.ftpParameters, &configParameters);
initFtpData();
//Socket main creator
ftpData.theSocket = createSocket(ftpData.ftpParameters.port);
printTimeStamp();
printf("uFTP server starting..");
printf("\nServer port: %d", ftpData.ftpParameters.port);
printf("\nServer: Clients connected: %d", ftpData.connectedClients);
printf("\nServer: Max Client Allowed: %d", ftpData.ftpParameters.maxClients);
FD_ZERO(&rset);
FD_ZERO(&wset);
FD_ZERO(&eset);
FD_ZERO(&rsetAll);
FD_ZERO(&wsetAll);
FD_ZERO(&esetAll);
DYNV_VectorGenericDataType configParameters;
fd_set rset, wset, eset, rsetAll, wsetAll, esetAll;
static int processingSock = 0;
static int maxSocketFD = 0;
FD_SET(ftpData.theSocket, &rsetAll);
FD_SET(ftpData.theSocket, &wsetAll);
FD_SET(ftpData.theSocket, &esetAll);
DYNV_VectorGeneric_Init(&configParameters);
readConfigurationFile("./config.cfg", &configParameters);
parseConfigurationFile(&ftpData.ftpParameters, &configParameters);
if (ftpData.ftpParameters.singleInstanceModeOn == 1)
{
int returnCode = isProcessAlreadyRunning();
printf("\nreturnCode : %d", returnCode);
if (returnCode == 1)
{
printf("\nThe process is already running..");
exit(0);
}
}
/* Fork the process daemon mode */
if (ftpData.ftpParameters.daemonModeOn == 1)
{
daemonize("uFTP");
}
initFtpData();
//Socket main creator
ftpData.theSocket = createSocket(ftpData.ftpParameters.port);
printTimeStamp();
printf("uFTP server starting..");
printf("\nServer port: %d", ftpData.ftpParameters.port);
printf("\nServer: Clients connected: %d", ftpData.connectedClients);
printf("\nServer: Max Client Allowed: %d", ftpData.ftpParameters.maxClients);
FD_ZERO(&rset);
FD_ZERO(&wset);
FD_ZERO(&eset);
FD_ZERO(&rsetAll);
FD_ZERO(&wsetAll);
FD_ZERO(&esetAll);
FD_SET(ftpData.theSocket, &rsetAll);
FD_SET(ftpData.theSocket, &wsetAll);
FD_SET(ftpData.theSocket, &esetAll);
maxSocketFD = ftpData.theSocket+1;
//Endless loop ftp process
@ -360,7 +401,6 @@ void runFtpServer(void)
wset = wsetAll;
eset = esetAll;
//printf("\n\nSelect will wait for socket data.. ");
if (ftpData.connectedClients < ftpData.ftpParameters.maxClients)
{
printf("\nServer: Clients connected: %d", ftpData.connectedClients);
@ -380,9 +420,10 @@ void runFtpServer(void)
for (processingSock = 0; processingSock < ftpData.ftpParameters.maxClients; processingSock++)
{
if (ftpData.clients[processingSock].closeTheClient == 1)
{
printf("\nQUIT FLAG SET!\n");
if (ftpData.clients[processingSock].pasvData.threadIsAlive == 1)
{
void *pReturn;
@ -403,7 +444,6 @@ void runFtpServer(void)
continue;
}
if (ftpData.clients[processingSock].socketIsConnected == 0 &&
FD_ISSET(ftpData.theSocket, &rset))
{
@ -452,7 +492,7 @@ void runFtpServer(void)
else
{
printf("\nErrno = %d", errno);
perror("Error: ");
//perror("Error: ");
/*
int readen = 0;
@ -556,7 +596,7 @@ void runFtpServer(void)
closeSocket(ftpData.theSocket);
printTimeStamp();
printf("Server: Closed.");
ftpData.clients[processingSock].socketIsConnected = 0;
return;
}
@ -692,7 +732,9 @@ static int processCommand(int processingElement)
(strncmp(ftpData.clients[processingElement].theCommandReceived, "USER", strlen("USER")) != 0 &&
strncmp(ftpData.clients[processingElement].theCommandReceived, "user", strlen("user")) != 0 &&
strncmp(ftpData.clients[processingElement].theCommandReceived, "PASS", strlen("PASS")) != 0 &&
strncmp(ftpData.clients[processingElement].theCommandReceived, "pass", strlen("pass")) != 0))
strncmp(ftpData.clients[processingElement].theCommandReceived, "pass", strlen("pass")) != 0 &&
strncmp(ftpData.clients[processingElement].theCommandReceived, "QUIT", strlen("QUIT")) != 0 &&
strncmp(ftpData.clients[processingElement].theCommandReceived, "quit", strlen("quit")) != 0))
{
toReturn = notLoggedInMessage(&ftpData.clients[processingElement]);
return 1;
@ -712,6 +754,12 @@ static int processCommand(int processingElement)
printf("\nPASS COMMAND RECEIVED");
toReturn = parseCommandPass(&ftpData, processingElement);
}
else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "SITE", strlen("SITE")) == 0 ||
strncmp(ftpData.clients[processingElement].theCommandReceived, "site", strlen("site")) == 0)
{
printf("\nSITE COMMAND RECEIVED");
toReturn = parseCommandSite(&ftpData.clients[processingElement]);
}
else if(strncmp(ftpData.clients[processingElement].theCommandReceived, "AUTH", strlen("AUTH")) == 0 ||
strncmp(ftpData.clients[processingElement].theCommandReceived, "auth", strlen("auth")) == 0)
{

View File

@ -1,3 +1,29 @@
/*
* The MIT License
*
* Copyright 2018 Ugo Cirmignani.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates

View File

@ -1,3 +1,29 @@
/*
* The MIT License
*
* Copyright 2018 Ugo Cirmignani.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/*
USER <SP> <username> <CRLF>
PASS <SP> <password> <CRLF>

View File

@ -1,3 +1,29 @@
/*
* The MIT License
*
* Copyright 2018 Ugo Cirmignani.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -241,6 +267,32 @@ int parseConfigurationFile(ftpParameters_DataType *ftpParameters, DYNV_VectorGen
}
ftpParameters->daemonModeOn = 0;
searchIndex = searchParameter("DAEMON_MODE", parametersVector);
if (searchIndex != -1)
{
if(strcmp(((parameter_DataType *) parametersVector->Data[searchIndex])->value, "true") == 0)
ftpParameters->daemonModeOn = 1;
if(strcmp(((parameter_DataType *) parametersVector->Data[searchIndex])->value, "TRUE") == 0)
ftpParameters->daemonModeOn = 1;
}
ftpParameters->singleInstanceModeOn = 0;
searchIndex = searchParameter("SINGLE_INSTANCE", parametersVector);
if (searchIndex != -1)
{
if(strcmp(((parameter_DataType *) parametersVector->Data[searchIndex])->value, "true") == 0)
ftpParameters->singleInstanceModeOn = 1;
if(strcmp(((parameter_DataType *) parametersVector->Data[searchIndex])->value, "TRUE") == 0)
ftpParameters->singleInstanceModeOn = 1;
}
printf("\nFtp singleInstanceModeOn: %d", ftpParameters->singleInstanceModeOn);
printf("\nFtp daemonModeOn: %d", ftpParameters->daemonModeOn);
searchIndex = searchParameter("FTP_SERVER_IP", parametersVector);
if (searchIndex != -1)
{

View File

@ -1,7 +1,7 @@
/*
* The MIT License
*
* Copyright 2018 ugo.
* Copyright 2018 Ugo Cirmignani.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@ -22,13 +22,6 @@
* THE SOFTWARE.
*/
/*
* File: configRead.h
* Author: ugo
*
* Created on 24 gennaio 2018, 14.11
*/
#ifndef CONFIGREAD_H
#define CONFIGREAD_H
@ -56,6 +49,8 @@ struct ftpParameters
int ftpIpAddress[4];
int port;
int maxClients;
int daemonModeOn;
int singleInstanceModeOn;
DYNV_VectorGenericDataType usersVector;
} typedef ftpParameters_DataType;

136
library/daemon.c Normal file
View File

@ -0,0 +1,136 @@
/*
* The MIT License
*
* Copyright 2018 Ugo Cirmignani.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <syslog.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/resource.h>
#include <signal.h>
#include "fileManagement.h"
#define LOCKFILE "/var/run/uFTP.pid"
#define LOCKMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
int isProcessAlreadyRunning(void)
{
int fd;
int returnCode;
char buf[16];
fd = open(LOCKFILE, O_RDWR|O_CREAT, LOCKMODE);
if (fd < 0)
{
syslog(LOG_ERR, "cant open %s: %s", LOCKFILE, strerror(errno));
exit(1);
}
printf("\nFile pid opened.");
if (returnCode = FILE_LockFile(fd) < 0)
{
if (errno == EACCES || errno == EAGAIN)
{
close(fd);
return(1);
}
syslog(LOG_ERR, "cant lock %s: %s", LOCKFILE, strerror(errno));
exit(1);
}
printf("\nFILE_LockFile returnCode = %d", returnCode);
ftruncate(fd, 0);
sprintf(buf, "%ld", (long)getpid());
write(fd, buf, strlen(buf)+1);
return(0);
}
void daemonize(const char *cmd)
{
int
i, fd0, fd1, fd2;
pid_t pid;
struct rlimit rl;
struct sigaction sa;
/*
* Clear file creation mask.
*/
umask(0);
/*
* Get maximum number of file descriptors.
*/
if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
printf("%s: cant get file limit", cmd);
/*
* Become a session leader to lose controlling TTY.
*/
if ((pid = fork()) < 0)
printf("%s: cant fork", cmd);
else if (pid != 0) /* parent */
exit(0);
setsid();
/*
* Ensure future opens wont allocate controlling TTYs.
*/
sa.sa_handler = SIG_IGN;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
if (sigaction(SIGHUP, &sa, NULL) < 0)
printf("%s: cant ignore SIGHUP", cmd);
if ((pid = fork()) < 0)
printf("%s: cant fork", cmd);
else if (pid != 0) /* parent */
exit(0);
/*
* Change the current working directory to the root so
* we wont prevent file systems from being unmounted.
*/
if (chdir("/") < 0)
printf("%s: cant change directory to /", cmd);
/*
* Close all open file descriptors.
*/
if (rl.rlim_max == RLIM_INFINITY)
rl.rlim_max = 1024;
for (i = 0; i < rl.rlim_max; i++)
close(i);
/*
* Attach file descriptors 0, 1, and 2 to /dev/null.
*/
fd0 = open("/dev/null", O_RDWR);
fd1 = dup(0);
fd2 = dup(0);
}

40
library/daemon.h Normal file
View File

@ -0,0 +1,40 @@
/*
* The MIT License
*
* Copyright 2018 Ugo Cirmignani.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef DAEMON_H
#define DAEMON_H
#ifdef __cplusplus
extern "C" {
#endif
int isProcessAlreadyRunning(void);
void daemonize(const char *cmd);
#ifdef __cplusplus
}
#endif
#endif /* DAEMON_H */

View File

@ -1,3 +1,29 @@
/*
* The MIT License
*
* Copyright 2018 Ugo Cirmignani.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

View File

@ -1,3 +1,26 @@
/*
* The MIT License
*
* Copyright 2018 Ugo Cirmignani.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef DYNAMIC_VECTORS_H
#define DYNAMIC_VECTORS_H

View File

@ -1,3 +1,29 @@
/*
* The MIT License
*
* Copyright 2018 Ugo Cirmignani.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <pwd.h>
#include <grp.h>
#include <stdio.h>
@ -6,6 +32,8 @@
#include <dirent.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <unistd.h>
#include <fcntl.h>
#include "fileManagement.h"
#include "dynamicVectors.h"
@ -503,3 +531,14 @@ void FILE_DirectoryToParent(char ** sourceString)
(*sourceString)[theNewSize] = '\0';
}
}
int FILE_LockFile(int fd)
{
struct flock fl;
fl.l_type = F_WRLCK;
fl.l_start = 0;
fl.l_whence = SEEK_SET;
fl.l_len = 0;
return(fcntl(fd, F_SETLK, &fl));
}

View File

@ -1,3 +1,27 @@
/*
* The MIT License
*
* Copyright 2018 Ugo Cirmignani.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef GEN_FILE_MANAGEMENT_TYPES
#include <stdio.h>
@ -40,5 +64,6 @@
time_t FILE_GetLastModifiedData(char *path);
void FILE_AppendToString(char ** sourceString, char *theString);
void FILE_DirectoryToParent(char ** sourceString);
int FILE_LockFile(int fd);
#define GEN_FILE_MANAGEMENT_TYPES
#endif

View File

@ -1,9 +1,29 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
* The MIT License
*
* Copyright 2018 Ugo Cirmignani.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <time.h>
#include "logFunctions.h"
#include <sys/time.h>

View File

@ -1,14 +1,25 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: logFunctions.h
* Author: ugo
* The MIT License
*
* Created on 20 ottobre 2017, 21.41
* Copyright 2018 Ugo Cirmignani.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef LOGFUNCTIONS_H

View File

@ -39,6 +39,7 @@ OBJECTFILES= \
${OBJECTDIR}/ftpData.o \
${OBJECTDIR}/ftpServer.o \
${OBJECTDIR}/library/configRead.o \
${OBJECTDIR}/library/daemon.o \
${OBJECTDIR}/library/dynamicVectors.o \
${OBJECTDIR}/library/fileManagement.o \
${OBJECTDIR}/library/logFunctions.o \
@ -89,6 +90,11 @@ ${OBJECTDIR}/library/configRead.o: library/configRead.c
${RM} "$@.d"
$(COMPILE.c) -g -Ilibrary -include library/dynamicVectors.h -include library/fileManagement.h -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/library/configRead.o library/configRead.c
${OBJECTDIR}/library/daemon.o: library/daemon.c
${MKDIR} -p ${OBJECTDIR}/library
${RM} "$@.d"
$(COMPILE.c) -g -Ilibrary -include library/dynamicVectors.h -include library/fileManagement.h -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/library/daemon.o library/daemon.c
${OBJECTDIR}/library/dynamicVectors.o: library/dynamicVectors.c
${MKDIR} -p ${OBJECTDIR}/library
${RM} "$@.d"

View File

@ -39,6 +39,7 @@ OBJECTFILES= \
${OBJECTDIR}/ftpData.o \
${OBJECTDIR}/ftpServer.o \
${OBJECTDIR}/library/configRead.o \
${OBJECTDIR}/library/daemon.o \
${OBJECTDIR}/library/dynamicVectors.o \
${OBJECTDIR}/library/fileManagement.o \
${OBJECTDIR}/library/logFunctions.o \
@ -89,6 +90,11 @@ ${OBJECTDIR}/library/configRead.o: library/configRead.c
${RM} "$@.d"
$(COMPILE.c) -O2 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/library/configRead.o library/configRead.c
${OBJECTDIR}/library/daemon.o: library/daemon.c
${MKDIR} -p ${OBJECTDIR}/library
${RM} "$@.d"
$(COMPILE.c) -O2 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/library/daemon.o library/daemon.c
${OBJECTDIR}/library/dynamicVectors.o: library/dynamicVectors.c
${MKDIR} -p ${OBJECTDIR}/library
${RM} "$@.d"

View File

@ -10,6 +10,7 @@
<itemPath>library/fileManagement.h</itemPath>
<itemPath>library/logFunctions.h</itemPath>
</logicalFolder>
<itemPath>library/daemon.h</itemPath>
<itemPath>ftpCommandsElaborate.h</itemPath>
<itemPath>ftpData.h</itemPath>
<itemPath>ftpServer.h</itemPath>
@ -24,6 +25,7 @@
projectFiles="true">
<logicalFolder name="library" displayName="library" projectFiles="true">
<itemPath>library/configRead.c</itemPath>
<itemPath>library/daemon.c</itemPath>
<itemPath>library/dynamicVectors.c</itemPath>
<itemPath>library/fileManagement.c</itemPath>
<itemPath>library/logFunctions.c</itemPath>
@ -94,6 +96,10 @@
</item>
<item path="library/configRead.h" ex="false" tool="3" flavor2="0">
</item>
<item path="library/daemon.c" ex="false" tool="0" flavor2="0">
</item>
<item path="library/daemon.h" ex="false" tool="3" flavor2="0">
</item>
<item path="library/dynamicVectors.c" ex="false" tool="0" flavor2="0">
</item>
<item path="library/dynamicVectors.h" ex="false" tool="3" flavor2="0">
@ -153,6 +159,10 @@
</item>
<item path="library/configRead.h" ex="false" tool="3" flavor2="0">
</item>
<item path="library/daemon.c" ex="false" tool="0" flavor2="0">
</item>
<item path="library/daemon.h" ex="false" tool="3" flavor2="0">
</item>
<item path="library/dynamicVectors.c" ex="false" tool="0" flavor2="0">
</item>
<item path="library/dynamicVectors.h" ex="false" tool="3" flavor2="0">

View File

@ -13,8 +13,6 @@
<gdb_interceptlist>
<gdbinterceptoptions gdb_all="false" gdb_unhandled="true" gdb_unexpected="true"/>
</gdb_interceptlist>
<gdb_signals>
</gdb_signals>
<gdb_options>
<DebugOptions>
</DebugOptions>

24
uFTP.c
View File

@ -1,3 +1,27 @@
/*
* The MIT License
*
* Copyright 2018 Ugo Cirmignani.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>