Artifact b7e1cac36e1b69d7b9e811234f74afcea3415d9cd348164a245c6a82ef418cf2:
- File
src/remote.c
— part of check-in
[27d847f693]
at
2026-11-18 16:19:19
on branch trunk
— suit syncs and clones over https
suit sync, suit clone and suit tend now work with https:// remotes. http:// and file:// keep libfossil's built-in channel.
libfossil is the private byot build 2f20f44ba4 in place of trunk f2983dcd78, approved while suit is being proven; only libfossil.c and libfossil.h differ from trunk's files. Its stream channel does all of the HTTP: request headers, Content-Length, chunked and close-delimited replies (the last only after a clean close), the reply-size limit and the timeout. suit supplies the transport: src/tls.c, over non-blocking TCP and BearSSL 0.6, now vendored.
- TLS 1.2 only, ECDHE key exchange, AES-GCM or ChaCha20-Poly1305. A server offering only TLS 1.3, RSA key exchange or CBC is refused. - The chain is verified against trust anchors and the host name against the certificate. Anchors come from the system's PEM bundle read at run time (SSL_CERT_FILE, then the usual macOS and Linux paths), or from a per-wardrobe CA file set with --ca, which replaces the bundle. No roots are compiled in and there is no way to trust anything. - A certificate file must be a regular file owned by the user or root, writable by no one else, in a folder that passes the same check; --ca refuses a symlink, and a FIFO is refused at once. - Redirects are refused, never followed, and the message names the new address. No proxy support. - Nothing is sent before the handshake has verified. The login goes out only as libfossil's login card. Commands that read the sync secret turn core files off first. - Every connect, read, write and name lookup waits in poll with a 30 s idle limit, and a whole command stops after 10 minutes. getaddrinfo has no deadline, so it runs in a forked child that is killed and reaped. - BearSSL makes no system call of its own: it is built without its own randomness or clock, and suit seeds it and gives it the time through its fault wrappers. - Exit 18 now means the secure connection failed (handshake, certificate, host name). 17 stays the network and 19 a refused login.
A per-wardrobe CA file is kept in cafiles in suit's home folder, one NAME PATH line each. suit-run is built without the network code and is unchanged at 35496 bytes; the speed budget passes. Release suit went from 2291288 to 2490024 bytes.
An independent security review found no critical or high issues. Its one medium finding, a CA file trusted without checking its owner, mode or type, is fixed here, as are its low ones: separate storage for each set of anchors, core files off, and the release check now covering every test-only switch.
Checked against a real server: an anonymous clone of this repository over https from its forge, 8.4 s, verified with stock fossil.
A new suite, test/https.sh, with 170 cases, runs local servers with test certificates made at test time, including servers that misbehave on purpose. It prints skip lines where openssl or nc is missing. A new fuzz target covers the bundle reader.
3788 unit checks (was 3669), 625 shell cases, 1153 store cases, 985 placement cases, 378 project cases, 236 usage cases, 288 curate cases, 376 curate-apply cases and 170 https cases, under clang, under gcc 16, with no user name set, and under a strict umask, in one run of make gate (76 minutes, on a loaded machine). 100% coverage in all 34 source files. Three store cases that expected "cannot use https yet" now expect the refusal for a missing certificate bundle. (user: dmestas, size: 22086) [annotate] [blame] [check-ins using]
#include "remote.h" #include "anchors.h" #include "conf.h" #include "platform.h" #include "stamp.h" #include <errno.h> #include <fcntl.h> #include <stdarg.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> #include <unistd.h> static char work[REMOTE_FILE_MAX + 1]; static char out_buf[REMOTE_FILE_MAX + 1]; static Remotes remotes; static Creds creds; static CaFiles cafiles; static Anchors check_anchors; __attribute__((format(printf, 3, 4))) static bool fail(RemoteError *e, int line, const char *fmt, ...) { va_list ap; e->line = line; va_start(ap, fmt); vsnprintf(e->msg, sizeof e->msg, fmt, ap); va_end(ap); return false; } bool no_core_dumps(void) { if (sys_no_core_dumps() == 0) return true; say(STDERR_FILENO, "suit: cannot turn off core files, which could hold a secret: %s\n", strerror(errno)); return false; } void secret_wipe(void *p, size_t n) { volatile unsigned char *v = p; for (size_t i = 0; i < n; i++) v[i] = 0; } static bool name_ok(const char *s, size_t max) { size_t len = strlen(s); return len <= max && conf_name_ok(s, len, false); } // ---- the address ----------------------------------------------------------- static bool port_ok(const char *s, size_t len) { if (len == 0 || len > 5) return false; unsigned v = 0; for (size_t i = 0; i < len; i++) { if (s[i] < '0' || s[i] > '9') return false; v = v * 10 + (unsigned)(s[i] - '0'); } return v >= 1 && v <= 65535; } const char *remote_url_check(const char *url, size_t len) { if (len == 0 || len > REMOTE_URL_MAX) return "an address is 1 to 1024 characters"; for (size_t i = 0; i < len; i++) if ((unsigned char)url[i] < 0x21 || (unsigned char)url[i] > 0x7e) return "an address has printable characters only, and no space"; bool file = strncmp(url, "file://", 7) == 0; bool https = strncmp(url, "https://", 8) == 0; if (!file && !https && strncmp(url, "http://", 7) != 0) return "an address starts with file://, http:// or https://"; const char *rest = url + (https ? 8 : 7); if (file) { if (rest[0] != '/') return "a file:// address needs an absolute path after file://"; if (strpbrk(rest, "?#%") != nullptr) return "an address has no query, fragment or % escape"; return nullptr; } size_t auth = strcspn(rest, "/"); if (memchr(rest, '@', auth) != nullptr) return "credentials do not go in the address; use suit login"; if (rest[auth] != '/') return "an http address is http://HOST[:PORT]/PATH"; if (strpbrk(rest, "?#%") != nullptr) return "an address has no query, fragment or % escape"; const char *colon = memchr(rest, ':', auth); size_t host = colon != nullptr ? (size_t)(colon - rest) : auth; if (host == 0 || strspn(rest, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-") != host) return "the host is letters, digits, . and - only"; if (colon != nullptr && !port_ok(colon + 1, auth - host - 1)) return "the port is 1 to 65535"; return nullptr; } void remote_host(const char *url, char *out, size_t cap) { const char *rest = strstr(url, "://") + 3; size_t n = strncmp(url, "file", 4) == 0 ? strlen(rest) : strcspn(rest, "/"); snprintf(out, cap, "%.*s", (int)n, rest); } // ---- parsing --------------------------------------------------------------- typedef bool (*AddFn)(void *state, char *const *f, int line, RemoteError *e); // Split each line of DATA on single spaces into exactly NF fields and give // them to ADD. WORK holds secrets for a while: it is wiped before returning. static bool parse_lines(const char *data, size_t len, size_t nf, const char *shape, AddFn add, void *state, RemoteError *e) { if (len > REMOTE_FILE_MAX) return fail(e, 0, "file is %zu bytes, limit is %zu", len, REMOTE_FILE_MAX); memcpy(work, data, len); work[len] = '\0'; char *p = work; char *end = work + len; int line = 0; bool ok = true; while (ok && p < end) { char *nl = memchr(p, '\n', (size_t)(end - p)); line++; if (nl == nullptr) { ok = fail(e, line, "line has no newline"); } else if ((size_t)(nl - p) > REMOTE_LINE_MAX) { ok = fail(e, line, "line is over the limit of %zu bytes", REMOTE_LINE_MAX); } else if (memchr(p, '\0', (size_t)(nl - p)) != nullptr) { ok = fail(e, line, "NUL byte in line"); } else { *nl = '\0'; char *f[4]; size_t k = 0; char *s = p; p = nl + 1; while (k < 4) { f[k++] = s; char *sp = strchr(s, ' '); if (sp == nullptr) break; *sp = '\0'; s = sp + 1; } if (k != nf) ok = fail(e, line, "expected %s", shape); else ok = add(state, f, line, e); } } secret_wipe(work, len); return ok; } static bool add_remote(void *state, char *const *f, int line, RemoteError *e) { Remotes *r = state; if (!name_ok(f[0], CONF_NAME_MAX)) return fail(e, line, "not a wardrobe name (letters, digits, . _ -)"); const char *why = remote_url_check(f[1], strlen(f[1])); if (why != nullptr) return fail(e, line, "%s", why); if (remotes_find(r, f[0]) != nullptr) return fail(e, line, "duplicate remote for %.80s", f[0]); if (r->n == REMOTE_ENTRY_MAX) return fail(e, line, "too many remotes (limit %u)", REMOTE_ENTRY_MAX); snprintf(r->e[r->n].name, sizeof r->e[0].name, "%s", f[0]); snprintf(r->e[r->n].url, sizeof r->e[0].url, "%s", f[1]); r->n++; return true; } static bool secret_ok(const char *s, size_t len) { if (len == 0 || len > CRED_SECRET_MAX) return false; for (size_t i = 0; i < len; i++) if ((unsigned char)s[i] < 0x21 || (unsigned char)s[i] > 0x7e) return false; return true; } // Nothing here repeats a field: one of them is a secret. static bool add_cred(void *state, char *const *f, int line, RemoteError *e) { Creds *c = state; if (!name_ok(f[0], CONF_NAME_MAX)) return fail(e, line, "not a wardrobe name (letters, digits, . _ -)"); if (!name_ok(f[1], CRED_USER_MAX)) return fail(e, line, "not a user name (1 to %zu letters, digits, . _ -)", CRED_USER_MAX); if (!secret_ok(f[2], strlen(f[2]))) return fail(e, line, "the secret is 1 to %zu printable characters, no space", CRED_SECRET_MAX); if (creds_find(c, f[0]) != nullptr) return fail(e, line, "duplicate login for %.80s", f[0]); if (c->n == REMOTE_ENTRY_MAX) return fail(e, line, "too many logins (limit %u)", REMOTE_ENTRY_MAX); snprintf(c->e[c->n].name, sizeof c->e[0].name, "%s", f[0]); snprintf(c->e[c->n].user, sizeof c->e[0].user, "%s", f[1]); snprintf(c->e[c->n].secret, sizeof c->e[0].secret, "%s", f[2]); c->n++; return true; } static bool add_cafile(void *state, char *const *f, int line, RemoteError *e) { CaFiles *c = state; if (!name_ok(f[0], CONF_NAME_MAX)) return fail(e, line, "not a wardrobe name (letters, digits, . _ -)"); const char *why = anchors_path_check(f[1]); if (why != nullptr) return fail(e, line, "%s", why); if (cafiles_find(c, f[0]) != nullptr) return fail(e, line, "duplicate CA file for %.80s", f[0]); if (c->n == REMOTE_ENTRY_MAX) return fail(e, line, "too many CA files (limit %u)", REMOTE_ENTRY_MAX); snprintf(c->e[c->n].name, sizeof c->e[0].name, "%s", f[0]); snprintf(c->e[c->n].path, sizeof c->e[0].path, "%s", f[1]); c->n++; return true; } bool cafiles_parse(CaFiles *c, const char *data, size_t len, RemoteError *e) { c->n = 0; bool ok = parse_lines(data, len, 2, "NAME PATH", add_cafile, c, e); c->n = ok ? c->n : 0; return ok; } bool remotes_parse(Remotes *r, const char *data, size_t len, RemoteError *e) { r->n = 0; bool ok = parse_lines(data, len, 2, "NAME URL", add_remote, r, e); r->n = ok ? r->n : 0; return ok; } bool creds_parse(Creds *c, const char *data, size_t len, RemoteError *e) { c->n = 0; bool ok = parse_lines(data, len, 3, "NAME USER SECRET", add_cred, c, e); if (!ok) secret_wipe(c, sizeof *c); c->n = ok ? c->n : 0; return ok; } RemoteEntry *remotes_find(Remotes *r, const char *name) { for (uint32_t i = 0; i < r->n; i++) if (strcmp(r->e[i].name, name) == 0) return &r->e[i]; return nullptr; } CaEntry *cafiles_find(CaFiles *c, const char *name) { for (uint32_t i = 0; i < c->n; i++) if (strcmp(c->e[i].name, name) == 0) return &c->e[i]; return nullptr; } CredEntry *creds_find(Creds *c, const char *name) { for (uint32_t i = 0; i < c->n; i++) if (strcmp(c->e[i].name, name) == 0) return &c->e[i]; return nullptr; } // ---- reading and writing --------------------------------------------------- // Read PATH into BUF. 0, or an errno value; ENOENT is for the caller. static int read_file(const char *path, char *buf, size_t *len) { int fd = sys_open(path, O_RDONLY, 0); if (fd < 0) return errno; *len = 0; ssize_t n = 1; while (*len <= REMOTE_FILE_MAX && n > 0) { n = sys_read(fd, buf + *len, REMOTE_FILE_MAX + 1 - *len); if (n < 0 && errno == EINTR) { n = 1; continue; } *len += n > 0 ? (size_t)n : 0; } int err = n < 0 ? errno : 0; (void)sys_close(fd); return err; } static char read_buf[REMOTE_FILE_MAX + 1]; bool remotes_load(Remotes *r, const char *path, RemoteError *e) { r->n = 0; size_t len = 0; int err = read_file(path, read_buf, &len); if (err == ENOENT) return true; if (err != 0) return fail(e, 0, "cannot read: %s", strerror(err)); return remotes_parse(r, read_buf, len, e); } bool cafiles_load(CaFiles *c, const char *path, RemoteError *e) { c->n = 0; size_t len = 0; int err = read_file(path, read_buf, &len); if (err == ENOENT) return true; if (err != 0) return fail(e, 0, "cannot read: %s", strerror(err)); return cafiles_parse(c, read_buf, len, e); } bool creds_load(Creds *c, const char *path, RemoteError *e) { c->n = 0; struct stat st; int found = sys_stat(path, &st); if (found != 0 && errno == ENOENT) return true; if (found != 0) return fail(e, 0, "cannot look at it: %s", strerror(errno)); if (!S_ISREG(st.st_mode)) return fail(e, 0, "not a regular file"); if ((st.st_mode & 077) != 0) return fail(e, 0, "group or other can use it (mode %04o); run chmod 600 on it", (unsigned)(st.st_mode & 07777)); size_t len = 0; int err = read_file(path, read_buf, &len); bool ok = err == 0 ? creds_parse(c, read_buf, len, e) : fail(e, 0, "cannot read: %s", strerror(err)); secret_wipe(read_buf, sizeof read_buf); return ok; } bool syncfail_parse(const char *data, size_t len, unsigned *n) { bool ok = len >= 2 && len <= 7 && data[len - 1] == '\n' && data[0] >= '1'; unsigned v = 0; for (size_t i = 0; ok && i + 1 < len; i++) { ok = data[i] >= '0' && data[i] <= '9'; v = v * 10 + (ok ? (unsigned)(data[i] - '0') : 0); } *n = ok ? v : 0; return ok; } bool syncfail_load(const char *path, unsigned *n, RemoteError *e) { *n = 0; size_t len = 0; int err = read_file(path, read_buf, &len); if (err == ENOENT) return true; if (err != 0) return fail(e, 0, "cannot read: %s", strerror(err)); if (!syncfail_parse(read_buf, len, n)) return fail(e, 0, "not a count of failed syncs (1 to %u and a newline)", SYNC_FAILS_MAX); return true; } int syncfail_write(const char *path, unsigned n) { char text[16]; int len = snprintf(text, sizeof text, "%u\n", n); ensure(n >= 1 && n <= SYNC_FAILS_MAX && len > 0); return sys_write_atomic(path, text, (size_t)len); } static int cmp_name(const void *a, const void *b) { return strcmp(a, b); } // Entries sorted by name: indexes into the array of STRIDE-byte records. static void sort_entries(void *base, size_t n, size_t stride) { qsort(base, n, stride, cmp_name); } int remotes_write(const Remotes *r, const char *path) { static Remotes sorted; sorted = *r; sort_entries(sorted.e, sorted.n, sizeof sorted.e[0]); size_t len = 0; for (uint32_t i = 0; i < sorted.n; i++) { int n = snprintf(out_buf + len, sizeof out_buf - len, "%s %s\n", sorted.e[i].name, sorted.e[i].url); if ((size_t)n >= sizeof out_buf - len) return EFBIG; len += (size_t)n; } return sys_write_atomic(path, out_buf, len); } int cafiles_write(const CaFiles *c, const char *path) { static CaFiles sorted; sorted = *c; sort_entries(sorted.e, sorted.n, sizeof sorted.e[0]); size_t len = 0; for (uint32_t i = 0; i < sorted.n; i++) { int n = snprintf(out_buf + len, sizeof out_buf - len, "%s %s\n", sorted.e[i].name, sorted.e[i].path); if ((size_t)n >= sizeof out_buf - len) return EFBIG; len += (size_t)n; } return sys_write_atomic(path, out_buf, len); } int creds_write(const Creds *c, const char *path) { static Creds sorted; sorted = *c; sort_entries(sorted.e, sorted.n, sizeof sorted.e[0]); size_t len = 0; int err = 0; for (uint32_t i = 0; i < sorted.n && err == 0; i++) { int n = snprintf(out_buf + len, sizeof out_buf - len, "%s %s %s\n", sorted.e[i].name, sorted.e[i].user, sorted.e[i].secret); err = (size_t)n >= sizeof out_buf - len ? EFBIG : 0; len += err == 0 ? (size_t)n : 0; } err = err != 0 ? err : sys_write_atomic_mode(path, out_buf, len, 0600); secret_wipe(out_buf, sizeof out_buf); secret_wipe(&sorted, sizeof sorted); return err; } // ---- the commands ---------------------------------------------------------- int remote_paths(char *remotes_path, char *creds_path, size_t cap) { if (suit_home_path(remotes_path, cap, "remotes") == 0) return 1; // The same folder, so a longer name fits if that did. [[maybe_unused]] size_t home = suit_home_path(creds_path, cap, "credentials"); ensure(home != 0); return 0; } static bool check_name(const char *name) { if (name_ok(name, CONF_NAME_MAX)) return true; say(STDERR_FILENO, "suit: '%.40s' is not a wardrobe name (letters, digits, . _ -)\n", name); return false; } static void remove_remote(Remotes *r, const char *name) { for (uint32_t i = 0; i < r->n; i++) { if (strcmp(r->e[i].name, name) == 0) { r->e[i] = r->e[--r->n]; return; } } } // Nullptr if CA may be the CA file of the remote URL, else why not. static const char *ca_check(const char *url, const char *ca, char *why, size_t cap) { char path[STAMP_PATH_MAX]; if (strncmp(url, "https://", 8) != 0) return "--ca is for an https:// address"; const char *bad = anchors_path_check(ca); if (bad != nullptr) return bad; if (!anchors_load(&check_anchors, ca, path, sizeof path, why, cap)) return why; return nullptr; } // Record CA as NAME's CA file, or forget NAME's when CA is null. static int apply_ca(const char *name, const char *ca) { char path[STAMP_PATH_MAX]; // The same folder as the remotes file, which was found. [[maybe_unused]] size_t home = suit_home_path(path, sizeof path, "cafiles"); ensure(home != 0); RemoteError e; if (!cafiles_load(&cafiles, path, &e)) { say(STDERR_FILENO, "suit: %s:%d: %s\n", path, e.line, e.msg); return 1; } CaEntry *old = cafiles_find(&cafiles, name); if (old == nullptr && ca == nullptr) return 0; if (old != nullptr) *old = cafiles.e[--cafiles.n]; if (ca != nullptr && cafiles.n == REMOTE_ENTRY_MAX) { say(STDERR_FILENO, "suit: cafiles: too many CA files (limit %u)\n", REMOTE_ENTRY_MAX); return 1; } if (ca != nullptr) { snprintf(cafiles.e[cafiles.n].name, sizeof cafiles.e[0].name, "%s", name); snprintf(cafiles.e[cafiles.n].path, sizeof cafiles.e[0].path, "%s", ca); cafiles.n++; } int err = cafiles_write(&cafiles, path); if (err != 0) say(STDERR_FILENO, "suit: cannot write %s: %s\n", path, strerror(err)); return err == 0 ? 0 : 1; } int remote_ca(const char *name, char *out, size_t cap) { char path[STAMP_PATH_MAX]; out[0] = '\0'; // The same folder as the remotes file, which was found. [[maybe_unused]] size_t home = suit_home_path(path, sizeof path, "cafiles"); ensure(home != 0); RemoteError e; if (!cafiles_load(&cafiles, path, &e)) { say(STDERR_FILENO, "suit: %s:%d: %s\n", path, e.line, e.msg); return 1; } const CaEntry *c = cafiles_find(&cafiles, name); if (c != nullptr) snprintf(out, cap, "%s", c->path); return 0; } static int apply_remote(Remotes *r, const char *name, const char *url, const char *ca, const char *path) { char detail[300]; bool none = strcmp(url, "--none") == 0; const char *why = none ? nullptr : remote_url_check(url, strlen(url)); why = why != nullptr || ca == nullptr ? why : ca_check(url, ca, detail, sizeof detail); if (why != nullptr) { say(STDERR_FILENO, "suit: remote %s: %s\n", name, why); return 2; } remove_remote(r, name); if (!none && r->n == REMOTE_ENTRY_MAX) { say(STDERR_FILENO, "suit: remotes: too many remotes (limit %u)\n", REMOTE_ENTRY_MAX); return 1; } if (!none) { snprintf(r->e[r->n].name, sizeof r->e[0].name, "%s", name); snprintf(r->e[r->n].url, sizeof r->e[0].url, "%s", url); r->n++; } if (apply_ca(name, ca) != 0) return 1; int err = remotes_write(r, path); if (err != 0) say(STDERR_FILENO, "suit: cannot write %s: %s\n", path, strerror(err)); return err == 0 ? 0 : 1; } int cmd_remote(const char *name, const char *url, const char *ca) { char path[STAMP_PATH_MAX]; char cpath[STAMP_PATH_MAX]; if (!check_name(name)) return 2; if (remote_paths(path, cpath, sizeof path) != 0) return 1; RemoteError e; if (!remotes_load(&remotes, path, &e)) { say(STDERR_FILENO, "suit: %s:%d: %s\n", path, e.line, e.msg); return 1; } if (url != nullptr) return apply_remote(&remotes, name, url, ca, path); const RemoteEntry *r = remotes_find(&remotes, name); if (r == nullptr) { say(STDERR_FILENO, "suit: no remote is set for %s\n", name); return 1; } char ca_path[REMOTE_URL_MAX + 1]; if (remote_ca(name, ca_path, sizeof ca_path) != 0) return 1; say(STDOUT_FILENO, "%s\n", r->url); if (ca_path[0] != '\0') say(STDOUT_FILENO, "ca %s\n", ca_path); return 0; } // One line from standard input into BUF (CAP bytes): its length without the // newline, or SIZE_MAX if it did not fit or could not be read. static size_t read_secret_line(char *buf, size_t cap) { size_t len = 0; ssize_t n = 1; while (len < cap && n > 0 && memchr(buf, '\n', len) == nullptr) { n = sys_read(STDIN_FILENO, buf + len, cap - len); if (n < 0 && errno == EINTR) { n = 1; continue; } len += n > 0 ? (size_t)n : 0; } const char *nl = memchr(buf, '\n', len); size_t line = nl != nullptr ? (size_t)(nl - buf) : len; return n < 0 || (nl == nullptr && len == cap) ? SIZE_MAX : line; } static int login_with(const char *name, const char *user, const char *path, RemoteError *e) { static char line[CRED_SECRET_MAX + 3]; if (!no_core_dumps()) return 1; size_t len = read_secret_line(line, sizeof line); int result = 1; if (len == SIZE_MAX) { say(STDERR_FILENO, "suit: login %s: cannot read the secret: one line of 1 to %zu characters on standard input\n", name, CRED_SECRET_MAX); } else if (!secret_ok(line, len)) { say(STDERR_FILENO, "suit: login %s: the secret is 1 to %zu printable characters, no space\n", name, CRED_SECRET_MAX); result = 2; } else if (!creds_load(&creds, path, e)) { say(STDERR_FILENO, "suit: %s:%d: %s\n", path, e->line, e->msg); } else if (creds_find(&creds, name) == nullptr && creds.n == REMOTE_ENTRY_MAX) { say(STDERR_FILENO, "suit: credentials: too many logins (limit %u)\n", REMOTE_ENTRY_MAX); } else { CredEntry *c = creds_find(&creds, name); c = c != nullptr ? c : &creds.e[creds.n++]; snprintf(c->name, sizeof c->name, "%s", name); snprintf(c->user, sizeof c->user, "%s", user); memcpy(c->secret, line, len); c->secret[len] = '\0'; int err = creds_write(&creds, path); if (err != 0) say(STDERR_FILENO, "suit: cannot write %s: %s\n", path, strerror(err)); result = err == 0 ? 0 : 1; } secret_wipe(line, sizeof line); secret_wipe(&creds, sizeof creds); return result; } int cmd_login(const char *name, const char *user) { char path[STAMP_PATH_MAX]; char rpath[STAMP_PATH_MAX]; if (!check_name(name)) return 2; if (!name_ok(user, CRED_USER_MAX)) { say(STDERR_FILENO, "suit: login %s: not a user name (1 to %zu letters, digits, . _ -)\n", name, CRED_USER_MAX); return 2; } if (remote_paths(rpath, path, sizeof path) != 0) return 1; RemoteError e; return login_with(name, user, path, &e); } int cmd_logout(const char *name) { char path[STAMP_PATH_MAX]; char rpath[STAMP_PATH_MAX]; if (!check_name(name)) return 2; if (remote_paths(rpath, path, sizeof path) != 0) return 1; RemoteError e; int result = 0; if (!no_core_dumps()) return 1; if (!creds_load(&creds, path, &e)) { say(STDERR_FILENO, "suit: %s:%d: %s\n", path, e.line, e.msg); result = 1; } else if (creds_find(&creds, name) != nullptr) { CredEntry *gone = creds_find(&creds, name); *gone = creds.e[--creds.n]; int err = creds_write(&creds, path); if (err != 0) say(STDERR_FILENO, "suit: cannot write %s: %s\n", path, strerror(err)); result = err == 0 ? 0 : 1; } secret_wipe(&creds, sizeof creds); return result; }