]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiAuth.pm
465a2758466851321c996189f79c30a22b06746d
[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 #
7 # timelines
8 # lei-daemon              |  LeiAuth worker #0      | other WQ workers
9 # ----------------------------------------------------------
10 # spawns all workers ---->[ workers all start and run ipc_atfork_child ]
11 #                         | do_auth_atfork          | wq_worker_loop sleep
12 #                         | # reads .netrc          |
13 #                         | # queries git-credential|
14 #                         | send net_merge_continue |
15 #                         |         |               |
16 #                         |         v               |
17 # recv net_merge_continue <---------/               |
18 #            |            |                         |
19 #            v            |                         |
20 # broadcast net_merge_all [ all workers (including LeiAuth worker #0) ]
21 #                         [ LeiAuth worker #0 becomes just another WQ worker ]
22 #                         |
23 # call net_merge_all_done ->-> do per-WQ-class defined actions
24 package PublicInbox::LeiAuth;
25 use strict;
26 use v5.10.1;
27
28 sub do_auth_atfork { # used by IPC WQ workers
29         my ($self, $wq) = @_;
30         return if $wq->{-wq_worker_nr} != 0; # only first worker calls this
31         my $lei = $wq->{lei};
32         my $net = $lei->{net};
33         eval { # fill auth info (may prompt user or read netrc)
34                 my $mics = $net->imap_common_init($lei);
35                 my $nn = $net->nntp_common_init($lei);
36                 # broadcast successful auth info to lei-daemon:
37                 $lei->{pkt_op_p}->pkt_do('net_merge_continue', $net) or
38                                 die "pkt_do net_merge_continue: $!";
39                 $net->{mics_cached} = $mics if $mics;
40                 $net->{nn_cached} = $nn if $nn;
41         };
42         $lei->fail($@) if $@;
43 }
44
45 sub net_merge_all { # called in wq worker via wq_broadcast
46         my ($wq, $net_new) = @_;
47         my $net = $wq->{lei}->{net};
48         %$net = (%$net, %$net_new);
49 }
50
51 # called by top-level lei-daemon when first worker is done with auth
52 # passes updated net auth info to current workers
53 sub net_merge_continue {
54         my ($wq, $net_new) = @_;
55         $wq->wq_broadcast('PublicInbox::LeiAuth::net_merge_all', $net_new);
56         $wq->net_merge_all_done; # defined per-WQ
57 }
58
59 sub op_merge { # prepares PktOp->pair ops
60         my ($self, $ops, $wq) = @_;
61         $ops->{net_merge_continue} = [ \&net_merge_continue, $wq ];
62 }
63
64 sub new { bless \(my $x), __PACKAGE__ }
65
66 1;