Skip to content

Commit

Permalink
Add option to run operations as specified user
Browse files Browse the repository at this point in the history
If a user name is supplied, fork a child process that attempts to switch to
that user prior to performing the requested file operation.

Note that the process must have the CAP_SETGID capability for this to
succeed.
  • Loading branch information
matthewvogt committed Jul 14, 2016
1 parent 922370c commit d788f0c
Show file tree
Hide file tree
Showing 4 changed files with 295 additions and 43 deletions.
22 changes: 11 additions & 11 deletions src/fileengine.cpp
Expand Up @@ -76,9 +76,9 @@ bool FileEngine::busy() const
return m_fileWorker->isRunning();
}

void FileEngine::deleteFiles(QStringList fileNames)
void FileEngine::deleteFiles(QStringList fileNames, QString asUser)
{
m_fileWorker->startDeleteFiles(fileNames);
m_fileWorker->startDeleteFiles(fileNames, asUser);
}

void FileEngine::cutFiles(QStringList fileNames)
Expand Down Expand Up @@ -108,7 +108,7 @@ void FileEngine::copyFiles(QStringList fileNames)
}
}

void FileEngine::pasteFiles(QString destDirectory)
void FileEngine::pasteFiles(QString destDirectory, QString asUser)
{
if (m_clipboardFiles.isEmpty()) {
qmlInfo(this) << "Paste called with empty clipboard.";
Expand Down Expand Up @@ -149,11 +149,11 @@ void FileEngine::pasteFiles(QString destDirectory)
emit clipboardCountChanged();

if (m_clipboardContainsCopy) {
m_fileWorker->startCopyFiles(files, destDirectory);
m_fileWorker->startCopyFiles(files, destDirectory, asUser);
return;
}

m_fileWorker->startMoveFiles(files, destDirectory);
m_fileWorker->startMoveFiles(files, destDirectory, asUser);
}

void FileEngine::cancel()
Expand All @@ -170,21 +170,21 @@ bool FileEngine::exists(QString fileName)
return QFile::exists(fileName);
}

bool FileEngine::mkdir(QString path, QString name)
bool FileEngine::mkdir(QString path, QString name, QString asUser)
{
if (!m_fileWorker->mkdir(path, name)) {
if (!m_fileWorker->mkdir(path, name, asUser)) {
emit error(ErrorFolderCreationFailed, name);
return false;
}
return true;
}

bool FileEngine::rename(QString fullOldFileName, QString newName)
bool FileEngine::rename(QString fullOldFileName, QString newName, QString asUser)
{
QFileInfo fileInfo(fullOldFileName);
QDir dir = fileInfo.absoluteDir();
QString fullNewFileName = dir.absoluteFilePath(newName);
if (!m_fileWorker->rename(fullOldFileName, fullNewFileName)) {
if (!m_fileWorker->rename(fullOldFileName, fullNewFileName, asUser)) {
emit error(ErrorRenameFailed, fileInfo.fileName());
return false;
}
Expand All @@ -194,7 +194,7 @@ bool FileEngine::rename(QString fullOldFileName, QString newName)
bool FileEngine::chmod(QString path,
bool ownerRead, bool ownerWrite, bool ownerExecute,
bool groupRead, bool groupWrite, bool groupExecute,
bool othersRead, bool othersWrite, bool othersExecute)
bool othersRead, bool othersWrite, bool othersExecute, QString asUser)
{
QFileDevice::Permissions p;
if (ownerRead) p |= QFileDevice::ReadOwner;
Expand All @@ -206,7 +206,7 @@ bool FileEngine::chmod(QString path,
if (othersRead) p |= QFileDevice::ReadOther;
if (othersWrite) p |= QFileDevice::WriteOther;
if (othersExecute) p |= QFileDevice::ExeOther;
if (!m_fileWorker->setPermissions(path, p)) {
if (!m_fileWorker->setPermissions(path, p, asUser)) {
emit error(ErrorChmodFailed, path);
return false;
}
Expand Down
13 changes: 7 additions & 6 deletions src/fileengine.h
Expand Up @@ -65,7 +65,8 @@ class FileEngine : public QObject
ErrorCannotCopyIntoItself,
ErrorFolderCopyFailed,
ErrorFolderCreationFailed,
ErrorChmodFailed
ErrorChmodFailed,
ErrorUserChangeFailed
};

enum Mode {
Expand All @@ -84,22 +85,22 @@ class FileEngine : public QObject
// methods accessible from QML

// asynch methods send signals when done or error occurs
Q_INVOKABLE void deleteFiles(QStringList fileNames);
Q_INVOKABLE void deleteFiles(QStringList fileNames, QString asUser = QString());
Q_INVOKABLE void cutFiles(QStringList fileNames);
Q_INVOKABLE void copyFiles(QStringList fileNames);
Q_INVOKABLE void pasteFiles(QString destDirectory);
Q_INVOKABLE void pasteFiles(QString destDirectory, QString asUser = QString());

// cancel asynch methods
Q_INVOKABLE void cancel();

// synchronous methods
Q_INVOKABLE bool exists(QString fileName);
Q_INVOKABLE bool mkdir(QString path, QString name);
Q_INVOKABLE bool rename(QString fullOldFileName, QString newName);
Q_INVOKABLE bool mkdir(QString path, QString name, QString asUser = QString());
Q_INVOKABLE bool rename(QString fullOldFileName, QString newName, QString asUser = QString());
Q_INVOKABLE bool chmod(QString path,
bool ownerRead, bool ownerWrite, bool ownerExecute,
bool groupRead, bool groupWrite, bool groupExecute,
bool othersRead, bool othersWrite, bool othersExecute);
bool othersRead, bool othersWrite, bool othersExecute, QString asUser = QString());


signals:
Expand Down

0 comments on commit d788f0c

Please sign in to comment.