xref: /csrg-svn/usr.sbin/sendmail/src/daemon.c (revision 68857)
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*68857Seric static char sccsid[] = "@(#)daemon.c	8.82 (Berkeley) 04/22/95 (with daemon mode)";
1533780Sbostic #else
16*68857Seric static char sccsid[] = "@(#)daemon.c	8.82 (Berkeley) 04/22/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 
284535Seric /*
294535Seric **  DAEMON.C -- routines to use when running as a daemon.
307556Seric **
317556Seric **	This entire file is highly dependent on the 4.2 BSD
327556Seric **	interprocess communication primitives.  No attempt has
337556Seric **	been made to make this file portable to Version 7,
347556Seric **	Version 6, MPX files, etc.  If you should try such a
357556Seric **	thing yourself, I recommend chucking the entire file
367556Seric **	and starting from scratch.  Basic semantics are:
377556Seric **
387556Seric **	getrequests()
397556Seric **		Opens a port and initiates a connection.
407556Seric **		Returns in a child.  Must set InChannel and
417556Seric **		OutChannel appropriately.
4210206Seric **	clrdaemon()
4310206Seric **		Close any open files associated with getting
4410206Seric **		the connection; this is used when running the queue,
4510206Seric **		etc., to avoid having extra file descriptors during
4610206Seric **		the queue run and to avoid confusing the network
4710206Seric **		code (if it cares).
4852106Seric **	makeconnection(host, port, outfile, infile, usesecureport)
497556Seric **		Make a connection to the named host on the given
507556Seric **		port.  Set *outfile and *infile to the files
517556Seric **		appropriate for communication.  Returns zero on
527556Seric **		success, else an exit status describing the
537556Seric **		error.
5460089Seric **	host_map_lookup(map, hbuf, avp, pstat)
5556823Seric **		Convert the entry in hbuf into a canonical form.
564535Seric */
574535Seric /*
584535Seric **  GETREQUESTS -- open mail IPC port and get requests.
594535Seric **
604535Seric **	Parameters:
614535Seric **		none.
624535Seric **
634535Seric **	Returns:
644535Seric **		none.
654535Seric **
664535Seric **	Side Effects:
674535Seric **		Waits until some interesting activity occurs.  When
684535Seric **		it does, a child is created to process it, and the
694535Seric **		parent waits for completion.  Return from this
709886Seric **		routine is always in the child.  The file pointers
719886Seric **		"InChannel" and "OutChannel" should be set to point
729886Seric **		to the communication channel.
734535Seric */
744535Seric 
7558849Seric int		DaemonSocket	= -1;		/* fd describing socket */
7658849Seric SOCKADDR	DaemonAddr;			/* socket for incoming */
7759783Seric int		ListenQueueSize = 10;		/* size of listen queue */
7864381Seric int		TcpRcvBufferSize = 0;		/* size of TCP receive buffer */
7964381Seric int		TcpSndBufferSize = 0;		/* size of TCP send buffer */
8016144Seric 
8168693Seric void
824535Seric getrequests()
834535Seric {
849610Seric 	int t;
8553751Seric 	bool refusingconnections = TRUE;
8658419Seric 	FILE *pidf;
8764828Seric 	int socksize;
8866793Seric #ifdef XDEBUG
8966793Seric 	bool j_has_dot;
9066793Seric #endif
9146928Sbostic 	extern void reapchild();
927117Seric 
939610Seric 	/*
949610Seric 	**  Set up the address for the mailer.
959610Seric 	*/
969610Seric 
9758849Seric 	if (DaemonAddr.sin.sin_family == 0)
9858849Seric 		DaemonAddr.sin.sin_family = AF_INET;
9958849Seric 	if (DaemonAddr.sin.sin_addr.s_addr == 0)
10058849Seric 		DaemonAddr.sin.sin_addr.s_addr = INADDR_ANY;
10158849Seric 	if (DaemonAddr.sin.sin_port == 0)
1029610Seric 	{
10365169Seric 		register struct servent *sp;
10465169Seric 
10558849Seric 		sp = getservbyname("smtp", "tcp");
10658849Seric 		if (sp == NULL)
10758849Seric 		{
10858909Seric 			syserr("554 service \"smtp\" unknown");
10965169Seric 			DaemonAddr.sin.sin_port = htons(25);
11058849Seric 		}
11165169Seric 		else
11265169Seric 			DaemonAddr.sin.sin_port = sp->s_port;
1139610Seric 	}
1149610Seric 
1159610Seric 	/*
1169610Seric 	**  Try to actually open the connection.
1179610Seric 	*/
1189610Seric 
1199610Seric 	if (tTd(15, 1))
12058849Seric 		printf("getrequests: port 0x%x\n", DaemonAddr.sin.sin_port);
1219610Seric 
1229610Seric 	/* get a socket for the SMTP connection */
12366854Seric 	socksize = opendaemonsocket(TRUE);
12410347Seric 
12564035Seric 	(void) setsignal(SIGCHLD, reapchild);
12624945Seric 
12758419Seric 	/* write the pid to the log file for posterity */
12858419Seric 	pidf = fopen(PidFile, "w");
12958419Seric 	if (pidf != NULL)
13058419Seric 	{
13163863Seric 		extern char *CommandLineArgs;
13263863Seric 
13363863Seric 		/* write the process id on line 1 */
13458419Seric 		fprintf(pidf, "%d\n", getpid());
13563863Seric 
13663863Seric 		/* line 2 contains all command line flags */
13763863Seric 		fprintf(pidf, "%s\n", CommandLineArgs);
13863863Seric 
13963863Seric 		/* flush and close */
14058419Seric 		fclose(pidf);
14158419Seric 	}
14258419Seric 
14366793Seric #ifdef XDEBUG
14466793Seric 	{
14566812Seric 		char jbuf[MAXHOSTNAMELEN];
14658419Seric 
14768693Seric 		expand("\201j", jbuf, sizeof jbuf, CurEnv);
14866812Seric 		j_has_dot = strchr(jbuf, '.') != NULL;
14966793Seric 	}
15066793Seric #endif
15166793Seric 
1529610Seric 	if (tTd(15, 1))
15310206Seric 		printf("getrequests: %d\n", DaemonSocket);
1549610Seric 
1554631Seric 	for (;;)
1564631Seric 	{
15714875Seric 		register int pid;
15811147Seric 		auto int lotherend;
15953751Seric 		extern bool refuseconnections();
16068693Seric 		extern int getla();
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 
17968693Seric 		/* arrange to (re)open the socket if necessary */
18053751Seric 		if (refusingconnections)
18153751Seric 		{
18267690Seric 			(void) opendaemonsocket(FALSE);
18353751Seric 			setproctitle("accepting connections");
18453751Seric 			refusingconnections = FALSE;
18553751Seric 		}
18653751Seric 
18766793Seric #ifdef XDEBUG
18866793Seric 		/* check for disaster */
18966793Seric 		{
19066812Seric 			char jbuf[MAXHOSTNAMELEN];
19166793Seric 
19268693Seric 			expand("\201j", jbuf, sizeof jbuf, CurEnv);
19368693Seric 			if (!wordinclass(jbuf, 'w'))
19466793Seric 			{
19566793Seric 				dumpstate("daemon lost $j");
19666793Seric 				syslog(LOG_ALERT, "daemon process doesn't have $j in $=w; see syslog");
19766793Seric 				abort();
19866793Seric 			}
19966812Seric 			else if (j_has_dot && strchr(jbuf, '.') == NULL)
20066793Seric 			{
20166793Seric 				dumpstate("daemon $j lost dot");
20266793Seric 				syslog(LOG_ALERT, "daemon process $j lost dot; see syslog");
20366793Seric 				abort();
20466793Seric 			}
20566793Seric 		}
20666793Seric #endif
20766793Seric 
2089610Seric 		/* wait for a connection */
2099610Seric 		do
2109610Seric 		{
2119610Seric 			errno = 0;
21264828Seric 			lotherend = socksize;
21346928Sbostic 			t = accept(DaemonSocket,
21446928Sbostic 			    (struct sockaddr *)&RealHostAddr, &lotherend);
2159610Seric 		} while (t < 0 && errno == EINTR);
2169610Seric 		if (t < 0)
2175978Seric 		{
2189610Seric 			syserr("getrequests: accept");
21968693Seric 
22068693Seric 			/* arrange to re-open the socket next time around */
22168693Seric 			(void) close(DaemonSocket);
22268693Seric 			DaemonSocket = -1;
2239610Seric 			sleep(5);
2249610Seric 			continue;
2255978Seric 		}
2264631Seric 
2275978Seric 		/*
2285978Seric 		**  Create a subprocess to process the mail.
2295978Seric 		*/
2305978Seric 
2317677Seric 		if (tTd(15, 2))
2329610Seric 			printf("getrequests: forking (fd = %d)\n", t);
2335978Seric 
2344636Seric 		pid = fork();
2354636Seric 		if (pid < 0)
2364631Seric 		{
2374636Seric 			syserr("daemon: cannot fork");
2384636Seric 			sleep(10);
2399610Seric 			(void) close(t);
2404636Seric 			continue;
2414631Seric 		}
2424631Seric 
2434636Seric 		if (pid == 0)
2444631Seric 		{
24564086Seric 			char *p;
24658951Seric 			extern char *hostnamebyanyaddr();
24768693Seric 			extern void intsig();
24811147Seric 
2494636Seric 			/*
2504636Seric 			**  CHILD -- return to caller.
25111147Seric 			**	Collect verified idea of sending host.
2524636Seric 			**	Verify calling user id if possible here.
2534636Seric 			*/
2544631Seric 
25564035Seric 			(void) setsignal(SIGCHLD, SIG_DFL);
25668693Seric 			(void) setsignal(SIGHUP, intsig);
25768693Seric 			(void) close(DaemonSocket);
25866017Seric 			DisConnected = FALSE;
25924950Seric 
26066032Seric 			setproctitle("startup with %s",
26166032Seric 				anynet_ntoa(&RealHostAddr));
26266032Seric 
26311147Seric 			/* determine host name */
26464086Seric 			p = hostnamebyanyaddr(&RealHostAddr);
26564086Seric 			RealHostName = newstr(p);
26666032Seric 			setproctitle("startup with %s", p);
26758778Seric 
26855173Seric #ifdef LOG
26963842Seric 			if (LogLevel > 11)
27055173Seric 			{
27155173Seric 				/* log connection information */
27255173Seric 				syslog(LOG_INFO, "connect from %s (%s)",
27358951Seric 					RealHostName, anynet_ntoa(&RealHostAddr));
27455173Seric 			}
27555173Seric #endif
27655173Seric 
27764724Seric 			if ((InChannel = fdopen(t, "r")) == NULL ||
27864724Seric 			    (t = dup(t)) < 0 ||
27964724Seric 			    (OutChannel = fdopen(t, "w")) == NULL)
28064724Seric 			{
28164724Seric 				syserr("cannot open SMTP server channel, fd=%d", t);
28264724Seric 				exit(0);
28364724Seric 			}
28459254Seric 
28516884Seric 			/* should we check for illegal connection here? XXX */
28659156Seric #ifdef XLA
28759156Seric 			if (!xla_host_ok(RealHostName))
28859156Seric 			{
28959254Seric 				message("421 Too many SMTP sessions for this host");
29059156Seric 				exit(0);
29159156Seric 			}
29259156Seric #endif
29316884Seric 
2947677Seric 			if (tTd(15, 2))
2955978Seric 				printf("getreq: returning\n");
2964636Seric 			return;
2974631Seric 		}
2984631Seric 
2997117Seric 		/* close the port so that others will hang (for a while) */
3009610Seric 		(void) close(t);
3014631Seric 	}
3029886Seric 	/*NOTREACHED*/
3034631Seric }
3045978Seric /*
30566845Seric **  OPENDAEMONSOCKET -- open the SMTP socket
30666845Seric **
30766845Seric **	Deals with setting all appropriate options.  DaemonAddr must
30866845Seric **	be set up in advance.
30966845Seric **
31066845Seric **	Parameters:
31166854Seric **		firsttime -- set if this is the initial open.
31266845Seric **
31366845Seric **	Returns:
31466845Seric **		Size in bytes of the daemon socket addr.
31566845Seric **
31666845Seric **	Side Effects:
31766845Seric **		Leaves DaemonSocket set to the open socket.
31866845Seric **		Exits if the socket cannot be created.
31966845Seric */
32066845Seric 
32166861Seric #define MAXOPENTRIES	10	/* maximum number of tries to open connection */
32266861Seric 
32366845Seric int
32466854Seric opendaemonsocket(firsttime)
32566854Seric 	bool firsttime;
32666845Seric {
32766845Seric 	int on = 1;
32868693Seric 	int socksize = 0;
32966861Seric 	int ntries = 0;
33066861Seric 	int saveerrno;
33166845Seric 
33266845Seric 	if (tTd(15, 2))
33366845Seric 		printf("opendaemonsocket()\n");
33466845Seric 
33566861Seric 	do
33666845Seric 	{
33766862Seric 		if (ntries > 0)
33866862Seric 			sleep(5);
33966861Seric 		if (firsttime || DaemonSocket < 0)
34066854Seric 		{
34166861Seric 			DaemonSocket = socket(DaemonAddr.sa.sa_family, SOCK_STREAM, 0);
34266861Seric 			if (DaemonSocket < 0)
34366861Seric 			{
34466861Seric 				/* probably another daemon already */
34566861Seric 				saveerrno = errno;
34666861Seric 				syserr("opendaemonsocket: can't create server SMTP socket");
34766861Seric 			  severe:
34866845Seric # ifdef LOG
34966861Seric 				if (LogLevel > 0)
35066861Seric 					syslog(LOG_ALERT, "problem creating SMTP socket");
35166845Seric # endif /* LOG */
35266861Seric 				DaemonSocket = -1;
35366861Seric 				continue;
35466861Seric 			}
35566845Seric 
35666861Seric 			/* turn on network debugging? */
35766861Seric 			if (tTd(15, 101))
35866861Seric 				(void) setsockopt(DaemonSocket, SOL_SOCKET,
35966861Seric 						  SO_DEBUG, (char *)&on,
36066861Seric 						  sizeof on);
36166845Seric 
36266861Seric 			(void) setsockopt(DaemonSocket, SOL_SOCKET,
36366861Seric 					  SO_REUSEADDR, (char *)&on, sizeof on);
36466861Seric 			(void) setsockopt(DaemonSocket, SOL_SOCKET,
36566861Seric 					  SO_KEEPALIVE, (char *)&on, sizeof on);
36666845Seric 
36766845Seric #ifdef SO_RCVBUF
36866861Seric 			if (TcpRcvBufferSize > 0)
36966861Seric 			{
37066861Seric 				if (setsockopt(DaemonSocket, SOL_SOCKET,
37166861Seric 					       SO_RCVBUF,
37266861Seric 					       (char *) &TcpRcvBufferSize,
37366861Seric 					       sizeof(TcpRcvBufferSize)) < 0)
37466861Seric 					syserr("getrequests: setsockopt(SO_RCVBUF)");
37566861Seric 			}
37666845Seric #endif
37766845Seric 
37866861Seric 			switch (DaemonAddr.sa.sa_family)
37966861Seric 			{
38066845Seric # ifdef NETINET
38166861Seric 			  case AF_INET:
38266861Seric 				socksize = sizeof DaemonAddr.sin;
38366861Seric 				break;
38466845Seric # endif
38566845Seric 
38666845Seric # ifdef NETISO
38766861Seric 			  case AF_ISO:
38866861Seric 				socksize = sizeof DaemonAddr.siso;
38966861Seric 				break;
39066845Seric # endif
39166845Seric 
39266861Seric 			  default:
39366861Seric 				socksize = sizeof DaemonAddr;
39466861Seric 				break;
39566861Seric 			}
39666861Seric 
39766861Seric 			if (bind(DaemonSocket, &DaemonAddr.sa, socksize) < 0)
39866861Seric 			{
39966861Seric 				saveerrno = errno;
40066861Seric 				syserr("getrequests: cannot bind");
40166861Seric 				(void) close(DaemonSocket);
40266861Seric 				goto severe;
40366861Seric 			}
40466854Seric 		}
40566861Seric 		if (!firsttime && listen(DaemonSocket, ListenQueueSize) < 0)
40666854Seric 		{
40766861Seric 			saveerrno = errno;
40866861Seric 			syserr("getrequests: cannot listen");
40966854Seric 			(void) close(DaemonSocket);
41066854Seric 			goto severe;
41166854Seric 		}
41266861Seric 		return socksize;
41366861Seric 	} while (ntries++ < MAXOPENTRIES && transienterror(saveerrno));
41468693Seric 	syserr("!opendaemonsocket: server SMTP socket wedged: exiting");
41566861Seric 	finis();
41666845Seric }
41766845Seric /*
41810206Seric **  CLRDAEMON -- reset the daemon connection
41910206Seric **
42010206Seric **	Parameters:
42110206Seric **		none.
42210206Seric **
42310206Seric **	Returns:
42410206Seric **		none.
42510206Seric **
42610206Seric **	Side Effects:
42710206Seric **		releases any resources used by the passive daemon.
42810206Seric */
42910206Seric 
43068693Seric void
43110206Seric clrdaemon()
43210206Seric {
43310206Seric 	if (DaemonSocket >= 0)
43410206Seric 		(void) close(DaemonSocket);
43510206Seric 	DaemonSocket = -1;
43610206Seric }
43710206Seric /*
43858849Seric **  SETDAEMONOPTIONS -- set options for running the daemon
43958849Seric **
44058849Seric **	Parameters:
44158849Seric **		p -- the options line.
44258849Seric **
44358849Seric **	Returns:
44458849Seric **		none.
44558849Seric */
44658849Seric 
44768693Seric void
44858849Seric setdaemonoptions(p)
44958849Seric 	register char *p;
45058849Seric {
45158873Seric 	if (DaemonAddr.sa.sa_family == AF_UNSPEC)
45258873Seric 		DaemonAddr.sa.sa_family = AF_INET;
45358873Seric 
45458849Seric 	while (p != NULL)
45558849Seric 	{
45658849Seric 		register char *f;
45758849Seric 		register char *v;
45858849Seric 
45958849Seric 		while (isascii(*p) && isspace(*p))
46058849Seric 			p++;
46158849Seric 		if (*p == '\0')
46258849Seric 			break;
46358849Seric 		f = p;
46458849Seric 		p = strchr(p, ',');
46558849Seric 		if (p != NULL)
46658849Seric 			*p++ = '\0';
46758849Seric 		v = strchr(f, '=');
46858849Seric 		if (v == NULL)
46958849Seric 			continue;
47058849Seric 		while (isascii(*++v) && isspace(*v))
47158849Seric 			continue;
47258849Seric 
47358849Seric 		switch (*f)
47458849Seric 		{
47558873Seric 		  case 'F':		/* address family */
47658849Seric 			if (isascii(*v) && isdigit(*v))
47758873Seric 				DaemonAddr.sa.sa_family = atoi(v);
47858873Seric #ifdef NETINET
47958873Seric 			else if (strcasecmp(v, "inet") == 0)
48058873Seric 				DaemonAddr.sa.sa_family = AF_INET;
48158873Seric #endif
48258873Seric #ifdef NETISO
48358873Seric 			else if (strcasecmp(v, "iso") == 0)
48458873Seric 				DaemonAddr.sa.sa_family = AF_ISO;
48558873Seric #endif
48658873Seric #ifdef NETNS
48758873Seric 			else if (strcasecmp(v, "ns") == 0)
48858873Seric 				DaemonAddr.sa.sa_family = AF_NS;
48958873Seric #endif
49058873Seric #ifdef NETX25
49158873Seric 			else if (strcasecmp(v, "x.25") == 0)
49258873Seric 				DaemonAddr.sa.sa_family = AF_CCITT;
49358873Seric #endif
49458849Seric 			else
49558873Seric 				syserr("554 Unknown address family %s in Family=option", v);
49658873Seric 			break;
49758873Seric 
49858873Seric 		  case 'A':		/* address */
49958873Seric 			switch (DaemonAddr.sa.sa_family)
50058849Seric 			{
50158873Seric #ifdef NETINET
50258873Seric 			  case AF_INET:
50358873Seric 				if (isascii(*v) && isdigit(*v))
50468693Seric 					DaemonAddr.sin.sin_addr.s_addr = htonl(inet_network(v));
50558873Seric 				else
50658873Seric 				{
50758873Seric 					register struct netent *np;
50858849Seric 
50958873Seric 					np = getnetbyname(v);
51058873Seric 					if (np == NULL)
51158873Seric 						syserr("554 network \"%s\" unknown", v);
51258873Seric 					else
51358873Seric 						DaemonAddr.sin.sin_addr.s_addr = np->n_net;
51458873Seric 				}
51558873Seric 				break;
51658873Seric #endif
51758873Seric 
51858873Seric 			  default:
51958873Seric 				syserr("554 Address= option unsupported for family %d",
52058873Seric 					DaemonAddr.sa.sa_family);
52158873Seric 				break;
52258849Seric 			}
52358849Seric 			break;
52458849Seric 
52558873Seric 		  case 'P':		/* port */
52658873Seric 			switch (DaemonAddr.sa.sa_family)
52758849Seric 			{
52858873Seric 				short port;
52958849Seric 
53058873Seric #ifdef NETINET
53158873Seric 			  case AF_INET:
53258873Seric 				if (isascii(*v) && isdigit(*v))
53364366Seric 					DaemonAddr.sin.sin_port = htons(atoi(v));
53458849Seric 				else
53558873Seric 				{
53658873Seric 					register struct servent *sp;
53758873Seric 
53858873Seric 					sp = getservbyname(v, "tcp");
53958873Seric 					if (sp == NULL)
54058909Seric 						syserr("554 service \"%s\" unknown", v);
54158873Seric 					else
54258873Seric 						DaemonAddr.sin.sin_port = sp->s_port;
54358873Seric 				}
54458873Seric 				break;
54558873Seric #endif
54658873Seric 
54758873Seric #ifdef NETISO
54858873Seric 			  case AF_ISO:
54958873Seric 				/* assume two byte transport selector */
55058873Seric 				if (isascii(*v) && isdigit(*v))
55164366Seric 					port = htons(atoi(v));
55258873Seric 				else
55358873Seric 				{
55458873Seric 					register struct servent *sp;
55558873Seric 
55658873Seric 					sp = getservbyname(v, "tcp");
55758873Seric 					if (sp == NULL)
55858909Seric 						syserr("554 service \"%s\" unknown", v);
55958873Seric 					else
56058873Seric 						port = sp->s_port;
56158873Seric 				}
56258873Seric 				bcopy((char *) &port, TSEL(&DaemonAddr.siso), 2);
56358873Seric 				break;
56458873Seric #endif
56558873Seric 
56658873Seric 			  default:
56758873Seric 				syserr("554 Port= option unsupported for family %d",
56858873Seric 					DaemonAddr.sa.sa_family);
56958873Seric 				break;
57058849Seric 			}
57158849Seric 			break;
57259783Seric 
57359783Seric 		  case 'L':		/* listen queue size */
57459783Seric 			ListenQueueSize = atoi(v);
57559783Seric 			break;
57664381Seric 
57764381Seric 		  case 'S':		/* send buffer size */
57864381Seric 			TcpSndBufferSize = atoi(v);
57964381Seric 			break;
58064381Seric 
58164381Seric 		  case 'R':		/* receive buffer size */
58264381Seric 			TcpRcvBufferSize = atoi(v);
58364381Seric 			break;
58458849Seric 		}
58558849Seric 	}
58658849Seric }
58758849Seric /*
5886039Seric **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
5896039Seric **
5906039Seric **	Parameters:
5916039Seric **		host -- the name of the host.
5926633Seric **		port -- the port number to connect to.
59353739Seric **		mci -- a pointer to the mail connection information
59453739Seric **			structure to be filled in.
59552106Seric **		usesecureport -- if set, use a low numbered (reserved)
59652106Seric **			port to provide some rudimentary authentication.
5976039Seric **
5986039Seric **	Returns:
5996039Seric **		An exit code telling whether the connection could be
6006039Seric **			made and if not why not.
6016039Seric **
6026039Seric **	Side Effects:
6036039Seric **		none.
6046039Seric */
6055978Seric 
60658755Seric SOCKADDR	CurHostAddr;		/* address of current host */
60758305Seric 
60854967Seric int
60953739Seric makeconnection(host, port, mci, usesecureport)
6106039Seric 	char *host;
6117286Seric 	u_short port;
61254967Seric 	register MCI *mci;
61352106Seric 	bool usesecureport;
6146039Seric {
61568693Seric 	register int i = 0;
61668693Seric 	register int s;
61729430Sbloom 	register struct hostent *hp = (struct hostent *)NULL;
61858755Seric 	SOCKADDR addr;
61952106Seric 	int sav_errno;
62058755Seric 	int addrlen;
62168693Seric 	bool firstconnect;
62266334Seric #if NAMED_BIND
62335651Seric 	extern int h_errno;
62435651Seric #endif
6256039Seric 
6266039Seric 	/*
6276039Seric 	**  Set up the address for the mailer.
6289308Seric 	**	Accept "[a.b.c.d]" syntax for host name.
6296039Seric 	*/
6306039Seric 
63166334Seric #if NAMED_BIND
63225475Smiriam 	h_errno = 0;
63335651Seric #endif
63425475Smiriam 	errno = 0;
63558864Seric 	bzero(&CurHostAddr, sizeof CurHostAddr);
63664334Seric 	SmtpPhase = mci->mci_phase = "initial connection";
63758906Seric 	CurHostName = host;
63825475Smiriam 
6399308Seric 	if (host[0] == '[')
6409308Seric 	{
64111147Seric 		long hid;
64256795Seric 		register char *p = strchr(host, ']');
6439308Seric 
64411147Seric 		if (p != NULL)
6459308Seric 		{
64611147Seric 			*p = '\0';
64759884Seric #ifdef NETINET
64811147Seric 			hid = inet_addr(&host[1]);
64958360Seric 			if (hid == -1)
65059884Seric #endif
65158360Seric 			{
65258360Seric 				/* try it as a host name (avoid MX lookup) */
65368693Seric 				hp = sm_gethostbyname(&host[1]);
65466349Seric 				if (hp == NULL && p[-1] == '.')
65566349Seric 				{
65668693Seric #if NAMED_BIND
65768693Seric 					int oldopts = _res.options;
65868693Seric 
65968693Seric 					_res.options &= ~(RES_DEFNAMES|RES_DNSRCH);
66068693Seric #endif
66166349Seric 					p[-1] = '\0';
66268693Seric 					hp = sm_gethostbyname(&host[1]);
66366349Seric 					p[-1] = '.';
66468693Seric #if NAMED_BIND
66568693Seric 					_res.options = oldopts;
66668693Seric #endif
66766349Seric 				}
66858360Seric 				*p = ']';
66958360Seric 				goto gothostent;
67058360Seric 			}
67111147Seric 			*p = ']';
6729308Seric 		}
67358360Seric 		if (p == NULL)
6749308Seric 		{
67558151Seric 			usrerr("553 Invalid numeric domain spec \"%s\"", host);
676*68857Seric 			mci->mci_status = "5.1.2";
6779308Seric 			return (EX_NOHOST);
6789308Seric 		}
67959884Seric #ifdef NETINET
68059884Seric 		addr.sin.sin_family = AF_INET;		/*XXX*/
68158778Seric 		addr.sin.sin_addr.s_addr = hid;
68259884Seric #endif
6839308Seric 	}
6849610Seric 	else
6859610Seric 	{
68666349Seric 		register char *p = &host[strlen(host) - 1];
68766349Seric 
68868693Seric 		hp = sm_gethostbyname(host);
68966349Seric 		if (hp == NULL && *p == '.')
69066349Seric 		{
69168693Seric #if NAMED_BIND
69268693Seric 			int oldopts = _res.options;
69368693Seric 
69468693Seric 			_res.options &= ~(RES_DEFNAMES|RES_DNSRCH);
69568693Seric #endif
69666349Seric 			*p = '\0';
69768693Seric 			hp = sm_gethostbyname(host);
69866349Seric 			*p = '.';
69968693Seric #if NAMED_BIND
70068693Seric 			_res.options = oldopts;
70168693Seric #endif
70266349Seric 		}
70358360Seric gothostent:
70425475Smiriam 		if (hp == NULL)
70524945Seric 		{
70666334Seric #if NAMED_BIND
70768693Seric 			/* check for name server timeouts */
70868693Seric 			if (errno == ETIMEDOUT || h_errno == TRY_AGAIN ||
70968693Seric 			    (errno == ECONNREFUSED && UseNameServer))
71068693Seric 			{
71168693Seric 				mci->mci_status = "4.4.3";
71225475Smiriam 				return (EX_TEMPFAIL);
71368693Seric 			}
71435651Seric #endif
71525475Smiriam 			return (EX_NOHOST);
71624945Seric 		}
71758778Seric 		addr.sa.sa_family = hp->h_addrtype;
71858778Seric 		switch (hp->h_addrtype)
71958778Seric 		{
72058778Seric #ifdef NETINET
72158778Seric 		  case AF_INET:
72258755Seric 			bcopy(hp->h_addr,
72358778Seric 				&addr.sin.sin_addr,
72468693Seric 				INADDRSZ);
72558778Seric 			break;
72658778Seric #endif
72758778Seric 
72858778Seric 		  default:
72958755Seric 			bcopy(hp->h_addr,
73058778Seric 				addr.sa.sa_data,
73158755Seric 				hp->h_length);
73258778Seric 			break;
73358778Seric 		}
73429430Sbloom 		i = 1;
7359610Seric 	}
7369610Seric 
7379610Seric 	/*
7389610Seric 	**  Determine the port number.
7399610Seric 	*/
7409610Seric 
74110011Seric 	if (port != 0)
74258755Seric 		port = htons(port);
74310011Seric 	else
7449610Seric 	{
7459610Seric 		register struct servent *sp = getservbyname("smtp", "tcp");
7469610Seric 
7479610Seric 		if (sp == NULL)
7489610Seric 		{
74968745Seric #ifdef LOG
75068745Seric 			if (LogLevel > 2)
75168745Seric 				syslog(LOG_ERR, "makeconnection: service \"smtp\" unknown");
75268745Seric #endif
75365169Seric 			port = htons(25);
7549610Seric 		}
75565169Seric 		else
75665169Seric 			port = sp->s_port;
7579610Seric 	}
7586039Seric 
75958778Seric 	switch (addr.sa.sa_family)
76058755Seric 	{
76159884Seric #ifdef NETINET
76258755Seric 	  case AF_INET:
76358778Seric 		addr.sin.sin_port = port;
76458755Seric 		addrlen = sizeof (struct sockaddr_in);
76558755Seric 		break;
76659884Seric #endif
76758755Seric 
76858755Seric #ifdef NETISO
76958755Seric 	  case AF_ISO:
77058755Seric 		/* assume two byte transport selector */
77158755Seric 		bcopy((char *) &port, TSEL((struct sockaddr_iso *) &addr), 2);
77258755Seric 		addrlen = sizeof (struct sockaddr_iso);
77358755Seric 		break;
77458755Seric #endif
77558755Seric 
77658755Seric 	  default:
77758778Seric 		syserr("Can't connect to address family %d", addr.sa.sa_family);
77858755Seric 		return (EX_NOHOST);
77958755Seric 	}
78058755Seric 
7816039Seric 	/*
7826039Seric 	**  Try to actually open the connection.
7836039Seric 	*/
7846039Seric 
78559156Seric #ifdef XLA
78659156Seric 	/* if too many connections, don't bother trying */
78759156Seric 	if (!xla_noqueue_ok(host))
78859156Seric 		return EX_TEMPFAIL;
78959156Seric #endif
79059156Seric 
79168693Seric 	firstconnect = TRUE;
79257736Seric 	for (;;)
79352106Seric 	{
79457736Seric 		if (tTd(16, 1))
79558755Seric 			printf("makeconnection (%s [%s])\n",
79658755Seric 				host, anynet_ntoa(&addr));
79752106Seric 
79858588Seric 		/* save for logging */
79958588Seric 		CurHostAddr = addr;
80058588Seric 
80157736Seric 		if (usesecureport)
80257736Seric 		{
80357736Seric 			int rport = IPPORT_RESERVED - 1;
8046039Seric 
80557736Seric 			s = rresvport(&rport);
80657736Seric 		}
80757736Seric 		else
80857736Seric 		{
80957736Seric 			s = socket(AF_INET, SOCK_STREAM, 0);
81057736Seric 		}
81157736Seric 		if (s < 0)
81257736Seric 		{
81357736Seric 			sav_errno = errno;
81457736Seric 			syserr("makeconnection: no socket");
81557736Seric 			goto failure;
81657736Seric 		}
81710347Seric 
81864381Seric #ifdef SO_SNDBUF
81964381Seric 		if (TcpSndBufferSize > 0)
82064381Seric 		{
82164381Seric 			if (setsockopt(s, SOL_SOCKET, SO_SNDBUF,
82264561Seric 				       (char *) &TcpSndBufferSize,
82364381Seric 				       sizeof(TcpSndBufferSize)) < 0)
82464381Seric 				syserr("makeconnection: setsockopt(SO_SNDBUF)");
82564381Seric 		}
82664381Seric #endif
82764381Seric 
82857736Seric 		if (tTd(16, 1))
82957736Seric 			printf("makeconnection: fd=%d\n", s);
83057736Seric 
83157736Seric 		/* turn on network debugging? */
83257736Seric 		if (tTd(16, 101))
83357736Seric 		{
83457736Seric 			int on = 1;
83566861Seric 			(void) setsockopt(s, SOL_SOCKET, SO_DEBUG,
83657736Seric 					  (char *)&on, sizeof on);
83757736Seric 		}
83857736Seric 		if (CurEnv->e_xfp != NULL)
83957736Seric 			(void) fflush(CurEnv->e_xfp);		/* for debugging */
84057736Seric 		errno = 0;					/* for debugging */
84158755Seric 		if (connect(s, (struct sockaddr *) &addr, addrlen) >= 0)
84257736Seric 			break;
84357736Seric 
84468693Seric 		/* if running demand-dialed connection, try again */
84568693Seric 		if (DialDelay > 0 && firstconnect)
84668693Seric 		{
84768693Seric 			if (tTd(16, 1))
84868693Seric 				printf("Connect failed (%s); trying again...\n",
84968693Seric 					errstring(sav_errno));
85068693Seric 			firstconnect = FALSE;
85168693Seric 			sleep(DialDelay);
85268693Seric 			continue;
85368693Seric 		}
85468693Seric 
85557736Seric 		/* couldn't connect.... figure out why */
85627744Sbloom 		sav_errno = errno;
85727744Sbloom 		(void) close(s);
85868693Seric 		if (hp != NULL && hp->h_addr_list[i])
85929430Sbloom 		{
86057736Seric 			if (tTd(16, 1))
86158755Seric 				printf("Connect failed (%s); trying new address....\n",
86258755Seric 					errstring(sav_errno));
86358778Seric 			switch (addr.sa.sa_family)
86458778Seric 			{
86558778Seric #ifdef NETINET
86658778Seric 			  case AF_INET:
86758755Seric 				bcopy(hp->h_addr_list[i++],
86858778Seric 				      &addr.sin.sin_addr,
86968693Seric 				      INADDRSZ);
87058778Seric 				break;
87158778Seric #endif
87258778Seric 
87358778Seric 			  default:
87458755Seric 				bcopy(hp->h_addr_list[i++],
87558778Seric 					addr.sa.sa_data,
87652106Seric 					hp->h_length);
87758778Seric 				break;
87858778Seric 			}
87957736Seric 			continue;
88029430Sbloom 		}
88129430Sbloom 
8826039Seric 		/* failure, decide if temporary or not */
8836039Seric 	failure:
88459254Seric #ifdef XLA
88559254Seric 		xla_host_end(host);
88659254Seric #endif
88758542Seric 		if (transienterror(sav_errno))
88858542Seric 			return EX_TEMPFAIL;
88958542Seric 		else
89058542Seric 		{
89158542Seric 			message("%s", errstring(sav_errno));
89258542Seric 			return (EX_UNAVAILABLE);
8936039Seric 		}
8946039Seric 	}
8956039Seric 
8966039Seric 	/* connection ok, put it into canonical form */
89764724Seric 	if ((mci->mci_out = fdopen(s, "w")) == NULL ||
89864724Seric 	    (s = dup(s)) < 0 ||
89964725Seric 	    (mci->mci_in = fdopen(s, "r")) == NULL)
90064724Seric 	{
90164724Seric 		syserr("cannot open SMTP client channel, fd=%d", s);
90264724Seric 		return EX_TEMPFAIL;
90364724Seric 	}
9046039Seric 
90510098Seric 	return (EX_OK);
9066039Seric }
90710758Seric /*
90810758Seric **  MYHOSTNAME -- return the name of this host.
90910758Seric **
91010758Seric **	Parameters:
91110758Seric **		hostbuf -- a place to return the name of this host.
91212313Seric **		size -- the size of hostbuf.
91310758Seric **
91410758Seric **	Returns:
91510758Seric **		A list of aliases for this host.
91610758Seric **
91710758Seric **	Side Effects:
91864338Seric **		Adds numeric codes to $=w.
91910758Seric */
9206039Seric 
92168693Seric struct hostent *
92212313Seric myhostname(hostbuf, size)
92310758Seric 	char hostbuf[];
92412313Seric 	int size;
92510758Seric {
92658110Seric 	register struct hostent *hp;
92768693Seric 	extern bool getcanonname();
92868693Seric 	extern int h_errno;
92910758Seric 
93023120Seric 	if (gethostname(hostbuf, size) < 0)
93123120Seric 	{
93223120Seric 		(void) strcpy(hostbuf, "localhost");
93323120Seric 	}
93468693Seric 	hp = sm_gethostbyname(hostbuf);
93566853Seric 	if (hp == NULL)
93668693Seric 		return NULL;
93768693Seric 	if (strchr(hp->h_name, '.') != NULL || strchr(hostbuf, '.') == NULL)
93867448Seric 	{
93968693Seric 		(void) strncpy(hostbuf, hp->h_name, size - 1);
94068693Seric 		hostbuf[size - 1] = '\0';
94167448Seric 	}
94266853Seric 
94366853Seric #if NAMED_BIND
94468693Seric 	/*
94568693Seric 	**  If still no dot, try DNS directly (i.e., avoid NIS problems).
94668693Seric 	**  This ought to be driven from the configuration file, but
94768693Seric 	**  we are called before the configuration is read.  We could
94868693Seric 	**  check for an /etc/resolv.conf file, but that isn't required.
94968693Seric 	**  All in all, a bit of a mess.
95068693Seric 	*/
95168693Seric 
95268693Seric 	if (strchr(hostbuf, '.') == NULL &&
95368693Seric 	    !getcanonname(hostbuf, size, TRUE) &&
95468693Seric 	    h_errno == TRY_AGAIN)
95568612Seric 	{
95666853Seric 		/* try twice in case name server not yet started up */
95768693Seric 		message("My unqualifed host name (%s) unknown to DNS; sleeping for retry",
95868693Seric 			hostbuf);
95968693Seric 		sleep(60);
96068693Seric 		if (!getcanonname(hostbuf, size, TRUE))
96166853Seric 			errno = h_errno + E_DNSBASE;
96266853Seric 	}
96366777Seric #endif
96468693Seric 	return (hp);
96510758Seric }
96651315Seric /*
96758951Seric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
96858308Seric **
96958951Seric **	Uses RFC1413 protocol to try to get info from the other end.
97058951Seric **
97158308Seric **	Parameters:
97258308Seric **		fd -- the descriptor
97358308Seric **
97458308Seric **	Returns:
97558951Seric **		The user@host information associated with this descriptor.
97658308Seric */
97758308Seric 
97858951Seric static jmp_buf	CtxAuthTimeout;
97958951Seric 
98068693Seric static void
98158951Seric authtimeout()
98258951Seric {
98358951Seric 	longjmp(CtxAuthTimeout, 1);
98458951Seric }
98558951Seric 
98658308Seric char *
98758951Seric getauthinfo(fd)
98858308Seric 	int fd;
98958308Seric {
99058951Seric 	int falen;
99159104Seric 	register char *p;
99258951Seric 	SOCKADDR la;
99358951Seric 	int lalen;
99458951Seric 	register struct servent *sp;
99558951Seric 	int s;
99658951Seric 	int i;
99758951Seric 	EVENT *ev;
99868444Seric 	int nleft;
99968462Seric 	char ibuf[MAXNAME + 1];
100058951Seric 	static char hbuf[MAXNAME * 2 + 2];
100158951Seric 	extern char *hostnamebyanyaddr();
100258951Seric 	extern char RealUserName[];			/* main.c */
100358308Seric 
100466761Seric 	falen = sizeof RealHostAddr;
100568693Seric 	if (isatty(fd) || getpeername(fd, &RealHostAddr.sa, &falen) < 0 ||
100668693Seric 	    falen <= 0 || RealHostAddr.sa.sa_family == 0)
100758951Seric 	{
100858951Seric 		(void) sprintf(hbuf, "%s@localhost", RealUserName);
100958957Seric 		if (tTd(9, 1))
101058951Seric 			printf("getauthinfo: %s\n", hbuf);
101158951Seric 		return hbuf;
101258951Seric 	}
101358951Seric 
101466761Seric 	if (RealHostName == NULL)
101566761Seric 	{
101666761Seric 		/* translate that to a host name */
101766761Seric 		RealHostName = newstr(hostnamebyanyaddr(&RealHostAddr));
101866761Seric 	}
101966761Seric 
102065831Seric 	if (TimeOuts.to_ident == 0)
102165831Seric 		goto noident;
102265831Seric 
102358951Seric 	lalen = sizeof la;
102466761Seric 	if (RealHostAddr.sa.sa_family != AF_INET ||
102558951Seric 	    getsockname(fd, &la.sa, &lalen) < 0 || lalen <= 0 ||
102658951Seric 	    la.sa.sa_family != AF_INET)
102758951Seric 	{
102858951Seric 		/* no ident info */
102958951Seric 		goto noident;
103058951Seric 	}
103158951Seric 
103258951Seric 	/* create ident query */
103368457Seric 	(void) sprintf(ibuf, "%d,%d\r\n",
103466761Seric 		ntohs(RealHostAddr.sin.sin_port), ntohs(la.sin.sin_port));
103558951Seric 
103658951Seric 	/* create local address */
103764747Seric 	la.sin.sin_port = 0;
103858951Seric 
103958951Seric 	/* create foreign address */
104058951Seric 	sp = getservbyname("auth", "tcp");
104158951Seric 	if (sp != NULL)
104266761Seric 		RealHostAddr.sin.sin_port = sp->s_port;
104358308Seric 	else
104466761Seric 		RealHostAddr.sin.sin_port = htons(113);
104558951Seric 
104658951Seric 	s = -1;
104758951Seric 	if (setjmp(CtxAuthTimeout) != 0)
104858951Seric 	{
104958951Seric 		if (s >= 0)
105058951Seric 			(void) close(s);
105158951Seric 		goto noident;
105258951Seric 	}
105358951Seric 
105458951Seric 	/* put a timeout around the whole thing */
105564255Seric 	ev = setevent(TimeOuts.to_ident, authtimeout, 0);
105658951Seric 
105764747Seric 	/* connect to foreign IDENT server using same address as SMTP socket */
105858951Seric 	s = socket(AF_INET, SOCK_STREAM, 0);
105958951Seric 	if (s < 0)
106058951Seric 	{
106158951Seric 		clrevent(ev);
106258951Seric 		goto noident;
106358951Seric 	}
106464747Seric 	if (bind(s, &la.sa, sizeof la.sin) < 0 ||
106566761Seric 	    connect(s, &RealHostAddr.sa, sizeof RealHostAddr.sin) < 0)
106658951Seric 	{
106766011Seric 		goto closeident;
106858951Seric 	}
106958951Seric 
107058957Seric 	if (tTd(9, 10))
107168457Seric 		printf("getauthinfo: sent %s", ibuf);
107258951Seric 
107358951Seric 	/* send query */
107468457Seric 	if (write(s, ibuf, strlen(ibuf)) < 0)
107558951Seric 		goto closeident;
107658951Seric 
107758951Seric 	/* get result */
107868457Seric 	p = &ibuf[0];
107968525Seric 	nleft = sizeof ibuf - 1;
108068444Seric 	while ((i = read(s, p, nleft)) > 0)
108168444Seric 	{
108268444Seric 		p += i;
108368444Seric 		nleft -= i;
108468444Seric 	}
108558951Seric 	(void) close(s);
108658951Seric 	clrevent(ev);
108768457Seric 	if (i < 0 || p == &ibuf[0])
108858951Seric 		goto noident;
108958951Seric 
109068444Seric 	if (*--p == '\n' && *--p == '\r')
109168444Seric 		p--;
109268444Seric 	*++p = '\0';
109368444Seric 
109458957Seric 	if (tTd(9, 3))
109568457Seric 		printf("getauthinfo:  got %s\n", ibuf);
109658951Seric 
109758951Seric 	/* parse result */
109868457Seric 	p = strchr(ibuf, ':');
109958951Seric 	if (p == NULL)
110058951Seric 	{
110158951Seric 		/* malformed response */
110258951Seric 		goto noident;
110358951Seric 	}
110458951Seric 	while (isascii(*++p) && isspace(*p))
110558951Seric 		continue;
110658951Seric 	if (strncasecmp(p, "userid", 6) != 0)
110758951Seric 	{
110858951Seric 		/* presumably an error string */
110958951Seric 		goto noident;
111058951Seric 	}
111158951Seric 	p += 6;
111258951Seric 	while (isascii(*p) && isspace(*p))
111358951Seric 		p++;
111458951Seric 	if (*p++ != ':')
111558951Seric 	{
111658951Seric 		/* either useridxx or malformed response */
111758951Seric 		goto noident;
111858951Seric 	}
111958951Seric 
112058951Seric 	/* p now points to the OSTYPE field */
112168693Seric 	while (isascii(*p) && isspace(*p))
112268693Seric 		p++;
112368693Seric 	if (strncasecmp(p, "other", 5) == 0 &&
112468693Seric 	    (p[5] == ':' || p[5] == ' ' || p[5] == ',' || p[5] == '\0'))
112568693Seric 	{
112668693Seric 		/* not useful information */
112768693Seric 		goto noident;
112868693Seric 	}
112958951Seric 	p = strchr(p, ':');
113058951Seric 	if (p == NULL)
113158951Seric 	{
113258951Seric 		/* malformed response */
113358951Seric 		goto noident;
113458951Seric 	}
113558951Seric 
113658957Seric 	/* 1413 says don't do this -- but it's broken otherwise */
113758957Seric 	while (isascii(*++p) && isspace(*p))
113858957Seric 		continue;
113958957Seric 
114067935Seric 	/* p now points to the authenticated name -- copy carefully */
114168457Seric 	cleanstrcpy(hbuf, p, MAXNAME);
114267935Seric 	hbuf[i++] = '@';
114367935Seric 	strcpy(&hbuf[i], RealHostName == NULL ? "localhost" : RealHostName);
114458957Seric 	goto finish;
114558957Seric 
114666011Seric closeident:
114766011Seric 	(void) close(s);
114866011Seric 	clrevent(ev);
114966011Seric 
115058957Seric noident:
115166003Seric 	if (RealHostName == NULL)
115266003Seric 	{
115366003Seric 		if (tTd(9, 1))
115466003Seric 			printf("getauthinfo: NULL\n");
115566003Seric 		return NULL;
115666003Seric 	}
115758957Seric 	(void) strcpy(hbuf, RealHostName);
115858957Seric 
115958957Seric finish:
116066003Seric 	if (RealHostName != NULL && RealHostName[0] != '[')
116158951Seric 	{
116258951Seric 		p = &hbuf[strlen(hbuf)];
116358951Seric 		(void) sprintf(p, " [%s]", anynet_ntoa(&RealHostAddr));
116458951Seric 	}
116558957Seric 	if (tTd(9, 1))
116658951Seric 		printf("getauthinfo: %s\n", hbuf);
116758308Seric 	return hbuf;
116858308Seric }
116958308Seric /*
117060089Seric **  HOST_MAP_LOOKUP -- turn a hostname into canonical form
117153751Seric **
117253751Seric **	Parameters:
117356823Seric **		map -- a pointer to this map (unused).
117460089Seric **		name -- the (presumably unqualified) hostname.
117560257Seric **		av -- unused -- for compatibility with other mapping
117655019Seric **			functions.
117759084Seric **		statp -- an exit status (out parameter) -- set to
117859084Seric **			EX_TEMPFAIL if the name server is unavailable.
117953751Seric **
118053751Seric **	Returns:
118153751Seric **		The mapping, if found.
118253751Seric **		NULL if no mapping found.
118353751Seric **
118453751Seric **	Side Effects:
118553751Seric **		Looks up the host specified in hbuf.  If it is not
118653751Seric **		the canonical name for that host, return the canonical
118753751Seric **		name.
118853751Seric */
118951315Seric 
119053751Seric char *
119160257Seric host_map_lookup(map, name, av, statp)
119256823Seric 	MAP *map;
119360089Seric 	char *name;
119460257Seric 	char **av;
119559084Seric 	int *statp;
119616911Seric {
119716911Seric 	register struct hostent *hp;
119868693Seric 	struct in_addr in_addr;
119956823Seric 	char *cp;
120059671Seric 	register STAB *s;
120168693Seric 	char hbuf[MAXNAME + 1];
120266334Seric #if NAMED_BIND
120359671Seric 	extern int h_errno;
120466029Seric #endif
120516911Seric 
120625574Smiriam 	/*
120759671Seric 	**  See if we have already looked up this name.  If so, just
120859671Seric 	**  return it.
120959671Seric 	*/
121053751Seric 
121160089Seric 	s = stab(name, ST_NAMECANON, ST_ENTER);
121259671Seric 	if (bitset(NCF_VALID, s->s_namecanon.nc_flags))
121359671Seric 	{
121459986Seric 		if (tTd(9, 1))
121560089Seric 			printf("host_map_lookup(%s) => CACHE %s\n",
121660089Seric 				name, s->s_namecanon.nc_cname);
121759671Seric 		errno = s->s_namecanon.nc_errno;
121866334Seric #if NAMED_BIND
121959671Seric 		h_errno = s->s_namecanon.nc_herrno;
122066029Seric #endif
122159671Seric 		*statp = s->s_namecanon.nc_stat;
122268817Seric 		if (*statp == EX_TEMPFAIL)
122365199Seric 		{
1224*68857Seric 			CurEnv->e_status = "4.4.3";
122568817Seric 			usrerr("451 %s: Name server timeout",
122665199Seric 				shortenstring(name, 33));
122765199Seric 		}
122859671Seric 		return s->s_namecanon.nc_cname;
122959671Seric 	}
123059671Seric 
123159671Seric 	/*
123259671Seric 	**  If first character is a bracket, then it is an address
123359671Seric 	**  lookup.  Address is copied into a temporary buffer to
123460089Seric 	**  strip the brackets and to preserve name if address is
123559671Seric 	**  unknown.
123659671Seric 	*/
123759671Seric 
123860089Seric 	if (*name != '[')
123953751Seric 	{
124055019Seric 		extern bool getcanonname();
124155019Seric 
124258798Seric 		if (tTd(9, 1))
124360089Seric 			printf("host_map_lookup(%s) => ", name);
124459671Seric 		s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
124568693Seric 		if (strlen(name) < sizeof hbuf)
124668693Seric 			(void) strcpy(hbuf, name);
124768693Seric 		else
124868693Seric 		{
124968693Seric 			bcopy(name, hbuf, sizeof hbuf - 1);
125068693Seric 			hbuf[sizeof hbuf - 1] = '\0';
125168693Seric 		}
125268759Seric 		if (getcanonname(hbuf, sizeof hbuf - 1, !NoMXforCanon))
125358796Seric 		{
125458796Seric 			if (tTd(9, 1))
125558796Seric 				printf("%s\n", hbuf);
125660257Seric 			cp = map_rewrite(map, hbuf, strlen(hbuf), av);
125760257Seric 			s->s_namecanon.nc_cname = newstr(cp);
125860257Seric 			return cp;
125958796Seric 		}
126053751Seric 		else
126158796Seric 		{
126259084Seric 			register struct hostent *hp;
126359084Seric 
126466029Seric 			s->s_namecanon.nc_errno = errno;
126566334Seric #if NAMED_BIND
126666029Seric 			s->s_namecanon.nc_herrno = h_errno;
126758796Seric 			if (tTd(9, 1))
126859084Seric 				printf("FAIL (%d)\n", h_errno);
126959084Seric 			switch (h_errno)
127059084Seric 			{
127159084Seric 			  case TRY_AGAIN:
127259596Seric 				if (UseNameServer)
127359734Seric 				{
1274*68857Seric 					CurEnv->e_status = "4.4.3";
127568817Seric 					usrerr("451 %s: Name server timeout",
127665199Seric 						shortenstring(name, 33));
127759734Seric 				}
127859084Seric 				*statp = EX_TEMPFAIL;
127959084Seric 				break;
128059084Seric 
128159084Seric 			  case HOST_NOT_FOUND:
128259084Seric 				*statp = EX_NOHOST;
128359084Seric 				break;
128459084Seric 
128559084Seric 			  case NO_RECOVERY:
128659084Seric 				*statp = EX_SOFTWARE;
128759084Seric 				break;
128859084Seric 
128959084Seric 			  default:
129059084Seric 				*statp = EX_UNAVAILABLE;
129159084Seric 				break;
129259084Seric 			}
129366029Seric #else
129466029Seric 			if (tTd(9, 1))
129566029Seric 				printf("FAIL\n");
129666029Seric 			*statp = EX_NOHOST;
129766029Seric #endif
129859671Seric 			s->s_namecanon.nc_stat = *statp;
129968693Seric 			if ((*statp != EX_TEMPFAIL && *statp != EX_NOHOST) ||
130068693Seric 			    UseNameServer)
130159084Seric 				return NULL;
130259084Seric 
130359084Seric 			/*
130459084Seric 			**  Try to look it up in /etc/hosts
130559084Seric 			*/
130659084Seric 
130768693Seric 			hp = sm_gethostbyname(name);
130859084Seric 			if (hp == NULL)
130959084Seric 			{
131059084Seric 				/* no dice there either */
131159671Seric 				s->s_namecanon.nc_stat = *statp = EX_NOHOST;
131259084Seric 				return NULL;
131359084Seric 			}
131459084Seric 
131559671Seric 			s->s_namecanon.nc_stat = *statp = EX_OK;
131660257Seric 			cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
131760257Seric 			s->s_namecanon.nc_cname = newstr(cp);
131860257Seric 			return cp;
131958796Seric 		}
132053751Seric 	}
132160089Seric 	if ((cp = strchr(name, ']')) == NULL)
132253751Seric 		return (NULL);
132340994Sbostic 	*cp = '\0';
132468693Seric 	in_addr.s_addr = inet_addr(&name[1]);
132558110Seric 
132658110Seric 	/* nope -- ask the name server */
132768693Seric 	hp = sm_gethostbyaddr((char *)&in_addr, INADDRSZ, AF_INET);
132859671Seric 	s->s_namecanon.nc_errno = errno;
132966334Seric #if NAMED_BIND
133059671Seric 	s->s_namecanon.nc_herrno = h_errno;
133166029Seric #endif
133259671Seric 	s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
133333932Sbostic 	if (hp == NULL)
133459671Seric 	{
133559671Seric 		s->s_namecanon.nc_stat = *statp = EX_NOHOST;
133653751Seric 		return (NULL);
133759671Seric 	}
133853751Seric 
133958110Seric 	/* found a match -- copy out */
134060257Seric 	cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
134159671Seric 	s->s_namecanon.nc_stat = *statp = EX_OK;
134260257Seric 	s->s_namecanon.nc_cname = newstr(cp);
134360257Seric 	return cp;
134433932Sbostic }
134558755Seric /*
134658755Seric **  ANYNET_NTOA -- convert a network address to printable form.
134758755Seric **
134858755Seric **	Parameters:
134958755Seric **		sap -- a pointer to a sockaddr structure.
135058755Seric **
135158755Seric **	Returns:
135258755Seric **		A printable version of that sockaddr.
135358755Seric */
135416911Seric 
135558755Seric char *
135658755Seric anynet_ntoa(sap)
135758755Seric 	register SOCKADDR *sap;
135858755Seric {
135958755Seric 	register char *bp;
136058755Seric 	register char *ap;
136158755Seric 	int l;
136264734Seric 	static char buf[100];
136358755Seric 
136458798Seric 	/* check for null/zero family */
136558798Seric 	if (sap == NULL)
136658798Seric 		return "NULLADDR";
136758798Seric 	if (sap->sa.sa_family == 0)
136858798Seric 		return "0";
136958798Seric 
137064734Seric 	switch (sap->sa.sa_family)
137164734Seric 	{
137264821Seric #ifdef NETUNIX
137364734Seric 	  case AF_UNIX:
137464758Seric 	  	if (sap->sunix.sun_path[0] != '\0')
137564758Seric 	  		sprintf(buf, "[UNIX: %.64s]", sap->sunix.sun_path);
137664734Seric 	  	else
137764734Seric 	  		sprintf(buf, "[UNIX: localhost]");
137864734Seric 		return buf;
137964734Seric #endif
138064734Seric 
138158778Seric #ifdef NETINET
138264734Seric 	  case AF_INET:
138368776Seric 		return inet_ntoa(sap->sin.sin_addr);
138458778Seric #endif
138558755Seric 
138664734Seric 	  default:
138764734Seric 	  	/* this case is only to ensure syntactic correctness */
138864734Seric 	  	break;
138964734Seric 	}
139064734Seric 
139158755Seric 	/* unknown family -- just dump bytes */
139258778Seric 	(void) sprintf(buf, "Family %d: ", sap->sa.sa_family);
139358755Seric 	bp = &buf[strlen(buf)];
139458778Seric 	ap = sap->sa.sa_data;
139558778Seric 	for (l = sizeof sap->sa.sa_data; --l >= 0; )
139658755Seric 	{
139758755Seric 		(void) sprintf(bp, "%02x:", *ap++ & 0377);
139858755Seric 		bp += 3;
139958755Seric 	}
140058755Seric 	*--bp = '\0';
140158755Seric 	return buf;
140258755Seric }
140358951Seric /*
140458951Seric **  HOSTNAMEBYANYADDR -- return name of host based on address
140558951Seric **
140658951Seric **	Parameters:
140758951Seric **		sap -- SOCKADDR pointer
140858951Seric **
140958951Seric **	Returns:
141058951Seric **		text representation of host name.
141158951Seric **
141258951Seric **	Side Effects:
141358951Seric **		none.
141458951Seric */
141558755Seric 
141658951Seric char *
141758951Seric hostnamebyanyaddr(sap)
141858951Seric 	register SOCKADDR *sap;
141958951Seric {
142058951Seric 	register struct hostent *hp;
142164734Seric 	int saveretry;
142258951Seric 
142366334Seric #if NAMED_BIND
142459042Seric 	/* shorten name server timeout to avoid higher level timeouts */
142559042Seric 	saveretry = _res.retry;
142659042Seric 	_res.retry = 3;
142759042Seric #endif /* NAMED_BIND */
142859042Seric 
142958951Seric 	switch (sap->sa.sa_family)
143058951Seric 	{
143158951Seric #ifdef NETINET
143258951Seric 	  case AF_INET:
143368693Seric 		hp = sm_gethostbyaddr((char *) &sap->sin.sin_addr,
143468693Seric 			INADDRSZ,
143558951Seric 			AF_INET);
143658951Seric 		break;
143758951Seric #endif
143858951Seric 
143958951Seric #ifdef NETISO
144058951Seric 	  case AF_ISO:
144168693Seric 		hp = sm_gethostbyaddr((char *) &sap->siso.siso_addr,
144258951Seric 			sizeof sap->siso.siso_addr,
144358951Seric 			AF_ISO);
144458951Seric 		break;
144558951Seric #endif
144658951Seric 
144764734Seric 	  case AF_UNIX:
144864734Seric 		hp = NULL;
144964734Seric 		break;
145064734Seric 
145158951Seric 	  default:
145268693Seric 		hp = sm_gethostbyaddr(sap->sa.sa_data,
145358951Seric 			   sizeof sap->sa.sa_data,
145458951Seric 			   sap->sa.sa_family);
145558951Seric 		break;
145658951Seric 	}
145758951Seric 
145866334Seric #if NAMED_BIND
145959042Seric 	_res.retry = saveretry;
146059042Seric #endif /* NAMED_BIND */
146159042Seric 
146258951Seric 	if (hp != NULL)
146358951Seric 		return hp->h_name;
146458951Seric 	else
146558951Seric 	{
146658951Seric 		/* produce a dotted quad */
146758951Seric 		static char buf[512];
146858951Seric 
146958951Seric 		(void) sprintf(buf, "[%s]", anynet_ntoa(sap));
147058951Seric 		return buf;
147158951Seric 	}
147258951Seric }
147358951Seric 
147456795Seric # else /* DAEMON */
147516911Seric /* code for systems without sophisticated networking */
147610758Seric 
147710758Seric /*
147810758Seric **  MYHOSTNAME -- stub version for case of no daemon code.
147911297Seric **
148011297Seric **	Can't convert to upper case here because might be a UUCP name.
148112313Seric **
148212313Seric **	Mark, you can change this to be anything you want......
148310758Seric */
148410758Seric 
148510758Seric char **
148612313Seric myhostname(hostbuf, size)
148710758Seric 	char hostbuf[];
148812313Seric 	int size;
148910758Seric {
149010758Seric 	register FILE *f;
149110758Seric 
149210758Seric 	hostbuf[0] = '\0';
149310758Seric 	f = fopen("/usr/include/whoami", "r");
149410758Seric 	if (f != NULL)
149510758Seric 	{
149612313Seric 		(void) fgets(hostbuf, size, f);
149710758Seric 		fixcrlf(hostbuf, TRUE);
149810758Seric 		(void) fclose(f);
149910758Seric 	}
150010758Seric 	return (NULL);
150110758Seric }
150216911Seric /*
150358951Seric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
150458308Seric **
150558308Seric **	Parameters:
150658308Seric **		fd -- the descriptor
150758308Seric **
150858308Seric **	Returns:
150958308Seric **		The host name associated with this descriptor, if it can
151058308Seric **			be determined.
151158308Seric **		NULL otherwise.
151258308Seric **
151358308Seric **	Side Effects:
151458308Seric **		none
151558308Seric */
151658308Seric 
151758308Seric char *
151858951Seric getauthinfo(fd)
151958308Seric 	int fd;
152058308Seric {
152158308Seric 	return NULL;
152258308Seric }
152358308Seric /*
152416911Seric **  MAPHOSTNAME -- turn a hostname into canonical form
152516911Seric **
152616911Seric **	Parameters:
152756823Seric **		map -- a pointer to the database map.
152860089Seric **		name -- a buffer containing a hostname.
152953751Seric **		avp -- a pointer to a (cf file defined) argument vector.
153059084Seric **		statp -- an exit status (out parameter).
153116911Seric **
153216911Seric **	Returns:
153353751Seric **		mapped host name
153451315Seric **		FALSE otherwise.
153516911Seric **
153616911Seric **	Side Effects:
153760089Seric **		Looks up the host specified in name.  If it is not
153816911Seric **		the canonical name for that host, replace it with
153916911Seric **		the canonical name.  If the name is unknown, or it
154016911Seric **		is already the canonical name, leave it unchanged.
154116911Seric */
154210758Seric 
154316911Seric /*ARGSUSED*/
154453751Seric char *
154560089Seric host_map_lookup(map, name, avp, statp)
154656823Seric 	MAP *map;
154760089Seric 	char *name;
154853751Seric 	char **avp;
154959084Seric 	char *statp;
155016911Seric {
155159084Seric 	register struct hostent *hp;
155259084Seric 
155368693Seric 	hp = sm_gethostbyname(name);
155459084Seric 	if (hp != NULL)
155559084Seric 		return hp->h_name;
155659084Seric 	*statp = EX_NOHOST;
155753751Seric 	return NULL;
155816911Seric }
155916911Seric 
156056795Seric #endif /* DAEMON */
1561