jail: add jexec -d, to specify a working directory

PR:		283170
Submitted by:	DtxdF at disroot.org
This commit is contained in:
Jamie Gritton
2025-03-05 02:14:47 -08:00
parent 0698ce429f
commit d56f3b051f
2 changed files with 22 additions and 9 deletions
+8 -2
View File
@@ -23,7 +23,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.Dd August 12, 2024
.Dd March 5, 2025
.Dt JEXEC 8
.Os
.Sh NAME
@@ -32,6 +32,7 @@
.Sh SYNOPSIS
.Nm
.Op Fl l
.Op Fl d Ar working-directory
.Op Fl u Ar username | Fl U Ar username
.Ar jail Op Ar command ...
.Sh DESCRIPTION
@@ -48,6 +49,9 @@ is not specified then the user's shell is used.
.Pp
The following options are available:
.Bl -tag -width indent
.It Fl d Ar working-directory
The working directory for running commands inside the jail.
The default is the jail root directory.
.It Fl l
Execute in a clean environment.
The environment is discarded except for
@@ -59,7 +63,9 @@ If a user is specified (via
.Fl u
or
.Fl U ) ,
commands are run from that (possibly jailed) user's directory.
and absent the
.Fl d
option, commands are run from that (possibly jailed) user's directory.
.It Fl u Ar username
The user name from host environment as whom the
.Ar command
+14 -7
View File
@@ -58,16 +58,22 @@ main(int argc, char *argv[])
{
int jid;
login_cap_t *lcap = NULL;
int ch, clean, uflag, Uflag;
int ch, clean, dflag, uflag, Uflag;
char *cleanenv;
const struct passwd *pwd = NULL;
const char *username, *shell, *term;
const char *workdir;
ch = clean = uflag = Uflag = 0;
ch = clean = dflag = uflag = Uflag = 0;
username = NULL;
workdir = "/";
while ((ch = getopt(argc, argv, "lnu:U:")) != -1) {
while ((ch = getopt(argc, argv, "d:lnu:U:")) != -1) {
switch (ch) {
case 'd':
workdir = optarg;
dflag = 1;
break;
case 'l':
clean = 1;
break;
@@ -102,8 +108,8 @@ main(int argc, char *argv[])
errx(1, "%s", jail_errmsg);
if (jail_attach(jid) == -1)
err(1, "jail_attach(%d)", jid);
if (chdir("/") == -1)
err(1, "chdir(): /");
if (chdir(workdir) == -1)
err(1, "chdir(): %s", workdir);
/* Set up user environment */
if (clean || username != NULL) {
@@ -129,7 +135,7 @@ main(int argc, char *argv[])
setenv("HOME", pwd->pw_dir, 1);
setenv("SHELL",
*pwd->pw_shell ? pwd->pw_shell : _PATH_BSHELL, 1);
if (clean && username && chdir(pwd->pw_dir) < 0)
if (clean && username && !dflag && chdir(pwd->pw_dir) < 0)
err(1, "chdir: %s", pwd->pw_dir);
endpwent();
}
@@ -186,6 +192,7 @@ usage(void)
{
fprintf(stderr, "%s\n",
"usage: jexec [-l] [-u username | -U username] jail [command ...]");
"usage: jexec [-l] [-d working-directory] [-u username | -U username] jail\n"
" [command ...]");
exit(1);
}