]> git.cameronkatri.com Git - apple_cmds.git/blob - system_cmds/newgrp.tproj/newgrp.c
Merge branch 'apple'
[apple_cmds.git] / system_cmds / newgrp.tproj / newgrp.c
1 /*-
2 * Copyright (c) 2002 Tim J. Robbins.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 /*
28 * newgrp -- change to a new group
29 */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD: src/usr.bin/newgrp/newgrp.c,v 1.5 2009/12/13 03:14:06 delphij Exp $");
33
34 #include <sys/types.h>
35
36 #include <err.h>
37 #include <errno.h>
38 #include <grp.h>
39 #include <libgen.h>
40 #include <limits.h>
41 #ifndef __APPLE__
42 #include <login_cap.h>
43 #endif /* !__APPLE__ */
44 #ifdef __APPLE__
45 #include <membership.h>
46 #endif /* __APPLE__ */
47 #include <pwd.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52 #ifdef __APPLE__
53 #include <paths.h>
54 #endif /* __APPLE__ */
55
56 #include <libiosexec.h>
57
58 static void addgroup(const char *grpname);
59 static void doshell(void);
60 static int inarray(gid_t, const gid_t[], int);
61 static void loginshell(void);
62 static void restoregrps(void);
63 static void usage(void);
64
65 static struct passwd *pwd;
66 static uid_t euid;
67
68 extern char **environ;
69
70 /* Manipulate effective user ID. */
71 #define PRIV_START do { \
72 if (seteuid(euid) < 0) \
73 err(1, "seteuid"); \
74 } while (0)
75 #define PRIV_END do { \
76 if (seteuid(getuid()) < 0) \
77 err(1, "seteuid"); \
78 } while (0)
79
80 int
81 main(int argc, char *argv[])
82 {
83 int ch, login;
84
85 euid = geteuid();
86 if (seteuid(getuid()) < 0)
87 err(1, "seteuid");
88
89 if ((pwd = getpwuid(getuid())) == NULL)
90 errx(1, "unknown user");
91
92 login = 0;
93 while ((ch = getopt(argc, argv, "-l")) != -1) {
94 switch (ch) {
95 case '-': /* Obsolescent */
96 case 'l':
97 login = 1;
98 break;
99 default:
100 usage();
101 }
102 }
103 argc -= optind;
104 argv += optind;
105
106 switch (argc) {
107 case 0:
108 restoregrps();
109 break;
110 case 1:
111 addgroup(*argv);
112 break;
113 default:
114 usage();
115 }
116
117 if (seteuid(euid) < 0)
118 err(1, "seteuid");
119 if (setuid(getuid()) < 0)
120 err(1, "setuid");
121
122 if (login)
123 loginshell();
124 else
125 doshell();
126
127 /*NOTREACHED*/
128 exit(1);
129 }
130
131 static void
132 usage(void)
133 {
134
135 fprintf(stderr, "usage: newgrp [-l] [group]\n");
136 exit(1);
137 }
138
139 static void
140 restoregrps(void)
141 {
142 int initres, setres;
143
144 PRIV_START;
145 initres = initgroups(pwd->pw_name, pwd->pw_gid);
146 setres = setgid(pwd->pw_gid);
147 PRIV_END;
148
149 if (initres < 0)
150 warn("initgroups");
151 if (setres < 0)
152 warn("setgid");
153 }
154
155 static void
156 addgroup(const char *grpname)
157 {
158 gid_t *grps;
159 long lgid, ngrps_max;
160 int dbmember, i, ngrps;
161 gid_t egid;
162 struct group *grp;
163 char *ep, *pass;
164 #ifndef __APPLE__
165 char **p;
166 #endif
167 char *grp_passwd;
168 #ifdef __APPLE__
169 uuid_t user_uuid;
170 uuid_t group_uuid;
171 int status;
172 #endif
173
174 egid = getegid();
175
176 /* Try it as a group name, then a group id. */
177 if ((grp = getgrnam(grpname)) == NULL)
178 if ((lgid = strtol(grpname, &ep, 10)) <= 0 || *ep != '\0' ||
179 (grp = getgrgid((gid_t)lgid)) == NULL ) {
180 warnx("%s: bad group name", grpname);
181 return;
182 }
183
184 #ifdef __APPLE__
185 status = mbr_uid_to_uuid(pwd->pw_uid, user_uuid);
186 if (status)
187 errc(1, status, "mbr_uid_to_uuid");
188
189 status = mbr_gid_to_uuid(grp->gr_gid, group_uuid);
190 if (status)
191 errc(1, status, "mbr_gid_to_uuid");
192
193 status = mbr_check_membership(user_uuid, group_uuid, &dbmember);
194 if (status)
195 errc(1, status, "mbr_check_membership");
196 #else
197 /*
198 * If the user is not a member of the requested group and the group
199 * has a password, prompt and check it.
200 */
201 dbmember = 0;
202 if (pwd->pw_gid == grp->gr_gid)
203 dbmember = 1;
204 for (p = grp->gr_mem; *p != NULL; p++)
205 if (strcmp(*p, pwd->pw_name) == 0) {
206 dbmember = 1;
207 break;
208 }
209 #endif
210
211 grp_passwd = grp->gr_passwd;
212 if ((grp_passwd == NULL) || (grp_passwd[0] == '\0'))
213 grp_passwd = "*";
214 if (!dbmember && getuid() != 0) {
215 pass = getpass("Password:");
216 if (pass == NULL ||
217 strcmp(grp_passwd, crypt(pass, grp_passwd)) != 0) {
218 fprintf(stderr, "Sorry\n");
219 return;
220 }
221 }
222
223 ngrps_max = sysconf(_SC_NGROUPS_MAX) + 1;
224 if ((grps = malloc(sizeof(gid_t) * ngrps_max)) == NULL)
225 err(1, "malloc");
226 if ((ngrps = getgroups((int)ngrps_max, (gid_t *)grps)) < 0) {
227 warn("getgroups");
228 goto end;
229 }
230
231 /* Remove requested gid from supp. list if it exists. */
232 if (grp->gr_gid != egid && inarray(grp->gr_gid, grps, ngrps)) {
233 for (i = 0; i < ngrps; i++)
234 if (grps[i] == grp->gr_gid)
235 break;
236 ngrps--;
237 memmove(&grps[i], &grps[i + 1], (ngrps - i) * sizeof(gid_t));
238 PRIV_START;
239 if (setgroups(ngrps, (const gid_t *)grps) < 0) {
240 PRIV_END;
241 warn("setgroups");
242 goto end;
243 }
244 PRIV_END;
245 }
246
247 PRIV_START;
248 if (setgid(grp->gr_gid)) {
249 PRIV_END;
250 warn("setgid");
251 goto end;
252 }
253 PRIV_END;
254 grps[0] = grp->gr_gid;
255
256 /* Add old effective gid to supp. list if it does not exist. */
257 if (egid != grp->gr_gid && !inarray(egid, grps, ngrps)) {
258 if (ngrps + 1 >= ngrps_max)
259 warnx("too many groups");
260 else {
261 grps[ngrps++] = egid;
262 PRIV_START;
263 if (setgroups(ngrps, (const gid_t *)grps)) {
264 PRIV_END;
265 warn("setgroups");
266 goto end;
267 }
268 PRIV_END;
269 }
270 }
271
272 end:
273 free(grps);
274 }
275
276 static int
277 inarray(gid_t gid, const gid_t grps[], int ngrps)
278 {
279 int i;
280
281 for (i = 0; i < ngrps; i++)
282 if (grps[i] == gid)
283 return (1);
284 return (0);
285 }
286
287 /*
288 * Set the environment to what would be expected if the user logged in
289 * again; this performs the same steps as su(1)'s -l option.
290 */
291 static void
292 loginshell(void)
293 {
294 char *args[2], **cleanenv, *term, *ticket;
295 const char *shell;
296 char *prog, progbuf[PATH_MAX];
297 #ifndef __APPLE__
298 login_cap_t *lc;
299 #endif /* !__APPLE__ */
300 shell = pwd->pw_shell;
301 if (*shell == '\0')
302 shell = _PATH_BSHELL;
303 if (chdir(pwd->pw_dir) < 0) {
304 warn("%s", pwd->pw_dir);
305 chdir("/");
306 }
307
308 term = getenv("TERM");
309 ticket = getenv("KRBTKFILE");
310
311 if ((cleanenv = calloc(20, sizeof(char *))) == NULL)
312 err(1, "calloc");
313 *cleanenv = NULL;
314 environ = cleanenv;
315 #ifndef __APPLE__
316 lc = login_getpwclass(pwd);
317 setusercontext(lc, pwd, pwd->pw_uid,
318 LOGIN_SETPATH|LOGIN_SETUMASK|LOGIN_SETENV);
319 login_close(lc);
320 #endif /* !__APPLE__ */
321 setenv("USER", pwd->pw_name, 1);
322 setenv("SHELL", shell, 1);
323 setenv("HOME", pwd->pw_dir, 1);
324 if (term != NULL)
325 setenv("TERM", term, 1);
326 if (ticket != NULL)
327 setenv("KRBTKFILE", ticket, 1);
328
329 strlcpy(progbuf, shell, sizeof(progbuf));
330 prog = basename(progbuf);
331
332 if (asprintf(args, "-%s", prog) < 0)
333 err(1, "asprintf");
334 args[1] = NULL;
335
336 execv(shell, args);
337 err(1, "%s", shell);
338 }
339
340 static void
341 doshell(void)
342 {
343 const char *shell;
344 char *prog, progbuf[PATH_MAX];
345
346 shell = pwd->pw_shell;
347 if (*shell == '\0')
348 shell = _PATH_BSHELL;
349
350 strlcpy(progbuf, shell, sizeof(progbuf));
351 prog = basename(progbuf);
352
353 execl(shell, prog, (char *)NULL);
354 err(1, "%s", shell);
355 }