]>
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 | #include <sys/utsname.h> |
8fd777cf | 23 | #include <string.h> |
56a69e4e | 24 | #include <ctype.h> |
3dab9162 | 25 | #include <moira.h> |
56a69e4e | 26 | |
56a69e4e | 27 | /* |
00bfb581 | 28 | * Canonicalize hostname: |
29 | * if it is in double-quotes, then strip the quotes and return the name. | |
30 | * if it is in the namespace, call the nameserver to expand it | |
31 | * otherwise uppercase it and append the default domain (using an, er, | |
32 | * undocumented global of the nameserver). | |
56a69e4e | 33 | * |
34 | * Assumes that host was allocated using malloc(); it may be freed or | |
35 | * realloc'ed, so the old pointer should not be considered valid. | |
36 | */ | |
37 | ||
5eaef520 | 38 | char *canonicalize_hostname(char *host) |
56a69e4e | 39 | { |
44d12d58 | 40 | struct hostent *hp; |
5eaef520 | 41 | int n_len; |
42 | int has_dot = 0; | |
43 | char tbuf[BUFSIZ]; | |
44 | struct utsname name; | |
44d12d58 | 45 | char *cp; |
5eaef520 | 46 | |
47 | if (strlen(host) > 2 && host[0] == '"' && host[strlen(host) - 1] == '"') | |
48 | { | |
49 | strcpy(tbuf, host + 1); | |
50 | free(host); | |
51 | tbuf[strlen(tbuf) - 1] = '\0'; | |
52 | return strsave(tbuf); | |
00bfb581 | 53 | } |
54 | ||
5eaef520 | 55 | if (strchr(host, '*') || strchr(host, '?') || strchr(host, '[')) |
56 | return host; | |
57 | ||
58 | hp = gethostbyname(host); | |
277817a5 | 59 | |
5eaef520 | 60 | if (hp) |
61 | { | |
62 | n_len = strlen(hp->h_name) + 1; | |
63 | host = realloc(host, n_len); | |
56a69e4e | 64 | |
5eaef520 | 65 | strcpy(host, hp->h_name); |
66 | return host; | |
67 | } | |
68 | else | |
69 | { | |
70 | /* can't get name from nameserver; fix up the format a bit */ | |
71 | for (cp = host; *cp; cp++) | |
72 | { | |
44d12d58 | 73 | int c; |
5eaef520 | 74 | if (islower(c = *cp)) |
75 | *cp = toupper(c); | |
76 | has_dot |= (c == '.'); | |
56a69e4e | 77 | } |
5eaef520 | 78 | if (!has_dot) |
79 | { | |
80 | static char *domain = NULL; | |
3dab9162 | 81 | |
5eaef520 | 82 | if (domain == NULL) |
83 | { | |
84 | uname(&name); | |
85 | hp = gethostbyname(name.nodename); | |
86 | cp = strchr(hp->h_name, '.'); | |
87 | if (cp) | |
88 | domain = strsave(++cp); | |
89 | else | |
90 | domain = ""; | |
3dab9162 | 91 | } |
5eaef520 | 92 | sprintf(tbuf, "%s.%s", host, domain); |
93 | free(host); | |
94 | host = strsave(tbuf); | |
56a69e4e | 95 | } |
5eaef520 | 96 | return host; |
56a69e4e | 97 | } |
98 | } |