587{
588 try {
589 if (::geteuid() != 0)
590 fail(
"Administrative privileges are required.");
593
594 if (options.command == "set-protection") {
595 if (options.protection != "on" && options.protection != "off")
596 fail(
"set-protection requires --state on|off.");
597 const bool enabled = options.protection == "on";
602 std::cout << (enabled
603 ? "Immediate blocking enabled for protected system paths; user home remains audit-only."
604 : "Immediate metadata blocking disabled.") << '\n';
605 } else if (options.command == "set-application-rules") {
607 } else if (options.command == "restore") {
609 } else if (options.command == "remove") {
611 } else if (options.command == "block-service") {
615 if (target->init == InitSystem::OpenRC || target->init == InitSystem::SysVInit) {
616 struct stat current{};
617 if (!options.device || !options.inode ||
618 ::lstat(path.c_str(), ¤t) != 0 ||
620 static_cast<std::uint64_t>(current.st_ino) != *options.inode) {
621 fail(
"Refusing to execute an OpenRC/SysV service definition without a matching protected identity.");
622 }
623 }
624 }
626 if (!result.success)
fail(result.message);
628 std::cout << result.message << '\n';
629 } else if (options.command == "unblock-service") {
633 if (!result.success)
fail(result.message);
634 std::cout << result.message << '\n';
635 } else {
636 fail(
"Unknown action: " + options.command);
637 }
638 return 0;
639 } catch (const std::exception& error) {
640 std::cerr << "RootGuard action: " << error.what() << '\n';
641 return 1;
642 }
643}
static __u64 kernelDeviceNumber(dev_t encodedDevice)
Policy loadPolicy(const fs::path &path)
void fail(const std::string &message)
Options parseOptions(const int argc, char *argv[])
void requireProtectedPath(const Policy &policy, const fs::path &path)
fs::path normalizedAbsolute(const fs::path &path)
void quarantineFile(const Options &options, const Policy &policy)
void rewriteApplicationRules(const Options &options)
void logAction(const Policy &policy, const fs::path &path, const std::uint32_t eventType, const std::uint32_t verdict, const struct stat *before=nullptr, const struct stat *after=nullptr)
void rewriteProtectionPolicy(const fs::path &policyPath, const bool enabled)
void restoreFile(const Options &options, const Policy &policy)
std::optional< ServiceTarget > identifySystemService(const std::filesystem::path &path) noexcept
ServiceActionResult blockSystemService(const std::filesystem::path &path) noexcept
ServiceActionResult unblockSystemService(const std::filesystem::path &path) noexcept
@ RG_EVENT_SERVICE_BLOCKED
@ RG_EVENT_PROTECTION_MODE_CHANGED