9 /* #include <get_hostname.h>
11 /* const char *get_hostname()
13 /* get_hostname() returns the local hostname as obtained
14 /* via gethostname() or its moral equivalent. This routine
15 /* goes to great length to avoid dependencies on any network
18 /* Fatal errors: no hostname, invalid hostname.
24 /* The Secure Mailer license must be distributed with this software.
27 /* IBM T.J. Watson Research
29 /* Yorktown Heights, NY 10598, USA
35 #include <sys/param.h>
39 #if (MAXHOSTNAMELEN < 256)
41 #define MAXHOSTNAMELEN 256
44 /* Utility library. */
48 #include "valid_hostname.h"
49 #include "get_hostname.h"
53 static char *my_host_name
;
55 /* get_hostname - look up my host name */
57 const char *get_hostname(void)
59 char namebuf
[MAXHOSTNAMELEN
+ 1];
62 * The gethostname() call is not (or not yet) in ANSI or POSIX, but it is
63 * part of the socket interface library. We avoid the more politically-
64 * correct uname() routine because that has no portable way of dealing
65 * with long (FQDN) hostnames.
67 * DO NOT CALL GETHOSTBYNAME FROM THIS FUNCTION. IT BREAKS MAILDIR DELIVERY
68 * AND OTHER THINGS WHEN THE MACHINE NAME IS NOT FOUND IN /ETC/HOSTS OR
69 * CAUSES PROCESSES TO HANG WHEN THE NETWORK IS DISCONNECTED.
71 * POSTFIX NO LONGER NEEDS A FULLY QUALIFIED HOSTNAME. INSTEAD POSTFIX WILL
72 * USE A DEFAULT DOMAIN NAME "LOCALDOMAIN".
74 if (my_host_name
== 0) {
75 /* DO NOT CALL GETHOSTBYNAME FROM THIS FUNCTION */
76 if (gethostname(namebuf
, sizeof(namebuf
)) < 0)
77 msg_fatal("gethostname: %m");
78 namebuf
[MAXHOSTNAMELEN
] = 0;
79 /* DO NOT CALL GETHOSTBYNAME FROM THIS FUNCTION */
80 if (valid_hostname(namebuf
, DO_GRIPE
) == 0)
81 msg_fatal("unable to use my own hostname");
82 /* DO NOT CALL GETHOSTBYNAME FROM THIS FUNCTION */
83 my_host_name
= mystrdup(namebuf
);
85 return (my_host_name
);