xref: /csrg-svn/usr.sbin/sendmail/src/daemon.c (revision 64334)
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*64334Seric static char sccsid[] = "@(#)daemon.c	8.8 (Berkeley) 08/23/93 (with daemon mode)";
1533780Sbostic #else
16*64334Seric static char sccsid[] = "@(#)daemon.c	8.8 (Berkeley) 08/23/93 (without daemon mode)";
1733780Sbostic #endif
1833780Sbostic #endif /* not lint */
194535Seric 
2033780Sbostic #ifdef DAEMON
2133780Sbostic 
2223120Seric # include <netdb.h>
2323120Seric # include <sys/wait.h>
2423120Seric # include <sys/time.h>
255978Seric 
2659042Seric #ifdef NAMED_BIND
2759042Seric # include <arpa/nameser.h>
2859042Seric # include <resolv.h>
2959042Seric #endif
3059042Seric 
314535Seric /*
324535Seric **  DAEMON.C -- routines to use when running as a daemon.
337556Seric **
347556Seric **	This entire file is highly dependent on the 4.2 BSD
357556Seric **	interprocess communication primitives.  No attempt has
367556Seric **	been made to make this file portable to Version 7,
377556Seric **	Version 6, MPX files, etc.  If you should try such a
387556Seric **	thing yourself, I recommend chucking the entire file
397556Seric **	and starting from scratch.  Basic semantics are:
407556Seric **
417556Seric **	getrequests()
427556Seric **		Opens a port and initiates a connection.
437556Seric **		Returns in a child.  Must set InChannel and
447556Seric **		OutChannel appropriately.
4510206Seric **	clrdaemon()
4610206Seric **		Close any open files associated with getting
4710206Seric **		the connection; this is used when running the queue,
4810206Seric **		etc., to avoid having extra file descriptors during
4910206Seric **		the queue run and to avoid confusing the network
5010206Seric **		code (if it cares).
5152106Seric **	makeconnection(host, port, outfile, infile, usesecureport)
527556Seric **		Make a connection to the named host on the given
537556Seric **		port.  Set *outfile and *infile to the files
547556Seric **		appropriate for communication.  Returns zero on
557556Seric **		success, else an exit status describing the
567556Seric **		error.
5760089Seric **	host_map_lookup(map, hbuf, avp, pstat)
5856823Seric **		Convert the entry in hbuf into a canonical form.
594535Seric */
604535Seric /*
614535Seric **  GETREQUESTS -- open mail IPC port and get requests.
624535Seric **
634535Seric **	Parameters:
644535Seric **		none.
654535Seric **
664535Seric **	Returns:
674535Seric **		none.
684535Seric **
694535Seric **	Side Effects:
704535Seric **		Waits until some interesting activity occurs.  When
714535Seric **		it does, a child is created to process it, and the
724535Seric **		parent waits for completion.  Return from this
739886Seric **		routine is always in the child.  The file pointers
749886Seric **		"InChannel" and "OutChannel" should be set to point
759886Seric **		to the communication channel.
764535Seric */
774535Seric 
7858849Seric int		DaemonSocket	= -1;		/* fd describing socket */
7958849Seric SOCKADDR	DaemonAddr;			/* socket for incoming */
8059783Seric int		ListenQueueSize = 10;		/* size of listen queue */
8116144Seric 
824535Seric getrequests()
834535Seric {
849610Seric 	int t;
859610Seric 	register struct servent *sp;
8625027Seric 	int on = 1;
8753751Seric 	bool refusingconnections = TRUE;
8858419Seric 	FILE *pidf;
8946928Sbostic 	extern void reapchild();
907117Seric 
919610Seric 	/*
929610Seric 	**  Set up the address for the mailer.
939610Seric 	*/
949610Seric 
9558849Seric 	if (DaemonAddr.sin.sin_family == 0)
9658849Seric 		DaemonAddr.sin.sin_family = AF_INET;
9758849Seric 	if (DaemonAddr.sin.sin_addr.s_addr == 0)
9858849Seric 		DaemonAddr.sin.sin_addr.s_addr = INADDR_ANY;
9958849Seric 	if (DaemonAddr.sin.sin_port == 0)
1009610Seric 	{
10158849Seric 		sp = getservbyname("smtp", "tcp");
10258849Seric 		if (sp == NULL)
10358849Seric 		{
10458909Seric 			syserr("554 service \"smtp\" unknown");
10558849Seric 			goto severe;
10658849Seric 		}
10758849Seric 		DaemonAddr.sin.sin_port = sp->s_port;
1089610Seric 	}
1099610Seric 
1109610Seric 	/*
1119610Seric 	**  Try to actually open the connection.
1129610Seric 	*/
1139610Seric 
1149610Seric 	if (tTd(15, 1))
11558849Seric 		printf("getrequests: port 0x%x\n", DaemonAddr.sin.sin_port);
1169610Seric 
1179610Seric 	/* get a socket for the SMTP connection */
11859041Seric 	DaemonSocket = socket(DaemonAddr.sa.sa_family, SOCK_STREAM, 0);
11910206Seric 	if (DaemonSocket < 0)
1209610Seric 	{
1219610Seric 		/* probably another daemon already */
1229610Seric 		syserr("getrequests: can't create socket");
1239610Seric 	  severe:
1249610Seric # ifdef LOG
1259610Seric 		if (LogLevel > 0)
12657663Seric 			syslog(LOG_ALERT, "problem creating SMTP socket");
12756795Seric # endif /* LOG */
1289610Seric 		finis();
1299610Seric 	}
13010347Seric 
13110347Seric 	/* turn on network debugging? */
13256328Seric 	if (tTd(15, 101))
13324945Seric 		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on);
13410347Seric 
13525027Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on);
13625027Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on);
13725027Seric 
13859041Seric 	switch (DaemonAddr.sa.sa_family)
1399610Seric 	{
14059041Seric # ifdef NETINET
14159041Seric 	  case AF_INET:
14259041Seric 		t = sizeof DaemonAddr.sin;
14359041Seric 		break;
14459041Seric # endif
14559041Seric 
14659041Seric # ifdef NETISO
14759041Seric 	  case AF_ISO:
14859041Seric 		t = sizeof DaemonAddr.siso;
14959041Seric 		break;
15059041Seric # endif
15159041Seric 
15259041Seric 	  default:
15359041Seric 		t = sizeof DaemonAddr;
15459041Seric 		break;
15559041Seric 	}
15659041Seric 
15759041Seric 	if (bind(DaemonSocket, &DaemonAddr.sa, t) < 0)
15859041Seric 	{
1599610Seric 		syserr("getrequests: cannot bind");
16010206Seric 		(void) close(DaemonSocket);
1619610Seric 		goto severe;
1629610Seric 	}
1639610Seric 
16464035Seric 	(void) setsignal(SIGCHLD, reapchild);
16524945Seric 
16658419Seric 	/* write the pid to the log file for posterity */
16758419Seric 	pidf = fopen(PidFile, "w");
16858419Seric 	if (pidf != NULL)
16958419Seric 	{
17063863Seric 		extern char *CommandLineArgs;
17163863Seric 
17263863Seric 		/* write the process id on line 1 */
17358419Seric 		fprintf(pidf, "%d\n", getpid());
17463863Seric 
17563863Seric 		/* line 2 contains all command line flags */
17663863Seric 		fprintf(pidf, "%s\n", CommandLineArgs);
17763863Seric 
17863863Seric 		/* flush and close */
17958419Seric 		fclose(pidf);
18058419Seric 	}
18158419Seric 
18258419Seric 
1839610Seric 	if (tTd(15, 1))
18410206Seric 		printf("getrequests: %d\n", DaemonSocket);
1859610Seric 
1864631Seric 	for (;;)
1874631Seric 	{
18814875Seric 		register int pid;
18911147Seric 		auto int lotherend;
19053751Seric 		extern bool refuseconnections();
19111147Seric 
19214875Seric 		/* see if we are rejecting connections */
19353751Seric 		CurrentLA = getla();
19453751Seric 		if (refuseconnections())
19536584Sbostic 		{
19653751Seric 			if (!refusingconnections)
19753751Seric 			{
19853751Seric 				/* don't queue so peer will fail quickly */
19953751Seric 				(void) listen(DaemonSocket, 0);
20053751Seric 				refusingconnections = TRUE;
20153751Seric 			}
20257385Seric 			setproctitle("rejecting connections: load average: %d",
20357385Seric 				CurrentLA);
20414875Seric 			sleep(5);
20553751Seric 			continue;
20636584Sbostic 		}
20714875Seric 
20853751Seric 		if (refusingconnections)
20953751Seric 		{
21053751Seric 			/* start listening again */
21159783Seric 			if (listen(DaemonSocket, ListenQueueSize) < 0)
21253751Seric 			{
21353751Seric 				syserr("getrequests: cannot listen");
21453751Seric 				(void) close(DaemonSocket);
21553751Seric 				goto severe;
21653751Seric 			}
21753751Seric 			setproctitle("accepting connections");
21853751Seric 			refusingconnections = FALSE;
21953751Seric 		}
22053751Seric 
2219610Seric 		/* wait for a connection */
2229610Seric 		do
2239610Seric 		{
2249610Seric 			errno = 0;
22536230Skarels 			lotherend = sizeof RealHostAddr;
22646928Sbostic 			t = accept(DaemonSocket,
22746928Sbostic 			    (struct sockaddr *)&RealHostAddr, &lotherend);
2289610Seric 		} while (t < 0 && errno == EINTR);
2299610Seric 		if (t < 0)
2305978Seric 		{
2319610Seric 			syserr("getrequests: accept");
2329610Seric 			sleep(5);
2339610Seric 			continue;
2345978Seric 		}
2354631Seric 
2365978Seric 		/*
2375978Seric 		**  Create a subprocess to process the mail.
2385978Seric 		*/
2395978Seric 
2407677Seric 		if (tTd(15, 2))
2419610Seric 			printf("getrequests: forking (fd = %d)\n", t);
2425978Seric 
2434636Seric 		pid = fork();
2444636Seric 		if (pid < 0)
2454631Seric 		{
2464636Seric 			syserr("daemon: cannot fork");
2474636Seric 			sleep(10);
2489610Seric 			(void) close(t);
2494636Seric 			continue;
2504631Seric 		}
2514631Seric 
2524636Seric 		if (pid == 0)
2534631Seric 		{
25464086Seric 			char *p;
25558951Seric 			extern char *hostnamebyanyaddr();
25611147Seric 
2574636Seric 			/*
2584636Seric 			**  CHILD -- return to caller.
25911147Seric 			**	Collect verified idea of sending host.
2604636Seric 			**	Verify calling user id if possible here.
2614636Seric 			*/
2624631Seric 
26364035Seric 			(void) setsignal(SIGCHLD, SIG_DFL);
26459156Seric 			OpMode = MD_SMTP;
26524950Seric 
26611147Seric 			/* determine host name */
26764086Seric 			p = hostnamebyanyaddr(&RealHostAddr);
26864086Seric 			RealHostName = newstr(p);
26958778Seric 
27055173Seric #ifdef LOG
27163842Seric 			if (LogLevel > 11)
27255173Seric 			{
27355173Seric 				/* log connection information */
27455173Seric 				syslog(LOG_INFO, "connect from %s (%s)",
27558951Seric 					RealHostName, anynet_ntoa(&RealHostAddr));
27655173Seric 			}
27755173Seric #endif
27855173Seric 
27959254Seric 			(void) close(DaemonSocket);
28059254Seric 			InChannel = fdopen(t, "r");
28159254Seric 			OutChannel = fdopen(dup(t), "w");
28259254Seric 
28316884Seric 			/* should we check for illegal connection here? XXX */
28459156Seric #ifdef XLA
28559156Seric 			if (!xla_host_ok(RealHostName))
28659156Seric 			{
28759254Seric 				message("421 Too many SMTP sessions for this host");
28859156Seric 				exit(0);
28959156Seric 			}
29059156Seric #endif
29116884Seric 
2927677Seric 			if (tTd(15, 2))
2935978Seric 				printf("getreq: returning\n");
2944636Seric 			return;
2954631Seric 		}
2964631Seric 
2977117Seric 		/* close the port so that others will hang (for a while) */
2989610Seric 		(void) close(t);
2994631Seric 	}
3009886Seric 	/*NOTREACHED*/
3014631Seric }
3025978Seric /*
30310206Seric **  CLRDAEMON -- reset the daemon connection
30410206Seric **
30510206Seric **	Parameters:
30610206Seric **		none.
30710206Seric **
30810206Seric **	Returns:
30910206Seric **		none.
31010206Seric **
31110206Seric **	Side Effects:
31210206Seric **		releases any resources used by the passive daemon.
31310206Seric */
31410206Seric 
31510206Seric clrdaemon()
31610206Seric {
31710206Seric 	if (DaemonSocket >= 0)
31810206Seric 		(void) close(DaemonSocket);
31910206Seric 	DaemonSocket = -1;
32010206Seric }
32110206Seric /*
32258849Seric **  SETDAEMONOPTIONS -- set options for running the daemon
32358849Seric **
32458849Seric **	Parameters:
32558849Seric **		p -- the options line.
32658849Seric **
32758849Seric **	Returns:
32858849Seric **		none.
32958849Seric */
33058849Seric 
33158849Seric setdaemonoptions(p)
33258849Seric 	register char *p;
33358849Seric {
33458873Seric 	if (DaemonAddr.sa.sa_family == AF_UNSPEC)
33558873Seric 		DaemonAddr.sa.sa_family = AF_INET;
33658873Seric 
33758849Seric 	while (p != NULL)
33858849Seric 	{
33958849Seric 		register char *f;
34058849Seric 		register char *v;
34158849Seric 
34258849Seric 		while (isascii(*p) && isspace(*p))
34358849Seric 			p++;
34458849Seric 		if (*p == '\0')
34558849Seric 			break;
34658849Seric 		f = p;
34758849Seric 		p = strchr(p, ',');
34858849Seric 		if (p != NULL)
34958849Seric 			*p++ = '\0';
35058849Seric 		v = strchr(f, '=');
35158849Seric 		if (v == NULL)
35258849Seric 			continue;
35358849Seric 		while (isascii(*++v) && isspace(*v))
35458849Seric 			continue;
35558849Seric 
35658849Seric 		switch (*f)
35758849Seric 		{
35858873Seric 		  case 'F':		/* address family */
35958849Seric 			if (isascii(*v) && isdigit(*v))
36058873Seric 				DaemonAddr.sa.sa_family = atoi(v);
36158873Seric #ifdef NETINET
36258873Seric 			else if (strcasecmp(v, "inet") == 0)
36358873Seric 				DaemonAddr.sa.sa_family = AF_INET;
36458873Seric #endif
36558873Seric #ifdef NETISO
36658873Seric 			else if (strcasecmp(v, "iso") == 0)
36758873Seric 				DaemonAddr.sa.sa_family = AF_ISO;
36858873Seric #endif
36958873Seric #ifdef NETNS
37058873Seric 			else if (strcasecmp(v, "ns") == 0)
37158873Seric 				DaemonAddr.sa.sa_family = AF_NS;
37258873Seric #endif
37358873Seric #ifdef NETX25
37458873Seric 			else if (strcasecmp(v, "x.25") == 0)
37558873Seric 				DaemonAddr.sa.sa_family = AF_CCITT;
37658873Seric #endif
37758849Seric 			else
37858873Seric 				syserr("554 Unknown address family %s in Family=option", v);
37958873Seric 			break;
38058873Seric 
38158873Seric 		  case 'A':		/* address */
38258873Seric 			switch (DaemonAddr.sa.sa_family)
38358849Seric 			{
38458873Seric #ifdef NETINET
38558873Seric 			  case AF_INET:
38658873Seric 				if (isascii(*v) && isdigit(*v))
38758873Seric 					DaemonAddr.sin.sin_addr.s_addr = inet_network(v);
38858873Seric 				else
38958873Seric 				{
39058873Seric 					register struct netent *np;
39158849Seric 
39258873Seric 					np = getnetbyname(v);
39358873Seric 					if (np == NULL)
39458873Seric 						syserr("554 network \"%s\" unknown", v);
39558873Seric 					else
39658873Seric 						DaemonAddr.sin.sin_addr.s_addr = np->n_net;
39758873Seric 				}
39858873Seric 				break;
39958873Seric #endif
40058873Seric 
40158873Seric 			  default:
40258873Seric 				syserr("554 Address= option unsupported for family %d",
40358873Seric 					DaemonAddr.sa.sa_family);
40458873Seric 				break;
40558849Seric 			}
40658849Seric 			break;
40758849Seric 
40858873Seric 		  case 'P':		/* port */
40958873Seric 			switch (DaemonAddr.sa.sa_family)
41058849Seric 			{
41158873Seric 				short port;
41258849Seric 
41358873Seric #ifdef NETINET
41458873Seric 			  case AF_INET:
41558873Seric 				if (isascii(*v) && isdigit(*v))
41658873Seric 					DaemonAddr.sin.sin_port = atoi(v);
41758849Seric 				else
41858873Seric 				{
41958873Seric 					register struct servent *sp;
42058873Seric 
42158873Seric 					sp = getservbyname(v, "tcp");
42258873Seric 					if (sp == NULL)
42358909Seric 						syserr("554 service \"%s\" unknown", v);
42458873Seric 					else
42558873Seric 						DaemonAddr.sin.sin_port = sp->s_port;
42658873Seric 				}
42758873Seric 				break;
42858873Seric #endif
42958873Seric 
43058873Seric #ifdef NETISO
43158873Seric 			  case AF_ISO:
43258873Seric 				/* assume two byte transport selector */
43358873Seric 				if (isascii(*v) && isdigit(*v))
43458873Seric 					port = atoi(v);
43558873Seric 				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 						port = sp->s_port;
44458873Seric 				}
44558873Seric 				bcopy((char *) &port, TSEL(&DaemonAddr.siso), 2);
44658873Seric 				break;
44758873Seric #endif
44858873Seric 
44958873Seric 			  default:
45058873Seric 				syserr("554 Port= option unsupported for family %d",
45158873Seric 					DaemonAddr.sa.sa_family);
45258873Seric 				break;
45358849Seric 			}
45458849Seric 			break;
45559783Seric 
45659783Seric 		  case 'L':		/* listen queue size */
45759783Seric 			ListenQueueSize = atoi(v);
45859783Seric 			break;
45958849Seric 		}
46058849Seric 	}
46158849Seric }
46258849Seric /*
4636039Seric **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
4646039Seric **
4656039Seric **	Parameters:
4666039Seric **		host -- the name of the host.
4676633Seric **		port -- the port number to connect to.
46853739Seric **		mci -- a pointer to the mail connection information
46953739Seric **			structure to be filled in.
47052106Seric **		usesecureport -- if set, use a low numbered (reserved)
47152106Seric **			port to provide some rudimentary authentication.
4726039Seric **
4736039Seric **	Returns:
4746039Seric **		An exit code telling whether the connection could be
4756039Seric **			made and if not why not.
4766039Seric **
4776039Seric **	Side Effects:
4786039Seric **		none.
4796039Seric */
4805978Seric 
48158755Seric SOCKADDR	CurHostAddr;		/* address of current host */
48258305Seric 
48354967Seric int
48453739Seric makeconnection(host, port, mci, usesecureport)
4856039Seric 	char *host;
4867286Seric 	u_short port;
48754967Seric 	register MCI *mci;
48852106Seric 	bool usesecureport;
4896039Seric {
49029430Sbloom 	register int i, s;
49129430Sbloom 	register struct hostent *hp = (struct hostent *)NULL;
49258755Seric 	SOCKADDR addr;
49352106Seric 	int sav_errno;
49458755Seric 	int addrlen;
49535651Seric #ifdef NAMED_BIND
49635651Seric 	extern int h_errno;
49735651Seric #endif
4986039Seric 
4996039Seric 	/*
5006039Seric 	**  Set up the address for the mailer.
5019308Seric 	**	Accept "[a.b.c.d]" syntax for host name.
5026039Seric 	*/
5036039Seric 
50435651Seric #ifdef NAMED_BIND
50525475Smiriam 	h_errno = 0;
50635651Seric #endif
50725475Smiriam 	errno = 0;
50858864Seric 	bzero(&CurHostAddr, sizeof CurHostAddr);
509*64334Seric 	SmtpPhase = mci->mci_phase = "initial connection";
51058906Seric 	CurHostName = host;
51125475Smiriam 
5129308Seric 	if (host[0] == '[')
5139308Seric 	{
51411147Seric 		long hid;
51556795Seric 		register char *p = strchr(host, ']');
5169308Seric 
51711147Seric 		if (p != NULL)
5189308Seric 		{
51911147Seric 			*p = '\0';
52059884Seric #ifdef NETINET
52111147Seric 			hid = inet_addr(&host[1]);
52258360Seric 			if (hid == -1)
52359884Seric #endif
52458360Seric 			{
52558360Seric 				/* try it as a host name (avoid MX lookup) */
52658360Seric 				hp = gethostbyname(&host[1]);
52758360Seric 				*p = ']';
52858360Seric 				goto gothostent;
52958360Seric 			}
53011147Seric 			*p = ']';
5319308Seric 		}
53258360Seric 		if (p == NULL)
5339308Seric 		{
53458151Seric 			usrerr("553 Invalid numeric domain spec \"%s\"", host);
5359308Seric 			return (EX_NOHOST);
5369308Seric 		}
53759884Seric #ifdef NETINET
53859884Seric 		addr.sin.sin_family = AF_INET;		/*XXX*/
53958778Seric 		addr.sin.sin_addr.s_addr = hid;
54059884Seric #endif
5419308Seric 	}
5429610Seric 	else
5439610Seric 	{
54429430Sbloom 		hp = gethostbyname(host);
54558360Seric gothostent:
54625475Smiriam 		if (hp == NULL)
54724945Seric 		{
54835651Seric #ifdef NAMED_BIND
54925475Smiriam 			if (errno == ETIMEDOUT || h_errno == TRY_AGAIN)
55025475Smiriam 				return (EX_TEMPFAIL);
55125657Seric 
55235651Seric 			/* if name server is specified, assume temp fail */
55335651Seric 			if (errno == ECONNREFUSED && UseNameServer)
55435651Seric 				return (EX_TEMPFAIL);
55535651Seric #endif
55625475Smiriam 			return (EX_NOHOST);
55724945Seric 		}
55858778Seric 		addr.sa.sa_family = hp->h_addrtype;
55958778Seric 		switch (hp->h_addrtype)
56058778Seric 		{
56158778Seric #ifdef NETINET
56258778Seric 		  case AF_INET:
56358755Seric 			bcopy(hp->h_addr,
56458778Seric 				&addr.sin.sin_addr,
56558755Seric 				hp->h_length);
56658778Seric 			break;
56758778Seric #endif
56858778Seric 
56958778Seric 		  default:
57058755Seric 			bcopy(hp->h_addr,
57158778Seric 				addr.sa.sa_data,
57258755Seric 				hp->h_length);
57358778Seric 			break;
57458778Seric 		}
57529430Sbloom 		i = 1;
5769610Seric 	}
5779610Seric 
5789610Seric 	/*
5799610Seric 	**  Determine the port number.
5809610Seric 	*/
5819610Seric 
58210011Seric 	if (port != 0)
58358755Seric 		port = htons(port);
58410011Seric 	else
5859610Seric 	{
5869610Seric 		register struct servent *sp = getservbyname("smtp", "tcp");
5879610Seric 
5889610Seric 		if (sp == NULL)
5899610Seric 		{
59058909Seric 			syserr("554 makeconnection: service \"smtp\" unknown");
59157977Seric 			return (EX_OSERR);
5929610Seric 		}
59358755Seric 		port = sp->s_port;
5949610Seric 	}
5956039Seric 
59658778Seric 	switch (addr.sa.sa_family)
59758755Seric 	{
59859884Seric #ifdef NETINET
59958755Seric 	  case AF_INET:
60058778Seric 		addr.sin.sin_port = port;
60158755Seric 		addrlen = sizeof (struct sockaddr_in);
60258755Seric 		break;
60359884Seric #endif
60458755Seric 
60558755Seric #ifdef NETISO
60658755Seric 	  case AF_ISO:
60758755Seric 		/* assume two byte transport selector */
60858755Seric 		bcopy((char *) &port, TSEL((struct sockaddr_iso *) &addr), 2);
60958755Seric 		addrlen = sizeof (struct sockaddr_iso);
61058755Seric 		break;
61158755Seric #endif
61258755Seric 
61358755Seric 	  default:
61458778Seric 		syserr("Can't connect to address family %d", addr.sa.sa_family);
61558755Seric 		return (EX_NOHOST);
61658755Seric 	}
61758755Seric 
6186039Seric 	/*
6196039Seric 	**  Try to actually open the connection.
6206039Seric 	*/
6216039Seric 
62259156Seric #ifdef XLA
62359156Seric 	/* if too many connections, don't bother trying */
62459156Seric 	if (!xla_noqueue_ok(host))
62559156Seric 		return EX_TEMPFAIL;
62659156Seric #endif
62759156Seric 
62857736Seric 	for (;;)
62952106Seric 	{
63057736Seric 		if (tTd(16, 1))
63158755Seric 			printf("makeconnection (%s [%s])\n",
63258755Seric 				host, anynet_ntoa(&addr));
63352106Seric 
63458588Seric 		/* save for logging */
63558588Seric 		CurHostAddr = addr;
63658588Seric 
63757736Seric 		if (usesecureport)
63857736Seric 		{
63957736Seric 			int rport = IPPORT_RESERVED - 1;
6406039Seric 
64157736Seric 			s = rresvport(&rport);
64257736Seric 		}
64357736Seric 		else
64457736Seric 		{
64557736Seric 			s = socket(AF_INET, SOCK_STREAM, 0);
64657736Seric 		}
64757736Seric 		if (s < 0)
64857736Seric 		{
64957736Seric 			sav_errno = errno;
65057736Seric 			syserr("makeconnection: no socket");
65157736Seric 			goto failure;
65257736Seric 		}
65310347Seric 
65457736Seric 		if (tTd(16, 1))
65557736Seric 			printf("makeconnection: fd=%d\n", s);
65657736Seric 
65757736Seric 		/* turn on network debugging? */
65857736Seric 		if (tTd(16, 101))
65957736Seric 		{
66057736Seric 			int on = 1;
66157736Seric 			(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG,
66257736Seric 					  (char *)&on, sizeof on);
66357736Seric 		}
66457736Seric 		if (CurEnv->e_xfp != NULL)
66557736Seric 			(void) fflush(CurEnv->e_xfp);		/* for debugging */
66657736Seric 		errno = 0;					/* for debugging */
66758755Seric 		if (connect(s, (struct sockaddr *) &addr, addrlen) >= 0)
66857736Seric 			break;
66957736Seric 
67057736Seric 		/* couldn't connect.... figure out why */
67127744Sbloom 		sav_errno = errno;
67227744Sbloom 		(void) close(s);
67329430Sbloom 		if (hp && hp->h_addr_list[i])
67429430Sbloom 		{
67557736Seric 			if (tTd(16, 1))
67658755Seric 				printf("Connect failed (%s); trying new address....\n",
67758755Seric 					errstring(sav_errno));
67858778Seric 			switch (addr.sa.sa_family)
67958778Seric 			{
68058778Seric #ifdef NETINET
68158778Seric 			  case AF_INET:
68258755Seric 				bcopy(hp->h_addr_list[i++],
68358778Seric 				      &addr.sin.sin_addr,
68458755Seric 				      hp->h_length);
68558778Seric 				break;
68658778Seric #endif
68758778Seric 
68858778Seric 			  default:
68958755Seric 				bcopy(hp->h_addr_list[i++],
69058778Seric 					addr.sa.sa_data,
69152106Seric 					hp->h_length);
69258778Seric 				break;
69358778Seric 			}
69457736Seric 			continue;
69529430Sbloom 		}
69629430Sbloom 
6976039Seric 		/* failure, decide if temporary or not */
6986039Seric 	failure:
69959254Seric #ifdef XLA
70059254Seric 		xla_host_end(host);
70159254Seric #endif
70258542Seric 		if (transienterror(sav_errno))
70358542Seric 			return EX_TEMPFAIL;
70458542Seric 		else
70558542Seric 		{
70658542Seric 			message("%s", errstring(sav_errno));
70758542Seric 			return (EX_UNAVAILABLE);
7086039Seric 		}
7096039Seric 	}
7106039Seric 
7116039Seric 	/* connection ok, put it into canonical form */
71253739Seric 	mci->mci_out = fdopen(s, "w");
71353739Seric 	mci->mci_in = fdopen(dup(s), "r");
7146039Seric 
71510098Seric 	return (EX_OK);
7166039Seric }
71710758Seric /*
71810758Seric **  MYHOSTNAME -- return the name of this host.
71910758Seric **
72010758Seric **	Parameters:
72110758Seric **		hostbuf -- a place to return the name of this host.
72212313Seric **		size -- the size of hostbuf.
72310758Seric **
72410758Seric **	Returns:
72510758Seric **		A list of aliases for this host.
72610758Seric **
72710758Seric **	Side Effects:
72858110Seric **		Sets the MyIpAddrs buffer to a list of my IP addresses.
72910758Seric */
7306039Seric 
73158110Seric struct in_addr	MyIpAddrs[MAXIPADDR + 1];
73258110Seric 
73310758Seric char **
73412313Seric myhostname(hostbuf, size)
73510758Seric 	char hostbuf[];
73612313Seric 	int size;
73710758Seric {
73858110Seric 	register struct hostent *hp;
73910758Seric 	extern struct hostent *gethostbyname();
74010758Seric 
74123120Seric 	if (gethostname(hostbuf, size) < 0)
74223120Seric 	{
74323120Seric 		(void) strcpy(hostbuf, "localhost");
74423120Seric 	}
74511147Seric 	hp = gethostbyname(hostbuf);
74611147Seric 	if (hp != NULL)
74716877Seric 	{
74858110Seric 		(void) strncpy(hostbuf, hp->h_name, size - 1);
74958110Seric 		hostbuf[size - 1] = '\0';
75058110Seric 
75158110Seric 		if (hp->h_addrtype == AF_INET && hp->h_length == 4)
75258110Seric 		{
75358110Seric 			register int i;
75458110Seric 
75558110Seric 			for (i = 0; i < MAXIPADDR; i++)
75658110Seric 			{
75758110Seric 				if (hp->h_addr_list[i] == NULL)
75858110Seric 					break;
75958110Seric 				MyIpAddrs[i].s_addr = *(u_long *) hp->h_addr_list[i];
76058110Seric 			}
76158110Seric 			MyIpAddrs[i].s_addr = 0;
76258110Seric 		}
76358110Seric 
76411147Seric 		return (hp->h_aliases);
76516877Seric 	}
76610758Seric 	else
76710758Seric 		return (NULL);
76810758Seric }
76951315Seric /*
77058951Seric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
77158308Seric **
77258951Seric **	Uses RFC1413 protocol to try to get info from the other end.
77358951Seric **
77458308Seric **	Parameters:
77558308Seric **		fd -- the descriptor
77658308Seric **
77758308Seric **	Returns:
77858951Seric **		The user@host information associated with this descriptor.
77958308Seric **
78058308Seric **	Side Effects:
78158951Seric **		Sets RealHostName to the name of the host at the other end.
78258308Seric */
78358308Seric 
78458951Seric #ifdef IDENTPROTO
78558951Seric 
78658951Seric static jmp_buf	CtxAuthTimeout;
78758951Seric 
78858951Seric static
78958951Seric authtimeout()
79058951Seric {
79158951Seric 	longjmp(CtxAuthTimeout, 1);
79258951Seric }
79358951Seric 
79458951Seric #endif
79558951Seric 
79658308Seric char *
79758951Seric getauthinfo(fd)
79858308Seric 	int fd;
79958308Seric {
80058951Seric 	SOCKADDR fa;
80158951Seric 	int falen;
80259104Seric 	register char *p;
80358951Seric #ifdef IDENTPROTO
80458951Seric 	SOCKADDR la;
80558951Seric 	int lalen;
80658951Seric 	register struct servent *sp;
80758951Seric 	int s;
80858951Seric 	int i;
80958951Seric 	EVENT *ev;
81058951Seric #endif
81158951Seric 	static char hbuf[MAXNAME * 2 + 2];
81258951Seric 	extern char *hostnamebyanyaddr();
81358951Seric 	extern char RealUserName[];			/* main.c */
81458308Seric 
81558951Seric 	falen = sizeof fa;
81658951Seric 	if (getpeername(fd, &fa.sa, &falen) < 0 || falen <= 0)
81758951Seric 	{
81858951Seric 		RealHostName = "localhost";
81958951Seric 		(void) sprintf(hbuf, "%s@localhost", RealUserName);
82058957Seric 		if (tTd(9, 1))
82158951Seric 			printf("getauthinfo: %s\n", hbuf);
82258951Seric 		return hbuf;
82358951Seric 	}
82458951Seric 
82564086Seric 	p = hostnamebyanyaddr(&fa);
82664086Seric 	RealHostName = newstr(p);
82758951Seric 	RealHostAddr = fa;
82858951Seric 
82958951Seric #ifdef IDENTPROTO
83058951Seric 	lalen = sizeof la;
83158951Seric 	if (fa.sa.sa_family != AF_INET ||
83258951Seric 	    getsockname(fd, &la.sa, &lalen) < 0 || lalen <= 0 ||
83358951Seric 	    la.sa.sa_family != AF_INET)
83458951Seric 	{
83558951Seric 		/* no ident info */
83658951Seric 		goto noident;
83758951Seric 	}
83858951Seric 
83958951Seric 	/* create ident query */
84060489Seric 	(void) sprintf(hbuf, "%d,%d\r\n",
84160489Seric 		ntohs(fa.sin.sin_port), ntohs(la.sin.sin_port));
84258951Seric 
84358951Seric 	/* create local address */
84458951Seric 	bzero(&la, sizeof la);
84558951Seric 
84658951Seric 	/* create foreign address */
84758951Seric 	sp = getservbyname("auth", "tcp");
84858951Seric 	if (sp != NULL)
84958951Seric 		fa.sin.sin_port = sp->s_port;
85058308Seric 	else
85159097Seric 		fa.sin.sin_port = htons(113);
85258951Seric 
85358951Seric 	s = -1;
85458951Seric 	if (setjmp(CtxAuthTimeout) != 0)
85558951Seric 	{
85658951Seric 		if (s >= 0)
85758951Seric 			(void) close(s);
85858951Seric 		goto noident;
85958951Seric 	}
86058951Seric 
86158951Seric 	/* put a timeout around the whole thing */
86264255Seric 	ev = setevent(TimeOuts.to_ident, authtimeout, 0);
86358951Seric 
86458951Seric 	/* connect to foreign IDENT server */
86558951Seric 	s = socket(AF_INET, SOCK_STREAM, 0);
86658951Seric 	if (s < 0)
86758951Seric 	{
86858951Seric 		clrevent(ev);
86958951Seric 		goto noident;
87058951Seric 	}
87158951Seric 	if (connect(s, &fa.sa, sizeof fa.sin) < 0)
87258951Seric 	{
87358951Seric closeident:
87458951Seric 		(void) close(s);
87558951Seric 		clrevent(ev);
87658951Seric 		goto noident;
87758951Seric 	}
87858951Seric 
87958957Seric 	if (tTd(9, 10))
88058951Seric 		printf("getauthinfo: sent %s", hbuf);
88158951Seric 
88258951Seric 	/* send query */
88358951Seric 	if (write(s, hbuf, strlen(hbuf)) < 0)
88458951Seric 		goto closeident;
88558951Seric 
88658951Seric 	/* get result */
88758951Seric 	i = read(s, hbuf, sizeof hbuf);
88858951Seric 	(void) close(s);
88958951Seric 	clrevent(ev);
89058951Seric 	if (i <= 0)
89158951Seric 		goto noident;
89258951Seric 	if (hbuf[--i] == '\n' && hbuf[--i] == '\r')
89358951Seric 		i--;
89458951Seric 	hbuf[++i] = '\0';
89558951Seric 
89658957Seric 	if (tTd(9, 3))
89758951Seric 		printf("getauthinfo:  got %s\n", hbuf);
89858951Seric 
89958951Seric 	/* parse result */
90058951Seric 	p = strchr(hbuf, ':');
90158951Seric 	if (p == NULL)
90258951Seric 	{
90358951Seric 		/* malformed response */
90458951Seric 		goto noident;
90558951Seric 	}
90658951Seric 	while (isascii(*++p) && isspace(*p))
90758951Seric 		continue;
90858951Seric 	if (strncasecmp(p, "userid", 6) != 0)
90958951Seric 	{
91058951Seric 		/* presumably an error string */
91158951Seric 		goto noident;
91258951Seric 	}
91358951Seric 	p += 6;
91458951Seric 	while (isascii(*p) && isspace(*p))
91558951Seric 		p++;
91658951Seric 	if (*p++ != ':')
91758951Seric 	{
91858951Seric 		/* either useridxx or malformed response */
91958951Seric 		goto noident;
92058951Seric 	}
92158951Seric 
92258951Seric 	/* p now points to the OSTYPE field */
92358951Seric 	p = strchr(p, ':');
92458951Seric 	if (p == NULL)
92558951Seric 	{
92658951Seric 		/* malformed response */
92758951Seric 		goto noident;
92858951Seric 	}
92958951Seric 
93058957Seric 	/* 1413 says don't do this -- but it's broken otherwise */
93158957Seric 	while (isascii(*++p) && isspace(*p))
93258957Seric 		continue;
93358957Seric 
93458951Seric 	/* p now points to the authenticated name */
93558951Seric 	(void) sprintf(hbuf, "%s@%s", p, RealHostName);
93658957Seric 	goto finish;
93758957Seric 
93858957Seric #endif /* IDENTPROTO */
93958957Seric 
94058957Seric noident:
94158957Seric 	(void) strcpy(hbuf, RealHostName);
94258957Seric 
94358957Seric finish:
94458951Seric 	if (RealHostName[0] != '[')
94558951Seric 	{
94658951Seric 		p = &hbuf[strlen(hbuf)];
94758951Seric 		(void) sprintf(p, " [%s]", anynet_ntoa(&RealHostAddr));
94858951Seric 	}
94958957Seric 	if (tTd(9, 1))
95058951Seric 		printf("getauthinfo: %s\n", hbuf);
95158308Seric 	return hbuf;
95258308Seric }
95358308Seric /*
95460089Seric **  HOST_MAP_LOOKUP -- turn a hostname into canonical form
95553751Seric **
95653751Seric **	Parameters:
95756823Seric **		map -- a pointer to this map (unused).
95860089Seric **		name -- the (presumably unqualified) hostname.
95960257Seric **		av -- unused -- for compatibility with other mapping
96055019Seric **			functions.
96159084Seric **		statp -- an exit status (out parameter) -- set to
96259084Seric **			EX_TEMPFAIL if the name server is unavailable.
96353751Seric **
96453751Seric **	Returns:
96553751Seric **		The mapping, if found.
96653751Seric **		NULL if no mapping found.
96753751Seric **
96853751Seric **	Side Effects:
96953751Seric **		Looks up the host specified in hbuf.  If it is not
97053751Seric **		the canonical name for that host, return the canonical
97153751Seric **		name.
97253751Seric */
97351315Seric 
97453751Seric char *
97560257Seric host_map_lookup(map, name, av, statp)
97656823Seric 	MAP *map;
97760089Seric 	char *name;
97860257Seric 	char **av;
97959084Seric 	int *statp;
98016911Seric {
98116911Seric 	register struct hostent *hp;
98233932Sbostic 	u_long in_addr;
98356823Seric 	char *cp;
98458110Seric 	int i;
98559671Seric 	register STAB *s;
98660257Seric 	char hbuf[MAXNAME];
98759671Seric 	extern struct hostent *gethostbyaddr();
98859671Seric 	extern int h_errno;
98916911Seric 
99025574Smiriam 	/*
99159671Seric 	**  See if we have already looked up this name.  If so, just
99259671Seric 	**  return it.
99359671Seric 	*/
99453751Seric 
99560089Seric 	s = stab(name, ST_NAMECANON, ST_ENTER);
99659671Seric 	if (bitset(NCF_VALID, s->s_namecanon.nc_flags))
99759671Seric 	{
99859986Seric 		if (tTd(9, 1))
99960089Seric 			printf("host_map_lookup(%s) => CACHE %s\n",
100060089Seric 				name, s->s_namecanon.nc_cname);
100159671Seric 		errno = s->s_namecanon.nc_errno;
100259671Seric 		h_errno = s->s_namecanon.nc_herrno;
100359671Seric 		*statp = s->s_namecanon.nc_stat;
100459671Seric 		return s->s_namecanon.nc_cname;
100559671Seric 	}
100659671Seric 
100759671Seric 	/*
100859671Seric 	**  If first character is a bracket, then it is an address
100959671Seric 	**  lookup.  Address is copied into a temporary buffer to
101060089Seric 	**  strip the brackets and to preserve name if address is
101159671Seric 	**  unknown.
101259671Seric 	*/
101359671Seric 
101460089Seric 	if (*name != '[')
101553751Seric 	{
101655019Seric 		extern bool getcanonname();
101755019Seric 
101858798Seric 		if (tTd(9, 1))
101960089Seric 			printf("host_map_lookup(%s) => ", name);
102059671Seric 		s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
102160089Seric 		(void) strcpy(hbuf, name);
102263842Seric 		if (getcanonname(hbuf, sizeof hbuf - 1, TRUE))
102358796Seric 		{
102458796Seric 			if (tTd(9, 1))
102558796Seric 				printf("%s\n", hbuf);
102660257Seric 			cp = map_rewrite(map, hbuf, strlen(hbuf), av);
102760257Seric 			s->s_namecanon.nc_cname = newstr(cp);
102860257Seric 			return cp;
102958796Seric 		}
103053751Seric 		else
103158796Seric 		{
103259084Seric 			register struct hostent *hp;
103359084Seric 
103458796Seric 			if (tTd(9, 1))
103559084Seric 				printf("FAIL (%d)\n", h_errno);
103659671Seric 			s->s_namecanon.nc_errno = errno;
103759671Seric 			s->s_namecanon.nc_herrno = h_errno;
103859084Seric 			switch (h_errno)
103959084Seric 			{
104059084Seric 			  case TRY_AGAIN:
104159596Seric 				if (UseNameServer)
104259734Seric 				{
104359734Seric 					char *msg = "Recipient domain nameserver timed out";
104459734Seric 
104559734Seric 					message(msg);
104659734Seric 					if (CurEnv->e_message == NULL)
104760009Seric 						CurEnv->e_message = newstr(msg);
104859734Seric 				}
104959084Seric 				*statp = EX_TEMPFAIL;
105059084Seric 				break;
105159084Seric 
105259084Seric 			  case HOST_NOT_FOUND:
105359084Seric 				*statp = EX_NOHOST;
105459084Seric 				break;
105559084Seric 
105659084Seric 			  case NO_RECOVERY:
105759084Seric 				*statp = EX_SOFTWARE;
105859084Seric 				break;
105959084Seric 
106059084Seric 			  default:
106159084Seric 				*statp = EX_UNAVAILABLE;
106259084Seric 				break;
106359084Seric 			}
106459671Seric 			s->s_namecanon.nc_stat = *statp;
106559084Seric 			if (*statp != EX_TEMPFAIL || UseNameServer)
106659084Seric 				return NULL;
106759084Seric 
106859084Seric 			/*
106959084Seric 			**  Try to look it up in /etc/hosts
107059084Seric 			*/
107159084Seric 
107260089Seric 			hp = gethostbyname(name);
107359084Seric 			if (hp == NULL)
107459084Seric 			{
107559084Seric 				/* no dice there either */
107659671Seric 				s->s_namecanon.nc_stat = *statp = EX_NOHOST;
107759084Seric 				return NULL;
107859084Seric 			}
107959084Seric 
108059671Seric 			s->s_namecanon.nc_stat = *statp = EX_OK;
108160257Seric 			cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
108260257Seric 			s->s_namecanon.nc_cname = newstr(cp);
108360257Seric 			return cp;
108458796Seric 		}
108553751Seric 	}
108660089Seric 	if ((cp = strchr(name, ']')) == NULL)
108753751Seric 		return (NULL);
108840994Sbostic 	*cp = '\0';
108960089Seric 	in_addr = inet_addr(&name[1]);
109058110Seric 
109158110Seric 	/* check to see if this is one of our addresses */
109258110Seric 	for (i = 0; MyIpAddrs[i].s_addr != 0; i++)
109358110Seric 	{
109458110Seric 		if (MyIpAddrs[i].s_addr == in_addr)
109558110Seric 		{
109660257Seric 			return map_rewrite(map, MyHostName, strlen(MyHostName), av);
109758110Seric 		}
109858110Seric 	}
109958110Seric 
110058110Seric 	/* nope -- ask the name server */
110133932Sbostic 	hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET);
110259671Seric 	s->s_namecanon.nc_errno = errno;
110359671Seric 	s->s_namecanon.nc_herrno = h_errno;
110459671Seric 	s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
110533932Sbostic 	if (hp == NULL)
110659671Seric 	{
110759671Seric 		s->s_namecanon.nc_stat = *statp = EX_NOHOST;
110853751Seric 		return (NULL);
110959671Seric 	}
111053751Seric 
111158110Seric 	/* found a match -- copy out */
111260257Seric 	cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
111359671Seric 	s->s_namecanon.nc_stat = *statp = EX_OK;
111460257Seric 	s->s_namecanon.nc_cname = newstr(cp);
111560257Seric 	return cp;
111633932Sbostic }
111758755Seric /*
111858755Seric **  ANYNET_NTOA -- convert a network address to printable form.
111958755Seric **
112058755Seric **	Parameters:
112158755Seric **		sap -- a pointer to a sockaddr structure.
112258755Seric **
112358755Seric **	Returns:
112458755Seric **		A printable version of that sockaddr.
112558755Seric */
112616911Seric 
112758755Seric char *
112858755Seric anynet_ntoa(sap)
112958755Seric 	register SOCKADDR *sap;
113058755Seric {
113158755Seric 	register char *bp;
113258755Seric 	register char *ap;
113358755Seric 	int l;
113458755Seric 	static char buf[80];
113558755Seric 
113658798Seric 	/* check for null/zero family */
113758798Seric 	if (sap == NULL)
113858798Seric 		return "NULLADDR";
113958798Seric 	if (sap->sa.sa_family == 0)
114058798Seric 		return "0";
114158798Seric 
114258778Seric #ifdef NETINET
114358778Seric 	if (sap->sa.sa_family == AF_INET)
114458755Seric 	{
114558755Seric 		extern char *inet_ntoa();
114658755Seric 
114758755Seric 		return inet_ntoa(((struct sockaddr_in *) sap)->sin_addr);
114858755Seric 	}
114958778Seric #endif
115058755Seric 
115158755Seric 	/* unknown family -- just dump bytes */
115258778Seric 	(void) sprintf(buf, "Family %d: ", sap->sa.sa_family);
115358755Seric 	bp = &buf[strlen(buf)];
115458778Seric 	ap = sap->sa.sa_data;
115558778Seric 	for (l = sizeof sap->sa.sa_data; --l >= 0; )
115658755Seric 	{
115758755Seric 		(void) sprintf(bp, "%02x:", *ap++ & 0377);
115858755Seric 		bp += 3;
115958755Seric 	}
116058755Seric 	*--bp = '\0';
116158755Seric 	return buf;
116258755Seric }
116358951Seric /*
116458951Seric **  HOSTNAMEBYANYADDR -- return name of host based on address
116558951Seric **
116658951Seric **	Parameters:
116758951Seric **		sap -- SOCKADDR pointer
116858951Seric **
116958951Seric **	Returns:
117058951Seric **		text representation of host name.
117158951Seric **
117258951Seric **	Side Effects:
117358951Seric **		none.
117458951Seric */
117558755Seric 
117658951Seric char *
117758951Seric hostnamebyanyaddr(sap)
117858951Seric 	register SOCKADDR *sap;
117958951Seric {
118058951Seric 	register struct hostent *hp;
118158951Seric 
118259042Seric #ifdef NAMED_BIND
118359042Seric 	int saveretry;
118459042Seric 
118559042Seric 	/* shorten name server timeout to avoid higher level timeouts */
118659042Seric 	saveretry = _res.retry;
118759042Seric 	_res.retry = 3;
118859042Seric #endif /* NAMED_BIND */
118959042Seric 
119058951Seric 	switch (sap->sa.sa_family)
119158951Seric 	{
119258951Seric #ifdef NETINET
119358951Seric 	  case AF_INET:
119458951Seric 		hp = gethostbyaddr((char *) &sap->sin.sin_addr,
119558951Seric 			sizeof sap->sin.sin_addr,
119658951Seric 			AF_INET);
119758951Seric 		break;
119858951Seric #endif
119958951Seric 
120058951Seric #ifdef NETISO
120158951Seric 	  case AF_ISO:
120258951Seric 		hp = gethostbyaddr((char *) &sap->siso.siso_addr,
120358951Seric 			sizeof sap->siso.siso_addr,
120458951Seric 			AF_ISO);
120558951Seric 		break;
120658951Seric #endif
120758951Seric 
120858951Seric 	  default:
120958951Seric 		hp = gethostbyaddr(sap->sa.sa_data,
121058951Seric 			   sizeof sap->sa.sa_data,
121158951Seric 			   sap->sa.sa_family);
121258951Seric 		break;
121358951Seric 	}
121458951Seric 
121559042Seric #ifdef NAMED_BIND
121659042Seric 	_res.retry = saveretry;
121759042Seric #endif /* NAMED_BIND */
121859042Seric 
121958951Seric 	if (hp != NULL)
122058951Seric 		return hp->h_name;
122158951Seric 	else
122258951Seric 	{
122358951Seric 		/* produce a dotted quad */
122458951Seric 		static char buf[512];
122558951Seric 
122658951Seric 		(void) sprintf(buf, "[%s]", anynet_ntoa(sap));
122758951Seric 		return buf;
122858951Seric 	}
122958951Seric }
123058951Seric 
123156795Seric # else /* DAEMON */
123216911Seric /* code for systems without sophisticated networking */
123310758Seric 
123410758Seric /*
123510758Seric **  MYHOSTNAME -- stub version for case of no daemon code.
123611297Seric **
123711297Seric **	Can't convert to upper case here because might be a UUCP name.
123812313Seric **
123912313Seric **	Mark, you can change this to be anything you want......
124010758Seric */
124110758Seric 
124210758Seric char **
124312313Seric myhostname(hostbuf, size)
124410758Seric 	char hostbuf[];
124512313Seric 	int size;
124610758Seric {
124710758Seric 	register FILE *f;
124810758Seric 
124910758Seric 	hostbuf[0] = '\0';
125010758Seric 	f = fopen("/usr/include/whoami", "r");
125110758Seric 	if (f != NULL)
125210758Seric 	{
125312313Seric 		(void) fgets(hostbuf, size, f);
125410758Seric 		fixcrlf(hostbuf, TRUE);
125510758Seric 		(void) fclose(f);
125610758Seric 	}
125710758Seric 	return (NULL);
125810758Seric }
125916911Seric /*
126058951Seric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
126158308Seric **
126258308Seric **	Parameters:
126358308Seric **		fd -- the descriptor
126458308Seric **
126558308Seric **	Returns:
126658308Seric **		The host name associated with this descriptor, if it can
126758308Seric **			be determined.
126858308Seric **		NULL otherwise.
126958308Seric **
127058308Seric **	Side Effects:
127158308Seric **		none
127258308Seric */
127358308Seric 
127458308Seric char *
127558951Seric getauthinfo(fd)
127658308Seric 	int fd;
127758308Seric {
127858308Seric 	return NULL;
127958308Seric }
128058308Seric /*
128116911Seric **  MAPHOSTNAME -- turn a hostname into canonical form
128216911Seric **
128316911Seric **	Parameters:
128456823Seric **		map -- a pointer to the database map.
128560089Seric **		name -- a buffer containing a hostname.
128653751Seric **		avp -- a pointer to a (cf file defined) argument vector.
128759084Seric **		statp -- an exit status (out parameter).
128816911Seric **
128916911Seric **	Returns:
129053751Seric **		mapped host name
129151315Seric **		FALSE otherwise.
129216911Seric **
129316911Seric **	Side Effects:
129460089Seric **		Looks up the host specified in name.  If it is not
129516911Seric **		the canonical name for that host, replace it with
129616911Seric **		the canonical name.  If the name is unknown, or it
129716911Seric **		is already the canonical name, leave it unchanged.
129816911Seric */
129910758Seric 
130016911Seric /*ARGSUSED*/
130153751Seric char *
130260089Seric host_map_lookup(map, name, avp, statp)
130356823Seric 	MAP *map;
130460089Seric 	char *name;
130553751Seric 	char **avp;
130659084Seric 	char *statp;
130716911Seric {
130859084Seric 	register struct hostent *hp;
130959084Seric 
131060089Seric 	hp = gethostbyname(name);
131159084Seric 	if (hp != NULL)
131259084Seric 		return hp->h_name;
131359084Seric 	*statp = EX_NOHOST;
131453751Seric 	return NULL;
131516911Seric }
131616911Seric 
131756795Seric #endif /* DAEMON */
1318