2013-10-07 16:03:26 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2014-02-13 13:02:12 +00:00
|
|
|
#include <errno.h>
|
2013-06-14 16:55:25 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "util.h"
|
|
|
|
|
2014-04-22 12:13:51 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
eprintf("usage: chroot dir [command [arg...]]\n");
|
|
|
|
}
|
2013-06-14 16:55:25 +00:00
|
|
|
|
|
|
|
int
|
2014-04-18 10:51:18 +00:00
|
|
|
main(int argc, char *argv[])
|
2013-06-14 16:55:25 +00:00
|
|
|
{
|
2014-02-13 13:02:12 +00:00
|
|
|
char *shell[] = { "/bin/sh", "-i", NULL }, *aux, *p;
|
|
|
|
int savederrno;
|
2013-07-20 05:27:42 +00:00
|
|
|
|
2014-02-13 12:57:16 +00:00
|
|
|
ARGBEGIN {
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
2013-06-14 16:55:25 +00:00
|
|
|
|
2014-02-13 12:57:16 +00:00
|
|
|
if(argc < 1)
|
2013-06-14 16:55:25 +00:00
|
|
|
usage();
|
2013-07-20 05:27:42 +00:00
|
|
|
|
2014-02-13 12:57:16 +00:00
|
|
|
if((aux = getenv("SHELL")))
|
|
|
|
shell[0] = aux;
|
|
|
|
|
|
|
|
if(chroot(argv[0]) == -1)
|
2014-02-13 13:02:12 +00:00
|
|
|
eprintf("chroot %s:", argv[0]);
|
2013-07-20 05:27:42 +00:00
|
|
|
|
2013-06-14 16:55:25 +00:00
|
|
|
if(chdir("/") == -1)
|
2014-02-13 13:02:12 +00:00
|
|
|
eprintf("chdir:");
|
2013-06-14 16:55:25 +00:00
|
|
|
|
2014-02-13 12:57:16 +00:00
|
|
|
if(argc == 1) {
|
2014-02-13 13:02:12 +00:00
|
|
|
p = *shell;
|
2013-06-14 16:55:25 +00:00
|
|
|
execvp(*shell, shell);
|
|
|
|
} else {
|
2014-02-13 13:02:12 +00:00
|
|
|
p = argv[1];
|
2014-02-13 12:57:16 +00:00
|
|
|
execvp(argv[1], argv+1);
|
2013-06-14 16:55:25 +00:00
|
|
|
}
|
2013-07-20 05:27:42 +00:00
|
|
|
|
2014-02-13 13:02:12 +00:00
|
|
|
savederrno = errno;
|
|
|
|
weprintf("execvp %s:", p);
|
|
|
|
_exit(savederrno == ENOENT ? 127 : 126);
|
2013-10-07 15:41:55 +00:00
|
|
|
return EXIT_FAILURE;
|
2013-06-14 16:55:25 +00:00
|
|
|
}
|
|
|
|
|