linuxinstall/src/sandbox.c

270 lines
6.5 KiB
C
Raw Normal View History

#include "sandbox.h"
2024-02-18 18:09:53 +01:00
#include "utils.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
2024-02-19 02:06:12 +01:00
Command COMMANDS[] = {
{
command_help,
{{"help", NULL}, NULL},
NULL,
{{"command", "TODO: Add description.", false}, NULL},
"TODO: Add description.",
"TODO: Add details."
},
{
command_version,
{{"version", NULL}, NULL},
NULL,
NULL,
"TODO: Add description.",
"TODO: Add details."
},
{
command_config,
{{"config", NULL}, NULL},
NULL,
NULL,
"TODO: Add description.",
"TODO: Add details."
},
{
command_container_add,
{{"container", "add", NULL}, NULL},
{
{
{"-r", "--root", NULL},
{{"size", "TODO: Add description.", true}, NULL},
"TODO: Add description."
},
{
{"-i", "--image", NULL},
{{"image", "TODO: Add description.", true}, NULL},
"TODO: Add description."
},
NULL
},
{{"container", "TODO: Add description.", true}, NULL},
"TODO: Add description.",
"TODO: Add details."
},
{
command_container_remove,
{{"container", "rm", NULL}, {"container", "remove", NULL}, {"container", "del", NULL}, {"container", "delete", NULL}, NULL},
NULL,
{{"container", "TODO: Add description.", true}, NULL},
"TODO: Add description.",
"TODO: Add details."
},
{
command_container_reset,
{{"container", "reset", NULL}, NULL},
NULL,
{{"container", "TODO: Add description.", true}, NULL},
"Resets the container.",
"TODO: Add details."
},
{
command_container_trim,
{{"container", "trim", NULL}, NULL},
NULL,
{{"container", "TODO: Add description.", true}, NULL},
"TODO: Add description.",
"TODO: Add details."
},
{
command_container_info,
{{"container", "info", NULL}, NULL},
NULL,
{{"container", "TODO: Add description.", true}, NULL},
"TODO: Add description.",
"TODO: Add details."
},
{
command_container_list,
{{"container", "ls", NULL}, {"container", "list", NULL}, NULL},
NULL,
NULL,
"TODO: Add description.",
"TODO: Add details."
},
{
command_container_wipe,
{{"container", "wipe", NULL}, NULL},
NULL,
NULL,
"TODO: Add description.",
"TODO: Add details."
},
{
command_container_start,
{{"container", "start", NULL}, NULL},
{
{
{"-p", "--no-pci", NULL},
NULL,
"TODO: Add description."
},
{
{"-v", "--vnc", NULL},
{
{"port", "TODO: Add description.", true},
{"password", "TODO: Add description.", true},
NULL
},
"TODO: Add description."
},
{
{"-i", "--iso", NULL},
{
{"iso", "TODO: Add description.", true},
NULL
},
"TODO: Add description."
},
NULL
},
{{"container", "TODO: Add description.", true}, NULL},
"TODO: Add description.",
"TODO: Add details."
},
{
command_container_stop,
{{"container", "stop", NULL}, NULL},
{
{
{"-f", "--force", NULL},
NULL,
"TODO: Add description."
},
{
{"-t", "--timeout", NULL},
{
{"timeout", "TODO: Add description.", true},
NULL
},
"TODO: Add description."
},
NULL
},
{{"container", "TODO: Add description.", true}, NULL},
"TODO: Add description.",
"TODO: Add details."
},
{
command_container_ps,
{{"container", "ps", NULL}, NULL},
NULL,
NULL,
"TODO: Add description.",
"TODO: Add details."
},
{
command_image_sync,
{{"image", "sync", NULL}, NULL},
NULL,
NULL,
"TODO: Add description.",
"TODO: Add details."
},
{
command_image_add,
{{"image", "add", NULL}, NULL},
NULL,
{
{"container", "The name of the container to add.", true},
NULL
},
"TODO: Add details.",
"TODO: Add details."
},
{
command_image_import,
{{"image", "import", NULL}, NULL},
NULL,
{
{"file", "TODO: Add description.", true},
NULL
},
"TODO: Add description.",
"TODO: Add details."
},
{
command_image_remove,
{{"image", "rm", NULL}, {"image", "remove", NULL}, NULL},
NULL,
{{"image", "TODO: Add description.", true}, NULL},
"TODO: Add description.",
"TODO: Add details."
},
{
command_image_default,
{{"image", "default", NULL}, NULL},
NULL,
{{"image", "TODO: Add description.", false}, NULL},
"TODO: Add description.",
"TODO: Add details."
},
{
command_image_list,
{{"image", "ls", NULL}, {"image", "list", NULL}, NULL},
NULL,
NULL,
"TODO: Add description.",
"TODO: Add details."
}
};
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-18 18:09:53 +01:00
Log(LOG_LEVEL_ERROR, "User '%s' does not exist. Please check that the program is installed correctly.", 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) {
Log(LOG_LEVEL_ERROR, "This program must be run as root or as the user '%s'.", 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) {
Log(LOG_LEVEL_ERROR, "Failed to switch to the group '%s'.", pw->pw_name);
return EXIT_FAILURE;
}
2024-02-18 18:09:53 +01:00
if (setreuid(pw->pw_uid, pw->pw_uid) != 0) {
Log(LOG_LEVEL_ERROR, "Failed to switch to the user '%s'.", 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
2024-02-19 02:06:12 +01:00
}
int command_help(int argc, char** argv) { return EXIT_SUCCESS; }
int command_version(int argc, char** argv) { return EXIT_SUCCESS; }
int command_config(int argc, char** argv) { return EXIT_SUCCESS; }
int command_container_add(int argc, char** argv) { return EXIT_SUCCESS; }
int command_container_remove(int argc, char** argv) { return EXIT_SUCCESS; }
int command_container_reset(int argc, char** argv) { return EXIT_SUCCESS; }
int command_container_trim(int argc, char** argv) { return EXIT_SUCCESS; }
int command_container_info(int argc, char** argv) { return EXIT_SUCCESS; }
int command_container_list(int argc, char** argv) { return EXIT_SUCCESS; }
int command_container_wipe(int argc, char** argv) { return EXIT_SUCCESS; }
int command_container_start(int argc, char** argv) { return EXIT_SUCCESS; }
int command_container_stop(int argc, char** argv) { return EXIT_SUCCESS; }
int command_container_ps(int argc, char** argv) { return EXIT_SUCCESS; }
int command_image_sync(int argc, char** argv) { return EXIT_SUCCESS; }
int command_image_add(int argc, char** argv) { return EXIT_SUCCESS; }
int command_image_import(int argc, char** argv) { return EXIT_SUCCESS; }
int command_image_remove(int argc, char** argv) { return EXIT_SUCCESS; }
int command_image_default(int argc, char** argv) { return EXIT_SUCCESS; }
int command_image_list(int argc, char** argv) { return EXIT_SUCCESS; }