]>
Commit | Line | Data |
---|---|---|
fa59b86f | 1 | /* $Id$ |
7ac48069 | 2 | * |
3 | * Canonicalize a hostname | |
56a69e4e | 4 | * |
7ac48069 | 5 | * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology |
6 | * For copying and distribution information, please see the file | |
7 | * <mit-copyright.h>. | |
56a69e4e | 8 | */ |
9 | ||
babbc197 | 10 | #include <mit-copyright.h> |
7ac48069 | 11 | #include <moira.h> |
12 | ||
56a69e4e | 13 | #include <sys/types.h> |
14 | #include <sys/socket.h> | |
7ac48069 | 15 | #include <sys/utsname.h> |
16 | ||
56a69e4e | 17 | #include <netdb.h> |
7ac48069 | 18 | #include <netinet/in.h> |
19 | ||
20 | #include <ctype.h> | |
56a69e4e | 21 | #include <stdio.h> |
a43ce477 | 22 | #include <stdlib.h> |
8fd777cf | 23 | #include <string.h> |
7ac48069 | 24 | |
25 | RCSID("$Header$"); | |
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; |
a3bbcc2b | 41 | int len; |
5eaef520 | 42 | int has_dot = 0; |
a3bbcc2b | 43 | char *tbuf, *cp; |
5eaef520 | 44 | |
a3bbcc2b | 45 | len = strlen(host); |
46 | if (len > 2 && host[0] == '"' && host[len - 1] == '"') | |
5eaef520 | 47 | { |
a3bbcc2b | 48 | tbuf = malloc(len - 1); |
49 | if (!tbuf) | |
50 | return NULL; | |
51 | strncpy(tbuf, host + 1, len - 2); | |
9736f954 | 52 | tbuf[len - 2] = '\0'; |
5eaef520 | 53 | free(host); |
a3bbcc2b | 54 | return tbuf; |
00bfb581 | 55 | } |
56 | ||
95e83191 | 57 | if (strchr(host, '*') || strchr(host, '?') || !strcmp(host, "[NONE]")) |
5eaef520 | 58 | return host; |
59 | ||
60 | hp = gethostbyname(host); | |
277817a5 | 61 | |
5eaef520 | 62 | if (hp) |
63 | { | |
a3bbcc2b | 64 | host = realloc(host, strlen(hp->h_name) + 1); |
65 | if (host) | |
66 | strcpy(host, hp->h_name); | |
5eaef520 | 67 | return host; |
68 | } | |
69 | else | |
70 | { | |
71 | /* can't get name from nameserver; fix up the format a bit */ | |
72 | for (cp = host; *cp; cp++) | |
73 | { | |
a3bbcc2b | 74 | if (islower(*cp)) |
75 | *cp = toupper(*cp); | |
76 | has_dot |= (*cp == '.'); | |
56a69e4e | 77 | } |
5eaef520 | 78 | if (!has_dot) |
79 | { | |
80 | static char *domain = NULL; | |
3dab9162 | 81 | |
5eaef520 | 82 | if (domain == NULL) |
83 | { | |
8b29656d | 84 | char hostbuf[256]; |
a3bbcc2b | 85 | |
8b29656d | 86 | if (mr_host(hostbuf, sizeof(hostbuf)) == MR_SUCCESS) |
a3bbcc2b | 87 | { |
8b29656d | 88 | cp = strchr(hostbuf, '.'); |
a3bbcc2b | 89 | if (cp) |
90 | domain = strdup(++cp); | |
a3bbcc2b | 91 | } |
5eaef520 | 92 | else |
8b29656d | 93 | { |
94 | struct utsname name; | |
95 | uname(&name); | |
96 | hp = gethostbyname(name.nodename); | |
97 | if (hp) | |
98 | { | |
99 | cp = strchr(hp->h_name, '.'); | |
100 | if (cp) | |
101 | domain = strdup(++cp); | |
102 | } | |
103 | } | |
104 | if (!domain) | |
5eaef520 | 105 | domain = ""; |
3dab9162 | 106 | } |
a3bbcc2b | 107 | tbuf = malloc(strlen(host) + strlen(domain) + 2); |
108 | if (!tbuf) | |
109 | return NULL; | |
5eaef520 | 110 | sprintf(tbuf, "%s.%s", host, domain); |
111 | free(host); | |
a3bbcc2b | 112 | host = tbuf; |
56a69e4e | 113 | } |
5eaef520 | 114 | return host; |
56a69e4e | 115 | } |
116 | } |