Import refactored xdg.c from pounce

weechat-hashes
C. McEnroe 2021-10-16 17:03:31 -04:00
parent 54c632e8c3
commit b6cf41ab77
5 changed files with 67 additions and 70 deletions

4
chat.c
View File

@ -149,8 +149,8 @@ static void sandboxEarly(bool log) {
char *end = &promises[sizeof(promises)]; char *end = &promises[sizeof(promises)];
if (log) { if (log) {
const char *logdir = dataMkdir("log"); char buf[PATH_MAX];
int error = unveil(logdir, "wc"); int error = unveil(dataPath(buf, sizeof(buf), "log", 0), "wc");
if (error) err(EX_OSERR, "unveil"); if (error) err(EX_OSERR, "unveil");
ptr = seprintf(ptr, end, " wpath cpath"); ptr = seprintf(ptr, end, " wpath cpath");
} }

5
chat.h
View File

@ -404,11 +404,10 @@ void logFormat(uint id, const time_t *time, const char *format, ...)
__attribute__((format(printf, 3, 4))); __attribute__((format(printf, 3, 4)));
void logClose(void); void logClose(void);
const char *configPath(const char **dirs, const char *path); char *configPath(char *buf, size_t cap, const char *path, int i);
const char *dataPath(const char **dirs, const char *path); char *dataPath(char *buf, size_t cap, const char *path, int i);
FILE *configOpen(const char *path, const char *mode); FILE *configOpen(const char *path, const char *mode);
FILE *dataOpen(const char *path, const char *mode); FILE *dataOpen(const char *path, const char *mode);
const char *dataMkdir(const char *path);
int getopt_config( int getopt_config(
int argc, char *const *argv, int argc, char *const *argv,

22
irc.c
View File

@ -29,6 +29,7 @@
#include <err.h> #include <err.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <limits.h>
#include <netdb.h> #include <netdb.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <stdarg.h> #include <stdarg.h>
@ -49,19 +50,20 @@ static struct tls_config *config;
void ircConfig( void ircConfig(
bool insecure, const char *trust, const char *cert, const char *priv bool insecure, const char *trust, const char *cert, const char *priv
) { ) {
int error = 0;
char buf[PATH_MAX];
config = tls_config_new(); config = tls_config_new();
if (!config) errx(EX_SOFTWARE, "tls_config_new"); if (!config) errx(EX_SOFTWARE, "tls_config_new");
int error = 0;
if (insecure) { if (insecure) {
tls_config_insecure_noverifycert(config); tls_config_insecure_noverifycert(config);
tls_config_insecure_noverifyname(config); tls_config_insecure_noverifyname(config);
} }
if (trust) { if (trust) {
tls_config_insecure_noverifyname(config); tls_config_insecure_noverifyname(config);
const char *dirs = NULL; for (int i = 0; configPath(buf, sizeof(buf), trust, i); ++i) {
for (const char *path; NULL != (path = configPath(&dirs, trust));) { error = tls_config_set_ca_file(config, buf);
error = tls_config_set_ca_file(config, path);
if (!error) break; if (!error) break;
} }
if (error) errx(EX_NOINPUT, "%s: %s", trust, tls_config_error(config)); if (error) errx(EX_NOINPUT, "%s: %s", trust, tls_config_error(config));
@ -79,21 +81,19 @@ void ircConfig(
#endif #endif
if (cert) { if (cert) {
const char *dirs = NULL; for (int i = 0; configPath(buf, sizeof(buf), cert, i); ++i) {
for (const char *path; NULL != (path = configPath(&dirs, cert));) {
if (priv) { if (priv) {
error = tls_config_set_cert_file(config, path); error = tls_config_set_cert_file(config, buf);
} else { } else {
error = tls_config_set_keypair_file(config, path, path); error = tls_config_set_keypair_file(config, buf, buf);
} }
if (!error) break; if (!error) break;
} }
if (error) errx(EX_NOINPUT, "%s: %s", cert, tls_config_error(config)); if (error) errx(EX_NOINPUT, "%s: %s", cert, tls_config_error(config));
} }
if (priv) { if (priv) {
const char *dirs = NULL; for (int i = 0; configPath(buf, sizeof(buf), priv, i); ++i) {
for (const char *path; NULL != (path = configPath(&dirs, priv));) { error = tls_config_set_key_file(config, buf);
error = tls_config_set_key_file(config, path);
if (!error) break; if (!error) break;
} }
if (error) errx(EX_NOINPUT, "%s: %s", priv, tls_config_error(config)); if (error) errx(EX_NOINPUT, "%s: %s", priv, tls_config_error(config));

15
log.c
View File

@ -47,10 +47,15 @@
static int logDir = -1; static int logDir = -1;
void logOpen(void) { void logOpen(void) {
dataMkdir(""); char buf[PATH_MAX];
const char *path = dataMkdir("log"); int error = mkdir(dataPath(buf, sizeof(buf), "", 0), S_IRWXU);
logDir = open(path, O_RDONLY | O_CLOEXEC); if (error && errno != EEXIST) err(EX_CANTCREAT, "%s", buf);
if (logDir < 0) err(EX_CANTCREAT, "%s", path);
error = mkdir(dataPath(buf, sizeof(buf), "log", 0), S_IRWXU);
if (error && errno != EEXIST) err(EX_CANTCREAT, "%s", buf);
logDir = open(buf, O_RDONLY | O_CLOEXEC);
if (logDir < 0) err(EX_CANTCREAT, "%s", buf);
#ifdef __FreeBSD__ #ifdef __FreeBSD__
cap_rights_t rights; cap_rights_t rights;
@ -58,7 +63,7 @@ void logOpen(void) {
&rights, CAP_MKDIRAT, CAP_CREATE, CAP_WRITE, &rights, CAP_MKDIRAT, CAP_CREATE, CAP_WRITE,
/* for fdopen(3) */ CAP_FCNTL, CAP_FSTAT /* for fdopen(3) */ CAP_FCNTL, CAP_FSTAT
); );
int error = caph_rights_limit(logDir, &rights); error = caph_rights_limit(logDir, &rights);
if (error) err(EX_OSERR, "cap_rights_limit"); if (error) err(EX_OSERR, "cap_rights_limit");
#endif #endif
} }

91
xdg.c
View File

@ -32,6 +32,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sysexits.h>
#include "chat.h" #include "chat.h"
@ -58,81 +59,73 @@ static const struct Base Data = {
.defDirs = "/usr/local/share:/usr/share", .defDirs = "/usr/local/share:/usr/share",
}; };
static const char * static char *basePath(
basePath(struct Base base, const char **dirs, const char *path) { struct Base base, char *buf, size_t cap, const char *path, int i
static char buf[PATH_MAX]; ) {
if (path[strspn(path, ".")] == '/') {
if (*dirs) { if (i > 0) return NULL;
if (!**dirs) return NULL; snprintf(buf, cap, "%s", path);
size_t len = strcspn(*dirs, ":");
snprintf(buf, sizeof(buf), "%.*s/" SUBDIR "/%s", (int)len, *dirs, path);
*dirs += len;
if (**dirs) *dirs += 1;
return buf; return buf;
} }
if (path[strspn(path, ".")] == '/') { if (i > 0) {
*dirs = ""; const char *dirs = getenv(base.envDirs);
return path; if (!dirs) dirs = base.defDirs;
for (; i > 1; --i) {
dirs += strcspn(dirs, ":");
dirs += (*dirs == ':');
}
if (!*dirs) return NULL;
snprintf(
buf, cap, "%.*s/" SUBDIR "/%s",
(int)strcspn(dirs, ":"), dirs, path
);
return buf;
} }
*dirs = getenv(base.envDirs);
if (!*dirs) *dirs = base.defDirs;
const char *home = getenv("HOME"); const char *home = getenv("HOME");
const char *baseHome = getenv(base.envHome); const char *baseHome = getenv(base.envHome);
if (baseHome) { if (baseHome) {
snprintf(buf, sizeof(buf), "%s/" SUBDIR "/%s", baseHome, path); snprintf(buf, cap, "%s/" SUBDIR "/%s", baseHome, path);
} else if (home) { } else if (home) {
snprintf( snprintf(buf, cap, "%s/%s/" SUBDIR "/%s", home, base.defHome, path);
buf, sizeof(buf), "%s/%s/" SUBDIR "/%s",
home, base.defHome, path
);
} else { } else {
errx(EX_CONFIG, "HOME unset"); errx(EX_USAGE, "HOME unset");
} }
return buf; return buf;
} }
const char *configPath(const char **dirs, const char *path) { char *configPath(char *buf, size_t cap, const char *path, int i) {
return basePath(Config, dirs, path); return basePath(Config, buf, cap, path, i);
} }
const char *dataPath(const char **dirs, const char *path) { char *dataPath(char *buf, size_t cap, const char *path, int i) {
return basePath(Data, dirs, path); return basePath(Data, buf, cap, path, i);
} }
FILE *configOpen(const char *path, const char *mode) { FILE *configOpen(const char *path, const char *mode) {
const char *dirs = NULL; char buf[PATH_MAX];
for (const char *abs; NULL != (abs = configPath(&dirs, path));) { for (int i = 0; configPath(buf, sizeof(buf), path, i); ++i) {
FILE *file = fopen(abs, mode); FILE *file = fopen(buf, mode);
if (file) return file; if (file) return file;
if (errno != ENOENT) warn("%s", abs); if (errno != ENOENT) warn("%s", buf);
} }
dirs = NULL; warn("%s", configPath(buf, sizeof(buf), path, 0));
warn("%s", configPath(&dirs, path));
return NULL; return NULL;
} }
const char *dataMkdir(const char *path) {
const char *dirs = NULL;
path = dataPath(&dirs, path);
int error = mkdir(path, S_IRWXU);
if (error && errno != EEXIST) err(EX_CANTCREAT, "%s", path);
return path;
}
FILE *dataOpen(const char *path, const char *mode) { FILE *dataOpen(const char *path, const char *mode) {
const char *dirs = NULL; char buf[PATH_MAX];
for (const char *abs; NULL != (abs = dataPath(&dirs, path));) { for (int i = 0; dataPath(buf, sizeof(buf), path, i); ++i) {
FILE *file = fopen(abs, mode); FILE *file = fopen(buf, mode);
if (file) return file; if (file) return file;
if (errno != ENOENT) warn("%s", abs); if (errno != ENOENT) warn("%s", buf);
} }
if (mode[0] != 'r') dataMkdir(""); if (mode[0] != 'r') {
dirs = NULL; int error = mkdir(dataPath(buf, sizeof(buf), "", 0), S_IRWXU);
path = dataPath(&dirs, path); if (error && errno != EEXIST) warn("%s", buf);
FILE *file = fopen(path, mode); }
if (!file) warn("%s", path); FILE *file = fopen(dataPath(buf, sizeof(buf), path, 0), mode);
if (!file) warn("%s", buf);
return file; return file;
} }