aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/cmd_create.c32
-rw-r--r--src/dbx.h32
-rw-r--r--src/engine_container.c141
-rw-r--r--src/main.c16
-rw-r--r--src/proc.c229
-rw-r--r--src/ssh.c97
6 files changed, 458 insertions, 89 deletions
diff --git a/src/cmd_create.c b/src/cmd_create.c
index 7b1e185..a68fcb7 100644
--- a/src/cmd_create.c
+++ b/src/cmd_create.c
@@ -1,3 +1,4 @@
+#include <stdio.h>
#include <stdlib.h>
#include <sysexits.h>
#include <unistd.h>
@@ -5,24 +6,39 @@
#include "dbx.h"
int dbx_create(struct dbx_engine *engine, struct dbx_options const *options) {
- if (!dbx_ssh_init(options->name)) {
+ 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,
+ if (!dbx_engine_create(engine, options->image, options->name, ssh_pubkey,
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.
-
+ int result = EXIT_SUCCESS;
if (options->post_install != NULL) {
- dbx_printerr("-x SCRIPT not yet supported");
- return EX_USAGE;
+ 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_fpath(tmp, "%s:/tmp/post-install", options->name) < 0 ||
+ !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 EXIT_SUCCESS;
+ return result;
}
diff --git a/src/dbx.h b/src/dbx.h
index 4e1b82d..675ae62 100644
--- a/src/dbx.h
+++ b/src/dbx.h
@@ -6,31 +6,41 @@
#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
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,
+int dbx_fpath(char path[PATH_MAX], const char *format, ...);
+
+enum dbx_proc_fds {
+ DBXFD_NONE = 0,
+ DBXFD_STDIN = 1,
+ DBXFD_STDOUT = 2,
+ DBXFD_STDERR = 4,
+ DBXFD_ALL = 7,
};
bool dbx_proc_find(const char *name, char path[PATH_MAX]);
-int dbx_proc_run(const char *const command[], enum dbx_proc_output out);
+int dbx_proc_run(const char *const command[], enum dbx_proc_fds fds);
+int dbx_proc_output(const char *const command[], char *out, int outlen);
bool dbx_proc_exec(const char *const command[]);
+#define DBX_CMD(...) ((const char *const[]){__VA_ARGS__, NULL})
-bool dbx_ssh_init(const char *name);
-bool dbx_ssh_set_hostname(const char *name, const char *hostname);
+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, uint16_t sshd_port);
+ 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 {
diff --git a/src/engine_container.c b/src/engine_container.c
index 6ddf122..da704a8 100644
--- a/src/engine_container.c
+++ b/src/engine_container.c
@@ -1,5 +1,8 @@
+#include <assert.h>
#include <errno.h>
+#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include "dbx.h"
@@ -18,8 +21,8 @@ struct dbx_engine *dbx_engine_init(void) {
goto error;
}
- const char *const status[] = {engine->container, "system", "status", NULL};
- if (dbx_proc_run(status, DBXP_NONE) != 0) {
+ if (dbx_proc_run(DBX_CMD(engine->container, "system", "status"),
+ DBXFD_NONE) != 0) {
dbx_printerr("container engine must started with `container system start`");
goto error;
}
@@ -31,19 +34,139 @@ error:
return NULL;
}
+#define PUBKEY_MAX 256
+
+static int read_pubkey(const char *file, char key[PUBKEY_MAX]) {
+ FILE *f = fopen(file, "rb");
+ if (!f) {
+ dbx_perror("fopen(ssh-pubkey)", errno);
+ return -1;
+ }
+
+ int total = 0;
+ while (total < PUBKEY_MAX) {
+ size_t n = fread(key + total, 1, PUBKEY_MAX - total, f);
+ if (n == 0) {
+ if (!feof(f)) {
+ dbx_printerr("failed to read %s", file);
+ total = -1;
+ }
+ break;
+ }
+ total += n;
+ }
+
+ if (--total >= 0 && total < PUBKEY_MAX && key[total] == '\n') {
+ key[total] = '\0';
+ } else {
+ total = -1;
+ }
+
+ if (fclose(f) != 0) {
+ dbx_perror("fclose(ssh-pubkey)", errno);
+ total = -1;
+ };
+ return total;
+}
+
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) {
+ 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;
+
+ if (dbx_proc_run(DBX_CMD(engine->container, "machine", "create", "--name",
+ name, "--no-boot", "--home-mount=none", image),
+ DBXFD_STDOUT | DBXFD_STDERR) != 0) {
+ 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[PUBKEY_MAX];
+ if (read_pubkey(ssh_pubkey, key) < 0) {
+ 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)) {
+ 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;
}
- // TODO(nlordell): Set the SSHD port
- // TODO(nlordell): Copy over the SSH key.
+ 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/main.c b/src/main.c
index 442d0a0..68ba075 100644
--- a/src/main.c
+++ b/src/main.c
@@ -50,6 +50,17 @@ void dbx_perror(const char *s, int e) {
dbx_printerr("%s: %s", s, strerror(e));
}
+int dbx_fpath(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) {
+ n = -1;
+ }
+ return n;
+}
+
#define USAGE_ERROR(...) (dbx_printerr(__VA_ARGS__), EX_USAGE)
static bool parse_port(const char *str, uint16_t *port) {
@@ -112,10 +123,9 @@ int dummy_run(struct dbx_engine *engine, struct dbx_options const *options) {
return EXIT_FAILURE;
}
- const char *const cmd[] = {container, "--version", NULL};
- int exit_code = dbx_proc_run(cmd, DBXP_NONE);
+ int exit_code = dbx_proc_run(DBX_CMD(container, "--version"), DBXFD_NONE);
printf("- cmd: %s:%d\n", container, exit_code);
- dbx_proc_exec(cmd);
+ dbx_proc_exec(DBX_CMD(container, "--version"));
return EXIT_SUCCESS;
}
diff --git a/src/proc.c b/src/proc.c
index 5c6fe83..59aa9a7 100644
--- a/src/proc.c
+++ b/src/proc.c
@@ -9,6 +9,7 @@
#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>
@@ -75,55 +76,93 @@ bool dbx_proc_find(const char *name, char file[PATH_MAX]) {
return found;
}
-int dbx_proc_run(const char *const command[], enum dbx_proc_output out) {
- assert(command != NULL && command[0] != NULL);
+static char **dup_argv(const char *const command[]) {
+ if (command == NULL || command[0] == NULL) {
+ dbx_perror("dup_argv", EINVAL);
+ return NULL;
+ }
- int result;
- posix_spawn_file_actions_t file_actions;
- if ((result = posix_spawn_file_actions_init(&file_actions)) != 0) {
- dbx_perror("spawn_file_actions_init", result);
- return -1;
+ 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) {
+ dbx_perror("malloc", ENOMEM);
+ 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 = -1;
- result = posix_spawn_file_actions_addclose(&file_actions, STDIN_FILENO);
+
+ char **argv = dup_argv(command);
+ if (argv == NULL) {
+ goto exit;
+ }
+
+ posix_spawn_file_actions_t file_actions;
+ int result = posix_spawn_file_actions_init(&file_actions);
if (result != 0) {
- dbx_perror("posix_spawn_file_actions_addclose(stdin)", result);
- goto cleanup;
+ dbx_perror("spawn_file_actions_init", result);
+ goto cleanup_argv;
}
- if ((out & DBXP_STDOUT) == 0) {
+ if ((fds & DBXFD_STDIN) == 0) {
+ result = posix_spawn_file_actions_addopen(&file_actions, STDIN_FILENO,
+ _PATH_DEVNULL, O_RDONLY, 0);
+ if (result != 0) {
+ dbx_perror("posix_spawn_file_actions_addopen(stdin)", result);
+ 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) {
dbx_perror("posix_spawn_file_actions_addopen(stdout)", result);
- goto cleanup;
+ goto cleanup_fa;
}
}
- if ((out & DBXP_STDERR) == 0) {
+ if ((fds & DBXFD_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;
+ goto cleanup_fa;
}
}
- // POSIX guarantees that the pointer is not modified, so the cast is safe.
- char *const *argv = (char *const *)command;
-
pid_t pid;
- result = posix_spawnp(&pid, command[0], &file_actions, NULL, argv, environ);
+ result = posix_spawnp(&pid, argv[0], &file_actions, NULL, argv, environ);
if (result != 0) {
dbx_perror("posix_spawnp", result);
- goto cleanup;
+ goto cleanup_fa;
}
int status;
- if (waitpid(pid, &status, 0) != pid) {
+ while (waitpid(pid, &status, 0) != pid) {
+ if (errno == EINTR) {
+ continue;
+ }
+
dbx_perror("waitpid", errno);
- goto cleanup;
+ goto cleanup_fa;
}
if (WIFEXITED(status)) {
@@ -132,25 +171,157 @@ int dbx_proc_run(const char *const command[], enum dbx_proc_output out) {
exit_code = 128 + WTERMSIG(status);
}
-cleanup:
+cleanup_fa:
result = posix_spawn_file_actions_destroy(&file_actions);
if (result != 0) {
dbx_perror("posix_spawn_file_actions_destroy", result);
- // Don't change the exit code on cleanup failure: issues cleaning up do not
- // change the status of the child process's execution.
}
+
+cleanup_argv:
+ free(argv);
+
+exit:
return exit_code;
}
-bool dbx_proc_exec(const char *const command[]) {
- assert(command != NULL && command[0] != NULL);
+int dbx_proc_output(const char *const command[], char *out, int outlen) {
+ if (out == NULL || outlen < 1) {
+ return -1;
+ }
- // POSIX guarantees that the pointer is not modified, so the cast is safe.
- char *const *argv = (char *const *)command;
+ size_t total = 0;
+ int exit_code = -1;
+
+ char **argv = dup_argv(command);
+ if (argv == NULL) {
+ goto exit;
+ }
- execvp(command[0], argv);
+ int pipe_fds[2];
+ if (pipe(pipe_fds) != 0) {
+ dbx_perror("pipe", errno);
+ goto cleanup_argv;
+ }
+ int read_fd = pipe_fds[0];
+ int write_fd = pipe_fds[1];
+
+ if (write_fd == STDOUT_FILENO) {
+ dbx_printerr("unexpected pipe file descriptor values");
+ goto cleanup_fds;
+ }
+
+ posix_spawn_file_actions_t file_actions;
+ int result = posix_spawn_file_actions_init(&file_actions);
+ if (result != 0) {
+ dbx_perror("spawn_file_actions_init", result);
+ goto cleanup_fds;
+ }
+
+ result = posix_spawn_file_actions_addclose(&file_actions, read_fd);
+ if (result != 0) {
+ dbx_perror("posix_spawn_file_actions_addclose(read_fd)", result);
+ goto cleanup_fa;
+ }
+
+ result =
+ posix_spawn_file_actions_adddup2(&file_actions, write_fd, STDOUT_FILENO);
+ if (result != 0) {
+ dbx_perror("posix_spawn_file_actions_adddup2(stdout)", result);
+ goto cleanup_fa;
+ }
+
+ result = posix_spawn_file_actions_addclose(&file_actions, write_fd);
+ if (result != 0) {
+ dbx_perror("posix_spawn_file_actions_addclose(write_fd)", result);
+ goto cleanup_fa;
+ }
+
+ pid_t pid;
+ result = posix_spawnp(&pid, command[0], &file_actions, NULL, argv, environ);
+ if (result != 0) {
+ dbx_perror("posix_spawnp", result);
+ goto cleanup_fa;
+ }
+
+ if (close(write_fd) != 0) {
+ dbx_perror("close", errno);
+ }
+ write_fd = -1;
+
+ size_t limit = outlen - 1;
+ while (total < limit) {
+ ssize_t n = read(read_fd, out + total, limit - total);
+ if (n == 0) {
+ break;
+ } else if (n < 0) {
+ if (errno == EINTR) {
+ continue;
+ }
+ dbx_perror("read", errno);
+ result = errno;
+ goto cleanup_pid;
+ }
+ total += (size_t)n;
+ }
+ out[total] = '\0';
+
+cleanup_pid:
+ if (close(read_fd) != 0) {
+ dbx_perror("close", errno);
+ }
+ read_fd = -1;
+
+ int status;
+ while (waitpid(pid, &status, 0) != pid) {
+ if (errno == EINTR) {
+ continue;
+ }
+
+ dbx_perror("waitpid", errno);
+ goto cleanup_fa;
+ }
+
+ if (result == 0) {
+ if (WIFEXITED(status)) {
+ exit_code = WEXITSTATUS(status);
+ } else if (WIFSIGNALED(status)) {
+ exit_code = 128 + WTERMSIG(status);
+ }
+ }
+
+cleanup_fa:
+ result = posix_spawn_file_actions_destroy(&file_actions);
+ if (result != 0) {
+ dbx_perror("posix_spawn_file_actions_destroy", result);
+ }
+
+cleanup_fds:
+ if (write_fd >= 0 && close(write_fd) != 0) {
+ dbx_perror("close", errno);
+ }
+ if (read_fd >= 0 && close(read_fd) != 0) {
+ dbx_perror("close", errno);
+ }
+
+cleanup_argv:
+ free(argv);
+
+exit:
+ out[total] = '\0';
+ return exit_code;
+}
+
+bool dbx_proc_exec(const char *const command[]) {
+ char **argv = dup_argv(command);
+ if (argv == NULL) {
+ return false;
+ }
+
+ execvp(argv[0], argv);
// execvp only ever returns if there was an error.
dbx_perror("execv", errno);
+
+ free(argv);
return false;
}
diff --git a/src/ssh.c b/src/ssh.c
index 59cd51a..2c61a30 100644
--- a/src/ssh.c
+++ b/src/ssh.c
@@ -1,3 +1,4 @@
+#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <stdarg.h>
@@ -10,34 +11,33 @@
#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;
+ int result = -1;
const char *xdg = getenv(env);
if (xdg != NULL) {
- result = format_path(path, "%s/dbx", xdg);
+ result = dbx_fpath(path, "%s/dbx", xdg);
} else {
const char *home = getenv("HOME");
if (home != NULL) {
- result = format_path(path, "%s/%s/dbx", home, def);
+ result = dbx_fpath(path, "%s/%s/dbx", home, def);
}
}
- return result;
+ return result >= 0;
+}
+
+static bool get_data_dir(char path[PATH_MAX]) {
+ return get_xdg_dir(path, "XDG_DATA_HOME", ".local/share");
+}
+
+static bool get_config_dir(char path[PATH_MAX]) {
+ return get_xdg_dir(path, "XDG_CONFIG_HOME", ".config");
}
static bool mkdir_p(const char *path) {
char buffer[PATH_MAX];
- if (!format_path(buffer, "%s/", path)) {
+ if (dbx_fpath(buffer, "%s/", path) < 0) {
return false;
}
@@ -54,15 +54,17 @@ static bool mkdir_p(const char *path) {
return true;
}
-bool dbx_ssh_init(const char *name) {
+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_xdg_dir(data_dir, "XDG_DATA_HOME", ".local/share") ||
- !mkdir_p(data_dir)) {
+ if (!get_data_dir(data_dir) || !mkdir_p(data_dir)) {
return false;
}
char ssh_id[PATH_MAX];
- if (!format_path(ssh_id, "%s/%s.id_ed25519", data_dir, name)) {
+ if (dbx_fpath(ssh_id, "%s/%s.id_ed25519", data_dir, name) < 0 ||
+ dbx_fpath(ssh_pubkey, "%s/%s.id_ed25519.pub", data_dir, name) < 0) {
return false;
}
if (unlink(ssh_id) != 0 && errno != ENOENT) {
@@ -70,16 +72,17 @@ bool dbx_ssh_init(const char *name) {
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;
+ return dbx_proc_run(DBX_CMD("ssh-keygen", "-C", "dbx", "-f", ssh_id, "-N", "",
+ "-t", "ed25519"),
+ DBXFD_NONE) == 0;
}
-bool dbx_ssh_set_hostname(const char *name, const char *hostname) {
+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_xdg_dir(data_dir, "XDG_DATA_HOME", ".local/share") ||
- !get_xdg_dir(config_dir, "XDG_CONFIG_HOME", ".config")) {
+ if (!get_data_dir(data_dir) || !get_config_dir(config_dir)) {
return false;
}
@@ -87,10 +90,10 @@ bool dbx_ssh_set_hostname(const char *name, const char *hostname) {
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)) {
+ if (dbx_fpath(ssh_config, "%s/%s.ssh_config", data_dir, name) < 0 ||
+ dbx_fpath(ssh_id, "%s/%s.id_ed25519", data_dir, name) < 0 ||
+ dbx_fpath(known_hosts, "%s/%s.known_hosts", data_dir, name) < 0 ||
+ dbx_fpath(user_config, "%s/%s.ssh_config", config_dir, name) < 0) {
return false;
}
@@ -103,12 +106,14 @@ bool dbx_ssh_set_hostname(const char *name, const char *hostname) {
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, ssh_id, known_hosts, user_config);
+ 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;
@@ -121,3 +126,37 @@ bool dbx_ssh_set_hostname(const char *name, const char *hostname) {
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_fpath(ssh_config, "%s/%s.ssh_config", data_dir, name) < 0) {
+ 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_fpath(ssh_config, "%s/%s.ssh_config", data_dir, name) < 0) {
+ return false;
+ }
+
+ return dbx_proc_run(DBX_CMD("ssh", "-F", ssh_config, name, command),
+ DBXFD_ALL) == 0;
+}