~apparmor-dev/apparmor/2_3

« back to all changes in this revision

Viewing changes to management/yastui/src/agents/ag_reports_confined

  • Committer: jrjohansen
  • Date: 2008-05-19 22:46:34 UTC
  • Revision ID: svn-v4:40609528-9d10-0410-9bd8-e926d5471da9:trunk:1247
move yastui to deprecated as the YaST ui is now being maintained and developed in the YaST svn

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/perl
2
 
# ------------------------------------------------------------------
3
 
#
4
 
#    Copyright (C) 2002-2005 Novell/SUSE
5
 
#
6
 
#    This program is free software; you can redistribute it and/or
7
 
#    modify it under the terms of version 2 of the GNU General Public 
8
 
#    License published by the Free Software Foundation.
9
 
#
10
 
# ------------------------------------------------------------------
11
 
 
12
 
use strict;
13
 
use Locale::gettext;
14
 
use POSIX;
15
 
use ycp;
16
 
 
17
 
setlocale(LC_MESSAGES, "");
18
 
textdomain("yast2-apparmor");
19
 
 
20
 
# Routines
21
 
################################################################################
22
 
sub debug {
23
 
 
24
 
        my $db = shift;
25
 
 
26
 
        if ((@$db) > 1)  {
27
 
                for ( @$db ) {
28
 
                        print "Prog: $_->{'prog'}, Prof: $_->{'prof'}, PID: $_->{'pid'}, ";
29
 
                        print "State: $_->{'state'}, Type: $_->{'type'}\n";
30
 
                }
31
 
                print "\n";
32
 
 
33
 
        } else {
34
 
                print "$db\n";
35
 
                exit 1;
36
 
        }
37
 
 
38
 
        return 0;       # Shouldn't get here
39
 
}
40
 
 
41
 
sub test_getCfInfo {
42
 
 
43
 
        my $cfTest = shift;
44
 
 
45
 
        my @cfDb = ();
46
 
 
47
 
        my $host = `hostname`;
48
 
        chomp($host);
49
 
 
50
 
        my $date = localtime;
51
 
 
52
 
        for (@$cfTest) {
53
 
                my $ref = ();
54
 
                my $all = undef;
55
 
                chomp;
56
 
 
57
 
                $ref->{'host'} = $host;
58
 
                $ref->{'date'} = $date;
59
 
 
60
 
        ($ref->{'pid'}, $ref->{'prog'}, $all) = split(/\s+/, $_, 3);
61
 
                $all = /\s*((not)*\s*confined\s*(by)*)/;
62
 
                $ref->{'state'} = $1;
63
 
                $ref->{'state'} =~ s/\s*by//g;
64
 
                $ref->{'state'} =~ s/not\s+/not-/g;
65
 
                ($ref->{'prof'}, $ref->{'type'}) = split(/\s+/, $_);
66
 
 
67
 
                push (@cfDb, $ref);
68
 
        }
69
 
 
70
 
        if ( scalar(@cfDb) < 1 ) {
71
 
                return "Error: parsing all messed up!!\n";
72
 
        }
73
 
 
74
 
        return (\@cfDb);
75
 
}
76
 
 
77
 
 
78
 
# Writes out file to allow for multiple pages in YaST form
79
 
#   -For large number of entries in the array
80
 
sub writePagingFile {
81
 
 
82
 
    my $db = shift;
83
 
 
84
 
    my $pagingFile = "/var/log/apparmor/reports/events.rpt";
85
 
 
86
 
    if ( open(PF, ">$pagingFile") ) {
87
 
 
88
 
        my $i = 1;
89
 
        my $page = 1;
90
 
        my $skip = 0;
91
 
 
92
 
        print PF "Page $page\n";
93
 
        $page++;
94
 
 
95
 
        for (@$db) {
96
 
 
97
 
            # Order (for YaST): "host", "date", "prog", "prof", "pid", "state", "type"
98
 
            print PF "$_->{'host'},$_->{'date'},$_->{'prog'},$_->{'prof'},$_->{'pid'},$_->{'state'},$_->{'type'}\n";
99
 
 
100
 
            if ( ($i % 100) == 0 && $skip == 0) {
101
 
                print PF "Page $page\n";
102
 
                $page++;
103
 
                $skip = 1;
104
 
            } else {
105
 
                $i++;
106
 
                $skip = 0;
107
 
            }
108
 
        }
109
 
 
110
 
        close PF;
111
 
 
112
 
    } else {
113
 
        ycp::y2error(sprintf(gettext("ag_reports_confined: Couldn't open %s for writing."), $pagingFile));
114
 
    }
115
 
 
116
 
    return;
117
 
}
118
 
 
119
 
