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>
|
2014-11-13 17:29:30 +00:00
|
|
|
|
2013-06-14 16:55:25 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2014-04-22 12:13:51 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-03-02 16:41:58 +00:00
|
|
|
eprintf("usage: %s dir [cmd [arg ...]]\n", argv0);
|
2014-04-22 12:13:51 +00:00
|
|
|
}
|
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
|
|
|
{
|
2015-03-02 16:41:58 +00:00
|
|
|
char *shell[] = { "/bin/sh", "-i", NULL }, *aux, *cmd;
|
2014-02-13 13:02:12 +00:00
|
|
|
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
|
|
|
|
2015-03-02 16:41:58 +00:00
|
|
|
if (!argc)
|
2013-06-14 16:55:25 +00:00
|
|
|
usage();
|
2013-07-20 05:27:42 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if ((aux = getenv("SHELL")))
|
2014-02-13 12:57:16 +00:00
|
|
|
shell[0] = aux;
|
|
|
|
|
2014-11-19 19:59:37 +00:00
|
|
|
if (chroot(argv[0]) < 0)
|
2014-02-13 13:02:12 +00:00
|
|
|
eprintf("chroot %s:", argv[0]);
|
2013-07-20 05:27:42 +00:00
|
|
|
|
2014-11-19 19:59:37 +00:00
|
|
|
if (chdir("/") < 0)
|
2014-02-13 13:02:12 +00:00
|
|
|
eprintf("chdir:");
|
2013-06-14 16:55:25 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (argc == 1) {
|
2015-03-02 16:41:58 +00:00
|
|
|
cmd = *shell;
|
2013-06-14 16:55:25 +00:00
|
|
|
execvp(*shell, shell);
|
|
|
|
} else {
|
2015-03-02 16:41:58 +00:00
|
|
|
cmd = argv[1];
|
|
|
|
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;
|
2015-03-02 16:41:58 +00:00
|
|
|
weprintf("execvp %s:", cmd);
|
|
|
|
_exit(126 + (savederrno == ENOENT));
|
|
|
|
|
|
|
|
return 0; /* not reached */
|
2013-06-14 16:55:25 +00:00
|
|
|
}
|