xref: /csrg-svn/usr.sbin/sendmail/src/daemon.c (revision 67419)
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*67419Seric static char sccsid[] = "@(#)daemon.c	8.53 (Berkeley) 06/17/94 (with daemon mode)";
1533780Sbostic #else
16*67419Seric static char sccsid[] = "@(#)daemon.c	8.53 (Berkeley) 06/17/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 
2566334Seric #if 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;
8653751Seric 	bool refusingconnections = TRUE;
8758419Seric 	FILE *pidf;
8864828Seric 	int socksize;
8966793Seric #ifdef XDEBUG
9066793Seric 	bool j_has_dot;
9166793Seric #endif
9246928Sbostic 	extern void reapchild();
937117Seric 
949610Seric 	/*
959610Seric 	**  Set up the address for the mailer.
969610Seric 	*/
979610Seric 
9858849Seric 	if (DaemonAddr.sin.sin_family == 0)
9958849Seric 		DaemonAddr.sin.sin_family = AF_INET;
10058849Seric 	if (DaemonAddr.sin.sin_addr.s_addr == 0)
10158849Seric 		DaemonAddr.sin.sin_addr.s_addr = INADDR_ANY;
10258849Seric 	if (DaemonAddr.sin.sin_port == 0)
1039610Seric 	{
10465169Seric 		register struct servent *sp;
10565169Seric 
10658849Seric 		sp = getservbyname("smtp", "tcp");
10758849Seric 		if (sp == NULL)
10858849Seric 		{
10958909Seric 			syserr("554 service \"smtp\" unknown");
11065169Seric 			DaemonAddr.sin.sin_port = htons(25);
11158849Seric 		}
11265169Seric 		else
11365169Seric 			DaemonAddr.sin.sin_port = sp->s_port;
1149610Seric 	}
1159610Seric 
1169610Seric 	/*
1179610Seric 	**  Try to actually open the connection.
1189610Seric 	*/
1199610Seric 
1209610Seric 	if (tTd(15, 1))
12158849Seric 		printf("getrequests: port 0x%x\n", DaemonAddr.sin.sin_port);
1229610Seric 
1239610Seric 	/* get a socket for the SMTP connection */
12466854Seric 	socksize = opendaemonsocket(TRUE);
12510347Seric 
12664035Seric 	(void) setsignal(SIGCHLD, reapchild);
12724945Seric 
12858419Seric 	/* write the pid to the log file for posterity */
12958419Seric 	pidf = fopen(PidFile, "w");
13058419Seric 	if (pidf != NULL)
13158419Seric 	{
13263863Seric 		extern char *CommandLineArgs;
13363863Seric 
13463863Seric 		/* write the process id on line 1 */
13558419Seric 		fprintf(pidf, "%d\n", getpid());
13663863Seric 
13763863Seric 		/* line 2 contains all command line flags */
13863863Seric 		fprintf(pidf, "%s\n", CommandLineArgs);
13963863Seric 
14063863Seric 		/* flush and close */
14158419Seric 		fclose(pidf);
14258419Seric 	}
14358419Seric 
14466793Seric #ifdef XDEBUG
14566793Seric 	{
14666812Seric 		char jbuf[MAXHOSTNAMELEN];
14758419Seric 
14866812Seric 		expand("\201j", jbuf, &jbuf[sizeof jbuf - 1], CurEnv);
14966812Seric 		j_has_dot = strchr(jbuf, '.') != NULL;
15066793Seric 	}
15166793Seric #endif
15266793Seric 
1539610Seric 	if (tTd(15, 1))
15410206Seric 		printf("getrequests: %d\n", DaemonSocket);
1559610Seric 
1564631Seric 	for (;;)
1574631Seric 	{
15814875Seric 		register int pid;
15911147Seric 		auto int lotherend;
16053751Seric 		extern bool refuseconnections();
16111147Seric 
16214875Seric 		/* see if we are rejecting connections */
16353751Seric 		CurrentLA = getla();
16453751Seric 		if (refuseconnections())
16536584Sbostic 		{
16666845Seric 			if (DaemonSocket >= 0)
16753751Seric 			{
16866845Seric 				/* close socket so peer will fail quickly */
16966845Seric 				(void) close(DaemonSocket);
17066845Seric 				DaemonSocket = -1;
17153751Seric 			}
17266845Seric 			refusingconnections = TRUE;
17357385Seric 			setproctitle("rejecting connections: load average: %d",
17457385Seric 				CurrentLA);
17566845Seric 			sleep(15);
17653751Seric 			continue;
17736584Sbostic 		}
17814875Seric 
17953751Seric 		if (refusingconnections)
18053751Seric 		{
18153751Seric 			/* start listening again */
18266854Seric 			(void) opendaemonsocket(FALSE);
18353751Seric 			setproctitle("accepting connections");
18453751Seric 			refusingconnections = FALSE;
18553751Seric 		}
18653751Seric 
18766793Seric #ifdef XDEBUG
18866793Seric 		/* check for disaster */
18966793Seric 		{
19066793Seric 			register STAB *s;
19166812Seric 			char jbuf[MAXHOSTNAMELEN];
19266793Seric 
19366812Seric 			expand("\201j", jbuf, &jbuf[sizeof jbuf - 1], CurEnv);
19466812Seric 			if ((s = stab(jbuf, ST_CLASS, ST_FIND)) == NULL ||
19566793Seric 			    !bitnset('w', s->s_class))
19666793Seric 			{
19766793Seric 				dumpstate("daemon lost $j");
19866793Seric 				syslog(LOG_ALERT, "daemon process doesn't have $j in $=w; see syslog");
19966793Seric 				abort();
20066793Seric 			}
20166812Seric 			else if (j_has_dot && strchr(jbuf, '.') == NULL)
20266793Seric 			{
20366793Seric 				dumpstate("daemon $j lost dot");
20466793Seric 				syslog(LOG_ALERT, "daemon process $j lost dot; see syslog");
20566793Seric 				abort();
20666793Seric 			}
20766793Seric 		}
20866793Seric #endif
20966793Seric 
2109610Seric 		/* wait for a connection */
2119610Seric 		do
2129610Seric 		{
2139610Seric 			errno = 0;
21464828Seric 			lotherend = socksize;
21546928Sbostic 			t = accept(DaemonSocket,
21646928Sbostic 			    (struct sockaddr *)&RealHostAddr, &lotherend);
2179610Seric 		} while (t < 0 && errno == EINTR);
2189610Seric 		if (t < 0)
2195978Seric 		{
2209610Seric 			syserr("getrequests: accept");
2219610Seric 			sleep(5);
2229610Seric 			continue;
2235978Seric 		}
2244631Seric 
2255978Seric 		/*
2265978Seric 		**  Create a subprocess to process the mail.
2275978Seric 		*/
2285978Seric 
2297677Seric 		if (tTd(15, 2))
2309610Seric 			printf("getrequests: forking (fd = %d)\n", t);
2315978Seric 
2324636Seric 		pid = fork();
2334636Seric 		if (pid < 0)
2344631Seric 		{
2354636Seric 			syserr("daemon: cannot fork");
2364636Seric 			sleep(10);
2379610Seric 			(void) close(t);
2384636Seric 			continue;
2394631Seric 		}
2404631Seric 
2414636Seric 		if (pid == 0)
2424631Seric 		{
24364086Seric 			char *p;
24458951Seric 			extern char *hostnamebyanyaddr();
24511147Seric 
2464636Seric 			/*
2474636Seric 			**  CHILD -- return to caller.
24811147Seric 			**	Collect verified idea of sending host.
2494636Seric 			**	Verify calling user id if possible here.
2504636Seric 			*/
2514631Seric 
25264035Seric 			(void) setsignal(SIGCHLD, SIG_DFL);
25367171Seric 			(void) close(DaemonSocket);
25466017Seric 			DisConnected = FALSE;
25524950Seric 
25666032Seric 			setproctitle("startup with %s",
25766032Seric 				anynet_ntoa(&RealHostAddr));
25866032Seric 
25911147Seric 			/* determine host name */
26064086Seric 			p = hostnamebyanyaddr(&RealHostAddr);
26164086Seric 			RealHostName = newstr(p);
26266032Seric 			setproctitle("startup with %s", p);
26358778Seric 
26455173Seric #ifdef LOG
26563842Seric 			if (LogLevel > 11)
26655173Seric 			{
26755173Seric 				/* log connection information */
26855173Seric 				syslog(LOG_INFO, "connect from %s (%s)",
26958951Seric 					RealHostName, anynet_ntoa(&RealHostAddr));
27055173Seric 			}
27155173Seric #endif
27255173Seric 
27364724Seric 			if ((InChannel = fdopen(t, "r")) == NULL ||
27464724Seric 			    (t = dup(t)) < 0 ||
27564724Seric 			    (OutChannel = fdopen(t, "w")) == NULL)
27664724Seric 			{
27764724Seric 				syserr("cannot open SMTP server channel, fd=%d", t);
27864724Seric 				exit(0);
27964724Seric 			}
28059254Seric 
28116884Seric 			/* should we check for illegal connection here? XXX */
28259156Seric #ifdef XLA
28359156Seric 			if (!xla_host_ok(RealHostName))
28459156Seric 			{
28559254Seric 				message("421 Too many SMTP sessions for this host");
28659156Seric 				exit(0);
28759156Seric 			}
28859156Seric #endif
28916884Seric 
2907677Seric 			if (tTd(15, 2))
2915978Seric 				printf("getreq: returning\n");
2924636Seric 			return;
2934631Seric 		}
2944631Seric 
2957117Seric 		/* close the port so that others will hang (for a while) */
2969610Seric 		(void) close(t);
2974631Seric 	}
2989886Seric 	/*NOTREACHED*/
2994631Seric }
3005978Seric /*
30166845Seric **  OPENDAEMONSOCKET -- open the SMTP socket
30266845Seric **
30366845Seric **	Deals with setting all appropriate options.  DaemonAddr must
30466845Seric **	be set up in advance.
30566845Seric **
30666845Seric **	Parameters:
30766854Seric **		firsttime -- set if this is the initial open.
30866845Seric **
30966845Seric **	Returns:
31066845Seric **		Size in bytes of the daemon socket addr.
31166845Seric **
31266845Seric **	Side Effects:
31366845Seric **		Leaves DaemonSocket set to the open socket.
31466845Seric **		Exits if the socket cannot be created.
31566845Seric */
31666845Seric 
31766861Seric #define MAXOPENTRIES	10	/* maximum number of tries to open connection */
31866861Seric 
31966845Seric int
32066854Seric opendaemonsocket(firsttime)
32166854Seric 	bool firsttime;
32266845Seric {
32366845Seric 	int on = 1;
32466845Seric 	int socksize;
32566861Seric 	int ntries = 0;
32666861Seric 	int saveerrno;
32766845Seric 
32866845Seric 	if (tTd(15, 2))
32966845Seric 		printf("opendaemonsocket()\n");
33066845Seric 
33166861Seric 	do
33266845Seric 	{
33366862Seric 		if (ntries > 0)
33466862Seric 			sleep(5);
33566861Seric 		if (firsttime || DaemonSocket < 0)
33666854Seric 		{
33766861Seric 			DaemonSocket = socket(DaemonAddr.sa.sa_family, SOCK_STREAM, 0);
33866861Seric 			if (DaemonSocket < 0)
33966861Seric 			{
34066861Seric 				/* probably another daemon already */
34166861Seric 				saveerrno = errno;
34266861Seric 				syserr("opendaemonsocket: can't create server SMTP socket");
34366861Seric 			  severe:
34466845Seric # ifdef LOG
34566861Seric 				if (LogLevel > 0)
34666861Seric 					syslog(LOG_ALERT, "problem creating SMTP socket");
34766845Seric # endif /* LOG */
34866861Seric 				DaemonSocket = -1;
34966861Seric 				continue;
35066861Seric 			}
35166845Seric 
35266861Seric 			/* turn on network debugging? */
35366861Seric 			if (tTd(15, 101))
35466861Seric 				(void) setsockopt(DaemonSocket, SOL_SOCKET,
35566861Seric 						  SO_DEBUG, (char *)&on,
35666861Seric 						  sizeof on);
35766845Seric 
35866861Seric 			(void) setsockopt(DaemonSocket, SOL_SOCKET,
35966861Seric 					  SO_REUSEADDR, (char *)&on, sizeof on);
36066861Seric 			(void) setsockopt(DaemonSocket, SOL_SOCKET,
36166861Seric 					  SO_KEEPALIVE, (char *)&on, sizeof on);
36266845Seric 
36366845Seric #ifdef SO_RCVBUF
36466861Seric 			if (TcpRcvBufferSize > 0)
36566861Seric 			{
36666861Seric 				if (setsockopt(DaemonSocket, SOL_SOCKET,
36766861Seric 					       SO_RCVBUF,
36866861Seric 					       (char *) &TcpRcvBufferSize,
36966861Seric 					       sizeof(TcpRcvBufferSize)) < 0)
37066861Seric 					syserr("getrequests: setsockopt(SO_RCVBUF)");
37166861Seric 			}
37266845Seric #endif
37366845Seric 
37466861Seric 			switch (DaemonAddr.sa.sa_family)
37566861Seric 			{
37666845Seric # ifdef NETINET
37766861Seric 			  case AF_INET:
37866861Seric 				socksize = sizeof DaemonAddr.sin;
37966861Seric 				break;
38066845Seric # endif
38166845Seric 
38266845Seric # ifdef NETISO
38366861Seric 			  case AF_ISO:
38466861Seric 				socksize = sizeof DaemonAddr.siso;
38566861Seric 				break;
38666845Seric # endif
38766845Seric 
38866861Seric 			  default:
38966861Seric 				socksize = sizeof DaemonAddr;
39066861Seric 				break;
39166861Seric 			}
39266861Seric 
39366861Seric 			if (bind(DaemonSocket, &DaemonAddr.sa, socksize) < 0)
39466861Seric 			{
39566861Seric 				saveerrno = errno;
39666861Seric 				syserr("getrequests: cannot bind");
39766861Seric 				(void) close(DaemonSocket);
39866861Seric 				goto severe;
39966861Seric 			}
40066854Seric 		}
40166861Seric 		if (!firsttime && listen(DaemonSocket, ListenQueueSize) < 0)
40266854Seric 		{
40366861Seric 			saveerrno = errno;
40466861Seric 			syserr("getrequests: cannot listen");
40566854Seric 			(void) close(DaemonSocket);
40666854Seric 			goto severe;
40766854Seric 		}
40866861Seric 		return socksize;
40966861Seric 	} while (ntries++ < MAXOPENTRIES && transienterror(saveerrno));
41066861Seric 	finis();
41166845Seric }
41266845Seric /*
41310206Seric **  CLRDAEMON -- reset the daemon connection
41410206Seric **
41510206Seric **	Parameters:
41610206Seric **		none.
41710206Seric **
41810206Seric **	Returns:
41910206Seric **		none.
42010206Seric **
42110206Seric **	Side Effects:
42210206Seric **		releases any resources used by the passive daemon.
42310206Seric */
42410206Seric 
42510206Seric clrdaemon()
42610206Seric {
42710206Seric 	if (DaemonSocket >= 0)
42810206Seric 		(void) close(DaemonSocket);
42910206Seric 	DaemonSocket = -1;
43010206Seric }
43110206Seric /*
43258849Seric **  SETDAEMONOPTIONS -- set options for running the daemon
43358849Seric **
43458849Seric **	Parameters:
43558849Seric **		p -- the options line.
43658849Seric **
43758849Seric **	Returns:
43858849Seric **		none.
43958849Seric */
44058849Seric 
44158849Seric setdaemonoptions(p)
44258849Seric 	register char *p;
44358849Seric {
44458873Seric 	if (DaemonAddr.sa.sa_family == AF_UNSPEC)
44558873Seric 		DaemonAddr.sa.sa_family = AF_INET;
44658873Seric 
44758849Seric 	while (p != NULL)
44858849Seric 	{
44958849Seric 		register char *f;
45058849Seric 		register char *v;
45158849Seric 
45258849Seric 		while (isascii(*p) && isspace(*p))
45358849Seric 			p++;
45458849Seric 		if (*p == '\0')
45558849Seric 			break;
45658849Seric 		f = p;
45758849Seric 		p = strchr(p, ',');
45858849Seric 		if (p != NULL)
45958849Seric 			*p++ = '\0';
46058849Seric 		v = strchr(f, '=');
46158849Seric 		if (v == NULL)
46258849Seric 			continue;
46358849Seric 		while (isascii(*++v) && isspace(*v))
46458849Seric 			continue;
46558849Seric 
46658849Seric 		switch (*f)
46758849Seric 		{
46858873Seric 		  case 'F':		/* address family */
46958849Seric 			if (isascii(*v) && isdigit(*v))
47058873Seric 				DaemonAddr.sa.sa_family = atoi(v);
47158873Seric #ifdef NETINET
47258873Seric 			else if (strcasecmp(v, "inet") == 0)
47358873Seric 				DaemonAddr.sa.sa_family = AF_INET;
47458873Seric #endif
47558873Seric #ifdef NETISO
47658873Seric 			else if (strcasecmp(v, "iso") == 0)
47758873Seric 				DaemonAddr.sa.sa_family = AF_ISO;
47858873Seric #endif
47958873Seric #ifdef NETNS
48058873Seric 			else if (strcasecmp(v, "ns") == 0)
48158873Seric 				DaemonAddr.sa.sa_family = AF_NS;
48258873Seric #endif
48358873Seric #ifdef NETX25
48458873Seric 			else if (strcasecmp(v, "x.25") == 0)
48558873Seric 				DaemonAddr.sa.sa_family = AF_CCITT;
48658873Seric #endif
48758849Seric 			else
48858873Seric 				syserr("554 Unknown address family %s in Family=option", v);
48958873Seric 			break;
49058873Seric 
49158873Seric 		  case 'A':		/* address */
49258873Seric 			switch (DaemonAddr.sa.sa_family)
49358849Seric 			{
49458873Seric #ifdef NETINET
49558873Seric 			  case AF_INET:
49658873Seric 				if (isascii(*v) && isdigit(*v))
49758873Seric 					DaemonAddr.sin.sin_addr.s_addr = inet_network(v);
49858873Seric 				else
49958873Seric 				{
50058873Seric 					register struct netent *np;
50158849Seric 
50258873Seric 					np = getnetbyname(v);
50358873Seric 					if (np == NULL)
50458873Seric 						syserr("554 network \"%s\" unknown", v);
50558873Seric 					else
50658873Seric 						DaemonAddr.sin.sin_addr.s_addr = np->n_net;
50758873Seric 				}
50858873Seric 				break;
50958873Seric #endif
51058873Seric 
51158873Seric 			  default:
51258873Seric 				syserr("554 Address= option unsupported for family %d",
51358873Seric 					DaemonAddr.sa.sa_family);
51458873Seric 				break;
51558849Seric 			}
51658849Seric 			break;
51758849Seric 
51858873Seric 		  case 'P':		/* port */
51958873Seric 			switch (DaemonAddr.sa.sa_family)
52058849Seric 			{
52158873Seric 				short port;
52258849Seric 
52358873Seric #ifdef NETINET
52458873Seric 			  case AF_INET:
52558873Seric 				if (isascii(*v) && isdigit(*v))
52664366Seric 					DaemonAddr.sin.sin_port = htons(atoi(v));
52758849Seric 				else
52858873Seric 				{
52958873Seric 					register struct servent *sp;
53058873Seric 
53158873Seric 					sp = getservbyname(v, "tcp");
53258873Seric 					if (sp == NULL)
53358909Seric 						syserr("554 service \"%s\" unknown", v);
53458873Seric 					else
53558873Seric 						DaemonAddr.sin.sin_port = sp->s_port;
53658873Seric 				}
53758873Seric 				break;
53858873Seric #endif
53958873Seric 
54058873Seric #ifdef NETISO
54158873Seric 			  case AF_ISO:
54258873Seric 				/* assume two byte transport selector */
54358873Seric 				if (isascii(*v) && isdigit(*v))
54464366Seric 					port = htons(atoi(v));
54558873Seric 				else
54658873Seric 				{
54758873Seric 					register struct servent *sp;
54858873Seric 
54958873Seric 					sp = getservbyname(v, "tcp");
55058873Seric 					if (sp == NULL)
55158909Seric 						syserr("554 service \"%s\" unknown", v);
55258873Seric 					else
55358873Seric 						port = sp->s_port;
55458873Seric 				}
55558873Seric 				bcopy((char *) &port, TSEL(&DaemonAddr.siso), 2);
55658873Seric 				break;
55758873Seric #endif
55858873Seric 
55958873Seric 			  default:
56058873Seric 				syserr("554 Port= option unsupported for family %d",
56158873Seric 					DaemonAddr.sa.sa_family);
56258873Seric 				break;
56358849Seric 			}
56458849Seric 			break;
56559783Seric 
56659783Seric 		  case 'L':		/* listen queue size */
56759783Seric 			ListenQueueSize = atoi(v);
56859783Seric 			break;
56964381Seric 
57064381Seric 		  case 'S':		/* send buffer size */
57164381Seric 			TcpSndBufferSize = atoi(v);
57264381Seric 			break;
57364381Seric 
57464381Seric 		  case 'R':		/* receive buffer size */
57564381Seric 			TcpRcvBufferSize = atoi(v);
57664381Seric 			break;
57758849Seric 		}
57858849Seric 	}
57958849Seric }
58058849Seric /*
5816039Seric **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
5826039Seric **
5836039Seric **	Parameters:
5846039Seric **		host -- the name of the host.
5856633Seric **		port -- the port number to connect to.
58653739Seric **		mci -- a pointer to the mail connection information
58753739Seric **			structure to be filled in.
58852106Seric **		usesecureport -- if set, use a low numbered (reserved)
58952106Seric **			port to provide some rudimentary authentication.
5906039Seric **
5916039Seric **	Returns:
5926039Seric **		An exit code telling whether the connection could be
5936039Seric **			made and if not why not.
5946039Seric **
5956039Seric **	Side Effects:
5966039Seric **		none.
5976039Seric */
5985978Seric 
59958755Seric SOCKADDR	CurHostAddr;		/* address of current host */
60058305Seric 
60154967Seric int
60253739Seric makeconnection(host, port, mci, usesecureport)
6036039Seric 	char *host;
6047286Seric 	u_short port;
60554967Seric 	register MCI *mci;
60652106Seric 	bool usesecureport;
6076039Seric {
60829430Sbloom 	register int i, s;
60929430Sbloom 	register struct hostent *hp = (struct hostent *)NULL;
61058755Seric 	SOCKADDR addr;
61152106Seric 	int sav_errno;
61258755Seric 	int addrlen;
61366334Seric #if NAMED_BIND
61435651Seric 	extern int h_errno;
61535651Seric #endif
6166039Seric 
6176039Seric 	/*
6186039Seric 	**  Set up the address for the mailer.
6199308Seric 	**	Accept "[a.b.c.d]" syntax for host name.
6206039Seric 	*/
6216039Seric 
62266334Seric #if NAMED_BIND
62325475Smiriam 	h_errno = 0;
62435651Seric #endif
62525475Smiriam 	errno = 0;
62658864Seric 	bzero(&CurHostAddr, sizeof CurHostAddr);
62764334Seric 	SmtpPhase = mci->mci_phase = "initial connection";
62858906Seric 	CurHostName = host;
62925475Smiriam 
6309308Seric 	if (host[0] == '[')
6319308Seric 	{
63211147Seric 		long hid;
63356795Seric 		register char *p = strchr(host, ']');
6349308Seric 
63511147Seric 		if (p != NULL)
6369308Seric 		{
63711147Seric 			*p = '\0';
63859884Seric #ifdef NETINET
63911147Seric 			hid = inet_addr(&host[1]);
64058360Seric 			if (hid == -1)
64159884Seric #endif
64258360Seric 			{
64358360Seric 				/* try it as a host name (avoid MX lookup) */
64458360Seric 				hp = gethostbyname(&host[1]);
64566349Seric 				if (hp == NULL && p[-1] == '.')
64666349Seric 				{
64767265Seric #ifdef NAMED_BIND
64867265Seric 					int oldopts = _res.options;
64967265Seric 
65067265Seric 					_res.options &= ~(RES_DEFNAMES|RES_DNSRCH);
65167265Seric #endif
65266349Seric 					p[-1] = '\0';
65366349Seric 					hp = gethostbyname(&host[1]);
65466349Seric 					p[-1] = '.';
65567265Seric #ifdef NAMED_BIND
65667265Seric 					_res.options = oldopts;
65767265Seric #endif
65866349Seric 				}
65958360Seric 				*p = ']';
66058360Seric 				goto gothostent;
66158360Seric 			}
66211147Seric 			*p = ']';
6639308Seric 		}
66458360Seric 		if (p == NULL)
6659308Seric 		{
66658151Seric 			usrerr("553 Invalid numeric domain spec \"%s\"", host);
6679308Seric 			return (EX_NOHOST);
6689308Seric 		}
66959884Seric #ifdef NETINET
67059884Seric 		addr.sin.sin_family = AF_INET;		/*XXX*/
67158778Seric 		addr.sin.sin_addr.s_addr = hid;
67259884Seric #endif
6739308Seric 	}
6749610Seric 	else
6759610Seric 	{
67666349Seric 		register char *p = &host[strlen(host) - 1];
67766349Seric 
67829430Sbloom 		hp = gethostbyname(host);
67966349Seric 		if (hp == NULL && *p == '.')
68066349Seric 		{
68167265Seric #ifdef NAMED_BIND
68267265Seric 			int oldopts = _res.options;
68367265Seric 
68467265Seric 			_res.options &= ~(RES_DEFNAMES|RES_DNSRCH);
68567265Seric #endif
68666349Seric 			*p = '\0';
68766349Seric 			hp = gethostbyname(host);
68866349Seric 			*p = '.';
68967265Seric #ifdef NAMED_BIND
69067265Seric 			_res.options = oldopts;
69167265Seric #endif
69266349Seric 		}
69358360Seric gothostent:
69425475Smiriam 		if (hp == NULL)
69524945Seric 		{
69666334Seric #if NAMED_BIND
69725475Smiriam 			if (errno == ETIMEDOUT || h_errno == TRY_AGAIN)
69825475Smiriam 				return (EX_TEMPFAIL);
69925657Seric 
70035651Seric 			/* if name server is specified, assume temp fail */
70135651Seric 			if (errno == ECONNREFUSED && UseNameServer)
70235651Seric 				return (EX_TEMPFAIL);
70335651Seric #endif
70425475Smiriam 			return (EX_NOHOST);
70524945Seric 		}
70658778Seric 		addr.sa.sa_family = hp->h_addrtype;
70758778Seric 		switch (hp->h_addrtype)
70858778Seric 		{
70958778Seric #ifdef NETINET
71058778Seric 		  case AF_INET:
71158755Seric 			bcopy(hp->h_addr,
71258778Seric 				&addr.sin.sin_addr,
713*67419Seric 				IPADDRSIZE);
71458778Seric 			break;
71558778Seric #endif
71658778Seric 
71758778Seric 		  default:
71858755Seric 			bcopy(hp->h_addr,
71958778Seric 				addr.sa.sa_data,
72058755Seric 				hp->h_length);
72158778Seric 			break;
72258778Seric 		}
72329430Sbloom 		i = 1;
7249610Seric 	}
7259610Seric 
7269610Seric 	/*
7279610Seric 	**  Determine the port number.
7289610Seric 	*/
7299610Seric 
73010011Seric 	if (port != 0)
73158755Seric 		port = htons(port);
73210011Seric 	else
7339610Seric 	{
7349610Seric 		register struct servent *sp = getservbyname("smtp", "tcp");
7359610Seric 
7369610Seric 		if (sp == NULL)
7379610Seric 		{
73858909Seric 			syserr("554 makeconnection: service \"smtp\" unknown");
73965169Seric 			port = htons(25);
7409610Seric 		}
74165169Seric 		else
74265169Seric 			port = sp->s_port;
7439610Seric 	}
7446039Seric 
74558778Seric 	switch (addr.sa.sa_family)
74658755Seric 	{
74759884Seric #ifdef NETINET
74858755Seric 	  case AF_INET:
74958778Seric 		addr.sin.sin_port = port;
75058755Seric 		addrlen = sizeof (struct sockaddr_in);
75158755Seric 		break;
75259884Seric #endif
75358755Seric 
75458755Seric #ifdef NETISO
75558755Seric 	  case AF_ISO:
75658755Seric 		/* assume two byte transport selector */
75758755Seric 		bcopy((char *) &port, TSEL((struct sockaddr_iso *) &addr), 2);
75858755Seric 		addrlen = sizeof (struct sockaddr_iso);
75958755Seric 		break;
76058755Seric #endif
76158755Seric 
76258755Seric 	  default:
76358778Seric 		syserr("Can't connect to address family %d", addr.sa.sa_family);
76458755Seric 		return (EX_NOHOST);
76558755Seric 	}
76658755Seric 
7676039Seric 	/*
7686039Seric 	**  Try to actually open the connection.
7696039Seric 	*/
7706039Seric 
77159156Seric #ifdef XLA
77259156Seric 	/* if too many connections, don't bother trying */
77359156Seric 	if (!xla_noqueue_ok(host))
77459156Seric 		return EX_TEMPFAIL;
77559156Seric #endif
77659156Seric 
77757736Seric 	for (;;)
77852106Seric 	{
77957736Seric 		if (tTd(16, 1))
78058755Seric 			printf("makeconnection (%s [%s])\n",
78158755Seric 				host, anynet_ntoa(&addr));
78252106Seric 
78358588Seric 		/* save for logging */
78458588Seric 		CurHostAddr = addr;
78558588Seric 
78657736Seric 		if (usesecureport)
78757736Seric 		{
78857736Seric 			int rport = IPPORT_RESERVED - 1;
7896039Seric 
79057736Seric 			s = rresvport(&rport);
79157736Seric 		}
79257736Seric 		else
79357736Seric 		{
79457736Seric 			s = socket(AF_INET, SOCK_STREAM, 0);
79557736Seric 		}
79657736Seric 		if (s < 0)
79757736Seric 		{
79857736Seric 			sav_errno = errno;
79957736Seric 			syserr("makeconnection: no socket");
80057736Seric 			goto failure;
80157736Seric 		}
80210347Seric 
80364381Seric #ifdef SO_SNDBUF
80464381Seric 		if (TcpSndBufferSize > 0)
80564381Seric 		{
80664381Seric 			if (setsockopt(s, SOL_SOCKET, SO_SNDBUF,
80764561Seric 				       (char *) &TcpSndBufferSize,
80864381Seric 				       sizeof(TcpSndBufferSize)) < 0)
80964381Seric 				syserr("makeconnection: setsockopt(SO_SNDBUF)");
81064381Seric 		}
81164381Seric #endif
81264381Seric 
81357736Seric 		if (tTd(16, 1))
81457736Seric 			printf("makeconnection: fd=%d\n", s);
81557736Seric 
81657736Seric 		/* turn on network debugging? */
81757736Seric 		if (tTd(16, 101))
81857736Seric 		{
81957736Seric 			int on = 1;
82066861Seric 			(void) setsockopt(s, SOL_SOCKET, SO_DEBUG,
82157736Seric 					  (char *)&on, sizeof on);
82257736Seric 		}
82357736Seric 		if (CurEnv->e_xfp != NULL)
82457736Seric 			(void) fflush(CurEnv->e_xfp);		/* for debugging */
82557736Seric 		errno = 0;					/* for debugging */
82658755Seric 		if (connect(s, (struct sockaddr *) &addr, addrlen) >= 0)
82757736Seric 			break;
82857736Seric 
82957736Seric 		/* couldn't connect.... figure out why */
83027744Sbloom 		sav_errno = errno;
83127744Sbloom 		(void) close(s);
83229430Sbloom 		if (hp && hp->h_addr_list[i])
83329430Sbloom 		{
83457736Seric 			if (tTd(16, 1))
83558755Seric 				printf("Connect failed (%s); trying new address....\n",
83658755Seric 					errstring(sav_errno));
83758778Seric 			switch (addr.sa.sa_family)
83858778Seric 			{
83958778Seric #ifdef NETINET
84058778Seric 			  case AF_INET:
84158755Seric 				bcopy(hp->h_addr_list[i++],
84258778Seric 				      &addr.sin.sin_addr,
843*67419Seric 				      IPADDRSIZE);
84458778Seric 				break;
84558778Seric #endif
84658778Seric 
84758778Seric 			  default:
84858755Seric 				bcopy(hp->h_addr_list[i++],
84958778Seric 					addr.sa.sa_data,
85052106Seric 					hp->h_length);
85158778Seric 				break;
85258778Seric 			}
85357736Seric 			continue;
85429430Sbloom 		}
85529430Sbloom 
8566039Seric 		/* failure, decide if temporary or not */
8576039Seric 	failure:
85859254Seric #ifdef XLA
85959254Seric 		xla_host_end(host);
86059254Seric #endif
86158542Seric 		if (transienterror(sav_errno))
86258542Seric 			return EX_TEMPFAIL;
86358542Seric 		else
86458542Seric 		{
86558542Seric 			message("%s", errstring(sav_errno));
86658542Seric 			return (EX_UNAVAILABLE);
8676039Seric 		}
8686039Seric 	}
8696039Seric 
8706039Seric 	/* connection ok, put it into canonical form */
87164724Seric 	if ((mci->mci_out = fdopen(s, "w")) == NULL ||
87264724Seric 	    (s = dup(s)) < 0 ||
87364725Seric 	    (mci->mci_in = fdopen(s, "r")) == NULL)
87464724Seric 	{
87564724Seric 		syserr("cannot open SMTP client channel, fd=%d", s);
87664724Seric 		return EX_TEMPFAIL;
87764724Seric 	}
8786039Seric 
87910098Seric 	return (EX_OK);
8806039Seric }
88110758Seric /*
88210758Seric **  MYHOSTNAME -- return the name of this host.
88310758Seric **
88410758Seric **	Parameters:
88510758Seric **		hostbuf -- a place to return the name of this host.
88612313Seric **		size -- the size of hostbuf.
88710758Seric **
88810758Seric **	Returns:
88910758Seric **		A list of aliases for this host.
89010758Seric **
89110758Seric **	Side Effects:
89264338Seric **		Adds numeric codes to $=w.
89310758Seric */
8946039Seric 
89567140Seric struct hostent *
89612313Seric myhostname(hostbuf, size)
89710758Seric 	char hostbuf[];
89812313Seric 	int size;
89910758Seric {
90058110Seric 	register struct hostent *hp;
90110758Seric 	extern struct hostent *gethostbyname();
90210758Seric 
90323120Seric 	if (gethostname(hostbuf, size) < 0)
90423120Seric 	{
90523120Seric 		(void) strcpy(hostbuf, "localhost");
90623120Seric 	}
90711147Seric 	hp = gethostbyname(hostbuf);
90866853Seric 	if (hp == NULL)
90916877Seric 	{
91066853Seric 		syserr("!My host name (%s) does not seem to exist!", hostbuf);
91166853Seric 	}
91266853Seric 	(void) strncpy(hostbuf, hp->h_name, size - 1);
91366853Seric 	hostbuf[size - 1] = '\0';
91466853Seric 
91566853Seric #if NAMED_BIND
91666853Seric 	/* if still no dot, try DNS directly (i.e., avoid NIS problems) */
91766853Seric 	if (strchr(hostbuf, '.') == NULL)
91866853Seric 	{
91966853Seric 		extern bool getcanonname();
92066853Seric 		extern int h_errno;
92166853Seric 
92266853Seric 		/* try twice in case name server not yet started up */
92366853Seric 		if (!getcanonname(hostbuf, size, TRUE) &&
92466853Seric 		    UseNameServer &&
92566853Seric 		    (h_errno != TRY_AGAIN ||
92666853Seric 		     (sleep(30), !getcanonname(hostbuf, size, TRUE))))
92766777Seric 		{
92866853Seric 			errno = h_errno + E_DNSBASE;
92966853Seric 			syserr("!My host name (%s) not known to DNS",
93066853Seric 				hostbuf);
93166777Seric 		}
93266853Seric 	}
93366777Seric #endif
93467140Seric 	return (hp);
93510758Seric }
93651315Seric /*
93758951Seric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
93858308Seric **
93958951Seric **	Uses RFC1413 protocol to try to get info from the other end.
94058951Seric **
94158308Seric **	Parameters:
94258308Seric **		fd -- the descriptor
94358308Seric **
94458308Seric **	Returns:
94558951Seric **		The user@host information associated with this descriptor.
94658308Seric */
94758308Seric 
94864927Seric #if IDENTPROTO
94958951Seric 
95058951Seric static jmp_buf	CtxAuthTimeout;
95158951Seric 
95258951Seric static
95358951Seric authtimeout()
95458951Seric {
95558951Seric 	longjmp(CtxAuthTimeout, 1);
95658951Seric }
95758951Seric 
95858951Seric #endif
95958951Seric 
96058308Seric char *
96158951Seric getauthinfo(fd)
96258308Seric 	int fd;
96358308Seric {
96458951Seric 	int falen;
96559104Seric 	register char *p;
96664927Seric #if IDENTPROTO
96758951Seric 	SOCKADDR la;
96858951Seric 	int lalen;
96958951Seric 	register struct servent *sp;
97058951Seric 	int s;
97158951Seric 	int i;
97258951Seric 	EVENT *ev;
97358951Seric #endif
97458951Seric 	static char hbuf[MAXNAME * 2 + 2];
97558951Seric 	extern char *hostnamebyanyaddr();
97658951Seric 	extern char RealUserName[];			/* main.c */
97758308Seric 
97866761Seric 	falen = sizeof RealHostAddr;
97966761Seric 	if (getpeername(fd, &RealHostAddr.sa, &falen) < 0 || falen <= 0 ||
98066761Seric 	    RealHostAddr.sa.sa_family == 0)
98158951Seric 	{
98258951Seric 		(void) sprintf(hbuf, "%s@localhost", RealUserName);
98358957Seric 		if (tTd(9, 1))
98458951Seric 			printf("getauthinfo: %s\n", hbuf);
98558951Seric 		return hbuf;
98658951Seric 	}
98758951Seric 
98866761Seric 	if (RealHostName == NULL)
98966761Seric 	{
99066761Seric 		/* translate that to a host name */
99166761Seric 		RealHostName = newstr(hostnamebyanyaddr(&RealHostAddr));
99266761Seric 	}
99366761Seric 
99464927Seric #if IDENTPROTO
99565831Seric 	if (TimeOuts.to_ident == 0)
99665831Seric 		goto noident;
99765831Seric 
99858951Seric 	lalen = sizeof la;
99966761Seric 	if (RealHostAddr.sa.sa_family != AF_INET ||
100058951Seric 	    getsockname(fd, &la.sa, &lalen) < 0 || lalen <= 0 ||
100158951Seric 	    la.sa.sa_family != AF_INET)
100258951Seric 	{
100358951Seric 		/* no ident info */
100458951Seric 		goto noident;
100558951Seric 	}
100658951Seric 
100758951Seric 	/* create ident query */
100860489Seric 	(void) sprintf(hbuf, "%d,%d\r\n",
100966761Seric 		ntohs(RealHostAddr.sin.sin_port), ntohs(la.sin.sin_port));
101058951Seric 
101158951Seric 	/* create local address */
101264747Seric 	la.sin.sin_port = 0;
101358951Seric 
101458951Seric 	/* create foreign address */
101558951Seric 	sp = getservbyname("auth", "tcp");
101658951Seric 	if (sp != NULL)
101766761Seric 		RealHostAddr.sin.sin_port = sp->s_port;
101858308Seric 	else
101966761Seric 		RealHostAddr.sin.sin_port = htons(113);
102058951Seric 
102158951Seric 	s = -1;
102258951Seric 	if (setjmp(CtxAuthTimeout) != 0)
102358951Seric 	{
102458951Seric 		if (s >= 0)
102558951Seric 			(void) close(s);
102658951Seric 		goto noident;
102758951Seric 	}
102858951Seric 
102958951Seric 	/* put a timeout around the whole thing */
103064255Seric 	ev = setevent(TimeOuts.to_ident, authtimeout, 0);
103158951Seric 
103264747Seric 	/* connect to foreign IDENT server using same address as SMTP socket */
103358951Seric 	s = socket(AF_INET, SOCK_STREAM, 0);
103458951Seric 	if (s < 0)
103558951Seric 	{
103658951Seric 		clrevent(ev);
103758951Seric 		goto noident;
103858951Seric 	}
103964747Seric 	if (bind(s, &la.sa, sizeof la.sin) < 0 ||
104066761Seric 	    connect(s, &RealHostAddr.sa, sizeof RealHostAddr.sin) < 0)
104158951Seric 	{
104266011Seric 		goto closeident;
104358951Seric 	}
104458951Seric 
104558957Seric 	if (tTd(9, 10))
104658951Seric 		printf("getauthinfo: sent %s", hbuf);
104758951Seric 
104858951Seric 	/* send query */
104958951Seric 	if (write(s, hbuf, strlen(hbuf)) < 0)
105058951Seric 		goto closeident;
105158951Seric 
105258951Seric 	/* get result */
105358951Seric 	i = read(s, hbuf, sizeof hbuf);
105458951Seric 	(void) close(s);
105558951Seric 	clrevent(ev);
105658951Seric 	if (i <= 0)
105758951Seric 		goto noident;
105858951Seric 	if (hbuf[--i] == '\n' && hbuf[--i] == '\r')
105958951Seric 		i--;
106058951Seric 	hbuf[++i] = '\0';
106158951Seric 
106258957Seric 	if (tTd(9, 3))
106358951Seric 		printf("getauthinfo:  got %s\n", hbuf);
106458951Seric 
106558951Seric 	/* parse result */
106658951Seric 	p = strchr(hbuf, ':');
106758951Seric 	if (p == NULL)
106858951Seric 	{
106958951Seric 		/* malformed response */
107058951Seric 		goto noident;
107158951Seric 	}
107258951Seric 	while (isascii(*++p) && isspace(*p))
107358951Seric 		continue;
107458951Seric 	if (strncasecmp(p, "userid", 6) != 0)
107558951Seric 	{
107658951Seric 		/* presumably an error string */
107758951Seric 		goto noident;
107858951Seric 	}
107958951Seric 	p += 6;
108058951Seric 	while (isascii(*p) && isspace(*p))
108158951Seric 		p++;
108258951Seric 	if (*p++ != ':')
108358951Seric 	{
108458951Seric 		/* either useridxx or malformed response */
108558951Seric 		goto noident;
108658951Seric 	}
108758951Seric 
108858951Seric 	/* p now points to the OSTYPE field */
108958951Seric 	p = strchr(p, ':');
109058951Seric 	if (p == NULL)
109158951Seric 	{
109258951Seric 		/* malformed response */
109358951Seric 		goto noident;
109458951Seric 	}
109558951Seric 
109658957Seric 	/* 1413 says don't do this -- but it's broken otherwise */
109758957Seric 	while (isascii(*++p) && isspace(*p))
109858957Seric 		continue;
109958957Seric 
110058951Seric 	/* p now points to the authenticated name */
110166003Seric 	(void) sprintf(hbuf, "%s@%s",
110266003Seric 		p, RealHostName == NULL ? "localhost" : RealHostName);
110358957Seric 	goto finish;
110458957Seric 
110566011Seric closeident:
110666011Seric 	(void) close(s);
110766011Seric 	clrevent(ev);
110866011Seric 
110958957Seric #endif /* IDENTPROTO */
111058957Seric 
111158957Seric noident:
111266003Seric 	if (RealHostName == NULL)
111366003Seric 	{
111466003Seric 		if (tTd(9, 1))
111566003Seric 			printf("getauthinfo: NULL\n");
111666003Seric 		return NULL;
111766003Seric 	}
111858957Seric 	(void) strcpy(hbuf, RealHostName);
111958957Seric 
112058957Seric finish:
112166003Seric 	if (RealHostName != NULL && RealHostName[0] != '[')
112258951Seric 	{
112358951Seric 		p = &hbuf[strlen(hbuf)];
112458951Seric 		(void) sprintf(p, " [%s]", anynet_ntoa(&RealHostAddr));
112558951Seric 	}
112658957Seric 	if (tTd(9, 1))
112758951Seric 		printf("getauthinfo: %s\n", hbuf);
112858308Seric 	return hbuf;
112958308Seric }
113058308Seric /*
113160089Seric **  HOST_MAP_LOOKUP -- turn a hostname into canonical form
113253751Seric **
113353751Seric **	Parameters:
113456823Seric **		map -- a pointer to this map (unused).
113560089Seric **		name -- the (presumably unqualified) hostname.
113660257Seric **		av -- unused -- for compatibility with other mapping
113755019Seric **			functions.
113859084Seric **		statp -- an exit status (out parameter) -- set to
113959084Seric **			EX_TEMPFAIL if the name server is unavailable.
114053751Seric **
114153751Seric **	Returns:
114253751Seric **		The mapping, if found.
114353751Seric **		NULL if no mapping found.
114453751Seric **
114553751Seric **	Side Effects:
114653751Seric **		Looks up the host specified in hbuf.  If it is not
114753751Seric **		the canonical name for that host, return the canonical
114853751Seric **		name.
114953751Seric */
115051315Seric 
115153751Seric char *
115260257Seric host_map_lookup(map, name, av, statp)
115356823Seric 	MAP *map;
115460089Seric 	char *name;
115560257Seric 	char **av;
115659084Seric 	int *statp;
115716911Seric {
115816911Seric 	register struct hostent *hp;
1159*67419Seric 	struct in_addr in_addr;
116056823Seric 	char *cp;
116158110Seric 	int i;
116259671Seric 	register STAB *s;
116360257Seric 	char hbuf[MAXNAME];
116459671Seric 	extern struct hostent *gethostbyaddr();
116566334Seric #if NAMED_BIND
116659671Seric 	extern int h_errno;
116766029Seric #endif
116816911Seric 
116925574Smiriam 	/*
117059671Seric 	**  See if we have already looked up this name.  If so, just
117159671Seric 	**  return it.
117259671Seric 	*/
117353751Seric 
117460089Seric 	s = stab(name, ST_NAMECANON, ST_ENTER);
117559671Seric 	if (bitset(NCF_VALID, s->s_namecanon.nc_flags))
117659671Seric 	{
117759986Seric 		if (tTd(9, 1))
117860089Seric 			printf("host_map_lookup(%s) => CACHE %s\n",
117960089Seric 				name, s->s_namecanon.nc_cname);
118059671Seric 		errno = s->s_namecanon.nc_errno;
118166334Seric #if NAMED_BIND
118259671Seric 		h_errno = s->s_namecanon.nc_herrno;
118366029Seric #endif
118459671Seric 		*statp = s->s_namecanon.nc_stat;
118564797Seric 		if (CurEnv->e_message == NULL && *statp == EX_TEMPFAIL)
118665199Seric 		{
118765199Seric 			sprintf(hbuf, "%s: Name server timeout",
118865199Seric 				shortenstring(name, 33));
118965199Seric 			CurEnv->e_message = newstr(hbuf);
119065199Seric 		}
119159671Seric 		return s->s_namecanon.nc_cname;
119259671Seric 	}
119359671Seric 
119459671Seric 	/*
119559671Seric 	**  If first character is a bracket, then it is an address
119659671Seric 	**  lookup.  Address is copied into a temporary buffer to
119760089Seric 	**  strip the brackets and to preserve name if address is
119859671Seric 	**  unknown.
119959671Seric 	*/
120059671Seric 
120160089Seric 	if (*name != '[')
120253751Seric 	{
120355019Seric 		extern bool getcanonname();
120455019Seric 
120558798Seric 		if (tTd(9, 1))
120660089Seric 			printf("host_map_lookup(%s) => ", name);
120759671Seric 		s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
120860089Seric 		(void) strcpy(hbuf, name);
120963842Seric 		if (getcanonname(hbuf, sizeof hbuf - 1, TRUE))
121058796Seric 		{
121158796Seric 			if (tTd(9, 1))
121258796Seric 				printf("%s\n", hbuf);
121360257Seric 			cp = map_rewrite(map, hbuf, strlen(hbuf), av);
121460257Seric 			s->s_namecanon.nc_cname = newstr(cp);
121560257Seric 			return cp;
121658796Seric 		}
121753751Seric 		else
121858796Seric 		{
121959084Seric 			register struct hostent *hp;
122059084Seric 
122166029Seric 			s->s_namecanon.nc_errno = errno;
122266334Seric #if NAMED_BIND
122366029Seric 			s->s_namecanon.nc_herrno = h_errno;
122458796Seric 			if (tTd(9, 1))
122559084Seric 				printf("FAIL (%d)\n", h_errno);
122659084Seric 			switch (h_errno)
122759084Seric 			{
122859084Seric 			  case TRY_AGAIN:
122959596Seric 				if (UseNameServer)
123059734Seric 				{
123165202Seric 					sprintf(hbuf, "%s: Name server timeout",
123265199Seric 						shortenstring(name, 33));
123365202Seric 					message("%s", hbuf);
123459734Seric 					if (CurEnv->e_message == NULL)
123565202Seric 						CurEnv->e_message = newstr(hbuf);
123659734Seric 				}
123759084Seric 				*statp = EX_TEMPFAIL;
123859084Seric 				break;
123959084Seric 
124059084Seric 			  case HOST_NOT_FOUND:
124159084Seric 				*statp = EX_NOHOST;
124259084Seric 				break;
124359084Seric 
124459084Seric 			  case NO_RECOVERY:
124559084Seric 				*statp = EX_SOFTWARE;
124659084Seric 				break;
124759084Seric 
124859084Seric 			  default:
124959084Seric 				*statp = EX_UNAVAILABLE;
125059084Seric 				break;
125159084Seric 			}
125266029Seric #else
125366029Seric 			if (tTd(9, 1))
125466029Seric 				printf("FAIL\n");
125566029Seric 			*statp = EX_NOHOST;
125666029Seric #endif
125759671Seric 			s->s_namecanon.nc_stat = *statp;
125859084Seric 			if (*statp != EX_TEMPFAIL || UseNameServer)
125959084Seric 				return NULL;
126059084Seric 
126159084Seric 			/*
126259084Seric 			**  Try to look it up in /etc/hosts
126359084Seric 			*/
126459084Seric 
126560089Seric 			hp = gethostbyname(name);
126659084Seric 			if (hp == NULL)
126759084Seric 			{
126859084Seric 				/* no dice there either */
126959671Seric 				s->s_namecanon.nc_stat = *statp = EX_NOHOST;
127059084Seric 				return NULL;
127159084Seric 			}
127259084Seric 
127359671Seric 			s->s_namecanon.nc_stat = *statp = EX_OK;
127460257Seric 			cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
127560257Seric 			s->s_namecanon.nc_cname = newstr(cp);
127660257Seric 			return cp;
127758796Seric 		}
127853751Seric 	}
127960089Seric 	if ((cp = strchr(name, ']')) == NULL)
128053751Seric 		return (NULL);
128140994Sbostic 	*cp = '\0';
1282*67419Seric 	in_addr.s_addr = inet_addr(&name[1]);
128358110Seric 
128458110Seric 	/* nope -- ask the name server */
1285*67419Seric 	hp = gethostbyaddr((char *)&in_addr, IPADDRSIZE, AF_INET);
128659671Seric 	s->s_namecanon.nc_errno = errno;
128766334Seric #if NAMED_BIND
128859671Seric 	s->s_namecanon.nc_herrno = h_errno;
128966029Seric #endif
129059671Seric 	s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
129133932Sbostic 	if (hp == NULL)
129259671Seric 	{
129359671Seric 		s->s_namecanon.nc_stat = *statp = EX_NOHOST;
129453751Seric 		return (NULL);
129559671Seric 	}
129653751Seric 
129758110Seric 	/* found a match -- copy out */
129860257Seric 	cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
129959671Seric 	s->s_namecanon.nc_stat = *statp = EX_OK;
130060257Seric 	s->s_namecanon.nc_cname = newstr(cp);
130160257Seric 	return cp;
130233932Sbostic }
130358755Seric /*
130458755Seric **  ANYNET_NTOA -- convert a network address to printable form.
130558755Seric **
130658755Seric **	Parameters:
130758755Seric **		sap -- a pointer to a sockaddr structure.
130858755Seric **
130958755Seric **	Returns:
131058755Seric **		A printable version of that sockaddr.
131158755Seric */
131216911Seric 
131358755Seric char *
131458755Seric anynet_ntoa(sap)
131558755Seric 	register SOCKADDR *sap;
131658755Seric {
131758755Seric 	register char *bp;
131858755Seric 	register char *ap;
131958755Seric 	int l;
132064734Seric 	static char buf[100];
132158755Seric 
132258798Seric 	/* check for null/zero family */
132358798Seric 	if (sap == NULL)
132458798Seric 		return "NULLADDR";
132558798Seric 	if (sap->sa.sa_family == 0)
132658798Seric 		return "0";
132758798Seric 
132864734Seric 	switch (sap->sa.sa_family)
132964734Seric 	{
133064734Seric #ifdef MAYBENEXTRELEASE		/*** UNTESTED *** UNTESTED *** UNTESTED ***/
133164821Seric #ifdef NETUNIX
133264734Seric 	  case AF_UNIX:
133364758Seric 	  	if (sap->sunix.sun_path[0] != '\0')
133464758Seric 	  		sprintf(buf, "[UNIX: %.64s]", sap->sunix.sun_path);
133564734Seric 	  	else
133664734Seric 	  		sprintf(buf, "[UNIX: localhost]");
133764734Seric 		return buf;
133864734Seric #endif
133964821Seric #endif
134064734Seric 
134158778Seric #ifdef NETINET
134264734Seric 	  case AF_INET:
134358755Seric 		return inet_ntoa(((struct sockaddr_in *) sap)->sin_addr);
134458778Seric #endif
134558755Seric 
134664734Seric 	  default:
134764734Seric 	  	/* this case is only to ensure syntactic correctness */
134864734Seric 	  	break;
134964734Seric 	}
135064734Seric 
135158755Seric 	/* unknown family -- just dump bytes */
135258778Seric 	(void) sprintf(buf, "Family %d: ", sap->sa.sa_family);
135358755Seric 	bp = &buf[strlen(buf)];
135458778Seric 	ap = sap->sa.sa_data;
135558778Seric 	for (l = sizeof sap->sa.sa_data; --l >= 0; )
135658755Seric 	{
135758755Seric 		(void) sprintf(bp, "%02x:", *ap++ & 0377);
135858755Seric 		bp += 3;
135958755Seric 	}
136058755Seric 	*--bp = '\0';
136158755Seric 	return buf;
136258755Seric }
136358951Seric /*
136458951Seric **  HOSTNAMEBYANYADDR -- return name of host based on address
136558951Seric **
136658951Seric **	Parameters:
136758951Seric **		sap -- SOCKADDR pointer
136858951Seric **
136958951Seric **	Returns:
137058951Seric **		text representation of host name.
137158951Seric **
137258951Seric **	Side Effects:
137358951Seric **		none.
137458951Seric */
137558755Seric 
137658951Seric char *
137758951Seric hostnamebyanyaddr(sap)
137858951Seric 	register SOCKADDR *sap;
137958951Seric {
138058951Seric 	register struct hostent *hp;
138164734Seric 	int saveretry;
138258951Seric 
138366334Seric #if NAMED_BIND
138459042Seric 	/* shorten name server timeout to avoid higher level timeouts */
138559042Seric 	saveretry = _res.retry;
138659042Seric 	_res.retry = 3;
138759042Seric #endif /* NAMED_BIND */
138859042Seric 
138958951Seric 	switch (sap->sa.sa_family)
139058951Seric 	{
139158951Seric #ifdef NETINET
139258951Seric 	  case AF_INET:
139358951Seric 		hp = gethostbyaddr((char *) &sap->sin.sin_addr,
1394*67419Seric 			IPADDRSIZE,
139558951Seric 			AF_INET);
139658951Seric 		break;
139758951Seric #endif
139858951Seric 
139958951Seric #ifdef NETISO
140058951Seric 	  case AF_ISO:
140158951Seric 		hp = gethostbyaddr((char *) &sap->siso.siso_addr,
140258951Seric 			sizeof sap->siso.siso_addr,
140358951Seric 			AF_ISO);
140458951Seric 		break;
140558951Seric #endif
140658951Seric 
140764734Seric #ifdef MAYBENEXTRELEASE		/*** UNTESTED *** UNTESTED *** UNTESTED ***/
140864734Seric 	  case AF_UNIX:
140964734Seric 		hp = NULL;
141064734Seric 		break;
141164734Seric #endif
141264734Seric 
141358951Seric 	  default:
141458951Seric 		hp = gethostbyaddr(sap->sa.sa_data,
141558951Seric 			   sizeof sap->sa.sa_data,
141658951Seric 			   sap->sa.sa_family);
141758951Seric 		break;
141858951Seric 	}
141958951Seric 
142066334Seric #if NAMED_BIND
142159042Seric 	_res.retry = saveretry;
142259042Seric #endif /* NAMED_BIND */
142359042Seric 
142458951Seric 	if (hp != NULL)
142558951Seric 		return hp->h_name;
142658951Seric 	else
142758951Seric 	{
142858951Seric 		/* produce a dotted quad */
142958951Seric 		static char buf[512];
143058951Seric 
143158951Seric 		(void) sprintf(buf, "[%s]", anynet_ntoa(sap));
143258951Seric 		return buf;
143358951Seric 	}
143458951Seric }
143558951Seric 
143656795Seric # else /* DAEMON */
143716911Seric /* code for systems without sophisticated networking */
143810758Seric 
143910758Seric /*
144010758Seric **  MYHOSTNAME -- stub version for case of no daemon code.
144111297Seric **
144211297Seric **	Can't convert to upper case here because might be a UUCP name.
144312313Seric **
144412313Seric **	Mark, you can change this to be anything you want......
144510758Seric */
144610758Seric 
144710758Seric char **
144812313Seric myhostname(hostbuf, size)
144910758Seric 	char hostbuf[];
145012313Seric 	int size;
145110758Seric {
145210758Seric 	register FILE *f;
145310758Seric 
145410758Seric 	hostbuf[0] = '\0';
145510758Seric 	f = fopen("/usr/include/whoami", "r");
145610758Seric 	if (f != NULL)
145710758Seric 	{
145812313Seric 		(void) fgets(hostbuf, size, f);
145910758Seric 		fixcrlf(hostbuf, TRUE);
146010758Seric 		(void) fclose(f);
146110758Seric 	}
146210758Seric 	return (NULL);
146310758Seric }
146416911Seric /*
146558951Seric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
146658308Seric **
146758308Seric **	Parameters:
146858308Seric **		fd -- the descriptor
146958308Seric **
147058308Seric **	Returns:
147158308Seric **		The host name associated with this descriptor, if it can
147258308Seric **			be determined.
147358308Seric **		NULL otherwise.
147458308Seric **
147558308Seric **	Side Effects:
147658308Seric **		none
147758308Seric */
147858308Seric 
147958308Seric char *
148058951Seric getauthinfo(fd)
148158308Seric 	int fd;
148258308Seric {
148358308Seric 	return NULL;
148458308Seric }
148558308Seric /*
148616911Seric **  MAPHOSTNAME -- turn a hostname into canonical form
148716911Seric **
148816911Seric **	Parameters:
148956823Seric **		map -- a pointer to the database map.
149060089Seric **		name -- a buffer containing a hostname.
149153751Seric **		avp -- a pointer to a (cf file defined) argument vector.
149259084Seric **		statp -- an exit status (out parameter).
149316911Seric **
149416911Seric **	Returns:
149553751Seric **		mapped host name
149651315Seric **		FALSE otherwise.
149716911Seric **
149816911Seric **	Side Effects:
149960089Seric **		Looks up the host specified in name.  If it is not
150016911Seric **		the canonical name for that host, replace it with
150116911Seric **		the canonical name.  If the name is unknown, or it
150216911Seric **		is already the canonical name, leave it unchanged.
150316911Seric */
150410758Seric 
150516911Seric /*ARGSUSED*/
150653751Seric char *
150760089Seric host_map_lookup(map, name, avp, statp)
150856823Seric 	MAP *map;
150960089Seric 	char *name;
151053751Seric 	char **avp;
151159084Seric 	char *statp;
151216911Seric {
151359084Seric 	register struct hostent *hp;
151459084Seric 
151560089Seric 	hp = gethostbyname(name);
151659084Seric 	if (hp != NULL)
151759084Seric 		return hp->h_name;
151859084Seric 	*statp = EX_NOHOST;
151953751Seric 	return NULL;
152016911Seric }
152116911Seric 
152256795Seric #endif /* DAEMON */
1523