~ubuntu-branches/ubuntu/quantal/apparmor/quantal-updates

« back to all changes in this revision

Viewing changes to utils/apparmor_status

  • 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 -w
 
2
# $Id: apparmor_status 10 2006-04-12 20:31:08Z steve-beattie $
 
3
# ------------------------------------------------------------------
 
4
#
 
5
#    Copyright (C) 2005-2006 Novell/SUSE
 
6
#
 
7
#    This program is free software; you can redistribute it and/or
 
8
#    modify it under the terms of version 2 of the GNU General Public
 
9
#    License published by the Free Software Foundation.
 
10
#
 
11
# ------------------------------------------------------------------
 
12
 
 
13
 
 
14
use strict;
 
15
use Getopt::Long;
 
16
 
 
17
my $confdir = "/etc/apparmor";
 
18
my $sd_mountpoint;
 
19
my $check_enabled = 0;
 
20
my $count_enforced = 0;
 
21
my $count_profiled = 0;
 
22
my $count_complain = 0;
 
23
my $verbose = 0;
 
24
my $help;
 
25
 
 
26
GetOptions(
 
27
  'complaining'         => \$count_complain,
 
28
  'enabled'             => \$check_enabled,
 
29
  'enforced'            => \$count_enforced,
 
30
  'profiled'            => \$count_profiled,
 
31
  'verbose|v'           => \$verbose,
 
32
  'help|h'              => \$help,
 
33
) or usage();
 
34
 
 
35
sub usage {
 
36
  print "Usage: $0 [OPTIONS]\n";
 
37
  print "Displays various information about the currently loaded AppArmor policy.\n";
 
38
  print "OPTIONS (one only):\n";
 
39
  print "  --enabled       returns error code if subdomain not enabled\n";
 
40
  print "  --profiled      prints the number of loaded policies\n";
 
41
  print "  --enforced      prints the number of loaded enforcing policies\n";
 
42
  print "  --complaining   prints the number of loaded non-enforcing policies\n";
 
43
  print "  --verbose       (default) displays multiple data points about loaded policy set\n";
 
44
  print "  --help          this message\n";
 
45
  exit;
 
46
}
 
47
 
 
48
$verbose = 1 if ($count_complain + $check_enabled + $count_enforced + $count_profiled == 0);
 
49
usage() if $help or ($count_complain + $check_enabled + $count_enforced + $count_profiled + $verbose > 1);
 
50
 
 
51
sub is_subdomain_loaded() {
 
52
  if(open(MODULES, "/proc/modules")) {
 
53
    while(<MODULES>) {
 
54
      return 1 if m/^(subdomain|apparmor)\s+/;
 
55
    }
 
56
  }
 
57
 
 
58
  return 0;
 
59
}
 
60
 
 
61
sub find_subdomainfs() {
 
62
 
 
63
  my $sd_mountpoint;
 
64
  if(open(MOUNTS, "/proc/mounts")) {
 
65
    while(<MOUNTS>) {
 
66
      $sd_mountpoint = "$1/apparmor" if m/^\S+\s+(\S+)\s+securityfs\s/ && -e "$1/apparmor";
 
67
      $sd_mountpoint = "$1/subdomain" if m/^\S+\s+(\S+)\s+securityfs\s/ && -e "$1/subdomain";
 
68
      $sd_mountpoint = $1 if m/^\S+\s+(\S+)\s+subdomainfs\s/ && -e "$1";
 
69
    }
 
70
    close(MOUNTS);
 
71
  }
 
72
 
 
73
  return $sd_mountpoint;
 
74
}
 
75
 
 
76
sub count_profiles {
 
77
  my $mountpoint = shift;
 
78
  my $profiles = 0;
 
79
  my $enforced = 0;
 
80
  my $complain = 0;
 
81
 
 
82
  if (open(PROFILES, "$mountpoint/profiles")) {
 
83
    while(<PROFILES>) {
 
84
      $profiles++;
 
85
      $enforced++ if m/\(enforce\)$/;
 
86
      $complain++ if m/\(complain\)$/;
 
87
    }
 
88
    close(PROFILES);
 
89
  }
 
90
  return ($profiles, $enforced, $complain);
 
91
}
 
92
 
 
93
sub count_processes {
 
94
  my $processes = 0;
 
95
  my $confined = 0;
 
96
  my $enforced = 0;
 
97
  my $complain = 0;
 
98
  if (opendir(PROC, "/proc")) {
 
99
    my $file;
 
100
    while (defined($file = readdir(PROC))) {
 
101
      if ($file =~ m/^\d+/ && open(CURRENT, "/proc/$file/attr/current")) {
 
102
        while (<CURRENT>) {
 
103
          $processes++;
 
104
          $confined++ if not m/^unconstrained$/;
 
105
          $enforced++ if m/\(enforce\)$/;
 
106
          $complain++ if m/\(complain\)$/;
 
107
        }
 
108
        close(CURRENT);
 
109
      }
 
110
    }
 
111
    closedir(PROC);
 
112
  }
 
113
  return ($processes, $confined, $enforced, $complain);
 
114
}
 
115
 
 
116
my $is_loaded = is_subdomain_loaded();
 
117
 
 
118
if (!$is_loaded) {
 
119
  print STDERR "apparmor module is not loaded.\n" if $verbose;
 
120
  exit 1;
 
121
}
 
122
 
 
123
print "apparmor module is loaded.\n" if $verbose;
 
124
 
 
125
$sd_mountpoint = find_subdomainfs();
 
126
if (!$sd_mountpoint) {
 
127
  print STDERR "apparmor filesystem is not mounted.\n" if $verbose;
 
128
  exit 3;
 
129
}
 
130
 
 
131
if (! -r "$sd_mountpoint/profiles") {
 
132
  print STDERR "You do not have enough privilege to read the profile set.\n" if $verbose;
 
133
  exit 4;
 
134
}
 
135
 
 
136
#print "subdomainfs is at $sd_mountpoint.\n" if $verbose;
 
137
 
 
138
my $processes;
 
139
my $profiles;
 
140
my $enforced;
 
141
my $complain;
 
142
 
 
143
($profiles, $enforced, $complain) = count_profiles($sd_mountpoint);
 
144
 
 
145
# we consider the case where no profiles are loaded to be "disabled" as well
 
146
my $rc = ($profiles == 0) ? 2 : 0;
 
147
 
 
148
if ($check_enabled) {
 
149
  exit $rc;
 
150
}
 
151
 
 
152
if ($count_profiled) {
 
153
  print "$profiles\n";
 
154
  exit $rc;
 
155
}
 
156
 
 
157
if ($count_enforced) {
 
158
  print "$enforced\n";
 
159
  exit $rc;
 
160
}
 
161
 
 
162
if ($count_complain) {
 
163
  print "$complain\n";
 
164
  exit $rc;
 
165
}
 
166
 
 
167
 
 
168
if ($verbose) {
 
169
  print "$profiles profiles are loaded.\n";
 
170
  print "$enforced profiles are in enforce mode.\n";
 
171
  print "$complain profiles are in complain mode.\n";
 
172
}
 
173
 
 
174
($processes, $profiles, $enforced, $complain) = count_processes();
 
175
 
 
176
if ($verbose) {
 
177
  print "Out of $processes processes running:\n";
 
178
  print "$profiles processes have profiles defined.\n";
 
179
  print "$enforced processes have profiles in enforce mode.\n";
 
180
  print "$complain processes have profiles in complain mode.\n";
 
181
}
 
182
 
 
183
exit $rc;