2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 * Copyright (c) 2015 Ted Unangst <tedu@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <sys/queue.h>
38 #include "got_error.h"
45 static struct gotd_auth {
48 struct gotd_repo *repo;
51 static void auth_shutdown(void);
54 auth_sighdlr(int sig, short event, void *arg)
57 * Normal signal handler rules don't apply because libevent
72 fatalx("unexpected signal");
77 gotd_auth_parseuid(const char *s, uid_t *uid)
82 if ((pw = getpwnam(s)) != NULL) {
88 *uid = strtonum(s, 0, UID_MAX - 1, &errstr);
95 uidcheck(const char *s, uid_t desired)
99 if (gotd_auth_parseuid(s, &uid) != 0)
107 parsegid(const char *s, gid_t *gid)
112 if ((gr = getgrnam(s)) != NULL) {
118 *gid = strtonum(s, 0, GID_MAX - 1, &errstr);
125 match_identifier(const char *identifier, gid_t *groups, int ngroups,
126 uid_t euid, gid_t egid)
130 if (identifier[0] == ':') {
132 if (parsegid(identifier + 1, &rgid) == -1)
136 for (i = 0; i < ngroups; i++) {
137 if (rgid == groups[i])
142 } else if (uidcheck(identifier, euid) != 0)
148 static const struct got_error *
149 auth_check(struct gotd_access_rule_list *rules, const char *repo_name,
150 uid_t euid, gid_t egid, int required_auth)
152 struct gotd_access_rule *rule;
153 enum gotd_access access = GOTD_ACCESS_DENIED;
155 gid_t groups[NGROUPS_MAX];
156 int ngroups = NGROUPS_MAX;
161 return got_error_from_errno("getpwuid");
163 return got_error_set_errno(EACCES, repo_name);
166 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups) == -1)
167 log_warnx("group membership list truncated");
169 STAILQ_FOREACH(rule, rules, entry) {
170 if (!match_identifier(rule->identifier, groups, ngroups,
174 access = rule->access;
175 if (rule->access == GOTD_ACCESS_PERMITTED &&
176 (rule->authorization & required_auth) != required_auth)
177 access = GOTD_ACCESS_DENIED;
180 if (access == GOTD_ACCESS_DENIED)
181 return got_error_set_errno(EACCES, repo_name);
183 if (access == GOTD_ACCESS_PERMITTED)
186 /* should not happen, this would be a bug */
187 return got_error_msg(GOT_ERR_NOT_IMPL, "bad access rule");
190 static const struct got_error *
191 recv_authreq(struct imsg *imsg, struct gotd_imsgev *iev)
193 const struct got_error *err;
194 struct imsgbuf *ibuf = &iev->ibuf;
195 struct gotd_imsg_auth iauth;
200 log_debug("authentication request received");
202 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
203 if (datalen != sizeof(iauth))
204 return got_error(GOT_ERR_PRIVSEP_LEN);
206 memcpy(&iauth, imsg->data, datalen);
209 return got_error(GOT_ERR_PRIVSEP_NO_FD);
211 if (getpeereid(imsg->fd, &euid, &egid) == -1)
212 return got_error_from_errno("getpeerid");
214 if (iauth.euid != euid)
215 return got_error(GOT_ERR_UID);
216 if (iauth.egid != egid)
217 return got_error(GOT_ERR_GID);
219 log_debug("authenticating uid %d gid %d", euid, egid);
221 err = auth_check(&gotd_auth.repo->rules, gotd_auth.repo->name,
222 iauth.euid, iauth.egid, iauth.required_auth);
224 gotd_imsg_send_error(ibuf, PROC_AUTH, iauth.client_id, err);
228 if (gotd_imsg_compose_event(iev, GOTD_IMSG_ACCESS_GRANTED,
229 PROC_AUTH, -1, NULL, 0) == -1)
230 return got_error_from_errno("imsg compose ACCESS_GRANTED");
236 auth_dispatch(int fd, short event, void *arg)
238 const struct got_error *err = NULL;
239 struct gotd_imsgev *iev = arg;
240 struct imsgbuf *ibuf = &iev->ibuf;
245 if (event & EV_READ) {
246 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
247 fatal("imsg_read error");
248 if (n == 0) /* Connection closed. */
252 if (event & EV_WRITE) {
253 n = msgbuf_write(&ibuf->w);
254 if (n == -1 && errno != EAGAIN)
255 fatal("msgbuf_write");
256 if (n == 0) /* Connection closed. */
261 if ((n = imsg_get(ibuf, &imsg)) == -1)
262 fatal("%s: imsg_get", __func__);
263 if (n == 0) /* No more messages. */
266 switch (imsg.hdr.type) {
267 case GOTD_IMSG_AUTHENTICATE:
268 err = recv_authreq(&imsg, iev);
270 log_warnx("%s", err->msg);
273 log_debug("unexpected imsg %d", imsg.hdr.type);
281 gotd_imsg_event_add(iev);
283 /* This pipe is dead. Remove its event handler */
285 event_loopexit(NULL);
290 auth_main(const char *title, struct gotd_repolist *repos,
291 const char *repo_path)
293 struct gotd_repo *repo = NULL;
294 struct gotd_imsgev iev;
295 struct event evsigint, evsigterm, evsighup, evsigusr1;
297 gotd_auth.title = title;
298 gotd_auth.pid = getpid();
299 TAILQ_FOREACH(repo, repos, entry) {
300 if (got_path_cmp(repo->path, repo_path,
301 strlen(repo->path), strlen(repo_path)) == 0)
305 fatalx("repository %s not found in config", repo_path);
306 gotd_auth.repo = repo;
308 signal_set(&evsigint, SIGINT, auth_sighdlr, NULL);
309 signal_set(&evsigterm, SIGTERM, auth_sighdlr, NULL);
310 signal_set(&evsighup, SIGHUP, auth_sighdlr, NULL);
311 signal_set(&evsigusr1, SIGUSR1, auth_sighdlr, NULL);
312 signal(SIGPIPE, SIG_IGN);
314 signal_add(&evsigint, NULL);
315 signal_add(&evsigterm, NULL);
316 signal_add(&evsighup, NULL);
317 signal_add(&evsigusr1, NULL);
319 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
320 iev.handler = auth_dispatch;
321 iev.events = EV_READ;
322 iev.handler_arg = NULL;
323 event_set(&iev.ev, iev.ibuf.fd, EV_READ, auth_dispatch, &iev);
324 if (event_add(&iev.ev, NULL) == -1)
335 log_debug("shutting down");