/* hook.c - the mode git calls, and the only one a push depends on. * * This is what `core.hooksPath` points at. The shape is git's: an identity in * the environment, one "old new ref" line per ref on stdin, and a non-zero exit * to stop the whole push. Nothing is asked of the client and nothing is * inferred from the transport, because ref names travel inside a compressed * request body - the hook is the first place they exist as data. * * Two facts are not in the hook's input, and only the object store has them: * whether "old -> new" is a fast-forward, and which branch HEAD points at. * Those are asked of git, and an answer that cannot be had is taken the * expensive way: a rewrite until proven a fast-forward, and any deletion of a * branch treated as the default one while HEAD itself is unknown. Guessing the * cheap way would be the hole. */ #include "authz.h" #include #include #include #include #include #include #define DEV_NULL "/dev/null" /* Run a program, optionally keeping the first line of its stdout. * * stdin is shut off from /dev/null: a child left holding the hook's stdin eats * the ref lines that have not been read yet, and a push that loses refs from * its own list is a push that skips checks. stderr goes to /dev/null too, so * that a failed lookup does not shout "fatal:" at the person pushing about * machinery they cannot touch. */ static int spawn(const char *const argv[], int capture, char **out) { int fds[2] = { -1, -1 }; int status = -1; pid_t pid; if (out != NULL) *out = NULL; if (capture && pipe(fds) != 0) return -1; pid = fork(); if (pid < 0) { if (capture) { close(fds[0]); close(fds[1]); } return -1; } if (pid == 0) { int devnull = open(DEV_NULL, O_RDWR); if (devnull >= 0) { dup2(devnull, STDIN_FILENO); if (!capture) dup2(devnull, STDOUT_FILENO); dup2(devnull, STDERR_FILENO); if (devnull > 2) close(devnull); } if (capture) { dup2(fds[1], STDOUT_FILENO); close(fds[0]); close(fds[1]); } execvp(argv[0], (char *const *)argv); _exit(127); } if (capture) { FILE *f; close(fds[1]); f = fdopen(fds[0], "r"); if (f != NULL) { char *line = NULL; size_t cap = 0; if (getline(&line, &cap, f) != -1) { size_t n = strlen(line); while (n > 0 && (line[n - 1] == '\n' || line[n - 1] == '\r')) line[--n] = '\0'; *out = line; } else { free(line); } fclose(f); } else { close(fds[0]); } } while (waitpid(pid, &status, 0) == -1) ; if (WIFEXITED(status)) return WEXITSTATUS(status); return -1; } static int is_zero(const char *s) { int i; if (s == NULL || s[0] == '\0') return 0; for (i = 0; s[i] != '\0'; i++) { if (s[i] != '0') return 0; } return 1; } /* Object ids are 40 or 64 hex digits, or all zeroes for "does not exist". * Anything else is a protocol oddity, and an oddity gets denied rather than * interpreted. */ static int object_id_ok(const char *s) { size_t i; int hex = 0; for (i = 0; s[i] != '\0'; i++) { char c = s[i]; if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))) return 0; hex++; } return hex == 40 || hex == 64; } static char *head_ref(hook_opts o, int *unknown) { /* Terminated by hand, and by name: execvp walks this array until it finds * the NULL, and an array that runs off the end takes whatever is on the * stack next as an argument - which is not a crash you can read, and made * every deletion cost admin because the answer was never received. */ const char *argv[5] = { "git", "symbolic-ref", "-q", "HEAD", NULL }; char *out = NULL; int rc; *unknown = 0; if (strcmp(o.head, "none") == 0) return NULL; if (strcmp(o.head, "auto") != 0) return xstrdup(o.head); if (!o.use_git) return NULL; /* told not to ask, so do not guess either way */ rc = spawn(argv, 1, &out); if (rc == 0 && out != NULL && out[0] != '\0') return out; free(out); *unknown = 1; /* cannot tell: charge deletions the most */ return NULL; } /* Is old an ancestor of new? */ static int fast_forward(hook_opts o, const char *old, const char *new) { /* Same as head_ref: the NULL is part of the contract, not padding. */ const char *argv[6] = { "git", "merge-base", "--is-ancestor", NULL, NULL, NULL }; int rc; if (strcmp(o.ff, "yes") == 0) return 1; if (strcmp(o.ff, "no") == 0) return 0; if (!o.use_git) return 0; argv[3] = old; argv[4] = new; rc = spawn(argv, 0, NULL); return rc == 0; } static char *derive_repo(hook_opts o) { const char *e; const char *root; size_t rootlen; char *cwd; char *name; if (o.repo != NULL) return authz_norm_repo(o.repo); e = getenv("GIT_HOST_REPO"); if (e != NULL && e[0] != '\0') return authz_norm_repo(e); cwd = getcwd(NULL, 0); if (cwd == NULL) return xstrdup(""); root = getenv("GIT_REPO_ROOT"); if (root == NULL || root[0] == '\0') root = "/var/lib/git"; rootlen = strlen(root); while (rootlen > 1 && root[rootlen - 1] == '/') rootlen--; /* "/var/lib/git/" is "/var/lib/git" */ if (strncmp(cwd, root, rootlen) == 0 && cwd[rootlen] == '/') name = cwd + rootlen + 1; else { char *slash = strrchr(cwd, '/'); name = (slash != NULL && slash[1] != '\0') ? slash + 1 : cwd; } { char *out = authz_norm_repo(name); free(cwd); return out; } } static const char *identity(hook_opts o) { const char *e; if (o.user != NULL) return o.user; e = getenv("GIT_HOST_USER"); if (e != NULL && e[0] != '\0') return e; e = getenv("REMOTE_USER"); if (e != NULL && e[0] != '\0') return e; return NULL; } static int flag(const char *arg, const char *name, const char **slot) { size_t n = strlen(name); if (strncmp(arg, name, n) != 0 || arg[n] != '=') return 0; *slot = arg + n + 1; return 1; } int cmd_authorize(const policy_t *p, hook_opts o, int argc, char **argv) { char *line = NULL; size_t cap = 0; ssize_t len; char *repo; const char *user; const char *head; int head_unknown; int seen = 0; int i; if (o.ff == NULL) o.ff = "auto"; if (o.head == NULL) o.head = "auto"; for (i = 0; i < argc; i++) { if (flag(argv[i], "--user", &o.user)) continue; if (flag(argv[i], "--repo", &o.repo)) continue; if (flag(argv[i], "--ff", &o.ff)) continue; if (flag(argv[i], "--head", &o.head)) continue; if (strcmp(argv[i], "--no-git") == 0) { o.use_git = 0; continue; } if (strcmp(argv[i], "--explain") == 0) { o.explain = 1; continue; } fprintf(stderr, "%s: authorize: unknown argument %s\n", progname, argv[i]); return 64; } if (strcmp(o.ff, "auto") != 0 && strcmp(o.ff, "yes") != 0 && strcmp(o.ff, "no") != 0) { fprintf(stderr, "%s: --ff wants auto, yes or no\n", progname); return 64; } user = identity(o); repo = derive_repo(o); head = head_ref(o, &head_unknown); while ((len = getline(&line, &cap, stdin)) != -1) { char *fields[4]; int nf = 0; char *ref; char *old; char *new; level_t need; decision_t d; /* Split on whitespace by hand: the object ids and refnames in this line * never contain it, and it keeps the reader's index the same as the * writer's. */ { char *q = line; while (*q != '\0') { while (*q == ' ' || *q == '\t' || *q == '\n' || *q == '\r') q++; if (*q == '\0') break; fields[nf++] = q; while (*q != '\0' && *q != ' ' && *q != '\t' && *q != '\n' && *q != '\r') q++; if (*q != '\0') *q++ = '\0'; if (nf == 4) break; } } if (nf == 0) continue; if (nf != 3) { fprintf(stderr, "%s: deny: expected \"old new ref\", got %d field(s)\n", progname, nf); free(line); free(repo); free((char *)head); return 1; } old = fields[0]; new = fields[1]; ref = fields[2]; seen++; if (!object_id_ok(old) || !object_id_ok(new) || strncmp(ref, "refs/", 5) != 0) { fprintf(stderr, "%s: deny: \"%s %s %s\" is not an update git would send\n", progname, old, new, ref); free(line); free(repo); free((char *)head); return 1; } if (is_zero(new)) { /* A deletion. Branches cost force; the one HEAD points at costs * an admin, because a repository without a default branch is a * repository whose clones check out nothing. */ need = LV_FORCE; if (head_unknown || (head != NULL && strcmp(ref, head) == 0)) need = LV_ADMIN; } else if (is_zero(old)) { need = LV_WRITE; /* a creation */ } else { need = fast_forward(o, old, new) ? LV_WRITE : LV_FORCE; } authz_check(p, user, repo, need, ref, &d); if (!d.allowed) { fprintf(stderr, "%s: deny: %s\n", progname, d.why); free(line); free(repo); free((char *)head); return 1; } if (o.explain) printf("%s: allow: %s\n", progname, d.why); } free(line); free(repo); free((char *)head); if (seen == 0 && o.explain) printf("%s: nothing to check\n", progname); return 0; }