chroot.c revision 29448
11553Srgrimes/*
21553Srgrimes * Copyright (c) 1988, 1993
31553Srgrimes *	The Regents of the University of California.  All rights reserved.
41553Srgrimes *
51553Srgrimes * Redistribution and use in source and binary forms, with or without
61553Srgrimes * modification, are permitted provided that the following conditions
71553Srgrimes * are met:
81553Srgrimes * 1. Redistributions of source code must retain the above copyright
91553Srgrimes *    notice, this list of conditions and the following disclaimer.
101553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111553Srgrimes *    notice, this list of conditions and the following disclaimer in the
121553Srgrimes *    documentation and/or other materials provided with the distribution.
131553Srgrimes * 3. All advertising materials mentioning features or use of this software
141553Srgrimes *    must display the following acknowledgement:
151553Srgrimes *	This product includes software developed by the University of
161553Srgrimes *	California, Berkeley and its contributors.
171553Srgrimes * 4. Neither the name of the University nor the names of its contributors
181553Srgrimes *    may be used to endorse or promote products derived from this software
191553Srgrimes *    without specific prior written permission.
201553Srgrimes *
211553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241553Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311553Srgrimes * SUCH DAMAGE.
321553Srgrimes */
331553Srgrimes
341553Srgrimes#ifndef lint
3529448Scharnierstatic const char copyright[] =
361553Srgrimes"@(#) Copyright (c) 1988, 1993\n\
371553Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381553Srgrimes#endif /* not lint */
391553Srgrimes
401553Srgrimes#ifndef lint
4129448Scharnier#if 0
421553Srgrimesstatic char sccsid[] = "@(#)chroot.c	8.1 (Berkeley) 6/9/93";
4329448Scharnier#endif
4429448Scharnierstatic const char rcsid[] =
4529448Scharnier	"$Id$";
461553Srgrimes#endif /* not lint */
471553Srgrimes
481553Srgrimes#include <sys/types.h>
491553Srgrimes
501553Srgrimes#include <err.h>
511553Srgrimes#include <paths.h>
521553Srgrimes#include <stdio.h>
531553Srgrimes#include <stdlib.h>
541553Srgrimes#include <string.h>
551553Srgrimes#include <unistd.h>
561553Srgrimes
5729448Scharnierstatic void usage __P((void));
581553Srgrimes
591553Srgrimesint
601553Srgrimesmain(argc, argv)
611553Srgrimes	int argc;
621553Srgrimes	char *argv[];
631553Srgrimes{
641553Srgrimes	int ch;
651553Srgrimes	char *shell;
661553Srgrimes
6724428Simp	while ((ch = getopt(argc, argv, "")) != -1)
681553Srgrimes		switch(ch) {
691553Srgrimes		case '?':
701553Srgrimes		default:
711553Srgrimes			usage();
721553Srgrimes		}
731553Srgrimes	argc -= optind;
741553Srgrimes	argv += optind;
751553Srgrimes
761553Srgrimes	if (argc < 1)
771553Srgrimes		usage();
781553Srgrimes
791553Srgrimes	if (chdir(argv[0]) || chroot("."))
801553Srgrimes		err(1, "%s", argv[0]);
811553Srgrimes
821553Srgrimes	if (argv[1]) {
831553Srgrimes		execvp(argv[1], &argv[1]);
841553Srgrimes		err(1, "%s", argv[1]);
851553Srgrimes	}
861553Srgrimes
871553Srgrimes	if (!(shell = getenv("SHELL")))
881553Srgrimes		shell = _PATH_BSHELL;
891553Srgrimes	execlp(shell, shell, "-i", NULL);
901553Srgrimes	err(1, "%s", shell);
911553Srgrimes	/* NOTREACHED */
921553Srgrimes}
931553Srgrimes
9429448Scharnierstatic void
951553Srgrimesusage()
961553Srgrimes{
971553Srgrimes	(void)fprintf(stderr, "usage: chroot newroot [command]\n");
981553Srgrimes	exit(1);
991553Srgrimes}
100