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