summaryrefslogtreecommitdiffstats
path: root/usr.sbin
diff options
context:
space:
mode:
authorbz <bz@FreeBSD.org>2008-11-29 14:32:14 +0000
committerbz <bz@FreeBSD.org>2008-11-29 14:32:14 +0000
commitd2730d5b27273f2e5a9b9f2703b896b5194496ee (patch)
tree2b8f4835032d12a0e61cc40dad151e279cf7a49f /usr.sbin
parent3b10ad8d71152b89fda1bef42dc519fc460aba1b (diff)
downloadFreeBSD-src-d2730d5b27273f2e5a9b9f2703b896b5194496ee.zip
FreeBSD-src-d2730d5b27273f2e5a9b9f2703b896b5194496ee.tar.gz
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
Diffstat (limited to 'usr.sbin')
-rw-r--r--usr.sbin/jail/Makefile6
-rw-r--r--usr.sbin/jail/jail.846
-rw-r--r--usr.sbin/jail/jail.c252
-rw-r--r--usr.sbin/jexec/Makefile2
-rw-r--r--usr.sbin/jexec/jexec.843
-rw-r--r--usr.sbin/jexec/jexec.c245
-rw-r--r--usr.sbin/jls/Makefile2
-rw-r--r--usr.sbin/jls/jls.834
-rw-r--r--usr.sbin/jls/jls.c202
9 files changed, 708 insertions, 124 deletions
diff --git a/usr.sbin/jail/Makefile b/usr.sbin/jail/Makefile
index 812cbc9..e92ced0 100644
--- a/usr.sbin/jail/Makefile
+++ b/usr.sbin/jail/Makefile
@@ -1,5 +1,7 @@
# $FreeBSD$
+.include <bsd.own.mk>
+
PROG= jail
MAN= jail.8
DPADD= ${LIBUTIL}
@@ -7,4 +9,8 @@ LDADD= -lutil
WARNS?= 6
+.if ${MK_INET6_SUPPORT} != "no"
+CFLAGS+= -DINET6
+.endif
+
.include <bsd.prog.mk>
diff --git a/usr.sbin/jail/jail.8 b/usr.sbin/jail/jail.8
index 147c210..95af6ec 100644
--- a/usr.sbin/jail/jail.8
+++ b/usr.sbin/jail/jail.8
@@ -33,7 +33,7 @@
.\"
.\" $FreeBSD$
.\"
-.Dd April 5, 2007
+.Dd November 29, 2008
.Dt JAIL 8
.Os
.Sh NAME
@@ -41,11 +41,12 @@
.Nd "imprison process and its descendants"
.Sh SYNOPSIS
.Nm
-.Op Fl i
+.Op Fl hi
+.Op Fl n Ar jailname
.Op Fl J Ar jid_file
.Op Fl s Ar securelevel
.Op Fl l u Ar username | Fl U Ar username
-.Ar path hostname ip-number command ...
+.Ar path hostname [ip[,..]] command ...
.Sh DESCRIPTION
The
.Nm
@@ -53,8 +54,26 @@ utility imprisons a process and all future descendants.
.Pp
The options are as follows:
.Bl -tag -width ".Fl u Ar username"
+.It Fl h
+Resolve
+.Va hostname
+and add all IP addresses returned by the resolver
+to the list of
+.Va ip-addresses
+for this prison.
+This may affect default address selection for outgoing IPv4 connections
+of prisons.
+The address first returned by the resolver for the IPv4 address family
+will be used as default.
+For IPv6 source address selection is done by a well defined algorithm.
.It Fl i
Output the jail identifier of the newly created jail.
+.It Fl n Ar jailname
+Assign and administrative name to the jail that can be used for management
+or auditing purposes.
+The system will
+.Sy not enforce
+the name to be unique.
.It Fl J Ar jid_file
Write a
.Ar jid_file
@@ -92,8 +111,14 @@ should run.
Directory which is to be the root of the prison.
.It Ar hostname
Hostname of the prison.
-.It Ar ip-number
-IP number assigned to the prison.
+.It Ar ip-addresses
+None, one or more IPv4 and IPv6 addresses assigned to the prison.
+The first address of each address family that was assigned to the jail will
+be used as the source address in case source address selection on unbound
+sockets cannot find a better match.
+It is only possible to start multiple jails with the same IP address,
+if none of the jails has more than this single overlapping IP address
+assigned to itself for the address family in question.
.It Ar command
Pathname of the program which is to be executed.
.El
@@ -179,7 +204,7 @@ is to disable IP services on the host system that listen on all local
IP addresses for a service.
If a network service is present in the host environment that binds all
available IP addresses rather than specific IP addresses, it may service
-requests sent to jail IP addresses.
+requests sent to jail IP addresses if the jail did not bind the port.
This means changing
.Xr inetd 8
to only listen on the
@@ -555,6 +580,9 @@ command can be used to find file system types available for mount from within
a jail.
This functionality is disabled by default, but can be enabled by setting this
MIB entry to 1.
+.It Va security.jail.jail_max_af_ips
+This MIB entry determines how may address per address family a prison
+may have. The default is 255.
.El
.Pp
The read-only sysctl variable
@@ -622,6 +650,12 @@ who contributed it to
.An Robert Watson
wrote the extended documentation, found a few bugs, added
a few new features, and cleaned up the userland jail environment.
+.Pp
+.An Bjoern A. Zeeb
+added multi-IP jail support for IPv4 and IPv6 based on a patch
+originally done by
+.An Pawel Jakub Dawidek
+for IPv4.
.Sh BUGS
Jail currently lacks the ability to allow access to
specific jail information via
diff --git a/usr.sbin/jail/jail.c b/usr.sbin/jail/jail.c
index e21fcc8..be337b7 100644
--- a/usr.sbin/jail/jail.c
+++ b/usr.sbin/jail/jail.c
@@ -12,10 +12,14 @@ __FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/jail.h>
+#include <sys/queue.h>
+#include <sys/socket.h>
#include <sys/sysctl.h>
+#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
+#include <netdb.h>
#include <err.h>
#include <errno.h>
@@ -25,12 +29,34 @@ __FBSDID("$FreeBSD$");
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
+#include <strings.h>
#include <string.h>
#include <unistd.h>
-static void usage(void);
+static void usage(void);
+static int add_addresses(struct addrinfo *);
+static struct in_addr *copy_addr4(void);
+#ifdef INET6
+static struct in6_addr *copy_addr6(void);
+#endif
+
extern char **environ;
+struct addr4entry {
+ STAILQ_ENTRY(addr4entry) addr4entries;
+ struct in_addr ip4;
+ int count;
+};
+struct addr6entry {
+ STAILQ_ENTRY(addr6entry) addr6entries;
+#ifdef INET6
+ struct in6_addr ip6;
+#endif
+ int count;
+};
+STAILQ_HEAD(addr4head, addr4entry) addr4 = STAILQ_HEAD_INITIALIZER(addr4);
+STAILQ_HEAD(addr6head, addr6entry) addr6 = STAILQ_HEAD_INITIALIZER(addr6);
+
#define GET_USER_INFO do { \
pwd = getpwnam(username); \
if (pwd == NULL) { \
@@ -53,22 +79,26 @@ main(int argc, char **argv)
login_cap_t *lcap = NULL;
struct jail j;
struct passwd *pwd = NULL;
- struct in_addr in;
gid_t groups[NGROUPS];
- int ch, i, iflag, Jflag, lflag, ngroups, securelevel, uflag, Uflag;
- char path[PATH_MAX], *ep, *username, *JidFile;
+ int ch, error, i, ngroups, securelevel;
+ int hflag, iflag, Jflag, lflag, uflag, Uflag;
+ char path[PATH_MAX], *jailname, *ep, *username, *JidFile, *ip;
static char *cleanenv;
const char *shell, *p = NULL;
long ltmp;
FILE *fp;
+ struct addrinfo hints, *res0;
- iflag = Jflag = lflag = uflag = Uflag = 0;
+ hflag = iflag = Jflag = lflag = uflag = Uflag = 0;
securelevel = -1;
- username = JidFile = cleanenv = NULL;
+ jailname = username = JidFile = cleanenv = NULL;
fp = NULL;
- while ((ch = getopt(argc, argv, "ils:u:U:J:")) != -1) {
+ while ((ch = getopt(argc, argv, "hiln:s:u:U:J:")) != -1) {
switch (ch) {
+ case 'h':
+ hflag = 1;
+ break;
case 'i':
iflag = 1;
break;
@@ -76,6 +106,9 @@ main(int argc, char **argv)
JidFile = optarg;
Jflag = 1;
break;
+ case 'n':
+ jailname = optarg;
+ break;
case 's':
ltmp = strtol(optarg, &ep, 0);
if (*ep || ep == optarg || ltmp > INT_MAX || !ltmp)
@@ -111,13 +144,62 @@ main(int argc, char **argv)
err(1, "realpath: %s", argv[0]);
if (chdir(path) != 0)
err(1, "chdir: %s", path);
+ /* Initialize struct jail. */
memset(&j, 0, sizeof(j));
- j.version = 0;
+ j.version = JAIL_API_VERSION;
j.path = path;
j.hostname = argv[1];
- if (inet_aton(argv[2], &in) == 0)
- errx(1, "Could not make sense of ip-number: %s", argv[2]);
- j.ip_number = ntohl(in.s_addr);
+ if (jailname != NULL)
+ j.jailname = jailname;
+
+ /* Handle IP addresses. If requested resolve hostname too. */
+ bzero(&hints, sizeof(struct addrinfo));
+ hints.ai_protocol = IPPROTO_TCP;
+ hints.ai_socktype = SOCK_STREAM;
+ if (JAIL_API_VERSION < 2)
+ hints.ai_family = PF_INET;
+ else
+ hints.ai_family = PF_UNSPEC;
+ /* Handle hostname. */
+ if (hflag != 0) {
+ error = getaddrinfo(j.hostname, NULL, &hints, &res0);
+ if (error != 0)
+ errx(1, "failed to handle hostname: %s",
+ gai_strerror(error));
+ error = add_addresses(res0);
+ freeaddrinfo(res0);
+ if (error != 0)
+ errx(1, "failed to add addresses.");
+ }
+ /* Handle IP addresses. */
+ hints.ai_flags = AI_NUMERICHOST;
+ ip = strtok(argv[2], ",");
+ while (ip != NULL) {
+ error = getaddrinfo(ip, NULL, &hints, &res0);
+ if (error != 0)
+ errx(1, "failed to handle ip: %s", gai_strerror(error));
+ error = add_addresses(res0);
+ freeaddrinfo(res0);
+ if (error != 0)
+ errx(1, "failed to add addresses.");
+ ip = strtok(NULL, ",");
+ }
+ /* Count IP addresses and add them to struct jail. */
+ if (!STAILQ_EMPTY(&addr4)) {
+ j.ip4s = STAILQ_FIRST(&addr4)->count;
+ j.ip4 = copy_addr4();
+ if (j.ip4s > 0 && j.ip4 == NULL)
+ errx(1, "copy_addr4()");
+ }
+#ifdef INET6
+ if (!STAILQ_EMPTY(&addr6)) {
+ j.ip6s = STAILQ_FIRST(&addr6)->count;
+ j.ip6 = copy_addr6();
+ if (j.ip6s > 0 && j.ip6 == NULL)
+ errx(1, "copy_addr6()");
+ }
+#endif
+
if (Jflag) {
fp = fopen(JidFile, "w");
if (fp == NULL)
@@ -125,7 +207,7 @@ main(int argc, char **argv)
}
i = jail(&j);
if (i == -1)
- err(1, "jail");
+ err(1, "syscall failed with");
if (iflag) {
printf("%d\n", i);
fflush(stdout);
@@ -183,8 +265,148 @@ usage(void)
{
(void)fprintf(stderr, "%s%s%s\n",
- "usage: jail [-i] [-J jid_file] [-s securelevel] [-l -u ",
- "username | -U username]",
- " path hostname ip-number command ...");
+ "usage: jail [-hi] [-n jailname] [-J jid_file] ",
+ "[-s securelevel] [-l -u username | -U username] ",
+ "path hostname [ip[,..]] command ...");
exit(1);
}
+
+static int
+add_addresses(struct addrinfo *res0)
+{
+ int error;
+ struct addrinfo *res;
+ struct addr4entry *a4p;
+ struct sockaddr_in *sai;
+#ifdef INET6
+ struct addr6entry *a6p;
+ struct sockaddr_in6 *sai6;
+#endif
+ int count;
+
+ error = 0;
+ for (res = res0; res && error == 0; res = res->ai_next) {
+ switch (res->ai_family) {
+ case AF_INET:
+ sai = (struct sockaddr_in *)(void *)res->ai_addr;
+ STAILQ_FOREACH(a4p, &addr4, addr4entries) {
+ if (bcmp(&sai->sin_addr, &a4p->ip4,
+ sizeof(struct in_addr)) == 0) {
+ err(1, "Ignoring duplicate IPv4 address.");
+ break;
+ }
+ }
+ a4p = (struct addr4entry *) malloc(
+ sizeof(struct addr4entry));
+ if (a4p == NULL) {
+ error = 1;
+ break;
+ }
+ bzero(a4p, sizeof(struct addr4entry));
+ bcopy(&sai->sin_addr, &a4p->ip4,
+ sizeof(struct in_addr));
+ if (!STAILQ_EMPTY(&addr4))
+ count = STAILQ_FIRST(&addr4)->count;
+ else
+ count = 0;
+ STAILQ_INSERT_TAIL(&addr4, a4p, addr4entries);
+ STAILQ_FIRST(&addr4)->count = count + 1;
+ break;
+#ifdef INET6
+ case AF_INET6:
+ sai6 = (struct sockaddr_in6 *)(void *)res->ai_addr;
+ STAILQ_FOREACH(a6p, &addr6, addr6entries) {
+ if (bcmp(&sai6->sin6_addr, &a6p->ip6,
+ sizeof(struct in6_addr)) == 0) {
+ err(1, "Ignoring duplicate IPv6 address.");
+ break;
+ }
+ }
+ a6p = (struct addr6entry *) malloc(
+ sizeof(struct addr6entry));
+ if (a6p == NULL) {
+ error = 1;
+ break;
+ }
+ bzero(a6p, sizeof(struct addr6entry));
+ bcopy(&sai6->sin6_addr, &a6p->ip6,
+ sizeof(struct in6_addr));
+ if (!STAILQ_EMPTY(&addr6))
+ count = STAILQ_FIRST(&addr6)->count;
+ else
+ count = 0;
+ STAILQ_INSERT_TAIL(&addr6, a6p, addr6entries);
+ STAILQ_FIRST(&addr6)->count = count + 1;
+ break;
+#endif
+ default:
+ err(1, "Address family %d not supported. Ignoring.\n",
+ res->ai_family);
+ break;
+ }
+ }
+
+ return (error);
+}
+
+static struct in_addr *
+copy_addr4(void)
+{
+ size_t len;
+ struct in_addr *ip4s, *p, ia;
+ struct addr4entry *a4p;
+
+ if (STAILQ_EMPTY(&addr4))
+ return NULL;
+
+ len = STAILQ_FIRST(&addr4)->count * sizeof(struct in_addr);
+
+ ip4s = p = (struct in_addr *)malloc(len);
+ if (ip4s == NULL)
+ return (NULL);
+
+ bzero(p, len);
+
+ while (!STAILQ_EMPTY(&addr4)) {
+ a4p = STAILQ_FIRST(&addr4);
+ STAILQ_REMOVE_HEAD(&addr4, addr4entries);
+ ia.s_addr = a4p->ip4.s_addr;
+ bcopy(&ia, p, sizeof(struct in_addr));
+ p++;
+ free(a4p);
+ }
+
+ return (ip4s);
+}
+
+#ifdef INET6
+static struct in6_addr *
+copy_addr6(void)
+{
+ size_t len;
+ struct in6_addr *ip6s, *p;
+ struct addr6entry *a6p;
+
+ if (STAILQ_EMPTY(&addr6))
+ return NULL;
+
+ len = STAILQ_FIRST(&addr6)->count * sizeof(struct in6_addr);
+
+ ip6s = p = (struct in6_addr *)malloc(len);
+ if (ip6s == NULL)
+ return (NULL);
+
+ bzero(p, len);
+
+ while (!STAILQ_EMPTY(&addr6)) {
+ a6p = STAILQ_FIRST(&addr6);
+ STAILQ_REMOVE_HEAD(&addr6, addr6entries);
+ bcopy(&a6p->ip6, p, sizeof(struct in6_addr));
+ p++;
+ free(a6p);
+ }
+
+ return (ip6s);
+}
+#endif
+
diff --git a/usr.sbin/jexec/Makefile b/usr.sbin/jexec/Makefile
index 2bf817c..049ccd4 100644
--- a/usr.sbin/jexec/Makefile
+++ b/usr.sbin/jexec/Makefile
@@ -6,4 +6,6 @@ DPADD= ${LIBUTIL}
LDADD= -lutil
WARNS?= 6
+CFLAGS+= -DSUPPORT_OLD_XPRISON
+
.include <bsd.prog.mk>
diff --git a/usr.sbin/jexec/jexec.8 b/usr.sbin/jexec/jexec.8
index 40c4979..bdda23d 100644
--- a/usr.sbin/jexec/jexec.8
+++ b/usr.sbin/jexec/jexec.8
@@ -25,7 +25,7 @@
.\"
.\" $FreeBSD$
.\"
-.Dd May 26, 2008
+.Dd November 29, 2008
.Dt JEXEC 8
.Os
.Sh NAME
@@ -34,22 +34,36 @@
.Sh SYNOPSIS
.Nm
.Op Fl u Ar username | Fl U Ar username
-.Op Fl h Ar hostname | Fl h Ar ip-number | Ar jid
-.Ar command ...
+.Op Fl n Ar jailname
+.Ar jid command ...
.Sh DESCRIPTION
The
.Nm
utility executes
.Ar command
-inside the jail identified by
-.Ar hostname
+inside the jail identified by either
+.Ar jailname
or
-.Ar ip-number
-or
-.Ar jid .
+.Ar jid
+or both.
+.Pp
+If the jail cannot be identified uniquely by the given parameters,
+an error message is printed.
+.Nm
+will also check the state of the jail (once supported) to be
+.Dv ALIVE
+and ignore jails in other states.
+The mandatory argument
+.Ar jid
+is the unique jail identifier as given by
+.Xr jls 8 .
+In case you only want to match on other criteria, give an empty string.
.Pp
The following options are available:
.Bl -tag -width indent
+.It Fl n Ar jailname
+The name of the jail, if given upon creation of the jail.
+This is not the hostname of the jail.
.It Fl u Ar username
The user name from host environment as whom the
.Ar command
@@ -59,9 +73,6 @@ The user name from jailed environment as whom the
.Ar command
should run.
.El
-.Sh "CAUTIONS"
-Only jid is guaranteed to uniquely identify a jail.
-Hostname or ip-number only work here if matched to one unique jail.
.Sh SEE ALSO
.Xr jail_attach 2 ,
.Xr jail 8 ,
@@ -71,3 +82,13 @@ The
.Nm
utility was added in
.Fx 5.1 .
+.Sh BUGS
+If the jail is not identified by
+.Ar jid
+there is a possible race in between the lookup of the jail
+and executing the command inside the jail.
+Giving a
+.Ar jid
+has a similar race as another process can stop the jail and
+start another one after the user looked up the
+.Ar jid .
diff --git a/usr.sbin/jexec/jexec.c b/usr.sbin/jexec/jexec.c
index 137eec3..69bc8f0 100644
--- a/usr.sbin/jexec/jexec.c
+++ b/usr.sbin/jexec/jexec.c
@@ -1,5 +1,6 @@
/*-
* Copyright (c) 2003 Mike Barcroft <mike@FreeBSD.org>
+ * Copyright (c) 2008 Bjoern A. Zeeb <bz@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -30,20 +31,171 @@
#include <sys/jail.h>
#include <sys/sysctl.h>
-#include <arpa/inet.h>
+#include <netinet/in.h>
#include <err.h>
#include <errno.h>
-#include <limits.h>
#include <login_cap.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <pwd.h>
#include <unistd.h>
-#include <string.h>
static void usage(void);
-static int addr2jid(const char *addr);
+
+#ifdef SUPPORT_OLD_XPRISON
+static
+char *lookup_xprison_v1(void *p, char *end, int *id)
+{
+ struct xprison_v1 *xp;
+
+ if (id == NULL)
+ errx(1, "Internal error. Invalid ID pointer.");
+
+ if ((char *)p + sizeof(struct xprison_v1) > end)
+ errx(1, "Invalid length for jail");
+
+ xp = (struct xprison_v1 *)p;
+
+ *id = xp->pr_id;
+ return ((char *)(xp + 1));
+}
+#endif
+
+static
+char *lookup_xprison_v3(void *p, char *end, int *id, char *jailname)
+{
+ struct xprison *xp;
+ char *q;
+ int ok;
+
+ if (id == NULL)
+ errx(1, "Internal error. Invalid ID pointer.");
+
+ if ((char *)p + sizeof(struct xprison) > end)
+ errx(1, "Invalid length for jail");
+
+ xp = (struct xprison *)p;
+ ok = 1;
+
+ /* Jail state and name. */
+ if (xp->pr_state < 0 || xp->pr_state >
+ (int)((sizeof(prison_states) / sizeof(struct prison_state))))
+ errx(1, "Invalid jail state.");
+ else if (xp->pr_state != PRISON_STATE_ALIVE)
+ ok = 0;
+ if (jailname != NULL) {
+ if (xp->pr_name == NULL)
+ ok = 0;
+ else if (strcmp(jailname, xp->pr_name) != 0)
+ ok = 0;
+ }
+
+ q = (char *)(xp + 1);
+ /* IPv4 addresses. */
+ q += (xp->pr_ip4s * sizeof(struct in_addr));
+ if ((char *)q > end)
+ errx(1, "Invalid length for jail");
+ /* IPv6 addresses. */
+ q += (xp->pr_ip6s * sizeof(struct in6_addr));
+ if ((char *)q > end)
+ errx(1, "Invalid length for jail");
+
+ if (ok)
+ *id = xp->pr_id;
+ return (q);
+}
+
+static int
+lookup_jail(int jid, char *jailname)
+{
+ size_t i, j, len;
+ void *p, *q;
+ int version, id, xid, count;
+
+ if (sysctlbyname("security.jail.list", NULL, &len, NULL, 0) == -1)
+ err(1, "sysctlbyname(): security.jail.list");
+
+ j = len;
+ for (i = 0; i < 4; i++) {
+ if (len <= 0)
+ exit(0);
+ p = q = malloc(len);
+ if (p == NULL)
+ err(1, "malloc()");
+
+ if (sysctlbyname("security.jail.list", q, &len, NULL, 0) == -1) {
+ if (errno == ENOMEM) {
+ free(p);
+ p = NULL;
+ len += j;
+ continue;
+ }
+ err(1, "sysctlbyname(): security.jail.list");
+ }
+ break;
+ }
+ if (p == NULL)
+ err(1, "sysctlbyname(): security.jail.list");
+ if (len < sizeof(int))
+ errx(1, "This is no prison. Kernel and userland out of sync?");
+ version = *(int *)p;
+ if (version > XPRISON_VERSION)
+ errx(1, "Sci-Fi prison. Kernel/userland out of sync?");
+
+ count = 0;
+ xid = -1;
+ for (; q != NULL && (char *)q + sizeof(int) < (char *)p + len;) {
+ version = *(int *)q;
+ if (version > XPRISON_VERSION)
+ errx(1, "Sci-Fi prison. Kernel/userland out of sync?");
+ id = -1;
+ switch (version) {
+#ifdef SUPPORT_OLD_XPRISON
+ case 1:
+ if (jailname != NULL)
+ errx(1, "Version 1 prisons did not "
+ "support jail names.");
+ q = lookup_xprison_v1(q, (char *)p + len, &id);
+ break;
+ case 2:
+ errx(1, "Version 2 was used by multi-IPv4 jail "
+ "implementations that never made it into the "
+ "official kernel.");
+ /* NOTREACHED */
+ break;
+#endif
+ case 3:
+ q = lookup_xprison_v3(q, (char *)p + len, &id, jailname);
+ break;
+ default:
+ errx(1, "Prison unknown. Kernel/userland out of sync?");
+ /* NOTREACHED */
+ break;
+ }
+ /* Possible match. */
+ if (id > 0) {
+ /* Do we have a jail ID to match as well? */
+ if (jid > 0) {
+ if (jid == id) {
+ xid = id;
+ count++;
+ }
+ } else {
+ xid = id;
+ count++;
+ }
+ }
+ }
+
+ free(p);
+
+ if (count != 1)
+ errx(1, "Could not uniquely identify the jail.");
+
+ return (xid);
+}
#define GET_USER_INFO do { \
pwd = getpwnam(username); \
@@ -68,13 +220,18 @@ main(int argc, char *argv[])
login_cap_t *lcap = NULL;
struct passwd *pwd = NULL;
gid_t groups[NGROUPS];
- int ch, ngroups, uflag, Uflag, hflag;
- char *username;
- ch = uflag = Uflag = hflag = 0;
- username = NULL;
+ int ch, ngroups, uflag, Uflag;
+ char *jailname, *username;
+
+ ch = uflag = Uflag = 0;
+ jailname = username = NULL;
+ jid = -1;
- while ((ch = getopt(argc, argv, "u:U:h")) != -1) {
+ while ((ch = getopt(argc, argv, "i:n:u:U:")) != -1) {
switch (ch) {
+ case 'n':
+ jailname = optarg;
+ break;
case 'u':
username = optarg;
uflag = 1;
@@ -83,9 +240,6 @@ main(int argc, char *argv[])
username = optarg;
Uflag = 1;
break;
- case 'h':
- hflag = 1;
- break;
default:
usage();
}
@@ -94,15 +248,22 @@ main(int argc, char *argv[])
argv += optind;
if (argc < 2)
usage();
+ if (strlen(argv[0]) > 0) {
+ jid = (int)strtol(argv[0], NULL, 10);
+ if (errno)
+ err(1, "Unable to parse jail ID.");
+ }
+ if (jid <= 0 && jailname == NULL) {
+ fprintf(stderr, "Neither jail ID nor jail name given.\n");
+ usage();
+ }
if (uflag && Uflag)
usage();
if (uflag)
GET_USER_INFO;
- if (hflag) {
- if ((jid = addr2jid(argv[0])) == 0)
- errx(1, "jail_attach(): Cannot convert %s to jid", argv[0]);
- } else
- jid = (int)strtol(argv[0], NULL, 10);
+ jid = lookup_jail(jid, jailname);
+ if (jid <= 0)
+ errx(1, "Cannot identify jail.");
if (jail_attach(jid) == -1)
err(1, "jail_attach(): %d", jid);
if (chdir("/") == -1)
@@ -130,54 +291,6 @@ usage(void)
fprintf(stderr, "%s%s\n",
"usage: jexec [-u username | -U username]",
- " [-h hostname | -h ip-number | jid] command ...");
+ " [-n jailname] jid command ...");
exit(1);
}
-
-static int
-addr2jid(const char *addr)
-{
- struct xprison *sxp, *xp;
- struct in_addr in;
- size_t i, len;
- int jid, cnt;
- jid = cnt = 0;
-
- if (sysctlbyname("security.jail.list", NULL, &len, NULL, 0) == -1)
- err(1, "sysctlbyname(): security.jail.list");
- for (i = 0; i < 4; i++) {
- if (len <= 0)
- err(1, "sysctlbyname(): len <=0");
- sxp = xp = malloc(len);
- if (sxp == NULL)
- err(1, "malloc()");
- if (sysctlbyname("security.jail.list", xp, &len, NULL, 0) == -1) {
- if (errno == ENOMEM) {
- free(sxp);
- sxp = NULL;
- continue;
- }
- err(1, "sysctlbyname(): security.jail.list");
- }
- break;
- }
- if (sxp == NULL)
- err(1, "sysctlbyname(): security.jail.list");
- if (len < sizeof(*xp) || len % sizeof(*xp) ||
- xp->pr_version != XPRISON_VERSION)
- errx(1, "Kernel and userland out of sync");
- for (i = 0; i < len / sizeof(*xp); i++) {
- in.s_addr = ntohl(xp->pr_ip);
- if ((strcmp(inet_ntoa(in), addr) == 0) ||
- (strcmp(xp->pr_host, addr) == 0)) {
- jid = xp->pr_id;
- cnt++;
- }
- xp++;
- }
- free(sxp);
- if (cnt == 1)
- return (jid);
- else
- return(0);
-}
diff --git a/usr.sbin/jls/Makefile b/usr.sbin/jls/Makefile
index 3968946..01294bd 100644
--- a/usr.sbin/jls/Makefile
+++ b/usr.sbin/jls/Makefile
@@ -4,4 +4,6 @@ PROG= jls
MAN= jls.8
WARNS?= 6
+CFLAGS+= -DSUPPORT_OLD_XPRISON
+
.include <bsd.prog.mk>
diff --git a/usr.sbin/jls/jls.8 b/usr.sbin/jls/jls.8
index 4dc68c7..aff9848 100644
--- a/usr.sbin/jls/jls.8
+++ b/usr.sbin/jls/jls.8
@@ -25,20 +25,44 @@
.\"
.\" $FreeBSD$
.\"
-.Dd April 8, 2003
+.Dd November 29, 2008
.Dt JLS 8
.Os
.Sh NAME
.Nm jls
-.Nd "list active jails"
+.Nd "list jails"
.Sh SYNOPSIS
.Nm
+.Op Fl av
.Sh DESCRIPTION
The
.Nm
-utility lists all active jails.
-Each jail is represented by one row which contains the following columns:
-jail identifier (JID), IP address, hostname, and path.
+utility lists all jails.
+By default only active jails are listed.
+.Pp
+The options are as follows:
+.Bl -tag -width ".Fl a"
+.It Fl a
+Show jails in all states, not only active ones.
+.It Fl v
+Show more verbose information.
+This also lists cpusets, jail state, multi-IP, etc. instead of the
+classic single-IP jail output.
+.El
+.Pp
+Each jail is represented by rows which, depending on
+.Fl v ,
+contain the following columns:
+.Bl -item -offset indent -compact
+.It
+jail identifier (JID), hostname and path
+.It
+jail state and name
+.It
+jail cpuset
+.It
+followed by one IP adddress per line.
+.El
.Sh SEE ALSO
.Xr jail 2 ,
.Xr jail 8 ,
diff --git a/usr.sbin/jls/jls.c b/usr.sbin/jls/jls.c
index 23a3946..4488c31 100644
--- a/usr.sbin/jls/jls.c
+++ b/usr.sbin/jls/jls.c
@@ -1,5 +1,6 @@
/*-
* Copyright (c) 2003 Mike Barcroft <mike@FreeBSD.org>
+ * Copyright (c) 2008 Bjoern A. Zeeb <bz@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -27,57 +28,216 @@
*/
#include <sys/param.h>
+#include <sys/types.h>
#include <sys/jail.h>
#include <sys/sysctl.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
#include <arpa/inet.h>
#include <err.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#define FLAG_A 0x00001
+#define FLAG_V 0x00002
+
+#ifdef SUPPORT_OLD_XPRISON
+static
+char *print_xprison_v1(void *p, char *end)
+{
+ struct xprison_v1 *xp;
+ struct in_addr in;
+
+ if ((char *)p + sizeof(struct xprison_v1) > end)
+ errx(1, "Invalid length for jail");
+
+ xp = (struct xprison_v1 *)p;
+ printf("%6d %-29.29s %.74s\n",
+ xp->pr_id, xp->pr_host, xp->pr_path);
+
+ /* We are not printing an empty line here for state and name. */
+ /* We are not printing an empty line here for cpusetid. */
+
+ /* IPv4 address. */
+ in.s_addr = htonl(xp->pr_ip);
+ printf("%6s %-15.15s\n", "", inet_ntoa(in));
+
+ return ((char *)(xp + 1));
+}
+#endif
+
+static
+char *print_xprison_v3(void *p, char *end, unsigned flags)
+{
+ struct xprison *xp;
+ struct in_addr *iap, in;
+ struct in6_addr *ia6p;
+ char buf[INET6_ADDRSTRLEN];
+ const char *state;
+ char *q;
+ uint32_t i;
+
+ if ((char *)p + sizeof(struct xprison) > end)
+ errx(1, "Invalid length for jail");
+ xp = (struct xprison *)p;
+
+ if (xp->pr_state < 0 || xp->pr_state > (int)
+ ((sizeof(prison_states) / sizeof(struct prison_state))))
+ state = "(bogus)";
+ else
+ state = prison_states[xp->pr_state].state_name;
+
+ /* See if we should print non-ACTIVE jails. No? */
+ if ((flags & FLAG_A) == 0 && strcmp(state, "ALIVE")) {
+ q = (char *)(xp + 1);
+ q += (xp->pr_ip4s * sizeof(struct in_addr));
+ if (q > end)
+ errx(1, "Invalid length for jail");
+ q += (xp->pr_ip6s * sizeof(struct in6_addr));
+ if (q > end)
+ errx(1, "Invalid length for jail");
+ return (q);
+ }
+
+ printf("%6d %-29.29s %.74s\n",
+ xp->pr_id, xp->pr_host, xp->pr_path);
+
+ /* Jail state and name. */
+ if (flags & FLAG_V)
+ printf("%6s %-29.29s %.74s\n",
+ "", (xp->pr_name != NULL) ? xp->pr_name : "", state);
+
+ /* cpusetid. */
+ if (flags & FLAG_V)
+ printf("%6s %-6d\n",
+ "", xp->pr_cpusetid);
+
+ q = (char *)(xp + 1);
+ /* IPv4 addresses. */
+ iap = (struct in_addr *)(void *)q;
+ q += (xp->pr_ip4s * sizeof(struct in_addr));
+ if (q > end)
+ errx(1, "Invalid length for jail");
+ for (i = 0; i < xp->pr_ip4s; i++) {
+ if (i == 0 || flags & FLAG_V) {
+ in.s_addr = iap[i].s_addr;
+ printf("%6s %-15.15s\n", "", inet_ntoa(in));
+ }
+ }
+ /* IPv6 addresses. */
+ ia6p = (struct in6_addr *)(void *)q;
+ q += (xp->pr_ip6s * sizeof(struct in6_addr));
+ if (q > end)
+ errx(1, "Invalid length for jail");
+ for (i = 0; i < xp->pr_ip6s; i++) {
+ if (flags & FLAG_V) {
+ inet_ntop(AF_INET6, &ia6p[i], buf, sizeof(buf));
+ printf("%6s %s\n", "", buf);
+ }
+ }
+
+ return (q);
+}
+
+static void
+usage(void)
+{
+
+ (void)fprintf(stderr, "usage: jls [-av]\n");
+ exit(1);
+}
int
-main(void)
+main(int argc, char *argv[])
{
- struct xprison *sxp, *xp;
- struct in_addr in;
- size_t i, len;
+ int ch, version;
+ unsigned flags;
+ size_t i, j, len;
+ void *p, *q;
+
+ flags = 0;
+ while ((ch = getopt(argc, argv, "av")) != -1) {
+ switch (ch) {
+ case 'a':
+ flags |= FLAG_A;
+ break;
+ case 'v':
+ flags |= FLAG_V;
+ break;
+ default:
+ usage();
+ }
+ }
+ argc -= optind;
+ argv += optind;
if (sysctlbyname("security.jail.list", NULL, &len, NULL, 0) == -1)
err(1, "sysctlbyname(): security.jail.list");
+ j = len;
for (i = 0; i < 4; i++) {
if (len <= 0)
exit(0);
- sxp = xp = malloc(len);
- if (sxp == NULL)
+ p = q = malloc(len);
+ if (p == NULL)
err(1, "malloc()");
- if (sysctlbyname("security.jail.list", xp, &len, NULL, 0) == -1) {
+ if (sysctlbyname("security.jail.list", q, &len, NULL, 0) == -1) {
if (errno == ENOMEM) {
- free(sxp);
- sxp = NULL;
+ free(p);
+ p = NULL;
+ len += j;
continue;
}
err(1, "sysctlbyname(): security.jail.list");
}
break;
}
- if (sxp == NULL)
+ if (p == NULL)
err(1, "sysctlbyname(): security.jail.list");
- if (len < sizeof(*xp) || len % sizeof(*xp) ||
- xp->pr_version != XPRISON_VERSION)
- errx(1, "Kernel and userland out of sync");
-
- printf(" JID IP Address Hostname Path\n");
- for (i = 0; i < len / sizeof(*xp); i++) {
- in.s_addr = ntohl(xp->pr_ip);
- printf("%6d %-15.15s %-29.29s %.74s\n",
- xp->pr_id, inet_ntoa(in), xp->pr_host, xp->pr_path);
- xp++;
+ if (len < sizeof(int))
+ errx(1, "This is no prison. Kernel and userland out of sync?");
+ version = *(int *)p;
+ if (version > XPRISON_VERSION)
+ errx(1, "Sci-Fi prison. Kernel/userland out of sync?");
+
+ printf(" JID Hostname Path\n");
+ if (flags & FLAG_V) {
+ printf(" Name State\n");
+ printf(" CPUSetID\n");
+ }
+ printf(" IP Address(es)\n");
+ for (; q != NULL && (char *)q + sizeof(int) < (char *)p + len;) {
+ version = *(int *)q;
+ if (version > XPRISON_VERSION)
+ errx(1, "Sci-Fi prison. Kernel/userland out of sync?");
+ switch (version) {
+#ifdef SUPPORT_OLD_XPRISON
+ case 1:
+ q = print_xprison_v1(q, (char *)p + len);
+ break;
+ case 2:
+ errx(1, "Version 2 was used by multi-IPv4 jail "
+ "implementations that never made it into the "
+ "official kernel.");
+ /* NOTREACHED */
+ break;
+#endif
+ case 3:
+ q = print_xprison_v3(q, (char *)p + len, flags);
+ break;
+ default:
+ errx(1, "Prison unknown. Kernel/userland out of sync?");
+ /* NOTREACHED */
+ break;
+ }
}
- free(sxp);
+
+ free(p);
exit(0);
}
OpenPOWER on IntegriCloud