Debian Codegraph @

plugins/auth/auth_cvm_unix_local

raw
1 #!perl -w
2
3 =head1 NAME
4
5 auth_cvm_unix_local - SMTP AUTH LOGIN module using
6 Bruce Guenther's Credential Validation Module (CVM)
7 http://untroubled.org/cvm/
8
9 =head1 SYNOPSIS
10
11 In config/plugins:
12
13 auth/auth_cvm_unix_local \
14 cvm_socket /var/lib/cvm/cvm-unix-local.socket \
15 enable_smtp no \
16 enable_ssmtp yes
17
18 =head1 BUGS
19
20 - Should probably handle auth-cram-md5 as well. However, this requires
21 access to the plain text password. We could store a separate database
22 of passwords purely for SMTP AUTH, for example as an optional
23 SMTPAuthPassword property of an account in the esmith::AccountsDB;
24
25 =head1 DESCRIPTION
26
27 This plugin implements an authentication plugin using Bruce Guenther's
28 Credential Validation Module (http://untroubled.org/cvm).
29
30 =head1 AUTHOR
31
32 Copyright 2005 Gordon Rowell <[email protected]>
33
34 This software is free software and may be distributed under the same
35 terms as qpsmtpd itself.
36
37 =head1 VERSION
38
39 Version $Id: auth_cvm_unix_local,v 1.1 2005/06/09 22:50:06 gordonr Exp gordonr $
40
41 =cut
42
43 use strict;
44 use warnings;
45
46 use Qpsmtpd::Constants;
47
48 use Socket;
49 use constant SMTP_PORT => getservbyname("smtp", "tcp") || 25;
50 use constant SSMTP_PORT => getservbyname("ssmtp", "tcp") || 465;
51
52 sub register {
53 my ($self, $qp, %arg) = @_;
54
55 unless ($arg{cvm_socket}) {
56 $self->log(LOGERROR, "skip: requires cvm_socket argument");
57 return 0;
58 }
59
60 $self->{_args} = {%arg};
61 $self->{_enable_smtp} = $arg{enable_smtp} || 'no';
62 $self->{_enable_ssmtp} = $arg{enable_ssmtp} || 'yes';
63
64 my $port = $ENV{PORT} || SMTP_PORT;
65
66 if ($arg{enable_smtp} ne 'yes' && ($port == SMTP_PORT || $port == 587)) {
67 $self->log(LOGDEBUG, "skip: enable_smtp=no");
68 return 0;
69 }
70 if ($port == SSMTP_PORT && $arg{enable_ssmtp} ne 'yes') {
71 $self->log(LOGDEBUG, "skip: enable_ssmtp=no");
72 return 0;
73 };
74
75 if ($arg{cvm_socket} =~ /^([\w\/.-]+)$/) {
76 $self->{_cvm_socket} = $1;
77 }
78
79 unless (-S $self->{_cvm_socket}) {
80 $self->log(LOGERROR, "skip: cvm_socket missing or not usable");
81 return 0;
82 }
83
84 $self->register_hook("auth-plain", "authcvm_plain");
85 $self->register_hook("auth-login", "authcvm_plain");
86 #$self->register_hook("auth-cram-md5", "authcvm_hash");
87 }
88
89 sub authcvm_plain {
90 my ($self, $transaction, $method, $user, $passClear, $passHash, $ticket) =
91 @_;
92
93 if ($user =~ /\x00/) {
94 $self->log(LOGERROR, "deny: invalid username");
95 return (DENY, "authcvm, invalid username");
96 };
97
98 socket(SOCK, PF_UNIX, SOCK_STREAM, 0) or do {
99 $self->log(LOGERROR, "skip: socket creation attempt for: $user");
100 return (DENY, "authcvm");
101 };
102
103 # DENY, really? Should this plugin return a DENY when it cannot connect
104 # to the cvs socket? I'd expect such a failure to return DECLINED, so
105 # any other auth plugins could take a stab at authenticating the user
106
107 connect(SOCK, sockaddr_un($self->{_cvm_socket})) or do {
108 $self->log(LOGERROR, "skip: socket connection attempt for: $user");
109 return (DENY, "authcvm, connection failed");
110 };
111
112 my $o = select(SOCK);
113 $| = 1;
114 select($o);
115
116 my ($u, $host) = split(/\@/, $user);
117 $host ||= "localhost";
118
119 print SOCK "\001$u\000$host\000$passClear\000\000";
120
121 shutdown SOCK, 1; # tell remote we're finished
122
123 my $ret = <SOCK>;
124 my ($s) = unpack("C", $ret);
125
126 if (!defined $s) {
127 $self->log(LOGERROR, "skip: no response from cvm for $user");
128 return (DECLINED);
129 }
130
131 if ($s == 0) {
132 $self->log(LOGINFO, "pass: authentication for: $user");
133 return (OK, "auth success for $user");
134 }
135
136 if ($s == 100) {
137 $self->log(LOGINFO, "fail: authentication failure for: $user");
138 return (DENY, 'auth failure (100)');
139 }
140
141 $self->log(LOGERROR, "skip: unknown response from cvm for $user");
142 return (DECLINED, "unknown result code ($s)");
143 }