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)
|
|
|
|
{
|
2015-03-02 11:41:58 -05:00
|
|
|
eprintf("usage: %s dir [cmd [arg ...]]\n", argv0);
|
2014-04-22 08:13:51 -04:00
|
|
|
}
|
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
|
|
|
{
|
2015-03-02 11:41:58 -05:00
|
|
|
char *shell[] = { "/bin/sh", "-i", NULL }, *aux, *cmd;
|
2014-02-13 08:02:12 -05:00
|
|
|
int savederrno;
|
2013-07-20 01:27:42 -04:00
|
|
|
|
2019-06-28 03:18:48 -04:00
|
|
|
ARGBEGIN {
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND
|
2013-06-14 12:55:25 -04:00
|
|
|
|
2015-03-02 11:41:58 -05:00
|
|
|
if (!argc)
|
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-19 14:59:37 -05:00
|
|
|
if (chroot(argv[0]) < 0)
|
2014-02-13 08:02:12 -05:00
|
|
|
eprintf("chroot %s:", argv[0]);
|
2013-07-20 01:27:42 -04:00
|
|
|
|
2014-11-19 14:59:37 -05:00
|
|
|
if (chdir("/") < 0)
|
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) {
|
2015-03-02 11:41:58 -05:00
|
|
|
cmd = *shell;
|
2015-03-08 20:04:34 -04:00
|
|
|
execvp(cmd, shell);
|
2013-06-14 12:55:25 -04:00
|
|
|
} else {
|
2015-03-02 11:41:58 -05:00
|
|
|
cmd = argv[1];
|
2015-03-08 20:04:34 -04:00
|
|
|
execvp(cmd, 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;
|
2015-03-02 11:41:58 -05:00
|
|
|
weprintf("execvp %s:", cmd);
|
|
|
|
|
2015-03-08 20:04:34 -04:00
|
|
|
_exit(126 + (savederrno == ENOENT));
|
2013-06-14 12:55:25 -04:00
|
|
|
}
|