Blame


1 729a7e24 2022-11-17 thomas /*
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>
4 729a7e24 2022-11-17 thomas *
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.
8 729a7e24 2022-11-17 thomas *
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.
16 729a7e24 2022-11-17 thomas */
17 729a7e24 2022-11-17 thomas
18 729a7e24 2022-11-17 thomas #include <sys/types.h>
19 729a7e24 2022-11-17 thomas #include <sys/queue.h>
20 729a7e24 2022-11-17 thomas #include <sys/uio.h>
21 729a7e24 2022-11-17 thomas
22 729a7e24 2022-11-17 thomas #include <errno.h>
23 729a7e24 2022-11-17 thomas #include <event.h>
24 729a7e24 2022-11-17 thomas #include <limits.h>
25 729a7e24 2022-11-17 thomas #include <pwd.h>
26 729a7e24 2022-11-17 thomas #include <grp.h>
27 729a7e24 2022-11-17 thomas #include <sha1.h>
28 729a7e24 2022-11-17 thomas #include <stdint.h>
29 729a7e24 2022-11-17 thomas #include <stdio.h>
30 729a7e24 2022-11-17 thomas #include <stdlib.h>
31 729a7e24 2022-11-17 thomas #include <imsg.h>
32 729a7e24 2022-11-17 thomas
33 729a7e24 2022-11-17 thomas #include "got_error.h"
34 729a7e24 2022-11-17 thomas
35 729a7e24 2022-11-17 thomas #include "gotd.h"
36 729a7e24 2022-11-17 thomas #include "auth.h"
37 729a7e24 2022-11-17 thomas
38 729a7e24 2022-11-17 thomas static int
39 729a7e24 2022-11-17 thomas parseuid(const char *s, uid_t *uid)
40 729a7e24 2022-11-17 thomas {
41 729a7e24 2022-11-17 thomas struct passwd *pw;
42 729a7e24 2022-11-17 thomas const char *errstr;
43 729a7e24 2022-11-17 thomas
44 729a7e24 2022-11-17 thomas if ((pw = getpwnam(s)) != NULL) {
45 729a7e24 2022-11-17 thomas *uid = pw->pw_uid;
46 729a7e24 2022-11-17 thomas if (*uid == UID_MAX)
47 729a7e24 2022-11-17 thomas return -1;
48 729a7e24 2022-11-17 thomas return 0;
49 729a7e24 2022-11-17 thomas }
50 729a7e24 2022-11-17 thomas *uid = strtonum(s, 0, UID_MAX - 1, &errstr);
51 729a7e24 2022-11-17 thomas if (errstr)
52 729a7e24 2022-11-17 thomas return -1;
53 729a7e24 2022-11-17 thomas return 0;
54 729a7e24 2022-11-17 thomas }
55 729a7e24 2022-11-17 thomas
56 729a7e24 2022-11-17 thomas static int
57 729a7e24 2022-11-17 thomas uidcheck(const char *s, uid_t desired)
58 729a7e24 2022-11-17 thomas {
59 729a7e24 2022-11-17 thomas uid_t uid;
60 729a7e24 2022-11-17 thomas
61 729a7e24 2022-11-17 thomas if (parseuid(s, &uid) != 0)
62 729a7e24 2022-11-17 thomas return -1;
63 729a7e24 2022-11-17 thomas if (uid != desired)
64 729a7e24 2022-11-17 thomas return -1;
65 729a7e24 2022-11-17 thomas return 0;
66 729a7e24 2022-11-17 thomas }
67 729a7e24 2022-11-17 thomas
68 729a7e24 2022-11-17 thomas static int
69 729a7e24 2022-11-17 thomas parsegid(const char *s, gid_t *gid)
70 729a7e24 2022-11-17 thomas {
71 729a7e24 2022-11-17 thomas struct group *gr;
72 729a7e24 2022-11-17 thomas const char *errstr;
73 729a7e24 2022-11-17 thomas
74 729a7e24 2022-11-17 thomas if ((gr = getgrnam(s)) != NULL) {
75 729a7e24 2022-11-17 thomas *gid = gr->gr_gid;
76 729a7e24 2022-11-17 thomas if (*gid == GID_MAX)
77 729a7e24 2022-11-17 thomas return -1;
78 729a7e24 2022-11-17 thomas return 0;
79 729a7e24 2022-11-17 thomas }
80 729a7e24 2022-11-17 thomas *gid = strtonum(s, 0, GID_MAX - 1, &errstr);
81 729a7e24 2022-11-17 thomas if (errstr)
82 729a7e24 2022-11-17 thomas return -1;
83 729a7e24 2022-11-17 thomas return 0;
84 729a7e24 2022-11-17 thomas }
85 729a7e24 2022-11-17 thomas
86 729a7e24 2022-11-17 thomas static int
87 729a7e24 2022-11-17 thomas match_identifier(const char *identifier, gid_t *groups, int ngroups,
88 729a7e24 2022-11-17 thomas uid_t euid, gid_t egid)
89 729a7e24 2022-11-17 thomas {
90 729a7e24 2022-11-17 thomas int i;
91 729a7e24 2022-11-17 thomas
92 729a7e24 2022-11-17 thomas if (identifier[0] == ':') {
93 729a7e24 2022-11-17 thomas gid_t rgid;
94 729a7e24 2022-11-17 thomas if (parsegid(identifier + 1, &rgid) == -1)
95 729a7e24 2022-11-17 thomas return 0;
96 729a7e24 2022-11-17 thomas for (i = 0; i < ngroups; i++) {
97 729a7e24 2022-11-17 thomas if (rgid == groups[i] && egid == rgid)
98 729a7e24 2022-11-17 thomas break;
99 729a7e24 2022-11-17 thomas }
100 729a7e24 2022-11-17 thomas if (i == ngroups)
101 729a7e24 2022-11-17 thomas return 0;
102 729a7e24 2022-11-17 thomas } else if (uidcheck(identifier, euid) != 0)
103 729a7e24 2022-11-17 thomas return 0;
104 729a7e24 2022-11-17 thomas
105 729a7e24 2022-11-17 thomas return 1;
106 729a7e24 2022-11-17 thomas }
107 729a7e24 2022-11-17 thomas
108 729a7e24 2022-11-17 thomas const struct got_error *
109 729a7e24 2022-11-17 thomas gotd_auth_check(struct gotd_access_rule_list *rules, const char *repo_name,
110 729a7e24 2022-11-17 thomas gid_t *groups, int ngroups, uid_t euid, gid_t egid,
111 729a7e24 2022-11-17 thomas int required_auth)
112 729a7e24 2022-11-17 thomas {
113 729a7e24 2022-11-17 thomas struct gotd_access_rule *rule;
114 729a7e24 2022-11-17 thomas enum gotd_access access = GOTD_ACCESS_DENIED;
115 729a7e24 2022-11-17 thomas
116 729a7e24 2022-11-17 thomas STAILQ_FOREACH(rule, rules, entry) {
117 729a7e24 2022-11-17 thomas if (!match_identifier(rule->identifier, groups, ngroups,
118 729a7e24 2022-11-17 thomas euid, egid))
119 729a7e24 2022-11-17 thomas continue;
120 729a7e24 2022-11-17 thomas
121 729a7e24 2022-11-17 thomas access = rule->access;
122 729a7e24 2022-11-17 thomas if (rule->access == GOTD_ACCESS_PERMITTED &&
123 729a7e24 2022-11-17 thomas (rule->authorization & required_auth) != required_auth)
124 729a7e24 2022-11-17 thomas access = GOTD_ACCESS_DENIED;
125 729a7e24 2022-11-17 thomas }
126 729a7e24 2022-11-17 thomas
127 729a7e24 2022-11-17 thomas if (access == GOTD_ACCESS_DENIED)
128 729a7e24 2022-11-17 thomas return got_error_set_errno(EACCES, repo_name);
129 729a7e24 2022-11-17 thomas
130 729a7e24 2022-11-17 thomas if (access == GOTD_ACCESS_PERMITTED)
131 729a7e24 2022-11-17 thomas return NULL;
132 729a7e24 2022-11-17 thomas
133 729a7e24 2022-11-17 thomas /* should not happen, this would be a bug */
134 729a7e24 2022-11-17 thomas return got_error_msg(GOT_ERR_NOT_IMPL, "bad access rule");
135 729a7e24 2022-11-17 thomas }