]> andersk Git - moira.git/blob - lib/fixhost.c
d712d6eb7cb6043fd7fbff03b44f2a92875d7c1e
[moira.git] / lib / fixhost.c
1 /*
2  *      $Source$
3  *      $Author$
4  *      $Header$
5  *
6  *      Copyright (C) 1987 by the Massachusetts Institute of Technology
7  *      For copying and distribution information, please see the file
8  *      <mit-copyright.h>.
9  */
10
11 #ifndef lint
12 static char *rcsid_fixhost_c = "$Header$";
13 #endif
14
15 #include <mit-copyright.h>
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <netdb.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #ifdef POSIX
23 #include <sys/utsname.h>
24 #endif
25 #include <string.h>
26 #include <ctype.h>
27 #include <moira.h>
28
29 /*
30  * Canonicalize hostname:
31  *  if it is in double-quotes, then strip the quotes and return the name.
32  *  if it is in the namespace, call the nameserver to expand it
33  *  otherwise uppercase it and append the default domain (using an, er,
34  *    undocumented global of the nameserver).
35  *
36  * Assumes that host was allocated using malloc(); it may be freed or
37  * realloc'ed, so the old pointer should not be considered valid.
38  */
39
40 char *
41 canonicalize_hostname(host)
42     char *host;
43 {
44     register struct hostent *hp;
45     int n_len;
46     int has_dot = 0;
47     char tbuf[BUFSIZ];
48 #ifdef POSIX
49     struct utsname name;
50 #endif
51     register char *cp;
52     
53     if (strlen(host) > 2 && host[0] == '"' && host[strlen(host)-1] == '"') {
54         strcpy(tbuf, host+1);
55         free(host);
56         tbuf[strlen(tbuf)-1] = 0;
57         return(strsave(tbuf));
58     }
59
60     if (strchr(host, '*') || strchr(host, '?') || strchr(host, '['))
61       return(host);
62
63     hp = gethostbyname(host);
64
65     if (hp) {
66         n_len = strlen(hp->h_name) + 1;
67         host = realloc(host, (unsigned)n_len);
68         
69         (void) strcpy(host, hp->h_name);
70         return host;
71     } else {
72         /* can't get name from nameserver; fix up the format a bit */
73         for (cp = host; *cp; cp++) {
74             register int c;     /* pcc doesn't like register char */
75             if (islower(c = *cp)) *cp = toupper(c);
76             has_dot |= (c == '.');
77         }
78         if (!has_dot) {
79             static char *domain = NULL;
80
81             if (domain == NULL) {
82 #ifdef POSIX
83                 (void) uname(&name);
84                 strncpy(tbuf, name.nodename, sizeof(tbuf));
85 #else
86                 gethostname(tbuf, sizeof(tbuf));
87 #endif
88                 hp = gethostbyname(tbuf);
89                 cp = strchr(hp->h_name, '.');
90                 if (cp)
91                   domain = strsave(++cp);
92                 else
93                   domain = "";
94             }
95             (void) sprintf(tbuf, "%s.%s", host, domain);
96             free(host);
97             host = strsave(tbuf);
98         }
99         return host;
100     }
101 }
This page took 0.032923 seconds and 3 git commands to generate.