store.c keeps accounts.json, secrets.json and identity.json in a 0700 directory with 0600 files, writes through a temporary file with fsync/rename/dir-fsync, repairs a wrong file mode on load, moves invalid JSON to NAME.bad and rejects path names. test_store covers accounts/secrets/identity, overwrite, corrupt/missing files, stale tmp, read-only directory, default HOME path and permissions. make mcde-host exit=0; test_json, test_frame, test_loop and test_store all ok; ASan/UBSan clean.
408 lines
7.9 KiB
C
408 lines
7.9 KiB
C
/*
|
|
* store.c -- dtchat's on-disk state
|
|
*
|
|
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
|
*
|
|
* ~/.dt/dtchat is 0700, every file 0600. Writes go to a temporary file,
|
|
* are fsynced, renamed over the target and the directory is fsynced. A
|
|
* file that is not valid JSON is moved aside as NAME.bad; it is never
|
|
* silently parsed and never overwritten by a default.
|
|
*/
|
|
#include "dc_internal.h"
|
|
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/types.h>
|
|
#include <unistd.h>
|
|
|
|
#ifndef PATH_MAX
|
|
#define PATH_MAX 4096
|
|
#endif
|
|
|
|
struct DcStore {
|
|
char *dir;
|
|
};
|
|
|
|
static const char *
|
|
errtext(int err)
|
|
{
|
|
return strerror(err);
|
|
}
|
|
|
|
static int
|
|
valid_name(const char *name)
|
|
{
|
|
const unsigned char *p;
|
|
size_t len;
|
|
|
|
if (name == NULL || *name == '\0' || *name == '.' ||
|
|
(len = strlen(name)) > 200)
|
|
return 0;
|
|
for (p = (const unsigned char *)name; *p != '\0'; p++) {
|
|
if ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') ||
|
|
(*p >= '0' && *p <= '9') || *p == '.' || *p == '_' ||
|
|
*p == '-')
|
|
continue;
|
|
return 0;
|
|
}
|
|
return strstr(name, "..") == NULL;
|
|
}
|
|
|
|
static int
|
|
join_path(char *out, size_t cap, const char *dir, const char *name)
|
|
{
|
|
int n;
|
|
|
|
if (dir == NULL || name == NULL)
|
|
return -1;
|
|
n = snprintf(out, cap, "%s/%s", dir, name);
|
|
return (n > 0 && (size_t)n < cap) ? 0 : -1;
|
|
}
|
|
|
|
static int
|
|
make_dir_p(const char *path)
|
|
{
|
|
char tmp[PATH_MAX];
|
|
struct stat st;
|
|
char *p;
|
|
size_t len;
|
|
|
|
if (path == NULL || *path == '\0' || strcmp(path, "/") == 0)
|
|
return -1;
|
|
len = strlen(path);
|
|
if (len >= sizeof(tmp))
|
|
return -1;
|
|
memcpy(tmp, path, len + 1);
|
|
|
|
for (p = tmp + 1; *p != '\0'; p++) {
|
|
if (*p != '/')
|
|
continue;
|
|
*p = '\0';
|
|
if (mkdir(tmp, 0700) != 0 && errno != EEXIST)
|
|
return -1;
|
|
*p = '/';
|
|
}
|
|
if (mkdir(tmp, 0700) != 0 && errno != EEXIST)
|
|
return -1;
|
|
if (stat(tmp, &st) != 0 || !S_ISDIR(st.st_mode))
|
|
return -1;
|
|
if (chmod(tmp, 0700) != 0)
|
|
return -1;
|
|
return 0;
|
|
}
|
|
|
|
DcStore *
|
|
dc_store_open(const char *dir)
|
|
{
|
|
char home_path[PATH_MAX];
|
|
const char *home;
|
|
DcStore *store;
|
|
|
|
if (dir == NULL) {
|
|
home = getenv("HOME");
|
|
if (home == NULL || *home == '\0')
|
|
return NULL;
|
|
if (snprintf(home_path, sizeof(home_path), "%s/.dt/dtchat",
|
|
home) >= (int)sizeof(home_path))
|
|
return NULL;
|
|
dir = home_path;
|
|
}
|
|
if (make_dir_p(dir) != 0)
|
|
return NULL;
|
|
store = calloc(1, sizeof(*store));
|
|
if (store == NULL)
|
|
return NULL;
|
|
store->dir = strdup(dir);
|
|
if (store->dir == NULL) {
|
|
free(store);
|
|
return NULL;
|
|
}
|
|
return store;
|
|
}
|
|
|
|
void
|
|
dc_store_close(DcStore *store)
|
|
{
|
|
if (store == NULL)
|
|
return;
|
|
free(store->dir);
|
|
free(store);
|
|
}
|
|
|
|
const char *
|
|
dc_store_dir(const DcStore *store)
|
|
{
|
|
return store ? store->dir : NULL;
|
|
}
|
|
|
|
static char *
|
|
read_file(const char *path, size_t *len, int *missing, char *err,
|
|
size_t errlen)
|
|
{
|
|
struct stat st;
|
|
char *buf;
|
|
size_t used = 0;
|
|
int fd;
|
|
|
|
*missing = 0;
|
|
fd = open(path, O_RDONLY | O_CLOEXEC);
|
|
if (fd < 0) {
|
|
if (errno == ENOENT) {
|
|
*missing = 1;
|
|
return NULL;
|
|
}
|
|
if (err != NULL && errlen > 0)
|
|
snprintf(err, errlen, "open %s: %s", path,
|
|
errtext(errno));
|
|
return NULL;
|
|
}
|
|
if (fstat(fd, &st) != 0) {
|
|
if (err != NULL && errlen > 0)
|
|
snprintf(err, errlen, "stat %s: %s", path,
|
|
errtext(errno));
|
|
close(fd);
|
|
return NULL;
|
|
}
|
|
if (st.st_size < 0 || (unsigned long long)st.st_size >
|
|
DC_IPC_MAX_LINE) {
|
|
if (err != NULL && errlen > 0)
|
|
snprintf(err, errlen, "%s: file too large", path);
|
|
close(fd);
|
|
return NULL;
|
|
}
|
|
buf = malloc((size_t)st.st_size + 1);
|
|
if (buf == NULL) {
|
|
if (err != NULL && errlen > 0)
|
|
snprintf(err, errlen, "out of memory");
|
|
close(fd);
|
|
return NULL;
|
|
}
|
|
while (used < (size_t)st.st_size) {
|
|
ssize_t n = read(fd, buf + used, (size_t)st.st_size - used);
|
|
|
|
if (n < 0) {
|
|
if (errno == EINTR)
|
|
continue;
|
|
if (err != NULL && errlen > 0)
|
|
snprintf(err, errlen, "read %s: %s", path,
|
|
errtext(errno));
|
|
free(buf);
|
|
close(fd);
|
|
return NULL;
|
|
}
|
|
if (n == 0)
|
|
break;
|
|
used += (size_t)n;
|
|
}
|
|
close(fd);
|
|
buf[used] = '\0';
|
|
*len = used;
|
|
return buf;
|
|
}
|
|
|
|
static void
|
|
move_bad(DcStore *store, const char *name)
|
|
{
|
|
char path[PATH_MAX];
|
|
char bad[PATH_MAX];
|
|
|
|
if (join_path(path, sizeof(path), store->dir, name) != 0)
|
|
return;
|
|
if (snprintf(bad, sizeof(bad), "%s/%s.bad", store->dir, name) >=
|
|
(int)sizeof(bad))
|
|
return;
|
|
unlink(bad);
|
|
(void)rename(path, bad);
|
|
}
|
|
|
|
DcJson *
|
|
dc_store_load(DcStore *store, const char *name, int *missing, char *err,
|
|
size_t errlen)
|
|
{
|
|
char path[PATH_MAX];
|
|
char *text;
|
|
size_t len = 0;
|
|
int miss = 0;
|
|
DcJson *json;
|
|
|
|
if (missing != NULL)
|
|
*missing = 0;
|
|
if (err != NULL && errlen > 0)
|
|
err[0] = '\0';
|
|
if (store == NULL || !valid_name(name)) {
|
|
if (err != NULL && errlen > 0)
|
|
snprintf(err, errlen, "invalid store name");
|
|
return NULL;
|
|
}
|
|
if (join_path(path, sizeof(path), store->dir, name) != 0) {
|
|
if (err != NULL && errlen > 0)
|
|
snprintf(err, errlen, "store path too long");
|
|
return NULL;
|
|
}
|
|
text = read_file(path, &len, &miss, err, errlen);
|
|
if (text == NULL) {
|
|
if (miss && missing != NULL)
|
|
*missing = 1;
|
|
return NULL;
|
|
}
|
|
json = dc_json_parse(text, len, err, errlen);
|
|
free(text);
|
|
if (json == NULL) {
|
|
move_bad(store, name);
|
|
if (err != NULL && errlen > 0 && err[0] == '\0')
|
|
snprintf(err, errlen, "invalid JSON in %s", name);
|
|
return NULL;
|
|
}
|
|
(void)chmod(path, 0600);
|
|
return json;
|
|
}
|
|
|
|
static void
|
|
sync_dir(const char *dir)
|
|
{
|
|
int fd = open(dir, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
|
|
|
|
if (fd < 0)
|
|
return;
|
|
(void)fsync(fd);
|
|
close(fd);
|
|
}
|
|
|
|
int
|
|
dc_store_save(DcStore *store, const char *name, const DcJson *json)
|
|
{
|
|
char path[PATH_MAX];
|
|
char tmp[PATH_MAX];
|
|
char *text;
|
|
size_t len = 0;
|
|
size_t off = 0;
|
|
static unsigned counter;
|
|
int fd;
|
|
|
|
if (store == NULL || !valid_name(name) || json == NULL)
|
|
return -1;
|
|
text = dc_json_write(json, &len);
|
|
if (text == NULL || len > DC_IPC_MAX_LINE) {
|
|
free(text);
|
|
return -1;
|
|
}
|
|
if (join_path(path, sizeof(path), store->dir, name) != 0) {
|
|
free(text);
|
|
return -1;
|
|
}
|
|
if (snprintf(tmp, sizeof(tmp), "%s/.%s.tmp.%ld.%u", store->dir,
|
|
name, (long)getpid(), counter++) >= (int)sizeof(tmp)) {
|
|
free(text);
|
|
return -1;
|
|
}
|
|
fd = open(tmp, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, 0600);
|
|
if (fd < 0 && errno == EEXIST) {
|
|
unlink(tmp);
|
|
fd = open(tmp, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, 0600);
|
|
}
|
|
if (fd < 0) {
|
|
free(text);
|
|
return -1;
|
|
}
|
|
(void)fchmod(fd, 0600);
|
|
while (off < len) {
|
|
ssize_t n = write(fd, text + off, len - off);
|
|
|
|
if (n < 0) {
|
|
if (errno == EINTR)
|
|
continue;
|
|
close(fd);
|
|
unlink(tmp);
|
|
free(text);
|
|
return -1;
|
|
}
|
|
if (n == 0)
|
|
break;
|
|
off += (size_t)n;
|
|
}
|
|
{
|
|
int failed = 0;
|
|
|
|
if (off != len)
|
|
failed = 1;
|
|
if (!failed && fsync(fd) != 0)
|
|
failed = 1;
|
|
if (close(fd) != 0)
|
|
failed = 1;
|
|
fd = -1;
|
|
if (failed) {
|
|
unlink(tmp);
|
|
free(text);
|
|
return -1;
|
|
}
|
|
}
|
|
if (rename(tmp, path) != 0) {
|
|
unlink(tmp);
|
|
free(text);
|
|
return -1;
|
|
}
|
|
sync_dir(store->dir);
|
|
free(text);
|
|
return 0;
|
|
}
|
|
|
|
static DcJson *
|
|
empty_object(void)
|
|
{
|
|
return dc_json_new_object();
|
|
}
|
|
|
|
DcJson *
|
|
dc_store_load_accounts(DcStore *store)
|
|
{
|
|
int missing = 0;
|
|
DcJson *json = dc_store_load(store, "accounts.json", &missing, NULL, 0);
|
|
DcJson *empty;
|
|
|
|
if (json != NULL)
|
|
return json;
|
|
empty = empty_object();
|
|
if (empty != NULL)
|
|
(void)dc_json_object_set(empty, "accounts",
|
|
dc_json_new_array());
|
|
return empty;
|
|
}
|
|
|
|
int
|
|
dc_store_save_accounts(DcStore *store, const DcJson *json)
|
|
{
|
|
return dc_store_save(store, "accounts.json", json);
|
|
}
|
|
|
|
DcJson *
|
|
dc_store_load_secrets(DcStore *store)
|
|
{
|
|
DcJson *json = dc_store_load(store, "secrets.json", NULL, NULL, 0);
|
|
|
|
if (json != NULL)
|
|
return json;
|
|
return empty_object();
|
|
}
|
|
|
|
int
|
|
dc_store_save_secrets(DcStore *store, const DcJson *json)
|
|
{
|
|
return dc_store_save(store, "secrets.json", json);
|
|
}
|
|
|
|
DcJson *
|
|
dc_store_load_identity(DcStore *store)
|
|
{
|
|
return dc_store_load(store, "identity.json", NULL, NULL, 0);
|
|
}
|
|
|
|
int
|
|
dc_store_save_identity(DcStore *store, const DcJson *json)
|
|
{
|
|
return dc_store_save(store, "identity.json", json);
|
|
}
|