Artifact 0b1c6fb557d430f7ac2d05db44e6469ce4b6cf6109d37af0acd40ed730aab4eb:
- File
src/remote.c
— part of check-in
[fe29cebc6a]
at
2026-11-13 20:38:51
on branch trunk
— Phase 7b-1: a wardrobe syncs with a remote, over file:// and http://
suit remote sets, shows or clears a wardrobe's remote address. An address that carries a user name or password is refused. suit login reads a secret from standard input into a credentials file of mode 0600, and suit refuses to use that file if anyone else can read it. The secret is never printed, never put in an error message or another file, and is wiped from memory after each use.
suit sync pulls, brings the working copy forward with the safe update, and pushes. When both sides have new check-ins it tries a merge as a dry run. A clean merge is checked in and pushed. A conflicting one changes no file, is pushed as a fork so nothing is lost, and is written to the attention file. No conflict marker is ever written to a live file. With local changes in the working copy the pull is kept and nothing else happens.
suit tend now continues into a sync after its check-in.
Three limits of libfossil, each handled here: its merge drops a file the other side deleted, so suit finds those itself and treats a delete against an edit as a conflict; a merge that changes no file cannot be checked in, so the files are put back and the fork stays open; and a file:// remote is reached through the stock fossil program, which must be on the path. http:// uses libfossil's own socket code and starts no other program.
An https:// address is accepted and stored, and syncing it says this build cannot use https yet. There is no clone command yet.
Also: LICENSE (BSD 2-Clause, the same as libfossil), a license section and current status in the README, and a state table in docs/phases.md.
1204 unit checks, 227 shell cases, 555 store cases, 612 placement cases and 378 project cases, under clang, under gcc 16, with no user name set, and under a strict umask. 100% coverage in all 22 source files. Seven fuzz targets. No suite has fewer cases than at the last check-in. (user: dmestas, size: 16301) [annotate] [blame] [check-ins using]
#include "remote.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; __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; } 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; } 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; } 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 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; } 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 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; } } } static int apply_remote(Remotes *r, const char *name, const char *url, const char *path) { bool none = strcmp(url, "--none") == 0; const char *why = none ? nullptr : remote_url_check(url, strlen(url)); 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++; } 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) { 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, 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; } say(STDOUT_FILENO, "%s\n", r->url); 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]; 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 (!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; }