1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Authentication worker for anything that needs auth for read/write IMAP
5 # (eventually for read-only NNTP access)
6 package PublicInbox::LeiAuth;
9 use parent qw(PublicInbox::IPC);
10 use PublicInbox::PktOp qw(pkt_do);
11 use PublicInbox::NetReader;
14 my ($lei, $net_new) = @_;
15 if ($lei->{pkt_op_p}) { # from lei_convert worker
16 pkt_do($lei->{pkt_op_p}, 'net_merge', $net_new);
17 } else { # single lei-daemon consumer
18 my $self = $lei->{auth} or return; # client disconnected
19 my $net = $self->{net};
20 %$net = (%$net, %$net_new);
24 sub do_auth { # called via wq_io_do
26 my ($lei, $net) = @$self{qw(lei net)};
27 $net->imap_common_init($lei);
28 net_merge($lei, $net); # tell lei-daemon updated auth info
31 sub do_finish_auth { # dwaitpid callback
33 my ($self, $lei, $post_auth_cb, @args) = @$arg;
34 $? ? $lei->dclose : $post_auth_cb->(@args);
38 my ($lei, $post_auth_cb, @args) = @_;
39 my $self = delete $lei->{auth} or return;
40 $self->wq_wait_old(\&do_finish_auth, $lei, $post_auth_cb, @args);
44 my ($self, $lei, $post_auth_cb, @args) = @_;
45 my $op = $lei->workers_start($self, 'auth', 1, {
46 'net_merge' => [ \&net_merge, $lei ],
47 '' => [ \&auth_eof, $lei, $post_auth_cb, @args ],
49 $self->wq_io_do('do_auth', []);
51 while ($op && $op->{sock}) { $op->event_step }
54 sub ipc_atfork_child {
56 delete $self->{lei}->{auth}; # drop circular ref
57 $self->{lei}->lei_atfork_child;
58 $self->SUPER::ipc_atfork_child;
62 my ($cls, $net) = @_; # net may be NetReader or descendant (NetWriter)
63 bless { net => $net }, $cls;