xref: /csrg-svn/usr.sbin/sendmail/src/daemon.c (revision 64747)
122700Sdist /*
234920Sbostic  * Copyright (c) 1983 Eric P. Allman
362522Sbostic  * Copyright (c) 1988, 1993
462522Sbostic  *	The Regents of the University of California.  All rights reserved.
533780Sbostic  *
642825Sbostic  * %sccs.include.redist.c%
733780Sbostic  */
822700Sdist 
933932Sbostic #include <errno.h>
1040962Sbostic #include "sendmail.h"
114535Seric 
1233780Sbostic #ifndef lint
1333780Sbostic #ifdef DAEMON
14*64747Seric static char sccsid[] = "@(#)daemon.c	8.19 (Berkeley) 10/23/93 (with daemon mode)";
1533780Sbostic #else
16*64747Seric static char sccsid[] = "@(#)daemon.c	8.19 (Berkeley) 10/23/93 (without daemon mode)";
1733780Sbostic #endif
1833780Sbostic #endif /* not lint */
194535Seric 
2033780Sbostic #ifdef DAEMON
2133780Sbostic 
2223120Seric # include <netdb.h>
2323120Seric # include <sys/time.h>
2464338Seric # include <arpa/inet.h>
255978Seric 
2659042Seric #ifdef NAMED_BIND
2759042Seric # include <arpa/nameser.h>
2859042Seric # include <resolv.h>
2959042Seric #endif
3059042Seric 
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.
5760089Seric **	host_map_lookup(map, hbuf, avp, pstat)
5856823Seric **		Convert the entry in hbuf into a canonical form.
594535Seric */
604535Seric /*
614535Seric **  GETREQUESTS -- open mail IPC port and get requests.
624535Seric **
634535Seric **	Parameters:
644535Seric **		none.
654535Seric **
664535Seric **	Returns:
674535Seric **		none.
684535Seric **
694535Seric **	Side Effects:
704535Seric **		Waits until some interesting activity occurs.  When
714535Seric **		it does, a child is created to process it, and the
724535Seric **		parent waits for completion.  Return from this
739886Seric **		routine is always in the child.  The file pointers
749886Seric **		"InChannel" and "OutChannel" should be set to point
759886Seric **		to the communication channel.
764535Seric */
774535Seric 
7858849Seric int		DaemonSocket	= -1;		/* fd describing socket */
7958849Seric SOCKADDR	DaemonAddr;			/* socket for incoming */
8059783Seric int		ListenQueueSize = 10;		/* size of listen queue */
8164381Seric int		TcpRcvBufferSize = 0;		/* size of TCP receive buffer */
8264381Seric int		TcpSndBufferSize = 0;		/* size of TCP send buffer */
8316144Seric 
844535Seric getrequests()
854535Seric {
869610Seric 	int t;
879610Seric 	register struct servent *sp;
8825027Seric 	int on = 1;
8953751Seric 	bool refusingconnections = TRUE;
9058419Seric 	FILE *pidf;
9146928Sbostic 	extern void reapchild();
927117Seric 
939610Seric 	/*
949610Seric 	**  Set up the address for the mailer.
959610Seric 	*/
969610Seric 
9758849Seric 	if (DaemonAddr.sin.sin_family == 0)
9858849Seric 		DaemonAddr.sin.sin_family = AF_INET;
9958849Seric 	if (DaemonAddr.sin.sin_addr.s_addr == 0)
10058849Seric 		DaemonAddr.sin.sin_addr.s_addr = INADDR_ANY;
10158849Seric 	if (DaemonAddr.sin.sin_port == 0)
1029610Seric 	{
10358849Seric 		sp = getservbyname("smtp", "tcp");
10458849Seric 		if (sp == NULL)
10558849Seric 		{
10658909Seric 			syserr("554 service \"smtp\" unknown");
10758849Seric 			goto severe;
10858849Seric 		}
10958849Seric 		DaemonAddr.sin.sin_port = sp->s_port;
1109610Seric 	}
1119610Seric 
1129610Seric 	/*
1139610Seric 	**  Try to actually open the connection.
1149610Seric 	*/
1159610Seric 
1169610Seric 	if (tTd(15, 1))
11758849Seric 		printf("getrequests: port 0x%x\n", DaemonAddr.sin.sin_port);
1189610Seric 
1199610Seric 	/* get a socket for the SMTP connection */
12059041Seric 	DaemonSocket = socket(DaemonAddr.sa.sa_family, SOCK_STREAM, 0);
12110206Seric 	if (DaemonSocket < 0)
1229610Seric 	{
1239610Seric 		/* probably another daemon already */
1249610Seric 		syserr("getrequests: can't create socket");
1259610Seric 	  severe:
1269610Seric # ifdef LOG
1279610Seric 		if (LogLevel > 0)
12857663Seric 			syslog(LOG_ALERT, "problem creating SMTP socket");
12956795Seric # endif /* LOG */
1309610Seric 		finis();
1319610Seric 	}
13210347Seric 
13310347Seric 	/* turn on network debugging? */
13456328Seric 	if (tTd(15, 101))
13524945Seric 		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on);
13610347Seric 
13725027Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on);
13825027Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on);
13925027Seric 
14064391Seric #ifdef SO_RCVBUF
14164391Seric 	if (TcpRcvBufferSize > 0)
14264391Seric 	{
14364391Seric 		if (setsockopt(DaemonSocket, SOL_SOCKET, SO_RCVBUF,
14464561Seric 			       (char *) &TcpRcvBufferSize,
14564391Seric 			       sizeof(TcpRcvBufferSize)) < 0)
14664391Seric 			syserr("getrequests: setsockopt(SO_RCVBUF)");
14764391Seric 	}
14864391Seric #endif
14964391Seric 
15059041Seric 	switch (DaemonAddr.sa.sa_family)
1519610Seric 	{
15259041Seric # ifdef NETINET
15359041Seric 	  case AF_INET:
15459041Seric 		t = sizeof DaemonAddr.sin;
15559041Seric 		break;
15659041Seric # endif
15759041Seric 
15859041Seric # ifdef NETISO
15959041Seric 	  case AF_ISO:
16059041Seric 		t = sizeof DaemonAddr.siso;
16159041Seric 		break;
16259041Seric # endif
16359041Seric 
16459041Seric 	  default:
16559041Seric 		t = sizeof DaemonAddr;
16659041Seric 		break;
16759041Seric 	}
16859041Seric 
16959041Seric 	if (bind(DaemonSocket, &DaemonAddr.sa, t) < 0)
17059041Seric 	{
1719610Seric 		syserr("getrequests: cannot bind");
17210206Seric 		(void) close(DaemonSocket);
1739610Seric 		goto severe;
1749610Seric 	}
1759610Seric 
17664035Seric 	(void) setsignal(SIGCHLD, reapchild);
17724945Seric 
17858419Seric 	/* write the pid to the log file for posterity */
17958419Seric 	pidf = fopen(PidFile, "w");
18058419Seric 	if (pidf != NULL)
18158419Seric 	{
18263863Seric 		extern char *CommandLineArgs;
18363863Seric 
18463863Seric 		/* write the process id on line 1 */
18558419Seric 		fprintf(pidf, "%d\n", getpid());
18663863Seric 
18763863Seric 		/* line 2 contains all command line flags */
18863863Seric 		fprintf(pidf, "%s\n", CommandLineArgs);
18963863Seric 
19063863Seric 		/* flush and close */
19158419Seric 		fclose(pidf);
19258419Seric 	}
19358419Seric 
19458419Seric 
1959610Seric 	if (tTd(15, 1))
19610206Seric 		printf("getrequests: %d\n", DaemonSocket);
1979610Seric 
1984631Seric 	for (;;)
1994631Seric 	{
20014875Seric 		register int pid;
20111147Seric 		auto int lotherend;
20253751Seric 		extern bool refuseconnections();
20311147Seric 
20414875Seric 		/* see if we are rejecting connections */
20553751Seric 		CurrentLA = getla();
20653751Seric 		if (refuseconnections())
20736584Sbostic 		{
20853751Seric 			if (!refusingconnections)
20953751Seric 			{
21053751Seric 				/* don't queue so peer will fail quickly */
21153751Seric 				(void) listen(DaemonSocket, 0);
21253751Seric 				refusingconnections = TRUE;
21353751Seric 			}
21457385Seric 			setproctitle("rejecting connections: load average: %d",
21557385Seric 				CurrentLA);
21614875Seric 			sleep(5);
21753751Seric 			continue;
21836584Sbostic 		}
21914875Seric 
22053751Seric 		if (refusingconnections)
22153751Seric 		{
22253751Seric 			/* start listening again */
22359783Seric 			if (listen(DaemonSocket, ListenQueueSize) < 0)
22453751Seric 			{
22553751Seric 				syserr("getrequests: cannot listen");
22653751Seric 				(void) close(DaemonSocket);
22753751Seric 				goto severe;
22853751Seric 			}
22953751Seric 			setproctitle("accepting connections");
23053751Seric 			refusingconnections = FALSE;
23153751Seric 		}
23253751Seric 
2339610Seric 		/* wait for a connection */
2349610Seric 		do
2359610Seric 		{
2369610Seric 			errno = 0;
23736230Skarels 			lotherend = sizeof RealHostAddr;
23846928Sbostic 			t = accept(DaemonSocket,
23946928Sbostic 			    (struct sockaddr *)&RealHostAddr, &lotherend);
2409610Seric 		} while (t < 0 && errno == EINTR);
2419610Seric 		if (t < 0)
2425978Seric 		{
2439610Seric 			syserr("getrequests: accept");
2449610Seric 			sleep(5);
2459610Seric 			continue;
2465978Seric 		}
2474631Seric 
2485978Seric 		/*
2495978Seric 		**  Create a subprocess to process the mail.
2505978Seric 		*/
2515978Seric 
2527677Seric 		if (tTd(15, 2))
2539610Seric 			printf("getrequests: forking (fd = %d)\n", t);
2545978Seric 
2554636Seric 		pid = fork();
2564636Seric 		if (pid < 0)
2574631Seric 		{
2584636Seric 			syserr("daemon: cannot fork");
2594636Seric 			sleep(10);
2609610Seric 			(void) close(t);
2614636Seric 			continue;
2624631Seric 		}
2634631Seric 
2644636Seric 		if (pid == 0)
2654631Seric 		{
26664086Seric 			char *p;
26758951Seric 			extern char *hostnamebyanyaddr();
26811147Seric 
2694636Seric 			/*
2704636Seric 			**  CHILD -- return to caller.
27111147Seric 			**	Collect verified idea of sending host.
2724636Seric 			**	Verify calling user id if possible here.
2734636Seric 			*/
2744631Seric 
27564035Seric 			(void) setsignal(SIGCHLD, SIG_DFL);
27659156Seric 			OpMode = MD_SMTP;
27724950Seric 
27811147Seric 			/* determine host name */
27964086Seric 			p = hostnamebyanyaddr(&RealHostAddr);
28064086Seric 			RealHostName = newstr(p);
28158778Seric 
28255173Seric #ifdef LOG
28363842Seric 			if (LogLevel > 11)
28455173Seric 			{
28555173Seric 				/* log connection information */
28655173Seric 				syslog(LOG_INFO, "connect from %s (%s)",
28758951Seric 					RealHostName, anynet_ntoa(&RealHostAddr));
28855173Seric 			}
28955173Seric #endif
29055173Seric 
29159254Seric 			(void) close(DaemonSocket);
29264724Seric 			if ((InChannel = fdopen(t, "r")) == NULL ||
29364724Seric 			    (t = dup(t)) < 0 ||
29464724Seric 			    (OutChannel = fdopen(t, "w")) == NULL)
29564724Seric 			{
29664724Seric 				syserr("cannot open SMTP server channel, fd=%d", t);
29764724Seric 				exit(0);
29864724Seric 			}
29959254Seric 
30016884Seric 			/* should we check for illegal connection here? XXX */
30159156Seric #ifdef XLA
30259156Seric 			if (!xla_host_ok(RealHostName))
30359156Seric 			{
30459254Seric 				message("421 Too many SMTP sessions for this host");
30559156Seric 				exit(0);
30659156Seric 			}
30759156Seric #endif
30816884Seric 
3097677Seric 			if (tTd(15, 2))
3105978Seric 				printf("getreq: returning\n");
3114636Seric 			return;
3124631Seric 		}
3134631Seric 
3147117Seric 		/* close the port so that others will hang (for a while) */
3159610Seric 		(void) close(t);
3164631Seric 	}
3179886Seric 	/*NOTREACHED*/
3184631Seric }
3195978Seric /*
32010206Seric **  CLRDAEMON -- reset the daemon connection
32110206Seric **
32210206Seric **	Parameters:
32310206Seric **		none.
32410206Seric **
32510206Seric **	Returns:
32610206Seric **		none.
32710206Seric **
32810206Seric **	Side Effects:
32910206Seric **		releases any resources used by the passive daemon.
33010206Seric */
33110206Seric 
33210206Seric clrdaemon()
33310206Seric {
33410206Seric 	if (DaemonSocket >= 0)
33510206Seric 		(void) close(DaemonSocket);
33610206Seric 	DaemonSocket = -1;
33710206Seric }
33810206Seric /*
33958849Seric **  SETDAEMONOPTIONS -- set options for running the daemon
34058849Seric **
34158849Seric **	Parameters:
34258849Seric **		p -- the options line.
34358849Seric **
34458849Seric **	Returns:
34558849Seric **		none.
34658849Seric */
34758849Seric 
34858849Seric setdaemonoptions(p)
34958849Seric 	register char *p;
35058849Seric {
35158873Seric 	if (DaemonAddr.sa.sa_family == AF_UNSPEC)
35258873Seric 		DaemonAddr.sa.sa_family = AF_INET;
35358873Seric 
35458849Seric 	while (p != NULL)
35558849Seric 	{
35658849Seric 		register char *f;
35758849Seric 		register char *v;
35858849Seric 
35958849Seric 		while (isascii(*p) && isspace(*p))
36058849Seric 			p++;
36158849Seric 		if (*p == '\0')
36258849Seric 			break;
36358849Seric 		f = p;
36458849Seric 		p = strchr(p, ',');
36558849Seric 		if (p != NULL)
36658849Seric 			*p++ = '\0';
36758849Seric 		v = strchr(f, '=');
36858849Seric 		if (v == NULL)
36958849Seric 			continue;
37058849Seric 		while (isascii(*++v) && isspace(*v))
37158849Seric 			continue;
37258849Seric 
37358849Seric 		switch (*f)
37458849Seric 		{
37558873Seric 		  case 'F':		/* address family */
37658849Seric 			if (isascii(*v) && isdigit(*v))
37758873Seric 				DaemonAddr.sa.sa_family = atoi(v);
37858873Seric #ifdef NETINET
37958873Seric 			else if (strcasecmp(v, "inet") == 0)
38058873Seric 				DaemonAddr.sa.sa_family = AF_INET;
38158873Seric #endif
38258873Seric #ifdef NETISO
38358873Seric 			else if (strcasecmp(v, "iso") == 0)
38458873Seric 				DaemonAddr.sa.sa_family = AF_ISO;
38558873Seric #endif
38658873Seric #ifdef NETNS
38758873Seric 			else if (strcasecmp(v, "ns") == 0)
38858873Seric 				DaemonAddr.sa.sa_family = AF_NS;
38958873Seric #endif
39058873Seric #ifdef NETX25
39158873Seric 			else if (strcasecmp(v, "x.25") == 0)
39258873Seric 				DaemonAddr.sa.sa_family = AF_CCITT;
39358873Seric #endif
39458849Seric 			else
39558873Seric 				syserr("554 Unknown address family %s in Family=option", v);
39658873Seric 			break;
39758873Seric 
39858873Seric 		  case 'A':		/* address */
39958873Seric 			switch (DaemonAddr.sa.sa_family)
40058849Seric 			{
40158873Seric #ifdef NETINET
40258873Seric 			  case AF_INET:
40358873Seric 				if (isascii(*v) && isdigit(*v))
40458873Seric 					DaemonAddr.sin.sin_addr.s_addr = inet_network(v);
40558873Seric 				else
40658873Seric 				{
40758873Seric 					register struct netent *np;
40858849Seric 
40958873Seric 					np = getnetbyname(v);
41058873Seric 					if (np == NULL)
41158873Seric 						syserr("554 network \"%s\" unknown", v);
41258873Seric 					else
41358873Seric 						DaemonAddr.sin.sin_addr.s_addr = np->n_net;
41458873Seric 				}
41558873Seric 				break;
41658873Seric #endif
41758873Seric 
41858873Seric 			  default:
41958873Seric 				syserr("554 Address= option unsupported for family %d",
42058873Seric 					DaemonAddr.sa.sa_family);
42158873Seric 				break;
42258849Seric 			}
42358849Seric 			break;
42458849Seric 
42558873Seric 		  case 'P':		/* port */
42658873Seric 			switch (DaemonAddr.sa.sa_family)
42758849Seric 			{
42858873Seric 				short port;
42958849Seric 
43058873Seric #ifdef NETINET
43158873Seric 			  case AF_INET:
43258873Seric 				if (isascii(*v) && isdigit(*v))
43364366Seric 					DaemonAddr.sin.sin_port = htons(atoi(v));
43458849Seric 				else
43558873Seric 				{
43658873Seric 					register struct servent *sp;
43758873Seric 
43858873Seric 					sp = getservbyname(v, "tcp");
43958873Seric 					if (sp == NULL)
44058909Seric 						syserr("554 service \"%s\" unknown", v);
44158873Seric 					else
44258873Seric 						DaemonAddr.sin.sin_port = sp->s_port;
44358873Seric 				}
44458873Seric 				break;
44558873Seric #endif
44658873Seric 
44758873Seric #ifdef NETISO
44858873Seric 			  case AF_ISO:
44958873Seric 				/* assume two byte transport selector */
45058873Seric 				if (isascii(*v) && isdigit(*v))
45164366Seric 					port = htons(atoi(v));
45258873Seric 				else
45358873Seric 				{
45458873Seric 					register struct servent *sp;
45558873Seric 
45658873Seric 					sp = getservbyname(v, "tcp");
45758873Seric 					if (sp == NULL)
45858909Seric 						syserr("554 service \"%s\" unknown", v);
45958873Seric 					else
46058873Seric 						port = sp->s_port;
46158873Seric 				}
46258873Seric 				bcopy((char *) &port, TSEL(&DaemonAddr.siso), 2);
46358873Seric 				break;
46458873Seric #endif
46558873Seric 
46658873Seric 			  default:
46758873Seric 				syserr("554 Port= option unsupported for family %d",
46858873Seric 					DaemonAddr.sa.sa_family);
46958873Seric 				break;
47058849Seric 			}
47158849Seric 			break;
47259783Seric 
47359783Seric 		  case 'L':		/* listen queue size */
47459783Seric 			ListenQueueSize = atoi(v);
47559783Seric 			break;
47664381Seric 
47764381Seric 		  case 'S':		/* send buffer size */
47864381Seric 			TcpSndBufferSize = atoi(v);
47964381Seric 			break;
48064381Seric 
48164381Seric 		  case 'R':		/* receive buffer size */
48264381Seric 			TcpRcvBufferSize = atoi(v);
48364381Seric 			break;
48458849Seric 		}
48558849Seric 	}
48658849Seric }
48758849Seric /*
4886039Seric **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
4896039Seric **
4906039Seric **	Parameters:
4916039Seric **		host -- the name of the host.
4926633Seric **		port -- the port number to connect to.
49353739Seric **		mci -- a pointer to the mail connection information
49453739Seric **			structure to be filled in.
49552106Seric **		usesecureport -- if set, use a low numbered (reserved)
49652106Seric **			port to provide some rudimentary authentication.
4976039Seric **
4986039Seric **	Returns:
4996039Seric **		An exit code telling whether the connection could be
5006039Seric **			made and if not why not.
5016039Seric **
5026039Seric **	Side Effects:
5036039Seric **		none.
5046039Seric */
5055978Seric 
50658755Seric SOCKADDR	CurHostAddr;		/* address of current host */
50758305Seric 
50854967Seric int
50953739Seric makeconnection(host, port, mci, usesecureport)
5106039Seric 	char *host;
5117286Seric 	u_short port;
51254967Seric 	register MCI *mci;
51352106Seric 	bool usesecureport;
5146039Seric {
51529430Sbloom 	register int i, s;
51629430Sbloom 	register struct hostent *hp = (struct hostent *)NULL;
51758755Seric 	SOCKADDR addr;
51852106Seric 	int sav_errno;
51958755Seric 	int addrlen;
52035651Seric #ifdef NAMED_BIND
52135651Seric 	extern int h_errno;
52235651Seric #endif
5236039Seric 
5246039Seric 	/*
5256039Seric 	**  Set up the address for the mailer.
5269308Seric 	**	Accept "[a.b.c.d]" syntax for host name.
5276039Seric 	*/
5286039Seric 
52935651Seric #ifdef NAMED_BIND
53025475Smiriam 	h_errno = 0;
53135651Seric #endif
53225475Smiriam 	errno = 0;
53358864Seric 	bzero(&CurHostAddr, sizeof CurHostAddr);
53464334Seric 	SmtpPhase = mci->mci_phase = "initial connection";
53558906Seric 	CurHostName = host;
53625475Smiriam 
5379308Seric 	if (host[0] == '[')
5389308Seric 	{
53911147Seric 		long hid;
54056795Seric 		register char *p = strchr(host, ']');
5419308Seric 
54211147Seric 		if (p != NULL)
5439308Seric 		{
54411147Seric 			*p = '\0';
54559884Seric #ifdef NETINET
54611147Seric 			hid = inet_addr(&host[1]);
54758360Seric 			if (hid == -1)
54859884Seric #endif
54958360Seric 			{
55058360Seric 				/* try it as a host name (avoid MX lookup) */
55158360Seric 				hp = gethostbyname(&host[1]);
55258360Seric 				*p = ']';
55358360Seric 				goto gothostent;
55458360Seric 			}
55511147Seric 			*p = ']';
5569308Seric 		}
55758360Seric 		if (p == NULL)
5589308Seric 		{
55958151Seric 			usrerr("553 Invalid numeric domain spec \"%s\"", host);
5609308Seric 			return (EX_NOHOST);
5619308Seric 		}
56259884Seric #ifdef NETINET
56359884Seric 		addr.sin.sin_family = AF_INET;		/*XXX*/
56458778Seric 		addr.sin.sin_addr.s_addr = hid;
56559884Seric #endif
5669308Seric 	}
5679610Seric 	else
5689610Seric 	{
56929430Sbloom 		hp = gethostbyname(host);
57058360Seric gothostent:
57125475Smiriam 		if (hp == NULL)
57224945Seric 		{
57335651Seric #ifdef NAMED_BIND
57425475Smiriam 			if (errno == ETIMEDOUT || h_errno == TRY_AGAIN)
57525475Smiriam 				return (EX_TEMPFAIL);
57625657Seric 
57735651Seric 			/* if name server is specified, assume temp fail */
57835651Seric 			if (errno == ECONNREFUSED && UseNameServer)
57935651Seric 				return (EX_TEMPFAIL);
58035651Seric #endif
58125475Smiriam 			return (EX_NOHOST);
58224945Seric 		}
58358778Seric 		addr.sa.sa_family = hp->h_addrtype;
58458778Seric 		switch (hp->h_addrtype)
58558778Seric 		{
58658778Seric #ifdef NETINET
58758778Seric 		  case AF_INET:
58858755Seric 			bcopy(hp->h_addr,
58958778Seric 				&addr.sin.sin_addr,
59058755Seric 				hp->h_length);
59158778Seric 			break;
59258778Seric #endif
59358778Seric 
59458778Seric 		  default:
59558755Seric 			bcopy(hp->h_addr,
59658778Seric 				addr.sa.sa_data,
59758755Seric 				hp->h_length);
59858778Seric 			break;
59958778Seric 		}
60029430Sbloom 		i = 1;
6019610Seric 	}
6029610Seric 
6039610Seric 	/*
6049610Seric 	**  Determine the port number.
6059610Seric 	*/
6069610Seric 
60710011Seric 	if (port != 0)
60858755Seric 		port = htons(port);
60910011Seric 	else
6109610Seric 	{
6119610Seric 		register struct servent *sp = getservbyname("smtp", "tcp");
6129610Seric 
6139610Seric 		if (sp == NULL)
6149610Seric 		{
61558909Seric 			syserr("554 makeconnection: service \"smtp\" unknown");
61657977Seric 			return (EX_OSERR);
6179610Seric 		}
61858755Seric 		port = sp->s_port;
6199610Seric 	}
6206039Seric 
62158778Seric 	switch (addr.sa.sa_family)
62258755Seric 	{
62359884Seric #ifdef NETINET
62458755Seric 	  case AF_INET:
62558778Seric 		addr.sin.sin_port = port;
62658755Seric 		addrlen = sizeof (struct sockaddr_in);
62758755Seric 		break;
62859884Seric #endif
62958755Seric 
63058755Seric #ifdef NETISO
63158755Seric 	  case AF_ISO:
63258755Seric 		/* assume two byte transport selector */
63358755Seric 		bcopy((char *) &port, TSEL((struct sockaddr_iso *) &addr), 2);
63458755Seric 		addrlen = sizeof (struct sockaddr_iso);
63558755Seric 		break;
63658755Seric #endif
63758755Seric 
63858755Seric 	  default:
63958778Seric 		syserr("Can't connect to address family %d", addr.sa.sa_family);
64058755Seric 		return (EX_NOHOST);
64158755Seric 	}
64258755Seric 
6436039Seric 	/*
6446039Seric 	**  Try to actually open the connection.
6456039Seric 	*/
6466039Seric 
64759156Seric #ifdef XLA
64859156Seric 	/* if too many connections, don't bother trying */
64959156Seric 	if (!xla_noqueue_ok(host))
65059156Seric 		return EX_TEMPFAIL;
65159156Seric #endif
65259156Seric 
65357736Seric 	for (;;)
65452106Seric 	{
65557736Seric 		if (tTd(16, 1))
65658755Seric 			printf("makeconnection (%s [%s])\n",
65758755Seric 				host, anynet_ntoa(&addr));
65852106Seric 
65958588Seric 		/* save for logging */
66058588Seric 		CurHostAddr = addr;
66158588Seric 
66257736Seric 		if (usesecureport)
66357736Seric 		{
66457736Seric 			int rport = IPPORT_RESERVED - 1;
6656039Seric 
66657736Seric 			s = rresvport(&rport);
66757736Seric 		}
66857736Seric 		else
66957736Seric 		{
67057736Seric 			s = socket(AF_INET, SOCK_STREAM, 0);
67157736Seric 		}
67257736Seric 		if (s < 0)
67357736Seric 		{
67457736Seric 			sav_errno = errno;
67557736Seric 			syserr("makeconnection: no socket");
67657736Seric 			goto failure;
67757736Seric 		}
67810347Seric 
67964381Seric #ifdef SO_SNDBUF
68064381Seric 		if (TcpSndBufferSize > 0)
68164381Seric 		{
68264381Seric 			if (setsockopt(s, SOL_SOCKET, SO_SNDBUF,
68364561Seric 				       (char *) &TcpSndBufferSize,
68464381Seric 				       sizeof(TcpSndBufferSize)) < 0)
68564381Seric 				syserr("makeconnection: setsockopt(SO_SNDBUF)");
68664381Seric 		}
68764381Seric #endif
68864381Seric 
68957736Seric 		if (tTd(16, 1))
69057736Seric 			printf("makeconnection: fd=%d\n", s);
69157736Seric 
69257736Seric 		/* turn on network debugging? */
69357736Seric 		if (tTd(16, 101))
69457736Seric 		{
69557736Seric 			int on = 1;
69657736Seric 			(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG,
69757736Seric 					  (char *)&on, sizeof on);
69857736Seric 		}
69957736Seric 		if (CurEnv->e_xfp != NULL)
70057736Seric 			(void) fflush(CurEnv->e_xfp);		/* for debugging */
70157736Seric 		errno = 0;					/* for debugging */
70258755Seric 		if (connect(s, (struct sockaddr *) &addr, addrlen) >= 0)
70357736Seric 			break;
70457736Seric 
70557736Seric 		/* couldn't connect.... figure out why */
70627744Sbloom 		sav_errno = errno;
70727744Sbloom 		(void) close(s);
70829430Sbloom 		if (hp && hp->h_addr_list[i])
70929430Sbloom 		{
71057736Seric 			if (tTd(16, 1))
71158755Seric 				printf("Connect failed (%s); trying new address....\n",
71258755Seric 					errstring(sav_errno));
71358778Seric 			switch (addr.sa.sa_family)
71458778Seric 			{
71558778Seric #ifdef NETINET
71658778Seric 			  case AF_INET:
71758755Seric 				bcopy(hp->h_addr_list[i++],
71858778Seric 				      &addr.sin.sin_addr,
71958755Seric 				      hp->h_length);
72058778Seric 				break;
72158778Seric #endif
72258778Seric 
72358778Seric 			  default:
72458755Seric 				bcopy(hp->h_addr_list[i++],
72558778Seric 					addr.sa.sa_data,
72652106Seric 					hp->h_length);
72758778Seric 				break;
72858778Seric 			}
72957736Seric 			continue;
73029430Sbloom 		}
73129430Sbloom 
7326039Seric 		/* failure, decide if temporary or not */
7336039Seric 	failure:
73459254Seric #ifdef XLA
73559254Seric 		xla_host_end(host);
73659254Seric #endif
73758542Seric 		if (transienterror(sav_errno))
73858542Seric 			return EX_TEMPFAIL;
73958542Seric 		else
74058542Seric 		{
74158542Seric 			message("%s", errstring(sav_errno));
74258542Seric 			return (EX_UNAVAILABLE);
7436039Seric 		}
7446039Seric 	}
7456039Seric 
7466039Seric 	/* connection ok, put it into canonical form */
74764724Seric 	if ((mci->mci_out = fdopen(s, "w")) == NULL ||
74864724Seric 	    (s = dup(s)) < 0 ||
74964725Seric 	    (mci->mci_in = fdopen(s, "r")) == NULL)
75064724Seric 	{
75164724Seric 		syserr("cannot open SMTP client channel, fd=%d", s);
75264724Seric 		return EX_TEMPFAIL;
75364724Seric 	}
7546039Seric 
75510098Seric 	return (EX_OK);
7566039Seric }
75710758Seric /*
75810758Seric **  MYHOSTNAME -- return the name of this host.
75910758Seric **
76010758Seric **	Parameters:
76110758Seric **		hostbuf -- a place to return the name of this host.
76212313Seric **		size -- the size of hostbuf.
76310758Seric **
76410758Seric **	Returns:
76510758Seric **		A list of aliases for this host.
76610758Seric **
76710758Seric **	Side Effects:
76864338Seric **		Adds numeric codes to $=w.
76910758Seric */
7706039Seric 
77110758Seric char **
77212313Seric myhostname(hostbuf, size)
77310758Seric 	char hostbuf[];
77412313Seric 	int size;
77510758Seric {
77658110Seric 	register struct hostent *hp;
77710758Seric 	extern struct hostent *gethostbyname();
77810758Seric 
77923120Seric 	if (gethostname(hostbuf, size) < 0)
78023120Seric 	{
78123120Seric 		(void) strcpy(hostbuf, "localhost");
78223120Seric 	}
78311147Seric 	hp = gethostbyname(hostbuf);
78411147Seric 	if (hp != NULL)
78516877Seric 	{
78658110Seric 		(void) strncpy(hostbuf, hp->h_name, size - 1);
78758110Seric 		hostbuf[size - 1] = '\0';
78858110Seric 
78958110Seric 		if (hp->h_addrtype == AF_INET && hp->h_length == 4)
79058110Seric 		{
79158110Seric 			register int i;
79258110Seric 
79364338Seric 			for (i = 0; hp->h_addr_list[i] != NULL; i++)
79458110Seric 			{
79564338Seric 				char ipbuf[100];
79664338Seric 
79764338Seric 				sprintf(ipbuf, "[%s]",
79864338Seric 					inet_ntoa(*((struct in_addr *) hp->h_addr_list[i])));
79964338Seric 				setclass('w', ipbuf);
80058110Seric 			}
80158110Seric 		}
80258110Seric 
80311147Seric 		return (hp->h_aliases);
80416877Seric 	}
80510758Seric 	else
80610758Seric 		return (NULL);
80710758Seric }
80851315Seric /*
80958951Seric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
81058308Seric **
81158951Seric **	Uses RFC1413 protocol to try to get info from the other end.
81258951Seric **
81358308Seric **	Parameters:
81458308Seric **		fd -- the descriptor
81558308Seric **
81658308Seric **	Returns:
81758951Seric **		The user@host information associated with this descriptor.
81858308Seric **
81958308Seric **	Side Effects:
82058951Seric **		Sets RealHostName to the name of the host at the other end.
82158308Seric */
82258308Seric 
82358951Seric #ifdef IDENTPROTO
82458951Seric 
82558951Seric static jmp_buf	CtxAuthTimeout;
82658951Seric 
82758951Seric static
82858951Seric authtimeout()
82958951Seric {
83058951Seric 	longjmp(CtxAuthTimeout, 1);
83158951Seric }
83258951Seric 
83358951Seric #endif
83458951Seric 
83558308Seric char *
83658951Seric getauthinfo(fd)
83758308Seric 	int fd;
83858308Seric {
83958951Seric 	SOCKADDR fa;
84058951Seric 	int falen;
84159104Seric 	register char *p;
84258951Seric #ifdef IDENTPROTO
84358951Seric 	SOCKADDR la;
84458951Seric 	int lalen;
84558951Seric 	register struct servent *sp;
84658951Seric 	int s;
84758951Seric 	int i;
84858951Seric 	EVENT *ev;
84958951Seric #endif
85058951Seric 	static char hbuf[MAXNAME * 2 + 2];
85158951Seric 	extern char *hostnamebyanyaddr();
85258951Seric 	extern char RealUserName[];			/* main.c */
85358308Seric 
85458951Seric 	falen = sizeof fa;
85558951Seric 	if (getpeername(fd, &fa.sa, &falen) < 0 || falen <= 0)
85658951Seric 	{
85758951Seric 		RealHostName = "localhost";
85858951Seric 		(void) sprintf(hbuf, "%s@localhost", RealUserName);
85958957Seric 		if (tTd(9, 1))
86058951Seric 			printf("getauthinfo: %s\n", hbuf);
86158951Seric 		return hbuf;
86258951Seric 	}
86358951Seric 
86464086Seric 	p = hostnamebyanyaddr(&fa);
86564086Seric 	RealHostName = newstr(p);
86658951Seric 	RealHostAddr = fa;
86758951Seric 
86858951Seric #ifdef IDENTPROTO
86958951Seric 	lalen = sizeof la;
87058951Seric 	if (fa.sa.sa_family != AF_INET ||
87158951Seric 	    getsockname(fd, &la.sa, &lalen) < 0 || lalen <= 0 ||
87258951Seric 	    la.sa.sa_family != AF_INET)
87358951Seric 	{
87458951Seric 		/* no ident info */
87558951Seric 		goto noident;
87658951Seric 	}
87758951Seric 
87858951Seric 	/* create ident query */
87960489Seric 	(void) sprintf(hbuf, "%d,%d\r\n",
88060489Seric 		ntohs(fa.sin.sin_port), ntohs(la.sin.sin_port));
88158951Seric 
88258951Seric 	/* create local address */
883*64747Seric 	la.sin.sin_port = 0;
88458951Seric 
88558951Seric 	/* create foreign address */
88658951Seric 	sp = getservbyname("auth", "tcp");
88758951Seric 	if (sp != NULL)
88858951Seric 		fa.sin.sin_port = sp->s_port;
88958308Seric 	else
89059097Seric 		fa.sin.sin_port = htons(113);
89158951Seric 
89258951Seric 	s = -1;
89358951Seric 	if (setjmp(CtxAuthTimeout) != 0)
89458951Seric 	{
89558951Seric 		if (s >= 0)
89658951Seric 			(void) close(s);
89758951Seric 		goto noident;
89858951Seric 	}
89958951Seric 
90058951Seric 	/* put a timeout around the whole thing */
90164255Seric 	ev = setevent(TimeOuts.to_ident, authtimeout, 0);
90258951Seric 
903*64747Seric 	/* connect to foreign IDENT server using same address as SMTP socket */
90458951Seric 	s = socket(AF_INET, SOCK_STREAM, 0);
90558951Seric 	if (s < 0)
90658951Seric 	{
90758951Seric 		clrevent(ev);
90858951Seric 		goto noident;
90958951Seric 	}
910*64747Seric 	if (bind(s, &la.sa, sizeof la.sin) < 0 ||
911*64747Seric 	    connect(s, &fa.sa, sizeof fa.sin) < 0)
91258951Seric 	{
91358951Seric closeident:
91458951Seric 		(void) close(s);
91558951Seric 		clrevent(ev);
91658951Seric 		goto noident;
91758951Seric 	}
91858951Seric 
91958957Seric 	if (tTd(9, 10))
92058951Seric 		printf("getauthinfo: sent %s", hbuf);
92158951Seric 
92258951Seric 	/* send query */
92358951Seric 	if (write(s, hbuf, strlen(hbuf)) < 0)
92458951Seric 		goto closeident;
92558951Seric 
92658951Seric 	/* get result */
92758951Seric 	i = read(s, hbuf, sizeof hbuf);
92858951Seric 	(void) close(s);
92958951Seric 	clrevent(ev);
93058951Seric 	if (i <= 0)
93158951Seric 		goto noident;
93258951Seric 	if (hbuf[--i] == '\n' && hbuf[--i] == '\r')
93358951Seric 		i--;
93458951Seric 	hbuf[++i] = '\0';
93558951Seric 
93658957Seric 	if (tTd(9, 3))
93758951Seric 		printf("getauthinfo:  got %s\n", hbuf);
93858951Seric 
93958951Seric 	/* parse result */
94058951Seric 	p = strchr(hbuf, ':');
94158951Seric 	if (p == NULL)
94258951Seric 	{
94358951Seric 		/* malformed response */
94458951Seric 		goto noident;
94558951Seric 	}
94658951Seric 	while (isascii(*++p) && isspace(*p))
94758951Seric 		continue;
94858951Seric 	if (strncasecmp(p, "userid", 6) != 0)
94958951Seric 	{
95058951Seric 		/* presumably an error string */
95158951Seric 		goto noident;
95258951Seric 	}
95358951Seric 	p += 6;
95458951Seric 	while (isascii(*p) && isspace(*p))
95558951Seric 		p++;
95658951Seric 	if (*p++ != ':')
95758951Seric 	{
95858951Seric 		/* either useridxx or malformed response */
95958951Seric 		goto noident;
96058951Seric 	}
96158951Seric 
96258951Seric 	/* p now points to the OSTYPE field */
96358951Seric 	p = strchr(p, ':');
96458951Seric 	if (p == NULL)
96558951Seric 	{
96658951Seric 		/* malformed response */
96758951Seric 		goto noident;
96858951Seric 	}
96958951Seric 
97058957Seric 	/* 1413 says don't do this -- but it's broken otherwise */
97158957Seric 	while (isascii(*++p) && isspace(*p))
97258957Seric 		continue;
97358957Seric 
97458951Seric 	/* p now points to the authenticated name */
97558951Seric 	(void) sprintf(hbuf, "%s@%s", p, RealHostName);
97658957Seric 	goto finish;
97758957Seric 
97858957Seric #endif /* IDENTPROTO */
97958957Seric 
98058957Seric noident:
98158957Seric 	(void) strcpy(hbuf, RealHostName);
98258957Seric 
98358957Seric finish:
98458951Seric 	if (RealHostName[0] != '[')
98558951Seric 	{
98658951Seric 		p = &hbuf[strlen(hbuf)];
98758951Seric 		(void) sprintf(p, " [%s]", anynet_ntoa(&RealHostAddr));
98858951Seric 	}
98958957Seric 	if (tTd(9, 1))
99058951Seric 		printf("getauthinfo: %s\n", hbuf);
99158308Seric 	return hbuf;
99258308Seric }
99358308Seric /*
99460089Seric **  HOST_MAP_LOOKUP -- turn a hostname into canonical form
99553751Seric **
99653751Seric **	Parameters:
99756823Seric **		map -- a pointer to this map (unused).
99860089Seric **		name -- the (presumably unqualified) hostname.
99960257Seric **		av -- unused -- for compatibility with other mapping
100055019Seric **			functions.
100159084Seric **		statp -- an exit status (out parameter) -- set to
100259084Seric **			EX_TEMPFAIL if the name server is unavailable.
100353751Seric **
100453751Seric **	Returns:
100553751Seric **		The mapping, if found.
100653751Seric **		NULL if no mapping found.
100753751Seric **
100853751Seric **	Side Effects:
100953751Seric **		Looks up the host specified in hbuf.  If it is not
101053751Seric **		the canonical name for that host, return the canonical
101153751Seric **		name.
101253751Seric */
101351315Seric 
101453751Seric char *
101560257Seric host_map_lookup(map, name, av, statp)
101656823Seric 	MAP *map;
101760089Seric 	char *name;
101860257Seric 	char **av;
101959084Seric 	int *statp;
102016911Seric {
102116911Seric 	register struct hostent *hp;
102233932Sbostic 	u_long in_addr;
102356823Seric 	char *cp;
102458110Seric 	int i;
102559671Seric 	register STAB *s;
102660257Seric 	char hbuf[MAXNAME];
102759671Seric 	extern struct hostent *gethostbyaddr();
102859671Seric 	extern int h_errno;
102916911Seric 
103025574Smiriam 	/*
103159671Seric 	**  See if we have already looked up this name.  If so, just
103259671Seric 	**  return it.
103359671Seric 	*/
103453751Seric 
103560089Seric 	s = stab(name, ST_NAMECANON, ST_ENTER);
103659671Seric 	if (bitset(NCF_VALID, s->s_namecanon.nc_flags))
103759671Seric 	{
103859986Seric 		if (tTd(9, 1))
103960089Seric 			printf("host_map_lookup(%s) => CACHE %s\n",
104060089Seric 				name, s->s_namecanon.nc_cname);
104159671Seric 		errno = s->s_namecanon.nc_errno;
104259671Seric 		h_errno = s->s_namecanon.nc_herrno;
104359671Seric 		*statp = s->s_namecanon.nc_stat;
104459671Seric 		return s->s_namecanon.nc_cname;
104559671Seric 	}
104659671Seric 
104759671Seric 	/*
104859671Seric 	**  If first character is a bracket, then it is an address
104959671Seric 	**  lookup.  Address is copied into a temporary buffer to
105060089Seric 	**  strip the brackets and to preserve name if address is
105159671Seric 	**  unknown.
105259671Seric 	*/
105359671Seric 
105460089Seric 	if (*name != '[')
105553751Seric 	{
105655019Seric 		extern bool getcanonname();
105755019Seric 
105858798Seric 		if (tTd(9, 1))
105960089Seric 			printf("host_map_lookup(%s) => ", name);
106059671Seric 		s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
106160089Seric 		(void) strcpy(hbuf, name);
106263842Seric 		if (getcanonname(hbuf, sizeof hbuf - 1, TRUE))
106358796Seric 		{
106458796Seric 			if (tTd(9, 1))
106558796Seric 				printf("%s\n", hbuf);
106660257Seric 			cp = map_rewrite(map, hbuf, strlen(hbuf), av);
106760257Seric 			s->s_namecanon.nc_cname = newstr(cp);
106860257Seric 			return cp;
106958796Seric 		}
107053751Seric 		else
107158796Seric 		{
107259084Seric 			register struct hostent *hp;
107359084Seric 
107458796Seric 			if (tTd(9, 1))
107559084Seric 				printf("FAIL (%d)\n", h_errno);
107659671Seric 			s->s_namecanon.nc_errno = errno;
107759671Seric 			s->s_namecanon.nc_herrno = h_errno;
107859084Seric 			switch (h_errno)
107959084Seric 			{
108059084Seric 			  case TRY_AGAIN:
108159596Seric 				if (UseNameServer)
108259734Seric 				{
108359734Seric 					char *msg = "Recipient domain nameserver timed out";
108459734Seric 
108559734Seric 					message(msg);
108659734Seric 					if (CurEnv->e_message == NULL)
108760009Seric 						CurEnv->e_message = newstr(msg);
108859734Seric 				}
108959084Seric 				*statp = EX_TEMPFAIL;
109059084Seric 				break;
109159084Seric 
109259084Seric 			  case HOST_NOT_FOUND:
109359084Seric 				*statp = EX_NOHOST;
109459084Seric 				break;
109559084Seric 
109659084Seric 			  case NO_RECOVERY:
109759084Seric 				*statp = EX_SOFTWARE;
109859084Seric 				break;
109959084Seric 
110059084Seric 			  default:
110159084Seric 				*statp = EX_UNAVAILABLE;
110259084Seric 				break;
110359084Seric 			}
110459671Seric 			s->s_namecanon.nc_stat = *statp;
110559084Seric 			if (*statp != EX_TEMPFAIL || UseNameServer)
110659084Seric 				return NULL;
110759084Seric 
110859084Seric 			/*
110959084Seric 			**  Try to look it up in /etc/hosts
111059084Seric 			*/
111159084Seric 
111260089Seric 			hp = gethostbyname(name);
111359084Seric 			if (hp == NULL)
111459084Seric 			{
111559084Seric 				/* no dice there either */
111659671Seric 				s->s_namecanon.nc_stat = *statp = EX_NOHOST;
111759084Seric 				return NULL;
111859084Seric 			}
111959084Seric 
112059671Seric 			s->s_namecanon.nc_stat = *statp = EX_OK;
112160257Seric 			cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
112260257Seric 			s->s_namecanon.nc_cname = newstr(cp);
112360257Seric 			return cp;
112458796Seric 		}
112553751Seric 	}
112660089Seric 	if ((cp = strchr(name, ']')) == NULL)
112753751Seric 		return (NULL);
112840994Sbostic 	*cp = '\0';
112960089Seric 	in_addr = inet_addr(&name[1]);
113058110Seric 
113158110Seric 	/* nope -- ask the name server */
113233932Sbostic 	hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET);
113359671Seric 	s->s_namecanon.nc_errno = errno;
113459671Seric 	s->s_namecanon.nc_herrno = h_errno;
113559671Seric 	s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
113633932Sbostic 	if (hp == NULL)
113759671Seric 	{
113859671Seric 		s->s_namecanon.nc_stat = *statp = EX_NOHOST;
113953751Seric 		return (NULL);
114059671Seric 	}
114153751Seric 
114258110Seric 	/* found a match -- copy out */
114360257Seric 	cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
114459671Seric 	s->s_namecanon.nc_stat = *statp = EX_OK;
114560257Seric 	s->s_namecanon.nc_cname = newstr(cp);
114660257Seric 	return cp;
114733932Sbostic }
114858755Seric /*
114958755Seric **  ANYNET_NTOA -- convert a network address to printable form.
115058755Seric **
115158755Seric **	Parameters:
115258755Seric **		sap -- a pointer to a sockaddr structure.
115358755Seric **
115458755Seric **	Returns:
115558755Seric **		A printable version of that sockaddr.
115658755Seric */
115716911Seric 
115858755Seric char *
115958755Seric anynet_ntoa(sap)
116058755Seric 	register SOCKADDR *sap;
116158755Seric {
116258755Seric 	register char *bp;
116358755Seric 	register char *ap;
116458755Seric 	int l;
116564734Seric 	static char buf[100];
116658755Seric 
116758798Seric 	/* check for null/zero family */
116858798Seric 	if (sap == NULL)
116958798Seric 		return "NULLADDR";
117058798Seric 	if (sap->sa.sa_family == 0)
117158798Seric 		return "0";
117258798Seric 
117364734Seric 	switch (sap->sa.sa_family)
117464734Seric 	{
117564734Seric #ifdef MAYBENEXTRELEASE		/*** UNTESTED *** UNTESTED *** UNTESTED ***/
117664734Seric 	  case AF_UNIX:
117764734Seric 	  	if (sap->sun.sun_path[0] != '\0')
117864734Seric 	  		sprintf(buf, "[UNIX: %.64s]", sap->sun.sun_path);
117964734Seric 	  	else
118064734Seric 	  		sprintf(buf, "[UNIX: localhost]");
118164734Seric 		return buf;
118264734Seric #endif
118364734Seric 
118458778Seric #ifdef NETINET
118564734Seric 	  case AF_INET:
118658755Seric 		return inet_ntoa(((struct sockaddr_in *) sap)->sin_addr);
118758778Seric #endif
118858755Seric 
118964734Seric 	  default:
119064734Seric 	  	/* this case is only to ensure syntactic correctness */
119164734Seric 	  	break;
119264734Seric 	}
119364734Seric 
119458755Seric 	/* unknown family -- just dump bytes */
119558778Seric 	(void) sprintf(buf, "Family %d: ", sap->sa.sa_family);
119658755Seric 	bp = &buf[strlen(buf)];
119758778Seric 	ap = sap->sa.sa_data;
119858778Seric 	for (l = sizeof sap->sa.sa_data; --l >= 0; )
119958755Seric 	{
120058755Seric 		(void) sprintf(bp, "%02x:", *ap++ & 0377);
120158755Seric 		bp += 3;
120258755Seric 	}
120358755Seric 	*--bp = '\0';
120458755Seric 	return buf;
120558755Seric }
120658951Seric /*
120758951Seric **  HOSTNAMEBYANYADDR -- return name of host based on address
120858951Seric **
120958951Seric **	Parameters:
121058951Seric **		sap -- SOCKADDR pointer
121158951Seric **
121258951Seric **	Returns:
121358951Seric **		text representation of host name.
121458951Seric **
121558951Seric **	Side Effects:
121658951Seric **		none.
121758951Seric */
121858755Seric 
121958951Seric char *
122058951Seric hostnamebyanyaddr(sap)
122158951Seric 	register SOCKADDR *sap;
122258951Seric {
122358951Seric 	register struct hostent *hp;
122464734Seric 	int saveretry;
122558951Seric 
122659042Seric #ifdef NAMED_BIND
122759042Seric 	/* shorten name server timeout to avoid higher level timeouts */
122859042Seric 	saveretry = _res.retry;
122959042Seric 	_res.retry = 3;
123059042Seric #endif /* NAMED_BIND */
123159042Seric 
123258951Seric 	switch (sap->sa.sa_family)
123358951Seric 	{
123458951Seric #ifdef NETINET
123558951Seric 	  case AF_INET:
123658951Seric 		hp = gethostbyaddr((char *) &sap->sin.sin_addr,
123758951Seric 			sizeof sap->sin.sin_addr,
123858951Seric 			AF_INET);
123958951Seric 		break;
124058951Seric #endif
124158951Seric 
124258951Seric #ifdef NETISO
124358951Seric 	  case AF_ISO:
124458951Seric 		hp = gethostbyaddr((char *) &sap->siso.siso_addr,
124558951Seric 			sizeof sap->siso.siso_addr,
124658951Seric 			AF_ISO);
124758951Seric 		break;
124858951Seric #endif
124958951Seric 
125064734Seric #ifdef MAYBENEXTRELEASE		/*** UNTESTED *** UNTESTED *** UNTESTED ***/
125164734Seric 	  case AF_UNIX:
125264734Seric 		hp = NULL;
125364734Seric 		break;
125464734Seric #endif
125564734Seric 
125658951Seric 	  default:
125758951Seric 		hp = gethostbyaddr(sap->sa.sa_data,
125858951Seric 			   sizeof sap->sa.sa_data,
125958951Seric 			   sap->sa.sa_family);
126058951Seric 		break;
126158951Seric 	}
126258951Seric 
126359042Seric #ifdef NAMED_BIND
126459042Seric 	_res.retry = saveretry;
126559042Seric #endif /* NAMED_BIND */
126659042Seric 
126758951Seric 	if (hp != NULL)
126858951Seric 		return hp->h_name;
126958951Seric 	else
127058951Seric 	{
127158951Seric 		/* produce a dotted quad */
127258951Seric 		static char buf[512];
127358951Seric 
127458951Seric 		(void) sprintf(buf, "[%s]", anynet_ntoa(sap));
127558951Seric 		return buf;
127658951Seric 	}
127758951Seric }
127858951Seric 
127956795Seric # else /* DAEMON */
128016911Seric /* code for systems without sophisticated networking */
128110758Seric 
128210758Seric /*
128310758Seric **  MYHOSTNAME -- stub version for case of no daemon code.
128411297Seric **
128511297Seric **	Can't convert to upper case here because might be a UUCP name.
128612313Seric **
128712313Seric **	Mark, you can change this to be anything you want......
128810758Seric */
128910758Seric 
129010758Seric char **
129112313Seric myhostname(hostbuf, size)
129210758Seric 	char hostbuf[];
129312313Seric 	int size;
129410758Seric {
129510758Seric 	register FILE *f;
129610758Seric 
129710758Seric 	hostbuf[0] = '\0';
129810758Seric 	f = fopen("/usr/include/whoami", "r");
129910758Seric 	if (f != NULL)
130010758Seric 	{
130112313Seric 		(void) fgets(hostbuf, size, f);
130210758Seric 		fixcrlf(hostbuf, TRUE);
130310758Seric 		(void) fclose(f);
130410758Seric 	}
130510758Seric 	return (NULL);
130610758Seric }
130716911Seric /*
130858951Seric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
130958308Seric **
131058308Seric **	Parameters:
131158308Seric **		fd -- the descriptor
131258308Seric **
131358308Seric **	Returns:
131458308Seric **		The host name associated with this descriptor, if it can
131558308Seric **			be determined.
131658308Seric **		NULL otherwise.
131758308Seric **
131858308Seric **	Side Effects:
131958308Seric **		none
132058308Seric */
132158308Seric 
132258308Seric char *
132358951Seric getauthinfo(fd)
132458308Seric 	int fd;
132558308Seric {
132658308Seric 	return NULL;
132758308Seric }
132858308Seric /*
132916911Seric **  MAPHOSTNAME -- turn a hostname into canonical form
133016911Seric **
133116911Seric **	Parameters:
133256823Seric **		map -- a pointer to the database map.
133360089Seric **		name -- a buffer containing a hostname.
133453751Seric **		avp -- a pointer to a (cf file defined) argument vector.
133559084Seric **		statp -- an exit status (out parameter).
133616911Seric **
133716911Seric **	Returns:
133853751Seric **		mapped host name
133951315Seric **		FALSE otherwise.
134016911Seric **
134116911Seric **	Side Effects:
134260089Seric **		Looks up the host specified in name.  If it is not
134316911Seric **		the canonical name for that host, replace it with
134416911Seric **		the canonical name.  If the name is unknown, or it
134516911Seric **		is already the canonical name, leave it unchanged.
134616911Seric */
134710758Seric 
134816911Seric /*ARGSUSED*/
134953751Seric char *
135060089Seric host_map_lookup(map, name, avp, statp)
135156823Seric 	MAP *map;
135260089Seric 	char *name;
135353751Seric 	char **avp;
135459084Seric 	char *statp;
135516911Seric {
135659084Seric 	register struct hostent *hp;
135759084Seric 
135860089Seric 	hp = gethostbyname(name);
135959084Seric 	if (hp != NULL)
136059084Seric 		return hp->h_name;
136159084Seric 	*statp = EX_NOHOST;
136253751Seric 	return NULL;
136316911Seric }
136416911Seric 
136556795Seric #endif /* DAEMON */
1366