diff --git a/include/cfg.h b/include/cfg.h index 0a1a1da..68ebaa7 100644 --- a/include/cfg.h +++ b/include/cfg.h @@ -35,8 +35,10 @@ X(string, xkb_layout) \ X(string, xkb_variant) \ X(string, xkb_options) \ + X(bool, use_relative_paths) \ struct cfg { + char* directory; #define string char* #define uint uint32_t #define X(type, name) type name; diff --git a/src/cfg.c b/src/cfg.c index 9caf385..dd4ff5f 100644 --- a/src/cfg.c +++ b/src/cfg.c @@ -18,6 +18,8 @@ #include #include #include +#include +#include #include "cfg.h" @@ -106,11 +108,16 @@ static int cfg__load_line(struct cfg* self, char* line) return cfg__load_key_value(self, key, value); } +static char* cfg__dirname(const char* path) +{ + char buffer[PATH_MAX]; + return strdup(dirname(realpath(path, buffer))); +} + int cfg_load(struct cfg* self, const char* requested_path) { const char* path = requested_path ? requested_path : cfg__get_default_path(); - if (!path) return -1; @@ -118,6 +125,8 @@ int cfg_load(struct cfg* self, const char* requested_path) if (!stream) return -1; + self->directory = cfg__dirname(path); + char* line = NULL; size_t len = 0; int lineno = 0; @@ -136,6 +145,7 @@ int cfg_load(struct cfg* self, const char* requested_path) failure: cfg_destroy(self); free(line); + free(self->directory); fclose(stream); return lineno; } @@ -153,4 +163,5 @@ void cfg_destroy(struct cfg* self) #undef DESTROY_string #undef DESTROY_uint #undef DESTROY_bool + free(self->directory); } diff --git a/src/main.c b/src/main.c index 8d608b6..4c43d83 100644 --- a/src/main.c +++ b/src/main.c @@ -705,7 +705,18 @@ static int blank_screen(struct wayvnc* self) return 0; } -int init_nvnc(struct wayvnc* self, const char* addr, uint16_t port, +static char* get_cfg_path(const struct cfg* cfg, char* dst, const char* src) +{ + if (!cfg->use_relative_paths) { + strlcpy(dst, src, PATH_MAX); + return dst; + } + + snprintf(dst, PATH_MAX, "%s/%s", cfg->directory, src); + return dst; +} + +static int init_nvnc(struct wayvnc* self, const char* addr, uint16_t port, enum socket_type socket_type) { switch (socket_type) { @@ -755,16 +766,25 @@ int init_nvnc(struct wayvnc* self, const char* addr, uint16_t port, } if (self->cfg.rsa_private_key_file) { - if (nvnc_set_rsa_creds(self->nvnc, self->cfg.rsa_private_key_file) < 0) { + char tmp[PATH_MAX]; + const char* key_file = get_cfg_path(&self->cfg, tmp, + self->cfg.rsa_private_key_file); + if (nvnc_set_rsa_creds(self->nvnc, key_file) < 0) { nvnc_log(NVNC_LOG_ERROR, "Failed to load RSA credentials"); goto failure; } } if (self->cfg.private_key_file) { - int r = nvnc_set_tls_creds(self->nvnc, - self->cfg.private_key_file, + char key_file[PATH_MAX]; + char cert_file[PATH_MAX]; + + get_cfg_path(&self->cfg, key_file, + self->cfg.private_key_file); + get_cfg_path(&self->cfg, cert_file, self->cfg.certificate_file); + int r = nvnc_set_tls_creds(self->nvnc, key_file, + cert_file); if (r < 0) { nvnc_log(NVNC_LOG_ERROR, "Failed to enable TLS authentication"); goto failure;