]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiAuth.pm
lei convert: mail format conversion sub-command
[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 nrd_merge {
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);
21         }
22 }
23
24 sub do_auth { # called via wq_io_do
25         my ($self) = @_;
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
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         $lei->_lei_cfg(1); # workers may need to read config
46         my $ops = {
47                 '!' => [ $lei->can('fail_handler'), $lei ],
48                 '|' => [ $lei->can('sigpipe_handler'), $lei ],
49                 'x_it' => [ $lei->can('x_it'), $lei ],
50                 'child_error' => [ $lei->can('child_error'), $lei ],
51                 'nrd_merge' => [ \&nrd_merge, $lei ],
52                 '' => [ \&auth_eof, $lei, $post_auth_cb, @args ],
53         };
54         ($lei->{pkt_op_c}, $lei->{pkt_op_p}) = PublicInbox::PktOp->pair($ops);
55         $self->wq_workers_start('lei_auth', 1, $lei->oldset, {lei => $lei});
56         my $op = delete $lei->{pkt_op_c};
57         delete $lei->{pkt_op_p};
58         $self->wq_io_do('do_auth', []);
59         $self->wq_close(1);
60         $lei->event_step_init; # wait for shutdowns
61         if ($lei->{oneshot}) {
62                 while ($op->{sock}) { $op->event_step }
63         }
64 }
65
66 sub ipc_atfork_child {
67         my ($self) = @_;
68         # prevent {sock} from being closed in lei_atfork_child:
69         my $s = delete $self->{lei}->{sock};
70         delete $self->{lei}->{auth}; # drop circular ref
71         $self->{lei}->lei_atfork_child;
72         $self->{lei}->{sock} = $s if $s;
73         $self->SUPER::ipc_atfork_child;
74 }
75
76 sub new {
77         my ($cls, $nrd) = @_;
78         bless { nrd => $nrd }, $cls;
79 }
80
81 1;