xref: /original-bsd/usr.sbin/sendmail/src/daemon.c (revision 6286bb75)
1939f5b94Sdist /*
2939f5b94Sdist **  Sendmail
3939f5b94Sdist **  Copyright (c) 1983  Eric P. Allman
4939f5b94Sdist **  Berkeley, California
5939f5b94Sdist **
6939f5b94Sdist **  Copyright (c) 1983 Regents of the University of California.
7939f5b94Sdist **  All rights reserved.  The Berkeley software License Agreement
8939f5b94Sdist **  specifies the terms and conditions for redistribution.
9939f5b94Sdist */
10939f5b94Sdist 
11939f5b94Sdist 
127aa493c5Seric # include <errno.h>
137fa39d90Seric # include "sendmail.h"
147fa39d90Seric 
15d0a9e852Seric # ifndef DAEMON
16af5e902cSeric # ifndef lint
17*6286bb75Sbloom static char	SccsId[] = "@(#)daemon.c	5.19 (Berkeley) 05/06/86	(w/o daemon mode)";
18af5e902cSeric # endif not lint
19d0a9e852Seric # else
20d0a9e852Seric 
211c71e510Seric # include <netdb.h>
2252308a50Seric # include <sys/signal.h>
231e1663f7Swnj # include <sys/wait.h>
24af5e902cSeric # include <sys/time.h>
25af5e902cSeric # include <sys/resource.h>
26d0a9e852Seric 
27af5e902cSeric # ifndef lint
28*6286bb75Sbloom static char	SccsId[] = "@(#)daemon.c	5.19 (Berkeley) 05/06/86 (with daemon mode)";
29af5e902cSeric # endif not lint
307fa39d90Seric 
317fa39d90Seric /*
327fa39d90Seric **  DAEMON.C -- routines to use when running as a daemon.
3347b12ae1Seric **
3447b12ae1Seric **	This entire file is highly dependent on the 4.2 BSD
3547b12ae1Seric **	interprocess communication primitives.  No attempt has
3647b12ae1Seric **	been made to make this file portable to Version 7,
3747b12ae1Seric **	Version 6, MPX files, etc.  If you should try such a
3847b12ae1Seric **	thing yourself, I recommend chucking the entire file
3947b12ae1Seric **	and starting from scratch.  Basic semantics are:
4047b12ae1Seric **
4147b12ae1Seric **	getrequests()
4247b12ae1Seric **		Opens a port and initiates a connection.
4347b12ae1Seric **		Returns in a child.  Must set InChannel and
4447b12ae1Seric **		OutChannel appropriately.
45b7d7afcbSeric **	clrdaemon()
46b7d7afcbSeric **		Close any open files associated with getting
47b7d7afcbSeric **		the connection; this is used when running the queue,
48b7d7afcbSeric **		etc., to avoid having extra file descriptors during
49b7d7afcbSeric **		the queue run and to avoid confusing the network
50b7d7afcbSeric **		code (if it cares).
5147b12ae1Seric **	makeconnection(host, port, outfile, infile)
5247b12ae1Seric **		Make a connection to the named host on the given
5347b12ae1Seric **		port.  Set *outfile and *infile to the files
5447b12ae1Seric **		appropriate for communication.  Returns zero on
5547b12ae1Seric **		success, else an exit status describing the
5647b12ae1Seric **		error.
578e8f00b5Seric **	maphostname(hbuf, hbufsize)
588e8f00b5Seric **		Convert the entry in hbuf into a canonical form.  It
598e8f00b5Seric **		may not be larger than hbufsize.
607fa39d90Seric */
617fa39d90Seric /*
627fa39d90Seric **  GETREQUESTS -- open mail IPC port and get requests.
637fa39d90Seric **
647fa39d90Seric **	Parameters:
657fa39d90Seric **		none.
667fa39d90Seric **
677fa39d90Seric **	Returns:
687fa39d90Seric **		none.
697fa39d90Seric **
707fa39d90Seric **	Side Effects:
717fa39d90Seric **		Waits until some interesting activity occurs.  When
727fa39d90Seric **		it does, a child is created to process it, and the
737fa39d90Seric **		parent waits for completion.  Return from this
74147303b1Seric **		routine is always in the child.  The file pointers
75147303b1Seric **		"InChannel" and "OutChannel" should be set to point
76147303b1Seric **		to the communication channel.
777fa39d90Seric */
787fa39d90Seric 
79b7d7afcbSeric struct sockaddr_in	SendmailAddress;/* internet address of sendmail */
809478786eSeric 
81b7d7afcbSeric int	DaemonSocket	= -1;		/* fd describing socket */
82be388505Seric char	*NetName;			/* name of home (local?) network */
831c71e510Seric 
847fa39d90Seric getrequests()
857fa39d90Seric {
861c71e510Seric 	int t;
871c71e510Seric 	register struct servent *sp;
887868dfc2Seric 	int on = 1;
8952308a50Seric 	extern reapchild();
90eb889047Seric 
91a8268164Seric 	/*
921c71e510Seric 	**  Set up the address for the mailer.
93eb889047Seric 	*/
94eb889047Seric 
951c71e510Seric 	sp = getservbyname("smtp", "tcp");
961c71e510Seric 	if (sp == NULL)
97d0a9e852Seric 	{
981c71e510Seric 		syserr("server \"smtp\" unknown");
99a1961f2aSeric 		goto severe;
1001c71e510Seric 	}
1011c71e510Seric 	SendmailAddress.sin_family = AF_INET;
1021c71e510Seric 	SendmailAddress.sin_addr.s_addr = INADDR_ANY;
103f43fa3c2Ssam 	SendmailAddress.sin_port = sp->s_port;
1041c71e510Seric 
1051c71e510Seric 	/*
1061c71e510Seric 	**  Try to actually open the connection.
1071c71e510Seric 	*/
1081c71e510Seric 
1091c71e510Seric # ifdef DEBUG
1101c71e510Seric 	if (tTd(15, 1))
1111c71e510Seric 		printf("getrequests: port 0x%x\n", SendmailAddress.sin_port);
1121c71e510Seric # endif DEBUG
1131c71e510Seric 
1141c71e510Seric 	/* get a socket for the SMTP connection */
115af5e902cSeric 	DaemonSocket = socket(AF_INET, SOCK_STREAM, 0);
116b7d7afcbSeric 	if (DaemonSocket < 0)
1171c71e510Seric 	{
1181c71e510Seric 		/* probably another daemon already */
1191c71e510Seric 		syserr("getrequests: can't create socket");
1201c71e510Seric 	  severe:
121b0ba8827Seric # ifdef LOG
122b0ba8827Seric 		if (LogLevel > 0)
123d2aa4720Seric 			syslog(LOG_ALERT, "cannot get connection");
124b0ba8827Seric # endif LOG
12547b12ae1Seric 		finis();
126d0a9e852Seric 	}
1271b6e4a15Seric 
1281b6e4a15Seric #ifdef DEBUG
1291b6e4a15Seric 	/* turn on network debugging? */
1301b6e4a15Seric 	if (tTd(15, 15))
13152308a50Seric 		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on);
1321b6e4a15Seric #endif DEBUG
1331b6e4a15Seric 
1347868dfc2Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on);
1357868dfc2Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on);
1367868dfc2Seric 
137af5e902cSeric 	if (bind(DaemonSocket, &SendmailAddress, sizeof SendmailAddress) < 0)
1381c71e510Seric 	{
1391c71e510Seric 		syserr("getrequests: cannot bind");
140b7d7afcbSeric 		(void) close(DaemonSocket);
1411c71e510Seric 		goto severe;
1421c71e510Seric 	}
143af5e902cSeric 	if (listen(DaemonSocket, 10) < 0)
144af5e902cSeric 	{
145af5e902cSeric 		syserr("getrequests: cannot listen");
146af5e902cSeric 		(void) close(DaemonSocket);
147af5e902cSeric 		goto severe;
148af5e902cSeric 	}
1491c71e510Seric 
1501cd247eeSeric 	(void) signal(SIGCHLD, reapchild);
15152308a50Seric 
1521c71e510Seric # ifdef DEBUG
1531c71e510Seric 	if (tTd(15, 1))
154b7d7afcbSeric 		printf("getrequests: %d\n", DaemonSocket);
1551c71e510Seric # endif DEBUG
1561c71e510Seric 
1571c71e510Seric 	for (;;)
1581c71e510Seric 	{
1593a099713Seric 		register int pid;
160a44d5a5eSeric 		auto int lotherend;
161a44d5a5eSeric 		struct sockaddr_in otherend;
1623a099713Seric 		extern int RefuseLA;
1633a099713Seric 
1643a099713Seric 		/* see if we are rejecting connections */
1653a099713Seric 		while (getla() > RefuseLA)
1663a099713Seric 			sleep(5);
167a44d5a5eSeric 
1681c71e510Seric 		/* wait for a connection */
1691c71e510Seric 		do
1701c71e510Seric 		{
1711c71e510Seric 			errno = 0;
1721c71e510Seric 			lotherend = sizeof otherend;
173af5e902cSeric 			t = accept(DaemonSocket, &otherend, &lotherend);
1741c71e510Seric 		} while (t < 0 && errno == EINTR);
1751c71e510Seric 		if (t < 0)
1761c71e510Seric 		{
1771c71e510Seric 			syserr("getrequests: accept");
1781c71e510Seric 			sleep(5);
1791c71e510Seric 			continue;
1801c71e510Seric 		}
181d0a9e852Seric 
182d0a9e852Seric 		/*
183d0a9e852Seric 		**  Create a subprocess to process the mail.
184d0a9e852Seric 		*/
185d0a9e852Seric 
186d0a9e852Seric # ifdef DEBUG
18761e4310fSeric 		if (tTd(15, 2))
1881c71e510Seric 			printf("getrequests: forking (fd = %d)\n", t);
189d0a9e852Seric # endif DEBUG
190eb889047Seric 
191a8268164Seric 		pid = fork();
192a8268164Seric 		if (pid < 0)
193a8268164Seric 		{
194a8268164Seric 			syserr("daemon: cannot fork");
195a8268164Seric 			sleep(10);
1961c71e510Seric 			(void) close(t);
197a8268164Seric 			continue;
198a8268164Seric 		}
199a8268164Seric 
200a8268164Seric 		if (pid == 0)
201a8268164Seric 		{
202a44d5a5eSeric 			extern struct hostent *gethostbyaddr();
203a44d5a5eSeric 			register struct hostent *hp;
204a44d5a5eSeric 			char buf[MAXNAME];
205a44d5a5eSeric 
206a8268164Seric 			/*
207a8268164Seric 			**  CHILD -- return to caller.
208a44d5a5eSeric 			**	Collect verified idea of sending host.
209a8268164Seric 			**	Verify calling user id if possible here.
210a8268164Seric 			*/
211a8268164Seric 
2121cd247eeSeric 			(void) signal(SIGCHLD, SIG_DFL);
213779ac194Seric 
214a44d5a5eSeric 			/* determine host name */
21531601fa7Seric 			hp = gethostbyaddr((char *) &otherend.sin_addr, sizeof otherend.sin_addr, AF_INET);
216a44d5a5eSeric 			if (hp != NULL)
217be388505Seric 			{
218fefbbe29Seric 				(void) strcpy(buf, hp->h_name);
219be388505Seric 				if (NetName != NULL && NetName[0] != '\0' &&
2207dcf5c8cSeric 				    index(hp->h_name, '.') == NULL)
221be388505Seric 				{
222fefbbe29Seric 					(void) strcat(buf, ".");
223fefbbe29Seric 					(void) strcat(buf, NetName);
224be388505Seric 				}
225be388505Seric 			}
226a44d5a5eSeric 			else
22729dcf4baSeric 			{
22829dcf4baSeric 				extern char *inet_ntoa();
22929dcf4baSeric 
23029dcf4baSeric 				/* produce a dotted quad */
23129dcf4baSeric 				(void) sprintf(buf, "[%s]",
23229dcf4baSeric 					inet_ntoa(otherend.sin_addr));
23329dcf4baSeric 			}
23429dcf4baSeric 
23529dcf4baSeric 			/* should we check for illegal connection here? XXX */
23629dcf4baSeric 
237a44d5a5eSeric 			RealHostName = newstr(buf);
238a44d5a5eSeric 
239b7d7afcbSeric 			(void) close(DaemonSocket);
2401c71e510Seric 			InChannel = fdopen(t, "r");
2411d5bd586Seric 			OutChannel = fdopen(dup(t), "w");
242d0a9e852Seric # ifdef DEBUG
24361e4310fSeric 			if (tTd(15, 2))
244d0a9e852Seric 				printf("getreq: returning\n");
245d0a9e852Seric # endif DEBUG
246252c8a22Seric # ifdef LOG
247252c8a22Seric 			if (LogLevel > 11)
248252c8a22Seric 				syslog(LOG_DEBUG, "connected, pid=%d", getpid());
249252c8a22Seric # endif LOG
250a8268164Seric 			return;
251a8268164Seric 		}
252a8268164Seric 
2533c154354Seric 		/* close the port so that others will hang (for a while) */
2543c154354Seric 		(void) close(t);
2558e3e4b17Seric 	}
2563c154354Seric 	/*NOTREACHED*/
2573c154354Seric }
2588e3e4b17Seric /*
259b7d7afcbSeric **  CLRDAEMON -- reset the daemon connection
260b7d7afcbSeric **
261b7d7afcbSeric **	Parameters:
262b7d7afcbSeric **		none.
263b7d7afcbSeric **
264b7d7afcbSeric **	Returns:
265b7d7afcbSeric **		none.
266b7d7afcbSeric **
267b7d7afcbSeric **	Side Effects:
268b7d7afcbSeric **		releases any resources used by the passive daemon.
269b7d7afcbSeric */
270b7d7afcbSeric 
271b7d7afcbSeric clrdaemon()
272b7d7afcbSeric {
273b7d7afcbSeric 	if (DaemonSocket >= 0)
274b7d7afcbSeric 		(void) close(DaemonSocket);
275b7d7afcbSeric 	DaemonSocket = -1;
276b7d7afcbSeric }
277b7d7afcbSeric /*
2787aa493c5Seric **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
2797aa493c5Seric **
2807aa493c5Seric **	Parameters:
2817aa493c5Seric **		host -- the name of the host.
28248ff0a9dSeric **		port -- the port number to connect to.
2837aa493c5Seric **		outfile -- a pointer to a place to put the outfile
2847aa493c5Seric **			descriptor.
2857aa493c5Seric **		infile -- ditto for infile.
2867aa493c5Seric **
2877aa493c5Seric **	Returns:
2887aa493c5Seric **		An exit code telling whether the connection could be
2897aa493c5Seric **			made and if not why not.
2907aa493c5Seric **
2917aa493c5Seric **	Side Effects:
2927aa493c5Seric **		none.
2937aa493c5Seric */
2947aa493c5Seric 
295794bdbb9Smiriam int	h_errno;	/*this will go away when code implemented*/
296794bdbb9Smiriam 
29748ff0a9dSeric makeconnection(host, port, outfile, infile)
2987aa493c5Seric 	char *host;
299210215eaSeric 	u_short port;
3007aa493c5Seric 	FILE **outfile;
3017aa493c5Seric 	FILE **infile;
3027aa493c5Seric {
3037aa493c5Seric 	register int s;
304*6286bb75Sbloom 	int sav_errno;
3057aa493c5Seric 
3067aa493c5Seric 	/*
3077aa493c5Seric 	**  Set up the address for the mailer.
30871096d12Seric 	**	Accept "[a.b.c.d]" syntax for host name.
3097aa493c5Seric 	*/
3107aa493c5Seric 
311794bdbb9Smiriam 	h_errno = 0;
312794bdbb9Smiriam 	errno = 0;
313794bdbb9Smiriam 
31471096d12Seric 	if (host[0] == '[')
31571096d12Seric 	{
316a44d5a5eSeric 		long hid;
317a44d5a5eSeric 		register char *p = index(host, ']');
31871096d12Seric 
319a44d5a5eSeric 		if (p != NULL)
32071096d12Seric 		{
321a44d5a5eSeric 			*p = '\0';
322a44d5a5eSeric 			hid = inet_addr(&host[1]);
323a44d5a5eSeric 			*p = ']';
32471096d12Seric 		}
325a44d5a5eSeric 		if (p == NULL || hid == -1)
32671096d12Seric 		{
32771096d12Seric 			usrerr("Invalid numeric domain spec \"%s\"", host);
32871096d12Seric 			return (EX_NOHOST);
32971096d12Seric 		}
33071096d12Seric 		SendmailAddress.sin_addr.s_addr = hid;
33171096d12Seric 	}
3321c71e510Seric 	else
3331c71e510Seric 	{
3341c71e510Seric 		register struct hostent *hp = gethostbyname(host);
3351c71e510Seric 
336794bdbb9Smiriam 		if (hp == NULL)
337794bdbb9Smiriam 		{
338794bdbb9Smiriam 			if (errno == ETIMEDOUT || h_errno == TRY_AGAIN)
33952308a50Seric 				return (EX_TEMPFAIL);
34082e5d8ddSeric 
34182e5d8ddSeric 			/*
34282e5d8ddSeric 			**  XXX Should look for mail forwarder record here
34382e5d8ddSeric 			**  XXX if (h_errno == NO_ADDRESS).
34482e5d8ddSeric 			*/
34582e5d8ddSeric 
3467aa493c5Seric 			return (EX_NOHOST);
347794bdbb9Smiriam 		}
34829dcf4baSeric 		bcopy(hp->h_addr, (char *) &SendmailAddress.sin_addr, hp->h_length);
3491c71e510Seric 	}
3501c71e510Seric 
3511c71e510Seric 	/*
3521c71e510Seric 	**  Determine the port number.
3531c71e510Seric 	*/
3541c71e510Seric 
355fd7c0790Seric 	if (port != 0)
356fd7c0790Seric 		SendmailAddress.sin_port = htons(port);
357fd7c0790Seric 	else
3581c71e510Seric 	{
3591c71e510Seric 		register struct servent *sp = getservbyname("smtp", "tcp");
3601c71e510Seric 
3611c71e510Seric 		if (sp == NULL)
3621c71e510Seric 		{
3631c71e510Seric 			syserr("makeconnection: server \"smtp\" unknown");
3641c71e510Seric 			return (EX_OSFILE);
3651c71e510Seric 		}
366fd7c0790Seric 		SendmailAddress.sin_port = sp->s_port;
3671c71e510Seric 	}
3687aa493c5Seric 
3697aa493c5Seric 	/*
3707aa493c5Seric 	**  Try to actually open the connection.
3717aa493c5Seric 	*/
3727aa493c5Seric 
3737aa493c5Seric # ifdef DEBUG
37461e4310fSeric 	if (tTd(16, 1))
3757aa493c5Seric 		printf("makeconnection (%s)\n", host);
3767aa493c5Seric # endif DEBUG
3777aa493c5Seric 
378af5e902cSeric 	s = socket(AF_INET, SOCK_STREAM, 0);
3797aa493c5Seric 	if (s < 0)
3807aa493c5Seric 	{
3817aa493c5Seric 		syserr("makeconnection: no socket");
382*6286bb75Sbloom 		sav_errno = errno;
3837aa493c5Seric 		goto failure;
3847aa493c5Seric 	}
3857aa493c5Seric 
3867aa493c5Seric # ifdef DEBUG
38761e4310fSeric 	if (tTd(16, 1))
3887aa493c5Seric 		printf("makeconnection: %d\n", s);
3891b6e4a15Seric 
3901b6e4a15Seric 	/* turn on network debugging? */
3911b6e4a15Seric 	if (tTd(16, 14))
39252308a50Seric 	{
39352308a50Seric 		int on = 1;
39452308a50Seric 		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on);
39552308a50Seric 	}
3967aa493c5Seric # endif DEBUG
397877a6142Seric 	(void) fflush(CurEnv->e_xfp);			/* for debugging */
3984bd6a662Seric 	errno = 0;					/* for debugging */
3991c71e510Seric 	SendmailAddress.sin_family = AF_INET;
400af5e902cSeric 	if (connect(s, &SendmailAddress, sizeof SendmailAddress) < 0)
4017aa493c5Seric 	{
402*6286bb75Sbloom 		sav_errno = errno;
403*6286bb75Sbloom 		(void) close(s);
4047aa493c5Seric 		/* failure, decide if temporary or not */
4057aa493c5Seric 	failure:
406*6286bb75Sbloom 		switch (sav_errno)
4077aa493c5Seric 		{
4087aa493c5Seric 		  case EISCONN:
4097aa493c5Seric 		  case ETIMEDOUT:
410292668b9Seric 		  case EINPROGRESS:
411292668b9Seric 		  case EALREADY:
412292668b9Seric 		  case EADDRINUSE:
41336bd23a8Seric 		  case EHOSTDOWN:
414292668b9Seric 		  case ENETDOWN:
415292668b9Seric 		  case ENETRESET:
416292668b9Seric 		  case ENOBUFS:
417a1645df8Seric 		  case ECONNREFUSED:
418efe49624Seric 		  case ECONNRESET:
419cae8261aSeric 		  case EHOSTUNREACH:
420dca8e1f7Seric 		  case ENETUNREACH:
4217aa493c5Seric 			/* there are others, I'm sure..... */
4227aa493c5Seric 			return (EX_TEMPFAIL);
4237aa493c5Seric 
424a44d5a5eSeric 		  case EPERM:
425a44d5a5eSeric 			/* why is this happening? */
426a44d5a5eSeric 			syserr("makeconnection: funny failure, addr=%lx, port=%x",
427a44d5a5eSeric 				SendmailAddress.sin_addr.s_addr, SendmailAddress.sin_port);
4284bd6a662Seric 			return (EX_TEMPFAIL);
429a44d5a5eSeric 
4307aa493c5Seric 		  default:
4317aa493c5Seric 			return (EX_UNAVAILABLE);
4327aa493c5Seric 		}
4337aa493c5Seric 	}
4347aa493c5Seric 
4357aa493c5Seric 	/* connection ok, put it into canonical form */
4367aa493c5Seric 	*outfile = fdopen(s, "w");
4377aa493c5Seric 	*infile = fdopen(s, "r");
4387aa493c5Seric 
439dca8e1f7Seric 	return (EX_OK);
4407aa493c5Seric }
441444eaf03Seric /*
442444eaf03Seric **  MYHOSTNAME -- return the name of this host.
443444eaf03Seric **
444444eaf03Seric **	Parameters:
445444eaf03Seric **		hostbuf -- a place to return the name of this host.
446897f1869Seric **		size -- the size of hostbuf.
447444eaf03Seric **
448444eaf03Seric **	Returns:
449444eaf03Seric **		A list of aliases for this host.
450444eaf03Seric **
451444eaf03Seric **	Side Effects:
452444eaf03Seric **		none.
453444eaf03Seric */
454444eaf03Seric 
455444eaf03Seric char **
456897f1869Seric myhostname(hostbuf, size)
457444eaf03Seric 	char hostbuf[];
458897f1869Seric 	int size;
459444eaf03Seric {
460444eaf03Seric 	extern struct hostent *gethostbyname();
461a44d5a5eSeric 	struct hostent *hp;
462444eaf03Seric 
463af5e902cSeric 	if (gethostname(hostbuf, size) < 0)
464af5e902cSeric 	{
465af5e902cSeric 		(void) strcpy(hostbuf, "localhost");
466af5e902cSeric 	}
467a44d5a5eSeric 	hp = gethostbyname(hostbuf);
468a44d5a5eSeric 	if (hp != NULL)
4697364df9fSeric 	{
470fefbbe29Seric 		(void) strcpy(hostbuf, hp->h_name);
471a44d5a5eSeric 		return (hp->h_aliases);
4727364df9fSeric 	}
473444eaf03Seric 	else
474444eaf03Seric 		return (NULL);
475444eaf03Seric }
47699f7cf32Seric /*
47799f7cf32Seric **  MAPHOSTNAME -- turn a hostname into canonical form
47899f7cf32Seric **
47999f7cf32Seric **	Parameters:
48099f7cf32Seric **		hbuf -- a buffer containing a hostname.
48199f7cf32Seric **		hbsize -- the size of hbuf.
48299f7cf32Seric **
48399f7cf32Seric **	Returns:
48499f7cf32Seric **		none.
48599f7cf32Seric **
48699f7cf32Seric **	Side Effects:
48799f7cf32Seric **		Looks up the host specified in hbuf.  If it is not
48899f7cf32Seric **		the canonical name for that host, replace it with
48999f7cf32Seric **		the canonical name.  If the name is unknown, or it
49099f7cf32Seric **		is already the canonical name, leave it unchanged.
49199f7cf32Seric */
492444eaf03Seric 
49399f7cf32Seric maphostname(hbuf, hbsize)
49499f7cf32Seric 	char *hbuf;
49599f7cf32Seric 	int hbsize;
49699f7cf32Seric {
49799f7cf32Seric 	register struct hostent *hp;
49899f7cf32Seric 	extern struct hostent *gethostbyname();
49999f7cf32Seric 
5005f78836eSmiriam 	/*
50131601fa7Seric 	**  If first character is a bracket, then it is an address
50231601fa7Seric 	**  lookup.  Address is copied into a temporary buffer to
50331601fa7Seric 	**  strip the brackets and to preserve hbuf if address is
50431601fa7Seric 	**  unknown.
5055f78836eSmiriam 	*/
5065f78836eSmiriam 
5075f78836eSmiriam 	if (*hbuf == '[')
5085f78836eSmiriam 	{
5095f78836eSmiriam 		extern struct hostent *gethostbyaddr();
5105f78836eSmiriam 		u_long in_addr;
5115f78836eSmiriam 		char ptr[256];
5125f78836eSmiriam 		char *bptr;
5135f78836eSmiriam 
51431601fa7Seric 		(void) strcpy(ptr, hbuf);
5155f78836eSmiriam 		bptr = index(ptr,']');
5165f78836eSmiriam 		*bptr = '\0';
5175f78836eSmiriam 		in_addr = inet_addr(&ptr[1]);
51831601fa7Seric 		hp = gethostbyaddr((char *) &in_addr, sizeof(struct in_addr), AF_INET);
5195f78836eSmiriam 		if (hp == NULL)
52031601fa7Seric 			return;
5215f78836eSmiriam 	}
5225f78836eSmiriam 	else
5235f78836eSmiriam 	{
52499f7cf32Seric 		makelower(hbuf);
52599f7cf32Seric 		hp = gethostbyname(hbuf);
5265f78836eSmiriam 	}
52799f7cf32Seric 	if (hp != NULL)
52899f7cf32Seric 	{
52999f7cf32Seric 		int i = strlen(hp->h_name);
53099f7cf32Seric 
53199f7cf32Seric 		if (i >= hbsize)
53299f7cf32Seric 			hp->h_name[--i] = '\0';
533fefbbe29Seric 		(void) strcpy(hbuf, hp->h_name);
53499f7cf32Seric 	}
53599f7cf32Seric }
53699f7cf32Seric 
537444eaf03Seric # else DAEMON
53899f7cf32Seric /* code for systems without sophisticated networking */
539444eaf03Seric 
540444eaf03Seric /*
541444eaf03Seric **  MYHOSTNAME -- stub version for case of no daemon code.
54221e9914dSeric **
54321e9914dSeric **	Can't convert to upper case here because might be a UUCP name.
544897f1869Seric **
545897f1869Seric **	Mark, you can change this to be anything you want......
546444eaf03Seric */
547444eaf03Seric 
548444eaf03Seric char **
549897f1869Seric myhostname(hostbuf, size)
550444eaf03Seric 	char hostbuf[];
551897f1869Seric 	int size;
552444eaf03Seric {
553444eaf03Seric 	register FILE *f;
554444eaf03Seric 
555444eaf03Seric 	hostbuf[0] = '\0';
556444eaf03Seric 	f = fopen("/usr/include/whoami", "r");
557444eaf03Seric 	if (f != NULL)
558444eaf03Seric 	{
559897f1869Seric 		(void) fgets(hostbuf, size, f);
560444eaf03Seric 		fixcrlf(hostbuf, TRUE);
561444eaf03Seric 		(void) fclose(f);
562444eaf03Seric 	}
563444eaf03Seric 	return (NULL);
564444eaf03Seric }
56599f7cf32Seric /*
56699f7cf32Seric **  MAPHOSTNAME -- turn a hostname into canonical form
56799f7cf32Seric **
56899f7cf32Seric **	Parameters:
56999f7cf32Seric **		hbuf -- a buffer containing a hostname.
57099f7cf32Seric **		hbsize -- the size of hbuf.
57199f7cf32Seric **
57299f7cf32Seric **	Returns:
57399f7cf32Seric **		none.
57499f7cf32Seric **
57599f7cf32Seric **	Side Effects:
57699f7cf32Seric **		Looks up the host specified in hbuf.  If it is not
57799f7cf32Seric **		the canonical name for that host, replace it with
57899f7cf32Seric **		the canonical name.  If the name is unknown, or it
57999f7cf32Seric **		is already the canonical name, leave it unchanged.
58099f7cf32Seric */
58199f7cf32Seric 
58299f7cf32Seric /*ARGSUSED*/
58399f7cf32Seric maphostname(hbuf, hbsize)
58499f7cf32Seric 	char *hbuf;
58599f7cf32Seric 	int hbsize;
58699f7cf32Seric {
58799f7cf32Seric 	return;
58899f7cf32Seric }
58999f7cf32Seric 
590d0a9e852Seric #endif DAEMON
591