working on permissions

This commit is contained in:
Ugo Cirmignani
2018-12-31 00:10:27 +01:00
parent b2684cb503
commit 2b74b9f9a3
12 changed files with 190 additions and 37 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -167,6 +167,8 @@ int parseCommandPass(ftpDataType * data, int socketId)
returnCode = socketPrintf(data, socketId, "s", "230 Login Ok.\r\n"); returnCode = socketPrintf(data, socketId, "s", "230 Login Ok.\r\n");
if (returnCode <= 0) if (returnCode <= 0)
return FTP_COMMAND_PROCESSED_WRITE_ERROR; return FTP_COMMAND_PROCESSED_WRITE_ERROR;
return 1;
} }
@ -205,6 +207,11 @@ 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.ownerShip.uid = ((usersParameters_DataType *) data->ftpParameters.usersVector.Data[searchUserNameIndex])->ownerShip.uid;
data->clients[socketId].login.userLoggedIn = 1; data->clients[socketId].login.userLoggedIn = 1;
printf("\ndata->clients[socketId].login.ownerShip.ownerShipSet = %d", data->clients[socketId].login.ownerShip.ownerShipSet);
printf("\ndata->clients[socketId].login.ownerShip.gid = %d", data->clients[socketId].login.ownerShip.gid);
printf("\ndata->clients[socketId].login.ownerShip.uid = %d", data->clients[socketId].login.ownerShip.uid);
returnCode = socketPrintf(data, socketId, "s", "230 Login Ok.\r\n"); returnCode = socketPrintf(data, socketId, "s", "230 Login Ok.\r\n");
if (returnCode <= 0) if (returnCode <= 0)
return FTP_COMMAND_PROCESSED_WRITE_ERROR; return FTP_COMMAND_PROCESSED_WRITE_ERROR;
@ -281,9 +288,11 @@ int parseCommandAuth(ftpDataType * data, int socketId)
int parseCommandPwd(ftpDataType * data, int socketId) int parseCommandPwd(ftpDataType * data, int socketId)
{ {
printf("\n pwd is %s", data->clients[socketId].login.ftpPath.text);
int returnCode; int returnCode;
returnCode = socketPrintf(data, socketId, "sss", "257 \"", data->clients[socketId].login.ftpPath.text ,"\" is your current location\r\n"); returnCode = socketPrintf(data, socketId, "sss", "257 \"", data->clients[socketId].login.ftpPath.text ,"\" is your current location\r\n");
if (returnCode <= 0) if (returnCode <= 0)
return FTP_COMMAND_PROCESSED_WRITE_ERROR; return FTP_COMMAND_PROCESSED_WRITE_ERROR;
@ -741,7 +750,7 @@ int parseCommandCwd(ftpDataType * data, int socketId)
if (isSafePath == 1) if (isSafePath == 1)
{ {
//printf("\n The Path requested for CWD IS:%s", theSafePath.text); printf("\n The Path requested for CWD IS:%s", theSafePath.text);
setDynamicStringDataType(&absolutePathPrevious, data->clients[socketId].login.absolutePath.text, data->clients[socketId].login.absolutePath.textLen, &data->clients[socketId].memoryTable); setDynamicStringDataType(&absolutePathPrevious, data->clients[socketId].login.absolutePath.text, data->clients[socketId].login.absolutePath.textLen, &data->clients[socketId].memoryTable);
setDynamicStringDataType(&ftpPathPrevious, data->clients[socketId].login.ftpPath.text, data->clients[socketId].login.ftpPath.textLen, &data->clients[socketId].memoryTable); setDynamicStringDataType(&ftpPathPrevious, data->clients[socketId].login.ftpPath.text, data->clients[socketId].login.ftpPath.textLen, &data->clients[socketId].memoryTable);
@ -779,15 +788,26 @@ int parseCommandCwd(ftpDataType * data, int socketId)
} }
} }
if (FILE_IsDirectory(data->clients[socketId].login.absolutePath.text) == 1) printf("\nCheck the directory: %s", data->clients[socketId].login.absolutePath.text);
if (FILE_IsDirectory(data->clients[socketId].login.absolutePath.text) == 1 )
{
if((checkUserFilePermissions(data->clients[socketId].login.absolutePath.text, data->clients[socketId].login.ownerShip.uid, data->clients[socketId].login.ownerShip.gid) & FILE_PERMISSION_R ) == FILE_PERMISSION_R)
{ {
returnCode = socketPrintf(data, socketId, "sss", "250 OK. Current directory is ", data->clients[socketId].login.ftpPath.text, "\r\n"); returnCode = socketPrintf(data, socketId, "sss", "250 OK. Current directory is ", data->clients[socketId].login.ftpPath.text, "\r\n");
} }
else else
{ {
returnCode = socketPrintf(data, socketId, "sss", "530 Can't change directory to ", data->clients[socketId].login.absolutePath.text, ": no permissions\r\n");
setDynamicStringDataType(&data->clients[socketId].login.absolutePath, absolutePathPrevious.text, absolutePathPrevious.textLen, &data->clients[socketId].memoryTable); setDynamicStringDataType(&data->clients[socketId].login.absolutePath, absolutePathPrevious.text, absolutePathPrevious.textLen, &data->clients[socketId].memoryTable);
setDynamicStringDataType(&data->clients[socketId].login.ftpPath, ftpPathPrevious.text, ftpPathPrevious.textLen, &data->clients[socketId].memoryTable); setDynamicStringDataType(&data->clients[socketId].login.ftpPath, ftpPathPrevious.text, ftpPathPrevious.textLen, &data->clients[socketId].memoryTable);
}
}
else
{
returnCode = socketPrintf(data, socketId, "sss", "530 Can't change directory to ", data->clients[socketId].login.absolutePath.text, ": No such file or directory\r\n"); returnCode = socketPrintf(data, socketId, "sss", "530 Can't change directory to ", data->clients[socketId].login.absolutePath.text, ": No such file or directory\r\n");
setDynamicStringDataType(&data->clients[socketId].login.absolutePath, absolutePathPrevious.text, absolutePathPrevious.textLen, &data->clients[socketId].memoryTable);
setDynamicStringDataType(&data->clients[socketId].login.ftpPath, ftpPathPrevious.text, ftpPathPrevious.textLen, &data->clients[socketId].memoryTable);
} }
cleanDynamicStringDataType(&absolutePathPrevious, 0, &data->clients[socketId].memoryTable); cleanDynamicStringDataType(&absolutePathPrevious, 0, &data->clients[socketId].memoryTable);
@ -940,6 +960,9 @@ int parseCommandDele(ftpDataType * data, int socketId)
{ {
//printf("\nThe file to delete is: %s", deleFileName.text); //printf("\nThe file to delete is: %s", deleFileName.text);
if (FILE_IsFile(deleFileName.text) == 1) if (FILE_IsFile(deleFileName.text) == 1)
{
if ((checkUserFilePermissions(deleFileName.text, data->clients[socketId].login.ownerShip.uid, data->clients[socketId].login.ownerShip.gid) & FILE_PERMISSION_W) == FILE_PERMISSION_W)
{ {
returnStatus = remove(deleFileName.text); returnStatus = remove(deleFileName.text);
@ -956,6 +979,17 @@ int parseCommandDele(ftpDataType * data, int socketId)
if (returnCode <= 0) if (returnCode <= 0)
functionReturnCode = FTP_COMMAND_PROCESSED_WRITE_ERROR; functionReturnCode = FTP_COMMAND_PROCESSED_WRITE_ERROR;
}
else
{
returnCode = socketPrintf(data, socketId, "sss", "550 Could not delete the file: ", theFileToDelete, " no permissions\r\n");
functionReturnCode = FTP_COMMAND_PROCESSED;
if (returnCode <= 0)
functionReturnCode = FTP_COMMAND_PROCESSED_WRITE_ERROR;
}
} }
else else
{ {
@ -1027,6 +1061,8 @@ int parseCommandRmd(ftpDataType * data, int socketId)
if (isSafePath == 1) if (isSafePath == 1)
{ {
if (FILE_IsDirectory(rmdFileName.text) == 1) if (FILE_IsDirectory(rmdFileName.text) == 1)
{
if ((checkUserFilePermissions(rmdFileName.text, data->clients[socketId].login.ownerShip.uid, data->clients[socketId].login.ownerShip.gid) & FILE_PERMISSION_W) == FILE_PERMISSION_W)
{ {
returnStatus = rmdir(rmdFileName.text); returnStatus = rmdir(rmdFileName.text);
@ -1045,6 +1081,15 @@ int parseCommandRmd(ftpDataType * data, int socketId)
functionReturnCode = FTP_COMMAND_PROCESSED_WRITE_ERROR; functionReturnCode = FTP_COMMAND_PROCESSED_WRITE_ERROR;
} }
else else
{
returnCode = socketPrintf(data, socketId, "s", "550 Could not delete the directory: No permissions\r\n");
functionReturnCode = FTP_COMMAND_PROCESSED;
if (returnCode <= 0)
functionReturnCode = FTP_COMMAND_PROCESSED_WRITE_ERROR;
}
}
else
{ {
returnCode = socketPrintf(data, socketId, "s", "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; functionReturnCode = FTP_COMMAND_PROCESSED;

View File

@ -379,6 +379,19 @@ void *connectionWorkerHandle(void * socketId)
else if (compareStringCaseInsensitive(ftpData.clients[theSocketId].workerData.theCommandReceived, "NLST", strlen("NLST")) == 1) else if (compareStringCaseInsensitive(ftpData.clients[theSocketId].workerData.theCommandReceived, "NLST", strlen("NLST")) == 1)
theCommandType = COMMAND_TYPE_NLST; theCommandType = COMMAND_TYPE_NLST;
if ((checkUserFilePermissions(ftpData.clients[theSocketId].listPath.text, ftpData.clients[theSocketId].login.ownerShip.uid, ftpData.clients[theSocketId].login.ownerShip.gid) & FILE_PERMISSION_R) != FILE_PERMISSION_R)
{
returnCode = socketPrintf(&ftpData, theSocketId, "s", "550 No permissions\r\n");
if (returnCode <= 0)
{
ftpData.clients[theSocketId].closeTheClient = 1;
printf("\n Closing the client 8");
pthread_exit(NULL);
}
break;
}
returnCode = socketPrintf(&ftpData, theSocketId, "s", "150 Accepted data connection\r\n"); returnCode = socketPrintf(&ftpData, theSocketId, "s", "150 Accepted data connection\r\n");
if (returnCode <= 0) if (returnCode <= 0)
{ {
@ -418,6 +431,18 @@ void *connectionWorkerHandle(void * socketId)
pthread_exit(NULL); pthread_exit(NULL);
} }
if ((checkUserFilePermissions(ftpData.clients[theSocketId].fileToRetr.text, ftpData.clients[theSocketId].login.ownerShip.uid, ftpData.clients[theSocketId].login.ownerShip.gid) & FILE_PERMISSION_R) != FILE_PERMISSION_R)
{
writeReturn = socketPrintf(&ftpData, theSocketId, "s", "550 no reading permission on the file\r\n");
if (writeReturn <= 0)
{
ftpData.clients[theSocketId].closeTheClient = 1;
printf("\n Closing the client 12");
pthread_exit(NULL);
}
break;
}
writenSize = writeRetrFile(&ftpData, theSocketId, ftpData.clients[theSocketId].workerData.retrRestartAtByte, ftpData.clients[theSocketId].workerData.theStorFile); writenSize = writeRetrFile(&ftpData, theSocketId, ftpData.clients[theSocketId].workerData.retrRestartAtByte, ftpData.clients[theSocketId].workerData.theStorFile);
ftpData.clients[theSocketId].workerData.retrRestartAtByte = 0; ftpData.clients[theSocketId].workerData.retrRestartAtByte = 0;
@ -683,7 +708,7 @@ static int processCommand(int processingElement)
{ {
int toReturn = 0; int toReturn = 0;
//printTimeStamp(); //printTimeStamp();
//printf ("Command received from (%d): %s", processingElement, ftpData.clients[processingElement].theCommandReceived); printf ("\nCommand received from (%d): %s", processingElement, ftpData.clients[processingElement].theCommandReceived);
cleanDynamicStringDataType(&ftpData.clients[processingElement].ftpCommand.commandArgs, 0, &ftpData.clients[processingElement].memoryTable); cleanDynamicStringDataType(&ftpData.clients[processingElement].ftpCommand.commandArgs, 0, &ftpData.clients[processingElement].memoryTable);
cleanDynamicStringDataType(&ftpData.clients[processingElement].ftpCommand.commandOps, 0, &ftpData.clients[processingElement].memoryTable); cleanDynamicStringDataType(&ftpData.clients[processingElement].ftpCommand.commandOps, 0, &ftpData.clients[processingElement].memoryTable);

View File

@ -78,14 +78,28 @@ void loginCheck(char *name, char *password, loginDataType *login, DYNMEM_MemoryT
if (pass == NULL) if (pass == NULL)
{ {
cleanLoginData(login, 0, &*memoryTable); login->userLoggedIn = 0;
return;
} }
else else
{ {
//printf("Authenticate with %s - %s through system\n", login, password); //printf("Authenticate with %s - %s through system\n", login, password);
setDynamicStringDataType(&login->name, name, strlen(name), &*memoryTable); setDynamicStringDataType(&login->name, name, strlen(name), &*memoryTable);
setDynamicStringDataType(&login->homePath, pass->pw_dir, strlen(pass->pw_dir), &*memoryTable); //setDynamicStringDataType(&login->homePath, pass->pw_dir, strlen(pass->pw_dir), &*memoryTable);
setDynamicStringDataType(&login->homePath, "/", 1, &*memoryTable);
setDynamicStringDataType(&login->absolutePath, pass->pw_dir, strlen(pass->pw_dir), &*memoryTable); setDynamicStringDataType(&login->absolutePath, pass->pw_dir, strlen(pass->pw_dir), &*memoryTable);
/*
if (login->homePath.text[login->homePath.textLen-1] != '/')
{
appendToDynamicStringDataType(&login->homePath, "/", 1, &*memoryTable);
}
if (login->absolutePath.text[login->absolutePath.textLen-1] != '/')
{
appendToDynamicStringDataType(&login->absolutePath, "/", 1, &*memoryTable);
}*/
setDynamicStringDataType(&login->ftpPath, "/", strlen("/"), &*memoryTable); setDynamicStringDataType(&login->ftpPath, "/", strlen("/"), &*memoryTable);
login->ownerShip.uid = pass->pw_gid; login->ownerShip.uid = pass->pw_gid;
@ -98,6 +112,8 @@ void loginCheck(char *name, char *password, loginDataType *login, DYNMEM_MemoryT
printf("\nDir: %s", pass->pw_dir); printf("\nDir: %s", pass->pw_dir);
printf("\nGid: %d", pass->pw_gid); printf("\nGid: %d", pass->pw_gid);
printf("\nUid: %d", pass->pw_uid); printf("\nUid: %d", pass->pw_uid);
printf("\nlogin->homePath.text: %s", login->homePath.text);
printf("\nlogin->absolutePath.text: %s", login->absolutePath.text);
} }
} }
else else

View File

@ -603,11 +603,25 @@ static int parseConfigurationFile(ftpParameters_DataType *ftpParameters, DYNV_Ve
userData.ownerShip.gid = FILE_getGID(userData.ownerShip.groupOwnerString); userData.ownerShip.gid = FILE_getGID(userData.ownerShip.groupOwnerString);
userData.ownerShip.uid = FILE_getUID(userData.ownerShip.userOwnerString); userData.ownerShip.uid = FILE_getUID(userData.ownerShip.userOwnerString);
if (userData.ownerShip.gid != -1 &&
userData.ownerShip.uid != -1)
{
userData.ownerShip.ownerShipSet = 1; userData.ownerShip.ownerShipSet = 1;
} }
else else
{ {
userData.ownerShip.gid = 0;
userData.ownerShip.uid = 0;
userData.ownerShip.ownerShipSet = 0; userData.ownerShip.ownerShipSet = 0;
}
}
else
{
userData.ownerShip.ownerShipSet = 0;
userData.ownerShip.gid = 0;
userData.ownerShip.uid = 0;
userData.ownerShip.groupOwnerString = NULL; userData.ownerShip.groupOwnerString = NULL;
userData.ownerShip.userOwnerString = NULL; userData.ownerShip.userOwnerString = NULL;
} }

View File

@ -561,6 +561,51 @@ char * FILE_GetListPermissionsString(char *file, DYNMEM_MemoryTable_DataType **
return modeval; return modeval;
} }
int checkUserFilePermissions(char *fileName, int uid, int gid)
{
if (uid == 0 || gid == 0)
{
return FILE_PERMISSION_RW;
}
static int init = 0;
if (init == 0)
{
}
init = 1;
int filePermissions = FILE_PERMISSION_NO_RW;
int returnCode = 0;
char *toReturn;
struct stat info;
if ((returnCode = stat(fileName, &info)) == -1)
{
return -1;
}
if (info.st_uid == uid ||
info.st_gid == gid)
{
filePermissions = FILE_PERMISSION_RW;
}
else
{
mode_t perm = info.st_mode;
if ((perm & S_IROTH))
filePermissions |= FILE_PERMISSION_R;
if ((perm & S_IWOTH))
filePermissions |= FILE_PERMISSION_W;
}
return filePermissions;
}
char * FILE_GetOwner(char *fileName, DYNMEM_MemoryTable_DataType **memoryTable) char * FILE_GetOwner(char *fileName, DYNMEM_MemoryTable_DataType **memoryTable)
{ {
int returnCode = 0; int returnCode = 0;

View File

@ -32,6 +32,13 @@
#define FILE_MAX_LINE_LENGHT 512 #define FILE_MAX_LINE_LENGHT 512
#define FILE_MAX_PAR_VAR_SIZE 256 #define FILE_MAX_PAR_VAR_SIZE 256
#define FILE_PERMISSION_NO_RW 0
#define FILE_PERMISSION_R 1
#define FILE_PERMISSION_W 2
#define FILE_PERMISSION_RW 3
typedef struct FILE_StringParameterDataStruct typedef struct FILE_StringParameterDataStruct
{ {
char Name[FILE_MAX_PAR_VAR_SIZE]; char Name[FILE_MAX_PAR_VAR_SIZE];
@ -73,5 +80,6 @@
gid_t FILE_getGID(const char *group_name); gid_t FILE_getGID(const char *group_name);
void FILE_checkAllOpenedFD(void); void FILE_checkAllOpenedFD(void);
int fd_is_valid(int fd); int fd_is_valid(int fd);
int checkUserFilePermissions(char *fileName, int uid, int gid);
#define GEN_FILE_MANAGEMENT_TYPES #define GEN_FILE_MANAGEMENT_TYPES
#endif #endif