| 1 | package Protocol::DBus::Authn; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Module::Load (); | ||
| 7 | |||
| 8 | use IO::Framed (); | ||
| 9 | |||
| 10 | use Protocol::DBus::Authn::IO (); | ||
| 11 | |||
| 12 | use constant _CRLF => "\x0d\x0a"; | ||
| 13 | |||
| 14 | sub new { | ||
| 15 | my ($class, %opts) = @_; | ||
| 16 | |||
| 17 | my @missing = grep { !$opts{$_} } qw( socket mechanism ); | ||
| 18 | die "Need: @missing" if @missing; | ||
| 19 | |||
| 20 | if (!ref $opts{'mechanism'}) { | ||
| 21 | my $module = __PACKAGE__ . "::Mechanism::$opts{'mechanism'}"; | ||
| 22 | Module::Load::load($module); | ||
| 23 | |||
| 24 | $opts{'mechanism'} = $module->new(); | ||
| 25 | } | ||
| 26 | |||
| 27 | $opts{"_$_"} = delete $opts{$_} for keys %opts; | ||
| 28 | |||
| 29 | $opts{'_io'} = IO::Framed->new( $opts{'_socket'} )->enable_write_queue(); | ||
| 30 | |||
| 31 | my $self = bless \%opts, $class; | ||
| 32 | |||
| 33 | $self->{'_xaction'} = $self->_create_xaction(); | ||
| 34 | |||
| 35 | return $self; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub negotiate_unix_fd { | ||
| 39 | my ($self) = @_; | ||
| 40 | |||
| 41 | $self->{'_negotiate_unix_fd'} = 1; | ||
| 42 | |||
| 43 | return $self; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub _create_xaction { | ||
| 47 | my ($self) = @_; | ||
| 48 | |||
| 49 | # 0 = send; 1 = receive | ||
| 50 | my @xaction = ( | ||
| 51 | [ 0 => 'AUTH', $self->{'_mechanism'}->label(), $self->{'_mechanism'}->INITIAL_RESPONSE() ], | ||
| 52 | $self->{'_mechanism'}->AFTER_AUTH(), | ||
| 53 | |||
| 54 | [ 1 => \&_consume_ok ], | ||
| 55 | |||
| 56 | $self->{'_mechanism'}->AFTER_OK(), | ||
| 57 | |||
| 58 | [ 0 => 'BEGIN' ], | ||
| 59 | ); | ||
| 60 | |||
| 61 | return \@xaction; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub _consume_ok { | ||
| 65 | my ($self, $line) = @_; | ||
| 66 | |||
| 67 | if (index($line, 'OK ') == 0) { | ||
| 68 | $self->{'_server_guid'} = substr($line, 3); | ||
| 69 | } | ||
| 70 | else { | ||
| 71 | die "Unrecognized response: $line"; | ||
| 72 | } | ||
| 73 | |||
| 74 | return; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub pending_send { | ||
| 78 | my ($self) = @_; | ||
| 79 | |||
| 80 | my $next_is_receive = $self->{'_xaction'}[0]; | ||
| 81 | $next_is_receive &&= $next_is_receive->[0]; | ||
| 82 | |||
| 83 | if (!defined $next_is_receive) { | ||
| 84 | die "Authn transaction is done!"; | ||
| 85 | } | ||
| 86 | |||
| 87 | return !$next_is_receive; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub go { | ||
| 91 | my ($self) = @_; | ||
| 92 | |||
| 93 | my $s = $self->{'_socket'}; | ||
| 94 | |||
| 95 | $self->{'_sent_initial'} ||= do { | ||
| 96 | $self->{'_mechanism'}->send_initial($s); | ||
| 97 | }; | ||
| 98 | |||
| 99 | if ($self->{'_sent_initial'}) { | ||
| 100 | LINES: | ||
| 101 | { | ||
| 102 | if ( $self->{'_io'}->get_write_queue_count() ) { | ||
| 103 | $self->{'_io'}->flush_write_queue() or last LINES; | ||
| 104 | } | ||
| 105 | |||
| 106 | while ( my $cur = $self->{'_xaction'}[0] ) { | ||
| 107 | if ($cur->[0]) { | ||
| 108 | my $line = $self->_read_line() or last LINES; | ||
| 109 | $cur->[1]->($self, $line); | ||
| 110 | } | ||
| 111 | else { | ||
| 112 | $self->_send_line(join(' ', @{$cur}[ 1 .. $#$cur ])) or last LINES; | ||
| 113 | } | ||
| 114 | |||
| 115 | shift @{ $self->{'_xaction'} }; | ||
| 116 | } | ||
| 117 | |||
| 118 | return 1; | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | return undef; | ||
| 123 | } | ||
| 124 | |||
| 125 | sub cancel { | ||
| 126 | my ($self) = @_; | ||
| 127 | |||
| 128 | die 'unimplemented'; | ||
| 129 | } | ||
| 130 | |||
| 131 | sub _send_line { | ||
| 132 | my ($self) = @_; | ||
| 133 | |||
| 134 | my $ok = $self->{'_io'}->write( $_[1] . _CRLF() ); | ||
| 135 | return $self->_flush_write_queue(); | ||
| 136 | } | ||
| 137 | |||
| 138 | sub _flush_write_queue { | ||
| 139 | my ($self) = @_; | ||
| 140 | |||
| 141 | local $SIG{'PIPE'} = 'IGNORE'; | ||
| 142 | |||
| 143 | return $self->{'_io'}->flush_write_queue(); | ||
| 144 | } | ||
| 145 | |||
| 146 | sub _read_line { | ||
| 147 | my $line; | ||
| 148 | |||
| 149 | if ($line = $_[0]->{'_io'}->read_until("\x0d\x0a")) { | ||
| 150 | chop $line; | ||
| 151 | chop $line; | ||
| 152 | } | ||
| 153 | |||
| 154 | return $line; | ||
| 155 | } | ||
| 156 | |||
| 157 | 1; |