xref: /csrg-svn/usr.sbin/sendmail/src/daemon.c (revision 69397)
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*69397Seric static char sccsid[] = "@(#)daemon.c	8.85 (Berkeley) 05/13/95 (with daemon mode)";
1533780Sbostic #else
16*69397Seric static char sccsid[] = "@(#)daemon.c	8.85 (Berkeley) 05/13/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;
472*69397Seric 		if (isascii(*f) && isupper(*f))
473*69397Seric 			*f = tolower(*f);
47458849Seric 
47558849Seric 		switch (*f)
47658849Seric 		{
47758873Seric 		  case 'F':		/* address family */
47858849Seric 			if (isascii(*v) && isdigit(*v))
47958873Seric 				DaemonAddr.sa.sa_family = atoi(v);
48058873Seric #ifdef NETINET
48158873Seric 			else if (strcasecmp(v, "inet") == 0)
48258873Seric 				DaemonAddr.sa.sa_family = AF_INET;
48358873Seric #endif
48458873Seric #ifdef NETISO
48558873Seric 			else if (strcasecmp(v, "iso") == 0)
48658873Seric 				DaemonAddr.sa.sa_family = AF_ISO;
48758873Seric #endif
48858873Seric #ifdef NETNS
48958873Seric 			else if (strcasecmp(v, "ns") == 0)
49058873Seric 				DaemonAddr.sa.sa_family = AF_NS;
49158873Seric #endif
49258873Seric #ifdef NETX25
49358873Seric 			else if (strcasecmp(v, "x.25") == 0)
49458873Seric 				DaemonAddr.sa.sa_family = AF_CCITT;
49558873Seric #endif
49658849Seric 			else
49758873Seric 				syserr("554 Unknown address family %s in Family=option", v);
49858873Seric 			break;
49958873Seric 
50058873Seric 		  case 'A':		/* address */
50158873Seric 			switch (DaemonAddr.sa.sa_family)
50258849Seric 			{
50358873Seric #ifdef NETINET
50458873Seric 			  case AF_INET:
50558873Seric 				if (isascii(*v) && isdigit(*v))
50668693Seric 					DaemonAddr.sin.sin_addr.s_addr = htonl(inet_network(v));
50758873Seric 				else
50858873Seric 				{
50958873Seric 					register struct netent *np;
51058849Seric 
51158873Seric 					np = getnetbyname(v);
51258873Seric 					if (np == NULL)
51358873Seric 						syserr("554 network \"%s\" unknown", v);
51458873Seric 					else
51558873Seric 						DaemonAddr.sin.sin_addr.s_addr = np->n_net;
51658873Seric 				}
51758873Seric 				break;
51858873Seric #endif
51958873Seric 
52058873Seric 			  default:
52158873Seric 				syserr("554 Address= option unsupported for family %d",
52258873Seric 					DaemonAddr.sa.sa_family);
52358873Seric 				break;
52458849Seric 			}
52558849Seric 			break;
52658849Seric 
52758873Seric 		  case 'P':		/* port */
52858873Seric 			switch (DaemonAddr.sa.sa_family)
52958849Seric 			{
53058873Seric 				short port;
53158849Seric 
53258873Seric #ifdef NETINET
53358873Seric 			  case AF_INET:
53458873Seric 				if (isascii(*v) && isdigit(*v))
53564366Seric 					DaemonAddr.sin.sin_port = htons(atoi(v));
53658849Seric 				else
53758873Seric 				{
53858873Seric 					register struct servent *sp;
53958873Seric 
54058873Seric 					sp = getservbyname(v, "tcp");
54158873Seric 					if (sp == NULL)
54258909Seric 						syserr("554 service \"%s\" unknown", v);
54358873Seric 					else
54458873Seric 						DaemonAddr.sin.sin_port = sp->s_port;
54558873Seric 				}
54658873Seric 				break;
54758873Seric #endif
54858873Seric 
54958873Seric #ifdef NETISO
55058873Seric 			  case AF_ISO:
55158873Seric 				/* assume two byte transport selector */
55258873Seric 				if (isascii(*v) && isdigit(*v))
55364366Seric 					port = htons(atoi(v));
55458873Seric 				else
55558873Seric 				{
55658873Seric 					register struct servent *sp;
55758873Seric 
55858873Seric 					sp = getservbyname(v, "tcp");
55958873Seric 					if (sp == NULL)
56058909Seric 						syserr("554 service \"%s\" unknown", v);
56158873Seric 					else
56258873Seric 						port = sp->s_port;
56358873Seric 				}
56458873Seric 				bcopy((char *) &port, TSEL(&DaemonAddr.siso), 2);
56558873Seric 				break;
56658873Seric #endif
56758873Seric 
56858873Seric 			  default:
56958873Seric 				syserr("554 Port= option unsupported for family %d",
57058873Seric 					DaemonAddr.sa.sa_family);
57158873Seric 				break;
57258849Seric 			}
57358849Seric 			break;
57459783Seric 
57559783Seric 		  case 'L':		/* listen queue size */
57659783Seric 			ListenQueueSize = atoi(v);
57759783Seric 			break;
57864381Seric 
57964381Seric 		  case 'S':		/* send buffer size */
58064381Seric 			TcpSndBufferSize = atoi(v);
58164381Seric 			break;
58264381Seric 
58364381Seric 		  case 'R':		/* receive buffer size */
58464381Seric 			TcpRcvBufferSize = atoi(v);
58564381Seric 			break;
586*69397Seric 
587*69397Seric 		  default:
588*69397Seric 			syserr("554 DaemonPortOptions parameter \"%s\" unknown", f);
58958849Seric 		}
59058849Seric 	}
59158849Seric }
59258849Seric /*
5936039Seric **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
5946039Seric **
5956039Seric **	Parameters:
5966039Seric **		host -- the name of the host.
5976633Seric **		port -- the port number to connect to.
59853739Seric **		mci -- a pointer to the mail connection information
59953739Seric **			structure to be filled in.
60052106Seric **		usesecureport -- if set, use a low numbered (reserved)
60152106Seric **			port to provide some rudimentary authentication.
6026039Seric **
6036039Seric **	Returns:
6046039Seric **		An exit code telling whether the connection could be
6056039Seric **			made and if not why not.
6066039Seric **
6076039Seric **	Side Effects:
6086039Seric **		none.
6096039Seric */
6105978Seric 
61158755Seric SOCKADDR	CurHostAddr;		/* address of current host */
61258305Seric 
61354967Seric int
61453739Seric makeconnection(host, port, mci, usesecureport)
6156039Seric 	char *host;
6167286Seric 	u_short port;
61754967Seric 	register MCI *mci;
61852106Seric 	bool usesecureport;
6196039Seric {
62068693Seric 	register int i = 0;
62168693Seric 	register int s;
62229430Sbloom 	register struct hostent *hp = (struct hostent *)NULL;
62358755Seric 	SOCKADDR addr;
62452106Seric 	int sav_errno;
62558755Seric 	int addrlen;
62668693Seric 	bool firstconnect;
62766334Seric #if NAMED_BIND
62835651Seric 	extern int h_errno;
62935651Seric #endif
6306039Seric 
6316039Seric 	/*
6326039Seric 	**  Set up the address for the mailer.
6339308Seric 	**	Accept "[a.b.c.d]" syntax for host name.
6346039Seric 	*/
6356039Seric 
63666334Seric #if NAMED_BIND
63725475Smiriam 	h_errno = 0;
63835651Seric #endif
63925475Smiriam 	errno = 0;
64058864Seric 	bzero(&CurHostAddr, sizeof CurHostAddr);
64164334Seric 	SmtpPhase = mci->mci_phase = "initial connection";
64258906Seric 	CurHostName = host;
64325475Smiriam 
6449308Seric 	if (host[0] == '[')
6459308Seric 	{
64611147Seric 		long hid;
64756795Seric 		register char *p = strchr(host, ']');
6489308Seric 
64911147Seric 		if (p != NULL)
6509308Seric 		{
65111147Seric 			*p = '\0';
65259884Seric #ifdef NETINET
65311147Seric 			hid = inet_addr(&host[1]);
65458360Seric 			if (hid == -1)
65559884Seric #endif
65658360Seric 			{
65758360Seric 				/* try it as a host name (avoid MX lookup) */
65868693Seric 				hp = sm_gethostbyname(&host[1]);
65966349Seric 				if (hp == NULL && p[-1] == '.')
66066349Seric 				{
66168693Seric #if NAMED_BIND
66268693Seric 					int oldopts = _res.options;
66368693Seric 
66468693Seric 					_res.options &= ~(RES_DEFNAMES|RES_DNSRCH);
66568693Seric #endif
66666349Seric 					p[-1] = '\0';
66768693Seric 					hp = sm_gethostbyname(&host[1]);
66866349Seric 					p[-1] = '.';
66968693Seric #if NAMED_BIND
67068693Seric 					_res.options = oldopts;
67168693Seric #endif
67266349Seric 				}
67358360Seric 				*p = ']';
67458360Seric 				goto gothostent;
67558360Seric 			}
67611147Seric 			*p = ']';
6779308Seric 		}
67858360Seric 		if (p == NULL)
6799308Seric 		{
68058151Seric 			usrerr("553 Invalid numeric domain spec \"%s\"", host);
68168857Seric 			mci->mci_status = "5.1.2";
6829308Seric 			return (EX_NOHOST);
6839308Seric 		}
68459884Seric #ifdef NETINET
68559884Seric 		addr.sin.sin_family = AF_INET;		/*XXX*/
68658778Seric 		addr.sin.sin_addr.s_addr = hid;
68759884Seric #endif
6889308Seric 	}
6899610Seric 	else
6909610Seric 	{
69166349Seric 		register char *p = &host[strlen(host) - 1];
69266349Seric 
69368693Seric 		hp = sm_gethostbyname(host);
69466349Seric 		if (hp == NULL && *p == '.')
69566349Seric 		{
69668693Seric #if NAMED_BIND
69768693Seric 			int oldopts = _res.options;
69868693Seric 
69968693Seric 			_res.options &= ~(RES_DEFNAMES|RES_DNSRCH);
70068693Seric #endif
70166349Seric 			*p = '\0';
70268693Seric 			hp = sm_gethostbyname(host);
70366349Seric 			*p = '.';
70468693Seric #if NAMED_BIND
70568693Seric 			_res.options = oldopts;
70668693Seric #endif
70766349Seric 		}
70858360Seric gothostent:
70925475Smiriam 		if (hp == NULL)
71024945Seric 		{
71166334Seric #if NAMED_BIND
71268693Seric 			/* check for name server timeouts */
71368693Seric 			if (errno == ETIMEDOUT || h_errno == TRY_AGAIN ||
71468693Seric 			    (errno == ECONNREFUSED && UseNameServer))
71568693Seric 			{
71668693Seric 				mci->mci_status = "4.4.3";
71725475Smiriam 				return (EX_TEMPFAIL);
71868693Seric 			}
71935651Seric #endif
72025475Smiriam 			return (EX_NOHOST);
72124945Seric 		}
72258778Seric 		addr.sa.sa_family = hp->h_addrtype;
72358778Seric 		switch (hp->h_addrtype)
72458778Seric 		{
72558778Seric #ifdef NETINET
72658778Seric 		  case AF_INET:
72758755Seric 			bcopy(hp->h_addr,
72858778Seric 				&addr.sin.sin_addr,
72968693Seric 				INADDRSZ);
73058778Seric 			break;
73158778Seric #endif
73258778Seric 
73358778Seric 		  default:
73458755Seric 			bcopy(hp->h_addr,
73558778Seric 				addr.sa.sa_data,
73658755Seric 				hp->h_length);
73758778Seric 			break;
73858778Seric 		}
73929430Sbloom 		i = 1;
7409610Seric 	}
7419610Seric 
7429610Seric 	/*
7439610Seric 	**  Determine the port number.
7449610Seric 	*/
7459610Seric 
74610011Seric 	if (port != 0)
74758755Seric 		port = htons(port);
74810011Seric 	else
7499610Seric 	{
7509610Seric 		register struct servent *sp = getservbyname("smtp", "tcp");
7519610Seric 
7529610Seric 		if (sp == NULL)
7539610Seric 		{
75468745Seric #ifdef LOG
75568745Seric 			if (LogLevel > 2)
75668745Seric 				syslog(LOG_ERR, "makeconnection: service \"smtp\" unknown");
75768745Seric #endif
75865169Seric 			port = htons(25);
7599610Seric 		}
76065169Seric 		else
76165169Seric 			port = sp->s_port;
7629610Seric 	}
7636039Seric 
76458778Seric 	switch (addr.sa.sa_family)
76558755Seric 	{
76659884Seric #ifdef NETINET
76758755Seric 	  case AF_INET:
76858778Seric 		addr.sin.sin_port = port;
76958755Seric 		addrlen = sizeof (struct sockaddr_in);
77058755Seric 		break;
77159884Seric #endif
77258755Seric 
77358755Seric #ifdef NETISO
77458755Seric 	  case AF_ISO:
77558755Seric 		/* assume two byte transport selector */
77658755Seric 		bcopy((char *) &port, TSEL((struct sockaddr_iso *) &addr), 2);
77758755Seric 		addrlen = sizeof (struct sockaddr_iso);
77858755Seric 		break;
77958755Seric #endif
78058755Seric 
78158755Seric 	  default:
78258778Seric 		syserr("Can't connect to address family %d", addr.sa.sa_family);
78358755Seric 		return (EX_NOHOST);
78458755Seric 	}
78558755Seric 
7866039Seric 	/*
7876039Seric 	**  Try to actually open the connection.
7886039Seric 	*/
7896039Seric 
79059156Seric #ifdef XLA
79159156Seric 	/* if too many connections, don't bother trying */
79259156Seric 	if (!xla_noqueue_ok(host))
79359156Seric 		return EX_TEMPFAIL;
79459156Seric #endif
79559156Seric 
79668693Seric 	firstconnect = TRUE;
79757736Seric 	for (;;)
79852106Seric 	{
79957736Seric 		if (tTd(16, 1))
80058755Seric 			printf("makeconnection (%s [%s])\n",
80158755Seric 				host, anynet_ntoa(&addr));
80252106Seric 
80358588Seric 		/* save for logging */
80458588Seric 		CurHostAddr = addr;
80558588Seric 
80657736Seric 		if (usesecureport)
80757736Seric 		{
80857736Seric 			int rport = IPPORT_RESERVED - 1;
8096039Seric 
81057736Seric 			s = rresvport(&rport);
81157736Seric 		}
81257736Seric 		else
81357736Seric 		{
81457736Seric 			s = socket(AF_INET, SOCK_STREAM, 0);
81557736Seric 		}
81657736Seric 		if (s < 0)
81757736Seric 		{
81857736Seric 			sav_errno = errno;
81957736Seric 			syserr("makeconnection: no socket");
82057736Seric 			goto failure;
82157736Seric 		}
82210347Seric 
82364381Seric #ifdef SO_SNDBUF
82464381Seric 		if (TcpSndBufferSize > 0)
82564381Seric 		{
82664381Seric 			if (setsockopt(s, SOL_SOCKET, SO_SNDBUF,
82764561Seric 				       (char *) &TcpSndBufferSize,
82864381Seric 				       sizeof(TcpSndBufferSize)) < 0)
82964381Seric 				syserr("makeconnection: setsockopt(SO_SNDBUF)");
83064381Seric 		}
83164381Seric #endif
83264381Seric 
83357736Seric 		if (tTd(16, 1))
83457736Seric 			printf("makeconnection: fd=%d\n", s);
83557736Seric 
83657736Seric 		/* turn on network debugging? */
83757736Seric 		if (tTd(16, 101))
83857736Seric 		{
83957736Seric 			int on = 1;
84066861Seric 			(void) setsockopt(s, SOL_SOCKET, SO_DEBUG,
84157736Seric 					  (char *)&on, sizeof on);
84257736Seric 		}
84357736Seric 		if (CurEnv->e_xfp != NULL)
84457736Seric 			(void) fflush(CurEnv->e_xfp);		/* for debugging */
84557736Seric 		errno = 0;					/* for debugging */
84658755Seric 		if (connect(s, (struct sockaddr *) &addr, addrlen) >= 0)
84757736Seric 			break;
84857736Seric 
84968693Seric 		/* if running demand-dialed connection, try again */
85068693Seric 		if (DialDelay > 0 && firstconnect)
85168693Seric 		{
85268693Seric 			if (tTd(16, 1))
85368693Seric 				printf("Connect failed (%s); trying again...\n",
85468693Seric 					errstring(sav_errno));
85568693Seric 			firstconnect = FALSE;
85668693Seric 			sleep(DialDelay);
85768693Seric 			continue;
85868693Seric 		}
85968693Seric 
86057736Seric 		/* couldn't connect.... figure out why */
86127744Sbloom 		sav_errno = errno;
86227744Sbloom 		(void) close(s);
86368693Seric 		if (hp != NULL && hp->h_addr_list[i])
86429430Sbloom 		{
86557736Seric 			if (tTd(16, 1))
86658755Seric 				printf("Connect failed (%s); trying new address....\n",
86758755Seric 					errstring(sav_errno));
86858778Seric 			switch (addr.sa.sa_family)
86958778Seric 			{
87058778Seric #ifdef NETINET
87158778Seric 			  case AF_INET:
87258755Seric 				bcopy(hp->h_addr_list[i++],
87358778Seric 				      &addr.sin.sin_addr,
87468693Seric 				      INADDRSZ);
87558778Seric 				break;
87658778Seric #endif
87758778Seric 
87858778Seric 			  default:
87958755Seric 				bcopy(hp->h_addr_list[i++],
88058778Seric 					addr.sa.sa_data,
88152106Seric 					hp->h_length);
88258778Seric 				break;
88358778Seric 			}
88457736Seric 			continue;
88529430Sbloom 		}
88629430Sbloom 
8876039Seric 		/* failure, decide if temporary or not */
8886039Seric 	failure:
88959254Seric #ifdef XLA
89059254Seric 		xla_host_end(host);
89159254Seric #endif
89258542Seric 		if (transienterror(sav_errno))
89358542Seric 			return EX_TEMPFAIL;
89458542Seric 		else
89558542Seric 		{
89658542Seric 			message("%s", errstring(sav_errno));
89758542Seric 			return (EX_UNAVAILABLE);
8986039Seric 		}
8996039Seric 	}
9006039Seric 
9016039Seric 	/* connection ok, put it into canonical form */
90264724Seric 	if ((mci->mci_out = fdopen(s, "w")) == NULL ||
90364724Seric 	    (s = dup(s)) < 0 ||
90464725Seric 	    (mci->mci_in = fdopen(s, "r")) == NULL)
90564724Seric 	{
90664724Seric 		syserr("cannot open SMTP client channel, fd=%d", s);
90764724Seric 		return EX_TEMPFAIL;
90864724Seric 	}
9096039Seric 
91010098Seric 	return (EX_OK);
9116039Seric }
91210758Seric /*
91310758Seric **  MYHOSTNAME -- return the name of this host.
91410758Seric **
91510758Seric **	Parameters:
91610758Seric **		hostbuf -- a place to return the name of this host.
91712313Seric **		size -- the size of hostbuf.
91810758Seric **
91910758Seric **	Returns:
92010758Seric **		A list of aliases for this host.
92110758Seric **
92210758Seric **	Side Effects:
92364338Seric **		Adds numeric codes to $=w.
92410758Seric */
9256039Seric 
92668693Seric struct hostent *
92712313Seric myhostname(hostbuf, size)
92810758Seric 	char hostbuf[];
92912313Seric 	int size;
93010758Seric {
93158110Seric 	register struct hostent *hp;
93268693Seric 	extern bool getcanonname();
93368693Seric 	extern int h_errno;
93410758Seric 
93523120Seric 	if (gethostname(hostbuf, size) < 0)
93623120Seric 	{
93723120Seric 		(void) strcpy(hostbuf, "localhost");
93823120Seric 	}
93968693Seric 	hp = sm_gethostbyname(hostbuf);
94066853Seric 	if (hp == NULL)
94168693Seric 		return NULL;
94268693Seric 	if (strchr(hp->h_name, '.') != NULL || strchr(hostbuf, '.') == NULL)
94367448Seric 	{
94468693Seric 		(void) strncpy(hostbuf, hp->h_name, size - 1);
94568693Seric 		hostbuf[size - 1] = '\0';
94667448Seric 	}
94766853Seric 
94866853Seric #if NAMED_BIND
94968693Seric 	/*
95068693Seric 	**  If still no dot, try DNS directly (i.e., avoid NIS problems).
95168693Seric 	**  This ought to be driven from the configuration file, but
95268693Seric 	**  we are called before the configuration is read.  We could
95368693Seric 	**  check for an /etc/resolv.conf file, but that isn't required.
95468693Seric 	**  All in all, a bit of a mess.
95568693Seric 	*/
95668693Seric 
95768693Seric 	if (strchr(hostbuf, '.') == NULL &&
95868693Seric 	    !getcanonname(hostbuf, size, TRUE) &&
95968693Seric 	    h_errno == TRY_AGAIN)
96068612Seric 	{
96166853Seric 		/* try twice in case name server not yet started up */
96268693Seric 		message("My unqualifed host name (%s) unknown to DNS; sleeping for retry",
96368693Seric 			hostbuf);
96468693Seric 		sleep(60);
96568693Seric 		if (!getcanonname(hostbuf, size, TRUE))
96666853Seric 			errno = h_errno + E_DNSBASE;
96766853Seric 	}
96866777Seric #endif
96968693Seric 	return (hp);
97010758Seric }
97151315Seric /*
97258951Seric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
97358308Seric **
97458951Seric **	Uses RFC1413 protocol to try to get info from the other end.
97558951Seric **
97658308Seric **	Parameters:
97758308Seric **		fd -- the descriptor
97858308Seric **
97958308Seric **	Returns:
98058951Seric **		The user@host information associated with this descriptor.
98158308Seric */
98258308Seric 
98358951Seric static jmp_buf	CtxAuthTimeout;
98458951Seric 
98568693Seric static void
98658951Seric authtimeout()
98758951Seric {
98858951Seric 	longjmp(CtxAuthTimeout, 1);
98958951Seric }
99058951Seric 
99158308Seric char *
99258951Seric getauthinfo(fd)
99358308Seric 	int fd;
99458308Seric {
99558951Seric 	int falen;
99659104Seric 	register char *p;
99758951Seric 	SOCKADDR la;
99858951Seric 	int lalen;
99958951Seric 	register struct servent *sp;
100058951Seric 	int s;
100158951Seric 	int i;
100258951Seric 	EVENT *ev;
100368444Seric 	int nleft;
100468462Seric 	char ibuf[MAXNAME + 1];
100558951Seric 	static char hbuf[MAXNAME * 2 + 2];
100658951Seric 	extern char *hostnamebyanyaddr();
100758951Seric 	extern char RealUserName[];			/* main.c */
100858308Seric 
100966761Seric 	falen = sizeof RealHostAddr;
101068693Seric 	if (isatty(fd) || getpeername(fd, &RealHostAddr.sa, &falen) < 0 ||
101168693Seric 	    falen <= 0 || RealHostAddr.sa.sa_family == 0)
101258951Seric 	{
101358951Seric 		(void) sprintf(hbuf, "%s@localhost", RealUserName);
101458957Seric 		if (tTd(9, 1))
101558951Seric 			printf("getauthinfo: %s\n", hbuf);
101658951Seric 		return hbuf;
101758951Seric 	}
101858951Seric 
101966761Seric 	if (RealHostName == NULL)
102066761Seric 	{
102166761Seric 		/* translate that to a host name */
102266761Seric 		RealHostName = newstr(hostnamebyanyaddr(&RealHostAddr));
102366761Seric 	}
102466761Seric 
102565831Seric 	if (TimeOuts.to_ident == 0)
102665831Seric 		goto noident;
102765831Seric 
102858951Seric 	lalen = sizeof la;
102966761Seric 	if (RealHostAddr.sa.sa_family != AF_INET ||
103058951Seric 	    getsockname(fd, &la.sa, &lalen) < 0 || lalen <= 0 ||
103158951Seric 	    la.sa.sa_family != AF_INET)
103258951Seric 	{
103358951Seric 		/* no ident info */
103458951Seric 		goto noident;
103558951Seric 	}
103658951Seric 
103758951Seric 	/* create ident query */
103868457Seric 	(void) sprintf(ibuf, "%d,%d\r\n",
103966761Seric 		ntohs(RealHostAddr.sin.sin_port), ntohs(la.sin.sin_port));
104058951Seric 
104158951Seric 	/* create local address */
104264747Seric 	la.sin.sin_port = 0;
104358951Seric 
104458951Seric 	/* create foreign address */
104558951Seric 	sp = getservbyname("auth", "tcp");
104658951Seric 	if (sp != NULL)
104766761Seric 		RealHostAddr.sin.sin_port = sp->s_port;
104858308Seric 	else
104966761Seric 		RealHostAddr.sin.sin_port = htons(113);
105058951Seric 
105158951Seric 	s = -1;
105258951Seric 	if (setjmp(CtxAuthTimeout) != 0)
105358951Seric 	{
105458951Seric 		if (s >= 0)
105558951Seric 			(void) close(s);
105658951Seric 		goto noident;
105758951Seric 	}
105858951Seric 
105958951Seric 	/* put a timeout around the whole thing */
106064255Seric 	ev = setevent(TimeOuts.to_ident, authtimeout, 0);
106158951Seric 
106264747Seric 	/* connect to foreign IDENT server using same address as SMTP socket */
106358951Seric 	s = socket(AF_INET, SOCK_STREAM, 0);
106458951Seric 	if (s < 0)
106558951Seric 	{
106658951Seric 		clrevent(ev);
106758951Seric 		goto noident;
106858951Seric 	}
106964747Seric 	if (bind(s, &la.sa, sizeof la.sin) < 0 ||
107066761Seric 	    connect(s, &RealHostAddr.sa, sizeof RealHostAddr.sin) < 0)
107158951Seric 	{
107266011Seric 		goto closeident;
107358951Seric 	}
107458951Seric 
107558957Seric 	if (tTd(9, 10))
107668457Seric 		printf("getauthinfo: sent %s", ibuf);
107758951Seric 
107858951Seric 	/* send query */
107968457Seric 	if (write(s, ibuf, strlen(ibuf)) < 0)
108058951Seric 		goto closeident;
108158951Seric 
108258951Seric 	/* get result */
108368457Seric 	p = &ibuf[0];
108468525Seric 	nleft = sizeof ibuf - 1;
108568444Seric 	while ((i = read(s, p, nleft)) > 0)
108668444Seric 	{
108768444Seric 		p += i;
108868444Seric 		nleft -= i;
108968444Seric 	}
109058951Seric 	(void) close(s);
109158951Seric 	clrevent(ev);
109268457Seric 	if (i < 0 || p == &ibuf[0])
109358951Seric 		goto noident;
109458951Seric 
109568444Seric 	if (*--p == '\n' && *--p == '\r')
109668444Seric 		p--;
109768444Seric 	*++p = '\0';
109868444Seric 
109958957Seric 	if (tTd(9, 3))
110068457Seric 		printf("getauthinfo:  got %s\n", ibuf);
110158951Seric 
110258951Seric 	/* parse result */
110368457Seric 	p = strchr(ibuf, ':');
110458951Seric 	if (p == NULL)
110558951Seric 	{
110658951Seric 		/* malformed response */
110758951Seric 		goto noident;
110858951Seric 	}
110958951Seric 	while (isascii(*++p) && isspace(*p))
111058951Seric 		continue;
111158951Seric 	if (strncasecmp(p, "userid", 6) != 0)
111258951Seric 	{
111358951Seric 		/* presumably an error string */
111458951Seric 		goto noident;
111558951Seric 	}
111658951Seric 	p += 6;
111758951Seric 	while (isascii(*p) && isspace(*p))
111858951Seric 		p++;
111958951Seric 	if (*p++ != ':')
112058951Seric 	{
112158951Seric 		/* either useridxx or malformed response */
112258951Seric 		goto noident;
112358951Seric 	}
112458951Seric 
112558951Seric 	/* p now points to the OSTYPE field */
112668693Seric 	while (isascii(*p) && isspace(*p))
112768693Seric 		p++;
112868693Seric 	if (strncasecmp(p, "other", 5) == 0 &&
112968693Seric 	    (p[5] == ':' || p[5] == ' ' || p[5] == ',' || p[5] == '\0'))
113068693Seric 	{
113168693Seric 		/* not useful information */
113268693Seric 		goto noident;
113368693Seric 	}
113458951Seric 	p = strchr(p, ':');
113558951Seric 	if (p == NULL)
113658951Seric 	{
113758951Seric 		/* malformed response */
113858951Seric 		goto noident;
113958951Seric 	}
114058951Seric 
114158957Seric 	/* 1413 says don't do this -- but it's broken otherwise */
114258957Seric 	while (isascii(*++p) && isspace(*p))
114358957Seric 		continue;
114458957Seric 
114567935Seric 	/* p now points to the authenticated name -- copy carefully */
114668457Seric 	cleanstrcpy(hbuf, p, MAXNAME);
114768875Seric 	i = strlen(hbuf);
114867935Seric 	hbuf[i++] = '@';
114967935Seric 	strcpy(&hbuf[i], RealHostName == NULL ? "localhost" : RealHostName);
115058957Seric 	goto finish;
115158957Seric 
115266011Seric closeident:
115366011Seric 	(void) close(s);
115466011Seric 	clrevent(ev);
115566011Seric 
115658957Seric noident:
115766003Seric 	if (RealHostName == NULL)
115866003Seric 	{
115966003Seric 		if (tTd(9, 1))
116066003Seric 			printf("getauthinfo: NULL\n");
116166003Seric 		return NULL;
116266003Seric 	}
116358957Seric 	(void) strcpy(hbuf, RealHostName);
116458957Seric 
116558957Seric finish:
116666003Seric 	if (RealHostName != NULL && RealHostName[0] != '[')
116758951Seric 	{
116858951Seric 		p = &hbuf[strlen(hbuf)];
116958951Seric 		(void) sprintf(p, " [%s]", anynet_ntoa(&RealHostAddr));
117058951Seric 	}
117158957Seric 	if (tTd(9, 1))
117258951Seric 		printf("getauthinfo: %s\n", hbuf);
117358308Seric 	return hbuf;
117458308Seric }
117558308Seric /*
117660089Seric **  HOST_MAP_LOOKUP -- turn a hostname into canonical form
117753751Seric **
117853751Seric **	Parameters:
117956823Seric **		map -- a pointer to this map (unused).
118060089Seric **		name -- the (presumably unqualified) hostname.
118160257Seric **		av -- unused -- for compatibility with other mapping
118255019Seric **			functions.
118359084Seric **		statp -- an exit status (out parameter) -- set to
118459084Seric **			EX_TEMPFAIL if the name server is unavailable.
118553751Seric **
118653751Seric **	Returns:
118753751Seric **		The mapping, if found.
118853751Seric **		NULL if no mapping found.
118953751Seric **
119053751Seric **	Side Effects:
119153751Seric **		Looks up the host specified in hbuf.  If it is not
119253751Seric **		the canonical name for that host, return the canonical
119353751Seric **		name.
119453751Seric */
119551315Seric 
119653751Seric char *
119760257Seric host_map_lookup(map, name, av, statp)
119856823Seric 	MAP *map;
119960089Seric 	char *name;
120060257Seric 	char **av;
120159084Seric 	int *statp;
120216911Seric {
120316911Seric 	register struct hostent *hp;
120468693Seric 	struct in_addr in_addr;
120556823Seric 	char *cp;
120659671Seric 	register STAB *s;
120768693Seric 	char hbuf[MAXNAME + 1];
120866334Seric #if NAMED_BIND
120959671Seric 	extern int h_errno;
121066029Seric #endif
121116911Seric 
121225574Smiriam 	/*
121359671Seric 	**  See if we have already looked up this name.  If so, just
121459671Seric 	**  return it.
121559671Seric 	*/
121653751Seric 
121760089Seric 	s = stab(name, ST_NAMECANON, ST_ENTER);
121859671Seric 	if (bitset(NCF_VALID, s->s_namecanon.nc_flags))
121959671Seric 	{
122059986Seric 		if (tTd(9, 1))
122160089Seric 			printf("host_map_lookup(%s) => CACHE %s\n",
122260089Seric 				name, s->s_namecanon.nc_cname);
122359671Seric 		errno = s->s_namecanon.nc_errno;
122466334Seric #if NAMED_BIND
122559671Seric 		h_errno = s->s_namecanon.nc_herrno;
122666029Seric #endif
122759671Seric 		*statp = s->s_namecanon.nc_stat;
122868817Seric 		if (*statp == EX_TEMPFAIL)
122965199Seric 		{
123068857Seric 			CurEnv->e_status = "4.4.3";
123168817Seric 			usrerr("451 %s: Name server timeout",
123265199Seric 				shortenstring(name, 33));
123365199Seric 		}
123459671Seric 		return s->s_namecanon.nc_cname;
123559671Seric 	}
123659671Seric 
123759671Seric 	/*
123859671Seric 	**  If first character is a bracket, then it is an address
123959671Seric 	**  lookup.  Address is copied into a temporary buffer to
124060089Seric 	**  strip the brackets and to preserve name if address is
124159671Seric 	**  unknown.
124259671Seric 	*/
124359671Seric 
124460089Seric 	if (*name != '[')
124553751Seric 	{
124655019Seric 		extern bool getcanonname();
124755019Seric 
124858798Seric 		if (tTd(9, 1))
124960089Seric 			printf("host_map_lookup(%s) => ", name);
125059671Seric 		s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
125168693Seric 		if (strlen(name) < sizeof hbuf)
125268693Seric 			(void) strcpy(hbuf, name);
125368693Seric 		else
125468693Seric 		{
125568693Seric 			bcopy(name, hbuf, sizeof hbuf - 1);
125668693Seric 			hbuf[sizeof hbuf - 1] = '\0';
125768693Seric 		}
125868759Seric 		if (getcanonname(hbuf, sizeof hbuf - 1, !NoMXforCanon))
125958796Seric 		{
126058796Seric 			if (tTd(9, 1))
126158796Seric 				printf("%s\n", hbuf);
126260257Seric 			cp = map_rewrite(map, hbuf, strlen(hbuf), av);
126360257Seric 			s->s_namecanon.nc_cname = newstr(cp);
126460257Seric 			return cp;
126558796Seric 		}
126653751Seric 		else
126758796Seric 		{
126859084Seric 			register struct hostent *hp;
126959084Seric 
127066029Seric 			s->s_namecanon.nc_errno = errno;
127166334Seric #if NAMED_BIND
127266029Seric 			s->s_namecanon.nc_herrno = h_errno;
127358796Seric 			if (tTd(9, 1))
127459084Seric 				printf("FAIL (%d)\n", h_errno);
127559084Seric 			switch (h_errno)
127659084Seric 			{
127759084Seric 			  case TRY_AGAIN:
127859596Seric 				if (UseNameServer)
127959734Seric 				{
128068857Seric 					CurEnv->e_status = "4.4.3";
128168817Seric 					usrerr("451 %s: Name server timeout",
128265199Seric 						shortenstring(name, 33));
128359734Seric 				}
128459084Seric 				*statp = EX_TEMPFAIL;
128559084Seric 				break;
128659084Seric 
128759084Seric 			  case HOST_NOT_FOUND:
128868881Seric 			  case NO_DATA:
128959084Seric 				*statp = EX_NOHOST;
129059084Seric 				break;
129159084Seric 
129259084Seric 			  case NO_RECOVERY:
129359084Seric 				*statp = EX_SOFTWARE;
129459084Seric 				break;
129559084Seric 
129659084Seric 			  default:
129759084Seric 				*statp = EX_UNAVAILABLE;
129859084Seric 				break;
129959084Seric 			}
130066029Seric #else
130166029Seric 			if (tTd(9, 1))
130266029Seric 				printf("FAIL\n");
130366029Seric 			*statp = EX_NOHOST;
130466029Seric #endif
130559671Seric 			s->s_namecanon.nc_stat = *statp;
130668693Seric 			if ((*statp != EX_TEMPFAIL && *statp != EX_NOHOST) ||
130768693Seric 			    UseNameServer)
130859084Seric 				return NULL;
130959084Seric 
131059084Seric 			/*
131159084Seric 			**  Try to look it up in /etc/hosts
131259084Seric 			*/
131359084Seric 
131468693Seric 			hp = sm_gethostbyname(name);
131559084Seric 			if (hp == NULL)
131659084Seric 			{
131759084Seric 				/* no dice there either */
131859671Seric 				s->s_namecanon.nc_stat = *statp = EX_NOHOST;
131959084Seric 				return NULL;
132059084Seric 			}
132159084Seric 
132259671Seric 			s->s_namecanon.nc_stat = *statp = EX_OK;
132360257Seric 			cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
132460257Seric 			s->s_namecanon.nc_cname = newstr(cp);
132560257Seric 			return cp;
132658796Seric 		}
132753751Seric 	}
132860089Seric 	if ((cp = strchr(name, ']')) == NULL)
132953751Seric 		return (NULL);
133040994Sbostic 	*cp = '\0';
133168693Seric 	in_addr.s_addr = inet_addr(&name[1]);
133258110Seric 
133358110Seric 	/* nope -- ask the name server */
133468693Seric 	hp = sm_gethostbyaddr((char *)&in_addr, INADDRSZ, AF_INET);
133559671Seric 	s->s_namecanon.nc_errno = errno;
133666334Seric #if NAMED_BIND
133759671Seric 	s->s_namecanon.nc_herrno = h_errno;
133866029Seric #endif
133959671Seric 	s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
134033932Sbostic 	if (hp == NULL)
134159671Seric 	{
134259671Seric 		s->s_namecanon.nc_stat = *statp = EX_NOHOST;
134353751Seric 		return (NULL);
134459671Seric 	}
134553751Seric 
134658110Seric 	/* found a match -- copy out */
134760257Seric 	cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
134859671Seric 	s->s_namecanon.nc_stat = *statp = EX_OK;
134960257Seric 	s->s_namecanon.nc_cname = newstr(cp);
135060257Seric 	return cp;
135133932Sbostic }
135258755Seric /*
135358755Seric **  ANYNET_NTOA -- convert a network address to printable form.
135458755Seric **
135558755Seric **	Parameters:
135658755Seric **		sap -- a pointer to a sockaddr structure.
135758755Seric **
135858755Seric **	Returns:
135958755Seric **		A printable version of that sockaddr.
136058755Seric */
136116911Seric 
136258755Seric char *
136358755Seric anynet_ntoa(sap)
136458755Seric 	register SOCKADDR *sap;
136558755Seric {
136658755Seric 	register char *bp;
136758755Seric 	register char *ap;
136858755Seric 	int l;
136964734Seric 	static char buf[100];
137058755Seric 
137158798Seric 	/* check for null/zero family */
137258798Seric 	if (sap == NULL)
137358798Seric 		return "NULLADDR";
137458798Seric 	if (sap->sa.sa_family == 0)
137558798Seric 		return "0";
137658798Seric 
137764734Seric 	switch (sap->sa.sa_family)
137864734Seric 	{
137964821Seric #ifdef NETUNIX
138064734Seric 	  case AF_UNIX:
138164758Seric 	  	if (sap->sunix.sun_path[0] != '\0')
138264758Seric 	  		sprintf(buf, "[UNIX: %.64s]", sap->sunix.sun_path);
138364734Seric 	  	else
138464734Seric 	  		sprintf(buf, "[UNIX: localhost]");
138564734Seric 		return buf;
138664734Seric #endif
138764734Seric 
138858778Seric #ifdef NETINET
138964734Seric 	  case AF_INET:
139068776Seric 		return inet_ntoa(sap->sin.sin_addr);
139158778Seric #endif
139258755Seric 
139364734Seric 	  default:
139464734Seric 	  	/* this case is only to ensure syntactic correctness */
139564734Seric 	  	break;
139664734Seric 	}
139764734Seric 
139858755Seric 	/* unknown family -- just dump bytes */
139958778Seric 	(void) sprintf(buf, "Family %d: ", sap->sa.sa_family);
140058755Seric 	bp = &buf[strlen(buf)];
140158778Seric 	ap = sap->sa.sa_data;
140258778Seric 	for (l = sizeof sap->sa.sa_data; --l >= 0; )
140358755Seric 	{
140458755Seric 		(void) sprintf(bp, "%02x:", *ap++ & 0377);
140558755Seric 		bp += 3;
140658755Seric 	}
140758755Seric 	*--bp = '\0';
140858755Seric 	return buf;
140958755Seric }
141058951Seric /*
141158951Seric **  HOSTNAMEBYANYADDR -- return name of host based on address
141258951Seric **
141358951Seric **	Parameters:
141458951Seric **		sap -- SOCKADDR pointer
141558951Seric **
141658951Seric **	Returns:
141758951Seric **		text representation of host name.
141858951Seric **
141958951Seric **	Side Effects:
142058951Seric **		none.
142158951Seric */
142258755Seric 
142358951Seric char *
142458951Seric hostnamebyanyaddr(sap)
142558951Seric 	register SOCKADDR *sap;
142658951Seric {
142758951Seric 	register struct hostent *hp;
142864734Seric 	int saveretry;
142958951Seric 
143066334Seric #if NAMED_BIND
143159042Seric 	/* shorten name server timeout to avoid higher level timeouts */
143259042Seric 	saveretry = _res.retry;
143359042Seric 	_res.retry = 3;
143459042Seric #endif /* NAMED_BIND */
143559042Seric 
143658951Seric 	switch (sap->sa.sa_family)
143758951Seric 	{
143858951Seric #ifdef NETINET
143958951Seric 	  case AF_INET:
144068693Seric 		hp = sm_gethostbyaddr((char *) &sap->sin.sin_addr,
144168693Seric 			INADDRSZ,
144258951Seric 			AF_INET);
144358951Seric 		break;
144458951Seric #endif
144558951Seric 
144658951Seric #ifdef NETISO
144758951Seric 	  case AF_ISO:
144868693Seric 		hp = sm_gethostbyaddr((char *) &sap->siso.siso_addr,
144958951Seric 			sizeof sap->siso.siso_addr,
145058951Seric 			AF_ISO);
145158951Seric 		break;
145258951Seric #endif
145358951Seric 
145464734Seric 	  case AF_UNIX:
145564734Seric 		hp = NULL;
145664734Seric 		break;
145764734Seric 
145858951Seric 	  default:
145968693Seric 		hp = sm_gethostbyaddr(sap->sa.sa_data,
146058951Seric 			   sizeof sap->sa.sa_data,
146158951Seric 			   sap->sa.sa_family);
146258951Seric 		break;
146358951Seric 	}
146458951Seric 
146566334Seric #if NAMED_BIND
146659042Seric 	_res.retry = saveretry;
146759042Seric #endif /* NAMED_BIND */
146859042Seric 
146958951Seric 	if (hp != NULL)
147058951Seric 		return hp->h_name;
147158951Seric 	else
147258951Seric 	{
147358951Seric 		/* produce a dotted quad */
147458951Seric 		static char buf[512];
147558951Seric 
147658951Seric 		(void) sprintf(buf, "[%s]", anynet_ntoa(sap));
147758951Seric 		return buf;
147858951Seric 	}
147958951Seric }
148058951Seric 
148156795Seric # else /* DAEMON */
148216911Seric /* code for systems without sophisticated networking */
148310758Seric 
148410758Seric /*
148510758Seric **  MYHOSTNAME -- stub version for case of no daemon code.
148611297Seric **
148711297Seric **	Can't convert to upper case here because might be a UUCP name.
148812313Seric **
148912313Seric **	Mark, you can change this to be anything you want......
149010758Seric */
149110758Seric 
149210758Seric char **
149312313Seric myhostname(hostbuf, size)
149410758Seric 	char hostbuf[];
149512313Seric 	int size;
149610758Seric {
149710758Seric 	register FILE *f;
149810758Seric 
149910758Seric 	hostbuf[0] = '\0';
150010758Seric 	f = fopen("/usr/include/whoami", "r");
150110758Seric 	if (f != NULL)
150210758Seric 	{
150312313Seric 		(void) fgets(hostbuf, size, f);
150410758Seric 		fixcrlf(hostbuf, TRUE);
150510758Seric 		(void) fclose(f);
150610758Seric 	}
150710758Seric 	return (NULL);
150810758Seric }
150916911Seric /*
151058951Seric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
151158308Seric **
151258308Seric **	Parameters:
151358308Seric **		fd -- the descriptor
151458308Seric **
151558308Seric **	Returns:
151658308Seric **		The host name associated with this descriptor, if it can
151758308Seric **			be determined.
151858308Seric **		NULL otherwise.
151958308Seric **
152058308Seric **	Side Effects:
152158308Seric **		none
152258308Seric */
152358308Seric 
152458308Seric char *
152558951Seric getauthinfo(fd)
152658308Seric 	int fd;
152758308Seric {
152858308Seric 	return NULL;
152958308Seric }
153058308Seric /*
153116911Seric **  MAPHOSTNAME -- turn a hostname into canonical form
153216911Seric **
153316911Seric **	Parameters:
153456823Seric **		map -- a pointer to the database map.
153560089Seric **		name -- a buffer containing a hostname.
153653751Seric **		avp -- a pointer to a (cf file defined) argument vector.
153759084Seric **		statp -- an exit status (out parameter).
153816911Seric **
153916911Seric **	Returns:
154053751Seric **		mapped host name
154151315Seric **		FALSE otherwise.
154216911Seric **
154316911Seric **	Side Effects:
154460089Seric **		Looks up the host specified in name.  If it is not
154516911Seric **		the canonical name for that host, replace it with
154616911Seric **		the canonical name.  If the name is unknown, or it
154716911Seric **		is already the canonical name, leave it unchanged.
154816911Seric */
154910758Seric 
155016911Seric /*ARGSUSED*/
155153751Seric char *
155260089Seric host_map_lookup(map, name, avp, statp)
155356823Seric 	MAP *map;
155460089Seric 	char *name;
155553751Seric 	char **avp;
155659084Seric 	char *statp;
155716911Seric {
155859084Seric 	register struct hostent *hp;
155959084Seric 
156068693Seric 	hp = sm_gethostbyname(name);
156159084Seric 	if (hp != NULL)
156259084Seric 		return hp->h_name;
156359084Seric 	*statp = EX_NOHOST;
156453751Seric 	return NULL;
156516911Seric }
156616911Seric 
156756795Seric #endif /* DAEMON */
1568