~ubuntu-branches/ubuntu/wily/apparmor/wily

« back to all changes in this revision

Viewing changes to utils/unconfined

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook
  • Date: 2011-04-27 10:38:07 UTC
  • mfrom: (5.1.118 natty)
  • Revision ID: james.westby@ubuntu.com-20110427103807-ym3rhwys6o84ith0
Tags: 2.6.1-2
debian/copyright: clarify for some full organization names.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/perl -w
2
 
#
3
 
# $Id: unconfined 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
 
#  unconfined -
25
 
#    audit local system for processes listening on network connections
26
 
#    that are not currently running with a profile.
27
 
 
28
 
use Getopt::Long;
29
 
 
30
 
use Immunix::SubDomain;
31
 
use Locale::gettext;
32
 
use POSIX;
33
 
 
34
 
setlocale(LC_MESSAGES, "");
35
 
textdomain("apparmor-utils");
36
 
 
37
 
# options variables
38
 
my $paranoid = '';
39
 
my $help     = '';
40
 
 
41
 
GetOptions(
42
 
    'paranoid' => \$paranoid,
43
 
    'help|h'   => \$help,
44
 
);
45
 
 
46
 
# tell 'em how to use it...
47
 
&usage && exit if $help;
48
 
 
49
 
sub usage {
50
 
    printf(gettext("Usage: %s [ --paranoid ]\n"), $0);
51
 
    exit 0;
52
 
}
53
 
 
54
 
my $subdomainfs = check_for_subdomain();
55
 
 
56
 
die gettext("SubDomain does not appear to be started. Please enable SubDomain and try again.") . "\n"
57
 
  unless $subdomainfs;
58
 
 
59
 
my @pids;
60
 
if ($paranoid) {
61
 
    opendir(PROC, "/proc") or die gettext("Can't read /proc\n");
62
 
    @pids = grep { /^\d+$/ } readdir(PROC);
63
 
    closedir(PROC);
64
 
} else {
65
 
    if (open(NETSTAT, "/bin/netstat -nlp |")) {
66
 
        while (<NETSTAT>) {
67
 
            chomp;
68
 
            push @pids, $5
69
 
              if /^(tcp|udp)\s+\d+\s+\d+\s+\S+\:(\d+)\s+\S+\:(\*|\d+)\s+(LISTEN|\s+)\s+(\d+)\/(\S+)/;
70
 
        }
71
 
        close(NETSTAT);
72
 
    }
73
 
}
74
 
 
75
 
for my $pid (sort { $a <=> $b } @pids) {
76
 
    my $prog = readlink "/proc/$pid/exe" or next;
77
 
    my $attr;
78
 
    if (open(CURRENT, "/proc/$pid/attr/current")) {
79
 
        while (<CURRENT>) {
80
 
            chomp;
81
 
            $attr = $_ if (/^\// || /^null/);
82
 
        }
83
 
        close(CURRENT);
84
 
    }
85
 
    if (not $attr) {
86
 
        if ($prog =~ m/^(\/usr\/bin\/python|\/usr\/bin\/perl|\/bin\/bash)$/) {
87
 
 
88
 
            #my $scriptname = (split(/\0/, `cat /proc/$pid/cmdline`))[1];
89
 
            my $cmdline = `cat /proc/$pid/cmdline`;
90
 
            $cmdline =~ s/\0/ /g;
91
 
            $cmdline =~ s/\s+$//;
92
 
            chomp $cmdline;
93
 
            print "$pid $prog ($cmdline) " . gettext("not confined\n");
94
 
        } else {
95
 
            print "$pid $prog " . gettext("not confined\n");
96
 
        }
97
 
    } else {
98
 
        if ($prog =~ m/^(\/usr\/bin\/python|\/usr\/bin\/perl|\/bin\/bash)$/) {
99
 
 
100
 
            #my $scriptname = (split(/\0/, `cat /proc/$pid/cmdline`))[1];
101
 
            my $cmdline = `cat /proc/$pid/cmdline`;
102
 
            $cmdline =~ s/\0/ /g;
103
 
            $cmdline =~ s/\s+$//;
104
 
            chomp $cmdline;
105
 
            print "$pid $prog ($cmdline) " . gettext("confined by") . " '$attr'\n";
106
 
        } else {
107
 
            print "$pid $prog " . gettext("confined by") . " '$attr'\n";
108
 
        }
109
 
    }
110
 
}