2003-04-09 03:04:12 +00:00
|
|
|
/*-
|
2023-05-10 15:40:58 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2017-11-27 15:37:16 +00:00
|
|
|
*
|
2003-04-09 03:04:12 +00:00
|
|
|
* Copyright (c) 2003 Mike Barcroft <mike@FreeBSD.org>
|
MFp4:
Bring in updated jail support from bz_jail branch.
This enhances the current jail implementation to permit multiple
addresses per jail. In addtion to IPv4, IPv6 is supported as well.
Due to updated checks it is even possible to have jails without
an IP address at all, which basically gives one a chroot with
restricted process view, no networking,..
SCTP support was updated and supports IPv6 in jails as well.
Cpuset support permits jails to be bound to specific processor
sets after creation.
Jails can have an unrestricted (no duplicate protection, etc.) name
in addition to the hostname. The jail name cannot be changed from
within a jail and is considered to be used for management purposes
or as audit-token in the future.
DDB 'show jails' command was added to aid debugging.
Proper compat support permits 32bit jail binaries to be used on 64bit
systems to manage jails. Also backward compatibility was preserved where
possible: for jail v1 syscalls, as well as with user space management
utilities.
Both jail as well as prison version were updated for the new features.
A gap was intentionally left as the intermediate versions had been
used by various patches floating around the last years.
Bump __FreeBSD_version for the afore mentioned and in kernel changes.
Special thanks to:
- Pawel Jakub Dawidek (pjd) for his multi-IPv4 patches
and Olivier Houchard (cognet) for initial single-IPv6 patches.
- Jeff Roberson (jeff) and Randall Stewart (rrs) for their
help, ideas and review on cpuset and SCTP support.
- Robert Watson (rwatson) for lots and lots of help, discussions,
suggestions and review of most of the patch at various stages.
- John Baldwin (jhb) for his help.
- Simon L. Nielsen (simon) as early adopter testing changes
on cluster machines as well as all the testers and people
who provided feedback the last months on freebsd-jail and
other channels.
- My employer, CK Software GmbH, for the support so I could work on this.
Reviewed by: (see above)
MFC after: 3 months (this is just so that I get the mail)
X-MFC Before: 7.2-RELEASE if possible
2008-11-29 14:32:14 +00:00
|
|
|
* Copyright (c) 2008 Bjoern A. Zeeb <bz@FreeBSD.org>
|
2003-04-09 03:04:12 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/jail.h>
|
2009-05-27 14:30:26 +00:00
|
|
|
#include <sys/socket.h>
|
2008-05-26 11:57:49 +00:00
|
|
|
#include <sys/sysctl.h>
|
|
|
|
|
2009-05-27 14:30:26 +00:00
|
|
|
#include <arpa/inet.h>
|
MFp4:
Bring in updated jail support from bz_jail branch.
This enhances the current jail implementation to permit multiple
addresses per jail. In addtion to IPv4, IPv6 is supported as well.
Due to updated checks it is even possible to have jails without
an IP address at all, which basically gives one a chroot with
restricted process view, no networking,..
SCTP support was updated and supports IPv6 in jails as well.
Cpuset support permits jails to be bound to specific processor
sets after creation.
Jails can have an unrestricted (no duplicate protection, etc.) name
in addition to the hostname. The jail name cannot be changed from
within a jail and is considered to be used for management purposes
or as audit-token in the future.
DDB 'show jails' command was added to aid debugging.
Proper compat support permits 32bit jail binaries to be used on 64bit
systems to manage jails. Also backward compatibility was preserved where
possible: for jail v1 syscalls, as well as with user space management
utilities.
Both jail as well as prison version were updated for the new features.
A gap was intentionally left as the intermediate versions had been
used by various patches floating around the last years.
Bump __FreeBSD_version for the afore mentioned and in kernel changes.
Special thanks to:
- Pawel Jakub Dawidek (pjd) for his multi-IPv4 patches
and Olivier Houchard (cognet) for initial single-IPv6 patches.
- Jeff Roberson (jeff) and Randall Stewart (rrs) for their
help, ideas and review on cpuset and SCTP support.
- Robert Watson (rwatson) for lots and lots of help, discussions,
suggestions and review of most of the patch at various stages.
- John Baldwin (jhb) for his help.
- Simon L. Nielsen (simon) as early adopter testing changes
on cluster machines as well as all the testers and people
who provided feedback the last months on freebsd-jail and
other channels.
- My employer, CK Software GmbH, for the support so I could work on this.
Reviewed by: (see above)
MFC after: 3 months (this is just so that I get the mail)
X-MFC Before: 7.2-RELEASE if possible
2008-11-29 14:32:14 +00:00
|
|
|
#include <netinet/in.h>
|
2003-04-09 03:04:12 +00:00
|
|
|
|
|
|
|
#include <err.h>
|
2006-04-19 10:12:10 +00:00
|
|
|
#include <errno.h>
|
2009-06-24 18:18:35 +00:00
|
|
|
#include <jail.h>
|
2009-05-27 14:30:26 +00:00
|
|
|
#include <limits.h>
|
2006-04-19 10:12:10 +00:00
|
|
|
#include <login_cap.h>
|
2015-07-12 17:03:50 +00:00
|
|
|
#include <paths.h>
|
|
|
|
#include <pwd.h>
|
2003-04-09 03:04:12 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
MFp4:
Bring in updated jail support from bz_jail branch.
This enhances the current jail implementation to permit multiple
addresses per jail. In addtion to IPv4, IPv6 is supported as well.
Due to updated checks it is even possible to have jails without
an IP address at all, which basically gives one a chroot with
restricted process view, no networking,..
SCTP support was updated and supports IPv6 in jails as well.
Cpuset support permits jails to be bound to specific processor
sets after creation.
Jails can have an unrestricted (no duplicate protection, etc.) name
in addition to the hostname. The jail name cannot be changed from
within a jail and is considered to be used for management purposes
or as audit-token in the future.
DDB 'show jails' command was added to aid debugging.
Proper compat support permits 32bit jail binaries to be used on 64bit
systems to manage jails. Also backward compatibility was preserved where
possible: for jail v1 syscalls, as well as with user space management
utilities.
Both jail as well as prison version were updated for the new features.
A gap was intentionally left as the intermediate versions had been
used by various patches floating around the last years.
Bump __FreeBSD_version for the afore mentioned and in kernel changes.
Special thanks to:
- Pawel Jakub Dawidek (pjd) for his multi-IPv4 patches
and Olivier Houchard (cognet) for initial single-IPv6 patches.
- Jeff Roberson (jeff) and Randall Stewart (rrs) for their
help, ideas and review on cpuset and SCTP support.
- Robert Watson (rwatson) for lots and lots of help, discussions,
suggestions and review of most of the patch at various stages.
- John Baldwin (jhb) for his help.
- Simon L. Nielsen (simon) as early adopter testing changes
on cluster machines as well as all the testers and people
who provided feedback the last months on freebsd-jail and
other channels.
- My employer, CK Software GmbH, for the support so I could work on this.
Reviewed by: (see above)
MFC after: 3 months (this is just so that I get the mail)
X-MFC Before: 7.2-RELEASE if possible
2008-11-29 14:32:14 +00:00
|
|
|
#include <string.h>
|
2003-04-09 03:04:12 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2015-07-12 17:03:50 +00:00
|
|
|
extern char **environ;
|
MFp4:
Bring in updated jail support from bz_jail branch.
This enhances the current jail implementation to permit multiple
addresses per jail. In addtion to IPv4, IPv6 is supported as well.
Due to updated checks it is even possible to have jails without
an IP address at all, which basically gives one a chroot with
restricted process view, no networking,..
SCTP support was updated and supports IPv6 in jails as well.
Cpuset support permits jails to be bound to specific processor
sets after creation.
Jails can have an unrestricted (no duplicate protection, etc.) name
in addition to the hostname. The jail name cannot be changed from
within a jail and is considered to be used for management purposes
or as audit-token in the future.
DDB 'show jails' command was added to aid debugging.
Proper compat support permits 32bit jail binaries to be used on 64bit
systems to manage jails. Also backward compatibility was preserved where
possible: for jail v1 syscalls, as well as with user space management
utilities.
Both jail as well as prison version were updated for the new features.
A gap was intentionally left as the intermediate versions had been
used by various patches floating around the last years.
Bump __FreeBSD_version for the afore mentioned and in kernel changes.
Special thanks to:
- Pawel Jakub Dawidek (pjd) for his multi-IPv4 patches
and Olivier Houchard (cognet) for initial single-IPv6 patches.
- Jeff Roberson (jeff) and Randall Stewart (rrs) for their
help, ideas and review on cpuset and SCTP support.
- Robert Watson (rwatson) for lots and lots of help, discussions,
suggestions and review of most of the patch at various stages.
- John Baldwin (jhb) for his help.
- Simon L. Nielsen (simon) as early adopter testing changes
on cluster machines as well as all the testers and people
who provided feedback the last months on freebsd-jail and
other channels.
- My employer, CK Software GmbH, for the support so I could work on this.
Reviewed by: (see above)
MFC after: 3 months (this is just so that I get the mail)
X-MFC Before: 7.2-RELEASE if possible
2008-11-29 14:32:14 +00:00
|
|
|
|
2015-07-12 17:03:50 +00:00
|
|
|
static void get_user_info(const char *username, const struct passwd **pwdp,
|
|
|
|
login_cap_t **lcapp);
|
|
|
|
static void usage(void);
|
2006-04-19 10:12:10 +00:00
|
|
|
|
2003-04-09 03:04:12 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int jid;
|
2006-04-19 10:12:10 +00:00
|
|
|
login_cap_t *lcap = NULL;
|
2015-07-12 17:03:50 +00:00
|
|
|
int ch, clean, uflag, Uflag;
|
|
|
|
char *cleanenv;
|
|
|
|
const struct passwd *pwd = NULL;
|
|
|
|
const char *username, *shell, *term;
|
2009-06-23 14:40:08 +00:00
|
|
|
|
2015-07-12 17:03:50 +00:00
|
|
|
ch = clean = uflag = Uflag = 0;
|
2009-05-27 14:30:26 +00:00
|
|
|
username = NULL;
|
2009-06-19 15:58:24 +00:00
|
|
|
|
2015-07-12 17:03:50 +00:00
|
|
|
while ((ch = getopt(argc, argv, "lnu:U:")) != -1) {
|
2006-04-19 10:12:10 +00:00
|
|
|
switch (ch) {
|
2015-07-12 17:03:50 +00:00
|
|
|
case 'l':
|
|
|
|
clean = 1;
|
|
|
|
break;
|
MFp4:
Bring in updated jail support from bz_jail branch.
This enhances the current jail implementation to permit multiple
addresses per jail. In addtion to IPv4, IPv6 is supported as well.
Due to updated checks it is even possible to have jails without
an IP address at all, which basically gives one a chroot with
restricted process view, no networking,..
SCTP support was updated and supports IPv6 in jails as well.
Cpuset support permits jails to be bound to specific processor
sets after creation.
Jails can have an unrestricted (no duplicate protection, etc.) name
in addition to the hostname. The jail name cannot be changed from
within a jail and is considered to be used for management purposes
or as audit-token in the future.
DDB 'show jails' command was added to aid debugging.
Proper compat support permits 32bit jail binaries to be used on 64bit
systems to manage jails. Also backward compatibility was preserved where
possible: for jail v1 syscalls, as well as with user space management
utilities.
Both jail as well as prison version were updated for the new features.
A gap was intentionally left as the intermediate versions had been
used by various patches floating around the last years.
Bump __FreeBSD_version for the afore mentioned and in kernel changes.
Special thanks to:
- Pawel Jakub Dawidek (pjd) for his multi-IPv4 patches
and Olivier Houchard (cognet) for initial single-IPv6 patches.
- Jeff Roberson (jeff) and Randall Stewart (rrs) for their
help, ideas and review on cpuset and SCTP support.
- Robert Watson (rwatson) for lots and lots of help, discussions,
suggestions and review of most of the patch at various stages.
- John Baldwin (jhb) for his help.
- Simon L. Nielsen (simon) as early adopter testing changes
on cluster machines as well as all the testers and people
who provided feedback the last months on freebsd-jail and
other channels.
- My employer, CK Software GmbH, for the support so I could work on this.
Reviewed by: (see above)
MFC after: 3 months (this is just so that I get the mail)
X-MFC Before: 7.2-RELEASE if possible
2008-11-29 14:32:14 +00:00
|
|
|
case 'n':
|
2009-05-27 14:30:26 +00:00
|
|
|
/* Specified name, now unused */
|
MFp4:
Bring in updated jail support from bz_jail branch.
This enhances the current jail implementation to permit multiple
addresses per jail. In addtion to IPv4, IPv6 is supported as well.
Due to updated checks it is even possible to have jails without
an IP address at all, which basically gives one a chroot with
restricted process view, no networking,..
SCTP support was updated and supports IPv6 in jails as well.
Cpuset support permits jails to be bound to specific processor
sets after creation.
Jails can have an unrestricted (no duplicate protection, etc.) name
in addition to the hostname. The jail name cannot be changed from
within a jail and is considered to be used for management purposes
or as audit-token in the future.
DDB 'show jails' command was added to aid debugging.
Proper compat support permits 32bit jail binaries to be used on 64bit
systems to manage jails. Also backward compatibility was preserved where
possible: for jail v1 syscalls, as well as with user space management
utilities.
Both jail as well as prison version were updated for the new features.
A gap was intentionally left as the intermediate versions had been
used by various patches floating around the last years.
Bump __FreeBSD_version for the afore mentioned and in kernel changes.
Special thanks to:
- Pawel Jakub Dawidek (pjd) for his multi-IPv4 patches
and Olivier Houchard (cognet) for initial single-IPv6 patches.
- Jeff Roberson (jeff) and Randall Stewart (rrs) for their
help, ideas and review on cpuset and SCTP support.
- Robert Watson (rwatson) for lots and lots of help, discussions,
suggestions and review of most of the patch at various stages.
- John Baldwin (jhb) for his help.
- Simon L. Nielsen (simon) as early adopter testing changes
on cluster machines as well as all the testers and people
who provided feedback the last months on freebsd-jail and
other channels.
- My employer, CK Software GmbH, for the support so I could work on this.
Reviewed by: (see above)
MFC after: 3 months (this is just so that I get the mail)
X-MFC Before: 7.2-RELEASE if possible
2008-11-29 14:32:14 +00:00
|
|
|
break;
|
2006-04-19 10:12:10 +00:00
|
|
|
case 'u':
|
|
|
|
username = optarg;
|
|
|
|
uflag = 1;
|
|
|
|
break;
|
|
|
|
case 'U':
|
|
|
|
username = optarg;
|
|
|
|
Uflag = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
2015-07-12 17:03:50 +00:00
|
|
|
if (argc < 1)
|
2006-04-19 10:12:10 +00:00
|
|
|
usage();
|
|
|
|
if (uflag && Uflag)
|
2003-04-09 03:04:12 +00:00
|
|
|
usage();
|
2015-07-12 17:03:50 +00:00
|
|
|
if (uflag || (clean && !Uflag))
|
|
|
|
/* User info from the home environment */
|
|
|
|
get_user_info(username, &pwd, &lcap);
|
|
|
|
|
|
|
|
/* Attach to the jail */
|
2009-06-24 18:18:35 +00:00
|
|
|
jid = jail_getid(argv[0]);
|
|
|
|
if (jid < 0)
|
|
|
|
errx(1, "%s", jail_errmsg);
|
2008-05-29 17:00:01 +00:00
|
|
|
if (jail_attach(jid) == -1)
|
2009-06-24 18:18:35 +00:00
|
|
|
err(1, "jail_attach(%d)", jid);
|
2003-04-09 03:04:12 +00:00
|
|
|
if (chdir("/") == -1)
|
|
|
|
err(1, "chdir(): /");
|
2015-07-12 17:03:50 +00:00
|
|
|
|
|
|
|
/* Set up user environment */
|
|
|
|
if (clean || username != NULL) {
|
2006-04-19 10:12:10 +00:00
|
|
|
if (Uflag)
|
2015-07-12 17:03:50 +00:00
|
|
|
/* User info from the jail environment */
|
|
|
|
get_user_info(username, &pwd, &lcap);
|
|
|
|
if (clean) {
|
|
|
|
term = getenv("TERM");
|
|
|
|
cleanenv = NULL;
|
|
|
|
environ = &cleanenv;
|
|
|
|
setenv("PATH", "/bin:/usr/bin", 1);
|
|
|
|
if (term != NULL)
|
|
|
|
setenv("TERM", term, 1);
|
|
|
|
}
|
2006-04-19 10:12:10 +00:00
|
|
|
if (setgid(pwd->pw_gid) != 0)
|
|
|
|
err(1, "setgid");
|
2015-07-12 17:03:50 +00:00
|
|
|
if (setusercontext(lcap, pwd, pwd->pw_uid, username
|
|
|
|
? LOGIN_SETALL & ~LOGIN_SETGROUP & ~LOGIN_SETLOGIN
|
|
|
|
: LOGIN_SETPATH | LOGIN_SETENV) != 0)
|
2006-04-19 10:12:10 +00:00
|
|
|
err(1, "setusercontext");
|
|
|
|
login_close(lcap);
|
2015-07-12 17:03:50 +00:00
|
|
|
setenv("USER", pwd->pw_name, 1);
|
|
|
|
setenv("HOME", pwd->pw_dir, 1);
|
|
|
|
setenv("SHELL",
|
|
|
|
*pwd->pw_shell ? pwd->pw_shell : _PATH_BSHELL, 1);
|
|
|
|
if (clean && chdir(pwd->pw_dir) < 0)
|
|
|
|
err(1, "chdir: %s", pwd->pw_dir);
|
|
|
|
endpwent();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Run the specified command, or the shell */
|
|
|
|
if (argc > 1) {
|
|
|
|
if (execvp(argv[1], argv + 1) < 0)
|
|
|
|
err(1, "execvp: %s", argv[1]);
|
|
|
|
} else {
|
|
|
|
if (!(shell = getenv("SHELL")))
|
|
|
|
shell = _PATH_BSHELL;
|
|
|
|
if (execlp(shell, shell, "-i", NULL) < 0)
|
|
|
|
err(1, "execlp: %s", shell);
|
2006-04-19 10:12:10 +00:00
|
|
|
}
|
2003-04-09 03:04:12 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2015-07-12 17:03:50 +00:00
|
|
|
static void
|
|
|
|
get_user_info(const char *username, const struct passwd **pwdp,
|
|
|
|
login_cap_t **lcapp)
|
|
|
|
{
|
|
|
|
uid_t uid;
|
|
|
|
const struct passwd *pwd;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
if (username) {
|
|
|
|
pwd = getpwnam(username);
|
|
|
|
if (pwd == NULL) {
|
|
|
|
if (errno)
|
|
|
|
err(1, "getpwnam: %s", username);
|
|
|
|
else
|
|
|
|
errx(1, "%s: no such user", username);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
uid = getuid();
|
|
|
|
pwd = getpwuid(uid);
|
|
|
|
if (pwd == NULL) {
|
|
|
|
if (errno)
|
|
|
|
err(1, "getpwuid: %d", uid);
|
|
|
|
else
|
|
|
|
errx(1, "unknown uid: %d", uid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*pwdp = pwd;
|
|
|
|
*lcapp = login_getpwclass(pwd);
|
|
|
|
if (*lcapp == NULL)
|
|
|
|
err(1, "getpwclass: %s", pwd->pw_name);
|
|
|
|
if (initgroups(pwd->pw_name, pwd->pw_gid) < 0)
|
|
|
|
err(1, "initgroups: %s", pwd->pw_name);
|
|
|
|
}
|
|
|
|
|
2003-04-09 03:04:12 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
|
2009-05-27 14:30:26 +00:00
|
|
|
fprintf(stderr, "%s\n",
|
2015-07-12 17:03:50 +00:00
|
|
|
"usage: jexec [-l] [-u username | -U username] jail [command ...]");
|
2017-11-10 14:53:16 +00:00
|
|
|
exit(1);
|
2003-04-09 03:04:12 +00:00
|
|
|
}
|