xref: /original-bsd/usr.sbin/sendmail/src/daemon.c (revision 6775ec03)
1939f5b94Sdist /*
20942ea6aSbostic  * Copyright (c) 1983 Eric P. Allman
3da1c6175Sbostic  * Copyright (c) 1988 Regents of the University of California.
4da1c6175Sbostic  * All rights reserved.
5da1c6175Sbostic  *
6da1c6175Sbostic  * Redistribution and use in source and binary forms are permitted
70942ea6aSbostic  * provided that the above copyright notice and this paragraph are
80942ea6aSbostic  * duplicated in all such forms and that any documentation,
90942ea6aSbostic  * advertising materials, and other materials related to such
100942ea6aSbostic  * distribution and use acknowledge that the software was developed
110942ea6aSbostic  * by the University of California, Berkeley.  The name of the
120942ea6aSbostic  * University may not be used to endorse or promote products derived
130942ea6aSbostic  * from this software without specific prior written permission.
140942ea6aSbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
150942ea6aSbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
160942ea6aSbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17939f5b94Sdist  */
18939f5b94Sdist 
197aa493c5Seric #include <errno.h>
20f36ede03Sbostic #include <sendmail.h>
217fa39d90Seric 
22af5e902cSeric #ifndef lint
23da1c6175Sbostic #ifdef DAEMON
24*6775ec03Sbostic static char sccsid[] = "@(#)daemon.c	5.30 (Berkeley) 01/19/89 (with daemon mode)";
25d0a9e852Seric #else
26*6775ec03Sbostic static char sccsid[] = "@(#)daemon.c	5.30 (Berkeley) 01/19/89 (without daemon mode)";
27da1c6175Sbostic #endif
28da1c6175Sbostic #endif /* not lint */
29da1c6175Sbostic 
30da1c6175Sbostic #ifdef DAEMON
31d0a9e852Seric 
321c71e510Seric # include <netdb.h>
3352308a50Seric # include <sys/signal.h>
341e1663f7Swnj # include <sys/wait.h>
35af5e902cSeric # include <sys/time.h>
36af5e902cSeric # include <sys/resource.h>
37d0a9e852Seric 
387fa39d90Seric /*
397fa39d90Seric **  DAEMON.C -- routines to use when running as a daemon.
4047b12ae1Seric **
4147b12ae1Seric **	This entire file is highly dependent on the 4.2 BSD
4247b12ae1Seric **	interprocess communication primitives.  No attempt has
4347b12ae1Seric **	been made to make this file portable to Version 7,
4447b12ae1Seric **	Version 6, MPX files, etc.  If you should try such a
4547b12ae1Seric **	thing yourself, I recommend chucking the entire file
4647b12ae1Seric **	and starting from scratch.  Basic semantics are:
4747b12ae1Seric **
4847b12ae1Seric **	getrequests()
4947b12ae1Seric **		Opens a port and initiates a connection.
5047b12ae1Seric **		Returns in a child.  Must set InChannel and
5147b12ae1Seric **		OutChannel appropriately.
52b7d7afcbSeric **	clrdaemon()
53b7d7afcbSeric **		Close any open files associated with getting
54b7d7afcbSeric **		the connection; this is used when running the queue,
55b7d7afcbSeric **		etc., to avoid having extra file descriptors during
56b7d7afcbSeric **		the queue run and to avoid confusing the network
57b7d7afcbSeric **		code (if it cares).
5847b12ae1Seric **	makeconnection(host, port, outfile, infile)
5947b12ae1Seric **		Make a connection to the named host on the given
6047b12ae1Seric **		port.  Set *outfile and *infile to the files
6147b12ae1Seric **		appropriate for communication.  Returns zero on
6247b12ae1Seric **		success, else an exit status describing the
6347b12ae1Seric **		error.
648e8f00b5Seric **	maphostname(hbuf, hbufsize)
658e8f00b5Seric **		Convert the entry in hbuf into a canonical form.  It
668e8f00b5Seric **		may not be larger than hbufsize.
677fa39d90Seric */
687fa39d90Seric /*
697fa39d90Seric **  GETREQUESTS -- open mail IPC port and get requests.
707fa39d90Seric **
717fa39d90Seric **	Parameters:
727fa39d90Seric **		none.
737fa39d90Seric **
747fa39d90Seric **	Returns:
757fa39d90Seric **		none.
767fa39d90Seric **
777fa39d90Seric **	Side Effects:
787fa39d90Seric **		Waits until some interesting activity occurs.  When
797fa39d90Seric **		it does, a child is created to process it, and the
807fa39d90Seric **		parent waits for completion.  Return from this
81147303b1Seric **		routine is always in the child.  The file pointers
82147303b1Seric **		"InChannel" and "OutChannel" should be set to point
83147303b1Seric **		to the communication channel.
847fa39d90Seric */
857fa39d90Seric 
86b7d7afcbSeric struct sockaddr_in	SendmailAddress;/* internet address of sendmail */
879478786eSeric 
88b7d7afcbSeric int	DaemonSocket	= -1;		/* fd describing socket */
89be388505Seric char	*NetName;			/* name of home (local?) network */
901c71e510Seric 
917fa39d90Seric getrequests()
927fa39d90Seric {
931c71e510Seric 	int t;
941c71e510Seric 	register struct servent *sp;
957868dfc2Seric 	int on = 1;
9652308a50Seric 	extern reapchild();
97eb889047Seric 
98a8268164Seric 	/*
991c71e510Seric 	**  Set up the address for the mailer.
100eb889047Seric 	*/
101eb889047Seric 
1021c71e510Seric 	sp = getservbyname("smtp", "tcp");
1031c71e510Seric 	if (sp == NULL)
104d0a9e852Seric 	{
1051c71e510Seric 		syserr("server \"smtp\" unknown");
106a1961f2aSeric 		goto severe;
1071c71e510Seric 	}
1081c71e510Seric 	SendmailAddress.sin_family = AF_INET;
1091c71e510Seric 	SendmailAddress.sin_addr.s_addr = INADDR_ANY;
110f43fa3c2Ssam 	SendmailAddress.sin_port = sp->s_port;
1111c71e510Seric 
1121c71e510Seric 	/*
1131c71e510Seric 	**  Try to actually open the connection.
1141c71e510Seric 	*/
1151c71e510Seric 
1161c71e510Seric 	if (tTd(15, 1))
1171c71e510Seric 		printf("getrequests: port 0x%x\n", SendmailAddress.sin_port);
1181c71e510Seric 
1191c71e510Seric 	/* get a socket for the SMTP connection */
120af5e902cSeric 	DaemonSocket = socket(AF_INET, SOCK_STREAM, 0);
121b7d7afcbSeric 	if (DaemonSocket < 0)
1221c71e510Seric 	{
1231c71e510Seric 		/* probably another daemon already */
1241c71e510Seric 		syserr("getrequests: can't create socket");
1251c71e510Seric 	  severe:
126b0ba8827Seric # ifdef LOG
127b0ba8827Seric 		if (LogLevel > 0)
128d2aa4720Seric 			syslog(LOG_ALERT, "cannot get connection");
129b0ba8827Seric # endif LOG
13047b12ae1Seric 		finis();
131d0a9e852Seric 	}
1321b6e4a15Seric 
1331b6e4a15Seric 	/* turn on network debugging? */
1341b6e4a15Seric 	if (tTd(15, 15))
13552308a50Seric 		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on);
1361b6e4a15Seric 
1377868dfc2Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on);
1387868dfc2Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on);
1397868dfc2Seric 
140af5e902cSeric 	if (bind(DaemonSocket, &SendmailAddress, sizeof SendmailAddress) < 0)
1411c71e510Seric 	{
1421c71e510Seric 		syserr("getrequests: cannot bind");
143b7d7afcbSeric 		(void) close(DaemonSocket);
1441c71e510Seric 		goto severe;
1451c71e510Seric 	}
146af5e902cSeric 	if (listen(DaemonSocket, 10) < 0)
147af5e902cSeric 	{
148af5e902cSeric 		syserr("getrequests: cannot listen");
149af5e902cSeric 		(void) close(DaemonSocket);
150af5e902cSeric 		goto severe;
151af5e902cSeric 	}
1521c71e510Seric 
1531cd247eeSeric 	(void) signal(SIGCHLD, reapchild);
15452308a50Seric 
1551c71e510Seric 	if (tTd(15, 1))
156b7d7afcbSeric 		printf("getrequests: %d\n", DaemonSocket);
1571c71e510Seric 
1581c71e510Seric 	for (;;)
1591c71e510Seric 	{
1603a099713Seric 		register int pid;
161a44d5a5eSeric 		auto int lotherend;
1623a099713Seric 		extern int RefuseLA;
163*6775ec03Sbostic 		register int la;
1643a099713Seric 
1653a099713Seric 		/* see if we are rejecting connections */
166*6775ec03Sbostic 		while ((la = getla()) > RefuseLA)
167*6775ec03Sbostic 		{
168*6775ec03Sbostic 			setproctitle("rejecting connections: load average: %.2f", la);
1693a099713Seric 			sleep(5);
170*6775ec03Sbostic 		}
171a44d5a5eSeric 
1721c71e510Seric 		/* wait for a connection */
173*6775ec03Sbostic 		setproctitle("accepting connections");
1741c71e510Seric 		do
1751c71e510Seric 		{
1761c71e510Seric 			errno = 0;
1779f9a15b6Skarels 			lotherend = sizeof RealHostAddr;
1789f9a15b6Skarels 			t = accept(DaemonSocket, &RealHostAddr, &lotherend);
1791c71e510Seric 		} while (t < 0 && errno == EINTR);
1801c71e510Seric 		if (t < 0)
1811c71e510Seric 		{
1821c71e510Seric 			syserr("getrequests: accept");
1831c71e510Seric 			sleep(5);
1841c71e510Seric 			continue;
1851c71e510Seric 		}
186d0a9e852Seric 
187d0a9e852Seric 		/*
188d0a9e852Seric 		**  Create a subprocess to process the mail.
189d0a9e852Seric 		*/
190d0a9e852Seric 
19161e4310fSeric 		if (tTd(15, 2))
1921c71e510Seric 			printf("getrequests: forking (fd = %d)\n", t);
193eb889047Seric 
194a8268164Seric 		pid = fork();
195a8268164Seric 		if (pid < 0)
196a8268164Seric 		{
197a8268164Seric 			syserr("daemon: cannot fork");
198a8268164Seric 			sleep(10);
1991c71e510Seric 			(void) close(t);
200a8268164Seric 			continue;
201a8268164Seric 		}
202a8268164Seric 
203a8268164Seric 		if (pid == 0)
204a8268164Seric 		{
205a44d5a5eSeric 			extern struct hostent *gethostbyaddr();
206a44d5a5eSeric 			register struct hostent *hp;
207a44d5a5eSeric 			char buf[MAXNAME];
208a44d5a5eSeric 
209a8268164Seric 			/*
210a8268164Seric 			**  CHILD -- return to caller.
211a44d5a5eSeric 			**	Collect verified idea of sending host.
212a8268164Seric 			**	Verify calling user id if possible here.
213a8268164Seric 			*/
214a8268164Seric 
2151cd247eeSeric 			(void) signal(SIGCHLD, SIG_DFL);
216779ac194Seric 
217a44d5a5eSeric 			/* determine host name */
2189f9a15b6Skarels 			hp = gethostbyaddr((char *) &RealHostAddr.sin_addr, sizeof RealHostAddr.sin_addr, AF_INET);
219a44d5a5eSeric 			if (hp != NULL)
220fefbbe29Seric 				(void) strcpy(buf, hp->h_name);
221a44d5a5eSeric 			else
22229dcf4baSeric 			{
22329dcf4baSeric 				extern char *inet_ntoa();
22429dcf4baSeric 
22529dcf4baSeric 				/* produce a dotted quad */
22629dcf4baSeric 				(void) sprintf(buf, "[%s]",
2279f9a15b6Skarels 					inet_ntoa(RealHostAddr.sin_addr));
22829dcf4baSeric 			}
22929dcf4baSeric 
23029dcf4baSeric 			/* should we check for illegal connection here? XXX */
23129dcf4baSeric 
232a44d5a5eSeric 			RealHostName = newstr(buf);
233a44d5a5eSeric 
234b7d7afcbSeric 			(void) close(DaemonSocket);
2351c71e510Seric 			InChannel = fdopen(t, "r");
2361d5bd586Seric 			OutChannel = fdopen(dup(t), "w");
23761e4310fSeric 			if (tTd(15, 2))
238d0a9e852Seric 				printf("getreq: returning\n");
239252c8a22Seric # ifdef LOG
240252c8a22Seric 			if (LogLevel > 11)
241252c8a22Seric 				syslog(LOG_DEBUG, "connected, pid=%d", getpid());
242252c8a22Seric # endif LOG
243a8268164Seric 			return;
244a8268164Seric 		}
245a8268164Seric 
2463c154354Seric 		/* close the port so that others will hang (for a while) */
2473c154354Seric 		(void) close(t);
2488e3e4b17Seric 	}
2493c154354Seric 	/*NOTREACHED*/
2503c154354Seric }
2518e3e4b17Seric /*
252b7d7afcbSeric **  CLRDAEMON -- reset the daemon connection
253b7d7afcbSeric **
254b7d7afcbSeric **	Parameters:
255b7d7afcbSeric **		none.
256b7d7afcbSeric **
257b7d7afcbSeric **	Returns:
258b7d7afcbSeric **		none.
259b7d7afcbSeric **
260b7d7afcbSeric **	Side Effects:
261b7d7afcbSeric **		releases any resources used by the passive daemon.
262b7d7afcbSeric */
263b7d7afcbSeric 
264b7d7afcbSeric clrdaemon()
265b7d7afcbSeric {
266b7d7afcbSeric 	if (DaemonSocket >= 0)
267b7d7afcbSeric 		(void) close(DaemonSocket);
268b7d7afcbSeric 	DaemonSocket = -1;
269b7d7afcbSeric }
270b7d7afcbSeric /*
2717aa493c5Seric **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
2727aa493c5Seric **
2737aa493c5Seric **	Parameters:
2747aa493c5Seric **		host -- the name of the host.
27548ff0a9dSeric **		port -- the port number to connect to.
2767aa493c5Seric **		outfile -- a pointer to a place to put the outfile
2777aa493c5Seric **			descriptor.
2787aa493c5Seric **		infile -- ditto for infile.
2797aa493c5Seric **
2807aa493c5Seric **	Returns:
2817aa493c5Seric **		An exit code telling whether the connection could be
2827aa493c5Seric **			made and if not why not.
2837aa493c5Seric **
2847aa493c5Seric **	Side Effects:
2857aa493c5Seric **		none.
2867aa493c5Seric */
2877aa493c5Seric 
28848ff0a9dSeric makeconnection(host, port, outfile, infile)
2897aa493c5Seric 	char *host;
290210215eaSeric 	u_short port;
2917aa493c5Seric 	FILE **outfile;
2927aa493c5Seric 	FILE **infile;
2937aa493c5Seric {
29404344589Sbloom 	register int i, s;
29504344589Sbloom 	register struct hostent *hp = (struct hostent *)NULL;
29604344589Sbloom 	extern char *inet_ntoa();
2976286bb75Sbloom 	int sav_errno;
298134746fbSeric #ifdef NAMED_BIND
299134746fbSeric 	extern int h_errno;
300134746fbSeric #endif
3017aa493c5Seric 
3027aa493c5Seric 	/*
3037aa493c5Seric 	**  Set up the address for the mailer.
30471096d12Seric 	**	Accept "[a.b.c.d]" syntax for host name.
3057aa493c5Seric 	*/
3067aa493c5Seric 
307134746fbSeric #ifdef NAMED_BIND
308794bdbb9Smiriam 	h_errno = 0;
309134746fbSeric #endif
310794bdbb9Smiriam 	errno = 0;
311794bdbb9Smiriam 
31271096d12Seric 	if (host[0] == '[')
31371096d12Seric 	{
314a44d5a5eSeric 		long hid;
315a44d5a5eSeric 		register char *p = index(host, ']');
31671096d12Seric 
317a44d5a5eSeric 		if (p != NULL)
31871096d12Seric 		{
319a44d5a5eSeric 			*p = '\0';
320a44d5a5eSeric 			hid = inet_addr(&host[1]);
321a44d5a5eSeric 			*p = ']';
32271096d12Seric 		}
323a44d5a5eSeric 		if (p == NULL || hid == -1)
32471096d12Seric 		{
32571096d12Seric 			usrerr("Invalid numeric domain spec \"%s\"", host);
32671096d12Seric 			return (EX_NOHOST);
32771096d12Seric 		}
32871096d12Seric 		SendmailAddress.sin_addr.s_addr = hid;
32971096d12Seric 	}
3301c71e510Seric 	else
3311c71e510Seric 	{
33204344589Sbloom 		hp = gethostbyname(host);
333794bdbb9Smiriam 		if (hp == NULL)
334794bdbb9Smiriam 		{
335134746fbSeric #ifdef NAMED_BIND
336794bdbb9Smiriam 			if (errno == ETIMEDOUT || h_errno == TRY_AGAIN)
33752308a50Seric 				return (EX_TEMPFAIL);
33882e5d8ddSeric 
339134746fbSeric 			/* if name server is specified, assume temp fail */
340134746fbSeric 			if (errno == ECONNREFUSED && UseNameServer)
341134746fbSeric 				return (EX_TEMPFAIL);
342134746fbSeric #endif
343134746fbSeric 
34482e5d8ddSeric 			/*
34582e5d8ddSeric 			**  XXX Should look for mail forwarder record here
34682e5d8ddSeric 			**  XXX if (h_errno == NO_ADDRESS).
34782e5d8ddSeric 			*/
34882e5d8ddSeric 
3497aa493c5Seric 			return (EX_NOHOST);
350794bdbb9Smiriam 		}
35129dcf4baSeric 		bcopy(hp->h_addr, (char *) &SendmailAddress.sin_addr, hp->h_length);
35204344589Sbloom 		i = 1;
3531c71e510Seric 	}
3541c71e510Seric 
3551c71e510Seric 	/*
3561c71e510Seric 	**  Determine the port number.
3571c71e510Seric 	*/
3581c71e510Seric 
359fd7c0790Seric 	if (port != 0)
360fd7c0790Seric 		SendmailAddress.sin_port = htons(port);
361fd7c0790Seric 	else
3621c71e510Seric 	{
3631c71e510Seric 		register struct servent *sp = getservbyname("smtp", "tcp");
3641c71e510Seric 
3651c71e510Seric 		if (sp == NULL)
3661c71e510Seric 		{
3671c71e510Seric 			syserr("makeconnection: server \"smtp\" unknown");
3681c71e510Seric 			return (EX_OSFILE);
3691c71e510Seric 		}
370fd7c0790Seric 		SendmailAddress.sin_port = sp->s_port;
3711c71e510Seric 	}
3727aa493c5Seric 
3737aa493c5Seric 	/*
3747aa493c5Seric 	**  Try to actually open the connection.
3757aa493c5Seric 	*/
3767aa493c5Seric 
37704344589Sbloom again:
37861e4310fSeric 	if (tTd(16, 1))
37904344589Sbloom 		printf("makeconnection (%s [%s])\n", host,
38004344589Sbloom 		    inet_ntoa(SendmailAddress.sin_addr.s_addr));
3817aa493c5Seric 
382af5e902cSeric 	s = socket(AF_INET, SOCK_STREAM, 0);
3837aa493c5Seric 	if (s < 0)
3847aa493c5Seric 	{
3857aa493c5Seric 		syserr("makeconnection: no socket");
3866286bb75Sbloom 		sav_errno = errno;
3877aa493c5Seric 		goto failure;
3887aa493c5Seric 	}
3897aa493c5Seric 
39061e4310fSeric 	if (tTd(16, 1))
3917aa493c5Seric 		printf("makeconnection: %d\n", s);
3921b6e4a15Seric 
3931b6e4a15Seric 	/* turn on network debugging? */
3941b6e4a15Seric 	if (tTd(16, 14))
39552308a50Seric 	{
39652308a50Seric 		int on = 1;
39752308a50Seric 		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on);
39852308a50Seric 	}
399877a6142Seric 	(void) fflush(CurEnv->e_xfp);			/* for debugging */
4004bd6a662Seric 	errno = 0;					/* for debugging */
4011c71e510Seric 	SendmailAddress.sin_family = AF_INET;
402af5e902cSeric 	if (connect(s, &SendmailAddress, sizeof SendmailAddress) < 0)
4037aa493c5Seric 	{
4046286bb75Sbloom 		sav_errno = errno;
4056286bb75Sbloom 		(void) close(s);
40604344589Sbloom 		if (hp && hp->h_addr_list[i])
40704344589Sbloom 		{
40804344589Sbloom 			bcopy(hp->h_addr_list[i++],
40904344589Sbloom 			    (char *)&SendmailAddress.sin_addr, hp->h_length);
41004344589Sbloom 			goto again;
41104344589Sbloom 		}
41204344589Sbloom 
4137aa493c5Seric 		/* failure, decide if temporary or not */
4147aa493c5Seric 	failure:
4156286bb75Sbloom 		switch (sav_errno)
4167aa493c5Seric 		{
4177aa493c5Seric 		  case EISCONN:
4187aa493c5Seric 		  case ETIMEDOUT:
419292668b9Seric 		  case EINPROGRESS:
420292668b9Seric 		  case EALREADY:
421292668b9Seric 		  case EADDRINUSE:
42236bd23a8Seric 		  case EHOSTDOWN:
423292668b9Seric 		  case ENETDOWN:
424292668b9Seric 		  case ENETRESET:
425292668b9Seric 		  case ENOBUFS:
426a1645df8Seric 		  case ECONNREFUSED:
427efe49624Seric 		  case ECONNRESET:
428cae8261aSeric 		  case EHOSTUNREACH:
429dca8e1f7Seric 		  case ENETUNREACH:
4307aa493c5Seric 			/* there are others, I'm sure..... */
4317aa493c5Seric 			return (EX_TEMPFAIL);
4327aa493c5Seric 
433a44d5a5eSeric 		  case EPERM:
434a44d5a5eSeric 			/* why is this happening? */
435a44d5a5eSeric 			syserr("makeconnection: funny failure, addr=%lx, port=%x",
436a44d5a5eSeric 				SendmailAddress.sin_addr.s_addr, SendmailAddress.sin_port);
4374bd6a662Seric 			return (EX_TEMPFAIL);
438a44d5a5eSeric 
4397aa493c5Seric 		  default:
440e8212f61Sbostic 			message(Arpa_Info, "%s", errstring(sav_errno));
4417aa493c5Seric 			return (EX_UNAVAILABLE);
4427aa493c5Seric 		}
4437aa493c5Seric 	}
4447aa493c5Seric 
4457aa493c5Seric 	/* connection ok, put it into canonical form */
4467aa493c5Seric 	*outfile = fdopen(s, "w");
4477aa493c5Seric 	*infile = fdopen(s, "r");
4487aa493c5Seric 
449dca8e1f7Seric 	return (EX_OK);
4507aa493c5Seric }
451444eaf03Seric /*
452444eaf03Seric **  MYHOSTNAME -- return the name of this host.
453444eaf03Seric **
454444eaf03Seric **	Parameters:
455444eaf03Seric **		hostbuf -- a place to return the name of this host.
456897f1869Seric **		size -- the size of hostbuf.
457444eaf03Seric **
458444eaf03Seric **	Returns:
459444eaf03Seric **		A list of aliases for this host.
460444eaf03Seric **
461444eaf03Seric **	Side Effects:
462444eaf03Seric **		none.
463444eaf03Seric */
464444eaf03Seric 
465444eaf03Seric char **
466897f1869Seric myhostname(hostbuf, size)
467444eaf03Seric 	char hostbuf[];
468897f1869Seric 	int size;
469444eaf03Seric {
470444eaf03Seric 	extern struct hostent *gethostbyname();
471a44d5a5eSeric 	struct hostent *hp;
472444eaf03Seric 
473af5e902cSeric 	if (gethostname(hostbuf, size) < 0)
474af5e902cSeric 	{
475af5e902cSeric 		(void) strcpy(hostbuf, "localhost");
476af5e902cSeric 	}
477a44d5a5eSeric 	hp = gethostbyname(hostbuf);
478a44d5a5eSeric 	if (hp != NULL)
4797364df9fSeric 	{
480fefbbe29Seric 		(void) strcpy(hostbuf, hp->h_name);
481a44d5a5eSeric 		return (hp->h_aliases);
4827364df9fSeric 	}
483444eaf03Seric 	else
484444eaf03Seric 		return (NULL);
485444eaf03Seric }
486444eaf03Seric 
487f36ede03Sbostic /*
488f36ede03Sbostic  *  MAPHOSTNAME -- turn a hostname into canonical form
489f36ede03Sbostic  *
490f36ede03Sbostic  *	Parameters:
491f36ede03Sbostic  *		hbuf -- a buffer containing a hostname.
492f36ede03Sbostic  *		hbsize -- the size of hbuf.
493f36ede03Sbostic  *
494f36ede03Sbostic  *	Returns:
495f36ede03Sbostic  *		none.
496f36ede03Sbostic  *
497f36ede03Sbostic  *	Side Effects:
498f36ede03Sbostic  *		Looks up the host specified in hbuf.  If it is not
499f36ede03Sbostic  *		the canonical name for that host, replace it with
500f36ede03Sbostic  *		the canonical name.  If the name is unknown, or it
501f36ede03Sbostic  *		is already the canonical name, leave it unchanged.
502f36ede03Sbostic  */
50399f7cf32Seric maphostname(hbuf, hbsize)
50499f7cf32Seric 	char *hbuf;
50599f7cf32Seric 	int hbsize;
50699f7cf32Seric {
50799f7cf32Seric 	register struct hostent *hp;
5085f78836eSmiriam 	u_long in_addr;
5095f78836eSmiriam 	char ptr[256];
510f36ede03Sbostic 	struct hostent *gethostbyaddr();
5115f78836eSmiriam 
512f36ede03Sbostic 	/*
513f36ede03Sbostic 	 * If first character is a bracket, then it is an address
514f36ede03Sbostic 	 * lookup.  Address is copied into a temporary buffer to
515f36ede03Sbostic 	 * strip the brackets and to preserve hbuf if address is
516f36ede03Sbostic 	 * unknown.
517f36ede03Sbostic 	 */
518f36ede03Sbostic 	if (*hbuf != '[') {
519f36ede03Sbostic 		getcanonname(hbuf, hbsize);
520f36ede03Sbostic 		return;
521f36ede03Sbostic 	}
522f36ede03Sbostic 	*index(strcpy(ptr, hbuf), ']') = '\0';
5235f78836eSmiriam 	in_addr = inet_addr(&ptr[1]);
52431601fa7Seric 	hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET);
5255f78836eSmiriam 	if (hp == NULL)
52631601fa7Seric 		return;
527f36ede03Sbostic 	if (strlen(hp->h_name) >= hbsize)
528301df7d6Sbloom 		hp->h_name[hbsize - 1] = '\0';
529fefbbe29Seric 	(void)strcpy(hbuf, hp->h_name);
53099f7cf32Seric }
531f36ede03Sbostic 
532444eaf03Seric # else DAEMON
53399f7cf32Seric /* code for systems without sophisticated networking */
534444eaf03Seric 
535444eaf03Seric /*
536444eaf03Seric **  MYHOSTNAME -- stub version for case of no daemon code.
53721e9914dSeric **
53821e9914dSeric **	Can't convert to upper case here because might be a UUCP name.
539897f1869Seric **
540897f1869Seric **	Mark, you can change this to be anything you want......
541444eaf03Seric */
542444eaf03Seric 
543444eaf03Seric char **
544897f1869Seric myhostname(hostbuf, size)
545444eaf03Seric 	char hostbuf[];
546897f1869Seric 	int size;
547444eaf03Seric {
548444eaf03Seric 	register FILE *f;
549444eaf03Seric 
550444eaf03Seric 	hostbuf[0] = '\0';
551444eaf03Seric 	f = fopen("/usr/include/whoami", "r");
552444eaf03Seric 	if (f != NULL)
553444eaf03Seric 	{
554897f1869Seric 		(void) fgets(hostbuf, size, f);
555444eaf03Seric 		fixcrlf(hostbuf, TRUE);
556444eaf03Seric 		(void) fclose(f);
557444eaf03Seric 	}
558444eaf03Seric 	return (NULL);
559444eaf03Seric }
56099f7cf32Seric /*
56199f7cf32Seric **  MAPHOSTNAME -- turn a hostname into canonical form
56299f7cf32Seric **
56399f7cf32Seric **	Parameters:
56499f7cf32Seric **		hbuf -- a buffer containing a hostname.
56599f7cf32Seric **		hbsize -- the size of hbuf.
56699f7cf32Seric **
56799f7cf32Seric **	Returns:
56899f7cf32Seric **		none.
56999f7cf32Seric **
57099f7cf32Seric **	Side Effects:
57199f7cf32Seric **		Looks up the host specified in hbuf.  If it is not
57299f7cf32Seric **		the canonical name for that host, replace it with
57399f7cf32Seric **		the canonical name.  If the name is unknown, or it
57499f7cf32Seric **		is already the canonical name, leave it unchanged.
57599f7cf32Seric */
57699f7cf32Seric 
57799f7cf32Seric /*ARGSUSED*/
57899f7cf32Seric maphostname(hbuf, hbsize)
57999f7cf32Seric 	char *hbuf;
58099f7cf32Seric 	int hbsize;
58199f7cf32Seric {
58299f7cf32Seric 	return;
58399f7cf32Seric }
58499f7cf32Seric 
585d0a9e852Seric #endif DAEMON
586