2
0
mirror of https://gitlab.com/apparmor/apparmor synced 2025-08-31 06:16:03 +00:00

try to make some general writer routines

This commit is contained in:
John Johansen
2008-04-18 20:55:11 +00:00
parent e48fccb6d0
commit 7ec531f4e8

View File

@@ -4410,12 +4410,25 @@ sub is_active_profile ($) {
} }
} }
sub strip_quotes ($) {
my $data = shift;
$data = $1 if $data =~ /^\"(.*)\"$/;
return $data;
}
sub quote_if_needed ($) {
my $data = shift;
$data = "\"$data\"" if $data =~ /\s/;
return $data;
}
sub escape ($) { sub escape ($) {
my $dangerous = shift; my $dangerous = shift;
if ($dangerous =~ m/^"(.+)"$/) { $dangerous = strip_quotes($dangerous);
$dangerous = $1;
}
$dangerous =~ s/((?<!\\))"/$1\\"/g; $dangerous =~ s/((?<!\\))"/$1\\"/g;
if ($dangerous =~ m/(\s|^$|")/) { if ($dangerous =~ m/(\s|^$|")/) {
$dangerous = "\"$dangerous\""; $dangerous = "\"$dangerous\"";
@@ -4429,7 +4442,7 @@ sub writeheader ($$$$) {
my @data; my @data;
# deal with whitespace in profile names... # deal with whitespace in profile names...
$name = "\"$name\"" if $name =~ /\s/; $name = quote_if_needed($name);
push @data, "#include <tunables/global>" unless ( $is_hat ); push @data, "#include <tunables/global>" unless ( $is_hat );
if ($write_flags and $profile_data->{flags}) { if ($write_flags and $profile_data->{flags}) {
push @data, "$name flags=($profile_data->{flags}) {"; push @data, "$name flags=($profile_data->{flags}) {";
@@ -4440,36 +4453,49 @@ sub writeheader ($$$$) {
return @data; return @data;
} }
sub writeincludes ($) { sub write_single ($$$$) {
my $profile_data = shift; my ($profile_data, $name, $prefix, $tail) = @_;
my @data; my @data;
# dump out the includes # dump out the data
if (exists $profile_data->{include}) { if (exists $profile_data->{$name}) {
for my $include (sort keys %{$profile_data->{include}}) { for my $key (sort keys %{$profile_data->{$name}}) {
push @data, " #include <$include>"; my $qkey = quote_if_needed($key);
push @data, " ${prefix}${qkey}${tail}";
} }
push @data, "" if keys %{$profile_data->{include}}; push @data, "" if keys %{$profile_data->{$name}};
} }
return @data; return @data;
} }
sub writecapabilities ($) { sub write_pair ($$$$$) {
my $profile_data = shift; my ($profile_data, $name, $prefix, $sep, $tail) = @_;
my @data; my @data;
# dump out the capability entries... # dump out the data
if (exists $profile_data->{capability}) { if (exists $profile_data->{$name}) {
for my $capability (sort keys %{$profile_data->{capability}}) { for my $key (sort keys %{$profile_data->{$name}}) {
push @data, " capability $capability,"; my $qkey = quote_if_needed($key);
my $value = quote_if_needed($profile_data->{$name}{$key});
push @data, " ${prefix}${key}${sep}${value}${tail}";
} }
push @data, "" if keys %{$profile_data->{capability}}; push @data, "" if keys %{$profile_data->{$name}};
} }
return @data; return @data;
} }
sub writeincludes ($) {
my $profile_data = shift;
return write_single($profile_data, 'include', "#include <", ">");
}
sub writecapabilities ($) {
my $profile_data = shift;
return write_single($profile_data, 'capability', "capability ", ",");
}
sub writenetdomain ($) { sub writenetdomain ($) {
my $profile_data = shift; my $profile_data = shift;