2006-04-11 21:52:54 +00:00
#!/usr/bin/perl
#
2006-04-12 19:31:08 +00:00
# $Id$
2006-04-11 21:52:54 +00:00
#
# ----------------------------------------------------------------------
# Copyright (c) 2005 Novell, Inc. All Rights Reserved.
2007-03-20 21:58:38 +00:00
#
2006-04-11 21:52:54 +00:00
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public
# License as published by the Free Software Foundation.
2007-03-20 21:58:38 +00:00
#
2006-04-11 21:52:54 +00:00
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
2007-03-20 21:58:38 +00:00
#
2006-04-11 21:52:54 +00:00
# You should have received a copy of the GNU General Public License
# along with this program; if not, contact Novell, Inc.
2007-03-20 21:58:38 +00:00
#
# To contact Novell about this file by physical or electronic mail,
2006-04-11 21:52:54 +00:00
# you may find current contact information at www.novell.com.
# ----------------------------------------------------------------------
use strict;
use Getopt::Long;
use Immunix::SubDomain;
use Data::Dumper;
use Locale::gettext;
use POSIX;
# force $PATH to be sane
$ENV{PATH} = "/bin:/sbin:/usr/bin:/usr/sbin";
# initialize the local poo
setlocale(LC_MESSAGES, "");
textdomain("apparmor-utils");
# options variables
2007-03-20 21:58:38 +00:00
my $help = '';
2006-04-11 21:52:54 +00:00
GetOptions(
2007-03-20 21:58:38 +00:00
'file|f=s' => \$filename,
'dir|d=s' => \$profiledir,
'help|h' => \$help,
2006-04-11 21:52:54 +00:00
);
2007-03-20 21:58:38 +00:00
2006-04-11 21:52:54 +00:00
# tell 'em how to use it...
&usage && exit if $help;
my $sd_mountpoint = check_for_subdomain();
2007-03-20 21:58:38 +00:00
unless ($sd_mountpoint) {
fatal_error(gettext("SubDomain does not appear to be started. Please enable SubDomain and try again."));
2006-04-11 21:52:54 +00:00
}
# let's convert it to full path...
$profiledir = get_full_path($profiledir);
2007-03-20 21:58:38 +00:00
unless (-d $profiledir) {
fatal_error "Can't find subdomain profiles in $profiledir.";
2006-04-11 21:52:54 +00:00
}
# what are we profiling?
my $profiling = shift;
2007-03-20 21:58:38 +00:00
unless ($profiling) {
$profiling = UI_GetString(gettext("Please enter the program to profile: "), "")
|| exit 0;
2006-04-11 21:52:54 +00:00
}
my $fqdbin;
2007-03-20 21:58:38 +00:00
if (-e $profiling) {
$fqdbin = get_full_path($profiling);
chomp($fqdbin);
2006-04-11 21:52:54 +00:00
} else {
2007-03-20 21:58:38 +00:00
if ($profiling !~ /\//) {
my $which = which($profiling);
if ($which) {
$fqdbin = get_full_path($which);
}
2006-04-11 21:52:54 +00:00
}
}
2007-03-20 21:58:38 +00:00
unless ($fqdbin && -e $fqdbin) {
if ($profiling =~ /^[^\/]+$/) {
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));
} else {
fatal_error(sprintf(gettext('%s does not exist, please double-check the path.'), $profiling));
}
2006-04-11 21:52:54 +00:00
}
# make sure that the app they're requesting to profile is not marked as
# not allowed to have it's own profile
2007-04-25 21:06:52 +00:00
check_qualifiers($fqdbin);
2006-04-11 21:52:54 +00:00
# load all the include files
loadincludes();
my $profilefilename = getprofilefilename($fqdbin);
2007-03-20 21:58:38 +00:00
if (-e $profilefilename) {
$helpers{$fqdbin} = getprofileflags($profilefilename) || "enforce";
2006-04-11 21:52:54 +00:00
} else {
2007-03-20 21:58:38 +00:00
autodep($fqdbin);
$helpers{$fqdbin} = "enforce";
2006-04-11 21:52:54 +00:00
}
2007-03-20 21:58:38 +00:00
if ($helpers{$fqdbin} eq "enforce") {
complain($fqdbin);
reload($fqdbin);
2006-04-11 21:52:54 +00:00
}
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."));
2007-03-20 21:58:38 +00:00
my $syslog = 1;
my $logmark = "";
2006-04-11 21:52:54 +00:00
my $done_profiling = 0;
2007-03-20 21:58:38 +00:00
$syslog = 0 if (-e "/var/log/audit/audit.log");
2006-04-11 21:52:54 +00:00
2007-03-20 21:58:38 +00:00
while (not $done_profiling) {
if ($syslog) {
$logmark = `date | md5sum`;
chomp $logmark;
$logmark = $1 if $logmark =~ /^([0-9a-f]+)/;
system("/bin/logger -p kern.warn 'GenProf: $logmark'");
} else {
$logmark = last_audit_entry_time();
}
2006-04-11 21:52:54 +00:00
2007-03-20 21:58:38 +00:00
my $q = {};
$q->{headers} = [ gettext("Profiling"), $fqdbin ];
$q->{functions} = [ "CMD_SCAN", "CMD_FINISHED" ];
$q->{default} = "CMD_SCAN";
2006-04-11 21:52:54 +00:00
2007-03-20 21:58:38 +00:00
my ($ans, $arg) = UI_PromptUser($q);
2006-04-11 21:52:54 +00:00
2007-03-20 21:58:38 +00:00
if ($ans eq "CMD_SCAN") {
2006-04-11 21:52:54 +00:00
2007-03-20 21:58:38 +00:00
my $lp_ret = do_logprof_pass($logmark);
2006-04-11 21:52:54 +00:00
2007-03-20 21:58:38 +00:00
$done_profiling = 1 if $lp_ret eq "FINISHED";
2006-04-11 21:52:54 +00:00
2007-03-20 21:58:38 +00:00
} else {
2006-04-11 21:52:54 +00:00
2007-03-20 21:58:38 +00:00
$done_profiling = 1;
2006-04-11 21:52:54 +00:00
2007-03-20 21:58:38 +00:00
}
2006-04-11 21:52:54 +00:00
}
2007-03-20 21:58:38 +00:00
2006-04-11 21:52:54 +00:00
for my $p (sort keys %helpers) {
2007-03-20 21:58:38 +00:00
if ($helpers{$p} eq "enforce") {
enforce($p);
reload($p);
}
2006-04-11 21:52:54 +00:00
}
UI_Info(gettext("Reloaded SubDomain profiles in enforce mode."));
UI_Info(sprintf(gettext('Finished generating profile for %s.'), $fqdbin));
exit 0;
sub usage {
2007-03-20 21:58:38 +00:00
UI_Info(sprintf(gettext("usage: \%s [ -d /path/to/profiles ] [ -f /path/to/logfile ] [ program to profile ]"), $0));
exit 0;
2006-04-11 21:52:54 +00:00
}
sub last_audit_entry_time {
2007-03-20 21:58:38 +00:00
local $_ = `tail -1 /var/log/audit/audit.log`;
my $logmark;
if (/^*msg\=audit\((\d+\.\d+\:\d+).*\).*$/) {
$logmark = $1;
} else {
$logmark = "";
}
return $logmark;
}