| 1 |
#!perl -w |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
auth_checkpassword - Authenticate against a DJB style checkpassword program |
| 6 |
|
| 7 |
=head1 DESCRIPTION |
| 8 |
|
| 9 |
This plugin authenticates users against a DJB style checkpassword |
| 10 |
program. Unlike previous checkpassword implementations, this plugin |
| 11 |
expects qpsmtpd to be running as the qpsmtpd user. Privilege |
| 12 |
escalation can be attained by running the checkpassword binary setuid |
| 13 |
or with sudo. |
| 14 |
|
| 15 |
=head1 CONFIGURATION |
| 16 |
|
| 17 |
Configure the path to your checkpassword binary. You can configure this in |
| 18 |
config/plugins by defining the checkpw and true arguments as follows: |
| 19 |
|
| 20 |
auth/auth_checkpassword checkpw /usr/local/vpopmail/bin/vchkpw true /bin/true |
| 21 |
|
| 22 |
or by editing the config file config/smtpauth-checkpassword: |
| 23 |
|
| 24 |
echo "/usr/local/vpopmail/bin/vchkpw /bin/true" > ~qpsmtpd/config/smtpauth-checkpassword |
| 25 |
|
| 26 |
vchkpw is the checkpassword program provided by vpopmail. Substitute |
| 27 |
your own checkpassword app as appropriate. |
| 28 |
|
| 29 |
If you are using vchkpw and this plugin is being executed by a user ID |
| 30 |
other than 89 or 0 (as is the default), and the vchkpw binary is not |
| 31 |
setuid (as is the default), this plugin will automatically prepend the |
| 32 |
vchkpw command with sudo. If that is the case, you must configure sudo |
| 33 |
by adding these two lines to your sudoers file: |
| 34 |
|
| 35 |
Defaults:qpsmtpd closefrom_override |
| 36 |
qpsmtpd ALL = (ALL) NOPASSWD: /usr/local/vpopmail/bin/vchkpw |
| 37 |
|
| 38 |
The closefrom_override option is necessary because, by default, sudo |
| 39 |
appropriates the first 3 file descriptors. Those descriptors are |
| 40 |
necessary to communicate with the checkpassword program. If you run |
| 41 |
qpsmtpd as some other user, adjust the sudo lines approriately. |
| 42 |
|
| 43 |
Using sudo is preferable to enabling setuid on the vchkpw binary. If |
| 44 |
you reinstall vpopmail and the setuid bit is lost, this plugin will be |
| 45 |
broken. |
| 46 |
|
| 47 |
=head1 SEE ALSO |
| 48 |
|
| 49 |
If you are using this plugin with vpopmail, please read the VPOPMAIL |
| 50 |
section in docs/authentication.pod |
| 51 |
|
| 52 |
=head1 DIAGNOSTICS |
| 53 |
|
| 54 |
Is the path in the config/smtpauth-checkpassword correct? |
| 55 |
|
| 56 |
Is the path to true in config/smtpauth-checkpassword correct? |
| 57 |
|
| 58 |
Is qpsmtpd running as the qpsmtpd user? If not, did you adjust the |
| 59 |
sudo configuration appropriately? |
| 60 |
|
| 61 |
If you are not using sudo, did you remember to make the vchkpw binary |
| 62 |
setuid (chmod 4711 ~vpopmail/bin/vchkpw)? |
| 63 |
|
| 64 |
While writing this plugin, I first wrote myself a little test script, |
| 65 |
which helped me identify the sudo closefrom_override issue. Here is |
| 66 |
that script: |
| 67 |
|
| 68 |
#!/usr/bin/perl |
| 69 |
use strict; |
| 70 |
my $sudo = "/usr/local/bin/sudo"; |
| 71 |
$sudo .= " -C4 -u vpopmail"; |
| 72 |
my $vchkpw = "/usr/local/vpopmail/bin/vchkpw"; |
| 73 |
my $true = "/bin/true"; |
| 74 |
|
| 75 |
open(CPW,"|$sudo $vchkpw $true 3<&0"); |
| 76 |
printf(CPW "%s\0%s\0Y123456\0",'[email protected]','pa55word'); |
| 77 |
close(CPW); |
| 78 |
|
| 79 |
my $status = $?; |
| 80 |
print "FAIL\n" and exit if ( $status != 0 ); |
| 81 |
print "OK\n"; |
| 82 |
|
| 83 |
Save that script to vchkpw.pl and then run it as the same user that |
| 84 |
qpsmtpd runs as: |
| 85 |
|
| 86 |
setuidgid qpsmtpd perl vchkpw.pl |
| 87 |
|
| 88 |
If you aren't using sudo, then remove $sudo from the open line. |
| 89 |
|
| 90 |
=head1 ACKNOWLEDGEMENTS |
| 91 |
|
| 92 |
based upon authcheckpassword by Michael Holzt |
| 93 |
and adapted by Johan Almqvist 2006-01-18 |
| 94 |
|
| 95 |
=head1 AUTHOR |
| 96 |
|
| 97 |
Matt Simerson <[email protected]> |
| 98 |
|
| 99 |
=head1 COPYRIGHT AND LICENSE |
| 100 |
|
| 101 |
Copyright (c) 2010 Matt Simerson |
| 102 |
|
| 103 |
This plugin is licensed under the same terms as the qpsmtpd package itself. |
| 104 |
Please see the LICENSE file included with qpsmtpd for details. |
| 105 |
|
| 106 |
=cut |
| 107 |
|
| 108 |
sub register { |
| 109 |
my ($self, $qp, %args) = @_; |
| 110 |
|
| 111 |
my ($checkpw, $true) = $self->get_checkpw(\%args); |
| 112 |
return DECLINED if !$checkpw || !$true; |
| 113 |
|
| 114 |
$self->connection->notes('auth_checkpassword_bin', $checkpw); |
| 115 |
$self->connection->notes('auth_checkpassword_true', $true); |
| 116 |
|
| 117 |
$self->register_hook('auth-plain', 'auth_checkpassword'); |
| 118 |
$self->register_hook('auth-login', 'auth_checkpassword'); |
| 119 |
} |
| 120 |
|
| 121 |
sub auth_checkpassword { |
| 122 |
my ($self, $transaction, $method, $user, $passClear, $passHash, $ticket) = |
| 123 |
@_; |
| 124 |
|
| 125 |
my $binary = $self->connection->notes('auth_checkpassword_bin'); |
| 126 |
my $true = $self->connection->notes('auth_checkpassword_true'); |
| 127 |
chomp($binary, $true); |
| 128 |
|
| 129 |
my $sudo = get_sudo($binary); |
| 130 |
|
| 131 |
$self->log(LOGDEBUG, "auth_checkpassword: $sudo $binary $true 3<&0"); |
| 132 |
open(CPW, "|$sudo $binary $true 3<&0"); |
| 133 |
printf(CPW "%s\0%s\0Y123456\0", $user, $passClear); |
| 134 |
close(CPW); |
| 135 |
|
| 136 |
my $status = $?; |
| 137 |
|
| 138 |
if ($status != 0) { |
| 139 |
$self->log(LOGNOTICE, "fail, auth failed: $status"); |
| 140 |
return (DECLINED); |
| 141 |
} |
| 142 |
|
| 143 |
$self->connection->notes('authuser', $user); |
| 144 |
$self->log(LOGINFO, "pass, auth success with $method"); |
| 145 |
return (OK, "auth_checkpassword"); |
| 146 |
} |
| 147 |
|
| 148 |
sub get_checkpw { |
| 149 |
my ($self, $args) = @_; |
| 150 |
|
| 151 |
my ($checkpw) = $args->{checkpw} =~ /^(.*)$/ if $args->{checkpw}; # untaint |
| 152 |
my ($true) = $args->{true} =~ /^(.*)$/ if $args->{true}; # untaint |
| 153 |
|
| 154 |
return ($checkpw, $true) |
| 155 |
if ($checkpw && $true && -x $checkpw && -x $true); |
| 156 |
|
| 157 |
my $missing_config = |
| 158 |
"disabled due to invalid configuration. See 'perldoc plugins/auth/auth_checkpassword' for how to configure."; |
| 159 |
|
| 160 |
if (!$self->qp->config('smtpauth-checkpassword')) { |
| 161 |
$self->log(LOGERROR, $missing_config); |
| 162 |
return; |
| 163 |
} |
| 164 |
|
| 165 |
$self->log(LOGNOTICE, "reading config from smtpauth-checkpassword"); |
| 166 |
my $config = $self->qp->config("smtpauth-checkpassword"); |
| 167 |
($checkpw, $true) = $config =~ /^(\S+)\s+(\S+)\s*$/; |
| 168 |
|
| 169 |
if (!$checkpw || !$true || !-x $checkpw || !-x $true) { |
| 170 |
$self->log(LOGERROR, $missing_config); |
| 171 |
return; |
| 172 |
} |
| 173 |
return ($checkpw, $true); |
| 174 |
} |
| 175 |
|
| 176 |
sub get_sudo { |
| 177 |
my $binary = shift; |
| 178 |
|
| 179 |
return '' if $> == 0; # running as root |
| 180 |
return '' if $> == 89 && $binary =~ /vchkpw$/; # running as vpopmail |
| 181 |
|
| 182 |
my $mode = (stat($binary))[2]; |
| 183 |
$mode = sprintf "%lo", $mode & 07777; |
| 184 |
return '' if $mode eq '4711'; # $binary is setuid |
| 185 |
|
| 186 |
my $sudo = `which sudo` || '/usr/local/bin/sudo'; |
| 187 |
return '' if !-x $sudo; |
| 188 |
|
| 189 |
$sudo .= ' -C4'; # prevent sudo from clobbering file descriptor 3 |
| 190 |
|
| 191 |
return $sudo if $binary !~ /vchkpw$/; |
| 192 |
return "$sudo -u vpopmail"; |
| 193 |
} |
| 194 |
|