xref: /original-bsd/usr.sbin/sendmail/src/daemon.c (revision f36ede03)
1939f5b94Sdist /*
2da1c6175Sbostic  * Copyright (c) 1988 Regents of the University of California.
3da1c6175Sbostic  * All rights reserved.
4da1c6175Sbostic  *
5da1c6175Sbostic  * Redistribution and use in source and binary forms are permitted
6da1c6175Sbostic  * provided that this notice is preserved and that due credit is given
7da1c6175Sbostic  * to the University of California at Berkeley. The name of the University
8da1c6175Sbostic  * may not be used to endorse or promote products derived from this
9da1c6175Sbostic  * software without specific prior written permission. This software
10da1c6175Sbostic  * is provided ``as is'' without express or implied warranty.
11da1c6175Sbostic  *
12da1c6175Sbostic  *  Sendmail
13da1c6175Sbostic  *  Copyright (c) 1983  Eric P. Allman
14da1c6175Sbostic  *  Berkeley, California
15939f5b94Sdist  */
16939f5b94Sdist 
177aa493c5Seric #include <errno.h>
18*f36ede03Sbostic #include <sendmail.h>
197fa39d90Seric 
20af5e902cSeric #ifndef lint
21da1c6175Sbostic #ifdef DAEMON
22*f36ede03Sbostic static char sccsid[] = "@(#)daemon.c	5.25 (Berkeley) 04/01/88 (with daemon mode)";
23d0a9e852Seric #else
24*f36ede03Sbostic static char sccsid[] = "@(#)daemon.c	5.25 (Berkeley) 04/01/88 (without daemon mode)";
25da1c6175Sbostic #endif
26da1c6175Sbostic #endif /* not lint */
27da1c6175Sbostic 
28da1c6175Sbostic #ifdef DAEMON
29d0a9e852Seric 
301c71e510Seric # include <netdb.h>
3152308a50Seric # include <sys/signal.h>
321e1663f7Swnj # include <sys/wait.h>
33af5e902cSeric # include <sys/time.h>
34af5e902cSeric # include <sys/resource.h>
35d0a9e852Seric 
367fa39d90Seric /*
377fa39d90Seric **  DAEMON.C -- routines to use when running as a daemon.
3847b12ae1Seric **
3947b12ae1Seric **	This entire file is highly dependent on the 4.2 BSD
4047b12ae1Seric **	interprocess communication primitives.  No attempt has
4147b12ae1Seric **	been made to make this file portable to Version 7,
4247b12ae1Seric **	Version 6, MPX files, etc.  If you should try such a
4347b12ae1Seric **	thing yourself, I recommend chucking the entire file
4447b12ae1Seric **	and starting from scratch.  Basic semantics are:
4547b12ae1Seric **
4647b12ae1Seric **	getrequests()
4747b12ae1Seric **		Opens a port and initiates a connection.
4847b12ae1Seric **		Returns in a child.  Must set InChannel and
4947b12ae1Seric **		OutChannel appropriately.
50b7d7afcbSeric **	clrdaemon()
51b7d7afcbSeric **		Close any open files associated with getting
52b7d7afcbSeric **		the connection; this is used when running the queue,
53b7d7afcbSeric **		etc., to avoid having extra file descriptors during
54b7d7afcbSeric **		the queue run and to avoid confusing the network
55b7d7afcbSeric **		code (if it cares).
5647b12ae1Seric **	makeconnection(host, port, outfile, infile)
5747b12ae1Seric **		Make a connection to the named host on the given
5847b12ae1Seric **		port.  Set *outfile and *infile to the files
5947b12ae1Seric **		appropriate for communication.  Returns zero on
6047b12ae1Seric **		success, else an exit status describing the
6147b12ae1Seric **		error.
628e8f00b5Seric **	maphostname(hbuf, hbufsize)
638e8f00b5Seric **		Convert the entry in hbuf into a canonical form.  It
648e8f00b5Seric **		may not be larger than hbufsize.
657fa39d90Seric */
667fa39d90Seric /*
677fa39d90Seric **  GETREQUESTS -- open mail IPC port and get requests.
687fa39d90Seric **
697fa39d90Seric **	Parameters:
707fa39d90Seric **		none.
717fa39d90Seric **
727fa39d90Seric **	Returns:
737fa39d90Seric **		none.
747fa39d90Seric **
757fa39d90Seric **	Side Effects:
767fa39d90Seric **		Waits until some interesting activity occurs.  When
777fa39d90Seric **		it does, a child is created to process it, and the
787fa39d90Seric **		parent waits for completion.  Return from this
79147303b1Seric **		routine is always in the child.  The file pointers
80147303b1Seric **		"InChannel" and "OutChannel" should be set to point
81147303b1Seric **		to the communication channel.
827fa39d90Seric */
837fa39d90Seric 
84b7d7afcbSeric struct sockaddr_in	SendmailAddress;/* internet address of sendmail */
859478786eSeric 
86b7d7afcbSeric int	DaemonSocket	= -1;		/* fd describing socket */
87be388505Seric char	*NetName;			/* name of home (local?) network */
881c71e510Seric 
897fa39d90Seric getrequests()
907fa39d90Seric {
911c71e510Seric 	int t;
921c71e510Seric 	register struct servent *sp;
937868dfc2Seric 	int on = 1;
9452308a50Seric 	extern reapchild();
95eb889047Seric 
96a8268164Seric 	/*
971c71e510Seric 	**  Set up the address for the mailer.
98eb889047Seric 	*/
99eb889047Seric 
1001c71e510Seric 	sp = getservbyname("smtp", "tcp");
1011c71e510Seric 	if (sp == NULL)
102d0a9e852Seric 	{
1031c71e510Seric 		syserr("server \"smtp\" unknown");
104a1961f2aSeric 		goto severe;
1051c71e510Seric 	}
1061c71e510Seric 	SendmailAddress.sin_family = AF_INET;
1071c71e510Seric 	SendmailAddress.sin_addr.s_addr = INADDR_ANY;
108f43fa3c2Ssam 	SendmailAddress.sin_port = sp->s_port;
1091c71e510Seric 
1101c71e510Seric 	/*
1111c71e510Seric 	**  Try to actually open the connection.
1121c71e510Seric 	*/
1131c71e510Seric 
1141c71e510Seric # ifdef DEBUG
1151c71e510Seric 	if (tTd(15, 1))
1161c71e510Seric 		printf("getrequests: port 0x%x\n", SendmailAddress.sin_port);
1171c71e510Seric # endif DEBUG
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 #ifdef DEBUG
1341b6e4a15Seric 	/* turn on network debugging? */
1351b6e4a15Seric 	if (tTd(15, 15))
13652308a50Seric 		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on);
1371b6e4a15Seric #endif DEBUG
1381b6e4a15Seric 
1397868dfc2Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on);
1407868dfc2Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on);
1417868dfc2Seric 
142af5e902cSeric 	if (bind(DaemonSocket, &SendmailAddress, sizeof SendmailAddress) < 0)
1431c71e510Seric 	{
1441c71e510Seric 		syserr("getrequests: cannot bind");
145b7d7afcbSeric 		(void) close(DaemonSocket);
1461c71e510Seric 		goto severe;
1471c71e510Seric 	}
148af5e902cSeric 	if (listen(DaemonSocket, 10) < 0)
149af5e902cSeric 	{
150af5e902cSeric 		syserr("getrequests: cannot listen");
151af5e902cSeric 		(void) close(DaemonSocket);
152af5e902cSeric 		goto severe;
153af5e902cSeric 	}
1541c71e510Seric 
1551cd247eeSeric 	(void) signal(SIGCHLD, reapchild);
15652308a50Seric 
1571c71e510Seric # ifdef DEBUG
1581c71e510Seric 	if (tTd(15, 1))
159b7d7afcbSeric 		printf("getrequests: %d\n", DaemonSocket);
1601c71e510Seric # endif DEBUG
1611c71e510Seric 
1621c71e510Seric 	for (;;)
1631c71e510Seric 	{
1643a099713Seric 		register int pid;
165a44d5a5eSeric 		auto int lotherend;
166a44d5a5eSeric 		struct sockaddr_in otherend;
1673a099713Seric 		extern int RefuseLA;
1683a099713Seric 
1693a099713Seric 		/* see if we are rejecting connections */
1703a099713Seric 		while (getla() > RefuseLA)
1713a099713Seric 			sleep(5);
172a44d5a5eSeric 
1731c71e510Seric 		/* wait for a connection */
1741c71e510Seric 		do
1751c71e510Seric 		{
1761c71e510Seric 			errno = 0;
1771c71e510Seric 			lotherend = sizeof otherend;
178af5e902cSeric 			t = accept(DaemonSocket, &otherend, &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 
191d0a9e852Seric # ifdef DEBUG
19261e4310fSeric 		if (tTd(15, 2))
1931c71e510Seric 			printf("getrequests: forking (fd = %d)\n", t);
194d0a9e852Seric # endif DEBUG
195eb889047Seric 
196a8268164Seric 		pid = fork();
197a8268164Seric 		if (pid < 0)
198a8268164Seric 		{
199a8268164Seric 			syserr("daemon: cannot fork");
200a8268164Seric 			sleep(10);
2011c71e510Seric 			(void) close(t);
202a8268164Seric 			continue;
203a8268164Seric 		}
204a8268164Seric 
205a8268164Seric 		if (pid == 0)
206a8268164Seric 		{
207a44d5a5eSeric 			extern struct hostent *gethostbyaddr();
208a44d5a5eSeric 			register struct hostent *hp;
209a44d5a5eSeric 			char buf[MAXNAME];
210a44d5a5eSeric 
211a8268164Seric 			/*
212a8268164Seric 			**  CHILD -- return to caller.
213a44d5a5eSeric 			**	Collect verified idea of sending host.
214a8268164Seric 			**	Verify calling user id if possible here.
215a8268164Seric 			*/
216a8268164Seric 
2171cd247eeSeric 			(void) signal(SIGCHLD, SIG_DFL);
218779ac194Seric 
219a44d5a5eSeric 			/* determine host name */
22031601fa7Seric 			hp = gethostbyaddr((char *) &otherend.sin_addr, sizeof otherend.sin_addr, AF_INET);
221a44d5a5eSeric 			if (hp != NULL)
222be388505Seric 			{
223fefbbe29Seric 				(void) strcpy(buf, hp->h_name);
224be388505Seric 				if (NetName != NULL && NetName[0] != '\0' &&
2257dcf5c8cSeric 				    index(hp->h_name, '.') == NULL)
226be388505Seric 				{
227fefbbe29Seric 					(void) strcat(buf, ".");
228fefbbe29Seric 					(void) strcat(buf, NetName);
229be388505Seric 				}
230be388505Seric 			}
231a44d5a5eSeric 			else
23229dcf4baSeric 			{
23329dcf4baSeric 				extern char *inet_ntoa();
23429dcf4baSeric 
23529dcf4baSeric 				/* produce a dotted quad */
23629dcf4baSeric 				(void) sprintf(buf, "[%s]",
23729dcf4baSeric 					inet_ntoa(otherend.sin_addr));
23829dcf4baSeric 			}
23929dcf4baSeric 
24029dcf4baSeric 			/* should we check for illegal connection here? XXX */
24129dcf4baSeric 
242a44d5a5eSeric 			RealHostName = newstr(buf);
243a44d5a5eSeric 
244b7d7afcbSeric 			(void) close(DaemonSocket);
2451c71e510Seric 			InChannel = fdopen(t, "r");
2461d5bd586Seric 			OutChannel = fdopen(dup(t), "w");
247d0a9e852Seric # ifdef DEBUG
24861e4310fSeric 			if (tTd(15, 2))
249d0a9e852Seric 				printf("getreq: returning\n");
250d0a9e852Seric # endif DEBUG
251252c8a22Seric # ifdef LOG
252252c8a22Seric 			if (LogLevel > 11)
253252c8a22Seric 				syslog(LOG_DEBUG, "connected, pid=%d", getpid());
254252c8a22Seric # endif LOG
255a8268164Seric 			return;
256a8268164Seric 		}
257a8268164Seric 
2583c154354Seric 		/* close the port so that others will hang (for a while) */
2593c154354Seric 		(void) close(t);
2608e3e4b17Seric 	}
2613c154354Seric 	/*NOTREACHED*/
2623c154354Seric }
2638e3e4b17Seric /*
264b7d7afcbSeric **  CLRDAEMON -- reset the daemon connection
265b7d7afcbSeric **
266b7d7afcbSeric **	Parameters:
267b7d7afcbSeric **		none.
268b7d7afcbSeric **
269b7d7afcbSeric **	Returns:
270b7d7afcbSeric **		none.
271b7d7afcbSeric **
272b7d7afcbSeric **	Side Effects:
273b7d7afcbSeric **		releases any resources used by the passive daemon.
274b7d7afcbSeric */
275b7d7afcbSeric 
276b7d7afcbSeric clrdaemon()
277b7d7afcbSeric {
278b7d7afcbSeric 	if (DaemonSocket >= 0)
279b7d7afcbSeric 		(void) close(DaemonSocket);
280b7d7afcbSeric 	DaemonSocket = -1;
281b7d7afcbSeric }
282b7d7afcbSeric /*
2837aa493c5Seric **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
2847aa493c5Seric **
2857aa493c5Seric **	Parameters:
2867aa493c5Seric **		host -- the name of the host.
28748ff0a9dSeric **		port -- the port number to connect to.
2887aa493c5Seric **		outfile -- a pointer to a place to put the outfile
2897aa493c5Seric **			descriptor.
2907aa493c5Seric **		infile -- ditto for infile.
2917aa493c5Seric **
2927aa493c5Seric **	Returns:
2937aa493c5Seric **		An exit code telling whether the connection could be
2947aa493c5Seric **			made and if not why not.
2957aa493c5Seric **
2967aa493c5Seric **	Side Effects:
2977aa493c5Seric **		none.
2987aa493c5Seric */
2997aa493c5Seric 
300794bdbb9Smiriam int	h_errno;	/*this will go away when code implemented*/
301794bdbb9Smiriam 
30248ff0a9dSeric makeconnection(host, port, outfile, infile)
3037aa493c5Seric 	char *host;
304210215eaSeric 	u_short port;
3057aa493c5Seric 	FILE **outfile;
3067aa493c5Seric 	FILE **infile;
3077aa493c5Seric {
30804344589Sbloom 	register int i, s;
30904344589Sbloom 	register struct hostent *hp = (struct hostent *)NULL;
31004344589Sbloom 	extern char *inet_ntoa();
3116286bb75Sbloom 	int sav_errno;
3127aa493c5Seric 
3137aa493c5Seric 	/*
3147aa493c5Seric 	**  Set up the address for the mailer.
31571096d12Seric 	**	Accept "[a.b.c.d]" syntax for host name.
3167aa493c5Seric 	*/
3177aa493c5Seric 
318794bdbb9Smiriam 	h_errno = 0;
319794bdbb9Smiriam 	errno = 0;
320794bdbb9Smiriam 
32171096d12Seric 	if (host[0] == '[')
32271096d12Seric 	{
323a44d5a5eSeric 		long hid;
324a44d5a5eSeric 		register char *p = index(host, ']');
32571096d12Seric 
326a44d5a5eSeric 		if (p != NULL)
32771096d12Seric 		{
328a44d5a5eSeric 			*p = '\0';
329a44d5a5eSeric 			hid = inet_addr(&host[1]);
330a44d5a5eSeric 			*p = ']';
33171096d12Seric 		}
332a44d5a5eSeric 		if (p == NULL || hid == -1)
33371096d12Seric 		{
33471096d12Seric 			usrerr("Invalid numeric domain spec \"%s\"", host);
33571096d12Seric 			return (EX_NOHOST);
33671096d12Seric 		}
33771096d12Seric 		SendmailAddress.sin_addr.s_addr = hid;
33871096d12Seric 	}
3391c71e510Seric 	else
3401c71e510Seric 	{
34104344589Sbloom 		hp = gethostbyname(host);
342794bdbb9Smiriam 		if (hp == NULL)
343794bdbb9Smiriam 		{
344794bdbb9Smiriam 			if (errno == ETIMEDOUT || h_errno == TRY_AGAIN)
34552308a50Seric 				return (EX_TEMPFAIL);
34682e5d8ddSeric 
34782e5d8ddSeric 			/*
34882e5d8ddSeric 			**  XXX Should look for mail forwarder record here
34982e5d8ddSeric 			**  XXX if (h_errno == NO_ADDRESS).
35082e5d8ddSeric 			*/
35182e5d8ddSeric 
3527aa493c5Seric 			return (EX_NOHOST);
353794bdbb9Smiriam 		}
35429dcf4baSeric 		bcopy(hp->h_addr, (char *) &SendmailAddress.sin_addr, hp->h_length);
35504344589Sbloom 		i = 1;
3561c71e510Seric 	}
3571c71e510Seric 
3581c71e510Seric 	/*
3591c71e510Seric 	**  Determine the port number.
3601c71e510Seric 	*/
3611c71e510Seric 
362fd7c0790Seric 	if (port != 0)
363fd7c0790Seric 		SendmailAddress.sin_port = htons(port);
364fd7c0790Seric 	else
3651c71e510Seric 	{
3661c71e510Seric 		register struct servent *sp = getservbyname("smtp", "tcp");
3671c71e510Seric 
3681c71e510Seric 		if (sp == NULL)
3691c71e510Seric 		{
3701c71e510Seric 			syserr("makeconnection: server \"smtp\" unknown");
3711c71e510Seric 			return (EX_OSFILE);
3721c71e510Seric 		}
373fd7c0790Seric 		SendmailAddress.sin_port = sp->s_port;
3741c71e510Seric 	}
3757aa493c5Seric 
3767aa493c5Seric 	/*
3777aa493c5Seric 	**  Try to actually open the connection.
3787aa493c5Seric 	*/
3797aa493c5Seric 
38004344589Sbloom again:
3817aa493c5Seric # ifdef DEBUG
38261e4310fSeric 	if (tTd(16, 1))
38304344589Sbloom 		printf("makeconnection (%s [%s])\n", host,
38404344589Sbloom 		    inet_ntoa(SendmailAddress.sin_addr.s_addr));
3857aa493c5Seric # endif DEBUG
3867aa493c5Seric 
387af5e902cSeric 	s = socket(AF_INET, SOCK_STREAM, 0);
3887aa493c5Seric 	if (s < 0)
3897aa493c5Seric 	{
3907aa493c5Seric 		syserr("makeconnection: no socket");
3916286bb75Sbloom 		sav_errno = errno;
3927aa493c5Seric 		goto failure;
3937aa493c5Seric 	}
3947aa493c5Seric 
3957aa493c5Seric # ifdef DEBUG
39661e4310fSeric 	if (tTd(16, 1))
3977aa493c5Seric 		printf("makeconnection: %d\n", s);
3981b6e4a15Seric 
3991b6e4a15Seric 	/* turn on network debugging? */
4001b6e4a15Seric 	if (tTd(16, 14))
40152308a50Seric 	{
40252308a50Seric 		int on = 1;
40352308a50Seric 		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on);
40452308a50Seric 	}
4057aa493c5Seric # endif DEBUG
406877a6142Seric 	(void) fflush(CurEnv->e_xfp);			/* for debugging */
4074bd6a662Seric 	errno = 0;					/* for debugging */
4081c71e510Seric 	SendmailAddress.sin_family = AF_INET;
409af5e902cSeric 	if (connect(s, &SendmailAddress, sizeof SendmailAddress) < 0)
4107aa493c5Seric 	{
4116286bb75Sbloom 		sav_errno = errno;
4126286bb75Sbloom 		(void) close(s);
41304344589Sbloom 		if (hp && hp->h_addr_list[i])
41404344589Sbloom 		{
41504344589Sbloom 			bcopy(hp->h_addr_list[i++],
41604344589Sbloom 			    (char *)&SendmailAddress.sin_addr, hp->h_length);
41704344589Sbloom 			goto again;
41804344589Sbloom 		}
41904344589Sbloom 
4207aa493c5Seric 		/* failure, decide if temporary or not */
4217aa493c5Seric 	failure:
4226286bb75Sbloom 		switch (sav_errno)
4237aa493c5Seric 		{
4247aa493c5Seric 		  case EISCONN:
4257aa493c5Seric 		  case ETIMEDOUT:
426292668b9Seric 		  case EINPROGRESS:
427292668b9Seric 		  case EALREADY:
428292668b9Seric 		  case EADDRINUSE:
42936bd23a8Seric 		  case EHOSTDOWN:
430292668b9Seric 		  case ENETDOWN:
431292668b9Seric 		  case ENETRESET:
432292668b9Seric 		  case ENOBUFS:
433a1645df8Seric 		  case ECONNREFUSED:
434efe49624Seric 		  case ECONNRESET:
435cae8261aSeric 		  case EHOSTUNREACH:
436dca8e1f7Seric 		  case ENETUNREACH:
4377aa493c5Seric 			/* there are others, I'm sure..... */
4387aa493c5Seric 			return (EX_TEMPFAIL);
4397aa493c5Seric 
440a44d5a5eSeric 		  case EPERM:
441a44d5a5eSeric 			/* why is this happening? */
442a44d5a5eSeric 			syserr("makeconnection: funny failure, addr=%lx, port=%x",
443a44d5a5eSeric 				SendmailAddress.sin_addr.s_addr, SendmailAddress.sin_port);
4444bd6a662Seric 			return (EX_TEMPFAIL);
445a44d5a5eSeric 
4467aa493c5Seric 		  default:
447e8212f61Sbostic 			message(Arpa_Info, "%s", errstring(sav_errno));
4487aa493c5Seric 			return (EX_UNAVAILABLE);
4497aa493c5Seric 		}
4507aa493c5Seric 	}
4517aa493c5Seric 
4527aa493c5Seric 	/* connection ok, put it into canonical form */
4537aa493c5Seric 	*outfile = fdopen(s, "w");
4547aa493c5Seric 	*infile = fdopen(s, "r");
4557aa493c5Seric 
456dca8e1f7Seric 	return (EX_OK);
4577aa493c5Seric }
458444eaf03Seric /*
459444eaf03Seric **  MYHOSTNAME -- return the name of this host.
460444eaf03Seric **
461444eaf03Seric **	Parameters:
462444eaf03Seric **		hostbuf -- a place to return the name of this host.
463897f1869Seric **		size -- the size of hostbuf.
464444eaf03Seric **
465444eaf03Seric **	Returns:
466444eaf03Seric **		A list of aliases for this host.
467444eaf03Seric **
468444eaf03Seric **	Side Effects:
469444eaf03Seric **		none.
470444eaf03Seric */
471444eaf03Seric 
472444eaf03Seric char **
473897f1869Seric myhostname(hostbuf, size)
474444eaf03Seric 	char hostbuf[];
475897f1869Seric 	int size;
476444eaf03Seric {
477444eaf03Seric 	extern struct hostent *gethostbyname();
478a44d5a5eSeric 	struct hostent *hp;
479444eaf03Seric 
480af5e902cSeric 	if (gethostname(hostbuf, size) < 0)
481af5e902cSeric 	{
482af5e902cSeric 		(void) strcpy(hostbuf, "localhost");
483af5e902cSeric 	}
484a44d5a5eSeric 	hp = gethostbyname(hostbuf);
485a44d5a5eSeric 	if (hp != NULL)
4867364df9fSeric 	{
487fefbbe29Seric 		(void) strcpy(hostbuf, hp->h_name);
488a44d5a5eSeric 		return (hp->h_aliases);
4897364df9fSeric 	}
490444eaf03Seric 	else
491444eaf03Seric 		return (NULL);
492444eaf03Seric }
493444eaf03Seric 
494*f36ede03Sbostic /*
495*f36ede03Sbostic  *  MAPHOSTNAME -- turn a hostname into canonical form
496*f36ede03Sbostic  *
497*f36ede03Sbostic  *	Parameters:
498*f36ede03Sbostic  *		hbuf -- a buffer containing a hostname.
499*f36ede03Sbostic  *		hbsize -- the size of hbuf.
500*f36ede03Sbostic  *
501*f36ede03Sbostic  *	Returns:
502*f36ede03Sbostic  *		none.
503*f36ede03Sbostic  *
504*f36ede03Sbostic  *	Side Effects:
505*f36ede03Sbostic  *		Looks up the host specified in hbuf.  If it is not
506*f36ede03Sbostic  *		the canonical name for that host, replace it with
507*f36ede03Sbostic  *		the canonical name.  If the name is unknown, or it
508*f36ede03Sbostic  *		is already the canonical name, leave it unchanged.
509*f36ede03Sbostic  */
51099f7cf32Seric maphostname(hbuf, hbsize)
51199f7cf32Seric 	char *hbuf;
51299f7cf32Seric 	int hbsize;
51399f7cf32Seric {
51499f7cf32Seric 	register struct hostent *hp;
5155f78836eSmiriam 	u_long in_addr;
5165f78836eSmiriam 	char ptr[256];
517*f36ede03Sbostic 	struct hostent *gethostbyaddr();
5185f78836eSmiriam 
519*f36ede03Sbostic 	/*
520*f36ede03Sbostic 	 * If first character is a bracket, then it is an address
521*f36ede03Sbostic 	 * lookup.  Address is copied into a temporary buffer to
522*f36ede03Sbostic 	 * strip the brackets and to preserve hbuf if address is
523*f36ede03Sbostic 	 * unknown.
524*f36ede03Sbostic 	 */
525*f36ede03Sbostic 	if (*hbuf != '[') {
526*f36ede03Sbostic 		getcanonname(hbuf, hbsize);
527*f36ede03Sbostic 		return;
528*f36ede03Sbostic 	}
529*f36ede03Sbostic 	*index(strcpy(ptr, hbuf), ']') = '\0';
5305f78836eSmiriam 	in_addr = inet_addr(&ptr[1]);
53131601fa7Seric 	hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET);
5325f78836eSmiriam 	if (hp == NULL)
53331601fa7Seric 		return;
534*f36ede03Sbostic 	if (strlen(hp->h_name) >= hbsize)
535301df7d6Sbloom 		hp->h_name[hbsize - 1] = '\0';
536fefbbe29Seric 	(void)strcpy(hbuf, hp->h_name);
53799f7cf32Seric }
538*f36ede03Sbostic 
539444eaf03Seric # else DAEMON
54099f7cf32Seric /* code for systems without sophisticated networking */
541444eaf03Seric 
542444eaf03Seric /*
543444eaf03Seric **  MYHOSTNAME -- stub version for case of no daemon code.
54421e9914dSeric **
54521e9914dSeric **	Can't convert to upper case here because might be a UUCP name.
546897f1869Seric **
547897f1869Seric **	Mark, you can change this to be anything you want......
548444eaf03Seric */
549444eaf03Seric 
550444eaf03Seric char **
551897f1869Seric myhostname(hostbuf, size)
552444eaf03Seric 	char hostbuf[];
553897f1869Seric 	int size;
554444eaf03Seric {
555444eaf03Seric 	register FILE *f;
556444eaf03Seric 
557444eaf03Seric 	hostbuf[0] = '\0';
558444eaf03Seric 	f = fopen("/usr/include/whoami", "r");
559444eaf03Seric 	if (f != NULL)
560444eaf03Seric 	{
561897f1869Seric 		(void) fgets(hostbuf, size, f);
562444eaf03Seric 		fixcrlf(hostbuf, TRUE);
563444eaf03Seric 		(void) fclose(f);
564444eaf03Seric 	}
565444eaf03Seric 	return (NULL);
566444eaf03Seric }
56799f7cf32Seric /*
56899f7cf32Seric **  MAPHOSTNAME -- turn a hostname into canonical form
56999f7cf32Seric **
57099f7cf32Seric **	Parameters:
57199f7cf32Seric **		hbuf -- a buffer containing a hostname.
57299f7cf32Seric **		hbsize -- the size of hbuf.
57399f7cf32Seric **
57499f7cf32Seric **	Returns:
57599f7cf32Seric **		none.
57699f7cf32Seric **
57799f7cf32Seric **	Side Effects:
57899f7cf32Seric **		Looks up the host specified in hbuf.  If it is not
57999f7cf32Seric **		the canonical name for that host, replace it with
58099f7cf32Seric **		the canonical name.  If the name is unknown, or it
58199f7cf32Seric **		is already the canonical name, leave it unchanged.
58299f7cf32Seric */
58399f7cf32Seric 
58499f7cf32Seric /*ARGSUSED*/
58599f7cf32Seric maphostname(hbuf, hbsize)
58699f7cf32Seric 	char *hbuf;
58799f7cf32Seric 	int hbsize;
58899f7cf32Seric {
58999f7cf32Seric 	return;
59099f7cf32Seric }
59199f7cf32Seric 
592d0a9e852Seric #endif DAEMON
593