Fixed typo in mh-alias man page.
[mmh] / docs / historical / mh-6.8.5 / mts / sendmail / hosts.c
1 /* hosts.c - find out the official name of a host */
2 #ifndef lint
3 static char ident[] = "@(#)$Id: hosts.c,v 1.7 1992/12/15 00:20:22 jromine Exp $";
4 #endif  /* lint */
5
6 /* LINTLIBRARY */
7
8 /* In the SendMail world, we really don't know what the valid hosts are.
9    We could poke around in the sendmail.cf file, but that still isn't a
10    guarantee.  As a result, we'll say that everything is a valid host, and
11    let SendMail worry about it. */
12
13
14 #include "../h/strings.h"
15 #include <stdio.h>
16 #include "../zotnet/mts.h"
17 #include <ctype.h>
18 #if     defined(BSD42) || defined(SOCKETS)
19 #include <netdb.h>
20 #endif  /* BSD42 or SOCKETS */
21
22
23 #define NOTOK   (-1)
24
25
26 static struct host {
27     char   *h_name;
28     char  **h_aliases;
29     struct host   *h_next;
30 }                   hosts;
31
32 char   *getcpy ();
33
34 static int      init_hs();
35
36 /* \f */
37
38 char   *OfficialName (name)
39 register char   *name;
40 {
41     register char  *p;
42     char   *q,
43             site[BUFSIZ];
44 #if     defined(BSD42) || defined(SOCKETS)
45     register struct hostent *hp;
46 #endif  /* BSD42 or SOCKETS */
47     static char buffer[BUFSIZ];
48     register char **r;
49     register struct host   *h;
50
51     for (p = name, q = site; *p; p++, q++)
52         *q = isupper (*p) ? tolower (*p) : *p;
53     *q = 0;
54     q = site;
55
56     if (uleq (LocalName (), site))
57         return LocalName ();
58
59 #ifdef  BSD41A
60     if (rhost (&q) != NOTOK) {
61         (void) strcpy (buffer, q);
62         free (q);
63         return buffer;
64     }
65 #endif  /* BSD41A */
66 #if     defined(BSD42) || defined(SOCKETS)
67 #ifndef BIND
68     sethostent (1);
69 #endif
70     if (hp = gethostbyname (q)) {
71         (void) strcpy (buffer, hp -> h_name);
72         return buffer;
73     }
74 #endif  /* BSD42 or SOCKETS */
75
76     if (hosts.h_name || init_hs ())
77         for (h = hosts.h_next; h; h = h -> h_next)
78             if (uleq (h -> h_name, q))
79                 return h -> h_name;
80             else
81                 for (r = h -> h_aliases; *r; r++)
82                     if (uleq (*r, q))
83                         return h -> h_name;
84
85     (void) strcpy (buffer, site);
86     return buffer;
87 }
88
89 /* \f */
90
91 /* Use hostable as an exception file for those hosts that aren't on the
92    Internet (listed in /etc/hosts).  These are usually PhoneNet and UUCP
93    sites. */
94
95
96 #define NALIASES        50
97
98 static int  init_hs () {
99     register char  *cp,
100                    *dp,
101                   **q,
102                   **r;
103     char    buffer[BUFSIZ],
104            *aliases[NALIASES];
105     register struct host   *h;
106     register FILE  *fp;
107
108     if ((fp = fopen (hostable, "r")) == NULL)
109         return 0;
110
111     h = &hosts;
112     while (fgets (buffer, sizeof buffer, fp) != NULL) {
113         if (cp = index (buffer, '#'))
114             *cp = 0;
115         if (cp = index (buffer, '\n'))
116             *cp = 0;
117         for (cp = buffer; *cp; cp++)
118             if (isspace (*cp))
119                 *cp = ' ';
120         for (cp = buffer; isspace (*cp); cp++)
121             continue;
122         if (*cp == 0)
123             continue;
124
125         q = aliases;
126         if (cp = index (dp = cp, ' ')) {
127             *cp = 0;
128             for (cp++; *cp; cp++) {
129                 while (isspace (*cp))
130                     cp++;
131                 if (*cp == 0)
132                     break;
133                 if (cp = index (*q++ = cp, ' '))
134                     *cp = 0;
135                 else
136                     break;
137                 if (q >= aliases + NALIASES)
138                     break;
139             }
140         }
141
142         *q = 0;
143
144         h -> h_next = (struct host *) calloc (1, sizeof *h);
145         h = h -> h_next;
146         h -> h_name = getcpy (dp);
147         r = h -> h_aliases =
148                 (char **) calloc ((unsigned) (q - aliases + 1), sizeof *q);
149         for (q = aliases; *q; q++)
150             *r++ = getcpy (*q);
151         *r = 0;
152     }
153
154     (void) fclose (fp);
155     return 1;
156 }