xref: /csrg-svn/usr.sbin/sendmail/src/daemon.c (revision 64561)
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*64561Seric static char sccsid[] = "@(#)daemon.c	8.15 (Berkeley) 09/22/93 (with daemon mode)";
1533780Sbostic #else
16*64561Seric static char sccsid[] = "@(#)daemon.c	8.15 (Berkeley) 09/22/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,
144*64561Seric 			       (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);
29259254Seric 			InChannel = fdopen(t, "r");
29359254Seric 			OutChannel = fdopen(dup(t), "w");
29459254Seric 
29516884Seric 			/* should we check for illegal connection here? XXX */
29659156Seric #ifdef XLA
29759156Seric 			if (!xla_host_ok(RealHostName))
29859156Seric 			{
29959254Seric 				message("421 Too many SMTP sessions for this host");
30059156Seric 				exit(0);
30159156Seric 			}
30259156Seric #endif
30316884Seric 
3047677Seric 			if (tTd(15, 2))
3055978Seric 				printf("getreq: returning\n");
3064636Seric 			return;
3074631Seric 		}
3084631Seric 
3097117Seric 		/* close the port so that others will hang (for a while) */
3109610Seric 		(void) close(t);
3114631Seric 	}
3129886Seric 	/*NOTREACHED*/
3134631Seric }
3145978Seric /*
31510206Seric **  CLRDAEMON -- reset the daemon connection
31610206Seric **
31710206Seric **	Parameters:
31810206Seric **		none.
31910206Seric **
32010206Seric **	Returns:
32110206Seric **		none.
32210206Seric **
32310206Seric **	Side Effects:
32410206Seric **		releases any resources used by the passive daemon.
32510206Seric */
32610206Seric 
32710206Seric clrdaemon()
32810206Seric {
32910206Seric 	if (DaemonSocket >= 0)
33010206Seric 		(void) close(DaemonSocket);
33110206Seric 	DaemonSocket = -1;
33210206Seric }
33310206Seric /*
33458849Seric **  SETDAEMONOPTIONS -- set options for running the daemon
33558849Seric **
33658849Seric **	Parameters:
33758849Seric **		p -- the options line.
33858849Seric **
33958849Seric **	Returns:
34058849Seric **		none.
34158849Seric */
34258849Seric 
34358849Seric setdaemonoptions(p)
34458849Seric 	register char *p;
34558849Seric {
34658873Seric 	if (DaemonAddr.sa.sa_family == AF_UNSPEC)
34758873Seric 		DaemonAddr.sa.sa_family = AF_INET;
34858873Seric 
34958849Seric 	while (p != NULL)
35058849Seric 	{
35158849Seric 		register char *f;
35258849Seric 		register char *v;
35358849Seric 
35458849Seric 		while (isascii(*p) && isspace(*p))
35558849Seric 			p++;
35658849Seric 		if (*p == '\0')
35758849Seric 			break;
35858849Seric 		f = p;
35958849Seric 		p = strchr(p, ',');
36058849Seric 		if (p != NULL)
36158849Seric 			*p++ = '\0';
36258849Seric 		v = strchr(f, '=');
36358849Seric 		if (v == NULL)
36458849Seric 			continue;
36558849Seric 		while (isascii(*++v) && isspace(*v))
36658849Seric 			continue;
36758849Seric 
36858849Seric 		switch (*f)
36958849Seric 		{
37058873Seric 		  case 'F':		/* address family */
37158849Seric 			if (isascii(*v) && isdigit(*v))
37258873Seric 				DaemonAddr.sa.sa_family = atoi(v);
37358873Seric #ifdef NETINET
37458873Seric 			else if (strcasecmp(v, "inet") == 0)
37558873Seric 				DaemonAddr.sa.sa_family = AF_INET;
37658873Seric #endif
37758873Seric #ifdef NETISO
37858873Seric 			else if (strcasecmp(v, "iso") == 0)
37958873Seric 				DaemonAddr.sa.sa_family = AF_ISO;
38058873Seric #endif
38158873Seric #ifdef NETNS
38258873Seric 			else if (strcasecmp(v, "ns") == 0)
38358873Seric 				DaemonAddr.sa.sa_family = AF_NS;
38458873Seric #endif
38558873Seric #ifdef NETX25
38658873Seric 			else if (strcasecmp(v, "x.25") == 0)
38758873Seric 				DaemonAddr.sa.sa_family = AF_CCITT;
38858873Seric #endif
38958849Seric 			else
39058873Seric 				syserr("554 Unknown address family %s in Family=option", v);
39158873Seric 			break;
39258873Seric 
39358873Seric 		  case 'A':		/* address */
39458873Seric 			switch (DaemonAddr.sa.sa_family)
39558849Seric 			{
39658873Seric #ifdef NETINET
39758873Seric 			  case AF_INET:
39858873Seric 				if (isascii(*v) && isdigit(*v))
39958873Seric 					DaemonAddr.sin.sin_addr.s_addr = inet_network(v);
40058873Seric 				else
40158873Seric 				{
40258873Seric 					register struct netent *np;
40358849Seric 
40458873Seric 					np = getnetbyname(v);
40558873Seric 					if (np == NULL)
40658873Seric 						syserr("554 network \"%s\" unknown", v);
40758873Seric 					else
40858873Seric 						DaemonAddr.sin.sin_addr.s_addr = np->n_net;
40958873Seric 				}
41058873Seric 				break;
41158873Seric #endif
41258873Seric 
41358873Seric 			  default:
41458873Seric 				syserr("554 Address= option unsupported for family %d",
41558873Seric 					DaemonAddr.sa.sa_family);
41658873Seric 				break;
41758849Seric 			}
41858849Seric 			break;
41958849Seric 
42058873Seric 		  case 'P':		/* port */
42158873Seric 			switch (DaemonAddr.sa.sa_family)
42258849Seric 			{
42358873Seric 				short port;
42458849Seric 
42558873Seric #ifdef NETINET
42658873Seric 			  case AF_INET:
42758873Seric 				if (isascii(*v) && isdigit(*v))
42864366Seric 					DaemonAddr.sin.sin_port = htons(atoi(v));
42958849Seric 				else
43058873Seric 				{
43158873Seric 					register struct servent *sp;
43258873Seric 
43358873Seric 					sp = getservbyname(v, "tcp");
43458873Seric 					if (sp == NULL)
43558909Seric 						syserr("554 service \"%s\" unknown", v);
43658873Seric 					else
43758873Seric 						DaemonAddr.sin.sin_port = sp->s_port;
43858873Seric 				}
43958873Seric 				break;
44058873Seric #endif
44158873Seric 
44258873Seric #ifdef NETISO
44358873Seric 			  case AF_ISO:
44458873Seric 				/* assume two byte transport selector */
44558873Seric 				if (isascii(*v) && isdigit(*v))
44664366Seric 					port = htons(atoi(v));
44758873Seric 				else
44858873Seric 				{
44958873Seric 					register struct servent *sp;
45058873Seric 
45158873Seric 					sp = getservbyname(v, "tcp");
45258873Seric 					if (sp == NULL)
45358909Seric 						syserr("554 service \"%s\" unknown", v);
45458873Seric 					else
45558873Seric 						port = sp->s_port;
45658873Seric 				}
45758873Seric 				bcopy((char *) &port, TSEL(&DaemonAddr.siso), 2);
45858873Seric 				break;
45958873Seric #endif
46058873Seric 
46158873Seric 			  default:
46258873Seric 				syserr("554 Port= option unsupported for family %d",
46358873Seric 					DaemonAddr.sa.sa_family);
46458873Seric 				break;
46558849Seric 			}
46658849Seric 			break;
46759783Seric 
46859783Seric 		  case 'L':		/* listen queue size */
46959783Seric 			ListenQueueSize = atoi(v);
47059783Seric 			break;
47164381Seric 
47264381Seric 		  case 'S':		/* send buffer size */
47364381Seric 			TcpSndBufferSize = atoi(v);
47464381Seric 			break;
47564381Seric 
47664381Seric 		  case 'R':		/* receive buffer size */
47764381Seric 			TcpRcvBufferSize = atoi(v);
47864381Seric 			break;
47958849Seric 		}
48058849Seric 	}
48158849Seric }
48258849Seric /*
4836039Seric **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
4846039Seric **
4856039Seric **	Parameters:
4866039Seric **		host -- the name of the host.
4876633Seric **		port -- the port number to connect to.
48853739Seric **		mci -- a pointer to the mail connection information
48953739Seric **			structure to be filled in.
49052106Seric **		usesecureport -- if set, use a low numbered (reserved)
49152106Seric **			port to provide some rudimentary authentication.
4926039Seric **
4936039Seric **	Returns:
4946039Seric **		An exit code telling whether the connection could be
4956039Seric **			made and if not why not.
4966039Seric **
4976039Seric **	Side Effects:
4986039Seric **		none.
4996039Seric */
5005978Seric 
50158755Seric SOCKADDR	CurHostAddr;		/* address of current host */
50258305Seric 
50354967Seric int
50453739Seric makeconnection(host, port, mci, usesecureport)
5056039Seric 	char *host;
5067286Seric 	u_short port;
50754967Seric 	register MCI *mci;
50852106Seric 	bool usesecureport;
5096039Seric {
51029430Sbloom 	register int i, s;
51129430Sbloom 	register struct hostent *hp = (struct hostent *)NULL;
51258755Seric 	SOCKADDR addr;
51352106Seric 	int sav_errno;
51458755Seric 	int addrlen;
51535651Seric #ifdef NAMED_BIND
51635651Seric 	extern int h_errno;
51735651Seric #endif
5186039Seric 
5196039Seric 	/*
5206039Seric 	**  Set up the address for the mailer.
5219308Seric 	**	Accept "[a.b.c.d]" syntax for host name.
5226039Seric 	*/
5236039Seric 
52435651Seric #ifdef NAMED_BIND
52525475Smiriam 	h_errno = 0;
52635651Seric #endif
52725475Smiriam 	errno = 0;
52858864Seric 	bzero(&CurHostAddr, sizeof CurHostAddr);
52964334Seric 	SmtpPhase = mci->mci_phase = "initial connection";
53058906Seric 	CurHostName = host;
53125475Smiriam 
5329308Seric 	if (host[0] == '[')
5339308Seric 	{
53411147Seric 		long hid;
53556795Seric 		register char *p = strchr(host, ']');
5369308Seric 
53711147Seric 		if (p != NULL)
5389308Seric 		{
53911147Seric 			*p = '\0';
54059884Seric #ifdef NETINET
54111147Seric 			hid = inet_addr(&host[1]);
54258360Seric 			if (hid == -1)
54359884Seric #endif
54458360Seric 			{
54558360Seric 				/* try it as a host name (avoid MX lookup) */
54658360Seric 				hp = gethostbyname(&host[1]);
54758360Seric 				*p = ']';
54858360Seric 				goto gothostent;
54958360Seric 			}
55011147Seric 			*p = ']';
5519308Seric 		}
55258360Seric 		if (p == NULL)
5539308Seric 		{
55458151Seric 			usrerr("553 Invalid numeric domain spec \"%s\"", host);
5559308Seric 			return (EX_NOHOST);
5569308Seric 		}
55759884Seric #ifdef NETINET
55859884Seric 		addr.sin.sin_family = AF_INET;		/*XXX*/
55958778Seric 		addr.sin.sin_addr.s_addr = hid;
56059884Seric #endif
5619308Seric 	}
5629610Seric 	else
5639610Seric 	{
56429430Sbloom 		hp = gethostbyname(host);
56558360Seric gothostent:
56625475Smiriam 		if (hp == NULL)
56724945Seric 		{
56835651Seric #ifdef NAMED_BIND
56925475Smiriam 			if (errno == ETIMEDOUT || h_errno == TRY_AGAIN)
57025475Smiriam 				return (EX_TEMPFAIL);
57125657Seric 
57235651Seric 			/* if name server is specified, assume temp fail */
57335651Seric 			if (errno == ECONNREFUSED && UseNameServer)
57435651Seric 				return (EX_TEMPFAIL);
57535651Seric #endif
57625475Smiriam 			return (EX_NOHOST);
57724945Seric 		}
57858778Seric 		addr.sa.sa_family = hp->h_addrtype;
57958778Seric 		switch (hp->h_addrtype)
58058778Seric 		{
58158778Seric #ifdef NETINET
58258778Seric 		  case AF_INET:
58358755Seric 			bcopy(hp->h_addr,
58458778Seric 				&addr.sin.sin_addr,
58558755Seric 				hp->h_length);
58658778Seric 			break;
58758778Seric #endif
58858778Seric 
58958778Seric 		  default:
59058755Seric 			bcopy(hp->h_addr,
59158778Seric 				addr.sa.sa_data,
59258755Seric 				hp->h_length);
59358778Seric 			break;
59458778Seric 		}
59529430Sbloom 		i = 1;
5969610Seric 	}
5979610Seric 
5989610Seric 	/*
5999610Seric 	**  Determine the port number.
6009610Seric 	*/
6019610Seric 
60210011Seric 	if (port != 0)
60358755Seric 		port = htons(port);
60410011Seric 	else
6059610Seric 	{
6069610Seric 		register struct servent *sp = getservbyname("smtp", "tcp");
6079610Seric 
6089610Seric 		if (sp == NULL)
6099610Seric 		{
61058909Seric 			syserr("554 makeconnection: service \"smtp\" unknown");
61157977Seric 			return (EX_OSERR);
6129610Seric 		}
61358755Seric 		port = sp->s_port;
6149610Seric 	}
6156039Seric 
61658778Seric 	switch (addr.sa.sa_family)
61758755Seric 	{
61859884Seric #ifdef NETINET
61958755Seric 	  case AF_INET:
62058778Seric 		addr.sin.sin_port = port;
62158755Seric 		addrlen = sizeof (struct sockaddr_in);
62258755Seric 		break;
62359884Seric #endif
62458755Seric 
62558755Seric #ifdef NETISO
62658755Seric 	  case AF_ISO:
62758755Seric 		/* assume two byte transport selector */
62858755Seric 		bcopy((char *) &port, TSEL((struct sockaddr_iso *) &addr), 2);
62958755Seric 		addrlen = sizeof (struct sockaddr_iso);
63058755Seric 		break;
63158755Seric #endif
63258755Seric 
63358755Seric 	  default:
63458778Seric 		syserr("Can't connect to address family %d", addr.sa.sa_family);
63558755Seric 		return (EX_NOHOST);
63658755Seric 	}
63758755Seric 
6386039Seric 	/*
6396039Seric 	**  Try to actually open the connection.
6406039Seric 	*/
6416039Seric 
64259156Seric #ifdef XLA
64359156Seric 	/* if too many connections, don't bother trying */
64459156Seric 	if (!xla_noqueue_ok(host))
64559156Seric 		return EX_TEMPFAIL;
64659156Seric #endif
64759156Seric 
64857736Seric 	for (;;)
64952106Seric 	{
65057736Seric 		if (tTd(16, 1))
65158755Seric 			printf("makeconnection (%s [%s])\n",
65258755Seric 				host, anynet_ntoa(&addr));
65352106Seric 
65458588Seric 		/* save for logging */
65558588Seric 		CurHostAddr = addr;
65658588Seric 
65757736Seric 		if (usesecureport)
65857736Seric 		{
65957736Seric 			int rport = IPPORT_RESERVED - 1;
6606039Seric 
66157736Seric 			s = rresvport(&rport);
66257736Seric 		}
66357736Seric 		else
66457736Seric 		{
66557736Seric 			s = socket(AF_INET, SOCK_STREAM, 0);
66657736Seric 		}
66757736Seric 		if (s < 0)
66857736Seric 		{
66957736Seric 			sav_errno = errno;
67057736Seric 			syserr("makeconnection: no socket");
67157736Seric 			goto failure;
67257736Seric 		}
67310347Seric 
67464381Seric #ifdef SO_SNDBUF
67564381Seric 		if (TcpSndBufferSize > 0)
67664381Seric 		{
67764381Seric 			if (setsockopt(s, SOL_SOCKET, SO_SNDBUF,
678*64561Seric 				       (char *) &TcpSndBufferSize,
67964381Seric 				       sizeof(TcpSndBufferSize)) < 0)
68064381Seric 				syserr("makeconnection: setsockopt(SO_SNDBUF)");
68164381Seric 		}
68264381Seric #endif
68364381Seric 
68457736Seric 		if (tTd(16, 1))
68557736Seric 			printf("makeconnection: fd=%d\n", s);
68657736Seric 
68757736Seric 		/* turn on network debugging? */
68857736Seric 		if (tTd(16, 101))
68957736Seric 		{
69057736Seric 			int on = 1;
69157736Seric 			(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG,
69257736Seric 					  (char *)&on, sizeof on);
69357736Seric 		}
69457736Seric 		if (CurEnv->e_xfp != NULL)
69557736Seric 			(void) fflush(CurEnv->e_xfp);		/* for debugging */
69657736Seric 		errno = 0;					/* for debugging */
69758755Seric 		if (connect(s, (struct sockaddr *) &addr, addrlen) >= 0)
69857736Seric 			break;
69957736Seric 
70057736Seric 		/* couldn't connect.... figure out why */
70127744Sbloom 		sav_errno = errno;
70227744Sbloom 		(void) close(s);
70329430Sbloom 		if (hp && hp->h_addr_list[i])
70429430Sbloom 		{
70557736Seric 			if (tTd(16, 1))
70658755Seric 				printf("Connect failed (%s); trying new address....\n",
70758755Seric 					errstring(sav_errno));
70858778Seric 			switch (addr.sa.sa_family)
70958778Seric 			{
71058778Seric #ifdef NETINET
71158778Seric 			  case AF_INET:
71258755Seric 				bcopy(hp->h_addr_list[i++],
71358778Seric 				      &addr.sin.sin_addr,
71458755Seric 				      hp->h_length);
71558778Seric 				break;
71658778Seric #endif
71758778Seric 
71858778Seric 			  default:
71958755Seric 				bcopy(hp->h_addr_list[i++],
72058778Seric 					addr.sa.sa_data,
72152106Seric 					hp->h_length);
72258778Seric 				break;
72358778Seric 			}
72457736Seric 			continue;
72529430Sbloom 		}
72629430Sbloom 
7276039Seric 		/* failure, decide if temporary or not */
7286039Seric 	failure:
72959254Seric #ifdef XLA
73059254Seric 		xla_host_end(host);
73159254Seric #endif
73258542Seric 		if (transienterror(sav_errno))
73358542Seric 			return EX_TEMPFAIL;
73458542Seric 		else
73558542Seric 		{
73658542Seric 			message("%s", errstring(sav_errno));
73758542Seric 			return (EX_UNAVAILABLE);
7386039Seric 		}
7396039Seric 	}
7406039Seric 
7416039Seric 	/* connection ok, put it into canonical form */
74253739Seric 	mci->mci_out = fdopen(s, "w");
74353739Seric 	mci->mci_in = fdopen(dup(s), "r");
7446039Seric 
74510098Seric 	return (EX_OK);
7466039Seric }
74710758Seric /*
74810758Seric **  MYHOSTNAME -- return the name of this host.
74910758Seric **
75010758Seric **	Parameters:
75110758Seric **		hostbuf -- a place to return the name of this host.
75212313Seric **		size -- the size of hostbuf.
75310758Seric **
75410758Seric **	Returns:
75510758Seric **		A list of aliases for this host.
75610758Seric **
75710758Seric **	Side Effects:
75864338Seric **		Adds numeric codes to $=w.
75910758Seric */
7606039Seric 
76110758Seric char **
76212313Seric myhostname(hostbuf, size)
76310758Seric 	char hostbuf[];
76412313Seric 	int size;
76510758Seric {
76658110Seric 	register struct hostent *hp;
76710758Seric 	extern struct hostent *gethostbyname();
76810758Seric 
76923120Seric 	if (gethostname(hostbuf, size) < 0)
77023120Seric 	{
77123120Seric 		(void) strcpy(hostbuf, "localhost");
77223120Seric 	}
77311147Seric 	hp = gethostbyname(hostbuf);
77411147Seric 	if (hp != NULL)
77516877Seric 	{
77658110Seric 		(void) strncpy(hostbuf, hp->h_name, size - 1);
77758110Seric 		hostbuf[size - 1] = '\0';
77858110Seric 
77958110Seric 		if (hp->h_addrtype == AF_INET && hp->h_length == 4)
78058110Seric 		{
78158110Seric 			register int i;
78258110Seric 
78364338Seric 			for (i = 0; hp->h_addr_list[i] != NULL; i++)
78458110Seric 			{
78564338Seric 				char ipbuf[100];
78664338Seric 
78764338Seric 				sprintf(ipbuf, "[%s]",
78864338Seric 					inet_ntoa(*((struct in_addr *) hp->h_addr_list[i])));
78964338Seric 				setclass('w', ipbuf);
79058110Seric 			}
79158110Seric 		}
79258110Seric 
79311147Seric 		return (hp->h_aliases);
79416877Seric 	}
79510758Seric 	else
79610758Seric 		return (NULL);
79710758Seric }
79851315Seric /*
79958951Seric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
80058308Seric **
80158951Seric **	Uses RFC1413 protocol to try to get info from the other end.
80258951Seric **
80358308Seric **	Parameters:
80458308Seric **		fd -- the descriptor
80558308Seric **
80658308Seric **	Returns:
80758951Seric **		The user@host information associated with this descriptor.
80858308Seric **
80958308Seric **	Side Effects:
81058951Seric **		Sets RealHostName to the name of the host at the other end.
81158308Seric */
81258308Seric 
81358951Seric #ifdef IDENTPROTO
81458951Seric 
81558951Seric static jmp_buf	CtxAuthTimeout;
81658951Seric 
81758951Seric static
81858951Seric authtimeout()
81958951Seric {
82058951Seric 	longjmp(CtxAuthTimeout, 1);
82158951Seric }
82258951Seric 
82358951Seric #endif
82458951Seric 
82558308Seric char *
82658951Seric getauthinfo(fd)
82758308Seric 	int fd;
82858308Seric {
82958951Seric 	SOCKADDR fa;
83058951Seric 	int falen;
83159104Seric 	register char *p;
83258951Seric #ifdef IDENTPROTO
83358951Seric 	SOCKADDR la;
83458951Seric 	int lalen;
83558951Seric 	register struct servent *sp;
83658951Seric 	int s;
83758951Seric 	int i;
83858951Seric 	EVENT *ev;
83958951Seric #endif
84058951Seric 	static char hbuf[MAXNAME * 2 + 2];
84158951Seric 	extern char *hostnamebyanyaddr();
84258951Seric 	extern char RealUserName[];			/* main.c */
84358308Seric 
84458951Seric 	falen = sizeof fa;
84558951Seric 	if (getpeername(fd, &fa.sa, &falen) < 0 || falen <= 0)
84658951Seric 	{
84758951Seric 		RealHostName = "localhost";
84858951Seric 		(void) sprintf(hbuf, "%s@localhost", RealUserName);
84958957Seric 		if (tTd(9, 1))
85058951Seric 			printf("getauthinfo: %s\n", hbuf);
85158951Seric 		return hbuf;
85258951Seric 	}
85358951Seric 
85464086Seric 	p = hostnamebyanyaddr(&fa);
85564086Seric 	RealHostName = newstr(p);
85658951Seric 	RealHostAddr = fa;
85758951Seric 
85858951Seric #ifdef IDENTPROTO
85958951Seric 	lalen = sizeof la;
86058951Seric 	if (fa.sa.sa_family != AF_INET ||
86158951Seric 	    getsockname(fd, &la.sa, &lalen) < 0 || lalen <= 0 ||
86258951Seric 	    la.sa.sa_family != AF_INET)
86358951Seric 	{
86458951Seric 		/* no ident info */
86558951Seric 		goto noident;
86658951Seric 	}
86758951Seric 
86858951Seric 	/* create ident query */
86960489Seric 	(void) sprintf(hbuf, "%d,%d\r\n",
87060489Seric 		ntohs(fa.sin.sin_port), ntohs(la.sin.sin_port));
87158951Seric 
87258951Seric 	/* create local address */
87358951Seric 	bzero(&la, sizeof la);
87458951Seric 
87558951Seric 	/* create foreign address */
87658951Seric 	sp = getservbyname("auth", "tcp");
87758951Seric 	if (sp != NULL)
87858951Seric 		fa.sin.sin_port = sp->s_port;
87958308Seric 	else
88059097Seric 		fa.sin.sin_port = htons(113);
88158951Seric 
88258951Seric 	s = -1;
88358951Seric 	if (setjmp(CtxAuthTimeout) != 0)
88458951Seric 	{
88558951Seric 		if (s >= 0)
88658951Seric 			(void) close(s);
88758951Seric 		goto noident;
88858951Seric 	}
88958951Seric 
89058951Seric 	/* put a timeout around the whole thing */
89164255Seric 	ev = setevent(TimeOuts.to_ident, authtimeout, 0);
89258951Seric 
89358951Seric 	/* connect to foreign IDENT server */
89458951Seric 	s = socket(AF_INET, SOCK_STREAM, 0);
89558951Seric 	if (s < 0)
89658951Seric 	{
89758951Seric 		clrevent(ev);
89858951Seric 		goto noident;
89958951Seric 	}
90058951Seric 	if (connect(s, &fa.sa, sizeof fa.sin) < 0)
90158951Seric 	{
90258951Seric closeident:
90358951Seric 		(void) close(s);
90458951Seric 		clrevent(ev);
90558951Seric 		goto noident;
90658951Seric 	}
90758951Seric 
90858957Seric 	if (tTd(9, 10))
90958951Seric 		printf("getauthinfo: sent %s", hbuf);
91058951Seric 
91158951Seric 	/* send query */
91258951Seric 	if (write(s, hbuf, strlen(hbuf)) < 0)
91358951Seric 		goto closeident;
91458951Seric 
91558951Seric 	/* get result */
91658951Seric 	i = read(s, hbuf, sizeof hbuf);
91758951Seric 	(void) close(s);
91858951Seric 	clrevent(ev);
91958951Seric 	if (i <= 0)
92058951Seric 		goto noident;
92158951Seric 	if (hbuf[--i] == '\n' && hbuf[--i] == '\r')
92258951Seric 		i--;
92358951Seric 	hbuf[++i] = '\0';
92458951Seric 
92558957Seric 	if (tTd(9, 3))
92658951Seric 		printf("getauthinfo:  got %s\n", hbuf);
92758951Seric 
92858951Seric 	/* parse result */
92958951Seric 	p = strchr(hbuf, ':');
93058951Seric 	if (p == NULL)
93158951Seric 	{
93258951Seric 		/* malformed response */
93358951Seric 		goto noident;
93458951Seric 	}
93558951Seric 	while (isascii(*++p) && isspace(*p))
93658951Seric 		continue;
93758951Seric 	if (strncasecmp(p, "userid", 6) != 0)
93858951Seric 	{
93958951Seric 		/* presumably an error string */
94058951Seric 		goto noident;
94158951Seric 	}
94258951Seric 	p += 6;
94358951Seric 	while (isascii(*p) && isspace(*p))
94458951Seric 		p++;
94558951Seric 	if (*p++ != ':')
94658951Seric 	{
94758951Seric 		/* either useridxx or malformed response */
94858951Seric 		goto noident;
94958951Seric 	}
95058951Seric 
95158951Seric 	/* p now points to the OSTYPE field */
95258951Seric 	p = strchr(p, ':');
95358951Seric 	if (p == NULL)
95458951Seric 	{
95558951Seric 		/* malformed response */
95658951Seric 		goto noident;
95758951Seric 	}
95858951Seric 
95958957Seric 	/* 1413 says don't do this -- but it's broken otherwise */
96058957Seric 	while (isascii(*++p) && isspace(*p))
96158957Seric 		continue;
96258957Seric 
96358951Seric 	/* p now points to the authenticated name */
96458951Seric 	(void) sprintf(hbuf, "%s@%s", p, RealHostName);
96558957Seric 	goto finish;
96658957Seric 
96758957Seric #endif /* IDENTPROTO */
96858957Seric 
96958957Seric noident:
97058957Seric 	(void) strcpy(hbuf, RealHostName);
97158957Seric 
97258957Seric finish:
97358951Seric 	if (RealHostName[0] != '[')
97458951Seric 	{
97558951Seric 		p = &hbuf[strlen(hbuf)];
97658951Seric 		(void) sprintf(p, " [%s]", anynet_ntoa(&RealHostAddr));
97758951Seric 	}
97858957Seric 	if (tTd(9, 1))
97958951Seric 		printf("getauthinfo: %s\n", hbuf);
98058308Seric 	return hbuf;
98158308Seric }
98258308Seric /*
98360089Seric **  HOST_MAP_LOOKUP -- turn a hostname into canonical form
98453751Seric **
98553751Seric **	Parameters:
98656823Seric **		map -- a pointer to this map (unused).
98760089Seric **		name -- the (presumably unqualified) hostname.
98860257Seric **		av -- unused -- for compatibility with other mapping
98955019Seric **			functions.
99059084Seric **		statp -- an exit status (out parameter) -- set to
99159084Seric **			EX_TEMPFAIL if the name server is unavailable.
99253751Seric **
99353751Seric **	Returns:
99453751Seric **		The mapping, if found.
99553751Seric **		NULL if no mapping found.
99653751Seric **
99753751Seric **	Side Effects:
99853751Seric **		Looks up the host specified in hbuf.  If it is not
99953751Seric **		the canonical name for that host, return the canonical
100053751Seric **		name.
100153751Seric */
100251315Seric 
100353751Seric char *
100460257Seric host_map_lookup(map, name, av, statp)
100556823Seric 	MAP *map;
100660089Seric 	char *name;
100760257Seric 	char **av;
100859084Seric 	int *statp;
100916911Seric {
101016911Seric 	register struct hostent *hp;
101133932Sbostic 	u_long in_addr;
101256823Seric 	char *cp;
101358110Seric 	int i;
101459671Seric 	register STAB *s;
101560257Seric 	char hbuf[MAXNAME];
101659671Seric 	extern struct hostent *gethostbyaddr();
101759671Seric 	extern int h_errno;
101816911Seric 
101925574Smiriam 	/*
102059671Seric 	**  See if we have already looked up this name.  If so, just
102159671Seric 	**  return it.
102259671Seric 	*/
102353751Seric 
102460089Seric 	s = stab(name, ST_NAMECANON, ST_ENTER);
102559671Seric 	if (bitset(NCF_VALID, s->s_namecanon.nc_flags))
102659671Seric 	{
102759986Seric 		if (tTd(9, 1))
102860089Seric 			printf("host_map_lookup(%s) => CACHE %s\n",
102960089Seric 				name, s->s_namecanon.nc_cname);
103059671Seric 		errno = s->s_namecanon.nc_errno;
103159671Seric 		h_errno = s->s_namecanon.nc_herrno;
103259671Seric 		*statp = s->s_namecanon.nc_stat;
103359671Seric 		return s->s_namecanon.nc_cname;
103459671Seric 	}
103559671Seric 
103659671Seric 	/*
103759671Seric 	**  If first character is a bracket, then it is an address
103859671Seric 	**  lookup.  Address is copied into a temporary buffer to
103960089Seric 	**  strip the brackets and to preserve name if address is
104059671Seric 	**  unknown.
104159671Seric 	*/
104259671Seric 
104360089Seric 	if (*name != '[')
104453751Seric 	{
104555019Seric 		extern bool getcanonname();
104655019Seric 
104758798Seric 		if (tTd(9, 1))
104860089Seric 			printf("host_map_lookup(%s) => ", name);
104959671Seric 		s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
105060089Seric 		(void) strcpy(hbuf, name);
105163842Seric 		if (getcanonname(hbuf, sizeof hbuf - 1, TRUE))
105258796Seric 		{
105358796Seric 			if (tTd(9, 1))
105458796Seric 				printf("%s\n", hbuf);
105560257Seric 			cp = map_rewrite(map, hbuf, strlen(hbuf), av);
105660257Seric 			s->s_namecanon.nc_cname = newstr(cp);
105760257Seric 			return cp;
105858796Seric 		}
105953751Seric 		else
106058796Seric 		{
106159084Seric 			register struct hostent *hp;
106259084Seric 
106358796Seric 			if (tTd(9, 1))
106459084Seric 				printf("FAIL (%d)\n", h_errno);
106559671Seric 			s->s_namecanon.nc_errno = errno;
106659671Seric 			s->s_namecanon.nc_herrno = h_errno;
106759084Seric 			switch (h_errno)
106859084Seric 			{
106959084Seric 			  case TRY_AGAIN:
107059596Seric 				if (UseNameServer)
107159734Seric 				{
107259734Seric 					char *msg = "Recipient domain nameserver timed out";
107359734Seric 
107459734Seric 					message(msg);
107559734Seric 					if (CurEnv->e_message == NULL)
107660009Seric 						CurEnv->e_message = newstr(msg);
107759734Seric 				}
107859084Seric 				*statp = EX_TEMPFAIL;
107959084Seric 				break;
108059084Seric 
108159084Seric 			  case HOST_NOT_FOUND:
108259084Seric 				*statp = EX_NOHOST;
108359084Seric 				break;
108459084Seric 
108559084Seric 			  case NO_RECOVERY:
108659084Seric 				*statp = EX_SOFTWARE;
108759084Seric 				break;
108859084Seric 
108959084Seric 			  default:
109059084Seric 				*statp = EX_UNAVAILABLE;
109159084Seric 				break;
109259084Seric 			}
109359671Seric 			s->s_namecanon.nc_stat = *statp;
109459084Seric 			if (*statp != EX_TEMPFAIL || UseNameServer)
109559084Seric 				return NULL;
109659084Seric 
109759084Seric 			/*
109859084Seric 			**  Try to look it up in /etc/hosts
109959084Seric 			*/
110059084Seric 
110160089Seric 			hp = gethostbyname(name);
110259084Seric 			if (hp == NULL)
110359084Seric 			{
110459084Seric 				/* no dice there either */
110559671Seric 				s->s_namecanon.nc_stat = *statp = EX_NOHOST;
110659084Seric 				return NULL;
110759084Seric 			}
110859084Seric 
110959671Seric 			s->s_namecanon.nc_stat = *statp = EX_OK;
111060257Seric 			cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
111160257Seric 			s->s_namecanon.nc_cname = newstr(cp);
111260257Seric 			return cp;
111358796Seric 		}
111453751Seric 	}
111560089Seric 	if ((cp = strchr(name, ']')) == NULL)
111653751Seric 		return (NULL);
111740994Sbostic 	*cp = '\0';
111860089Seric 	in_addr = inet_addr(&name[1]);
111958110Seric 
112058110Seric 	/* nope -- ask the name server */
112133932Sbostic 	hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET);
112259671Seric 	s->s_namecanon.nc_errno = errno;
112359671Seric 	s->s_namecanon.nc_herrno = h_errno;
112459671Seric 	s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
112533932Sbostic 	if (hp == NULL)
112659671Seric 	{
112759671Seric 		s->s_namecanon.nc_stat = *statp = EX_NOHOST;
112853751Seric 		return (NULL);
112959671Seric 	}
113053751Seric 
113158110Seric 	/* found a match -- copy out */
113260257Seric 	cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
113359671Seric 	s->s_namecanon.nc_stat = *statp = EX_OK;
113460257Seric 	s->s_namecanon.nc_cname = newstr(cp);
113560257Seric 	return cp;
113633932Sbostic }
113758755Seric /*
113858755Seric **  ANYNET_NTOA -- convert a network address to printable form.
113958755Seric **
114058755Seric **	Parameters:
114158755Seric **		sap -- a pointer to a sockaddr structure.
114258755Seric **
114358755Seric **	Returns:
114458755Seric **		A printable version of that sockaddr.
114558755Seric */
114616911Seric 
114758755Seric char *
114858755Seric anynet_ntoa(sap)
114958755Seric 	register SOCKADDR *sap;
115058755Seric {
115158755Seric 	register char *bp;
115258755Seric 	register char *ap;
115358755Seric 	int l;
115458755Seric 	static char buf[80];
115558755Seric 
115658798Seric 	/* check for null/zero family */
115758798Seric 	if (sap == NULL)
115858798Seric 		return "NULLADDR";
115958798Seric 	if (sap->sa.sa_family == 0)
116058798Seric 		return "0";
116158798Seric 
116258778Seric #ifdef NETINET
116358778Seric 	if (sap->sa.sa_family == AF_INET)
116458755Seric 		return inet_ntoa(((struct sockaddr_in *) sap)->sin_addr);
116558778Seric #endif
116658755Seric 
116758755Seric 	/* unknown family -- just dump bytes */
116858778Seric 	(void) sprintf(buf, "Family %d: ", sap->sa.sa_family);
116958755Seric 	bp = &buf[strlen(buf)];
117058778Seric 	ap = sap->sa.sa_data;
117158778Seric 	for (l = sizeof sap->sa.sa_data; --l >= 0; )
117258755Seric 	{
117358755Seric 		(void) sprintf(bp, "%02x:", *ap++ & 0377);
117458755Seric 		bp += 3;
117558755Seric 	}
117658755Seric 	*--bp = '\0';
117758755Seric 	return buf;
117858755Seric }
117958951Seric /*
118058951Seric **  HOSTNAMEBYANYADDR -- return name of host based on address
118158951Seric **
118258951Seric **	Parameters:
118358951Seric **		sap -- SOCKADDR pointer
118458951Seric **
118558951Seric **	Returns:
118658951Seric **		text representation of host name.
118758951Seric **
118858951Seric **	Side Effects:
118958951Seric **		none.
119058951Seric */
119158755Seric 
119258951Seric char *
119358951Seric hostnamebyanyaddr(sap)
119458951Seric 	register SOCKADDR *sap;
119558951Seric {
119658951Seric 	register struct hostent *hp;
119758951Seric 
119859042Seric #ifdef NAMED_BIND
119959042Seric 	int saveretry;
120059042Seric 
120159042Seric 	/* shorten name server timeout to avoid higher level timeouts */
120259042Seric 	saveretry = _res.retry;
120359042Seric 	_res.retry = 3;
120459042Seric #endif /* NAMED_BIND */
120559042Seric 
120658951Seric 	switch (sap->sa.sa_family)
120758951Seric 	{
120858951Seric #ifdef NETINET
120958951Seric 	  case AF_INET:
121058951Seric 		hp = gethostbyaddr((char *) &sap->sin.sin_addr,
121158951Seric 			sizeof sap->sin.sin_addr,
121258951Seric 			AF_INET);
121358951Seric 		break;
121458951Seric #endif
121558951Seric 
121658951Seric #ifdef NETISO
121758951Seric 	  case AF_ISO:
121858951Seric 		hp = gethostbyaddr((char *) &sap->siso.siso_addr,
121958951Seric 			sizeof sap->siso.siso_addr,
122058951Seric 			AF_ISO);
122158951Seric 		break;
122258951Seric #endif
122358951Seric 
122458951Seric 	  default:
122558951Seric 		hp = gethostbyaddr(sap->sa.sa_data,
122658951Seric 			   sizeof sap->sa.sa_data,
122758951Seric 			   sap->sa.sa_family);
122858951Seric 		break;
122958951Seric 	}
123058951Seric 
123159042Seric #ifdef NAMED_BIND
123259042Seric 	_res.retry = saveretry;
123359042Seric #endif /* NAMED_BIND */
123459042Seric 
123558951Seric 	if (hp != NULL)
123658951Seric 		return hp->h_name;
123758951Seric 	else
123858951Seric 	{
123958951Seric 		/* produce a dotted quad */
124058951Seric 		static char buf[512];
124158951Seric 
124258951Seric 		(void) sprintf(buf, "[%s]", anynet_ntoa(sap));
124358951Seric 		return buf;
124458951Seric 	}
124558951Seric }
124658951Seric 
124756795Seric # else /* DAEMON */
124816911Seric /* code for systems without sophisticated networking */
124910758Seric 
125010758Seric /*
125110758Seric **  MYHOSTNAME -- stub version for case of no daemon code.
125211297Seric **
125311297Seric **	Can't convert to upper case here because might be a UUCP name.
125412313Seric **
125512313Seric **	Mark, you can change this to be anything you want......
125610758Seric */
125710758Seric 
125810758Seric char **
125912313Seric myhostname(hostbuf, size)
126010758Seric 	char hostbuf[];
126112313Seric 	int size;
126210758Seric {
126310758Seric 	register FILE *f;
126410758Seric 
126510758Seric 	hostbuf[0] = '\0';
126610758Seric 	f = fopen("/usr/include/whoami", "r");
126710758Seric 	if (f != NULL)
126810758Seric 	{
126912313Seric 		(void) fgets(hostbuf, size, f);
127010758Seric 		fixcrlf(hostbuf, TRUE);
127110758Seric 		(void) fclose(f);
127210758Seric 	}
127310758Seric 	return (NULL);
127410758Seric }
127516911Seric /*
127658951Seric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
127758308Seric **
127858308Seric **	Parameters:
127958308Seric **		fd -- the descriptor
128058308Seric **
128158308Seric **	Returns:
128258308Seric **		The host name associated with this descriptor, if it can
128358308Seric **			be determined.
128458308Seric **		NULL otherwise.
128558308Seric **
128658308Seric **	Side Effects:
128758308Seric **		none
128858308Seric */
128958308Seric 
129058308Seric char *
129158951Seric getauthinfo(fd)
129258308Seric 	int fd;
129358308Seric {
129458308Seric 	return NULL;
129558308Seric }
129658308Seric /*
129716911Seric **  MAPHOSTNAME -- turn a hostname into canonical form
129816911Seric **
129916911Seric **	Parameters:
130056823Seric **		map -- a pointer to the database map.
130160089Seric **		name -- a buffer containing a hostname.
130253751Seric **		avp -- a pointer to a (cf file defined) argument vector.
130359084Seric **		statp -- an exit status (out parameter).
130416911Seric **
130516911Seric **	Returns:
130653751Seric **		mapped host name
130751315Seric **		FALSE otherwise.
130816911Seric **
130916911Seric **	Side Effects:
131060089Seric **		Looks up the host specified in name.  If it is not
131116911Seric **		the canonical name for that host, replace it with
131216911Seric **		the canonical name.  If the name is unknown, or it
131316911Seric **		is already the canonical name, leave it unchanged.
131416911Seric */
131510758Seric 
131616911Seric /*ARGSUSED*/
131753751Seric char *
131860089Seric host_map_lookup(map, name, avp, statp)
131956823Seric 	MAP *map;
132060089Seric 	char *name;
132153751Seric 	char **avp;
132259084Seric 	char *statp;
132316911Seric {
132459084Seric 	register struct hostent *hp;
132559084Seric 
132660089Seric 	hp = gethostbyname(name);
132759084Seric 	if (hp != NULL)
132859084Seric 		return hp->h_name;
132959084Seric 	*statp = EX_NOHOST;
133053751Seric 	return NULL;
133116911Seric }
133216911Seric 
133356795Seric #endif /* DAEMON */
1334