2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2002 Tim J. Robbins.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * newgrp -- change to a new group
33 #include <sys/cdefs.h>
34 #include <sys/types.h>
40 #include <login_cap.h>
48 static void addgroup(const char *grpname);
49 static void doshell(void);
50 static int inarray(gid_t, const gid_t[], int);
51 static void loginshell(void);
52 static void restoregrps(void);
53 static void usage(void);
55 static struct passwd *pwd;
58 extern char **environ;
60 /* Manipulate effective user ID. */
61 #define PRIV_START do { \
62 if (seteuid(euid) < 0) \
65 #define PRIV_END do { \
66 if (seteuid(getuid()) < 0) \
71 main(int argc, char *argv[])
75 if ((euid = geteuid()) != 0)
76 warnx("need root permissions to function properly, check setuid bit");
77 if (seteuid(getuid()) < 0)
80 if ((pwd = getpwuid(getuid())) == NULL)
81 errx(1, "unknown user");
84 while ((ch = getopt(argc, argv, "-l")) != -1) {
86 case '-': /* Obsolescent */
108 if (seteuid(euid) < 0)
110 if (setuid(getuid()) < 0)
126 fprintf(stderr, "usage: newgrp [-l] [group]\n");
136 initres = initgroups(pwd->pw_name, pwd->pw_gid);
137 setres = setgid(pwd->pw_gid);
147 addgroup(const char *grpname)
150 long lgid, ngrps_max;
151 int dbmember, i, ngrps;
154 char *ep, *pass, *cryptpw;
159 /* Try it as a group name, then a group id. */
160 if ((grp = getgrnam(grpname)) == NULL)
161 if ((lgid = strtol(grpname, &ep, 10)) <= 0 || *ep != '\0' ||
162 (grp = getgrgid((gid_t)lgid)) == NULL ) {
163 warnx("%s: bad group name", grpname);
168 * If the user is not a member of the requested group and the group
169 * has a password, prompt and check it.
172 if (pwd->pw_gid == grp->gr_gid)
174 for (p = grp->gr_mem; *p != NULL; p++)
175 if (strcmp(*p, pwd->pw_name) == 0) {
179 if (!dbmember && *grp->gr_passwd != '\0' && getuid() != 0) {
180 pass = getpass("Password:");
183 cryptpw = crypt(pass, grp->gr_passwd);
184 if (cryptpw == NULL || strcmp(grp->gr_passwd, cryptpw) != 0) {
185 fprintf(stderr, "Sorry\n");
190 ngrps_max = sysconf(_SC_NGROUPS_MAX) + 1;
191 if ((grps = malloc(sizeof(gid_t) * ngrps_max)) == NULL)
193 if ((ngrps = getgroups(ngrps_max, (gid_t *)grps)) < 0) {
198 /* Remove requested gid from supp. list if it exists. */
199 if (grp->gr_gid != egid && inarray(grp->gr_gid, grps, ngrps)) {
200 for (i = 0; i < ngrps; i++)
201 if (grps[i] == grp->gr_gid)
204 memmove(&grps[i], &grps[i + 1], (ngrps - i) * sizeof(gid_t));
206 if (setgroups(ngrps, (const gid_t *)grps) < 0) {
215 if (setgid(grp->gr_gid)) {
221 grps[0] = grp->gr_gid;
223 /* Add old effective gid to supp. list if it does not exist. */
224 if (egid != grp->gr_gid && !inarray(egid, grps, ngrps)) {
225 if (ngrps == ngrps_max)
226 warnx("too many groups");
228 grps[ngrps++] = egid;
230 if (setgroups(ngrps, (const gid_t *)grps)) {
243 inarray(gid_t gid, const gid_t grps[], int ngrps)
247 for (i = 0; i < ngrps; i++)
254 * Set the environment to what would be expected if the user logged in
255 * again; this performs the same steps as su(1)'s -l option.
260 char *args[2], **cleanenv, *term, *ticket;
264 shell = pwd->pw_shell;
266 shell = _PATH_BSHELL;
267 if (chdir(pwd->pw_dir) < 0) {
268 warn("%s", pwd->pw_dir);
272 term = getenv("TERM");
273 ticket = getenv("KRBTKFILE");
275 if ((cleanenv = calloc(20, sizeof(char *))) == NULL)
280 lc = login_getpwclass(pwd);
281 setusercontext(lc, pwd, pwd->pw_uid,
282 LOGIN_SETPATH|LOGIN_SETUMASK|LOGIN_SETENV);
284 setenv("USER", pwd->pw_name, 1);
285 setenv("SHELL", shell, 1);
286 setenv("HOME", pwd->pw_dir, 1);
288 setenv("TERM", term, 1);
290 setenv("KRBTKFILE", ticket, 1);
292 if (asprintf(args, "-%s", shell) < 0)
305 shell = pwd->pw_shell;
307 shell = _PATH_BSHELL;
308 execl(shell, shell, (char *)NULL);