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 0ccf3acb 2022-11-16 stsp #include <sys/queue.h>
20 0ccf3acb 2022-11-16 stsp #include <sys/uio.h>
22 0ccf3acb 2022-11-16 stsp #include <errno.h>
23 0ccf3acb 2022-11-16 stsp #include <event.h>
24 0ccf3acb 2022-11-16 stsp #include <limits.h>
25 0ccf3acb 2022-11-16 stsp #include <pwd.h>
26 0ccf3acb 2022-11-16 stsp #include <grp.h>
27 0ccf3acb 2022-11-16 stsp #include <sha1.h>
28 5e25db14 2022-12-29 stsp #include <signal.h>
29 0ccf3acb 2022-11-16 stsp #include <stdint.h>
30 0ccf3acb 2022-11-16 stsp #include <stdio.h>
31 0ccf3acb 2022-11-16 stsp #include <stdlib.h>
32 5e25db14 2022-12-29 stsp #include <string.h>
33 0ccf3acb 2022-11-16 stsp #include <imsg.h>
34 ddbe612c 2022-11-17 stsp #include <unistd.h>
36 0ccf3acb 2022-11-16 stsp #include "got_error.h"
37 5e25db14 2022-12-29 stsp #include "got_path.h"
39 0ccf3acb 2022-11-16 stsp #include "gotd.h"
40 ddbe612c 2022-11-17 stsp #include "log.h"
41 0ccf3acb 2022-11-16 stsp #include "auth.h"
43 5e25db14 2022-12-29 stsp static struct gotd_auth {
45 5e25db14 2022-12-29 stsp const char *title;
46 5e25db14 2022-12-29 stsp struct gotd_repo *repo;
47 5e25db14 2022-12-29 stsp } gotd_auth;
49 5e25db14 2022-12-29 stsp static void auth_shutdown(void);
52 5e25db14 2022-12-29 stsp auth_sighdlr(int sig, short event, void *arg)
55 5e25db14 2022-12-29 stsp * Normal signal handler rules don't apply because libevent
56 5e25db14 2022-12-29 stsp * decouples for us.
59 5e25db14 2022-12-29 stsp switch (sig) {
60 5e25db14 2022-12-29 stsp case SIGHUP:
62 5e25db14 2022-12-29 stsp case SIGUSR1:
64 5e25db14 2022-12-29 stsp case SIGTERM:
65 5e25db14 2022-12-29 stsp case SIGINT:
66 5e25db14 2022-12-29 stsp auth_shutdown();
67 5e25db14 2022-12-29 stsp /* NOTREACHED */
70 5e25db14 2022-12-29 stsp fatalx("unexpected signal");
75 0ccf3acb 2022-11-16 stsp parseuid(const char *s, uid_t *uid)
77 0ccf3acb 2022-11-16 stsp struct passwd *pw;
78 0ccf3acb 2022-11-16 stsp const char *errstr;
80 0ccf3acb 2022-11-16 stsp if ((pw = getpwnam(s)) != NULL) {
81 0ccf3acb 2022-11-16 stsp *uid = pw->pw_uid;
82 0ccf3acb 2022-11-16 stsp if (*uid == UID_MAX)
86 0ccf3acb 2022-11-16 stsp *uid = strtonum(s, 0, UID_MAX - 1, &errstr);
93 0ccf3acb 2022-11-16 stsp uidcheck(const char *s, uid_t desired)
97 0ccf3acb 2022-11-16 stsp if (parseuid(s, &uid) != 0)
99 0ccf3acb 2022-11-16 stsp if (uid != desired)
105 0ccf3acb 2022-11-16 stsp parsegid(const char *s, gid_t *gid)
107 0ccf3acb 2022-11-16 stsp struct group *gr;
108 0ccf3acb 2022-11-16 stsp const char *errstr;
110 0ccf3acb 2022-11-16 stsp if ((gr = getgrnam(s)) != NULL) {
111 0ccf3acb 2022-11-16 stsp *gid = gr->gr_gid;
112 0ccf3acb 2022-11-16 stsp if (*gid == GID_MAX)
116 0ccf3acb 2022-11-16 stsp *gid = strtonum(s, 0, GID_MAX - 1, &errstr);
117 0ccf3acb 2022-11-16 stsp if (errstr)
123 0ccf3acb 2022-11-16 stsp match_identifier(const char *identifier, gid_t *groups, int ngroups,
124 0ccf3acb 2022-11-16 stsp uid_t euid, gid_t egid)
128 0ccf3acb 2022-11-16 stsp if (identifier[0] == ':') {
129 0ccf3acb 2022-11-16 stsp gid_t rgid;
130 0ccf3acb 2022-11-16 stsp if (parsegid(identifier + 1, &rgid) == -1)
132 ddbe612c 2022-11-17 stsp if (rgid == egid)
134 0ccf3acb 2022-11-16 stsp for (i = 0; i < ngroups; i++) {
135 ddbe612c 2022-11-17 stsp if (rgid == groups[i])
138 0ccf3acb 2022-11-16 stsp if (i == ngroups)
140 0ccf3acb 2022-11-16 stsp } else if (uidcheck(identifier, euid) != 0)
146 5e25db14 2022-12-29 stsp static const struct got_error *
147 5e25db14 2022-12-29 stsp auth_check(struct gotd_access_rule_list *rules, const char *repo_name,
148 ddbe612c 2022-11-17 stsp uid_t euid, gid_t egid, int required_auth)
150 0ccf3acb 2022-11-16 stsp struct gotd_access_rule *rule;
151 0ccf3acb 2022-11-16 stsp enum gotd_access access = GOTD_ACCESS_DENIED;
152 ddbe612c 2022-11-17 stsp struct passwd *pw;
153 ddbe612c 2022-11-17 stsp gid_t groups[NGROUPS_MAX];
154 ddbe612c 2022-11-17 stsp int ngroups = NGROUPS_MAX;
156 ddbe612c 2022-11-17 stsp pw = getpwuid(euid);
157 e18d071f 2022-11-20 stsp if (pw == NULL) {
159 e18d071f 2022-11-20 stsp return got_error_from_errno("getpwuid");
161 e18d071f 2022-11-20 stsp return got_error_set_errno(EACCES, repo_name);
164 ddbe612c 2022-11-17 stsp if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups) == -1)
165 ddbe612c 2022-11-17 stsp log_warnx("group membership list truncated");
167 0ccf3acb 2022-11-16 stsp STAILQ_FOREACH(rule, rules, entry) {
168 0ccf3acb 2022-11-16 stsp if (!match_identifier(rule->identifier, groups, ngroups,
169 0ccf3acb 2022-11-16 stsp euid, egid))
172 0ccf3acb 2022-11-16 stsp access = rule->access;
173 0ccf3acb 2022-11-16 stsp if (rule->access == GOTD_ACCESS_PERMITTED &&
174 0ccf3acb 2022-11-16 stsp (rule->authorization & required_auth) != required_auth)
175 0ccf3acb 2022-11-16 stsp access = GOTD_ACCESS_DENIED;
178 0ccf3acb 2022-11-16 stsp if (access == GOTD_ACCESS_DENIED)
179 0ccf3acb 2022-11-16 stsp return got_error_set_errno(EACCES, repo_name);
181 0ccf3acb 2022-11-16 stsp if (access == GOTD_ACCESS_PERMITTED)
182 0ccf3acb 2022-11-16 stsp return NULL;
184 0ccf3acb 2022-11-16 stsp /* should not happen, this would be a bug */
185 0ccf3acb 2022-11-16 stsp return got_error_msg(GOT_ERR_NOT_IMPL, "bad access rule");
188 5e25db14 2022-12-29 stsp static const struct got_error *
189 5e25db14 2022-12-29 stsp recv_authreq(struct imsg *imsg, struct gotd_imsgev *iev)
191 5e25db14 2022-12-29 stsp const struct got_error *err;
192 5e25db14 2022-12-29 stsp struct imsgbuf *ibuf = &iev->ibuf;
193 5e25db14 2022-12-29 stsp struct gotd_imsg_auth iauth;
194 5e25db14 2022-12-29 stsp size_t datalen;
196 5e25db14 2022-12-29 stsp log_debug("authentication request received");
198 5e25db14 2022-12-29 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
199 5e25db14 2022-12-29 stsp if (datalen != sizeof(iauth))
200 5e25db14 2022-12-29 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
202 5e25db14 2022-12-29 stsp memcpy(&iauth, imsg->data, datalen);
204 5e25db14 2022-12-29 stsp err = auth_check(&gotd_auth.repo->rules, gotd_auth.repo->name,
205 5e25db14 2022-12-29 stsp iauth.euid, iauth.egid, iauth.required_auth);
207 5e25db14 2022-12-29 stsp gotd_imsg_send_error(ibuf, PROC_AUTH, iauth.client_id, err);
208 5e25db14 2022-12-29 stsp return err;
211 5e25db14 2022-12-29 stsp if (gotd_imsg_compose_event(iev, GOTD_IMSG_ACCESS_GRANTED,
212 5e25db14 2022-12-29 stsp PROC_AUTH, -1, NULL, 0) == -1)
213 5e25db14 2022-12-29 stsp return got_error_from_errno("imsg compose ACCESS_GRANTED");
215 5e25db14 2022-12-29 stsp return NULL;
218 5e25db14 2022-12-29 stsp static void
219 5e25db14 2022-12-29 stsp auth_dispatch(int fd, short event, void *arg)
221 5e25db14 2022-12-29 stsp const struct got_error *err = NULL;
222 5e25db14 2022-12-29 stsp struct gotd_imsgev *iev = arg;
223 5e25db14 2022-12-29 stsp struct imsgbuf *ibuf = &iev->ibuf;
224 5e25db14 2022-12-29 stsp struct imsg imsg;
226 5e25db14 2022-12-29 stsp int shut = 0;
228 5e25db14 2022-12-29 stsp if (event & EV_READ) {
229 5e25db14 2022-12-29 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
230 5e25db14 2022-12-29 stsp fatal("imsg_read error");
231 5e25db14 2022-12-29 stsp if (n == 0) /* Connection closed. */
235 5e25db14 2022-12-29 stsp if (event & EV_WRITE) {
236 5e25db14 2022-12-29 stsp n = msgbuf_write(&ibuf->w);
237 5e25db14 2022-12-29 stsp if (n == -1 && errno != EAGAIN)
238 5e25db14 2022-12-29 stsp fatal("msgbuf_write");
239 5e25db14 2022-12-29 stsp if (n == 0) /* Connection closed. */
244 5e25db14 2022-12-29 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
245 5e25db14 2022-12-29 stsp fatal("%s: imsg_get", __func__);
246 5e25db14 2022-12-29 stsp if (n == 0) /* No more messages. */
249 5e25db14 2022-12-29 stsp switch (imsg.hdr.type) {
250 5e25db14 2022-12-29 stsp case GOTD_IMSG_AUTHENTICATE:
251 5e25db14 2022-12-29 stsp err = recv_authreq(&imsg, iev);
253 5e25db14 2022-12-29 stsp log_warnx("%s: %s", gotd_auth.title, err->msg);
256 5e25db14 2022-12-29 stsp log_debug("%s: unexpected imsg %d", gotd_auth.title,
257 5e25db14 2022-12-29 stsp imsg.hdr.type);
261 5e25db14 2022-12-29 stsp imsg_free(&imsg);
264 5e25db14 2022-12-29 stsp if (!shut) {
265 5e25db14 2022-12-29 stsp gotd_imsg_event_add(iev);
267 5e25db14 2022-12-29 stsp /* This pipe is dead. Remove its event handler */
268 5e25db14 2022-12-29 stsp event_del(&iev->ev);
269 5e25db14 2022-12-29 stsp event_loopexit(NULL);
274 5e25db14 2022-12-29 stsp auth_main(const char *title, struct gotd_repolist *repos,
275 5e25db14 2022-12-29 stsp const char *repo_path)
277 5e25db14 2022-12-29 stsp struct gotd_repo *repo = NULL;
278 5e25db14 2022-12-29 stsp struct gotd_imsgev iev;
279 5e25db14 2022-12-29 stsp struct event evsigint, evsigterm, evsighup, evsigusr1;
281 5e25db14 2022-12-29 stsp gotd_auth.title = title;
282 5e25db14 2022-12-29 stsp gotd_auth.pid = getpid();
283 5e25db14 2022-12-29 stsp TAILQ_FOREACH(repo, repos, entry) {
284 5e25db14 2022-12-29 stsp if (got_path_cmp(repo->path, repo_path,
285 5e25db14 2022-12-29 stsp strlen(repo->path), strlen(repo_path)) == 0)
288 5e25db14 2022-12-29 stsp if (repo == NULL)
289 5e25db14 2022-12-29 stsp fatalx("repository %s not found in config", repo_path);
290 5e25db14 2022-12-29 stsp gotd_auth.repo = repo;
292 5e25db14 2022-12-29 stsp signal_set(&evsigint, SIGINT, auth_sighdlr, NULL);
293 5e25db14 2022-12-29 stsp signal_set(&evsigterm, SIGTERM, auth_sighdlr, NULL);
294 5e25db14 2022-12-29 stsp signal_set(&evsighup, SIGHUP, auth_sighdlr, NULL);
295 5e25db14 2022-12-29 stsp signal_set(&evsigusr1, SIGUSR1, auth_sighdlr, NULL);
296 5e25db14 2022-12-29 stsp signal(SIGPIPE, SIG_IGN);
298 5e25db14 2022-12-29 stsp signal_add(&evsigint, NULL);
299 5e25db14 2022-12-29 stsp signal_add(&evsigterm, NULL);
300 5e25db14 2022-12-29 stsp signal_add(&evsighup, NULL);
301 5e25db14 2022-12-29 stsp signal_add(&evsigusr1, NULL);
303 5e25db14 2022-12-29 stsp imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
304 5e25db14 2022-12-29 stsp iev.handler = auth_dispatch;
305 5e25db14 2022-12-29 stsp iev.events = EV_READ;
306 5e25db14 2022-12-29 stsp iev.handler_arg = NULL;
307 5e25db14 2022-12-29 stsp event_set(&iev.ev, iev.ibuf.fd, EV_READ, auth_dispatch, &iev);
308 5e25db14 2022-12-29 stsp if (event_add(&iev.ev, NULL) == -1)
309 5e25db14 2022-12-29 stsp fatalx("event add");
311 5e25db14 2022-12-29 stsp event_dispatch();
313 5e25db14 2022-12-29 stsp auth_shutdown();
316 5e25db14 2022-12-29 stsp static void
317 5e25db14 2022-12-29 stsp auth_shutdown(void)
319 5e25db14 2022-12-29 stsp log_debug("%s: shutting down", gotd_auth.title);