GetDomainName() implementation lazarus / 62-General ---------------------------------------------------------------------- MarkMLl · 2019-12-29 11:30 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 ---------------------------------------------------------------------- trev · 2019-12-29 13:40 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 · 2019-12-29 13:47 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 · 2019-12-29 14:18 @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 ---------------------------------------------------------------------- marcov · 2019-12-29 15:28 Quote from: MarkMLl 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? 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 · 2019-12-29 15:45 @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 ---------------------------------------------------------------------- trev · 2019-12-30 00:50 Quote from: MarkMLl 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 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 · 2019-12-30 07:35 Quote from: trev on December 30, 2019, 01:50:11 amQuote from: MarkMLl 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 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 from: trev on December 30, 2019, 01:50:11 amQuote 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 ---------------------------------------------------------------------- PascalDragon · 2019-12-30 08:26 Quote from: MarkMLl on December 30, 2019, 08:35:38 amQuote from: trev on December 30, 2019, 01:50:11 amQuote from: MarkMLl 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 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 · 2019-12-30 08:47 Quote from: PascalDragon on December 30, 2019, 09:26:06 amBut 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 ---------------------------------------------------------------------- trev · 2019-12-30 10:18 Quote from: MarkMLl on December 30, 2019, 09:47:47 amQuote from: PascalDragon on December 30, 2019, 09:26:06 amBut 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 · 2019-12-30 10:35 Quote from: MarkMLl on December 30, 2019, 09:47:47 amQuote from: PascalDragon on December 30, 2019, 09:26:06 amBut 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 · 2019-12-30 10:42 Quote from: marcov on December 30, 2019, 11:35:22 amIt 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 ---------------------------------------------------------------------- marcov · 2019-12-30 10:51 Quote from: MarkMLl on December 30, 2019, 11:42:44 amQuote from: marcov on December 30, 2019, 11:35:22 amIt 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 · 2019-12-30 23:59 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function DomainName : AnsiString; var  status : Integer;  len : size_t;  p   : PChar; begin  status := fpSysCtlByName('kern.hostname', Nil, @len, Nil, 0);  if status <> 0 then RaiseLastOSError;   GetMem(p, len);   try    status := fpSysCtlByName('kern.hostname', p, @len, Nil, 0);    if status <> 0 then RaiseLastOSError;    Result := p;  finally    FreeMem(p);  end;end; ---------------------------------------------------------------------- winni · 2019-12-31 00:53 Hi! I don't know much about FreeBSD but in Linux you should take care of the following: Don't rely on the contents of files like /etc/host or /etc/hostname. There are a lot of possibilities that they are changed while booting or working. If you are alone with your computer and not member of the University of XYZ or whatever  your DomainName will be empty. For internal and connecting reasons the local domain is taken from/etc/hostname which is in my case linux-vqeu.fritz.box - I am only connected to my FritzBox who does the connection to the rest of the world. The entry in /etc/hosts 127.0.0.1 localhost must  be present - otherwise your machine wont work. A lot of jobs in Unix/Linux are connected internal via net to localhost. And to simplyfy the needs: there are the two linux commands hostname and domainname which could be read. Winni ---------------------------------------------------------------------- MarkMLl · 2019-12-31 08:28 > domainname ...as I've already said, is documented as using NIS/YP and as such is useless. MarkMLl