~ubuntu-branches/ubuntu/lucid/sbuild/lucid

« back to all changes in this revision

Viewing changes to lib/Sbuild/ChrootPlain.pm

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook
  • Date: 2009-05-09 16:06:44 UTC
  • mfrom: (8.1.6 upstream) (3.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090509160644-9k0fgp6c2ajcu54h
Tags: 0.58.2-1ubuntu1
* Merge from debian unstable, remaining changes:
  - bin/sbuild, lib/Sbuild/{Base,Conf,Options}.pm: add --setup-hook
    to allow pre-build modifications to underlying chroots (needed
    to adjust pockets and components in sources.list).  (debian bug
    500746).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Chroot.pm: chroot library for sbuild
 
3
# Copyright © 2005      Ryan Murray <rmurray@debian.org>
 
4
# Copyright © 2005-2006 Roger Leigh <rleigh@debian.org>
 
5
# Copyright © 2008      Simon McVittie <smcv@debian.org>
 
6
#
 
7
# This program is free software: you can redistribute it and/or modify
 
8
# it under the terms of the GNU General Public License as published by
 
9
# the Free Software Foundation, either version 2 of the License, or
 
10
# (at your option) any later version.
 
11
#
 
12
# This program is distributed in the hope that it will be useful, but
 
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
# General Public License for more details.
 
16
#
 
17
# You should have received a copy of the GNU General Public License
 
18
# along with this program.  If not, see
 
19
# <http://www.gnu.org/licenses/>.
 
20
#
 
21
#######################################################################
 
22
 
 
23
package Sbuild::ChrootPlain;
 
24
 
 
25
use strict;
 
26
use warnings;
 
27
 
 
28
use POSIX;
 
29
use FileHandle;
 
30
use File::Temp ();
 
31
 
 
32
use Sbuild::Conf;
 
33
use Sbuild::Log;
 
34
use Sbuild::Sysconfig;
 
35
 
 
36
BEGIN {
 
37
    use Exporter ();
 
38
    use Sbuild::Chroot;
 
39
    our (@ISA, @EXPORT);
 
40
 
 
41
    @ISA = qw(Exporter Sbuild::Chroot);
 
42
 
 
43
    @EXPORT = qw();
 
44
}
 
45
 
 
46
sub new {
 
47
    my $class = shift;
 
48
    my $conf = shift;
 
49
    my $chroot_id = shift;
 
50
 
 
51
    my $self = $class->SUPER::new($conf, $chroot_id);
 
52
    bless($self, $class);
 
53
 
 
54
    # Only run split, because plain makes no guarantee that networking
 
55
    # works inside the chroot.
 
56
    $self->set('Split', 1);
 
57
 
 
58
    return $self;
 
59
}
 
60
 
 
61
sub begin_session {
 
62
    my $self = shift;
 
63
 
 
64
    $self->set('Priority', 0);
 
65
    $self->set('Location', $self->get('Chroot ID'));
 
66
    $self->set('Session Purged', 0);
 
67
 
 
68
    $self->_setup_options();
 
69
 
 
70
    return 1;
 
71
}
 
72
 
 
73
sub end_session {
 
74
    my $self = shift;
 
75
 
 
76
    # No-op for sudo.
 
77
 
 
78
    return 1;
 
79
}
 
80
 
 
81
sub get_command_internal {
 
82
    my $self = shift;
 
83
    my $options = shift;
 
84
 
 
85
    my $command = $options->{'INTCOMMAND'}; # Command to run
 
86
    my $user = $options->{'USER'};          # User to run command under
 
87
    my $chroot = $options->{'CHROOT'};      # Run in chroot?
 
88
    my $dir;                                # Directory to use (optional)
 
89
    $dir = $self->get('Defaults')->{'DIR'} if
 
90
        (defined($self->get('Defaults')) &&
 
91
         defined($self->get('Defaults')->{'DIR'}));
 
92
    $dir = $options->{'DIR'} if
 
93
        defined($options->{'DIR'}) && $options->{'DIR'};
 
94
 
 
95
    if (!defined $user || $user eq "") {
 
96
        $user = $self->get_conf('USERNAME');
 
97
    }
 
98
    if (!defined $chroot) {
 
99
        $chroot = 1;
 
100
    }
 
101
 
 
102
    my @cmdline;
 
103
    my $chdir = undef;
 
104
    if ($chroot != 0) { # Run command inside chroot
 
105
        if (!defined($dir)) {
 
106
            $dir = '/';
 
107
        }
 
108
 
 
109
        my $shellcommand;
 
110
        foreach (@$command) {
 
111
            my $tmp = $_;
 
112
            $tmp =~ s/'//g; # Strip any single quotes for security
 
113
            if ($_ ne $tmp) {
 
114
                $self->log_warning("Stripped single quote from command for security: $_\n");
 
115
            }
 
116
            if ($shellcommand) {
 
117
                $shellcommand .= " '$tmp'";
 
118
            } else {
 
119
                $shellcommand = "'$tmp'";
 
120
            }
 
121
        }
 
122
 
 
123
        @cmdline = ('/usr/sbin/chroot', $self->get('Location'),
 
124
                    $self->get_conf('SU'), '-p', "$user", '-s',
 
125
                    $Sbuild::Sysconfig::programs{'SHELL'}, '-c',
 
126
                    "cd '$dir' && $shellcommand");
 
127
    } else { # Run command outside chroot
 
128
        if ($options->{'CHDIR_CHROOT'}) {
 
129
            my $tmpdir = $self->get('Location');
 
130
            $tmpdir = $tmpdir . $dir if defined($dir);
 
131
            $dir = $tmpdir;
 
132
        }
 
133
        if ($user ne $self->get_conf('USERNAME')) {
 
134
            print main::LOG "Command \"$command\" cannot be run as user $user on the host system\n";
 
135
        }
 
136
        $chdir = $dir if defined($dir);
 
137
        push(@cmdline, @$command);
 
138
    }
 
139
 
 
140
    $options->{'CHROOT'} = $chroot;
 
141
    $options->{'USER'} = $user;
 
142
    $options->{'COMMAND'} = $command;
 
143
    $options->{'EXPCOMMAND'} = \@cmdline;
 
144
    $options->{'CHDIR'} = $chdir;
 
145
    $options->{'DIR'} = $dir;
 
146
}
 
147
 
 
148
1;