1999-05-04 18:20:53 +00:00
|
|
|
/*
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
* "THE BEER-WARE LICENSE" (Revision 42):
|
|
|
|
* <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you
|
|
|
|
* can do whatever you want with this stuff. If we meet some day, and you think
|
|
|
|
* this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2003-07-06 12:44:11 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2003-03-27 12:16:58 +00:00
|
|
|
#include <sys/param.h>
|
This Implements the mumbled about "Jail" feature.
This is a seriously beefed up chroot kind of thing. The process
is jailed along the same lines as a chroot does it, but with
additional tough restrictions imposed on what the superuser can do.
For all I know, it is safe to hand over the root bit inside a
prison to the customer living in that prison, this is what
it was developed for in fact: "real virtual servers".
Each prison has an ip number associated with it, which all IP
communications will be coerced to use and each prison has its own
hostname.
Needless to say, you need more RAM this way, but the advantage is
that each customer can run their own particular version of apache
and not stomp on the toes of their neighbors.
It generally does what one would expect, but setting up a jail
still takes a little knowledge.
A few notes:
I have no scripts for setting up a jail, don't ask me for them.
The IP number should be an alias on one of the interfaces.
mount a /proc in each jail, it will make ps more useable.
/proc/<pid>/status tells the hostname of the prison for
jailed processes.
Quotas are only sensible if you have a mountpoint per prison.
There are no privisions for stopping resource-hogging.
Some "#ifdef INET" and similar may be missing (send patches!)
If somebody wants to take it from here and develop it into
more of a "virtual machine" they should be most welcome!
Tools, comments, patches & documentation most welcome.
Have fun...
Sponsored by: http://www.rndassociates.com/
Run for almost a year by: http://www.servetheweb.com/
1999-04-28 11:38:52 +00:00
|
|
|
#include <sys/jail.h>
|
2001-06-24 20:28:19 +00:00
|
|
|
|
This Implements the mumbled about "Jail" feature.
This is a seriously beefed up chroot kind of thing. The process
is jailed along the same lines as a chroot does it, but with
additional tough restrictions imposed on what the superuser can do.
For all I know, it is safe to hand over the root bit inside a
prison to the customer living in that prison, this is what
it was developed for in fact: "real virtual servers".
Each prison has an ip number associated with it, which all IP
communications will be coerced to use and each prison has its own
hostname.
Needless to say, you need more RAM this way, but the advantage is
that each customer can run their own particular version of apache
and not stomp on the toes of their neighbors.
It generally does what one would expect, but setting up a jail
still takes a little knowledge.
A few notes:
I have no scripts for setting up a jail, don't ask me for them.
The IP number should be an alias on one of the interfaces.
mount a /proc in each jail, it will make ps more useable.
/proc/<pid>/status tells the hostname of the prison for
jailed processes.
Quotas are only sensible if you have a mountpoint per prison.
There are no privisions for stopping resource-hogging.
Some "#ifdef INET" and similar may be missing (send patches!)
If somebody wants to take it from here and develop it into
more of a "virtual machine" they should be most welcome!
Tools, comments, patches & documentation most welcome.
Have fun...
Sponsored by: http://www.rndassociates.com/
Run for almost a year by: http://www.servetheweb.com/
1999-04-28 11:38:52 +00:00
|
|
|
#include <netinet/in.h>
|
2001-06-24 20:28:19 +00:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
2004-05-29 18:39:27 +00:00
|
|
|
#include <errno.h>
|
2003-03-27 12:16:58 +00:00
|
|
|
#include <grp.h>
|
|
|
|
#include <login_cap.h>
|
2004-08-15 08:21:50 +00:00
|
|
|
#include <paths.h>
|
2003-03-27 12:16:58 +00:00
|
|
|
#include <pwd.h>
|
2001-06-24 20:28:19 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
This Implements the mumbled about "Jail" feature.
This is a seriously beefed up chroot kind of thing. The process
is jailed along the same lines as a chroot does it, but with
additional tough restrictions imposed on what the superuser can do.
For all I know, it is safe to hand over the root bit inside a
prison to the customer living in that prison, this is what
it was developed for in fact: "real virtual servers".
Each prison has an ip number associated with it, which all IP
communications will be coerced to use and each prison has its own
hostname.
Needless to say, you need more RAM this way, but the advantage is
that each customer can run their own particular version of apache
and not stomp on the toes of their neighbors.
It generally does what one would expect, but setting up a jail
still takes a little knowledge.
A few notes:
I have no scripts for setting up a jail, don't ask me for them.
The IP number should be an alias on one of the interfaces.
mount a /proc in each jail, it will make ps more useable.
/proc/<pid>/status tells the hostname of the prison for
jailed processes.
Quotas are only sensible if you have a mountpoint per prison.
There are no privisions for stopping resource-hogging.
Some "#ifdef INET" and similar may be missing (send patches!)
If somebody wants to take it from here and develop it into
more of a "virtual machine" they should be most welcome!
Tools, comments, patches & documentation most welcome.
Have fun...
Sponsored by: http://www.rndassociates.com/
Run for almost a year by: http://www.servetheweb.com/
1999-04-28 11:38:52 +00:00
|
|
|
|
2003-03-27 12:16:58 +00:00
|
|
|
static void usage(void);
|
2004-08-15 08:21:50 +00:00
|
|
|
extern char **environ;
|
2003-03-27 12:16:58 +00:00
|
|
|
|
2004-05-29 18:39:27 +00:00
|
|
|
#define GET_USER_INFO do { \
|
|
|
|
pwd = getpwnam(username); \
|
|
|
|
if (pwd == NULL) { \
|
|
|
|
if (errno) \
|
|
|
|
err(1, "getpwnam: %s", username); \
|
|
|
|
else \
|
|
|
|
errx(1, "%s: no such user", username); \
|
|
|
|
} \
|
|
|
|
lcap = login_getpwclass(pwd); \
|
|
|
|
if (lcap == NULL) \
|
|
|
|
err(1, "getpwclass: %s", username); \
|
|
|
|
ngroups = NGROUPS; \
|
|
|
|
if (getgrouplist(username, pwd->pw_gid, groups, &ngroups) != 0) \
|
|
|
|
err(1, "getgrouplist: %s", username); \
|
|
|
|
} while (0)
|
|
|
|
|
This Implements the mumbled about "Jail" feature.
This is a seriously beefed up chroot kind of thing. The process
is jailed along the same lines as a chroot does it, but with
additional tough restrictions imposed on what the superuser can do.
For all I know, it is safe to hand over the root bit inside a
prison to the customer living in that prison, this is what
it was developed for in fact: "real virtual servers".
Each prison has an ip number associated with it, which all IP
communications will be coerced to use and each prison has its own
hostname.
Needless to say, you need more RAM this way, but the advantage is
that each customer can run their own particular version of apache
and not stomp on the toes of their neighbors.
It generally does what one would expect, but setting up a jail
still takes a little knowledge.
A few notes:
I have no scripts for setting up a jail, don't ask me for them.
The IP number should be an alias on one of the interfaces.
mount a /proc in each jail, it will make ps more useable.
/proc/<pid>/status tells the hostname of the prison for
jailed processes.
Quotas are only sensible if you have a mountpoint per prison.
There are no privisions for stopping resource-hogging.
Some "#ifdef INET" and similar may be missing (send patches!)
If somebody wants to take it from here and develop it into
more of a "virtual machine" they should be most welcome!
Tools, comments, patches & documentation most welcome.
Have fun...
Sponsored by: http://www.rndassociates.com/
Run for almost a year by: http://www.servetheweb.com/
1999-04-28 11:38:52 +00:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2003-03-27 12:16:58 +00:00
|
|
|
login_cap_t *lcap;
|
This Implements the mumbled about "Jail" feature.
This is a seriously beefed up chroot kind of thing. The process
is jailed along the same lines as a chroot does it, but with
additional tough restrictions imposed on what the superuser can do.
For all I know, it is safe to hand over the root bit inside a
prison to the customer living in that prison, this is what
it was developed for in fact: "real virtual servers".
Each prison has an ip number associated with it, which all IP
communications will be coerced to use and each prison has its own
hostname.
Needless to say, you need more RAM this way, but the advantage is
that each customer can run their own particular version of apache
and not stomp on the toes of their neighbors.
It generally does what one would expect, but setting up a jail
still takes a little knowledge.
A few notes:
I have no scripts for setting up a jail, don't ask me for them.
The IP number should be an alias on one of the interfaces.
mount a /proc in each jail, it will make ps more useable.
/proc/<pid>/status tells the hostname of the prison for
jailed processes.
Quotas are only sensible if you have a mountpoint per prison.
There are no privisions for stopping resource-hogging.
Some "#ifdef INET" and similar may be missing (send patches!)
If somebody wants to take it from here and develop it into
more of a "virtual machine" they should be most welcome!
Tools, comments, patches & documentation most welcome.
Have fun...
Sponsored by: http://www.rndassociates.com/
Run for almost a year by: http://www.servetheweb.com/
1999-04-28 11:38:52 +00:00
|
|
|
struct jail j;
|
2003-03-27 12:16:58 +00:00
|
|
|
struct passwd *pwd;
|
This Implements the mumbled about "Jail" feature.
This is a seriously beefed up chroot kind of thing. The process
is jailed along the same lines as a chroot does it, but with
additional tough restrictions imposed on what the superuser can do.
For all I know, it is safe to hand over the root bit inside a
prison to the customer living in that prison, this is what
it was developed for in fact: "real virtual servers".
Each prison has an ip number associated with it, which all IP
communications will be coerced to use and each prison has its own
hostname.
Needless to say, you need more RAM this way, but the advantage is
that each customer can run their own particular version of apache
and not stomp on the toes of their neighbors.
It generally does what one would expect, but setting up a jail
still takes a little knowledge.
A few notes:
I have no scripts for setting up a jail, don't ask me for them.
The IP number should be an alias on one of the interfaces.
mount a /proc in each jail, it will make ps more useable.
/proc/<pid>/status tells the hostname of the prison for
jailed processes.
Quotas are only sensible if you have a mountpoint per prison.
There are no privisions for stopping resource-hogging.
Some "#ifdef INET" and similar may be missing (send patches!)
If somebody wants to take it from here and develop it into
more of a "virtual machine" they should be most welcome!
Tools, comments, patches & documentation most welcome.
Have fun...
Sponsored by: http://www.rndassociates.com/
Run for almost a year by: http://www.servetheweb.com/
1999-04-28 11:38:52 +00:00
|
|
|
struct in_addr in;
|
2004-08-15 08:21:50 +00:00
|
|
|
int ch, groups[NGROUPS], i, iflag, lflag, ngroups, uflag, Uflag;
|
2004-06-27 10:10:16 +00:00
|
|
|
char path[PATH_MAX], *username;
|
2004-08-15 08:21:50 +00:00
|
|
|
static char *cleanenv;
|
|
|
|
const char *shell, *p;
|
2003-03-27 12:16:58 +00:00
|
|
|
|
2004-08-15 08:21:50 +00:00
|
|
|
iflag = lflag = uflag = Uflag = 0;
|
|
|
|
username = cleanenv = NULL;
|
This Implements the mumbled about "Jail" feature.
This is a seriously beefed up chroot kind of thing. The process
is jailed along the same lines as a chroot does it, but with
additional tough restrictions imposed on what the superuser can do.
For all I know, it is safe to hand over the root bit inside a
prison to the customer living in that prison, this is what
it was developed for in fact: "real virtual servers".
Each prison has an ip number associated with it, which all IP
communications will be coerced to use and each prison has its own
hostname.
Needless to say, you need more RAM this way, but the advantage is
that each customer can run their own particular version of apache
and not stomp on the toes of their neighbors.
It generally does what one would expect, but setting up a jail
still takes a little knowledge.
A few notes:
I have no scripts for setting up a jail, don't ask me for them.
The IP number should be an alias on one of the interfaces.
mount a /proc in each jail, it will make ps more useable.
/proc/<pid>/status tells the hostname of the prison for
jailed processes.
Quotas are only sensible if you have a mountpoint per prison.
There are no privisions for stopping resource-hogging.
Some "#ifdef INET" and similar may be missing (send patches!)
If somebody wants to take it from here and develop it into
more of a "virtual machine" they should be most welcome!
Tools, comments, patches & documentation most welcome.
Have fun...
Sponsored by: http://www.rndassociates.com/
Run for almost a year by: http://www.servetheweb.com/
1999-04-28 11:38:52 +00:00
|
|
|
|
2004-08-15 08:21:50 +00:00
|
|
|
while ((ch = getopt(argc, argv, "ilu:U:")) != -1) {
|
2003-03-27 12:16:58 +00:00
|
|
|
switch (ch) {
|
2003-04-09 03:04:12 +00:00
|
|
|
case 'i':
|
|
|
|
iflag = 1;
|
|
|
|
break;
|
2003-03-27 12:16:58 +00:00
|
|
|
case 'u':
|
|
|
|
username = optarg;
|
2004-05-29 18:39:27 +00:00
|
|
|
uflag = 1;
|
|
|
|
break;
|
|
|
|
case 'U':
|
|
|
|
username = optarg;
|
|
|
|
Uflag = 1;
|
2003-03-27 12:16:58 +00:00
|
|
|
break;
|
2004-08-15 08:21:50 +00:00
|
|
|
case 'l':
|
|
|
|
lflag = 1;
|
|
|
|
break;
|
2003-03-27 12:16:58 +00:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
2003-04-09 03:04:12 +00:00
|
|
|
}
|
2003-03-27 12:16:58 +00:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
if (argc < 4)
|
|
|
|
usage();
|
2004-05-29 18:39:27 +00:00
|
|
|
if (uflag && Uflag)
|
|
|
|
usage();
|
2004-08-15 08:21:50 +00:00
|
|
|
if (lflag && username == NULL)
|
|
|
|
usage();
|
2004-05-29 18:39:27 +00:00
|
|
|
if (uflag)
|
|
|
|
GET_USER_INFO;
|
2004-06-27 10:10:16 +00:00
|
|
|
if (realpath(argv[0], path) == NULL)
|
|
|
|
err(1, "realpath: %s", argv[0]);
|
|
|
|
if (chdir(path) != 0)
|
|
|
|
err(1, "chdir: %s", path);
|
1999-09-19 08:36:37 +00:00
|
|
|
memset(&j, 0, sizeof(j));
|
|
|
|
j.version = 0;
|
2004-06-27 10:10:16 +00:00
|
|
|
j.path = path;
|
2003-03-27 12:16:58 +00:00
|
|
|
j.hostname = argv[1];
|
2003-04-02 09:20:08 +00:00
|
|
|
if (inet_aton(argv[2], &in) == 0)
|
|
|
|
errx(1, "Could not make sense of ip-number: %s", argv[2]);
|
1999-05-04 18:20:53 +00:00
|
|
|
j.ip_number = ntohl(in.s_addr);
|
2003-04-09 03:04:12 +00:00
|
|
|
i = jail(&j);
|
|
|
|
if (i == -1)
|
2003-04-02 09:20:08 +00:00
|
|
|
err(1, "jail");
|
2003-04-21 17:20:48 +00:00
|
|
|
if (iflag) {
|
2003-04-09 03:04:12 +00:00
|
|
|
printf("%d\n", i);
|
2003-04-21 17:20:48 +00:00
|
|
|
fflush(stdout);
|
|
|
|
}
|
2003-03-27 12:16:58 +00:00
|
|
|
if (username != NULL) {
|
2004-05-29 18:39:27 +00:00
|
|
|
if (Uflag)
|
|
|
|
GET_USER_INFO;
|
2004-08-15 08:21:50 +00:00
|
|
|
if (lflag) {
|
|
|
|
p = getenv("TERM");
|
|
|
|
environ = &cleanenv;
|
|
|
|
}
|
2003-04-02 09:20:08 +00:00
|
|
|
if (setgroups(ngroups, groups) != 0)
|
|
|
|
err(1, "setgroups");
|
|
|
|
if (setgid(pwd->pw_gid) != 0)
|
|
|
|
err(1, "setgid");
|
|
|
|
if (setusercontext(lcap, pwd, pwd->pw_uid,
|
|
|
|
LOGIN_SETALL & ~LOGIN_SETGROUP) != 0)
|
|
|
|
err(1, "setusercontext");
|
2003-04-07 10:16:37 +00:00
|
|
|
login_close(lcap);
|
2003-03-27 12:16:58 +00:00
|
|
|
}
|
2004-08-15 08:21:50 +00:00
|
|
|
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);
|
|
|
|
}
|
2003-04-02 09:20:08 +00:00
|
|
|
if (execv(argv[3], argv + 3) != 0)
|
|
|
|
err(1, "execv: %s", argv[3]);
|
2003-04-09 03:04:12 +00:00
|
|
|
exit(0);
|
This Implements the mumbled about "Jail" feature.
This is a seriously beefed up chroot kind of thing. The process
is jailed along the same lines as a chroot does it, but with
additional tough restrictions imposed on what the superuser can do.
For all I know, it is safe to hand over the root bit inside a
prison to the customer living in that prison, this is what
it was developed for in fact: "real virtual servers".
Each prison has an ip number associated with it, which all IP
communications will be coerced to use and each prison has its own
hostname.
Needless to say, you need more RAM this way, but the advantage is
that each customer can run their own particular version of apache
and not stomp on the toes of their neighbors.
It generally does what one would expect, but setting up a jail
still takes a little knowledge.
A few notes:
I have no scripts for setting up a jail, don't ask me for them.
The IP number should be an alias on one of the interfaces.
mount a /proc in each jail, it will make ps more useable.
/proc/<pid>/status tells the hostname of the prison for
jailed processes.
Quotas are only sensible if you have a mountpoint per prison.
There are no privisions for stopping resource-hogging.
Some "#ifdef INET" and similar may be missing (send patches!)
If somebody wants to take it from here and develop it into
more of a "virtual machine" they should be most welcome!
Tools, comments, patches & documentation most welcome.
Have fun...
Sponsored by: http://www.rndassociates.com/
Run for almost a year by: http://www.servetheweb.com/
1999-04-28 11:38:52 +00:00
|
|
|
}
|
2003-03-27 12:16:58 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
|
2004-05-29 18:39:27 +00:00
|
|
|
(void)fprintf(stderr, "%s%s\n",
|
2004-08-15 08:21:50 +00:00
|
|
|
"usage: jail [-i] [-l -u username | -U username]",
|
2004-05-29 18:39:27 +00:00
|
|
|
" path hostname ip-number command ...");
|
2003-04-02 09:20:08 +00:00
|
|
|
exit(1);
|
2003-03-27 12:16:58 +00:00
|
|
|
}
|