FoxtrotUniform has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

I'm trying to write a mail filter using the excellent Mail::Audit module with sendmail 8.11.6, but when the script is run by the mailer (through a .forward file), Sys::Hostname throws errors:

----- The following addresses had permanent fatal errors ----- |/home/matt/devel/bin/mail_filter (reason: 1) (expanded from: <matt@infernus.net>) ----- Transcript of session follows ----- Use of uninitialized value at /usr/libdata/perl/5.00503/Sys/Hostname.p +m line 100. Use of uninitialized value at /usr/libdata/perl/5.00503/Sys/Hostname.p +m line 109. Can't exec "/com/host": No such file or directory at /usr/libdata/perl/5.00503/Sys/Hostname.pm line 115. Cannot get host name of local machine at /usr/local/lib/perl5/site_perl/5.005/Mail/Audit.pm line 17 BEGIN failed--compilation aborted at /home/matt/devel/bin/mail_filter +line 7. 554 5.3.0 unknown mailer error 1

However, when I run ~/devel/bin/mail_filter by hand, it has no problems whatsoever, and neither does Sys::Hostname:

matt@shub-niggurath:~ Thu Jun 20-14:10:28>perl -MSys::Hostname -e 'print hostname, "\n"' shub-niggurath.infernus.net matt@shub-niggurath:~ Thu Jun 20-14:18:00>

I thought that it might be an environment issue... but my mail_filter script is chmod 744: only I can execute it! Since it's obviously being executed by me, it should have the standard paths, and be able to find /bin/hostname... right?

I can't find any indication of this happening to other Mail::Audit users, nor any mention of similar Sys::Hostname bugs. The local documentation of sendmail's .forward files isn't especially helpful: it says what they do, but not how. Help?

--
The hell with paco, vote for Erudil!
:wq

Replies are listed 'Best First'.
Re: Mail::Audit, sendmail, and Sys::Hostname: hostname fails in .forward
by robobunny (Friar) on Jun 20, 2002 at 20:56 UTC
    you can't really trust your path when running something that isn't an interactive shell. things like that typically don't read your /etc/profile, .profile, .bashrc, etc, so you are stuck with a default path the system gives you. of course, that should have /bin in it. you can always set your path at the beginning of your script:
    $ENV{'PATH'} = "/bin:/usr/bin";
    it's a little surprising that it actually got that far, i would expect it to get the hostname long before it resorts to running external commands, from POSIX::uname or one of the other methods it employs.

      Tried setting the path in a BEGIN block, no change whatsoever in the script's behaviour. :-(

      --
      The hell with paco, vote for Erudil!
      :wq

Re: Mail::Audit, sendmail, and Sys::Hostname: hostname fails in .forward
by robobunny (Friar) on Jun 20, 2002 at 21:12 UTC
    try testing POSIX::uname to see if it works
    use POSIX; $host = (POSIX::uname())[1]; print "$host\n";
    it should try to use POSIX::uname after it tries the hostname command, but before it tries uname and /com/host commands. if that works, and you aren't too concerned about portability to non-POSIX systems, you could just use that instead of Sys::Hostname (it's even a standard module, so you don't have anything extra to install).

      sigh

      POSIX::uname works just fine, as does hostname and all the others... EXCEPT when called by Mail::Audit in the mail_filter script, and then only when mail_filter is run by sendmail from the .forward.

      (I'm frustrated, but not with you, robobunny; thanks for your help!)

      --
      The hell with paco, vote for Erudil!
      :wq

Hack -- Success! Re: Mail::Audit, sendmail, and Sys::Hostname: hostname fails in .forward
by FoxtrotUniform (Prior) on Jun 20, 2002 at 21:33 UTC

    By the simple expedient of creating a bogus /com/host script, I've coerced my script into running. (Turns out you can't really do anything with %ENV in a .forward jail.)

    --
    The hell with paco, vote for Erudil!
    :wq