File: auth_vpopmaild

package info (click to toggle)
qpsmtpd 0.94-8
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,340 kB
  • sloc: perl: 17,176; sh: 543; makefile: 186; sql: 100
file content (149 lines) | stat: -rw-r--r-- 3,700 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!perl -w

use strict;
use warnings;

use Qpsmtpd::Constants;
use IO::Socket;
use version;
my $VERSION = qv('1.0.4');

sub register {
    my ($self, $qp, %args) = @_;

    $self->{_vpopmaild_host} = $args{host} || 'localhost';
    $self->{_vpopmaild_port} = $args{port} || '89';

    $self->register_hook('auth-plain', 'auth_vpopmaild');
    $self->register_hook('auth-login', 'auth_vpopmaild');

    #$self->register_hook('auth-cram-md5', 'auth_vpopmaild'); # not supported
}

sub auth_vpopmaild {
    my ($self, $transaction, $method, $user, $passClear, $passHash, $ticket) =
      @_;

    if (!$passClear) {
        $self->log(LOGINFO, "skip: vpopmaild does not support cram-md5");
        return DECLINED;
    }

    my $socket = $self->get_socket() or return DECLINED;

    $self->log(LOGDEBUG, "attempting $method");

    # Get server greeting (+OK)
    my $response = $self->get_response( $socket, '' )
        or return DECLINED;

    if ($response !~ /^\+OK/) {
        $self->log(LOGERROR, "skip, bad connection response: $response");
        close $socket;
        return DECLINED;
    }

    print $socket "login $user $passClear\n\r";  # send login details
    $response = $self->get_response( $socket, "login $user $passClear\n\r" )
        or return DECLINED;

    close $socket;

    # check for successful login (single line (+OK) or multiline (+OK+))
    if ($response =~ /^\+OK/) {
        $self->log(LOGINFO, "pass, clear");
        return (OK, 'auth_vpopmaild');
    }

    chomp $response;
    $self->log(LOGNOTICE, "fail, $response");
    return DECLINED;
}

sub get_response {
    my ($self, $socket, $send) = @_;

    print $socket $send if $send;     # send request
    my $response = <$socket>;         # get response
    chomp $response;

    if ( ! defined $response ) {
        $self->log(LOGERROR, "error, no connection response");
        close $socket;
        return;
    }

    if ($response =~ /^([ -~\n\r]+)$/) { # match ascii printable
        $response = $1;                  # $response now untainted
    }
    else {
        $self->log(LOGERROR, "error, response unsafe.");
    };

    return $response;
};

sub get_socket {
    my ($self) = @_;

    # create socket
    my $socket =
      IO::Socket::INET->new(
                            PeerAddr => $self->{_vpopmaild_host},
                            PeerPort => $self->{_vpopmaild_port},
                            Proto    => 'tcp',
                            Type     => SOCK_STREAM
                           )
      or do {
        $self->log(LOGERROR, "skip, socket connection to vpopmaild failed");
        return;
      };
    return $socket;
};

__END__

=head1 NAME

auth_vpopmaild - Authenticate to vpopmaild

=head1 DESCRIPTION

Authenticates the user against against vpopmaild [1] daemon.

=head1 CONFIGURATION

Add a line to C<config/plugins> as follows:

auth_vpopmaild

By default, the plugin connects to localhot on port 89. If your vpopmaild
daemon is running on a different host or port, specify as follows:

auth_vpopmaild host [host] port [port]

=head1 SEE ALSO

For an overview of the vpopmail authentication plugins and their merits,
please read the VPOPMAIL section in doc/authentication.pod

=head1 LINKS

[1] http://www.qmailwiki.org/Vpopmaild

=head1 AUTHOR

Robin Bowes <robin.bowes@yo61.com>

2012 Matt Simerson (updated response parsing, added logging)

2013 Matt Simerson - split get_response and get_socket into new methods, added taint checking to responses

=head1 COPYRIGHT AND LICENSE

Copyright (c) 2010 Robin Bowes

This plugin is licensed under the same terms as the qpsmtpd package itself.
Please see the LICENSE file included with qpsmtpd for details.

=cut