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, $nrd_new) = @_;
15 if ($lei->{pkt_op_p}) { # from lei_convert worker
16 pkt_do($lei->{pkt_op_p}, 'nrd_merge', $nrd_new);
17 } else { # single lei-daemon consumer
18 my $self = $lei->{auth} or return; # client disconnected
19 my $nrd = $self->{nrd};
20 %$nrd = (%$nrd, %$nrd_new);
24 sub do_auth { # called via wq_io_do
26 my ($lei, $nrd) = @$self{qw(lei nrd)};
27 $nrd->imap_common_init($lei);
28 nrd_merge($lei, $nrd); # 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 $lei->_lei_cfg(1); # workers may need to read config
46 my $op = $lei->workers_start($self, 'auth', 1, {
47 'nrd_merge' => [ \&nrd_merge, $lei ],
48 '' => [ \&auth_eof, $lei, $post_auth_cb, @args ],
50 $self->wq_io_do('do_auth', []);
52 while ($op && $op->{sock}) { $op->event_step }
55 sub ipc_atfork_child {
57 # prevent {sock} from being closed in lei_atfork_child:
58 my $s = delete $self->{lei}->{sock};
59 delete $self->{lei}->{auth}; # drop circular ref
60 $self->{lei}->lei_atfork_child;
61 $self->{lei}->{sock} = $s if $s;
62 $self->SUPER::ipc_atfork_child;
67 bless { nrd => $nrd }, $cls;