xref: /csrg-svn/usr.sbin/sendmail/src/daemon.c (revision 64086)
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*64086Seric static char sccsid[] = "@(#)daemon.c	8.6 (Berkeley) 07/29/93 (with daemon mode)";
1533780Sbostic #else
16*64086Seric static char sccsid[] = "@(#)daemon.c	8.6 (Berkeley) 07/29/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 		{
254*64086Seric 			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 */
267*64086Seric 			p = hostnamebyanyaddr(&RealHostAddr);
268*64086Seric 			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);
50958906Seric 	CurHostName = host;
51025475Smiriam 
5119308Seric 	if (host[0] == '[')
5129308Seric 	{
51311147Seric 		long hid;
51456795Seric 		register char *p = strchr(host, ']');
5159308Seric 
51611147Seric 		if (p != NULL)
5179308Seric 		{
51811147Seric 			*p = '\0';
51959884Seric #ifdef NETINET
52011147Seric 			hid = inet_addr(&host[1]);
52158360Seric 			if (hid == -1)
52259884Seric #endif
52358360Seric 			{
52458360Seric 				/* try it as a host name (avoid MX lookup) */
52558360Seric 				hp = gethostbyname(&host[1]);
52658360Seric 				*p = ']';
52758360Seric 				goto gothostent;
52858360Seric 			}
52911147Seric 			*p = ']';
5309308Seric 		}
53158360Seric 		if (p == NULL)
5329308Seric 		{
53358151Seric 			usrerr("553 Invalid numeric domain spec \"%s\"", host);
5349308Seric 			return (EX_NOHOST);
5359308Seric 		}
53659884Seric #ifdef NETINET
53759884Seric 		addr.sin.sin_family = AF_INET;		/*XXX*/
53858778Seric 		addr.sin.sin_addr.s_addr = hid;
53959884Seric #endif
5409308Seric 	}
5419610Seric 	else
5429610Seric 	{
54329430Sbloom 		hp = gethostbyname(host);
54458360Seric gothostent:
54525475Smiriam 		if (hp == NULL)
54624945Seric 		{
54735651Seric #ifdef NAMED_BIND
54825475Smiriam 			if (errno == ETIMEDOUT || h_errno == TRY_AGAIN)
54925475Smiriam 				return (EX_TEMPFAIL);
55025657Seric 
55135651Seric 			/* if name server is specified, assume temp fail */
55235651Seric 			if (errno == ECONNREFUSED && UseNameServer)
55335651Seric 				return (EX_TEMPFAIL);
55435651Seric #endif
55525475Smiriam 			return (EX_NOHOST);
55624945Seric 		}
55758778Seric 		addr.sa.sa_family = hp->h_addrtype;
55858778Seric 		switch (hp->h_addrtype)
55958778Seric 		{
56058778Seric #ifdef NETINET
56158778Seric 		  case AF_INET:
56258755Seric 			bcopy(hp->h_addr,
56358778Seric 				&addr.sin.sin_addr,
56458755Seric 				hp->h_length);
56558778Seric 			break;
56658778Seric #endif
56758778Seric 
56858778Seric 		  default:
56958755Seric 			bcopy(hp->h_addr,
57058778Seric 				addr.sa.sa_data,
57158755Seric 				hp->h_length);
57258778Seric 			break;
57358778Seric 		}
57429430Sbloom 		i = 1;
5759610Seric 	}
5769610Seric 
5779610Seric 	/*
5789610Seric 	**  Determine the port number.
5799610Seric 	*/
5809610Seric 
58110011Seric 	if (port != 0)
58258755Seric 		port = htons(port);
58310011Seric 	else
5849610Seric 	{
5859610Seric 		register struct servent *sp = getservbyname("smtp", "tcp");
5869610Seric 
5879610Seric 		if (sp == NULL)
5889610Seric 		{
58958909Seric 			syserr("554 makeconnection: service \"smtp\" unknown");
59057977Seric 			return (EX_OSERR);
5919610Seric 		}
59258755Seric 		port = sp->s_port;
5939610Seric 	}
5946039Seric 
59558778Seric 	switch (addr.sa.sa_family)
59658755Seric 	{
59759884Seric #ifdef NETINET
59858755Seric 	  case AF_INET:
59958778Seric 		addr.sin.sin_port = port;
60058755Seric 		addrlen = sizeof (struct sockaddr_in);
60158755Seric 		break;
60259884Seric #endif
60358755Seric 
60458755Seric #ifdef NETISO
60558755Seric 	  case AF_ISO:
60658755Seric 		/* assume two byte transport selector */
60758755Seric 		bcopy((char *) &port, TSEL((struct sockaddr_iso *) &addr), 2);
60858755Seric 		addrlen = sizeof (struct sockaddr_iso);
60958755Seric 		break;
61058755Seric #endif
61158755Seric 
61258755Seric 	  default:
61358778Seric 		syserr("Can't connect to address family %d", addr.sa.sa_family);
61458755Seric 		return (EX_NOHOST);
61558755Seric 	}
61658755Seric 
6176039Seric 	/*
6186039Seric 	**  Try to actually open the connection.
6196039Seric 	*/
6206039Seric 
62159156Seric #ifdef XLA
62259156Seric 	/* if too many connections, don't bother trying */
62359156Seric 	if (!xla_noqueue_ok(host))
62459156Seric 		return EX_TEMPFAIL;
62559156Seric #endif
62659156Seric 
62757736Seric 	for (;;)
62852106Seric 	{
62957736Seric 		if (tTd(16, 1))
63058755Seric 			printf("makeconnection (%s [%s])\n",
63158755Seric 				host, anynet_ntoa(&addr));
63252106Seric 
63358588Seric 		/* save for logging */
63458588Seric 		CurHostAddr = addr;
63558588Seric 
63657736Seric 		if (usesecureport)
63757736Seric 		{
63857736Seric 			int rport = IPPORT_RESERVED - 1;
6396039Seric 
64057736Seric 			s = rresvport(&rport);
64157736Seric 		}
64257736Seric 		else
64357736Seric 		{
64457736Seric 			s = socket(AF_INET, SOCK_STREAM, 0);
64557736Seric 		}
64657736Seric 		if (s < 0)
64757736Seric 		{
64857736Seric 			sav_errno = errno;
64957736Seric 			syserr("makeconnection: no socket");
65057736Seric 			goto failure;
65157736Seric 		}
65210347Seric 
65357736Seric 		if (tTd(16, 1))
65457736Seric 			printf("makeconnection: fd=%d\n", s);
65557736Seric 
65657736Seric 		/* turn on network debugging? */
65757736Seric 		if (tTd(16, 101))
65857736Seric 		{
65957736Seric 			int on = 1;
66057736Seric 			(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG,
66157736Seric 					  (char *)&on, sizeof on);
66257736Seric 		}
66357736Seric 		if (CurEnv->e_xfp != NULL)
66457736Seric 			(void) fflush(CurEnv->e_xfp);		/* for debugging */
66557736Seric 		errno = 0;					/* for debugging */
66658755Seric 		if (connect(s, (struct sockaddr *) &addr, addrlen) >= 0)
66757736Seric 			break;
66857736Seric 
66957736Seric 		/* couldn't connect.... figure out why */
67027744Sbloom 		sav_errno = errno;
67127744Sbloom 		(void) close(s);
67229430Sbloom 		if (hp && hp->h_addr_list[i])
67329430Sbloom 		{
67457736Seric 			if (tTd(16, 1))
67558755Seric 				printf("Connect failed (%s); trying new address....\n",
67658755Seric 					errstring(sav_errno));
67758778Seric 			switch (addr.sa.sa_family)
67858778Seric 			{
67958778Seric #ifdef NETINET
68058778Seric 			  case AF_INET:
68158755Seric 				bcopy(hp->h_addr_list[i++],
68258778Seric 				      &addr.sin.sin_addr,
68358755Seric 				      hp->h_length);
68458778Seric 				break;
68558778Seric #endif
68658778Seric 
68758778Seric 			  default:
68858755Seric 				bcopy(hp->h_addr_list[i++],
68958778Seric 					addr.sa.sa_data,
69052106Seric 					hp->h_length);
69158778Seric 				break;
69258778Seric 			}
69357736Seric 			continue;
69429430Sbloom 		}
69529430Sbloom 
6966039Seric 		/* failure, decide if temporary or not */
6976039Seric 	failure:
69859254Seric #ifdef XLA
69959254Seric 		xla_host_end(host);
70059254Seric #endif
70158542Seric 		if (transienterror(sav_errno))
70258542Seric 			return EX_TEMPFAIL;
70358542Seric 		else
70458542Seric 		{
70558542Seric 			message("%s", errstring(sav_errno));
70658542Seric 			return (EX_UNAVAILABLE);
7076039Seric 		}
7086039Seric 	}
7096039Seric 
7106039Seric 	/* connection ok, put it into canonical form */
71153739Seric 	mci->mci_out = fdopen(s, "w");
71253739Seric 	mci->mci_in = fdopen(dup(s), "r");
7136039Seric 
71410098Seric 	return (EX_OK);
7156039Seric }
71610758Seric /*
71710758Seric **  MYHOSTNAME -- return the name of this host.
71810758Seric **
71910758Seric **	Parameters:
72010758Seric **		hostbuf -- a place to return the name of this host.
72112313Seric **		size -- the size of hostbuf.
72210758Seric **
72310758Seric **	Returns:
72410758Seric **		A list of aliases for this host.
72510758Seric **
72610758Seric **	Side Effects:
72758110Seric **		Sets the MyIpAddrs buffer to a list of my IP addresses.
72810758Seric */
7296039Seric 
73058110Seric struct in_addr	MyIpAddrs[MAXIPADDR + 1];
73158110Seric 
73210758Seric char **
73312313Seric myhostname(hostbuf, size)
73410758Seric 	char hostbuf[];
73512313Seric 	int size;
73610758Seric {
73758110Seric 	register struct hostent *hp;
73810758Seric 	extern struct hostent *gethostbyname();
73910758Seric 
74023120Seric 	if (gethostname(hostbuf, size) < 0)
74123120Seric 	{
74223120Seric 		(void) strcpy(hostbuf, "localhost");
74323120Seric 	}
74411147Seric 	hp = gethostbyname(hostbuf);
74511147Seric 	if (hp != NULL)
74616877Seric 	{
74758110Seric 		(void) strncpy(hostbuf, hp->h_name, size - 1);
74858110Seric 		hostbuf[size - 1] = '\0';
74958110Seric 
75058110Seric 		if (hp->h_addrtype == AF_INET && hp->h_length == 4)
75158110Seric 		{
75258110Seric 			register int i;
75358110Seric 
75458110Seric 			for (i = 0; i < MAXIPADDR; i++)
75558110Seric 			{
75658110Seric 				if (hp->h_addr_list[i] == NULL)
75758110Seric 					break;
75858110Seric 				MyIpAddrs[i].s_addr = *(u_long *) hp->h_addr_list[i];
75958110Seric 			}
76058110Seric 			MyIpAddrs[i].s_addr = 0;
76158110Seric 		}
76258110Seric 
76311147Seric 		return (hp->h_aliases);
76416877Seric 	}
76510758Seric 	else
76610758Seric 		return (NULL);
76710758Seric }
76851315Seric /*
76958951Seric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
77058308Seric **
77158951Seric **	Uses RFC1413 protocol to try to get info from the other end.
77258951Seric **
77358308Seric **	Parameters:
77458308Seric **		fd -- the descriptor
77558308Seric **
77658308Seric **	Returns:
77758951Seric **		The user@host information associated with this descriptor.
77858308Seric **
77958308Seric **	Side Effects:
78058951Seric **		Sets RealHostName to the name of the host at the other end.
78158308Seric */
78258308Seric 
78358951Seric #ifdef IDENTPROTO
78458951Seric 
78558951Seric static jmp_buf	CtxAuthTimeout;
78658951Seric 
78758951Seric static
78858951Seric authtimeout()
78958951Seric {
79058951Seric 	longjmp(CtxAuthTimeout, 1);
79158951Seric }
79258951Seric 
79358951Seric #endif
79458951Seric 
79558308Seric char *
79658951Seric getauthinfo(fd)
79758308Seric 	int fd;
79858308Seric {
79958951Seric 	SOCKADDR fa;
80058951Seric 	int falen;
80159104Seric 	register char *p;
80258951Seric #ifdef IDENTPROTO
80358951Seric 	SOCKADDR la;
80458951Seric 	int lalen;
80558951Seric 	register struct servent *sp;
80658951Seric 	int s;
80758951Seric 	int i;
80858951Seric 	EVENT *ev;
80958951Seric #endif
81058951Seric 	static char hbuf[MAXNAME * 2 + 2];
81158951Seric 	extern char *hostnamebyanyaddr();
81258951Seric 	extern char RealUserName[];			/* main.c */
81358308Seric 
81458951Seric 	falen = sizeof fa;
81558951Seric 	if (getpeername(fd, &fa.sa, &falen) < 0 || falen <= 0)
81658951Seric 	{
81758951Seric 		RealHostName = "localhost";
81858951Seric 		(void) sprintf(hbuf, "%s@localhost", RealUserName);
81958957Seric 		if (tTd(9, 1))
82058951Seric 			printf("getauthinfo: %s\n", hbuf);
82158951Seric 		return hbuf;
82258951Seric 	}
82358951Seric 
824*64086Seric 	p = hostnamebyanyaddr(&fa);
825*64086Seric 	RealHostName = newstr(p);
82658951Seric 	RealHostAddr = fa;
82758951Seric 
82858951Seric #ifdef IDENTPROTO
82958951Seric 	lalen = sizeof la;
83058951Seric 	if (fa.sa.sa_family != AF_INET ||
83158951Seric 	    getsockname(fd, &la.sa, &lalen) < 0 || lalen <= 0 ||
83258951Seric 	    la.sa.sa_family != AF_INET)
83358951Seric 	{
83458951Seric 		/* no ident info */
83558951Seric 		goto noident;
83658951Seric 	}
83758951Seric 
83858951Seric 	/* create ident query */
83960489Seric 	(void) sprintf(hbuf, "%d,%d\r\n",
84060489Seric 		ntohs(fa.sin.sin_port), ntohs(la.sin.sin_port));
84158951Seric 
84258951Seric 	/* create local address */
84358951Seric 	bzero(&la, sizeof la);
84458951Seric 
84558951Seric 	/* create foreign address */
84658951Seric 	sp = getservbyname("auth", "tcp");
84758951Seric 	if (sp != NULL)
84858951Seric 		fa.sin.sin_port = sp->s_port;
84958308Seric 	else
85059097Seric 		fa.sin.sin_port = htons(113);
85158951Seric 
85258951Seric 	s = -1;
85358951Seric 	if (setjmp(CtxAuthTimeout) != 0)
85458951Seric 	{
85558951Seric 		if (s >= 0)
85658951Seric 			(void) close(s);
85758951Seric 		goto noident;
85858951Seric 	}
85958951Seric 
86058951Seric 	/* put a timeout around the whole thing */
86158951Seric 	ev = setevent((time_t) 30, authtimeout, 0);
86258951Seric 
86358951Seric 	/* connect to foreign IDENT server */
86458951Seric 	s = socket(AF_INET, SOCK_STREAM, 0);
86558951Seric 	if (s < 0)
86658951Seric 	{
86758951Seric 		clrevent(ev);
86858951Seric 		goto noident;
86958951Seric 	}
87058951Seric 	if (connect(s, &fa.sa, sizeof fa.sin) < 0)
87158951Seric 	{
87258951Seric closeident:
87358951Seric 		(void) close(s);
87458951Seric 		clrevent(ev);
87558951Seric 		goto noident;
87658951Seric 	}
87758951Seric 
87858957Seric 	if (tTd(9, 10))
87958951Seric 		printf("getauthinfo: sent %s", hbuf);
88058951Seric 
88158951Seric 	/* send query */
88258951Seric 	if (write(s, hbuf, strlen(hbuf)) < 0)
88358951Seric 		goto closeident;
88458951Seric 
88558951Seric 	/* get result */
88658951Seric 	i = read(s, hbuf, sizeof hbuf);
88758951Seric 	(void) close(s);
88858951Seric 	clrevent(ev);
88958951Seric 	if (i <= 0)
89058951Seric 		goto noident;
89158951Seric 	if (hbuf[--i] == '\n' && hbuf[--i] == '\r')
89258951Seric 		i--;
89358951Seric 	hbuf[++i] = '\0';
89458951Seric 
89558957Seric 	if (tTd(9, 3))
89658951Seric 		printf("getauthinfo:  got %s\n", hbuf);
89758951Seric 
89858951Seric 	/* parse result */
89958951Seric 	p = strchr(hbuf, ':');
90058951Seric 	if (p == NULL)
90158951Seric 	{
90258951Seric 		/* malformed response */
90358951Seric 		goto noident;
90458951Seric 	}
90558951Seric 	while (isascii(*++p) && isspace(*p))
90658951Seric 		continue;
90758951Seric 	if (strncasecmp(p, "userid", 6) != 0)
90858951Seric 	{
90958951Seric 		/* presumably an error string */
91058951Seric 		goto noident;
91158951Seric 	}
91258951Seric 	p += 6;
91358951Seric 	while (isascii(*p) && isspace(*p))
91458951Seric 		p++;
91558951Seric 	if (*p++ != ':')
91658951Seric 	{
91758951Seric 		/* either useridxx or malformed response */
91858951Seric 		goto noident;
91958951Seric 	}
92058951Seric 
92158951Seric 	/* p now points to the OSTYPE field */
92258951Seric 	p = strchr(p, ':');
92358951Seric 	if (p == NULL)
92458951Seric 	{
92558951Seric 		/* malformed response */
92658951Seric 		goto noident;
92758951Seric 	}
92858951Seric 
92958957Seric 	/* 1413 says don't do this -- but it's broken otherwise */
93058957Seric 	while (isascii(*++p) && isspace(*p))
93158957Seric 		continue;
93258957Seric 
93358951Seric 	/* p now points to the authenticated name */
93458951Seric 	(void) sprintf(hbuf, "%s@%s", p, RealHostName);
93558957Seric 	goto finish;
93658957Seric 
93758957Seric #endif /* IDENTPROTO */
93858957Seric 
93958957Seric noident:
94058957Seric 	(void) strcpy(hbuf, RealHostName);
94158957Seric 
94258957Seric finish:
94358951Seric 	if (RealHostName[0] != '[')
94458951Seric 	{
94558951Seric 		p = &hbuf[strlen(hbuf)];
94658951Seric 		(void) sprintf(p, " [%s]", anynet_ntoa(&RealHostAddr));
94758951Seric 	}
94858957Seric 	if (tTd(9, 1))
94958951Seric 		printf("getauthinfo: %s\n", hbuf);
95058308Seric 	return hbuf;
95158308Seric }
95258308Seric /*
95360089Seric **  HOST_MAP_LOOKUP -- turn a hostname into canonical form
95453751Seric **
95553751Seric **	Parameters:
95656823Seric **		map -- a pointer to this map (unused).
95760089Seric **		name -- the (presumably unqualified) hostname.
95860257Seric **		av -- unused -- for compatibility with other mapping
95955019Seric **			functions.
96059084Seric **		statp -- an exit status (out parameter) -- set to
96159084Seric **			EX_TEMPFAIL if the name server is unavailable.
96253751Seric **
96353751Seric **	Returns:
96453751Seric **		The mapping, if found.
96553751Seric **		NULL if no mapping found.
96653751Seric **
96753751Seric **	Side Effects:
96853751Seric **		Looks up the host specified in hbuf.  If it is not
96953751Seric **		the canonical name for that host, return the canonical
97053751Seric **		name.
97153751Seric */
97251315Seric 
97353751Seric char *
97460257Seric host_map_lookup(map, name, av, statp)
97556823Seric 	MAP *map;
97660089Seric 	char *name;
97760257Seric 	char **av;
97859084Seric 	int *statp;
97916911Seric {
98016911Seric 	register struct hostent *hp;
98133932Sbostic 	u_long in_addr;
98256823Seric 	char *cp;
98358110Seric 	int i;
98459671Seric 	register STAB *s;
98560257Seric 	char hbuf[MAXNAME];
98659671Seric 	extern struct hostent *gethostbyaddr();
98759671Seric 	extern int h_errno;
98816911Seric 
98925574Smiriam 	/*
99059671Seric 	**  See if we have already looked up this name.  If so, just
99159671Seric 	**  return it.
99259671Seric 	*/
99353751Seric 
99460089Seric 	s = stab(name, ST_NAMECANON, ST_ENTER);
99559671Seric 	if (bitset(NCF_VALID, s->s_namecanon.nc_flags))
99659671Seric 	{
99759986Seric 		if (tTd(9, 1))
99860089Seric 			printf("host_map_lookup(%s) => CACHE %s\n",
99960089Seric 				name, s->s_namecanon.nc_cname);
100059671Seric 		errno = s->s_namecanon.nc_errno;
100159671Seric 		h_errno = s->s_namecanon.nc_herrno;
100259671Seric 		*statp = s->s_namecanon.nc_stat;
100359671Seric 		return s->s_namecanon.nc_cname;
100459671Seric 	}
100559671Seric 
100659671Seric 	/*
100759671Seric 	**  If first character is a bracket, then it is an address
100859671Seric 	**  lookup.  Address is copied into a temporary buffer to
100960089Seric 	**  strip the brackets and to preserve name if address is
101059671Seric 	**  unknown.
101159671Seric 	*/
101259671Seric 
101360089Seric 	if (*name != '[')
101453751Seric 	{
101555019Seric 		extern bool getcanonname();
101655019Seric 
101758798Seric 		if (tTd(9, 1))
101860089Seric 			printf("host_map_lookup(%s) => ", name);
101959671Seric 		s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
102060089Seric 		(void) strcpy(hbuf, name);
102163842Seric 		if (getcanonname(hbuf, sizeof hbuf - 1, TRUE))
102258796Seric 		{
102358796Seric 			if (tTd(9, 1))
102458796Seric 				printf("%s\n", hbuf);
102560257Seric 			cp = map_rewrite(map, hbuf, strlen(hbuf), av);
102660257Seric 			s->s_namecanon.nc_cname = newstr(cp);
102760257Seric 			return cp;
102858796Seric 		}
102953751Seric 		else
103058796Seric 		{
103159084Seric 			register struct hostent *hp;
103259084Seric 
103358796Seric 			if (tTd(9, 1))
103459084Seric 				printf("FAIL (%d)\n", h_errno);
103559671Seric 			s->s_namecanon.nc_errno = errno;
103659671Seric 			s->s_namecanon.nc_herrno = h_errno;
103759084Seric 			switch (h_errno)
103859084Seric 			{
103959084Seric 			  case TRY_AGAIN:
104059596Seric 				if (UseNameServer)
104159734Seric 				{
104259734Seric 					char *msg = "Recipient domain nameserver timed out";
104359734Seric 
104459734Seric 					message(msg);
104559734Seric 					if (CurEnv->e_message == NULL)
104660009Seric 						CurEnv->e_message = newstr(msg);
104759734Seric 				}
104859084Seric 				*statp = EX_TEMPFAIL;
104959084Seric 				break;
105059084Seric 
105159084Seric 			  case HOST_NOT_FOUND:
105259084Seric 				*statp = EX_NOHOST;
105359084Seric 				break;
105459084Seric 
105559084Seric 			  case NO_RECOVERY:
105659084Seric 				*statp = EX_SOFTWARE;
105759084Seric 				break;
105859084Seric 
105959084Seric 			  default:
106059084Seric 				*statp = EX_UNAVAILABLE;
106159084Seric 				break;
106259084Seric 			}
106359671Seric 			s->s_namecanon.nc_stat = *statp;
106459084Seric 			if (*statp != EX_TEMPFAIL || UseNameServer)
106559084Seric 				return NULL;
106659084Seric 
106759084Seric 			/*
106859084Seric 			**  Try to look it up in /etc/hosts
106959084Seric 			*/
107059084Seric 
107160089Seric 			hp = gethostbyname(name);
107259084Seric 			if (hp == NULL)
107359084Seric 			{
107459084Seric 				/* no dice there either */
107559671Seric 				s->s_namecanon.nc_stat = *statp = EX_NOHOST;
107659084Seric 				return NULL;
107759084Seric 			}
107859084Seric 
107959671Seric 			s->s_namecanon.nc_stat = *statp = EX_OK;
108060257Seric 			cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
108160257Seric 			s->s_namecanon.nc_cname = newstr(cp);
108260257Seric 			return cp;
108358796Seric 		}
108453751Seric 	}
108560089Seric 	if ((cp = strchr(name, ']')) == NULL)
108653751Seric 		return (NULL);
108740994Sbostic 	*cp = '\0';
108860089Seric 	in_addr = inet_addr(&name[1]);
108958110Seric 
109058110Seric 	/* check to see if this is one of our addresses */
109158110Seric 	for (i = 0; MyIpAddrs[i].s_addr != 0; i++)
109258110Seric 	{
109358110Seric 		if (MyIpAddrs[i].s_addr == in_addr)
109458110Seric 		{
109560257Seric 			return map_rewrite(map, MyHostName, strlen(MyHostName), av);
109658110Seric 		}
109758110Seric 	}
109858110Seric 
109958110Seric 	/* nope -- ask the name server */
110033932Sbostic 	hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET);
110159671Seric 	s->s_namecanon.nc_errno = errno;
110259671Seric 	s->s_namecanon.nc_herrno = h_errno;
110359671Seric 	s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
110433932Sbostic 	if (hp == NULL)
110559671Seric 	{
110659671Seric 		s->s_namecanon.nc_stat = *statp = EX_NOHOST;
110753751Seric 		return (NULL);
110859671Seric 	}
110953751Seric 
111058110Seric 	/* found a match -- copy out */
111160257Seric 	cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
111259671Seric 	s->s_namecanon.nc_stat = *statp = EX_OK;
111360257Seric 	s->s_namecanon.nc_cname = newstr(cp);
111460257Seric 	return cp;
111533932Sbostic }
111658755Seric /*
111758755Seric **  ANYNET_NTOA -- convert a network address to printable form.
111858755Seric **
111958755Seric **	Parameters:
112058755Seric **		sap -- a pointer to a sockaddr structure.
112158755Seric **
112258755Seric **	Returns:
112358755Seric **		A printable version of that sockaddr.
112458755Seric */
112516911Seric 
112658755Seric char *
112758755Seric anynet_ntoa(sap)
112858755Seric 	register SOCKADDR *sap;
112958755Seric {
113058755Seric 	register char *bp;
113158755Seric 	register char *ap;
113258755Seric 	int l;
113358755Seric 	static char buf[80];
113458755Seric 
113558798Seric 	/* check for null/zero family */
113658798Seric 	if (sap == NULL)
113758798Seric 		return "NULLADDR";
113858798Seric 	if (sap->sa.sa_family == 0)
113958798Seric 		return "0";
114058798Seric 
114158778Seric #ifdef NETINET
114258778Seric 	if (sap->sa.sa_family == AF_INET)
114358755Seric 	{
114458755Seric 		extern char *inet_ntoa();
114558755Seric 
114658755Seric 		return inet_ntoa(((struct sockaddr_in *) sap)->sin_addr);
114758755Seric 	}
114858778Seric #endif
114958755Seric 
115058755Seric 	/* unknown family -- just dump bytes */
115158778Seric 	(void) sprintf(buf, "Family %d: ", sap->sa.sa_family);
115258755Seric 	bp = &buf[strlen(buf)];
115358778Seric 	ap = sap->sa.sa_data;
115458778Seric 	for (l = sizeof sap->sa.sa_data; --l >= 0; )
115558755Seric 	{
115658755Seric 		(void) sprintf(bp, "%02x:", *ap++ & 0377);
115758755Seric 		bp += 3;
115858755Seric 	}
115958755Seric 	*--bp = '\0';
116058755Seric 	return buf;
116158755Seric }
116258951Seric /*
116358951Seric **  HOSTNAMEBYANYADDR -- return name of host based on address
116458951Seric **
116558951Seric **	Parameters:
116658951Seric **		sap -- SOCKADDR pointer
116758951Seric **
116858951Seric **	Returns:
116958951Seric **		text representation of host name.
117058951Seric **
117158951Seric **	Side Effects:
117258951Seric **		none.
117358951Seric */
117458755Seric 
117558951Seric char *
117658951Seric hostnamebyanyaddr(sap)
117758951Seric 	register SOCKADDR *sap;
117858951Seric {
117958951Seric 	register struct hostent *hp;
118058951Seric 
118159042Seric #ifdef NAMED_BIND
118259042Seric 	int saveretry;
118359042Seric 
118459042Seric 	/* shorten name server timeout to avoid higher level timeouts */
118559042Seric 	saveretry = _res.retry;
118659042Seric 	_res.retry = 3;
118759042Seric #endif /* NAMED_BIND */
118859042Seric 
118958951Seric 	switch (sap->sa.sa_family)
119058951Seric 	{
119158951Seric #ifdef NETINET
119258951Seric 	  case AF_INET:
119358951Seric 		hp = gethostbyaddr((char *) &sap->sin.sin_addr,
119458951Seric 			sizeof sap->sin.sin_addr,
119558951Seric 			AF_INET);
119658951Seric 		break;
119758951Seric #endif
119858951Seric 
119958951Seric #ifdef NETISO
120058951Seric 	  case AF_ISO:
120158951Seric 		hp = gethostbyaddr((char *) &sap->siso.siso_addr,
120258951Seric 			sizeof sap->siso.siso_addr,
120358951Seric 			AF_ISO);
120458951Seric 		break;
120558951Seric #endif
120658951Seric 
120758951Seric 	  default:
120858951Seric 		hp = gethostbyaddr(sap->sa.sa_data,
120958951Seric 			   sizeof sap->sa.sa_data,
121058951Seric 			   sap->sa.sa_family);
121158951Seric 		break;
121258951Seric 	}
121358951Seric 
121459042Seric #ifdef NAMED_BIND
121559042Seric 	_res.retry = saveretry;
121659042Seric #endif /* NAMED_BIND */
121759042Seric 
121858951Seric 	if (hp != NULL)
121958951Seric 		return hp->h_name;
122058951Seric 	else
122158951Seric 	{
122258951Seric 		/* produce a dotted quad */
122358951Seric 		static char buf[512];
122458951Seric 
122558951Seric 		(void) sprintf(buf, "[%s]", anynet_ntoa(sap));
122658951Seric 		return buf;
122758951Seric 	}
122858951Seric }
122958951Seric 
123056795Seric # else /* DAEMON */
123116911Seric /* code for systems without sophisticated networking */
123210758Seric 
123310758Seric /*
123410758Seric **  MYHOSTNAME -- stub version for case of no daemon code.
123511297Seric **
123611297Seric **	Can't convert to upper case here because might be a UUCP name.
123712313Seric **
123812313Seric **	Mark, you can change this to be anything you want......
123910758Seric */
124010758Seric 
124110758Seric char **
124212313Seric myhostname(hostbuf, size)
124310758Seric 	char hostbuf[];
124412313Seric 	int size;
124510758Seric {
124610758Seric 	register FILE *f;
124710758Seric 
124810758Seric 	hostbuf[0] = '\0';
124910758Seric 	f = fopen("/usr/include/whoami", "r");
125010758Seric 	if (f != NULL)
125110758Seric 	{
125212313Seric 		(void) fgets(hostbuf, size, f);
125310758Seric 		fixcrlf(hostbuf, TRUE);
125410758Seric 		(void) fclose(f);
125510758Seric 	}
125610758Seric 	return (NULL);
125710758Seric }
125816911Seric /*
125958951Seric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
126058308Seric **
126158308Seric **	Parameters:
126258308Seric **		fd -- the descriptor
126358308Seric **
126458308Seric **	Returns:
126558308Seric **		The host name associated with this descriptor, if it can
126658308Seric **			be determined.
126758308Seric **		NULL otherwise.
126858308Seric **
126958308Seric **	Side Effects:
127058308Seric **		none
127158308Seric */
127258308Seric 
127358308Seric char *
127458951Seric getauthinfo(fd)
127558308Seric 	int fd;
127658308Seric {
127758308Seric 	return NULL;
127858308Seric }
127958308Seric /*
128016911Seric **  MAPHOSTNAME -- turn a hostname into canonical form
128116911Seric **
128216911Seric **	Parameters:
128356823Seric **		map -- a pointer to the database map.
128460089Seric **		name -- a buffer containing a hostname.
128553751Seric **		avp -- a pointer to a (cf file defined) argument vector.
128659084Seric **		statp -- an exit status (out parameter).
128716911Seric **
128816911Seric **	Returns:
128953751Seric **		mapped host name
129051315Seric **		FALSE otherwise.
129116911Seric **
129216911Seric **	Side Effects:
129360089Seric **		Looks up the host specified in name.  If it is not
129416911Seric **		the canonical name for that host, replace it with
129516911Seric **		the canonical name.  If the name is unknown, or it
129616911Seric **		is already the canonical name, leave it unchanged.
129716911Seric */
129810758Seric 
129916911Seric /*ARGSUSED*/
130053751Seric char *
130160089Seric host_map_lookup(map, name, avp, statp)
130256823Seric 	MAP *map;
130360089Seric 	char *name;
130453751Seric 	char **avp;
130559084Seric 	char *statp;
130616911Seric {
130759084Seric 	register struct hostent *hp;
130859084Seric 
130960089Seric 	hp = gethostbyname(name);
131059084Seric 	if (hp != NULL)
131159084Seric 		return hp->h_name;
131259084Seric 	*statp = EX_NOHOST;
131353751Seric 	return NULL;
131416911Seric }
131516911Seric 
131656795Seric #endif /* DAEMON */
1317