]> andersk Git - moira.git/blobdiff - lib/fixhost.c
Command line printer manipulation client, and build goo.
[moira.git] / lib / fixhost.c
index 9a529736462adeee56c43f5941c156bf270d8cf8..ae574ada446fc7fb96a86dc9e39ab7c54b96ffe1 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id $
+/* $Id$
  *
  * Canonicalize a hostname
  *
 #include <moira.h>
 
 #include <sys/types.h>
-#include <sys/socket.h>
+
+#ifdef HAVE_UNAME
 #include <sys/utsname.h>
+#endif
 
+#ifndef _WIN32
+#include <sys/socket.h>
 #include <netdb.h>
 #include <netinet/in.h>
+#endif /* _WIN32 */
 
 #include <ctype.h>
 #include <stdio.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:
  *  if it is in double-quotes, then strip the quotes and return the name.
@@ -38,61 +90,52 @@ RCSID("$Header$");
 char *canonicalize_hostname(char *host)
 {
   struct hostent *hp;
-  int n_len;
-  int has_dot = 0;
-  char tbuf[BUFSIZ];
-  struct utsname name;
-  char *cp;
+  int len;
+  char *tbuf, *cp;
 
-  if (strlen(host) > 2 && host[0] == '"' && host[strlen(host) - 1] == '"')
+  len = strlen(host);
+  if (len > 2 && host[0] == '"' && host[len - 1] == '"')
     {
-      strcpy(tbuf, host + 1);
+      tbuf = malloc(len - 1);
+      if (!tbuf)
+       return NULL;
+      strncpy(tbuf, host + 1, len - 2);
+      tbuf[len - 2] = '\0';
       free(host);
-      tbuf[strlen(tbuf) - 1] = '\0';
-      return strdup(tbuf);
+      return tbuf;
     }
 
-  if (strchr(host, '*') || strchr(host, '?') || strchr(host, '['))
+  if (strchr(host, '*') || strchr(host, '?') || *host == '[')
     return host;
 
   hp = gethostbyname(host);
 
   if (hp)
     {
-      n_len = strlen(hp->h_name) + 1;
-      host = realloc(host, n_len);
-
-      strcpy(host, hp->h_name);
+      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 */
-      for (cp = host; *cp; cp++)
+      cp = strchr(host, '.');
+      if (!cp)
        {
-         int c;
-         if (islower(c = *cp))
-           *cp = toupper(c);
-         has_dot |= (c == '.');
-       }
-      if (!has_dot)
-       {
-         static char *domain = NULL;
-
-         if (domain == NULL)
-           {
-             uname(&name);
-             hp = gethostbyname(name.nodename);
-             cp = strchr(hp->h_name, '.');
-             if (cp)
-               domain = strdup(++cp);
-             else
-               domain = "";
-           }
-         sprintf(tbuf, "%s.%s", host, domain);
+         tbuf = malloc(strlen(host) + strlen(local_domain()) + 2);
+         if (!tbuf)
+           return NULL;
+         sprintf(tbuf, "%s.%s", host, local_domain());
          free(host);
-         host = strdup(tbuf);
+         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;
     }
 }
This page took 0.049524 seconds and 4 git commands to generate.