1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Each instance of this represents a POP3 client connected to
5 # public-inbox-{netd,pop3d}. Much of this was taken from IMAP.pm and NNTP.pm
7 # POP3 is one mailbox per-user, so the "USER" command is like the
8 # format of -imapd and is mapped to $NEWSGROUP.$SLICE (large inboxes
9 # are sliced into 50K mailboxes in both POP3 and IMAP to avoid overloading
12 # Unlike IMAP, the "$NEWSGROUP" mailbox (without $SLICE) is a rolling
13 # window of the latest messages. We can do this for POP3 since the
14 # typical POP3 session is short-lived while long-lived IMAP sessions
15 # would cause slices to grow on the server side without bounds.
17 # Like IMAP, POP3 also has per-session message sequence numbers (MSN),
18 # which require mapping to UIDs. The offset of an entry into our
19 # per-client cache is: (MSN-1)
22 # - uuid - 16-byte (binary) UUID representation (before successful login)
23 # - cache - one-dimentional arrayref of (UID, bytesize, oidhex)
24 # - nr_dele - number of deleted messages
25 # - expire - string of packed unsigned short offsets
26 # - user_id - user-ID mapped to UUID (on successful login + lock)
27 # - txn_max_uid - for storing max deleted UID persistently
28 # - ibx - PublicInbox::Inbox object
29 # - slice - unsigned integer slice number (0..Inf), -1 => latest
30 # - salt - pre-auth for APOP
31 # - uid_dele - maximum deleted from previous session at login (NNTP ARTICLE)
32 # - uid_base - base UID for mailbox slice (0-based) (same as IMAP)
33 package PublicInbox::POP3;
35 use parent qw(PublicInbox::DS);
36 use PublicInbox::Syscall qw(EPOLLIN EPOLLONESHOT);
37 use PublicInbox::GitAsyncCat;
38 use PublicInbox::DS qw(now);
40 use Digest::MD5 qw(md5);
41 use PublicInbox::IMAP; # for UID slice stuff
44 LINE_MAX => 512, # XXX unsure
47 # XXX FIXME: duplicated stuff from NNTP.pm and IMAP.pm
50 my ($self, $fmt, @args) = @_;
51 printf { $self->{pop3d}->{err} } $fmt."\n", @args;
55 my ($self, $fmt, @args) = @_;
56 printf { $self->{pop3d}->{out} } $fmt."\n", @args;
61 sub requeue_once ($) {
63 # COMPRESS users all share the same DEFLATE context.
64 # Flush it here to ensure clients don't see
68 # no recursion, schedule another call ASAP,
69 # but only after all pending writes are done.
71 my $new_size = push(@{$self->{wbuf}}, \&long_step);
73 # wbuf may be populated by $cb, no need to rearm if so:
74 $self->requeue if $new_size == 1;
79 # wbuf is unset or empty, here; {long} may add to it
80 my ($fd, $cb, $t0, @args) = @{$self->{long_cb}};
81 my $more = eval { $cb->($self, @args) };
82 if ($@ || !$self->{sock}) { # something bad happened...
83 delete $self->{long_cb};
84 my $elapsed = now() - $t0;
87 "%s during long response[$fd] - %0.6f",
90 out($self, " deferred[$fd] aborted - %0.6f", $elapsed);
92 } elsif ($more) { # $self->{wbuf}:
93 # control passed to ibx_async_cat if $more == \undef
94 requeue_once($self) if !ref($more);
96 delete $self->{long_cb};
97 my $elapsed = now() - $t0;
98 my $fd = fileno($self->{sock});
99 out($self, " deferred[$fd] done - %0.6f", $elapsed);
100 my $wbuf = $self->{wbuf}; # do NOT autovivify
101 $self->requeue unless $wbuf && @$wbuf;
105 sub long_response ($$;@) {
106 my ($self, $cb, @args) = @_; # cb returns true if more, false if done
107 my $sock = $self->{sock} or return;
108 # make sure we disable reading during a long response,
109 # clients should not be sending us stuff and making us do more
110 # work while we are stream a response to them
111 $self->{long_cb} = [ fileno($sock), $cb, now(), @args ];
112 long_step($self); # kick off!
118 my $s = $self->{salt} = sprintf('%x.%x', int(rand(0x7fffffff)), time);
119 $self->write("+OK POP3 server ready <$s\@public-inbox>\r\n");
123 my ($class, $sock, $pop3d) = @_;
124 my $self = bless { pop3d => $pop3d }, __PACKAGE__;
127 if ($sock->can('accept_SSL') && !$sock->accept_SSL) {
128 return CORE::close($sock) if $! != EAGAIN;
129 $ev = PublicInbox::TLS::epollbit() or return CORE::close($sock);
130 $wbuf = [ \&PublicInbox::DS::accept_tls_step, \&_greet ];
132 $self->SUPER::new($sock, $ev | EPOLLONESHOT);
134 $self->{wbuf} = $wbuf;
141 # POP user is $UUID1@$NEWSGROUP.$SLICE
143 my ($self, $mailbox) = @_;
144 $self->{salt} // return \"-ERR already authed\r\n";
145 $mailbox =~ s/\A([a-f0-9\-]+)\@//i or
146 return \"-ERR no UUID@ in mailbox name\r\n";
148 $user =~ tr/-//d; # most have dashes, some (dbus-uuidgen) don't
149 $user =~ m!\A[a-f0-9]{32}\z!i or return \"-ERR user has no UUID\r\n";
151 $mailbox =~ s/\.([0-9]+)\z// and $slice = $1 + 0;
152 my $ibx = $self->{pop3d}->{pi_cfg}->lookup_newsgroup($mailbox) //
153 return \"-ERR $mailbox does not exist\r\n";
154 my $uidmax = $ibx->mm(1)->num_highwater // 0;
155 if (defined $slice) {
156 my $max = int($uidmax / PublicInbox::IMAP::UID_SLICE);
157 my $tip = "$mailbox.$max";
158 return \"-ERR $mailbox.$slice does not exist ($tip does)\r\n"
160 $self->{uid_base} = $slice * PublicInbox::IMAP::UID_SLICE;
161 $self->{slice} = $slice;
162 } else { # latest 50K messages
163 my $base = $uidmax - PublicInbox::IMAP::UID_SLICE;
164 $self->{uid_base} = $base < 0 ? 0 : $base;
168 $self->{uuid} = pack('H*', $user); # deleted by _login_ok
169 $slice //= '(latest)';
170 \"+OK $ibx->{newsgroup} slice=$slice selected\r\n";
175 if ($self->{pop3d}->lock_mailbox($self)) {
176 $self->{uid_max} = $self->{ibx}->over(1)->max;
177 \"+OK logged in\r\n";
179 \"-ERR [IN-USE] unable to lock maildrop\r\n";
184 my ($self, $mailbox, $hex) = @_;
185 my $res = cmd_user($self, $mailbox); # sets {uuid}
186 return $res if substr($$res, 0, 1) eq '-';
187 my $s = delete($self->{salt}) // die 'BUG: salt missing';
188 return _login_ok($self) if md5("<$s\@public-inbox>anonymous") eq
191 \"-ERR APOP password mismatch\r\n";
195 my ($self, $pass) = @_;
196 $self->{ibx} // return \"-ERR mailbox unspecified\r\n";
197 my $s = delete($self->{salt}) // return \"-ERR already authed\r\n";
198 return _login_ok($self) if $pass eq 'anonymous';
200 \"-ERR password is not `anonymous'\r\n";
205 my $sock = $self->{sock} or return;
206 return \"-ERR TLS already enabled\r\n" if $sock->can('stop_SSL');
207 my $opt = $self->{pop3d}->{accept_tls} or
208 return \"-ERR can't start TLS negotiation\r\n";
209 $self->write(\"+OK begin TLS negotiation now\r\n");
210 $self->{sock} = IO::Socket::SSL->start_SSL($sock, %$opt);
211 $self->requeue if PublicInbox::DS::accept_tls_step($self);
216 exists($_[0]->{salt}) ? \"-ERR not in TRANSACTION\r\n" : undef;
219 sub _stat_cache ($) {
221 my ($beg, $end) = (($self->{uid_dele} // -1) + 1, $self->{uid_max});
222 PublicInbox::IMAP::uid_clamp($self, \$beg, \$end);
223 my $opt = { limit => PublicInbox::IMAP::UID_SLICE };
224 my $m = $self->{ibx}->over(1)->do_get(<<'', $opt, $beg, $end);
225 SELECT num,ddd FROM over WHERE num >= ? AND num <= ?
228 [ map { ($_->{num}, $_->{bytes} + 0, $_->{blob}) } @$m ];
233 my $err; $err = need_txn($self) and return $err;
234 my $cache = $self->{cache} //= _stat_cache($self);
236 for (my $i = 1; $i < scalar(@$cache); $i += 3) { $tot += $cache->[$i] }
237 my $nr = @$cache / 3 - ($self->{nr_dele} // 0);
243 my ($desc, $idx, $self, $msn) = @_;
244 my $err; $err = need_txn($self) and return $err;
245 my $cache = $self->{cache} //= _stat_cache($self);
247 my $base_off = ($msn - 1) * 3;
248 my $val = $cache->[$base_off + $idx] //
249 return \"-ERR no such message\r\n";
250 "+OK $desc listing follows\r\n$msn $val\r\n.\r\n";
251 } else { # always +OK, even if no messages
252 my $res = "+OK $desc listing follows\r\n";
254 for (my $i = 0; $i < scalar(@$cache); $i += 3) {
256 defined($cache->[$i]) and
257 $res .= "$msn $cache->[$i + $idx]\r\n";
263 sub cmd_list { _list('scan', 1, @_) }
264 sub cmd_uidl { _list('unique-id', 2, @_) }
267 my ($self, $off) = @_;
268 my $base_off = $off * 3;
269 my $cache = $self->{cache};
270 my $uid = $cache->[$base_off] // return; # already deleted
272 my $old = $self->{txn_max_uid} //= $uid;
273 $self->{txn_max_uid} = $uid if $uid > $old;
275 $cache->[$base_off] = undef; # clobber UID
276 $cache->[$base_off + 1] = 0; # zero bytes (simplifies cmd_stat)
277 $cache->[$base_off + 2] = undef; # clobber oidhex
281 sub retr_cb { # called by git->cat_async via ibx_async_cat
282 my ($bref, $oid, $type, $size, $args) = @_;
283 my ($self, $off, $top_nr) = @$args;
284 my $hex = $self->{cache}->[$off * 3 + 2] //
285 die "BUG: no hex (oid=$oid)";
286 if (!defined($oid)) {
287 # it's possible to have TOCTOU if an admin runs
288 # public-inbox-(edit|purge), just move onto the next message
289 warn "E: $hex missing in $self->{ibx}->{inboxdir}\n";
290 $self->write(\"-ERR no such message\r\n");
291 return $self->requeue;
292 } elsif ($hex ne $oid) {
294 die "BUG: $hex != $oid";
296 PublicInbox::IMAP::to_crlf_full($bref);
297 if (defined $top_nr) {
298 my ($hdr, $bdy) = split(/\r\n\r\n/, $$bref, 2);
301 my @tmp = split(/^/m, $bdy);
302 $hdr .= join('', splice(@tmp, 0, $top_nr));
303 } elsif (exists $self->{expire}) {
304 $self->{expire} .= pack('S', $off + 1);
306 $$bref =~ s/^\./../gms;
307 $$bref .= substr($$bref, -2, 2) eq "\r\n" ? ".\r\n" : "\r\n.\r\n";
308 $self->msg_more("+OK message follows\r\n");
314 my ($self, $msn, $top_nr) = @_;
315 return \"-ERR lines must be a non-negative number\r\n" if
316 (defined($top_nr) && $top_nr !~ /\A[0-9]+\z/);
317 my $err; $err = need_txn($self) and return $err;
318 my $cache = $self->{cache} //= _stat_cache($self);
320 my $hex = $cache->[$off * 3 + 2] // return \"-ERR no such message\r\n";
321 ${ibx_async_cat($self->{ibx}, $hex, \&retr_cb,
322 [ $self, $off, $top_nr ])};
325 sub cmd_noop { $_[0]->write(\"+OK\r\n") }
329 my $err; $err = need_txn($self) and return $err;
330 delete $self->{cache};
331 delete $self->{txn_max_uid};
336 my ($self, $msn) = @_;
337 my $err; $err = need_txn($self) and return $err;
338 $self->{cache} //= _stat_cache($self);
339 $msn =~ /\A[1-9][0-9]*\z/ or return \"-ERR no such message\r\n";
340 mark_dele($self, $msn - 1) ? \"+OK\r\n" : \"-ERR no such message\r\n";
346 $self->{expire} = ''; # "EXPIRE 0" allows clients to avoid DELE commands
348 +OK Capability list follows\r
361 $self->{pop3d}->unlock_mailbox($self);
367 if (defined(my $txn_id = $self->{txn_id})) {
368 my $user_id = $self->{user_id} // die 'BUG: no {user_id}';
369 if (my $exp = delete $self->{expire}) {
370 mark_dele($self, $_) for unpack('S*', $exp);
372 my $dbh = $self->{pop3d}->{-state_dbh};
373 my $lk = $self->{pop3d}->lock_for_scope;
377 if (defined $self->{txn_max_uid}) {
378 $sth = $dbh->prepare_cached(<<'');
379 UPDATE deletes SET uid_dele = ? WHERE txn_id = ? AND uid_dele < ?
381 $sth->execute($self->{txn_max_uid}, $txn_id,
382 $self->{txn_max_uid});
384 $sth = $dbh->prepare_cached(<<'');
385 UPDATE users SET last_seen = ? WHERE user_id = ?
387 $sth->execute(time, $user_id);
390 $self->write(\"+OK public-inbox POP3 server signing off\r\n");
395 # returns 1 if we can continue, 0 if not due to buffered writes or disconnect
396 sub process_line ($$) {
398 my ($req, @args) = split(/[ \t]+/, $l);
399 return 1 unless defined($req); # skip blank line
400 $req = $self->can('cmd_'.lc($req));
401 my $res = $req ? eval { $req->($self, @args) } :
402 \"-ERR command not recognized\r\n";
404 if ($err && $self->{sock}) {
406 err($self, 'error from: %s (%s)', $l, $err);
407 $res = \"-ERR program fault - command not performed\r\n";
409 defined($res) ? $self->write($res) : 0;
412 # callback used by PublicInbox::DS for any (e)poll (in/out/hup/err)
415 return unless $self->flush_write && $self->{sock} && !$self->{long_cb};
417 # only read more requests if we've drained the write buffer,
418 # otherwise we can be buffering infinitely w/o backpressure
419 my $rbuf = $self->{rbuf} // \(my $x = '');
420 my $line = index($$rbuf, "\n");
422 return $self->close if length($$rbuf) >= LINE_MAX;
423 $self->do_read($rbuf, LINE_MAX, length($$rbuf)) or return;
424 $line = index($$rbuf, "\n");
426 $line = substr($$rbuf, 0, $line + 1, '');
427 $line =~ s/\r?\n\z//s;
428 return $self->close if $line =~ /[[:cntrl:]]/s;
430 my $fd = fileno($self->{sock}); # may become invalid after process_line
431 my $r = eval { process_line($self, $line) };
432 my $pending = $self->{wbuf} ? ' pending' : '';
433 out($self, "[$fd] %s - %0.6f$pending - $r", $line, now() - $t0);
434 return $self->close if $r < 0;
435 $self->rbuf_idle($rbuf);
437 # maybe there's more pipelined data, or we'll have
438 # to register it for socket-readiness notifications
439 $self->requeue unless $pending;
443 *cmd_top = \&cmd_retr;