]> git.donarmstrong.com Git - debbugs.git/blob - cgi/bugreport.cgi
* Add blocked hosts to bugreport.cgi
[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               $$this .= "<pre class=\"message\">";
168               $$this .= $body;
169               $$this .= "</pre>\n";
170          }
171     }
172 }
173
174 my %maintainer = %{getmaintainers()};
175 my %pkgsrc = %{getpkgsrc()};
176
177 my $indexentry;
178 my $showseverity;
179
180 my $tpack;
181 my $tmain;
182
183 $ENV{"TZ"} = 'UTC';
184 tzset();
185
186 my $dtime = strftime "%a, %e %b %Y %T UTC", localtime;
187 $tail_html = $debbugs::gHTMLTail;
188 $tail_html =~ s/SUBSTITUTE_DTIME/$dtime/;
189
190 my %status = %{getbugstatus($ref)};
191 unless (%status) {
192     print <<EOF;
193 Content-Type: text/html; charset=utf-8
194
195 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
196 <html>
197 <head><title>$short - $debbugs::gProject $debbugs::gBug report logs</title></head>
198 <body>
199 <h1>$debbugs::gProject $debbugs::gBug report logs - $short</h1>
200 <p>There is no record of $debbugs::gBug $short.
201 Try the <a href="http://$gWebDomain/">search page</a> instead.</p>
202 $tail_html</body></html>
203 EOF
204     exit 0;
205 }
206
207 $|=1;
208
209 $tpack = lc $status{'package'};
210 my @tpacks = splitpackages($tpack);
211
212 if  ($status{severity} eq 'normal') {
213         $showseverity = '';
214 } elsif (isstrongseverity($status{severity})) {
215         $showseverity = "Severity: <em class=\"severity\">$status{severity}</em>;\n";
216 } else {
217         $showseverity = "Severity: $status{severity};\n";
218 }
219
220 $indexentry .= "<div class=\"msgreceived\">\n";
221 $indexentry .= htmlpackagelinks($status{package}, 0) . ";\n";
222
223 foreach my $pkg (@tpacks) {
224     my $tmaint = defined($maintainer{$pkg}) ? $maintainer{$pkg} : '(unknown)';
225     my $tsrc = defined($pkgsrc{$pkg}) ? $pkgsrc{$pkg} : '(unknown)';
226
227     $indexentry .=
228             htmlmaintlinks(sub { $_[0] == 1 ? "Maintainer for $pkg is\n"
229                                             : "Maintainers for $pkg are\n" },
230                            $tmaint);
231     $indexentry .= ";\nSource for $pkg is\n".
232             '<a href="'.srcurl($tsrc)."\">$tsrc</a>" if ($tsrc ne "(unknown)");
233     $indexentry .= ".\n";
234 }
235
236 $indexentry .= "<br>";
237 $indexentry .= htmladdresslinks("Reported by: ", \&submitterurl,
238                                 $status{originator}) . ";\n";
239 $indexentry .= sprintf "Date: %s.\n",
240                 (strftime "%a, %e %b %Y %T UTC", localtime($status{date}));
241
242 $indexentry .= "<br>Owned by: " . htmlsanit($status{owner}) . ".\n"
243               if length $status{owner};
244
245 $indexentry .= "</div>\n";
246
247 my @descstates;
248
249 $indexentry .= "<h3>$showseverity";
250 $indexentry .= sprintf "Tags: %s;\n", 
251                 htmlsanit(join(", ", sort(split(/\s+/, $status{tags}))))
252                         if length($status{tags});
253 $indexentry .= "<br>" if (length($showseverity) or length($status{tags}));
254
255 my @merged= split(/ /,$status{mergedwith});
256 if (@merged) {
257         my $descmerged = 'Merged with ';
258         my $mseparator = '';
259         for my $m (@merged) {
260                 $descmerged .= $mseparator."<a href=\"" . bugurl($m) . "\">#$m</a>";
261                 $mseparator= ",\n";
262         }
263         push @descstates, $descmerged;
264 }
265
266 if (@{$status{found_versions}}) {
267     my $foundtext = 'Found in ';
268     $foundtext .= (@{$status{found_versions}} == 1) ? 'version ' : 'versions ';
269     $foundtext .= join ', ', map htmlsanit($_), @{$status{found_versions}};
270     push @descstates, $foundtext;
271 }
272
273 if (@{$status{fixed_versions}}) {
274     my $fixedtext = '<strong>Fixed</strong> in ';
275     $fixedtext .= (@{$status{fixed_versions}} == 1) ? 'version ' : 'versions ';
276     $fixedtext .= join ', ', map htmlsanit($_), @{$status{fixed_versions}};
277     if (length($status{done})) {
278         $fixedtext .= ' by ' . htmlsanit(decode_rfc1522($status{done}));
279     }
280     push @descstates, $fixedtext;
281 } elsif (length($status{done})) {
282     push @descstates, "<strong>Done:</strong> ".htmlsanit(decode_rfc1522($status{done}));
283 } elsif (length($status{forwarded})) {
284     push @descstates, "<strong>Forwarded</strong> to ".maybelink($status{forwarded});
285 }
286
287
288 my @blockedby= split(/ /, $status{blockedby});
289 if (@blockedby && $status{"pending"} ne 'fixed' && ! length($status{done})) {
290     for my $b (@blockedby) {
291         my %s = %{getbugstatus($b)};
292         next if $s{"pending"} eq 'fixed' || length $s{done};
293         push @descstates, "Fix blocked by <a href=\"" . bugurl($b) . "\">#$b</a>: ".htmlsanit($s{subject});
294     }
295 }
296
297 my @blocks= split(/ /, $status{blocks});
298 if (@blocks && $status{"pending"} ne 'fixed' && ! length($status{done})) {
299     for my $b (@blocks) {
300         my %s = %{getbugstatus($b)};
301         next if $s{"pending"} eq 'fixed' || length $s{done};
302         push @descstates, "Blocking fix for <a href=\"" . bugurl($b) . "\">#$b</a>: ".htmlsanit($s{subject});
303     }
304 }
305
306 if ($buglog !~ m#^\Q$gSpoolDir/db#) {
307     push @descstates, "Bug is archived. No further changes may be made";
308 }
309
310 $indexentry .= join(";\n<br>", @descstates) . ".\n" if @descstates;
311 $indexentry .= "</h3>\n";
312
313 my $descriptivehead = $indexentry;
314
315 my $buglogfh;
316 if ($buglog =~ m/\.gz$/) {
317     my $oldpath = $ENV{'PATH'};
318     $ENV{'PATH'} = '/bin:/usr/bin';
319     $buglogfh = new IO::File "zcat $buglog |" or &quitcgi("open log for $ref: $!");
320     $ENV{'PATH'} = $oldpath;
321 } else {
322     $buglogfh = new IO::File "<$buglog" or &quitcgi("open log for $ref: $!");
323 }
324
325
326 my @records;
327 eval{
328      @records = read_log_records($buglogfh);
329 };
330 if ($@) {
331      quitcgi("Bad bug log for $debbugs::gBug $ref. Unable to read records: $@");
332 }
333 undef $buglogfh;
334
335 =head2 handle_email_message
336
337      handle_email_message($record->{text},
338                           ref        => $bug_number,
339                           msg_number => $msg_number,
340                          );
341
342 Returns a decoded e-mail message and displays entities/attachments as
343 appropriate.
344
345
346 =cut
347
348 sub handle_email_message{
349      my ($email,%options) = @_;
350
351      my $output = '';
352      my $parser = new MIME::Parser;
353      $parser->tmp_to_core(1);
354      $parser->output_to_core(1);
355      # $parser->output_under("/tmp");
356      my $entity = $parser->parse_data( $email);
357      # TODO: make local subdir, clean it ourselves
358      # the following does NOT delete the msg dirs in /tmp
359      END { if ( $entity ) { $entity->purge; } if ( $parser ) { $parser->filer->purge; } }
360      my @attachments = ();
361      display_entity($entity, $options{ref}, 1, $options{msg_number}, $output, @attachments);
362      return $output;
363
364 }
365
366 =head2 bug_links
367
368      bug_links($one_bug);
369      bug_links($starting_bug,$stoping_bugs,);
370
371 Creates a set of links to bugs, starting with bug number
372 $starting_bug, and finishing with $stoping_bug; if only one bug is
373 passed, makes a link to only a single bug.
374
375 The content of the link is the bug number.
376
377 =cut
378
379 sub bug_links{
380      my ($start,$stop,$query_arguments) = @_;
381      $stop = $stop || $start;
382      $query_arguments ||= '';
383      my @output;
384      for my $bug ($start..$stop) {
385           push @output,'<a href="'.bugurl($bug,'').qq(">$bug</a>);
386      }
387      return join(', ',@output);
388 }
389
390 =head2 handle_record
391
392      push @log, handle_record($record,$ref,$msg_num);
393
394 Deals with a record in a bug log as returned by
395 L<Debbugs::Log::read_log_records>; returns the log information that
396 should be output to the browser.
397
398 =cut
399
400 sub handle_record{
401      my ($record,$bug_number,$msg_number,$seen_msg_ids) = @_;
402
403      my $output = '';
404      local $_ = $record->{type};
405      if (/html/) {
406           $output .= decode_rfc1522($record->{text});
407           # Link to forwarded http:// urls in the midst of the report
408           # (even though these links already exist at the top)
409           $output =~ s,((?:ftp|http|https)://[\S~-]+?/?)([\)\'\:\.\,]?(?:\s|\.<|$)),<a href=\"$1\">$1</a>$2,go;
410           # Add links to the cloned bugs
411           $output =~ s{(Bug )(\d+)( cloned as bugs? )(\d+)(?:\-(\d+)|)}{$1.bug_links($2).$3.bug_links($4,$5)}eo;
412           # Add links to merged bugs
413           $output =~ s{(?<=Merged )([\d\s]+)(?=\.)}{join(' ',map {bug_links($_)} (split /\s+/, $1))}eo;
414           $output .= '<a href="' . bugurl($ref, 'msg='.($msg_number+1)) . '">Full text</a> and <a href="' .
415                bugurl($ref, 'msg='.($msg_number+1), 'mbox') . '">rfc822 format</a> available.';
416
417           $output = "<div class=\"msgreceived\">\n" . $output . "</div>\n";
418      }
419      elsif (/recips/) {
420           my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
421           if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
422                return ();
423           }
424           elsif (defined $msg_id) {
425                $$seen_msg_ids{$msg_id} = 1;
426           }
427           $output .= 'View this message in <a href="' . bugurl($ref, "msg=$msg_number", "mbox") . '">rfc822 format</a>';
428           $output .= handle_email_message($record->{text},
429                                     ref        => $bug_number,
430                                     msg_number => $msg_number,
431                                    );
432      }
433      elsif (/autocheck/) {
434           # Do nothing
435      }
436      elsif (/incoming-recv/) {
437           my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
438           if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
439                return ();
440           }
441           elsif (defined $msg_id) {
442                $$seen_msg_ids{$msg_id} = 1;
443           }
444           # Incomming Mail Message
445           my ($received,$hostname) = $record->{text} =~ m/Received: \(at (\S+)\) by (\S+)\;/;
446           $output .= qq|<p class="msgreceived"><a name="msg$msg_number">Message received</a> at |.
447                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";
448           $output .= handle_email_message($record->{text},
449                                     ref        => $bug_number,
450                                     msg_number => $msg_number,
451                                    );
452      }
453      else {
454           die "Unknown record type $_";
455      }
456      return $output;
457 }
458
459 my $log='';
460 my $msg_num = 0;
461 my $skip_next = 0;
462 if (looks_like_number($msg) and ($msg-1) <= $#records) {
463      @records = ($records[$msg-1]);
464      $msg_num = $msg - 1;
465 }
466 my @log;
467 if ( $mbox ) {
468      if (@records > 1) {
469           print qq(Content-Disposition: attachment; filename="bug_${ref}.mbox"\n);
470           print "Content-Type: text/plain\n\n";
471      }
472      else {
473           $msg_num++;
474           print qq(Content-Disposition: attachment; filename="bug_${ref}_message_${msg_num}.mbox"\n);
475           print "Content-Type: message/rfc822\n\n";
476      }
477      for my $record (@records) {
478           next if $record->{type} !~ /^(?:recips|incoming-recv)$/;
479           next if not $boring and $record->{type} eq 'recips' and @records > 1;
480           my @lines = split( "\n", $record->{text}, -1 );
481           if ( $lines[ 1 ] =~ m/^From / ) {
482                my $tmp = $lines[ 0 ];
483                $lines[ 0 ] = $lines[ 1 ];
484                $lines[ 1 ] = $tmp;
485           }
486           if ( !( $lines[ 0 ] =~ m/^From / ) ) {
487                my $date = strftime "%a %b %d %T %Y", localtime;
488                unshift @lines, "From unknown $date";
489           }
490           map { s/^(>*From )/>$1/ } @lines[ 1 .. $#lines ];
491           print join( "\n", @lines ) . "\n";
492      }
493      exit 0;
494 }
495
496 else {
497      my %seen_msg_ids;
498      for my $record (@records) {
499           $msg_num++;
500           if ($skip_next) {
501                $skip_next = 0;
502                next;
503           }
504           $skip_next = 1 if $record->{type} eq 'html' and not $boring;
505           push @log, handle_record($record,$ref,$msg_num,\%seen_msg_ids);
506      }
507 }
508
509 @log = reverse @log if $reverse;
510 $log = join('<hr>',@log);
511
512
513 print "Content-Type: text/html; charset=utf-8\n\n";
514
515 my $title = htmlsanit($status{subject});
516
517 my $dummy2 = $debbugs::gWebHostBugDir;
518
519 print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
520 print "<HTML><HEAD>\n" . 
521     "<TITLE>$short - $title - $debbugs::gProject $debbugs::gBug report logs</TITLE>\n" .
522      '<meta http-equiv="Content-Type" content="text/html;charset=utf-8">'.
523      "<link rel=\"stylesheet\" href=\"$debbugs::gWebHostBugDir/css/bugs.css\" type=\"text/css\">" .
524     "</HEAD>\n" .
525     '<BODY>' .
526     "\n";
527 print "<H1>" . "$debbugs::gProject $debbugs::gBug report logs - <A HREF=\"mailto:$ref\@$gEmailDomain\">$short</A>" .
528       "<BR>" . $title . "</H1>\n";
529
530 print "$descriptivehead\n";
531 printf "<div class=\"msgreceived\"><p>View this report as an <a href=\"%s\">mbox folder</a>.</p></div>\n", bugurl($ref, "mbox");
532 print "<HR>";
533 print "$log";
534 print "<HR>";
535 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";
536 print $tail_html;
537
538 print "</BODY></HTML>\n";
539
540 exit 0;