bsd-jail-object/Object.pm
author mahlon
Fri, 05 Sep 2008 05:15:48 +0000
branchperl-modules
changeset 0 315eb12b224b
permissions -rw-r--r--
Reorganizing for Trac.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
     1
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
     2
package BSD::Jail::Object;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
     3
use strict;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
     4
use warnings;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
     5
use vars qw/ @ISA @EXPORT_OK /;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
     6
use Exporter;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
     7
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
     8
our $VERSION = '0.02';
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
     9
@ISA         = qw/ Exporter /;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    10
@EXPORT_OK   = qw/ jids /;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    11
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    12
use Inline C       => 'DATA',
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    13
           NAME    => 'BSD::Jail::Object',
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    14
           VERSION => '0.02';
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    15
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    16
sub new
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    17
{
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    18
    my ( $class, $opts ) = @_;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    19
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    20
    my $self = {};
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    21
    bless $self, $class;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    22
    return $self unless $opts;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    23
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    24
    if ( ref $opts eq 'HASH' ) {
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    25
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    26
        # create a new jail
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    27
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    28
        if ( $< ) {
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    29
            $@ = "jail() requires root";
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    30
            return;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    31
        }
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    32
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    33
        unless ( $opts->{'path'}     &&
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    34
                 $opts->{'hostname'} &&
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    35
                 $opts->{'ip'} ) {
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    36
            $@ = "Missing arguments to create() - need 'path', 'hostname', and 'ip'";
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    37
            return;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    38
        }
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    39
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    40
        my $jid = _create( $opts->{'path'}, $opts->{'hostname'}, $opts->{'ip'} )
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    41
            or return;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    42
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    43
        $self->{'_data'} = [
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    44
            $jid, $opts->{'ip'}, $opts->{'hostname'}, $opts->{'path'}
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    45
        ];
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    46
    
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    47
        return $self;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    48
    }
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    49
    else {
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    50
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    51
        # this object should be linked to an existing jail
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    52
        return $self->_init( $opts );
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    53
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    54
    }
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    55
}
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    56
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    57
sub _init
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    58
{
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    59
    my $self = shift;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    60
    my $key  = shift;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    61
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    62
    return unless $key;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    63
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    64
    my ( @data, $type );
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    65
    if ( $key =~ /^\d+$/ ) {
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    66
        $type = 'jid';
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    67
        @data = _find_jail( 0, $key );
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    68
    }
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    69
    elsif ( $key =~ /^\d+\.\d+\.\d+\.\d+$/ ) {
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    70
        $type = 'ip';
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    71
        @data = _find_jail( 1, $key );
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    72
    }
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    73
    else {
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    74
        $type = 'hostname';
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    75
        @data = _find_jail( 2, $key );
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    76
    }
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    77
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    78
    unless ( scalar @data ) {
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    79
        $@ = "No such jail $type: $key";
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    80
        return;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    81
    }
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    82
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    83
    $self->{'_data'} = \@data;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    84
    return $self;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    85
}
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    86
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    87
sub jid       { shift()->{'_data'}->[0] }
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    88
sub ip        { shift()->{'_data'}->[1] }
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    89
sub hostname  { shift()->{'_data'}->[2] }
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    90
sub path      { shift()->{'_data'}->[3] }
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    91
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    92
sub attach
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    93
{
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    94
    my $self = shift;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    95
    return unless $self->jid;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    96
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    97
    if ( $< ) {
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    98
        $@ = "jail_attach() requires root";
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
    99
        return;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   100
    }
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   101
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   102
    return _attach( $self->jid );
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   103
}
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   104
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   105
sub jids
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   106
{
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   107
    return if ref $_[0]; # shouldn't be used as an object method
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   108
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   109
    my %opts = @_;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   110
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   111
    my @jids = _find_jids();
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   112
    return @jids unless $opts{'instantiate'};
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   113
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   114
    map { $_ = __PACKAGE__->new( $_ ) } @jids;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   115
    return @jids;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   116
}
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   117
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   118
1;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   119
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   120
__DATA__
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   121
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   122
=pod
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   123
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   124
=head1 DESCRIPTION
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   125
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   126
This is an object oriented wrapper around the FreeBSD jail subsystem.
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   127
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   128
A 5.x or higher FreeBSD system is required.
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   129
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   130
=head1 SYNOPSIS
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   131
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   132
Here is an exact replica of the 'jls' utility in just a few lines of perl:
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   133
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   134
 use BSD::Jail::Object 'jids';
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   135
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   136
 print "   JID  IP Address      Hostname                      Path\n";
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   137
 printf "%6d  %-15.15s %-29.29s %.74s\n",
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   138
        $_->jid, $_->ip, $_->hostname, $_->path foreach jids( instantiate => 1 );
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   139
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   140
And here's 'jexec' (actually, a jexec that lets you optionally select by
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   141
something other than jid):
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   142
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   143
 my $j = BSD::Jail::Object->new( $ARGV[0] ) or die $@;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   144
 $j->attach && chdir('/') && exec $ARGV[1] or exit;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   145
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   146
=head1 EXAMPLES
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   147
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   148
=over 4
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   149
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   150
=item B<Create a new jail>
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   151
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   152
 $options = {
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   153
     path     => '/tmp',
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   154
     ip       => '127.0.0.1',
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   155
     hostname => 'example.com'
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   156
 };
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   157
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   158
 $j = BSD::Jail::Object->new( $options ) or die $@;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   159
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   160
=item B<Attach to an existing jail>
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   161
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   162
 $j = BSD::Jail::Object->new( 'example.com' );
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   163
 $j->attach;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   164
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   165
=item B<Do something in all jails>
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   166
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   167
 foreach $j ( jids(instantiate => 1) ) {
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   168
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   169
     if ( fork ) {
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   170
         $j->attach;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   171
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   172
         #
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   173
         # do something exciting
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   174
         #
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   175
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   176
         exit;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   177
     }
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   178
 }
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   179
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   180
=item B<Get information on a jail>
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   181
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   182
(See the B<SYNOPSIS> section above)
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   183
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   184
=back
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   185
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   186
=head1 OBJECT METHODS
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   187
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   188
=head2 new()
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   189
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   190
Instantiate a new BSD::Jail::Object object, either by associating
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   191
ourselves with an already running jail, or by creating a new one from
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   192
scratch.
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   193
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   194
To associate with an already active jail, I<new()> accepts a jid,
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   195
hostname, or ip address.  Errors are placed into $@.
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   196
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   197
 # existing jail, find by jid
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   198
 $j = BSD::Jail::Object->new( 23 ) or die $@;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   199
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   200
 # existing jail, find by hostname
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   201
 $j = BSD::Jail::Object->new( 'example.com' ) or die $@;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   202
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   203
 # existing jail, find by ip address
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   204
 $j = BSD::Jail::Object->new( '127.0.0.1' ) or die $@;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   205
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   206
Note that if you're selecting a jail by hostname or IP, those aren't
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   207
always unique values.  Two jails could be running with the same hostname
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   208
or IP address - this module will always select the highest numbered jid
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   209
in that case.  If you need to be sure you're in the 'right' jail when
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   210
there are duplicates, select by JID.
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   211
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   212
Create a new jail by passing a hash reference.  Required keys are
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   213
'hostname', 'ip', and 'path'.  See the I<jail(8)> man page for specifics
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   214
on these keys.
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   215
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   216
 # create a new jail under /tmp
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   217
 $j = BSD::Jail::Object->new({
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   218
        hostname => 'example.com',
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   219
        ip       => '127.0.0.1',
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   220
        path     => '/tmp'
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   221
 }) or die $@;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   222
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   223
=head2 jid()
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   224
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   225
Get the current jail identifier.  JIDs are assigned sequentially from
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   226
the kernel.
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   227
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   228
=head2 hostname()
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   229
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   230
Get the current jail hostname.
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   231
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   232
=head2 path()
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   233
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   234
Get the root path the jail was bound to.
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   235
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   236
=head2 attach()
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   237
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   238
Imprison ourselves within a jail.  Note that this generally requires
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   239
root access, and is a one way operation.  Once the script process
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   240
is imprisioned, there is no way to perform a jailbreak!  You'd need
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   241
to I<fork()> if you intended to attach to more than one jail.  See
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   242
I<EXAMPLES>.
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   243
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   244
=head1 EXPORTABLE METHODS
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   245
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   246
=head2 jids()
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   247
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   248
Returns an array of active JIDs.  Can also return them as
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   249
pre-instantiated objects by passing 'instantiate => 1' as an argument.
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   250
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   251
 my @jail_jids    = jids();
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   252
 my @jail_objects = jids( instantiate => 1 );
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   253
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   254
Only exported upon request.
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   255
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   256
=head1 ACKNOWLEDGEMENTS
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   257
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   258
Most of the jail specific C code was based on work 
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   259
by Mike Barcroft <mike@freebsd.org> and Poul-Henning Kamp <phk@freebsd.org>
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   260
for the FreeBSD Project.
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   261
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   262
=head1 AUTHOR
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   263
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   264
Mahlon E. Smith I<mahlon@martini.nu> for Spime Solutions Group
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   265
I<(www.spime.net)>
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   266
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   267
=cut
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   268
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   269
__C__
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   270
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   271
#include <stdio.h>
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   272
#include <stdlib.h>
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   273
#include <unistd.h>
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   274
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   275
#include <arpa/inet.h>
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   276
#include <errno.h>
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   277
#include <limits.h>
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   278
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   279
#include <sys/param.h>
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   280
#include <sys/jail.h>
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   281
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   282
size_t
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   283
sysctl_len()
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   284
{
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   285
    size_t len;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   286
    if ( sysctlbyname( "security.jail.list", NULL, &len, NULL, 0 ) == -1 ) return 0;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   287
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   288
    return len;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   289
}
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   290
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   291
// get jail structure from kernel
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   292
struct xprison
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   293
*get_xp()
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   294
{
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   295
    struct xprison *sxp, *xp;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   296
    size_t len;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   297
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   298
    len = sysctl_len();
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   299
    if ( len <= 0 ) return NULL;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   300
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   301
    sxp = xp = malloc(len);
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   302
    if ( sxp == NULL ) return NULL;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   303
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   304
    // populate the xprison list
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   305
    if ( sysctlbyname( "security.jail.list", xp, &len, NULL, 0 ) == -1 ) {
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   306
        if (errno == ENOMEM) {
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   307
            free( sxp );
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   308
            return NULL;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   309
        }
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   310
        return NULL;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   311
    }
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   312
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   313
    // check if kernel and userland is in sync
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   314
    if ( len < sizeof(*xp) || len % sizeof(*xp) ||
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   315
            xp->pr_version != XPRISON_VERSION ) {
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   316
        warn("%s", "Kernel out of sync with userland");
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   317
        return NULL;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   318
    }
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   319
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   320
    free( sxp );
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   321
    return xp;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   322
}
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   323
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   324
// fetch a specific jail's information
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   325
void
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   326
_find_jail( int compare, char *string )
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   327
{ 
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   328
    struct xprison *xp;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   329
    struct in_addr in;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   330
    size_t i, len;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   331
    Inline_Stack_Vars;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   332
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   333
    Inline_Stack_Reset;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   334
    xp  = get_xp();
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   335
    len = sysctl_len();
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   336
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   337
    /*
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   338
       compare == 0    jid
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   339
       compare == 1    ip address
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   340
       compare == 2    hostname
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   341
    */
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   342
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   343
    for (i = 0; i < len / sizeof(*xp); i++) {
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   344
        in.s_addr = ntohl(xp->pr_ip);
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   345
        if (
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   346
                ( compare == 0 && xp->pr_id == atoi(string) )
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   347
                ||
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   348
                ( compare == 1 && strcmp( string, inet_ntoa(in) ) == 0 )
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   349
                ||
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   350
                ( compare == 2 && strcmp( string, xp->pr_host ) == 0 )
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   351
           ) {
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   352
            Inline_Stack_Push( sv_2mortal( newSViv( xp->pr_id ) ));
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   353
            Inline_Stack_Push( sv_2mortal( newSVpvf( inet_ntoa(in) ) ));
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   354
            Inline_Stack_Push( sv_2mortal( newSVpvf( xp->pr_host ) ));
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   355
            Inline_Stack_Push( sv_2mortal( newSVpvf( xp->pr_path ) ));
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   356
            break;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   357
        }
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   358
        else {
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   359
            xp++;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   360
        }
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   361
    }
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   362
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   363
    Inline_Stack_Done;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   364
}
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   365
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   366
// return an array of all current jail ids
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   367
void
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   368
_find_jids()
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   369
{ 
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   370
    struct xprison *xp;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   371
    size_t i, len;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   372
    Inline_Stack_Vars;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   373
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   374
    Inline_Stack_Reset;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   375
    xp  = get_xp();
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   376
    len = sysctl_len();
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   377
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   378
    for (i = 0; i < len / sizeof(*xp); i++) {
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   379
        Inline_Stack_Push( sv_2mortal( newSViv( xp->pr_id ) ));
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   380
        xp++;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   381
    }
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   382
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   383
    Inline_Stack_Done;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   384
}
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   385
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   386
// attach to a jail
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   387
int
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   388
_attach( int jid )
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   389
{
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   390
    return ( jail_attach(jid) == -1 ? 0 : 1 );
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   391
}
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   392
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   393
// create a new jail
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   394
int
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   395
_create( char *path, char *hostname, char *ipaddr )
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   396
{
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   397
    struct in_addr ip;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   398
    struct jail    j;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   399
    int            jid;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   400
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   401
    if ( inet_aton( ipaddr, &ip ) == 0 ) return 0;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   402
    
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   403
    j.path      = path;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   404
    j.hostname  = hostname;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   405
    j.ip_number = ntohl( ip.s_addr );
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   406
    j.version   = 0;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   407
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   408
    if ( (jid = jail( &j )) == -1 ) return 0;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   409
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   410
    return jid;
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   411
}
315eb12b224b Reorganizing for Trac.
mahlon
parents:
diff changeset
   412