2023-01-25 16:47:25 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2023 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: FileTrust.Unix.cpp
|
|
|
|
Date: 2023-1-25
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#include <Source/RuntimeInternal.hpp>
|
|
|
|
#include "FS.hpp"
|
|
|
|
#include "FileTrust.Unix.hpp"
|
|
|
|
|
|
|
|
#include <sys/xattr.h>
|
2023-08-11 15:51:42 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2023-01-25 16:47:25 +00:00
|
|
|
|
|
|
|
namespace Aurora::IO::FS
|
|
|
|
{
|
|
|
|
AUKN_SYM bool BlockFile(const AuString &path)
|
|
|
|
{
|
2023-08-29 18:43:45 +00:00
|
|
|
auto srcPath = NormalizePathRet(path);
|
|
|
|
if (srcPath.empty())
|
|
|
|
{
|
|
|
|
SysPushErrorMemory();
|
|
|
|
return {};
|
|
|
|
}
|
2023-01-25 16:47:25 +00:00
|
|
|
|
|
|
|
AuString subdir;
|
|
|
|
|
|
|
|
if ((path.size() > 1) &&
|
|
|
|
((path[path.size() - 1] == '/') ||
|
|
|
|
(path[path.size() - 1] == '\\')))
|
|
|
|
{
|
2023-08-29 18:43:45 +00:00
|
|
|
subdir = srcPath.substr(0, srcPath.size() - 1);
|
2023-01-25 16:47:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-08-29 18:43:45 +00:00
|
|
|
subdir = srcPath;
|
2023-01-25 16:47:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GoUpToSeparator(subdir, subdir);
|
|
|
|
|
|
|
|
subdir = "file:///" + subdir;
|
|
|
|
|
2023-08-29 18:43:45 +00:00
|
|
|
if (::setxattr(srcPath.c_str(), "user.xdg.origin.url", subdir.c_str(), subdir.size(), XATTR_CREATE) == -1)
|
2023-01-25 16:47:25 +00:00
|
|
|
{
|
|
|
|
if (errno == EEXIST)
|
|
|
|
{
|
2023-08-29 18:43:45 +00:00
|
|
|
if (::setxattr(srcPath.c_str(), "user.xdg.origin.url", subdir.c_str(), subdir.size(), XATTR_REPLACE) == -1)
|
2023-01-25 16:47:25 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-29 18:43:45 +00:00
|
|
|
if (::setxattr(srcPath.c_str(), "user.xdg.referrer.url", subdir.c_str(), subdir.size(), XATTR_CREATE) == -1)
|
2023-01-25 16:47:25 +00:00
|
|
|
{
|
|
|
|
if (errno == EEXIST)
|
|
|
|
{
|
2023-08-29 18:43:45 +00:00
|
|
|
if (::setxattr(srcPath.c_str(), "user.xdg.referrer.url", subdir.c_str(), subdir.size(), XATTR_REPLACE) == -1)
|
2023-01-25 16:47:25 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mode_t mode { 0644 };
|
|
|
|
struct stat s;
|
2023-08-29 18:43:45 +00:00
|
|
|
if (::stat(srcPath.c_str(), &s) < 0)
|
2023-01-25 16:47:25 +00:00
|
|
|
{
|
|
|
|
mode = s.st_mode;
|
|
|
|
mode &= ~( 0111 );
|
|
|
|
}
|
|
|
|
|
2023-08-29 18:43:45 +00:00
|
|
|
if (::chmod(srcPath.c_str(), mode) != 0)
|
2023-01-25 16:47:25 +00:00
|
|
|
{
|
|
|
|
SysPushErrorIO("BlockFile chmod failed: {}", path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM bool UnblockFile(const AuString &path)
|
|
|
|
{
|
2023-08-29 18:43:45 +00:00
|
|
|
auto srcPath = NormalizePathRet(path);
|
|
|
|
if (srcPath.empty())
|
|
|
|
{
|
|
|
|
SysPushErrorMemory();
|
|
|
|
return {};
|
|
|
|
}
|
2023-01-25 16:47:25 +00:00
|
|
|
|
2023-08-29 18:43:45 +00:00
|
|
|
::removexattr(srcPath.c_str(), "user.xdg.origin.url");
|
|
|
|
::removexattr(srcPath.c_str(), "user.xdg.referrer.url");
|
2023-01-25 16:47:25 +00:00
|
|
|
|
|
|
|
mode_t mode { 0644 };
|
|
|
|
struct stat s;
|
2023-08-29 18:43:45 +00:00
|
|
|
if (::stat(srcPath.c_str(), &s) < 0)
|
2023-01-25 16:47:25 +00:00
|
|
|
{
|
|
|
|
mode = s.st_mode;
|
|
|
|
mode &= ~(0111);
|
|
|
|
}
|
|
|
|
|
2023-08-29 18:43:45 +00:00
|
|
|
if (::chmod(srcPath.c_str(), mode) != 0)
|
2023-01-25 16:47:25 +00:00
|
|
|
{
|
|
|
|
SysPushErrorIO("UnblockFile chmod failed: {}", path);
|
|
|
|
}
|
|
|
|
|
2023-08-29 18:43:45 +00:00
|
|
|
return AuFS::FileExists(srcPath);
|
2023-01-25 16:47:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM bool TrustFile(const AuString &path)
|
|
|
|
{
|
2023-08-29 18:43:45 +00:00
|
|
|
struct stat s;
|
2023-01-25 16:47:25 +00:00
|
|
|
mode_t mode { 0755 };
|
|
|
|
|
2023-08-29 18:43:45 +00:00
|
|
|
auto srcPath = NormalizePathRet(path);
|
|
|
|
if (srcPath.empty())
|
|
|
|
{
|
|
|
|
SysPushErrorMemory();
|
|
|
|
return {};
|
|
|
|
}
|
2023-01-25 16:47:25 +00:00
|
|
|
|
2023-08-29 18:43:45 +00:00
|
|
|
if (::stat(srcPath.c_str(), &s) < 0)
|
2023-01-25 16:47:25 +00:00
|
|
|
{
|
|
|
|
mode = s.st_mode;
|
|
|
|
mode |= 0111;
|
|
|
|
}
|
|
|
|
|
2023-08-29 18:43:45 +00:00
|
|
|
if (::chmod(srcPath.c_str(), mode) != 0)
|
2023-01-25 16:47:25 +00:00
|
|
|
{
|
|
|
|
SysPushErrorIO("chmod failed: {}", path);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2023-08-29 18:43:45 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2023-01-25 16:47:25 +00:00
|
|
|
}
|