Add config option to make paths relative to config file

pull/143/merge
Andri Yngvason 2023-09-29 20:33:41 +00:00
parent 16ff0fa11d
commit 6d59ab8323
3 changed files with 38 additions and 5 deletions

View File

@ -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;

View File

@ -18,6 +18,8 @@
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <libgen.h>
#include <limits.h>
#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);
}

View File

@ -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;