]>
Commit | Line | Data |
---|---|---|
7ac48069 | 1 | /* $Id $ |
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; |
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'; | |
7ac48069 | 52 | return strdup(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) | |
7ac48069 | 88 | domain = strdup(++cp); |
5eaef520 | 89 | else |
90 | domain = ""; | |
3dab9162 | 91 | } |
5eaef520 | 92 | sprintf(tbuf, "%s.%s", host, domain); |
93 | free(host); | |
7ac48069 | 94 | host = strdup(tbuf); |
56a69e4e | 95 | } |
5eaef520 | 96 | return host; |
56a69e4e | 97 | } |
98 | } |