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