Blob


1 /*
2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 * Copyright (c) 2015 Ted Unangst <tedu@openbsd.org>
4 *
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.
8 *
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.
16 */
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <sys/queue.h>
21 #include <sys/uio.h>
23 #include <errno.h>
24 #include <event.h>
25 #include <limits.h>
26 #include <pwd.h>
27 #include <grp.h>
28 #include <sha1.h>
29 #include <sha2.h>
30 #include <signal.h>
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <imsg.h>
36 #include <unistd.h>
38 #include "got_error.h"
39 #include "got_path.h"
41 #include "gotd.h"
42 #include "log.h"
43 #include "auth.h"
45 static struct gotd_auth {
46 pid_t pid;
47 const char *title;
48 struct gotd_repo *repo;
49 } gotd_auth;
51 static void auth_shutdown(void);
53 static void
54 auth_sighdlr(int sig, short event, void *arg)
55 {
56 /*
57 * Normal signal handler rules don't apply because libevent
58 * decouples for us.
59 */
61 switch (sig) {
62 case SIGHUP:
63 break;
64 case SIGUSR1:
65 break;
66 case SIGTERM:
67 case SIGINT:
68 auth_shutdown();
69 /* NOTREACHED */
70 break;
71 default:
72 fatalx("unexpected signal");
73 }
74 }
76 int
77 gotd_auth_parseuid(const char *s, uid_t *uid)
78 {
79 struct passwd *pw;
80 const char *errstr;
82 if ((pw = getpwnam(s)) != NULL) {
83 *uid = pw->pw_uid;
84 if (*uid == UID_MAX)
85 return -1;
86 return 0;
87 }
88 *uid = strtonum(s, 0, UID_MAX - 1, &errstr);
89 if (errstr)
90 return -1;
91 return 0;
92 }
94 static int
95 uidcheck(const char *s, uid_t desired)
96 {
97 uid_t uid;
99 if (gotd_auth_parseuid(s, &uid) != 0)
100 return -1;
101 if (uid != desired)
102 return -1;
103 return 0;
106 static int
107 parsegid(const char *s, gid_t *gid)
109 struct group *gr;
110 const char *errstr;
112 if ((gr = getgrnam(s)) != NULL) {
113 *gid = gr->gr_gid;
114 if (*gid == GID_MAX)
115 return -1;
116 return 0;
118 *gid = strtonum(s, 0, GID_MAX - 1, &errstr);
119 if (errstr)
120 return -1;
121 return 0;
124 static int
125 match_identifier(const char *identifier, gid_t *groups, int ngroups,
126 uid_t euid, gid_t egid)
128 int i;
130 if (identifier[0] == ':') {
131 gid_t rgid;
132 if (parsegid(identifier + 1, &rgid) == -1)
133 return 0;
134 if (rgid == egid)
135 return 1;
136 for (i = 0; i < ngroups; i++) {
137 if (rgid == groups[i])
138 break;
140 if (i == ngroups)
141 return 0;
142 } else if (uidcheck(identifier, euid) != 0)
143 return 0;
145 return 1;
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;
154 struct passwd *pw;
155 gid_t groups[NGROUPS_MAX];
156 int ngroups = NGROUPS_MAX;
158 pw = getpwuid(euid);
159 if (pw == NULL) {
160 if (errno)
161 return got_error_from_errno("getpwuid");
162 else
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,
171 euid, egid))
172 continue;
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)
184 return NULL;
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;
196 size_t datalen;
197 uid_t euid;
198 gid_t egid;
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);
208 if (imsg->fd == -1)
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);
223 if (err) {
224 gotd_imsg_send_error(ibuf, PROC_AUTH, iauth.client_id, err);
225 return 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");
232 return NULL;
235 static void
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;
241 struct imsg imsg;
242 ssize_t n;
243 int shut = 0;
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. */
249 shut = 1;
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. */
257 shut = 1;
260 for (;;) {
261 if ((n = imsg_get(ibuf, &imsg)) == -1)
262 fatal("%s: imsg_get", __func__);
263 if (n == 0) /* No more messages. */
264 break;
266 switch (imsg.hdr.type) {
267 case GOTD_IMSG_AUTHENTICATE:
268 err = recv_authreq(&imsg, iev);
269 if (err)
270 log_warnx("%s: %s", gotd_auth.title, err->msg);
271 break;
272 default:
273 log_debug("%s: unexpected imsg %d", gotd_auth.title,
274 imsg.hdr.type);
275 break;
278 imsg_free(&imsg);
281 if (!shut) {
282 gotd_imsg_event_add(iev);
283 } else {
284 /* This pipe is dead. Remove its event handler */
285 event_del(&iev->ev);
286 event_loopexit(NULL);
290 void
291 auth_main(const char *title, struct gotd_repolist *repos,
292 const char *repo_path)
294 struct gotd_repo *repo = NULL;
295 struct gotd_imsgev iev;
296 struct event evsigint, evsigterm, evsighup, evsigusr1;
298 gotd_auth.title = title;
299 gotd_auth.pid = getpid();
300 TAILQ_FOREACH(repo, repos, entry) {
301 if (got_path_cmp(repo->path, repo_path,
302 strlen(repo->path), strlen(repo_path)) == 0)
303 break;
305 if (repo == NULL)
306 fatalx("repository %s not found in config", repo_path);
307 gotd_auth.repo = repo;
309 signal_set(&evsigint, SIGINT, auth_sighdlr, NULL);
310 signal_set(&evsigterm, SIGTERM, auth_sighdlr, NULL);
311 signal_set(&evsighup, SIGHUP, auth_sighdlr, NULL);
312 signal_set(&evsigusr1, SIGUSR1, auth_sighdlr, NULL);
313 signal(SIGPIPE, SIG_IGN);
315 signal_add(&evsigint, NULL);
316 signal_add(&evsigterm, NULL);
317 signal_add(&evsighup, NULL);
318 signal_add(&evsigusr1, NULL);
320 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
321 iev.handler = auth_dispatch;
322 iev.events = EV_READ;
323 iev.handler_arg = NULL;
324 event_set(&iev.ev, iev.ibuf.fd, EV_READ, auth_dispatch, &iev);
325 if (event_add(&iev.ev, NULL) == -1)
326 fatalx("event add");
328 event_dispatch();
330 auth_shutdown();
333 static void
334 auth_shutdown(void)
336 log_debug("%s: shutting down", gotd_auth.title);
337 exit(0);