# Authentication worker for anything that needs auth for read/write IMAP
# (eventually for read-only NNTP access)
+#
+# timelines
+# lei-daemon | LeiAuth worker #0 | other WQ workers
+# ----------------------------------------------------------
+# spawns all workers ---->[ workers all start and run ipc_atfork_child ]
+# | do_auth_atfork | wq_worker_loop sleep
+# | # reads .netrc |
+# | # queries git-credential|
+# | send net_merge_continue |
+# | | |
+# | v |
+# recv net_merge_continue <---------/ |
+# | | |
+# v | |
+# broadcast net_merge_all [ all workers (including LeiAuth worker #0) ]
+# [ LeiAuth worker #0 becomes just another WQ worker ]
+# |
+# call net_merge_all_done ->-> do per-WQ-class defined actions
package PublicInbox::LeiAuth;
use strict;
use v5.10.1;
-use parent qw(PublicInbox::IPC);
-use PublicInbox::PktOp qw(pkt_do);
-use PublicInbox::NetReader;
-sub net_merge {
- my ($lei, $net_new) = @_;
- if ($lei->{pkt_op_p}) { # from lei_convert worker
- pkt_do($lei->{pkt_op_p}, 'net_merge', $net_new);
- } else { # single lei-daemon consumer
- my $self = $lei->{auth} or return; # client disconnected
- my $net = $self->{net};
- %$net = (%$net, %$net_new);
+sub do_auth_atfork { # used by IPC WQ workers
+ my ($self, $wq) = @_;
+ return if $wq->{-wq_worker_nr} != 0; # only first worker calls this
+ my $lei = $wq->{lei};
+ my $net = $lei->{net};
+ if ($net->{-auth_done}) { # from previous worker... (ugly)
+ $lei->{pkt_op_p}->pkt_do('net_merge_continue', $net) or
+ $lei->fail("pkt_do net_merge_continue: $!");
+ return;
}
+ eval { # fill auth info (may prompt user or read netrc)
+ my $mics = $net->imap_common_init($lei);
+ my $nn = $net->nntp_common_init($lei);
+ # broadcast successful auth info to lei-daemon:
+ $net->{-auth_done} = 1;
+ $lei->{pkt_op_p}->pkt_do('net_merge_continue', $net) or
+ die "pkt_do net_merge_continue: $!";
+ $net->{mics_cached} = $mics if $mics;
+ $net->{nn_cached} = $nn if $nn;
+ };
+ $lei->fail($@) if $@;
}
-sub do_auth { # called via wq_io_do
- my ($self) = @_;
- my ($lei, $net) = @$self{qw(lei net)};
- $net->imap_common_init($lei);
- net_merge($lei, $net); # tell lei-daemon updated auth info
+sub net_merge_all { # called in wq worker via wq_broadcast
+ my ($wq, $net_new) = @_;
+ my $net = $wq->{lei}->{net};
+ %$net = (%$net, %$net_new);
}
-sub do_finish_auth { # dwaitpid callback
- my ($arg, $pid) = @_;
- my ($self, $lei, $post_auth_cb, @args) = @$arg;
- $? ? $lei->dclose : $post_auth_cb->(@args);
+# called by top-level lei-daemon when first worker is done with auth
+# passes updated net auth info to current workers
+sub net_merge_continue {
+ my ($wq, $lei, $net_new) = @_;
+ $wq->{-net_new} = $net_new; # for "lei up"
+ $wq->wq_broadcast('PublicInbox::LeiAuth::net_merge_all', $net_new);
+ $wq->net_merge_all_done($lei); # defined per-WQ
}
-sub auth_eof {
- my ($lei, $post_auth_cb, @args) = @_;
- my $self = delete $lei->{auth} or return;
- $self->wq_wait_old(\&do_finish_auth, $lei, $post_auth_cb, @args);
+sub op_merge { # prepares PktOp->pair ops
+ my ($self, $ops, $wq, $lei) = @_;
+ $ops->{net_merge_continue} = [ \&net_merge_continue, $wq, $lei ];
}
-sub auth_start {
- my ($self, $lei, $post_auth_cb, @args) = @_;
- $lei->_lei_cfg(1); # workers may need to read config
- my $op = $lei->workers_start($self, 'auth', 1, {
- 'net_merge' => [ \&net_merge, $lei ],
- '' => [ \&auth_eof, $lei, $post_auth_cb, @args ],
- });
- $self->wq_io_do('do_auth', []);
- $self->wq_close(1);
- while ($op && $op->{sock}) { $op->event_step }
-}
-
-sub ipc_atfork_child {
- my ($self) = @_;
- # prevent {sock} from being closed in lei_atfork_child:
- my $s = delete $self->{lei}->{sock};
- delete $self->{lei}->{auth}; # drop circular ref
- $self->{lei}->lei_atfork_child;
- $self->{lei}->{sock} = $s if $s;
- $self->SUPER::ipc_atfork_child;
-}
-
-sub new {
- my ($cls, $net) = @_; # net may be NetReader or descendant (NetWriter)
- bless { net => $net }, $cls;
-}
+sub new { bless \(my $x), __PACKAGE__ }
1;