]> andersk Git - moira.git/blame - lib/fixhost.c
Command line printer manipulation client, and build goo.
[moira.git] / lib / fixhost.c
CommitLineData
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>
533bacb3 14
15#ifdef HAVE_UNAME
7ac48069 16#include <sys/utsname.h>
533bacb3 17#endif
7ac48069 18
533bacb3 19#ifndef _WIN32
20#include <sys/socket.h>
56a69e4e 21#include <netdb.h>
7ac48069 22#include <netinet/in.h>
533bacb3 23#endif /* _WIN32 */
7ac48069 24
25#include <ctype.h>
56a69e4e 26#include <stdio.h>
a43ce477 27#include <stdlib.h>
8fd777cf 28#include <string.h>
7ac48069 29
30RCSID("$Header$");
56a69e4e 31
533bacb3 32static struct hostent *local_gethostbyname(void)
33{
34#ifdef HAVE_UNAME
35 struct utsname name;
36 uname(&name);
37 return gethostbyname(name.nodename);
38#else
39 char hostname[128];
40 gethostname(hostname, sizeof(hostname));
41 hostname[sizeof(hostname)-1] = 0;
42 return gethostbyname(hostname);
43#endif
44}
45
8ed2ebc0 46static char *local_domain(void)
47{
48 static char *domain = NULL;
49 char *cp;
50 struct hostent *hp;
51
52 if (domain == NULL)
53 {
54 char hostbuf[256];
55
56 if (mr_host(hostbuf, sizeof(hostbuf)) == MR_SUCCESS)
57 {
58 cp = strchr(hostbuf, '.');
59 if (cp)
60 domain = strdup(++cp);
61 }
62 else
63 {
533bacb3 64 hp = local_gethostbyname();
8ed2ebc0 65 if (hp)
66 {
67 cp = strchr(hp->h_name, '.');
68 if (cp)
69 domain = strdup(++cp);
70 }
71 }
72 if (!domain)
73 domain = "";
74 }
75
76 return domain;
77}
78
56a69e4e 79/*
00bfb581 80 * Canonicalize hostname:
81 * if it is in double-quotes, then strip the quotes and return the name.
82 * if it is in the namespace, call the nameserver to expand it
83 * otherwise uppercase it and append the default domain (using an, er,
84 * undocumented global of the nameserver).
56a69e4e 85 *
86 * Assumes that host was allocated using malloc(); it may be freed or
87 * realloc'ed, so the old pointer should not be considered valid.
88 */
89
5eaef520 90char *canonicalize_hostname(char *host)
56a69e4e 91{
44d12d58 92 struct hostent *hp;
a3bbcc2b 93 int len;
a3bbcc2b 94 char *tbuf, *cp;
5eaef520 95
a3bbcc2b 96 len = strlen(host);
97 if (len > 2 && host[0] == '"' && host[len - 1] == '"')
5eaef520 98 {
a3bbcc2b 99 tbuf = malloc(len - 1);
100 if (!tbuf)
101 return NULL;
102 strncpy(tbuf, host + 1, len - 2);
9736f954 103 tbuf[len - 2] = '\0';
5eaef520 104 free(host);
a3bbcc2b 105 return tbuf;
00bfb581 106 }
107
7a12712a 108 if (strchr(host, '*') || strchr(host, '?') || *host == '[')
5eaef520 109 return host;
110
111 hp = gethostbyname(host);
277817a5 112
5eaef520 113 if (hp)
114 {
a3bbcc2b 115 host = realloc(host, strlen(hp->h_name) + 1);
116 if (host)
117 strcpy(host, hp->h_name);
5eaef520 118 return host;
119 }
120 else
121 {
122 /* can't get name from nameserver; fix up the format a bit */
8ed2ebc0 123 cp = strchr(host, '.');
124 if (!cp)
5eaef520 125 {
8ed2ebc0 126 tbuf = malloc(strlen(host) + strlen(local_domain()) + 2);
a3bbcc2b 127 if (!tbuf)
128 return NULL;
8ed2ebc0 129 sprintf(tbuf, "%s.%s", host, local_domain());
5eaef520 130 free(host);
a3bbcc2b 131 host = tbuf;
56a69e4e 132 }
8ed2ebc0 133 else if (strcasecmp(cp + 1, local_domain()) != 0)
134 return host;
135
136 /* This is a host in our local domain, so capitalize it. */
137 for (cp = host; *cp; cp++)
138 *cp = toupper(*cp);
5eaef520 139 return host;
56a69e4e 140 }
141}
This page took 0.780898 seconds and 5 git commands to generate.