~ubuntu-branches/ubuntu/raring/apparmor/raring

« back to all changes in this revision

Viewing changes to utils/genprof

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook
  • Date: 2007-03-23 16:42:01 UTC
  • Revision ID: james.westby@ubuntu.com-20070323164201-jkax6f0oku087b7l
Tags: upstream-2.0.1+510.dfsg
ImportĀ upstreamĀ versionĀ 2.0.1+510.dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl
 
2
#
 
3
# $Id: genprof 458 2007-03-20 22:58:38Z jmichael-at-suse-de $
 
4
#
 
5
# ----------------------------------------------------------------------
 
6
#    Copyright (c) 2005 Novell, Inc. All Rights Reserved.
 
7
#
 
8
#    This program is free software; you can redistribute it and/or
 
9
#    modify it under the terms of version 2 of the GNU General Public
 
10
#    License as published by the Free Software Foundation.
 
11
#
 
12
#    This program is distributed in the hope that it will be useful,
 
13
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
#    GNU 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, contact Novell, Inc.
 
19
#
 
20
#    To contact Novell about this file by physical or electronic mail,
 
21
#    you may find current contact information at www.novell.com.
 
22
# ----------------------------------------------------------------------
 
23
 
 
24
use strict;
 
25
use Getopt::Long;
 
26
 
 
27
use Immunix::SubDomain;
 
28
 
 
29
use Data::Dumper;
 
30
 
 
31
use Locale::gettext;
 
32
use POSIX;
 
33
 
 
34
# force $PATH to be sane
 
35
$ENV{PATH} = "/bin:/sbin:/usr/bin:/usr/sbin";
 
36
 
 
37
# initialize the local poo
 
38
setlocale(LC_MESSAGES, "");
 
39
textdomain("apparmor-utils");
 
40
 
 
41
# options variables
 
42
my $help = '';
 
43
 
 
44
GetOptions(
 
45
    'file|f=s' => \$filename,
 
46
    'dir|d=s'  => \$profiledir,
 
47
    'help|h'   => \$help,
 
48
);
 
49
 
 
50
# tell 'em how to use it...
 
51
&usage && exit if $help;
 
52
 
 
53
my $sd_mountpoint = check_for_subdomain();
 
54
unless ($sd_mountpoint) {
 
55
    fatal_error(gettext("SubDomain does not appear to be started.  Please enable SubDomain and try again."));
 
56
}
 
57
 
 
58
# let's convert it to full path...
 
59
$profiledir = get_full_path($profiledir);
 
60
 
 
61
unless (-d $profiledir) {
 
62
    fatal_error "Can't find subdomain profiles in $profiledir.";
 
63
}
 
64
 
 
65
# what are we profiling?
 
66
my $profiling = shift;
 
67
 
 
68
unless ($profiling) {
 
69
    $profiling = UI_GetString(gettext("Please enter the program to profile: "), "")
 
70
      || exit 0;
 
71
}
 
72
 
 
73
my $fqdbin;
 
74
if (-e $profiling) {
 
75
    $fqdbin = get_full_path($profiling);
 
76
    chomp($fqdbin);
 
77
} else {
 
78
    if ($profiling !~ /\//) {
 
79
        my $which = which($profiling);
 
80
        if ($which) {
 
81
            $fqdbin = get_full_path($which);
 
82
        }
 
83
    }
 
84
}
 
85
 
 
86
unless ($fqdbin && -e $fqdbin) {
 
87
    if ($profiling =~ /^[^\/]+$/) {
 
88
        fatal_error(sprintf(gettext('Can\'t find %s in the system path list.  If the name of the application is correct, please run \'which %s\' in the other window in order to find the fully-qualified path.'), $profiling, $profiling));
 
89
    } else {
 
90
        fatal_error(sprintf(gettext('%s does not exist, please double-check the path.'), $profiling));
 
91
    }
 
92
}
 
93
 
 
94
# read the settings in /etc/logprof.conf
 
95
readconfig();
 
96
 
 
97
# make sure that the app they're requesting to profile is not marked as
 
