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)
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
13 # | # queries git-credential|
14 # | send net_merge_continue |
17 # recv net_merge_continue <---------/ |
20 # broadcast net_merge_all [ all workers (including LeiAuth worker #0) ]
21 # [ LeiAuth worker #0 becomes just another WQ worker ]
23 # call net_merge_all_done ->-> do per-WQ-class defined actions
24 package PublicInbox::LeiAuth;
28 sub do_auth_atfork { # used by IPC WQ workers
30 return if $wq->{-wq_worker_nr} != 0; # only first worker calls this
32 my $net = $lei->{net};
33 if ($net->{-auth_done}) { # from previous worker... (ugly)
34 $lei->{pkt_op_p}->pkt_do('net_merge_continue', $net) or
35 $lei->fail("pkt_do net_merge_continue: $!");
38 eval { # fill auth info (may prompt user or read netrc)
39 my $mics = $net->imap_common_init($lei);
40 my $nn = $net->nntp_common_init($lei);
41 # broadcast successful auth info to lei-daemon:
42 $net->{-auth_done} = 1;
43 $lei->{pkt_op_p}->pkt_do('net_merge_continue', $net) or
44 die "pkt_do net_merge_continue: $!";
45 $net->{mics_cached} = $mics if $mics;
46 $net->{nn_cached} = $nn if $nn;
51 sub net_merge_all { # called in wq worker via wq_broadcast
52 my ($wq, $net_new) = @_;
53 my $net = $wq->{lei}->{net};
54 %$net = (%$net, %$net_new);
57 # called by top-level lei-daemon when first worker is done with auth
58 # passes updated net auth info to current workers
59 sub net_merge_continue {
60 my ($wq, $lei, $net_new) = @_;
61 $wq->{-net_new} = $net_new; # for "lei up"
62 $wq->wq_broadcast('PublicInbox::LeiAuth::net_merge_all', $net_new);
63 $wq->net_merge_all_done($lei); # defined per-WQ
66 sub op_merge { # prepares PktOp->pair ops
67 my ($self, $ops, $wq, $lei) = @_;
68 $ops->{net_merge_continue} = [ \&net_merge_continue, $wq, $lei ];
71 sub new { bless \(my $x), __PACKAGE__ }