1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1988, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
15 * 3. Neither the name of the University nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/types.h>
33#include <sys/procctl.h>
34
35#include <ctype.h>
36#include <err.h>
37#include <grp.h>
38#include <limits.h>
39#include <paths.h>
40#include <pwd.h>
41#include <stdbool.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45#include <unistd.h>
46
47static void usage(void) __dead2;
48
49int
50main(int argc, char *argv[])
51{
52	struct group	*gp;
53	struct passwd	*pw;
54	char		*endp, *p, *user, *group, *grouplist;
55	const char	*shell;
56	gid_t		gid, *gidlist;
57	uid_t		uid;
58	int		arg, ch, error, gids;
59	long		ngroups_max;
60	bool		nonprivileged;
61
62	gid = 0;
63	uid = 0;
64	user = group = grouplist = NULL;
65	nonprivileged = false;
66	while ((ch = getopt(argc, argv, "G:g:u:n")) != -1) {
67		switch(ch) {
68		case 'u':
69			user = optarg;
70			if (*user == '\0')
71				usage();
72			break;
73		case 'g':
74			group = optarg;
75			if (*group == '\0')
76				usage();
77			break;
78		case 'G':
79			grouplist = optarg;
80			if (*grouplist == '\0')
81				usage();
82			break;
83		case 'n':
84			nonprivileged = true;
85			break;
86		case '?':
87		default:
88			usage();
89		}
90	}
91	argc -= optind;
92	argv += optind;
93
94	if (argc < 1)
95		usage();
96
97	if (group != NULL) {
98		if (isdigit((unsigned char)*group)) {
99			gid = (gid_t)strtoul(group, &endp, 0);
100			if (*endp != '\0')
101				goto getgroup;
102		} else {
103 getgroup:
104			if ((gp = getgrnam(group)) != NULL)
105				gid = gp->gr_gid;
106			else
107				errx(1, "no such group `%s'", group);
108		}
109	}
110
111	ngroups_max = sysconf(_SC_NGROUPS_MAX) + 1;
112	if ((gidlist = malloc(sizeof(gid_t) * ngroups_max)) == NULL)
113		err(1, "malloc");
114	for (gids = 0;
115	    (p = strsep(&grouplist, ",")) != NULL && gids < ngroups_max; ) {
116		if (*p == '\0')
117			continue;
118
119		if (isdigit((unsigned char)*p)) {
120			gidlist[gids] = (gid_t)strtoul(p, &endp, 0);
121			if (*endp != '\0')
122				goto getglist;
123		} else {
124 getglist:
125			if ((gp = getgrnam(p)) != NULL)
126				gidlist[gids] = gp->gr_gid;
127			else
128				errx(1, "no such group `%s'", p);
129		}
130		gids++;
131	}
132	if (p != NULL && gids == ngroups_max)
133		errx(1, "too many supplementary groups provided");
134
135	if (user != NULL) {
136		if (isdigit((unsigned char)*user)) {
137			uid = (uid_t)strtoul(user, &endp, 0);
138			if (*endp != '\0')
139				goto getuser;
140		} else {
141 getuser:
142			if ((pw = getpwnam(user)) != NULL)
143				uid = pw->pw_uid;
144			else
145				errx(1, "no such user `%s'", user);
146		}
147	}
148
149	if (nonprivileged) {
150		arg = PROC_NO_NEW_PRIVS_ENABLE;
151		error = procctl(P_PID, getpid(), PROC_NO_NEW_PRIVS_CTL, &arg);
152		if (error != 0)
153			err(1, "procctl");
154	}
155
156	if (chdir(argv[0]) == -1 || chroot(".") == -1)
157		err(1, "%s", argv[0]);
158
159	if (gids && setgroups(gids, gidlist) == -1)
160		err(1, "setgroups");
161	if (group && setgid(gid) == -1)
162		err(1, "setgid");
163	if (user && setuid(uid) == -1)
164		err(1, "setuid");
165
166	if (argv[1]) {
167		execvp(argv[1], &argv[1]);
168		err(1, "%s", argv[1]);
169	}
170
171	if (!(shell = getenv("SHELL")))
172		shell = _PATH_BSHELL;
173	execlp(shell, shell, "-i", (char *)NULL);
174	err(1, "%s", shell);
175	/* NOTREACHED */
176}
177
178static void
179usage(void)
180{
181	(void)fprintf(stderr, "usage: chroot [-g group] [-G group,group,...] "
182	    "[-u user] [-n] newroot [command]\n");
183	exit(1);
184}
185