]>
Commit | Line | Data |
---|---|---|
56a69e4e | 1 | /* |
2 | * $Source$ | |
3 | * $Author$ | |
4 | * $Header$ | |
5 | * | |
6 | * Copyright (C) 1987 by the Massachusetts Institute of Technology | |
babbc197 | 7 | * For copying and distribution information, please see the file |
8 | * <mit-copyright.h>. | |
56a69e4e | 9 | */ |
10 | ||
11 | #ifndef lint | |
12 | static char *rcsid_fixhost_c = "$Header$"; | |
a43ce477 | 13 | #endif |
56a69e4e | 14 | |
babbc197 | 15 | #include <mit-copyright.h> |
56a69e4e | 16 | #include <sys/types.h> |
17 | #include <sys/socket.h> | |
18 | #include <netinet/in.h> | |
56a69e4e | 19 | #include <netdb.h> |
20 | #include <stdio.h> | |
a43ce477 | 21 | #include <stdlib.h> |
8fd777cf | 22 | #ifdef POSIX |
23 | #include <sys/utsname.h> | |
24 | #endif | |
25 | #include <string.h> | |
56a69e4e | 26 | #include <ctype.h> |
3dab9162 | 27 | #include <moira.h> |
56a69e4e | 28 | |
56a69e4e | 29 | /* |
00bfb581 | 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). | |
56a69e4e | 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]; | |
8fd777cf | 48 | #ifdef POSIX |
49 | struct utsname name; | |
50 | #endif | |
56a69e4e | 51 | register char *cp; |
52 | ||
00bfb581 | 53 | if (strlen(host) > 2 && host[0] == '"' && host[strlen(host)-1] == '"') { |
7fb8be7f | 54 | strcpy(tbuf, host+1); |
00bfb581 | 55 | free(host); |
56 | tbuf[strlen(tbuf)-1] = 0; | |
57 | return(strsave(tbuf)); | |
58 | } | |
59 | ||
8fd777cf | 60 | if (strchr(host, '*') || strchr(host, '?') || strchr(host, '[')) |
277817a5 | 61 | return(host); |
62 | ||
56a69e4e | 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) { | |
3dab9162 | 79 | static char *domain = NULL; |
80 | ||
81 | if (domain == NULL) { | |
8fd777cf | 82 | #ifdef POSIX |
83 | (void) uname(&name); | |
84 | strncpy(tbuf, name.nodename, sizeof(tbuf)); | |
85 | #else | |
3dab9162 | 86 | gethostname(tbuf, sizeof(tbuf)); |
8fd777cf | 87 | #endif |
3dab9162 | 88 | hp = gethostbyname(tbuf); |
8fd777cf | 89 | cp = strchr(hp->h_name, '.'); |
3dab9162 | 90 | if (cp) |
91 | domain = strsave(++cp); | |
92 | else | |
93 | domain = ""; | |
94 | } | |
95 | (void) sprintf(tbuf, "%s.%s", host, domain); | |
56a69e4e | 96 | free(host); |
97 | host = strsave(tbuf); | |
98 | } | |
99 | return host; | |
100 | } | |
101 | } |