Skip to content
suit Artifact [e5c31d2bc5]
Login

Artifact [e5c31d2bc5]

Artifact e5c31d2bc550aec5c4b7646cca1c1102494317454a5eafb327bd633c5b91aa9d:


#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.
//
// credential-commands: mode 0600, one line per wardrobe, "NAME USER PROG ARG...".
// Fields are one space apart, with no quoting, so an argument has no space or
// newline. PROG is an absolute path. When a wardrobe has one, the command is
// run at sync time and its output is the secret, in place of a stored one.
//
// NAME.refused: mode 0600, the fingerprint of a login the remote refused: 64
// lowercase hex digits and a newline. A tend does not try that login again.

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;

constexpr size_t CRED_CMD_MAX = 1024;
constexpr size_t CRED_CMD_WORDS_MAX = 16;
// How long a credential command may run before it is killed.
constexpr unsigned CRED_CMD_SECONDS = 10;

typedef struct {
    char name[129];
    char user[CRED_USER_MAX + 1];
    char cmd[CRED_CMD_MAX + 1];  // "PROG ARG...": words of printable characters, one space apart
} CredCmdEntry;

typedef struct {
    uint32_t n;
    CredCmdEntry e[REMOTE_ENTRY_MAX];
} CredCmds;

// A fingerprint: 64 hex digits and the end of the string.
constexpr size_t FINGERPRINT_SIZE = 65;

// 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);
[[nodiscard]] bool credcmds_parse(CredCmds *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);
// Like creds_load, it refuses a file that anyone but its owner can use: what it names is run.
[[nodiscard]] bool credcmds_load(CredCmds *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);
int credcmds_write(const CredCmds *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);
CredCmdEntry *credcmds_find(CredCmds *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 fingerprint of a login: a hash of the address, the user and the secret sent, as
// FINGERPRINT_SIZE bytes. The secret cannot be had back from it.
void login_fingerprint(const char *url, const char *user, const char *secret, char *out);

// NAME.refused: DATA is a fingerprint and a newline.
[[nodiscard]] bool refused_parse(const char *data, size_t len, char *fingerprint);
// Read PATH. A file that is not there is HAVE false.
[[nodiscard]] bool refused_load(const char *path, char *fingerprint, bool *have, RemoteError *err);
int refused_write(const char *path, const char *fingerprint);
// $SUIT_HOME/NAME.refused into OUT; the caller has found the home.
void refused_path(const char *name, char *out, size_t cap);
// Remove it; there being none is fine. 0, or 1 after saying why not.
int refused_remove(const char *name);

// Run the credential command CMD ("PROG ARG...") of the wardrobe NAME: standard input from
// /dev/null, standard output to a pipe, standard error as it is, no shell, and at most
// CRED_CMD_SECONDS. What it prints, less one newline at its end, is put in SECRET
// (CRED_SECRET_MAX + 1 bytes). False, with SECRET wiped, after saying on stderr what went wrong,
// starting with "suit: VERB NAME": a status that is not 0, a signal, the time limit, output that
// is empty, too long or not a secret.
[[nodiscard]] bool credential_run(const char *verb, const char *name, const char *cmd, char *secret);

// 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, and forgets a credential command of NAME and a refusal:
// a login given again is tried again, even with the same secret (an account on hold may be let back in).
int cmd_login(const char *name, const char *user);
// Keeps the command PROG ARG... (ARGV, ARGC of them) in place of a secret, and forgets NAME's
// secret and a refusal. 2 for a PROG that is not an absolute path or a word that has a space or is not printable.
int cmd_login_command(const char *name, const char *user, int argc, char *const *argv);
// Forgets NAME's secret and command, and a refusal.
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