Re: [exim] getpwuid equivalent for username lookup?

Top Page
Delete this message
Reply to this message
Author: Phil Pennock
Date:  
To: exim-users
Subject: Re: [exim] getpwuid equivalent for username lookup?
On 2007-05-25 at 14:31 +0100, John Robinson wrote:
> I'm trying to get the name of the owner of a file, because I want to run 
> spamassassin as that user (one real user per domain, basically). I 
> started with
>    address_data = ${extract{uid} 
> {${stat:/etc/mail/domains/$domain/mailboxes}} {$value} fail}
> but of course that gets me the uid, not the user name. I've looked and I 
> don't see any expansion method of turning a uid into a username, and a 
> grep of the source doesn't point up any calls to getpwuid() in 
> likely-looking places. So then I've tried


So spamassassin doesn't like a uid on the command-line, so you need Exim
to supply a usercode instead of a uid?

Are you willing to embed Perl in Exim instead of calling an external
command? The Perl interpreter will need to be in memory for
SpamAssassin anyway, so the overhead shouldn't be too horrendous.
The below is untested.

perl_startup = do '/etc/exim/exim_perl_routines.pl'

address_data = ${perl{usercode_of_file_owner}{/etc/mail/domains/$domain/mailboxes}}

/etc/exim/exim_perl_routines.pl :
----------------------------8< cut here >8------------------------------
sub usercode_of_file_owner
{
    my $fn = shift;
    my @st = stat($fn);
    return undef unless scalar @st;
    return scalar getpwuid($st[4]);
}
----------------------------8< cut here >8------------------------------


Comment as appropriate for your site; undef return is a forced fail, a
failed stat (no such file) gives an empty array, hence the return undef.
getpwuid() in scalar context gives the usercode, or undef if the user
doesn't exist, which is also what you want the function to return in
such a case.

-Phil