How do I make Net::DNS::Resolver take /etc/hosts into account?

View: New views
11 Messages — Rating Filter:   Alert me  

How do I make Net::DNS::Resolver take /etc/hosts into account?

by Per Jessen :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

All,

for whatever reason, Net::DNS::Resolver (as used in SA) doesn't appear
to look at /etc/hosts.  I thought it was a Net::DNS::Resolver
peculiarity, maybe something to do with cross-platform support, so in
a plugin module I've been writing, I tried to gethostbyname() instead.
I verified it outside of SA, and gethostbyname() does, as expected,
also consult /etc/hosts (provided /etc/nsswitch is correct configured).
However, when I use gethostbyname() from within SA, it does not
consult /etc/hosts ... which led me to conclude that Net::DNS::Resolver
does something to the resolver setup (at init-time) which makes it
ignore /etc/hosts.

Back to the subject line - how do I make Net::DNS::Resolver
take /etc/hosts into account?


/Per Jessen, Zürich


Re: How do I make Net::DNS::Resolver take /etc/hosts into account?

by Per Jessen :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Per Jessen wrote:

> All,
>
> for whatever reason, Net::DNS::Resolver (as used in SA) doesn't appear

Sorry, wrong list.


/Per Jessen, Zürich


Re: How do I make Net::DNS::Resolver take /etc/hosts into account?

by Theo Van Dinter :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wed, Jul 1, 2009 at 3:23 AM, Per Jessen<per@...> wrote:
> Back to the subject line - how do I make Net::DNS::Resolver
> take /etc/hosts into account?

a) of course it doesn't, /etc/hosts isn't DNS, so why would Net::DNS
look at it? :)
b) my guess is that you can't, but it's a question for the Net::DNS
folks, not SA.

Re: How do I make Net::DNS::Resolver take /etc/hosts into account?

by Per Jessen :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Theo Van Dinter wrote:

> On Wed, Jul 1, 2009 at 3:23 AM, Per Jessen<per@...> wrote:
>> Back to the subject line - how do I make Net::DNS::Resolver
>> take /etc/hosts into account?
>
> b) my guess is that you can't, but it's a question for the Net::DNS
> folks, not SA.

Theo, I do appreciate that, but something SA or Net::DNS does changes
the behaviour of subsequent calls to gethostbyname(), so the question
is who is the culprit?  Can you say for certain that SA isn't the
guilty part?  (just so I can start digging into Net::DNS instead).



/Per Jessen, Zürich


Re: How do I make Net::DNS::Resolver take /etc/hosts into account?

by René Berber-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Per Jessen wrote:

> Theo Van Dinter wrote:
>
>> On Wed, Jul 1, 2009 at 3:23 AM, Per Jessen<per@...> wrote:
>>> Back to the subject line - how do I make Net::DNS::Resolver
>>> take /etc/hosts into account?
>> b) my guess is that you can't, but it's a question for the Net::DNS
>> folks, not SA.
>
> Theo, I do appreciate that, but something SA or Net::DNS does changes
> the behaviour of subsequent calls to gethostbyname(), so the question
> is who is the culprit?  Can you say for certain that SA isn't the
> guilty part?  (just so I can start digging into Net::DNS instead).

On many operating systems (Solaris, Fedora 11, and Gentoo Linux are the
ones I have) the file /etc/nsswitch.conf controls exactly what you are
asking, the usual relevant line is:

hosts:       files dns

Which means first look at /etc/hosts, then ask bind (named).

In Fedora 11 the same line is more complex, returning an error before
using named, but that is not your problem:

hosts:       files mdns4_minimal [NOTFOUND=return] dns
--
René Berber


Re: How do I make Net::DNS::Resolver take /etc/hosts into account?

by Per Jessen :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

René Berber wrote:

> On many operating systems (Solaris, Fedora 11, and Gentoo Linux are
> the ones I have) the file /etc/nsswitch.conf controls exactly what you
> are asking, the usual relevant line is:
>
> hosts:       files dns
>
> Which means first look at /etc/hosts, then ask bind (named).
>
> In Fedora 11 the same line is more complex, returning an error before
> using named, but that is not your problem:
>
> hosts:       files mdns4_minimal [NOTFOUND=return] dns

Rene, that's not the issue here at all.

Here it is in a nutshell:

1) a tiny perl test-script using gethostbyname() will look at /etc/hosts
and try to resolve the name from there. Works fine and just as
expected.

2) a call to gethostbyname() from within an SA plugin does NOT look
at /etc/hosts.  

I've tried just instantiating Net::DNS::Resolver in my test-script, but
that changed nothing, which is why I suspect something happens within
SA.


