commit e54351df48b1ca1473604b22b1ede0f1145340d0463c4bcc2775418330203b54 from: Stefan Sperling date: Mon Sep 22 19:52:41 2025 UTC check access rules in gotweb.c ok op@ commit - c817a5d46ea7f05c5013b643e1e11055aafd553fe5dd61e596bf2c695ce69788 commit + e54351df48b1ca1473604b22b1ede0f1145340d0463c4bcc2775418330203b54 blob - 1a96690071f790669544eec759d011be2b15faf2d59d7076f8f4cbb88dcf5a74 blob + 28164c2b953c6f5eda250db907b40afff53eae66246132e4bfab02209687718c --- gotwebd/auth.c +++ gotwebd/auth.c @@ -262,6 +262,7 @@ do_login(struct request *c) const char *identifier = NULL; const time_t validity = 24 * 60 * 60; /* 1 day */ struct gotweb_url url; + struct gotwebd_repo *repo; int r; @@ -294,6 +295,20 @@ do_login(struct request *c) goto err; } + TAILQ_FOREACH(repo, &srv->repos, entry) { + switch (auth_check(&identifier, uid, &repo->access_rules)) { + case GOTWEBD_ACCESS_PERMITTED: + goto logged_in; + case GOTWEBD_ACCESS_DENIED: + case GOTWEBD_ACCESS_NO_MATCH: + break; + default: + error = got_error_fmt(GOT_ERR_LOGIN_FAILED, + "access check error for uid %u\n", uid); + goto err; + } + } + switch (auth_check(&identifier, uid, &srv->access_rules)) { case GOTWEBD_ACCESS_PERMITTED: goto logged_in; @@ -450,6 +465,36 @@ process_request(struct request *c) "access check error for uid %u\n", uid); goto done; } + } else if (c->fcgi_params.qs.action == INDEX) { + int have_public_repo = 0; + + /* + * The index page may contain a mix of repositories we have + * access to and/or for which authentication is disabled. + */ + TAILQ_FOREACH(repo, &srv->repos, entry) { + if (repo->auth_config == GOTWEBD_AUTH_DISABLED) + have_public_repo = -1; + + switch (auth_check(&identifier, uid, + &repo->access_rules)) { + case GOTWEBD_ACCESS_PERMITTED: + goto permitted; + case GOTWEBD_ACCESS_DENIED: + case GOTWEBD_ACCESS_NO_MATCH: + break; + default: + error = got_error_fmt(GOT_ERR_LOGIN_FAILED, + "access check error for uid %u\n", uid); + goto done; + } + } + + /* We have access to public repositories only. */ + if (have_public_repo) { + identifier = ""; + goto permitted; + } } switch (auth_check(&identifier, uid, &srv->access_rules)) { @@ -482,6 +527,14 @@ process_request(struct request *c) } permitted: + /* + * At this point, identifier should either be the empty string (if + * the request is allowed because authentication is partly disabled), + * or a user or group name. + */ + if (identifier == NULL) + fatalx("have no known user identifier"); + if (strlcpy(c->access_identifier, identifier, sizeof(c->access_identifier)) >= sizeof(c->access_identifier)) { error = got_error_msg(GOT_ERR_NO_SPACE, blob - f1f912488272b75c448a28239fdefb14f513a076c1cfa7b5a539ada5e4b33230 blob + 419f4fa82ff02963acd4d7eb1c52dd7b4c5f6106f71f8a22afd28e95042c06c7 --- gotwebd/gotweb.c +++ gotwebd/gotweb.c @@ -980,16 +980,48 @@ validate_path(const char *path, const char *parent_pat return error; } +static enum gotwebd_access +auth_check(struct request *c, struct gotwebd_access_rule_list *rules) +{ + struct gotwebd *env = gotwebd_env; + enum gotwebd_access access = GOTWEBD_ACCESS_NO_MATCH; + struct gotwebd_access_rule *rule; + + /* + * The www user ID implies that no user authentication occurred. + * But authentication is enabled so we must deny this request. + */ + if (c->client_uid == env->www_uid) + return GOTWEBD_ACCESS_DENIED; + + /* + * We cannot access /etc/passwd in this process so we cannot + * verify the client's user ID outselves here. + * Match rules against the access identifier which has already + * passed authentication in the auth process. + */ + STAILQ_FOREACH(rule, rules, entry) { + if (strcmp(rule->identifier, c->access_identifier) == 0) + access = rule->access; + } + + return access; +} + static const struct got_error * gotweb_load_got_path(struct repo_dir **rp, const char *dir, struct request *c) { const struct got_error *error = NULL; + struct gotwebd *env = gotwebd_env; struct server *srv = c->srv; struct transport *t = c->t; struct repo_dir *repo_dir; DIR *dt; char *dir_test; + struct gotwebd_repo *repo; + enum gotwebd_auth_config auth_config = 0; + enum gotwebd_access access = GOTWEBD_ACCESS_DENIED; *rp = calloc(1, sizeof(**rp)); if (*rp == NULL) @@ -1033,6 +1065,50 @@ gotweb_load_got_path(struct repo_dir **rp, const char if (error) goto err; + repo = gotweb_get_repository(srv, repo_dir->name); + if (repo) { + auth_config = repo->auth_config; + switch (auth_config) { + case GOTWEBD_AUTH_DISABLED: + access = GOTWEBD_ACCESS_PERMITTED; + break; + case GOTWEBD_AUTH_SECURE: + case GOTWEBD_AUTH_INSECURE: + access = auth_check(c, &repo->access_rules); + if (access == GOTWEBD_ACCESS_NO_MATCH) + access = auth_check(c, &srv->access_rules); + if (access == GOTWEBD_ACCESS_NO_MATCH) + access = auth_check(c, &env->access_rules); + if (access == GOTWEBD_ACCESS_NO_MATCH) + access = GOTWEBD_ACCESS_DENIED; + break; + } + } else { + auth_config = srv->auth_config; + switch (auth_config) { + case GOTWEBD_AUTH_DISABLED: + access = GOTWEBD_ACCESS_PERMITTED; + break; + case GOTWEBD_AUTH_SECURE: + case GOTWEBD_AUTH_INSECURE: + access = auth_check(c, &srv->access_rules); + if (access == GOTWEBD_ACCESS_NO_MATCH) + access = auth_check(c, &env->access_rules); + if (access == GOTWEBD_ACCESS_NO_MATCH) + access = GOTWEBD_ACCESS_DENIED; + break; + } + } + + if (access != GOTWEBD_ACCESS_PERMITTED && + access != GOTWEBD_ACCESS_DENIED) + fatalx("invalid access check result %d", access); + + if (access != GOTWEBD_ACCESS_PERMITTED) { + error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO); + goto err; + } + if (srv->respect_exportok && faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) { error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);