sub readMultiAudLog {
120
 
 
121
 
    my $args = shift;
122
 
    my @audDb = ();
123
 
    my @rawDb = ();
124
 
    my @repList = ();
125
 
    my $dir = '/var/log/apparmor/reports-archived';
126
 
    my $logFile = undef;
127
 
    my $error = undef;
128
 
    my @errors = undef;
129
 
 
130
 
    if ( $args->{'repPath'} ) { $dir = $args->{'repPath'}; }
131
 
 
132
 
    # Get list of files in archived report directory
133
 
    if ( opendir (RDIR, $dir) ) {
134
 
 
135
 
        @repList = grep(/Applications.Audit/, readdir(RDIR));
136
 
        close RDIR;
137
 
 
138
 
    } else {
139
 
        $error = sprintf(gettext("Failure in readMultiAudLog() - couldn't open %s."), $dir);
140
 
        return($error);     # debug - exit instead?
141
 
    }
142
 
 
143
 
    for (@repList) {
144
 
 
145
 
        my $file = $_;
146
 
 
147
 
                next if $file =~ /$\.html/;
148
 
 
149
 
        # Cycle through each $file in $dir
150
 
        if (open (RPT, "<$dir/$file") ) {
151
 
            push(@rawDb, <RPT>);
152
 
            close RPT;
153
 
        } else {
154
 
            $error = sprintf(gettext("Problem in readMultiAudLog\(\) - couldn't open %s\/%s."), $dir, $file);
155
 
            #$error = "Problem in readMultiAudLog() - couldn't open $dir/$file.";
156
 
            push(@errors, $error);
157
 
        }
158
 
    }
159
 
 
160
 
        for (@rawDb) {
161
 
                next if /^#/;
162
 
                chomp;
163
 
        next if (! $_ || $_ eq "");
164
 
 
165
 
        my $rec = undef;
166
 
 
167
 
        ( $rec->{'host'}, $rec->{'date'}, $rec->{'prog'}, $rec->{'prof'}, $rec->{'pid'},
168
 
            $rec->{'state'}, $rec->{'type'} ) = split(/\,/, $_);
169
 
 
170
 
        push(@audDb, $rec);
171
 
    }
172
 
 
173
 
 
174
 
    return (\@audDb);
175
 
}
176
 
 
177
 
sub readAudLog {
178
 
 
179
 
    my $args = shift;
180
 
    my @audDb = ();
181
 
    my $dir = '/var/log/apparmor/reports-archived';
182
 
        my $logFile = undef;
183
 
        my $error = undef;
184
 
 
185
 
        if ($args->{'file'}) {
186
 
            $logFile = $args->{'file'};
187
 
        } else {
188
 
                $error = gettext("readAudLog() wasn't passed an input file.");
189
 
                ycp::y2error($error);
190
 
        exit 1;
191
 
        }
192
 
 
193
 
    if ( open(AUD, "<$dir/$logFile") ) {
194
 
 
195
 
        while (<AUD>) {
196
 
 
197
 
            next if /^#/;
198
 
            chomp;
199
 
            next unless ($_);
200
 
 
201
 
            my $rec = undef;
202
 
 
203
 
            ( $rec->{'host'}, $rec->{'date'}, $rec->{'prog'}, $rec->{'prof'}, $rec->{'pid'},
204
 
                $rec->{'state'}, $rec->{'type'} ) = split(/\,/, $_);
205
 
 
206
 
            push(@audDb, $rec);
207
 
        }
208
 
 
209
 
        close AUD;
210
 
 
211
 
    } else {
212
 
        $error = sprintf(gettext("readAudLog\(\) couldn't open %s."), $logFile);
213
 
        ycp::y2error($error);
214
 
        exit 1;
215
 
    }
216
 
 
217
 
    return (\@audDb);
218
 
}
219
 
 
220
 
