summaryrefslogtreecommitdiff
path: root/spamassassin-courier/AuthCourier.pm
blob: cadd660e5a0e3f41a56bbb39f192015fbe0d1faf (plain)
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
#    Copyright (C) 2003  Corporation of Balclutha.  All rights reserved.
#
#    Visit us at http://www.balclutha.org for all of your open source
#    software development and support requirements and hosted solutions.
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
package Mail::SpamAssassin::AuthCourier;

$VERSION=1.0.1;

#
# A mechanism which uses the Courier MTA's authdaemond server to
# determine mail account information.  Courier is found at http://courier-mta.org
#
# This module automagically overrides the builtin getpwnam and getpwuid
# functions.
#
use Exporter;
use IO::Socket::UNIX;


@ISA = qw(Exporter);
@EXPORT = qw( getpwnam getpwuid );

#
# ensure overriding for our own internal usage as well ...
#
use subs qw( getpwnam getpwuid );

#
# For some frustrating reason, the socket seems to be unusable unless set each time.
# Please contact us if you have the solution to this enhancement.
#

BEGIN {
    # $socket = IO::Socket::UNIX->new('/usr/lib/courier/var/authdaemon/socket');
}

END {
    $socket->close if $socket;
}

sub getpwnam {
    my $name = shift;
    my $socket = IO::Socket::UNIX->new('/usr/lib/courier/var/authdaemon/socket');
         $socket = IO::Socket::UNIX->new('/var/run/courier/authdaemon/socket') unless $socket;
         $socket = IO::Socket::UNIX->new('/run/authdaemon/socket') unless $socket;
         die "authdaemond socket error: $!\n" unless $socket;

    print $socket "PRE . login $name\n";

    my %results = ();
    my ($k, $v);
    while (<$socket>) {
	($k,$v) = split '=', $_, 2;
	chomp $v if $v;
	$results{$k} = $v;
    }

    $socket->close if $socket;

    # some auth mechanisms don't return UID - these must be fetched from /etc/passwd or
    # it's moral equivalent until Sam patches these as per my request...
    my $uid = $results{'UID'} || CORE::getpwnam($name); 

    # stop some naf 'uninitialized' errors ...
    return wantarray ? ('','','','','','','') : undef unless $uid; # uid 0 = root !!!

    return wantarray ? ( $results{'USERNAME'}, 
			 $results{'PASSWD'},
			 int($uid),
			 int($results{'GID'}),
			 $results{'QUOTA'},
			 $results{'COMMENT'},
			 $results{'GCOS'},
			 $results{'HOME'},
			 '/bin/bash') : $uid;
}

sub getpwuid {
    return (getpwnam($_[0]))[2];
}


1;


__END__

=head1 SYNOPSIS

  use Mail::SpamAssassin::AuthCourier;


=head1 DESCRIPTION

  Overrides the builtin functions getpwnam, and getpwuid to return 
  information as per the Courier Mail (http://courier-mta.org) 
  authdaemond process rather than from the /etc/passwd file (or it's
  moral equivalent).

  All of the magick happens courtesy of the Exporter module.  If you 
  need the old functions, these are still available via the CORE module.


=head1 INSTALLATION

  Copy this file into the Mail/SpamAssassin directory on your @INC path
  and edit spamd as per the SYNOPSIS.


=head1 AUTHOR

Alan Milligan <alan\x40balclutha.org> ("\x40" is "@" of course)

=cut