This is an object oriented wrapper around the FreeBSD jail subsystem.
A 5.x or higher FreeBSD system is required.
Here is an exact replica of the 'jls' utility in just a few lines of perl:
use BSD::Jail::Object 'jids'; print " JID IP Address Hostname Path\n"; printf "%6d %-15.15s %-29.29s %.74s\n", $_->jid, $_->ip, $_->hostname, $_->path foreach jids( instantiate => 1 );
And here's 'jexec' (actually, a jexec that lets you optionally select by something other than jid):
my $j = BSD::Jail::Object->new( $ARGV[0] ) or die $@; $j->attach && chdir('/') && exec $ARGV[1] or exit;
$options = { path => '/tmp', ip => '127.0.0.1', hostname => 'example.com' }; $j = BSD::Jail::Object->new( $options ) or die $@;
$j = BSD::Jail::Object->new( 'example.com' ); $j->attach;
foreach $j ( jids(instantiate => 1) ) { if ( fork ) { $j->attach; # # do something exciting # exit; } }
(See the SYNOPSIS section above)
Instantiate a new BSD::Jail::Object object, either by associating ourselves with an already running jail, or by creating a new one from scratch.
To associate with an already active jail, new() accepts a jid, hostname, or ip address. Errors are placed into $@.
# existing jail, find by jid $j = BSD::Jail::Object->new( 23 ) or die $@; # existing jail, find by hostname $j = BSD::Jail::Object->new( 'example.com' ) or die $@; # existing jail, find by ip address $j = BSD::Jail::Object->new( '127.0.0.1' ) or die $@;
Note that if you're selecting a jail by hostname or IP, those aren't always unique values. Two jails could be running with the same hostname or IP address - this module will always select the highest numbered jid in that case. If you need to be sure you're in the 'right' jail when there are duplicates, select by JID.
Create a new jail by passing a hash reference. Required keys are 'hostname', 'ip', and 'path'. See the jail(8) man page for specifics on these keys.
# create a new jail under /tmp $j = BSD::Jail::Object->new({ hostname => 'example.com', ip => '127.0.0.1', path => '/tmp' }) or die $@;
Get the current jail identifier. JIDs are assigned sequentially from the kernel.
Get the current jail hostname.
Get the root path the jail was bound to.
Imprison ourselves within a jail. Note that this generally requires root access, and is a one way operation. Once the script process is imprisioned, there is no way to perform a jailbreak! You'd need to fork() if you intended to attach to more than one jail. See EXAMPLES.
Returns an array of active JIDs. Can also return them as pre-instantiated objects by passing 'instantiate => 1' as an argument.
my @jail_jids = jids(); my @jail_objects = jids( instantiate => 1 );
Only exported upon request.
Most of the jail specific C code was based on work by Mike Barcroft <mike@freebsd.org> and Poul-Henning Kamp <phk@freebsd.org> for the FreeBSD Project.
Mahlon E. Smith mahlon@martini.nu for Spime Solutions Group (www.spime.net)
To install BSD::Jail::Object, copy and paste the appropriate command in to your terminal.
cpanm
cpanm BSD::Jail::Object
CPAN shell
perl -MCPAN -e shell install BSD::Jail::Object
For more information on module installation, please visit the detailed CPAN module installation guide.