aboutsummaryrefslogtreecommitdiffstats
path: root/lib/MGA/Advisories.pm
blob: b9e3efc32e34b264382a5c7f56a7edd501de395b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package MGA::Advisories;

use warnings;
use strict;
use YAML qw(LoadFile DumpFile Load);
use Template;
use DateTime;
use Email::Sender::Simple qw(try_to_sendmail);
use Email::Simple;
use Email::Simple::Creator;
use LWP::UserAgent;
use File::Basename;
#use Data::Dump qw(dd);

our $config_file = '/usr/share/mga-advisories/config';
our $config = LoadFile($ENV{MGAADV_CONF} || $config_file);
our $home_config_file = $ENV{HOME} . '/.mga-advisories/mga-advisories.conf';
my $custom_config_file = -f $home_config_file ? $home_config_file 
        : '/etc/mga-advisories.conf';
my $custom_config = LoadFile($custom_config_file);
foreach my $k (keys %$custom_config) {
    $config->{$k} = $custom_config->{$k};
}

my %basename = (
    CVE => sub { $_[0] },
    ID  => sub { $_[0] },
    rel => sub { $_[0] },
    src => sub { 'src_' . $_[0] },
);

my %tools = (
    pkgname => sub { $_[0] =~ m/(.+)-[^-]+-[^-]+/; $1; },
);

my @report_logs;
sub report_log {
    push @report_logs, @_;
}

sub report_exit {
    report_log($_[0]);
    send_report({ error => $_[0] });
    exit 1;
}

sub status_file {
    $config->{status_dir} . '/' . $_[0];
}

sub save_status {
    my ($advdb, $adv) = @_;
    return if $advdb->{advisories}{$adv}{no_save_status};
    my $statusfile = status_file($adv);
    DumpFile($statusfile, $advdb->{advisories}{$adv}{status});
}

sub get_advisories_from_dir {
    my %advisories;
    foreach my $advfile (glob "$config->{advisories_dir}/*.adv") {
        my $adv = LoadFile($advfile);
        if (!$adv->{ID}) {
            next unless $config->{mode} eq 'qa';
            $adv->{ID} = next_id('TODO', keys %advisories);
            $adv->{no_save_status} = 1;
        }
        report_exit("Duplicate advisory $adv->{ID}") if $advisories{$adv->{ID}};
        report_exit("Unknown type $adv->{type}") unless
                $config->{advisory_types}{$adv->{type}};
        $advisories{$adv->{ID}} = $adv;
        my $statusfile = status_file($adv->{ID});
        $adv->{status} = -f $statusfile ? LoadFile($statusfile) : {};
    }
    return \%advisories;
}

sub next_id {
    my $prefix = shift;
    my $year = DateTime->now->year;
    my @used_ids = map { m/^$prefix-$year-(\d+)$/ ? int $1 : () } @_;
    my $newid = (0, sort { $a <=> $b } @used_ids)[-1] + 1;
    return sprintf("%s-%s-%.4d", $prefix, $year, $newid);
}

sub assign_id {
    my ($bugnum) = @_;
    my $advfile = "$config->{advisories_dir}/$bugnum.adv";
    my $adv = LoadFile($advfile);
    if ($adv->{ID}) {
        print STDERR "$bugnum already has an ID assigned: $adv->{ID}\n";
        return;
    }
    my $type = $config->{advisory_types}{$adv->{type}}{prefix};
    if (!$type) {
        print STDERR "Unknow type $adv->{type}\n";
        return;
    }
    $adv->{ID} = next_id($type, keys %{get_advisories_from_dir()});
    open(my $fh, '>>', $advfile) or die "Error opening $advfile";
    print $fh "ID: $adv->{ID}\n";
    close $fh;
    print "Assigned ID $adv->{ID} to advisory $bugnum\n";
}

sub advdb_dumpfile {
    $config->{advdb_dumpfile} || $ENV{HOME} . '/.mga-advisories/advisories.yaml';
}

sub get_advisories_from_dump {
    my $advfile = advdb_dumpfile;
    return -f $advfile ? LoadFile($advfile) : {};
}

sub get_advisories {
    return $config->{mode} eq 'dump' ? get_advisories_from_dump
        : get_advisories_from_dir;
}