/Per Jessen, Zürich


Re: How do I make Net::DNS::Resolver take /etc/hosts into account?

by Henrik K :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, Jul 02, 2009 at 09:10:54AM +0200, Per Jessen wrote:

> René Berber wrote:
>
> > On many operating systems (Solaris, Fedora 11, and Gentoo Linux are
> > the ones I have) the file /etc/nsswitch.conf controls exactly what you
> > are asking, the usual relevant line is:
> >
> > hosts:       files dns
> >
> > Which means first look at /etc/hosts, then ask bind (named).
> >
> > In Fedora 11 the same line is more complex, returning an error before
> > using named, but that is not your problem:
> >
> > hosts:       files mdns4_minimal [NOTFOUND=return] dns
>
> Rene, that's not the issue here at all.
>
> Here it is in a nutshell:
>
> 1) a tiny perl test-script using gethostbyname() will look at /etc/hosts
> and try to resolve the name from there. Works fine and just as
> expected.
>
> 2) a call to gethostbyname() from within an SA plugin does NOT look
> at /etc/hosts.  

Please share your code so we can verify and test it. There is nothing in SA
code that would redefine gethostbyname.


Re: How do I make Net::DNS::Resolver take /etc/hosts into account?

by Per Jessen :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Henrik K wrote:

> On Thu, Jul 02, 2009 at 09:10:54AM +0200, Per Jessen wrote:
>>
>> Here it is in a nutshell:
>>
>> 1) a tiny perl test-script using gethostbyname() will look at
>> /etc/hosts and try to resolve the name from there. Works fine and
>> just as expected.
>>
>> 2) a call to gethostbyname() from within an SA plugin does NOT look
>> at /etc/hosts.
>
> Please share your code so we can verify and test it. There is nothing
> in SA code that would redefine gethostbyname.

No that's not what I meant - I meant that something changes the
_behaviour_ of gethostbyname() such that it ignores /etc/hosts.  This
could be the resolver config for instance.

To verify it:

Add "1.2.3.4  www.doesnt.matter" to your /etc/hosts file.

Run this script:

http://jessen.ch/files/test_gethostbyname.pl 

"./test_gethostbyname www.doesnt.matter"

This will say "1.2.3.4" as expected.

Now for calling gethostbyname() from within SA - I could share the
plugin code, but it won't work without a few other things, so if you
can think of another/easier way of calling gethostbyname() from within
SA, then you'll see that "www.doesnt.matter" does not resolve.


/Per Jessen, Zürich


Re: How do I make Net::DNS::Resolver take /etc/hosts into account?

by Henrik K :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, Jul 02, 2009 at 10:08:31AM +0200, Per Jessen wrote:
>
> Now for calling gethostbyname() from within SA - I could share the
> plugin code, but it won't work without a few other things, so if you
> can think of another/easier way of calling gethostbyname() from within
> SA, then you'll see that "www.doesnt.matter" does not resolve.

http://sa.hege.li/name.pm
http://sa.hege.li/name.cf

[16752] dbg: gethostbyname: 1.2.3.4

Not that I'm saying your code is bad, but it's impossible to verify it's
good when you don't share it. I just can't believe something inside perl/SA
could affect on how gethostbyname works, but feel free to prove me wrong. :)


Re: How do I make Net::DNS::Resolver take /etc/hosts into account?

by Per Jessen :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Henrik K wrote:

> On Thu, Jul 02, 2009 at 10:08:31AM +0200, Per Jessen wrote:
>>
>> Now for calling gethostbyname() from within SA - I could share the
>> plugin code, but it won't work without a few other things, so if you
>> can think of another/easier way of calling gethostbyname() from
>> within SA, then you'll see that "www.doesnt.matter" does not resolve.
>
> http://sa.hege.li/name.pm
> http://sa.hege.li/name.cf
>
> [16752] dbg: gethostbyname: 1.2.3.4
>
> Not that I'm saying your code is bad, but it's impossible to verify
> it's good when you don't share it.

Well - you've just proven that it IS my fault.  Dunno where yet, but
your code above worked just fine.  Thanks, that is actually good news!


/Per Jessen, Zürich


Re: How do I make Net::DNS::Resolver take /etc/hosts into account?

by Charles Gregory :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, 2 Jul 2009, Per Jessen wrote:
> 1) a tiny perl test-script using gethostbyname() will look at /etc/hosts
> and try to resolve the name from there. Works fine and just as
> expected.
> 2) a call to gethostbyname() from within an SA plugin does NOT look
> at /etc/hosts.

When in doubt, blame permissions. Make sure your first test is being run
as the same user as spamd/spamassassin (or as any unprivileged user).

- C