Debian Codegraph @

plugins/auth/auth_vpopmaild

raw
1 #!perl -w
2
3 use strict;
4 use warnings;
5
6 use Qpsmtpd::Constants;
7 use IO::Socket;
8 use version;
9 my $VERSION = qv('1.0.4');
10
11 sub register {
12 my ($self, $qp, %args) = @_;
13
14 $self->{_vpopmaild_host} = $args{host} || 'localhost';
15 $self->{_vpopmaild_port} = $args{port} || '89';
16
17 $self->register_hook('auth-plain', 'auth_vpopmaild');
18 $self->register_hook('auth-login', 'auth_vpopmaild');
19
20 #$self->register_hook('auth-cram-md5', 'auth_vpopmaild'); # not supported
21 }
22
23 sub auth_vpopmaild {
24 my ($self, $transaction, $method, $user, $passClear, $passHash, $ticket) =
25 @_;
26
27 if (!$passClear) {
28 $self->log(LOGINFO, "skip: vpopmaild does not support cram-md5");
29 return DECLINED;
30 }
31
32 my $socket = $self->get_socket() or return DECLINED;
33
34 $self->log(LOGDEBUG, "attempting $method");
35
36 # Get server greeting (+OK)
37 my $response = $self->get_response( $socket, '' )
38 or return DECLINED;
39
40 if ($response !~ /^\+OK/) {
41 $self->log(LOGERROR, "skip, bad connection response: $response");
42 close $socket;
43 return DECLINED;
44 }
45
46 print $socket "login $user $passClear\n\r"; # send login details
47 $response = $self->get_response( $socket, "login $user $passClear\n\r" )
48 or return DECLINED;
49
50 close $socket;
51
52 # check for successful login (single line (+OK) or multiline (+OK+))
53 if ($response =~ /^\+OK/) {
54 $self->log(LOGINFO, "pass, clear");
55 return (OK, 'auth_vpopmaild');
56 }
57
58 chomp $response;
59 $self->log(LOGNOTICE, "fail, $response");
60 return DECLINED;
61 }
62
63 sub get_response {
64 my ($self, $socket, $send) = @_;
65
66 print $socket $send if $send; # send request
67 my $response = <$socket>; # get response
68 chomp $response;
69
70 if ( ! defined $response ) {
71 $self->log(LOGERROR, "error, no connection response");
72 close $socket;
73 return;
74 }
75
76 if ($response =~ /^([ -~\n\r]+)$/) { # match ascii printable
77 $response = $1; # $response now untainted
78 }
79 else {
80 $self->log(LOGERROR, "error, response unsafe.");
81 };
82
83 return $response;
84 };
85
86 sub get_socket {
87 my ($self) = @_;
88
89 # create socket
90 my $socket =
91 IO::Socket::INET->new(
92 PeerAddr => $self->{_vpopmaild_host},
93 PeerPort => $self->{_vpopmaild_port},
94 Proto => 'tcp',
95 Type => SOCK_STREAM
96 )
97 or do {
98 $self->log(LOGERROR, "skip, socket connection to vpopmaild failed");
99 return;
100 };
101 return $socket;
102 };
103
104 __END__
105
106 =head1 NAME
107
108 auth_vpopmaild - Authenticate to vpopmaild
109
110 =head1 DESCRIPTION
111
112 Authenticates the user against against vpopmaild [1] daemon.
113
114 =head1 CONFIGURATION
115
116 Add a line to C<config/plugins> as follows:
117
118 auth_vpopmaild
119
120 By default, the plugin connects to localhot on port 89. If your vpopmaild
121 daemon is running on a different host or port, specify as follows:
122
123 auth_vpopmaild host [host] port [port]
124
125 =head1 SEE ALSO
126
127 For an overview of the vpopmail authentication plugins and their merits,
128 please read the VPOPMAIL section in doc/authentication.pod
129
130 =head1 LINKS
131
132 [1] http://www.qmailwiki.org/Vpopmaild
133
134 =head1 AUTHOR
135
136 Robin Bowes <[email protected]>
137
138 2012 Matt Simerson (updated response parsing, added logging)
139
140 2013 Matt Simerson - split get_response and get_socket into new methods, added taint checking to responses
141
142 =head1 COPYRIGHT AND LICENSE
143
144 Copyright (c) 2010 Robin Bowes
145
146 This plugin is licensed under the same terms as the qpsmtpd package itself.
147 Please see the LICENSE file included with qpsmtpd for details.
148
149 =cut