xref: /original-bsd/usr.sbin/sendmail/src/daemon.c (revision 3bc94712)
1939f5b94Sdist /*
20942ea6aSbostic  * Copyright (c) 1983 Eric P. Allman
3da1c6175Sbostic  * Copyright (c) 1988 Regents of the University of California.
4da1c6175Sbostic  * All rights reserved.
5da1c6175Sbostic  *
6*3bc94712Sbostic  * %sccs.include.redist.c%
7939f5b94Sdist  */
8939f5b94Sdist 
97aa493c5Seric #include <errno.h>
106c05f684Sbostic #include "sendmail.h"
117fa39d90Seric 
12af5e902cSeric #ifndef lint
13da1c6175Sbostic #ifdef DAEMON
14*3bc94712Sbostic static char sccsid[] = "@(#)daemon.c	5.36 (Berkeley) 06/01/90 (with daemon mode)";
15d0a9e852Seric #else
16*3bc94712Sbostic static char sccsid[] = "@(#)daemon.c	5.36 (Berkeley) 06/01/90 (without daemon mode)";
17da1c6175Sbostic #endif
18da1c6175Sbostic #endif /* not lint */
19da1c6175Sbostic 
2087d6e633Srick int la;	/* load average */
2187d6e633Srick 
22da1c6175Sbostic #ifdef DAEMON
23d0a9e852Seric 
241c71e510Seric # include <netdb.h>
2552308a50Seric # include <sys/signal.h>
261e1663f7Swnj # include <sys/wait.h>
27af5e902cSeric # include <sys/time.h>
28af5e902cSeric # include <sys/resource.h>
29d0a9e852Seric 
307fa39d90Seric /*
317fa39d90Seric **  DAEMON.C -- routines to use when running as a daemon.
3247b12ae1Seric **
3347b12ae1Seric **	This entire file is highly dependent on the 4.2 BSD
3447b12ae1Seric **	interprocess communication primitives.  No attempt has
3547b12ae1Seric **	been made to make this file portable to Version 7,
3647b12ae1Seric **	Version 6, MPX files, etc.  If you should try such a
3747b12ae1Seric **	thing yourself, I recommend chucking the entire file
3847b12ae1Seric **	and starting from scratch.  Basic semantics are:
3947b12ae1Seric **
4047b12ae1Seric **	getrequests()
4147b12ae1Seric **		Opens a port and initiates a connection.
4247b12ae1Seric **		Returns in a child.  Must set InChannel and
4347b12ae1Seric **		OutChannel appropriately.
44b7d7afcbSeric **	clrdaemon()
45b7d7afcbSeric **		Close any open files associated with getting
46b7d7afcbSeric **		the connection; this is used when running the queue,
47b7d7afcbSeric **		etc., to avoid having extra file descriptors during
48b7d7afcbSeric **		the queue run and to avoid confusing the network
49b7d7afcbSeric **		code (if it cares).
5047b12ae1Seric **	makeconnection(host, port, outfile, infile)
5147b12ae1Seric **		Make a connection to the named host on the given
5247b12ae1Seric **		port.  Set *outfile and *infile to the files
5347b12ae1Seric **		appropriate for communication.  Returns zero on
5447b12ae1Seric **		success, else an exit status describing the
5547b12ae1Seric **		error.
568e8f00b5Seric **	maphostname(hbuf, hbufsize)
578e8f00b5Seric **		Convert the entry in hbuf into a canonical form.  It
588e8f00b5Seric **		may not be larger than hbufsize.
597fa39d90Seric */
607fa39d90Seric /*
617fa39d90Seric **  GETREQUESTS -- open mail IPC port and get requests.
627fa39d90Seric **
637fa39d90Seric **	Parameters:
647fa39d90Seric **		none.
657fa39d90Seric **
667fa39d90Seric **	Returns:
677fa39d90Seric **		none.
687fa39d90Seric **
697fa39d90Seric **	Side Effects:
707fa39d90Seric **		Waits until some interesting activity occurs.  When
717fa39d90Seric **		it does, a child is created to process it, and the
727fa39d90Seric **		parent waits for completion.  Return from this
73147303b1Seric **		routine is always in the child.  The file pointers
74147303b1Seric **		"InChannel" and "OutChannel" should be set to point
75147303b1Seric **		to the communication channel.
767fa39d90Seric */
777fa39d90Seric 
78b7d7afcbSeric struct sockaddr_in	SendmailAddress;/* internet address of sendmail */
799478786eSeric 
80b7d7afcbSeric int	DaemonSocket	= -1;		/* fd describing socket */
81be388505Seric char	*NetName;			/* name of home (local?) network */
821c71e510Seric 
837fa39d90Seric getrequests()
847fa39d90Seric {
851c71e510Seric 	int t;
861c71e510Seric 	register struct servent *sp;
877868dfc2Seric 	int on = 1;
8852308a50Seric 	extern reapchild();
89eb889047Seric 
90a8268164Seric 	/*
911c71e510Seric 	**  Set up the address for the mailer.
92eb889047Seric 	*/
93eb889047Seric 
941c71e510Seric 	sp = getservbyname("smtp", "tcp");
951c71e510Seric 	if (sp == NULL)
96d0a9e852Seric 	{
971c71e510Seric 		syserr("server \"smtp\" unknown");
98a1961f2aSeric 		goto severe;
991c71e510Seric 	}
1001c71e510Seric 	SendmailAddress.sin_family = AF_INET;
1011c71e510Seric 	SendmailAddress.sin_addr.s_addr = INADDR_ANY;
102f43fa3c2Ssam 	SendmailAddress.sin_port = sp->s_port;
1031c71e510Seric 
1041c71e510Seric 	/*
1051c71e510Seric 	**  Try to actually open the connection.
1061c71e510Seric 	*/
1071c71e510Seric 
1081c71e510Seric 	if (tTd(15, 1))
1091c71e510Seric 		printf("getrequests: port 0x%x\n", SendmailAddress.sin_port);
1101c71e510Seric 
1111c71e510Seric 	/* get a socket for the SMTP connection */
112af5e902cSeric 	DaemonSocket = socket(AF_INET, SOCK_STREAM, 0);
113b7d7afcbSeric 	if (DaemonSocket < 0)
1141c71e510Seric 	{
1151c71e510Seric 		/* probably another daemon already */
1161c71e510Seric 		syserr("getrequests: can't create socket");
1171c71e510Seric 	  severe:
118b0ba8827Seric # ifdef LOG
119b0ba8827Seric 		if (LogLevel > 0)
120d2aa4720Seric 			syslog(LOG_ALERT, "cannot get connection");
121b0ba8827Seric # endif LOG
12247b12ae1Seric 		finis();
123d0a9e852Seric 	}
1241b6e4a15Seric 
1251b6e4a15Seric 	/* turn on network debugging? */
1261b6e4a15Seric 	if (tTd(15, 15))
12752308a50Seric 		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on);
1281b6e4a15Seric 
1297868dfc2Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on);
1307868dfc2Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on);
1317868dfc2Seric 
132af5e902cSeric 	if (bind(DaemonSocket, &SendmailAddress, sizeof SendmailAddress) < 0)
1331c71e510Seric 	{
1341c71e510Seric 		syserr("getrequests: cannot bind");
135b7d7afcbSeric 		(void) close(DaemonSocket);
1361c71e510Seric 		goto severe;
1371c71e510Seric 	}
138af5e902cSeric 	if (listen(DaemonSocket, 10) < 0)
139af5e902cSeric 	{
140af5e902cSeric 		syserr("getrequests: cannot listen");
141af5e902cSeric 		(void) close(DaemonSocket);
142af5e902cSeric 		goto severe;
143af5e902cSeric 	}
1441c71e510Seric 
1451cd247eeSeric 	(void) signal(SIGCHLD, reapchild);
14652308a50Seric 
1471c71e510Seric 	if (tTd(15, 1))
148b7d7afcbSeric 		printf("getrequests: %d\n", DaemonSocket);
1491c71e510Seric 
1501c71e510Seric 	for (;;)
1511c71e510Seric 	{
1523a099713Seric 		register int pid;
153a44d5a5eSeric 		auto int lotherend;
1543a099713Seric 		extern int RefuseLA;
1553a099713Seric 
1563a099713Seric 		/* see if we are rejecting connections */
1576775ec03Sbostic 		while ((la = getla()) > RefuseLA)
1586775ec03Sbostic 		{
159096bebd2Smckusick 			setproctitle("rejecting connections: load average: %.2f", (double)la);
1603a099713Seric 			sleep(5);
1616775ec03Sbostic 		}
162a44d5a5eSeric 
1631c71e510Seric 		/* wait for a connection */
1646775ec03Sbostic 		setproctitle("accepting connections");
1651c71e510Seric 		do
1661c71e510Seric 		{
1671c71e510Seric 			errno = 0;
1689f9a15b6Skarels 			lotherend = sizeof RealHostAddr;
1699f9a15b6Skarels 			t = accept(DaemonSocket, &RealHostAddr, &lotherend);
1701c71e510Seric 		} while (t < 0 && errno == EINTR);
1711c71e510Seric 		if (t < 0)
1721c71e510Seric 		{
1731c71e510Seric 			syserr("getrequests: accept");
1741c71e510Seric 			sleep(5);
1751c71e510Seric 			continue;
1761c71e510Seric 		}
177d0a9e852Seric 
178d0a9e852Seric 		/*
179d0a9e852Seric 		**  Create a subprocess to process the mail.
180d0a9e852Seric 		*/
181d0a9e852Seric 
18261e4310fSeric 		if (tTd(15, 2))
1831c71e510Seric 			printf("getrequests: forking (fd = %d)\n", t);
184eb889047Seric 
185a8268164Seric 		pid = fork();
186a8268164Seric 		if (pid < 0)
187a8268164Seric 		{
188a8268164Seric 			syserr("daemon: cannot fork");
189a8268164Seric 			sleep(10);
1901c71e510Seric 			(void) close(t);
191a8268164Seric 			continue;
192a8268164Seric 		}
193a8268164Seric 
194a8268164Seric 		if (pid == 0)
195a8268164Seric 		{
196a44d5a5eSeric 			extern struct hostent *gethostbyaddr();
197a44d5a5eSeric 			register struct hostent *hp;
198a44d5a5eSeric 			char buf[MAXNAME];
199a44d5a5eSeric 
200a8268164Seric 			/*
201a8268164Seric 			**  CHILD -- return to caller.
202a44d5a5eSeric 			**	Collect verified idea of sending host.
203a8268164Seric 			**	Verify calling user id if possible here.
204a8268164Seric 			*/
205a8268164Seric 
2061cd247eeSeric 			(void) signal(SIGCHLD, SIG_DFL);
207779ac194Seric 
208a44d5a5eSeric 			/* determine host name */
2099f9a15b6Skarels 			hp = gethostbyaddr((char *) &RealHostAddr.sin_addr, sizeof RealHostAddr.sin_addr, AF_INET);
210a44d5a5eSeric 			if (hp != NULL)
211fefbbe29Seric 				(void) strcpy(buf, hp->h_name);
212a44d5a5eSeric 			else
21329dcf4baSeric 			{
21429dcf4baSeric 				extern char *inet_ntoa();
21529dcf4baSeric 
21629dcf4baSeric 				/* produce a dotted quad */
21729dcf4baSeric 				(void) sprintf(buf, "[%s]",
2189f9a15b6Skarels 					inet_ntoa(RealHostAddr.sin_addr));
21929dcf4baSeric 			}
22029dcf4baSeric 
22129dcf4baSeric 			/* should we check for illegal connection here? XXX */
22229dcf4baSeric 
223a44d5a5eSeric 			RealHostName = newstr(buf);
224a44d5a5eSeric 
225b7d7afcbSeric 			(void) close(DaemonSocket);
2261c71e510Seric 			InChannel = fdopen(t, "r");
2271d5bd586Seric 			OutChannel = fdopen(dup(t), "w");
22861e4310fSeric 			if (tTd(15, 2))
229d0a9e852Seric 				printf("getreq: returning\n");
230252c8a22Seric # ifdef LOG
231252c8a22Seric 			if (LogLevel > 11)
232252c8a22Seric 				syslog(LOG_DEBUG, "connected, pid=%d", getpid());
233252c8a22Seric # endif LOG
234a8268164Seric 			return;
235a8268164Seric 		}
236a8268164Seric 
2373c154354Seric 		/* close the port so that others will hang (for a while) */
2383c154354Seric 		(void) close(t);
2398e3e4b17Seric 	}
2403c154354Seric 	/*NOTREACHED*/
2413c154354Seric }
2428e3e4b17Seric /*
243b7d7afcbSeric **  CLRDAEMON -- reset the daemon connection
244b7d7afcbSeric **
245b7d7afcbSeric **	Parameters:
246b7d7afcbSeric **		none.
247b7d7afcbSeric **
248b7d7afcbSeric **	Returns:
249b7d7afcbSeric **		none.
250b7d7afcbSeric **
251b7d7afcbSeric **	Side Effects:
252b7d7afcbSeric **		releases any resources used by the passive daemon.
253b7d7afcbSeric */
254b7d7afcbSeric 
255b7d7afcbSeric clrdaemon()
256b7d7afcbSeric {
257b7d7afcbSeric 	if (DaemonSocket >= 0)
258b7d7afcbSeric 		(void) close(DaemonSocket);
259b7d7afcbSeric 	DaemonSocket = -1;
260b7d7afcbSeric }
261b7d7afcbSeric /*
2627aa493c5Seric **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
2637aa493c5Seric **
2647aa493c5Seric **	Parameters:
2657aa493c5Seric **		host -- the name of the host.
26648ff0a9dSeric **		port -- the port number to connect to.
2677aa493c5Seric **		outfile -- a pointer to a place to put the outfile
2687aa493c5Seric **			descriptor.
2697aa493c5Seric **		infile -- ditto for infile.
2707aa493c5Seric **
2717aa493c5Seric **	Returns:
2727aa493c5Seric **		An exit code telling whether the connection could be
2737aa493c5Seric **			made and if not why not.
2747aa493c5Seric **
2757aa493c5Seric **	Side Effects:
2767aa493c5Seric **		none.
2777aa493c5Seric */
2787aa493c5Seric 
27948ff0a9dSeric makeconnection(host, port, outfile, infile)
2807aa493c5Seric 	char *host;
281210215eaSeric 	u_short port;
2827aa493c5Seric 	FILE **outfile;
2837aa493c5Seric 	FILE **infile;
2847aa493c5Seric {
28504344589Sbloom 	register int i, s;
28604344589Sbloom 	register struct hostent *hp = (struct hostent *)NULL;
28704344589Sbloom 	extern char *inet_ntoa();
2886286bb75Sbloom 	int sav_errno;
289134746fbSeric #ifdef NAMED_BIND
290134746fbSeric 	extern int h_errno;
291134746fbSeric #endif
2927aa493c5Seric 
2937aa493c5Seric 	/*
2947aa493c5Seric 	**  Set up the address for the mailer.
29571096d12Seric 	**	Accept "[a.b.c.d]" syntax for host name.
2967aa493c5Seric 	*/
2977aa493c5Seric 
298134746fbSeric #ifdef NAMED_BIND
299794bdbb9Smiriam 	h_errno = 0;
300134746fbSeric #endif
301794bdbb9Smiriam 	errno = 0;
302794bdbb9Smiriam 
30371096d12Seric 	if (host[0] == '[')
30471096d12Seric 	{
305a44d5a5eSeric 		long hid;
306a44d5a5eSeric 		register char *p = index(host, ']');
30771096d12Seric 
308a44d5a5eSeric 		if (p != NULL)
30971096d12Seric 		{
310a44d5a5eSeric 			*p = '\0';
311a44d5a5eSeric 			hid = inet_addr(&host[1]);
312a44d5a5eSeric 			*p = ']';
31371096d12Seric 		}
314a44d5a5eSeric 		if (p == NULL || hid == -1)
31571096d12Seric 		{
31671096d12Seric 			usrerr("Invalid numeric domain spec \"%s\"", host);
31771096d12Seric 			return (EX_NOHOST);
31871096d12Seric 		}
31971096d12Seric 		SendmailAddress.sin_addr.s_addr = hid;
32071096d12Seric 	}
3211c71e510Seric 	else
3221c71e510Seric 	{
32304344589Sbloom 		hp = gethostbyname(host);
324794bdbb9Smiriam 		if (hp == NULL)
325794bdbb9Smiriam 		{
326134746fbSeric #ifdef NAMED_BIND
327794bdbb9Smiriam 			if (errno == ETIMEDOUT || h_errno == TRY_AGAIN)
32852308a50Seric 				return (EX_TEMPFAIL);
32982e5d8ddSeric 
330134746fbSeric 			/* if name server is specified, assume temp fail */
331134746fbSeric 			if (errno == ECONNREFUSED && UseNameServer)
332134746fbSeric 				return (EX_TEMPFAIL);
333134746fbSeric #endif
334134746fbSeric 
33582e5d8ddSeric 			/*
33682e5d8ddSeric 			**  XXX Should look for mail forwarder record here
33782e5d8ddSeric 			**  XXX if (h_errno == NO_ADDRESS).
33882e5d8ddSeric 			*/
33982e5d8ddSeric 
3407aa493c5Seric 			return (EX_NOHOST);
341794bdbb9Smiriam 		}
34229dcf4baSeric 		bcopy(hp->h_addr, (char *) &SendmailAddress.sin_addr, hp->h_length);
34304344589Sbloom 		i = 1;
3441c71e510Seric 	}
3451c71e510Seric 
3461c71e510Seric 	/*
3471c71e510Seric 	**  Determine the port number.
3481c71e510Seric 	*/
3491c71e510Seric 
350fd7c0790Seric 	if (port != 0)
351fd7c0790Seric 		SendmailAddress.sin_port = htons(port);
352fd7c0790Seric 	else
3531c71e510Seric 	{
3541c71e510Seric 		register struct servent *sp = getservbyname("smtp", "tcp");
3551c71e510Seric 
3561c71e510Seric 		if (sp == NULL)
3571c71e510Seric 		{
3581c71e510Seric 			syserr("makeconnection: server \"smtp\" unknown");
3591c71e510Seric 			return (EX_OSFILE);
3601c71e510Seric 		}
361fd7c0790Seric 		SendmailAddress.sin_port = sp->s_port;
3621c71e510Seric 	}
3637aa493c5Seric 
3647aa493c5Seric 	/*
3657aa493c5Seric 	**  Try to actually open the connection.
3667aa493c5Seric 	*/
3677aa493c5Seric 
36804344589Sbloom again:
36961e4310fSeric 	if (tTd(16, 1))
37004344589Sbloom 		printf("makeconnection (%s [%s])\n", host,
37104344589Sbloom 		    inet_ntoa(SendmailAddress.sin_addr.s_addr));
3727aa493c5Seric 
373af5e902cSeric 	s = socket(AF_INET, SOCK_STREAM, 0);
3747aa493c5Seric 	if (s < 0)
3757aa493c5Seric 	{
3767aa493c5Seric 		syserr("makeconnection: no socket");
3776286bb75Sbloom 		sav_errno = errno;
3787aa493c5Seric 		goto failure;
3797aa493c5Seric 	}
3807aa493c5Seric 
38161e4310fSeric 	if (tTd(16, 1))
3827aa493c5Seric 		printf("makeconnection: %d\n", s);
3831b6e4a15Seric 
3841b6e4a15Seric 	/* turn on network debugging? */
3851b6e4a15Seric 	if (tTd(16, 14))
38652308a50Seric 	{
38752308a50Seric 		int on = 1;
38852308a50Seric 		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on);
38952308a50Seric 	}
39087d6e633Srick 	if (CurEnv->e_xfp != NULL)
391877a6142Seric 		(void) fflush(CurEnv->e_xfp);		/* for debugging */
3924bd6a662Seric 	errno = 0;					/* for debugging */
3931c71e510Seric 	SendmailAddress.sin_family = AF_INET;
394af5e902cSeric 	if (connect(s, &SendmailAddress, sizeof SendmailAddress) < 0)
3957aa493c5Seric 	{
3966286bb75Sbloom 		sav_errno = errno;
3976286bb75Sbloom 		(void) close(s);
39804344589Sbloom 		if (hp && hp->h_addr_list[i])
39904344589Sbloom 		{
40004344589Sbloom 			bcopy(hp->h_addr_list[i++],
40104344589Sbloom 			    (char *)&SendmailAddress.sin_addr, hp->h_length);
40204344589Sbloom 			goto again;
40304344589Sbloom 		}
40404344589Sbloom 
4057aa493c5Seric 		/* failure, decide if temporary or not */
4067aa493c5Seric 	failure:
4076286bb75Sbloom 		switch (sav_errno)
4087aa493c5Seric 		{
4097aa493c5Seric 		  case EISCONN:
4107aa493c5Seric 		  case ETIMEDOUT:
411292668b9Seric 		  case EINPROGRESS:
412292668b9Seric 		  case EALREADY:
413292668b9Seric 		  case EADDRINUSE:
41436bd23a8Seric 		  case EHOSTDOWN:
415292668b9Seric 		  case ENETDOWN:
416292668b9Seric 		  case ENETRESET:
417292668b9Seric 		  case ENOBUFS:
418a1645df8Seric 		  case ECONNREFUSED:
419efe49624Seric 		  case ECONNRESET:
420cae8261aSeric 		  case EHOSTUNREACH:
421dca8e1f7Seric 		  case ENETUNREACH:
4227aa493c5Seric 			/* there are others, I'm sure..... */
4237aa493c5Seric 			return (EX_TEMPFAIL);
4247aa493c5Seric 
425a44d5a5eSeric 		  case EPERM:
426a44d5a5eSeric 			/* why is this happening? */
427a44d5a5eSeric 			syserr("makeconnection: funny failure, addr=%lx, port=%x",
428a44d5a5eSeric 				SendmailAddress.sin_addr.s_addr, SendmailAddress.sin_port);
4294bd6a662Seric 			return (EX_TEMPFAIL);
430a44d5a5eSeric 
4317aa493c5Seric 		  default:
43287d6e633Srick 			{
43387d6e633Srick 				extern char *errstring();
43487d6e633Srick 
435e8212f61Sbostic 				message(Arpa_Info, "%s", errstring(sav_errno));
4367aa493c5Seric 				return (EX_UNAVAILABLE);
4377aa493c5Seric 			}
4387aa493c5Seric 		}
43987d6e633Srick 	}
4407aa493c5Seric 
4417aa493c5Seric 	/* connection ok, put it into canonical form */
4427aa493c5Seric 	*outfile = fdopen(s, "w");
4434d7d1e20Sbostic 	*infile = fdopen(dup(s), "r");
4447aa493c5Seric 
445dca8e1f7Seric 	return (EX_OK);
4467aa493c5Seric }
447444eaf03Seric /*
448444eaf03Seric **  MYHOSTNAME -- return the name of this host.
449444eaf03Seric **
450444eaf03Seric **	Parameters:
451444eaf03Seric **		hostbuf -- a place to return the name of this host.
452897f1869Seric **		size -- the size of hostbuf.
453444eaf03Seric **
454444eaf03Seric **	Returns:
455444eaf03Seric **		A list of aliases for this host.
456444eaf03Seric **
457444eaf03Seric **	Side Effects:
458444eaf03Seric **		none.
459444eaf03Seric */
460444eaf03Seric 
461444eaf03Seric char **
462897f1869Seric myhostname(hostbuf, size)
463444eaf03Seric 	char hostbuf[];
464897f1869Seric 	int size;
465444eaf03Seric {
466444eaf03Seric 	extern struct hostent *gethostbyname();
467a44d5a5eSeric 	struct hostent *hp;
468444eaf03Seric 
469af5e902cSeric 	if (gethostname(hostbuf, size) < 0)
470af5e902cSeric 	{
471af5e902cSeric 		(void) strcpy(hostbuf, "localhost");
472af5e902cSeric 	}
473a44d5a5eSeric 	hp = gethostbyname(hostbuf);
474a44d5a5eSeric 	if (hp != NULL)
4757364df9fSeric 	{
476fefbbe29Seric 		(void) strcpy(hostbuf, hp->h_name);
477a44d5a5eSeric 		return (hp->h_aliases);
4787364df9fSeric 	}
479444eaf03Seric 	else
480444eaf03Seric 		return (NULL);
481444eaf03Seric }
482444eaf03Seric 
483f36ede03Sbostic /*
484f36ede03Sbostic  *  MAPHOSTNAME -- turn a hostname into canonical form
485f36ede03Sbostic  *
486f36ede03Sbostic  *	Parameters:
487f36ede03Sbostic  *		hbuf -- a buffer containing a hostname.
488f36ede03Sbostic  *		hbsize -- the size of hbuf.
489f36ede03Sbostic  *
490f36ede03Sbostic  *	Returns:
491f36ede03Sbostic  *		none.
492f36ede03Sbostic  *
493f36ede03Sbostic  *	Side Effects:
494f36ede03Sbostic  *		Looks up the host specified in hbuf.  If it is not
495f36ede03Sbostic  *		the canonical name for that host, replace it with
496f36ede03Sbostic  *		the canonical name.  If the name is unknown, or it
497f36ede03Sbostic  *		is already the canonical name, leave it unchanged.
498f36ede03Sbostic  */
49999f7cf32Seric maphostname(hbuf, hbsize)
50099f7cf32Seric 	char *hbuf;
50199f7cf32Seric 	int hbsize;
50299f7cf32Seric {
50399f7cf32Seric 	register struct hostent *hp;
5045f78836eSmiriam 	u_long in_addr;
50534e39927Sbostic 	char ptr[256], *cp;
506f36ede03Sbostic 	struct hostent *gethostbyaddr();
5075f78836eSmiriam 
508f36ede03Sbostic 	/*
509f36ede03Sbostic 	 * If first character is a bracket, then it is an address
510f36ede03Sbostic 	 * lookup.  Address is copied into a temporary buffer to
511f36ede03Sbostic 	 * strip the brackets and to preserve hbuf if address is
512f36ede03Sbostic 	 * unknown.
513f36ede03Sbostic 	 */
514f36ede03Sbostic 	if (*hbuf != '[') {
515f36ede03Sbostic 		getcanonname(hbuf, hbsize);
516f36ede03Sbostic 		return;
517f36ede03Sbostic 	}
51834e39927Sbostic 	if ((cp = index(strcpy(ptr, hbuf), ']')) == NULL)
51934e39927Sbostic 		return;
52034e39927Sbostic 	*cp = '\0';
5215f78836eSmiriam 	in_addr = inet_addr(&ptr[1]);
52231601fa7Seric 	hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET);
5235f78836eSmiriam 	if (hp == NULL)
52431601fa7Seric 		return;
525f36ede03Sbostic 	if (strlen(hp->h_name) >= hbsize)
526301df7d6Sbloom 		hp->h_name[hbsize - 1] = '\0';
527fefbbe29Seric 	(void)strcpy(hbuf, hp->h_name);
52899f7cf32Seric }
529f36ede03Sbostic 
530444eaf03Seric # else DAEMON
53199f7cf32Seric /* code for systems without sophisticated networking */
532444eaf03Seric 
533444eaf03Seric /*
534444eaf03Seric **  MYHOSTNAME -- stub version for case of no daemon code.
53521e9914dSeric **
53621e9914dSeric **	Can't convert to upper case here because might be a UUCP name.
537897f1869Seric **
538897f1869Seric **	Mark, you can change this to be anything you want......
539444eaf03Seric */
540444eaf03Seric 
541444eaf03Seric char **
542897f1869Seric myhostname(hostbuf, size)
543444eaf03Seric 	char hostbuf[];
544897f1869Seric 	int size;
545444eaf03Seric {
546444eaf03Seric 	register FILE *f;
547444eaf03Seric 
548444eaf03Seric 	hostbuf[0] = '\0';
549444eaf03Seric 	f = fopen("/usr/include/whoami", "r");
550444eaf03Seric 	if (f != NULL)
551444eaf03Seric 	{
552897f1869Seric 		(void) fgets(hostbuf, size, f);
553444eaf03Seric 		fixcrlf(hostbuf, TRUE);
554444eaf03Seric 		(void) fclose(f);
555444eaf03Seric 	}
556444eaf03Seric 	return (NULL);
557444eaf03Seric }
55899f7cf32Seric /*
55999f7cf32Seric **  MAPHOSTNAME -- turn a hostname into canonical form
56099f7cf32Seric **
56199f7cf32Seric **	Parameters:
56299f7cf32Seric **		hbuf -- a buffer containing a hostname.
56399f7cf32Seric **		hbsize -- the size of hbuf.
56499f7cf32Seric **
56599f7cf32Seric **	Returns:
56699f7cf32Seric **		none.
56799f7cf32Seric **
56899f7cf32Seric **	Side Effects:
56999f7cf32Seric **		Looks up the host specified in hbuf.  If it is not
57099f7cf32Seric **		the canonical name for that host, replace it with
57199f7cf32Seric **		the canonical name.  If the name is unknown, or it
57299f7cf32Seric **		is already the canonical name, leave it unchanged.
57399f7cf32Seric */
57499f7cf32Seric 
57599f7cf32Seric /*ARGSUSED*/
57699f7cf32Seric maphostname(hbuf, hbsize)
57799f7cf32Seric 	char *hbuf;
57899f7cf32Seric 	int hbsize;
57999f7cf32Seric {
58099f7cf32Seric 	return;
58199f7cf32Seric }
58299f7cf32Seric 
583d0a9e852Seric #endif DAEMON
584