xref: /csrg-svn/usr.sbin/sendmail/src/daemon.c (revision 58778)
122700Sdist /*
234920Sbostic  * Copyright (c) 1983 Eric P. Allman
333780Sbostic  * Copyright (c) 1988 Regents of the University of California.
433780Sbostic  * All rights reserved.
533780Sbostic  *
642825Sbostic  * %sccs.include.redist.c%
733780Sbostic  */
822700Sdist 
933932Sbostic #include <errno.h>
1058153Seric #include <signal.h>
1140962Sbostic #include "sendmail.h"
124535Seric 
1333780Sbostic #ifndef lint
1433780Sbostic #ifdef DAEMON
15*58778Seric static char sccsid[] = "@(#)daemon.c	6.21 (Berkeley) 03/23/93 (with daemon mode)";
1633780Sbostic #else
17*58778Seric static char sccsid[] = "@(#)daemon.c	6.21 (Berkeley) 03/23/93 (without daemon mode)";
1833780Sbostic #endif
1933780Sbostic #endif /* not lint */
204535Seric 
2133780Sbostic #ifdef DAEMON
2233780Sbostic 
2323120Seric # include <netdb.h>
2423120Seric # include <sys/wait.h>
2523120Seric # include <sys/time.h>
265978Seric 
2758755Seric #ifdef NETISO
2858755Seric # include <netiso/iso.h>
2958755Seric #endif
3058755Seric 
314535Seric /*
324535Seric **  DAEMON.C -- routines to use when running as a daemon.
337556Seric **
347556Seric **	This entire file is highly dependent on the 4.2 BSD
357556Seric **	interprocess communication primitives.  No attempt has
367556Seric **	been made to make this file portable to Version 7,
377556Seric **	Version 6, MPX files, etc.  If you should try such a
387556Seric **	thing yourself, I recommend chucking the entire file
397556Seric **	and starting from scratch.  Basic semantics are:
407556Seric **
417556Seric **	getrequests()
427556Seric **		Opens a port and initiates a connection.
437556Seric **		Returns in a child.  Must set InChannel and
447556Seric **		OutChannel appropriately.
4510206Seric **	clrdaemon()
4610206Seric **		Close any open files associated with getting
4710206Seric **		the connection; this is used when running the queue,
4810206Seric **		etc., to avoid having extra file descriptors during
4910206Seric **		the queue run and to avoid confusing the network
5010206Seric **		code (if it cares).
5152106Seric **	makeconnection(host, port, outfile, infile, usesecureport)
527556Seric **		Make a connection to the named host on the given
537556Seric **		port.  Set *outfile and *infile to the files
547556Seric **		appropriate for communication.  Returns zero on
557556Seric **		success, else an exit status describing the
567556Seric **		error.
5756823Seric **	maphostname(map, hbuf, hbufsiz, avp)
5856823Seric **		Convert the entry in hbuf into a canonical form.
594535Seric */
6058755Seric 
6158755Seric extern char	*anynet_ntoa();
624535Seric /*
634535Seric **  GETREQUESTS -- open mail IPC port and get requests.
644535Seric **
654535Seric **	Parameters:
664535Seric **		none.
674535Seric **
684535Seric **	Returns:
694535Seric **		none.
704535Seric **
714535Seric **	Side Effects:
724535Seric **		Waits until some interesting activity occurs.  When
734535Seric **		it does, a child is created to process it, and the
744535Seric **		parent waits for completion.  Return from this
759886Seric **		routine is always in the child.  The file pointers
769886Seric **		"InChannel" and "OutChannel" should be set to point
779886Seric **		to the communication channel.
784535Seric */
794535Seric 
8016144Seric int	DaemonSocket	= -1;		/* fd describing socket */
8116144Seric 
824535Seric getrequests()
834535Seric {
849610Seric 	int t;
859610Seric 	register struct servent *sp;
8625027Seric 	int on = 1;
8753751Seric 	bool refusingconnections = TRUE;
8858419Seric 	FILE *pidf;
89*58778Seric 	SOCKADDR srvraddr;
9046928Sbostic 	extern void reapchild();
917117Seric 
929610Seric 	/*
939610Seric 	**  Set up the address for the mailer.
949610Seric 	*/
959610Seric 
969610Seric 	sp = getservbyname("smtp", "tcp");
979610Seric 	if (sp == NULL)
989610Seric 	{
9958151Seric 		syserr("554 server \"smtp\" unknown");
10010167Seric 		goto severe;
1019610Seric 	}
102*58778Seric 	srvraddr.sin.sin_family = AF_INET;
103*58778Seric 	srvraddr.sin.sin_addr.s_addr = INADDR_ANY;
104*58778Seric 	srvraddr.sin.sin_port = sp->s_port;
1059610Seric 
1069610Seric 	/*
1079610Seric 	**  Try to actually open the connection.
1089610Seric 	*/
1099610Seric 
1109610Seric 	if (tTd(15, 1))
111*58778Seric 		printf("getrequests: port 0x%x\n", srvraddr.sin.sin_port);
1129610Seric 
1139610Seric 	/* get a socket for the SMTP connection */
11423120Seric 	DaemonSocket = socket(AF_INET, SOCK_STREAM, 0);
11510206Seric 	if (DaemonSocket < 0)
1169610Seric 	{
1179610Seric 		/* probably another daemon already */
1189610Seric 		syserr("getrequests: can't create socket");
1199610Seric 	  severe:
1209610Seric # ifdef LOG
1219610Seric 		if (LogLevel > 0)
12257663Seric 			syslog(LOG_ALERT, "problem creating SMTP socket");
12356795Seric # endif /* LOG */
1249610Seric 		finis();
1259610Seric 	}
12610347Seric 
12710347Seric 	/* turn on network debugging? */
12856328Seric 	if (tTd(15, 101))
12924945Seric 		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on);
13010347Seric 
13125027Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on);
13225027Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on);
13325027Seric 
134*58778Seric 	if (bind(DaemonSocket, &srvraddr.sa, sizeof srvraddr) < 0)
1359610Seric 	{
1369610Seric 		syserr("getrequests: cannot bind");
13710206Seric 		(void) close(DaemonSocket);
1389610Seric 		goto severe;
1399610Seric 	}
1409610Seric 
14124955Seric 	(void) signal(SIGCHLD, reapchild);
14224945Seric 
14358419Seric 	/* write the pid to the log file for posterity */
14458419Seric 	pidf = fopen(PidFile, "w");
14558419Seric 	if (pidf != NULL)
14658419Seric 	{
14758419Seric 		fprintf(pidf, "%d\n", getpid());
14858419Seric 		fclose(pidf);
14958419Seric 	}
15058419Seric 
15158419Seric 
1529610Seric 	if (tTd(15, 1))
15310206Seric 		printf("getrequests: %d\n", DaemonSocket);
1549610Seric 
1554631Seric 	for (;;)
1564631Seric 	{
15714875Seric 		register int pid;
15811147Seric 		auto int lotherend;
15953751Seric 		extern bool refuseconnections();
16011147Seric 
16114875Seric 		/* see if we are rejecting connections */
16253751Seric 		CurrentLA = getla();
16353751Seric 		if (refuseconnections())
16436584Sbostic 		{
16553751Seric 			if (!refusingconnections)
16653751Seric 			{
16753751Seric 				/* don't queue so peer will fail quickly */
16853751Seric 				(void) listen(DaemonSocket, 0);
16953751Seric 				refusingconnections = TRUE;
17053751Seric 			}
17157385Seric 			setproctitle("rejecting connections: load average: %d",
17257385Seric 				CurrentLA);
17314875Seric 			sleep(5);
17453751Seric 			continue;
17536584Sbostic 		}
17614875Seric 
17753751Seric 		if (refusingconnections)
17853751Seric 		{
17953751Seric 			/* start listening again */
18053751Seric 			if (listen(DaemonSocket, 10) < 0)
18153751Seric 			{
18253751Seric 				syserr("getrequests: cannot listen");
18353751Seric 				(void) close(DaemonSocket);
18453751Seric 				goto severe;
18553751Seric 			}
18653751Seric 			setproctitle("accepting connections");
18753751Seric 			refusingconnections = FALSE;
18853751Seric 		}
18953751Seric 
1909610Seric 		/* wait for a connection */
1919610Seric 		do
1929610Seric 		{
1939610Seric 			errno = 0;
19436230Skarels 			lotherend = sizeof RealHostAddr;
19546928Sbostic 			t = accept(DaemonSocket,
19646928Sbostic 			    (struct sockaddr *)&RealHostAddr, &lotherend);
1979610Seric 		} while (t < 0 && errno == EINTR);
1989610Seric 		if (t < 0)
1995978Seric 		{
2009610Seric 			syserr("getrequests: accept");
2019610Seric 			sleep(5);
2029610Seric 			continue;
2035978Seric 		}
2044631Seric 
2055978Seric 		/*
2065978Seric 		**  Create a subprocess to process the mail.
2075978Seric 		*/
2085978Seric 
2097677Seric 		if (tTd(15, 2))
2109610Seric 			printf("getrequests: forking (fd = %d)\n", t);
2115978Seric 
2124636Seric 		pid = fork();
2134636Seric 		if (pid < 0)
2144631Seric 		{
2154636Seric 			syserr("daemon: cannot fork");
2164636Seric 			sleep(10);
2179610Seric 			(void) close(t);
2184636Seric 			continue;
2194631Seric 		}
2204631Seric 
2214636Seric 		if (pid == 0)
2224631Seric 		{
22311147Seric 			extern struct hostent *gethostbyaddr();
22411147Seric 			register struct hostent *hp;
22511147Seric 			char buf[MAXNAME];
22611147Seric 
2274636Seric 			/*
2284636Seric 			**  CHILD -- return to caller.
22911147Seric 			**	Collect verified idea of sending host.
2304636Seric 			**	Verify calling user id if possible here.
2314636Seric 			*/
2324631Seric 
23324955Seric 			(void) signal(SIGCHLD, SIG_DFL);
23424950Seric 
23511147Seric 			/* determine host name */
236*58778Seric 			switch (RealHostAddr.sa.sa_family)
237*58778Seric 			{
238*58778Seric #ifdef NETINET
239*58778Seric 			  case AF_INET:
240*58778Seric 				hp = gethostbyaddr((char *) &RealHostAddr.sin.sin_addr,
241*58778Seric 					sizeof RealHostAddr.sin.sin_addr,
242*58778Seric 					AF_INET);
243*58778Seric 				break;
244*58778Seric #endif
245*58778Seric 
246*58778Seric #ifdef NETISO
247*58778Seric 			  case AF_ISO:
248*58778Seric 				hp = gethostbyaddr((char *) &RealHostAddr.siso.siso_addr,
249*58778Seric 					sizeof RealHostAddr.siso.siso_addr,
250*58778Seric 					AF_ISO);
251*58778Seric 				break;
252*58778Seric #endif
253*58778Seric 
254*58778Seric 			  default:
255*58778Seric 				hp = gethostbyaddr(RealHostAddr.sa.sa_data,
256*58778Seric 					   sizeof RealHostAddr.sa.sa_data,
257*58778Seric 					   RealHostAddr.sa.sa_family);
258*58778Seric 				break;
259*58778Seric 			}
260*58778Seric 
26111147Seric 			if (hp != NULL)
26223104Seric 				(void) strcpy(buf, hp->h_name);
26311147Seric 			else
26416884Seric 			{
26516884Seric 				/* produce a dotted quad */
26616884Seric 				(void) sprintf(buf, "[%s]",
26758755Seric 					anynet_ntoa(&RealHostAddr));
26816884Seric 			}
26916884Seric 
27055173Seric #ifdef LOG
27157977Seric 			if (LogLevel > 10)
27255173Seric 			{
27355173Seric 				/* log connection information */
27455173Seric 				syslog(LOG_INFO, "connect from %s (%s)",
27558755Seric 					buf, anynet_ntoa(&RealHostAddr));
27655173Seric 			}
27755173Seric #endif
27855173Seric 
27916884Seric 			/* should we check for illegal connection here? XXX */
28016884Seric 
28111147Seric 			RealHostName = newstr(buf);
28211147Seric 
28310206Seric 			(void) close(DaemonSocket);
2849610Seric 			InChannel = fdopen(t, "r");
28521062Seric 			OutChannel = fdopen(dup(t), "w");
2867677Seric 			if (tTd(15, 2))
2875978Seric 				printf("getreq: returning\n");
2884636Seric 			return;
2894631Seric 		}
2904631Seric 
2917117Seric 		/* close the port so that others will hang (for a while) */
2929610Seric 		(void) close(t);
2934631Seric 	}
2949886Seric 	/*NOTREACHED*/
2954631Seric }
2965978Seric /*
29710206Seric **  CLRDAEMON -- reset the daemon connection
29810206Seric **
29910206Seric **	Parameters:
30010206Seric **		none.
30110206Seric **
30210206Seric **	Returns:
30310206Seric **		none.
30410206Seric **
30510206Seric **	Side Effects:
30610206Seric **		releases any resources used by the passive daemon.
30710206Seric */
30810206Seric 
30910206Seric clrdaemon()
31010206Seric {
31110206Seric 	if (DaemonSocket >= 0)
31210206Seric 		(void) close(DaemonSocket);
31310206Seric 	DaemonSocket = -1;
31410206Seric }
31510206Seric /*
3166039Seric **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
3176039Seric **
3186039Seric **	Parameters:
3196039Seric **		host -- the name of the host.
3206633Seric **		port -- the port number to connect to.
32153739Seric **		mci -- a pointer to the mail connection information
32253739Seric **			structure to be filled in.
32352106Seric **		usesecureport -- if set, use a low numbered (reserved)
32452106Seric **			port to provide some rudimentary authentication.
3256039Seric **
3266039Seric **	Returns:
3276039Seric **		An exit code telling whether the connection could be
3286039Seric **			made and if not why not.
3296039Seric **
3306039Seric **	Side Effects:
3316039Seric **		none.
3326039Seric */
3335978Seric 
33458755Seric SOCKADDR	CurHostAddr;		/* address of current host */
33558305Seric 
33654967Seric int
33753739Seric makeconnection(host, port, mci, usesecureport)
3386039Seric 	char *host;
3397286Seric 	u_short port;
34054967Seric 	register MCI *mci;
34152106Seric 	bool usesecureport;
3426039Seric {
34329430Sbloom 	register int i, s;
34429430Sbloom 	register struct hostent *hp = (struct hostent *)NULL;
34558755Seric 	SOCKADDR addr;
34652106Seric 	int sav_errno;
34758755Seric 	int addrlen;
34835651Seric #ifdef NAMED_BIND
34935651Seric 	extern int h_errno;
35035651Seric #endif
3516039Seric 
3526039Seric 	/*
3536039Seric 	**  Set up the address for the mailer.
3549308Seric 	**	Accept "[a.b.c.d]" syntax for host name.
3556039Seric 	*/
3566039Seric 
35735651Seric #ifdef NAMED_BIND
35825475Smiriam 	h_errno = 0;
35935651Seric #endif
36025475Smiriam 	errno = 0;
36125475Smiriam 
3629308Seric 	if (host[0] == '[')
3639308Seric 	{
36411147Seric 		long hid;
36556795Seric 		register char *p = strchr(host, ']');
3669308Seric 
36711147Seric 		if (p != NULL)
3689308Seric 		{
36911147Seric 			*p = '\0';
37011147Seric 			hid = inet_addr(&host[1]);
37158360Seric 			if (hid == -1)
37258360Seric 			{
37358360Seric 				/* try it as a host name (avoid MX lookup) */
37458360Seric 				hp = gethostbyname(&host[1]);
37558360Seric 				*p = ']';
37658360Seric 				goto gothostent;
37758360Seric 			}
37811147Seric 			*p = ']';
3799308Seric 		}
38058360Seric 		if (p == NULL)
3819308Seric 		{
38258151Seric 			usrerr("553 Invalid numeric domain spec \"%s\"", host);
3839308Seric 			return (EX_NOHOST);
3849308Seric 		}
385*58778Seric 		addr.sin.sin_family = AF_INET;
386*58778Seric 		addr.sin.sin_addr.s_addr = hid;
3879308Seric 	}
3889610Seric 	else
3899610Seric 	{
39029430Sbloom 		hp = gethostbyname(host);
39158360Seric gothostent:
39225475Smiriam 		if (hp == NULL)
39324945Seric 		{
39435651Seric #ifdef NAMED_BIND
39525475Smiriam 			if (errno == ETIMEDOUT || h_errno == TRY_AGAIN)
39625475Smiriam 				return (EX_TEMPFAIL);
39725657Seric 
39835651Seric 			/* if name server is specified, assume temp fail */
39935651Seric 			if (errno == ECONNREFUSED && UseNameServer)
40035651Seric 				return (EX_TEMPFAIL);
40135651Seric #endif
40225475Smiriam 			return (EX_NOHOST);
40324945Seric 		}
404*58778Seric 		addr.sa.sa_family = hp->h_addrtype;
405*58778Seric 		switch (hp->h_addrtype)
406*58778Seric 		{
407*58778Seric #ifdef NETINET
408*58778Seric 		  case AF_INET:
40958755Seric 			bcopy(hp->h_addr,
410*58778Seric 				&addr.sin.sin_addr,
41158755Seric 				hp->h_length);
412*58778Seric 			break;
413*58778Seric #endif
414*58778Seric 
415*58778Seric 		  default:
41658755Seric 			bcopy(hp->h_addr,
417*58778Seric 				addr.sa.sa_data,
41858755Seric 				hp->h_length);
419*58778Seric 			break;
420*58778Seric 		}
42129430Sbloom 		i = 1;
4229610Seric 	}
4239610Seric 
4249610Seric 	/*
4259610Seric 	**  Determine the port number.
4269610Seric 	*/
4279610Seric 
42810011Seric 	if (port != 0)
42958755Seric 		port = htons(port);
43010011Seric 	else
4319610Seric 	{
4329610Seric 		register struct servent *sp = getservbyname("smtp", "tcp");
4339610Seric 
4349610Seric 		if (sp == NULL)
4359610Seric 		{
43658151Seric 			syserr("554 makeconnection: server \"smtp\" unknown");
43757977Seric 			return (EX_OSERR);
4389610Seric 		}
43958755Seric 		port = sp->s_port;
4409610Seric 	}
4416039Seric 
442*58778Seric 	switch (addr.sa.sa_family)
44358755Seric 	{
44458755Seric 	  case AF_INET:
445*58778Seric 		addr.sin.sin_port = port;
44658755Seric 		addrlen = sizeof (struct sockaddr_in);
44758755Seric 		break;
44858755Seric 
44958755Seric #ifdef NETISO
45058755Seric 	  case AF_ISO:
45158755Seric 		/* assume two byte transport selector */
45258755Seric 		bcopy((char *) &port, TSEL((struct sockaddr_iso *) &addr), 2);
45358755Seric 		addrlen = sizeof (struct sockaddr_iso);
45458755Seric 		break;
45558755Seric #endif
45658755Seric 
45758755Seric 	  default:
458*58778Seric 		syserr("Can't connect to address family %d", addr.sa.sa_family);
45958755Seric 		return (EX_NOHOST);
46058755Seric 	}
46158755Seric 
4626039Seric 	/*
4636039Seric 	**  Try to actually open the connection.
4646039Seric 	*/
4656039Seric 
46657736Seric 	for (;;)
46752106Seric 	{
46857736Seric 		if (tTd(16, 1))
46958755Seric 			printf("makeconnection (%s [%s])\n",
47058755Seric 				host, anynet_ntoa(&addr));
47152106Seric 
47258588Seric 		/* save for logging */
47358588Seric 		CurHostAddr = addr;
47458588Seric 
47557736Seric 		if (usesecureport)
47657736Seric 		{
47757736Seric 			int rport = IPPORT_RESERVED - 1;
4786039Seric 
47957736Seric 			s = rresvport(&rport);
48057736Seric 		}
48157736Seric 		else
48257736Seric 		{
48357736Seric 			s = socket(AF_INET, SOCK_STREAM, 0);
48457736Seric 		}
48557736Seric 		if (s < 0)
48657736Seric 		{
48757736Seric 			sav_errno = errno;
48857736Seric 			syserr("makeconnection: no socket");
48957736Seric 			goto failure;
49057736Seric 		}
49110347Seric 
49257736Seric 		if (tTd(16, 1))
49357736Seric 			printf("makeconnection: fd=%d\n", s);
49457736Seric 
49557736Seric 		/* turn on network debugging? */
49657736Seric 		if (tTd(16, 101))
49757736Seric 		{
49857736Seric 			int on = 1;
49957736Seric 			(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG,
50057736Seric 					  (char *)&on, sizeof on);
50157736Seric 		}
50257736Seric 		if (CurEnv->e_xfp != NULL)
50357736Seric 			(void) fflush(CurEnv->e_xfp);		/* for debugging */
50457736Seric 		errno = 0;					/* for debugging */
50558755Seric 		if (connect(s, (struct sockaddr *) &addr, addrlen) >= 0)
50657736Seric 			break;
50757736Seric 
50857736Seric 		/* couldn't connect.... figure out why */
50927744Sbloom 		sav_errno = errno;
51027744Sbloom 		(void) close(s);
51129430Sbloom 		if (hp && hp->h_addr_list[i])
51229430Sbloom 		{
51358755Seric 			extern char *errstring();
51458755Seric 
51557736Seric 			if (tTd(16, 1))
51658755Seric 				printf("Connect failed (%s); trying new address....\n",
51758755Seric 					errstring(sav_errno));
518*58778Seric 			switch (addr.sa.sa_family)
519*58778Seric 			{
520*58778Seric #ifdef NETINET
521*58778Seric 			  case AF_INET:
52258755Seric 				bcopy(hp->h_addr_list[i++],
523*58778Seric 				      &addr.sin.sin_addr,
52458755Seric 				      hp->h_length);
525*58778Seric 				break;
526*58778Seric #endif
527*58778Seric 
528*58778Seric 			  default:
52958755Seric 				bcopy(hp->h_addr_list[i++],
530*58778Seric 					addr.sa.sa_data,
53152106Seric 					hp->h_length);
532*58778Seric 				break;
533*58778Seric 			}
53457736Seric 			continue;
53529430Sbloom 		}
53629430Sbloom 
5376039Seric 		/* failure, decide if temporary or not */
5386039Seric 	failure:
53958542Seric 		if (transienterror(sav_errno))
54058542Seric 			return EX_TEMPFAIL;
54158542Seric 		else
54258542Seric 		{
54358542Seric 			extern char *errstring();
54411147Seric 
54558542Seric 			message("%s", errstring(sav_errno));
54658542Seric 			return (EX_UNAVAILABLE);
5476039Seric 		}
5486039Seric 	}
5496039Seric 
5506039Seric 	/* connection ok, put it into canonical form */
55153739Seric 	mci->mci_out = fdopen(s, "w");
55253739Seric 	mci->mci_in = fdopen(dup(s), "r");
5536039Seric 
55410098Seric 	return (EX_OK);
5556039Seric }
55610758Seric /*
55710758Seric **  MYHOSTNAME -- return the name of this host.
55810758Seric **
55910758Seric **	Parameters:
56010758Seric **		hostbuf -- a place to return the name of this host.
56112313Seric **		size -- the size of hostbuf.
56210758Seric **
56310758Seric **	Returns:
56410758Seric **		A list of aliases for this host.
56510758Seric **
56610758Seric **	Side Effects:
56758110Seric **		Sets the MyIpAddrs buffer to a list of my IP addresses.
56810758Seric */
5696039Seric 
57058110Seric struct in_addr	MyIpAddrs[MAXIPADDR + 1];
57158110Seric 
57210758Seric char **
57312313Seric myhostname(hostbuf, size)
57410758Seric 	char hostbuf[];
57512313Seric 	int size;
57610758Seric {
57758110Seric 	register struct hostent *hp;
57810758Seric 	extern struct hostent *gethostbyname();
57910758Seric 
58023120Seric 	if (gethostname(hostbuf, size) < 0)
58123120Seric 	{
58223120Seric 		(void) strcpy(hostbuf, "localhost");
58323120Seric 	}
58411147Seric 	hp = gethostbyname(hostbuf);
58511147Seric 	if (hp != NULL)
58616877Seric 	{
58758110Seric 		(void) strncpy(hostbuf, hp->h_name, size - 1);
58858110Seric 		hostbuf[size - 1] = '\0';
58958110Seric 
59058110Seric 		if (hp->h_addrtype == AF_INET && hp->h_length == 4)
59158110Seric 		{
59258110Seric 			register int i;
59358110Seric 
59458110Seric 			for (i = 0; i < MAXIPADDR; i++)
59558110Seric 			{
59658110Seric 				if (hp->h_addr_list[i] == NULL)
59758110Seric 					break;
59858110Seric 				MyIpAddrs[i].s_addr = *(u_long *) hp->h_addr_list[i];
59958110Seric 			}
60058110Seric 			MyIpAddrs[i].s_addr = 0;
60158110Seric 		}
60258110Seric 
60311147Seric 		return (hp->h_aliases);
60416877Seric 	}
60510758Seric 	else
60610758Seric 		return (NULL);
60710758Seric }
60851315Seric /*
60958308Seric **  GETREALHOSTNAME -- get the real host name asociated with a file descriptor
61058308Seric **
61158308Seric **	Parameters:
61258308Seric **		fd -- the descriptor
61358308Seric **
61458308Seric **	Returns:
61558308Seric **		The host name associated with this descriptor, if it can
61658308Seric **			be determined.
61758308Seric **		NULL otherwise.
61858308Seric **
61958308Seric **	Side Effects:
62058308Seric **		none
62158308Seric */
62258308Seric 
62358308Seric char *
62458308Seric getrealhostname(fd)
62558308Seric 	int fd;
62658308Seric {
62758308Seric 	register struct hostent *hp;
62858755Seric 	struct sockaddr sa;
62958755Seric 	int salen;
63058308Seric 	char hbuf[MAXNAME];
63158308Seric 	extern struct hostent *gethostbyaddr();
63258308Seric 
63358755Seric 	salen = sizeof sa;
63458755Seric 	if (getsockname(fd, &sa, &salen) < 0 || salen <= 0)
63558308Seric 		return NULL;
63658755Seric 	hp = gethostbyaddr(sa.sa_data, sa.sa_len,
63758755Seric 			   sa.sa_family);
63858308Seric 	if (hp != NULL)
63958308Seric 		(void) strcpy(hbuf, hp->h_name);
64058308Seric 	else
64158755Seric 		(void) sprintf(hbuf, "[%s]", anynet_ntoa(&sa));
64258308Seric 	return hbuf;
64358308Seric }
64458308Seric /*
64553751Seric **  MAPHOSTNAME -- turn a hostname into canonical form
64653751Seric **
64753751Seric **	Parameters:
64856823Seric **		map -- a pointer to this map (unused).
64953751Seric **		hbuf -- a buffer containing a hostname.
65053751Seric **		hbsize -- the size of hbuf.
65155019Seric **		avp -- unused -- for compatibility with other mapping
65255019Seric **			functions.
65353751Seric **
65453751Seric **	Returns:
65553751Seric **		The mapping, if found.
65653751Seric **		NULL if no mapping found.
65753751Seric **
65853751Seric **	Side Effects:
65953751Seric **		Looks up the host specified in hbuf.  If it is not
66053751Seric **		the canonical name for that host, return the canonical
66153751Seric **		name.
66253751Seric */
66351315Seric 
66453751Seric char *
66556823Seric maphostname(map, hbuf, hbsize, avp)
66656823Seric 	MAP *map;
66716911Seric 	char *hbuf;
66816911Seric 	int hbsize;
66953751Seric 	char **avp;
67016911Seric {
67116911Seric 	register struct hostent *hp;
67233932Sbostic 	u_long in_addr;
67356823Seric 	char *cp;
67458110Seric 	int i;
67533932Sbostic 	struct hostent *gethostbyaddr();
67616911Seric 
67756836Seric 	/* allow room for null */
67856823Seric 	hbsize--;
67953751Seric 
68025574Smiriam 	/*
68133932Sbostic 	 * If first character is a bracket, then it is an address
68233932Sbostic 	 * lookup.  Address is copied into a temporary buffer to
68333932Sbostic 	 * strip the brackets and to preserve hbuf if address is
68433932Sbostic 	 * unknown.
68533932Sbostic 	 */
68653751Seric 
68751315Seric 	if (*hbuf != '[')
68853751Seric 	{
68955019Seric 		extern bool getcanonname();
69055019Seric 
69158674Seric 		if (getcanonname(hbuf, hbsize))
69253751Seric 			return hbuf;
69353751Seric 		else
69453751Seric 			return NULL;
69553751Seric 	}
69656823Seric 	if ((cp = strchr(hbuf, ']')) == NULL)
69753751Seric 		return (NULL);
69840994Sbostic 	*cp = '\0';
69956823Seric 	in_addr = inet_addr(&hbuf[1]);
70058110Seric 
70158110Seric 	/* check to see if this is one of our addresses */
70258110Seric 	for (i = 0; MyIpAddrs[i].s_addr != 0; i++)
70358110Seric 	{
70458110Seric 		if (MyIpAddrs[i].s_addr == in_addr)
70558110Seric 		{
70658110Seric 			strncpy(hbuf, MyHostName, hbsize);
70758110Seric 			hbuf[hbsize] = '\0';
70858110Seric 			return hbuf;
70958110Seric 		}
71058110Seric 	}
71158110Seric 
71258110Seric 	/* nope -- ask the name server */
71333932Sbostic 	hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET);
71433932Sbostic 	if (hp == NULL)
71553751Seric 		return (NULL);
71653751Seric 
71758110Seric 	/* found a match -- copy out */
71856823Seric 	if (strlen(hp->h_name) > hbsize)
71956823Seric 		hp->h_name[hbsize] = '\0';
72053751Seric 	(void) strcpy(hbuf, hp->h_name);
72153751Seric 	return hbuf;
72233932Sbostic }
72358755Seric /*
72458755Seric **  ANYNET_NTOA -- convert a network address to printable form.
72558755Seric **
72658755Seric **	Parameters:
72758755Seric **		sap -- a pointer to a sockaddr structure.
72858755Seric **
72958755Seric **	Returns:
73058755Seric **		A printable version of that sockaddr.
73158755Seric */
73216911Seric 
73358755Seric char *
73458755Seric anynet_ntoa(sap)
73558755Seric 	register SOCKADDR *sap;
73658755Seric {
73758755Seric 	register char *bp;
73858755Seric 	register char *ap;
73958755Seric 	int l;
74058755Seric 	static char buf[80];
74158755Seric 
742*58778Seric #ifdef NETINET
743*58778Seric 	if (sap->sa.sa_family == AF_INET)
74458755Seric 	{
74558755Seric 		extern char *inet_ntoa();
74658755Seric 
74758755Seric 		return inet_ntoa(((struct sockaddr_in *) sap)->sin_addr);
74858755Seric 	}
749*58778Seric #endif
75058755Seric 
75158755Seric 	/* unknown family -- just dump bytes */
752*58778Seric 	(void) sprintf(buf, "Family %d: ", sap->sa.sa_family);
75358755Seric 	bp = &buf[strlen(buf)];
754*58778Seric 	ap = sap->sa.sa_data;
755*58778Seric 	for (l = sizeof sap->sa.sa_data; --l >= 0; )
75658755Seric 	{
75758755Seric 		(void) sprintf(bp, "%02x:", *ap++ & 0377);
75858755Seric 		bp += 3;
75958755Seric 	}
76058755Seric 	*--bp = '\0';
76158755Seric 	return buf;
76258755Seric }
76358755Seric 
76456795Seric # else /* DAEMON */
76516911Seric /* code for systems without sophisticated networking */
76610758Seric 
76710758Seric /*
76810758Seric **  MYHOSTNAME -- stub version for case of no daemon code.
76911297Seric **
77011297Seric **	Can't convert to upper case here because might be a UUCP name.
77112313Seric **
77212313Seric **	Mark, you can change this to be anything you want......
77310758Seric */
77410758Seric 
77510758Seric char **
77612313Seric myhostname(hostbuf, size)
77710758Seric 	char hostbuf[];
77812313Seric 	int size;
77910758Seric {
78010758Seric 	register FILE *f;
78110758Seric 
78210758Seric 	hostbuf[0] = '\0';
78310758Seric 	f = fopen("/usr/include/whoami", "r");
78410758Seric 	if (f != NULL)
78510758Seric 	{
78612313Seric 		(void) fgets(hostbuf, size, f);
78710758Seric 		fixcrlf(hostbuf, TRUE);
78810758Seric 		(void) fclose(f);
78910758Seric 	}
79010758Seric 	return (NULL);
79110758Seric }
79216911Seric /*
79358308Seric **  GETREALHOSTNAME -- get the real host name asociated with a file descriptor
79458308Seric **
79558308Seric **	Parameters:
79658308Seric **		fd -- the descriptor
79758308Seric **
79858308Seric **	Returns:
79958308Seric **		The host name associated with this descriptor, if it can
80058308Seric **			be determined.
80158308Seric **		NULL otherwise.
80258308Seric **
80358308Seric **	Side Effects:
80458308Seric **		none
80558308Seric */
80658308Seric 
80758308Seric char *
80858308Seric getrealhostname(fd)
80958308Seric 	int fd;
81058308Seric {
81158308Seric 	return NULL;
81258308Seric }
81358308Seric /*
81416911Seric **  MAPHOSTNAME -- turn a hostname into canonical form
81516911Seric **
81616911Seric **	Parameters:
81756823Seric **		map -- a pointer to the database map.
81816911Seric **		hbuf -- a buffer containing a hostname.
81953751Seric **		avp -- a pointer to a (cf file defined) argument vector.
82016911Seric **
82116911Seric **	Returns:
82253751Seric **		mapped host name
82351315Seric **		FALSE otherwise.
82416911Seric **
82516911Seric **	Side Effects:
82616911Seric **		Looks up the host specified in hbuf.  If it is not
82716911Seric **		the canonical name for that host, replace it with
82816911Seric **		the canonical name.  If the name is unknown, or it
82916911Seric **		is already the canonical name, leave it unchanged.
83016911Seric */
83110758Seric 
83216911Seric /*ARGSUSED*/
83353751Seric char *
83456823Seric maphostname(map, hbuf, hbsize, avp)
83556823Seric 	MAP *map;
83616911Seric 	char *hbuf;
83716911Seric 	int hbsize;
83853751Seric 	char **avp;
83916911Seric {
84053751Seric 	return NULL;
84116911Seric }
84216911Seric 
84356795Seric #endif /* DAEMON */
844