[+] AuFS::IsFileBlocked

[+] AuFS::IsFileTrusted
This commit is contained in:
Reece Wilson 2023-08-29 19:43:45 +01:00
parent 0fcd63d3a8
commit b5a452a43d
3 changed files with 124 additions and 25 deletions

View File

@ -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.

View File

@ -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;
}
}

View File

@ -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;
}
}
}