#!/usr/bin/perl
###
### Pipes mail to a smbhost via smbclient
###
my($smbclient, $message1, $msg1_len, $message2, $msg2_len,
$maxbytes, $host);
###
### Configuration section
###
### Set secure path
$ENV{PATH} = "/usr/local/bin:/usr/bin/:/bin:/usr/local/sbin:/usr/sbin:/sbin";
### Where is the smbclient
$smbclient = "/usr/bin/smbclient -U MAIL";
### The continue message
$message1 = "\n\n...continued in next message\n";
### Length of message1, add one byte for every \n, because
### smbclient converts \n to \r\n
$msg1_len = length($message1)+3;
### The continued message
$message2 = "...continued message:\n\n";
### Length of message2, add one byte for every \n, because
### smbclient converts \n to \r\n
$msg2_len = length($message2)+2;
###
### Nothing to configure below
###
### Maximum bytes per Messages
$maxbytes = 1600 - $msg1_len;
### Get the receiving NetBIOS host as the #1 argument
$host = shift;
### If called non interactivly use the logger to report errors
-t STDERR || open(STDERR,"|logger -tmail2smb")
|| die "$0: Can't open logger: $!\n";
### Hostname is required
($host) || die "No hostname\n";
### Standard output goes to nowhere
open(STDOUT,">/dev/null" );
my($smballow, $smbdeny, %allow, %deny, $key, $val, $checkit, $sendit);
### Set default values
$checkit = 0;
$sendit = 1;
### Read the 'no forward' file
### (this file will be ignored, of the 'forward' file exists)
$smbdeny = 0;
if (open(A,"<.smbnoforward")) {
while () {
s/[\r\n]//g;
($key,$val) = split(/ /, $_, 2);
$deny{$key} = $val;
}
close(A);
$smbdeny = 1;
$checkit = 1;
$sendit = 1;
}
### Read the 'forward' file
$smballow = 0;
if (open(A,"<.smbforward")) {
while () {
s/[\r\n]//g;
($key,$val) = split(/ /, $_, 2);
$allow{$key} = $val;
}
close(A);
$smballow = 1;
$checkit = 1;
$sendit = 0;
$smbdeny = 0;
}
my($inheader,$bytes,$mkey,$mval);
$inheader = 1;
$bytes = $maxbytes;
while (<>) {
if ($inheader) {
if (/^[\r\n]+/) {
if (! $sendit) { exit 0 };
$inheader = 0;
open(S,"|$smbclient -M $host" ) || exit 0;
if ( $from ) { print S $from; $bytes -= length($from)+1; }
elsif ( $from1 ) { print S $from1; $bytes -= length($from1)+1; }
else { print S $return_path; $bytes -= length($return_path)+1; }
print S "$subject\n";
$bytes -= length($subject)+2;
} else {
# Check only if forward files present
if ($checkit) {
($mkey,$mval) = split(/ /, $_, 2);
$mval =~ s/[\r\n]//g;
if ($smbdeny) {
DENY: while (($key,$val) = each %deny) {
if (($mkey eq $key) && ($mval =~ /$val/)) {
$sendit = 0;
$checkit = 0;
last DENY;
}
}
} elsif ($smballow) {
ALLOW: while (($key,$val) = each %allow) {
if (($mkey eq $key) && ($mval =~ /$val/)) {
$sendit = 1;
$checkit = 0;
last ALLOW;
}
}
}
}
if (/^From:/ ) { $from = $_ unless $from; }
elsif (/^From / ) { $from1 = $_ unless $from1; }
elsif (/^Return-Path:/ ) { $return_path = $_ unless $return_path; }
elsif (/^Subject:/ ) { $subject = $_ unless $subject; }
}
} else {
if (length() > $bytes) {
print S $message1;
close(S);
open(S,"|$smbclient -M $host") || exit 0;
print S $message2;
$bytes = $maxbytes - $msg2_len;
}
print S;
$bytes -= length()+1;
}
}
close(S);
exit 0;