98
# not allowed to have it's own profile
 
99
if ($qualifiers{$fqdbin}) {
 
100
    unless ($qualifiers{$fqdbin} =~ /p/) {
 
101
        fatal_error(sprintf(gettext("\%s is currently marked as a program that should not have it's own profile.  Usually, programs are marked this way if creating a profile for them is likely to break the rest of the system.  If you know what you're doing and are certain you want to create a profile for this program, edit the corresponding entry in the [qualifiers] section in /etc/apparmor/logprof.conf."), $fqdbin));
 
102
    }
 
103
}
 
104
 
 
105
# load all the include files
 
106
loadincludes();
 
107
 
 
108
my $profilefilename = getprofilefilename($fqdbin);
 
109
if (-e $profilefilename) {
 
110
    $helpers{$fqdbin} = getprofileflags($profilefilename) || "enforce";
 
111
} else {
 
112
    autodep($fqdbin);
 
113
    $helpers{$fqdbin} = "enforce";
 
114
}
 
115
 
 
116
if ($helpers{$fqdbin} eq "enforce") {
 
117
    complain($fqdbin);
 
118
    reload($fqdbin);
 
119
}
 
120
 
 
121
UI_Important(gettext("Please start the application to be profiled in \nanother window and exercise its functionality now.\n\nOnce completed, select the \"Scan\" button below in \norder to scan the system logs for AppArmor events.  \n\nFor each AppArmor event, you will be given the  \nopportunity to choose whether the access should be  \nallowed or denied."));
 
122
 
 
123
my $syslog         = 1;
 
124
my $logmark        = "";
 
125
my $done_profiling = 0;
 
126
 
 
127
$syslog = 0 if (-e "/var/log/audit/audit.log");
 
128
 
 
129
while (not $done_profiling) {
 
130
    if ($syslog) {
 
131
        $logmark = `date | md5sum`;
 
132
        chomp $logmark;
 
133
        $logmark = $1 if $logmark =~ /^([0-9a-f]+)/;
 
134
        system("/bin/logger -p kern.warn 'GenProf: $logmark'");
 
135
    } else {
 
136
        $logmark = last_audit_entry_time();
 
137
    }
 
138
 
 
139
    my $q = {};
 
140
    $q->{headers} = [ gettext("Profiling"), $fqdbin ];
 
141
    $q->{functions} = [ "CMD_SCAN", "CMD_FINISHED" ];
 
142
    $q->{default} = "CMD_SCAN";
 
143
 
 
144
    my ($ans, $arg) = UI_PromptUser($q);
 
145
 
 
146
    if ($ans eq "CMD_SCAN") {
 
147
 
 
148
        my $lp_ret = do_logprof_pass($logmark);
 
149
 
 
150
        $done_profiling = 1 if $lp_ret eq "FINISHED";
 
151
 
 
152
    } else {
 
153
 
 
154
        $done_profiling = 1;
 
155
 
 
156
    }
 
157
 
 
158
}
 
159
 
 
160
for my $p (sort keys %helpers) {
 
161
    if ($helpers{$p} eq "enforce") {
 
162
        enforce($p);
 
163
        reload($p);
 
164
    }
 
165
}
 
166
 
 
167
UI_Info(gettext("Reloaded SubDomain profiles in enforce mode."));
 
168
UI_Info(sprintf(gettext('Finished generating profile for %s.'), $fqdbin));
 
169
exit 0;
 
170
 
 
171
sub usage {
 
172
    UI_Info(sprintf(gettext("usage: \%s [ -d /path/to/profiles ] [ -f /path/to/logfile ] [ program to profile ]"), $0));
 
173
    exit 0;
 
174
}
 
175
 
 
176
sub last_audit_entry_time {
 
177
    local $_ = `tail -1 /var/log/audit/audit.log`;
 
178
    my $logmark;
 
179
    if (/^*msg\=audit\((\d+\.\d+\:\d+).*\).*$/) {
 
180
        $logmark = $1;
 
181
    } else {
 
182
        $logmark = "";
 
183
    }
 
184
    return $logmark;
 
185
}