Sam Trenholme's webpage
This article was posted to the Usenet group alt.hackers in 1995; any technical information is probably outdated.

perl program question


Article: 7557 of alt.hackers
Newsgroups: alt.hackers
From: byerr@netcom.com (*greeeeaaaaaaat*)
Subject: perl program question
Message-ID: byerrD66o07.AHG@netcom.com
Organization: NETCOM On-line Communication Services (408 261-4700 guest)
Date: Wed, 29 Mar 1995 03:50:31 GMT
Approved: What?
Lines: 158
Sender: byerr@netcom20.netcom.com
Status: RO

ObHack/ObQuestion:

I'm using term on my linux box.  I got sendmail compiled, and modified
it to listen for smtp connections on port 4225 or the machine I'm
dialed into.  It's run out of my .forward.  If I'm logged in, it's
supoosed to deliver the mail immediately. Otherwise, it's supposed to
defer it into a file, to be forwarded through a script.  (It basically
does formail -s smtpforward).  Now, the problem is, it always defers
the mail fine.  When I'm logged in, it never actually forwards it,
just defers it (when run from the .forward).  But, when I run the
script that forwards all of the deferred mail, it works fine!  Anyway,
here is the script:

(Oh, and uhost is a script that takes a username as an argument and
returns something like 'byerr@netcom20')

smtpforward:

#!/usr/local/bin/perl  --       # -*-Perl-*-

open(FOO,"/u1/byerr/bin/uhost byerr|");
$_=<FOO>;
split('@');
$them=$_[1] if $_[1] =~ /netcom/;
$trycount=0;
@message=<STDIN>;
foreach (@message) {
    if(/From /) {
        @foo=split;
        $from=$foo[1];
    }
}
print "Host: $them\n";
print "From: $from\n";

main:
{
# ($them,$port)=@ARGV;
$port=4225 unless $port;
$them= "localhost" unless $them;

$AF_INET=2;
$SOCK_STREAM=1;

$SIG{'INT'}='dokill';
sub dokill {
    kill 9,$child if $child;
}

$sockaddr='S n a4 x8';
chop($hostname=`hostname`);

($name,$aliases,$proto)=getprotobyname('tcp');
($name,$aliases,$port)=getservbyname($port, 'tcp') unless $port=~
/^\d+$/;;
($name,$aliases,$type,$len,$thisaddr)=gethostbyname($hostname);
($name,$aliases,$type,$le,$thataddr)=gethostbyname($them);

$this=pack($sockaddr, $AF_INET, 0, $thisaddr);
$that=pack($sockaddr, $AF_INET, $port, $thataddr);

# Make the socket filehandle.

if(socket(S, $AF_INET, $SOCK_STREAM, $proto)) {
    print "socket ok\n";
}
else {
    die $!;
}

# Give the socket an address.

if (bind(S, $this)) {
    print "bind ok\n";
}
else {
    die $!;
}

# Call up the server,
if (connect(S, $that)) {
    print "connect ok\n";
}
else {
#    die $!;
    $trycount++;
    if($trycount<4)
    {
        print "retry $trycount\n";
        goto main;
    }
    # Defer the message for when sendmail is running again.
    open(DEFER,">>/u1/berr/incoming/deferred");
    foreach(@message) {
        print DEFER;
    }
    print "mail deferred\n";
    exit;
}

# Set socket to be command buffered.

select(S); $|=1; select(STDOUT);

sleep 3;
print S "EHLO smtpforward\n";
sleep 5;
print S "MAIL FROM: $from\n";
sleep 2;
print S "RCPT TO: root\n";
sleep 5;
print S "data\n";
sleep 2;
foreach(@message) {
    print S;
}
print S ".\n";
sleep 20;
print S "quit\n";
sleep 3;
print "Done.\n";
if(-r "/u1/byerr/incoming/deferred") {
#    print "Sending deferred messages...\n";
#    system("/u1/byerr/dodeferred");
    }
exit;
}

(if you notice, the commented out part at the end doesn't work,
either... *sigh*)

this is my .forward:
"|exec /usr/local/bin/procmail #byerr "

this is part of my .procmailrc:
:0 c
|/u1/byerr/smtpforward

this is dodeferred:
#!/bin/sh

if test -s /u1/byerr/incoming/deferred;
then
        sleep 180
        mv /u1/byerr/incoming/deferred /u1/byerr/incoming/deferred.sending
        cat /u1/byerr/incoming/deferred.sending|formail -s
        /u1/byerr/smtpforward
else
        echo No deferred mail.
fi


Thanks for the help!

--
Ben Byer      byerr@netcom.com     I am not a bushing
GC2.1:G!d?H+s+:g-p?+!aua--w+v-C++++US++P+L++3-EN+++K---W---
M--V--poY+t+5!jRG+tv-b+++D++B---e(see a)u+(*)h!f+n----!y




Back to index