1 # Copyright (C) 2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3 package PublicInbox::NNTP;
6 use base qw(Danga::Socket);
7 use fields qw(nntpd article rbuf ng long_res);
8 use PublicInbox::Msgmap;
9 use PublicInbox::GitCatFile;
10 use PublicInbox::MID qw(mid2path);
12 use Data::Dumper qw(Dumper);
13 use POSIX qw(strftime);
14 use Time::HiRes qw(clock_gettime ualarm CLOCK_MONOTONIC);
16 r501 => '501 command syntax error',
17 long_response_limit => 0xffffffff,
20 sub now () { clock_gettime(CLOCK_MONOTONIC) };
22 my @OVERVIEW = qw(Subject From Date Message-ID References Bytes Lines);
23 my %OVERVIEW = map { $_ => 1 } @OVERVIEW;
25 # disable commands with easy DoS potential:
26 # LISTGROUP could get pretty bad, too...
27 my %DISABLED; # = map { $_ => 1 } qw(xover list_overview_fmt newnews xhdr);
30 my ($class, $sock, $nntpd) = @_;
31 my $self = fields::new($class);
32 binmode $sock, ':utf8'; # RFC 3977
33 $self->SUPER::new($sock);
34 $self->{nntpd} = $nntpd;
35 res($self, '201 server ready - post via email');
43 my $tot = prototype $cb;
44 my ($nreq, undef) = split(';', $tot);
45 $nreq = ($nreq =~ tr/$//) - 1;
46 $tot = ($tot =~ tr/$//) - 1;
47 ($argc <= $tot && $argc >= $nreq);
50 # returns 1 if we can continue, 0 if not due to buffered writes or disconnect
51 sub process_line ($$) {
53 my ($req, @args) = split(/\s+/, $l);
57 $req = $DISABLED{$req} ? undef : *{'cmd_'.$req}{CODE};
59 return res($self, '500 command not recognized') unless $req;
60 return res($self, r501) unless args_ok($req, scalar @args);
62 my $res = eval { $req->($self, @args) };
64 if ($err && !$self->{closed}) {
65 chomp($l = Dumper(\$l));
66 err($self, "error from: $l $err");
67 $res = '503 program fault - command not performed';
69 return 0 unless defined $res;
74 my ($self, $arg) = @_;
76 return r501 unless $arg eq 'READER';
77 '200 reader status acknowledged';
80 sub cmd_slave ($) { '202 slave status noted' }
82 sub cmd_xgtitle ($;$) {
83 my ($self, $wildmat) = @_;
84 more($self, '282 list of groups and descriptions follows');
85 list_newsgroups($self, $wildmat);
89 sub list_overview_fmt ($$) {
91 more($self, $_ . ':') foreach @OVERVIEW;
94 sub list_active ($;$) {
95 my ($self, $wildmat) = @_;
97 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
98 $ng->{name} =~ $wildmat or next;
99 group_line($self, $ng);
103 sub list_active_times ($;$) {
104 my ($self, $wildmat) = @_;
105 wildmat2re($wildmat);
106 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
107 $ng->{name} =~ $wildmat or next;
108 my $c = eval { $ng->mm->created_at } || time;
109 more($self, "$ng->{name} $c $ng->{address}");
113 sub list_newsgroups ($;$) {
114 my ($self, $wildmat) = @_;
115 wildmat2re($wildmat);
116 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
117 $ng->{name} =~ $wildmat or next;
118 my $d = $ng->description;
119 more($self, "$ng->{name} $d");
123 # LIST SUBSCRIPTIONS not supported
124 sub cmd_list ($;$$) {
125 my ($self, @args) = @_;
127 my $arg = shift @args;
128 $arg =~ tr/A-Z./a-z_/;
130 return '503 function not performed' if $DISABLED{$arg};
136 return r501 unless $arg && args_ok($arg, scalar @args);
137 more($self, '215 information follows');
138 $arg->($self, @args);
140 more($self, '215 list of newsgroups follows');
141 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
142 group_line($self, $ng);
148 sub cmd_listgroup ($;$) {
149 my ($self, $group) = @_;
150 if (defined $group) {
151 my $res = cmd_group($self, $group);
152 return $res if ($res !~ /\A211 /);
156 $self->{ng} or return '412 no newsgroup selected';
157 $self->long_response(0, long_response_limit, sub {
159 my $nr = $self->{ng}->mm->id_batch($$i, sub {
161 more($self, join("\r\n", @$ary));
164 # -1 to adjust for implicit increment in long_response
165 $$i = $nr ? $$i + $nr - 1 : long_response_limit;
169 sub parse_time ($$;$) {
170 my ($date, $time, $gmt) = @_;
171 use Time::Local qw();
172 my ($hh, $mm, $ss) = unpack('A2A2A2', $time);
174 $gmt =~ /\A(?:UTC|GMT)\z/i or die "GM invalid: $gmt\n";
177 my @now = $gmt ? gmtime : localtime;
178 my ($YYYY, $MM, $DD);
179 if (length($date) == 8) { # RFC 3977 allows YYYYMMDD
180 ($YYYY, $MM, $DD) = unpack('A4A2A2', $date);
181 } else { # legacy clients send YYMMDD
182 ($YYYY, $MM, $DD) = unpack('A2A2A2', $date);
183 if ($YYYY > strftime('%y', @now)) {
184 my $cur_year = $now[5] + 1900;
185 $YYYY += int($cur_year / 1000) * 1000 - 100;
189 Time::Local::timegm($ss, $mm, $hh, $DD, $MM - 1, $YYYY);
191 Time::Local::timelocal($ss, $mm, $hh, $DD, $MM - 1, $YYYY);
195 sub group_line ($$) {
196 my ($self, $ng) = @_;
197 my ($min, $max) = $ng->mm->minmax;
198 more($self, "$ng->{name} $max $min n") if defined $min && defined $max;
201 sub cmd_newgroups ($$$;$$) {
202 my ($self, $date, $time, $gmt, $dists) = @_;
203 my $ts = eval { parse_time($date, $time, $gmt) };
207 more($self, '231 list of new newsgroups follows');
208 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
209 my $c = eval { $ng->mm->created_at } || 0;
210 next unless $c > $ts;
211 group_line($self, $ng);
216 sub wildmat2re (;$) {
217 return $_[0] = qr/.*/ if (!defined $_[0] || $_[0] eq '*');
220 use Digest::SHA qw(sha1_hex);
223 $tmp =~ s#(?<!\\)\[(.+)(?<!\\)\]#
225 my $key = sha1_hex($orig . $salt);
226 $orig =~ s/([^\w\-])+/\Q$1/g;
230 my %map = ('*' => '.*', '?' => '.' );
231 $tmp =~ s#(?<!\\)([^\w\\])#$map{$1} || "\Q$1"#ge;
233 $tmp =~ s#([a-f0-9]{40})#
234 my $orig = $keep{$1};
235 defined $orig ? $orig : $1;
238 $_[0] = qr/\A$tmp\z/;
242 return $_[0] = qr/\A\z/ unless defined $_[0];
243 my %map = ('*' => '.*', ',' => '|');
244 $_[0] =~ s!(.)!$map{$1} || "\Q$1"!ge;
245 $_[0] = qr/\A(?:$_[0])\z/;
248 sub cmd_newnews ($$$$;$$) {
249 my ($self, $newsgroups, $date, $time, $gmt, $dists) = @_;
250 my $ts = eval { parse_time($date, $time, $gmt) };
252 more($self, '230 list of new articles by message-id follows');
253 my ($keep, $skip) = split('!', $newsgroups, 2);
257 foreach my $ng (@{$self->{nntpd}->{grouplist}}) {
258 $ng->{name} =~ $keep or next;
259 $ng->{name} =~ $skip and next;
260 my $srch = $ng->search or next;
263 return '.' unless @srch;
266 my $opts = { asc => 1, limit => 1000, offset => 0 };
267 $self->long_response(0, long_response_limit, sub {
270 my $res = $srch->query($ts, $opts);
271 my $msgs = $res->{msgs};
272 if (my $nr = scalar @$msgs) {
274 join(">\r\n<", map { $_->mid } @$msgs ).
276 $opts->{offset} += $nr;
279 if (@srch) { # continue onto next newsgroup
281 } else { # break out of the long response.
282 $$i = long_response_limit;
289 my ($self, $group) = @_;
290 my $no_such = '411 no such news group';
291 my $ng = $self->{nntpd}->{groups}->{$group} or return $no_such;
294 my ($min, $max) = $ng->mm->minmax;
297 $self->{article} = $min;
298 my $est_size = $max - $min;
299 "211 $est_size $min $max $group";
302 sub article_adj ($$) {
303 my ($self, $off) = @_;
304 my $ng = $self->{ng} or return '412 no newsgroup selected';
306 my $n = $self->{article};
307 defined $n or return '420 no current article has been selected';
310 my $mid = $ng->mm->mid_for($n);
312 $n = $off > 0 ? 'next' : 'previous';
313 return "421 no $n article in this group";
315 $self->{article} = $n;
316 "223 $n <$mid> article retrieved - request text separately";
319 sub cmd_next ($) { article_adj($_[0], 1) }
320 sub cmd_last ($) { article_adj($_[0], -1) }
322 # We want to encourage using email and CC-ing everybody involved to avoid
323 # the single-point-of-failure a single server provides.
326 my $ng = $self->{ng};
327 $ng ? "440 mailto:$ng->{address} to post" : '440 posting not allowed'
332 res($self, '205 closing connection - goodbye!');
337 sub art_lookup ($$$) {
338 my ($self, $art, $set_headers) = @_;
339 my $ng = $self->{ng};
343 if ($art =~ /\A\d+\z/o) {
344 $err = '423 no such article number in this group';
347 } elsif ($art =~ /\A<([^>]+)>\z/) {
349 $err = '430 no such article found';
350 $n = $ng->mm->num_for($mid) if $ng;
351 goto found if defined $n;
352 foreach my $g (values %{$self->{nntpd}->{groups}}) {
353 $n = $g->mm->num_for($mid);
364 $err = '420 no current article has been selected';
365 $n = $self->{article};
366 defined $n or return $err;
368 $ng or return '412 no newsgroup has been selected';
369 $mid = $ng->mm->mid_for($n);
370 defined $mid or return $err;
373 my $o = 'HEAD:' . mid2path($mid);
375 my $s = eval { Email::MIME->new($ng->gcf->cat_file($o, \$bytes)) };
376 return $err unless $s;
379 $s->header_set('Newsgroups', $ng->{name});
380 $s->header_set('Xref', xref($ng, $n));
381 $lines = $s->body =~ tr!\n!\n!;
384 $s->body_set('') if ($set_headers == 2);
386 [ $n, $mid, $s, $bytes, $lines, $ng ];
389 sub simple_body_write ($$) {
393 $body =~ s/^\./../smg;
394 $body =~ s/(?<!\r)\n/\r\n/sg;
395 do_more($self, $body);
396 do_more($self, "\r\n") unless $body =~ /\r\n\z/s;
401 my ($self, $art) = @_;
402 $self->{article} = $art if defined $art && $art =~ /\A\d+\z/;
405 sub cmd_article ($;$) {
406 my ($self, $art) = @_;
407 my $r = $self->art_lookup($art, 1);
408 return $r unless ref $r;
409 my ($n, $mid, $s) = @$r;
410 set_art($self, $art);
411 more($self, "220 $n <$mid> article retrieved - head and body follow");
412 do_more($self, $s->header_obj->as_string);
413 do_more($self, "\r\n");
414 simple_body_write($self, $s);
418 my ($self, $art) = @_;
419 my $r = $self->art_lookup($art, 2);
420 return $r unless ref $r;
421 my ($n, $mid, $s) = @$r;
422 set_art($self, $art);
423 more($self, "221 $n <$mid> article retrieved - head follows");
424 do_more($self, $s->header_obj->as_string);
429 my ($self, $art) = @_;
430 my $r = $self->art_lookup($art, 0);
431 return $r unless ref $r;
432 my ($n, $mid, $s) = @$r;
433 set_art($self, $art);
434 more($self, "222 $n <$mid> article retrieved - body follows");
435 simple_body_write($self, $s);
439 my ($self, $art) = @_;
440 my $r = $self->art_lookup($art, 0);
441 return $r unless ref $r;
442 my ($n, $mid, undef) = @$r;
443 set_art($self, $art);
444 "223 $n <$mid> article retrieved - request text separately";
447 sub cmd_ihave ($) { '435 article not wanted - do not send it' }
449 sub cmd_date ($) { '111 '.strftime('%Y%m%d%H%M%S', gmtime(time)) }
453 more($self, '100 help text follows');
458 my ($self, $range) = @_;
459 my $ng = $self->{ng} or return '412 no news group has been selected';
460 defined $range or return '420 No article(s) selected';
462 my ($min, $max) = $ng->mm->minmax;
463 if ($range =~ /\A(\d+)\z/) {
465 } elsif ($range =~ /\A(\d+)-\z/) {
466 ($beg, $end) = ($1, $max);
467 } elsif ($range =~ /\A(\d+)-(\d+)\z/) {
468 ($beg, $end) = ($1, $2);
472 $beg = $min if ($beg < $min);
473 $end = $max if ($end > $max);
474 return '420 No article(s) selected' if ($beg > $end);
479 my ($r, $header) = @_;
480 $header = lc $header;
481 return $r->[3] if ($header eq 'bytes');
482 return $r->[4] if ($header eq 'lines');
483 $r = $r->[2]->header_obj->header($header);
484 defined $r or return;
485 $r =~ s/[\r\n\t]+/ /sg;
489 sub long_response ($$$$) {
490 my ($self, $beg, $end, $cb) = @_;
491 die "BUG: nested long response" if $self->{long_res};
493 # make sure we disable reading during a long response,
494 # clients should not be sending us stuff and making us do more
495 # work while we are stream a response to them
496 $self->watch_read(0);
497 my $fd = fileno $self->{sock};
499 $self->{long_res} = sub {
500 # limit our own running time for fairness with other
501 # clients and to avoid buffering too much:
503 local $SIG{ALRM} = sub { $yield = 1 };
508 eval { $cb->(\$beg) };
509 } until (($err = $@) || $self->{closed} || $yield ||
510 $self->{write_buf_size} || ++$beg > $end);
513 if ($err || $self->{closed}) {
514 $self->{long_res} = undef;
518 "$err during long response[$fd] - %0.6f",
521 if ($self->{closed}) {
522 out($self, " deferred[$fd] aborted - %0.6f",
525 $self->watch_read(1);
527 } elsif ($yield || $self->{write_buf_size}) {
528 # no recursion, schedule another call ASAP
529 # but only after all pending writes are done
530 Danga::Socket->AddTimer(0, sub {
531 $self->write($self->{long_res});
534 $self->{long_res} = undef;
535 $self->watch_read(1);
537 out($self, " deferred[$fd] done - %0.6f", now() - $t0);
540 $self->{long_res}->(); # kick off!
544 sub xhdr_message_id ($$) { # optimize XHDR Message-ID [range] for slrnpull.
545 my ($self, $range) = @_;
547 my $mm = $self->{ng}->mm;
548 if (defined $range && $range =~ /\A<(.+)>\z/) { # Message-ID
549 my $n = $mm->num_for($1);
550 return '430 No article with that message-id' unless $n;
551 more($self, '221 Header follows');
552 more($self, "$range $range");
554 } else { # numeric range
555 $range = $self->{article} unless defined $range;
556 my $r = get_range($self, $range);
557 return $r unless ref $r;
558 my ($beg, $end) = @$r;
559 more($self, '221 Header follows');
560 $self->long_response($beg, $end, sub {
562 my $mid = $mm->mid_for($$i);
563 more($self, "$$i <$mid>") if defined $mid;
570 "$ng->{domain} $ng->{name}:$n"
573 sub xhdr_xref ($$) { # optimize XHDR Xref [range] for rtin
574 my ($self, $range) = @_;
576 my $ng = $self->{ng};
578 if (defined $range && $range =~ /\A<(.+)>\z/) { # Message-ID
579 my $n = $mm->num_for($1);
580 more($self, '221 Header follows');
581 more($self, "$range ".xref($ng, $n)) if defined $n;
583 } else { # numeric range
584 $range = $self->{article} unless defined $range;
585 my $r = get_range($self, $range);
586 return $r unless ref $r;
587 my ($beg, $end) = @$r;
588 more($self, '221 Header follows');
589 $self->long_response($beg, $end, sub {
591 my $mid = $mm->mid_for($$i);
592 more($self, "$$i ".xref($ng, $$i)) if defined $mid;
598 my ($srch, $mid) = @_;
600 my $smsg = $srch->lookup_message($mid);
601 $smsg = PublicInbox::SearchMsg->load_doc($smsg->{doc});
602 $smsg->mini_mime->header_obj;
606 sub xhdr_searchmsg ($$$) {
607 my ($self, $sub, $range) = @_;
608 my $srch = $self->{ng}->search;
609 my $emit = ($sub eq 'date') ? sub {
611 my @t = gmtime($m->header('X-PI-TS'));
612 more($self, "$pfx ". strftime('%a, %d %b %Y %T %z', @t));
615 my $h = $m->header($sub);
616 more($self, "$pfx $h") if defined $h;
619 if (defined $range && $range =~ /\A<(.+)>\z/) { # Message-ID
620 more($self, '221 Header follows');
621 my $m = header_obj_for($srch, $1);
622 $emit->($range, $m) if defined $m;
624 } else { # numeric range
625 $range = $self->{article} unless defined $range;
626 my $mm = $self->{ng}->mm;
627 my $r = get_range($self, $range);
628 return $r unless ref $r;
629 my ($beg, $end) = @$r;
630 more($self, '221 Header follows');
631 $self->long_response($beg, $end, sub {
633 my $mid = $mm->mid_for($$i) or return;
634 my $m = header_obj_for($srch, $mid) or return;
640 sub cmd_xhdr ($$;$) {
641 my ($self, $header, $range) = @_;
642 my $ng = $self->{ng};
643 defined $ng or return '412 no news group currently selected';
644 my $sub = lc $header;
645 if ($sub eq 'message-id') {
646 xhdr_message_id($self, $range);
647 } elsif ($sub eq 'xref') {
648 xhdr_xref($self, $range);
649 } elsif ($sub =~ /\A(subject|references|date)\z/ && $ng->search) {
650 xhdr_searchmsg($self, $sub, $range);
652 xhdr_slow($self, $header, $range);
656 sub xhdr_slow ($$$) {
657 my ($self, $header, $range) = @_;
659 if (defined $range && $range =~ /\A<.+>\z/) { # Message-ID
660 my $r = $self->art_lookup($range, 2);
661 return $r unless ref $r;
662 more($self, '221 Header follows');
663 if (defined($r = xhdr($r, $header))) {
664 more($self, "$range $r");
667 } else { # numeric range
668 $range = $self->{article} unless defined $range;
669 my $r = get_range($self, $range);
670 return $r unless ref $r;
671 my ($beg, $end) = @$r;
672 more($self, '221 Header follows');
673 $self->long_response($beg, $end, sub {
675 $r = $self->art_lookup($$i, 2);
676 return unless ref $r;
677 defined($r = xhdr($r, $header)) or return;
678 more($self, "$$i $r");
683 sub cmd_xrover ($;$) {
684 my ($self, $range) = @_;
685 my $ng = $self->{ng} or return '412 no newsgroup selected';
686 (defined $range && $range =~ /[<>]/) and
687 return '420 No article(s) selected'; # no message IDs
689 $range = $self->{article} unless defined $range;
690 my $r = get_range($self, $range);
691 return $r unless ref $r;
692 my ($beg, $end) = @$r;
694 my $srch = $ng->search;
695 more($self, '224 Overview information follows');
696 $self->long_response($beg, $end, sub {
698 my $mid = $mm->mid_for($$i) or return;
699 my $m = header_obj_for($srch, $mid) or return;
700 my $h = $m->header('references');
701 more($self, "$$i $h") if defined $h;
708 more($self, join("\t", $r->[0], map {
709 my $h = xhdr($r, $_);
710 defined $h ? $h : '';
715 my ($self, $range) = @_;
716 if ($range && $range =~ /\A<.+>\z/) {
717 my $r = $self->art_lookup($range, 2);
718 return '430 No article with that message-id' unless ref $r;
719 more($self, '224 Overview information follows (multi-line)');
721 # Only set article number column if it's the current group
722 my $ng = $self->{ng};
723 $r->[0] = 0 if (!$ng || $ng ne $r->[5]);
724 over_line($self, $r);
727 cmd_xover($self, $range);
731 sub cmd_xover ($;$) {
732 my ($self, $range) = @_;
733 $range = $self->{article} unless defined $range;
734 my $r = get_range($self, $range);
735 return $r unless ref $r;
736 my ($beg, $end) = @$r;
737 more($self, "224 Overview information follows for $beg to $end");
738 $self->long_response($beg, $end, sub {
740 my $r = $self->art_lookup($$i, 2);
741 return unless ref $r;
742 over_line($self, $r);
747 my ($self, $mid) = @_;
748 return r501 unless $mid =~ /\A<(.+)>\z/;
751 foreach my $ng (values %{$self->{nntpd}->{groups}}) {
752 my $n = $ng->mm->num_for($mid);
753 push @paths, "$ng->{name}/$n" if defined $n;
755 return '430 no such article on server' unless @paths;
756 '223 '.join(' ', @paths);
760 my ($self, $line) = @_;
761 do_write($self, $line . "\r\n");
765 my ($self, $line) = @_;
766 do_more($self, $line . "\r\n");
770 my ($self, $data) = @_;
771 my $done = $self->write($data);
772 die if $self->{closed};
774 # Do not watch for readability if we have data in the queue,
775 # instead re-enable watching for readability when we can
776 $self->watch_read(0) if (!$done || $self->{long_res});
782 my ($self, $fmt, @args) = @_;
783 printf { $self->{nntpd}->{err} } $fmt."\n", @args;
787 my ($self, $fmt, @args) = @_;
788 printf { $self->{nntpd}->{out} } $fmt."\n", @args;
791 use constant MSG_MORE => ($^O eq 'linux') ? 0x8000 : 0;
794 my ($self, $data) = @_;
795 if (MSG_MORE && !$self->{write_buf_size}) {
796 my $n = send($self->{sock}, $data, MSG_MORE);
798 my $dlen = length($data);
799 return 1 if $n == $dlen; # all done!
800 $data = substr($data, $n, $dlen - $n);
803 $self->do_write($data);
806 # callbacks for by Danga::Socket
808 sub event_hup { $_[0]->close }
809 sub event_err { $_[0]->close }
813 # only continue watching for readability when we are done writing:
814 if ($self->write(undef) == 1 && !$self->{long_res}) {
815 $self->watch_read(1);
821 use constant LINE_MAX => 512; # RFC 977 section 2.3
825 while ($r > 0 && $self->{rbuf} =~ s/\A\s*([^\r\n]+)\r?\n//) {
828 $r = eval { $self->process_line($line) };
829 my $d = $self->{long_res} ?
830 ' deferred['.fileno($self->{sock}).']' : '';
831 out($self, "$line - %0.6f$d", now() - $t0);
833 unless (defined $line) {
834 my $buf = $self->read(LINE_MAX) or return $self->close;
835 $self->{rbuf} .= $$buf;
839 return $self->close if $r < 0;
840 my $len = length($self->{rbuf});
841 return $self->close if ($len >= LINE_MAX);
845 my ($self, $bool) = @_;
846 my $rv = $self->SUPER::watch_read($bool);
847 if ($bool && $self->{rbuf} ne '') {
848 # Force another read if there is a pipelined request.
849 # We don't know if the socket has anything for us to read,
850 # and we must double-check again by the time the timer fires
851 # in case we really did dispatch a read event and started
852 # another long response.
853 Danga::Socket->AddTimer(0, sub {
854 if (&Danga::Socket::POLLIN & $self->{event_watch}) {