3

I have test code that I want to have a couple of hostnames resolve to the loopback while testing. When deployed, this code will use the normal system name resolution as appropriate. Test and deployment host are recent linux distros (SLES11SP1, e.g.).

I'd like to override hostname resolution for a single process, without being superuser. Is there a way to manipulate the nsswitch/hostsbehavior in such a narrow fashion?

Yes, of course I could override the hostnames themselves, but I prefer not to (unless this feature really isn't available).


EDIT:

glibc's HOSTALIASES feature sounds like exactly what I want -- but its availability/effectiveness seems inconsistent among the hosts I surveyed. At some point, it was added to be among a list of insecure environment variables. But does that mean it's ignored globally or only in suid binaries? Will it still work for programs which do getnameinfo()?


More edit: IMO, HOSTALIAS wins hands down. Disabling nscd is a workaround for platforms which don't respect it -- like mine (SuSE). And maybe they will release a fix.

Brian Cain
  • 14,403
  • 3
  • 50
  • 88
  • Specific test results using `HOSTALIASES`: Red Hat Linux release 9 (Shrike) w/glibc 2.3.3 -- success overriding, SUSE Linux Enterprise Server 11 (x86_64) w/glibc 2.11.1 -- failure, CentOS 5.6 w/glibc 2.5 -- success. – Brian Cain Oct 31 '11 at 04:24
  • Bugs opened: https://bugzilla.novell.com/show_bug.cgi?id=727360 and upstream - http://sourceware.org/bugzilla/show_bug.cgi?id=13375 – Brian Cain Nov 02 '11 at 15:52

3 Answers3

2

LD_LIBRARY_PATH for the win!

http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html

Also: What is the LD_PRELOAD trick?

Also: http://www.linuxjournal.com/article/7795

Community
  • 1
  • 1
MK.
  • 33,605
  • 18
  • 74
  • 111
  • That will affect the path that the dynamic library loader searches for DSOs. But AFAIK it won't impact name resolution. Unless you're suggesting something super-clever? Put my own host resolution library in the place? If so, please help me connect the dots. – Brian Cain Oct 28 '11 at 21:32
  • 2
    `LD_LIBRARY_PATH` is unlikely to win. `LD_PRELOAD` has a better chance! – Employed Russian Oct 29 '11 at 00:12
  • @EmployedRussian yeah, you right, LD_PRELOAD is probably better for this. – MK. Oct 29 '11 at 01:40
  • @BrianCain yes, you will have to implement your own version of whatever api the program uses to do resolution. – MK. Oct 29 '11 at 01:41
  • Using the LD_PRELOAD suggestion, here's a library to do it: https://github.com/hadess/resolvconf-override – rgs Sep 14 '15 at 01:29
1

Assuming you want to intercept e.g. gethostbyname(), and have it return 127.0.0.1 for certain hostnames ...

If your code is C++, the simplest answer might be to use gMock.

If you can't, you may want to interpose gethostbyname. A sample interposer is documented here.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
1

Brian, another option would be to use chroot. You could create a directory with a bunch of mount --rbind for each of the directories usr, lib, home, etc. - enough to simulate a working root directory. Then use mount -t aufs to "layer" mount the existing etc together with a writable empty layer. In essence, after all that, whatever you change in etc ends up changing only inside that chroot environment. You could override and simulate all kinds of environments that way. If this is of any interest and need me to elaborate further, let me know.

Mike
  • 2,393
  • 3
  • 25
  • 37
  • Yeah, that's a good suggestion. I never stated it in the question (shame on me), but I really wanted to make a very local (just the one process) name resolution change so I wouldn't have to become the superuser. – Brian Cain Nov 16 '11 at 02:58
  • 1
    Brian, I got curious about this, because I can foresee myself having to run a test-case where I want an external host name to resolve to localhost only inside the test while not affecting the rest of the system. This led to HOSTALIASES, and it not working because NSCD caches results, plus there is another cache in GLIBC apparently. I've actually spotted a couple of Debian bug entries with you coincidentally just now asking if it is possible to bypass the NSCD using an environment variable. Apparently this is not possible. I'm thinking that my self I will use the chroot method for the test. – Mike Nov 17 '11 at 03:39
  • "Apparently this is not possible." Because of the lack of reply to [my question](http://sourceware.org/bugzilla/show_bug.cgi?id=13375#c2), or because you've confirmed from another source that it's not possible? – Brian Cain Nov 17 '11 at 05:25
  • I should have said, "after searching around for variables that nscd uses, I could not find anything that would make it bypass its cache or flush its cache. I have yet to see or think up, any way at all to make a host name resolve to something else merely through the setting of environment variables". – Mike Nov 17 '11 at 20:35