chiark / gitweb /
Actually WAITING is correct because duct will never delete both F and D
[innduct.git] / lib / getfqdn.c
1 /*  $Id: getfqdn.c 6155 2003-01-19 19:58:25Z rra $
2 **
3 */
4
5 #include "config.h"
6 #include "clibrary.h"
7 #include <netdb.h>
8
9 #include "libinn.h"
10 #include "paths.h"
11
12
13 /*
14 **  Get the fully-qualified domain name for this host.
15 */
16 char *GetFQDN(char *domain)
17 {
18     static char         buff[SMBUF];
19     struct hostent      *hp;
20     char                *p;
21     char                **ap;
22 #if     0
23     /* See comments below. */
24     char                temp[SMBUF + 2];
25 #endif  /* 0 */
26
27     /* Return any old results. */
28     if (buff[0])
29         return buff;
30
31     /* Try gethostname. */
32     if (gethostname(buff, (int)sizeof buff) < 0)
33         return NULL;
34     if (strchr(buff, '.') != NULL)
35         return buff;
36
37     /* See if DNS (or /etc/hosts) gives us a full domain name. */
38     if ((hp = gethostbyname(buff)) == NULL)
39         return NULL;
40 #if     0
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);
51     }
52 #endif  /* 0 */
53
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));
58             return buff;
59         }
60         /* Doesn't fit; make sure we don't return bad data next time. */
61         buff[0] = '\0';
62         return hp->h_name;
63     }
64
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));
72                     return buff;
73                 }
74                 buff[0] = '\0';
75                 return p ;
76             }
77
78     /* Give up:  Get the domain config param and append it. */
79     if ((p = domain) == NULL || *p == '\0')
80         return NULL;
81     if (strlen(buff) + 1 + strlen(p) > sizeof buff - 1)
82         /* Doesn't fit. */
83         return NULL;
84     strlcat(buff, ".", sizeof(buff));
85     strlcat(buff, p, sizeof(buff));
86     return buff;
87 }