aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2026-09-08 01:50:55 +0200
committerNicholas Rodrigues Lordello <[email protected]>2026-09-08 01:50:55 +0200
commit046ce317bb7e1cfeba947ace6aea237cb0f91e2d (patch)
tree8d83cdaa6c9a1b1176e3d9773f650465f9fe4b17
parentcd6d183fc8cd96cfb14de1ee0800d2613297012d (diff)
Back to Shell
I ended up fighting with C for no benefit. We are back to a Shell script, but this time we use (mostly) POSIX shell (we use `local` because it is so convenient, although it is technically not POSIX). Additionally, this time we create a monolithic script, and already add hooks to allow for multiple contianer engines to work with the script (for now, just `container` is implemented and `podman krun` is stubbed).
-rw-r--r--.gitignore2
-rw-r--r--Makefile36
-rwxr-xr-xdbx340
-rw-r--r--src/cmd_create.c44
-rw-r--r--src/dbx.h64
-rw-r--r--src/engine_container.c157
-rw-r--r--src/err.c18
-rw-r--r--src/fs.c112
-rw-r--r--src/main.c248
-rw-r--r--src/proc.c373
-rw-r--r--src/ssh.c162
-rw-r--r--ssh/config.in6
12 files changed, 354 insertions, 1208 deletions
diff --git a/.gitignore b/.gitignore
deleted file mode 100644
index 33d3062..0000000
--- a/.gitignore
+++ /dev/null
@@ -1,2 +0,0 @@
-dbx
-src/*.o
diff --git a/Makefile b/Makefile
index 867f413..b2bb4fe 100644
--- a/Makefile
+++ b/Makefile
@@ -1,33 +1,25 @@
+.POSIX:
-CONTAINER = $(if $(findstring Darwin,$(shell uname -s)),container,podman)
+PREFIX = $(HOME)/.local
+INSTALL = install
-CC = cc
-CFLAGS = -Wall -Wextra -O2
-LDFLAGS =
+ifeq ($(OS),Darwin)
+ CONTAINER = container
+else
+ CONTAINER = podman
+endif
-SRCS = src/cmd_create.c \
- src/err.c \
- src/engine_$(CONTAINER).c \
- src/fs.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 ;
+all:
+ @echo "usage: make [install|container]"
-dbx: $(OBJS)
- $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^
-
-%.o: %.c src/dbx.h
- $(CC) $(CFLAGS) -c -o $@ $<
+.PHONY: install
+install: dbx
+ $(INSTALL) -d $(PREFIX)/bin
+ $(INSTALL) -m 755 dbx $(PREFIX)/bin/dbx
.PHONY: container
container: container/Containerfile container/init
$(CONTAINER) build --tag $(IMAGE) container
-
-.PHONY: clean
-clean:
- rm -f dbx src/*.o
diff --git a/dbx b/dbx
new file mode 100755
index 0000000..77dd1aa
--- /dev/null
+++ b/dbx
@@ -0,0 +1,340 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-3.0-only
+# SPDX-FileCopyrightText: 2026 Nicholas Rodrigues Lordello <[email protected]>
+# shellcheck disable=SC3043
+
+set -eu
+
+command=enter
+name=dbx
+image=ghcr.io/nlordell/dbx:latest
+port=4222
+script=
+fileonly=
+hostport=
+nargs=
+
+DATADIR="${XDG_DATA_HOME:-$HOME/.local/share}/dbx"
+CONFIGDIR="${XDG_CONFIG_HOME:-$HOME/.config}/dbx"
+
+usage() {
+ cat <<USAGE
+usage: $0 [COMMAND] [OPTIONS...] [ARGS...]
+commands:
+ cp copies files to and from a devbox
+ create create a new devbox
+ enter opens a shell in the devbox (default)
+ proxy proxies a port to the host
+ ssh print \`ssh\` connection flags
+ start starts a devbox
+ stop stops a devbox
+options:
+ -n NAME name of the devbox container (default: $name)
+create options:
+ -i IMAGE image for devbox container (default: $image)
+ -p PORT host port \`sshd\` binds to (default: $port)
+ -x SCRIPT post installation script
+ssh options:
+ -F only print the path to the \`ssh_config\` file
+cp arguments:
+ SOURCE... one or more source files to copy from
+ TARGET the target to copy to
+exec arguments:
+ COMMAND... command and arguments to execute
+proxy arguments:
+ PORT[:HOST] the devbox port to proxy on the host; optionally
+ specify an alternate host port to bind to
+USAGE
+ exit 0
+}
+
+version() {
+ echo "0.0.1"
+ exit 0
+}
+
+perror() {
+ local code="$1"
+ local msg="$2"
+ echo "$0: $msg" >&2
+ exit "$code"
+}
+
+general_error() { perror 1 "$1"; }
+usage_error() { perror 1 "$1"; }
+todo() { perror 42 "not yet implemented"; }
+
+parse_port() {
+ local port="$1"
+ if \
+ ! [ "$port" -ge 1 ] 2>/dev/null || \
+ ! [ "$port" -le 65535 ] 2>/dev/null
+ then
+ usage_error "invalid port '$port'"
+ fi
+ echo "$port"
+}
+
+parse_file() {
+ local file="$1"
+ if ! [ -f "$file" ] || ! [ -r "$file" ]; then
+ usage_error "cannot read '$file'"
+ fi
+ echo "$file"
+}
+
+parse_opt() {
+ local opt="$1"
+ local arg="$2"
+ case "$opt" in
+ h) usage ;;
+ v) version ;;
+ n) name="$arg" ;;
+ i) image="$arg" ;;
+ p) port="$(parse_port "$arg")" ;;
+ x) script="$(parse_file "$arg")" ;;
+ F) fileonly=t ;;
+ :) usage_error "missing argument for -$arg" ;;
+ \?) usage_error "invalid option -$arg" ;;
+ esac
+}
+
+parse_opts() {
+ nargs=$#
+
+ local optstr=":hvn:"
+ while getopts "$optstr" opt; do
+ parse_opt "$opt" "${OPTARG:-?}"
+ done
+ shift $((OPTIND-1))
+
+ if [ $# -gt 0 ]; then
+ command="$1"
+ shift
+ fi
+
+ case "$command" in
+ create) optstr="${optstr}i:p:x:" ;;
+ ssh) optstr="${optstr}F" ;;
+ cp|enter|exec|proxy|start|stop) ;;
+ *) usage_error "unkonwn command '$command'" ;;
+ esac
+
+ while getopts "$optstr" opt; do
+ parse_opt "$opt" "${OPTARG:-?}"
+ done
+ shift $((OPTIND-1))
+
+ case "$command" in
+ cp)
+ case "$#" in
+ 0) usage_error "missing argument 'SOURCE'" ;;
+ 1) usage_error "missing argument 'TARGET'" ;;
+ *) ;;
+ esac
+ nargs=$((nargs-$#))
+ shift $#
+ ;;
+ exec)
+ case "$#" in
+ 0) usage_error "missing argument 'COMMAND'" ;;
+ *) ;;
+ esac
+ nargs=$((nargs-$#))
+ shift $#
+ ;;
+ proxy)
+ case "$#" in
+ 0) usage_error "missing argument 'PORT'" ;;
+ *) ;;
+ esac
+ port="$(parse_port "${1%%:*}")"
+ case "$1" in
+ *:*) hostport="$(parse_port "${1#*:}")" ;;
+ *) hostport="$port"
+ esac
+ shift
+ ;;
+ esac
+
+ if [ $# -gt 0 ]; then
+ usage_error "unexpected argument '$1'"
+ fi
+}
+
+ssh_id() {
+ echo "$DATADIR/$name.id_ed25519"
+}
+
+ssh_config() {
+ echo "$DATADIR/$name.ssh_config"
+}
+
+ssh_init() {
+ mkdir -p "$DATADIR" "$CONFIGDIR"
+ rm -f "$(ssh_id)"
+ ssh-keygen -C dbx -f "$(ssh_id)" -N "" -t ed25519 \
+ </dev/null >/dev/null 2>/dev/null
+}
+
+ssh_configure() {
+ local hostname; hostname="$(engine_hostname)"
+ local port; port="$(engine_port)"
+ local knownhosts="$DATADIR/$name.known_hosts"
+ local userconfig="$CONFIGDIR/$name.ssh_config"
+ cat <<SSH_CONFIG >"$(ssh_config)"
+Host $name
+ Hostname $hostname
+ Port $port
+ IdentitiesOnly yes
+ IdentityFile "$(ssh_id)"
+ StrictHostKeyChecking no
+ UserKnownHostsFile "$knownhosts"
+ Include "$userconfig"
+SSH_CONFIG
+}
+
+cmd_cp() {
+ exec scp -F "$(ssh_config)" -- "$@"
+}
+
+cmd_create() {
+ ssh_init
+ engine_create
+
+ if [ -n "$script" ]; then
+ cmd_start
+ scp -q -F "$(ssh_config)" "$script" "$name:/tmp/post-install"
+ ssh -q -F "$(ssh_config)" "$name" \
+ "chmod +x /tmp/post-install && /tmp/post-install"
+ engine_stop
+ fi
+}
+
+cmd_enter() {
+ cmd_start
+ exec ssh -q -F "$(ssh_config)" "$name"
+}
+
+cmd_exec() {
+ exec ssh -q -F "$(ssh_config)" "$name" -- "$@"
+}
+
+cmd_proxy() {
+ exec ssh -q -F "$(ssh_config)" "$name" \
+ -L "localhost:$hostport:localhost:$port" -N
+}
+
+cmd_ssh() {
+ if [ -n "$fileonly" ]; then
+ ssh_config
+ else
+ echo "-F '$(ssh_config)' $name"
+ fi
+}
+
+cmd_start() {
+ if engine_isrunning; then
+ return 0
+ fi
+
+ engine_start
+ ssh_configure
+}
+
+cmd_stop() {
+ engine_stop
+}
+
+
+if command -v container >/dev/null; then
+ engine_create() {
+ container machine create \
+ --name="$name" \
+ --no-boot \
+ --home-mount=none \
+ "$image"
+
+ # Unfortunately, there is no way to access the filesystem on the
+ # container machine while it is stopped; instead boot it up and run a
+ # small script to authorize our SSH key.
+ local pubkey; pubkey="$(cat "$(ssh_id).pub")"
+ container machine run \
+ --name="$name" \
+ --root \
+ "echo '$pubkey' > /etc/ssh/authorized_keys"
+ container machine stop "$name" >/dev/null
+ }
+
+ engine_start() {
+ # The only way to start a machine is to run a command - use one that
+ # does nothing but return `0` exit code.
+ container machine run \
+ --name="$name" \
+ --root \
+ true
+ }
+
+ engine_stop() {
+ container machine stop "$name" >/dev/null
+ }
+
+ engine_isrunning() {
+ local status; status="$(
+ container machine inspect "$name" \
+ | grep '"status"' \
+ | cut -d'"' -f4
+ )"
+ [ "$status" = "running" ]
+ }
+
+ engine_hostname() {
+ local ipaddress; ipaddress="$(
+ container machine inspect "$name" \
+ | grep '"ipAddress"' \
+ | cut -d'"' -f4
+ )"
+ if [ -z "$ipaddress" ]; then
+ general_error "failed to read devbox IP address"
+ fi
+ echo "$ipaddress"
+ }
+
+ engine_port() {
+ echo "22"
+ }
+elif command -v podman >/dev/null && command -v krun >/dev/null; then
+ engine_create() {
+ todo
+ }
+
+ engine_start() {
+ todo
+ }
+
+ engine_stop() {
+ todo
+ }
+
+ engine_isrunning() {
+ todo
+ }
+
+ engine_hostname() {
+ echo "127.0.0.1"
+ }
+
+ engine_port() {
+ todo
+ }
+else
+ general_error "no container engine installed"
+fi
+
+main() {
+ parse_opts "$@"
+ shift $nargs
+ "cmd_$command" "$@"
+}
+
+main "$@"
diff --git a/src/cmd_create.c b/src/cmd_create.c
deleted file mode 100644
index 9b5742f..0000000
--- a/src/cmd_create.c
+++ /dev/null
@@ -1,44 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <sysexits.h>
-#include <unistd.h>
-
-#include "dbx.h"
-
-int dbx_create(struct dbx_engine *engine, struct dbx_options const *options) {
- char ssh_pubkey[PATH_MAX];
- if (!dbx_ssh_init(options->name, ssh_pubkey)) {
- dbx_printerr("failed to generate SSH key");
- return EX_CANTCREAT;
- }
-
- if (!dbx_engine_create(engine, options->image, options->name, ssh_pubkey,
- options->ports.host)) {
- dbx_printerr("failed to create devbox");
- return EX_CANTCREAT;
- }
-
- int result = EXIT_SUCCESS;
- if (options->post_install != NULL) {
- char hostname[HOSTNAME_MAX];
- uint16_t sshd_port;
- if (!dbx_engine_start(engine, options->name, hostname, &sshd_port)) {
- return EX_CONFIG;
- }
-
- char tmp[PATH_MAX];
- if (dbx_formatpath(tmp, "%s:/tmp/post-install", options->name) ||
- !dbx_ssh_config(options->name, hostname, sshd_port) ||
- !dbx_ssh_cp(options->name, options->post_install, tmp) ||
- !dbx_ssh_sys(options->name,
- "chmod +x /tmp/post-install && /tmp/post-install")) {
- result = EX_CONFIG;
- }
-
- if (!dbx_engine_stop(engine, options->name)) {
- result = EX_CONFIG;
- }
- }
-
- return result;
-}
diff --git a/src/dbx.h b/src/dbx.h
deleted file mode 100644
index 3bf372d..0000000
--- a/src/dbx.h
+++ /dev/null
@@ -1,64 +0,0 @@
-// SPDX-License-Identifier: GPL-3.0-only
-// SPDX-FileCopyrightText: 2026 Nicholas Rodrigues Lordello <[email protected]>
-
-#ifndef _DBX_H_
-#define _DBX_H_
-
-#include <limits.h>
-#include <stdbool.h>
-#include <stddef.h>
-#include <stdint.h>
-
-#define COUNTOF(x) ((sizeof(x)) / (sizeof(*x)))
-#define HOSTNAME_MAX _POSIX_HOST_NAME_MAX
-
-extern const char *progname;
-void dbx_printerr(const char *format, ...);
-void dbx_perror(const char *s, int e);
-
-int dbx_formatpath(char path[PATH_MAX], const char *format, ...);
-int dbx_writefile(const char *file, const char *format, ...);
-int dbx_readfile(const char *file, char **contents);
-
-enum dbx_proc_fds {
- DBXFD_NONE = 0,
- DBXFD_STDIN = 1,
- DBXFD_STDOUT = 2,
- DBXFD_STDERR = 4,
- DBXFD_ALL = 7,
-};
-int dbx_proc_find2(const char *name, char path[PATH_MAX]);
-int dbx_proc_run2(const char *const command[], enum dbx_proc_fds fds,
- int *exit_code);
-int dbx_proc_output2(const char *const command[], int *exit_code, char **out,
- size_t *outlen);
-int dbx_proc_exec2(const char *const command[]);
-
-bool dbx_ssh_init(const char *name, char ssh_pubkey[PATH_MAX]);
-bool dbx_ssh_config(const char *name, const char *hostname, uint16_t port);
-bool dbx_ssh_cp(const char *name, const char *from, const char *to);
-bool dbx_ssh_sys(const char *name, const char *command);
-
-struct dbx_engine;
-struct dbx_engine *dbx_engine_init(void);
-bool dbx_engine_create(struct dbx_engine *engine, const char *image,
- const char *name, const char *ssh_pubkey,
- uint16_t sshd_port);
-bool dbx_engine_start(struct dbx_engine *engine, const char *name,
- char hostname[HOSTNAME_MAX], uint16_t *sshd_port);
-bool dbx_engine_stop(struct dbx_engine *engine, const char *name);
-void dbx_engine_destroy(struct dbx_engine *engine);
-
-struct dbx_options {
- char *command;
- char *name;
- char *image;
- struct dbx_options_ports {
- uint16_t container;
- uint16_t host;
- } ports;
- char *post_install;
-};
-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
deleted file mode 100644
index 89d3706..0000000
--- a/src/engine_container.c
+++ /dev/null
@@ -1,157 +0,0 @@
-#include <assert.h>
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include "dbx.h"
-
-struct dbx_engine {
- char container[PATH_MAX];
-};
-
-struct dbx_engine *dbx_engine_init(void) {
- struct dbx_engine *engine = malloc(sizeof(struct dbx_engine));
- if (engine == NULL) {
- dbx_perror("calloc", errno);
- return NULL;
- }
-
- int result = dbx_proc_find("container", engine->container);
- if (result) {
- dbx_perror("unable to find `container` executable", result);
- goto error;
- }
-
- int status_exit;
- const char *const status[] = {"system", "status", NULL};
- result = dbx_proc_run(status, DBXFD_NONE, &status_exit);
- if (result) {
- dbx_perror("unable to check `container` status", result);
- goto error;
- }
- if (status_exit != 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, const char *ssh_pubkey,
- uint16_t sshd_port) {
- // The container machine has its own network and does not bind-mount a port
- // on the host. We can therefore ignore the `sshd_port` setting.
- (void)sshd_port;
-
- int create_exit;
- const char *const status[] = {
- engine->container, "machine", "create", "--name", name,
- "--no-boot", "--home-mount=none", image, NULL};
- int result = dbx_proc_run(status, DBXFD_STDOUT | DBXFD_STDERR, &status_exit);
- if (result) {
- dbx_perror("unable to create `container`", result);
- return false;
- }
- if (create_exit != 0) {
- dbx_printerr("container creation failed");
- return false;
- }
-
- // Unfortunately, there is no way to access files from the container machine
- // while it is stopped; instead boot it and run a small script that can
- // authorize our SSH key.
- char *key;
- result = dbx_readfile(ssh_pubkey, &key);
- if (result) {
- dbx_perror("unable to read SSH public key", result);
- return false;
- }
- char script[1024];
- int n = snprintf(script, sizeof(script),
- "echo '%s' > /etc/ssh/authorized_keys", key);
- if (n < 0 || n >= (int)sizeof(script)) {
- dbx_printerr("invalid SSH public key format");
- return false;
- }
-
- // Note that `container machine run` _requires_ `stdin` for some reason (I
- // assume it is to attach to the VM process), make sure to not close it.
-
- if (dbx_proc_run(DBX_CMD(engine->container, "machine", "run", "--name", name,
- "--root", script),
- DBXFD_ALL) != 0) {
- return false;
- }
-
- // Make sure to shut down the machine, this will restart `SSHD` to ensure the
- // port configuration is correctly picked up.
- return dbx_engine_stop(engine, name);
-}
-
-static bool read_ipaddr(char *json, char hostname[HOSTNAME_MAX]) {
- assert(json != NULL && hostname != NULL);
-
- // We only need to extract the `ipAddress` field from the JSON, so abuse
- // `strtok_r` to read tokens until we get there. Assume that the JSON is valid
- // and that there is only a single `ipAddress` field in the document.
-
- char *tok, *save;
- const char *sep = " \t\r\n\":";
- for (tok = strtok_r(json, sep, &save); tok != NULL;
- tok = strtok_r(NULL, sep, &save)) {
- if (strcmp(tok, "ipAddress") == 0) {
- // The next token is going to be the IP address value.
- tok = strtok_r(NULL, sep, &save);
- break;
- }
- }
-
- if (tok == NULL) {
- return false;
- }
-
- int n = snprintf(hostname, HOSTNAME_MAX, "%s", tok);
- return n >= 0 && n < HOSTNAME_MAX;
-}
-
-bool dbx_engine_start(struct dbx_engine *engine, const char *name,
- char hostname[HOSTNAME_MAX], uint16_t *sshd_port) {
- // The only way to start a comand is with `run` - so run something that
- // does nothing but return `0` exit code.
- if (dbx_proc_run(DBX_CMD(engine->container, "machine", "run", "--name", name,
- "--root", "true"),
- DBXFD_STDERR) != 0) {
- return false;
- }
-
- if (hostname != NULL) {
- char json[8192];
- if (dbx_proc_output(DBX_CMD(engine->container, "machine", "inspect", name),
- json, sizeof(json)) != 0) {
- return false;
- }
-
- if (!read_ipaddr(json, hostname)) {
- return false;
- }
- }
-
- if (sshd_port != NULL) {
- *sshd_port = 22;
- }
-
- return true;
-}
-
-bool dbx_engine_stop(struct dbx_engine *engine, const char *name) {
- return dbx_proc_run(DBX_CMD(engine->container, "machine", "stop", name),
- DBXFD_STDERR) == 0;
-}
-
-void dbx_engine_destroy(struct dbx_engine *engine) { free(engine); }
diff --git a/src/err.c b/src/err.c
deleted file mode 100644
index 35b81f0..0000000
--- a/src/err.c
+++ /dev/null
@@ -1,18 +0,0 @@
-#include <stdarg.h>
-#include <stdio.h>
-#include <string.h>
-
-#include "dbx.h"
-
-void dbx_printerr(const char *format, ...) {
- va_list ap;
- va_start(ap, format);
- fprintf(stderr, "%s: ", progname);
- vfprintf(stderr, format, ap);
- fprintf(stderr, "\n");
- va_end(ap);
-}
-
-void dbx_perror(const char *s, int e) {
- fprintf(stderr, "%s: %s\n", s, strerror(e));
-}
diff --git a/src/fs.c b/src/fs.c
deleted file mode 100644
index 5ec2a1a..0000000
--- a/src/fs.c
+++ /dev/null
@@ -1,112 +0,0 @@
-#include <errno.h>
-#include <stdarg.h>
-#include <stdint.h>
-#include <stdio.h>
-#include <stdlib.h>
-
-#include "dbx.h"
-
-int dbx_formatpath(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);
- if (n < 0 || n >= PATH_MAX) {
- return ENAMETOOLONG;
- }
- return 0;
-}
-
-int dbx_writefile(const char *file, const char *format, ...) {
- if (file == NULL || format == NULL) {
- return EINVAL;
- }
-
- FILE *f = fopen(file, "w");
- if (f == NULL) {
- goto error;
- }
-
- errno = 0;
- va_list ap;
- va_start(ap, format);
- int n = vfprintf(f, format, ap);
- va_end(ap);
-
- if (n < 0) {
- goto error;
- }
- if (fclose(f) != 0) {
- f = NULL;
- goto error;
- }
-
- return 0;
-
-error:;
- int result = errno;
- if (result == 0) {
- result = EIO;
- }
-
- if (f != NULL) {
- fclose(f);
- }
- return result;
-}
-
-int dbx_readfile(const char *file, char **contents) {
- if (file == NULL || contents == NULL) {
- return EINVAL;
- }
-
- char *buf = NULL;
- FILE *f = fopen(file, "r");
- if (f == NULL) {
- goto error;
- }
-
- size_t end;
- {
- long endl;
- if (fseek(f, 0, SEEK_END) || (endl = ftell(f)) < 0 ||
- fseek(f, 0, SEEK_SET)) {
- goto error;
- }
- if ((unsigned long)endl >= SIZE_MAX) {
- errno = ENOMEM;
- goto error;
- }
- end = (size_t)endl;
- }
-
- buf = malloc(end + 1);
- if (buf == NULL) {
- goto error;
- }
-
- errno = 0;
- if (fread(buf, 1, end, f) != end) {
- goto error;
- }
- if (fclose(f) != 0) {
- f = NULL;
- goto error;
- }
-
- buf[end] = '\0';
- *contents = buf;
- return 0;
-
-error:;
- int result = errno;
- if (result == 0) {
- result = EIO;
- }
-
- if (f != NULL) {
- fclose(f);
- }
- free(buf);
- return result;
-}
diff --git a/src/main.c b/src/main.c
deleted file mode 100644
index b613850..0000000
--- a/src/main.c
+++ /dev/null
@@ -1,248 +0,0 @@
-// SPDX-License-Identifier: GPL-3.0-only
-// SPDX-FileCopyrightText: 2026 Nicholas Rodrigues Lordello <[email protected]>
-
-#include <assert.h>
-#include <errno.h>
-#include <stdarg.h>
-#include <stdbool.h>
-#include <stdint.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sysexits.h>
-#include <unistd.h>
-
-#include "dbx.h"
-
-const char *progname = "dbx";
-
-static void usage(FILE *output) {
- fprintf(
- output,
- "usage: %s [COMMAND] [OPTIONS...] [ARGS...]\n"
- "commands:\n"
- " create create a new devbox\n"
- " enter opens a shell in the devbox (default)\n"
- " proxy proxies a port to the host\n"
- " start starts a devbox\n"
- " stop stops a devbox\n"
- "options:\n"
- " -n NAME name of the devbox container (default: dbx)\n"
- "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",
- progname);
-}
-
-#define USAGE_ERROR(...) (dbx_printerr(__VA_ARGS__), EX_USAGE)
-
-static bool parse_port(const char *str, uint16_t *port) {
- char *end = NULL;
- errno = 0;
- long result = strtol(str, &end, 10);
-
- if (str == end || end[0] != '\0') {
- // not a valid string.
- return false;
- }
- if (errno == ERANGE || result <= 0 || result > UINT16_MAX) {
- // out of valid port range; note that we consider the `0` port as invalid as
- // well, since it does not make sense for the CLI
- return false;
- }
-
- *port = (uint16_t)result;
- return true;
-}
-
-bool proxy_ports_arg(struct dbx_options *options, const char *arg) {
- // TRICK: We make a buffer that fits the largest possible value for the ports
- // and add a sentinel 'X', so that if the `arg` would be too long, we would
- // be left with a dangling 'X' (which is not a valid character for integer
- // parsing) in either `container` or `host`.
- char buffer[14] = "65535:65535\0X";
- strncpy(buffer, arg, sizeof(buffer) - 2);
-
- char *container = buffer;
- char *host = strchr(buffer, ':');
- if (host != NULL) {
- *(host++) = '\0';
- }
-
- if (!parse_port(container, &options->ports.container) ||
- (host != NULL && !parse_port(host, &options->ports.host))) {
- return false;
- }
-
- if (host == NULL) {
- options->ports.host = options->ports.container;
- }
-
- return true;
-}
-
-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);
- printf("- image: %s\n", options->image);
- printf("- ports: %d:%d\n", options->ports.container, options->ports.host);
-
- char container[PATH_MAX];
- int result = dbx_proc_find("container", container);
- if (result) {
- dbx_perror("Can't find `container`", result);
- return EXIT_FAILURE;
- }
-
- int exit_code = dbx_proc_run(DBX_CMD(container, "--version"), DBXFD_NONE);
- printf("- cmd: %s:%d\n", container, exit_code);
- dbx_proc_exec(DBX_CMD(container, "--version"));
-
- return EXIT_SUCCESS;
-}
-
-struct command {
- const char *name;
- const char *optstr;
- struct argument {
- const char *name;
- bool (*parse)(struct dbx_options *options, const char *arg);
- } *args;
- int (*run)(struct dbx_engine *engine, struct dbx_options const *options);
-};
-
-static struct command commands[] = {
- {
- .name = "create",
- .optstr = "i:p:x:",
- .run = dbx_create,
- },
- {
- .name = "enter",
- .run = dummy_run,
- },
- {
- .name = "proxy",
- .args =
- (struct argument[]){
- {
- .name = "PORT",
- .parse = proxy_ports_arg,
- },
- {0},
- },
- .run = dummy_run,
- },
- {
- .name = "start",
- .run = dummy_run,
- },
- {
- .name = "stop",
- .run = dummy_run,
- },
-};
-
-int main(int argc, char **argv) {
- struct dbx_options options = {
- .command = "enter",
- .name = "dbx",
- .image = "ghcr.io/nlordell/dbx:latest",
- .ports =
- {
- .container = 22,
- .host = 4222,
- },
- .post_install = NULL,
- };
-
- if (argc > 0) {
- progname = argv[0];
- }
- if (argc > 1 && *argv[1] != '-') {
- options.command = argv[1];
- optind = 2;
- }
-
- struct command *command = NULL;
- for (int i = 0; i < (int)COUNTOF(commands); i++) {
- if (strcmp(options.command, commands[i].name) == 0) {
- command = &commands[i];
- break;
- }
- }
- if (command == NULL) {
- return USAGE_ERROR("unknown command '%s'", options.command);
- }
-
- char optstr[32] = ":hvn:";
- if (command->optstr != NULL) {
- int len = snprintf(optstr, sizeof(optstr), ":hvn:%s", command->optstr);
- assert(len < (int)sizeof(optstr));
- }
-
- int opt;
- while ((opt = getopt(argc, argv, optstr)) != -1) {
- switch (opt) {
- 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;
- case 'i':
- options.image = optarg;
- break;
- case 'p':
- if (!parse_port(optarg, &options.ports.host)) {
- 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 '?':
- default:
- return USAGE_ERROR("unknown option -%c", optopt);
- }
- }
-
- int nargs = argc - optind;
- char **args = argv + optind;
- for (int i = 0; i < nargs; i++) {
- const char *value = args[i];
- struct argument *arg = command->args ? &command->args[i] : NULL;
- if (arg == NULL || arg->parse == NULL) {
- return USAGE_ERROR("unexpected argument '%s'", value);
- }
- if (!arg->parse(&options, value)) {
- return USAGE_ERROR("invalid %s value '%s'", arg->name, value);
- }
- }
- struct argument *endarg = command->args ? &command->args[nargs] : NULL;
- if (endarg != NULL && endarg->name != NULL) {
- return USAGE_ERROR("missing argument %s", endarg->name);
- }
-
- 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;
-}
diff --git a/src/proc.c b/src/proc.c
deleted file mode 100644
index 8c784b2..0000000
--- a/src/proc.c
+++ /dev/null
@@ -1,373 +0,0 @@
-// SPDX-License-Identifier: GPL-3.0-only
-// SPDX-FileCopyrightText: 2026 Nicholas Rodrigues Lordello <[email protected]>
-
-#include <assert.h>
-#include <fcntl.h>
-#include <paths.h>
-#include <spawn.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/errno.h>
-#include <sys/fcntl.h>
-#include <sys/stat.h>
-#include <sys/wait.h>
-#include <unistd.h>
-
-#include "dbx.h"
-
-extern char **environ;
-
-static const char *get_path(void) {
- const char *path = getenv("PATH");
- if (path == NULL || path[0] == '\0') {
- // In case `PATH` is not specified, use the default search path.
- return _PATH_DEFPATH;
- }
- return path;
-}
-
-static const char *next_path_segment(char **path) {
- char *segment = *path;
- if (segment != NULL) {
- *path = strchr(segment, ':');
- if (*path != NULL) {
- *((*path)++) = '\0';
- }
-
- if (segment[0] == '\0') {
- // POSIX defines an empty PATH segment to mean "current directory"
- segment = ".";
- }
- }
-
- return segment;
-}
-
-int dbx_proc_find(const char *name, char file[PATH_MAX]) {
- if (name == NULL || name[0] == '\0' || strchr(name, '/') != NULL ||
- file == NULL) {
- return EINVAL;
- }
-
- char *path = strdup(get_path());
- if (path == NULL) {
- return ENOMEM;
- }
-
- char *pos = path;
- bool found = false;
- for (const char *dir = next_path_segment(&pos); dir != NULL;
- dir = next_path_segment(&pos)) {
- char candidate[PATH_MAX];
- if (dbx_formatpath(candidate, "%s/%s", dir, name)) {
- continue;
- }
-
- struct stat st;
- if (realpath(candidate, file) != NULL && access(file, X_OK) == 0 &&
- stat(file, &st) == 0 && S_ISREG(st.st_mode)) {
- found = true;
- break;
- }
- }
-
- free(path);
- return found;
-}
-
-static char **dup_argv(const char *const command[]) {
- size_t count = 0;
- size_t size = 0;
- for (; command[count] != NULL; count++) {
- size += strlen(command[count]) + 1;
- }
-
- // Allocate a single buffer for the `argv` array and its data. Include space
- // for the extra NULL pointer at the end of the array.
- char **argv = malloc((count + 1) * sizeof(char *) + size);
- if (argv == NULL) {
- return NULL;
- }
-
- char *buf = (char *)(argv + count + 1);
- for (size_t i = 0; i < count; i++) {
- argv[i] = buf;
- buf = stpcpy(buf, command[i]) + 1;
- }
-
- argv[count] = NULL;
- return argv;
-}
-
-int dbx_proc_run(const char *const command[], enum dbx_proc_fds fds,
- int *exit_code) {
- if (command == NULL || command[0] == NULL || exit_code == NULL) {
- return EINVAL;
- }
-
- int result = 0;
- char **argv = dup_argv(command);
- if (argv == NULL) {
- result = ENOMEM;
- goto exit;
- }
-
- posix_spawn_file_actions_t file_actions;
- result = posix_spawn_file_actions_init(&file_actions);
- if (result != 0) {
- goto cleanup_argv;
- }
-
- if ((fds & DBXFD_STDIN) == 0) {
- result = posix_spawn_file_actions_addopen(&file_actions, STDIN_FILENO,
- _PATH_DEVNULL, O_RDONLY, 0);
- if (result != 0) {
- goto cleanup_fa;
- }
- }
-
- if ((fds & DBXFD_STDOUT) == 0) {
- result = posix_spawn_file_actions_addopen(&file_actions, STDOUT_FILENO,
- _PATH_DEVNULL, O_WRONLY, 0);
- if (result != 0) {
- goto cleanup_fa;
- }
- }
-
- if ((fds & DBXFD_STDERR) == 0) {
- result = posix_spawn_file_actions_addopen(&file_actions, STDERR_FILENO,
- _PATH_DEVNULL, O_WRONLY, 0);
- if (result != 0) {
- goto cleanup_fa;
- }
- }
-
- pid_t pid;
- result = posix_spawnp(&pid, argv[0], &file_actions, NULL, argv, environ);
- if (result != 0) {
- goto cleanup_fa;
- }
-
- int status;
- while (waitpid(pid, &status, 0) != pid) {
- if (errno == EINTR) {
- continue;
- }
-
- result = errno;
- goto cleanup_fa;
- }
-
- if (WIFEXITED(status)) {
- *exit_code = WEXITSTATUS(status);
- } else if (WIFSIGNALED(status)) {
- *exit_code = 128 + WTERMSIG(status);
- } else {
- *exit_code = -1;
- }
-
-cleanup_fa:;
- int destroy_result = posix_spawn_file_actions_destroy(&file_actions);
- if (result == 0) {
- result = destroy_result;
- }
-
-cleanup_argv:
- free(argv);
-
-exit:
- return result;
-}
-
-static int read_all(int fd, char **out, size_t *outlen) {
- size_t cap = 4096;
- size_t len = 0;
- char *buf = malloc(cap);
- if (buf == NULL) {
- return ENOMEM;
- }
-
- for (;;) {
- if (len == cap) {
- size_t new_cap = cap * 2;
- if (new_cap >= SSIZE_MAX || new_cap <= cap) {
- free(buf);
- return ENOMEM;
- }
-
- char *new_buf = realloc(buf, new_cap);
- if (new_buf == NULL) {
- free(buf);
- return ENOMEM;
- }
-
- buf = new_buf;
- cap = new_cap;
- }
-
- ssize_t n = read(fd, buf + len, cap - len);
- if (n == 0) {
- break;
- } else if (n < 0) {
- if (errno == EINTR) {
- continue;
- }
-
- int result = errno;
- free(buf);
- return result;
- }
-
- len += (size_t)n;
- }
-
- char *final = realloc(buf, len + 1);
- if (!final) {
- free(buf);
- return ENOMEM;
- }
- final[len] = '\0';
-
- *out = final;
- *outlen = len;
- return 0;
-}
-
-int dbx_proc_output(const char *const command[], int *exit_code, char **out,
- size_t *outlen) {
- if (command == NULL || command[0] == NULL || exit_code == NULL ||
- out == NULL || outlen == NULL) {
- return EINVAL;
- }
-
- int result = 0;
- char *buf = NULL;
-
- char **argv = dup_argv(command);
- if (argv == NULL) {
- result = ENOMEM;
- goto exit;
- }
-
- int pipe_fds[2];
- if (pipe(pipe_fds) != 0) {
- result = errno;
- goto cleanup_argv;
- }
- int read_fd = pipe_fds[0];
- int write_fd = pipe_fds[1];
-
- if (write_fd == STDOUT_FILENO) {
- result = EINVAL;
- goto cleanup_fds;
- }
-
- posix_spawn_file_actions_t file_actions;
- result = posix_spawn_file_actions_init(&file_actions);
- if (result != 0) {
- goto cleanup_fds;
- }
-
- result = posix_spawn_file_actions_addclose(&file_actions, read_fd);
- if (result != 0) {
- goto cleanup_fa;
- }
-
- result =
- posix_spawn_file_actions_adddup2(&file_actions, write_fd, STDOUT_FILENO);
- if (result != 0) {
- goto cleanup_fa;
- }
-
- result = posix_spawn_file_actions_addclose(&file_actions, write_fd);
- if (result != 0) {
- goto cleanup_fa;
- }
-
- pid_t pid;
- result = posix_spawnp(&pid, command[0], &file_actions, NULL, argv, environ);
- if (result != 0) {
- goto cleanup_fa;
- }
-
- if (close(write_fd) != 0) {
- result = errno;
- }
- write_fd = -1;
- if (result != 0) {
- goto cleanup_pid;
- }
-
- size_t buflen;
- result = read_all(read_fd, &buf, &buflen);
-
-cleanup_pid:
- if (close(read_fd) != 0 && result == 0) {
- result = errno;
- }
- read_fd = -1;
-
- int status;
- while (waitpid(pid, &status, 0) != pid) {
- if (errno == EINTR) {
- continue;
- }
-
- if (result == 0) {
- result = errno;
- }
- goto cleanup_fa;
- }
-
-cleanup_fa:;
- int destroy_result = posix_spawn_file_actions_destroy(&file_actions);
- if (result == 0) {
- result = destroy_result;
- }
-
-cleanup_fds:
- if (write_fd >= 0 && close(write_fd) != 0 && result == 0) {
- result = errno;
- }
- if (read_fd >= 0 && close(read_fd) != 0 && result == 0) {
- result = errno;
- }
-
-cleanup_argv:
- free(argv);
-
-exit:
- if (result == 0) {
- if (WIFEXITED(status)) {
- *exit_code = WEXITSTATUS(status);
- } else if (WIFSIGNALED(status)) {
- *exit_code = 128 + WTERMSIG(status);
- } else {
- *exit_code = -1;
- }
- *out = buf;
- *outlen = buflen;
- } else {
- free(buf);
- }
-
- return result;
-}
-
-int dbx_proc_exec(const char *const command[]) {
- if (command == NULL || command[0] == NULL) {
- return EINVAL;
- }
-
- char **argv = dup_argv(command);
- if (argv == NULL) {
- return ENOMEM;
- }
-
- execvp(argv[0], argv);
-
- // execvp only ever returns if there was an error.
- free(argv);
- return errno;
-}
diff --git a/src/ssh.c b/src/ssh.c
deleted file mode 100644
index 15e8b07..0000000
--- a/src/ssh.c
+++ /dev/null
@@ -1,162 +0,0 @@
-#include <assert.h>
-#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 int get_xdg_dir(char path[PATH_MAX], const char *env, const char *def) {
- int result = ENOENT;
-
- const char *xdg = getenv(env);
- if (xdg != NULL) {
- result = dbx_formatpath(path, "%s/dbx", xdg);
- } else {
- const char *home = getenv("HOME");
- if (home != NULL) {
- result = dbx_formatpath(path, "%s/%s/dbx", home, def);
- }
- }
-
- return result;
-}
-
-static int get_data_dir(char path[PATH_MAX]) {
- return get_xdg_dir(path, "XDG_DATA_HOME", ".local/share");
-}
-
-static int get_config_dir(char path[PATH_MAX]) {
- return get_xdg_dir(path, "XDG_CONFIG_HOME", ".config");
-}
-
-static int mkdir_p(const char *path) {
- char buffer[PATH_MAX];
- int result = dbx_formatpath(buffer, "%s/", path);
- if (result != 0) {
- return result;
- }
-
- for (char *cursor = strchr(buffer + 1, '/'); cursor;
- cursor = strchr(cursor + 1, '/')) {
- *cursor = '\0';
- if (mkdir(path, 0755) != 0 && errno != EEXIST) {
- return errno;
- }
- *cursor = '/';
- }
-
- return 0;
-}
-
-bool dbx_ssh_init(const char *name, char ssh_pubkey[PATH_MAX]) {
- assert(name != NULL && ssh_pubkey != NULL);
-
- char data_dir[PATH_MAX];
- if (get_data_dir(data_dir) || mkdir_p(data_dir)) {
- return false;
- }
-
- char ssh_id[PATH_MAX];
- if (dbx_formatpath(ssh_id, "%s/%s.id_ed25519", data_dir, name) ||
- dbx_formatpath(ssh_pubkey, "%s/%s.id_ed25519.pub", data_dir, name)) {
- return false;
- }
- if (unlink(ssh_id) != 0 && errno != ENOENT) {
- dbx_perror("unlink(ssh-id)", errno);
- return false;
- }
-
- return dbx_proc_run(DBX_CMD("ssh-keygen", "-C", "dbx", "-f", ssh_id, "-N", "",
- "-t", "ed25519"),
- DBXFD_NONE) == 0;
-}
-
-bool dbx_ssh_config(const char *name, const char *hostname, uint16_t port) {
- assert(name != NULL && hostname != NULL);
-
- char data_dir[PATH_MAX];
- char config_dir[PATH_MAX];
- if (get_data_dir(data_dir) || get_config_dir(config_dir)) {
- return false;
- }
-
- char ssh_config[PATH_MAX];
- char ssh_id[PATH_MAX];
- char known_hosts[PATH_MAX];
- char user_config[PATH_MAX];
- if (dbx_formatpath(ssh_config, "%s/%s.ssh_config", data_dir, name) ||
- dbx_formatpath(ssh_id, "%s/%s.id_ed25519", data_dir, name) ||
- dbx_formatpath(known_hosts, "%s/%s.known_hosts", data_dir, name) ||
- dbx_formatpath(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"
- "\tPort %d\n"
- "\tIdentitiesOnly yes\n"
- "\tIdentityFile \"%s\"\n"
- "\tStrictHostKeyChecking no\n"
- "\tUserKnownHostsFile \"%s\"\n"
- "\tInclude \"%s\"\n";
- int n = fprintf(f, format, name, hostname, port, 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;
-}
-
-bool dbx_ssh_cp(const char *name, const char *from, const char *to) {
- assert(name != NULL && from != NULL && to != NULL);
-
- char data_dir[PATH_MAX];
- if (get_data_dir(data_dir)) {
- return false;
- }
-
- char ssh_config[PATH_MAX];
- if (dbx_formatpath(ssh_config, "%s/%s.ssh_config", data_dir, name)) {
- return false;
- }
-
- return dbx_proc_run(DBX_CMD("scp", "-F", ssh_config, from, to),
- DBXFD_STDERR) == 0;
-}
-
-bool dbx_ssh_sys(const char *name, const char *command) {
- assert(name != NULL && command != NULL);
-
- char data_dir[PATH_MAX];
- if (get_data_dir(data_dir)) {
- return false;
- }
-
- char ssh_config[PATH_MAX];
- if (dbx_formatpath(ssh_config, "%s/%s.ssh_config", data_dir, name)) {
- return false;
- }
-
- return dbx_proc_run(DBX_CMD("ssh", "-F", ssh_config, name, command),
- DBXFD_ALL) == 0;
-}
diff --git a/ssh/config.in b/ssh/config.in
deleted file mode 100644
index fbcbc40..0000000
--- a/ssh/config.in
+++ /dev/null
@@ -1,6 +0,0 @@
-Host $MACHINE
- HostName $IP
- IdentitiesOnly yes
- IdentityFile $ROOT/ssh/id_ed25519
- StrictHostKeyChecking no
- UserKnownHostsFile $ROOT/ssh/$MACHINE.known_hosts