1 /* $OpenBSD: chroot.c,v 1.14 2015/05/19 16:05:12 millert Exp $ */
2
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 <ctype.h>
34 #include <err.h>
35 #include <errno.h>
36 #include <grp.h>
37 #include <limits.h>
38 #include <login_cap.h>
39 #include <paths.h>
40 #include <pwd.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45
46 int main(int, char **);
47 __dead void usage(void);
48
49 int
main(int argc,char ** argv)50 main(int argc, char **argv)
51 {
52 struct group *grp;
53 struct passwd *pwd;
54 login_cap_t *lc;
55 const char *shell;
56 char *user, *group, *grouplist;
57 gid_t gidlist[NGROUPS_MAX];
58 int ch, ngids;
59 int flags = LOGIN_SETALL & ~(LOGIN_SETLOGIN|LOGIN_SETUSER);
60
61 lc = NULL;
62 ngids = 0;
63 pwd = NULL;
64 user = grouplist = NULL;
65 while ((ch = getopt(argc, argv, "g:u:")) != -1) {
66 switch(ch) {
67 case 'u':
68 user = optarg;
69 if (*user == '\0')
70 usage();
71 break;
72 case 'g':
73 grouplist = optarg;
74 if (*grouplist == '\0')
75 usage();
76 break;
77 default:
78 usage();
79 }
80 }
81 argc -= optind;
82 argv += optind;
83
84 if (argc < 1)
85 usage();
86
87 if (user != NULL) {
88 if ((pwd = getpwnam(user)) == NULL)
89 errx(1, "no such user `%s'", user);
90 if ((lc = login_getclass(pwd->pw_class)) == NULL)
91 err(1, "unable to get login class for `%s'", user);
92 }
93
94 while ((group = strsep(&grouplist, ",")) != NULL) {
95 if (*group == '\0')
96 continue;
97
98 if (ngids == NGROUPS_MAX)
99 errx(1, "too many supplementary groups provided");
100 if ((grp = getgrnam(group)) == NULL)
101 errx(1, "no such group `%s'", group);
102 gidlist[ngids++] = grp->gr_gid;
103 }
104
105 if (ngids != 0) {
106 if (setgid(gidlist[0]) != 0)
107 err(1, "setgid");
108 if (setgroups(ngids, gidlist) != 0)
109 err(1, "setgroups");
110 flags &= ~LOGIN_SETGROUP;
111 }
112 if (lc != NULL) {
113 if (setusercontext(lc, pwd, pwd->pw_uid, flags) == -1)
114 err(1, "setusercontext");
115 }
116
117 if (chroot(argv[0]) != 0 || chdir("/") != 0)
118 err(1, "%s", argv[0]);
119
120 if (pwd != NULL) {
121 /* only set login name if we are/can be a session leader */
122 if (getsid(0) == getpid() || setsid() != -1)
123 setlogin(pwd->pw_name);
124 if (setuid(pwd->pw_uid) != 0)
125 err(1, "setuid");
126 }
127
128 if (argv[1]) {
129 execvp(argv[1], &argv[1]);
130 err(1, "%s", argv[1]);
131 }
132
133 if ((shell = getenv("SHELL")) == NULL || *shell == '\0')
134 shell = _PATH_BSHELL;
135 execlp(shell, shell, "-i", (char *)NULL);
136 err(1, "%s", shell);
137 /* NOTREACHED */
138 }
139
140 __dead void
usage(void)141 usage(void)
142 {
143 extern char *__progname;
144
145 (void)fprintf(stderr, "usage: %s [-g group,group,...] [-u user] "
146 "newroot [command]\n", __progname);
147 exit(1);
148 }
149