2 0ccf3acb 2022-11-16 stsp * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 0ccf3acb 2022-11-16 stsp * Copyright (c) 2015 Ted Unangst <tedu@openbsd.org>
5 0ccf3acb 2022-11-16 stsp * Permission to use, copy, modify, and distribute this software for any
6 0ccf3acb 2022-11-16 stsp * purpose with or without fee is hereby granted, provided that the above
7 0ccf3acb 2022-11-16 stsp * copyright notice and this permission notice appear in all copies.
9 0ccf3acb 2022-11-16 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 0ccf3acb 2022-11-16 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 0ccf3acb 2022-11-16 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 0ccf3acb 2022-11-16 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 0ccf3acb 2022-11-16 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 0ccf3acb 2022-11-16 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 0ccf3acb 2022-11-16 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 0ccf3acb 2022-11-16 stsp #include <sys/types.h>
19 365cf0f3 2022-12-29 stsp #include <sys/socket.h>
20 0ccf3acb 2022-11-16 stsp #include <sys/queue.h>
21 0ccf3acb 2022-11-16 stsp #include <sys/uio.h>
23 0ccf3acb 2022-11-16 stsp #include <errno.h>
24 0ccf3acb 2022-11-16 stsp #include <event.h>
25 0ccf3acb 2022-11-16 stsp #include <limits.h>
26 0ccf3acb 2022-11-16 stsp #include <pwd.h>
27 0ccf3acb 2022-11-16 stsp #include <grp.h>
28 0ccf3acb 2022-11-16 stsp #include <sha1.h>
29 5822e79e 2023-02-23 op #include <sha2.h>
30 5e25db14 2022-12-29 stsp #include <signal.h>
31 0ccf3acb 2022-11-16 stsp #include <stdint.h>
32 0ccf3acb 2022-11-16 stsp #include <stdio.h>
33 0ccf3acb 2022-11-16 stsp #include <stdlib.h>
34 5e25db14 2022-12-29 stsp #include <string.h>
35 0ccf3acb 2022-11-16 stsp #include <imsg.h>
36 ddbe612c 2022-11-17 stsp #include <unistd.h>
38 0ccf3acb 2022-11-16 stsp #include "got_error.h"
39 1b1a386d 2024-07-09 op #include "got_object.h"
40 5e25db14 2022-12-29 stsp #include "got_path.h"
42 0ccf3acb 2022-11-16 stsp #include "gotd.h"
43 ddbe612c 2022-11-17 stsp #include "log.h"
44 0ccf3acb 2022-11-16 stsp #include "auth.h"
46 5e25db14 2022-12-29 stsp static struct gotd_auth {
48 5e25db14 2022-12-29 stsp const char *title;
49 5e25db14 2022-12-29 stsp struct gotd_repo *repo;
50 5e25db14 2022-12-29 stsp } gotd_auth;
52 5e25db14 2022-12-29 stsp static void auth_shutdown(void);
55 5e25db14 2022-12-29 stsp auth_sighdlr(int sig, short event, void *arg)
58 5e25db14 2022-12-29 stsp * Normal signal handler rules don't apply because libevent
59 5e25db14 2022-12-29 stsp * decouples for us.
62 5e25db14 2022-12-29 stsp switch (sig) {
63 5e25db14 2022-12-29 stsp case SIGHUP:
65 5e25db14 2022-12-29 stsp case SIGUSR1:
67 5e25db14 2022-12-29 stsp case SIGTERM:
68 5e25db14 2022-12-29 stsp case SIGINT:
69 5e25db14 2022-12-29 stsp auth_shutdown();
70 5e25db14 2022-12-29 stsp /* NOTREACHED */
73 5e25db14 2022-12-29 stsp fatalx("unexpected signal");
78 0ccf3acb 2022-11-16 stsp uidcheck(const char *s, uid_t desired)
82 1963be61 2023-04-14 stsp if (gotd_parseuid(s, &uid) != 0)
84 0ccf3acb 2022-11-16 stsp if (uid != desired)
90 0ccf3acb 2022-11-16 stsp parsegid(const char *s, gid_t *gid)
92 0ccf3acb 2022-11-16 stsp struct group *gr;
93 0ccf3acb 2022-11-16 stsp const char *errstr;
95 0ccf3acb 2022-11-16 stsp if ((gr = getgrnam(s)) != NULL) {
96 0ccf3acb 2022-11-16 stsp *gid = gr->gr_gid;
97 0ccf3acb 2022-11-16 stsp if (*gid == GID_MAX)
101 0ccf3acb 2022-11-16 stsp *gid = strtonum(s, 0, GID_MAX - 1, &errstr);
102 0ccf3acb 2022-11-16 stsp if (errstr)
108 0ccf3acb 2022-11-16 stsp match_identifier(const char *identifier, gid_t *groups, int ngroups,
109 0ccf3acb 2022-11-16 stsp uid_t euid, gid_t egid)
113 0ccf3acb 2022-11-16 stsp if (identifier[0] == ':') {
114 0ccf3acb 2022-11-16 stsp gid_t rgid;
115 0ccf3acb 2022-11-16 stsp if (parsegid(identifier + 1, &rgid) == -1)
117 ddbe612c 2022-11-17 stsp if (rgid == egid)
119 0ccf3acb 2022-11-16 stsp for (i = 0; i < ngroups; i++) {
120 ddbe612c 2022-11-17 stsp if (rgid == groups[i])
123 0ccf3acb 2022-11-16 stsp if (i == ngroups)
125 0ccf3acb 2022-11-16 stsp } else if (uidcheck(identifier, euid) != 0)
131 5e25db14 2022-12-29 stsp static const struct got_error *
132 56624d2b 2023-12-27 stsp auth_check(char **username, struct gotd_access_rule_list *rules,
133 56624d2b 2023-12-27 stsp const char *repo_name, uid_t euid, gid_t egid, int required_auth)
135 0ccf3acb 2022-11-16 stsp struct gotd_access_rule *rule;
136 0ccf3acb 2022-11-16 stsp enum gotd_access access = GOTD_ACCESS_DENIED;
137 ddbe612c 2022-11-17 stsp struct passwd *pw;
138 ddbe612c 2022-11-17 stsp gid_t groups[NGROUPS_MAX];
139 ddbe612c 2022-11-17 stsp int ngroups = NGROUPS_MAX;
140 9c574a76 2024-05-06 stsp int matched_user = 0;
142 56624d2b 2023-12-27 stsp *username = NULL;
144 ddbe612c 2022-11-17 stsp pw = getpwuid(euid);
145 e18d071f 2022-11-20 stsp if (pw == NULL) {
147 e18d071f 2022-11-20 stsp return got_error_from_errno("getpwuid");
149 e18d071f 2022-11-20 stsp return got_error_set_errno(EACCES, repo_name);
152 56624d2b 2023-12-27 stsp *username = strdup(pw->pw_name);
153 56624d2b 2023-12-27 stsp if (*username == NULL)
154 56624d2b 2023-12-27 stsp return got_error_from_errno("strdup");
156 ddbe612c 2022-11-17 stsp if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups) == -1)
157 ddbe612c 2022-11-17 stsp log_warnx("group membership list truncated");
159 0ccf3acb 2022-11-16 stsp STAILQ_FOREACH(rule, rules, entry) {
160 0ccf3acb 2022-11-16 stsp if (!match_identifier(rule->identifier, groups, ngroups,
161 0ccf3acb 2022-11-16 stsp euid, egid))
164 9c574a76 2024-05-06 stsp matched_user = 1;
165 0ccf3acb 2022-11-16 stsp access = rule->access;
166 0ccf3acb 2022-11-16 stsp if (rule->access == GOTD_ACCESS_PERMITTED &&
167 0ccf3acb 2022-11-16 stsp (rule->authorization & required_auth) != required_auth)
168 0ccf3acb 2022-11-16 stsp access = GOTD_ACCESS_DENIED;
171 9c574a76 2024-05-06 stsp if (access == GOTD_ACCESS_DENIED) {
173 9c574a76 2024-05-06 stsp * If a user has no explicit read or write access then
174 9c574a76 2024-05-06 stsp * do not leak the existence of a repository to them.
176 9c574a76 2024-05-06 stsp if (!matched_user)
177 9c574a76 2024-05-06 stsp return got_error(GOT_ERR_NOT_GIT_REPO);
179 9c574a76 2024-05-06 stsp return got_error_set_errno(EACCES, repo_name);
182 0ccf3acb 2022-11-16 stsp if (access == GOTD_ACCESS_PERMITTED)
183 0ccf3acb 2022-11-16 stsp return NULL;
185 0ccf3acb 2022-11-16 stsp /* should not happen, this would be a bug */
186 0ccf3acb 2022-11-16 stsp return got_error_msg(GOT_ERR_NOT_IMPL, "bad access rule");
189 5e25db14 2022-12-29 stsp static const struct got_error *
190 5e25db14 2022-12-29 stsp recv_authreq(struct imsg *imsg, struct gotd_imsgev *iev)
192 5e25db14 2022-12-29 stsp const struct got_error *err;
193 5e25db14 2022-12-29 stsp struct imsgbuf *ibuf = &iev->ibuf;
194 5e25db14 2022-12-29 stsp struct gotd_imsg_auth iauth;
195 5e25db14 2022-12-29 stsp size_t datalen;
196 365cf0f3 2022-12-29 stsp uid_t euid;
197 365cf0f3 2022-12-29 stsp gid_t egid;
198 56624d2b 2023-12-27 stsp char *username = NULL;
199 56624d2b 2023-12-27 stsp size_t len;
200 56624d2b 2023-12-27 stsp const size_t maxlen = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
203 5e25db14 2022-12-29 stsp log_debug("authentication request received");
205 5e25db14 2022-12-29 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
206 5e25db14 2022-12-29 stsp if (datalen != sizeof(iauth))
207 5e25db14 2022-12-29 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
209 5e25db14 2022-12-29 stsp memcpy(&iauth, imsg->data, datalen);
211 2c52c623 2024-01-30 op fd = imsg_get_fd(imsg);
212 2c52c623 2024-01-30 op if (fd == -1)
213 365cf0f3 2022-12-29 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
215 2c52c623 2024-01-30 op if (getpeereid(fd, &euid, &egid) == -1)
216 365cf0f3 2022-12-29 stsp return got_error_from_errno("getpeerid");
218 365cf0f3 2022-12-29 stsp if (iauth.euid != euid)
219 365cf0f3 2022-12-29 stsp return got_error(GOT_ERR_UID);
220 365cf0f3 2022-12-29 stsp if (iauth.egid != egid)
221 365cf0f3 2022-12-29 stsp return got_error(GOT_ERR_GID);
223 365cf0f3 2022-12-29 stsp log_debug("authenticating uid %d gid %d", euid, egid);
225 56624d2b 2023-12-27 stsp err = auth_check(&username, &gotd_auth.repo->rules,
226 56624d2b 2023-12-27 stsp gotd_auth.repo->name, iauth.euid, iauth.egid, iauth.required_auth);
228 5e25db14 2022-12-29 stsp gotd_imsg_send_error(ibuf, PROC_AUTH, iauth.client_id, err);
232 56624d2b 2023-12-27 stsp len = strlen(username);
233 56624d2b 2023-12-27 stsp if (len > maxlen)
234 56624d2b 2023-12-27 stsp len = maxlen;
236 56624d2b 2023-12-27 stsp if (gotd_imsg_compose_event(iev, GOTD_IMSG_ACCESS_GRANTED,
237 56624d2b 2023-12-27 stsp PROC_AUTH, -1, username, len) == -1)
238 56624d2b 2023-12-27 stsp err = got_error_from_errno("imsg compose ACCESS_GRANTED");
240 56624d2b 2023-12-27 stsp free(username);
241 56624d2b 2023-12-27 stsp return err;
244 5e25db14 2022-12-29 stsp static void
245 5e25db14 2022-12-29 stsp auth_dispatch(int fd, short event, void *arg)
247 5e25db14 2022-12-29 stsp const struct got_error *err = NULL;
248 5e25db14 2022-12-29 stsp struct gotd_imsgev *iev = arg;
249 5e25db14 2022-12-29 stsp struct imsgbuf *ibuf = &iev->ibuf;
250 5e25db14 2022-12-29 stsp struct imsg imsg;
252 5e25db14 2022-12-29 stsp int shut = 0;
254 5e25db14 2022-12-29 stsp if (event & EV_READ) {
255 5e25db14 2022-12-29 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
256 5e25db14 2022-12-29 stsp fatal("imsg_read error");
257 5e25db14 2022-12-29 stsp if (n == 0) /* Connection closed. */
261 5e25db14 2022-12-29 stsp if (event & EV_WRITE) {
262 5e25db14 2022-12-29 stsp n = msgbuf_write(&ibuf->w);
263 5e25db14 2022-12-29 stsp if (n == -1 && errno != EAGAIN)
264 5e25db14 2022-12-29 stsp fatal("msgbuf_write");
265 5e25db14 2022-12-29 stsp if (n == 0) /* Connection closed. */
270 5e25db14 2022-12-29 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
271 5e25db14 2022-12-29 stsp fatal("%s: imsg_get", __func__);
272 5e25db14 2022-12-29 stsp if (n == 0) /* No more messages. */
275 5e25db14 2022-12-29 stsp switch (imsg.hdr.type) {
276 5e25db14 2022-12-29 stsp case GOTD_IMSG_AUTHENTICATE:
277 5e25db14 2022-12-29 stsp err = recv_authreq(&imsg, iev);
279 2ec74a9e 2023-02-08 op log_warnx("%s", err->msg);
282 2ec74a9e 2023-02-08 op log_debug("unexpected imsg %d", imsg.hdr.type);
286 5e25db14 2022-12-29 stsp imsg_free(&imsg);
289 5e25db14 2022-12-29 stsp if (!shut) {
290 5e25db14 2022-12-29 stsp gotd_imsg_event_add(iev);
292 5e25db14 2022-12-29 stsp /* This pipe is dead. Remove its event handler */
293 5e25db14 2022-12-29 stsp event_del(&iev->ev);
294 5e25db14 2022-12-29 stsp event_loopexit(NULL);
299 5e25db14 2022-12-29 stsp auth_main(const char *title, struct gotd_repolist *repos,
300 5e25db14 2022-12-29 stsp const char *repo_path)
302 5e25db14 2022-12-29 stsp struct gotd_repo *repo = NULL;
303 5e25db14 2022-12-29 stsp struct gotd_imsgev iev;
304 5e25db14 2022-12-29 stsp struct event evsigint, evsigterm, evsighup, evsigusr1;
306 5e25db14 2022-12-29 stsp gotd_auth.title = title;
307 5e25db14 2022-12-29 stsp gotd_auth.pid = getpid();
308 5e25db14 2022-12-29 stsp TAILQ_FOREACH(repo, repos, entry) {
309 5e25db14 2022-12-29 stsp if (got_path_cmp(repo->path, repo_path,
310 5e25db14 2022-12-29 stsp strlen(repo->path), strlen(repo_path)) == 0)
313 5e25db14 2022-12-29 stsp if (repo == NULL)
314 5e25db14 2022-12-29 stsp fatalx("repository %s not found in config", repo_path);
315 5e25db14 2022-12-29 stsp gotd_auth.repo = repo;
317 5e25db14 2022-12-29 stsp signal_set(&evsigint, SIGINT, auth_sighdlr, NULL);
318 5e25db14 2022-12-29 stsp signal_set(&evsigterm, SIGTERM, auth_sighdlr, NULL);
319 5e25db14 2022-12-29 stsp signal_set(&evsighup, SIGHUP, auth_sighdlr, NULL);
320 5e25db14 2022-12-29 stsp signal_set(&evsigusr1, SIGUSR1, auth_sighdlr, NULL);
321 5e25db14 2022-12-29 stsp signal(SIGPIPE, SIG_IGN);
323 5e25db14 2022-12-29 stsp signal_add(&evsigint, NULL);
324 5e25db14 2022-12-29 stsp signal_add(&evsigterm, NULL);
325 5e25db14 2022-12-29 stsp signal_add(&evsighup, NULL);
326 5e25db14 2022-12-29 stsp signal_add(&evsigusr1, NULL);
328 5e25db14 2022-12-29 stsp imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
329 5e25db14 2022-12-29 stsp iev.handler = auth_dispatch;
330 5e25db14 2022-12-29 stsp iev.events = EV_READ;
331 5e25db14 2022-12-29 stsp iev.handler_arg = NULL;
332 5e25db14 2022-12-29 stsp event_set(&iev.ev, iev.ibuf.fd, EV_READ, auth_dispatch, &iev);
333 5e25db14 2022-12-29 stsp if (event_add(&iev.ev, NULL) == -1)
334 5e25db14 2022-12-29 stsp fatalx("event add");
336 5e25db14 2022-12-29 stsp event_dispatch();
338 5e25db14 2022-12-29 stsp auth_shutdown();
341 5e25db14 2022-12-29 stsp static void
342 5e25db14 2022-12-29 stsp auth_shutdown(void)
344 e8d451cc 2024-03-22 stsp log_debug("%s: shutting down", gotd_auth.title);