xref: /csrg-svn/usr.sbin/sendmail/src/daemon.c (revision 65831)
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*65831Seric static char sccsid[] = "@(#)daemon.c	8.32 (Berkeley) 01/22/94 (with daemon mode)";
1533780Sbostic #else
16*65831Seric static char sccsid[] = "@(#)daemon.c	8.32 (Berkeley) 01/22/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 
2559042Seric #ifdef 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);
27824950Seric 
27911147Seric 			/* determine host name */
28064086Seric 			p = hostnamebyanyaddr(&RealHostAddr);
28164086Seric 			RealHostName = newstr(p);
28258778Seric 
28355173Seric #ifdef LOG
28463842Seric 			if (LogLevel > 11)
28555173Seric 			{
28655173Seric 				/* log connection information */
28755173Seric 				syslog(LOG_INFO, "connect from %s (%s)",
28858951Seric 					RealHostName, anynet_ntoa(&RealHostAddr));
28955173Seric 			}
29055173Seric #endif
29155173Seric 
29259254Seric 			(void) close(DaemonSocket);
29364724Seric 			if ((InChannel = fdopen(t, "r")) == NULL ||
29464724Seric 			    (t = dup(t)) < 0 ||
29564724Seric 			    (OutChannel = fdopen(t, "w")) == NULL)
29664724Seric 			{
29764724Seric 				syserr("cannot open SMTP server channel, fd=%d", t);
29864724Seric 				exit(0);
29964724Seric 			}
30059254Seric 
30116884Seric 			/* should we check for illegal connection here? XXX */
30259156Seric #ifdef XLA
30359156Seric 			if (!xla_host_ok(RealHostName))
30459156Seric 			{
30559254Seric 				message("421 Too many SMTP sessions for this host");
30659156Seric 				exit(0);
30759156Seric 			}
30859156Seric #endif
30916884Seric 
3107677Seric 			if (tTd(15, 2))
3115978Seric 				printf("getreq: returning\n");
3124636Seric 			return;
3134631Seric 		}
3144631Seric 
3157117Seric 		/* close the port so that others will hang (for a while) */
3169610Seric 		(void) close(t);
3174631Seric 	}
3189886Seric 	/*NOTREACHED*/
3194631Seric }
3205978Seric /*
32110206Seric **  CLRDAEMON -- reset the daemon connection
32210206Seric **
32310206Seric **	Parameters:
32410206Seric **		none.
32510206Seric **
32610206Seric **	Returns:
32710206Seric **		none.
32810206Seric **
32910206Seric **	Side Effects:
33010206Seric **		releases any resources used by the passive daemon.
33110206Seric */
33210206Seric 
33310206Seric clrdaemon()
33410206Seric {
33510206Seric 	if (DaemonSocket >= 0)
33610206Seric 		(void) close(DaemonSocket);
33710206Seric 	DaemonSocket = -1;
33810206Seric }
33910206Seric /*
34058849Seric **  SETDAEMONOPTIONS -- set options for running the daemon
34158849Seric **
34258849Seric **	Parameters:
34358849Seric **		p -- the options line.
34458849Seric **
34558849Seric **	Returns:
34658849Seric **		none.
34758849Seric */
34858849Seric 
34958849Seric setdaemonoptions(p)
35058849Seric 	register char *p;
35158849Seric {
35258873Seric 	if (DaemonAddr.sa.sa_family == AF_UNSPEC)
35358873Seric 		DaemonAddr.sa.sa_family = AF_INET;
35458873Seric 
35558849Seric 	while (p != NULL)
35658849Seric 	{
35758849Seric 		register char *f;
35858849Seric 		register char *v;
35958849Seric 
36058849Seric 		while (isascii(*p) && isspace(*p))
36158849Seric 			p++;
36258849Seric 		if (*p == '\0')
36358849Seric 			break;
36458849Seric 		f = p;
36558849Seric 		p = strchr(p, ',');
36658849Seric 		if (p != NULL)
36758849Seric 			*p++ = '\0';
36858849Seric 		v = strchr(f, '=');
36958849Seric 		if (v == NULL)
37058849Seric 			continue;
37158849Seric 		while (isascii(*++v) && isspace(*v))
37258849Seric 			continue;
37358849Seric 
37458849Seric 		switch (*f)
37558849Seric 		{
37658873Seric 		  case 'F':		/* address family */
37758849Seric 			if (isascii(*v) && isdigit(*v))
37858873Seric 				DaemonAddr.sa.sa_family = atoi(v);
37958873Seric #ifdef NETINET
38058873Seric 			else if (strcasecmp(v, "inet") == 0)
38158873Seric 				DaemonAddr.sa.sa_family = AF_INET;
38258873Seric #endif
38358873Seric #ifdef NETISO
38458873Seric 			else if (strcasecmp(v, "iso") == 0)
38558873Seric 				DaemonAddr.sa.sa_family = AF_ISO;
38658873Seric #endif
38758873Seric #ifdef NETNS
38858873Seric 			else if (strcasecmp(v, "ns") == 0)
38958873Seric 				DaemonAddr.sa.sa_family = AF_NS;
39058873Seric #endif
39158873Seric #ifdef NETX25
39258873Seric 			else if (strcasecmp(v, "x.25") == 0)
39358873Seric 				DaemonAddr.sa.sa_family = AF_CCITT;
39458873Seric #endif
39558849Seric 			else
39658873Seric 				syserr("554 Unknown address family %s in Family=option", v);
39758873Seric 			break;
39858873Seric 
39958873Seric 		  case 'A':		/* address */
40058873Seric 			switch (DaemonAddr.sa.sa_family)
40158849Seric 			{
40258873Seric #ifdef NETINET
40358873Seric 			  case AF_INET:
40458873Seric 				if (isascii(*v) && isdigit(*v))
40558873Seric 					DaemonAddr.sin.sin_addr.s_addr = inet_network(v);
40658873Seric 				else
40758873Seric 				{
40858873Seric 					register struct netent *np;
40958849Seric 
41058873Seric 					np = getnetbyname(v);
41158873Seric 					if (np == NULL)
41258873Seric 						syserr("554 network \"%s\" unknown", v);
41358873Seric 					else
41458873Seric 						DaemonAddr.sin.sin_addr.s_addr = np->n_net;
41558873Seric 				}
41658873Seric 				break;
41758873Seric #endif
41858873Seric 
41958873Seric 			  default:
42058873Seric 				syserr("554 Address= option unsupported for family %d",
42158873Seric 					DaemonAddr.sa.sa_family);
42258873Seric 				break;
42358849Seric 			}
42458849Seric 			break;
42558849Seric 
42658873Seric 		  case 'P':		/* port */
42758873Seric 			switch (DaemonAddr.sa.sa_family)
42858849Seric 			{
42958873Seric 				short port;
43058849Seric 
43158873Seric #ifdef NETINET
43258873Seric 			  case AF_INET:
43358873Seric 				if (isascii(*v) && isdigit(*v))
43464366Seric 					DaemonAddr.sin.sin_port = htons(atoi(v));
43558849Seric 				else
43658873Seric 				{
43758873Seric 					register struct servent *sp;
43858873Seric 
43958873Seric 					sp = getservbyname(v, "tcp");
44058873Seric 					if (sp == NULL)
44158909Seric 						syserr("554 service \"%s\" unknown", v);
44258873Seric 					else
44358873Seric 						DaemonAddr.sin.sin_port = sp->s_port;
44458873Seric 				}
44558873Seric 				break;
44658873Seric #endif
44758873Seric 
44858873Seric #ifdef NETISO
44958873Seric 			  case AF_ISO:
45058873Seric 				/* assume two byte transport selector */
45158873Seric 				if (isascii(*v) && isdigit(*v))
45264366Seric 					port = htons(atoi(v));
45358873Seric 				else
45458873Seric 				{
45558873Seric 					register struct servent *sp;
45658873Seric 
45758873Seric 					sp = getservbyname(v, "tcp");
45858873Seric 					if (sp == NULL)
45958909Seric 						syserr("554 service \"%s\" unknown", v);
46058873Seric 					else
46158873Seric 						port = sp->s_port;
46258873Seric 				}
46358873Seric 				bcopy((char *) &port, TSEL(&DaemonAddr.siso), 2);
46458873Seric 				break;
46558873Seric #endif
46658873Seric 
46758873Seric 			  default:
46858873Seric 				syserr("554 Port= option unsupported for family %d",
46958873Seric 					DaemonAddr.sa.sa_family);
47058873Seric 				break;
47158849Seric 			}
47258849Seric 			break;
47359783Seric 
47459783Seric 		  case 'L':		/* listen queue size */
47559783Seric 			ListenQueueSize = atoi(v);
47659783Seric 			break;
47764381Seric 
47864381Seric 		  case 'S':		/* send buffer size */
47964381Seric 			TcpSndBufferSize = atoi(v);
48064381Seric 			break;
48164381Seric 
48264381Seric 		  case 'R':		/* receive buffer size */
48364381Seric 			TcpRcvBufferSize = atoi(v);
48464381Seric 			break;
48558849Seric 		}
48658849Seric 	}
48758849Seric }
48858849Seric /*
4896039Seric **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
4906039Seric **
4916039Seric **	Parameters:
4926039Seric **		host -- the name of the host.
4936633Seric **		port -- the port number to connect to.
49453739Seric **		mci -- a pointer to the mail connection information
49553739Seric **			structure to be filled in.
49652106Seric **		usesecureport -- if set, use a low numbered (reserved)
49752106Seric **			port to provide some rudimentary authentication.
4986039Seric **
4996039Seric **	Returns:
5006039Seric **		An exit code telling whether the connection could be
5016039Seric **			made and if not why not.
5026039Seric **
5036039Seric **	Side Effects:
5046039Seric **		none.
5056039Seric */
5065978Seric 
50758755Seric SOCKADDR	CurHostAddr;		/* address of current host */
50858305Seric 
50954967Seric int
51053739Seric makeconnection(host, port, mci, usesecureport)
5116039Seric 	char *host;
5127286Seric 	u_short port;
51354967Seric 	register MCI *mci;
51452106Seric 	bool usesecureport;
5156039Seric {
51629430Sbloom 	register int i, s;
51729430Sbloom 	register struct hostent *hp = (struct hostent *)NULL;
51858755Seric 	SOCKADDR addr;
51952106Seric 	int sav_errno;
52058755Seric 	int addrlen;
52135651Seric #ifdef NAMED_BIND
52235651Seric 	extern int h_errno;
52335651Seric #endif
5246039Seric 
5256039Seric 	/*
5266039Seric 	**  Set up the address for the mailer.
5279308Seric 	**	Accept "[a.b.c.d]" syntax for host name.
5286039Seric 	*/
5296039Seric 
53035651Seric #ifdef NAMED_BIND
53125475Smiriam 	h_errno = 0;
53235651Seric #endif
53325475Smiriam 	errno = 0;
53458864Seric 	bzero(&CurHostAddr, sizeof CurHostAddr);
53564334Seric 	SmtpPhase = mci->mci_phase = "initial connection";
53658906Seric 	CurHostName = host;
53725475Smiriam 
5389308Seric 	if (host[0] == '[')
5399308Seric 	{
54011147Seric 		long hid;
54156795Seric 		register char *p = strchr(host, ']');
5429308Seric 
54311147Seric 		if (p != NULL)
5449308Seric 		{
54511147Seric 			*p = '\0';
54659884Seric #ifdef NETINET
54711147Seric 			hid = inet_addr(&host[1]);
54858360Seric 			if (hid == -1)
54959884Seric #endif
55058360Seric 			{
55158360Seric 				/* try it as a host name (avoid MX lookup) */
55258360Seric 				hp = gethostbyname(&host[1]);
55358360Seric 				*p = ']';
55458360Seric 				goto gothostent;
55558360Seric 			}
55611147Seric 			*p = ']';
5579308Seric 		}
55858360Seric 		if (p == NULL)
5599308Seric 		{
56058151Seric 			usrerr("553 Invalid numeric domain spec \"%s\"", host);
5619308Seric 			return (EX_NOHOST);
5629308Seric 		}
56359884Seric #ifdef NETINET
56459884Seric 		addr.sin.sin_family = AF_INET;		/*XXX*/
56558778Seric 		addr.sin.sin_addr.s_addr = hid;
56659884Seric #endif
5679308Seric 	}
5689610Seric 	else
5699610Seric 	{
57029430Sbloom 		hp = gethostbyname(host);
57158360Seric gothostent:
57225475Smiriam 		if (hp == NULL)
57324945Seric 		{
57435651Seric #ifdef NAMED_BIND
57525475Smiriam 			if (errno == ETIMEDOUT || h_errno == TRY_AGAIN)
57625475Smiriam 				return (EX_TEMPFAIL);
57725657Seric 
57835651Seric 			/* if name server is specified, assume temp fail */
57935651Seric 			if (errno == ECONNREFUSED && UseNameServer)
58035651Seric 				return (EX_TEMPFAIL);
58135651Seric #endif
58225475Smiriam 			return (EX_NOHOST);
58324945Seric 		}
58458778Seric 		addr.sa.sa_family = hp->h_addrtype;
58558778Seric 		switch (hp->h_addrtype)
58658778Seric 		{
58758778Seric #ifdef NETINET
58858778Seric 		  case AF_INET:
58958755Seric 			bcopy(hp->h_addr,
59058778Seric 				&addr.sin.sin_addr,
59164943Seric 				sizeof addr.sin.sin_addr);
59258778Seric 			break;
59358778Seric #endif
59458778Seric 
59558778Seric 		  default:
59658755Seric 			bcopy(hp->h_addr,
59758778Seric 				addr.sa.sa_data,
59858755Seric 				hp->h_length);
59958778Seric 			break;
60058778Seric 		}
60129430Sbloom 		i = 1;
6029610Seric 	}
6039610Seric 
6049610Seric 	/*
6059610Seric 	**  Determine the port number.
6069610Seric 	*/
6079610Seric 
60810011Seric 	if (port != 0)
60958755Seric 		port = htons(port);
61010011Seric 	else
6119610Seric 	{
6129610Seric 		register struct servent *sp = getservbyname("smtp", "tcp");
6139610Seric 
6149610Seric 		if (sp == NULL)
6159610Seric 		{
61658909Seric 			syserr("554 makeconnection: service \"smtp\" unknown");
61765169Seric 			port = htons(25);
6189610Seric 		}
61965169Seric 		else
62065169Seric 			port = sp->s_port;
6219610Seric 	}
6226039Seric 
62358778Seric 	switch (addr.sa.sa_family)
62458755Seric 	{
62559884Seric #ifdef NETINET
62658755Seric 	  case AF_INET:
62758778Seric 		addr.sin.sin_port = port;
62858755Seric 		addrlen = sizeof (struct sockaddr_in);
62958755Seric 		break;
63059884Seric #endif
63158755Seric 
63258755Seric #ifdef NETISO
63358755Seric 	  case AF_ISO:
63458755Seric 		/* assume two byte transport selector */
63558755Seric 		bcopy((char *) &port, TSEL((struct sockaddr_iso *) &addr), 2);
63658755Seric 		addrlen = sizeof (struct sockaddr_iso);
63758755Seric 		break;
63858755Seric #endif
63958755Seric 
64058755Seric 	  default:
64158778Seric 		syserr("Can't connect to address family %d", addr.sa.sa_family);
64258755Seric 		return (EX_NOHOST);
64358755Seric 	}
64458755Seric 
6456039Seric 	/*
6466039Seric 	**  Try to actually open the connection.
6476039Seric 	*/
6486039Seric 
64959156Seric #ifdef XLA
65059156Seric 	/* if too many connections, don't bother trying */
65159156Seric 	if (!xla_noqueue_ok(host))
65259156Seric 		return EX_TEMPFAIL;
65359156Seric #endif
65459156Seric 
65557736Seric 	for (;;)
65652106Seric 	{
65757736Seric 		if (tTd(16, 1))
65858755Seric 			printf("makeconnection (%s [%s])\n",
65958755Seric 				host, anynet_ntoa(&addr));
66052106Seric 
66158588Seric 		/* save for logging */
66258588Seric 		CurHostAddr = addr;
66358588Seric 
66457736Seric 		if (usesecureport)
66557736Seric 		{
66657736Seric 			int rport = IPPORT_RESERVED - 1;
6676039Seric 
66857736Seric 			s = rresvport(&rport);
66957736Seric 		}
67057736Seric 		else
67157736Seric 		{
67257736Seric 			s = socket(AF_INET, SOCK_STREAM, 0);
67357736Seric 		}
67457736Seric 		if (s < 0)
67557736Seric 		{
67657736Seric 			sav_errno = errno;
67757736Seric 			syserr("makeconnection: no socket");
67857736Seric 			goto failure;
67957736Seric 		}
68010347Seric 
68164381Seric #ifdef SO_SNDBUF
68264381Seric 		if (TcpSndBufferSize > 0)
68364381Seric 		{
68464381Seric 			if (setsockopt(s, SOL_SOCKET, SO_SNDBUF,
68564561Seric 				       (char *) &TcpSndBufferSize,
68664381Seric 				       sizeof(TcpSndBufferSize)) < 0)
68764381Seric 				syserr("makeconnection: setsockopt(SO_SNDBUF)");
68864381Seric 		}
68964381Seric #endif
69064381Seric 
69157736Seric 		if (tTd(16, 1))
69257736Seric 			printf("makeconnection: fd=%d\n", s);
69357736Seric 
69457736Seric 		/* turn on network debugging? */
69557736Seric 		if (tTd(16, 101))
69657736Seric 		{
69757736Seric 			int on = 1;
69857736Seric 			(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG,
69957736Seric 					  (char *)&on, sizeof on);
70057736Seric 		}
70157736Seric 		if (CurEnv->e_xfp != NULL)
70257736Seric 			(void) fflush(CurEnv->e_xfp);		/* for debugging */
70357736Seric 		errno = 0;					/* for debugging */
70458755Seric 		if (connect(s, (struct sockaddr *) &addr, addrlen) >= 0)
70557736Seric 			break;
70657736Seric 
70757736Seric 		/* couldn't connect.... figure out why */
70827744Sbloom 		sav_errno = errno;
70927744Sbloom 		(void) close(s);
71029430Sbloom 		if (hp && hp->h_addr_list[i])
71129430Sbloom 		{
71257736Seric 			if (tTd(16, 1))
71358755Seric 				printf("Connect failed (%s); trying new address....\n",
71458755Seric 					errstring(sav_errno));
71558778Seric 			switch (addr.sa.sa_family)
71658778Seric 			{
71758778Seric #ifdef NETINET
71858778Seric 			  case AF_INET:
71958755Seric 				bcopy(hp->h_addr_list[i++],
72058778Seric 				      &addr.sin.sin_addr,
72164943Seric 				      sizeof addr.sin.sin_addr);
72258778Seric 				break;
72358778Seric #endif
72458778Seric 
72558778Seric 			  default:
72658755Seric 				bcopy(hp->h_addr_list[i++],
72758778Seric 					addr.sa.sa_data,
72852106Seric 					hp->h_length);
72958778Seric 				break;
73058778Seric 			}
73157736Seric 			continue;
73229430Sbloom 		}
73329430Sbloom 
7346039Seric 		/* failure, decide if temporary or not */
7356039Seric 	failure:
73659254Seric #ifdef XLA
73759254Seric 		xla_host_end(host);
73859254Seric #endif
73958542Seric 		if (transienterror(sav_errno))
74058542Seric 			return EX_TEMPFAIL;
74158542Seric 		else
74258542Seric 		{
74358542Seric 			message("%s", errstring(sav_errno));
74458542Seric 			return (EX_UNAVAILABLE);
7456039Seric 		}
7466039Seric 	}
7476039Seric 
7486039Seric 	/* connection ok, put it into canonical form */
74964724Seric 	if ((mci->mci_out = fdopen(s, "w")) == NULL ||
75064724Seric 	    (s = dup(s)) < 0 ||
75164725Seric 	    (mci->mci_in = fdopen(s, "r")) == NULL)
75264724Seric 	{
75364724Seric 		syserr("cannot open SMTP client channel, fd=%d", s);
75464724Seric 		return EX_TEMPFAIL;
75564724Seric 	}
7566039Seric 
75710098Seric 	return (EX_OK);
7586039Seric }
75910758Seric /*
76010758Seric **  MYHOSTNAME -- return the name of this host.
76110758Seric **
76210758Seric **	Parameters:
76310758Seric **		hostbuf -- a place to return the name of this host.
76412313Seric **		size -- the size of hostbuf.
76510758Seric **
76610758Seric **	Returns:
76710758Seric **		A list of aliases for this host.
76810758Seric **
76910758Seric **	Side Effects:
77064338Seric **		Adds numeric codes to $=w.
77110758Seric */
7726039Seric 
77310758Seric char **
77412313Seric myhostname(hostbuf, size)
77510758Seric 	char hostbuf[];
77612313Seric 	int size;
77710758Seric {
77858110Seric 	register struct hostent *hp;
77910758Seric 	extern struct hostent *gethostbyname();
78010758Seric 
78123120Seric 	if (gethostname(hostbuf, size) < 0)
78223120Seric 	{
78323120Seric 		(void) strcpy(hostbuf, "localhost");
78423120Seric 	}
78511147Seric 	hp = gethostbyname(hostbuf);
78611147Seric 	if (hp != NULL)
78716877Seric 	{
78858110Seric 		(void) strncpy(hostbuf, hp->h_name, size - 1);
78958110Seric 		hostbuf[size - 1] = '\0';
79058110Seric 
79158110Seric 		if (hp->h_addrtype == AF_INET && hp->h_length == 4)
79258110Seric 		{
79358110Seric 			register int i;
79458110Seric 
79564338Seric 			for (i = 0; hp->h_addr_list[i] != NULL; i++)
79658110Seric 			{
79764338Seric 				char ipbuf[100];
79864338Seric 
79964338Seric 				sprintf(ipbuf, "[%s]",
80064338Seric 					inet_ntoa(*((struct in_addr *) hp->h_addr_list[i])));
80164338Seric 				setclass('w', ipbuf);
80258110Seric 			}
80358110Seric 		}
80458110Seric 
80511147Seric 		return (hp->h_aliases);
80616877Seric 	}
80710758Seric 	else
80810758Seric 		return (NULL);
80910758Seric }
81051315Seric /*
81158951Seric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
81258308Seric **
81358951Seric **	Uses RFC1413 protocol to try to get info from the other end.
81458951Seric **
81558308Seric **	Parameters:
81658308Seric **		fd -- the descriptor
81758308Seric **
81858308Seric **	Returns:
81958951Seric **		The user@host information associated with this descriptor.
82058308Seric **
82158308Seric **	Side Effects:
82258951Seric **		Sets RealHostName to the name of the host at the other end.
82358308Seric */
82458308Seric 
82564927Seric #if IDENTPROTO
82658951Seric 
82758951Seric static jmp_buf	CtxAuthTimeout;
82858951Seric 
82958951Seric static
83058951Seric authtimeout()
83158951Seric {
83258951Seric 	longjmp(CtxAuthTimeout, 1);
83358951Seric }
83458951Seric 
83558951Seric #endif
83658951Seric 
83758308Seric char *
83858951Seric getauthinfo(fd)
83958308Seric 	int fd;
84058308Seric {
84158951Seric 	SOCKADDR fa;
84258951Seric 	int falen;
84359104Seric 	register char *p;
84464927Seric #if IDENTPROTO
84558951Seric 	SOCKADDR la;
84658951Seric 	int lalen;
84758951Seric 	register struct servent *sp;
84858951Seric 	int s;
84958951Seric 	int i;
85058951Seric 	EVENT *ev;
85158951Seric #endif
85258951Seric 	static char hbuf[MAXNAME * 2 + 2];
85358951Seric 	extern char *hostnamebyanyaddr();
85458951Seric 	extern char RealUserName[];			/* main.c */
85558308Seric 
85658951Seric 	falen = sizeof fa;
85764845Seric 	if (getpeername(fd, &fa.sa, &falen) < 0 || falen <= 0 ||
85864845Seric 	    fa.sa.sa_family == 0)
85958951Seric 	{
86058951Seric 		RealHostName = "localhost";
86158951Seric 		(void) sprintf(hbuf, "%s@localhost", RealUserName);
86258957Seric 		if (tTd(9, 1))
86358951Seric 			printf("getauthinfo: %s\n", hbuf);
86458951Seric 		return hbuf;
86558951Seric 	}
86658951Seric 
86764086Seric 	p = hostnamebyanyaddr(&fa);
86864086Seric 	RealHostName = newstr(p);
86958951Seric 	RealHostAddr = fa;
87058951Seric 
87164927Seric #if IDENTPROTO
872*65831Seric 	if (TimeOuts.to_ident == 0)
873*65831Seric 		goto noident;
874*65831Seric 
87558951Seric 	lalen = sizeof la;
87658951Seric 	if (fa.sa.sa_family != AF_INET ||
87758951Seric 	    getsockname(fd, &la.sa, &lalen) < 0 || lalen <= 0 ||
87858951Seric 	    la.sa.sa_family != AF_INET)
87958951Seric 	{
88058951Seric 		/* no ident info */
88158951Seric 		goto noident;
88258951Seric 	}
88358951Seric 
88458951Seric 	/* create ident query */
88560489Seric 	(void) sprintf(hbuf, "%d,%d\r\n",
88660489Seric 		ntohs(fa.sin.sin_port), ntohs(la.sin.sin_port));
88758951Seric 
88858951Seric 	/* create local address */
88964747Seric 	la.sin.sin_port = 0;
89058951Seric 
89158951Seric 	/* create foreign address */
89258951Seric 	sp = getservbyname("auth", "tcp");
89358951Seric 	if (sp != NULL)
89458951Seric 		fa.sin.sin_port = sp->s_port;
89558308Seric 	else
89659097Seric 		fa.sin.sin_port = htons(113);
89758951Seric 
89858951Seric 	s = -1;
89958951Seric 	if (setjmp(CtxAuthTimeout) != 0)
90058951Seric 	{
90158951Seric 		if (s >= 0)
90258951Seric 			(void) close(s);
90358951Seric 		goto noident;
90458951Seric 	}
90558951Seric 
90658951Seric 	/* put a timeout around the whole thing */
90764255Seric 	ev = setevent(TimeOuts.to_ident, authtimeout, 0);
90858951Seric 
90964747Seric 	/* connect to foreign IDENT server using same address as SMTP socket */
91058951Seric 	s = socket(AF_INET, SOCK_STREAM, 0);
91158951Seric 	if (s < 0)
91258951Seric 	{
91358951Seric 		clrevent(ev);
91458951Seric 		goto noident;
91558951Seric 	}
91664747Seric 	if (bind(s, &la.sa, sizeof la.sin) < 0 ||
91764747Seric 	    connect(s, &fa.sa, sizeof fa.sin) < 0)
91858951Seric 	{
91958951Seric closeident:
92058951Seric 		(void) close(s);
92158951Seric 		clrevent(ev);
92258951Seric 		goto noident;
92358951Seric 	}
92458951Seric 
92558957Seric 	if (tTd(9, 10))
92658951Seric 		printf("getauthinfo: sent %s", hbuf);
92758951Seric 
92858951Seric 	/* send query */
92958951Seric 	if (write(s, hbuf, strlen(hbuf)) < 0)
93058951Seric 		goto closeident;
93158951Seric 
93258951Seric 	/* get result */
93358951Seric 	i = read(s, hbuf, sizeof hbuf);
93458951Seric 	(void) close(s);
93558951Seric 	clrevent(ev);
93658951Seric 	if (i <= 0)
93758951Seric 		goto noident;
93858951Seric 	if (hbuf[--i] == '\n' && hbuf[--i] == '\r')
93958951Seric 		i--;
94058951Seric 	hbuf[++i] = '\0';
94158951Seric 
94258957Seric 	if (tTd(9, 3))
94358951Seric 		printf("getauthinfo:  got %s\n", hbuf);
94458951Seric 
94558951Seric 	/* parse result */
94658951Seric 	p = strchr(hbuf, ':');
94758951Seric 	if (p == NULL)
94858951Seric 	{
94958951Seric 		/* malformed response */
95058951Seric 		goto noident;
95158951Seric 	}
95258951Seric 	while (isascii(*++p) && isspace(*p))
95358951Seric 		continue;
95458951Seric 	if (strncasecmp(p, "userid", 6) != 0)
95558951Seric 	{
95658951Seric 		/* presumably an error string */
95758951Seric 		goto noident;
95858951Seric 	}
95958951Seric 	p += 6;
96058951Seric 	while (isascii(*p) && isspace(*p))
96158951Seric 		p++;
96258951Seric 	if (*p++ != ':')
96358951Seric 	{
96458951Seric 		/* either useridxx or malformed response */
96558951Seric 		goto noident;
96658951Seric 	}
96758951Seric 
96858951Seric 	/* p now points to the OSTYPE field */
96958951Seric 	p = strchr(p, ':');
97058951Seric 	if (p == NULL)
97158951Seric 	{
97258951Seric 		/* malformed response */
97358951Seric 		goto noident;
97458951Seric 	}
97558951Seric 
97658957Seric 	/* 1413 says don't do this -- but it's broken otherwise */
97758957Seric 	while (isascii(*++p) && isspace(*p))
97858957Seric 		continue;
97958957Seric 
98058951Seric 	/* p now points to the authenticated name */
98158951Seric 	(void) sprintf(hbuf, "%s@%s", p, RealHostName);
98258957Seric 	goto finish;
98358957Seric 
98458957Seric #endif /* IDENTPROTO */
98558957Seric 
98658957Seric noident:
98758957Seric 	(void) strcpy(hbuf, RealHostName);
98858957Seric 
98958957Seric finish:
99058951Seric 	if (RealHostName[0] != '[')
99158951Seric 	{
99258951Seric 		p = &hbuf[strlen(hbuf)];
99358951Seric 		(void) sprintf(p, " [%s]", anynet_ntoa(&RealHostAddr));
99458951Seric 	}
99558957Seric 	if (tTd(9, 1))
99658951Seric 		printf("getauthinfo: %s\n", hbuf);
99758308Seric 	return hbuf;
99858308Seric }
99958308Seric /*
100060089Seric **  HOST_MAP_LOOKUP -- turn a hostname into canonical form
100153751Seric **
100253751Seric **	Parameters:
100356823Seric **		map -- a pointer to this map (unused).
100460089Seric **		name -- the (presumably unqualified) hostname.
100560257Seric **		av -- unused -- for compatibility with other mapping
100655019Seric **			functions.
100759084Seric **		statp -- an exit status (out parameter) -- set to
100859084Seric **			EX_TEMPFAIL if the name server is unavailable.
100953751Seric **
101053751Seric **	Returns:
101153751Seric **		The mapping, if found.
101253751Seric **		NULL if no mapping found.
101353751Seric **
101453751Seric **	Side Effects:
101553751Seric **		Looks up the host specified in hbuf.  If it is not
101653751Seric **		the canonical name for that host, return the canonical
101753751Seric **		name.
101853751Seric */
101951315Seric 
102053751Seric char *
102160257Seric host_map_lookup(map, name, av, statp)
102256823Seric 	MAP *map;
102360089Seric 	char *name;
102460257Seric 	char **av;
102559084Seric 	int *statp;
102616911Seric {
102716911Seric 	register struct hostent *hp;
102833932Sbostic 	u_long in_addr;
102956823Seric 	char *cp;
103058110Seric 	int i;
103159671Seric 	register STAB *s;
103260257Seric 	char hbuf[MAXNAME];
103359671Seric 	extern struct hostent *gethostbyaddr();
103459671Seric 	extern int h_errno;
103516911Seric 
103625574Smiriam 	/*
103759671Seric 	**  See if we have already looked up this name.  If so, just
103859671Seric 	**  return it.
103959671Seric 	*/
104053751Seric 
104160089Seric 	s = stab(name, ST_NAMECANON, ST_ENTER);
104259671Seric 	if (bitset(NCF_VALID, s->s_namecanon.nc_flags))
104359671Seric 	{
104459986Seric 		if (tTd(9, 1))
104560089Seric 			printf("host_map_lookup(%s) => CACHE %s\n",
104660089Seric 				name, s->s_namecanon.nc_cname);
104759671Seric 		errno = s->s_namecanon.nc_errno;
104859671Seric 		h_errno = s->s_namecanon.nc_herrno;
104959671Seric 		*statp = s->s_namecanon.nc_stat;
105064797Seric 		if (CurEnv->e_message == NULL && *statp == EX_TEMPFAIL)
105165199Seric 		{
105265199Seric 			sprintf(hbuf, "%s: Name server timeout",
105365199Seric 				shortenstring(name, 33));
105465199Seric 			CurEnv->e_message = newstr(hbuf);
105565199Seric 		}
105659671Seric 		return s->s_namecanon.nc_cname;
105759671Seric 	}
105859671Seric 
105959671Seric 	/*
106059671Seric 	**  If first character is a bracket, then it is an address
106159671Seric 	**  lookup.  Address is copied into a temporary buffer to
106260089Seric 	**  strip the brackets and to preserve name if address is
106359671Seric 	**  unknown.
106459671Seric 	*/
106559671Seric 
106660089Seric 	if (*name != '[')
106753751Seric 	{
106855019Seric 		extern bool getcanonname();
106955019Seric 
107058798Seric 		if (tTd(9, 1))
107160089Seric 			printf("host_map_lookup(%s) => ", name);
107259671Seric 		s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
107360089Seric 		(void) strcpy(hbuf, name);
107463842Seric 		if (getcanonname(hbuf, sizeof hbuf - 1, TRUE))
107558796Seric 		{
107658796Seric 			if (tTd(9, 1))
107758796Seric 				printf("%s\n", hbuf);
107860257Seric 			cp = map_rewrite(map, hbuf, strlen(hbuf), av);
107960257Seric 			s->s_namecanon.nc_cname = newstr(cp);
108060257Seric 			return cp;
108158796Seric 		}
108253751Seric 		else
108358796Seric 		{
108459084Seric 			register struct hostent *hp;
108559084Seric 
108658796Seric 			if (tTd(9, 1))
108759084Seric 				printf("FAIL (%d)\n", h_errno);
108859671Seric 			s->s_namecanon.nc_errno = errno;
108959671Seric 			s->s_namecanon.nc_herrno = h_errno;
109059084Seric 			switch (h_errno)
109159084Seric 			{
109259084Seric 			  case TRY_AGAIN:
109359596Seric 				if (UseNameServer)
109459734Seric 				{
109565202Seric 					sprintf(hbuf, "%s: Name server timeout",
109665199Seric 						shortenstring(name, 33));
109765202Seric 					message("%s", hbuf);
109859734Seric 					if (CurEnv->e_message == NULL)
109965202Seric 						CurEnv->e_message = newstr(hbuf);
110059734Seric 				}
110159084Seric 				*statp = EX_TEMPFAIL;
110259084Seric 				break;
110359084Seric 
110459084Seric 			  case HOST_NOT_FOUND:
110559084Seric 				*statp = EX_NOHOST;
110659084Seric 				break;
110759084Seric 
110859084Seric 			  case NO_RECOVERY:
110959084Seric 				*statp = EX_SOFTWARE;
111059084Seric 				break;
111159084Seric 
111259084Seric 			  default:
111359084Seric 				*statp = EX_UNAVAILABLE;
111459084Seric 				break;
111559084Seric 			}
111659671Seric 			s->s_namecanon.nc_stat = *statp;
111759084Seric 			if (*statp != EX_TEMPFAIL || UseNameServer)
111859084Seric 				return NULL;
111959084Seric 
112059084Seric 			/*
112159084Seric 			**  Try to look it up in /etc/hosts
112259084Seric 			*/
112359084Seric 
112460089Seric 			hp = gethostbyname(name);
112559084Seric 			if (hp == NULL)
112659084Seric 			{
112759084Seric 				/* no dice there either */
112859671Seric 				s->s_namecanon.nc_stat = *statp = EX_NOHOST;
112959084Seric 				return NULL;
113059084Seric 			}
113159084Seric 
113259671Seric 			s->s_namecanon.nc_stat = *statp = EX_OK;
113360257Seric 			cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
113460257Seric 			s->s_namecanon.nc_cname = newstr(cp);
113560257Seric 			return cp;
113658796Seric 		}
113753751Seric 	}
113860089Seric 	if ((cp = strchr(name, ']')) == NULL)
113953751Seric 		return (NULL);
114040994Sbostic 	*cp = '\0';
114160089Seric 	in_addr = inet_addr(&name[1]);
114258110Seric 
114358110Seric 	/* nope -- ask the name server */
114433932Sbostic 	hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET);
114559671Seric 	s->s_namecanon.nc_errno = errno;
114659671Seric 	s->s_namecanon.nc_herrno = h_errno;
114759671Seric 	s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
114833932Sbostic 	if (hp == NULL)
114959671Seric 	{
115059671Seric 		s->s_namecanon.nc_stat = *statp = EX_NOHOST;
115153751Seric 		return (NULL);
115259671Seric 	}
115353751Seric 
115458110Seric 	/* found a match -- copy out */
115560257Seric 	cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
115659671Seric 	s->s_namecanon.nc_stat = *statp = EX_OK;
115760257Seric 	s->s_namecanon.nc_cname = newstr(cp);
115860257Seric 	return cp;
115933932Sbostic }
116058755Seric /*
116158755Seric **  ANYNET_NTOA -- convert a network address to printable form.
116258755Seric **
116358755Seric **	Parameters:
116458755Seric **		sap -- a pointer to a sockaddr structure.
116558755Seric **
116658755Seric **	Returns:
116758755Seric **		A printable version of that sockaddr.
116858755Seric */
116916911Seric 
117058755Seric char *
117158755Seric anynet_ntoa(sap)
117258755Seric 	register SOCKADDR *sap;
117358755Seric {
117458755Seric 	register char *bp;
117558755Seric 	register char *ap;
117658755Seric 	int l;
117764734Seric 	static char buf[100];
117858755Seric 
117958798Seric 	/* check for null/zero family */
118058798Seric 	if (sap == NULL)
118158798Seric 		return "NULLADDR";
118258798Seric 	if (sap->sa.sa_family == 0)
118358798Seric 		return "0";
118458798Seric 
118564734Seric 	switch (sap->sa.sa_family)
118664734Seric 	{
118764734Seric #ifdef MAYBENEXTRELEASE		/*** UNTESTED *** UNTESTED *** UNTESTED ***/
118864821Seric #ifdef NETUNIX
118964734Seric 	  case AF_UNIX:
119064758Seric 	  	if (sap->sunix.sun_path[0] != '\0')
119164758Seric 	  		sprintf(buf, "[UNIX: %.64s]", sap->sunix.sun_path);
119264734Seric 	  	else
119364734Seric 	  		sprintf(buf, "[UNIX: localhost]");
119464734Seric 		return buf;
119564734Seric #endif
119664821Seric #endif
119764734Seric 
119858778Seric #ifdef NETINET
119964734Seric 	  case AF_INET:
120058755Seric 		return inet_ntoa(((struct sockaddr_in *) sap)->sin_addr);
120158778Seric #endif
120258755Seric 
120364734Seric 	  default:
120464734Seric 	  	/* this case is only to ensure syntactic correctness */
120564734Seric 	  	break;
120664734Seric 	}
120764734Seric 
120858755Seric 	/* unknown family -- just dump bytes */
120958778Seric 	(void) sprintf(buf, "Family %d: ", sap->sa.sa_family);
121058755Seric 	bp = &buf[strlen(buf)];
121158778Seric 	ap = sap->sa.sa_data;
121258778Seric 	for (l = sizeof sap->sa.sa_data; --l >= 0; )
121358755Seric 	{
121458755Seric 		(void) sprintf(bp, "%02x:", *ap++ & 0377);
121558755Seric 		bp += 3;
121658755Seric 	}
121758755Seric 	*--bp = '\0';
121858755Seric 	return buf;
121958755Seric }
122058951Seric /*
122158951Seric **  HOSTNAMEBYANYADDR -- return name of host based on address
122258951Seric **
122358951Seric **	Parameters:
122458951Seric **		sap -- SOCKADDR pointer
122558951Seric **
122658951Seric **	Returns:
122758951Seric **		text representation of host name.
122858951Seric **
122958951Seric **	Side Effects:
123058951Seric **		none.
123158951Seric */
123258755Seric 
123358951Seric char *
123458951Seric hostnamebyanyaddr(sap)
123558951Seric 	register SOCKADDR *sap;
123658951Seric {
123758951Seric 	register struct hostent *hp;
123864734Seric 	int saveretry;
123958951Seric 
124059042Seric #ifdef NAMED_BIND
124159042Seric 	/* shorten name server timeout to avoid higher level timeouts */
124259042Seric 	saveretry = _res.retry;
124359042Seric 	_res.retry = 3;
124459042Seric #endif /* NAMED_BIND */
124559042Seric 
124658951Seric 	switch (sap->sa.sa_family)
124758951Seric 	{
124858951Seric #ifdef NETINET
124958951Seric 	  case AF_INET:
125058951Seric 		hp = gethostbyaddr((char *) &sap->sin.sin_addr,
125158951Seric 			sizeof sap->sin.sin_addr,
125258951Seric 			AF_INET);
125358951Seric 		break;
125458951Seric #endif
125558951Seric 
125658951Seric #ifdef NETISO
125758951Seric 	  case AF_ISO:
125858951Seric 		hp = gethostbyaddr((char *) &sap->siso.siso_addr,
125958951Seric 			sizeof sap->siso.siso_addr,
126058951Seric 			AF_ISO);
126158951Seric 		break;
126258951Seric #endif
126358951Seric 
126464734Seric #ifdef MAYBENEXTRELEASE		/*** UNTESTED *** UNTESTED *** UNTESTED ***/
126564734Seric 	  case AF_UNIX:
126664734Seric 		hp = NULL;
126764734Seric 		break;
126864734Seric #endif
126964734Seric 
127058951Seric 	  default:
127158951Seric 		hp = gethostbyaddr(sap->sa.sa_data,
127258951Seric 			   sizeof sap->sa.sa_data,
127358951Seric 			   sap->sa.sa_family);
127458951Seric 		break;
127558951Seric 	}
127658951Seric 
127759042Seric #ifdef NAMED_BIND
127859042Seric 	_res.retry = saveretry;
127959042Seric #endif /* NAMED_BIND */
128059042Seric 
128158951Seric 	if (hp != NULL)
128258951Seric 		return hp->h_name;
128358951Seric 	else
128458951Seric 	{
128558951Seric 		/* produce a dotted quad */
128658951Seric 		static char buf[512];
128758951Seric 
128858951Seric 		(void) sprintf(buf, "[%s]", anynet_ntoa(sap));
128958951Seric 		return buf;
129058951Seric 	}
129158951Seric }
129258951Seric 
129356795Seric # else /* DAEMON */
129416911Seric /* code for systems without sophisticated networking */
129510758Seric 
129610758Seric /*
129710758Seric **  MYHOSTNAME -- stub version for case of no daemon code.
129811297Seric **
129911297Seric **	Can't convert to upper case here because might be a UUCP name.
130012313Seric **
130112313Seric **	Mark, you can change this to be anything you want......
130210758Seric */
130310758Seric 
130410758Seric char **
130512313Seric myhostname(hostbuf, size)
130610758Seric 	char hostbuf[];
130712313Seric 	int size;
130810758Seric {
130910758Seric 	register FILE *f;
131010758Seric 
131110758Seric 	hostbuf[0] = '\0';
131210758Seric 	f = fopen("/usr/include/whoami", "r");
131310758Seric 	if (f != NULL)
131410758Seric 	{
131512313Seric 		(void) fgets(hostbuf, size, f);
131610758Seric 		fixcrlf(hostbuf, TRUE);
131710758Seric 		(void) fclose(f);
131810758Seric 	}
131910758Seric 	return (NULL);
132010758Seric }
132116911Seric /*
132258951Seric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
132358308Seric **
132458308Seric **	Parameters:
132558308Seric **		fd -- the descriptor
132658308Seric **
132758308Seric **	Returns:
132858308Seric **		The host name associated with this descriptor, if it can
132958308Seric **			be determined.
133058308Seric **		NULL otherwise.
133158308Seric **
133258308Seric **	Side Effects:
133358308Seric **		none
133458308Seric */
133558308Seric 
133658308Seric char *
133758951Seric getauthinfo(fd)
133858308Seric 	int fd;
133958308Seric {
134058308Seric 	return NULL;
134158308Seric }
134258308Seric /*
134316911Seric **  MAPHOSTNAME -- turn a hostname into canonical form
134416911Seric **
134516911Seric **	Parameters:
134656823Seric **		map -- a pointer to the database map.
134760089Seric **		name -- a buffer containing a hostname.
134853751Seric **		avp -- a pointer to a (cf file defined) argument vector.
134959084Seric **		statp -- an exit status (out parameter).
135016911Seric **
135116911Seric **	Returns:
135253751Seric **		mapped host name
135351315Seric **		FALSE otherwise.
135416911Seric **
135516911Seric **	Side Effects:
135660089Seric **		Looks up the host specified in name.  If it is not
135716911Seric **		the canonical name for that host, replace it with
135816911Seric **		the canonical name.  If the name is unknown, or it
135916911Seric **		is already the canonical name, leave it unchanged.
136016911Seric */
136110758Seric 
136216911Seric /*ARGSUSED*/
136353751Seric char *
136460089Seric host_map_lookup(map, name, avp, statp)
136556823Seric 	MAP *map;
136660089Seric 	char *name;
136753751Seric 	char **avp;
136859084Seric 	char *statp;
136916911Seric {
137059084Seric 	register struct hostent *hp;
137159084Seric 
137260089Seric 	hp = gethostbyname(name);
137359084Seric 	if (hp != NULL)
137459084Seric 		return hp->h_name;
137559084Seric 	*statp = EX_NOHOST;
137653751Seric 	return NULL;
137716911Seric }
137816911Seric 
137956795Seric #endif /* DAEMON */
1380