From b5a452a43df8ac06c91f2574bf6af6d0930efee2 Mon Sep 17 00:00:00 2001 From: Jamie Reece Wilson Date: Tue, 29 Aug 2023 19:43:45 +0100 Subject: [PATCH] [+] AuFS::IsFileBlocked [+] AuFS::IsFileTrusted --- Include/Aurora/IO/FS/FS.hpp | 6 ++- Source/IO/FS/FileTrust.NT.cpp | 47 +++++++++++++++- Source/IO/FS/FileTrust.Unix.cpp | 96 +++++++++++++++++++++++++-------- 3 files changed, 124 insertions(+), 25 deletions(-) diff --git a/Include/Aurora/IO/FS/FS.hpp b/Include/Aurora/IO/FS/FS.hpp index 6deed093..b781dc12 100644 --- a/Include/Aurora/IO/FS/FS.hpp +++ b/Include/Aurora/IO/FS/FS.hpp @@ -186,7 +186,11 @@ namespace Aurora::IO::FS * @return */ AUKN_SYM bool TrustFile(const AuString &path); - + + AUKN_SYM bool IsFileBlocked(const AuString &path); + + AUKN_SYM bool IsFileTrusted(const AuString &path); + /** * @brief Transfers the contents of the specified filepath through a * zstandard compression pipe to an ending path + ".zst" file. diff --git a/Source/IO/FS/FileTrust.NT.cpp b/Source/IO/FS/FileTrust.NT.cpp index d40e3e53..6f099a03 100644 --- a/Source/IO/FS/FileTrust.NT.cpp +++ b/Source/IO/FS/FileTrust.NT.cpp @@ -26,11 +26,54 @@ namespace Aurora::IO::FS AuString idc; auto uri = path + ":Zone.Identifier"; - if (AuFS::ReadString(uri, idc)) + + if (AuFS::FileExists(uri)) { - return AuFS::Remove(uri); + if (AuFS::ReadString(uri, idc)) + { + return AuFS::Remove(uri); + } } return true; } + + AUKN_SYM bool IsFileBlocked(const AuString &path) + { + AuString content; + + auto uri = path + ":Zone.Identifier"; + if (!AuFS::FileExists(uri)) + { + return !AuFS::FileExists(path); + } + + if (!AuFS::ReadString(uri, content)) + { + return !AuFS::FileExists(path); + } + + return AuStringContains(content, "ZoneId=1\r\n") || // intranet + AuStringContains(content, "ZoneId=3\r\n") || // internet + AuStringContains(content, "ZoneId=4\r\n"); // untrusted + } + + AUKN_SYM bool IsFileTrusted(const AuString &path) + { + AuString content; + + auto uri = path + ":Zone.Identifier"; + + if (!AuFS::FileExists(uri)) + { + return AuFS::FileExists(path); + } + + if (!AuFS::ReadString(uri, content)) + { + return AuFS::FileExists(path); + } + + return false; + } } \ No newline at end of file diff --git a/Source/IO/FS/FileTrust.Unix.cpp b/Source/IO/FS/FileTrust.Unix.cpp index 1226a866..046fdd43 100644 --- a/Source/IO/FS/FileTrust.Unix.cpp +++ b/Source/IO/FS/FileTrust.Unix.cpp @@ -19,8 +19,12 @@ namespace Aurora::IO::FS { AUKN_SYM bool BlockFile(const AuString &path) { - AuString path2 = path; - AuFS::NormalizePath(path2, path2); + auto srcPath = NormalizePathRet(path); + if (srcPath.empty()) + { + SysPushErrorMemory(); + return {}; + } AuString subdir; @@ -28,33 +32,33 @@ namespace Aurora::IO::FS ((path[path.size() - 1] == '/') || (path[path.size() - 1] == '\\'))) { - subdir = path2.substr(0, path2.size() - 1); + subdir = srcPath.substr(0, srcPath.size() - 1); } else { - subdir = path2; + subdir = srcPath; } GoUpToSeparator(subdir, subdir); subdir = "file:///" + subdir; - if (::setxattr(path2.c_str(), "user.xdg.origin.url", subdir.c_str(), subdir.size(), XATTR_CREATE) == -1) + if (::setxattr(srcPath.c_str(), "user.xdg.origin.url", subdir.c_str(), subdir.size(), XATTR_CREATE) == -1) { if (errno == EEXIST) { - if (::setxattr(path2.c_str(), "user.xdg.origin.url", subdir.c_str(), subdir.size(), XATTR_REPLACE) == -1) + if (::setxattr(srcPath.c_str(), "user.xdg.origin.url", subdir.c_str(), subdir.size(), XATTR_REPLACE) == -1) { return false; } } } - if (::setxattr(path2.c_str(), "user.xdg.referrer.url", subdir.c_str(), subdir.size(), XATTR_CREATE) == -1) + if (::setxattr(srcPath.c_str(), "user.xdg.referrer.url", subdir.c_str(), subdir.size(), XATTR_CREATE) == -1) { if (errno == EEXIST) { - if (::setxattr(path2.c_str(), "user.xdg.referrer.url", subdir.c_str(), subdir.size(), XATTR_REPLACE) == -1) + if (::setxattr(srcPath.c_str(), "user.xdg.referrer.url", subdir.c_str(), subdir.size(), XATTR_REPLACE) == -1) { return false; } @@ -63,13 +67,13 @@ namespace Aurora::IO::FS mode_t mode { 0644 }; struct stat s; - if (::stat(path2.c_str(), &s) != -1) + if (::stat(srcPath.c_str(), &s) < 0) { mode = s.st_mode; mode &= ~( 0111 ); } - if (::chmod(path2.c_str(), mode) != 0) + if (::chmod(srcPath.c_str(), mode) != 0) { SysPushErrorIO("BlockFile chmod failed: {}", path); } @@ -79,43 +83,51 @@ namespace Aurora::IO::FS AUKN_SYM bool UnblockFile(const AuString &path) { - AuString path2 = path; - AuFS::NormalizePath(path2, path2); + auto srcPath = NormalizePathRet(path); + if (srcPath.empty()) + { + SysPushErrorMemory(); + return {}; + } - ::removexattr(path2.c_str(), "user.xdg.origin.url"); - ::removexattr(path2.c_str(), "user.xdg.referrer.url"); + ::removexattr(srcPath.c_str(), "user.xdg.origin.url"); + ::removexattr(srcPath.c_str(), "user.xdg.referrer.url"); mode_t mode { 0644 }; struct stat s; - if (::stat(path2.c_str(), &s) != -1) + if (::stat(srcPath.c_str(), &s) < 0) { mode = s.st_mode; mode &= ~(0111); } - if (::chmod(path2.c_str(), mode) != 0) + if (::chmod(srcPath.c_str(), mode) != 0) { SysPushErrorIO("UnblockFile chmod failed: {}", path); } - return AuFS::FileExists(path2); + return AuFS::FileExists(srcPath); } AUKN_SYM bool TrustFile(const AuString &path) { + struct stat s; mode_t mode { 0755 }; - AuString path2 = path; - AuFS::NormalizePath(path2, path2); + auto srcPath = NormalizePathRet(path); + if (srcPath.empty()) + { + SysPushErrorMemory(); + return {}; + } - struct stat s; - if (::stat(path2.c_str(), &s) != -1) + if (::stat(srcPath.c_str(), &s) < 0) { mode = s.st_mode; mode |= 0111; } - if (::chmod(path2.c_str(), mode) != 0) + if (::chmod(srcPath.c_str(), mode) != 0) { SysPushErrorIO("chmod failed: {}", path); return false; @@ -123,4 +135,44 @@ namespace Aurora::IO::FS return true; } + + AUKN_SYM bool IsFileBlocked(const AuString &path) + { + auto srcPath = NormalizePathRet(path); + if (srcPath.empty()) + { + SysPushErrorMemory(); + return {}; + } + + auto length = ::getxattr(srcPath.c_str(), "user.xdg.referrer.url", nullptr, 0); + if (length <= 0) + { + return true; + } + else + { + return false; + } + } + + AUKN_SYM bool IsFileTrusted(const AuString &path) + { + struct stat s; + auto srcPath = NormalizePathRet(path); + if (srcPath.empty()) + { + SysPushErrorMemory(); + return {}; + } + + if (::stat(srcPath.c_str(), &s) < 0) + { + return (s.st_mode & 0111) != 0; + } + else + { + return false; + } + } } \ No newline at end of file