xref: /csrg-svn/usr.sbin/sendmail/src/daemon.c (revision 69840)
122700Sdist /*
268839Seric  * Copyright (c) 1983, 1995 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*69840Seric static char sccsid[] = "@(#)daemon.c	8.99 (Berkeley) 06/10/95 (with daemon mode)";
1533780Sbostic #else
16*69840Seric static char sccsid[] = "@(#)daemon.c	8.99 (Berkeley) 06/10/95 (without daemon mode)";
1733780Sbostic #endif
1833780Sbostic #endif /* not lint */
194535Seric 
2033780Sbostic #ifdef DAEMON
2133780Sbostic 
2264338Seric # include <arpa/inet.h>
235978Seric 
2466334Seric #if NAMED_BIND
2559042Seric # include <resolv.h>
2659042Seric #endif
2759042Seric 
2869601Seric #if IP_SRCROUTE
2969601Seric # include <netinet/in_systm.h>
3069601Seric # include <netinet/ip.h>
3169601Seric # include <netinet/ip_var.h>
3269601Seric #endif
3369601Seric 
344535Seric /*
354535Seric **  DAEMON.C -- routines to use when running as a daemon.
367556Seric **
377556Seric **	This entire file is highly dependent on the 4.2 BSD
387556Seric **	interprocess communication primitives.  No attempt has
397556Seric **	been made to make this file portable to Version 7,
407556Seric **	Version 6, MPX files, etc.  If you should try such a
417556Seric **	thing yourself, I recommend chucking the entire file
427556Seric **	and starting from scratch.  Basic semantics are:
437556Seric **
447556Seric **	getrequests()
457556Seric **		Opens a port and initiates a connection.
467556Seric **		Returns in a child.  Must set InChannel and
477556Seric **		OutChannel appropriately.
4810206Seric **	clrdaemon()
4910206Seric **		Close any open files associated with getting
5010206Seric **		the connection; this is used when running the queue,
5110206Seric **		etc., to avoid having extra file descriptors during
5210206Seric **		the queue run and to avoid confusing the network
5310206Seric **		code (if it cares).
5452106Seric **	makeconnection(host, port, outfile, infile, usesecureport)
557556Seric **		Make a connection to the named host on the given
567556Seric **		port.  Set *outfile and *infile to the files
577556Seric **		appropriate for communication.  Returns zero on
587556Seric **		success, else an exit status describing the
597556Seric **		error.
6060089Seric **	host_map_lookup(map, hbuf, avp, pstat)
6156823Seric **		Convert the entry in hbuf into a canonical form.
624535Seric */
634535Seric /*
644535Seric **  GETREQUESTS -- open mail IPC port and get requests.
654535Seric **
664535Seric **	Parameters:
674535Seric **		none.
684535Seric **
694535Seric **	Returns:
704535Seric **		none.
714535Seric **
724535Seric **	Side Effects:
734535Seric **		Waits until some interesting activity occurs.  When
744535Seric **		it does, a child is created to process it, and the
754535Seric **		parent waits for completion.  Return from this
769886Seric **		routine is always in the child.  The file pointers
779886Seric **		"InChannel" and "OutChannel" should be set to point
789886Seric **		to the communication channel.
794535Seric */
804535Seric 
8158849Seric int		DaemonSocket	= -1;		/* fd describing socket */
8258849Seric SOCKADDR	DaemonAddr;			/* socket for incoming */
8359783Seric int		ListenQueueSize = 10;		/* size of listen queue */
8464381Seric int		TcpRcvBufferSize = 0;		/* size of TCP receive buffer */
8564381Seric int		TcpSndBufferSize = 0;		/* size of TCP send buffer */
8616144Seric 
8768693Seric void
884535Seric getrequests()
894535Seric {
909610Seric 	int t;
9153751Seric 	bool refusingconnections = TRUE;
9258419Seric 	FILE *pidf;
9364828Seric 	int socksize;
9466793Seric #ifdef XDEBUG
9566793Seric 	bool j_has_dot;
9666793Seric #endif
9746928Sbostic 	extern void reapchild();
987117Seric 
999610Seric 	/*
1009610Seric 	**  Set up the address for the mailer.
1019610Seric 	*/
1029610Seric 
10358849Seric 	if (DaemonAddr.sin.sin_family == 0)
10458849Seric 		DaemonAddr.sin.sin_family = AF_INET;
10558849Seric 	if (DaemonAddr.sin.sin_addr.s_addr == 0)
10658849Seric 		DaemonAddr.sin.sin_addr.s_addr = INADDR_ANY;
10758849Seric 	if (DaemonAddr.sin.sin_port == 0)
1089610Seric 	{
10965169Seric 		register struct servent *sp;
11065169Seric 
11158849Seric 		sp = getservbyname("smtp", "tcp");
11258849Seric 		if (sp == NULL)
11358849Seric 		{
11458909Seric 			syserr("554 service \"smtp\" unknown");
11565169Seric 			DaemonAddr.sin.sin_port = htons(25);
11658849Seric 		}
11765169Seric 		else
11865169Seric 			DaemonAddr.sin.sin_port = sp->s_port;
1199610Seric 	}
1209610Seric 
1219610Seric 	/*
1229610Seric 	**  Try to actually open the connection.
1239610Seric 	*/
1249610Seric 
1259610Seric 	if (tTd(15, 1))
12658849Seric 		printf("getrequests: port 0x%x\n", DaemonAddr.sin.sin_port);
1279610Seric 
1289610Seric 	/* get a socket for the SMTP connection */
12966854Seric 	socksize = opendaemonsocket(TRUE);
13010347Seric 
13164035Seric 	(void) setsignal(SIGCHLD, reapchild);
13224945Seric 
13358419Seric 	/* write the pid to the log file for posterity */
13458419Seric 	pidf = fopen(PidFile, "w");
13558419Seric 	if (pidf != NULL)
13658419Seric 	{
13763863Seric 		extern char *CommandLineArgs;
13863863Seric 
13963863Seric 		/* write the process id on line 1 */
14058419Seric 		fprintf(pidf, "%d\n", getpid());
14163863Seric 
14263863Seric 		/* line 2 contains all command line flags */
14363863Seric 		fprintf(pidf, "%s\n", CommandLineArgs);
14463863Seric 
14563863Seric 		/* flush and close */
14658419Seric 		fclose(pidf);
14758419Seric 	}
14858419Seric 
14966793Seric #ifdef XDEBUG
15066793Seric 	{
15166812Seric 		char jbuf[MAXHOSTNAMELEN];
15258419Seric 
15368693Seric 		expand("\201j", jbuf, sizeof jbuf, CurEnv);
15466812Seric 		j_has_dot = strchr(jbuf, '.') != NULL;
15566793Seric 	}
15666793Seric #endif
15766793Seric 
1589610Seric 	if (tTd(15, 1))
15910206Seric 		printf("getrequests: %d\n", DaemonSocket);
1609610Seric 
1614631Seric 	for (;;)
1624631Seric 	{
16314875Seric 		register int pid;
16411147Seric 		auto int lotherend;
16553751Seric 		extern bool refuseconnections();
16668693Seric 		extern int getla();
16711147Seric 
16814875Seric 		/* see if we are rejecting connections */
16953751Seric 		CurrentLA = getla();
17053751Seric 		if (refuseconnections())
17136584Sbostic 		{
17266845Seric 			if (DaemonSocket >= 0)
17353751Seric 			{
17466845Seric 				/* close socket so peer will fail quickly */
17566845Seric 				(void) close(DaemonSocket);
17666845Seric 				DaemonSocket = -1;
17753751Seric 			}
17866845Seric 			refusingconnections = TRUE;
17966845Seric 			sleep(15);
18053751Seric 			continue;
18136584Sbostic 		}
18214875Seric 
18368693Seric 		/* arrange to (re)open the socket if necessary */
18453751Seric 		if (refusingconnections)
18553751Seric 		{
18667690Seric 			(void) opendaemonsocket(FALSE);
18753751Seric 			refusingconnections = FALSE;
18853751Seric 		}
18953751Seric 
19066793Seric #ifdef XDEBUG
19166793Seric 		/* check for disaster */
19266793Seric 		{
19366812Seric 			char jbuf[MAXHOSTNAMELEN];
19466793Seric 
19568693Seric 			expand("\201j", jbuf, sizeof jbuf, CurEnv);
19668693Seric 			if (!wordinclass(jbuf, 'w'))
19766793Seric 			{
19866793Seric 				dumpstate("daemon lost $j");
19966793Seric 				syslog(LOG_ALERT, "daemon process doesn't have $j in $=w; see syslog");
20066793Seric 				abort();
20166793Seric 			}
20266812Seric 			else if (j_has_dot && strchr(jbuf, '.') == NULL)
20366793Seric 			{
20466793Seric 				dumpstate("daemon $j lost dot");
20566793Seric 				syslog(LOG_ALERT, "daemon process $j lost dot; see syslog");
20666793Seric 				abort();
20766793Seric 			}
20866793Seric 		}
20966793Seric #endif
21066793Seric 
2119610Seric 		/* wait for a connection */
21269781Seric 		setproctitle("accepting connections");
2139610Seric 		do
2149610Seric 		{
2159610Seric 			errno = 0;
21664828Seric 			lotherend = socksize;
21746928Sbostic 			t = accept(DaemonSocket,
21846928Sbostic 			    (struct sockaddr *)&RealHostAddr, &lotherend);
2199610Seric 		} while (t < 0 && errno == EINTR);
2209610Seric 		if (t < 0)
2215978Seric 		{
2229610Seric 			syserr("getrequests: accept");
22368693Seric 
22468693Seric 			/* arrange to re-open the socket next time around */
22568693Seric 			(void) close(DaemonSocket);
22668693Seric 			DaemonSocket = -1;
2279610Seric 			sleep(5);
2289610Seric 			continue;
2295978Seric 		}
2304631Seric 
2315978Seric 		/*
2325978Seric 		**  Create a subprocess to process the mail.
2335978Seric 		*/
2345978Seric 
2357677Seric 		if (tTd(15, 2))
2369610Seric 			printf("getrequests: forking (fd = %d)\n", t);
2375978Seric 
2384636Seric 		pid = fork();
2394636Seric 		if (pid < 0)
2404631Seric 		{
2414636Seric 			syserr("daemon: cannot fork");
2424636Seric 			sleep(10);
2439610Seric 			(void) close(t);
2444636Seric 			continue;
2454631Seric 		}
2464631Seric 
2474636Seric 		if (pid == 0)
2484631Seric 		{
24964086Seric 			char *p;
25058951Seric 			extern char *hostnamebyanyaddr();
25168693Seric 			extern void intsig();
25211147Seric 
2534636Seric 			/*
2544636Seric 			**  CHILD -- return to caller.
25511147Seric 			**	Collect verified idea of sending host.
2564636Seric 			**	Verify calling user id if possible here.
2574636Seric 			*/
2584631Seric 
25964035Seric 			(void) setsignal(SIGCHLD, SIG_DFL);
26068693Seric 			(void) setsignal(SIGHUP, intsig);
26168693Seric 			(void) close(DaemonSocket);
26266017Seric 			DisConnected = FALSE;
26324950Seric 
26466032Seric 			setproctitle("startup with %s",
26566032Seric 				anynet_ntoa(&RealHostAddr));
26666032Seric 
26711147Seric 			/* determine host name */
26864086Seric 			p = hostnamebyanyaddr(&RealHostAddr);
26969471Seric 			if (strlen(p) > MAXNAME)
27069471Seric 				p[MAXNAME] = '\0';
27164086Seric 			RealHostName = newstr(p);
27266032Seric 			setproctitle("startup with %s", p);
27358778Seric 
27464724Seric 			if ((InChannel = fdopen(t, "r")) == NULL ||
27564724Seric 			    (t = dup(t)) < 0 ||
27664724Seric 			    (OutChannel = fdopen(t, "w")) == NULL)
27764724Seric 			{
27864724Seric 				syserr("cannot open SMTP server channel, fd=%d", t);
27964724Seric 				exit(0);
28064724Seric 			}
28159254Seric 
28216884Seric 			/* should we check for illegal connection here? XXX */
28359156Seric #ifdef XLA
28459156Seric 			if (!xla_host_ok(RealHostName))
28559156Seric 			{
28659254Seric 				message("421 Too many SMTP sessions for this host");
28759156Seric 				exit(0);
28859156Seric 			}
28959156Seric #endif
29016884Seric 
2917677Seric 			if (tTd(15, 2))
2925978Seric 				printf("getreq: returning\n");
2934636Seric 			return;
2944631Seric 		}
2954631Seric 
29669839Seric 		CurChildren++;
29769839Seric 
2987117Seric 		/* close the port so that others will hang (for a while) */
2999610Seric 		(void) close(t);
3004631Seric 	}
3019886Seric 	/*NOTREACHED*/
3024631Seric }
3035978Seric /*
30466845Seric **  OPENDAEMONSOCKET -- open the SMTP socket
30566845Seric **
30666845Seric **	Deals with setting all appropriate options.  DaemonAddr must
30766845Seric **	be set up in advance.
30866845Seric **
30966845Seric **	Parameters:
31066854Seric **		firsttime -- set if this is the initial open.
31166845Seric **
31266845Seric **	Returns:
31366845Seric **		Size in bytes of the daemon socket addr.
31466845Seric **
31566845Seric **	Side Effects:
31666845Seric **		Leaves DaemonSocket set to the open socket.
31766845Seric **		Exits if the socket cannot be created.
31866845Seric */
31966845Seric 
32066861Seric #define MAXOPENTRIES	10	/* maximum number of tries to open connection */
32166861Seric 
32266845Seric int
32366854Seric opendaemonsocket(firsttime)
32466854Seric 	bool firsttime;
32566845Seric {
32666845Seric 	int on = 1;
32768693Seric 	int socksize = 0;
32866861Seric 	int ntries = 0;
32966861Seric 	int saveerrno;
33066845Seric 
33166845Seric 	if (tTd(15, 2))
33266845Seric 		printf("opendaemonsocket()\n");
33366845Seric 
33466861Seric 	do
33566845Seric 	{
33666862Seric 		if (ntries > 0)
33766862Seric 			sleep(5);
33866861Seric 		if (firsttime || DaemonSocket < 0)
33966854Seric 		{
34066861Seric 			DaemonSocket = socket(DaemonAddr.sa.sa_family, SOCK_STREAM, 0);
34166861Seric 			if (DaemonSocket < 0)
34266861Seric 			{
34366861Seric 				/* probably another daemon already */
34466861Seric 				saveerrno = errno;
34566861Seric 				syserr("opendaemonsocket: can't create server SMTP socket");
34666861Seric 			  severe:
34766845Seric # ifdef LOG
34866861Seric 				if (LogLevel > 0)
34966861Seric 					syslog(LOG_ALERT, "problem creating SMTP socket");
35066845Seric # endif /* LOG */
35166861Seric 				DaemonSocket = -1;
35266861Seric 				continue;
35366861Seric 			}
35466845Seric 
35566861Seric 			/* turn on network debugging? */
35666861Seric 			if (tTd(15, 101))
35766861Seric 				(void) setsockopt(DaemonSocket, SOL_SOCKET,
35866861Seric 						  SO_DEBUG, (char *)&on,
35966861Seric 						  sizeof on);
36066845Seric 
36166861Seric 			(void) setsockopt(DaemonSocket, SOL_SOCKET,
36266861Seric 					  SO_REUSEADDR, (char *)&on, sizeof on);
36366861Seric 			(void) setsockopt(DaemonSocket, SOL_SOCKET,
36466861Seric 					  SO_KEEPALIVE, (char *)&on, sizeof on);
36566845Seric 
36666845Seric #ifdef SO_RCVBUF
36766861Seric 			if (TcpRcvBufferSize > 0)
36866861Seric 			{
36966861Seric 				if (setsockopt(DaemonSocket, SOL_SOCKET,
37066861Seric 					       SO_RCVBUF,
37166861Seric 					       (char *) &TcpRcvBufferSize,
37266861Seric 					       sizeof(TcpRcvBufferSize)) < 0)
37366861Seric 					syserr("getrequests: setsockopt(SO_RCVBUF)");
37466861Seric 			}
37566845Seric #endif
37666845Seric 
37766861Seric 			switch (DaemonAddr.sa.sa_family)
37866861Seric 			{
37966845Seric # ifdef NETINET
38066861Seric 			  case AF_INET:
38166861Seric 				socksize = sizeof DaemonAddr.sin;
38266861Seric 				break;
38366845Seric # endif
38466845Seric 
38566845Seric # ifdef NETISO
38666861Seric 			  case AF_ISO:
38766861Seric 				socksize = sizeof DaemonAddr.siso;
38866861Seric 				break;
38966845Seric # endif
39066845Seric 
39166861Seric 			  default:
39266861Seric 				socksize = sizeof DaemonAddr;
39366861Seric 				break;
39466861Seric 			}
39566861Seric 
39666861Seric 			if (bind(DaemonSocket, &DaemonAddr.sa, socksize) < 0)
39766861Seric 			{
39866861Seric 				saveerrno = errno;
39966861Seric 				syserr("getrequests: cannot bind");
40066861Seric 				(void) close(DaemonSocket);
40166861Seric 				goto severe;
40266861Seric 			}
40366854Seric 		}
40466861Seric 		if (!firsttime && listen(DaemonSocket, ListenQueueSize) < 0)
40566854Seric 		{
40666861Seric 			saveerrno = errno;
40766861Seric 			syserr("getrequests: cannot listen");
40866854Seric 			(void) close(DaemonSocket);
40966854Seric 			goto severe;
41066854Seric 		}
41166861Seric 		return socksize;
41266861Seric 	} while (ntries++ < MAXOPENTRIES && transienterror(saveerrno));
41368693Seric 	syserr("!opendaemonsocket: server SMTP socket wedged: exiting");
41466861Seric 	finis();
41566845Seric }
41666845Seric /*
41710206Seric **  CLRDAEMON -- reset the daemon connection
41810206Seric **
41910206Seric **	Parameters:
42010206Seric **		none.
42110206Seric **
42210206Seric **	Returns:
42310206Seric **		none.
42410206Seric **
42510206Seric **	Side Effects:
42610206Seric **		releases any resources used by the passive daemon.
42710206Seric */
42810206Seric 
42968693Seric void
43010206Seric clrdaemon()
43110206Seric {
43210206Seric 	if (DaemonSocket >= 0)
43310206Seric 		(void) close(DaemonSocket);
43410206Seric 	DaemonSocket = -1;
43510206Seric }
43610206Seric /*
43758849Seric **  SETDAEMONOPTIONS -- set options for running the daemon
43858849Seric **
43958849Seric **	Parameters:
44058849Seric **		p -- the options line.
44158849Seric **
44258849Seric **	Returns:
44358849Seric **		none.
44458849Seric */
44558849Seric 
44668693Seric void
44758849Seric setdaemonoptions(p)
44858849Seric 	register char *p;
44958849Seric {
45058873Seric 	if (DaemonAddr.sa.sa_family == AF_UNSPEC)
45158873Seric 		DaemonAddr.sa.sa_family = AF_INET;
45258873Seric 
45358849Seric 	while (p != NULL)
45458849Seric 	{
45558849Seric 		register char *f;
45658849Seric 		register char *v;
45758849Seric 
45858849Seric 		while (isascii(*p) && isspace(*p))
45958849Seric 			p++;
46058849Seric 		if (*p == '\0')
46158849Seric 			break;
46258849Seric 		f = p;
46358849Seric 		p = strchr(p, ',');
46458849Seric 		if (p != NULL)
46558849Seric 			*p++ = '\0';
46658849Seric 		v = strchr(f, '=');
46758849Seric 		if (v == NULL)
46858849Seric 			continue;
46958849Seric 		while (isascii(*++v) && isspace(*v))
47058849Seric 			continue;
47169397Seric 		if (isascii(*f) && isupper(*f))
47269397Seric 			*f = tolower(*f);
47358849Seric 
47458849Seric 		switch (*f)
47558849Seric 		{
47658873Seric 		  case 'F':		/* address family */
47758849Seric 			if (isascii(*v) && isdigit(*v))
47858873Seric 				DaemonAddr.sa.sa_family = atoi(v);
47958873Seric #ifdef NETINET
48058873Seric 			else if (strcasecmp(v, "inet") == 0)
48158873Seric 				DaemonAddr.sa.sa_family = AF_INET;
48258873Seric #endif
48358873Seric #ifdef NETISO
48458873Seric 			else if (strcasecmp(v, "iso") == 0)
48558873Seric 				DaemonAddr.sa.sa_family = AF_ISO;
48658873Seric #endif
48758873Seric #ifdef NETNS
48858873Seric 			else if (strcasecmp(v, "ns") == 0)
48958873Seric 				DaemonAddr.sa.sa_family = AF_NS;
49058873Seric #endif
49158873Seric #ifdef NETX25
49258873Seric 			else if (strcasecmp(v, "x.25") == 0)
49358873Seric 				DaemonAddr.sa.sa_family = AF_CCITT;
49458873Seric #endif
49558849Seric 			else
49658873Seric 				syserr("554 Unknown address family %s in Family=option", v);
49758873Seric 			break;
49858873Seric 
49958873Seric 		  case 'A':		/* address */
50058873Seric 			switch (DaemonAddr.sa.sa_family)
50158849Seric 			{
50258873Seric #ifdef NETINET
50358873Seric 			  case AF_INET:
50458873Seric 				if (isascii(*v) && isdigit(*v))
50568693Seric 					DaemonAddr.sin.sin_addr.s_addr = htonl(inet_network(v));
50658873Seric 				else
50758873Seric 				{
50858873Seric 					register struct netent *np;
50958849Seric 
51058873Seric 					np = getnetbyname(v);
51158873Seric 					if (np == NULL)
51258873Seric 						syserr("554 network \"%s\" unknown", v);
51358873Seric 					else
51458873Seric 						DaemonAddr.sin.sin_addr.s_addr = np->n_net;
51558873Seric 				}
51658873Seric 				break;
51758873Seric #endif
51858873Seric 
51958873Seric 			  default:
52058873Seric 				syserr("554 Address= option unsupported for family %d",
52158873Seric 					DaemonAddr.sa.sa_family);
52258873Seric 				break;
52358849Seric 			}
52458849Seric 			break;
52558849Seric 
52658873Seric 		  case 'P':		/* port */
52758873Seric 			switch (DaemonAddr.sa.sa_family)
52858849Seric 			{
52958873Seric 				short port;
53058849Seric 
53158873Seric #ifdef NETINET
53258873Seric 			  case AF_INET:
53358873Seric 				if (isascii(*v) && isdigit(*v))
53464366Seric 					DaemonAddr.sin.sin_port = htons(atoi(v));
53558849Seric 				else
53658873Seric 				{
53758873Seric 					register struct servent *sp;
53858873Seric 
53958873Seric 					sp = getservbyname(v, "tcp");
54058873Seric 					if (sp == NULL)
54158909Seric 						syserr("554 service \"%s\" unknown", v);
54258873Seric 					else
54358873Seric 						DaemonAddr.sin.sin_port = sp->s_port;
54458873Seric 				}
54558873Seric 				break;
54658873Seric #endif
54758873Seric 
54858873Seric #ifdef NETISO
54958873Seric 			  case AF_ISO:
55058873Seric 				/* assume two byte transport selector */
55158873Seric 				if (isascii(*v) && isdigit(*v))
55264366Seric 					port = htons(atoi(v));
55358873Seric 				else
55458873Seric 				{
55558873Seric 					register struct servent *sp;
55658873Seric 
55758873Seric 					sp = getservbyname(v, "tcp");
55858873Seric 					if (sp == NULL)
55958909Seric 						syserr("554 service \"%s\" unknown", v);
56058873Seric 					else
56158873Seric 						port = sp->s_port;
56258873Seric 				}
56358873Seric 				bcopy((char *) &port, TSEL(&DaemonAddr.siso), 2);
56458873Seric 				break;
56558873Seric #endif
56658873Seric 
56758873Seric 			  default:
56858873Seric 				syserr("554 Port= option unsupported for family %d",
56958873Seric 					DaemonAddr.sa.sa_family);
57058873Seric 				break;
57158849Seric 			}
57258849Seric 			break;
57359783Seric 
57459783Seric 		  case 'L':		/* listen queue size */
57559783Seric 			ListenQueueSize = atoi(v);
57659783Seric 			break;
57764381Seric 
57864381Seric 		  case 'S':		/* send buffer size */
57964381Seric 			TcpSndBufferSize = atoi(v);
58064381Seric 			break;
58164381Seric 
58264381Seric 		  case 'R':		/* receive buffer size */
58364381Seric 			TcpRcvBufferSize = atoi(v);
58464381Seric 			break;
58569397Seric 
58669397Seric 		  default:
58769397Seric 			syserr("554 DaemonPortOptions parameter \"%s\" unknown", f);
58858849Seric 		}
58958849Seric 	}
59058849Seric }
59158849Seric /*
5926039Seric **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
5936039Seric **
5946039Seric **	Parameters:
5956039Seric **		host -- the name of the host.
5966633Seric **		port -- the port number to connect to.
59753739Seric **		mci -- a pointer to the mail connection information
59853739Seric **			structure to be filled in.
59952106Seric **		usesecureport -- if set, use a low numbered (reserved)
60052106Seric **			port to provide some rudimentary authentication.
6016039Seric **
6026039Seric **	Returns:
6036039Seric **		An exit code telling whether the connection could be
6046039Seric **			made and if not why not.
6056039Seric **
6066039Seric **	Side Effects:
6076039Seric **		none.
6086039Seric */
6095978Seric 
61058755Seric SOCKADDR	CurHostAddr;		/* address of current host */
61158305Seric 
61254967Seric int
61353739Seric makeconnection(host, port, mci, usesecureport)
6146039Seric 	char *host;
6157286Seric 	u_short port;
61654967Seric 	register MCI *mci;
61752106Seric 	bool usesecureport;
6186039Seric {
61968693Seric 	register int i = 0;
62068693Seric 	register int s;
62129430Sbloom 	register struct hostent *hp = (struct hostent *)NULL;
62258755Seric 	SOCKADDR addr;
62352106Seric 	int sav_errno;
62458755Seric 	int addrlen;
62568693Seric 	bool firstconnect;
62666334Seric #if NAMED_BIND
62735651Seric 	extern int h_errno;
62835651Seric #endif
6296039Seric 
6306039Seric 	/*
6316039Seric 	**  Set up the address for the mailer.
6329308Seric 	**	Accept "[a.b.c.d]" syntax for host name.
6336039Seric 	*/
6346039Seric 
63566334Seric #if NAMED_BIND
63625475Smiriam 	h_errno = 0;
63735651Seric #endif
63825475Smiriam 	errno = 0;
63958864Seric 	bzero(&CurHostAddr, sizeof CurHostAddr);
64064334Seric 	SmtpPhase = mci->mci_phase = "initial connection";
64158906Seric 	CurHostName = host;
64225475Smiriam 
6439308Seric 	if (host[0] == '[')
6449308Seric 	{
64511147Seric 		long hid;
64656795Seric 		register char *p = strchr(host, ']');
6479308Seric 
64811147Seric 		if (p != NULL)
6499308Seric 		{
65011147Seric 			*p = '\0';
65159884Seric #ifdef NETINET
65211147Seric 			hid = inet_addr(&host[1]);
65358360Seric 			if (hid == -1)
65459884Seric #endif
65558360Seric 			{
65658360Seric 				/* try it as a host name (avoid MX lookup) */
65768693Seric 				hp = sm_gethostbyname(&host[1]);
65866349Seric 				if (hp == NULL && p[-1] == '.')
65966349Seric 				{
66068693Seric #if NAMED_BIND
66168693Seric 					int oldopts = _res.options;
66268693Seric 
66368693Seric 					_res.options &= ~(RES_DEFNAMES|RES_DNSRCH);
66468693Seric #endif
66566349Seric 					p[-1] = '\0';
66668693Seric 					hp = sm_gethostbyname(&host[1]);
66766349Seric 					p[-1] = '.';
66868693Seric #if NAMED_BIND
66968693Seric 					_res.options = oldopts;
67068693Seric #endif
67166349Seric 				}
67258360Seric 				*p = ']';
67358360Seric 				goto gothostent;
67458360Seric 			}
67511147Seric 			*p = ']';
6769308Seric 		}
67758360Seric 		if (p == NULL)
6789308Seric 		{
67958151Seric 			usrerr("553 Invalid numeric domain spec \"%s\"", host);
68068857Seric 			mci->mci_status = "5.1.2";
6819308Seric 			return (EX_NOHOST);
6829308Seric 		}
68359884Seric #ifdef NETINET
68459884Seric 		addr.sin.sin_family = AF_INET;		/*XXX*/
68558778Seric 		addr.sin.sin_addr.s_addr = hid;
68659884Seric #endif
6879308Seric 	}
6889610Seric 	else
6899610Seric 	{
69066349Seric 		register char *p = &host[strlen(host) - 1];
69166349Seric 
69268693Seric 		hp = sm_gethostbyname(host);
69366349Seric 		if (hp == NULL && *p == '.')
69466349Seric 		{
69568693Seric #if NAMED_BIND
69668693Seric 			int oldopts = _res.options;
69768693Seric 
69868693Seric 			_res.options &= ~(RES_DEFNAMES|RES_DNSRCH);
69968693Seric #endif
70066349Seric 			*p = '\0';
70168693Seric 			hp = sm_gethostbyname(host);
70266349Seric 			*p = '.';
70368693Seric #if NAMED_BIND
70468693Seric 			_res.options = oldopts;
70568693Seric #endif
70666349Seric 		}
70758360Seric gothostent:
70825475Smiriam 		if (hp == NULL)
70924945Seric 		{
71066334Seric #if NAMED_BIND
71168693Seric 			/* check for name server timeouts */
71268693Seric 			if (errno == ETIMEDOUT || h_errno == TRY_AGAIN ||
71368693Seric 			    (errno == ECONNREFUSED && UseNameServer))
71468693Seric 			{
71568693Seric 				mci->mci_status = "4.4.3";
71625475Smiriam 				return (EX_TEMPFAIL);
71768693Seric 			}
71835651Seric #endif
71925475Smiriam 			return (EX_NOHOST);
72024945Seric 		}
72158778Seric 		addr.sa.sa_family = hp->h_addrtype;
72258778Seric 		switch (hp->h_addrtype)
72358778Seric 		{
72458778Seric #ifdef NETINET
72558778Seric 		  case AF_INET:
72658755Seric 			bcopy(hp->h_addr,
72758778Seric 				&addr.sin.sin_addr,
72868693Seric 				INADDRSZ);
72958778Seric 			break;
73058778Seric #endif
73158778Seric 
73258778Seric 		  default:
73358755Seric 			bcopy(hp->h_addr,
73458778Seric 				addr.sa.sa_data,
73558755Seric 				hp->h_length);
73658778Seric 			break;
73758778Seric 		}
73829430Sbloom 		i = 1;
7399610Seric 	}
7409610Seric 
7419610Seric 	/*
7429610Seric 	**  Determine the port number.
7439610Seric 	*/
7449610Seric 
745*69840Seric 	if (port == 0)
7469610Seric 	{
7479610Seric 		register struct servent *sp = getservbyname("smtp", "tcp");
7489610Seric 
7499610Seric 		if (sp == NULL)
7509610Seric 		{
75168745Seric #ifdef LOG
75268745Seric 			if (LogLevel > 2)
75368745Seric 				syslog(LOG_ERR, "makeconnection: service \"smtp\" unknown");
75468745Seric #endif
75565169Seric 			port = htons(25);
7569610Seric 		}
75765169Seric 		else
75865169Seric 			port = sp->s_port;
7599610Seric 	}
7606039Seric 
76158778Seric 	switch (addr.sa.sa_family)
76258755Seric 	{
76359884Seric #ifdef NETINET
76458755Seric 	  case AF_INET:
76558778Seric 		addr.sin.sin_port = port;
76658755Seric 		addrlen = sizeof (struct sockaddr_in);
76758755Seric 		break;
76859884Seric #endif
76958755Seric 
77058755Seric #ifdef NETISO
77158755Seric 	  case AF_ISO:
77258755Seric 		/* assume two byte transport selector */
77358755Seric 		bcopy((char *) &port, TSEL((struct sockaddr_iso *) &addr), 2);
77458755Seric 		addrlen = sizeof (struct sockaddr_iso);
77558755Seric 		break;
77658755Seric #endif
77758755Seric 
77858755Seric 	  default:
77958778Seric 		syserr("Can't connect to address family %d", addr.sa.sa_family);
78058755Seric 		return (EX_NOHOST);
78158755Seric 	}
78258755Seric 
7836039Seric 	/*
7846039Seric 	**  Try to actually open the connection.
7856039Seric 	*/
7866039Seric 
78759156Seric #ifdef XLA
78859156Seric 	/* if too many connections, don't bother trying */
78959156Seric 	if (!xla_noqueue_ok(host))
79059156Seric 		return EX_TEMPFAIL;
79159156Seric #endif
79259156Seric 
79368693Seric 	firstconnect = TRUE;
79457736Seric 	for (;;)
79552106Seric 	{
79657736Seric 		if (tTd(16, 1))
79758755Seric 			printf("makeconnection (%s [%s])\n",
79858755Seric 				host, anynet_ntoa(&addr));
79952106Seric 
80058588Seric 		/* save for logging */
80158588Seric 		CurHostAddr = addr;
80258588Seric 
80357736Seric 		if (usesecureport)
80457736Seric 		{
80557736Seric 			int rport = IPPORT_RESERVED - 1;
8066039Seric 
80757736Seric 			s = rresvport(&rport);
80857736Seric 		}
80957736Seric 		else
81057736Seric 		{
81157736Seric 			s = socket(AF_INET, SOCK_STREAM, 0);
81257736Seric 		}
81357736Seric 		if (s < 0)
81457736Seric 		{
81557736Seric 			sav_errno = errno;
81657736Seric 			syserr("makeconnection: no socket");
81757736Seric 			goto failure;
81857736Seric 		}
81910347Seric 
82064381Seric #ifdef SO_SNDBUF
82164381Seric 		if (TcpSndBufferSize > 0)
82264381Seric 		{
82364381Seric 			if (setsockopt(s, SOL_SOCKET, SO_SNDBUF,
82464561Seric 				       (char *) &TcpSndBufferSize,
82564381Seric 				       sizeof(TcpSndBufferSize)) < 0)
82664381Seric 				syserr("makeconnection: setsockopt(SO_SNDBUF)");
82764381Seric 		}
82864381Seric #endif
82964381Seric 
83057736Seric 		if (tTd(16, 1))
83157736Seric 			printf("makeconnection: fd=%d\n", s);
83257736Seric 
83357736Seric 		/* turn on network debugging? */
83457736Seric 		if (tTd(16, 101))
83557736Seric 		{
83657736Seric 			int on = 1;
83766861Seric 			(void) setsockopt(s, SOL_SOCKET, SO_DEBUG,
83857736Seric 					  (char *)&on, sizeof on);
83957736Seric 		}
84057736Seric 		if (CurEnv->e_xfp != NULL)
84157736Seric 			(void) fflush(CurEnv->e_xfp);		/* for debugging */
84257736Seric 		errno = 0;					/* for debugging */
84358755Seric 		if (connect(s, (struct sockaddr *) &addr, addrlen) >= 0)
84457736Seric 			break;
84557736Seric 
84668693Seric 		/* if running demand-dialed connection, try again */
84768693Seric 		if (DialDelay > 0 && firstconnect)
84868693Seric 		{
84968693Seric 			if (tTd(16, 1))
85068693Seric 				printf("Connect failed (%s); trying again...\n",
85168693Seric 					errstring(sav_errno));
85268693Seric 			firstconnect = FALSE;
85368693Seric 			sleep(DialDelay);
85468693Seric 			continue;
85568693Seric 		}
85668693Seric 
85757736Seric 		/* couldn't connect.... figure out why */
85827744Sbloom 		sav_errno = errno;
85927744Sbloom 		(void) close(s);
86068693Seric 		if (hp != NULL && hp->h_addr_list[i])
86129430Sbloom 		{
86257736Seric 			if (tTd(16, 1))
86358755Seric 				printf("Connect failed (%s); trying new address....\n",
86458755Seric 					errstring(sav_errno));
86558778Seric 			switch (addr.sa.sa_family)
86658778Seric 			{
86758778Seric #ifdef NETINET
86858778Seric 			  case AF_INET:
86958755Seric 				bcopy(hp->h_addr_list[i++],
87058778Seric 				      &addr.sin.sin_addr,
87168693Seric 				      INADDRSZ);
87258778Seric 				break;
87358778Seric #endif
87458778Seric 
87558778Seric 			  default:
87658755Seric 				bcopy(hp->h_addr_list[i++],
87758778Seric 					addr.sa.sa_data,
87852106Seric 					hp->h_length);
87958778Seric 				break;
88058778Seric 			}
88157736Seric 			continue;
88229430Sbloom 		}
88329430Sbloom 
8846039Seric 		/* failure, decide if temporary or not */
8856039Seric 	failure:
88659254Seric #ifdef XLA
88759254Seric 		xla_host_end(host);
88859254Seric #endif
88958542Seric 		if (transienterror(sav_errno))
89058542Seric 			return EX_TEMPFAIL;
89158542Seric 		else
89258542Seric 		{
89358542Seric 			message("%s", errstring(sav_errno));
89458542Seric 			return (EX_UNAVAILABLE);
8956039Seric 		}
8966039Seric 	}
8976039Seric 
8986039Seric 	/* connection ok, put it into canonical form */
89964724Seric 	if ((mci->mci_out = fdopen(s, "w")) == NULL ||
90064724Seric 	    (s = dup(s)) < 0 ||
90164725Seric 	    (mci->mci_in = fdopen(s, "r")) == NULL)
90264724Seric 	{
90364724Seric 		syserr("cannot open SMTP client channel, fd=%d", s);
90464724Seric 		return EX_TEMPFAIL;
90564724Seric 	}
9066039Seric 
90710098Seric 	return (EX_OK);
9086039Seric }
90910758Seric /*
91010758Seric **  MYHOSTNAME -- return the name of this host.
91110758Seric **
91210758Seric **	Parameters:
91310758Seric **		hostbuf -- a place to return the name of this host.
91412313Seric **		size -- the size of hostbuf.
91510758Seric **
91610758Seric **	Returns:
91710758Seric **		A list of aliases for this host.
91810758Seric **
91910758Seric **	Side Effects:
92064338Seric **		Adds numeric codes to $=w.
92110758Seric */
9226039Seric 
92368693Seric struct hostent *
92412313Seric myhostname(hostbuf, size)
92510758Seric 	char hostbuf[];
92612313Seric 	int size;
92710758Seric {
92858110Seric 	register struct hostent *hp;
92968693Seric 	extern bool getcanonname();
93068693Seric 	extern int h_errno;
93110758Seric 
93223120Seric 	if (gethostname(hostbuf, size) < 0)
93323120Seric 	{
93423120Seric 		(void) strcpy(hostbuf, "localhost");
93523120Seric 	}
93668693Seric 	hp = sm_gethostbyname(hostbuf);
93766853Seric 	if (hp == NULL)
93868693Seric 		return NULL;
93968693Seric 	if (strchr(hp->h_name, '.') != NULL || strchr(hostbuf, '.') == NULL)
94067448Seric 	{
94168693Seric 		(void) strncpy(hostbuf, hp->h_name, size - 1);
94268693Seric 		hostbuf[size - 1] = '\0';
94367448Seric 	}
94466853Seric 
94566853Seric #if NAMED_BIND
94668693Seric 	/*
94768693Seric 	**  If still no dot, try DNS directly (i.e., avoid NIS problems).
94868693Seric 	**  This ought to be driven from the configuration file, but
94968693Seric 	**  we are called before the configuration is read.  We could
95068693Seric 	**  check for an /etc/resolv.conf file, but that isn't required.
95168693Seric 	**  All in all, a bit of a mess.
95268693Seric 	*/
95368693Seric 
95469818Seric 	if (strchr(hostbuf, '.') == NULL)
95568612Seric 	{
95669818Seric 		int nmaps;
95769818Seric 		int i;
95869818Seric 		char *maptype[MAXMAPSTACK];
95969818Seric 		short mapreturn[MAXMAPACTIONS];
96069818Seric 
96169818Seric 		nmaps = switch_map_find("hosts", maptype, mapreturn);
96269818Seric 		for (i = 0; i < nmaps; i++)
96369818Seric 			if (strcmp(maptype[i], "dns") == 0)
96469818Seric 				break;
96569818Seric 		if (i < nmaps &&
96669818Seric 		    !dns_getcanonname(hostbuf, size, TRUE) &&
96769818Seric 		    h_errno == TRY_AGAIN)
96869818Seric 		{
96969818Seric 			/* try twice in case name server not yet started up */
97069818Seric 			message("My unqualifed host name (%s) unknown to DNS; sleeping for retry",
97169818Seric 				hostbuf);
97269818Seric 			sleep(60);
97369818Seric 			if (!dns_getcanonname(hostbuf, size, TRUE))
97469818Seric 				errno = h_errno + E_DNSBASE;
97569818Seric 		}
97666853Seric 	}
97766777Seric #endif
97868693Seric 	return (hp);
97910758Seric }
98051315Seric /*
98158951Seric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
98258308Seric **
98358951Seric **	Uses RFC1413 protocol to try to get info from the other end.
98458951Seric **
98558308Seric **	Parameters:
98658308Seric **		fd -- the descriptor
98758308Seric **
98858308Seric **	Returns:
98958951Seric **		The user@host information associated with this descriptor.
99058308Seric */
99158308Seric 
99258951Seric static jmp_buf	CtxAuthTimeout;
99358951Seric 
99468693Seric static void
99558951Seric authtimeout()
99658951Seric {
99758951Seric 	longjmp(CtxAuthTimeout, 1);
99858951Seric }
99958951Seric 
100058308Seric char *
100158951Seric getauthinfo(fd)
100258308Seric 	int fd;
100358308Seric {
100458951Seric 	int falen;
100559104Seric 	register char *p;
100658951Seric 	SOCKADDR la;
100758951Seric 	int lalen;
100858951Seric 	register struct servent *sp;
100958951Seric 	int s;
101058951Seric 	int i;
101158951Seric 	EVENT *ev;
101268444Seric 	int nleft;
101368462Seric 	char ibuf[MAXNAME + 1];
101458951Seric 	static char hbuf[MAXNAME * 2 + 2];
101558951Seric 	extern char *hostnamebyanyaddr();
101658308Seric 
101766761Seric 	falen = sizeof RealHostAddr;
101868693Seric 	if (isatty(fd) || getpeername(fd, &RealHostAddr.sa, &falen) < 0 ||
101968693Seric 	    falen <= 0 || RealHostAddr.sa.sa_family == 0)
102058951Seric 	{
102158951Seric 		(void) sprintf(hbuf, "%s@localhost", RealUserName);
102258957Seric 		if (tTd(9, 1))
102358951Seric 			printf("getauthinfo: %s\n", hbuf);
102458951Seric 		return hbuf;
102558951Seric 	}
102658951Seric 
102766761Seric 	if (RealHostName == NULL)
102866761Seric 	{
102966761Seric 		/* translate that to a host name */
103066761Seric 		RealHostName = newstr(hostnamebyanyaddr(&RealHostAddr));
103166761Seric 	}
103266761Seric 
103365831Seric 	if (TimeOuts.to_ident == 0)
103465831Seric 		goto noident;
103565831Seric 
103658951Seric 	lalen = sizeof la;
103766761Seric 	if (RealHostAddr.sa.sa_family != AF_INET ||
103858951Seric 	    getsockname(fd, &la.sa, &lalen) < 0 || lalen <= 0 ||
103958951Seric 	    la.sa.sa_family != AF_INET)
104058951Seric 	{
104158951Seric 		/* no ident info */
104258951Seric 		goto noident;
104358951Seric 	}
104458951Seric 
104558951Seric 	/* create ident query */
104668457Seric 	(void) sprintf(ibuf, "%d,%d\r\n",
104766761Seric 		ntohs(RealHostAddr.sin.sin_port), ntohs(la.sin.sin_port));
104858951Seric 
104958951Seric 	/* create local address */
105064747Seric 	la.sin.sin_port = 0;
105158951Seric 
105258951Seric 	/* create foreign address */
105358951Seric 	sp = getservbyname("auth", "tcp");
105458951Seric 	if (sp != NULL)
105566761Seric 		RealHostAddr.sin.sin_port = sp->s_port;
105658308Seric 	else
105766761Seric 		RealHostAddr.sin.sin_port = htons(113);
105858951Seric 
105958951Seric 	s = -1;
106058951Seric 	if (setjmp(CtxAuthTimeout) != 0)
106158951Seric 	{
106258951Seric 		if (s >= 0)
106358951Seric 			(void) close(s);
106458951Seric 		goto noident;
106558951Seric 	}
106658951Seric 
106758951Seric 	/* put a timeout around the whole thing */
106864255Seric 	ev = setevent(TimeOuts.to_ident, authtimeout, 0);
106958951Seric 
107064747Seric 	/* connect to foreign IDENT server using same address as SMTP socket */
107158951Seric 	s = socket(AF_INET, SOCK_STREAM, 0);
107258951Seric 	if (s < 0)
107358951Seric 	{
107458951Seric 		clrevent(ev);
107558951Seric 		goto noident;
107658951Seric 	}
107764747Seric 	if (bind(s, &la.sa, sizeof la.sin) < 0 ||
107866761Seric 	    connect(s, &RealHostAddr.sa, sizeof RealHostAddr.sin) < 0)
107958951Seric 	{
108066011Seric 		goto closeident;
108158951Seric 	}
108258951Seric 
108358957Seric 	if (tTd(9, 10))
108468457Seric 		printf("getauthinfo: sent %s", ibuf);
108558951Seric 
108658951Seric 	/* send query */
108768457Seric 	if (write(s, ibuf, strlen(ibuf)) < 0)
108858951Seric 		goto closeident;
108958951Seric 
109058951Seric 	/* get result */
109168457Seric 	p = &ibuf[0];
109268525Seric 	nleft = sizeof ibuf - 1;
109368444Seric 	while ((i = read(s, p, nleft)) > 0)
109468444Seric 	{
109568444Seric 		p += i;
109668444Seric 		nleft -= i;
109768444Seric 	}
109858951Seric 	(void) close(s);
109958951Seric 	clrevent(ev);
110068457Seric 	if (i < 0 || p == &ibuf[0])
110158951Seric 		goto noident;
110258951Seric 
110368444Seric 	if (*--p == '\n' && *--p == '\r')
110468444Seric 		p--;
110568444Seric 	*++p = '\0';
110668444Seric 
110758957Seric 	if (tTd(9, 3))
110868457Seric 		printf("getauthinfo:  got %s\n", ibuf);
110958951Seric 
111058951Seric 	/* parse result */
111168457Seric 	p = strchr(ibuf, ':');
111258951Seric 	if (p == NULL)
111358951Seric 	{
111458951Seric 		/* malformed response */
111558951Seric 		goto noident;
111658951Seric 	}
111758951Seric 	while (isascii(*++p) && isspace(*p))
111858951Seric 		continue;
111958951Seric 	if (strncasecmp(p, "userid", 6) != 0)
112058951Seric 	{
112158951Seric 		/* presumably an error string */
112258951Seric 		goto noident;
112358951Seric 	}
112458951Seric 	p += 6;
112558951Seric 	while (isascii(*p) && isspace(*p))
112658951Seric 		p++;
112758951Seric 	if (*p++ != ':')
112858951Seric 	{
112958951Seric 		/* either useridxx or malformed response */
113058951Seric 		goto noident;
113158951Seric 	}
113258951Seric 
113358951Seric 	/* p now points to the OSTYPE field */
113468693Seric 	while (isascii(*p) && isspace(*p))
113568693Seric 		p++;
113668693Seric 	if (strncasecmp(p, "other", 5) == 0 &&
113768693Seric 	    (p[5] == ':' || p[5] == ' ' || p[5] == ',' || p[5] == '\0'))
113868693Seric 	{
113968693Seric 		/* not useful information */
114068693Seric 		goto noident;
114168693Seric 	}
114258951Seric 	p = strchr(p, ':');
114358951Seric 	if (p == NULL)
114458951Seric 	{
114558951Seric 		/* malformed response */
114658951Seric 		goto noident;
114758951Seric 	}
114858951Seric 
114958957Seric 	/* 1413 says don't do this -- but it's broken otherwise */
115058957Seric 	while (isascii(*++p) && isspace(*p))
115158957Seric 		continue;
115258957Seric 
115367935Seric 	/* p now points to the authenticated name -- copy carefully */
115468457Seric 	cleanstrcpy(hbuf, p, MAXNAME);
115568875Seric 	i = strlen(hbuf);
115667935Seric 	hbuf[i++] = '@';
115767935Seric 	strcpy(&hbuf[i], RealHostName == NULL ? "localhost" : RealHostName);
115869601Seric 	goto postident;
115958957Seric 
116066011Seric closeident:
116166011Seric 	(void) close(s);
116266011Seric 	clrevent(ev);
116366011Seric 
116458957Seric noident:
116566003Seric 	if (RealHostName == NULL)
116666003Seric 	{
116766003Seric 		if (tTd(9, 1))
116866003Seric 			printf("getauthinfo: NULL\n");
116966003Seric 		return NULL;
117066003Seric 	}
117158957Seric 	(void) strcpy(hbuf, RealHostName);
117258957Seric 
117369601Seric postident:
117469601Seric #if IP_SRCROUTE
117569601Seric 	/*
117669601Seric 	**  Extract IP source routing information.
117769601Seric 	**
117869601Seric 	**	Format of output for a connection from site a through b
117969601Seric 	**	through c to d:
118069601Seric 	**		loose:      @site-c@site-b:site-a
118169601Seric 	**		strict:	   !@site-c@site-b:site-a
118269601Seric 	**
118369601Seric 	**	o - pointer within ipopt_list structure.
118469601Seric 	**	q - pointer within ls/ss rr route data
118569601Seric 	**	p - pointer to hbuf
118669601Seric 	*/
118769601Seric 
118869601Seric 	if (RealHostAddr.sa.sa_family == AF_INET)
118969601Seric 	{
119069601Seric 		int ipoptlen, j;
119169637Seric 		u_char *q;
119269601Seric 		u_char *o;
119369601Seric 		struct in_addr addr;
119469601Seric 		struct ipoption ipopt;
119569601Seric 
119669601Seric 		ipoptlen = sizeof ipopt;
119769601Seric 		if (getsockopt(fd, IPPROTO_IP, IP_OPTIONS,
119869601Seric 			       (char *) &ipopt, &ipoptlen) < 0)
119969601Seric 			goto noipsr;
120069601Seric 		if (ipoptlen == 0)
120169601Seric 			goto noipsr;
120269637Seric 		o = (u_char *) ipopt.ipopt_list;
120369731Seric 		while (o != NULL && o < (u_char *) &ipopt + ipoptlen)
120469601Seric 		{
120569601Seric 			switch (*o)
120669601Seric 			{
120769601Seric 			  case IPOPT_EOL:
120869601Seric 				o = NULL;
120969601Seric 				break;
121069601Seric 
121169601Seric 			  case IPOPT_NOP:
121269601Seric 				o++;
121369601Seric 				break;
121469601Seric 
121569601Seric 			  case IPOPT_SSRR:
121669601Seric 			  case IPOPT_LSRR:
121769601Seric 				p = &hbuf[strlen(hbuf)];
121869601Seric 				sprintf(p, " [%s@%s",
121969601Seric 				    *o == IPOPT_SSRR ? "!" : "",
122069601Seric 				    inet_ntoa(ipopt.ipopt_dst));
122169601Seric 				p += strlen(p);
122269601Seric 
122369601Seric 				/* o[1] is option length */
122469601Seric 				j = *++o / sizeof(struct in_addr) - 1;
122569601Seric 
122669601Seric 				/* q skips length and router pointer to data */
122769601Seric 				q = o + 2;
122869601Seric 				for ( ; j >= 0; j--)
122969601Seric 				{
123069601Seric 					memcpy(&addr, q, sizeof(addr));
123169637Seric 					sprintf(p, "%c%s",
123269601Seric 						     j ? '@' : ':',
123369601Seric 						     inet_ntoa(addr));
123469637Seric 					p += strlen(p);
123569637Seric 					q += sizeof(struct in_addr);
123669601Seric 				}
123769601Seric 				o += *o;
123869601Seric 				break;
123969601Seric 
124069601Seric 			  default:
124169601Seric 				/* Skip over option */
124269601Seric 				o += o[1];
124369601Seric 				break;
124469601Seric 			}
124569601Seric 		}
124669601Seric 		strcat(hbuf,"]");
124769601Seric 		goto postipsr;
124869601Seric 	}
124969601Seric #endif
125069601Seric 
125169601Seric noipsr:
125266003Seric 	if (RealHostName != NULL && RealHostName[0] != '[')
125358951Seric 	{
125458951Seric 		p = &hbuf[strlen(hbuf)];
125558951Seric 		(void) sprintf(p, " [%s]", anynet_ntoa(&RealHostAddr));
125658951Seric 	}
125769601Seric 
125869601Seric postipsr:
125958957Seric 	if (tTd(9, 1))
126058951Seric 		printf("getauthinfo: %s\n", hbuf);
126158308Seric 	return hbuf;
126258308Seric }
126358308Seric /*
126460089Seric **  HOST_MAP_LOOKUP -- turn a hostname into canonical form
126553751Seric **
126653751Seric **	Parameters:
126756823Seric **		map -- a pointer to this map (unused).
126860089Seric **		name -- the (presumably unqualified) hostname.
126960257Seric **		av -- unused -- for compatibility with other mapping
127055019Seric **			functions.
127159084Seric **		statp -- an exit status (out parameter) -- set to
127259084Seric **			EX_TEMPFAIL if the name server is unavailable.
127353751Seric **
127453751Seric **	Returns:
127553751Seric **		The mapping, if found.
127653751Seric **		NULL if no mapping found.
127753751Seric **
127853751Seric **	Side Effects:
127953751Seric **		Looks up the host specified in hbuf.  If it is not
128053751Seric **		the canonical name for that host, return the canonical
128153751Seric **		name.
128253751Seric */
128351315Seric 
128453751Seric char *
128560257Seric host_map_lookup(map, name, av, statp)
128656823Seric 	MAP *map;
128760089Seric 	char *name;
128860257Seric 	char **av;
128959084Seric 	int *statp;
129016911Seric {
129116911Seric 	register struct hostent *hp;
129268693Seric 	struct in_addr in_addr;
129356823Seric 	char *cp;
129459671Seric 	register STAB *s;
129568693Seric 	char hbuf[MAXNAME + 1];
129666334Seric #if NAMED_BIND
129759671Seric 	extern int h_errno;
129866029Seric #endif
129916911Seric 
130025574Smiriam 	/*
130159671Seric 	**  See if we have already looked up this name.  If so, just
130259671Seric 	**  return it.
130359671Seric 	*/
130453751Seric 
130560089Seric 	s = stab(name, ST_NAMECANON, ST_ENTER);
130659671Seric 	if (bitset(NCF_VALID, s->s_namecanon.nc_flags))
130759671Seric 	{
130859986Seric 		if (tTd(9, 1))
130960089Seric 			printf("host_map_lookup(%s) => CACHE %s\n",
131069511Seric 			       name,
131169511Seric 			       s->s_namecanon.nc_cname == NULL
131269511Seric 					? "NULL"
131369512Seric 					: s->s_namecanon.nc_cname);
131459671Seric 		errno = s->s_namecanon.nc_errno;
131566334Seric #if NAMED_BIND
131659671Seric 		h_errno = s->s_namecanon.nc_herrno;
131766029Seric #endif
131859671Seric 		*statp = s->s_namecanon.nc_stat;
131968817Seric 		if (*statp == EX_TEMPFAIL)
132065199Seric 		{
132168857Seric 			CurEnv->e_status = "4.4.3";
132268817Seric 			usrerr("451 %s: Name server timeout",
132365199Seric 				shortenstring(name, 33));
132465199Seric 		}
132559671Seric 		return s->s_namecanon.nc_cname;
132659671Seric 	}
132759671Seric 
132859671Seric 	/*
132959671Seric 	**  If first character is a bracket, then it is an address
133059671Seric 	**  lookup.  Address is copied into a temporary buffer to
133160089Seric 	**  strip the brackets and to preserve name if address is
133259671Seric 	**  unknown.
133359671Seric 	*/
133459671Seric 
133560089Seric 	if (*name != '[')
133653751Seric 	{
133755019Seric 		extern bool getcanonname();
133855019Seric 
133958798Seric 		if (tTd(9, 1))
134060089Seric 			printf("host_map_lookup(%s) => ", name);
134159671Seric 		s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
134268693Seric 		if (strlen(name) < sizeof hbuf)
134368693Seric 			(void) strcpy(hbuf, name);
134468693Seric 		else
134568693Seric 		{
134668693Seric 			bcopy(name, hbuf, sizeof hbuf - 1);
134768693Seric 			hbuf[sizeof hbuf - 1] = '\0';
134868693Seric 		}
134968759Seric 		if (getcanonname(hbuf, sizeof hbuf - 1, !NoMXforCanon))
135058796Seric 		{
135158796Seric 			if (tTd(9, 1))
135258796Seric 				printf("%s\n", hbuf);
135360257Seric 			cp = map_rewrite(map, hbuf, strlen(hbuf), av);
135460257Seric 			s->s_namecanon.nc_cname = newstr(cp);
135560257Seric 			return cp;
135658796Seric 		}
135753751Seric 		else
135858796Seric 		{
135959084Seric 			register struct hostent *hp;
136059084Seric 
136166029Seric 			s->s_namecanon.nc_errno = errno;
136266334Seric #if NAMED_BIND
136366029Seric 			s->s_namecanon.nc_herrno = h_errno;
136458796Seric 			if (tTd(9, 1))
136559084Seric 				printf("FAIL (%d)\n", h_errno);
136659084Seric 			switch (h_errno)
136759084Seric 			{
136859084Seric 			  case TRY_AGAIN:
136959596Seric 				if (UseNameServer)
137059734Seric 				{
137168857Seric 					CurEnv->e_status = "4.4.3";
137268817Seric 					usrerr("451 %s: Name server timeout",
137365199Seric 						shortenstring(name, 33));
137459734Seric 				}
137559084Seric 				*statp = EX_TEMPFAIL;
137659084Seric 				break;
137759084Seric 
137859084Seric 			  case HOST_NOT_FOUND:
137968881Seric 			  case NO_DATA:
138059084Seric 				*statp = EX_NOHOST;
138159084Seric 				break;
138259084Seric 
138359084Seric 			  case NO_RECOVERY:
138459084Seric 				*statp = EX_SOFTWARE;
138559084Seric 				break;
138659084Seric 
138759084Seric 			  default:
138859084Seric 				*statp = EX_UNAVAILABLE;
138959084Seric 				break;
139059084Seric 			}
139166029Seric #else
139266029Seric 			if (tTd(9, 1))
139366029Seric 				printf("FAIL\n");
139466029Seric 			*statp = EX_NOHOST;
139566029Seric #endif
139659671Seric 			s->s_namecanon.nc_stat = *statp;
139768693Seric 			if ((*statp != EX_TEMPFAIL && *statp != EX_NOHOST) ||
139868693Seric 			    UseNameServer)
139959084Seric 				return NULL;
140059084Seric 
140159084Seric 			/*
140259084Seric 			**  Try to look it up in /etc/hosts
140359084Seric 			*/
140459084Seric 
140568693Seric 			hp = sm_gethostbyname(name);
140659084Seric 			if (hp == NULL)
140759084Seric 			{
140859084Seric 				/* no dice there either */
140959671Seric 				s->s_namecanon.nc_stat = *statp = EX_NOHOST;
141059084Seric 				return NULL;
141159084Seric 			}
141259084Seric 
141359671Seric 			s->s_namecanon.nc_stat = *statp = EX_OK;
141460257Seric 			cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
141560257Seric 			s->s_namecanon.nc_cname = newstr(cp);
141660257Seric 			return cp;
141758796Seric 		}
141853751Seric 	}
141960089Seric 	if ((cp = strchr(name, ']')) == NULL)
142053751Seric 		return (NULL);
142140994Sbostic 	*cp = '\0';
142268693Seric 	in_addr.s_addr = inet_addr(&name[1]);
142358110Seric 
142458110Seric 	/* nope -- ask the name server */
142568693Seric 	hp = sm_gethostbyaddr((char *)&in_addr, INADDRSZ, AF_INET);
142659671Seric 	s->s_namecanon.nc_errno = errno;
142766334Seric #if NAMED_BIND
142859671Seric 	s->s_namecanon.nc_herrno = h_errno;
142966029Seric #endif
143059671Seric 	s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
143133932Sbostic 	if (hp == NULL)
143259671Seric 	{
143359671Seric 		s->s_namecanon.nc_stat = *statp = EX_NOHOST;
143453751Seric 		return (NULL);
143559671Seric 	}
143653751Seric 
143758110Seric 	/* found a match -- copy out */
143860257Seric 	cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
143959671Seric 	s->s_namecanon.nc_stat = *statp = EX_OK;
144060257Seric 	s->s_namecanon.nc_cname = newstr(cp);
144160257Seric 	return cp;
144233932Sbostic }
144358755Seric /*
144458755Seric **  ANYNET_NTOA -- convert a network address to printable form.
144558755Seric **
144658755Seric **	Parameters:
144758755Seric **		sap -- a pointer to a sockaddr structure.
144858755Seric **
144958755Seric **	Returns:
145058755Seric **		A printable version of that sockaddr.
145158755Seric */
145216911Seric 
145369562Seric #ifdef NETLINK
145469516Seric # include <net/if_dl.h>
145569516Seric #endif
145669516Seric 
145758755Seric char *
145858755Seric anynet_ntoa(sap)
145958755Seric 	register SOCKADDR *sap;
146058755Seric {
146158755Seric 	register char *bp;
146258755Seric 	register char *ap;
146358755Seric 	int l;
146464734Seric 	static char buf[100];
146558755Seric 
146658798Seric 	/* check for null/zero family */
146758798Seric 	if (sap == NULL)
146858798Seric 		return "NULLADDR";
146958798Seric 	if (sap->sa.sa_family == 0)
147058798Seric 		return "0";
147158798Seric 
147264734Seric 	switch (sap->sa.sa_family)
147364734Seric 	{
147464821Seric #ifdef NETUNIX
147564734Seric 	  case AF_UNIX:
147664758Seric 	  	if (sap->sunix.sun_path[0] != '\0')
147764758Seric 	  		sprintf(buf, "[UNIX: %.64s]", sap->sunix.sun_path);
147864734Seric 	  	else
147964734Seric 	  		sprintf(buf, "[UNIX: localhost]");
148064734Seric 		return buf;
148164734Seric #endif
148264734Seric 
148358778Seric #ifdef NETINET
148464734Seric 	  case AF_INET:
148568776Seric 		return inet_ntoa(sap->sin.sin_addr);
148658778Seric #endif
148758755Seric 
148869562Seric #ifdef NETLINK
148969516Seric 	  case AF_LINK:
149069516Seric 		sprintf(buf, "[LINK: %s]",
149169516Seric 			link_ntoa((struct sockaddr_dl *) &sap->sa));
149269516Seric 		return buf;
149369516Seric #endif
149464734Seric 	  default:
149569516Seric 		/* this case is needed when nothing is #defined */
149669516Seric 		/* in order to keep the switch syntactically correct */
149769516Seric 		break;
149864734Seric 	}
149964734Seric 
150058755Seric 	/* unknown family -- just dump bytes */
150158778Seric 	(void) sprintf(buf, "Family %d: ", sap->sa.sa_family);
150258755Seric 	bp = &buf[strlen(buf)];
150358778Seric 	ap = sap->sa.sa_data;
150458778Seric 	for (l = sizeof sap->sa.sa_data; --l >= 0; )
150558755Seric 	{
150658755Seric 		(void) sprintf(bp, "%02x:", *ap++ & 0377);
150758755Seric 		bp += 3;
150858755Seric 	}
150958755Seric 	*--bp = '\0';
151058755Seric 	return buf;
151158755Seric }
151258951Seric /*
151358951Seric **  HOSTNAMEBYANYADDR -- return name of host based on address
151458951Seric **
151558951Seric **	Parameters:
151658951Seric **		sap -- SOCKADDR pointer
151758951Seric **
151858951Seric **	Returns:
151958951Seric **		text representation of host name.
152058951Seric **
152158951Seric **	Side Effects:
152258951Seric **		none.
152358951Seric */
152458755Seric 
152558951Seric char *
152658951Seric hostnamebyanyaddr(sap)
152758951Seric 	register SOCKADDR *sap;
152858951Seric {
152958951Seric 	register struct hostent *hp;
153064734Seric 	int saveretry;
153158951Seric 
153266334Seric #if NAMED_BIND
153359042Seric 	/* shorten name server timeout to avoid higher level timeouts */
153459042Seric 	saveretry = _res.retry;
153559042Seric 	_res.retry = 3;
153659042Seric #endif /* NAMED_BIND */
153759042Seric 
153858951Seric 	switch (sap->sa.sa_family)
153958951Seric 	{
154058951Seric #ifdef NETINET
154158951Seric 	  case AF_INET:
154268693Seric 		hp = sm_gethostbyaddr((char *) &sap->sin.sin_addr,
154368693Seric 			INADDRSZ,
154458951Seric 			AF_INET);
154558951Seric 		break;
154658951Seric #endif
154758951Seric 
154858951Seric #ifdef NETISO
154958951Seric 	  case AF_ISO:
155068693Seric 		hp = sm_gethostbyaddr((char *) &sap->siso.siso_addr,
155158951Seric 			sizeof sap->siso.siso_addr,
155258951Seric 			AF_ISO);
155358951Seric 		break;
155458951Seric #endif
155558951Seric 
155664734Seric 	  case AF_UNIX:
155764734Seric 		hp = NULL;
155864734Seric 		break;
155964734Seric 
156058951Seric 	  default:
156168693Seric 		hp = sm_gethostbyaddr(sap->sa.sa_data,
156258951Seric 			   sizeof sap->sa.sa_data,
156358951Seric 			   sap->sa.sa_family);
156458951Seric 		break;
156558951Seric 	}
156658951Seric 
156766334Seric #if NAMED_BIND
156859042Seric 	_res.retry = saveretry;
156959042Seric #endif /* NAMED_BIND */
157059042Seric 
157158951Seric 	if (hp != NULL)
157258951Seric 		return hp->h_name;
157358951Seric 	else
157458951Seric 	{
157558951Seric 		/* produce a dotted quad */
157658951Seric 		static char buf[512];
157758951Seric 
157858951Seric 		(void) sprintf(buf, "[%s]", anynet_ntoa(sap));
157958951Seric 		return buf;
158058951Seric 	}
158158951Seric }
158258951Seric 
158356795Seric # else /* DAEMON */
158416911Seric /* code for systems without sophisticated networking */
158510758Seric 
158610758Seric /*
158710758Seric **  MYHOSTNAME -- stub version for case of no daemon code.
158811297Seric **
158911297Seric **	Can't convert to upper case here because might be a UUCP name.
159012313Seric **
159112313Seric **	Mark, you can change this to be anything you want......
159210758Seric */
159310758Seric 
159410758Seric char **
159512313Seric myhostname(hostbuf, size)
159610758Seric 	char hostbuf[];
159712313Seric 	int size;
159810758Seric {
159910758Seric 	register FILE *f;
160010758Seric 
160110758Seric 	hostbuf[0] = '\0';
160210758Seric 	f = fopen("/usr/include/whoami", "r");
160310758Seric 	if (f != NULL)
160410758Seric 	{
160512313Seric 		(void) fgets(hostbuf, size, f);
160610758Seric 		fixcrlf(hostbuf, TRUE);
160710758Seric 		(void) fclose(f);
160810758Seric 	}
160910758Seric 	return (NULL);
161010758Seric }
161116911Seric /*
161258951Seric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
161358308Seric **
161458308Seric **	Parameters:
161558308Seric **		fd -- the descriptor
161658308Seric **
161758308Seric **	Returns:
161858308Seric **		The host name associated with this descriptor, if it can
161958308Seric **			be determined.
162058308Seric **		NULL otherwise.
162158308Seric **
162258308Seric **	Side Effects:
162358308Seric **		none
162458308Seric */
162558308Seric 
162658308Seric char *
162758951Seric getauthinfo(fd)
162858308Seric 	int fd;
162958308Seric {
163058308Seric 	return NULL;
163158308Seric }
163258308Seric /*
163316911Seric **  MAPHOSTNAME -- turn a hostname into canonical form
163416911Seric **
163516911Seric **	Parameters:
163656823Seric **		map -- a pointer to the database map.
163760089Seric **		name -- a buffer containing a hostname.
163853751Seric **		avp -- a pointer to a (cf file defined) argument vector.
163959084Seric **		statp -- an exit status (out parameter).
164016911Seric **
164116911Seric **	Returns:
164253751Seric **		mapped host name
164351315Seric **		FALSE otherwise.
164416911Seric **
164516911Seric **	Side Effects:
164660089Seric **		Looks up the host specified in name.  If it is not
164716911Seric **		the canonical name for that host, replace it with
164816911Seric **		the canonical name.  If the name is unknown, or it
164916911Seric **		is already the canonical name, leave it unchanged.
165016911Seric */
165110758Seric 
165216911Seric /*ARGSUSED*/
165353751Seric char *
165460089Seric host_map_lookup(map, name, avp, statp)
165556823Seric 	MAP *map;
165660089Seric 	char *name;
165753751Seric 	char **avp;
165859084Seric 	char *statp;
165916911Seric {
166059084Seric 	register struct hostent *hp;
166159084Seric 
166268693Seric 	hp = sm_gethostbyname(name);
166359084Seric 	if (hp != NULL)
166459084Seric 		return hp->h_name;
166559084Seric 	*statp = EX_NOHOST;
166653751Seric 	return NULL;
166716911Seric }
166816911Seric 
166956795Seric #endif /* DAEMON */
1670