linuxinstall/src/sandbox.c

73 lines
2.0 KiB
C
Raw Normal View History

#include "sandbox.h"
2024-02-18 18:09:53 +01:00
#include "utils.h"
2024-02-19 16:01:53 +01:00
#include <stdio.h>
2024-02-19 02:06:12 +01:00
#include <stdbool.h>
2024-02-17 15:15:40 +01:00
#include <pwd.h>
#include <unistd.h>
2024-02-17 23:59:38 +01:00
#define ALIAS(...) \
(const char*[]) { __VA_ARGS__, NULL }
#define ALIASES(...) \
(const char**[]) { __VA_ARGS__, NULL }
#define ARGUMENTS(...) \
(const Argument[]) { \
__VA_ARGS__, {} \
}
#define OPTIONS(...) \
(const Option[]) { \
__VA_ARGS__, {} \
}
Command COMMANDS[] = {
// Help
{
.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."}),
},
};
2024-02-18 18:09:53 +01:00
int main(int argc, char** argv) {
// Ensure the sandbox user exists
2024-02-17 23:59:38 +01:00
struct passwd* pw = getpwnam(SANDBOX_USER);
if (pw == NULL) {
2024-02-19 16:01:53 +01:00
fprintf(stderr, "User '%s' does not exist. Please check that the program is installed correctly.\n", SANDBOX_USER);
2024-02-17 15:15:40 +01:00
return EXIT_FAILURE;
}
2024-02-18 18:09:53 +01:00
// Check that the program is either run as root or as the sandbox user
if (geteuid() != 0 && geteuid() != pw->pw_uid) {
2024-02-19 16:01:53 +01:00
fprintf(stderr, "This program must be run as root or as the user '%s'.\n", SANDBOX_USER);
2024-02-17 15:15:40 +01:00
return EXIT_FAILURE;
}
2024-02-18 18:09:53 +01:00
// If the program is run as root, switch to the sandbox user
2024-02-17 15:15:40 +01:00
if (geteuid() == 0) {
2024-02-18 18:09:53 +01:00
if (setregid(pw->pw_gid, pw->pw_gid) != 0) {
2024-02-19 16:01:53 +01:00
fprintf(stderr, "Failed to switch to the group '%s'.\n", pw->pw_name);
return EXIT_FAILURE;
}
2024-02-18 18:09:53 +01:00
if (setreuid(pw->pw_uid, pw->pw_uid) != 0) {
2024-02-19 16:01:53 +01:00
fprintf(stderr, "Failed to switch to the user '%s'.\n", pw->pw_name);
2024-02-18 14:43:38 +01:00
return EXIT_FAILURE;
}
}
2024-02-18 18:09:53 +01:00
// TODO: Parse commands from the command line
}
int command_help(int argc, char* argv[]) {
return EXIT_SUCCESS;
}