Skip to content
suit Documentation
Login

Documentation

#ifndef SUIT_REMOTE_H
#define SUIT_REMOTE_H

#include <stddef.h>
#include <stdint.h>

// Two small files in $SUIT_HOME, each parsed strictly (a line that is not
// exactly what is described is an error and the whole file is rejected) and
// rewritten atomically.
//
// remotes: one line per wardrobe, "NAME URL". A URL is file://ABSOLUTE/PATH,
// http://HOST[:PORT]/PATH or https://HOST[:PORT]/PATH, with no user name or
// password in it.
//
// cafiles: one line per wardrobe whose https remote has a CA file of its own,
// "NAME PATH", PATH absolute. Suit trusts that file in place of the system's
// bundle for that remote.
//
// credentials: mode 0600, one line per wardrobe, "NAME USER SECRET". The
// secret is printable ASCII with no space. It is never printed or written
// anywhere else, and is wiped from memory after use.

constexpr size_t REMOTE_FILE_MAX = 65536;
constexpr size_t REMOTE_URL_MAX = 1024;
constexpr size_t REMOTE_LINE_MAX = 1280;
constexpr uint32_t REMOTE_ENTRY_MAX = 256;
constexpr size_t CRED_USER_MAX = 64;
constexpr size_t CRED_SECRET_MAX = 256;

typedef struct {
    int line;  // 0 when the error concerns the whole file
    char msg[200];
} RemoteError;

typedef struct {
    char name[129];
    char url[REMOTE_URL_MAX + 1];
} RemoteEntry;

typedef struct {
    uint32_t n;
    RemoteEntry e[REMOTE_ENTRY_MAX];
} Remotes;

typedef struct {
    char name[129];
    char path[REMOTE_URL_MAX + 1];
} CaEntry;

typedef struct {
    uint32_t n;
    CaEntry e[REMOTE_ENTRY_MAX];
} CaFiles;

typedef struct {
    char name[129];
    char user[CRED_USER_MAX + 1];
    char secret[CRED_SECRET_MAX + 1];
} CredEntry;

typedef struct {
    uint32_t n;
    CredEntry e[REMOTE_ENTRY_MAX];
} Creds;

// Nullptr if the LEN bytes are an address suit accepts, else why not. The
// reason never repeats the address, which might carry a password.
const char *remote_url_check(const char *url, size_t len);

// Parse DATA. On failure R is empty and ERR says what and where.
[[nodiscard]] bool remotes_parse(Remotes *r, const char *data, size_t len, RemoteError *err);
[[nodiscard]] bool creds_parse(Creds *c, const char *data, size_t len, RemoteError *err);
[[nodiscard]] bool cafiles_parse(CaFiles *c, const char *data, size_t len, RemoteError *err);

// Read PATH. A file that is not there is an empty list. creds_load also
// refuses a file that anyone but its owner can use.
[[nodiscard]] bool remotes_load(Remotes *r, const char *path, RemoteError *err);
[[nodiscard]] bool creds_load(Creds *c, const char *path, RemoteError *err);
[[nodiscard]] bool cafiles_load(CaFiles *c, const char *path, RemoteError *err);

// Write the entries sorted by name, atomically. Returns 0 or an errno value.
int remotes_write(const Remotes *r, const char *path);
int creds_write(const Creds *c, const char *path);
int cafiles_write(const CaFiles *c, const char *path);

RemoteEntry *remotes_find(Remotes *r, const char *name);
CredEntry *creds_find(Creds *c, const char *name);
CaEntry *cafiles_find(CaFiles *c, const char *name);

// The CA file recorded for NAME in OUT, empty when there is none. 0, or 1
// after saying on stderr why the cafiles file cannot be used.
int remote_ca(const char *name, char *out, size_t cap);

// Before anything reads a secret: turn off core files, which could hold it.
// False after saying why on stderr.
[[nodiscard]] bool no_core_dumps(void);

// Overwrite N bytes at P so the compiler cannot drop it.
void secret_wipe(void *p, size_t n);

// The host (and port) part of URL, or for file:// its path, for messages.
void remote_host(const char *url, char *out, size_t cap);

// The commands. Each returns an exit code after reporting on stderr or stdout.
// cmd_remote: URL null shows (exit 1 if none), "--none" clears. CA, when not
// null, is the https remote's own CA file; setting a remote without one
// forgets the old one.
int cmd_remote(const char *name, const char *url, const char *ca);
// Reads the secret from standard input.
int cmd_login(const char *name, const char *user);
int cmd_logout(const char *name);

// $SUIT_HOME/NAME.syncfail: how many syncs of a wardrobe have failed in a row,
// as 1 to 6 decimal digits (no leading zero, at least 1) and a newline. No
// such file means none. Rewritten atomically.
constexpr unsigned SYNC_FAILS_MAX = 999999;
[[nodiscard]] bool syncfail_parse(const char *data, size_t len, unsigned *n);
[[nodiscard]] bool syncfail_load(const char *path, unsigned *n, RemoteError *err);
int syncfail_write(const char *path, unsigned n);

// The files' paths for the sync.
int remote_paths(char *remotes, char *creds, size_t cap);

#endif