sub getCfInfo {
221
 
 
222
 
        my $ref = ();
223
 
        my @cfDb = ();
224
 
 
225
 
        my $cfApp = '/usr/sbin/unconfined';
226
 
 
227
 
        if ( open (CF, "$cfApp |") ) {
228
 
 
229
 
                my $host = `hostname`;
230
 
                chomp($host);
231
 
 
232
 
                my $date = localtime;
233
 
 
234
 
                while(<CF>) {
235
 
 
236
 
            my $ref = ();
237
 
            my $all = undef;
238
 
            my $info = undef;
239
 
            $ref->{'host'} = $host;
240
 
            $ref->{'date'} = $date;
241
 
            chomp;
242
 
 
243
 
            ($ref->{'pid'}, $ref->{'prog'}, $all) = split(/\s+/, $_, 3);
244
 
            $info = $all;
245
 
            $all = /\s*((not)*\s*confined\s*(by)*)/;
246
 
            $ref->{'state'} = $1;
247
 
            $ref->{'state'} =~ s/\s*by//g;
248
 
            $ref->{'state'} =~ s/not\s+/not-/g;
249
 
 
250
 
            if ($ref->{'state'} =~ /not-confined/ ) {
251
 
                $ref->{'prof'} = "-";
252
 
                $ref->{'type'} = "-";
253
 
            } else {
254
 
                ($info) = (split(/\'/, $info, 2))[1];
255
 
                ($ref->{'prof'}, $ref->{'type'}) = split(/\s+/, $info);
256
 
                $ref->{'type'} =~ s/\(|\)|\'//g;
257
 
            }
258
 
 
259
 
            if ( $ref->{'prog'} eq "") { $ref->{'prog'} = "-"; }
260
 
            if ( $ref->{'prof'} eq "") { $ref->{'prof'} = "-"; }
261
 
            if ( $ref->{'pid'} eq "") { $ref->{'pid'} = "-"; }
262
 
            if ( $ref->{'state'} eq "") { $ref->{'state'} = "-"; }
263
 
            if ( $ref->{'type'} eq "") { $ref->{'type'} = "-"; }
264
 
 
265
 
            push (@cfDb, $ref);
266
 
                }
267
 
                close CF;
268
 
 
269
 
        } else {
270
 
                my $error = sprintf(gettext("Can't run %s.  Exiting."), $cfApp);
271
 
                ycp::y2error($error);
272
 
                return $error;
273
 
        }
274
 
 
275
 
        return (\@cfDb);
276
 
}
277
 
 
278
 
# Main
279
 
################################################################################
280
 
 
281
 
 
282
 
while ( <STDIN> ) {
283
 
 
284
 
    my ($command, $path, $args) = ycp::ParseCommand ($_);
285
 
 
286
 
    if ( $command && $path && $args ) {
287
 
 
288
 
                my $db = undef;
289
 
 
290
 
                if ($args->{'audArch'} && $args->{'audArch'} == 1) {
291
 
 
292
 
                        if (! $args->{'single'} || $args->{'single'} != 1 ) {
293
 
                                $db = readMultiAudLog($args);
294
 
                        } else {
295
 
                                $db = readAudLog($args);
296
 
                        }
297
 
 
298
 
                } else {
299
 
                        $db = getCfInfo();
300
 
                }
301
 
 
302
 
                writePagingFile($db);
303
 
            ycp::Return( $db );
304
 
 
305
 
            } else {
306
 
                my $error = gettext("ag_reports_confined: Missing instruction or argument!");
307
 
                ycp::y2error($error);
308
 
                ycp::Return($error);
309
 
                exit 1;
310
 
            }
311
 
}
312
 
 
313
 
exit 0;
314