xref: /csrg-svn/usr.sbin/sendmail/src/daemon.c (revision 66777)
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*66777Seric static char sccsid[] = "@(#)daemon.c	8.41 (Berkeley) 04/13/94 (with daemon mode)";
1533780Sbostic #else
16*66777Seric static char sccsid[] = "@(#)daemon.c	8.41 (Berkeley) 04/13/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;
8625027Seric 	int on = 1;
8753751Seric 	bool refusingconnections = TRUE;
8858419Seric 	FILE *pidf;
8964828Seric 	int socksize;
9046928Sbostic 	extern void reapchild();
917117Seric 
929610Seric 	/*
939610Seric 	**  Set up the address for the mailer.
949610Seric 	*/
959610Seric 
9658849Seric 	if (DaemonAddr.sin.sin_family == 0)
9758849Seric 		DaemonAddr.sin.sin_family = AF_INET;
9858849Seric 	if (DaemonAddr.sin.sin_addr.s_addr == 0)
9958849Seric 		DaemonAddr.sin.sin_addr.s_addr = INADDR_ANY;
10058849Seric 	if (DaemonAddr.sin.sin_port == 0)
1019610Seric 	{
10265169Seric 		register struct servent *sp;
10365169Seric 
10458849Seric 		sp = getservbyname("smtp", "tcp");
10558849Seric 		if (sp == NULL)
10658849Seric 		{
10758909Seric 			syserr("554 service \"smtp\" unknown");
10865169Seric 			DaemonAddr.sin.sin_port = htons(25);
10958849Seric 		}
11065169Seric 		else
11165169Seric 			DaemonAddr.sin.sin_port = sp->s_port;
1129610Seric 	}
1139610Seric 
1149610Seric 	/*
1159610Seric 	**  Try to actually open the connection.
1169610Seric 	*/
1179610Seric 
1189610Seric 	if (tTd(15, 1))
11958849Seric 		printf("getrequests: port 0x%x\n", DaemonAddr.sin.sin_port);
1209610Seric 
1219610Seric 	/* get a socket for the SMTP connection */
12259041Seric 	DaemonSocket = socket(DaemonAddr.sa.sa_family, SOCK_STREAM, 0);
12310206Seric 	if (DaemonSocket < 0)
1249610Seric 	{
1259610Seric 		/* probably another daemon already */
1269610Seric 		syserr("getrequests: can't create socket");
1279610Seric 	  severe:
1289610Seric # ifdef LOG
1299610Seric 		if (LogLevel > 0)
13057663Seric 			syslog(LOG_ALERT, "problem creating SMTP socket");
13156795Seric # endif /* LOG */
1329610Seric 		finis();
1339610Seric 	}
13410347Seric 
13510347Seric 	/* turn on network debugging? */
13656328Seric 	if (tTd(15, 101))
13724945Seric 		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on);
13810347Seric 
13925027Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on);
14025027Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on);
14125027Seric 
14264391Seric #ifdef SO_RCVBUF
14364391Seric 	if (TcpRcvBufferSize > 0)
14464391Seric 	{
14564391Seric 		if (setsockopt(DaemonSocket, SOL_SOCKET, SO_RCVBUF,
14664561Seric 			       (char *) &TcpRcvBufferSize,
14764391Seric 			       sizeof(TcpRcvBufferSize)) < 0)
14864391Seric 			syserr("getrequests: setsockopt(SO_RCVBUF)");
14964391Seric 	}
15064391Seric #endif
15164391Seric 
15259041Seric 	switch (DaemonAddr.sa.sa_family)
1539610Seric 	{
15459041Seric # ifdef NETINET
15559041Seric 	  case AF_INET:
15664828Seric 		socksize = sizeof DaemonAddr.sin;
15759041Seric 		break;
15859041Seric # endif
15959041Seric 
16059041Seric # ifdef NETISO
16159041Seric 	  case AF_ISO:
16264828Seric 		socksize = sizeof DaemonAddr.siso;
16359041Seric 		break;
16459041Seric # endif
16559041Seric 
16659041Seric 	  default:
16764828Seric 		socksize = sizeof DaemonAddr;
16859041Seric 		break;
16959041Seric 	}
17059041Seric 
17164828Seric 	if (bind(DaemonSocket, &DaemonAddr.sa, socksize) < 0)
17259041Seric 	{
1739610Seric 		syserr("getrequests: cannot bind");
17410206Seric 		(void) close(DaemonSocket);
1759610Seric 		goto severe;
1769610Seric 	}
1779610Seric 
17864035Seric 	(void) setsignal(SIGCHLD, reapchild);
17924945Seric 
18058419Seric 	/* write the pid to the log file for posterity */
18158419Seric 	pidf = fopen(PidFile, "w");
18258419Seric 	if (pidf != NULL)
18358419Seric 	{
18463863Seric 		extern char *CommandLineArgs;
18563863Seric 
18663863Seric 		/* write the process id on line 1 */
18758419Seric 		fprintf(pidf, "%d\n", getpid());
18863863Seric 
18963863Seric 		/* line 2 contains all command line flags */
19063863Seric 		fprintf(pidf, "%s\n", CommandLineArgs);
19163863Seric 
19263863Seric 		/* flush and close */
19358419Seric 		fclose(pidf);
19458419Seric 	}
19558419Seric 
19658419Seric 
1979610Seric 	if (tTd(15, 1))
19810206Seric 		printf("getrequests: %d\n", DaemonSocket);
1999610Seric 
2004631Seric 	for (;;)
2014631Seric 	{
20214875Seric 		register int pid;
20311147Seric 		auto int lotherend;
20453751Seric 		extern bool refuseconnections();
20511147Seric 
20614875Seric 		/* see if we are rejecting connections */
20753751Seric 		CurrentLA = getla();
20853751Seric 		if (refuseconnections())
20936584Sbostic 		{
21053751Seric 			if (!refusingconnections)
21153751Seric 			{
21253751Seric 				/* don't queue so peer will fail quickly */
21353751Seric 				(void) listen(DaemonSocket, 0);
21453751Seric 				refusingconnections = TRUE;
21553751Seric 			}
21657385Seric 			setproctitle("rejecting connections: load average: %d",
21757385Seric 				CurrentLA);
21814875Seric 			sleep(5);
21953751Seric 			continue;
22036584Sbostic 		}
22114875Seric 
22253751Seric 		if (refusingconnections)
22353751Seric 		{
22453751Seric 			/* start listening again */
22559783Seric 			if (listen(DaemonSocket, ListenQueueSize) < 0)
22653751Seric 			{
22753751Seric 				syserr("getrequests: cannot listen");
22853751Seric 				(void) close(DaemonSocket);
22953751Seric 				goto severe;
23053751Seric 			}
23153751Seric 			setproctitle("accepting connections");
23253751Seric 			refusingconnections = FALSE;
23353751Seric 		}
23453751Seric 
2359610Seric 		/* wait for a connection */
2369610Seric 		do
2379610Seric 		{
2389610Seric 			errno = 0;
23964828Seric 			lotherend = socksize;
24046928Sbostic 			t = accept(DaemonSocket,
24146928Sbostic 			    (struct sockaddr *)&RealHostAddr, &lotherend);
2429610Seric 		} while (t < 0 && errno == EINTR);
2439610Seric 		if (t < 0)
2445978Seric 		{
2459610Seric 			syserr("getrequests: accept");
2469610Seric 			sleep(5);
2479610Seric 			continue;
2485978Seric 		}
2494631Seric 
2505978Seric 		/*
2515978Seric 		**  Create a subprocess to process the mail.
2525978Seric 		*/
2535978Seric 
2547677Seric 		if (tTd(15, 2))
2559610Seric 			printf("getrequests: forking (fd = %d)\n", t);
2565978Seric 
2574636Seric 		pid = fork();
2584636Seric 		if (pid < 0)
2594631Seric 		{
2604636Seric 			syserr("daemon: cannot fork");
2614636Seric 			sleep(10);
2629610Seric 			(void) close(t);
2634636Seric 			continue;
2644631Seric 		}
2654631Seric 
2664636Seric 		if (pid == 0)
2674631Seric 		{
26864086Seric 			char *p;
26958951Seric 			extern char *hostnamebyanyaddr();
27011147Seric 
2714636Seric 			/*
2724636Seric 			**  CHILD -- return to caller.
27311147Seric 			**	Collect verified idea of sending host.
2744636Seric 			**	Verify calling user id if possible here.
2754636Seric 			*/
2764631Seric 
27764035Seric 			(void) setsignal(SIGCHLD, SIG_DFL);
27866017Seric 			DisConnected = FALSE;
27924950Seric 
28066032Seric 			setproctitle("startup with %s",
28166032Seric 				anynet_ntoa(&RealHostAddr));
28266032Seric 
28311147Seric 			/* determine host name */
28464086Seric 			p = hostnamebyanyaddr(&RealHostAddr);
28564086Seric 			RealHostName = newstr(p);
28666032Seric 			setproctitle("startup with %s", p);
28758778Seric 
28855173Seric #ifdef LOG
28963842Seric 			if (LogLevel > 11)
29055173Seric 			{
29155173Seric 				/* log connection information */
29255173Seric 				syslog(LOG_INFO, "connect from %s (%s)",
29358951Seric 					RealHostName, anynet_ntoa(&RealHostAddr));
29455173Seric 			}
29555173Seric #endif
29655173Seric 
29759254Seric 			(void) close(DaemonSocket);
29864724Seric 			if ((InChannel = fdopen(t, "r")) == NULL ||
29964724Seric 			    (t = dup(t)) < 0 ||
30064724Seric 			    (OutChannel = fdopen(t, "w")) == NULL)
30164724Seric 			{
30264724Seric 				syserr("cannot open SMTP server channel, fd=%d", t);
30364724Seric 				exit(0);
30464724Seric 			}
30559254Seric 
30616884Seric 			/* should we check for illegal connection here? XXX */
30759156Seric #ifdef XLA
30859156Seric 			if (!xla_host_ok(RealHostName))
30959156Seric 			{
31059254Seric 				message("421 Too many SMTP sessions for this host");
31159156Seric 				exit(0);
31259156Seric 			}
31359156Seric #endif
31416884Seric 
3157677Seric 			if (tTd(15, 2))
3165978Seric 				printf("getreq: returning\n");
3174636Seric 			return;
3184631Seric 		}
3194631Seric 
3207117Seric 		/* close the port so that others will hang (for a while) */
3219610Seric 		(void) close(t);
3224631Seric 	}
3239886Seric 	/*NOTREACHED*/
3244631Seric }
3255978Seric /*
32610206Seric **  CLRDAEMON -- reset the daemon connection
32710206Seric **
32810206Seric **	Parameters:
32910206Seric **		none.
33010206Seric **
33110206Seric **	Returns:
33210206Seric **		none.
33310206Seric **
33410206Seric **	Side Effects:
33510206Seric **		releases any resources used by the passive daemon.
33610206Seric */
33710206Seric 
33810206Seric clrdaemon()
33910206Seric {
34010206Seric 	if (DaemonSocket >= 0)
34110206Seric 		(void) close(DaemonSocket);
34210206Seric 	DaemonSocket = -1;
34310206Seric }
34410206Seric /*
34558849Seric **  SETDAEMONOPTIONS -- set options for running the daemon
34658849Seric **
34758849Seric **	Parameters:
34858849Seric **		p -- the options line.
34958849Seric **
35058849Seric **	Returns:
35158849Seric **		none.
35258849Seric */
35358849Seric 
35458849Seric setdaemonoptions(p)
35558849Seric 	register char *p;
35658849Seric {
35758873Seric 	if (DaemonAddr.sa.sa_family == AF_UNSPEC)
35858873Seric 		DaemonAddr.sa.sa_family = AF_INET;
35958873Seric 
36058849Seric 	while (p != NULL)
36158849Seric 	{
36258849Seric 		register char *f;
36358849Seric 		register char *v;
36458849Seric 
36558849Seric 		while (isascii(*p) && isspace(*p))
36658849Seric 			p++;
36758849Seric 		if (*p == '\0')
36858849Seric 			break;
36958849Seric 		f = p;
37058849Seric 		p = strchr(p, ',');
37158849Seric 		if (p != NULL)
37258849Seric 			*p++ = '\0';
37358849Seric 		v = strchr(f, '=');
37458849Seric 		if (v == NULL)
37558849Seric 			continue;
37658849Seric 		while (isascii(*++v) && isspace(*v))
37758849Seric 			continue;
37858849Seric 
37958849Seric 		switch (*f)
38058849Seric 		{
38158873Seric 		  case 'F':		/* address family */
38258849Seric 			if (isascii(*v) && isdigit(*v))
38358873Seric 				DaemonAddr.sa.sa_family = atoi(v);
38458873Seric #ifdef NETINET
38558873Seric 			else if (strcasecmp(v, "inet") == 0)
38658873Seric 				DaemonAddr.sa.sa_family = AF_INET;
38758873Seric #endif
38858873Seric #ifdef NETISO
38958873Seric 			else if (strcasecmp(v, "iso") == 0)
39058873Seric 				DaemonAddr.sa.sa_family = AF_ISO;
39158873Seric #endif
39258873Seric #ifdef NETNS
39358873Seric 			else if (strcasecmp(v, "ns") == 0)
39458873Seric 				DaemonAddr.sa.sa_family = AF_NS;
39558873Seric #endif
39658873Seric #ifdef NETX25
39758873Seric 			else if (strcasecmp(v, "x.25") == 0)
39858873Seric 				DaemonAddr.sa.sa_family = AF_CCITT;
39958873Seric #endif
40058849Seric 			else
40158873Seric 				syserr("554 Unknown address family %s in Family=option", v);
40258873Seric 			break;
40358873Seric 
40458873Seric 		  case 'A':		/* address */
40558873Seric 			switch (DaemonAddr.sa.sa_family)
40658849Seric 			{
40758873Seric #ifdef NETINET
40858873Seric 			  case AF_INET:
40958873Seric 				if (isascii(*v) && isdigit(*v))
41058873Seric 					DaemonAddr.sin.sin_addr.s_addr = inet_network(v);
41158873Seric 				else
41258873Seric 				{
41358873Seric 					register struct netent *np;
41458849Seric 
41558873Seric 					np = getnetbyname(v);
41658873Seric 					if (np == NULL)
41758873Seric 						syserr("554 network \"%s\" unknown", v);
41858873Seric 					else
41958873Seric 						DaemonAddr.sin.sin_addr.s_addr = np->n_net;
42058873Seric 				}
42158873Seric 				break;
42258873Seric #endif
42358873Seric 
42458873Seric 			  default:
42558873Seric 				syserr("554 Address= option unsupported for family %d",
42658873Seric 					DaemonAddr.sa.sa_family);
42758873Seric 				break;
42858849Seric 			}
42958849Seric 			break;
43058849Seric 
43158873Seric 		  case 'P':		/* port */
43258873Seric 			switch (DaemonAddr.sa.sa_family)
43358849Seric 			{
43458873Seric 				short port;
43558849Seric 
43658873Seric #ifdef NETINET
43758873Seric 			  case AF_INET:
43858873Seric 				if (isascii(*v) && isdigit(*v))
43964366Seric 					DaemonAddr.sin.sin_port = htons(atoi(v));
44058849Seric 				else
44158873Seric 				{
44258873Seric 					register struct servent *sp;
44358873Seric 
44458873Seric 					sp = getservbyname(v, "tcp");
44558873Seric 					if (sp == NULL)
44658909Seric 						syserr("554 service \"%s\" unknown", v);
44758873Seric 					else
44858873Seric 						DaemonAddr.sin.sin_port = sp->s_port;
44958873Seric 				}
45058873Seric 				break;
45158873Seric #endif
45258873Seric 
45358873Seric #ifdef NETISO
45458873Seric 			  case AF_ISO:
45558873Seric 				/* assume two byte transport selector */
45658873Seric 				if (isascii(*v) && isdigit(*v))
45764366Seric 					port = htons(atoi(v));
45858873Seric 				else
45958873Seric 				{
46058873Seric 					register struct servent *sp;
46158873Seric 
46258873Seric 					sp = getservbyname(v, "tcp");
46358873Seric 					if (sp == NULL)
46458909Seric 						syserr("554 service \"%s\" unknown", v);
46558873Seric 					else
46658873Seric 						port = sp->s_port;
46758873Seric 				}
46858873Seric 				bcopy((char *) &port, TSEL(&DaemonAddr.siso), 2);
46958873Seric 				break;
47058873Seric #endif
47158873Seric 
47258873Seric 			  default:
47358873Seric 				syserr("554 Port= option unsupported for family %d",
47458873Seric 					DaemonAddr.sa.sa_family);
47558873Seric 				break;
47658849Seric 			}
47758849Seric 			break;
47859783Seric 
47959783Seric 		  case 'L':		/* listen queue size */
48059783Seric 			ListenQueueSize = atoi(v);
48159783Seric 			break;
48264381Seric 
48364381Seric 		  case 'S':		/* send buffer size */
48464381Seric 			TcpSndBufferSize = atoi(v);
48564381Seric 			break;
48664381Seric 
48764381Seric 		  case 'R':		/* receive buffer size */
48864381Seric 			TcpRcvBufferSize = atoi(v);
48964381Seric 			break;
49058849Seric 		}
49158849Seric 	}
49258849Seric }
49358849Seric /*
4946039Seric **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
4956039Seric **
4966039Seric **	Parameters:
4976039Seric **		host -- the name of the host.
4986633Seric **		port -- the port number to connect to.
49953739Seric **		mci -- a pointer to the mail connection information
50053739Seric **			structure to be filled in.
50152106Seric **		usesecureport -- if set, use a low numbered (reserved)
50252106Seric **			port to provide some rudimentary authentication.
5036039Seric **
5046039Seric **	Returns:
5056039Seric **		An exit code telling whether the connection could be
5066039Seric **			made and if not why not.
5076039Seric **
5086039Seric **	Side Effects:
5096039Seric **		none.
5106039Seric */
5115978Seric 
51258755Seric SOCKADDR	CurHostAddr;		/* address of current host */
51358305Seric 
51454967Seric int
51553739Seric makeconnection(host, port, mci, usesecureport)
5166039Seric 	char *host;
5177286Seric 	u_short port;
51854967Seric 	register MCI *mci;
51952106Seric 	bool usesecureport;
5206039Seric {
52129430Sbloom 	register int i, s;
52229430Sbloom 	register struct hostent *hp = (struct hostent *)NULL;
52358755Seric 	SOCKADDR addr;
52452106Seric 	int sav_errno;
52558755Seric 	int addrlen;
52666334Seric #if NAMED_BIND
52735651Seric 	extern int h_errno;
52835651Seric #endif
5296039Seric 
5306039Seric 	/*
5316039Seric 	**  Set up the address for the mailer.
5329308Seric 	**	Accept "[a.b.c.d]" syntax for host name.
5336039Seric 	*/
5346039Seric 
53566334Seric #if NAMED_BIND
53625475Smiriam 	h_errno = 0;
53735651Seric #endif
53825475Smiriam 	errno = 0;
53958864Seric 	bzero(&CurHostAddr, sizeof CurHostAddr);
54064334Seric 	SmtpPhase = mci->mci_phase = "initial connection";
54158906Seric 	CurHostName = host;
54225475Smiriam 
5439308Seric 	if (host[0] == '[')
5449308Seric 	{
54511147Seric 		long hid;
54656795Seric 		register char *p = strchr(host, ']');
5479308Seric 
54811147Seric 		if (p != NULL)
5499308Seric 		{
55011147Seric 			*p = '\0';
55159884Seric #ifdef NETINET
55211147Seric 			hid = inet_addr(&host[1]);
55358360Seric 			if (hid == -1)
55459884Seric #endif
55558360Seric 			{
55658360Seric 				/* try it as a host name (avoid MX lookup) */
55758360Seric 				hp = gethostbyname(&host[1]);
55866349Seric 				if (hp == NULL && p[-1] == '.')
55966349Seric 				{
56066349Seric 					p[-1] = '\0';
56166349Seric 					hp = gethostbyname(&host[1]);
56266349Seric 					p[-1] = '.';
56366349Seric 				}
56458360Seric 				*p = ']';
56558360Seric 				goto gothostent;
56658360Seric 			}
56711147Seric 			*p = ']';
5689308Seric 		}
56958360Seric 		if (p == NULL)
5709308Seric 		{
57158151Seric 			usrerr("553 Invalid numeric domain spec \"%s\"", host);
5729308Seric 			return (EX_NOHOST);
5739308Seric 		}
57459884Seric #ifdef NETINET
57559884Seric 		addr.sin.sin_family = AF_INET;		/*XXX*/
57658778Seric 		addr.sin.sin_addr.s_addr = hid;
57759884Seric #endif
5789308Seric 	}
5799610Seric 	else
5809610Seric 	{
58166349Seric 		register char *p = &host[strlen(host) - 1];
58266349Seric 
58329430Sbloom 		hp = gethostbyname(host);
58466349Seric 		if (hp == NULL && *p == '.')
58566349Seric 		{
58666349Seric 			*p = '\0';
58766349Seric 			hp = gethostbyname(host);
58866349Seric 			*p = '.';
58966349Seric 		}
59058360Seric gothostent:
59125475Smiriam 		if (hp == NULL)
59224945Seric 		{
59366334Seric #if NAMED_BIND
59425475Smiriam 			if (errno == ETIMEDOUT || h_errno == TRY_AGAIN)
59525475Smiriam 				return (EX_TEMPFAIL);
59625657Seric 
59735651Seric 			/* if name server is specified, assume temp fail */
59835651Seric 			if (errno == ECONNREFUSED && UseNameServer)
59935651Seric 				return (EX_TEMPFAIL);
60035651Seric #endif
60125475Smiriam 			return (EX_NOHOST);
60224945Seric 		}
60358778Seric 		addr.sa.sa_family = hp->h_addrtype;
60458778Seric 		switch (hp->h_addrtype)
60558778Seric 		{
60658778Seric #ifdef NETINET
60758778Seric 		  case AF_INET:
60858755Seric 			bcopy(hp->h_addr,
60958778Seric 				&addr.sin.sin_addr,
61064943Seric 				sizeof addr.sin.sin_addr);
61158778Seric 			break;
61258778Seric #endif
61358778Seric 
61458778Seric 		  default:
61558755Seric 			bcopy(hp->h_addr,
61658778Seric 				addr.sa.sa_data,
61758755Seric 				hp->h_length);
61858778Seric 			break;
61958778Seric 		}
62029430Sbloom 		i = 1;
6219610Seric 	}
6229610Seric 
6239610Seric 	/*
6249610Seric 	**  Determine the port number.
6259610Seric 	*/
6269610Seric 
62710011Seric 	if (port != 0)
62858755Seric 		port = htons(port);
62910011Seric 	else
6309610Seric 	{
6319610Seric 		register struct servent *sp = getservbyname("smtp", "tcp");
6329610Seric 
6339610Seric 		if (sp == NULL)
6349610Seric 		{
63558909Seric 			syserr("554 makeconnection: service \"smtp\" unknown");
63665169Seric 			port = htons(25);
6379610Seric 		}
63865169Seric 		else
63965169Seric 			port = sp->s_port;
6409610Seric 	}
6416039Seric 
64258778Seric 	switch (addr.sa.sa_family)
64358755Seric 	{
64459884Seric #ifdef NETINET
64558755Seric 	  case AF_INET:
64658778Seric 		addr.sin.sin_port = port;
64758755Seric 		addrlen = sizeof (struct sockaddr_in);
64858755Seric 		break;
64959884Seric #endif
65058755Seric 
65158755Seric #ifdef NETISO
65258755Seric 	  case AF_ISO:
65358755Seric 		/* assume two byte transport selector */
65458755Seric 		bcopy((char *) &port, TSEL((struct sockaddr_iso *) &addr), 2);
65558755Seric 		addrlen = sizeof (struct sockaddr_iso);
65658755Seric 		break;
65758755Seric #endif
65858755Seric 
65958755Seric 	  default:
66058778Seric 		syserr("Can't connect to address family %d", addr.sa.sa_family);
66158755Seric 		return (EX_NOHOST);
66258755Seric 	}
66358755Seric 
6646039Seric 	/*
6656039Seric 	**  Try to actually open the connection.
6666039Seric 	*/
6676039Seric 
66859156Seric #ifdef XLA
66959156Seric 	/* if too many connections, don't bother trying */
67059156Seric 	if (!xla_noqueue_ok(host))
67159156Seric 		return EX_TEMPFAIL;
67259156Seric #endif
67359156Seric 
67457736Seric 	for (;;)
67552106Seric 	{
67657736Seric 		if (tTd(16, 1))
67758755Seric 			printf("makeconnection (%s [%s])\n",
67858755Seric 				host, anynet_ntoa(&addr));
67952106Seric 
68058588Seric 		/* save for logging */
68158588Seric 		CurHostAddr = addr;
68258588Seric 
68357736Seric 		if (usesecureport)
68457736Seric 		{
68557736Seric 			int rport = IPPORT_RESERVED - 1;
6866039Seric 
68757736Seric 			s = rresvport(&rport);
68857736Seric 		}
68957736Seric 		else
69057736Seric 		{
69157736Seric 			s = socket(AF_INET, SOCK_STREAM, 0);
69257736Seric 		}
69357736Seric 		if (s < 0)
69457736Seric 		{
69557736Seric 			sav_errno = errno;
69657736Seric 			syserr("makeconnection: no socket");
69757736Seric 			goto failure;
69857736Seric 		}
69910347Seric 
70064381Seric #ifdef SO_SNDBUF
70164381Seric 		if (TcpSndBufferSize > 0)
70264381Seric 		{
70364381Seric 			if (setsockopt(s, SOL_SOCKET, SO_SNDBUF,
70464561Seric 				       (char *) &TcpSndBufferSize,
70564381Seric 				       sizeof(TcpSndBufferSize)) < 0)
70664381Seric 				syserr("makeconnection: setsockopt(SO_SNDBUF)");
70764381Seric 		}
70864381Seric #endif
70964381Seric 
71057736Seric 		if (tTd(16, 1))
71157736Seric 			printf("makeconnection: fd=%d\n", s);
71257736Seric 
71357736Seric 		/* turn on network debugging? */
71457736Seric 		if (tTd(16, 101))
71557736Seric 		{
71657736Seric 			int on = 1;
71757736Seric 			(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG,
71857736Seric 					  (char *)&on, sizeof on);
71957736Seric 		}
72057736Seric 		if (CurEnv->e_xfp != NULL)
72157736Seric 			(void) fflush(CurEnv->e_xfp);		/* for debugging */
72257736Seric 		errno = 0;					/* for debugging */
72358755Seric 		if (connect(s, (struct sockaddr *) &addr, addrlen) >= 0)
72457736Seric 			break;
72557736Seric 
72657736Seric 		/* couldn't connect.... figure out why */
72727744Sbloom 		sav_errno = errno;
72827744Sbloom 		(void) close(s);
72929430Sbloom 		if (hp && hp->h_addr_list[i])
73029430Sbloom 		{
73157736Seric 			if (tTd(16, 1))
73258755Seric 				printf("Connect failed (%s); trying new address....\n",
73358755Seric 					errstring(sav_errno));
73458778Seric 			switch (addr.sa.sa_family)
73558778Seric 			{
73658778Seric #ifdef NETINET
73758778Seric 			  case AF_INET:
73858755Seric 				bcopy(hp->h_addr_list[i++],
73958778Seric 				      &addr.sin.sin_addr,
74064943Seric 				      sizeof addr.sin.sin_addr);
74158778Seric 				break;
74258778Seric #endif
74358778Seric 
74458778Seric 			  default:
74558755Seric 				bcopy(hp->h_addr_list[i++],
74658778Seric 					addr.sa.sa_data,
74752106Seric 					hp->h_length);
74858778Seric 				break;
74958778Seric 			}
75057736Seric 			continue;
75129430Sbloom 		}
75229430Sbloom 
7536039Seric 		/* failure, decide if temporary or not */
7546039Seric 	failure:
75559254Seric #ifdef XLA
75659254Seric 		xla_host_end(host);
75759254Seric #endif
75858542Seric 		if (transienterror(sav_errno))
75958542Seric 			return EX_TEMPFAIL;
76058542Seric 		else
76158542Seric 		{
76258542Seric 			message("%s", errstring(sav_errno));
76358542Seric 			return (EX_UNAVAILABLE);
7646039Seric 		}
7656039Seric 	}
7666039Seric 
7676039Seric 	/* connection ok, put it into canonical form */
76864724Seric 	if ((mci->mci_out = fdopen(s, "w")) == NULL ||
76964724Seric 	    (s = dup(s)) < 0 ||
77064725Seric 	    (mci->mci_in = fdopen(s, "r")) == NULL)
77164724Seric 	{
77264724Seric 		syserr("cannot open SMTP client channel, fd=%d", s);
77364724Seric 		return EX_TEMPFAIL;
77464724Seric 	}
7756039Seric 
77610098Seric 	return (EX_OK);
7776039Seric }
77810758Seric /*
77910758Seric **  MYHOSTNAME -- return the name of this host.
78010758Seric **
78110758Seric **	Parameters:
78210758Seric **		hostbuf -- a place to return the name of this host.
78312313Seric **		size -- the size of hostbuf.
78410758Seric **
78510758Seric **	Returns:
78610758Seric **		A list of aliases for this host.
78710758Seric **
78810758Seric **	Side Effects:
78964338Seric **		Adds numeric codes to $=w.
79010758Seric */
7916039Seric 
79210758Seric char **
79312313Seric myhostname(hostbuf, size)
79410758Seric 	char hostbuf[];
79512313Seric 	int size;
79610758Seric {
79758110Seric 	register struct hostent *hp;
79810758Seric 	extern struct hostent *gethostbyname();
79910758Seric 
80023120Seric 	if (gethostname(hostbuf, size) < 0)
80123120Seric 	{
80223120Seric 		(void) strcpy(hostbuf, "localhost");
80323120Seric 	}
80411147Seric 	hp = gethostbyname(hostbuf);
80511147Seric 	if (hp != NULL)
80616877Seric 	{
807*66777Seric #ifdef NAMED_BIND
808*66777Seric 		if (strchr(hp->h_name, '.') == NULL)
809*66777Seric 		{
810*66777Seric 			extern bool getcanonname();
81158110Seric 
812*66777Seric 			(void) getcanonname(hostbuf, size, TRUE);
813*66777Seric 		}
814*66777Seric 		else
815*66777Seric #else
816*66777Seric 		{
817*66777Seric 			(void) strncpy(hostbuf, hp->h_name, size - 1);
818*66777Seric 			hostbuf[size - 1] = '\0';
819*66777Seric 		}
820*66777Seric #endif
821*66777Seric 
82258110Seric 		if (hp->h_addrtype == AF_INET && hp->h_length == 4)
82358110Seric 		{
82458110Seric 			register int i;
82558110Seric 
82664338Seric 			for (i = 0; hp->h_addr_list[i] != NULL; i++)
82758110Seric 			{
82864338Seric 				char ipbuf[100];
82964338Seric 
83064338Seric 				sprintf(ipbuf, "[%s]",
83164338Seric 					inet_ntoa(*((struct in_addr *) hp->h_addr_list[i])));
83264338Seric 				setclass('w', ipbuf);
83358110Seric 			}
83458110Seric 		}
83558110Seric 
83611147Seric 		return (hp->h_aliases);
83716877Seric 	}
83810758Seric 	else
83910758Seric 		return (NULL);
84010758Seric }
84151315Seric /*
84258951Seric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
84358308Seric **
84458951Seric **	Uses RFC1413 protocol to try to get info from the other end.
84558951Seric **
84658308Seric **	Parameters:
84758308Seric **		fd -- the descriptor
84858308Seric **
84958308Seric **	Returns:
85058951Seric **		The user@host information associated with this descriptor.
85158308Seric */
85258308Seric 
85364927Seric #if IDENTPROTO
85458951Seric 
85558951Seric static jmp_buf	CtxAuthTimeout;
85658951Seric 
85758951Seric static
85858951Seric authtimeout()
85958951Seric {
86058951Seric 	longjmp(CtxAuthTimeout, 1);
86158951Seric }
86258951Seric 
86358951Seric #endif
86458951Seric 
86558308Seric char *
86658951Seric getauthinfo(fd)
86758308Seric 	int fd;
86858308Seric {
86958951Seric 	int falen;
87059104Seric 	register char *p;
87164927Seric #if IDENTPROTO
87258951Seric 	SOCKADDR la;
87358951Seric 	int lalen;
87458951Seric 	register struct servent *sp;
87558951Seric 	int s;
87658951Seric 	int i;
87758951Seric 	EVENT *ev;
87858951Seric #endif
87958951Seric 	static char hbuf[MAXNAME * 2 + 2];
88058951Seric 	extern char *hostnamebyanyaddr();
88158951Seric 	extern char RealUserName[];			/* main.c */
88258308Seric 
88366761Seric 	falen = sizeof RealHostAddr;
88466761Seric 	if (getpeername(fd, &RealHostAddr.sa, &falen) < 0 || falen <= 0 ||
88566761Seric 	    RealHostAddr.sa.sa_family == 0)
88658951Seric 	{
88758951Seric 		(void) sprintf(hbuf, "%s@localhost", RealUserName);
88858957Seric 		if (tTd(9, 1))
88958951Seric 			printf("getauthinfo: %s\n", hbuf);
89058951Seric 		return hbuf;
89158951Seric 	}
89258951Seric 
89366761Seric 	if (RealHostName == NULL)
89466761Seric 	{
89566761Seric 		/* translate that to a host name */
89666761Seric 		RealHostName = newstr(hostnamebyanyaddr(&RealHostAddr));
89766761Seric 	}
89866761Seric 
89964927Seric #if IDENTPROTO
90065831Seric 	if (TimeOuts.to_ident == 0)
90165831Seric 		goto noident;
90265831Seric 
90358951Seric 	lalen = sizeof la;
90466761Seric 	if (RealHostAddr.sa.sa_family != AF_INET ||
90558951Seric 	    getsockname(fd, &la.sa, &lalen) < 0 || lalen <= 0 ||
90658951Seric 	    la.sa.sa_family != AF_INET)
90758951Seric 	{
90858951Seric 		/* no ident info */
90958951Seric 		goto noident;
91058951Seric 	}
91158951Seric 
91258951Seric 	/* create ident query */
91360489Seric 	(void) sprintf(hbuf, "%d,%d\r\n",
91466761Seric 		ntohs(RealHostAddr.sin.sin_port), ntohs(la.sin.sin_port));
91558951Seric 
91658951Seric 	/* create local address */
91764747Seric 	la.sin.sin_port = 0;
91858951Seric 
91958951Seric 	/* create foreign address */
92058951Seric 	sp = getservbyname("auth", "tcp");
92158951Seric 	if (sp != NULL)
92266761Seric 		RealHostAddr.sin.sin_port = sp->s_port;
92358308Seric 	else
92466761Seric 		RealHostAddr.sin.sin_port = htons(113);
92558951Seric 
92658951Seric 	s = -1;
92758951Seric 	if (setjmp(CtxAuthTimeout) != 0)
92858951Seric 	{
92958951Seric 		if (s >= 0)
93058951Seric 			(void) close(s);
93158951Seric 		goto noident;
93258951Seric 	}
93358951Seric 
93458951Seric 	/* put a timeout around the whole thing */
93564255Seric 	ev = setevent(TimeOuts.to_ident, authtimeout, 0);
93658951Seric 
93764747Seric 	/* connect to foreign IDENT server using same address as SMTP socket */
93858951Seric 	s = socket(AF_INET, SOCK_STREAM, 0);
93958951Seric 	if (s < 0)
94058951Seric 	{
94158951Seric 		clrevent(ev);
94258951Seric 		goto noident;
94358951Seric 	}
94464747Seric 	if (bind(s, &la.sa, sizeof la.sin) < 0 ||
94566761Seric 	    connect(s, &RealHostAddr.sa, sizeof RealHostAddr.sin) < 0)
94658951Seric 	{
94766011Seric 		goto closeident;
94858951Seric 	}
94958951Seric 
95058957Seric 	if (tTd(9, 10))
95158951Seric 		printf("getauthinfo: sent %s", hbuf);
95258951Seric 
95358951Seric 	/* send query */
95458951Seric 	if (write(s, hbuf, strlen(hbuf)) < 0)
95558951Seric 		goto closeident;
95658951Seric 
95758951Seric 	/* get result */
95858951Seric 	i = read(s, hbuf, sizeof hbuf);
95958951Seric 	(void) close(s);
96058951Seric 	clrevent(ev);
96158951Seric 	if (i <= 0)
96258951Seric 		goto noident;
96358951Seric 	if (hbuf[--i] == '\n' && hbuf[--i] == '\r')
96458951Seric 		i--;
96558951Seric 	hbuf[++i] = '\0';
96658951Seric 
96758957Seric 	if (tTd(9, 3))
96858951Seric 		printf("getauthinfo:  got %s\n", hbuf);
96958951Seric 
97058951Seric 	/* parse result */
97158951Seric 	p = strchr(hbuf, ':');
97258951Seric 	if (p == NULL)
97358951Seric 	{
97458951Seric 		/* malformed response */
97558951Seric 		goto noident;
97658951Seric 	}
97758951Seric 	while (isascii(*++p) && isspace(*p))
97858951Seric 		continue;
97958951Seric 	if (strncasecmp(p, "userid", 6) != 0)
98058951Seric 	{
98158951Seric 		/* presumably an error string */
98258951Seric 		goto noident;
98358951Seric 	}
98458951Seric 	p += 6;
98558951Seric 	while (isascii(*p) && isspace(*p))
98658951Seric 		p++;
98758951Seric 	if (*p++ != ':')
98858951Seric 	{
98958951Seric 		/* either useridxx or malformed response */
99058951Seric 		goto noident;
99158951Seric 	}
99258951Seric 
99358951Seric 	/* p now points to the OSTYPE field */
99458951Seric 	p = strchr(p, ':');
99558951Seric 	if (p == NULL)
99658951Seric 	{
99758951Seric 		/* malformed response */
99858951Seric 		goto noident;
99958951Seric 	}
100058951Seric 
100158957Seric 	/* 1413 says don't do this -- but it's broken otherwise */
100258957Seric 	while (isascii(*++p) && isspace(*p))
100358957Seric 		continue;
100458957Seric 
100558951Seric 	/* p now points to the authenticated name */
100666003Seric 	(void) sprintf(hbuf, "%s@%s",
100766003Seric 		p, RealHostName == NULL ? "localhost" : RealHostName);
100858957Seric 	goto finish;
100958957Seric 
101066011Seric closeident:
101166011Seric 	(void) close(s);
101266011Seric 	clrevent(ev);
101366011Seric 
101458957Seric #endif /* IDENTPROTO */
101558957Seric 
101658957Seric noident:
101766003Seric 	if (RealHostName == NULL)
101866003Seric 	{
101966003Seric 		if (tTd(9, 1))
102066003Seric 			printf("getauthinfo: NULL\n");
102166003Seric 		return NULL;
102266003Seric 	}
102358957Seric 	(void) strcpy(hbuf, RealHostName);
102458957Seric 
102558957Seric finish:
102666003Seric 	if (RealHostName != NULL && RealHostName[0] != '[')
102758951Seric 	{
102858951Seric 		p = &hbuf[strlen(hbuf)];
102958951Seric 		(void) sprintf(p, " [%s]", anynet_ntoa(&RealHostAddr));
103058951Seric 	}
103158957Seric 	if (tTd(9, 1))
103258951Seric 		printf("getauthinfo: %s\n", hbuf);
103358308Seric 	return hbuf;
103458308Seric }
103558308Seric /*
103660089Seric **  HOST_MAP_LOOKUP -- turn a hostname into canonical form
103753751Seric **
103853751Seric **	Parameters:
103956823Seric **		map -- a pointer to this map (unused).
104060089Seric **		name -- the (presumably unqualified) hostname.
104160257Seric **		av -- unused -- for compatibility with other mapping
104255019Seric **			functions.
104359084Seric **		statp -- an exit status (out parameter) -- set to
104459084Seric **			EX_TEMPFAIL if the name server is unavailable.
104553751Seric **
104653751Seric **	Returns:
104753751Seric **		The mapping, if found.
104853751Seric **		NULL if no mapping found.
104953751Seric **
105053751Seric **	Side Effects:
105153751Seric **		Looks up the host specified in hbuf.  If it is not
105253751Seric **		the canonical name for that host, return the canonical
105353751Seric **		name.
105453751Seric */
105551315Seric 
105653751Seric char *
105760257Seric host_map_lookup(map, name, av, statp)
105856823Seric 	MAP *map;
105960089Seric 	char *name;
106060257Seric 	char **av;
106159084Seric 	int *statp;
106216911Seric {
106316911Seric 	register struct hostent *hp;
106433932Sbostic 	u_long in_addr;
106556823Seric 	char *cp;
106658110Seric 	int i;
106759671Seric 	register STAB *s;
106860257Seric 	char hbuf[MAXNAME];
106959671Seric 	extern struct hostent *gethostbyaddr();
107066334Seric #if NAMED_BIND
107159671Seric 	extern int h_errno;
107266029Seric #endif
107316911Seric 
107425574Smiriam 	/*
107559671Seric 	**  See if we have already looked up this name.  If so, just
107659671Seric 	**  return it.
107759671Seric 	*/
107853751Seric 
107960089Seric 	s = stab(name, ST_NAMECANON, ST_ENTER);
108059671Seric 	if (bitset(NCF_VALID, s->s_namecanon.nc_flags))
108159671Seric 	{
108259986Seric 		if (tTd(9, 1))
108360089Seric 			printf("host_map_lookup(%s) => CACHE %s\n",
108460089Seric 				name, s->s_namecanon.nc_cname);
108559671Seric 		errno = s->s_namecanon.nc_errno;
108666334Seric #if NAMED_BIND
108759671Seric 		h_errno = s->s_namecanon.nc_herrno;
108866029Seric #endif
108959671Seric 		*statp = s->s_namecanon.nc_stat;
109064797Seric 		if (CurEnv->e_message == NULL && *statp == EX_TEMPFAIL)
109165199Seric 		{
109265199Seric 			sprintf(hbuf, "%s: Name server timeout",
109365199Seric 				shortenstring(name, 33));
109465199Seric 			CurEnv->e_message = newstr(hbuf);
109565199Seric 		}
109659671Seric 		return s->s_namecanon.nc_cname;
109759671Seric 	}
109859671Seric 
109959671Seric 	/*
110059671Seric 	**  If first character is a bracket, then it is an address
110159671Seric 	**  lookup.  Address is copied into a temporary buffer to
110260089Seric 	**  strip the brackets and to preserve name if address is
110359671Seric 	**  unknown.
110459671Seric 	*/
110559671Seric 
110660089Seric 	if (*name != '[')
110753751Seric 	{
110855019Seric 		extern bool getcanonname();
110955019Seric 
111058798Seric 		if (tTd(9, 1))
111160089Seric 			printf("host_map_lookup(%s) => ", name);
111259671Seric 		s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
111360089Seric 		(void) strcpy(hbuf, name);
111463842Seric 		if (getcanonname(hbuf, sizeof hbuf - 1, TRUE))
111558796Seric 		{
111658796Seric 			if (tTd(9, 1))
111758796Seric 				printf("%s\n", hbuf);
111860257Seric 			cp = map_rewrite(map, hbuf, strlen(hbuf), av);
111960257Seric 			s->s_namecanon.nc_cname = newstr(cp);
112060257Seric 			return cp;
112158796Seric 		}
112253751Seric 		else
112358796Seric 		{
112459084Seric 			register struct hostent *hp;
112559084Seric 
112666029Seric 			s->s_namecanon.nc_errno = errno;
112766334Seric #if NAMED_BIND
112866029Seric 			s->s_namecanon.nc_herrno = h_errno;
112958796Seric 			if (tTd(9, 1))
113059084Seric 				printf("FAIL (%d)\n", h_errno);
113159084Seric 			switch (h_errno)
113259084Seric 			{
113359084Seric 			  case TRY_AGAIN:
113459596Seric 				if (UseNameServer)
113559734Seric 				{
113665202Seric 					sprintf(hbuf, "%s: Name server timeout",
113765199Seric 						shortenstring(name, 33));
113865202Seric 					message("%s", hbuf);
113959734Seric 					if (CurEnv->e_message == NULL)
114065202Seric 						CurEnv->e_message = newstr(hbuf);
114159734Seric 				}
114259084Seric 				*statp = EX_TEMPFAIL;
114359084Seric 				break;
114459084Seric 
114559084Seric 			  case HOST_NOT_FOUND:
114659084Seric 				*statp = EX_NOHOST;
114759084Seric 				break;
114859084Seric 
114959084Seric 			  case NO_RECOVERY:
115059084Seric 				*statp = EX_SOFTWARE;
115159084Seric 				break;
115259084Seric 
115359084Seric 			  default:
115459084Seric 				*statp = EX_UNAVAILABLE;
115559084Seric 				break;
115659084Seric 			}
115766029Seric #else
115866029Seric 			if (tTd(9, 1))
115966029Seric 				printf("FAIL\n");
116066029Seric 			*statp = EX_NOHOST;
116166029Seric #endif
116259671Seric 			s->s_namecanon.nc_stat = *statp;
116359084Seric 			if (*statp != EX_TEMPFAIL || UseNameServer)
116459084Seric 				return NULL;
116559084Seric 
116659084Seric 			/*
116759084Seric 			**  Try to look it up in /etc/hosts
116859084Seric 			*/
116959084Seric 
117060089Seric 			hp = gethostbyname(name);
117159084Seric 			if (hp == NULL)
117259084Seric 			{
117359084Seric 				/* no dice there either */
117459671Seric 				s->s_namecanon.nc_stat = *statp = EX_NOHOST;
117559084Seric 				return NULL;
117659084Seric 			}
117759084Seric 
117859671Seric 			s->s_namecanon.nc_stat = *statp = EX_OK;
117960257Seric 			cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
118060257Seric 			s->s_namecanon.nc_cname = newstr(cp);
118160257Seric 			return cp;
118258796Seric 		}
118353751Seric 	}
118460089Seric 	if ((cp = strchr(name, ']')) == NULL)
118553751Seric 		return (NULL);
118640994Sbostic 	*cp = '\0';
118760089Seric 	in_addr = inet_addr(&name[1]);
118858110Seric 
118958110Seric 	/* nope -- ask the name server */
119033932Sbostic 	hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET);
119159671Seric 	s->s_namecanon.nc_errno = errno;
119266334Seric #if NAMED_BIND
119359671Seric 	s->s_namecanon.nc_herrno = h_errno;
119466029Seric #endif
119559671Seric 	s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
119633932Sbostic 	if (hp == NULL)
119759671Seric 	{
119859671Seric 		s->s_namecanon.nc_stat = *statp = EX_NOHOST;
119953751Seric 		return (NULL);
120059671Seric 	}
120153751Seric 
120258110Seric 	/* found a match -- copy out */
120360257Seric 	cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
120459671Seric 	s->s_namecanon.nc_stat = *statp = EX_OK;
120560257Seric 	s->s_namecanon.nc_cname = newstr(cp);
120660257Seric 	return cp;
120733932Sbostic }
120858755Seric /*
120958755Seric **  ANYNET_NTOA -- convert a network address to printable form.
121058755Seric **
121158755Seric **	Parameters:
121258755Seric **		sap -- a pointer to a sockaddr structure.
121358755Seric **
121458755Seric **	Returns:
121558755Seric **		A printable version of that sockaddr.
121658755Seric */
121716911Seric 
121858755Seric char *
121958755Seric anynet_ntoa(sap)
122058755Seric 	register SOCKADDR *sap;
122158755Seric {
122258755Seric 	register char *bp;
122358755Seric 	register char *ap;
122458755Seric 	int l;
122564734Seric 	static char buf[100];
122658755Seric 
122758798Seric 	/* check for null/zero family */
122858798Seric 	if (sap == NULL)
122958798Seric 		return "NULLADDR";
123058798Seric 	if (sap->sa.sa_family == 0)
123158798Seric 		return "0";
123258798Seric 
123364734Seric 	switch (sap->sa.sa_family)
123464734Seric 	{
123564734Seric #ifdef MAYBENEXTRELEASE		/*** UNTESTED *** UNTESTED *** UNTESTED ***/
123664821Seric #ifdef NETUNIX
123764734Seric 	  case AF_UNIX:
123864758Seric 	  	if (sap->sunix.sun_path[0] != '\0')
123964758Seric 	  		sprintf(buf, "[UNIX: %.64s]", sap->sunix.sun_path);
124064734Seric 	  	else
124164734Seric 	  		sprintf(buf, "[UNIX: localhost]");
124264734Seric 		return buf;
124364734Seric #endif
124464821Seric #endif
124564734Seric 
124658778Seric #ifdef NETINET
124764734Seric 	  case AF_INET:
124858755Seric 		return inet_ntoa(((struct sockaddr_in *) sap)->sin_addr);
124958778Seric #endif
125058755Seric 
125164734Seric 	  default:
125264734Seric 	  	/* this case is only to ensure syntactic correctness */
125364734Seric 	  	break;
125464734Seric 	}
125564734Seric 
125658755Seric 	/* unknown family -- just dump bytes */
125758778Seric 	(void) sprintf(buf, "Family %d: ", sap->sa.sa_family);
125858755Seric 	bp = &buf[strlen(buf)];
125958778Seric 	ap = sap->sa.sa_data;
126058778Seric 	for (l = sizeof sap->sa.sa_data; --l >= 0; )
126158755Seric 	{
126258755Seric 		(void) sprintf(bp, "%02x:", *ap++ & 0377);
126358755Seric 		bp += 3;
126458755Seric 	}
126558755Seric 	*--bp = '\0';
126658755Seric 	return buf;
126758755Seric }
126858951Seric /*
126958951Seric **  HOSTNAMEBYANYADDR -- return name of host based on address
127058951Seric **
127158951Seric **	Parameters:
127258951Seric **		sap -- SOCKADDR pointer
127358951Seric **
127458951Seric **	Returns:
127558951Seric **		text representation of host name.
127658951Seric **
127758951Seric **	Side Effects:
127858951Seric **		none.
127958951Seric */
128058755Seric 
128158951Seric char *
128258951Seric hostnamebyanyaddr(sap)
128358951Seric 	register SOCKADDR *sap;
128458951Seric {
128558951Seric 	register struct hostent *hp;
128664734Seric 	int saveretry;
128758951Seric 
128866334Seric #if NAMED_BIND
128959042Seric 	/* shorten name server timeout to avoid higher level timeouts */
129059042Seric 	saveretry = _res.retry;
129159042Seric 	_res.retry = 3;
129259042Seric #endif /* NAMED_BIND */
129359042Seric 
129458951Seric 	switch (sap->sa.sa_family)
129558951Seric 	{
129658951Seric #ifdef NETINET
129758951Seric 	  case AF_INET:
129858951Seric 		hp = gethostbyaddr((char *) &sap->sin.sin_addr,
129958951Seric 			sizeof sap->sin.sin_addr,
130058951Seric 			AF_INET);
130158951Seric 		break;
130258951Seric #endif
130358951Seric 
130458951Seric #ifdef NETISO
130558951Seric 	  case AF_ISO:
130658951Seric 		hp = gethostbyaddr((char *) &sap->siso.siso_addr,
130758951Seric 			sizeof sap->siso.siso_addr,
130858951Seric 			AF_ISO);
130958951Seric 		break;
131058951Seric #endif
131158951Seric 
131264734Seric #ifdef MAYBENEXTRELEASE		/*** UNTESTED *** UNTESTED *** UNTESTED ***/
131364734Seric 	  case AF_UNIX:
131464734Seric 		hp = NULL;
131564734Seric 		break;
131664734Seric #endif
131764734Seric 
131858951Seric 	  default:
131958951Seric 		hp = gethostbyaddr(sap->sa.sa_data,
132058951Seric 			   sizeof sap->sa.sa_data,
132158951Seric 			   sap->sa.sa_family);
132258951Seric 		break;
132358951Seric 	}
132458951Seric 
132566334Seric #if NAMED_BIND
132659042Seric 	_res.retry = saveretry;
132759042Seric #endif /* NAMED_BIND */
132859042Seric 
132958951Seric 	if (hp != NULL)
133058951Seric 		return hp->h_name;
133158951Seric 	else
133258951Seric 	{
133358951Seric 		/* produce a dotted quad */
133458951Seric 		static char buf[512];
133558951Seric 
133658951Seric 		(void) sprintf(buf, "[%s]", anynet_ntoa(sap));
133758951Seric 		return buf;
133858951Seric 	}
133958951Seric }
134058951Seric 
134156795Seric # else /* DAEMON */
134216911Seric /* code for systems without sophisticated networking */
134310758Seric 
134410758Seric /*
134510758Seric **  MYHOSTNAME -- stub version for case of no daemon code.
134611297Seric **
134711297Seric **	Can't convert to upper case here because might be a UUCP name.
134812313Seric **
134912313Seric **	Mark, you can change this to be anything you want......
135010758Seric */
135110758Seric 
135210758Seric char **
135312313Seric myhostname(hostbuf, size)
135410758Seric 	char hostbuf[];
135512313Seric 	int size;
135610758Seric {
135710758Seric 	register FILE *f;
135810758Seric 
135910758Seric 	hostbuf[0] = '\0';
136010758Seric 	f = fopen("/usr/include/whoami", "r");
136110758Seric 	if (f != NULL)
136210758Seric 	{
136312313Seric 		(void) fgets(hostbuf, size, f);
136410758Seric 		fixcrlf(hostbuf, TRUE);
136510758Seric 		(void) fclose(f);
136610758Seric 	}
136710758Seric 	return (NULL);
136810758Seric }
136916911Seric /*
137058951Seric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
137158308Seric **
137258308Seric **	Parameters:
137358308Seric **		fd -- the descriptor
137458308Seric **
137558308Seric **	Returns:
137658308Seric **		The host name associated with this descriptor, if it can
137758308Seric **			be determined.
137858308Seric **		NULL otherwise.
137958308Seric **
138058308Seric **	Side Effects:
138158308Seric **		none
138258308Seric */
138358308Seric 
138458308Seric char *
138558951Seric getauthinfo(fd)
138658308Seric 	int fd;
138758308Seric {
138858308Seric 	return NULL;
138958308Seric }
139058308Seric /*
139116911Seric **  MAPHOSTNAME -- turn a hostname into canonical form
139216911Seric **
139316911Seric **	Parameters:
139456823Seric **		map -- a pointer to the database map.
139560089Seric **		name -- a buffer containing a hostname.
139653751Seric **		avp -- a pointer to a (cf file defined) argument vector.
139759084Seric **		statp -- an exit status (out parameter).
139816911Seric **
139916911Seric **	Returns:
140053751Seric **		mapped host name
140151315Seric **		FALSE otherwise.
140216911Seric **
140316911Seric **	Side Effects:
140460089Seric **		Looks up the host specified in name.  If it is not
140516911Seric **		the canonical name for that host, replace it with
140616911Seric **		the canonical name.  If the name is unknown, or it
140716911Seric **		is already the canonical name, leave it unchanged.
140816911Seric */
140910758Seric 
141016911Seric /*ARGSUSED*/
141153751Seric char *
141260089Seric host_map_lookup(map, name, avp, statp)
141356823Seric 	MAP *map;
141460089Seric 	char *name;
141553751Seric 	char **avp;
141659084Seric 	char *statp;
141716911Seric {
141859084Seric 	register struct hostent *hp;
141959084Seric 
142060089Seric 	hp = gethostbyname(name);
142159084Seric 	if (hp != NULL)
142259084Seric 		return hp->h_name;
142359084Seric 	*statp = EX_NOHOST;
142453751Seric 	return NULL;
142516911Seric }
142616911Seric 
142756795Seric #endif /* DAEMON */
1428