/* * grepo: write and sign a repository index (REPO_DESIGN.txt section 4). * A maintainer tool, like gsign. * * grepo -s KEY.sec -r REPO [-n SERIAL] [-e DAYS] DIR * * Reads every NAME-VER.gpkg in DIR (its manifest, sha256 and size) and * writes DIR/repo.idx plus DIR/repo.idx.sig, signed with KEY.sec. Each * package's manifest must say REPO, and its file name must be * NAME-VER.gpkg as the manifest names it. The server is then any static * file host serving DIR. * * A REPO named X-src is a recipe repository (BUILD_DESIGN.txt section * 3): DIR holds recipe bundles, NAME-VER.grcp, each read with * gl_recipe_read; the recipe's REPO must be X. * * SERIAL (decided 2026-10-06) is the date form YYYYMMDDNN: the larger * of the previous DIR/repo.idx's SERIAL + 1 and today (UTC) * 100 + 1. * -n sets it explicitly; it must still be above the previous one, or * clients would refuse the index. VALID_UNTIL is now + DAYS days * (default 30; decision 10c). * * Exit 0 on success, 1 otherwise. */ #define _POSIX_C_SOURCE 200809L #define _XOPEN_SOURCE 700 #include #include #include #include #include #include #include #include #include #include #include #include #include "istoreutils.h" #include "log.h" #include "pkg.h" #include "recipe.h" #include "sha256.h" #include "sign.h" #define DEFAULT_DAYS 30 #define MAX_DAYS 3650 #define USAGE "usage: grepo [-hv] -s KEY.sec -r REPO [-n SERIAL] [-e DAYS] DIR\n" static const struct option longopts[] = { { "help", no_argument, NULL, 'h' }, { "version", no_argument, NULL, 'v' }, { "seckey", required_argument, NULL, 's' }, { "repo", required_argument, NULL, 'r' }, { "serial", required_argument, NULL, 'n' }, { "expire", required_argument, NULL, 'e' }, { NULL, 0, NULL, 0 } }; static void usage(void) { fprintf(stderr, USAGE); } static void help(void) { printf("grepo: Write and sign a repository index\n"); printf(USAGE); printf("\n"); printf("%-25s %s\n", "{-h --help}", "Show this message"); printf("%-25s %s\n", "{-v --version}", "Show the current version"); printf("%-25s %s\n", "{-s --seckey} KEY.sec", "Sign the index with this secret key"); printf("%-25s %s\n", "{-r --repo} REPO", "Repository the packages in DIR belong to"); printf("%-25s %s\n", "{-n --serial} SERIAL", "Index serial (default: YYYYMMDDNN)"); printf("%-25s Days until the index expires (default: %d)\n", "{-e --expire} DAYS", DEFAULT_DAYS); printf("\n"); printf("This program is free software.\n"); printf("See the GNU GPL version 3 for details.\n"); } static int by_name(const void *a, const void *b) { return strcmp(*(char *const *)a, *(char *const *)b); } /* sha256 ("sha256:") and size of a regular file, read once. */ static int hash_file(const char *path, char sha[72], int64_t *size) { int fd = open(path, O_RDONLY | O_NOFOLLOW); if (fd < 0) return -1; gl_sha256_ctx_t h; gl_sha256_init(&h); uint8_t buf[65536]; ssize_t n; int64_t total = 0; while ((n = read(fd, buf, sizeof buf)) > 0) { gl_sha256_update(&h, buf, (size_t)n); total += n; } close(fd); if (n < 0) return -1; uint8_t digest[32]; char hex[65]; gl_sha256_final(&h, digest); gl_sha256_to_hex(digest, hex); snprintf(sha, 72, "sha256:%s", hex); *size = total; return 0; } /* Reads a whole file (an index: at most GL_SIGN_MAX_MSG). NULL if it * doesn't exist (errno ENOENT) or can't be read. */ static char * slurp(const char *path, size_t *len) { FILE *f = fopen(path, "rb"); if (!f) return NULL; char *buf = malloc(GL_SIGN_MAX_MSG + 1); size_t n = buf ? fread(buf, 1, GL_SIGN_MAX_MSG + 1, f) : 0; int bad = !buf || ferror(f) || n > GL_SIGN_MAX_MSG; fclose(f); if (bad) { free(buf); errno = EIO; return NULL; } *len = n; return buf; } /* One DEPS/CONFS element. 0 having said why if the name is unsafe. */ static int relation(FILE *f, const char *path, const char *what, const char *name, const semver_t *v, int first) { if (!gl_valid_pkg_ident(name)) { lg_printf(LG_ERR, "%s has a %s that is not a safe name.", path, what); return 0; } fprintf(f, "%s { NAME = \"%s\"; VER = \"%d.%d.%d\"; }", first ? "" : ",", name, v->maj, v->min, v->pat); return 1; } /* Whether REPO is a recipe repository, "X-src" (X not empty). */ static int is_src(const char *repo) { size_t l = strlen(repo); return l > 4 && strcmp(repo + l - 4, "-src") == 0; } /* gl_recipe_t relation lines ("NAME VER[ IF]\n") as an index list. */ static void relation_lines(FILE *f, const char *key, const char *lines) { fprintf(f, "\n\t %s = (", key); const char *sep = ""; for (const char *p = lines; *p; p = strchr(p, '\n') + 1) { /* One line: "NAME VER" or "NAME VER IF", never more. */ char line[512]; snprintf(line, sizeof line, "%.*s", (int)(strchr(p, '\n') - p), p); char *ver = strchr(line, ' '); *ver++ = '\0'; char *cond = strchr(ver, ' '); if (cond) *cond++ = '\0'; fprintf(f, "%s { NAME = \"%s\"; VER = \"%s\";", sep, line, ver); if (cond) fprintf(f, " IF = \"%s\";", cond); fprintf(f, " }"); sep = ","; } fprintf(f, "%s);", *sep ? " " : ""); } /* One recipe bundle's PKGS entry (REPO is X-src). */ static int write_src_entry(FILE *f, const char *path, const char *file, const char *repo) { gl_recipe_t r; gl_recipe_status_t st = gl_recipe_read(path, &r); if (st != GL_RECIPE_OK) { lg_printf(LG_ERR, "%s is %s: %s.", path, st == GL_RECIPE_ERR_READ ? "not a readable recipe bundle" : st == GL_RECIPE_ERR_BUNDLE ? "not a valid recipe bundle" : "a bundle with a malformed recipe", gl_recipe_error()); return -1; } int rc = -1; char want[PATH_MAX]; snprintf(want, sizeof want, "%s-%s.grcp", r.name, r.ver); if (strlen(r.repo) != strlen(repo) - 4 || strncmp(r.repo, repo, strlen(r.repo)) != 0) { lg_printf(LG_ERR, "%s builds %s packages, not %.*s.", path, r.repo, (int)(strlen(repo) - 4), repo); goto out; } if (strcmp(file, want) != 0) { lg_printf(LG_ERR, "%s must be named %s, as its recipe says.", path, want); goto out; } char sha[72]; int64_t size; if (hash_file(path, sha, &size) != 0) { lg_printf(LG_ERR, "Cannot read %s.", path); goto out; } fprintf(f, "\t{ NAME = \"%s\"; VER = \"%s\"; FILE = \"%s\";\n" "\t SHA256 = \"%s\"; SIZE = %" PRId64 "L;", r.name, r.ver, file, sha, size); relation_lines(f, "DEPS", r.deps); relation_lines(f, "CONFS", r.confs); relation_lines(f, "BUILD_DEPS", r.build_deps); fprintf(f, "\n\t TRAITS = ("); const char *sep = ""; for (const char *p = r.traits; *p; p = strchr(p, '\n') + 1) { size_t l = (size_t)(strchr(p, ' ') - p); fprintf(f, "%s { NAME = \"%.*s\"; DEFAULT = %s; }", sep, (int)l, p, p[l + 1] == '1' ? "true" : "false"); sep = ","; } fprintf(f, "%s); }", *sep ? " " : ""); rc = 0; out: gl_recipe_free(&r); return rc; } /* One package's PKGS entry, appended to f. 0, or -1 having said why. */ static int write_entry(FILE *f, const char *dir, const char *file, const char *repo) { char path[PATH_MAX]; snprintf(path, sizeof path, "%s/%s", dir, file); struct stat st; if (lstat(path, &st) != 0 || !S_ISREG(st.st_mode)) { lg_printf(LG_ERR, "Refusing %s: not a regular file.", path); return -1; } if (is_src(repo)) return write_src_entry(f, path, file, repo); gl_pkg_t pkg = {0}; if (gl_read_pkg_manifest(path, &pkg) != 0) { lg_printf(LG_ERR, "Cannot read the manifest of %s.", path); return -1; } int rc = -1; char ver[64], want[PATH_MAX]; snprintf(ver, sizeof ver, "%d.%d.%d", pkg.pkg_ver.maj, pkg.pkg_ver.min, pkg.pkg_ver.pat); snprintf(want, sizeof want, "%s-%s.gpkg", pkg.pkg_name, ver); if (strcmp(pkg.pkg_repo, repo) != 0) { lg_printf(LG_ERR, "%s is a %s package, not %s.", path, pkg.pkg_repo, repo); goto out; } if (strcmp(file, want) != 0) { lg_printf(LG_ERR, "%s must be named %s, as its manifest says.", path, want); goto out; } char sha[72]; int64_t size; if (hash_file(path, sha, &size) != 0) { lg_printf(LG_ERR, "Cannot read %s.", path); goto out; } fprintf(f, "\t{ NAME = \"%s\"; VER = \"%s\"; FILE = \"%s\";\n" "\t SHA256 = \"%s\"; SIZE = %" PRId64 "L;\n\t DEPS = (", pkg.pkg_name, ver, file, sha, size); int first = 1; for (dep_t *d = pkg.pkg_dependencies; d && d->dep_name; d++) { if (!relation(f, path, "dependency", d->dep_name, &d->dep_ver, first)) goto out; first = 0; } fprintf(f, "%s); CONFS = (", first ? "" : " "); first = 1; for (conf_t *k = pkg.pkg_conflicts; k && k->conf_name; k++) { if (!relation(f, path, "conflict", k->conf_name, &k->conf_ver, first)) goto out; first = 0; } fprintf(f, "%s); }", first ? "" : " "); rc = 0; out: gl_free_pkg(&pkg); return rc; } int main(int argc, char *argv[]) { const char *sec = NULL, *repo = NULL; int64_t serial = 0; long days = DEFAULT_DAYS; int c; while ((c = getopt_long(argc, argv, "hvs:r:n:e:", longopts, NULL)) != -1) { char *end; switch (c) { case 'h': help(); return 0; case 'v': printf("v4.0.0\n"); return 0; case 's': sec = optarg; break; case 'r': repo = optarg; break; case 'n': errno = 0; serial = strtoll(optarg, &end, 10); if (errno || *end || end == optarg || serial <= 0) { lg_printf(LG_ERR, "SERIAL must be a whole number above 0."); return 1; } break; case 'e': errno = 0; days = strtol(optarg, &end, 10); if (errno || *end || end == optarg || days < 1 || days > MAX_DAYS) { lg_printf(LG_ERR, "DAYS must be a whole number from 1 to %d.", MAX_DAYS); return 1; } break; default: usage(); return 1; } } if (!sec || !repo || optind != argc - 1) { usage(); return 1; } const char *dir = argv[optind]; if (!gl_valid_pkg_ident(repo)) { lg_printf(LG_ERR, "Repository name %s is not a safe name ([A-Za-z0-9._-]).", repo); return 1; } int st = gl_sign_check_seckey(sec); if (st != GL_SIGN_OK) { lg_printf(LG_ERR, "Secret key %s %s.", sec, st == GL_SIGN_ERR_ENCRYPTED ? "is encrypted: sign with signify" : st == GL_SIGN_ERR_FORMAT ? "is malformed" : "cannot be read"); return 1; } char idx_path[PATH_MAX], sig_path[PATH_MAX]; if ((size_t)snprintf(idx_path, sizeof idx_path, "%s/repo.idx", dir) >= sizeof idx_path || (size_t)snprintf(sig_path, sizeof sig_path, "%s/repo.idx.sig", dir) >= sizeof sig_path) { lg_printf(LG_ERR, "Path too long: %s", dir); return 1; } /* The previous index, for its SERIAL. The maintainer's own file: * parsed, not verified, but it must be a well-formed index for * this repo (a mixed-up DIR is better caught here). */ int64_t prev = 0; size_t plen; char *ptext = slurp(idx_path, &plen); if (!ptext && errno != ENOENT) { lg_printf(LG_ERR, "Cannot read %s.", idx_path); return 1; } if (ptext) { gl_index_t old; gl_index_status_t ist = gl_index_parse(ptext, plen, repo, &old); free(ptext); if (ist == GL_INDEX_ERR_REPO) { lg_printf(LG_ERR, "%s is the index of another repository.", idx_path); return 1; } if (ist != GL_INDEX_OK) { lg_printf(LG_ERR, "Malformed %s.", idx_path); return 1; } prev = old.serial; gl_index_free(&old); } time_t now = time(NULL); if (serial == 0) { struct tm tm; gmtime_r(&now, &tm); int64_t today = ((int64_t)(tm.tm_year + 1900) * 10000 + (tm.tm_mon + 1) * 100 + tm.tm_mday) * 100 + 1; serial = prev + 1 > today ? prev + 1 : today; } else if (serial <= prev) { lg_printf(LG_ERR, "SERIAL must be above %" PRId64 ", the previous index's.", prev); return 1; } DIR *d = opendir(dir); if (!d) { lg_printf(LG_ERR, "Cannot open %s: %s", dir, strerror(errno)); return 1; } char **names = NULL; size_t n = 0; struct dirent *de; while ((de = readdir(d))) { size_t l = strlen(de->d_name); if (de->d_name[0] == '.' || l < 6 || strcmp(de->d_name + l - 5, is_src(repo) ? ".grcp" : ".gpkg") != 0) continue; char **grown = realloc(names, (n + 1) * sizeof *names); if (!grown || !(grown[n] = strdup(de->d_name))) { lg_printf(LG_ERR, "Out of memory."); return 1; } names = grown; n++; } closedir(d); qsort(names, n, sizeof *names, by_name); /* Written beside the result, then renamed over it: never in place. */ char tmp_idx[PATH_MAX], tmp_sig[PATH_MAX]; snprintf(tmp_idx, sizeof tmp_idx, "%s/.repo.idx.XXXXXX", dir); int fd = mkstemp(tmp_idx); FILE *f = fd < 0 ? NULL : fdopen(fd, "w"); if (!f) { lg_printf(LG_ERR, "Cannot write in %s: %s", dir, strerror(errno)); return 1; } snprintf(tmp_sig, sizeof tmp_sig, "%s.sig", tmp_idx); fprintf(f, "# Repository index written by grepo. Signed: repo.idx.sig.\n" "REPO = \"%s\";\nSERIAL = %" PRId64 "L;\nVALID_UNTIL = %" PRId64 "L;\n" "PKGS = (\n", repo, serial, (int64_t)now + (int64_t)days * 86400); int rc = 0; for (size_t i = 0; i < n && rc == 0; i++) { if (i) fprintf(f, ",\n"); rc = write_entry(f, dir, names[i], repo); } fprintf(f, "%s);\n", n ? "\n" : ""); if (fchmod(fd, 0644) != 0 || fflush(f) != 0 || fsync(fd) != 0) rc = rc ? rc : (lg_printf(LG_ERR, "Cannot write %s.", tmp_idx), -1); if (fclose(f) != 0 && rc == 0) { lg_printf(LG_ERR, "Cannot write %s.", tmp_idx); rc = -1; } for (size_t i = 0; i < n; i++) free(names[i]); free(names); if (rc == 0 && (st = gl_sign_file(sec, tmp_idx, tmp_sig)) != GL_SIGN_OK) { lg_printf(LG_ERR, "Cannot sign %s.", tmp_idx); rc = -1; } /* The signature first: a client that fetches between the renames * gets a new .sig with the old index, which fails to verify and is * refused, never an index accepted under the wrong signature. */ if (rc == 0 && (rename(tmp_sig, sig_path) != 0 || rename(tmp_idx, idx_path) != 0)) { lg_printf(LG_ERR, "Cannot replace %s: %s", idx_path, strerror(errno)); rc = -1; } if (rc != 0) { unlink(tmp_idx); unlink(tmp_sig); return 1; } lg_printf(LG_SUCCESS, "Wrote %s: %zu %s, serial %" PRId64 ".", idx_path, n, n == 1 ? "package" : "packages", serial); return 0; }