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