-/*
- * $Source$
- * $Author$
- * $Header$
+/* $Id$
+ *
+ * Canonicalize a hostname
*
- * Copyright (C) 1987 by the Massachusetts Institute of Technology
- * For copying and distribution information, please see the file
- * <mit-copyright.h>.
+ * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology
+ * For copying and distribution information, please see the file
+ * <mit-copyright.h>.
*/
-#ifndef lint
-static char *rcsid_fixhost_c = "$Header$";
-#endif
-
#include <mit-copyright.h>
+#include <moira.h>
+
#include <sys/types.h>
+
+#ifdef HAVE_UNAME
+#include <sys/utsname.h>
+#endif
+
+#ifndef _WIN32
#include <sys/socket.h>
-#include <netinet/in.h>
#include <netdb.h>
+#include <netinet/in.h>
+#endif /* _WIN32 */
+
+#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
-#ifdef POSIX
-#include <sys/utsname.h>
-#endif
#include <string.h>
-#include <ctype.h>
-#include <moira.h>
+
+RCSID("$Header$");
+
+static struct hostent *local_gethostbyname(void)
+{
+#ifdef HAVE_UNAME
+ struct utsname name;
+ uname(&name);
+ return gethostbyname(name.nodename);
+#else
+ char hostname[128];
+ gethostname(hostname, sizeof(hostname));
+ hostname[sizeof(hostname)-1] = 0;
+ return gethostbyname(hostname);
+#endif
+}
+
+static char *local_domain(void)
+{
+ static char *domain = NULL;
+ char *cp;
+ struct hostent *hp;
+
+ if (domain == NULL)
+ {
+ char hostbuf[256];
+
+ if (mr_host(hostbuf, sizeof(hostbuf)) == MR_SUCCESS)
+ {
+ cp = strchr(hostbuf, '.');
+ if (cp)
+ domain = strdup(++cp);
+ }
+ else
+ {
+ hp = local_gethostbyname();
+ if (hp)
+ {
+ cp = strchr(hp->h_name, '.');
+ if (cp)
+ domain = strdup(++cp);
+ }
+ }
+ if (!domain)
+ domain = "";
+ }
+
+ return domain;
+}
/*
* Canonicalize hostname:
* realloc'ed, so the old pointer should not be considered valid.
*/
-char *
-canonicalize_hostname(host)
- char *host;
+char *canonicalize_hostname(char *host)
{
- register struct hostent *hp;
- int n_len;
- int has_dot = 0;
- char tbuf[BUFSIZ];
-#ifdef POSIX
- struct utsname name;
-#endif
- register char *cp;
-
- if (strlen(host) > 2 && host[0] == '"' && host[strlen(host)-1] == '"') {
- strcpy(tbuf, host+1);
- free(host);
- tbuf[strlen(tbuf)-1] = 0;
- return(strsave(tbuf));
- }
+ struct hostent *hp;
+ int len;
+ char *tbuf, *cp;
- if (strchr(host, '*') || strchr(host, '?') || strchr(host, '['))
- return(host);
+ len = strlen(host);
+ if (len > 2 && host[0] == '"' && host[len - 1] == '"')
+ {
+ tbuf = malloc(len - 1);
+ if (!tbuf)
+ return NULL;
+ strncpy(tbuf, host + 1, len - 2);
+ tbuf[len - 2] = '\0';
+ free(host);
+ return tbuf;
+ }
- hp = gethostbyname(host);
+ if (strchr(host, '*') || strchr(host, '?') || *host == '[')
+ return host;
- if (hp) {
- n_len = strlen(hp->h_name) + 1;
- host = realloc(host, (unsigned)n_len);
-
- (void) strcpy(host, hp->h_name);
- return host;
- } else {
- /* can't get name from nameserver; fix up the format a bit */
- for (cp = host; *cp; cp++) {
- register int c; /* pcc doesn't like register char */
- if (islower(c = *cp)) *cp = toupper(c);
- has_dot |= (c == '.');
- }
- if (!has_dot) {
- static char *domain = NULL;
+ hp = gethostbyname(host);
- if (domain == NULL) {
-#ifdef POSIX
- (void) uname(&name);
- strncpy(tbuf, name.nodename, sizeof(tbuf));
-#else
- gethostname(tbuf, sizeof(tbuf));
-#endif
- hp = gethostbyname(tbuf);
- cp = strchr(hp->h_name, '.');
- if (cp)
- domain = strsave(++cp);
- else
- domain = "";
- }
- (void) sprintf(tbuf, "%s.%s", host, domain);
- free(host);
- host = strsave(tbuf);
+ if (hp)
+ {
+ host = realloc(host, strlen(hp->h_name) + 1);
+ if (host)
+ strcpy(host, hp->h_name);
+ return host;
+ }
+ else
+ {
+ /* can't get name from nameserver; fix up the format a bit */
+ cp = strchr(host, '.');
+ if (!cp)
+ {
+ tbuf = malloc(strlen(host) + strlen(local_domain()) + 2);
+ if (!tbuf)
+ return NULL;
+ sprintf(tbuf, "%s.%s", host, local_domain());
+ free(host);
+ host = tbuf;
}
+ else if (strcasecmp(cp + 1, local_domain()) != 0)
return host;
+
+ /* This is a host in our local domain, so capitalize it. */
+ for (cp = host; *cp; cp++)
+ *cp = toupper(*cp);
+ return host;
}
}