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 ff260661 2022-11-17 thomas #include <grp.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 729a7e24 2022-11-17 thomas #include <sha1.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 729a7e24 2022-11-17 thomas #include <imsg.h>
33 ff260661 2022-11-17 thomas #include <unistd.h>
34 729a7e24 2022-11-17 thomas
35 729a7e24 2022-11-17 thomas #include "got_error.h"
36 729a7e24 2022-11-17 thomas
37 729a7e24 2022-11-17 thomas #include "gotd.h"
38 ff260661 2022-11-17 thomas #include "log.h"
39 729a7e24 2022-11-17 thomas #include "auth.h"
40 729a7e24 2022-11-17 thomas
41 729a7e24 2022-11-17 thomas static int
42 729a7e24 2022-11-17 thomas parseuid(const char *s, uid_t *uid)
43 729a7e24 2022-11-17 thomas {
44 729a7e24 2022-11-17 thomas struct passwd *pw;
45 729a7e24 2022-11-17 thomas const char *errstr;
46 729a7e24 2022-11-17 thomas
47 729a7e24 2022-11-17 thomas if ((pw = getpwnam(s)) != NULL) {
48 729a7e24 2022-11-17 thomas *uid = pw->pw_uid;
49 729a7e24 2022-11-17 thomas if (*uid == UID_MAX)
50 729a7e24 2022-11-17 thomas return -1;
51 729a7e24 2022-11-17 thomas return 0;
52 729a7e24 2022-11-17 thomas }
53 729a7e24 2022-11-17 thomas *uid = strtonum(s, 0, UID_MAX - 1, &errstr);
54 729a7e24 2022-11-17 thomas if (errstr)
55 729a7e24 2022-11-17 thomas return -1;
56 729a7e24 2022-11-17 thomas return 0;
57 729a7e24 2022-11-17 thomas }
58 729a7e24 2022-11-17 thomas
59 729a7e24 2022-11-17 thomas static int
60 729a7e24 2022-11-17 thomas uidcheck(const char *s, uid_t desired)
61 729a7e24 2022-11-17 thomas {
62 729a7e24 2022-11-17 thomas uid_t uid;
63 729a7e24 2022-11-17 thomas
64 729a7e24 2022-11-17 thomas if (parseuid(s, &uid) != 0)
65 729a7e24 2022-11-17 thomas return -1;
66 729a7e24 2022-11-17 thomas if (uid != desired)
67 729a7e24 2022-11-17 thomas return -1;
68 729a7e24 2022-11-17 thomas return 0;
69 729a7e24 2022-11-17 thomas }
70 729a7e24 2022-11-17 thomas
71 729a7e24 2022-11-17 thomas static int
72 729a7e24 2022-11-17 thomas parsegid(const char *s, gid_t *gid)
73 729a7e24 2022-11-17 thomas {
74 729a7e24 2022-11-17 thomas struct group *gr;
75 729a7e24 2022-11-17 thomas const char *errstr;
76 729a7e24 2022-11-17 thomas
77 729a7e24 2022-11-17 thomas if ((gr = getgrnam(s)) != NULL) {
78 729a7e24 2022-11-17 thomas *gid = gr->gr_gid;
79 729a7e24 2022-11-17 thomas if (*gid == GID_MAX)
80 729a7e24 2022-11-17 thomas return -1;
81 729a7e24 2022-11-17 thomas return 0;
82 729a7e24 2022-11-17 thomas }
83 729a7e24 2022-11-17 thomas *gid = strtonum(s, 0, GID_MAX - 1, &errstr);
84 729a7e24 2022-11-17 thomas if (errstr)
85 729a7e24 2022-11-17 thomas return -1;
86 729a7e24 2022-11-17 thomas return 0;
87 729a7e24 2022-11-17 thomas }
88 729a7e24 2022-11-17 thomas
89 729a7e24 2022-11-17 thomas static int
90 729a7e24 2022-11-17 thomas match_identifier(const char *identifier, gid_t *groups, int ngroups,
91 729a7e24 2022-11-17 thomas uid_t euid, gid_t egid)
92 729a7e24 2022-11-17 thomas {
93 729a7e24 2022-11-17 thomas int i;
94 729a7e24 2022-11-17 thomas
95 729a7e24 2022-11-17 thomas if (identifier[0] == ':') {
96 729a7e24 2022-11-17 thomas gid_t rgid;
97 729a7e24 2022-11-17 thomas if (parsegid(identifier + 1, &rgid) == -1)
98 729a7e24 2022-11-17 thomas return 0;
99 ff260661 2022-11-17 thomas if (rgid == egid)
100 ff260661 2022-11-17 thomas return 1;
101 729a7e24 2022-11-17 thomas for (i = 0; i < ngroups; i++) {
102 ff260661 2022-11-17 thomas if (rgid == groups[i])
103 729a7e24 2022-11-17 thomas break;
104 729a7e24 2022-11-17 thomas }
105 729a7e24 2022-11-17 thomas if (i == ngroups)
106 729a7e24 2022-11-17 thomas return 0;
107 729a7e24 2022-11-17 thomas } else if (uidcheck(identifier, euid) != 0)
108 729a7e24 2022-11-17 thomas return 0;
109 729a7e24 2022-11-17 thomas
110 729a7e24 2022-11-17 thomas return 1;
111 729a7e24 2022-11-17 thomas }
112 729a7e24 2022-11-17 thomas
113 729a7e24 2022-11-17 thomas const struct got_error *
114 729a7e24 2022-11-17 thomas gotd_auth_check(struct gotd_access_rule_list *rules, const char *repo_name,
115 ff260661 2022-11-17 thomas uid_t euid, gid_t egid, int required_auth)
116 729a7e24 2022-11-17 thomas {
117 729a7e24 2022-11-17 thomas struct gotd_access_rule *rule;
118 729a7e24 2022-11-17 thomas enum gotd_access access = GOTD_ACCESS_DENIED;
119 ff260661 2022-11-17 thomas struct passwd *pw;
120 ff260661 2022-11-17 thomas gid_t groups[NGROUPS_MAX];
121 ff260661 2022-11-17 thomas int ngroups = NGROUPS_MAX;
122 729a7e24 2022-11-17 thomas
123 ff260661 2022-11-17 thomas pw = getpwuid(euid);
124 ff260661 2022-11-17 thomas if (pw == NULL)
125 ff260661 2022-11-17 thomas return got_error_from_errno("getpwuid");
126 ff260661 2022-11-17 thomas
127 ff260661 2022-11-17 thomas if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups) == -1)
128 ff260661 2022-11-17 thomas log_warnx("group membership list truncated");
129 ff260661 2022-11-17 thomas
130 729a7e24 2022-11-17 thomas STAILQ_FOREACH(rule, rules, entry) {
131 729a7e24 2022-11-17 thomas if (!match_identifier(rule->identifier, groups, ngroups,
132 729a7e24 2022-11-17 thomas euid, egid))
133 729a7e24 2022-11-17 thomas continue;
134 729a7e24 2022-11-17 thomas
135 729a7e24 2022-11-17 thomas access = rule->access;
136 729a7e24 2022-11-17 thomas if (rule->access == GOTD_ACCESS_PERMITTED &&
137 729a7e24 2022-11-17 thomas (rule->authorization & required_auth) != required_auth)
138 729a7e24 2022-11-17 thomas access = GOTD_ACCESS_DENIED;
139 729a7e24 2022-11-17 thomas }
140 729a7e24 2022-11-17 thomas
141 729a7e24 2022-11-17 thomas if (access == GOTD_ACCESS_DENIED)
142 729a7e24 2022-11-17 thomas return got_error_set_errno(EACCES, repo_name);
143 729a7e24 2022-11-17 thomas
144 729a7e24 2022-11-17 thomas if (access == GOTD_ACCESS_PERMITTED)
145 729a7e24 2022-11-17 thomas return NULL;
146 729a7e24 2022-11-17 thomas
147 729a7e24 2022-11-17 thomas /* should not happen, this would be a bug */
148 729a7e24 2022-11-17 thomas return got_error_msg(GOT_ERR_NOT_IMPL, "bad access rule");
149 729a7e24 2022-11-17 thomas }