1 | #!/usr/bin/perl |
---|
2 | |
---|
3 | use Mail::ExpandAliases; |
---|
4 | use File::Basename; |
---|
5 | use POSIX qw(getgroups); |
---|
6 | |
---|
7 | use strict; |
---|
8 | use warnings; |
---|
9 | |
---|
10 | sub debug { |
---|
11 | if (defined($ENV{'DEBATHENA_SENDMAIL_DEBUG'}) && |
---|
12 | ($ENV{'DEBATHENA_SENDMAIL_DEBUG'} eq 'yes')) { |
---|
13 | print STDERR "DEBUG: " . join(' ', @_) . "\n"; |
---|
14 | } |
---|
15 | } |
---|
16 | |
---|
17 | my $kuser; |
---|
18 | my $want_auth = $ENV{'DEBATHENA_SENDMAIL_AUTH'} || ''; |
---|
19 | |
---|
20 | system(qw(klist -s)); |
---|
21 | if (($? == 0) && |
---|
22 | (`klist 2>/dev/null` =~ /Default principal: (.*?)\@ATHENA.MIT.EDU/)) { |
---|
23 | $kuser = $1; |
---|
24 | } |
---|
25 | |
---|
26 | my $parser = Mail::ExpandAliases->new; |
---|
27 | |
---|
28 | if (basename($0) eq 'newaliases') { |
---|
29 | my $root = join(', ', @{$parser->expand('root')}); |
---|
30 | if ($root !~ /@/) { |
---|
31 | print STDERR <<EOF |
---|
32 | NOTE: root expands to: $root |
---|
33 | This does not appear to contain a remote address. Since debathena-msmtp |
---|
34 | does not support local delivery, you may wish to send root's mail |
---|
35 | somewhere useful (e.g. your MIT account). |
---|
36 | EOF |
---|
37 | } |
---|
38 | exit 0; |
---|
39 | } |
---|
40 | |
---|
41 | sub from_address { |
---|
42 | # If we have tickets, use them |
---|
43 | if ($kuser) { |
---|
44 | return "--from=" . join('@', $kuser, 'mit.edu'); |
---|
45 | } |
---|
46 | # Note that ATHENA_USER is explicitly not checked here. We've |
---|
47 | # already checked to see if you have Kerberos tickets, and |
---|
48 | # semantically, if you don't have Kerberos tickets, you're not |
---|
49 | # sending as an Athena user. |
---|
50 | my $uname = $ENV{'USER'} || $ENV{'LOGNAME'} || getpwuid($<); |
---|
51 | # Otherwise, assume user@fqdn ... |
---|
52 | chomp(my $maildomain = `hostname --fqdn`); |
---|
53 | # ... except that nss-nonlocal-users are @mit.edu |
---|
54 | if (getgrnam('nss-nonlocal-users')) { |
---|
55 | my $nssnonlocalgid = (getgrnam('nss-nonlocal-users'))[2]; |
---|
56 | if (grep(/^$nssnonlocalgid$/, getgroups())) { |
---|
57 | debug("Assuming \@mit.edu for nss-nonlocal-user $uname"); |
---|
58 | $maildomain = 'mit.edu'; |
---|
59 | } |
---|
60 | } |
---|
61 | return "--from=" . join('@', $uname, $maildomain); |
---|
62 | } |
---|
63 | |
---|
64 | my @aliases = (); |
---|
65 | foreach my $arg (@ARGV) { |
---|
66 | push @aliases, $parser->expand($arg); |
---|
67 | } |
---|
68 | |
---|
69 | if ($kuser && (($want_auth eq 'yes') || ($want_auth eq '')) ) { |
---|
70 | #send auth |
---|
71 | debug(qw{msmtp --host=outgoing.mit.edu --port=587 --auth=gssapi}, "--user=$kuser", from_address(), @aliases); |
---|
72 | exec(qw{msmtp --host=outgoing.mit.edu --port=587 --auth=gssapi}, "--user=$kuser", from_address(), @aliases); |
---|
73 | } |
---|
74 | elsif ($want_auth eq 'yes') { |
---|
75 | $! = 1; |
---|
76 | die "Could not find valid ATHENA.MIT.EDU Kerberos tickets.\n"; |
---|
77 | } |
---|
78 | else { |
---|
79 | #send unauth |
---|
80 | debug(qw{msmtp --host=outgoing.mit.edu --port=25 --auth=off}, from_address(), @aliases); |
---|
81 | exec(qw{msmtp --host=outgoing.mit.edu --port=25 --auth=off}, from_address(), @aliases); |
---|
82 | } |
---|