xref: /netbsd-src/external/ibm-public/postfix/dist/src/util/get_hostname.c (revision 41fbaed053f8fbfdf9d2a4ee0a7386a3c83f8505)
1*41fbaed0Stron /*	$NetBSD: get_hostname.c,v 1.1.1.1 2009/06/23 10:09:00 tron Exp $	*/
2*41fbaed0Stron 
3*41fbaed0Stron /*++
4*41fbaed0Stron /* NAME
5*41fbaed0Stron /*	get_hostname 3
6*41fbaed0Stron /* SUMMARY
7*41fbaed0Stron /*	network name lookup
8*41fbaed0Stron /* SYNOPSIS
9*41fbaed0Stron /*	#include <get_hostname.h>
10*41fbaed0Stron /*
11*41fbaed0Stron /*	const char *get_hostname()
12*41fbaed0Stron /* DESCRIPTION
13*41fbaed0Stron /*	get_hostname() returns the local hostname as obtained
14*41fbaed0Stron /*	via gethostname() or its moral equivalent. This routine
15*41fbaed0Stron /*	goes to great length to avoid dependencies on any network
16*41fbaed0Stron /*	services.
17*41fbaed0Stron /* DIAGNOSTICS
18*41fbaed0Stron /*	Fatal errors: no hostname, invalid hostname.
19*41fbaed0Stron /* SEE ALSO
20*41fbaed0Stron /*	valid_hostname(3)
21*41fbaed0Stron /* LICENSE
22*41fbaed0Stron /* .ad
23*41fbaed0Stron /* .fi
24*41fbaed0Stron /*	The Secure Mailer license must be distributed with this software.
25*41fbaed0Stron /* AUTHOR(S)
26*41fbaed0Stron /*	Wietse Venema
27*41fbaed0Stron /*	IBM T.J. Watson Research
28*41fbaed0Stron /*	P.O. Box 704
29*41fbaed0Stron /*	Yorktown Heights, NY 10598, USA
30*41fbaed0Stron /*--*/
31*41fbaed0Stron 
32*41fbaed0Stron /* System library. */
33*41fbaed0Stron 
34*41fbaed0Stron #include <sys_defs.h>
35*41fbaed0Stron #include <sys/param.h>
36*41fbaed0Stron #include <string.h>
37*41fbaed0Stron #include <unistd.h>
38*41fbaed0Stron 
39*41fbaed0Stron #if (MAXHOSTNAMELEN < 256)
40*41fbaed0Stron #undef MAXHOSTNAMELEN
41*41fbaed0Stron #define MAXHOSTNAMELEN	256
42*41fbaed0Stron #endif
43*41fbaed0Stron 
44*41fbaed0Stron /* Utility library. */
45*41fbaed0Stron 
46*41fbaed0Stron #include "mymalloc.h"
47*41fbaed0Stron #include "msg.h"
48*41fbaed0Stron #include "valid_hostname.h"
49*41fbaed0Stron #include "get_hostname.h"
50*41fbaed0Stron 
51*41fbaed0Stron /* Local stuff. */
52*41fbaed0Stron 
53*41fbaed0Stron static char *my_host_name;
54*41fbaed0Stron 
55*41fbaed0Stron /* get_hostname - look up my host name */
56*41fbaed0Stron 
get_hostname(void)57*41fbaed0Stron const char *get_hostname(void)
58*41fbaed0Stron {
59*41fbaed0Stron     char    namebuf[MAXHOSTNAMELEN + 1];
60*41fbaed0Stron 
61*41fbaed0Stron     /*
62*41fbaed0Stron      * The gethostname() call is not (or not yet) in ANSI or POSIX, but it is
63*41fbaed0Stron      * part of the socket interface library. We avoid the more politically-
64*41fbaed0Stron      * correct uname() routine because that has no portable way of dealing
65*41fbaed0Stron      * with long (FQDN) hostnames.
66*41fbaed0Stron      *
67*41fbaed0Stron      * DO NOT CALL GETHOSTBYNAME FROM THIS FUNCTION. IT BREAKS MAILDIR DELIVERY
68*41fbaed0Stron      * AND OTHER THINGS WHEN THE MACHINE NAME IS NOT FOUND IN /ETC/HOSTS OR
69*41fbaed0Stron      * CAUSES PROCESSES TO HANG WHEN THE NETWORK IS DISCONNECTED.
70*41fbaed0Stron      *
71*41fbaed0Stron      * POSTFIX NO LONGER NEEDS A FULLY QUALIFIED HOSTNAME. INSTEAD POSTFIX WILL
72*41fbaed0Stron      * USE A DEFAULT DOMAIN NAME "LOCALDOMAIN".
73*41fbaed0Stron      */
74*41fbaed0Stron     if (my_host_name == 0) {
75*41fbaed0Stron 	/* DO NOT CALL GETHOSTBYNAME FROM THIS FUNCTION */
76*41fbaed0Stron 	if (gethostname(namebuf, sizeof(namebuf)) < 0)
77*41fbaed0Stron 	    msg_fatal("gethostname: %m");
78*41fbaed0Stron 	namebuf[MAXHOSTNAMELEN] = 0;
79*41fbaed0Stron 	/* DO NOT CALL GETHOSTBYNAME FROM THIS FUNCTION */
80*41fbaed0Stron 	if (valid_hostname(namebuf, DO_GRIPE) == 0)
81*41fbaed0Stron 	    msg_fatal("unable to use my own hostname");
82*41fbaed0Stron 	/* DO NOT CALL GETHOSTBYNAME FROM THIS FUNCTION */
83*41fbaed0Stron 	my_host_name = mystrdup(namebuf);
84*41fbaed0Stron     }
85*41fbaed0Stron     return (my_host_name);
86*41fbaed0Stron }
87