aboutsummaryrefslogtreecommitdiff
path: root/clubber
blob: 27c4dd345b858f517306f2bb742e5db63b0b64ef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env perl

#
# Clubber -- Less painful chroot environment building and verification. Please
# see README.markdown for documentation. Be sure you trust your binaries! This
# script won't protect you from ldd exploits.
#
# @author Tom Ryder <tom@sanctum.geek.nz>
# @copyright 2012 Sanctum
#

use strict;
use warnings;
use Cwd qw(abs_path);
use File::Basename;
use Getopt::Long;

#
# Bail if we're not root.
#
if ($< != 0 || $> != 0) {
    error("You must have root permissions to use this script.");
}

#
# Check ldd is available.
#
chomp(my $ldd = `which ldd`);
if (!$ldd) {
    error("Couldn't find ldd in your \$PATH.");
}

#
# Check md5sum is available.
#
chomp(my $md5sum = `which md5sum`);
if (!$md5sum) {
    error("Couldn't find md5sum in your \$PATH.");
}

#
# Check options.
#
my ($chroot, $dry) = ("", 0);
my $config = GetOptions("chroot=s" => \$chroot,
                        "dry"      => \$dry);
if ($chroot) {
    $chroot = abs_path($chroot);
    if (!-d $chroot) {
        error("Nominated chroot %s doesn't seem to be a directory.", $chroot);
    }
} elsif ($dry) {
    error("Doesn't make sense to specify --dry without --chroot.");
}

#
# Check we were passed at least one parameter, otherwise print a helpful
# message.
#
if (!@ARGV) {
    printf STDOUT "USAGE: ${0} [--chroot] [--dry] binary1 binary2 ... \n";
    exit 0;
}

#
# Check that all our parameters are real files, or point to one.
#
my $binaries = [];
foreach my $argument (@ARGV) {
    $argument = abs_path($argument);
    if (-f $argument) {
        push @$binaries, $argument;
    } else {
        error("File %s doesn't seem to exist.", $argument);
    }
}

#
# Run ldd on all the files and slurp all the absolute paths; put them into a
# hash to keep things unique.
#
my $libraries = {};
foreach my $binary (@$binaries) {
    my $output = [qx/${ldd} ${binary}/];
    foreach my $line (@$output) {
        if ($line =~ m#(/\S*lib\S+)#) {
            $libraries->{$1} = 1;
        }
    }
}

#
# If we have a chroot, we need to figure out what libraries require importing
# and which directories require creating.
#
if ($chroot) {
    my ($directories, $imports) = ({}, {});

    #
    # First we'll recurse through the list of libraries and flag any
    # directories that require creation. We won't complicate things by
    # reproducing any symbolic link structures.
    #
    # If the directory does exist, we'll make sure the library is in place
    # too, and if it is that its md5sum is the same as our root system
    # library. If it doesn't exist or if it's different, we'll flag it for
    # overwriting.
    #
    foreach my $library (keys(%$libraries)) {
        my $directory = dirname($library);
        if (!-d "${chroot}${directory}") {
            $directories->{$directory} = 1;
        }
        if (-f "${chroot}{$library}") {
            if (qx/${md5sum} ${library}/ ne qx/${md5sum} ${chroot}${library}/) {
                $imports->{$library} = 1;
            }
        } else {
            $imports->{$library} = 1;
        }
    }

    #
    # Check there's something for us to do.
    #
    if (keys %$directories || keys %$imports) {

        #
        # If we're just supposed to print what we do, all the better, do
        # that and then quit.
        #
        if ($dry) {
            if (keys %$directories) {
                printf STDOUT "Create directories:\n";
                foreach my $directory (keys(%$directories)) {
                    printf STDOUT "    %s\n", "${chroot}${directory}";
                }
            }
            if (keys %$imports) {
                printf STDOUT "Copy libraries:\n";
                foreach my $import (keys(%$imports)) {
                    printf STDOUT "    %s -> %s\n", $import, "${chroot}${import}";
                }
            }

        #
        # Otherwise, we'd best get started.
        #
        } else {
            foreach my $directory (keys(%$directories)) {
                system("mkdir -pv ${chroot}${directory}");
            }
            foreach my $import (keys(%$imports)) {
                system("cp -pv ${import} ${chroot}${import}");
            }
        }

    #
    # If there's nothing we need to do, say so.
    #
    } else {
        printf STDOUT "Nothing to do.\n";
    }

#
# If we don't have a chroot, we can just print the list of libraries, and we're
# done.
#
} else {
    foreach my $library (keys(%$libraries)) {
        printf "%s\n", $library;
    }
}

#
# And one way or another, we're done.
#
exit 0;

#
# Print a usage message and exit with non-zero.
#
sub usage {
    my $message = shift @_;
    printf STDERR "USAGE: ${message}\n", @_;
    exit 1;
}

#
# Print a usage message and exit with non-zero.
#
sub error {
    my $message = shift @_;
    printf STDERR "ERROR: ${message}\n", @_;
    exit 1;
}