-
Notifications
You must be signed in to change notification settings - Fork 800
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
newly created folders will be read-only when needed #6343
Merged
mgallien
merged 10 commits into
master
from
feature/applyPermissionsFromServerToFolders
Mar 12, 2024
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9d6d289
correct category for some log output
mgallien e98c25a
switch to newer CI images
mgallien 969a873
update drone signature
mgallien b0a2d5f
adjust AppImage build script to the new build image
mgallien 5dfeb55
we require c++-17
mgallien 706d969
ensure we get proper logs for permissions automated tests
mgallien bf7f874
update automated test to work with read only folders
mgallien bbc976c
gather more information on exceptions that happen when running tests
mgallien 4844f32
newly created folders will be read-only when needed
mgallien ff9953b
make folder read-write before deleting it
mgallien File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
/* | ||
Check notice on line 1 in src/libsync/filesystem.cpp GitHub Actions / buildRun clang-format on src/libsync/filesystem.cpp
|
||
* Copyright (C) by Daniel Molkentin <[email protected]> | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
|
@@ -15,15 +15,20 @@ | |
#include "filesystem.h" | ||
|
||
#include "common/utility.h" | ||
#include "csync.h" | ||
#include "vio/csync_vio_local.h" | ||
#include "std/c_time.h" | ||
|
||
#include <QFile> | ||
#include <QFileInfo> | ||
#include <QDir> | ||
#include <QDirIterator> | ||
#include <QCoreApplication> | ||
|
||
#include "csync.h" | ||
#include "vio/csync_vio_local.h" | ||
#include "std/c_time.h" | ||
#ifdef Q_OS_WIN | ||
#include <securitybaseapi.h> | ||
#include <sddl.h> | ||
#endif | ||
|
||
namespace OCC { | ||
|
||
|
@@ -189,5 +194,173 @@ | |
return false; | ||
} | ||
|
||
bool FileSystem::setFolderPermissions(const QString &path, | ||
Check warning on line 197 in src/libsync/filesystem.cpp GitHub Actions / buildsrc/libsync/filesystem.cpp:197:18 [modernize-use-trailing-return-type]
|
||
FileSystem::FolderPermissions permissions) noexcept | ||
{ | ||
try { | ||
switch (permissions) { | ||
case OCC::FileSystem::FolderPermissions::ReadOnly: | ||
std::filesystem::permissions(path.toStdWString(), std::filesystem::perms::owner_write | std::filesystem::perms::group_write | std::filesystem::perms::others_write, std::filesystem::perm_options::remove); | ||
break; | ||
case OCC::FileSystem::FolderPermissions::ReadWrite: | ||
break; | ||
} | ||
} | ||
catch (const std::filesystem::filesystem_error &e) | ||
{ | ||
qCWarning(lcFileSystem()) << "exception when modifying folder permissions" << e.what() << e.path1().c_str() << e.path2().c_str(); | ||
return false; | ||
} | ||
|
||
#ifdef Q_OS_WIN | ||
SECURITY_INFORMATION info = DACL_SECURITY_INFORMATION; | ||
std::unique_ptr<char[]> securityDescriptor; | ||
auto neededLength = 0ul; | ||
|
||
if (!GetFileSecurityW(path.toStdWString().c_str(), info, nullptr, 0, &neededLength)) { | ||
const auto lastError = GetLastError(); | ||
if (lastError != ERROR_INSUFFICIENT_BUFFER) { | ||
qCWarning(lcFileSystem) << "error when calling GetFileSecurityW" << path << lastError; | ||
return false; | ||
} | ||
|
||
securityDescriptor.reset(new char[neededLength]); | ||
|
||
if (!GetFileSecurityW(path.toStdWString().c_str(), info, securityDescriptor.get(), neededLength, &neededLength)) { | ||
qCWarning(lcFileSystem) << "error when calling GetFileSecurityW" << path << GetLastError(); | ||
return false; | ||
} | ||
} | ||
|
||
int daclPresent = false, daclDefault = false; | ||
PACL resultDacl = nullptr; | ||
if (!GetSecurityDescriptorDacl(securityDescriptor.get(), &daclPresent, &resultDacl, &daclDefault)) { | ||
qCWarning(lcFileSystem) << "error when calling GetSecurityDescriptorDacl" << path << GetLastError(); | ||
return false; | ||
} | ||
if (!daclPresent) { | ||
qCWarning(lcFileSystem) << "error when calling DACL needed to set a folder read-only or read-write is missing" << path; | ||
return false; | ||
} | ||
|
||
PSID sid = nullptr; | ||
if (!ConvertStringSidToSidW(L"S-1-5-32-545", &sid)) | ||
{ | ||
qCWarning(lcFileSystem) << "error when calling ConvertStringSidToSidA" << path << GetLastError(); | ||
return false; | ||
} | ||
|
||
ACL_SIZE_INFORMATION aclSize; | ||
if (!GetAclInformation(resultDacl, &aclSize, sizeof(aclSize), AclSizeInformation)) { | ||
qCWarning(lcFileSystem) << "error when calling GetAclInformation" << path << GetLastError(); | ||
return false; | ||
} | ||
|
||
const auto newAclSize = aclSize.AclBytesInUse + sizeof(ACCESS_DENIED_ACE) + GetLengthSid(sid); | ||
qCDebug(lcFileSystem) << "allocated a new DACL object of size" << newAclSize; | ||
|
||
std::unique_ptr<ACL> newDacl{reinterpret_cast<PACL>(new char[newAclSize])}; | ||
if (!InitializeAcl(newDacl.get(), newAclSize, ACL_REVISION)) { | ||
const auto lastError = GetLastError(); | ||
if (lastError != ERROR_INSUFFICIENT_BUFFER) { | ||
qCWarning(lcFileSystem) << "insufficient memory error when calling InitializeAcl" << path; | ||
return false; | ||
} | ||
|
||
qCWarning(lcFileSystem) << "error when calling InitializeAcl" << path << lastError; | ||
return false; | ||
} | ||
|
||
if (permissions == FileSystem::FolderPermissions::ReadOnly) { | ||
qCInfo(lcFileSystem) << path << "will be read only"; | ||
if (!AddAccessDeniedAce(newDacl.get(), ACL_REVISION, FILE_WRITE_DATA | FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA | FILE_APPEND_DATA | FILE_DELETE_CHILD, sid)) { | ||
qCWarning(lcFileSystem) << "error when calling AddAccessDeniedAce << path" << GetLastError(); | ||
return false; | ||
} | ||
} | ||
|
||
for (int i = 0; i < aclSize.AceCount; ++i) { | ||
void *currentAce = nullptr; | ||
if (!GetAce(resultDacl, i, ¤tAce)) { | ||
qCWarning(lcFileSystem) << "error when calling GetAce" << path << GetLastError(); | ||
return false; | ||
} | ||
|
||
const auto currentAceHeader = reinterpret_cast<PACE_HEADER>(currentAce); | ||
mgallien marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (permissions == FileSystem::FolderPermissions::ReadWrite) { | ||
qCInfo(lcFileSystem) << path << "will be read write"; | ||
} | ||
if (permissions == FileSystem::FolderPermissions::ReadWrite && (ACCESS_DENIED_ACE_TYPE == (currentAceHeader->AceType & ACCESS_DENIED_ACE_TYPE))) { | ||
qCWarning(lcFileSystem) << "AceHeader" << path << currentAceHeader->AceFlags << currentAceHeader->AceSize << currentAceHeader->AceType; | ||
continue; | ||
} | ||
|
||
if (!AddAce(newDacl.get(), ACL_REVISION, i + 1, currentAce, currentAceHeader->AceSize)) { | ||
const auto lastError = GetLastError(); | ||
if (lastError != ERROR_INSUFFICIENT_BUFFER) { | ||
qCWarning(lcFileSystem) << "insufficient memory error when calling AddAce" << path; | ||
return false; | ||
} | ||
|
||
if (lastError != ERROR_INVALID_PARAMETER) { | ||
qCWarning(lcFileSystem) << "invalid parameter error when calling AddAce" << path << "ACL size" << newAclSize; | ||
return false; | ||
} | ||
|
||
qCWarning(lcFileSystem) << "error when calling AddAce" << path << lastError << "acl index" << (i + 1); | ||
return false; | ||
} | ||
} | ||
|
||
mgallien marked this conversation as resolved.
Show resolved
Hide resolved
|
||
SECURITY_DESCRIPTOR newSecurityDescriptor; | ||
if (!InitializeSecurityDescriptor(&newSecurityDescriptor, SECURITY_DESCRIPTOR_REVISION)) { | ||
qCWarning(lcFileSystem) << "error when calling InitializeSecurityDescriptor" << path << GetLastError(); | ||
return false; | ||
} | ||
|
||
if (!SetSecurityDescriptorDacl(&newSecurityDescriptor, true, newDacl.get(), false)) { | ||
qCWarning(lcFileSystem) << "error when calling SetSecurityDescriptorDacl" << path << GetLastError(); | ||
return false; | ||
} | ||
|
||
if (!SetFileSecurityW(path.toStdWString().c_str(), info, &newSecurityDescriptor)) { | ||
qCWarning(lcFileSystem) << "error when calling SetFileSecurityW" << path << GetLastError(); | ||
return false; | ||
} | ||
#endif | ||
|
||
try { | ||
switch (permissions) { | ||
case OCC::FileSystem::FolderPermissions::ReadOnly: | ||
break; | ||
case OCC::FileSystem::FolderPermissions::ReadWrite: | ||
std::filesystem::permissions(path.toStdWString(), std::filesystem::perms::owner_write, std::filesystem::perm_options::add); | ||
break; | ||
} | ||
} | ||
catch (const std::filesystem::filesystem_error &e) | ||
{ | ||
qCWarning(lcFileSystem()) << "exception when modifying folder permissions" << e.what() << e.path1().c_str() << e.path2().c_str(); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
bool FileSystem::isFolderReadOnly(const std::filesystem::path &path) noexcept | ||
{ | ||
try { | ||
const auto folderStatus = std::filesystem::status(path); | ||
const auto folderPermissions = folderStatus.permissions(); | ||
return (folderPermissions & std::filesystem::perms::owner_write) != std::filesystem::perms::owner_write; | ||
} | ||
catch (const std::filesystem::filesystem_error &e) | ||
{ | ||
qCWarning(lcFileSystem()) << "exception when checking folder permissions" << e.what() << e.path1().c_str() << e.path2().c_str(); | ||
return false; | ||
} | ||
} | ||
|
||
|
||
} // namespace OCC |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mgallien you could've used
std::vector<char>::resize()