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.
//
// 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 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);

// 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);

// 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);

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

// 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.
int cmd_remote(const char *name, const char *url);
// Reads the secret from standard input.
int cmd_login(const char *name, const char *user);
int cmd_logout(const char *name);

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

#endif