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
|
|
|
|
2015-04-24 21:54:17 +00:00
|
|
|
argv0 = argv[0], argc--, argv++;
|
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;
|
2015-03-09 00:04:34 +00:00
|
|
|
execvp(cmd, shell);
|
2013-06-14 16:55:25 +00:00
|
|
|
} else {
|
2015-03-02 16:41:58 +00:00
|
|
|
cmd = argv[1];
|
2015-03-09 00:04:34 +00:00
|
|
|
execvp(cmd, 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);
|
|
|
|
|
2015-03-09 00:04:34 +00:00
|
|
|
_exit(126 + (savederrno == ENOENT));
|
2013-06-14 16:55:25 +00:00
|
|
|
}
|