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