sub download_advisories {
    my $oldadvisories = get_advisories_from_dump;
    my $ua = LWP::UserAgent->new;
    my $resp = $ua->get($config->{dump_url});
    die "Error loading $config->{dump_url}" unless $resp->is_success;
    my $newadvisories = Load($resp->decoded_content);
    my @newadv = grep { ! $oldadvisories->{$_} } keys %$newadvisories;
    if (@newadv) {
        my %n;
        my @v = @{$newadvisories}{@newadv};
        @n{@newadv} = @v;
        print "New advisories have been downloaded :\n";
        listadv({advisories => \%n});
    } else {
        print "No new advisories available\n";
    }
    if (!-d dirname(advdb_dumpfile)) {
        mkdir dirname(advdb_dumpfile)
                || die "Error creating directory " . dirname(advdb_dumpfile);
    }
    open(my $fh, '>', advdb_dumpfile)
        || die "Could not open " . advdb_dumpfile;
    print $fh $resp->decoded_content;
    close $fh;
}

sub publish_advisories {
    my ($advdb) = @_;
    foreach my $adv (keys %{$advdb->{advisories}}) {
        next if $advdb->{advisories}{$adv}{status}{published};
        $advdb->{advisories}{$adv}{status}{published} =
                $advdb->{advisories}{$adv}{pubtime} || time();
        save_status($advdb, $adv);
    }
}

sub adv_sort {
    my $advdb = shift;
    sort {
        my $now = time;
        my $pa = $advdb->{advisories}{$a}{status}{published} || $now;
        my $pb = $advdb->{advisories}{$b}{status}{published} || $now;
        return $pa == $pb ? $b cmp $a : $pb cmp $pa;
    } @_;
}

sub sort_advisories {
    my ($advdb) = @_;
    foreach my $adv (keys %{$advdb->{advisories}}) {
        push @{$advdb->{by_type}{$advdb->{advisories}{$adv}{type}}}, $adv;
        foreach my $cve (@{$advdb->{advisories}{$adv}{CVE}}) {
            push @{$advdb->{by_cve}{$cve}}, $adv;
        }
        foreach my $rel (keys %{$advdb->{advisories}{$adv}{src}}) {
            push @{$advdb->{by_rel}{$rel}}, $adv;
            foreach my $media (keys %{$advdb->{advisories}{$adv}{src}{$rel}}) {
                push @{$advdb->{by_media}{$media}}, $adv;
                my %pkgs;
                foreach my $srpm (@{$advdb->{advisories}{$adv}{src}{$rel}{$media}}) {
                    my $pkgname = $tools{pkgname}->($srpm);
                    push @{$advdb->{by_src}{$pkgname}}, $adv
                        unless grep { $_ eq $adv } @{$advdb->{by_src}{$pkgname}};
                }
            }
        }
    }
    foreach my $by ('by_type', 'by_cve', 'by_rel', 'by_media', 'by_src') {
        foreach my $k (keys %{$advdb->{$by}}) {
            $advdb->{$by}{$k} = [ adv_sort($advdb, @{$advdb->{$by}{$k}}) ];
        }
    }
    $advdb->{sorted} = [ adv_sort($advdb, keys %{$advdb->{advisories}}) ];
}

sub process_template {
    my ($template, $src, $vars, $dest, $ext) = @_;
    foreach my $extension ($ext ? $ext : @{$config->{output_format}}) {
        next unless -f "$config->{tmpl_dir}/$src.$extension";
        $template->process("$src.$extension", $vars, ref $dest ? $dest : "$dest.$extension")
                || die $template->error, "\n";
    }
}

sub output_pages {
    my ($advdb) = @_;
    my $template = Template->new(
        INCLUDE_PATH => $config->{tmpl_dir},
        OUTPUT_PATH  => $config->{out_dir},
    );
    foreach my $adv (keys %{$advdb->{advisories}}) {
        my $vars = {
            config   => $config,
            advisory => $adv,
            advdb    => $advdb,
            basename => \%basename,
            tools    => \%tools,
        };
        process_template($template, 'advisory', $vars, $basename{ID}->($adv));
    }
    foreach my $by (['rel', 'by_rel'], ['CVE', 'by_cve'], ['src', 'by_src']) {
        foreach my $r (keys %{$advdb->{$by->[1]}}) {
            my $vars = {
                config   => $config,
                $by->[0] => $r,
                advdb    => $advdb,
                basename => \%basename,
                tools    => \%tools,
            };
            process_template($template, $by->[1], $vars, $basename{$by->[0]}->($r));
        }
    }
    my $vars = {
        config   => $config,
        advdb    => $advdb,
        basename => \%basename,
        tools    => \%tools,
    };
    process_template($template, 'index', $vars, 'index');
    process_template($template, 'advisories', $vars, 'advisories');
    process_template($template, 'CVE', $vars, 'CVE');
}

