2006-04-11 21:52:54 +00:00
#!/usr/bin/perl
# ----------------------------------------------------------------------
# Copyright (c) 2005 Novell, Inc. All Rights Reserved.
2011-05-23 11:28:26 -07:00
# Copyright (c) 2011 Canonical, Ltd.
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 FindBin;
use Getopt::Long;
2011-01-13 13:58:26 -08:00
use Immunix::AppArmor;
2006-04-11 21:52:54 +00:00
use Data::Dumper;
use Locale::gettext;
use POSIX;
# initialize the local poo
setlocale(LC_MESSAGES, "");
textdomain("apparmor-utils");
$UI_Mode = "text";
# 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
'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;
# let's convert it to full path...
$profiledir = get_full_path($profiledir);
2007-03-20 21:58:38 +00:00
unless (-d $profiledir) {
2011-01-13 13:58:26 -08:00
UI_Important("Can't find AppArmor profiles in $profiledir.");
2007-03-20 21:58:38 +00:00
exit 1;
2006-04-11 21:52:54 +00:00
}
# what are we profiling?
my @profiling = @ARGV;
2007-03-20 21:58:38 +00:00
unless (@profiling) {
@profiling = (UI_GetString(gettext("Please enter the program to switch to enforce mode: "), ""));
2006-04-11 21:52:54 +00:00
}
for my $profiling (@profiling) {
2007-03-20 21:58:38 +00:00
next unless $profiling;
2006-04-11 21:52:54 +00:00
2007-03-20 21:58:38 +00:00
my $fqdbin;
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 !~ /\//) {
2007-06-15 15:11:09 +00:00
opendir(DIR,$profiledir);
my @tmp_fqdbin = grep ( /$profiling/, readdir(DIR));
closedir(DIR);
if (scalar @tmp_fqdbin eq 1) {
$fqdbin = "$profiledir/$tmp_fqdbin[0]";
} else {
my $which = which($profiling);
if ($which) {
$fqdbin = get_full_path($which);
}
}
2007-03-20 21:58:38 +00:00
}
2006-04-11 21:52:54 +00:00
}
2007-03-20 21:58:38 +00:00
if (-e $fqdbin) {
my $filename;
if ($fqdbin =~ /^$profiledir\//) {
$filename = $fqdbin;
} else {
$filename = getprofilefilename($fqdbin);
}
# argh, skip directories
next unless -f $filename;
2006-04-11 21:52:54 +00:00
2007-03-20 21:58:38 +00:00
# skip rpm backup files
2007-03-23 18:52:22 +00:00
next if isSkippableFile($filename);
2006-04-11 21:52:54 +00:00
2007-03-20 21:58:38 +00:00
printf(gettext('Setting %s to enforce mode.'), $fqdbin);
print "\n";
setprofileflags($filename, "");
2006-04-11 21:52:54 +00:00
2009-11-11 11:38:26 -08:00
# remove symlink in $profiledir/force-complain as well
my $complainlink = $filename;
$complainlink =~ s/^$profiledir/$profiledir\/force-complain/;
-e $complainlink and unlink($complainlink);
# remove symlink in $profiledir/disable as well
my $disablelink = $filename;
$disablelink =~ s/^$profiledir/$profiledir\/disable/;
-e $disablelink and unlink($disablelink);
2008-04-24 18:24:02 +00:00
my $cmd_info = qx(cat $filename | $parser -I$profiledir -r 2>&1 1>/dev/null);
2008-04-10 07:25:46 +00:00
if ($? != 0) {
UI_Info($cmd_info);
exit $?;
}
# if check_for_subdomain();
2006-04-11 21:52:54 +00:00
} else {
2007-03-20 21:58:38 +00:00
if ($profiling =~ /^[^\/]+$/) {
UI_Info(sprintf(gettext('Can\'t find %s in the system path list. If the name of the application is correct, please run \'which %s\' as a user with the correct PATH environment set up in order to find the fully-qualified path.'), $profiling, $profiling));
exit 1;
} else {
2011-05-23 11:28:26 -07:00
UI_Info(sprintf(gettext('%s does not exist, please double-check the path.'), $profiling));
2007-03-20 21:58:38 +00:00
exit 1;
}
2006-04-11 21:52:54 +00:00
}
}
exit 0;
sub usage {
2007-03-20 21:58:38 +00:00
UI_Info(sprintf(gettext("usage: \%s [ -d /path/to/profiles ] [ program to switch to enforce mode ]"), $0));
exit 0;
2006-04-11 21:52:54 +00:00
}