xref: /csrg-svn/usr.sbin/sendmail/src/daemon.c (revision 66032)
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*66032Seric static char sccsid[] = "@(#)daemon.c	8.37 (Berkeley) 02/08/94 (with daemon mode)";
1533780Sbostic #else
16*66032Seric static char sccsid[] = "@(#)daemon.c	8.37 (Berkeley) 02/08/94 (without daemon mode)";
1733780Sbostic #endif
1833780Sbostic #endif /* not lint */
194535Seric 
2033780Sbostic #ifdef DAEMON
2133780Sbostic 
2223120Seric # include <netdb.h>
2364338Seric # include <arpa/inet.h>
245978Seric 
2559042Seric #ifdef NAMED_BIND
2659042Seric # include <arpa/nameser.h>
2759042Seric # include <resolv.h>
2859042Seric #endif
2959042Seric 
304535Seric /*
314535Seric **  DAEMON.C -- routines to use when running as a daemon.
327556Seric **
337556Seric **	This entire file is highly dependent on the 4.2 BSD
347556Seric **	interprocess communication primitives.  No attempt has
357556Seric **	been made to make this file portable to Version 7,
367556Seric **	Version 6, MPX files, etc.  If you should try such a
377556Seric **	thing yourself, I recommend chucking the entire file
387556Seric **	and starting from scratch.  Basic semantics are:
397556Seric **
407556Seric **	getrequests()
417556Seric **		Opens a port and initiates a connection.
427556Seric **		Returns in a child.  Must set InChannel and
437556Seric **		OutChannel appropriately.
4410206Seric **	clrdaemon()
4510206Seric **		Close any open files associated with getting
4610206Seric **		the connection; this is used when running the queue,
4710206Seric **		etc., to avoid having extra file descriptors during
4810206Seric **		the queue run and to avoid confusing the network
4910206Seric **		code (if it cares).
5052106Seric **	makeconnection(host, port, outfile, infile, usesecureport)
517556Seric **		Make a connection to the named host on the given
527556Seric **		port.  Set *outfile and *infile to the files
537556Seric **		appropriate for communication.  Returns zero on
547556Seric **		success, else an exit status describing the
557556Seric **		error.
5660089Seric **	host_map_lookup(map, hbuf, avp, pstat)
5756823Seric **		Convert the entry in hbuf into a canonical form.
584535Seric */
594535Seric /*
604535Seric **  GETREQUESTS -- open mail IPC port and get requests.
614535Seric **
624535Seric **	Parameters:
634535Seric **		none.
644535Seric **
654535Seric **	Returns:
664535Seric **		none.
674535Seric **
684535Seric **	Side Effects:
694535Seric **		Waits until some interesting activity occurs.  When
704535Seric **		it does, a child is created to process it, and the
714535Seric **		parent waits for completion.  Return from this
729886Seric **		routine is always in the child.  The file pointers
739886Seric **		"InChannel" and "OutChannel" should be set to point
749886Seric **		to the communication channel.
754535Seric */
764535Seric 
7758849Seric int		DaemonSocket	= -1;		/* fd describing socket */
7858849Seric SOCKADDR	DaemonAddr;			/* socket for incoming */
7959783Seric int		ListenQueueSize = 10;		/* size of listen queue */
8064381Seric int		TcpRcvBufferSize = 0;		/* size of TCP receive buffer */
8164381Seric int		TcpSndBufferSize = 0;		/* size of TCP send buffer */
8216144Seric 
834535Seric getrequests()
844535Seric {
859610Seric 	int t;
8625027Seric 	int on = 1;
8753751Seric 	bool refusingconnections = TRUE;
8858419Seric 	FILE *pidf;
8964828Seric 	int socksize;
9046928Sbostic 	extern void reapchild();
917117Seric 
929610Seric 	/*
939610Seric 	**  Set up the address for the mailer.
949610Seric 	*/
959610Seric 
9658849Seric 	if (DaemonAddr.sin.sin_family == 0)
9758849Seric 		DaemonAddr.sin.sin_family = AF_INET;
9858849Seric 	if (DaemonAddr.sin.sin_addr.s_addr == 0)
9958849Seric 		DaemonAddr.sin.sin_addr.s_addr = INADDR_ANY;
10058849Seric 	if (DaemonAddr.sin.sin_port == 0)
1019610Seric 	{
10265169Seric 		register struct servent *sp;
10365169Seric 
10458849Seric 		sp = getservbyname("smtp", "tcp");
10558849Seric 		if (sp == NULL)
10658849Seric 		{
10758909Seric 			syserr("554 service \"smtp\" unknown");
10865169Seric 			DaemonAddr.sin.sin_port = htons(25);
10958849Seric 		}
11065169Seric 		else
11165169Seric 			DaemonAddr.sin.sin_port = sp->s_port;
1129610Seric 	}
1139610Seric 
1149610Seric 	/*
1159610Seric 	**  Try to actually open the connection.
1169610Seric 	*/
1179610Seric 
1189610Seric 	if (tTd(15, 1))
11958849Seric 		printf("getrequests: port 0x%x\n", DaemonAddr.sin.sin_port);
1209610Seric 
1219610Seric 	/* get a socket for the SMTP connection */
12259041Seric 	DaemonSocket = socket(DaemonAddr.sa.sa_family, SOCK_STREAM, 0);
12310206Seric 	if (DaemonSocket < 0)
1249610Seric 	{
1259610Seric 		/* probably another daemon already */
1269610Seric 		syserr("getrequests: can't create socket");
1279610Seric 	  severe:
1289610Seric # ifdef LOG
1299610Seric 		if (LogLevel > 0)
13057663Seric 			syslog(LOG_ALERT, "problem creating SMTP socket");
13156795Seric # endif /* LOG */
1329610Seric 		finis();
1339610Seric 	}
13410347Seric 
13510347Seric 	/* turn on network debugging? */
13656328Seric 	if (tTd(15, 101))
13724945Seric 		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on);
13810347Seric 
13925027Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on);
14025027Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on);
14125027Seric 
14264391Seric #ifdef SO_RCVBUF
14364391Seric 	if (TcpRcvBufferSize > 0)
14464391Seric 	{
14564391Seric 		if (setsockopt(DaemonSocket, SOL_SOCKET, SO_RCVBUF,
14664561Seric 			       (char *) &TcpRcvBufferSize,
14764391Seric 			       sizeof(TcpRcvBufferSize)) < 0)
14864391Seric 			syserr("getrequests: setsockopt(SO_RCVBUF)");
14964391Seric 	}
15064391Seric #endif
15164391Seric 
15259041Seric 	switch (DaemonAddr.sa.sa_family)
1539610Seric 	{
15459041Seric # ifdef NETINET
15559041Seric 	  case AF_INET:
15664828Seric 		socksize = sizeof DaemonAddr.sin;
15759041Seric 		break;
15859041Seric # endif
15959041Seric 
16059041Seric # ifdef NETISO
16159041Seric 	  case AF_ISO:
16264828Seric 		socksize = sizeof DaemonAddr.siso;
16359041Seric 		break;
16459041Seric # endif
16559041Seric 
16659041Seric 	  default:
16764828Seric 		socksize = sizeof DaemonAddr;
16859041Seric 		break;
16959041Seric 	}
17059041Seric 
17164828Seric 	if (bind(DaemonSocket, &DaemonAddr.sa, socksize) < 0)
17259041Seric 	{
1739610Seric 		syserr("getrequests: cannot bind");
17410206Seric 		(void) close(DaemonSocket);
1759610Seric 		goto severe;
1769610Seric 	}
1779610Seric 
17864035Seric 	(void) setsignal(SIGCHLD, reapchild);
17924945Seric 
18058419Seric 	/* write the pid to the log file for posterity */
18158419Seric 	pidf = fopen(PidFile, "w");
18258419Seric 	if (pidf != NULL)
18358419Seric 	{
18463863Seric 		extern char *CommandLineArgs;
18563863Seric 
18663863Seric 		/* write the process id on line 1 */
18758419Seric 		fprintf(pidf, "%d\n", getpid());
18863863Seric 
18963863Seric 		/* line 2 contains all command line flags */
19063863Seric 		fprintf(pidf, "%s\n", CommandLineArgs);
19163863Seric 
19263863Seric 		/* flush and close */
19358419Seric 		fclose(pidf);
19458419Seric 	}
19558419Seric 
19658419Seric 
1979610Seric 	if (tTd(15, 1))
19810206Seric 		printf("getrequests: %d\n", DaemonSocket);
1999610Seric 
2004631Seric 	for (;;)
2014631Seric 	{
20214875Seric 		register int pid;
20311147Seric 		auto int lotherend;
20453751Seric 		extern bool refuseconnections();
20511147Seric 
20614875Seric 		/* see if we are rejecting connections */
20753751Seric 		CurrentLA = getla();
20853751Seric 		if (refuseconnections())
20936584Sbostic 		{
21053751Seric 			if (!refusingconnections)
21153751Seric 			{
21253751Seric 				/* don't queue so peer will fail quickly */
21353751Seric 				(void) listen(DaemonSocket, 0);
21453751Seric 				refusingconnections = TRUE;
21553751Seric 			}
21657385Seric 			setproctitle("rejecting connections: load average: %d",
21757385Seric 				CurrentLA);
21814875Seric 			sleep(5);
21953751Seric 			continue;
22036584Sbostic 		}
22114875Seric 
22253751Seric 		if (refusingconnections)
22353751Seric 		{
22453751Seric 			/* start listening again */
22559783Seric 			if (listen(DaemonSocket, ListenQueueSize) < 0)
22653751Seric 			{
22753751Seric 				syserr("getrequests: cannot listen");
22853751Seric 				(void) close(DaemonSocket);
22953751Seric 				goto severe;
23053751Seric 			}
23153751Seric 			setproctitle("accepting connections");
23253751Seric 			refusingconnections = FALSE;
23353751Seric 		}
23453751Seric 
2359610Seric 		/* wait for a connection */
2369610Seric 		do
2379610Seric 		{
2389610Seric 			errno = 0;
23964828Seric 			lotherend = socksize;
24046928Sbostic 			t = accept(DaemonSocket,
24146928Sbostic 			    (struct sockaddr *)&RealHostAddr, &lotherend);
2429610Seric 		} while (t < 0 && errno == EINTR);
2439610Seric 		if (t < 0)
2445978Seric 		{
2459610Seric 			syserr("getrequests: accept");
2469610Seric 			sleep(5);
2479610Seric 			continue;
2485978Seric 		}
2494631Seric 
2505978Seric 		/*
2515978Seric 		**  Create a subprocess to process the mail.
2525978Seric 		*/
2535978Seric 
2547677Seric 		if (tTd(15, 2))
2559610Seric 			printf("getrequests: forking (fd = %d)\n", t);
2565978Seric 
2574636Seric 		pid = fork();
2584636Seric 		if (pid < 0)
2594631Seric 		{
2604636Seric 			syserr("daemon: cannot fork");
2614636Seric 			sleep(10);
2629610Seric 			(void) close(t);
2634636Seric 			continue;
2644631Seric 		}
2654631Seric 
2664636Seric 		if (pid == 0)
2674631Seric 		{
26864086Seric 			char *p;
26958951Seric 			extern char *hostnamebyanyaddr();
27011147Seric 
2714636Seric 			/*
2724636Seric 			**  CHILD -- return to caller.
27311147Seric 			**	Collect verified idea of sending host.
2744636Seric 			**	Verify calling user id if possible here.
2754636Seric 			*/
2764631Seric 
27764035Seric 			(void) setsignal(SIGCHLD, SIG_DFL);
27866017Seric 			DisConnected = FALSE;
27924950Seric 
280*66032Seric 			setproctitle("startup with %s",
281*66032Seric 				anynet_ntoa(&RealHostAddr));
282*66032Seric 
28311147Seric 			/* determine host name */
28464086Seric 			p = hostnamebyanyaddr(&RealHostAddr);
28564086Seric 			RealHostName = newstr(p);
286*66032Seric 			setproctitle("startup with %s", p);
28758778Seric 
28855173Seric #ifdef LOG
28963842Seric 			if (LogLevel > 11)
29055173Seric 			{
29155173Seric 				/* log connection information */
29255173Seric 				syslog(LOG_INFO, "connect from %s (%s)",
29358951Seric 					RealHostName, anynet_ntoa(&RealHostAddr));
29455173Seric 			}
29555173Seric #endif
29655173Seric 
29759254Seric 			(void) close(DaemonSocket);
29864724Seric 			if ((InChannel = fdopen(t, "r")) == NULL ||
29964724Seric 			    (t = dup(t)) < 0 ||
30064724Seric 			    (OutChannel = fdopen(t, "w")) == NULL)
30164724Seric 			{
30264724Seric 				syserr("cannot open SMTP server channel, fd=%d", t);
30364724Seric 				exit(0);
30464724Seric 			}
30559254Seric 
30616884Seric 			/* should we check for illegal connection here? XXX */
30759156Seric #ifdef XLA
30859156Seric 			if (!xla_host_ok(RealHostName))
30959156Seric 			{
31059254Seric 				message("421 Too many SMTP sessions for this host");
31159156Seric 				exit(0);
31259156Seric 			}
31359156Seric #endif
31416884Seric 
3157677Seric 			if (tTd(15, 2))
3165978Seric 				printf("getreq: returning\n");
3174636Seric 			return;
3184631Seric 		}
3194631Seric 
3207117Seric 		/* close the port so that others will hang (for a while) */
3219610Seric 		(void) close(t);
3224631Seric 	}
3239886Seric 	/*NOTREACHED*/
3244631Seric }
3255978Seric /*
32610206Seric **  CLRDAEMON -- reset the daemon connection
32710206Seric **
32810206Seric **	Parameters:
32910206Seric **		none.
33010206Seric **
33110206Seric **	Returns:
33210206Seric **		none.
33310206Seric **
33410206Seric **	Side Effects:
33510206Seric **		releases any resources used by the passive daemon.
33610206Seric */
33710206Seric 
33810206Seric clrdaemon()
33910206Seric {
34010206Seric 	if (DaemonSocket >= 0)
34110206Seric 		(void) close(DaemonSocket);
34210206Seric 	DaemonSocket = -1;
34310206Seric }
34410206Seric /*
34558849Seric **  SETDAEMONOPTIONS -- set options for running the daemon
34658849Seric **
34758849Seric **	Parameters:
34858849Seric **		p -- the options line.
34958849Seric **
35058849Seric **	Returns:
35158849Seric **		none.
35258849Seric */
35358849Seric 
35458849Seric setdaemonoptions(p)
35558849Seric 	register char *p;
35658849Seric {
35758873Seric 	if (DaemonAddr.sa.sa_family == AF_UNSPEC)
35858873Seric 		DaemonAddr.sa.sa_family = AF_INET;
35958873Seric 
36058849Seric 	while (p != NULL)
36158849Seric 	{
36258849Seric 		register char *f;
36358849Seric 		register char *v;
36458849Seric 
36558849Seric 		while (isascii(*p) && isspace(*p))
36658849Seric 			p++;
36758849Seric 		if (*p == '\0')
36858849Seric 			break;
36958849Seric 		f = p;
37058849Seric 		p = strchr(p, ',');
37158849Seric 		if (p != NULL)
37258849Seric 			*p++ = '\0';
37358849Seric 		v = strchr(f, '=');
37458849Seric 		if (v == NULL)
37558849Seric 			continue;
37658849Seric 		while (isascii(*++v) && isspace(*v))
37758849Seric 			continue;
37858849Seric 
37958849Seric 		switch (*f)
38058849Seric 		{
38158873Seric 		  case 'F':		/* address family */
38258849Seric 			if (isascii(*v) && isdigit(*v))
38358873Seric 				DaemonAddr.sa.sa_family = atoi(v);
38458873Seric #ifdef NETINET
38558873Seric 			else if (strcasecmp(v, "inet") == 0)
38658873Seric 				DaemonAddr.sa.sa_family = AF_INET;
38758873Seric #endif
38858873Seric #ifdef NETISO
38958873Seric 			else if (strcasecmp(v, "iso") == 0)
39058873Seric 				DaemonAddr.sa.sa_family = AF_ISO;
39158873Seric #endif
39258873Seric #ifdef NETNS
39358873Seric 			else if (strcasecmp(v, "ns") == 0)
39458873Seric 				DaemonAddr.sa.sa_family = AF_NS;
39558873Seric #endif
39658873Seric #ifdef NETX25
39758873Seric 			else if (strcasecmp(v, "x.25") == 0)
39858873Seric 				DaemonAddr.sa.sa_family = AF_CCITT;
39958873Seric #endif
40058849Seric 			else
40158873Seric 				syserr("554 Unknown address family %s in Family=option", v);
40258873Seric 			break;
40358873Seric 
40458873Seric 		  case 'A':		/* address */
40558873Seric 			switch (DaemonAddr.sa.sa_family)
40658849Seric 			{
40758873Seric #ifdef NETINET
40858873Seric 			  case AF_INET:
40958873Seric 				if (isascii(*v) && isdigit(*v))
41058873Seric 					DaemonAddr.sin.sin_addr.s_addr = inet_network(v);
41158873Seric 				else
41258873Seric 				{
41358873Seric 					register struct netent *np;
41458849Seric 
41558873Seric 					np = getnetbyname(v);
41658873Seric 					if (np == NULL)
41758873Seric 						syserr("554 network \"%s\" unknown", v);
41858873Seric 					else
41958873Seric 						DaemonAddr.sin.sin_addr.s_addr = np->n_net;
42058873Seric 				}
42158873Seric 				break;
42258873Seric #endif
42358873Seric 
42458873Seric 			  default:
42558873Seric 				syserr("554 Address= option unsupported for family %d",
42658873Seric 					DaemonAddr.sa.sa_family);
42758873Seric 				break;
42858849Seric 			}
42958849Seric 			break;
43058849Seric 
43158873Seric 		  case 'P':		/* port */
43258873Seric 			switch (DaemonAddr.sa.sa_family)
43358849Seric 			{
43458873Seric 				short port;
43558849Seric 
43658873Seric #ifdef NETINET
43758873Seric 			  case AF_INET:
43858873Seric 				if (isascii(*v) && isdigit(*v))
43964366Seric 					DaemonAddr.sin.sin_port = htons(atoi(v));
44058849Seric 				else
44158873Seric 				{
44258873Seric 					register struct servent *sp;
44358873Seric 
44458873Seric 					sp = getservbyname(v, "tcp");
44558873Seric 					if (sp == NULL)
44658909Seric 						syserr("554 service \"%s\" unknown", v);
44758873Seric 					else
44858873Seric 						DaemonAddr.sin.sin_port = sp->s_port;
44958873Seric 				}
45058873Seric 				break;
45158873Seric #endif
45258873Seric 
45358873Seric #ifdef NETISO
45458873Seric 			  case AF_ISO:
45558873Seric 				/* assume two byte transport selector */
45658873Seric 				if (isascii(*v) && isdigit(*v))
45764366Seric 					port = htons(atoi(v));
45858873Seric 				else
45958873Seric 				{
46058873Seric 					register struct servent *sp;
46158873Seric 
46258873Seric 					sp = getservbyname(v, "tcp");
46358873Seric 					if (sp == NULL)
46458909Seric 						syserr("554 service \"%s\" unknown", v);
46558873Seric 					else
46658873Seric 						port = sp->s_port;
46758873Seric 				}
46858873Seric 				bcopy((char *) &port, TSEL(&DaemonAddr.siso), 2);
46958873Seric 				break;
47058873Seric #endif
47158873Seric 
47258873Seric 			  default:
47358873Seric 				syserr("554 Port= option unsupported for family %d",
47458873Seric 					DaemonAddr.sa.sa_family);
47558873Seric 				break;
47658849Seric 			}
47758849Seric 			break;
47859783Seric 
47959783Seric 		  case 'L':		/* listen queue size */
48059783Seric 			ListenQueueSize = atoi(v);
48159783Seric 			break;
48264381Seric 
48364381Seric 		  case 'S':		/* send buffer size */
48464381Seric 			TcpSndBufferSize = atoi(v);
48564381Seric 			break;
48664381Seric 
48764381Seric 		  case 'R':		/* receive buffer size */
48864381Seric 			TcpRcvBufferSize = atoi(v);
48964381Seric 			break;
49058849Seric 		}
49158849Seric 	}
49258849Seric }
49358849Seric /*
4946039Seric **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
4956039Seric **
4966039Seric **	Parameters:
4976039Seric **		host -- the name of the host.
4986633Seric **		port -- the port number to connect to.
49953739Seric **		mci -- a pointer to the mail connection information
50053739Seric **			structure to be filled in.
50152106Seric **		usesecureport -- if set, use a low numbered (reserved)
50252106Seric **			port to provide some rudimentary authentication.
5036039Seric **
5046039Seric **	Returns:
5056039Seric **		An exit code telling whether the connection could be
5066039Seric **			made and if not why not.
5076039Seric **
5086039Seric **	Side Effects:
5096039Seric **		none.
5106039Seric */
5115978Seric 
51258755Seric SOCKADDR	CurHostAddr;		/* address of current host */
51358305Seric 
51454967Seric int
51553739Seric makeconnection(host, port, mci, usesecureport)
5166039Seric 	char *host;
5177286Seric 	u_short port;
51854967Seric 	register MCI *mci;
51952106Seric 	bool usesecureport;
5206039Seric {
52129430Sbloom 	register int i, s;
52229430Sbloom 	register struct hostent *hp = (struct hostent *)NULL;
52358755Seric 	SOCKADDR addr;
52452106Seric 	int sav_errno;
52558755Seric 	int addrlen;
52635651Seric #ifdef NAMED_BIND
52735651Seric 	extern int h_errno;
52835651Seric #endif
5296039Seric 
5306039Seric 	/*
5316039Seric 	**  Set up the address for the mailer.
5329308Seric 	**	Accept "[a.b.c.d]" syntax for host name.
5336039Seric 	*/
5346039Seric 
53535651Seric #ifdef NAMED_BIND
53625475Smiriam 	h_errno = 0;
53735651Seric #endif
53825475Smiriam 	errno = 0;
53958864Seric 	bzero(&CurHostAddr, sizeof CurHostAddr);
54064334Seric 	SmtpPhase = mci->mci_phase = "initial connection";
54158906Seric 	CurHostName = host;
54225475Smiriam 
5439308Seric 	if (host[0] == '[')
5449308Seric 	{
54511147Seric 		long hid;
54656795Seric 		register char *p = strchr(host, ']');
5479308Seric 
54811147Seric 		if (p != NULL)
5499308Seric 		{
55011147Seric 			*p = '\0';
55159884Seric #ifdef NETINET
55211147Seric 			hid = inet_addr(&host[1]);
55358360Seric 			if (hid == -1)
55459884Seric #endif
55558360Seric 			{
55658360Seric 				/* try it as a host name (avoid MX lookup) */
55758360Seric 				hp = gethostbyname(&host[1]);
55858360Seric 				*p = ']';
55958360Seric 				goto gothostent;
56058360Seric 			}
56111147Seric 			*p = ']';
5629308Seric 		}
56358360Seric 		if (p == NULL)
5649308Seric 		{
56558151Seric 			usrerr("553 Invalid numeric domain spec \"%s\"", host);
5669308Seric 			return (EX_NOHOST);
5679308Seric 		}
56859884Seric #ifdef NETINET
56959884Seric 		addr.sin.sin_family = AF_INET;		/*XXX*/
57058778Seric 		addr.sin.sin_addr.s_addr = hid;
57159884Seric #endif
5729308Seric 	}
5739610Seric 	else
5749610Seric 	{
57529430Sbloom 		hp = gethostbyname(host);
57658360Seric gothostent:
57725475Smiriam 		if (hp == NULL)
57824945Seric 		{
57935651Seric #ifdef NAMED_BIND
58025475Smiriam 			if (errno == ETIMEDOUT || h_errno == TRY_AGAIN)
58125475Smiriam 				return (EX_TEMPFAIL);
58225657Seric 
58335651Seric 			/* if name server is specified, assume temp fail */
58435651Seric 			if (errno == ECONNREFUSED && UseNameServer)
58535651Seric 				return (EX_TEMPFAIL);
58635651Seric #endif
58725475Smiriam 			return (EX_NOHOST);
58824945Seric 		}
58958778Seric 		addr.sa.sa_family = hp->h_addrtype;
59058778Seric 		switch (hp->h_addrtype)
59158778Seric 		{
59258778Seric #ifdef NETINET
59358778Seric 		  case AF_INET:
59458755Seric 			bcopy(hp->h_addr,
59558778Seric 				&addr.sin.sin_addr,
59664943Seric 				sizeof addr.sin.sin_addr);
59758778Seric 			break;
59858778Seric #endif
59958778Seric 
60058778Seric 		  default:
60158755Seric 			bcopy(hp->h_addr,
60258778Seric 				addr.sa.sa_data,
60358755Seric 				hp->h_length);
60458778Seric 			break;
60558778Seric 		}
60629430Sbloom 		i = 1;
6079610Seric 	}
6089610Seric 
6099610Seric 	/*
6109610Seric 	**  Determine the port number.
6119610Seric 	*/
6129610Seric 
61310011Seric 	if (port != 0)
61458755Seric 		port = htons(port);
61510011Seric 	else
6169610Seric 	{
6179610Seric 		register struct servent *sp = getservbyname("smtp", "tcp");
6189610Seric 
6199610Seric 		if (sp == NULL)
6209610Seric 		{
62158909Seric 			syserr("554 makeconnection: service \"smtp\" unknown");
62265169Seric 			port = htons(25);
6239610Seric 		}
62465169Seric 		else
62565169Seric 			port = sp->s_port;
6269610Seric 	}
6276039Seric 
62858778Seric 	switch (addr.sa.sa_family)
62958755Seric 	{
63059884Seric #ifdef NETINET
63158755Seric 	  case AF_INET:
63258778Seric 		addr.sin.sin_port = port;
63358755Seric 		addrlen = sizeof (struct sockaddr_in);
63458755Seric 		break;
63559884Seric #endif
63658755Seric 
63758755Seric #ifdef NETISO
63858755Seric 	  case AF_ISO:
63958755Seric 		/* assume two byte transport selector */
64058755Seric 		bcopy((char *) &port, TSEL((struct sockaddr_iso *) &addr), 2);
64158755Seric 		addrlen = sizeof (struct sockaddr_iso);
64258755Seric 		break;
64358755Seric #endif
64458755Seric 
64558755Seric 	  default:
64658778Seric 		syserr("Can't connect to address family %d", addr.sa.sa_family);
64758755Seric 		return (EX_NOHOST);
64858755Seric 	}
64958755Seric 
6506039Seric 	/*
6516039Seric 	**  Try to actually open the connection.
6526039Seric 	*/
6536039Seric 
65459156Seric #ifdef XLA
65559156Seric 	/* if too many connections, don't bother trying */
65659156Seric 	if (!xla_noqueue_ok(host))
65759156Seric 		return EX_TEMPFAIL;
65859156Seric #endif
65959156Seric 
66057736Seric 	for (;;)
66152106Seric 	{
66257736Seric 		if (tTd(16, 1))
66358755Seric 			printf("makeconnection (%s [%s])\n",
66458755Seric 				host, anynet_ntoa(&addr));
66552106Seric 
66658588Seric 		/* save for logging */
66758588Seric 		CurHostAddr = addr;
66858588Seric 
66957736Seric 		if (usesecureport)
67057736Seric 		{
67157736Seric 			int rport = IPPORT_RESERVED - 1;
6726039Seric 
67357736Seric 			s = rresvport(&rport);
67457736Seric 		}
67557736Seric 		else
67657736Seric 		{
67757736Seric 			s = socket(AF_INET, SOCK_STREAM, 0);
67857736Seric 		}
67957736Seric 		if (s < 0)
68057736Seric 		{
68157736Seric 			sav_errno = errno;
68257736Seric 			syserr("makeconnection: no socket");
68357736Seric 			goto failure;
68457736Seric 		}
68510347Seric 
68664381Seric #ifdef SO_SNDBUF
68764381Seric 		if (TcpSndBufferSize > 0)
68864381Seric 		{
68964381Seric 			if (setsockopt(s, SOL_SOCKET, SO_SNDBUF,
69064561Seric 				       (char *) &TcpSndBufferSize,
69164381Seric 				       sizeof(TcpSndBufferSize)) < 0)
69264381Seric 				syserr("makeconnection: setsockopt(SO_SNDBUF)");
69364381Seric 		}
69464381Seric #endif
69564381Seric 
69657736Seric 		if (tTd(16, 1))
69757736Seric 			printf("makeconnection: fd=%d\n", s);
69857736Seric 
69957736Seric 		/* turn on network debugging? */
70057736Seric 		if (tTd(16, 101))
70157736Seric 		{
70257736Seric 			int on = 1;
70357736Seric 			(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG,
70457736Seric 					  (char *)&on, sizeof on);
70557736Seric 		}
70657736Seric 		if (CurEnv->e_xfp != NULL)
70757736Seric 			(void) fflush(CurEnv->e_xfp);		/* for debugging */
70857736Seric 		errno = 0;					/* for debugging */
70958755Seric 		if (connect(s, (struct sockaddr *) &addr, addrlen) >= 0)
71057736Seric 			break;
71157736Seric 
71257736Seric 		/* couldn't connect.... figure out why */
71327744Sbloom 		sav_errno = errno;
71427744Sbloom 		(void) close(s);
71529430Sbloom 		if (hp && hp->h_addr_list[i])
71629430Sbloom 		{
71757736Seric 			if (tTd(16, 1))
71858755Seric 				printf("Connect failed (%s); trying new address....\n",
71958755Seric 					errstring(sav_errno));
72058778Seric 			switch (addr.sa.sa_family)
72158778Seric 			{
72258778Seric #ifdef NETINET
72358778Seric 			  case AF_INET:
72458755Seric 				bcopy(hp->h_addr_list[i++],
72558778Seric 				      &addr.sin.sin_addr,
72664943Seric 				      sizeof addr.sin.sin_addr);
72758778Seric 				break;
72858778Seric #endif
72958778Seric 
73058778Seric 			  default:
73158755Seric 				bcopy(hp->h_addr_list[i++],
73258778Seric 					addr.sa.sa_data,
73352106Seric 					hp->h_length);
73458778Seric 				break;
73558778Seric 			}
73657736Seric 			continue;
73729430Sbloom 		}
73829430Sbloom 
7396039Seric 		/* failure, decide if temporary or not */
7406039Seric 	failure:
74159254Seric #ifdef XLA
74259254Seric 		xla_host_end(host);
74359254Seric #endif
74458542Seric 		if (transienterror(sav_errno))
74558542Seric 			return EX_TEMPFAIL;
74658542Seric 		else
74758542Seric 		{
74858542Seric 			message("%s", errstring(sav_errno));
74958542Seric 			return (EX_UNAVAILABLE);
7506039Seric 		}
7516039Seric 	}
7526039Seric 
7536039Seric 	/* connection ok, put it into canonical form */
75464724Seric 	if ((mci->mci_out = fdopen(s, "w")) == NULL ||
75564724Seric 	    (s = dup(s)) < 0 ||
75664725Seric 	    (mci->mci_in = fdopen(s, "r")) == NULL)
75764724Seric 	{
75864724Seric 		syserr("cannot open SMTP client channel, fd=%d", s);
75964724Seric 		return EX_TEMPFAIL;
76064724Seric 	}
7616039Seric 
76210098Seric 	return (EX_OK);
7636039Seric }
76410758Seric /*
76510758Seric **  MYHOSTNAME -- return the name of this host.
76610758Seric **
76710758Seric **	Parameters:
76810758Seric **		hostbuf -- a place to return the name of this host.
76912313Seric **		size -- the size of hostbuf.
77010758Seric **
77110758Seric **	Returns:
77210758Seric **		A list of aliases for this host.
77310758Seric **
77410758Seric **	Side Effects:
77564338Seric **		Adds numeric codes to $=w.
77610758Seric */
7776039Seric 
77810758Seric char **
77912313Seric myhostname(hostbuf, size)
78010758Seric 	char hostbuf[];
78112313Seric 	int size;
78210758Seric {
78358110Seric 	register struct hostent *hp;
78410758Seric 	extern struct hostent *gethostbyname();
78510758Seric 
78623120Seric 	if (gethostname(hostbuf, size) < 0)
78723120Seric 	{
78823120Seric 		(void) strcpy(hostbuf, "localhost");
78923120Seric 	}
79011147Seric 	hp = gethostbyname(hostbuf);
79111147Seric 	if (hp != NULL)
79216877Seric 	{
79358110Seric 		(void) strncpy(hostbuf, hp->h_name, size - 1);
79458110Seric 		hostbuf[size - 1] = '\0';
79558110Seric 
79658110Seric 		if (hp->h_addrtype == AF_INET && hp->h_length == 4)
79758110Seric 		{
79858110Seric 			register int i;
79958110Seric 
80064338Seric 			for (i = 0; hp->h_addr_list[i] != NULL; i++)
80158110Seric 			{
80264338Seric 				char ipbuf[100];
80364338Seric 
80464338Seric 				sprintf(ipbuf, "[%s]",
80564338Seric 					inet_ntoa(*((struct in_addr *) hp->h_addr_list[i])));
80664338Seric 				setclass('w', ipbuf);
80758110Seric 			}
80858110Seric 		}
80958110Seric 
81011147Seric 		return (hp->h_aliases);
81116877Seric 	}
81210758Seric 	else
81310758Seric 		return (NULL);
81410758Seric }
81551315Seric /*
81658951Seric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
81758308Seric **
81858951Seric **	Uses RFC1413 protocol to try to get info from the other end.
81958951Seric **
82058308Seric **	Parameters:
82158308Seric **		fd -- the descriptor
82258308Seric **
82358308Seric **	Returns:
82458951Seric **		The user@host information associated with this descriptor.
82558308Seric */
82658308Seric 
82764927Seric #if IDENTPROTO
82858951Seric 
82958951Seric static jmp_buf	CtxAuthTimeout;
83058951Seric 
83158951Seric static
83258951Seric authtimeout()
83358951Seric {
83458951Seric 	longjmp(CtxAuthTimeout, 1);
83558951Seric }
83658951Seric 
83758951Seric #endif
83858951Seric 
83958308Seric char *
84058951Seric getauthinfo(fd)
84158308Seric 	int fd;
84258308Seric {
84358951Seric 	SOCKADDR fa;
84458951Seric 	int falen;
84559104Seric 	register char *p;
84664927Seric #if IDENTPROTO
84758951Seric 	SOCKADDR la;
84858951Seric 	int lalen;
84958951Seric 	register struct servent *sp;
85058951Seric 	int s;
85158951Seric 	int i;
85258951Seric 	EVENT *ev;
85358951Seric #endif
85458951Seric 	static char hbuf[MAXNAME * 2 + 2];
85558951Seric 	extern char *hostnamebyanyaddr();
85658951Seric 	extern char RealUserName[];			/* main.c */
85758308Seric 
85858951Seric 	falen = sizeof fa;
85964845Seric 	if (getpeername(fd, &fa.sa, &falen) < 0 || falen <= 0 ||
86064845Seric 	    fa.sa.sa_family == 0)
86158951Seric 	{
86258951Seric 		(void) sprintf(hbuf, "%s@localhost", RealUserName);
86358957Seric 		if (tTd(9, 1))
86458951Seric 			printf("getauthinfo: %s\n", hbuf);
86558951Seric 		return hbuf;
86658951Seric 	}
86758951Seric 
86864927Seric #if IDENTPROTO
86965831Seric 	if (TimeOuts.to_ident == 0)
87065831Seric 		goto noident;
87165831Seric 
87258951Seric 	lalen = sizeof la;
87358951Seric 	if (fa.sa.sa_family != AF_INET ||
87458951Seric 	    getsockname(fd, &la.sa, &lalen) < 0 || lalen <= 0 ||
87558951Seric 	    la.sa.sa_family != AF_INET)
87658951Seric 	{
87758951Seric 		/* no ident info */
87858951Seric 		goto noident;
87958951Seric 	}
88058951Seric 
88158951Seric 	/* create ident query */
88260489Seric 	(void) sprintf(hbuf, "%d,%d\r\n",
88360489Seric 		ntohs(fa.sin.sin_port), ntohs(la.sin.sin_port));
88458951Seric 
88558951Seric 	/* create local address */
88664747Seric 	la.sin.sin_port = 0;
88758951Seric 
88858951Seric 	/* create foreign address */
88958951Seric 	sp = getservbyname("auth", "tcp");
89058951Seric 	if (sp != NULL)
89158951Seric 		fa.sin.sin_port = sp->s_port;
89258308Seric 	else
89359097Seric 		fa.sin.sin_port = htons(113);
89458951Seric 
89558951Seric 	s = -1;
89658951Seric 	if (setjmp(CtxAuthTimeout) != 0)
89758951Seric 	{
89858951Seric 		if (s >= 0)
89958951Seric 			(void) close(s);
90058951Seric 		goto noident;
90158951Seric 	}
90258951Seric 
90358951Seric 	/* put a timeout around the whole thing */
90464255Seric 	ev = setevent(TimeOuts.to_ident, authtimeout, 0);
90558951Seric 
90664747Seric 	/* connect to foreign IDENT server using same address as SMTP socket */
90758951Seric 	s = socket(AF_INET, SOCK_STREAM, 0);
90858951Seric 	if (s < 0)
90958951Seric 	{
91058951Seric 		clrevent(ev);
91158951Seric 		goto noident;
91258951Seric 	}
91364747Seric 	if (bind(s, &la.sa, sizeof la.sin) < 0 ||
91464747Seric 	    connect(s, &fa.sa, sizeof fa.sin) < 0)
91558951Seric 	{
91666011Seric 		goto closeident;
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 */
97566003Seric 	(void) sprintf(hbuf, "%s@%s",
97666003Seric 		p, RealHostName == NULL ? "localhost" : RealHostName);
97758957Seric 	goto finish;
97858957Seric 
97966011Seric closeident:
98066011Seric 	(void) close(s);
98166011Seric 	clrevent(ev);
98266011Seric 
98358957Seric #endif /* IDENTPROTO */
98458957Seric 
98558957Seric noident:
98666003Seric 	if (RealHostName == NULL)
98766003Seric 	{
98866003Seric 		if (tTd(9, 1))
98966003Seric 			printf("getauthinfo: NULL\n");
99066003Seric 		return NULL;
99166003Seric 	}
99258957Seric 	(void) strcpy(hbuf, RealHostName);
99358957Seric 
99458957Seric finish:
99566003Seric 	if (RealHostName != NULL && RealHostName[0] != '[')
99658951Seric 	{
99758951Seric 		p = &hbuf[strlen(hbuf)];
99858951Seric 		(void) sprintf(p, " [%s]", anynet_ntoa(&RealHostAddr));
99958951Seric 	}
100058957Seric 	if (tTd(9, 1))
100158951Seric 		printf("getauthinfo: %s\n", hbuf);
100258308Seric 	return hbuf;
100358308Seric }
100458308Seric /*
100560089Seric **  HOST_MAP_LOOKUP -- turn a hostname into canonical form
100653751Seric **
100753751Seric **	Parameters:
100856823Seric **		map -- a pointer to this map (unused).
100960089Seric **		name -- the (presumably unqualified) hostname.
101060257Seric **		av -- unused -- for compatibility with other mapping
101155019Seric **			functions.
101259084Seric **		statp -- an exit status (out parameter) -- set to
101359084Seric **			EX_TEMPFAIL if the name server is unavailable.
101453751Seric **
101553751Seric **	Returns:
101653751Seric **		The mapping, if found.
101753751Seric **		NULL if no mapping found.
101853751Seric **
101953751Seric **	Side Effects:
102053751Seric **		Looks up the host specified in hbuf.  If it is not
102153751Seric **		the canonical name for that host, return the canonical
102253751Seric **		name.
102353751Seric */
102451315Seric 
102553751Seric char *
102660257Seric host_map_lookup(map, name, av, statp)
102756823Seric 	MAP *map;
102860089Seric 	char *name;
102960257Seric 	char **av;
103059084Seric 	int *statp;
103116911Seric {
103216911Seric 	register struct hostent *hp;
103333932Sbostic 	u_long in_addr;
103456823Seric 	char *cp;
103558110Seric 	int i;
103659671Seric 	register STAB *s;
103760257Seric 	char hbuf[MAXNAME];
103859671Seric 	extern struct hostent *gethostbyaddr();
103966029Seric #ifdef NAMED_BIND
104059671Seric 	extern int h_errno;
104166029Seric #endif
104216911Seric 
104325574Smiriam 	/*
104459671Seric 	**  See if we have already looked up this name.  If so, just
104559671Seric 	**  return it.
104659671Seric 	*/
104753751Seric 
104860089Seric 	s = stab(name, ST_NAMECANON, ST_ENTER);
104959671Seric 	if (bitset(NCF_VALID, s->s_namecanon.nc_flags))
105059671Seric 	{
105159986Seric 		if (tTd(9, 1))
105260089Seric 			printf("host_map_lookup(%s) => CACHE %s\n",
105360089Seric 				name, s->s_namecanon.nc_cname);
105459671Seric 		errno = s->s_namecanon.nc_errno;
105566029Seric #ifdef NAMED_BIND
105659671Seric 		h_errno = s->s_namecanon.nc_herrno;
105766029Seric #endif
105859671Seric 		*statp = s->s_namecanon.nc_stat;
105964797Seric 		if (CurEnv->e_message == NULL && *statp == EX_TEMPFAIL)
106065199Seric 		{
106165199Seric 			sprintf(hbuf, "%s: Name server timeout",
106265199Seric 				shortenstring(name, 33));
106365199Seric 			CurEnv->e_message = newstr(hbuf);
106465199Seric 		}
106559671Seric 		return s->s_namecanon.nc_cname;
106659671Seric 	}
106759671Seric 
106859671Seric 	/*
106959671Seric 	**  If first character is a bracket, then it is an address
107059671Seric 	**  lookup.  Address is copied into a temporary buffer to
107160089Seric 	**  strip the brackets and to preserve name if address is
107259671Seric 	**  unknown.
107359671Seric 	*/
107459671Seric 
107560089Seric 	if (*name != '[')
107653751Seric 	{
107755019Seric 		extern bool getcanonname();
107855019Seric 
107958798Seric 		if (tTd(9, 1))
108060089Seric 			printf("host_map_lookup(%s) => ", name);
108159671Seric 		s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
108260089Seric 		(void) strcpy(hbuf, name);
108363842Seric 		if (getcanonname(hbuf, sizeof hbuf - 1, TRUE))
108458796Seric 		{
108558796Seric 			if (tTd(9, 1))
108658796Seric 				printf("%s\n", hbuf);
108760257Seric 			cp = map_rewrite(map, hbuf, strlen(hbuf), av);
108860257Seric 			s->s_namecanon.nc_cname = newstr(cp);
108960257Seric 			return cp;
109058796Seric 		}
109153751Seric 		else
109258796Seric 		{
109359084Seric 			register struct hostent *hp;
109459084Seric 
109566029Seric 			s->s_namecanon.nc_errno = errno;
109666029Seric #ifdef NAMED_BIND
109766029Seric 			s->s_namecanon.nc_herrno = h_errno;
109858796Seric 			if (tTd(9, 1))
109959084Seric 				printf("FAIL (%d)\n", h_errno);
110059084Seric 			switch (h_errno)
110159084Seric 			{
110259084Seric 			  case TRY_AGAIN:
110359596Seric 				if (UseNameServer)
110459734Seric 				{
110565202Seric 					sprintf(hbuf, "%s: Name server timeout",
110665199Seric 						shortenstring(name, 33));
110765202Seric 					message("%s", hbuf);
110859734Seric 					if (CurEnv->e_message == NULL)
110965202Seric 						CurEnv->e_message = newstr(hbuf);
111059734Seric 				}
111159084Seric 				*statp = EX_TEMPFAIL;
111259084Seric 				break;
111359084Seric 
111459084Seric 			  case HOST_NOT_FOUND:
111559084Seric 				*statp = EX_NOHOST;
111659084Seric 				break;
111759084Seric 
111859084Seric 			  case NO_RECOVERY:
111959084Seric 				*statp = EX_SOFTWARE;
112059084Seric 				break;
112159084Seric 
112259084Seric 			  default:
112359084Seric 				*statp = EX_UNAVAILABLE;
112459084Seric 				break;
112559084Seric 			}
112666029Seric #else
112766029Seric 			if (tTd(9, 1))
112866029Seric 				printf("FAIL\n");
112966029Seric 			*statp = EX_NOHOST;
113066029Seric #endif
113159671Seric 			s->s_namecanon.nc_stat = *statp;
113259084Seric 			if (*statp != EX_TEMPFAIL || UseNameServer)
113359084Seric 				return NULL;
113459084Seric 
113559084Seric 			/*
113659084Seric 			**  Try to look it up in /etc/hosts
113759084Seric 			*/
113859084Seric 
113960089Seric 			hp = gethostbyname(name);
114059084Seric 			if (hp == NULL)
114159084Seric 			{
114259084Seric 				/* no dice there either */
114359671Seric 				s->s_namecanon.nc_stat = *statp = EX_NOHOST;
114459084Seric 				return NULL;
114559084Seric 			}
114659084Seric 
114759671Seric 			s->s_namecanon.nc_stat = *statp = EX_OK;
114860257Seric 			cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
114960257Seric 			s->s_namecanon.nc_cname = newstr(cp);
115060257Seric 			return cp;
115158796Seric 		}
115253751Seric 	}
115360089Seric 	if ((cp = strchr(name, ']')) == NULL)
115453751Seric 		return (NULL);
115540994Sbostic 	*cp = '\0';
115660089Seric 	in_addr = inet_addr(&name[1]);
115758110Seric 
115858110Seric 	/* nope -- ask the name server */
115933932Sbostic 	hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET);
116059671Seric 	s->s_namecanon.nc_errno = errno;
116166029Seric #ifdef NAMED_BIND
116259671Seric 	s->s_namecanon.nc_herrno = h_errno;
116366029Seric #endif
116459671Seric 	s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
116533932Sbostic 	if (hp == NULL)
116659671Seric 	{
116759671Seric 		s->s_namecanon.nc_stat = *statp = EX_NOHOST;
116853751Seric 		return (NULL);
116959671Seric 	}
117053751Seric 
117158110Seric 	/* found a match -- copy out */
117260257Seric 	cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
117359671Seric 	s->s_namecanon.nc_stat = *statp = EX_OK;
117460257Seric 	s->s_namecanon.nc_cname = newstr(cp);
117560257Seric 	return cp;
117633932Sbostic }
117758755Seric /*
117858755Seric **  ANYNET_NTOA -- convert a network address to printable form.
117958755Seric **
118058755Seric **	Parameters:
118158755Seric **		sap -- a pointer to a sockaddr structure.
118258755Seric **
118358755Seric **	Returns:
118458755Seric **		A printable version of that sockaddr.
118558755Seric */
118616911Seric 
118758755Seric char *
118858755Seric anynet_ntoa(sap)
118958755Seric 	register SOCKADDR *sap;
119058755Seric {
119158755Seric 	register char *bp;
119258755Seric 	register char *ap;
119358755Seric 	int l;
119464734Seric 	static char buf[100];
119558755Seric 
119658798Seric 	/* check for null/zero family */
119758798Seric 	if (sap == NULL)
119858798Seric 		return "NULLADDR";
119958798Seric 	if (sap->sa.sa_family == 0)
120058798Seric 		return "0";
120158798Seric 
120264734Seric 	switch (sap->sa.sa_family)
120364734Seric 	{
120464734Seric #ifdef MAYBENEXTRELEASE		/*** UNTESTED *** UNTESTED *** UNTESTED ***/
120564821Seric #ifdef NETUNIX
120664734Seric 	  case AF_UNIX:
120764758Seric 	  	if (sap->sunix.sun_path[0] != '\0')
120864758Seric 	  		sprintf(buf, "[UNIX: %.64s]", sap->sunix.sun_path);
120964734Seric 	  	else
121064734Seric 	  		sprintf(buf, "[UNIX: localhost]");
121164734Seric 		return buf;
121264734Seric #endif
121364821Seric #endif
121464734Seric 
121558778Seric #ifdef NETINET
121664734Seric 	  case AF_INET:
121758755Seric 		return inet_ntoa(((struct sockaddr_in *) sap)->sin_addr);
121858778Seric #endif
121958755Seric 
122064734Seric 	  default:
122164734Seric 	  	/* this case is only to ensure syntactic correctness */
122264734Seric 	  	break;
122364734Seric 	}
122464734Seric 
122558755Seric 	/* unknown family -- just dump bytes */
122658778Seric 	(void) sprintf(buf, "Family %d: ", sap->sa.sa_family);
122758755Seric 	bp = &buf[strlen(buf)];
122858778Seric 	ap = sap->sa.sa_data;
122958778Seric 	for (l = sizeof sap->sa.sa_data; --l >= 0; )
123058755Seric 	{
123158755Seric 		(void) sprintf(bp, "%02x:", *ap++ & 0377);
123258755Seric 		bp += 3;
123358755Seric 	}
123458755Seric 	*--bp = '\0';
123558755Seric 	return buf;
123658755Seric }
123758951Seric /*
123858951Seric **  HOSTNAMEBYANYADDR -- return name of host based on address
123958951Seric **
124058951Seric **	Parameters:
124158951Seric **		sap -- SOCKADDR pointer
124258951Seric **
124358951Seric **	Returns:
124458951Seric **		text representation of host name.
124558951Seric **
124658951Seric **	Side Effects:
124758951Seric **		none.
124858951Seric */
124958755Seric 
125058951Seric char *
125158951Seric hostnamebyanyaddr(sap)
125258951Seric 	register SOCKADDR *sap;
125358951Seric {
125458951Seric 	register struct hostent *hp;
125564734Seric 	int saveretry;
125658951Seric 
125759042Seric #ifdef NAMED_BIND
125859042Seric 	/* shorten name server timeout to avoid higher level timeouts */
125959042Seric 	saveretry = _res.retry;
126059042Seric 	_res.retry = 3;
126159042Seric #endif /* NAMED_BIND */
126259042Seric 
126358951Seric 	switch (sap->sa.sa_family)
126458951Seric 	{
126558951Seric #ifdef NETINET
126658951Seric 	  case AF_INET:
126758951Seric 		hp = gethostbyaddr((char *) &sap->sin.sin_addr,
126858951Seric 			sizeof sap->sin.sin_addr,
126958951Seric 			AF_INET);
127058951Seric 		break;
127158951Seric #endif
127258951Seric 
127358951Seric #ifdef NETISO
127458951Seric 	  case AF_ISO:
127558951Seric 		hp = gethostbyaddr((char *) &sap->siso.siso_addr,
127658951Seric 			sizeof sap->siso.siso_addr,
127758951Seric 			AF_ISO);
127858951Seric 		break;
127958951Seric #endif
128058951Seric 
128164734Seric #ifdef MAYBENEXTRELEASE		/*** UNTESTED *** UNTESTED *** UNTESTED ***/
128264734Seric 	  case AF_UNIX:
128364734Seric 		hp = NULL;
128464734Seric 		break;
128564734Seric #endif
128664734Seric 
128758951Seric 	  default:
128858951Seric 		hp = gethostbyaddr(sap->sa.sa_data,
128958951Seric 			   sizeof sap->sa.sa_data,
129058951Seric 			   sap->sa.sa_family);
129158951Seric 		break;
129258951Seric 	}
129358951Seric 
129459042Seric #ifdef NAMED_BIND
129559042Seric 	_res.retry = saveretry;
129659042Seric #endif /* NAMED_BIND */
129759042Seric 
129858951Seric 	if (hp != NULL)
129958951Seric 		return hp->h_name;
130058951Seric 	else
130158951Seric 	{
130258951Seric 		/* produce a dotted quad */
130358951Seric 		static char buf[512];
130458951Seric 
130558951Seric 		(void) sprintf(buf, "[%s]", anynet_ntoa(sap));
130658951Seric 		return buf;
130758951Seric 	}
130858951Seric }
130958951Seric 
131056795Seric # else /* DAEMON */
131116911Seric /* code for systems without sophisticated networking */
131210758Seric 
131310758Seric /*
131410758Seric **  MYHOSTNAME -- stub version for case of no daemon code.
131511297Seric **
131611297Seric **	Can't convert to upper case here because might be a UUCP name.
131712313Seric **
131812313Seric **	Mark, you can change this to be anything you want......
131910758Seric */
132010758Seric 
132110758Seric char **
132212313Seric myhostname(hostbuf, size)
132310758Seric 	char hostbuf[];
132412313Seric 	int size;
132510758Seric {
132610758Seric 	register FILE *f;
132710758Seric 
132810758Seric 	hostbuf[0] = '\0';
132910758Seric 	f = fopen("/usr/include/whoami", "r");
133010758Seric 	if (f != NULL)
133110758Seric 	{
133212313Seric 		(void) fgets(hostbuf, size, f);
133310758Seric 		fixcrlf(hostbuf, TRUE);
133410758Seric 		(void) fclose(f);
133510758Seric 	}
133610758Seric 	return (NULL);
133710758Seric }
133816911Seric /*
133958951Seric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
134058308Seric **
134158308Seric **	Parameters:
134258308Seric **		fd -- the descriptor
134358308Seric **
134458308Seric **	Returns:
134558308Seric **		The host name associated with this descriptor, if it can
134658308Seric **			be determined.
134758308Seric **		NULL otherwise.
134858308Seric **
134958308Seric **	Side Effects:
135058308Seric **		none
135158308Seric */
135258308Seric 
135358308Seric char *
135458951Seric getauthinfo(fd)
135558308Seric 	int fd;
135658308Seric {
135758308Seric 	return NULL;
135858308Seric }
135958308Seric /*
136016911Seric **  MAPHOSTNAME -- turn a hostname into canonical form
136116911Seric **
136216911Seric **	Parameters:
136356823Seric **		map -- a pointer to the database map.
136460089Seric **		name -- a buffer containing a hostname.
136553751Seric **		avp -- a pointer to a (cf file defined) argument vector.
136659084Seric **		statp -- an exit status (out parameter).
136716911Seric **
136816911Seric **	Returns:
136953751Seric **		mapped host name
137051315Seric **		FALSE otherwise.
137116911Seric **
137216911Seric **	Side Effects:
137360089Seric **		Looks up the host specified in name.  If it is not
137416911Seric **		the canonical name for that host, replace it with
137516911Seric **		the canonical name.  If the name is unknown, or it
137616911Seric **		is already the canonical name, leave it unchanged.
137716911Seric */
137810758Seric 
137916911Seric /*ARGSUSED*/
138053751Seric char *
138160089Seric host_map_lookup(map, name, avp, statp)
138256823Seric 	MAP *map;
138360089Seric 	char *name;
138453751Seric 	char **avp;
138559084Seric 	char *statp;
138616911Seric {
138759084Seric 	register struct hostent *hp;
138859084Seric 
138960089Seric 	hp = gethostbyname(name);
139059084Seric 	if (hp != NULL)
139159084Seric 		return hp->h_name;
139259084Seric 	*statp = EX_NOHOST;
139353751Seric 	return NULL;
139416911Seric }
139516911Seric 
139656795Seric #endif /* DAEMON */
1397