aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJamie Gritton <jamie@FreeBSD.org>2025-03-05 10:14:47 +0000
committerJamie Gritton <jamie@FreeBSD.org>2025-03-05 10:14:47 +0000
commitd56f3b051f6135b4a675fd75ccfcef0310368781 (patch)
tree6dc9cc8588087e026f9c24f9a64aa60830d59553
parent0698ce429f78f548f7eb3e54476fb312109ddd8b (diff)
jail: add jexec -d, to specify a working directory
PR: 283170 Submitted by: DtxdF at disroot.org
-rw-r--r--usr.sbin/jexec/jexec.810
-rw-r--r--usr.sbin/jexec/jexec.c21
2 files changed, 22 insertions, 9 deletions
diff --git a/usr.sbin/jexec/jexec.8 b/usr.sbin/jexec/jexec.8
index 431978c4d0ae..afcc1839ef75 100644
--- a/usr.sbin/jexec/jexec.8
+++ b/usr.sbin/jexec/jexec.8
@@ -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
diff --git a/usr.sbin/jexec/jexec.c b/usr.sbin/jexec/jexec.c
index 35fd9c8d20e4..a1e443c5ba04 100644
--- a/usr.sbin/jexec/jexec.c
+++ b/usr.sbin/jexec/jexec.c
@@ -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);
}