]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiAuth.pm
lei: _lei_cfg: return empty hashref if unconfigured
[public-inbox.git] / lib / PublicInbox / LeiAuth.pm
1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # Authentication worker for anything that needs auth for read/write IMAP
5 # (eventually for read-only NNTP access)
6 package PublicInbox::LeiAuth;
7 use strict;
8 use v5.10.1;
9 use parent qw(PublicInbox::IPC);
10 use PublicInbox::PktOp qw(pkt_do);
11 use PublicInbox::NetReader;
12
13 sub net_merge {
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);
21         }
22 }
23
24 sub do_auth { # called via wq_io_do
25         my ($self) = @_;
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
29 }
30
31 sub do_finish_auth { # dwaitpid callback
32         my ($arg, $pid) = @_;
33         my ($self, $lei, $post_auth_cb, @args) = @$arg;
34         $? ? $lei->dclose : $post_auth_cb->(@args);
35 }
36
37 sub auth_eof {
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);
41 }
42
43 sub auth_start {
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 ],
48         });
49         $self->wq_io_do('do_auth', []);
50         $self->wq_close(1);
51         while ($op && $op->{sock}) { $op->event_step }
52 }
53
54 sub ipc_atfork_child {
55         my ($self) = @_;
56         delete $self->{lei}->{auth}; # drop circular ref
57         $self->{lei}->lei_atfork_child;
58         $self->SUPER::ipc_atfork_child;
59 }
60
61 sub new {
62         my ($cls, $net) = @_; # net may be NetReader or descendant (NetWriter)
63         bless { net => $net }, $cls;
64 }
65
66 1;