2020-12-26 13:24:34 +03:00
|
|
|
/*
|
2022-03-29 20:25:04 +03:00
|
|
|
* Copyright (c) 2020-2022, the SerenityOS developers.
|
2020-12-26 13:24:34 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-12-26 13:24:34 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibCore/ArgsParser.h>
|
2022-03-29 20:25:04 +03:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2021-05-23 17:46:11 +03:00
|
|
|
#include <limits.h>
|
2020-12-26 13:24:34 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2022-03-29 20:25:04 +03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2020-12-26 13:24:34 +03:00
|
|
|
{
|
|
|
|
Vector<StringView> paths_to_test;
|
2022-03-29 20:25:04 +03:00
|
|
|
StringView permissions = "r"sv;
|
2020-12-26 13:24:34 +03:00
|
|
|
bool should_sleep = false;
|
|
|
|
|
|
|
|
Core::ArgsParser parser;
|
|
|
|
parser.add_option(permissions, "Apply these permissions going forward", "permissions", 'p', "unveil-permissions");
|
|
|
|
parser.add_option(should_sleep, "Sleep after processing all arguments", "sleep", 's');
|
|
|
|
parser.add_option(Core::ArgsParser::Option {
|
2022-07-12 23:13:38 +03:00
|
|
|
.argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
|
2020-12-26 13:24:34 +03:00
|
|
|
.help_string = "Add a path to the unveil list",
|
|
|
|
.long_name = "unveil",
|
|
|
|
.short_name = 'u',
|
|
|
|
.value_name = "path",
|
2023-02-21 14:44:41 +03:00
|
|
|
.accept_value = [&](StringView path) {
|
2020-12-26 13:24:34 +03:00
|
|
|
if (path.is_empty())
|
|
|
|
return false;
|
2022-03-29 20:25:04 +03:00
|
|
|
auto maybe_error = Core::System::unveil(path, permissions);
|
|
|
|
if (maybe_error.is_error()) {
|
|
|
|
warnln("{}", maybe_error.error());
|
2020-12-26 13:24:34 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} });
|
|
|
|
parser.add_option(Core::ArgsParser::Option {
|
2022-07-12 23:13:38 +03:00
|
|
|
.argument_mode = Core::ArgsParser::OptionArgumentMode::None,
|
2020-12-26 13:24:34 +03:00
|
|
|
.help_string = "Lock the veil",
|
|
|
|
.long_name = "lock",
|
|
|
|
.short_name = 'l',
|
2023-02-21 14:44:41 +03:00
|
|
|
.accept_value = [&](StringView) {
|
2022-03-29 20:25:04 +03:00
|
|
|
auto maybe_error = Core::System::unveil(nullptr, nullptr);
|
|
|
|
if (maybe_error.is_error()) {
|
|
|
|
warnln("unveil(nullptr, nullptr): {}", maybe_error.error());
|
2020-12-26 13:24:34 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} });
|
|
|
|
parser.add_positional_argument(Core::ArgsParser::Arg {
|
|
|
|
.help_string = "Test a path against the veil",
|
|
|
|
.name = "path",
|
|
|
|
.min_values = 0,
|
|
|
|
.max_values = INT_MAX,
|
2023-02-21 14:44:41 +03:00
|
|
|
.accept_value = [&](StringView s) {
|
|
|
|
auto maybe_error = Core::System::access(s, X_OK);
|
2022-03-29 20:25:04 +03:00
|
|
|
if (maybe_error.is_error())
|
|
|
|
warnln("'{}' - fail: {}", s, maybe_error.error());
|
2020-12-26 13:24:34 +03:00
|
|
|
else
|
2022-03-29 20:25:04 +03:00
|
|
|
warnln("'{}' - ok", s);
|
2020-12-26 13:24:34 +03:00
|
|
|
return true;
|
|
|
|
} });
|
|
|
|
|
2022-03-29 20:25:04 +03:00
|
|
|
parser.parse(arguments);
|
2020-12-26 13:24:34 +03:00
|
|
|
if (should_sleep)
|
|
|
|
sleep(INT_MAX);
|
|
|
|
return 0;
|
|
|
|
}
|