sub send_adv_mail {
    my ($advdb) = @_;
    return unless $config->{send_adv_mail} eq 'yes';
    return unless $config->{mode} eq 'site';
    my $template = Template->new(
        INCLUDE_PATH => $config->{tmpl_dir},
    );
    foreach my $adv (keys %{$advdb->{advisories}}) {
        next if $advdb->{advisories}{$adv}{no_mail};
        next if $advdb->{advisories}{$adv}{no_save_status};
        next if $advdb->{advisories}{$adv}{status}{mail_sent};
        my $mailcontent;
        my $vars = {
            config   => $config,
            advisory => $adv,
            advdb    => $advdb,
            basename => \%basename,
            tools    => \%tools,
        };
        process_template($template, 'advisory', $vars, \$mailcontent, 'txt');
        my $email = Email::Simple->create(
            header => [
                To   => $config->{adv_mail_to},
                From => $config->{adv_mail_from},
                Subject => "$adv: " . $advdb->{advisories}{$adv}{subject},
            ],
            body   => $mailcontent
        );
        if (try_to_sendmail($email)) {
            report_log("Advisory mail for $adv sent");
            $advdb->{advisories}{$adv}{status}{mail_sent} = time();
            save_status($advdb, $adv);
        } else {
            report_log("Error sending advisory mail $adv");
        }
    }
}

sub send_report {
    my ($advdb) = @_;
    return unless @report_logs;
    my $template = Template->new(
        INCLUDE_PATH => $config->{tmpl_dir},
    );
    my $reportcontent;
    my $vars = {
        config      => $config,
        advdb       => $advdb,
        report_logs => \@report_logs,
    };
    process_template($template, 'report', $vars, \$reportcontent, 'txt');
    if ($config->{send_report_mail} eq 'yes' && $config->{mode} eq 'site') {
        my $email = Email::Simple->create(
            header => [
                To   => $config->{report_mail_to},
                From => $config->{report_mail_from},
                Subject => $advdb->{error} ? 'Advisories Error' : 'Advisories Update',
            ],
            body   => $reportcontent
        );
        try_to_sendmail($email);
    } else {
        print { $advdb->{error} ? *STDERR : *STDOUT } $reportcontent;
    }
}

sub dumpdb {
    my ($advdb) = @_;
    DumpFile($config->{out_dir} . '/advisories.yaml', $advdb->{advisories});
}

sub newadv {
    my ($type, $bugnum) = @_;
    my $file = $config->{advisories_dir} . '/' . $bugnum . '.adv';
    if (-f $file) {
        print STDERR "File $file already exists\n";
        return undef;
    }
    my $template = Template->new(
        INCLUDE_PATH => $config->{tmpl_dir},
        OUTPUT_PATH  => $config->{advisories_dir},
    );
    my $vars = {
        type   => $type,
        bugnum => $bugnum,
    };
    process_template($template, 'newadvisory', $vars, $bugnum, 'adv');
    return $file;
}

sub listadv {
    my ($advdb, @filter) = @_;
    my @advlist = keys %{$advdb->{advisories}};
    foreach my $f (@filter) {
        my $l = $advdb->{by_type}{$f} || $advdb->{by_cve}{$f}
                || $advdb->{by_rel}{$f} || $advdb->{by_media}{$f}
                || $advdb->{by_src}{$f} || [];
        my %z;
        @z{@$l} = (1) x @$l;
        @advlist = grep { $z{$_} } @advlist;
    }
    print map { "$_ . $advdb->{advisories}{$_}{subject}\n" }
        adv_sort($advdb, @advlist);
}

sub showadv {
    my ($advdb, $adv) = @_;
    if (!$advdb->{advisories}{$adv}) {
        print STDERR "Cannot find advisory $adv\n";
        return undef;
    }
    my $template = Template->new(
        INCLUDE_PATH => $config->{tmpl_dir},
    );
    my $vars = {
        config   => $config,
        advisory => $adv,
        advdb    => $advdb,
        basename => \%basename,
        tools    => \%tools,
    };
    my $advtxt;
    process_template($template, 'advisory', $vars, \$advtxt, 'txt');
    print $advtxt;
}

1;