diff options
| -rw-r--r-- | Makefile | 15 | ||||
| -rw-r--r-- | container/Containerfile | 17 | ||||
| -rw-r--r-- | custom/README.md | 15 | ||||
| -rw-r--r-- | src/cmd_create.c | 28 | ||||
| -rw-r--r-- | src/dbx.h | 35 | ||||
| -rw-r--r-- | src/engine_container.c | 49 | ||||
| -rw-r--r-- | src/main.c | 38 | ||||
| -rw-r--r-- | src/proc.c | 43 | ||||
| -rw-r--r-- | src/ssh.c | 123 |
9 files changed, 309 insertions, 54 deletions
@@ -1,12 +1,17 @@ + +CONTAINER = $(if $(findstring Darwin,$(shell uname -s)),container,podman) + CC = cc CFLAGS = -Wall -Wextra -O2 LDFLAGS = -SRCS = $(shell find src -name '*.c') -OBJS = $(patsubst %.c,%.o,$(SRCS)) - -CONTAINER = $(if $(findstring Darwin,$(shell uname -s)),container,podman) -IMAGE = dbx-next +SRCS = src/cmd_create.c \ + src/engine_$(CONTAINER).c \ + src/main.c \ + src/proc.c \ + src/ssh.c +OBJS = $(patsubst %.c,%.o,$(SRCS)) +IMAGE = ghcr.io/nlordell/dbx:latest .PHONY: all all: dbx ; diff --git a/container/Containerfile b/container/Containerfile index 2498b12..e8f8670 100644 --- a/container/Containerfile +++ b/container/Containerfile @@ -7,10 +7,27 @@ RUN touch /run/.dbxenv RUN rm /etc/apt/apt.conf.d/docker-* && \ apt-get update && \ apt-get install -y \ + bash-completion \ + bzip2 \ + curl \ + git \ + gnupg \ + htop \ + less \ locales \ + man-db \ + manpages \ + nano \ + openssh-client \ openssh-server \ + procps \ sudo \ tini \ + trash-cli \ + tree \ + vim \ + wget \ + zip \ && \ apt-get clean && \ rm -dr /var/lib/apt/lists/* diff --git a/custom/README.md b/custom/README.md deleted file mode 100644 index 5d9a8a5..0000000 --- a/custom/README.md +++ /dev/null @@ -1,15 +0,0 @@ -# Customizations - -This directory includes customizations that are not tracked by Git that can be used to configure the devbox on its first run. - -## Skeleton Directory - -All files in the `skel` directory will be copied over the devbox user's home directory (overwriting any files that may exist there from `/etc/skel`), allowing for easy customization of the devbox user. - -## Extra Packages - -A special `extra-packages` file can be added to this directory to include additional packages to install on first run. - -## Post Installation Script - -A special `post-install` executable can be added to this directory which will be run as a final step when creating the devbox. The post install script is run as the devbox user (with passwordless `sudo` access). diff --git a/src/cmd_create.c b/src/cmd_create.c new file mode 100644 index 0000000..7b1e185 --- /dev/null +++ b/src/cmd_create.c @@ -0,0 +1,28 @@ +#include <stdlib.h> +#include <sysexits.h> +#include <unistd.h> + +#include "dbx.h" + +int dbx_create(struct dbx_engine *engine, struct dbx_options const *options) { + if (!dbx_ssh_init(options->name)) { + dbx_printerr("failed to generate SSH key"); + return EX_CANTCREAT; + } + + if (!dbx_engine_create(engine, options->image, options->name, + options->ports.host)) { + dbx_printerr("failed to create devbox"); + return EX_CANTCREAT; + } + + // TODO: setup and install SSH configuration. + // TODO: copy over (SCP) and execute post-install script. + + if (options->post_install != NULL) { + dbx_printerr("-x SCRIPT not yet supported"); + return EX_USAGE; + } + + return EXIT_SUCCESS; +} @@ -9,6 +9,30 @@ #include <stddef.h> #include <stdint.h> +#define COUNTOF(x) ((sizeof(x)) / (sizeof(*x))) + +void dbx_printerr(const char *format, ...); +void dbx_perror(const char *s, int e); + +enum dbx_proc_output { + DBXP_NONE = 0, + DBXP_STDOUT = 1, + DBXP_STDERR = 2, + DBXP_ALL = 3, +}; +bool dbx_proc_find(const char *name, char path[PATH_MAX]); +int dbx_proc_run(const char *const command[], enum dbx_proc_output out); +bool dbx_proc_exec(const char *const command[]); + +bool dbx_ssh_init(const char *name); +bool dbx_ssh_set_hostname(const char *name, const char *hostname); + +struct dbx_engine; +struct dbx_engine *dbx_engine_init(void); +bool dbx_engine_create(struct dbx_engine *engine, const char *image, + const char *name, uint16_t sshd_port); +void dbx_engine_destroy(struct dbx_engine *engine); + struct dbx_options { char *command; char *name; @@ -17,15 +41,8 @@ struct dbx_options { uint16_t container; uint16_t host; } ports; + char *post_install; }; - -#define COUNTOF(x) ((sizeof(x)) / (sizeof(*x))) - -void dbx_printerr(const char *format, ...); -void dbx_perror(const char *s, int e); - -bool dbx_proc_find(const char *name, char path[PATH_MAX]); -int dbx_proc_run(char *const command[]); -bool dbx_proc_exec(char *const command[]); +int dbx_create(struct dbx_engine *engine, struct dbx_options const *options); #endif diff --git a/src/engine_container.c b/src/engine_container.c new file mode 100644 index 0000000..6ddf122 --- /dev/null +++ b/src/engine_container.c @@ -0,0 +1,49 @@ +#include <errno.h> +#include <stdlib.h> + +#include "dbx.h" + +struct dbx_engine { + char container[PATH_MAX]; +}; + +struct dbx_engine *dbx_engine_init(void) { + struct dbx_engine *engine = calloc(1, sizeof(struct dbx_engine)); + if (engine == NULL) { + dbx_perror("calloc", errno); + return NULL; + } + + if (!dbx_proc_find("container", engine->container)) { + goto error; + } + + const char *const status[] = {engine->container, "system", "status", NULL}; + if (dbx_proc_run(status, DBXP_NONE) != 0) { + dbx_printerr("container engine must started with `container system start`"); + goto error; + } + + return engine; + +error: + free(engine); + return NULL; +} + +bool dbx_engine_create(struct dbx_engine *engine, const char *image, + const char *name, uint16_t sshd_port) { + const char *const create[] = { + engine->container, "machine", "create", "--name", name, + "--no-boot", "--home-mount=none", image, NULL}; + if (dbx_proc_run(create, DBXP_ALL) != 0) { + return false; + } + + // TODO(nlordell): Set the SSHD port + // TODO(nlordell): Copy over the SSH key. + + return true; +} + +void dbx_engine_destroy(struct dbx_engine *engine) { free(engine); } @@ -30,6 +30,7 @@ static void usage(FILE *output) { "create options:\n" " -i IMAGE image for devbox container (default: dbx-next:latest)\n" " -p PORT host port `sshd` binds to (default: 4222)\n" + " -x SCRIPT post installation script\n" "proxy arguments:\n" " PORT[:HOST] the devbox port to proxy on the host; optionally\n" " specify an alternate host port to bind to\n", @@ -96,7 +97,9 @@ bool proxy_ports_arg(struct dbx_options *options, const char *arg) { return true; } -int dummy_run(struct dbx_options *options) { +int dummy_run(struct dbx_engine *engine, struct dbx_options const *options) { + (void)engine; + printf("Hello, DBX!\n"); printf("- command: %s\n", options->command); printf("- name: %s\n", options->name); @@ -109,8 +112,8 @@ int dummy_run(struct dbx_options *options) { return EXIT_FAILURE; } - char *const cmd[] = {container, "--version", NULL}; - int exit_code = dbx_proc_run(cmd); + const char *const cmd[] = {container, "--version", NULL}; + int exit_code = dbx_proc_run(cmd, DBXP_NONE); printf("- cmd: %s:%d\n", container, exit_code); dbx_proc_exec(cmd); @@ -124,14 +127,14 @@ struct command { const char *name; bool (*parse)(struct dbx_options *options, const char *arg); } *args; - int (*run)(struct dbx_options *options); + int (*run)(struct dbx_engine *engine, struct dbx_options const *options); }; static struct command commands[] = { { .name = "create", - .optstr = "i:p:", - .run = dummy_run, + .optstr = "i:p:x:", + .run = dbx_create, }, { .name = "enter", @@ -163,12 +166,13 @@ int main(int argc, char **argv) { struct dbx_options options = { .command = "enter", .name = "dbx", - .image = "dbx-next:latest", + .image = "ghcr.io/nlordell/dbx:latest", .ports = { .container = 22, .host = 4222, }, + .post_install = NULL, }; if (argc > 0) { @@ -190,9 +194,9 @@ int main(int argc, char **argv) { return USAGE_ERROR("unknown command '%s'", options.command); } - char optstr[32] = ":hn:"; + char optstr[32] = ":hvn:"; if (command->optstr != NULL) { - int len = snprintf(optstr, sizeof(optstr), ":hn:%s", command->optstr); + int len = snprintf(optstr, sizeof(optstr), ":hvn:%s", command->optstr); assert(len < (int)sizeof(optstr)); } @@ -202,6 +206,9 @@ int main(int argc, char **argv) { case 'h': usage(stdout); return EXIT_SUCCESS; + case 'v': + printf("%s 0.0.1\n", progname); + return EXIT_SUCCESS; case 'n': options.name = optarg; break; @@ -213,6 +220,9 @@ int main(int argc, char **argv) { return USAGE_ERROR("invalid port '%s'", optarg); } break; + case 'x': + options.post_install = optarg; + break; case ':': return USAGE_ERROR("missing value for -%c", optopt); case '?': @@ -238,5 +248,13 @@ int main(int argc, char **argv) { return USAGE_ERROR("missing argument %s", endarg->name); } - return command->run(&options); + struct dbx_engine *engine = dbx_engine_init(); + if (engine == NULL) { + dbx_printerr("failed to initialize container engine"); + return EX_UNAVAILABLE; + } + int result = command->run(engine, &options); + dbx_engine_destroy(engine); + + return result; } @@ -75,7 +75,7 @@ bool dbx_proc_find(const char *name, char file[PATH_MAX]) { return found; } -int dbx_proc_run(char *const command[]) { +int dbx_proc_run(const char *const command[], enum dbx_proc_output out) { assert(command != NULL && command[0] != NULL); int result; @@ -91,23 +91,32 @@ int dbx_proc_run(char *const command[]) { dbx_perror("posix_spawn_file_actions_addclose(stdin)", result); goto cleanup; } - result = posix_spawn_file_actions_addopen(&file_actions, STDOUT_FILENO, - _PATH_DEVNULL, O_WRONLY, 0); - if (result != 0) { - dbx_perror("posix_spawn_file_actions_addopen(stdout)", result); - goto cleanup; + + if ((out & DBXP_STDOUT) == 0) { + result = posix_spawn_file_actions_addopen(&file_actions, STDOUT_FILENO, + _PATH_DEVNULL, O_WRONLY, 0); + if (result != 0) { + dbx_perror("posix_spawn_file_actions_addopen(stdout)", result); + goto cleanup; + } } - result = posix_spawn_file_actions_addopen(&file_actions, STDERR_FILENO, - _PATH_DEVNULL, O_WRONLY, 0); - if (result != 0) { - dbx_perror("posix_spawn_file_actions_addopen(stderr)", result); - goto cleanup; + + if ((out & DBXP_STDERR) == 0) { + result = posix_spawn_file_actions_addopen(&file_actions, STDERR_FILENO, + _PATH_DEVNULL, O_WRONLY, 0); + if (result != 0) { + dbx_perror("posix_spawn_file_actions_addopen(stderr)", result); + goto cleanup; + } } + // POSIX guarantees that the pointer is not modified, so the cast is safe. + char *const *argv = (char *const *)command; + pid_t pid; - result = posix_spawn(&pid, command[0], &file_actions, NULL, command, environ); + result = posix_spawnp(&pid, command[0], &file_actions, NULL, argv, environ); if (result != 0) { - dbx_perror("posix_spawn", result); + dbx_perror("posix_spawnp", result); goto cleanup; } @@ -133,9 +142,13 @@ cleanup: return exit_code; } -bool dbx_proc_exec(char *const command[]) { +bool dbx_proc_exec(const char *const command[]) { assert(command != NULL && command[0] != NULL); - execv(command[0], command); + + // POSIX guarantees that the pointer is not modified, so the cast is safe. + char *const *argv = (char *const *)command; + + execvp(command[0], argv); // execvp only ever returns if there was an error. dbx_perror("execv", errno); diff --git a/src/ssh.c b/src/ssh.c new file mode 100644 index 0000000..59cd51a --- /dev/null +++ b/src/ssh.c @@ -0,0 +1,123 @@ +#include <errno.h> +#include <limits.h> +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/errno.h> +#include <sys/stat.h> +#include <unistd.h> + +#include "dbx.h" + +static bool format_path(char path[PATH_MAX], const char *format, ...) { + va_list ap; + va_start(ap, format); + int n = vsnprintf(path, PATH_MAX, format, ap); + va_end(ap); + + return n >= 0 && n < PATH_MAX; +} + +static bool get_xdg_dir(char path[PATH_MAX], const char *env, const char *def) { + bool result = false; + + const char *xdg = getenv(env); + if (xdg != NULL) { + result = format_path(path, "%s/dbx", xdg); + } else { + const char *home = getenv("HOME"); + if (home != NULL) { + result = format_path(path, "%s/%s/dbx", home, def); + } + } + + return result; +} + +static bool mkdir_p(const char *path) { + char buffer[PATH_MAX]; + if (!format_path(buffer, "%s/", path)) { + return false; + } + + for (char *cursor = strchr(buffer + 1, '/'); cursor; + cursor = strchr(cursor + 1, '/')) { + *cursor = '\0'; + if (mkdir(path, 0755) != 0 && errno != EEXIST) { + dbx_perror("mkdir", errno); + return false; + } + *cursor = '/'; + } + + return true; +} + +bool dbx_ssh_init(const char *name) { + char data_dir[PATH_MAX]; + if (!get_xdg_dir(data_dir, "XDG_DATA_HOME", ".local/share") || + !mkdir_p(data_dir)) { + return false; + } + + char ssh_id[PATH_MAX]; + if (!format_path(ssh_id, "%s/%s.id_ed25519", data_dir, name)) { + return false; + } + if (unlink(ssh_id) != 0 && errno != ENOENT) { + dbx_perror("unlink(ssh-id)", errno); + return false; + } + + const char *const keygen[] = {"ssh-keygen", "-C", name, "-f", ssh_id, + "-N", "", "-t", "ed25519", NULL}; + return dbx_proc_run(keygen, DBXP_ALL) == 0; +} + +bool dbx_ssh_set_hostname(const char *name, const char *hostname) { + char data_dir[PATH_MAX]; + char config_dir[PATH_MAX]; + if (!get_xdg_dir(data_dir, "XDG_DATA_HOME", ".local/share") || + !get_xdg_dir(config_dir, "XDG_CONFIG_HOME", ".config")) { + return false; + } + + char ssh_config[PATH_MAX]; + char ssh_id[PATH_MAX]; + char known_hosts[PATH_MAX]; + char user_config[PATH_MAX]; + if (!format_path(ssh_config, "%s/%s.ssh_config", data_dir, name) || + !format_path(ssh_id, "%s/%s.id_ed25519", data_dir, name) || + !format_path(known_hosts, "%s/%s.known_hosts", data_dir, name) || + !format_path(user_config, "%s/%s.ssh_config", config_dir, name)) { + return false; + } + + FILE *f = fopen(ssh_config, "w"); + if (f == NULL) { + dbx_perror("fopen(ssh-config)", errno); + return false; + } + + bool result = true; + char format[] = "Host %s\n" + "\tHostName %s\n" + "\tIdentitiesOnly yes\n" + "\tIdentityFile \"%s\"\n" + "\tStrictHostKeyChecking no\n" + "\tUserKnownHostsFile \"%s\"\n" + "\tInclude \"%s\"\n"; + int n = fprintf(f, format, name, hostname, ssh_id, known_hosts, user_config); + if (n < 0) { + dbx_perror("fprintf(ssh-config)", errno); + result = false; + } + + if (fclose(f) != 0) { + dbx_perror("fclose(ssh-config)", errno); + result = false; + } + + return result; +} |
