Getting the domain name - Unix

This is a discussion on Getting the domain name - Unix ; Can anyone recommend a portable way of getting the domain name of the local machine? I find it amazing that there's no builtin method for doing this (gethostname does not necessarily contain the domain). Mike...

+ Reply to Thread
Results 1 to 12 of 12

Thread: Getting the domain name

  1. Getting the domain name

    Can anyone recommend a portable way of getting the domain name of the
    local machine? I find it amazing that there's no builtin method for
    doing this (gethostname does not necessarily contain the domain).

    Mike


  2. Re: Getting the domain name

    On Fri, 23 Mar 2007 14:06:20 -0400, Michael B Allen wrote:

    > Can anyone recommend a portable way of getting the domain name of the
    > local machine? I find it amazing that there's no builtin method for
    > doing this (gethostname does not necessarily contain the domain).


    What you might want[1] is gethostname() -> getnameinfo(), and then
    chop the first node off. But that assumes the above will give you a
    domain name (and/or that you have one), which isn't guaranteed.

    [1] This is what dnsdomainname in net-tools does, for instance.

    --
    James Antill -- james@and.org
    http://www.and.org/and-httpd/ -- $2,000 security guarantee
    http://www.and.org/vstr/

  3. Re: Getting the domain name

    On Fri, 23 Mar 2007 15:49:35 -0500, James Antill wrote:

    > On Fri, 23 Mar 2007 14:06:20 -0400, Michael B Allen wrote:
    >
    >> Can anyone recommend a portable way of getting the domain name of the
    >> local machine? I find it amazing that there's no builtin method for
    >> doing this (gethostname does not necessarily contain the domain).

    >
    > What you might want[1] is gethostname() -> getnameinfo(), and then
    > chop the first node off. But that assumes the above will give you a
    > domain name (and/or that you have one), which isn't guaranteed.


    Hi James,

    Do you mean getaddrinfo and look at ai_canonname? The getnameinfo function
    takes an address.

    And at any rate if gethostname doesn't provide a domain name that's not
    going to work anyway. Thus this post.

    > [1] This is what dnsdomainname in net-tools does, for instance.


    Actually on the system in question even hostname -f doesn't quite give me
    what I want. It produces 'name.domain.com.' with a '.' at the end. Clearly
    it's just reading it from /etc/hosts which has that '.'.

    I want something that favors doing a PTR lookup in DNS on the IP of the
    default interface.

    Actually what I want is something that tries whatever it has to until
    it comes up with a name that has more than one label and ends on .com,
    ..net, .local etc.

    This is one of those things that you'd think someone would have
    solved long ago. Lot's of software use domain names as a namespace
    (e.g. Kerberos).

    Mike


  4. Re: Getting the domain name

    Michael B Allen wrote:
    > Can anyone recommend a portable way of getting the domain name of the
    > local machine? I find it amazing that there's no builtin method for
    > doing this (gethostname does not necessarily contain the domain).


    I'm not that amazed, since there is no particular reason why a Unix
    system should have to have a domain name.

    For one thing, Unix has been around longer than DNS. For another,
    a Unix machine can easily be reachable through more than one domain.
    For example, a single Unix machine might be both mail.example.com
    and mail.corp.example.com. Or ns1.texas.example.com and
    ns2.california.example.com.

    In situations like that, it's simpler not to have any notion of
    "the domainname" -- it would only cause confusion.

    However, one solution which is often (but not always) the right answer
    is to look for "search" or "domain" in /etc/resolv.conf. However,
    that is not correct in the case where DNS isn't the naming service
    being used, and even then I believe "search" is now preferred over
    "domain" precisely because the former only implies a search path
    rather than some kind of membership in a domain.

    - Logan

  5. Re: Getting the domain name

    On Fri, 23 Mar 2007 20:09:01 -0500, Logan Shaw wrote:

    > Michael B Allen wrote:
    >> Can anyone recommend a portable way of getting the domain name of the
    >> local machine? I find it amazing that there's no builtin method for
    >> doing this (gethostname does not necessarily contain the domain).

    >
    > I'm not that amazed, since there is no particular reason why a Unix
    > system should have to have a domain name.
    >
    > For one thing, Unix has been around longer than DNS. For another,
    > a Unix machine can easily be reachable through more than one domain.
    > For example, a single Unix machine might be both mail.example.com
    > and mail.corp.example.com. Or ns1.texas.example.com and
    > ns2.california.example.com.
    >
    > In situations like that, it's simpler not to have any notion of
    > "the domainname" -- it would only cause confusion.


    I fail to see why the fact that machine does not have to have a domain
    name or that Unix predates DNS or that a machine may be multi-homed
    is sufficient reason not to provide programmers with a consistent and
    portable way to get a string that represents a default namespace.

    Obviously getting the "domain name" implies that the function would be
    a DNS specific function. And it would not have to use complicated logic
    about which interface it queried to do the PTR lookup (if it needed to
    do that). It would only need to be consistent such that all programs
    that call the function get the same value.

    But you have showed me why this problem has persisted. This and the very
    similar problem if getting a non-loopback IP address.

    > However, one solution which is often (but not always) the right answer
    > is to look for "search" or "domain" in /etc/resolv.conf. However,
    > that is not correct in the case where DNS isn't the naming service
    > being used, and even then I believe "search" is now preferred over
    > "domain" precisely because the former only implies a search path
    > rather than some kind of membership in a domain.


    Actually James made me realize that emulating net-tools is a good way
    to go because no-doubt some scripts are calling them.

    For the benefit of the next poor bloke I've inlined something that
    emulates net-tools (dnsdomainname) with one slight modification - if the
    name has a trailing '.' it is stripped. Feel free to point out it's flaws.

    Thanks,
    Mike

    --8<--

    #include
    #include
    #include
    #include

    #include
    #include

    int
    dns_get_default_domain(unsigned char *dst, size_t dn)
    {
    unsigned char buf[HOST_NAME_MAX];
    unsigned char *name = buf, *dot, *dlim = dst + dn;

    if (gethostname(buf, sizeof(buf)) == -1) {
    return -1;
    }

    dot = strchr(name, '.');
    if (dot == NULL || dot[1] == '\0') {
    struct hostent *he;

    he = gethostbyname(buf);
    if (he == NULL) {
    return -1;
    }
    name = he->h_name;

    dot = strchr(name, '.');
    if (dot == NULL || dot[1] == '\0') {
    int ai;

    name = NULL;
    for (ai = 0; he->h_aliases[ai]; ai++) {
    name = he->h_aliases[ai];

    dot = strchr(name, '.');
    if (dot && dot[1] != '\0') {
    break;
    }
    name = NULL;
    }
    }
    }

    if (name == NULL) {
    errno = ENOENT;
    return -1;
    }

    /* Copy everything after first '.' to dst (but
    * not a trailing '.' if present)
    */
    dot = NULL;
    while (*name) {
    if (dot) {
    if (dst == dlim)
    break;
    *dst++ = *name;
    }
    if (*name == '.')
    dot = dst - 1;
    name++;
    }
    if (name[-1] == '.')
    dst = dot;

    if (dst >= dlim) {
    errno = ENOBUFS;
    return -1;
    }

    *dst = '\0';

    return 0;
    }
    int
    main(void)
    {
    unsigned char buf[255];

    if (dns_get_default_domain(buf, sizeof(buf)) == -1) {
    perror("dns_get_default_domain");
    return EXIT_FAILURE;
    }
    printf("%s\n", buf);

    return EXIT_SUCCESS;
    }



  6. Re: Getting the domain name

    At about the time of 3/23/2007 11:06 AM, Michael B Allen stated the
    following:
    > Can anyone recommend a portable way of getting the domain name of the
    > local machine? I find it amazing that there's no builtin method for
    > doing this (gethostname does not necessarily contain the domain).
    >
    > Mike
    >


    I had the exact same problem a few years ago. I wrote a tool to do just
    that. I'm running FreeBSD, so I just do this:

    DOMAINNAME="`hostname | domain`"

    That's using FreeBSD's version of Borne Shell.

    Since yours is adding a period on the end, you will have to modify the
    source to strip the period out. I have the source code posted on source
    forge.

    http://sourceforge.net/projects/ipdomaintools/

    Hope this helps.



    --
    Daniel Rudy

    Email address has been base64 encoded to reduce spam
    Decode email address using b64decode or uudecode -m

    Why geeks like computers: look chat date touch grep make unzip
    strip view finger mount fcsk more fcsk yes spray umount sleep

  7. Re: Getting the domain name

    >I want something that favors doing a PTR lookup in DNS on the IP of the
    >default interface.


    Many hosted webservers have over 100 domain names on the same IP
    of the same interface owned by over 100 different people/companies.
    And over 100 PTR records to go with it.

    How does one press the default key on the default keyboard with the
    default finger of the default person at the default time?

  8. Re: Getting the domain name

    On Sat, 24 Mar 2007 09:09:22 +0000, Gordon Burditt wrote:

    >>I want something that favors doing a PTR lookup in DNS on the IP of the
    >>default interface.

    >
    > Many hosted webservers have over 100 domain names on the same IP
    > of the same interface owned by over 100 different people/companies.
    > And over 100 PTR records to go with it.
    >
    > How does one press the default key on the default keyboard with the
    > default finger of the default person at the default time?


    You just do it and get one of those 100 domains. Doesn't matter which one,
    just as long as everyone using the function gets the same answer.

    There's a quote:

    "If you don't know what you're doing, do it neatly". - Unknown

    Mike


  9. Re: Getting the domain name

    >>>I want something that favors doing a PTR lookup in DNS on the IP of the
    >>>default interface.

    >>
    >> Many hosted webservers have over 100 domain names on the same IP
    >> of the same interface owned by over 100 different people/companies.
    >> And over 100 PTR records to go with it.
    >>
    >> How does one press the default key on the default keyboard with the
    >> default finger of the default person at the default time?

    >
    >You just do it and get one of those 100 domains. Doesn't matter which one,
    >just as long as everyone using the function gets the same answer.


    Due to the use of round-robin DNS, it is likely those over 100 PTR
    records WON'T come back in the same order every time. Plus, customers
    (and their domains) arrive and leave frequently on such a host.


  10. Re: Getting the domain name

    On Sat, 24 Mar 2007 17:36:07 +0000, Gordon Burditt wrote:

    >>>>I want something that favors doing a PTR lookup in DNS on the IP of the
    >>>>default interface.
    >>>
    >>> Many hosted webservers have over 100 domain names on the same IP
    >>> of the same interface owned by over 100 different people/companies.
    >>> And over 100 PTR records to go with it.
    >>>
    >>> How does one press the default key on the default keyboard with the
    >>> default finger of the default person at the default time?

    >>
    >>You just do it and get one of those 100 domains. Doesn't matter which one,
    >>just as long as everyone using the function gets the same answer.

    >
    > Due to the use of round-robin DNS, it is likely those over 100 PTR
    > records WON'T come back in the same order every time. Plus, customers
    > (and their domains) arrive and leave frequently on such a host.


    Then that guy can fix it so that gethostname returns an FQDN and the no
    PTR lookup will ever occur (the code I posted doesn't use DNS even when
    calling gethostbyname near as I can tell).

    Your whole argument perplexes me. Why would you care if there was some
    function that tried a *little* harder to come up with an FQDN? It doesn't
    hurt anyone. There's no downside.

    Mike


  11. Re: Getting the domain name

    >Then that guy can fix it so that gethostname returns an FQDN and the no
    >PTR lookup will ever occur (the code I posted doesn't use DNS even when
    >calling gethostbyname near as I can tell).
    >
    >Your whole argument perplexes me. Why would you care if there was some
    >function that tried a *little* harder to come up with an FQDN? It doesn't
    >hurt anyone. There's no downside.


    I believe that anything that perpetuates the myth of "the domain
    name for a machine" is in and of itself harmful. When the question
    is problematical itself, making an effort to come up with a misleading
    answer is not helpful.

    Now, if you just want it to come up with *A* FQDN, how about:

    return "a.root-servers.net";


  12. Re: Getting the domain name

    On Sun, 25 Mar 2007 01:33:03 +0000, Gordon Burditt wrote:

    >>Then that guy can fix it so that gethostname returns an FQDN and the no
    >>PTR lookup will ever occur (the code I posted doesn't use DNS even when
    >>calling gethostbyname near as I can tell).
    >>
    >>Your whole argument perplexes me. Why would you care if there was some
    >>function that tried a *little* harder to come up with an FQDN? It doesn't
    >>hurt anyone. There's no downside.

    >
    > I believe that anything that perpetuates the myth of "the domain
    > name for a machine" is in and of itself harmful. When the question
    > is problematical itself, making an effort to come up with a misleading
    > answer is not helpful.
    >
    > Now, if you just want it to come up with *A* FQDN, how about:
    >
    > return "a.root-servers.net";


    Yeah. Good idea. Seriously. If it tried gethostname(2) and then
    gethostbyname(3) and then a PTR lookup and *still* couldn't come up
    with an FQDN then some kind of default value like that would be quite
    reasonable (although I don't know if an Internet specific domain would
    be the best choice).

    Mike


+ Reply to Thread