jexec: Add -e parameter to customize the environment

Currently, to define a new environment variable or modify an existing
one, we need to use env(1), which may or may not be available inside
the jail, especially in OCI containers created with the scratch
layer (i.e., those containers that are only a single static binary,
plus configuration files and related stuff). With this option, we
can specify environment variables of arbitrary length for the
specified process running inside the jail.

Reviewed by:                jamie@
Approved by:                jamie@
Differential Revision:      https://reviews.freebsd.org/D54660
This commit is contained in:
Jesús Daniel Colmenares Oviedo committed 2026-01-15 01:33:25 -04:00
1 parent 008e5703e1
commit 5810786791
2 files changed
+36 -4

No files matched your search

+12 -1
View File
@@ -23,7 +23,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.Dd March 5, 2025
.Dd January 11, 2026
.Dt JEXEC 8
.Os
.Sh NAME
@@ -33,6 +33,7 @@
.Nm
.Op Fl l
.Op Fl d Ar working-directory
.Op Oo Fl e Ar name Ns = Ns Ar value Oc ...
.Op Fl u Ar username | Fl U Ar username
.Ar jail Op Ar command ...
.Sh DESCRIPTION
@@ -66,6 +67,16 @@ or
and absent the
.Fl d
option, commands are run from that (possibly jailed) user's directory.
.It Fl e Ar name Ns = Ns Ar value
Set environment variables.
.Pp
This parameter allows arbitrary environment variables that are available to the process
to be executed inside of the jail, overwriting any previously defined environment variables,
such as those specified by the
.Fl l
parameter.
.Pp
This option can be set multiple times.
.It Fl u Ar username
The user name from host environment as whom the
.Ar command
+24 -3
View File
@@ -59,21 +59,29 @@ main(int argc, char *argv[])
int jid;
login_cap_t *lcap = NULL;
int ch, clean, dflag, uflag, Uflag;
int env_argc = argc;
char **env_argv = argv;
char *cleanenv;
const struct passwd *pwd = NULL;
const char *username, *shell, *term;
const char *workdir;
const char *jexec_args = "d:e:lnu:U:";
ch = clean = dflag = uflag = Uflag = 0;
username = NULL;
workdir = "/";
while ((ch = getopt(argc, argv, "d:lnu:U:")) != -1) {
while ((ch = getopt(argc, argv, jexec_args)) != -1) {
switch (ch) {
case 'd':
workdir = optarg;
dflag = 1;
break;
case 'e':
/* Used later. */
if (strchr(optarg, '=') == NULL)
errx(1, "%s: Invalid environment variable.", optarg);
break;
case 'l':
clean = 1;
break;
@@ -140,6 +148,19 @@ main(int argc, char *argv[])
endpwent();
}
optreset = 1;
optind = 1;
/* Custom environment */
while ((ch = getopt(env_argc, env_argv, jexec_args)) != -1) {
switch (ch) {
case 'e':
if (putenv(optarg) == -1)
err(1, "putenv");
break;
}
}
/* Run the specified command, or the shell */
if (argc > 1) {
if (execvp(argv[1], argv + 1) < 0)
@@ -192,7 +213,7 @@ usage(void)
{
fprintf(stderr, "%s\n",
"usage: jexec [-l] [-d working-directory] [-u username | -U username] jail\n"
" [command ...]");
"usage: jexec [-l] [-d working-directory] [[-e name=value] ...]\n"
" [-u username | -U username] jail [command ...]");
exit(1);
}