Skip site navigation (1)Skip section navigation (2)
Date:      Thu,  5 Aug 2004 14:09:18 +0400 (MSD)
From:      Dmitry Sivachenko <mitya@demos.su>
To:        FreeBSD-gnats-submit@FreeBSD.org
Subject:    bin/70024: jail(8) enhancement: run program in the clean environment
Message-ID:  <20040805100918.231558A001@dwarf.demos.su>
Resent-Message-ID: <200408051010.i75AAJW4054789@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help

>Number:         70024
>Category:       bin
>Synopsis:       jail(8) enhancement: run program in the clean environment
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Thu Aug 05 10:10:19 GMT 2004
>Closed-Date:
>Last-Modified:
>Originator:     Dmitry Sivachenko
>Release:        FreeBSD 5.2-CURRENT i386
>Organization:
>Environment:
System: FreeBSD dwarf.demos.su 5.2-CURRENT FreeBSD 5.2-CURRENT #1: Mon Aug 2 10:41:29 MSD 2004 azher@dwarf.demos.su:/mnt/ad0s2d/obj/mnt/ad0s2d/src/sys/DWARF i386


	
>Description:
Add -l option to jail(8).  Before running jail'ed program under
specific user's credentials, clean the
environment and set only a few variables.
>How-To-Repeat:
	
>Fix:


--- /usr/src/usr.sbin/jail/jail.c	Mon Jun 28 00:51:09 2004
+++ jail.c	Thu Aug  5 13:40:10 2004
@@ -20,6 +20,7 @@
 #include <errno.h>
 #include <grp.h>
 #include <login_cap.h>
+#include <paths.h>
 #include <pwd.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -27,6 +28,7 @@
 #include <unistd.h>
 
 static void	usage(void);
+extern char	**environ;
 
 #define GET_USER_INFO do {						\
 	pwd = getpwnam(username);					\
@@ -51,13 +53,15 @@
 	struct jail j;
 	struct passwd *pwd;
 	struct in_addr in;
-	int ch, groups[NGROUPS], i, iflag, ngroups, uflag, Uflag;
+	int ch, groups[NGROUPS], i, iflag, lflag, ngroups, uflag, Uflag;
 	char path[PATH_MAX], *username;
+	static char *cleanenv;
+	const char *shell, *p;
 
-	iflag = uflag = Uflag = 0;
-	username = NULL;
+	iflag = lflag = uflag = Uflag = 0;
+	username = cleanenv = NULL;
 
-	while ((ch = getopt(argc, argv, "iu:U:")) != -1) {
+	while ((ch = getopt(argc, argv, "ilu:U:")) != -1) {
 		switch (ch) {
 		case 'i':
 			iflag = 1;
@@ -70,6 +74,9 @@
 			username = optarg;
 			Uflag = 1;
 			break;
+		case 'l':
+			lflag = 1;
+			break;
 		default:
 			usage();
 		}
@@ -80,6 +87,8 @@
 		usage();
 	if (uflag && Uflag)
 		usage();
+	if (lflag && username == NULL)
+		usage();
 	if (uflag)
 		GET_USER_INFO;
 	if (realpath(argv[0], path) == NULL)
@@ -103,6 +112,10 @@
 	if (username != NULL) {
 		if (Uflag)
 			GET_USER_INFO;
+		if (lflag) {
+			p = getenv("TERM");
+			environ = &cleanenv;
+		}
 		if (setgroups(ngroups, groups) != 0)
 			err(1, "setgroups");
 		if (setgid(pwd->pw_gid) != 0)
@@ -112,6 +125,19 @@
 			err(1, "setusercontext");
 		login_close(lcap);
 	}
+	if (lflag) {
+		if (*pwd->pw_shell)
+			shell = pwd->pw_shell;
+		else
+			shell = _PATH_BSHELL;
+		if (chdir(pwd->pw_dir) < 0)
+			errx(1, "no home directory");
+		setenv("HOME", pwd->pw_dir, 1);
+		setenv("SHELL", shell, 1);
+		setenv("USER", pwd->pw_name, 1);
+		if (p)
+			setenv("TERM", p, 1);
+	}
 	if (execv(argv[3], argv + 3) != 0)
 		err(1, "execv: %s", argv[3]);
 	exit(0);
@@ -122,7 +148,7 @@
 {
 
 	(void)fprintf(stderr, "%s%s\n",
-	     "usage: jail [-i] [-u username | -U username]",
+	     "usage: jail [-i] [-l -u username | -U username]",
 	     " path hostname ip-number command ...");
 	exit(1);
 }



--- /usr/src/usr.sbin/jail/jail.8	Sun Jul  4 00:51:15 2004
+++ jail.8	Thu Aug  5 14:03:49 2004
@@ -42,7 +42,7 @@
 .Sh SYNOPSIS
 .Nm
 .Op Fl i
-.Op Fl u Ar username | Fl U Ar username
+.Op Fl l Fl u Ar username | Fl U Ar username
 .Ar path hostname ip-number command ...
 .Sh DESCRIPTION
 The
@@ -53,6 +53,24 @@
 .Bl -tag -width ".Fl u Ar username"
 .It Fl i
 Output the jail identifier of the newly created jail.
+.It Fl l
+Run program in the clean environment.
+The environment is discarded except for
+.Ev HOME ,
+.Ev SHELL ,
+.Ev TERM
+and
+.Ev USER .
+.Ev HOME
+and
+.Ev SHELL
+are set to the target login's default values.
+.Ev USER
+is set to the target login.
+.Ev TERM
+is imported from your current environment.
+The environment variables from the login class capability database for the
+target login are also set.
 .It Fl u Ar username
 The user name from host environment as whom the
 .Ar command
>Release-Note:
>Audit-Trail:
>Unformatted:



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20040805100918.231558A001>