Recent

Author Topic: GetDomainName() implementation  (Read 3780 times)

MarkMLl

  • Hero Member
  • *****
  • Posts: 6692
GetDomainName() implementation
« on: December 29, 2019, 12:30:49 pm »
This has been discusssed before, without really getting beyond "the implementation in the unix unit doesn't do anything useful".

What appears to be happening is that GetDomainName() and GetHostName() use information returned by the (Linux) kernel which is the same as would be displayed by  nisdomainname  and  uname -n  (or the standard hostname program) respectively. If NIS (AKA YP) isn't active the domain will be returned as (none). The  nisdomainname  program is also known as ypdomainname and domainname, at least on Debian.

Using an fcl-net lookup to get the (or at least a plausible) domain name is not a realistic alternative: since GetHostName() returns the system's preferred name without delay then GetDomainName() could be expected to behave the same. Generally speaking, preferred names are set up during system installation, querying a DNS server often muddies the water by also returning various aliases.

The manpage for domainname (etc.) suggests that the preferred way of getting the domain name is to look for the host's entry in the /etc/hosts file, which generally speaking will be set up at installation. I can confirm that this appears to work fairly well on Linux, but have not had an opportunity to test on e.g. Solaris.

Would it be appropriate to submit my implementation via Mantis?

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2023
  • Former Delphi 1-7, 10.2 user
Re: GetDomainName() implementation
« Reply #1 on: December 29, 2019, 02:40:39 pm »
On FreeBSD and macOS, the FQDN is returned from the sysctl kern.hostname:

macOS

Code: [Select]
[/Users/trev/lazarus-src] $ sysctl kern.hostname
kern.hostname: macmini6.sentry.org

FreeBSD

Code: [Select]
[/home/trev] $ sysctl kern.hostname
kern.hostname: shadow.sentry.org

macOS hosts file:

Code: [Select]
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost
fe80::1%lo0     localhost

FreeBSD hosts file

Code: [Select]
    # $FreeBSD: stable/11/etc/hosts 109997 2003-01-28 21:29:23Z dbaker $
    #
    #
    # Host Database
    # This file should contain the addresses and aliases
    # for local hosts that share this file.
    # In the presence of the domain name service or NIS, this file may
    # not be consulted at all; see /etc/nsswitch.conf for the resolution order.
    #
    #
    ::1                     localhost localhost.my.domain myname.my.domain
    127.0.0.1               localhost localhost.my.domain myname.my.domain
   
    #
    # Imaginary network.
    #10.0.0.2               myname.my.domain myname
    #10.0.0.3               myfriend.my.domain myfriend
    #
    # According to RFC 1918, you can use the following IP networks for
    # private nets which will never be connected to the Internet:
    #
    #       10.0.0.0        -   10.255.255.255
    #       172.16.0.0      -   172.31.255.255
    #       192.168.0.0     -   192.168.255.255
    #

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11458
  • FPC developer.
Re: GetDomainName() implementation
« Reply #2 on: December 29, 2019, 02:47:22 pm »
In my linux (debian) server, domain doesn't seem to be setup, except as 127.0.0.1. (IIRC a workaround for openvpn)

I assume more machines that don't do mail don't have it set up?

I checked /proc/sys/kernel/domainname

but it lists none

MarkMLl

  • Hero Member
  • *****
  • Posts: 6692
Re: GetDomainName() implementation
« Reply #3 on: December 29, 2019, 03:18:00 pm »
@trev: Thanks for those. So FreeBSD appears to have a domain which can be extracted from the hosts file, but MacOS doesn't. Does MacOS have a domain in its  uname -a  output?

@marcov: Is there anything in your /etc/hosts file? It looks as though the Linux kernel (or possibly startup scripts or systemd) are for some reason assuming that the "native" name resolution is NIS/YP. I'll take a look at some startup scripts later (or possibly oevr the next day or so) and try to work out what's actuyally being parsed.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11458
  • FPC developer.
Re: GetDomainName() implementation
« Reply #4 on: December 29, 2019, 04:28:18 pm »
@trev: Thanks for those. So FreeBSD appears to have a domain which can be extracted from the hosts file, but MacOS doesn't. Does MacOS have a domain in its  uname -a  output?

@marcov: Is there anything in your /etc/hosts file?

This is debian oldstable, contents are:
Code: [Select]
127.0.0.1       localhost
127.0.1.1       zeus.fdqnsomething.com     zeus

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters


MarkMLl

  • Hero Member
  • *****
  • Posts: 6692
Re: GetDomainName() implementation
« Reply #5 on: December 29, 2019, 04:45:25 pm »
@marcov: Much what I see in Debian stable ("Buster"). not sure what effect an IP6-only system would have, but I think the names would probably be parseable in the same way.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2023
  • Former Delphi 1-7, 10.2 user
Re: GetDomainName() implementation
« Reply #6 on: December 30, 2019, 01:50:11 am »
@trev: Thanks for those. So FreeBSD appears to have a domain which can be extracted from the hosts file

Nope - domain is not in the hosts file. It is set during installation in etc/rc.conf:

Code: [Select]
hostname="shadow.sentry.org"
ifconfig_bge0="inet 192.168.1.4 netmask 255.255.255.0"
defaultrouter="192.168.1.1"
ifconfig_bge0_ipv6="inet6 2400:4dc0:0:1a01:3631:c4dd:0:22 prefixlen 64"
ipv6_defaultrouter="fe80::3631:c4ff:febc:d01f%bge0"

