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
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 ],
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', []);
60 $lei->event_step_init; # wait for shutdowns
61 if ($lei->{oneshot}) {
62 while ($op->{sock}) { $op->event_step }
66 sub ipc_atfork_child {
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;
78 bless { nrd => $nrd }, $cls;