2 729a7e24 2022-11-17 thomas * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 729a7e24 2022-11-17 thomas * Copyright (c) 2015 Ted Unangst <tedu@openbsd.org>
5 729a7e24 2022-11-17 thomas * Permission to use, copy, modify, and distribute this software for any
6 729a7e24 2022-11-17 thomas * purpose with or without fee is hereby granted, provided that the above
7 729a7e24 2022-11-17 thomas * copyright notice and this permission notice appear in all copies.
9 729a7e24 2022-11-17 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 729a7e24 2022-11-17 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 729a7e24 2022-11-17 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 729a7e24 2022-11-17 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 729a7e24 2022-11-17 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 729a7e24 2022-11-17 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 729a7e24 2022-11-17 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 729a7e24 2022-11-17 thomas #include <sys/types.h>
19 0bcde4c8 2022-12-30 thomas #include <sys/socket.h>
20 729a7e24 2022-11-17 thomas #include <sys/queue.h>
21 729a7e24 2022-11-17 thomas #include <sys/uio.h>
23 729a7e24 2022-11-17 thomas #include <errno.h>
24 729a7e24 2022-11-17 thomas #include <event.h>
25 729a7e24 2022-11-17 thomas #include <limits.h>
26 729a7e24 2022-11-17 thomas #include <pwd.h>
27 729a7e24 2022-11-17 thomas #include <grp.h>
28 c669c489 2022-12-30 thomas #include <signal.h>
29 729a7e24 2022-11-17 thomas #include <stdint.h>
30 729a7e24 2022-11-17 thomas #include <stdio.h>
31 729a7e24 2022-11-17 thomas #include <stdlib.h>
32 c669c489 2022-12-30 thomas #include <string.h>
33 729a7e24 2022-11-17 thomas #include <imsg.h>
34 ff260661 2022-11-17 thomas #include <unistd.h>
36 729a7e24 2022-11-17 thomas #include "got_error.h"
37 c669c489 2022-12-30 thomas #include "got_path.h"
39 729a7e24 2022-11-17 thomas #include "gotd.h"
40 ff260661 2022-11-17 thomas #include "log.h"
41 729a7e24 2022-11-17 thomas #include "auth.h"
43 c669c489 2022-12-30 thomas static struct gotd_auth {
44 c669c489 2022-12-30 thomas pid_t pid;
45 c669c489 2022-12-30 thomas const char *title;
46 c669c489 2022-12-30 thomas struct gotd_repo *repo;
47 c669c489 2022-12-30 thomas } gotd_auth;
49 c669c489 2022-12-30 thomas static void auth_shutdown(void);
51 c669c489 2022-12-30 thomas static void
52 c669c489 2022-12-30 thomas auth_sighdlr(int sig, short event, void *arg)
55 c669c489 2022-12-30 thomas * Normal signal handler rules don't apply because libevent
56 c669c489 2022-12-30 thomas * decouples for us.
59 c669c489 2022-12-30 thomas switch (sig) {
60 c669c489 2022-12-30 thomas case SIGHUP:
62 c669c489 2022-12-30 thomas case SIGUSR1:
64 c669c489 2022-12-30 thomas case SIGTERM:
65 c669c489 2022-12-30 thomas case SIGINT:
66 c669c489 2022-12-30 thomas auth_shutdown();
67 c669c489 2022-12-30 thomas /* NOTREACHED */
70 c669c489 2022-12-30 thomas fatalx("unexpected signal");
74 729a7e24 2022-11-17 thomas static int
75 729a7e24 2022-11-17 thomas uidcheck(const char *s, uid_t desired)
77 729a7e24 2022-11-17 thomas uid_t uid;
79 48488136 2023-04-22 thomas if (gotd_parseuid(s, &uid) != 0)
80 729a7e24 2022-11-17 thomas return -1;
81 729a7e24 2022-11-17 thomas if (uid != desired)
82 729a7e24 2022-11-17 thomas return -1;
86 729a7e24 2022-11-17 thomas static int
87 729a7e24 2022-11-17 thomas parsegid(const char *s, gid_t *gid)
89 729a7e24 2022-11-17 thomas struct group *gr;
90 729a7e24 2022-11-17 thomas const char *errstr;
92 729a7e24 2022-11-17 thomas if ((gr = getgrnam(s)) != NULL) {
93 729a7e24 2022-11-17 thomas *gid = gr->gr_gid;
94 729a7e24 2022-11-17 thomas if (*gid == GID_MAX)
95 729a7e24 2022-11-17 thomas return -1;
98 729a7e24 2022-11-17 thomas *gid = strtonum(s, 0, GID_MAX - 1, &errstr);
99 729a7e24 2022-11-17 thomas if (errstr)
100 729a7e24 2022-11-17 thomas return -1;
101 729a7e24 2022-11-17 thomas return 0;
104 729a7e24 2022-11-17 thomas static int
105 729a7e24 2022-11-17 thomas match_identifier(const char *identifier, gid_t *groups, int ngroups,
106 729a7e24 2022-11-17 thomas uid_t euid, gid_t egid)
110 729a7e24 2022-11-17 thomas if (identifier[0] == ':') {
111 729a7e24 2022-11-17 thomas gid_t rgid;
112 729a7e24 2022-11-17 thomas if (parsegid(identifier + 1, &rgid) == -1)
113 729a7e24 2022-11-17 thomas return 0;
114 ff260661 2022-11-17 thomas if (rgid == egid)
115 ff260661 2022-11-17 thomas return 1;
116 729a7e24 2022-11-17 thomas for (i = 0; i < ngroups; i++) {
117 ff260661 2022-11-17 thomas if (rgid == groups[i])
120 729a7e24 2022-11-17 thomas if (i == ngroups)
121 729a7e24 2022-11-17 thomas return 0;
122 729a7e24 2022-11-17 thomas } else if (uidcheck(identifier, euid) != 0)
123 729a7e24 2022-11-17 thomas return 0;
125 729a7e24 2022-11-17 thomas return 1;
128 c669c489 2022-12-30 thomas static const struct got_error *
129 c669c489 2022-12-30 thomas auth_check(struct gotd_access_rule_list *rules, const char *repo_name,
130 ff260661 2022-11-17 thomas uid_t euid, gid_t egid, int required_auth)
132 729a7e24 2022-11-17 thomas struct gotd_access_rule *rule;
133 729a7e24 2022-11-17 thomas enum gotd_access access = GOTD_ACCESS_DENIED;
134 ff260661 2022-11-17 thomas struct passwd *pw;
135 ff260661 2022-11-17 thomas gid_t groups[NGROUPS_MAX];
136 ff260661 2022-11-17 thomas int ngroups = NGROUPS_MAX;
138 ff260661 2022-11-17 thomas pw = getpwuid(euid);
139 9928b132 2022-11-20 thomas if (pw == NULL) {
140 9928b132 2022-11-20 thomas if (errno)
141 9928b132 2022-11-20 thomas return got_error_from_errno("getpwuid");
143 9928b132 2022-11-20 thomas return got_error_set_errno(EACCES, repo_name);
146 ff260661 2022-11-17 thomas if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups) == -1)
147 ff260661 2022-11-17 thomas log_warnx("group membership list truncated");
149 729a7e24 2022-11-17 thomas STAILQ_FOREACH(rule, rules, entry) {
150 729a7e24 2022-11-17 thomas if (!match_identifier(rule->identifier, groups, ngroups,
151 729a7e24 2022-11-17 thomas euid, egid))
152 729a7e24 2022-11-17 thomas continue;
154 729a7e24 2022-11-17 thomas access = rule->access;
155 729a7e24 2022-11-17 thomas if (rule->access == GOTD_ACCESS_PERMITTED &&
156 729a7e24 2022-11-17 thomas (rule->authorization & required_auth) != required_auth)
157 729a7e24 2022-11-17 thomas access = GOTD_ACCESS_DENIED;
160 729a7e24 2022-11-17 thomas if (access == GOTD_ACCESS_DENIED)
161 729a7e24 2022-11-17 thomas return got_error_set_errno(EACCES, repo_name);
163 729a7e24 2022-11-17 thomas if (access == GOTD_ACCESS_PERMITTED)
164 729a7e24 2022-11-17 thomas return NULL;
166 729a7e24 2022-11-17 thomas /* should not happen, this would be a bug */
167 729a7e24 2022-11-17 thomas return got_error_msg(GOT_ERR_NOT_IMPL, "bad access rule");
170 c669c489 2022-12-30 thomas static const struct got_error *
171 c669c489 2022-12-30 thomas recv_authreq(struct imsg *imsg, struct gotd_imsgev *iev)
173 c669c489 2022-12-30 thomas const struct got_error *err;
174 c669c489 2022-12-30 thomas struct imsgbuf *ibuf = &iev->ibuf;
175 c669c489 2022-12-30 thomas struct gotd_imsg_auth iauth;
176 c669c489 2022-12-30 thomas size_t datalen;
177 0bcde4c8 2022-12-30 thomas uid_t euid;
178 0bcde4c8 2022-12-30 thomas gid_t egid;
180 c669c489 2022-12-30 thomas log_debug("authentication request received");
182 c669c489 2022-12-30 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
183 c669c489 2022-12-30 thomas if (datalen != sizeof(iauth))
184 c669c489 2022-12-30 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
186 c669c489 2022-12-30 thomas memcpy(&iauth, imsg->data, datalen);
188 0bcde4c8 2022-12-30 thomas if (imsg->fd == -1)
189 0bcde4c8 2022-12-30 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
191 0bcde4c8 2022-12-30 thomas if (getpeereid(imsg->fd, &euid, &egid) == -1)
192 0bcde4c8 2022-12-30 thomas return got_error_from_errno("getpeerid");
194 0bcde4c8 2022-12-30 thomas if (iauth.euid != euid)
195 0bcde4c8 2022-12-30 thomas return got_error(GOT_ERR_UID);
196 0bcde4c8 2022-12-30 thomas if (iauth.egid != egid)
197 0bcde4c8 2022-12-30 thomas return got_error(GOT_ERR_GID);
199 0bcde4c8 2022-12-30 thomas log_debug("authenticating uid %d gid %d", euid, egid);
201 c669c489 2022-12-30 thomas err = auth_check(&gotd_auth.repo->rules, gotd_auth.repo->name,
202 c669c489 2022-12-30 thomas iauth.euid, iauth.egid, iauth.required_auth);
203 c669c489 2022-12-30 thomas if (err) {
204 c669c489 2022-12-30 thomas gotd_imsg_send_error(ibuf, PROC_AUTH, iauth.client_id, err);
205 c669c489 2022-12-30 thomas return err;
208 c669c489 2022-12-30 thomas if (gotd_imsg_compose_event(iev, GOTD_IMSG_ACCESS_GRANTED,
209 c669c489 2022-12-30 thomas PROC_AUTH, -1, NULL, 0) == -1)
210 c669c489 2022-12-30 thomas return got_error_from_errno("imsg compose ACCESS_GRANTED");
212 c669c489 2022-12-30 thomas return NULL;
215 c669c489 2022-12-30 thomas static void
216 c669c489 2022-12-30 thomas auth_dispatch(int fd, short event, void *arg)
218 c669c489 2022-12-30 thomas const struct got_error *err = NULL;
219 c669c489 2022-12-30 thomas struct gotd_imsgev *iev = arg;
220 c669c489 2022-12-30 thomas struct imsgbuf *ibuf = &iev->ibuf;
221 c669c489 2022-12-30 thomas struct imsg imsg;
222 c669c489 2022-12-30 thomas ssize_t n;
223 c669c489 2022-12-30 thomas int shut = 0;
225 c669c489 2022-12-30 thomas if (event & EV_READ) {
226 c669c489 2022-12-30 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
227 c669c489 2022-12-30 thomas fatal("imsg_read error");
228 c669c489 2022-12-30 thomas if (n == 0) /* Connection closed. */
229 c669c489 2022-12-30 thomas shut = 1;
232 c669c489 2022-12-30 thomas if (event & EV_WRITE) {
233 c669c489 2022-12-30 thomas n = msgbuf_write(&ibuf->w);
234 c669c489 2022-12-30 thomas if (n == -1 && errno != EAGAIN)
235 c669c489 2022-12-30 thomas fatal("msgbuf_write");
236 c669c489 2022-12-30 thomas if (n == 0) /* Connection closed. */
237 c669c489 2022-12-30 thomas shut = 1;
240 c669c489 2022-12-30 thomas for (;;) {
241 c669c489 2022-12-30 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
242 c669c489 2022-12-30 thomas fatal("%s: imsg_get", __func__);
243 c669c489 2022-12-30 thomas if (n == 0) /* No more messages. */
246 c669c489 2022-12-30 thomas switch (imsg.hdr.type) {
247 c669c489 2022-12-30 thomas case GOTD_IMSG_AUTHENTICATE:
248 c669c489 2022-12-30 thomas err = recv_authreq(&imsg, iev);
250 5330ab76 2023-02-17 thomas log_warnx("%s", err->msg);
253 5330ab76 2023-02-17 thomas log_debug("unexpected imsg %d", imsg.hdr.type);
257 c669c489 2022-12-30 thomas imsg_free(&imsg);
260 c669c489 2022-12-30 thomas if (!shut) {
261 c669c489 2022-12-30 thomas gotd_imsg_event_add(iev);
263 c669c489 2022-12-30 thomas /* This pipe is dead. Remove its event handler */
264 c669c489 2022-12-30 thomas event_del(&iev->ev);
265 c669c489 2022-12-30 thomas event_loopexit(NULL);
270 c669c489 2022-12-30 thomas auth_main(const char *title, struct gotd_repolist *repos,
271 c669c489 2022-12-30 thomas const char *repo_path)
273 c669c489 2022-12-30 thomas struct gotd_repo *repo = NULL;
274 c669c489 2022-12-30 thomas struct gotd_imsgev iev;
275 c669c489 2022-12-30 thomas struct event evsigint, evsigterm, evsighup, evsigusr1;
277 c669c489 2022-12-30 thomas gotd_auth.title = title;
278 c669c489 2022-12-30 thomas gotd_auth.pid = getpid();
279 c669c489 2022-12-30 thomas TAILQ_FOREACH(repo, repos, entry) {
280 c669c489 2022-12-30 thomas if (got_path_cmp(repo->path, repo_path,
281 c669c489 2022-12-30 thomas strlen(repo->path), strlen(repo_path)) == 0)
284 c669c489 2022-12-30 thomas if (repo == NULL)
285 c669c489 2022-12-30 thomas fatalx("repository %s not found in config", repo_path);
286 c669c489 2022-12-30 thomas gotd_auth.repo = repo;
288 c669c489 2022-12-30 thomas signal_set(&evsigint, SIGINT, auth_sighdlr, NULL);
289 c669c489 2022-12-30 thomas signal_set(&evsigterm, SIGTERM, auth_sighdlr, NULL);
290 c669c489 2022-12-30 thomas signal_set(&evsighup, SIGHUP, auth_sighdlr, NULL);
291 c669c489 2022-12-30 thomas signal_set(&evsigusr1, SIGUSR1, auth_sighdlr, NULL);
292 c669c489 2022-12-30 thomas signal(SIGPIPE, SIG_IGN);
294 c669c489 2022-12-30 thomas signal_add(&evsigint, NULL);
295 c669c489 2022-12-30 thomas signal_add(&evsigterm, NULL);
296 c669c489 2022-12-30 thomas signal_add(&evsighup, NULL);
297 c669c489 2022-12-30 thomas signal_add(&evsigusr1, NULL);
299 c669c489 2022-12-30 thomas imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
300 c669c489 2022-12-30 thomas iev.handler = auth_dispatch;
301 c669c489 2022-12-30 thomas iev.events = EV_READ;
302 c669c489 2022-12-30 thomas iev.handler_arg = NULL;
303 c669c489 2022-12-30 thomas event_set(&iev.ev, iev.ibuf.fd, EV_READ, auth_dispatch, &iev);
304 c669c489 2022-12-30 thomas if (event_add(&iev.ev, NULL) == -1)
305 c669c489 2022-12-30 thomas fatalx("event add");
307 c669c489 2022-12-30 thomas event_dispatch();
309 c669c489 2022-12-30 thomas auth_shutdown();
312 c669c489 2022-12-30 thomas static void
313 c669c489 2022-12-30 thomas auth_shutdown(void)
315 5330ab76 2023-02-17 thomas log_debug("shutting down");