aboutsummaryrefslogtreecommitdiffstats
path: root/english/security/parse-advisory.pl
blob: bd6796e120282cdac4989641b8f9b90eda515a45 (plain) (blame)
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
#!/usr/bin/perl
#
# parse-advisory.pl
#
# this script parses files in
# security.debian.org:/org/security.debian.org/advisories/DSA/
# and makes wmls out of them
# 
# Copyright (C) 2001 Josip Rodin
# Copyright (c) 2002,3 Josip Rodin, Martin Schulze
# Licensed under the GNU General Public License version 2.

use WWW::Mechanize;
use File::Path qw(remove_tree make_path);

my $debug = 0;
my $adv = $ARGV[0];
if ($adv eq "-d") {
    $debug = 1;
    $adv = $ARGV[1];
}

$adv || die "you must specify a parameter (original advisory file)!\n";
die "that advisory file either ain't there or doesn't have anything in it!\n" unless -s $adv;

# i'm lame, so shoot me
my %longmoy = (	en => [ 
  'January', 'February', 'March', 'April', 'May', 'June',
  'July', 'August', 'September', 'October', 'November', 'December' ]
);
my %shortmoy = ( en => [
  'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
  'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
);

my $curyear = (localtime())[5] + 1900;

my %arch = (
	    'alpha'   => 'Alpha',
	    'amd64'   => 'AMD64',
	    'hppa'    => 'HP Precision',
	    'i386'    => 'Intel IA-32',
	    'ia64'    => 'Intel IA-64',
	    'm68k'    => 'Motorola 680x0',
	    'mips'    => 'Big-endian MIPS',
	    'mipsel'  => 'Little-endian MIPS',
	    's390'    => 'IBM S/390',
	    'sparc'   => 'Sun Sparc',
	    'powerpc' => 'PowerPC',
	    'arm'     => 'ARM',
	    'armel'   => 'ARM EABI',
	    );

open ADV, $adv;
foreach $l (<ADV>) {
  if ($l =~ /^Debian Security Advisory (DSA[- ]\d+-\d+)/) {
    $dsa = $1;
  }
  if ($l =~ /^(\w+)\s+(\d+)(\D\D)?, (\d+)/) {
    $month = $1; $day = $2; $year = $4;
    while ($i < 12) {
      if ($month eq $longmoy{en}[$i]) {
        $month = $i + 1;
        $date = "$year-$month-$day";
        $i = 12;
      }
      elsif ($month eq $shortmoy{en}[$i]) {
        $month = $i + 1; 
        $date = "$year-$month-$day";
        $i = 12;
      }
      $i++
    }
  }
  if ($l =~ /^Package(?:s)*\s*: (.+)\s*/) {
    $package = $1;
  }
  if ($l =~ /^(Vulnerability)\s*: (.+)\s*/) {
    $desc = $2;
    $desc .= ' vulnerabilities' if $desc =~ /(several|multiple)\s*$/;
  }
  if ($desc eq "") { $desc = "security update"; }
  if ($l =~ /^(Debian Bug\(?s?\)?)\s*: (.+)/i) {
      for $id (split (/,? /, $2)) {
	  push @dbids, "Bug#".$id if ($id ne "none");
      }
  }
  if ($l =~ /^(CVE (names?|id\(?s?\)?|references?)?|CERT advisor(y|ies))\s*: (.+)/i) {
    push @dbids, join (" ", split (/,? /, $4));
  }
  if ($l =~ /^\s+((?:CVE-\d+-\d+[ ]*)+)$/i) {
    push @dbids, join (" ", split (/,? /, $1));
  }
  if ($l =~ /^\s+((?:VU#\d+[ ]*)+)$/i) {
    push @dbids, join (" ", split (/,? /, $1));
  }
  if ($l =~ /^Bugtraq Ids?\s*: (.+)/i) {
      for $id (split (/,? /, $1)) {
	  push @dbids, "BID".$id;
      }
  }
  last if ($l =~ /Further information about Debian Security Advisories.*$/i);
  last if ($l =~ /Thanks to.+for proof read/i);
  $mi = 0 if ($l =~ /^(wget url|Obtaining updates|Upgrade Instructions)/i);
  $moreinfo .= "<p>" if ($mi && $nl);
  $nl = 0;
  $nl = 1 if ($mi && ($l eq "\n") && $moreinfo);
  if ($mi) {
    if ($mi > 1) {
      $moreinfo .= $l;
    } else {
      $moreinfo .= "\n<p>".$l;
      $mi++;
    }
  }
  $headersnearingend++ if ($l =~ /^Package        :/);
  if ($headersnearingend && $l =~ /^\s*$/) {
    $mi++;
    $headersnearingend = 0;
  }

  $f++ if ($l =~ /^Debian (GNU\/Linux.*alias|.*\(.*\)).*/);
  $f = 0 if ($l =~ /^((- )?-- |(  )?These (files|packages) will (probably )?be moved)/);
  $files .= $l if ($f);
}
close ADV;


$moreinfo =~ s/(- )?-+\n//g;
$moreinfo =~ s/\n\n$/\n/s;
$moreinfo =~ s/\n<p>\n$//;
$moreinfo =~ s/\n<p>note\:/<p><b>Note<\/b>:/ig;
$moreinfo =~ s/(\s)"(\w[\w\.,'\(\)\s]*?\w)"([\:\.',\(\)\s])/$1<q>$2<\/q>$3/g;
$moreinfo =~ s/(\s)'(\w[\w\.,\(\)\s]*?\w)'([\:\.,\(\)\s])/$1<q>$2<\/q>$3/g;
$moreinfo =~ s|\n+(<p>(CAN\|CVE)-\d+-\d+[\:]*)\s?(\s*)(\S+)|\n\n$1\n$3$4|g;
$moreinfo =~ s/\n\n/<\/p>\n\n/sg;
$moreinfo =~ s|\n<p>((CAN\|CVE)-\d+-\d+[^\n]*)</p>\n|\n<li>$1\n|g;
$moreinfo =~ s|\n<p>((CAN\|CVE)-\d+-\d+[^\n]*)\n|\n<li>$1\n<p>\n|g;
$moreinfo =~ s|((CAN\|CVE)-\d+-\d+)|<a href="https://security-tracker.debian.org/tracker/$1">$1</a>|g;
$moreinfo =~ s|</p>\n\n<p>\n<p>(\w* \w* stable)|</p></li>\n\n</ul>\n\n<p>$1|; 
$moreinfo =~ s|<p>(\s+)|$1<p>|g;
$moreinfo =~ s|<p><p>|<p>|g;
$moreinfo =~ s|</p>\n\n<li>|</p></li>\n\n<li>|g;
$moreinfo =~ s|</li>\n\n<li>|\n\n<ul>\n\n<li>|;
$moreinfo =~ s|(\s+)(https?://[^\s<>{}\\^\[\]\"\'\`]+)|$1<a href="$2">$2</a>|g;

# matrix creation start
#  in matrix lines, each item cannot have space charecter sequence in it
#     space charecter sequence (>=2) is treated as a delimiter
# $matrix_h is used as header and $matrix_f is used as footer
my $matrix_h = qq|<div class="centerdiv">\n  <table cellspacing="0" cellpadding="2">\n|;
my $matrix_f = "  </table>\n</div>\n";
$moreinfo =~ s{(<p>The following matrix[\s\S]+?</p>\n+)\s+<p>([\s\S]+?)</p>}{$1<matrix>\n&nbsp;  $2\n</matrix>}g;
$moreinfo =~ m|<matrix>\n([\s\S]+?)</matrix>|;
my $matrix = $1;
$matrix =~ s/\n\s+/\n/g;
my @matrixl = split(/\n/,$matrix);
for my $i(0 .. $#matrixl){
  $matrixl[$i] = "    <tr>\n      <td>" .
		 join("</td>\n      <td>", split(/\s{2,}/,$matrixl[$i])) .
		 "</td>\n    </tr>\n";
# 1st line, use <th>
  $matrixl[$i] =~ s/td>/th>/g if($i<1);
}
$matrix = join("", @matrixl);
$moreinfo =~ s|<matrix>\n([\s\S]+?)</matrix>|$matrix_h$matrix$matrix_f|;
# matrix end

if (($moreinfo =~ /<ul>\n\n<li>/) && ($moreinfo !~ /<\/li>\n\n<\/ul>/)){
   $moreinfo =~ s{</p>\n\n<p>((\w+ \w+ \w* ?(old ?stable|stable|testing))|Th[eo]se)}{</p></li>\n\n</ul>\n\n<p>$1}; }
chomp ($moreinfo);

$files =~ s/(- )?-+\n//g;
$files =~ s/\n\n$/\n/s;

$files =~ s/.+ updates are available for .+\n//g;

$files =~ s/(  )?    (Size\/)?MD5 checksum: (\s*\d+ )?\w{32}\n//sg;
$files =~ s/(  )?Source archives:/<dt><source \/>/sg;
$files =~ s/(  )?Architecture.independent \w+:\n/<dt><arch-indep \/>\n/sg;
$files =~ s/HP Precision architecture/HPPA architecture/gi;
$files =~ s/(?:  )?(\w+) architecture \(([\w -()\/]+)\)/<dt>$arch{$1}:/sg;
$files =~ s/(?:  )?([\w -\/]+) architecture:/<dt>$1:/sg;
$files =~ s/(?:  )?  (http:\S+)/  <dd><fileurl $1 \/>/sg;
$files =~ s,[\n]?Debian (GNU/Linux )?(\S+) (alias |\()([a-z]+)\)?,</dl>\n\n<h3>Debian $2 ($4)</h3>\n\n<dl>,sg;

my @f = ();
my $ign = 0;
foreach $_ (split (/\n/, $files)) {
    if (!$ign && /was released/) {
	$ign = 1;
    } elsif ($ign && /^$/) {
	$ign = 0;
    } elsif (!$ign) {
	push (@f, $_);
    }
}
$files = join ("\n", @f);

if (defined($package) && $dsa =~ /DSA[- ](\d+)-(\d+)/ ) {
    $dsa_number=$1;
    $dsa_revision=$2;
    $wml = "$curyear/dsa-$dsa_number.wml";
    $data = "$curyear/dsa-$dsa_number.data";
    $pagetitle = "DSA-$dsa_number-$dsa_revision $package";
} else {
    die ("Could not parse advisory filename '$adv'. Must contain Package and DSA number information");
}
$data = $wml = "-" if ($debug);

if (!(-d $curyear)){
  print "directory $curyear does not exist!  Creating $curyear\n";
  make_path($curyear,{ verbose => 0, mode => 0755 }) or print "Could not create $curyear: $!\n";
}

&make_data;
&make_wml;
print "double check the content of $wml and $data, and eventually fix it before commit them.\n";
&make_index;
&make_makefile;


sub make_data{
  if (-f $data){
    print "$data already exists!\n";
    return;
  }
  $files =~ s,^</dl>\n\n,,;
  open DATA, ">", "$data";
  print DATA "<define-tag pagetitle>$pagetitle</define-tag>\n";
  print DATA "<define-tag report_date>$date</define-tag>\n";
  print DATA "<define-tag secrefs>@dbids</define-tag>\n" if @dbids;
  print DATA "<define-tag packages>$package</define-tag>\n";
  print DATA "<define-tag isvulnerable>yes</define-tag>\n";
  print DATA "<define-tag fixed>yes</define-tag>\n";
  print DATA "<define-tag fixed-section>no</define-tag>\n"; # Kaare, 2011-01-24: Line added because the "fixed in" section is no longer available
  print DATA "\n#use wml::debian::security\n\n";
  print DATA "$files\n\n</dl>\n";
  close DATA;
}

sub make_wml{
  if (-f $wml){
    print "$wml already exists!\n";
    return;
  }
  open WML, ">", "$wml";
  print WML "<define-tag description>$desc</define-tag>\n";
  print WML "<define-tag moreinfo>$moreinfo</p>\n</define-tag>\n";
  print WML "\n# do not modify the following line\n";
  print WML "#include \"\$(ENGLISHDIR)/security/$data\"\n";
  printf WML "# %sId: \$\n", "\$";
  close WML;
}

sub make_index{
  return if (-f "$curyear/index.wml");
  print "$curyear/index.wml does not exist! Creating...";
  my $ldo = '<a href="https://lists.debian.org/';
  my $dsan = 'debian-security-announce';
  my $index = "<define-tag pagetitle>Security Advisories from $curyear</define-tag>\n";
  $index .= qq|#use wml::debian::template title="<pagetitle>" GEN_TIME="yes"\n|;
  $index .= qq|#use wml::debian::recent_list\n\n|;
  $index .= qq|<:= get_recent_list ('.', '0', '\$(ENGLISHDIR)/security/$curyear', '', 'dsa-\\d+' ) :>\n\n|;
  $index .= qq|<p>You can get the latest Debian security advisories by subscribing to our\n|;
  $index .= qq|$ldo$dsan/">\\\n|;
  $index .= qq|<strong>$dsan</strong></a> mailing list.\n|;
  $index .= qq|You can also $ldo$dsan/$dsan-2013/">\\\n|;
  $index .= qq|browse the archives</a> for the list.</p>\n|;
  open INDEX, ">", "$curyear/index.wml";
  print INDEX $index;
  close INDEX;
  print "done\n";
  print "Do not forget to commit index.wml.\n";
}

sub make_makefile{
  return if (-f "$curyear/Makefile");
  print "$curyear/Makefile does not exist! Creating...";
  my $makefile = qq|# If this makefile is not generic enough to support a translation,\n|;
  $makefile .= qq|# please contact debian-www.\n\n|;
  $makefile .= qq|WMLBASE=../..\n|;
  $makefile .= qq|CUR_DIR=security/2013\n|;
  $makefile .= qq|SUBS=\n\n|;
  $makefile .= qq|GETTEXTFILES += security.mo\n\n|;
  $makefile .= qq|NOGENERICDEP := true\n|;
  $makefile .= qq|include \$(WMLBASE)/Make.lang\n\n\n|;
  $makefile .= qq|# The "\| $(VCSREVCACHE)" here is an order-only prerequisite - always|;
  $makefile .= qq|# check that the prerequisite exists and is up to date, but don't|;
  $makefile .= qq|# rebuild everything whenever it's updated - see|;
  $makefile .= qq|# https://www.gnu.org/software/make/manual/html_node/Prerequisite-Types.html|;

  $makefile .= qq|\%.\$(LANGUAGE).html: \%.wml \$(TEMPLDIR)/security.wml \\\n|;
  $makefile .= qq|  \$(ENGLISHSRCDIR)/\$(CUR_DIR)/\%.data \$(GETTEXTDEP) \| \$(VCSREVCACHE)\n|;
  $makefile .= qq|\t\$(WML) \$(<F)\n\n|;
  $makefile .= qq|index.\$(LANGUAGE).html: index.wml \$(sort \$(wildcard dsa-[0-9]*.wml)) \\\n|;
  $makefile .= qq|  \$(ENGLISHSRCDIR)/\$(CUR_DIR)/dsa-[0-9]*.data \\\n|;
  $makefile .= qq|  \$(TEMPLDIR)/template.wml \$(TEMPLDIR)/recent_list.wml \$(GETTEXTDEP) \| \$(VCSREVCACHE)\n|;
  $makefile .= qq|\t\$(WML) \$(<F)\n|;
  open MAKEFILE, ">", "$curyear/Makefile";
  print MAKEFILE $makefile;
  close MAKEFILE;
  print "done\n";
  print "Do not forget to commit Makefile.\n";
}

© 2014-2024 Faster IT GmbH | imprint | privacy policy