From b746e30627c203a40af50c415fb1ce868d796663 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexe=C3=AF=20KADIR?= Date: Fri, 23 Feb 2024 18:32:03 +0100 Subject: [PATCH] Added a crude alias system for commands --- scripts/sandbox-install | 2 +- src/domain.h | 10 ---- src/sandbox.c | 106 +++++++++++++++++++++++++++------------- src/sandbox.h | 15 +++--- 4 files changed, 82 insertions(+), 51 deletions(-) diff --git a/scripts/sandbox-install b/scripts/sandbox-install index c8cd12f..c1e3ac3 100644 --- a/scripts/sandbox-install +++ b/scripts/sandbox-install @@ -103,7 +103,7 @@ After=multi-user.target [Service] Type=oneshot -ExecStart=/usr/bin/fstrim -va +ExecStart= ExecStop=/usr/bin/fstrim -va RemainAfterExit=yes diff --git a/src/domain.h b/src/domain.h index a87892f..8208573 100644 --- a/src/domain.h +++ b/src/domain.h @@ -13,13 +13,3 @@ result_t container_to_domain_identifier(char** _domain, const char* container); /// @param domain The domain identifier to convert. /// @return The result of the operation. result_t domain_to_container_identifier(char** _container, const char* domain); - -/// @brief Checks whether the given string is a valid domain identifier. If the string is not a valid domain identifier, the call will return a failure result with an error message. -/// @param domain The string to check. -/// @return The result of the operation. -result_t check_domain_identifier(const char* domain); - -/// @brief Checks whether the given domain exists. If the domain does not exist, the call will return a failure result with an error message. -/// @param domain The domain to check. -/// @return The result of the operation. -result_t check_domain_exists(const char* domain); diff --git a/src/sandbox.c b/src/sandbox.c index e367e72..4252784 100755 --- a/src/sandbox.c +++ b/src/sandbox.c @@ -9,12 +9,17 @@ #include #include #include +#include -#define ALIAS(...) \ - (const char*[]) { __VA_ARGS__, NULL } +#define ALIAS(...) \ + (const char*[]) { \ + __VA_ARGS__, NULL \ + } -#define ALIASES(...) \ - (const char**[]) { __VA_ARGS__, NULL } +#define ALIASES(...) \ + (const char**[]) { \ + __VA_ARGS__, NULL \ + } #define ARGUMENTS(...) \ (const Argument[]) { \ @@ -26,51 +31,86 @@ __VA_ARGS__, {} \ } -Command COMMANDS[] = { - // Help +#define OPTION_ALIASES(...) \ + (const char*[]) { \ + __VA_ARGS__, NULL \ + } + +const Command COMMANDS[] = { { .handler = command_help, - .description = "Display help information.", - .details = "Display help information about the available commands and their options.", - .aliases = ALIASES(ALIAS("help")), - .arguments = ARGUMENTS({.name = "command", .required = false, .description = "The command to display help information for."}, - {.name = "test", .required = false, .description = "Test."}), - .options = OPTIONS({.aliases = ALIASES(ALIAS("--help"), ALIAS("-h")), .arguments = NULL, .description = "Display help information."}, - {.aliases = ALIASES(ALIAS("--test")), .arguments = NULL, .description = "Test."}), + .description = "Display help information", + .details = "Display help information for a specific command", + + .aliases = ALIASES(ALIAS("h"), ALIAS("help", "me")), + .arguments = ARGUMENTS({ + .name = "command", + .description = "The command to display help information for", + .optional = true, + }), + .options = OPTIONS({ + .aliases = OPTION_ALIASES("verbose", "v"), + .description = "Display verbose help information", + .arguments = ARGUMENTS({ + .name = "level", + .description = "The level of verbosity to display", + }), + }), }, -}; + {}}; int main(int argc, char** argv) { - // Ensure the sandbox user exists - struct passwd* pw = getpwnam(SANDBOX_USER); - if (pw == NULL) { - fprintf(stderr, "User '%s' does not exist. Please check that the program is installed correctly.\n", SANDBOX_USER); + if (argc == 1) { + // Handle no command + fprintf(stderr, "No command specified\n"); return EXIT_FAILURE; } - // Check that the program is either run as root or as the sandbox user - if (geteuid() != 0 && geteuid() != pw->pw_uid) { - fprintf(stderr, "This program must be run as root or as the user '%s'.\n", SANDBOX_USER); - return EXIT_FAILURE; - } + // For each command + for (int i = 0; COMMANDS[i].handler != NULL; i++) { + const Command* command = &COMMANDS[i]; - // If the program is run as root, switch to the sandbox user - if (geteuid() == 0) { - if (setregid(pw->pw_gid, pw->pw_gid) != 0) { - fprintf(stderr, "Failed to switch to the group '%s'.\n", pw->pw_name); - return EXIT_FAILURE; + // For each alias + bool command_matched = false; + int k = 0; + for (int j = 0; command->aliases[j] != NULL; j++) { + const char** alias = command->aliases[j]; + + // For each string in the alias + bool alias_matched = true; + for (k = 0; alias[k] != NULL; k++) + if (argc <= 1 + k || strcmp(argv[1 + k], alias[k]) != 0) { + alias_matched = false; + break; + } + + // Check if the alias matched + if (alias_matched) { + command_matched = true; + break; + } } - if (setreuid(pw->pw_uid, pw->pw_uid) != 0) { - fprintf(stderr, "Failed to switch to the user '%s'.\n", pw->pw_name); - return EXIT_FAILURE; + // If the command matched + if (command_matched) { + // Call the command handler + return command->handler(argc - 1 - k, argv + 1 + k); } } - // TODO: Parse commands from the command line - return EXIT_SUCCESS; + fprintf(stderr, "Unknown command: "); + for (int i = 1; i < argc; i++) + fprintf(stderr, "%s ", argv[i]); + fprintf(stderr, "\n"); + + return EXIT_FAILURE; } int command_help(int argc, char* argv[]) { + // Print argv + printf("argc: %d\n", argc); + for (int i = 0; i < argc; i++) + printf("%s\n", argv[i]); + return EXIT_SUCCESS; } diff --git a/src/sandbox.h b/src/sandbox.h index 227913b..a8c79ff 100755 --- a/src/sandbox.h +++ b/src/sandbox.h @@ -13,26 +13,27 @@ typedef struct { const char* name; - const bool required; const char* description; + const bool optional; } Argument; typedef struct { - const char*** aliases; - const Argument* arguments; + const char** aliases; const char* description; + const Argument* arguments; } Option; typedef struct { - int (*const handler)(int argc, char* argv[]); + int (*handler)(int argc, char* argv[]); + const char* description; + const char* details; + const char*** aliases; const Argument* arguments; const Option* options; - const char* description; - const char* details; } Command; -extern Command COMMANDS[]; +extern const Command COMMANDS[]; int main(int argc, char** argv);