1 /* $OpenBSD: chroot.c,v 1.12 2005/05/23 22:52:04 henning 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 #ifndef lint
33 static const char copyright[] =
34 "@(#) Copyright (c) 1988, 1993\n\
35 The Regents of the University of California. All rights reserved.\n";
36 #endif /* not lint */
37
38 #ifndef lint
39 #if 0
40 static const char sccsid[] = "@(#)chroot.c 8.1 (Berkeley) 6/9/93";
41 #else
42 static const char rcsid[] = "$OpenBSD: chroot.c,v 1.12 2005/05/23 22:52:04 henning Exp $";
43 #endif
44 #endif /* not lint */
45
46 #include <sys/types.h>
47 #include <ctype.h>
48 #include <err.h>
49 #include <errno.h>
50 #include <grp.h>
51 #include <limits.h>
52 #include <paths.h>
53 #include <pwd.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58
59 int main(int, char **);
60 __dead void usage(void);
61
62 int
main(int argc,char ** argv)63 main(int argc, char **argv)
64 {
65 struct group *grp;
66 struct passwd *pwd;
67 const char *shell;
68 char *user, *group, *grouplist;
69 gid_t gidlist[NGROUPS_MAX];
70 int ch, ngids;
71
72 ngids = 0;
73 pwd = NULL;
74 user = grouplist = NULL;
75 while ((ch = getopt(argc, argv, "g:u:")) != -1) {
76 switch(ch) {
77 case 'u':
78 user = optarg;
79 if (*user == '\0')
80 usage();
81 break;
82 case 'g':
83 grouplist = optarg;
84 if (*grouplist == '\0')
85 usage();
86 break;
87 default:
88 usage();
89 }
90 }
91 argc -= optind;
92 argv += optind;
93
94 if (argc < 1)
95 usage();
96
97 if (user != NULL && (pwd = getpwnam(user)) == NULL)
98 errx(1, "no such user `%s'", user);
99
100 while ((group = strsep(&grouplist, ",")) != NULL) {
101 if (*group == '\0')
102 continue;
103
104 if (ngids == NGROUPS_MAX)
105 errx(1, "too many supplementary groups provided");
106 if ((grp = getgrnam(group)) == NULL)
107 errx(1, "no such group `%s'", group);
108 gidlist[ngids++] = grp->gr_gid;
109 }
110
111 if (ngids != 0) {
112 if (setgid(gidlist[0]) != 0)
113 err(1, "setgid");
114 if (setgroups(ngids, gidlist) != 0)
115 err(1, "setgroups");
116 } else if (pwd != NULL) {
117 if (setgid(pwd->pw_gid) != 0)
118 err(1, "setgid");
119 if (initgroups(user, pwd->pw_gid) == -1)
120 err(1, "initgroups");
121 }
122
123 if (chroot(argv[0]) != 0 || chdir("/") != 0)
124 err(1, "%s", argv[0]);
125
126 if (pwd != NULL) {
127 /* only set login name if we are/can be a session leader */
128 if (getsid(0) == getpid() || setsid() != -1)
129 setlogin(pwd->pw_name);
130 if (setuid(pwd->pw_uid) != 0)
131 err(1, "setuid");
132 endgrent();
133 }
134
135 if (argv[1]) {
136 execvp(argv[1], &argv[1]);
137 err(1, "%s", argv[1]);
138 }
139
140 if ((shell = getenv("SHELL")) == NULL || *shell == '\0')
141 shell = _PATH_BSHELL;
142 execlp(shell, shell, "-i", (char *)NULL);
143 err(1, "%s", shell);
144 /* NOTREACHED */
145 }
146
147 __dead void
usage(void)148 usage(void)
149 {
150 extern char *__progname;
151
152 (void)fprintf(stderr, "usage: %s [-g group,group,...] [-u user] "
153 "newroot [command]\n", __progname);
154 exit(1);
155 }
156