xref: /csrg-svn/usr.sbin/sendmail/src/daemon.c (revision 66845)
122700Sdist /*
234920Sbostic  * Copyright (c) 1983 Eric P. Allman
362522Sbostic  * Copyright (c) 1988, 1993
462522Sbostic  *	The Regents of the University of California.  All rights reserved.
533780Sbostic  *
642825Sbostic  * %sccs.include.redist.c%
733780Sbostic  */
822700Sdist 
933932Sbostic #include <errno.h>
1040962Sbostic #include "sendmail.h"
114535Seric 
1233780Sbostic #ifndef lint
1333780Sbostic #ifdef DAEMON
14*66845Seric static char sccsid[] = "@(#)daemon.c	8.44 (Berkeley) 04/17/94 (with daemon mode)";
1533780Sbostic #else
16*66845Seric static char sccsid[] = "@(#)daemon.c	8.44 (Berkeley) 04/17/94 (without daemon mode)";
1733780Sbostic #endif
1833780Sbostic #endif /* not lint */
194535Seric 
2033780Sbostic #ifdef DAEMON
2133780Sbostic 
2223120Seric # include <netdb.h>
2364338Seric # include <arpa/inet.h>
245978Seric 
2566334Seric #if NAMED_BIND
2659042Seric # include <arpa/nameser.h>
2759042Seric # include <resolv.h>
2859042Seric #endif
2959042Seric 
304535Seric /*
314535Seric **  DAEMON.C -- routines to use when running as a daemon.
327556Seric **
337556Seric **	This entire file is highly dependent on the 4.2 BSD
347556Seric **	interprocess communication primitives.  No attempt has
357556Seric **	been made to make this file portable to Version 7,
367556Seric **	Version 6, MPX files, etc.  If you should try such a
377556Seric **	thing yourself, I recommend chucking the entire file
387556Seric **	and starting from scratch.  Basic semantics are:
397556Seric **
407556Seric **	getrequests()
417556Seric **		Opens a port and initiates a connection.
427556Seric **		Returns in a child.  Must set InChannel and
437556Seric **		OutChannel appropriately.
4410206Seric **	clrdaemon()
4510206Seric **		Close any open files associated with getting
4610206Seric **		the connection; this is used when running the queue,
4710206Seric **		etc., to avoid having extra file descriptors during
4810206Seric **		the queue run and to avoid confusing the network
4910206Seric **		code (if it cares).
5052106Seric **	makeconnection(host, port, outfile, infile, usesecureport)
517556Seric **		Make a connection to the named host on the given
527556Seric **		port.  Set *outfile and *infile to the files
537556Seric **		appropriate for communication.  Returns zero on
547556Seric **		success, else an exit status describing the
557556Seric **		error.
5660089Seric **	host_map_lookup(map, hbuf, avp, pstat)
5756823Seric **		Convert the entry in hbuf into a canonical form.
584535Seric */
594535Seric /*
604535Seric **  GETREQUESTS -- open mail IPC port and get requests.
614535Seric **
624535Seric **	Parameters:
634535Seric **		none.
644535Seric **
654535Seric **	Returns:
664535Seric **		none.
674535Seric **
684535Seric **	Side Effects:
694535Seric **		Waits until some interesting activity occurs.  When
704535Seric **		it does, a child is created to process it, and the
714535Seric **		parent waits for completion.  Return from this
729886Seric **		routine is always in the child.  The file pointers
739886Seric **		"InChannel" and "OutChannel" should be set to point
749886Seric **		to the communication channel.
754535Seric */
764535Seric 
7758849Seric int		DaemonSocket	= -1;		/* fd describing socket */
7858849Seric SOCKADDR	DaemonAddr;			/* socket for incoming */
7959783Seric int		ListenQueueSize = 10;		/* size of listen queue */
8064381Seric int		TcpRcvBufferSize = 0;		/* size of TCP receive buffer */
8164381Seric int		TcpSndBufferSize = 0;		/* size of TCP send buffer */
8216144Seric 
834535Seric getrequests()
844535Seric {
859610Seric 	int t;
8653751Seric 	bool refusingconnections = TRUE;
8758419Seric 	FILE *pidf;
8864828Seric 	int socksize;
8966793Seric #ifdef XDEBUG
9066793Seric 	bool j_has_dot;
9166793Seric #endif
9246928Sbostic 	extern void reapchild();
937117Seric 
949610Seric 	/*
959610Seric 	**  Set up the address for the mailer.
969610Seric 	*/
979610Seric 
9858849Seric 	if (DaemonAddr.sin.sin_family == 0)
9958849Seric 		DaemonAddr.sin.sin_family = AF_INET;
10058849Seric 	if (DaemonAddr.sin.sin_addr.s_addr == 0)
10158849Seric 		DaemonAddr.sin.sin_addr.s_addr = INADDR_ANY;
10258849Seric 	if (DaemonAddr.sin.sin_port == 0)
1039610Seric 	{
10465169Seric 		register struct servent *sp;
10565169Seric 
10658849Seric 		sp = getservbyname("smtp", "tcp");
10758849Seric 		if (sp == NULL)
10858849Seric 		{
10958909Seric 			syserr("554 service \"smtp\" unknown");
11065169Seric 			DaemonAddr.sin.sin_port = htons(25);
11158849Seric 		}
11265169Seric 		else
11365169Seric 			DaemonAddr.sin.sin_port = sp->s_port;
1149610Seric 	}
1159610Seric 
1169610Seric 	/*
1179610Seric 	**  Try to actually open the connection.
1189610Seric 	*/
1199610Seric 
1209610Seric 	if (tTd(15, 1))
12158849Seric 		printf("getrequests: port 0x%x\n", DaemonAddr.sin.sin_port);
1229610Seric 
1239610Seric 	/* get a socket for the SMTP connection */
124*66845Seric 	socksize = opendaemonsocket();
12510347Seric 
12664035Seric 	(void) setsignal(SIGCHLD, reapchild);
12724945Seric 
12858419Seric 	/* write the pid to the log file for posterity */
12958419Seric 	pidf = fopen(PidFile, "w");
13058419Seric 	if (pidf != NULL)
13158419Seric 	{
13263863Seric 		extern char *CommandLineArgs;
13363863Seric 
13463863Seric 		/* write the process id on line 1 */
13558419Seric 		fprintf(pidf, "%d\n", getpid());
13663863Seric 
13763863Seric 		/* line 2 contains all command line flags */
13863863Seric 		fprintf(pidf, "%s\n", CommandLineArgs);
13963863Seric 
14063863Seric 		/* flush and close */
14158419Seric 		fclose(pidf);
14258419Seric 	}
14358419Seric 
14466793Seric #ifdef XDEBUG
14566793Seric 	{
14666812Seric 		char jbuf[MAXHOSTNAMELEN];
14758419Seric 
14866812Seric 		expand("\201j", jbuf, &jbuf[sizeof jbuf - 1], CurEnv);
14966812Seric 		j_has_dot = strchr(jbuf, '.') != NULL;
15066793Seric 	}
15166793Seric #endif
15266793Seric 
1539610Seric 	if (tTd(15, 1))
15410206Seric 		printf("getrequests: %d\n", DaemonSocket);
1559610Seric 
1564631Seric 	for (;;)
1574631Seric 	{
15814875Seric 		register int pid;
15911147Seric 		auto int lotherend;
16053751Seric 		extern bool refuseconnections();
16111147Seric 
16214875Seric 		/* see if we are rejecting connections */
16353751Seric 		CurrentLA = getla();
16453751Seric 		if (refuseconnections())
16536584Sbostic 		{
166*66845Seric 			if (DaemonSocket >= 0)
16753751Seric 			{
168*66845Seric 				/* close socket so peer will fail quickly */
169*66845Seric 				(void) close(DaemonSocket);
170*66845Seric 				DaemonSocket = -1;
17153751Seric 			}
172*66845Seric 			refusingconnections = TRUE;
17357385Seric 			setproctitle("rejecting connections: load average: %d",
17457385Seric 				CurrentLA);
175*66845Seric 			sleep(15);
17653751Seric 			continue;
17736584Sbostic 		}
17814875Seric 
17953751Seric 		if (refusingconnections)
18053751Seric 		{
18153751Seric 			/* start listening again */
182*66845Seric 			if (DaemonSocket < 0)
183*66845Seric 				(void) opendaemonsocket();
18453751Seric 			setproctitle("accepting connections");
18553751Seric 			refusingconnections = FALSE;
18653751Seric 		}
18753751Seric 
18866793Seric #ifdef XDEBUG
18966793Seric 		/* check for disaster */
19066793Seric 		{
19166793Seric 			register STAB *s;
19266812Seric 			char jbuf[MAXHOSTNAMELEN];
19366793Seric 
19466812Seric 			expand("\201j", jbuf, &jbuf[sizeof jbuf - 1], CurEnv);
19566812Seric 			if ((s = stab(jbuf, ST_CLASS, ST_FIND)) == NULL ||
19666793Seric 			    !bitnset('w', s->s_class))
19766793Seric 			{
19866793Seric 				dumpstate("daemon lost $j");
19966793Seric 				syslog(LOG_ALERT, "daemon process doesn't have $j in $=w; see syslog");
20066793Seric 				abort();
20166793Seric 			}
20266812Seric 			else if (j_has_dot && strchr(jbuf, '.') == NULL)
20366793Seric 			{
20466793Seric 				dumpstate("daemon $j lost dot");
20566793Seric 				syslog(LOG_ALERT, "daemon process $j lost dot; see syslog");
20666793Seric 				abort();
20766793Seric 			}
20866793Seric 		}
20966793Seric #endif
21066793Seric 
2119610Seric 		/* wait for a connection */
2129610Seric 		do
2139610Seric 		{
2149610Seric 			errno = 0;
21564828Seric 			lotherend = socksize;
21646928Sbostic 			t = accept(DaemonSocket,
21746928Sbostic 			    (struct sockaddr *)&RealHostAddr, &lotherend);
2189610Seric 		} while (t < 0 && errno == EINTR);
2199610Seric 		if (t < 0)
2205978Seric 		{
2219610Seric 			syserr("getrequests: accept");
2229610Seric 			sleep(5);
2239610Seric 			continue;
2245978Seric 		}
2254631Seric 
2265978Seric 		/*
2275978Seric 		**  Create a subprocess to process the mail.
2285978Seric 		*/
2295978Seric 
2307677Seric 		if (tTd(15, 2))
2319610Seric 			printf("getrequests: forking (fd = %d)\n", t);
2325978Seric 
2334636Seric 		pid = fork();
2344636Seric 		if (pid < 0)
2354631Seric 		{
2364636Seric 			syserr("daemon: cannot fork");
2374636Seric 			sleep(10);
2389610Seric 			(void) close(t);
2394636Seric 			continue;
2404631Seric 		}
2414631Seric 
2424636Seric 		if (pid == 0)
2434631Seric 		{
24464086Seric 			char *p;
24558951Seric 			extern char *hostnamebyanyaddr();
24611147Seric 
2474636Seric 			/*
2484636Seric 			**  CHILD -- return to caller.
24911147Seric 			**	Collect verified idea of sending host.
2504636Seric 			**	Verify calling user id if possible here.
2514636Seric 			*/
2524631Seric 
25364035Seric 			(void) setsignal(SIGCHLD, SIG_DFL);
25466017Seric 			DisConnected = FALSE;
25524950Seric 
25666032Seric 			setproctitle("startup with %s",
25766032Seric 				anynet_ntoa(&RealHostAddr));
25866032Seric 
25911147Seric 			/* determine host name */
26064086Seric 			p = hostnamebyanyaddr(&RealHostAddr);
26164086Seric 			RealHostName = newstr(p);
26266032Seric 			setproctitle("startup with %s", p);
26358778Seric 
26455173Seric #ifdef LOG
26563842Seric 			if (LogLevel > 11)
26655173Seric 			{
26755173Seric 				/* log connection information */
26855173Seric 				syslog(LOG_INFO, "connect from %s (%s)",
26958951Seric 					RealHostName, anynet_ntoa(&RealHostAddr));
27055173Seric 			}
27155173Seric #endif
27255173Seric 
27359254Seric 			(void) close(DaemonSocket);
27464724Seric 			if ((InChannel = fdopen(t, "r")) == NULL ||
27564724Seric 			    (t = dup(t)) < 0 ||
27664724Seric 			    (OutChannel = fdopen(t, "w")) == NULL)
27764724Seric 			{
27864724Seric 				syserr("cannot open SMTP server channel, fd=%d", t);
27964724Seric 				exit(0);
28064724Seric 			}
28159254Seric 
28216884Seric 			/* should we check for illegal connection here? XXX */
28359156Seric #ifdef XLA
28459156Seric 			if (!xla_host_ok(RealHostName))
28559156Seric 			{
28659254Seric 				message("421 Too many SMTP sessions for this host");
28759156Seric 				exit(0);
28859156Seric 			}
28959156Seric #endif
29016884Seric 
2917677Seric 			if (tTd(15, 2))
2925978Seric 				printf("getreq: returning\n");
2934636Seric 			return;
2944631Seric 		}
2954631Seric 
2967117Seric 		/* close the port so that others will hang (for a while) */
2979610Seric 		(void) close(t);
2984631Seric 	}
2999886Seric 	/*NOTREACHED*/
3004631Seric }
3015978Seric /*
302*66845Seric **  OPENDAEMONSOCKET -- open the SMTP socket
303*66845Seric **
304*66845Seric **	Deals with setting all appropriate options.  DaemonAddr must
305*66845Seric **	be set up in advance.
306*66845Seric **
307*66845Seric **	Parameters:
308*66845Seric **		none
309*66845Seric **
310*66845Seric **	Returns:
311*66845Seric **		Size in bytes of the daemon socket addr.
312*66845Seric **
313*66845Seric **	Side Effects:
314*66845Seric **		Leaves DaemonSocket set to the open socket.
315*66845Seric **		Exits if the socket cannot be created.
316*66845Seric */
317*66845Seric 
318*66845Seric int
319*66845Seric opendaemonsocket()
320*66845Seric {
321*66845Seric 	int on = 1;
322*66845Seric 	int socksize;
323*66845Seric 
324*66845Seric 	if (tTd(15, 2))
325*66845Seric 		printf("opendaemonsocket()\n");
326*66845Seric 
327*66845Seric 	DaemonSocket = socket(DaemonAddr.sa.sa_family, SOCK_STREAM, 0);
328*66845Seric 	if (DaemonSocket < 0)
329*66845Seric 	{
330*66845Seric 		/* probably another daemon already */
331*66845Seric 		syserr("opendaemonsocket: can't create server SMTP socket");
332*66845Seric 	  severe:
333*66845Seric # ifdef LOG
334*66845Seric 		if (LogLevel > 0)
335*66845Seric 			syslog(LOG_ALERT, "problem creating SMTP socket");
336*66845Seric # endif /* LOG */
337*66845Seric 		finis();
338*66845Seric 	}
339*66845Seric 
340*66845Seric 	/* turn on network debugging? */
341*66845Seric 	if (tTd(15, 101))
342*66845Seric 		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on);
343*66845Seric 
344*66845Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on);
345*66845Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on);
346*66845Seric 
347*66845Seric #ifdef SO_RCVBUF
348*66845Seric 	if (TcpRcvBufferSize > 0)
349*66845Seric 	{
350*66845Seric 		if (setsockopt(DaemonSocket, SOL_SOCKET, SO_RCVBUF,
351*66845Seric 			       (char *) &TcpRcvBufferSize,
352*66845Seric 			       sizeof(TcpRcvBufferSize)) < 0)
353*66845Seric 			syserr("getrequests: setsockopt(SO_RCVBUF)");
354*66845Seric 	}
355*66845Seric #endif
356*66845Seric 
357*66845Seric 	switch (DaemonAddr.sa.sa_family)
358*66845Seric 	{
359*66845Seric # ifdef NETINET
360*66845Seric 	  case AF_INET:
361*66845Seric 		socksize = sizeof DaemonAddr.sin;
362*66845Seric 		break;
363*66845Seric # endif
364*66845Seric 
365*66845Seric # ifdef NETISO
366*66845Seric 	  case AF_ISO:
367*66845Seric 		socksize = sizeof DaemonAddr.siso;
368*66845Seric 		break;
369*66845Seric # endif
370*66845Seric 
371*66845Seric 	  default:
372*66845Seric 		socksize = sizeof DaemonAddr;
373*66845Seric 		break;
374*66845Seric 	}
375*66845Seric 
376*66845Seric 	if (bind(DaemonSocket, &DaemonAddr.sa, socksize) < 0)
377*66845Seric 	{
378*66845Seric 		syserr("getrequests: cannot bind");
379*66845Seric 		(void) close(DaemonSocket);
380*66845Seric 		goto severe;
381*66845Seric 	}
382*66845Seric 	if (listen(DaemonSocket, ListenQueueSize) < 0)
383*66845Seric 	{
384*66845Seric 		syserr("getrequests: cannot listen");
385*66845Seric 		(void) close(DaemonSocket);
386*66845Seric 		goto severe;
387*66845Seric 	}
388*66845Seric 	return socksize;
389*66845Seric }
390*66845Seric /*
39110206Seric **  CLRDAEMON -- reset the daemon connection
39210206Seric **
39310206Seric **	Parameters:
39410206Seric **		none.
39510206Seric **
39610206Seric **	Returns:
39710206Seric **		none.
39810206Seric **
39910206Seric **	Side Effects:
40010206Seric **		releases any resources used by the passive daemon.
40110206Seric */
40210206Seric 
40310206Seric clrdaemon()
40410206Seric {
40510206Seric 	if (DaemonSocket >= 0)
40610206Seric 		(void) close(DaemonSocket);
40710206Seric 	DaemonSocket = -1;
40810206Seric }
40910206Seric /*
41058849Seric **  SETDAEMONOPTIONS -- set options for running the daemon
41158849Seric **
41258849Seric **	Parameters:
41358849Seric **		p -- the options line.
41458849Seric **
41558849Seric **	Returns:
41658849Seric **		none.
41758849Seric */
41858849Seric 
41958849Seric setdaemonoptions(p)
42058849Seric 	register char *p;
42158849Seric {
42258873Seric 	if (DaemonAddr.sa.sa_family == AF_UNSPEC)
42358873Seric 		DaemonAddr.sa.sa_family = AF_INET;
42458873Seric 
42558849Seric 	while (p != NULL)
42658849Seric 	{
42758849Seric 		register char *f;
42858849Seric 		register char *v;
42958849Seric 
43058849Seric 		while (isascii(*p) && isspace(*p))
43158849Seric 			p++;
43258849Seric 		if (*p == '\0')
43358849Seric 			break;
43458849Seric 		f = p;
43558849Seric 		p = strchr(p, ',');
43658849Seric 		if (p != NULL)
43758849Seric 			*p++ = '\0';
43858849Seric 		v = strchr(f, '=');
43958849Seric 		if (v == NULL)
44058849Seric 			continue;
44158849Seric 		while (isascii(*++v) && isspace(*v))
44258849Seric 			continue;
44358849Seric 
44458849Seric 		switch (*f)
44558849Seric 		{
44658873Seric 		  case 'F':		/* address family */
44758849Seric 			if (isascii(*v) && isdigit(*v))
44858873Seric 				DaemonAddr.sa.sa_family = atoi(v);
44958873Seric #ifdef NETINET
45058873Seric 			else if (strcasecmp(v, "inet") == 0)
45158873Seric 				DaemonAddr.sa.sa_family = AF_INET;
45258873Seric #endif
45358873Seric #ifdef NETISO
45458873Seric 			else if (strcasecmp(v, "iso") == 0)
45558873Seric 				DaemonAddr.sa.sa_family = AF_ISO;
45658873Seric #endif
45758873Seric #ifdef NETNS
45858873Seric 			else if (strcasecmp(v, "ns") == 0)
45958873Seric 				DaemonAddr.sa.sa_family = AF_NS;
46058873Seric #endif
46158873Seric #ifdef NETX25
46258873Seric 			else if (strcasecmp(v, "x.25") == 0)
46358873Seric 				DaemonAddr.sa.sa_family = AF_CCITT;
46458873Seric #endif
46558849Seric 			else
46658873Seric 				syserr("554 Unknown address family %s in Family=option", v);
46758873Seric 			break;
46858873Seric 
46958873Seric 		  case 'A':		/* address */
47058873Seric 			switch (DaemonAddr.sa.sa_family)
47158849Seric 			{
47258873Seric #ifdef NETINET
47358873Seric 			  case AF_INET:
47458873Seric 				if (isascii(*v) && isdigit(*v))
47558873Seric 					DaemonAddr.sin.sin_addr.s_addr = inet_network(v);
47658873Seric 				else
47758873Seric 				{
47858873Seric 					register struct netent *np;
47958849Seric 
48058873Seric 					np = getnetbyname(v);
48158873Seric 					if (np == NULL)
48258873Seric 						syserr("554 network \"%s\" unknown", v);
48358873Seric 					else
48458873Seric 						DaemonAddr.sin.sin_addr.s_addr = np->n_net;
48558873Seric 				}
48658873Seric 				break;
48758873Seric #endif
48858873Seric 
48958873Seric 			  default:
49058873Seric 				syserr("554 Address= option unsupported for family %d",
49158873Seric 					DaemonAddr.sa.sa_family);
49258873Seric 				break;
49358849Seric 			}
49458849Seric 			break;
49558849Seric 
49658873Seric 		  case 'P':		/* port */
49758873Seric 			switch (DaemonAddr.sa.sa_family)
49858849Seric 			{
49958873Seric 				short port;
50058849Seric 
50158873Seric #ifdef NETINET
50258873Seric 			  case AF_INET:
50358873Seric 				if (isascii(*v) && isdigit(*v))
50464366Seric 					DaemonAddr.sin.sin_port = htons(atoi(v));
50558849Seric 				else
50658873Seric 				{
50758873Seric 					register struct servent *sp;
50858873Seric 
50958873Seric 					sp = getservbyname(v, "tcp");
51058873Seric 					if (sp == NULL)
51158909Seric 						syserr("554 service \"%s\" unknown", v);
51258873Seric 					else
51358873Seric 						DaemonAddr.sin.sin_port = sp->s_port;
51458873Seric 				}
51558873Seric 				break;
51658873Seric #endif
51758873Seric 
51858873Seric #ifdef NETISO
51958873Seric 			  case AF_ISO:
52058873Seric 				/* assume two byte transport selector */
52158873Seric 				if (isascii(*v) && isdigit(*v))
52264366Seric 					port = htons(atoi(v));
52358873Seric 				else
52458873Seric 				{
52558873Seric 					register struct servent *sp;
52658873Seric 
52758873Seric 					sp = getservbyname(v, "tcp");
52858873Seric 					if (sp == NULL)
52958909Seric 						syserr("554 service \"%s\" unknown", v);
53058873Seric 					else
53158873Seric 						port = sp->s_port;
53258873Seric 				}
53358873Seric 				bcopy((char *) &port, TSEL(&DaemonAddr.siso), 2);
53458873Seric 				break;
53558873Seric #endif
53658873Seric 
53758873Seric 			  default:
53858873Seric 				syserr("554 Port= option unsupported for family %d",
53958873Seric 					DaemonAddr.sa.sa_family);
54058873Seric 				break;
54158849Seric 			}
54258849Seric 			break;
54359783Seric 
54459783Seric 		  case 'L':		/* listen queue size */
54559783Seric 			ListenQueueSize = atoi(v);
54659783Seric 			break;
54764381Seric 
54864381Seric 		  case 'S':		/* send buffer size */
54964381Seric 			TcpSndBufferSize = atoi(v);
55064381Seric 			break;
55164381Seric 
55264381Seric 		  case 'R':		/* receive buffer size */
55364381Seric 			TcpRcvBufferSize = atoi(v);
55464381Seric 			break;
55558849Seric 		}
55658849Seric 	}
55758849Seric }
55858849Seric /*
5596039Seric **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
5606039Seric **
5616039Seric **	Parameters:
5626039Seric **		host -- the name of the host.
5636633Seric **		port -- the port number to connect to.
56453739Seric **		mci -- a pointer to the mail connection information
56553739Seric **			structure to be filled in.
56652106Seric **		usesecureport -- if set, use a low numbered (reserved)
56752106Seric **			port to provide some rudimentary authentication.
5686039Seric **
5696039Seric **	Returns:
5706039Seric **		An exit code telling whether the connection could be
5716039Seric **			made and if not why not.
5726039Seric **
5736039Seric **	Side Effects:
5746039Seric **		none.
5756039Seric */
5765978Seric 
57758755Seric SOCKADDR	CurHostAddr;		/* address of current host */
57858305Seric 
57954967Seric int
58053739Seric makeconnection(host, port, mci, usesecureport)
5816039Seric 	char *host;
5827286Seric 	u_short port;
58354967Seric 	register MCI *mci;
58452106Seric 	bool usesecureport;
5856039Seric {
58629430Sbloom 	register int i, s;
58729430Sbloom 	register struct hostent *hp = (struct hostent *)NULL;
58858755Seric 	SOCKADDR addr;
58952106Seric 	int sav_errno;
59058755Seric 	int addrlen;
59166334Seric #if NAMED_BIND
59235651Seric 	extern int h_errno;
59335651Seric #endif
5946039Seric 
5956039Seric 	/*
5966039Seric 	**  Set up the address for the mailer.
5979308Seric 	**	Accept "[a.b.c.d]" syntax for host name.
5986039Seric 	*/
5996039Seric 
60066334Seric #if NAMED_BIND
60125475Smiriam 	h_errno = 0;
60235651Seric #endif
60325475Smiriam 	errno = 0;
60458864Seric 	bzero(&CurHostAddr, sizeof CurHostAddr);
60564334Seric 	SmtpPhase = mci->mci_phase = "initial connection";
60658906Seric 	CurHostName = host;
60725475Smiriam 
6089308Seric 	if (host[0] == '[')
6099308Seric 	{
61011147Seric 		long hid;
61156795Seric 		register char *p = strchr(host, ']');
6129308Seric 
61311147Seric 		if (p != NULL)
6149308Seric 		{
61511147Seric 			*p = '\0';
61659884Seric #ifdef NETINET
61711147Seric 			hid = inet_addr(&host[1]);
61858360Seric 			if (hid == -1)
61959884Seric #endif
62058360Seric 			{
62158360Seric 				/* try it as a host name (avoid MX lookup) */
62258360Seric 				hp = gethostbyname(&host[1]);
62366349Seric 				if (hp == NULL && p[-1] == '.')
62466349Seric 				{
62566349Seric 					p[-1] = '\0';
62666349Seric 					hp = gethostbyname(&host[1]);
62766349Seric 					p[-1] = '.';
62866349Seric 				}
62958360Seric 				*p = ']';
63058360Seric 				goto gothostent;
63158360Seric 			}
63211147Seric 			*p = ']';
6339308Seric 		}
63458360Seric 		if (p == NULL)
6359308Seric 		{
63658151Seric 			usrerr("553 Invalid numeric domain spec \"%s\"", host);
6379308Seric 			return (EX_NOHOST);
6389308Seric 		}
63959884Seric #ifdef NETINET
64059884Seric 		addr.sin.sin_family = AF_INET;		/*XXX*/
64158778Seric 		addr.sin.sin_addr.s_addr = hid;
64259884Seric #endif
6439308Seric 	}
6449610Seric 	else
6459610Seric 	{
64666349Seric 		register char *p = &host[strlen(host) - 1];
64766349Seric 
64829430Sbloom 		hp = gethostbyname(host);
64966349Seric 		if (hp == NULL && *p == '.')
65066349Seric 		{
65166349Seric 			*p = '\0';
65266349Seric 			hp = gethostbyname(host);
65366349Seric 			*p = '.';
65466349Seric 		}
65558360Seric gothostent:
65625475Smiriam 		if (hp == NULL)
65724945Seric 		{
65866334Seric #if NAMED_BIND
65925475Smiriam 			if (errno == ETIMEDOUT || h_errno == TRY_AGAIN)
66025475Smiriam 				return (EX_TEMPFAIL);
66125657Seric 
66235651Seric 			/* if name server is specified, assume temp fail */
66335651Seric 			if (errno == ECONNREFUSED && UseNameServer)
66435651Seric 				return (EX_TEMPFAIL);
66535651Seric #endif
66625475Smiriam 			return (EX_NOHOST);
66724945Seric 		}
66858778Seric 		addr.sa.sa_family = hp->h_addrtype;
66958778Seric 		switch (hp->h_addrtype)
67058778Seric 		{
67158778Seric #ifdef NETINET
67258778Seric 		  case AF_INET:
67358755Seric 			bcopy(hp->h_addr,
67458778Seric 				&addr.sin.sin_addr,
67564943Seric 				sizeof addr.sin.sin_addr);
67658778Seric 			break;
67758778Seric #endif
67858778Seric 
67958778Seric 		  default:
68058755Seric 			bcopy(hp->h_addr,
68158778Seric 				addr.sa.sa_data,
68258755Seric 				hp->h_length);
68358778Seric 			break;
68458778Seric 		}
68529430Sbloom 		i = 1;
6869610Seric 	}
6879610Seric 
6889610Seric 	/*
6899610Seric 	**  Determine the port number.
6909610Seric 	*/
6919610Seric 
69210011Seric 	if (port != 0)
69358755Seric 		port = htons(port);
69410011Seric 	else
6959610Seric 	{
6969610Seric 		register struct servent *sp = getservbyname("smtp", "tcp");
6979610Seric 
6989610Seric 		if (sp == NULL)
6999610Seric 		{
70058909Seric 			syserr("554 makeconnection: service \"smtp\" unknown");
70165169Seric 			port = htons(25);
7029610Seric 		}
70365169Seric 		else
70465169Seric 			port = sp->s_port;
7059610Seric 	}
7066039Seric 
70758778Seric 	switch (addr.sa.sa_family)
70858755Seric 	{
70959884Seric #ifdef NETINET
71058755Seric 	  case AF_INET:
71158778Seric 		addr.sin.sin_port = port;
71258755Seric 		addrlen = sizeof (struct sockaddr_in);
71358755Seric 		break;
71459884Seric #endif
71558755Seric 
71658755Seric #ifdef NETISO
71758755Seric 	  case AF_ISO:
71858755Seric 		/* assume two byte transport selector */
71958755Seric 		bcopy((char *) &port, TSEL((struct sockaddr_iso *) &addr), 2);
72058755Seric 		addrlen = sizeof (struct sockaddr_iso);
72158755Seric 		break;
72258755Seric #endif
72358755Seric 
72458755Seric 	  default:
72558778Seric 		syserr("Can't connect to address family %d", addr.sa.sa_family);
72658755Seric 		return (EX_NOHOST);
72758755Seric 	}
72858755Seric 
7296039Seric 	/*
7306039Seric 	**  Try to actually open the connection.
7316039Seric 	*/
7326039Seric 
73359156Seric #ifdef XLA
73459156Seric 	/* if too many connections, don't bother trying */
73559156Seric 	if (!xla_noqueue_ok(host))
73659156Seric 		return EX_TEMPFAIL;
73759156Seric #endif
73859156Seric 
73957736Seric 	for (;;)
74052106Seric 	{
74157736Seric 		if (tTd(16, 1))
74258755Seric 			printf("makeconnection (%s [%s])\n",
74358755Seric 				host, anynet_ntoa(&addr));
74452106Seric 
74558588Seric 		/* save for logging */
74658588Seric 		CurHostAddr = addr;
74758588Seric 
74857736Seric 		if (usesecureport)
74957736Seric 		{
75057736Seric 			int rport = IPPORT_RESERVED - 1;
7516039Seric 
75257736Seric 			s = rresvport(&rport);
75357736Seric 		}
75457736Seric 		else
75557736Seric 		{
75657736Seric 			s = socket(AF_INET, SOCK_STREAM, 0);
75757736Seric 		}
75857736Seric 		if (s < 0)
75957736Seric 		{
76057736Seric 			sav_errno = errno;
76157736Seric 			syserr("makeconnection: no socket");
76257736Seric 			goto failure;
76357736Seric 		}
76410347Seric 
76564381Seric #ifdef SO_SNDBUF
76664381Seric 		if (TcpSndBufferSize > 0)
76764381Seric 		{
76864381Seric 			if (setsockopt(s, SOL_SOCKET, SO_SNDBUF,
76964561Seric 				       (char *) &TcpSndBufferSize,
77064381Seric 				       sizeof(TcpSndBufferSize)) < 0)
77164381Seric 				syserr("makeconnection: setsockopt(SO_SNDBUF)");
77264381Seric 		}
77364381Seric #endif
77464381Seric 
77557736Seric 		if (tTd(16, 1))
77657736Seric 			printf("makeconnection: fd=%d\n", s);
77757736Seric 
77857736Seric 		/* turn on network debugging? */
77957736Seric 		if (tTd(16, 101))
78057736Seric 		{
78157736Seric 			int on = 1;
78257736Seric 			(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG,
78357736Seric 					  (char *)&on, sizeof on);
78457736Seric 		}
78557736Seric 		if (CurEnv->e_xfp != NULL)
78657736Seric 			(void) fflush(CurEnv->e_xfp);		/* for debugging */
78757736Seric 		errno = 0;					/* for debugging */
78858755Seric 		if (connect(s, (struct sockaddr *) &addr, addrlen) >= 0)
78957736Seric 			break;
79057736Seric 
79157736Seric 		/* couldn't connect.... figure out why */
79227744Sbloom 		sav_errno = errno;
79327744Sbloom 		(void) close(s);
79429430Sbloom 		if (hp && hp->h_addr_list[i])
79529430Sbloom 		{
79657736Seric 			if (tTd(16, 1))
79758755Seric 				printf("Connect failed (%s); trying new address....\n",
79858755Seric 					errstring(sav_errno));
79958778Seric 			switch (addr.sa.sa_family)
80058778Seric 			{
80158778Seric #ifdef NETINET
80258778Seric 			  case AF_INET:
80358755Seric 				bcopy(hp->h_addr_list[i++],
80458778Seric 				      &addr.sin.sin_addr,
80564943Seric 				      sizeof addr.sin.sin_addr);
80658778Seric 				break;
80758778Seric #endif
80858778Seric 
80958778Seric 			  default:
81058755Seric 				bcopy(hp->h_addr_list[i++],
81158778Seric 					addr.sa.sa_data,
81252106Seric 					hp->h_length);
81358778Seric 				break;
81458778Seric 			}
81557736Seric 			continue;
81629430Sbloom 		}
81729430Sbloom 
8186039Seric 		/* failure, decide if temporary or not */
8196039Seric 	failure:
82059254Seric #ifdef XLA
82159254Seric 		xla_host_end(host);
82259254Seric #endif
82358542Seric 		if (transienterror(sav_errno))
82458542Seric 			return EX_TEMPFAIL;
82558542Seric 		else
82658542Seric 		{
82758542Seric 			message("%s", errstring(sav_errno));
82858542Seric 			return (EX_UNAVAILABLE);
8296039Seric 		}
8306039Seric 	}
8316039Seric 
8326039Seric 	/* connection ok, put it into canonical form */
83364724Seric 	if ((mci->mci_out = fdopen(s, "w")) == NULL ||
83464724Seric 	    (s = dup(s)) < 0 ||
83564725Seric 	    (mci->mci_in = fdopen(s, "r")) == NULL)
83664724Seric 	{
83764724Seric 		syserr("cannot open SMTP client channel, fd=%d", s);
83864724Seric 		return EX_TEMPFAIL;
83964724Seric 	}
8406039Seric 
84110098Seric 	return (EX_OK);
8426039Seric }
84310758Seric /*
84410758Seric **  MYHOSTNAME -- return the name of this host.
84510758Seric **
84610758Seric **	Parameters:
84710758Seric **		hostbuf -- a place to return the name of this host.
84812313Seric **		size -- the size of hostbuf.
84910758Seric **
85010758Seric **	Returns:
85110758Seric **		A list of aliases for this host.
85210758Seric **
85310758Seric **	Side Effects:
85464338Seric **		Adds numeric codes to $=w.
85510758Seric */
8566039Seric 
85710758Seric char **
85812313Seric myhostname(hostbuf, size)
85910758Seric 	char hostbuf[];
86012313Seric 	int size;
86110758Seric {
86258110Seric 	register struct hostent *hp;
86310758Seric 	extern struct hostent *gethostbyname();
86410758Seric 
86523120Seric 	if (gethostname(hostbuf, size) < 0)
86623120Seric 	{
86723120Seric 		(void) strcpy(hostbuf, "localhost");
86823120Seric 	}
86911147Seric 	hp = gethostbyname(hostbuf);
87011147Seric 	if (hp != NULL)
87116877Seric 	{
87266812Seric 		(void) strncpy(hostbuf, hp->h_name, size - 1);
87366812Seric 		hostbuf[size - 1] = '\0';
87466777Seric #ifdef NAMED_BIND
87566812Seric 		/* if still no dot, try DNS directly (i.e., avoid NIS) */
87666812Seric 		if (strchr(hostbuf, '.') == NULL)
87766777Seric 		{
87866777Seric 			extern bool getcanonname();
87958110Seric 
88066777Seric 			(void) getcanonname(hostbuf, size, TRUE);
88166777Seric 		}
88266777Seric #endif
88366777Seric 
88458110Seric 		if (hp->h_addrtype == AF_INET && hp->h_length == 4)
88558110Seric 		{
88658110Seric 			register int i;
88758110Seric 
88864338Seric 			for (i = 0; hp->h_addr_list[i] != NULL; i++)
88958110Seric 			{
89064338Seric 				char ipbuf[100];
89164338Seric 
89264338Seric 				sprintf(ipbuf, "[%s]",
89364338Seric 					inet_ntoa(*((struct in_addr *) hp->h_addr_list[i])));
89464338Seric 				setclass('w', ipbuf);
89558110Seric 			}
89658110Seric 		}
89758110Seric 
89811147Seric 		return (hp->h_aliases);
89916877Seric 	}
90010758Seric 	else
90110758Seric 		return (NULL);
90210758Seric }
90351315Seric /*
90458951Seric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
90558308Seric **
90658951Seric **	Uses RFC1413 protocol to try to get info from the other end.
90758951Seric **
90858308Seric **	Parameters:
90958308Seric **		fd -- the descriptor
91058308Seric **
91158308Seric **	Returns:
91258951Seric **		The user@host information associated with this descriptor.
91358308Seric */
91458308Seric 
91564927Seric #if IDENTPROTO
91658951Seric 
91758951Seric static jmp_buf	CtxAuthTimeout;
91858951Seric 
91958951Seric static
92058951Seric authtimeout()
92158951Seric {
92258951Seric 	longjmp(CtxAuthTimeout, 1);
92358951Seric }
92458951Seric 
92558951Seric #endif
92658951Seric 
92758308Seric char *
92858951Seric getauthinfo(fd)
92958308Seric 	int fd;
93058308Seric {
93158951Seric 	int falen;
93259104Seric 	register char *p;
93364927Seric #if IDENTPROTO
93458951Seric 	SOCKADDR la;
93558951Seric 	int lalen;
93658951Seric 	register struct servent *sp;
93758951Seric 	int s;
93858951Seric 	int i;
93958951Seric 	EVENT *ev;
94058951Seric #endif
94158951Seric 	static char hbuf[MAXNAME * 2 + 2];
94258951Seric 	extern char *hostnamebyanyaddr();
94358951Seric 	extern char RealUserName[];			/* main.c */
94458308Seric 
94566761Seric 	falen = sizeof RealHostAddr;
94666761Seric 	if (getpeername(fd, &RealHostAddr.sa, &falen) < 0 || falen <= 0 ||
94766761Seric 	    RealHostAddr.sa.sa_family == 0)
94858951Seric 	{
94958951Seric 		(void) sprintf(hbuf, "%s@localhost", RealUserName);
95058957Seric 		if (tTd(9, 1))
95158951Seric 			printf("getauthinfo: %s\n", hbuf);
95258951Seric 		return hbuf;
95358951Seric 	}
95458951Seric 
95566761Seric 	if (RealHostName == NULL)
95666761Seric 	{
95766761Seric 		/* translate that to a host name */
95866761Seric 		RealHostName = newstr(hostnamebyanyaddr(&RealHostAddr));
95966761Seric 	}
96066761Seric 
96164927Seric #if IDENTPROTO
96265831Seric 	if (TimeOuts.to_ident == 0)
96365831Seric 		goto noident;
96465831Seric 
96558951Seric 	lalen = sizeof la;
96666761Seric 	if (RealHostAddr.sa.sa_family != AF_INET ||
96758951Seric 	    getsockname(fd, &la.sa, &lalen) < 0 || lalen <= 0 ||
96858951Seric 	    la.sa.sa_family != AF_INET)
96958951Seric 	{
97058951Seric 		/* no ident info */
97158951Seric 		goto noident;
97258951Seric 	}
97358951Seric 
97458951Seric 	/* create ident query */
97560489Seric 	(void) sprintf(hbuf, "%d,%d\r\n",
97666761Seric 		ntohs(RealHostAddr.sin.sin_port), ntohs(la.sin.sin_port));
97758951Seric 
97858951Seric 	/* create local address */
97964747Seric 	la.sin.sin_port = 0;
98058951Seric 
98158951Seric 	/* create foreign address */
98258951Seric 	sp = getservbyname("auth", "tcp");
98358951Seric 	if (sp != NULL)
98466761Seric 		RealHostAddr.sin.sin_port = sp->s_port;
98558308Seric 	else
98666761Seric 		RealHostAddr.sin.sin_port = htons(113);
98758951Seric 
98858951Seric 	s = -1;
98958951Seric 	if (setjmp(CtxAuthTimeout) != 0)
99058951Seric 	{
99158951Seric 		if (s >= 0)
99258951Seric 			(void) close(s);
99358951Seric 		goto noident;
99458951Seric 	}
99558951Seric 
99658951Seric 	/* put a timeout around the whole thing */
99764255Seric 	ev = setevent(TimeOuts.to_ident, authtimeout, 0);
99858951Seric 
99964747Seric 	/* connect to foreign IDENT server using same address as SMTP socket */
100058951Seric 	s = socket(AF_INET, SOCK_STREAM, 0);
100158951Seric 	if (s < 0)
100258951Seric 	{
100358951Seric 		clrevent(ev);
100458951Seric 		goto noident;
100558951Seric 	}
100664747Seric 	if (bind(s, &la.sa, sizeof la.sin) < 0 ||
100766761Seric 	    connect(s, &RealHostAddr.sa, sizeof RealHostAddr.sin) < 0)
100858951Seric 	{
100966011Seric 		goto closeident;
101058951Seric 	}
101158951Seric 
101258957Seric 	if (tTd(9, 10))
101358951Seric 		printf("getauthinfo: sent %s", hbuf);
101458951Seric 
101558951Seric 	/* send query */
101658951Seric 	if (write(s, hbuf, strlen(hbuf)) < 0)
101758951Seric 		goto closeident;
101858951Seric 
101958951Seric 	/* get result */
102058951Seric 	i = read(s, hbuf, sizeof hbuf);
102158951Seric 	(void) close(s);
102258951Seric 	clrevent(ev);
102358951Seric 	if (i <= 0)
102458951Seric 		goto noident;
102558951Seric 	if (hbuf[--i] == '\n' && hbuf[--i] == '\r')
102658951Seric 		i--;
102758951Seric 	hbuf[++i] = '\0';
102858951Seric 
102958957Seric 	if (tTd(9, 3))
103058951Seric 		printf("getauthinfo:  got %s\n", hbuf);
103158951Seric 
103258951Seric 	/* parse result */
103358951Seric 	p = strchr(hbuf, ':');
103458951Seric 	if (p == NULL)
103558951Seric 	{
103658951Seric 		/* malformed response */
103758951Seric 		goto noident;
103858951Seric 	}
103958951Seric 	while (isascii(*++p) && isspace(*p))
104058951Seric 		continue;
104158951Seric 	if (strncasecmp(p, "userid", 6) != 0)
104258951Seric 	{
104358951Seric 		/* presumably an error string */
104458951Seric 		goto noident;
104558951Seric 	}
104658951Seric 	p += 6;
104758951Seric 	while (isascii(*p) && isspace(*p))
104858951Seric 		p++;
104958951Seric 	if (*p++ != ':')
105058951Seric 	{
105158951Seric 		/* either useridxx or malformed response */
105258951Seric 		goto noident;
105358951Seric 	}
105458951Seric 
105558951Seric 	/* p now points to the OSTYPE field */
105658951Seric 	p = strchr(p, ':');
105758951Seric 	if (p == NULL)
105858951Seric 	{
105958951Seric 		/* malformed response */
106058951Seric 		goto noident;
106158951Seric 	}
106258951Seric 
106358957Seric 	/* 1413 says don't do this -- but it's broken otherwise */
106458957Seric 	while (isascii(*++p) && isspace(*p))
106558957Seric 		continue;
106658957Seric 
106758951Seric 	/* p now points to the authenticated name */
106866003Seric 	(void) sprintf(hbuf, "%s@%s",
106966003Seric 		p, RealHostName == NULL ? "localhost" : RealHostName);
107058957Seric 	goto finish;
107158957Seric 
107266011Seric closeident:
107366011Seric 	(void) close(s);
107466011Seric 	clrevent(ev);
107566011Seric 
107658957Seric #endif /* IDENTPROTO */
107758957Seric 
107858957Seric noident:
107966003Seric 	if (RealHostName == NULL)
108066003Seric 	{
108166003Seric 		if (tTd(9, 1))
108266003Seric 			printf("getauthinfo: NULL\n");
108366003Seric 		return NULL;
108466003Seric 	}
108558957Seric 	(void) strcpy(hbuf, RealHostName);
108658957Seric 
108758957Seric finish:
108866003Seric 	if (RealHostName != NULL && RealHostName[0] != '[')
108958951Seric 	{
109058951Seric 		p = &hbuf[strlen(hbuf)];
109158951Seric 		(void) sprintf(p, " [%s]", anynet_ntoa(&RealHostAddr));
109258951Seric 	}
109358957Seric 	if (tTd(9, 1))
109458951Seric 		printf("getauthinfo: %s\n", hbuf);
109558308Seric 	return hbuf;
109658308Seric }
109758308Seric /*
109860089Seric **  HOST_MAP_LOOKUP -- turn a hostname into canonical form
109953751Seric **
110053751Seric **	Parameters:
110156823Seric **		map -- a pointer to this map (unused).
110260089Seric **		name -- the (presumably unqualified) hostname.
110360257Seric **		av -- unused -- for compatibility with other mapping
110455019Seric **			functions.
110559084Seric **		statp -- an exit status (out parameter) -- set to
110659084Seric **			EX_TEMPFAIL if the name server is unavailable.
110753751Seric **
110853751Seric **	Returns:
110953751Seric **		The mapping, if found.
111053751Seric **		NULL if no mapping found.
111153751Seric **
111253751Seric **	Side Effects:
111353751Seric **		Looks up the host specified in hbuf.  If it is not
111453751Seric **		the canonical name for that host, return the canonical
111553751Seric **		name.
111653751Seric */
111751315Seric 
111853751Seric char *
111960257Seric host_map_lookup(map, name, av, statp)
112056823Seric 	MAP *map;
112160089Seric 	char *name;
112260257Seric 	char **av;
112359084Seric 	int *statp;
112416911Seric {
112516911Seric 	register struct hostent *hp;
112633932Sbostic 	u_long in_addr;
112756823Seric 	char *cp;
112858110Seric 	int i;
112959671Seric 	register STAB *s;
113060257Seric 	char hbuf[MAXNAME];
113159671Seric 	extern struct hostent *gethostbyaddr();
113266334Seric #if NAMED_BIND
113359671Seric 	extern int h_errno;
113466029Seric #endif
113516911Seric 
113625574Smiriam 	/*
113759671Seric 	**  See if we have already looked up this name.  If so, just
113859671Seric 	**  return it.
113959671Seric 	*/
114053751Seric 
114160089Seric 	s = stab(name, ST_NAMECANON, ST_ENTER);
114259671Seric 	if (bitset(NCF_VALID, s->s_namecanon.nc_flags))
114359671Seric 	{
114459986Seric 		if (tTd(9, 1))
114560089Seric 			printf("host_map_lookup(%s) => CACHE %s\n",
114660089Seric 				name, s->s_namecanon.nc_cname);
114759671Seric 		errno = s->s_namecanon.nc_errno;
114866334Seric #if NAMED_BIND
114959671Seric 		h_errno = s->s_namecanon.nc_herrno;
115066029Seric #endif
115159671Seric 		*statp = s->s_namecanon.nc_stat;
115264797Seric 		if (CurEnv->e_message == NULL && *statp == EX_TEMPFAIL)
115365199Seric 		{
115465199Seric 			sprintf(hbuf, "%s: Name server timeout",
115565199Seric 				shortenstring(name, 33));
115665199Seric 			CurEnv->e_message = newstr(hbuf);
115765199Seric 		}
115859671Seric 		return s->s_namecanon.nc_cname;
115959671Seric 	}
116059671Seric 
116159671Seric 	/*
116259671Seric 	**  If first character is a bracket, then it is an address
116359671Seric 	**  lookup.  Address is copied into a temporary buffer to
116460089Seric 	**  strip the brackets and to preserve name if address is
116559671Seric 	**  unknown.
116659671Seric 	*/
116759671Seric 
116860089Seric 	if (*name != '[')
116953751Seric 	{
117055019Seric 		extern bool getcanonname();
117155019Seric 
117258798Seric 		if (tTd(9, 1))
117360089Seric 			printf("host_map_lookup(%s) => ", name);
117459671Seric 		s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
117560089Seric 		(void) strcpy(hbuf, name);
117663842Seric 		if (getcanonname(hbuf, sizeof hbuf - 1, TRUE))
117758796Seric 		{
117858796Seric 			if (tTd(9, 1))
117958796Seric 				printf("%s\n", hbuf);
118060257Seric 			cp = map_rewrite(map, hbuf, strlen(hbuf), av);
118160257Seric 			s->s_namecanon.nc_cname = newstr(cp);
118260257Seric 			return cp;
118358796Seric 		}
118453751Seric 		else
118558796Seric 		{
118659084Seric 			register struct hostent *hp;
118759084Seric 
118866029Seric 			s->s_namecanon.nc_errno = errno;
118966334Seric #if NAMED_BIND
119066029Seric 			s->s_namecanon.nc_herrno = h_errno;
119158796Seric 			if (tTd(9, 1))
119259084Seric 				printf("FAIL (%d)\n", h_errno);
119359084Seric 			switch (h_errno)
119459084Seric 			{
119559084Seric 			  case TRY_AGAIN:
119659596Seric 				if (UseNameServer)
119759734Seric 				{
119865202Seric 					sprintf(hbuf, "%s: Name server timeout",
119965199Seric 						shortenstring(name, 33));
120065202Seric 					message("%s", hbuf);
120159734Seric 					if (CurEnv->e_message == NULL)
120265202Seric 						CurEnv->e_message = newstr(hbuf);
120359734Seric 				}
120459084Seric 				*statp = EX_TEMPFAIL;
120559084Seric 				break;
120659084Seric 
120759084Seric 			  case HOST_NOT_FOUND:
120859084Seric 				*statp = EX_NOHOST;
120959084Seric 				break;
121059084Seric 
121159084Seric 			  case NO_RECOVERY:
121259084Seric 				*statp = EX_SOFTWARE;
121359084Seric 				break;
121459084Seric 
121559084Seric 			  default:
121659084Seric 				*statp = EX_UNAVAILABLE;
121759084Seric 				break;
121859084Seric 			}
121966029Seric #else
122066029Seric 			if (tTd(9, 1))
122166029Seric 				printf("FAIL\n");
122266029Seric 			*statp = EX_NOHOST;
122366029Seric #endif
122459671Seric 			s->s_namecanon.nc_stat = *statp;
122559084Seric 			if (*statp != EX_TEMPFAIL || UseNameServer)
122659084Seric 				return NULL;
122759084Seric 
122859084Seric 			/*
122959084Seric 			**  Try to look it up in /etc/hosts
123059084Seric 			*/
123159084Seric 
123260089Seric 			hp = gethostbyname(name);
123359084Seric 			if (hp == NULL)
123459084Seric 			{
123559084Seric 				/* no dice there either */
123659671Seric 				s->s_namecanon.nc_stat = *statp = EX_NOHOST;
123759084Seric 				return NULL;
123859084Seric 			}
123959084Seric 
124059671Seric 			s->s_namecanon.nc_stat = *statp = EX_OK;
124160257Seric 			cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
124260257Seric 			s->s_namecanon.nc_cname = newstr(cp);
124360257Seric 			return cp;
124458796Seric 		}
124553751Seric 	}
124660089Seric 	if ((cp = strchr(name, ']')) == NULL)
124753751Seric 		return (NULL);
124840994Sbostic 	*cp = '\0';
124960089Seric 	in_addr = inet_addr(&name[1]);
125058110Seric 
125158110Seric 	/* nope -- ask the name server */
125233932Sbostic 	hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET);
125359671Seric 	s->s_namecanon.nc_errno = errno;
125466334Seric #if NAMED_BIND
125559671Seric 	s->s_namecanon.nc_herrno = h_errno;
125666029Seric #endif
125759671Seric 	s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
125833932Sbostic 	if (hp == NULL)
125959671Seric 	{
126059671Seric 		s->s_namecanon.nc_stat = *statp = EX_NOHOST;
126153751Seric 		return (NULL);
126259671Seric 	}
126353751Seric 
126458110Seric 	/* found a match -- copy out */
126560257Seric 	cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
126659671Seric 	s->s_namecanon.nc_stat = *statp = EX_OK;
126760257Seric 	s->s_namecanon.nc_cname = newstr(cp);
126860257Seric 	return cp;
126933932Sbostic }
127058755Seric /*
127158755Seric **  ANYNET_NTOA -- convert a network address to printable form.
127258755Seric **
127358755Seric **	Parameters:
127458755Seric **		sap -- a pointer to a sockaddr structure.
127558755Seric **
127658755Seric **	Returns:
127758755Seric **		A printable version of that sockaddr.
127858755Seric */
127916911Seric 
128058755Seric char *
128158755Seric anynet_ntoa(sap)
128258755Seric 	register SOCKADDR *sap;
128358755Seric {
128458755Seric 	register char *bp;
128558755Seric 	register char *ap;
128658755Seric 	int l;
128764734Seric 	static char buf[100];
128858755Seric 
128958798Seric 	/* check for null/zero family */
129058798Seric 	if (sap == NULL)
129158798Seric 		return "NULLADDR";
129258798Seric 	if (sap->sa.sa_family == 0)
129358798Seric 		return "0";
129458798Seric 
129564734Seric 	switch (sap->sa.sa_family)
129664734Seric 	{
129764734Seric #ifdef MAYBENEXTRELEASE		/*** UNTESTED *** UNTESTED *** UNTESTED ***/
129864821Seric #ifdef NETUNIX
129964734Seric 	  case AF_UNIX:
130064758Seric 	  	if (sap->sunix.sun_path[0] != '\0')
130164758Seric 	  		sprintf(buf, "[UNIX: %.64s]", sap->sunix.sun_path);
130264734Seric 	  	else
130364734Seric 	  		sprintf(buf, "[UNIX: localhost]");
130464734Seric 		return buf;
130564734Seric #endif
130664821Seric #endif
130764734Seric 
130858778Seric #ifdef NETINET
130964734Seric 	  case AF_INET:
131058755Seric 		return inet_ntoa(((struct sockaddr_in *) sap)->sin_addr);
131158778Seric #endif
131258755Seric 
131364734Seric 	  default:
131464734Seric 	  	/* this case is only to ensure syntactic correctness */
131564734Seric 	  	break;
131664734Seric 	}
131764734Seric 
131858755Seric 	/* unknown family -- just dump bytes */
131958778Seric 	(void) sprintf(buf, "Family %d: ", sap->sa.sa_family);
132058755Seric 	bp = &buf[strlen(buf)];
132158778Seric 	ap = sap->sa.sa_data;
132258778Seric 	for (l = sizeof sap->sa.sa_data; --l >= 0; )
132358755Seric 	{
132458755Seric 		(void) sprintf(bp, "%02x:", *ap++ & 0377);
132558755Seric 		bp += 3;
132658755Seric 	}
132758755Seric 	*--bp = '\0';
132858755Seric 	return buf;
132958755Seric }
133058951Seric /*
133158951Seric **  HOSTNAMEBYANYADDR -- return name of host based on address
133258951Seric **
133358951Seric **	Parameters:
133458951Seric **		sap -- SOCKADDR pointer
133558951Seric **
133658951Seric **	Returns:
133758951Seric **		text representation of host name.
133858951Seric **
133958951Seric **	Side Effects:
134058951Seric **		none.
134158951Seric */
134258755Seric 
134358951Seric char *
134458951Seric hostnamebyanyaddr(sap)
134558951Seric 	register SOCKADDR *sap;
134658951Seric {
134758951Seric 	register struct hostent *hp;
134864734Seric 	int saveretry;
134958951Seric 
135066334Seric #if NAMED_BIND
135159042Seric 	/* shorten name server timeout to avoid higher level timeouts */
135259042Seric 	saveretry = _res.retry;
135359042Seric 	_res.retry = 3;
135459042Seric #endif /* NAMED_BIND */
135559042Seric 
135658951Seric 	switch (sap->sa.sa_family)
135758951Seric 	{
135858951Seric #ifdef NETINET
135958951Seric 	  case AF_INET:
136058951Seric 		hp = gethostbyaddr((char *) &sap->sin.sin_addr,
136158951Seric 			sizeof sap->sin.sin_addr,
136258951Seric 			AF_INET);
136358951Seric 		break;
136458951Seric #endif
136558951Seric 
136658951Seric #ifdef NETISO
136758951Seric 	  case AF_ISO:
136858951Seric 		hp = gethostbyaddr((char *) &sap->siso.siso_addr,
136958951Seric 			sizeof sap->siso.siso_addr,
137058951Seric 			AF_ISO);
137158951Seric 		break;
137258951Seric #endif
137358951Seric 
137464734Seric #ifdef MAYBENEXTRELEASE		/*** UNTESTED *** UNTESTED *** UNTESTED ***/
137564734Seric 	  case AF_UNIX:
137664734Seric 		hp = NULL;
137764734Seric 		break;
137864734Seric #endif
137964734Seric 
138058951Seric 	  default:
138158951Seric 		hp = gethostbyaddr(sap->sa.sa_data,
138258951Seric 			   sizeof sap->sa.sa_data,
138358951Seric 			   sap->sa.sa_family);
138458951Seric 		break;
138558951Seric 	}
138658951Seric 
138766334Seric #if NAMED_BIND
138859042Seric 	_res.retry = saveretry;
138959042Seric #endif /* NAMED_BIND */
139059042Seric 
139158951Seric 	if (hp != NULL)
139258951Seric 		return hp->h_name;
139358951Seric 	else
139458951Seric 	{
139558951Seric 		/* produce a dotted quad */
139658951Seric 		static char buf[512];
139758951Seric 
139858951Seric 		(void) sprintf(buf, "[%s]", anynet_ntoa(sap));
139958951Seric 		return buf;
140058951Seric 	}
140158951Seric }
140258951Seric 
140356795Seric # else /* DAEMON */
140416911Seric /* code for systems without sophisticated networking */
140510758Seric 
140610758Seric /*
140710758Seric **  MYHOSTNAME -- stub version for case of no daemon code.
140811297Seric **
140911297Seric **	Can't convert to upper case here because might be a UUCP name.
141012313Seric **
141112313Seric **	Mark, you can change this to be anything you want......
141210758Seric */
141310758Seric 
141410758Seric char **
141512313Seric myhostname(hostbuf, size)
141610758Seric 	char hostbuf[];
141712313Seric 	int size;
141810758Seric {
141910758Seric 	register FILE *f;
142010758Seric 
142110758Seric 	hostbuf[0] = '\0';
142210758Seric 	f = fopen("/usr/include/whoami", "r");
142310758Seric 	if (f != NULL)
142410758Seric 	{
142512313Seric 		(void) fgets(hostbuf, size, f);
142610758Seric 		fixcrlf(hostbuf, TRUE);
142710758Seric 		(void) fclose(f);
142810758Seric 	}
142910758Seric 	return (NULL);
143010758Seric }
143116911Seric /*
143258951Seric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
143358308Seric **
143458308Seric **	Parameters:
143558308Seric **		fd -- the descriptor
143658308Seric **
143758308Seric **	Returns:
143858308Seric **		The host name associated with this descriptor, if it can
143958308Seric **			be determined.
144058308Seric **		NULL otherwise.
144158308Seric **
144258308Seric **	Side Effects:
144358308Seric **		none
144458308Seric */
144558308Seric 
144658308Seric char *
144758951Seric getauthinfo(fd)
144858308Seric 	int fd;
144958308Seric {
145058308Seric 	return NULL;
145158308Seric }
145258308Seric /*
145316911Seric **  MAPHOSTNAME -- turn a hostname into canonical form
145416911Seric **
145516911Seric **	Parameters:
145656823Seric **		map -- a pointer to the database map.
145760089Seric **		name -- a buffer containing a hostname.
145853751Seric **		avp -- a pointer to a (cf file defined) argument vector.
145959084Seric **		statp -- an exit status (out parameter).
146016911Seric **
146116911Seric **	Returns:
146253751Seric **		mapped host name
146351315Seric **		FALSE otherwise.
146416911Seric **
146516911Seric **	Side Effects:
146660089Seric **		Looks up the host specified in name.  If it is not
146716911Seric **		the canonical name for that host, replace it with
146816911Seric **		the canonical name.  If the name is unknown, or it
146916911Seric **		is already the canonical name, leave it unchanged.
147016911Seric */
147110758Seric 
147216911Seric /*ARGSUSED*/
147353751Seric char *
147460089Seric host_map_lookup(map, name, avp, statp)
147556823Seric 	MAP *map;
147660089Seric 	char *name;
147753751Seric 	char **avp;
147859084Seric 	char *statp;
147916911Seric {
148059084Seric 	register struct hostent *hp;
148159084Seric 
148260089Seric 	hp = gethostbyname(name);
148359084Seric 	if (hp != NULL)
148459084Seric 		return hp->h_name;
148559084Seric 	*statp = EX_NOHOST;
148653751Seric 	return NULL;
148716911Seric }
148816911Seric 
148956795Seric #endif /* DAEMON */
1490