Debian Codegraph @

plugins/auth/auth_vpopmail_sql

raw
1 #!perl -w
2
3 =head1 NAME
4
5 auth_vpopmail_sql - Authenticate to vpopmail via MySQL
6
7 =head1 DESCRIPTION
8
9 This plugin authenticates vpopmail users directly against a standard
10 vpopmail MySQL database. It makes the not-unreasonable assumption that
11 both pw_name and pw_domain are lowercase only (qmail doesn't actually care).
12 If you are using CRAM-MD5, it also requires that vpopmail be built with the
13 recommended '--enable-clear-passwd=y' option, because there is no way
14 to compare the crypted password.
15
16 =head1 CONFIGURATION
17
18 echo "dbi:mysql:dbname=vpopmail;host=127.0.0.1" > config/vpopmail_mysql_dsn
19 echo "vpopmailuser" > config/vpopmail_mysql_user
20 echo "vpoppasswd" > config/vpopmail_mysql_pass
21
22 This can be a read-only database user since the plugin does not update the
23 last accessed time (yet, see below).
24
25 This module supports PLAIN, LOGIN, and CRAM-MD5 authentication methods. You
26 can disable undesired methods by editing this module and uncommenting
27 the lines in the register() sub. See the POD for Qspmtpd::Auth for more
28 details on the ramifications of supporting various authentication methods.
29
30 The remote user must login with a fully qualified e-mail address (i.e. both
31 account name and domain), even if they don't normally need to. This is
32 because the vpopmail table has a unique index on pw_name/pw_domain, and this
33 module requires that only a single record be returned from the database.
34
35 =head1 LIMITATIONS
36
37 This authentication modules does not recognize domain aliases. So, if you have
38 the domain example.com, with domain aliases for example.org and example.net,
39 smtp-auth will only work for [email protected]. If you have domain aliases,
40 consider using another plugin (see SEE ALSO).
41
42 =head1 FUTURE DIRECTION
43
44 The default MySQL configuration for vpopmail includes a table to log access,
45 lastauth, which could conceivably be updated upon sucessful authentication.
46 The addition of this feature is left as an exercise for someone who cares. ;)
47
48 =head1 SEE ALSO
49
50 For an overview of the vpopmail authentication plugins and their merits,
51 please read the VPOPMAIL section in docs/authentication.pod
52
53 =head1 AUTHOR
54
55 John Peacock <[email protected]>
56
57 =head1 COPYRIGHT AND LICENSE
58
59 Copyright (c) 2004 John Peacock
60
61 This plugin is licensed under the same terms as the qpsmtpd package itself.
62 Please see the LICENSE file included with qpsmtpd for details.
63
64 =cut
65
66 use strict;
67 use warnings;
68
69 use Qpsmtpd::Auth;
70 use Qpsmtpd::Constants;
71
72 #use DBI; # done in ->register
73
74 sub register {
75 my ($self, $qp) = @_;
76
77 eval 'use DBI';
78 if ($@) {
79 warn "plugin disabled. is DBI installed?\n";
80 $self->log(LOGERROR, "skip: plugin disabled. is DBI installed?\n");
81 return;
82 }
83
84 $self->register_hook('auth-plain', 'auth_vmysql');
85 $self->register_hook('auth-login', 'auth_vmysql');
86 $self->register_hook('auth-cram-md5', 'auth_vmysql');
87 }
88
89 sub get_db_handle {
90 my $self = shift;
91
92 my $dsn = $self->qp->config("vpopmail_mysql_dsn")
93 || "dbi:mysql:dbname=vpopmail;host=127.0.0.1";
94 my $dbuser = $self->qp->config("vpopmail_mysql_user") || "vpopmailuser";
95 my $dbpass = $self->qp->config("vpopmail_mysql_pass") || "vpoppasswd";
96
97 my $dbh = DBI->connect($dsn, $dbuser, $dbpass) or do {
98 $self->log(LOGERROR, "skip: db connection failed");
99 return;
100 };
101 $dbh->{ShowErrorStatement} = 1;
102 return $dbh;
103 }
104
105 sub get_vpopmail_user {
106 my ($self, $dbh, $user) = @_;
107
108 my ($pw_name, $pw_domain) = split /@/, lc($user);
109
110 if (!defined $pw_domain) {
111 $self->log(LOGINFO, "skip: missing domain: " . lc $user);
112 return;
113 }
114
115 $self->log(LOGDEBUG, "auth_vpopmail_sql: $user");
116
117 my $query = "SELECT pw_passwd,pw_clear_passwd
118 FROM vpopmail
119 WHERE pw_name = ?
120 AND pw_domain = ?";
121
122 my $sth = $dbh->prepare($query);
123 $sth->execute($pw_name, $pw_domain);
124 my $userd_ref = $sth->fetchrow_hashref;
125 $sth->finish;
126 $dbh->disconnect;
127 return $userd_ref;
128 }
129
130 sub auth_vmysql {
131 my ($self, $transaction, $method, $user, $passClear, $passHash, $ticket) =
132 @_;
133
134 my $dbh = $self->get_db_handle() or return DECLINED;
135 my $u = $self->get_vpopmail_user($dbh, $user) or return DECLINED;
136
137 # if vpopmail was not built with '--enable-clear-passwd=y'
138 # then pw_clear_passwd may not even exist
139 # my $pw_clear_passwd = $db_user->{'pw_clear_passwd'};
140
141 if (!$u->{pw_passwd} && !$u->{pw_clear_passwd}) {
142 $self->log(LOGINFO, "fail: no such user");
143 return (DENY, "auth_vmysql - no such user");
144 }
145
146 # at this point, the user name has matched
147
148 return
149 Qpsmtpd::Auth::validate_password(
150 $self,
151 src_clear => $u->{pw_clear_passwd},
152 src_crypt => $u->{pw_passwd},
153 attempt_clear => $passClear,
154 attempt_hash => $passHash,
155 method => $method,
156 ticket => $ticket,
157 deny => DENY,
158 );
159 }
160