1 /* $Id: getfqdn.c 6155 2003-01-19 19:58:25Z rra $
14 ** Get the fully-qualified domain name for this host.
16 char *GetFQDN(char *domain)
18 static char buff[SMBUF];
23 /* See comments below. */
27 /* Return any old results. */
31 /* Try gethostname. */
32 if (gethostname(buff, (int)sizeof buff) < 0)
34 if (strchr(buff, '.') != NULL)
37 /* See if DNS (or /etc/hosts) gives us a full domain name. */
38 if ((hp = gethostbyname(buff)) == NULL)
41 /* This code is a "feature" that allows multiple domains (NIS or
42 * DNS, I'm not sure) to work with a single INN server. However,
43 * it turns out to cause more problems for people, and they have to
44 * use hacks like __switch_gethostbyname, etc. So if you need this,
45 * turn it on, but don't complain to me. */
46 if (strchr(hp->h_name, '.') == NULL) {
47 /* Try to force DNS lookup if NIS/whatever gets in the way. */
48 strlcpy(temp, buff, sizeof(temp));
49 strlcat(temp, ".", sizeof(temp));
50 hp = gethostbyname(temp);
54 /* First, see if the main name is a FQDN. It should be. */
55 if (hp != NULL && strchr(hp->h_name, '.') != NULL) {
56 if (strlen(hp->h_name) < sizeof buff - 1) {
57 strlcpy(buff, hp->h_name, sizeof(buff));
60 /* Doesn't fit; make sure we don't return bad data next time. */
65 /* Second, see if any aliases are. */
66 if ((ap = hp->h_aliases) != NULL)
67 while ((p = *ap++) != NULL)
68 if (strchr(p, '.') != NULL) {
69 /* Deja-vous all over again. */
70 if (strlen(p) < sizeof buff - 1) {
71 strlcpy(buff, p, sizeof(buff));
78 /* Give up: Get the domain config param and append it. */
79 if ((p = domain) == NULL || *p == '\0')
81 if (strlen(buff) + 1 + strlen(p) > sizeof buff - 1)
84 strlcat(buff, ".", sizeof(buff));
85 strlcat(buff, p, sizeof(buff));