]> git.donarmstrong.com Git - debbugs.git/blob - cgi/bugreport.cgi
* Merge changes to bugreport.cgi from mainline
[debbugs.git] / cgi / bugreport.cgi
1 #!/usr/bin/perl -wT
2
3 package debbugs;
4
5 use strict;
6
7 if (defined $ENV{REMOTE_ADDR} and $ENV{REMOTE_ADDR} =~ m/^(72\.17\.168\.57|60\.238\.143\.248|61\.224\.27\.141|146\.82\.138\.7|61\.214\.28\.119|84\.189\.46\.3|82\.124\.165\.25|220\.181\.26\.108|220\.95\.221\.\d+|82\.120\.77\.220|80\.68\.89\.71|66\.227\.249\.188|194\.46\.224\.153|66\.246\.72\.81|60\.42\.181\.163)$/) {
8     print "Content-Type: text/html\n\nGo away.";
9     sleep(5);
10     exit 0;
11 }
12
13 use POSIX qw(strftime tzset);
14 use MIME::Parser;
15 use MIME::Decoder;
16 use IO::Scalar;
17 use IO::File;
18
19 #require '/usr/lib/debbugs/errorlib';
20 require './common.pl';
21
22 require '/etc/debbugs/config';
23 require '/etc/debbugs/text';
24
25 use vars(qw($gEmailDomain $gHTMLTail $gSpoolDir $gWebDomain));
26
27 # for read_log_records
28 use Debbugs::Log;
29 use Debbugs::MIME qw(convert_to_utf8 decode_rfc1522);
30
31 use Scalar::Util qw(looks_like_number);
32
33 my %param = readparse();
34
35 my $tail_html;
36
37 my $ref = $param{'bug'} || quitcgi("No bug number");
38 $ref =~ /(\d+)/ or quitcgi("Invalid bug number");
39 $ref = $1;
40 my $short = "#$ref";
41 my $msg = $param{'msg'} || "";
42 my $att = $param{'att'};
43 my $boring = ($param{'boring'} || 'no') eq 'yes'; 
44 my $terse = ($param{'terse'} || 'no') eq 'yes';
45 my $reverse = ($param{'reverse'} || 'no') eq 'yes';
46 my $mbox = ($param{'mbox'} || 'no') eq 'yes'; 
47 my $mime = ($param{'mime'} || 'yes') eq 'yes';
48
49 my $trim_headers = ($param{trim} || ($msg?'no':'yes')) eq 'yes';
50
51 # Not used by this script directly, but fetch these so that pkgurl() and
52 # friends can propagate them correctly.
53 my $archive = ($param{'archive'} || 'no') eq 'yes';
54 my $repeatmerged = ($param{'repeatmerged'} || 'yes') eq 'yes';
55 set_option('archive', $archive);
56 set_option('repeatmerged', $repeatmerged);
57
58 my $buglog = buglog($ref);
59
60 if (defined $ENV{REQUEST_METHOD} and $ENV{REQUEST_METHOD} eq 'HEAD' and not defined($att) and not $mbox) {
61     print "Content-Type: text/html; charset=utf-8\n";
62     my @stat = stat $buglog;
63     if (@stat) {
64         my $mtime = strftime '%a, %d %b %Y %T GMT', gmtime($stat[9]);
65         print "Last-Modified: $mtime\n";
66     }
67     print "\n";
68     exit 0;
69 }
70
71 sub display_entity ($$$$\$\@);
72 sub display_entity ($$$$\$\@) {
73     my $entity = shift;
74     my $ref = shift;
75     my $top = shift;
76     my $xmessage = shift;
77     my $this = shift;
78     my $attachments = shift;
79
80     my $head = $entity->head;
81     my $disposition = $head->mime_attr('content-disposition');
82     $disposition = 'inline' if not defined $disposition or $disposition eq '';
83     my $type = $entity->effective_type;
84     my $filename = $entity->head->recommended_filename;
85     $filename = '' unless defined $filename;
86     $filename = decode_rfc1522($filename);
87
88     if ($top and not $terse) {
89          my $header = $entity->head;
90          $$this .= "<pre class=\"headers\">\n";
91          if ($trim_headers) {
92               my @headers;
93               foreach (qw(From To Cc Subject Date)) {
94                    my $head_field = $head->get($_);
95                    next unless defined $head_field and $head_field ne '';
96                    push @headers, qq(<b>$_:</b> ) . htmlsanit(decode_rfc1522($head_field));
97               }
98               $$this .= join(qq(), @headers) unless $terse;
99          } else {
100               $$this .= htmlsanit(decode_rfc1522($entity->head->stringify));
101          }
102          $$this .= "</pre>\n";
103     }
104
105     unless (($top and $type =~ m[^text(?:/plain)?(?:;|$)]) or
106             ($type =~ m[^multipart/])) {
107         push @$attachments, $entity;
108         my @dlargs = ($ref, "msg=$xmessage", "att=$#$attachments");
109         push @dlargs, "filename=$filename" if $filename ne '';
110         my $printname = $filename;
111         $printname = 'Message part ' . ($#$attachments + 1) if $filename eq '';
112         $$this .= '<pre class="mime">[<a href="' . bugurl(@dlargs) . qq{">$printname</a> } .
113                   "($type, $disposition)]</pre>\n";
114
115         if ($msg and defined($att) and $att eq $#$attachments) {
116             my $head = $entity->head;
117             chomp(my $type = $entity->effective_type);
118             my $body = $entity->stringify_body;
119             print "Content-Type: $type";
120             my ($charset) = $head->get('Content-Type:') =~ m/charset\s*=\s*\"?([\w-]+)\"?/i;
121             print qq(; charset="$charset") if defined $charset;
122             print "\n";
123             if ($filename ne '') {
124                 my $qf = $filename;
125                 $qf =~ s/"/\\"/g;
126                 $qf =~ s[.*/][];
127                 print qq{Content-Disposition: inline; filename="$qf"\n};
128             }
129             print "\n";
130             my $decoder = new MIME::Decoder($head->mime_encoding);
131             $decoder->decode(new IO::Scalar(\$body), \*STDOUT);
132             exit(0);
133         }
134     }
135
136     return if not $top and $disposition eq 'attachment' and not defined($att);
137     return unless ($type =~ m[^text/?] and
138                    $type !~ m[^text/(?:html|enriched)(?:;|$)]) or
139                   $type =~ m[^application/pgp(?:;|$)] or
140                   $entity->parts;
141
142     if ($entity->is_multipart) {
143         my @parts = $entity->parts;
144         foreach my $part (@parts) {
145             display_entity($part, $ref, 0, $xmessage,
146                            $$this, @$attachments);
147             $$this .= "\n";
148         }
149     } elsif ($entity->parts) {
150         # We must be dealing with a nested message.
151         $$this .= "<blockquote>\n";
152         my @parts = $entity->parts;
153         foreach my $part (@parts) {
154             display_entity($part, $ref, 1, $xmessage,
155                            $$this, @$attachments);
156             $$this .= "\n";
157         }
158         $$this .= "</blockquote>\n";
159     } else {
160          if (not $terse) {
161               my $content_type = $entity->head->get('Content-Type:') || "text/html";
162               my ($charset) = $content_type =~ m/charset\s*=\s*\"?([\w-]+)\"?/i;
163               my $body = $entity->bodyhandle->as_string;
164               $body = convert_to_utf8($body,$charset) if defined $charset;
165               $body = htmlsanit($body);
166               $body =~ s,((ftp|http|https)://[\S~-]+?/?)((\&gt\;)?[)]?[']?[:.\,]?(\s|$)),<a href=\"$1\">$1</a>$3,go;
167               # The basic idea here is that we take every subpart of
168               # this regex; if it's a digit, it's a bug number, so link
169               # it; otherwise it's some other part, so kick it back
170               # out.
171               our @bugs;
172               $body =~ s[(?:(closes:\s*(?:bug)?\#?\s?)(?{ push @bugs, $^N })) #This sticks the recently closed parenthetical into @bugs
173                          (?:(\d+)(?{ push @bugs, $^N }))(?:(?:(,?\s*(?:bug)?\#?\s?)(?{push @bugs, $^N }))(?:(\d+)(?{ push @bugs, $^N })))*
174                         ][join('',map {/^\d+$/?(q(<a href=").bugurl($_).qq(">$_</a>)):$_} splice @bugs)]gxie;
175               $$this .= qq(<pre class="message">$body</pre>\n);
176          }
177     }
178 }
179
180 my %maintainer = %{getmaintainers()};
181 my %pkgsrc = %{getpkgsrc()};
182
183 my $indexentry;
184 my $showseverity;
185
186 my $tpack;
187 my $tmain;
188
189 $ENV{"TZ"} = 'UTC';
190 tzset();
191
192 my $dtime = strftime "%a, %e %b %Y %T UTC", localtime;
193 $tail_html = $debbugs::gHTMLTail;
194 $tail_html =~ s/SUBSTITUTE_DTIME/$dtime/;
195
196 my %status = %{getbugstatus($ref)};
197 unless (%status) {
198     print <<EOF;
199 Content-Type: text/html; charset=utf-8
200
201 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
202 <html>
203 <head><title>$short - $debbugs::gProject $debbugs::gBug report logs</title></head>
204 <body>
205 <h1>$debbugs::gProject $debbugs::gBug report logs - $short</h1>
206 <p>There is no record of $debbugs::gBug $short.
207 Try the <a href="http://$gWebDomain/">search page</a> instead.</p>
208 $tail_html</body></html>
209 EOF
210     exit 0;
211 }
212
213 $|=1;
214
215 $tpack = lc $status{'package'};
216 my @tpacks = splitpackages($tpack);
217
218 if  ($status{severity} eq 'normal') {
219         $showseverity = '';
220 } elsif (isstrongseverity($status{severity})) {
221         $showseverity = "Severity: <em class=\"severity\">$status{severity}</em>;\n";
222 } else {
223         $showseverity = "Severity: $status{severity};\n";
224 }
225
226 $indexentry .= "<div class=\"msgreceived\">\n";
227 $indexentry .= htmlpackagelinks($status{package}, 0) . ";\n";
228
229 foreach my $pkg (@tpacks) {
230     my $tmaint = defined($maintainer{$pkg}) ? $maintainer{$pkg} : '(unknown)';
231     my $tsrc = defined($pkgsrc{$pkg}) ? $pkgsrc{$pkg} : '(unknown)';
232
233     $indexentry .=
234             htmlmaintlinks(sub { $_[0] == 1 ? "Maintainer for $pkg is\n"
235                                             : "Maintainers for $pkg are\n" },
236                            $tmaint);
237     $indexentry .= ";\nSource for $pkg is\n".
238             '<a href="'.srcurl($tsrc)."\">$tsrc</a>" if ($tsrc ne "(unknown)");
239     $indexentry .= ".\n";
240 }
241
242 $indexentry .= "<br>";
243 $indexentry .= htmladdresslinks("Reported by: ", \&submitterurl,
244                                 $status{originator}) . ";\n";
245 $indexentry .= sprintf "Date: %s.\n",
246                 (strftime "%a, %e %b %Y %T UTC", localtime($status{date}));
247
248 $indexentry .= "<br>Owned by: " . htmlsanit($status{owner}) . ".\n"
249               if length $status{owner};
250
251 $indexentry .= "</div>\n";
252
253 my @descstates;
254
255 $indexentry .= "<h3>$showseverity";
256 $indexentry .= sprintf "Tags: %s;\n", 
257                 htmlsanit(join(", ", sort(split(/\s+/, $status{tags}))))
258                         if length($status{tags});
259 $indexentry .= "<br>" if (length($showseverity) or length($status{tags}));
260
261 my @merged= split(/ /,$status{mergedwith});
262 if (@merged) {
263         my $descmerged = 'Merged with ';
264         my $mseparator = '';
265         for my $m (@merged) {
266                 $descmerged .= $mseparator."<a href=\"" . bugurl($m) . "\">#$m</a>";
267                 $mseparator= ",\n";
268         }
269         push @descstates, $descmerged;
270 }
271
272 if (@{$status{found_versions}}) {
273     my $foundtext = 'Found in ';
274     $foundtext .= (@{$status{found_versions}} == 1) ? 'version ' : 'versions ';
275     $foundtext .= join ', ', map htmlsanit($_), @{$status{found_versions}};
276     push @descstates, $foundtext;
277 }
278
279 if (@{$status{fixed_versions}}) {
280     my $fixedtext = '<strong>Fixed</strong> in ';
281     $fixedtext .= (@{$status{fixed_versions}} == 1) ? 'version ' : 'versions ';
282     $fixedtext .= join ', ', map htmlsanit($_), @{$status{fixed_versions}};
283     if (length($status{done})) {
284         $fixedtext .= ' by ' . htmlsanit(decode_rfc1522($status{done}));
285     }
286     push @descstates, $fixedtext;
287 } elsif (length($status{done})) {
288     push @descstates, "<strong>Done:</strong> ".htmlsanit(decode_rfc1522($status{done}));
289 } elsif (length($status{forwarded})) {
290     push @descstates, "<strong>Forwarded</strong> to ".maybelink($status{forwarded});
291 }
292
293
294 my @blockedby= split(/ /, $status{blockedby});
295 if (@blockedby && $status{"pending"} ne 'fixed' && ! length($status{done})) {
296     for my $b (@blockedby) {
297         my %s = %{getbugstatus($b)};
298         next if $s{"pending"} eq 'fixed' || length $s{done};
299         push @descstates, "Fix blocked by <a href=\"" . bugurl($b) . "\">#$b</a>: ".htmlsanit($s{subject});
300     }
301 }
302
303 my @blocks= split(/ /, $status{blocks});
304 if (@blocks && $status{"pending"} ne 'fixed' && ! length($status{done})) {
305     for my $b (@blocks) {
306         my %s = %{getbugstatus($b)};
307         next if $s{"pending"} eq 'fixed' || length $s{done};
308         push @descstates, "Blocking fix for <a href=\"" . bugurl($b) . "\">#$b</a>: ".htmlsanit($s{subject});
309     }
310 }
311
312 if ($buglog !~ m#^\Q$gSpoolDir/db#) {
313     push @descstates, "Bug is archived. No further changes may be made";
314 }
315
316 $indexentry .= join(";\n<br>", @descstates) . ".\n" if @descstates;
317 $indexentry .= "</h3>\n";
318
319 my $descriptivehead = $indexentry;
320
321 my $buglogfh;
322 if ($buglog =~ m/\.gz$/) {
323     my $oldpath = $ENV{'PATH'};
324     $ENV{'PATH'} = '/bin:/usr/bin';
325     $buglogfh = new IO::File "zcat $buglog |" or &quitcgi("open log for $ref: $!");
326     $ENV{'PATH'} = $oldpath;
327 } else {
328     $buglogfh = new IO::File "<$buglog" or &quitcgi("open log for $ref: $!");
329 }
330
331
332 my @records;
333 eval{
334      @records = read_log_records($buglogfh);
335 };
336 if ($@) {
337      quitcgi("Bad bug log for $debbugs::gBug $ref. Unable to read records: $@");
338 }
339 undef $buglogfh;
340
341 =head2 handle_email_message
342
343      handle_email_message($record->{text},
344                           ref        => $bug_number,
345                           msg_number => $msg_number,
346                          );
347
348 Returns a decoded e-mail message and displays entities/attachments as
349 appropriate.
350
351
352 =cut
353
354 sub handle_email_message{
355      my ($email,%options) = @_;
356
357      my $output = '';
358      my $parser = new MIME::Parser;
359      # Because we are using memory, not tempfiles, there's no need to
360      # clean up here like in Debbugs::MIME
361      $parser->tmp_to_core(1);
362      $parser->output_to_core(1);
363      my $entity = $parser->parse_data( $email);
364      my @attachments = ();
365      display_entity($entity, $options{ref}, 1, $options{msg_number}, $output, @attachments);
366      return $output;
367
368 }
369
370 =head2 bug_links
371
372      bug_links($one_bug);
373      bug_links($starting_bug,$stoping_bugs,);
374
375 Creates a set of links to bugs, starting with bug number
376 $starting_bug, and finishing with $stoping_bug; if only one bug is
377 passed, makes a link to only a single bug.
378
379 The content of the link is the bug number.
380
381 =cut
382
383 sub bug_links{
384      my ($start,$stop,$query_arguments) = @_;
385      $stop = $stop || $start;
386      $query_arguments ||= '';
387      my @output;
388      for my $bug ($start..$stop) {
389           push @output,'<a href="'.bugurl($bug,'').qq(">$bug</a>);
390      }
391      return join(', ',@output);
392 }
393
394 =head2 handle_record
395
396      push @log, handle_record($record,$ref,$msg_num);
397
398 Deals with a record in a bug log as returned by
399 L<Debbugs::Log::read_log_records>; returns the log information that
400 should be output to the browser.
401
402 =cut
403
404 sub handle_record{
405      my ($record,$bug_number,$msg_number,$seen_msg_ids) = @_;
406
407      my $output = '';
408      local $_ = $record->{type};
409      if (/html/) {
410           $output .= decode_rfc1522($record->{text});
411           # Link to forwarded http:// urls in the midst of the report
412           # (even though these links already exist at the top)
413           $output =~ s,((?:ftp|http|https)://[\S~-]+?/?)([\)\'\:\.\,]?(?:\s|\.<|$)),<a href=\"$1\">$1</a>$2,go;
414           # Add links to the cloned bugs
415           $output =~ s{(Bug )(\d+)( cloned as bugs? )(\d+)(?:\-(\d+)|)}{$1.bug_links($2).$3.bug_links($4,$5)}eo;
416           # Add links to merged bugs
417           $output =~ s{(?<=Merged )([\d\s]+)(?=\.)}{join(' ',map {bug_links($_)} (split /\s+/, $1))}eo;
418           $output .= '<a href="' . bugurl($ref, 'msg='.($msg_number+1)) . '">Full text</a> and <a href="' .
419                bugurl($ref, 'msg='.($msg_number+1), 'mbox') . '">rfc822 format</a> available.';
420
421           $output = "<div class=\"msgreceived\">\n" . $output . "</div>\n";
422      }
423      elsif (/recips/) {
424           my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
425           if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
426                return ();
427           }
428           elsif (defined $msg_id) {
429                $$seen_msg_ids{$msg_id} = 1;
430           }
431           $output .= 'View this message in <a href="' . bugurl($ref, "msg=$msg_number", "mbox") . '">rfc822 format</a>';
432           $output .= handle_email_message($record->{text},
433                                     ref        => $bug_number,
434                                     msg_number => $msg_number,
435                                    );
436      }
437      elsif (/autocheck/) {
438           # Do nothing
439      }
440      elsif (/incoming-recv/) {
441           my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
442           if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
443                return ();
444           }
445           elsif (defined $msg_id) {
446                $$seen_msg_ids{$msg_id} = 1;
447           }
448           # Incomming Mail Message
449           my ($received,$hostname) = $record->{text} =~ m/Received: \(at (\S+)\) by (\S+)\;/;
450           $output .= qq|<p class="msgreceived"><a name="msg$msg_number">Message received</a> at |.
451                htmlsanit("$received\@$hostname") . q| (<a href="| . bugurl($ref, "msg=$msg_number") . '">full text</a>'.q|, <a href="| . bugurl($ref, "msg=$msg_number") . ';mbox=yes">mbox</a>)'.":</p>\n";
452           $output .= handle_email_message($record->{text},
453                                     ref        => $bug_number,
454                                     msg_number => $msg_number,
455                                    );
456      }
457      else {
458           die "Unknown record type $_";
459      }
460      return $output;
461 }
462
463 my $log='';
464 my $msg_num = 0;
465 my $skip_next = 0;
466 if (looks_like_number($msg) and ($msg-1) <= $#records) {
467      @records = ($records[$msg-1]);
468      $msg_num = $msg - 1;
469 }
470 my @log;
471 if ( $mbox ) {
472      if (@records > 1) {
473           print qq(Content-Disposition: attachment; filename="bug_${ref}.mbox"\n);
474           print "Content-Type: text/plain\n\n";
475      }
476      else {
477           $msg_num++;
478           print qq(Content-Disposition: attachment; filename="bug_${ref}_message_${msg_num}.mbox"\n);
479           print "Content-Type: message/rfc822\n\n";
480      }
481      for my $record (@records) {
482           next if $record->{type} !~ /^(?:recips|incoming-recv)$/;
483           next if not $boring and $record->{type} eq 'recips' and @records > 1;
484           my @lines = split( "\n", $record->{text}, -1 );
485           if ( $lines[ 1 ] =~ m/^From / ) {
486                my $tmp = $lines[ 0 ];
487                $lines[ 0 ] = $lines[ 1 ];
488                $lines[ 1 ] = $tmp;
489           }
490           if ( !( $lines[ 0 ] =~ m/^From / ) ) {
491                my $date = strftime "%a %b %d %T %Y", localtime;
492                unshift @lines, "From unknown $date";
493           }
494           map { s/^(>*From )/>$1/ } @lines[ 1 .. $#lines ];
495           print join( "\n", @lines ) . "\n";
496      }
497      exit 0;
498 }
499
500 else {
501      my %seen_msg_ids;
502      for my $record (@records) {
503           $msg_num++;
504           if ($skip_next) {
505                $skip_next = 0;
506                next;
507           }
508           $skip_next = 1 if $record->{type} eq 'html' and not $boring;
509           push @log, handle_record($record,$ref,$msg_num,\%seen_msg_ids);
510      }
511 }
512
513 @log = reverse @log if $reverse;
514 $log = join('<hr>',@log);
515
516
517 print "Content-Type: text/html; charset=utf-8\n\n";
518
519 my $title = htmlsanit($status{subject});
520
521 my $dummy2 = $debbugs::gWebHostBugDir;
522
523 print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
524 print "<HTML><HEAD>\n" . 
525     "<TITLE>$short - $title - $debbugs::gProject $debbugs::gBug report logs</TITLE>\n" .
526      '<meta http-equiv="Content-Type" content="text/html;charset=utf-8">'.
527      "<link rel=\"stylesheet\" href=\"$debbugs::gWebHostBugDir/css/bugs.css\" type=\"text/css\">" .
528     "</HEAD>\n" .
529     '<BODY>' .
530     "\n";
531 print "<H1>" . "$debbugs::gProject $debbugs::gBug report logs - <A HREF=\"mailto:$ref\@$gEmailDomain\">$short</A>" .
532       "<BR>" . $title . "</H1>\n";
533
534 print "$descriptivehead\n";
535 printf "<div class=\"msgreceived\"><p>View this report as an <a href=\"%s\">mbox folder</a>.</p></div>\n", bugurl($ref, "mbox");
536 print "<HR>";
537 print "$log";
538 print "<HR>";
539 print "<p class=\"msgreceived\">Send a report that <a href=\"/cgi-bin/bugspam.cgi?bug=$ref\">this bug log contains spam</a>.</p>\n<HR>\n";
540 print $tail_html;
541
542 print "</BODY></HTML>\n";
543
544 exit 0;