grutil.c revision 1.2.4.1 1 1.2.4.1 yamt /* $NetBSD: grutil.c,v 1.2.4.1 2014/05/22 11:42:46 yamt Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.1 christos * by Brian Ginsbach.
9 1.1 christos *
10 1.1 christos * Redistribution and use in source and binary forms, with or without
11 1.1 christos * modification, are permitted provided that the following conditions
12 1.1 christos * are met:
13 1.1 christos * 1. Redistributions of source code must retain the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer.
15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 christos * notice, this list of conditions and the following disclaimer in the
17 1.1 christos * documentation and/or other materials provided with the distribution.
18 1.1 christos *
19 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 christos * POSSIBILITY OF SUCH DAMAGE.
30 1.1 christos */
31 1.1 christos #include <sys/cdefs.h>
32 1.2.4.1 yamt __RCSID("$NetBSD: grutil.c,v 1.2.4.1 2014/05/22 11:42:46 yamt Exp $");
33 1.1 christos
34 1.1 christos #include <sys/param.h>
35 1.1 christos #include <err.h>
36 1.1 christos #include <errno.h>
37 1.1 christos #include <grp.h>
38 1.1 christos #include <pwd.h>
39 1.1 christos #include <stdio.h>
40 1.1 christos #include <stdlib.h>
41 1.1 christos #include <string.h>
42 1.1 christos #include <unistd.h>
43 1.1 christos #include <util.h>
44 1.1 christos
45 1.1 christos #ifdef LOGIN_CAP
46 1.1 christos #include <login_cap.h>
47 1.1 christos #endif
48 1.1 christos
49 1.1 christos #include "grutil.h"
50 1.1 christos
51 1.1 christos typedef enum {
52 1.1 christos ADDGRP_NOERROR = 0, /* must be zero */
53 1.1 christos ADDGRP_EMALLOC = 1,
54 1.1 christos ADDGRP_EGETGROUPS = 2,
55 1.1 christos ADDGRP_ESETGROUPS = 3
56 1.1 christos } addgrp_ret_t;
57 1.1 christos
58 1.1 christos static void
59 1.1 christos free_groups(void *groups)
60 1.1 christos {
61 1.1 christos int oerrno;
62 1.1 christos
63 1.1 christos oerrno = errno;
64 1.1 christos free(groups);
65 1.1 christos errno = oerrno;
66 1.1 christos }
67 1.1 christos
68 1.1 christos static addgrp_ret_t
69 1.1 christos alloc_groups(int *ngroups, gid_t **groups, int *ngroupsmax)
70 1.1 christos {
71 1.1 christos *ngroupsmax = (int)sysconf(_SC_NGROUPS_MAX);
72 1.1 christos if (*ngroupsmax < 0)
73 1.1 christos *ngroupsmax = NGROUPS_MAX;
74 1.1 christos
75 1.1 christos *groups = malloc(*ngroupsmax * sizeof(**groups));
76 1.1 christos if (*groups == NULL)
77 1.1 christos return ADDGRP_EMALLOC;
78 1.1 christos
79 1.1 christos *ngroups = getgroups(*ngroupsmax, *groups);
80 1.1 christos if (*ngroups == -1) {
81 1.1 christos free_groups(*groups);
82 1.1 christos return ADDGRP_ESETGROUPS;
83 1.1 christos }
84 1.1 christos return ADDGRP_NOERROR;
85 1.1 christos }
86 1.1 christos
87 1.1 christos static addgrp_ret_t
88 1.1 christos addgid(gid_t *groups, int ngroups, int ngroupsmax, gid_t gid, int makespace)
89 1.1 christos {
90 1.1 christos int i;
91 1.1 christos
92 1.1 christos /* search for gid in supplemental group list */
93 1.1 christos for (i = 0; i < ngroups && groups[i] != gid; i++)
94 1.1 christos continue;
95 1.1 christos
96 1.1 christos /* add the gid to the supplemental group list */
97 1.1 christos if (i == ngroups) {
98 1.1 christos if (ngroups < ngroupsmax)
99 1.1 christos groups[ngroups++] = gid;
100 1.1 christos else { /*
101 1.1 christos * setgroups(2) will fail with errno = EINVAL
102 1.1 christos * if ngroups > nmaxgroups. If makespace is
103 1.1 christos * set, replace the last group with the new
104 1.1 christos * one. Otherwise, fail the way setgroups(2)
105 1.1 christos * would if we passed the larger groups array.
106 1.1 christos */
107 1.1 christos if (makespace) {
108 1.1 christos /*
109 1.1 christos * Find a slot that doesn't contain
110 1.1 christos * the primary group.
111 1.1 christos */
112 1.1 christos struct passwd *pwd;
113 1.1 christos gid_t pgid;
114 1.1 christos pwd = getpwuid(getuid());
115 1.1 christos if (pwd == NULL)
116 1.1 christos goto error;
117 1.1 christos pgid = pwd->pw_gid;
118 1.1 christos for (i = ngroupsmax - 1; i >= 0; i--)
119 1.1 christos if (groups[i] != pgid)
120 1.1 christos break;
121 1.1 christos if (i < 0)
122 1.1 christos goto error;
123 1.1 christos groups[i] = gid;
124 1.1 christos }
125 1.1 christos else {
126 1.1 christos error:
127 1.1 christos errno = EINVAL;
128 1.1 christos return ADDGRP_ESETGROUPS;
129 1.1 christos }
130 1.1 christos }
131 1.1 christos if (setgroups(ngroups, groups) < 0)
132 1.1 christos return ADDGRP_ESETGROUPS;
133 1.1 christos }
134 1.1 christos return ADDGRP_NOERROR;
135 1.1 christos }
136 1.1 christos
137 1.1 christos static addgrp_ret_t
138 1.1 christos addgrp(gid_t newgid, int makespace)
139 1.1 christos {
140 1.2.4.1 yamt int ngroups, ngroupsmax;
141 1.2.4.1 yamt addgrp_ret_t rval;
142 1.1 christos gid_t *groups;
143 1.1 christos gid_t oldgid;
144 1.1 christos
145 1.1 christos oldgid = getgid();
146 1.1 christos if (oldgid == newgid) /* nothing to do */
147 1.1 christos return ADDGRP_NOERROR;
148 1.1 christos
149 1.1 christos rval = alloc_groups(&ngroups, &groups, &ngroupsmax);
150 1.1 christos if (rval != 0)
151 1.1 christos return rval;
152 1.1 christos
153 1.1 christos /*
154 1.1 christos * BSD based systems normally have the egid in the supplemental
155 1.1 christos * group list.
156 1.1 christos */
157 1.1 christos #if (defined(BSD) && BSD >= 199306)
158 1.1 christos /*
159 1.1 christos * According to POSIX/XPG6:
160 1.1 christos * On system where the egid is normally in the supplemental group list
161 1.1 christos * (or whenever the old egid actually is in the supplemental group
162 1.1 christos * list):
163 1.1 christos * o If the new egid is in the supplemental group list,
164 1.1 christos * just change the egid.
165 1.1 christos * o If the new egid is not in the supplemental group list,
166 1.1 christos * add the new egid to the list if there is room.
167 1.1 christos */
168 1.1 christos
169 1.1 christos rval = addgid(groups, ngroups, ngroupsmax, newgid, makespace);
170 1.1 christos #else
171 1.1 christos /*
172 1.1 christos * According to POSIX/XPG6:
173 1.1 christos * On systems where the egid is not normally in the supplemental group
174 1.1 christos * list (or whenever the old egid is not in the supplemental group
175 1.1 christos * list):
176 1.1 christos * o If the new egid is in the supplemental group list, delete
177 1.1 christos * it from the list.
178 1.1 christos * o If the old egid is not in the supplemental group list,
179 1.1 christos * add the old egid to the list if there is room.
180 1.1 christos */
181 1.1 christos {
182 1.1 christos int i;
183 1.1 christos
184 1.1 christos /* search for new egid in supplemental group list */
185 1.1 christos for (i = 0; i < ngroups && groups[i] != newgid; i++)
186 1.1 christos continue;
187 1.1 christos
188 1.1 christos /* remove new egid from supplemental group list */
189 1.1 christos if (i != ngroups)
190 1.1 christos for (--ngroups; i < ngroups; i++)
191 1.1 christos groups[i] = groups[i + 1];
192 1.1 christos
193 1.1 christos rval = addgid(groups, ngroups, ngroupsmax, oldgid, makespace);
194 1.1 christos }
195 1.1 christos #endif
196 1.1 christos free_groups(groups);
197 1.1 christos return rval;
198 1.1 christos }
199 1.1 christos
200 1.1 christos /*
201 1.1 christos * If newgrp fails, it returns (gid_t)-1 and the errno variable is
202 1.1 christos * set to:
203 1.1 christos * [EINVAL] Unknown group.
204 1.1 christos * [EPERM] Bad password.
205 1.1 christos */
206 1.1 christos static gid_t
207 1.1 christos newgrp(const char *gname, struct passwd *pwd, uid_t ruid, const char *prompt)
208 1.1 christos {
209 1.1 christos struct group *grp;
210 1.1 christos char **ap;
211 1.1 christos char *p;
212 1.1 christos gid_t *groups;
213 1.1 christos int ngroups, ngroupsmax;
214 1.1 christos
215 1.1 christos if (gname == NULL)
216 1.1 christos return pwd->pw_gid;
217 1.1 christos
218 1.1 christos grp = getgrnam(gname);
219 1.1 christos
220 1.1 christos #ifdef GRUTIL_ACCEPT_GROUP_NUMBERS
221 1.1 christos if (grp == NULL) {
222 1.1 christos gid_t gid;
223 1.1 christos if (*gname != '-') {
224 1.1 christos gid = (gid_t)strtol(gname, &p, 10);
225 1.1 christos if (*p == '\0')
226 1.1 christos grp = getgrgid(gid);
227 1.1 christos }
228 1.1 christos }
229 1.1 christos #endif
230 1.1 christos if (grp == NULL) {
231 1.1 christos errno = EINVAL;
232 1.1 christos return (gid_t)-1;
233 1.1 christos }
234 1.1 christos
235 1.1 christos if (ruid == 0 || pwd->pw_gid == grp->gr_gid)
236 1.1 christos return grp->gr_gid;
237 1.1 christos
238 1.1 christos if (alloc_groups(&ngroups, &groups, &ngroupsmax) == 0) {
239 1.1 christos int i;
240 1.1 christos for (i = 0; i < ngroups; i++)
241 1.1 christos if (groups[i] == grp->gr_gid) {
242 1.1 christos free_groups(groups);
243 1.1 christos return grp->gr_gid;
244 1.1 christos }
245 1.1 christos free_groups(groups);
246 1.1 christos }
247 1.1 christos
248 1.1 christos /*
249 1.1 christos * Check the group membership list in case the groups[] array
250 1.1 christos * was maxed out or the user has been added to it since login.
251 1.1 christos */
252 1.1 christos for (ap = grp->gr_mem; *ap != NULL; ap++)
253 1.1 christos if (strcmp(*ap, pwd->pw_name) == 0)
254 1.1 christos return grp->gr_gid;
255 1.1 christos
256 1.1 christos if (*grp->gr_passwd != '\0') {
257 1.1 christos p = getpass(prompt);
258 1.1 christos if (strcmp(grp->gr_passwd, crypt(p, grp->gr_passwd)) == 0) {
259 1.1 christos (void)memset(p, '\0', _PASSWORD_LEN);
260 1.1 christos return grp->gr_gid;
261 1.1 christos }
262 1.1 christos (void)memset(p, '\0', _PASSWORD_LEN);
263 1.1 christos }
264 1.1 christos
265 1.1 christos errno = EPERM;
266 1.1 christos return (gid_t)-1;
267 1.1 christos }
268 1.1 christos
269 1.1 christos #ifdef GRUTIL_SETGROUPS_MAKESPACE
270 1.1 christos # define ADDGRP_MAKESPACE 1
271 1.1 christos #else
272 1.1 christos # define ADDGRP_MAKESPACE 0
273 1.1 christos #endif
274 1.1 christos
275 1.1 christos #ifdef GRUTIL_ALLOW_GROUP_ERRORS
276 1.1 christos # define maybe_exit(e)
277 1.1 christos #else
278 1.1 christos # define maybe_exit(e) exit(e);
279 1.1 christos #endif
280 1.1 christos
281 1.1 christos void
282 1.1 christos addgroup(
283 1.1 christos #ifdef LOGIN_CAP
284 1.1 christos login_cap_t *lc,
285 1.1 christos #endif
286 1.1 christos const char *gname, struct passwd *pwd, uid_t ruid, const char *prompt)
287 1.1 christos {
288 1.1 christos pwd->pw_gid = newgrp(gname, pwd, ruid, prompt);
289 1.1 christos if (pwd->pw_gid == (gid_t)-1) {
290 1.1 christos switch (errno) {
291 1.1 christos case EINVAL:
292 1.1 christos warnx("Unknown group `%s'", gname);
293 1.1 christos maybe_exit(EXIT_FAILURE);
294 1.1 christos break;
295 1.1 christos case EPERM: /* password failure */
296 1.1 christos warnx("Sorry");
297 1.1 christos maybe_exit(EXIT_FAILURE);
298 1.1 christos break;
299 1.1 christos default: /* XXX - should never happen */
300 1.1 christos err(EXIT_FAILURE, "unknown error");
301 1.1 christos break;
302 1.1 christos }
303 1.1 christos pwd->pw_gid = getgid();
304 1.1 christos }
305 1.1 christos
306 1.1 christos switch (addgrp(pwd->pw_gid, ADDGRP_MAKESPACE)) {
307 1.1 christos case ADDGRP_NOERROR:
308 1.1 christos break;
309 1.1 christos case ADDGRP_EMALLOC:
310 1.1 christos err(EXIT_FAILURE, "malloc");
311 1.1 christos break;
312 1.1 christos case ADDGRP_EGETGROUPS:
313 1.1 christos err(EXIT_FAILURE, "getgroups");
314 1.1 christos break;
315 1.1 christos case ADDGRP_ESETGROUPS:
316 1.1 christos switch(errno) {
317 1.1 christos case EINVAL:
318 1.1 christos warnx("setgroups: ngroups > ngroupsmax");
319 1.1 christos maybe_exit(EXIT_FAILURE);
320 1.1 christos break;
321 1.1 christos case EPERM:
322 1.1 christos case EFAULT:
323 1.1 christos default:
324 1.1 christos warn("setgroups");
325 1.1 christos maybe_exit(EXIT_FAILURE);
326 1.1 christos break;
327 1.1 christos }
328 1.1 christos break;
329 1.1 christos }
330 1.1 christos
331 1.1 christos #ifdef LOGIN_CAP
332 1.1 christos if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETGID) == -1)
333 1.1 christos err(EXIT_FAILURE, "setting user context");
334 1.1 christos #else
335 1.1 christos if (setgid(pwd->pw_gid) == -1)
336 1.1 christos err(EXIT_FAILURE, "setgid");
337 1.1 christos #endif
338 1.1 christos }
339