Quote
but MacOS doesn't. Does MacOS have a domain in its  uname -a  output?

Code: [Select]
[/Users/trev] $ uname -a
Darwin macmini6.sentry.org 18.7.0 Darwin Kernel Version 18.7.0: Sun Dec  1 18:59:03 PST 2019; root:xnu-4903.278.19~1/RELEASE_X86_64 x86_64

MarkMLl

  • Hero Member
  • *****
  • Posts: 6692
Re: GetDomainName() implementation
« Reply #7 on: December 30, 2019, 08:35:38 am »
@trev: Thanks for those. So FreeBSD appears to have a domain which can be extracted from the hosts file

Nope - domain is not in the hosts file. It is set during installation in etc/rc.conf:

But there's /a/ domain in the hosts file, even if not properly configured.

Quote
but MacOS doesn't. Does MacOS have a domain in its  uname -a  output?

Code: [Select]
[/Users/trev] $ uname -a
Darwin macmini6.sentry.org 18.7.0 Darwin Kernel Version 18.7.0: Sun Dec  1 18:59:03 PST 2019; root:xnu-4903.278.19~1/RELEASE_X86_64 x86_64

Thanks for that, an FQDN at the start. Somewhere I think I've seen something about that appearing at the end of uname output, but there's obviously a lot implementation-defined.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

PascalDragon

  • Hero Member
  • *****
  • Posts: 5486
  • Compiler Developer
Re: GetDomainName() implementation
« Reply #8 on: December 30, 2019, 09:26:06 am »
@trev: Thanks for those. So FreeBSD appears to have a domain which can be extracted from the hosts file

Nope - domain is not in the hosts file. It is set during installation in etc/rc.conf:

But there's /a/ domain in the hosts file, even if not properly configured.
But a domain doesn't make it the right domain. So if FreeBSD apparently doesn't retrieve it from there, then you shouldn't retrieve it from there either.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6692
Re: GetDomainName() implementation
« Reply #9 on: December 30, 2019, 09:47:47 am »
But a domain doesn't make it the right domain. So if FreeBSD apparently doesn't retrieve it from there, then you shouldn't retrieve it from there either.

Broadly agreed, but (a) something set up at installation time is probably better than "(none)", and (b) almost anything is better than trying to retrieve what should be a local setting by contacting a DNS server which might not be accessible.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2023
  • Former Delphi 1-7, 10.2 user
Re: GetDomainName() implementation
« Reply #10 on: December 30, 2019, 11:18:08 am »
But a domain doesn't make it the right domain. So if FreeBSD apparently doesn't retrieve it from there, then you shouldn't retrieve it from there either.

Broadly agreed, but (a) something set up at installation time is probably better than "(none)", and (b) almost anything is better than trying to retrieve what should be a local setting by contacting a DNS server which might not be accessible.

Surely better to retrieve it from /etc/rc.conf which is setup at installation? If it's not set, the system uses amnesiac from memory ;)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11458
  • FPC developer.
Re: GetDomainName() implementation
« Reply #11 on: December 30, 2019, 11:35:22 am »
But a domain doesn't make it the right domain. So if FreeBSD apparently doesn't retrieve it from there, then you shouldn't retrieve it from there either.

Broadly agreed, but (a) something set up at installation time is probably better than "(none)", and (b) almost anything is better than trying to retrieve what should be a local setting by contacting a DNS server which might not be accessible.

It should be done by the already mentioned sysctl on the targets that support it, and only use hosts as last resort fallback (or on linux only)

MarkMLl

  • Hero Member
  • *****
  • Posts: 6692
Re: GetDomainName() implementation
« Reply #12 on: December 30, 2019, 11:42:44 am »
It should be done by the already mentioned sysctl on the targets that support it, and only use hosts as last resort fallback (or on linux only)

OK, but the sysctl stuff is not being configured on most distreaux and the domainname manpage suggests using /etc/hosts.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11458
  • FPC developer.
Re: GetDomainName() implementation
« Reply #13 on: December 30, 2019, 11:51:26 am »
It should be done by the already mentioned sysctl on the targets that support it, and only use hosts as last resort fallback (or on linux only)

OK, but the sysctl stuff is not being configured on most distreaux and the domainname manpage suggests using /etc/hosts.

On FreeBSD sysctls are used during startup, so I highly doubt that. Linux is a different matter, much more of such issues are managed by distro specific install tools. For that you only have the desperate (hosts) option. But that doesn't mean integrated OSes should suffer the same fate

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2023
  • Former Delphi 1-7, 10.2 user
Re: GetDomainName() implementation
« Reply #14 on: December 31, 2019, 12:59:58 am »
Hacking at host/rc files and uname command output is rather inelegant. As I originally suggested, this is much more elegant for macOS and FreeBSD (all *BSD derivatives probably):

Code: Pascal  [Select][+][-]
  1. function DomainName : AnsiString;
  2.  
  3. var
  4.   status : Integer;
  5.   len : size_t;
  6.   p   : PChar;
  7.  
  8. begin
  9.   status := fpSysCtlByName('kern.hostname', Nil, @len, Nil, 0);
  10.   if status <> 0 then RaiseLastOSError;
  11.  
  12.   GetMem(p, len);
  13.  
  14.   try
  15.     status := fpSysCtlByName('kern.hostname', p, @len, Nil, 0);
  16.     if status <> 0 then RaiseLastOSError;
  17.     Result := p;
  18.   finally
  19.     FreeMem(p);
  20.   end;
  21. end;      

 

TinyPortal © 2005-2018