xref: /csrg-svn/usr.sbin/sendmail/src/daemon.c (revision 60089)
122700Sdist /*
234920Sbostic  * Copyright (c) 1983 Eric P. Allman
333780Sbostic  * Copyright (c) 1988 Regents of the University of California.
433780Sbostic  * All rights reserved.
533780Sbostic  *
642825Sbostic  * %sccs.include.redist.c%
733780Sbostic  */
822700Sdist 
933932Sbostic #include <errno.h>
1058153Seric #include <signal.h>
1140962Sbostic #include "sendmail.h"
124535Seric 
1333780Sbostic #ifndef lint
1433780Sbostic #ifdef DAEMON
15*60089Seric static char sccsid[] = "@(#)daemon.c	6.49 (Berkeley) 05/17/93 (with daemon mode)";
1633780Sbostic #else
17*60089Seric static char sccsid[] = "@(#)daemon.c	6.49 (Berkeley) 05/17/93 (without daemon mode)";
1833780Sbostic #endif
1933780Sbostic #endif /* not lint */
204535Seric 
2133780Sbostic #ifdef DAEMON
2233780Sbostic 
2323120Seric # include <netdb.h>
2423120Seric # include <sys/wait.h>
2523120Seric # include <sys/time.h>
265978Seric 
2759042Seric #ifdef NAMED_BIND
2859042Seric # include <arpa/nameser.h>
2959042Seric # include <resolv.h>
3059042Seric #endif
3159042Seric 
324535Seric /*
334535Seric **  DAEMON.C -- routines to use when running as a daemon.
347556Seric **
357556Seric **	This entire file is highly dependent on the 4.2 BSD
367556Seric **	interprocess communication primitives.  No attempt has
377556Seric **	been made to make this file portable to Version 7,
387556Seric **	Version 6, MPX files, etc.  If you should try such a
397556Seric **	thing yourself, I recommend chucking the entire file
407556Seric **	and starting from scratch.  Basic semantics are:
417556Seric **
427556Seric **	getrequests()
437556Seric **		Opens a port and initiates a connection.
447556Seric **		Returns in a child.  Must set InChannel and
457556Seric **		OutChannel appropriately.
4610206Seric **	clrdaemon()
4710206Seric **		Close any open files associated with getting
4810206Seric **		the connection; this is used when running the queue,
4910206Seric **		etc., to avoid having extra file descriptors during
5010206Seric **		the queue run and to avoid confusing the network
5110206Seric **		code (if it cares).
5252106Seric **	makeconnection(host, port, outfile, infile, usesecureport)
537556Seric **		Make a connection to the named host on the given
547556Seric **		port.  Set *outfile and *infile to the files
557556Seric **		appropriate for communication.  Returns zero on
567556Seric **		success, else an exit status describing the
577556Seric **		error.
58*60089Seric **	host_map_lookup(map, hbuf, avp, pstat)
5956823Seric **		Convert the entry in hbuf into a canonical form.
604535Seric */
6158755Seric 
6258755Seric extern char	*anynet_ntoa();
634535Seric /*
644535Seric **  GETREQUESTS -- open mail IPC port and get requests.
654535Seric **
664535Seric **	Parameters:
674535Seric **		none.
684535Seric **
694535Seric **	Returns:
704535Seric **		none.
714535Seric **
724535Seric **	Side Effects:
734535Seric **		Waits until some interesting activity occurs.  When
744535Seric **		it does, a child is created to process it, and the
754535Seric **		parent waits for completion.  Return from this
769886Seric **		routine is always in the child.  The file pointers
779886Seric **		"InChannel" and "OutChannel" should be set to point
789886Seric **		to the communication channel.
794535Seric */
804535Seric 
8158849Seric int		DaemonSocket	= -1;		/* fd describing socket */
8258849Seric SOCKADDR	DaemonAddr;			/* socket for incoming */
8359783Seric int		ListenQueueSize = 10;		/* size of listen queue */
8416144Seric 
854535Seric getrequests()
864535Seric {
879610Seric 	int t;
889610Seric 	register struct servent *sp;
8925027Seric 	int on = 1;
9053751Seric 	bool refusingconnections = TRUE;
9158419Seric 	FILE *pidf;
9246928Sbostic 	extern void reapchild();
937117Seric 
949610Seric 	/*
959610Seric 	**  Set up the address for the mailer.
969610Seric 	*/
979610Seric 
9858849Seric 	if (DaemonAddr.sin.sin_family == 0)
9958849Seric 		DaemonAddr.sin.sin_family = AF_INET;
10058849Seric 	if (DaemonAddr.sin.sin_addr.s_addr == 0)
10158849Seric 		DaemonAddr.sin.sin_addr.s_addr = INADDR_ANY;
10258849Seric 	if (DaemonAddr.sin.sin_port == 0)
1039610Seric 	{
10458849Seric 		sp = getservbyname("smtp", "tcp");
10558849Seric 		if (sp == NULL)
10658849Seric 		{
10758909Seric 			syserr("554 service \"smtp\" unknown");
10858849Seric 			goto severe;
10958849Seric 		}
11058849Seric 		DaemonAddr.sin.sin_port = sp->s_port;
1119610Seric 	}
1129610Seric 
1139610Seric 	/*
1149610Seric 	**  Try to actually open the connection.
1159610Seric 	*/
1169610Seric 
1179610Seric 	if (tTd(15, 1))
11858849Seric 		printf("getrequests: port 0x%x\n", DaemonAddr.sin.sin_port);
1199610Seric 
1209610Seric 	/* get a socket for the SMTP connection */
12159041Seric 	DaemonSocket = socket(DaemonAddr.sa.sa_family, SOCK_STREAM, 0);
12210206Seric 	if (DaemonSocket < 0)
1239610Seric 	{
1249610Seric 		/* probably another daemon already */
1259610Seric 		syserr("getrequests: can't create socket");
1269610Seric 	  severe:
1279610Seric # ifdef LOG
1289610Seric 		if (LogLevel > 0)
12957663Seric 			syslog(LOG_ALERT, "problem creating SMTP socket");
13056795Seric # endif /* LOG */
1319610Seric 		finis();
1329610Seric 	}
13310347Seric 
13410347Seric 	/* turn on network debugging? */
13556328Seric 	if (tTd(15, 101))
13624945Seric 		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on);
13710347Seric 
13825027Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on);
13925027Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on);
14025027Seric 
14159041Seric 	switch (DaemonAddr.sa.sa_family)
1429610Seric 	{
14359041Seric # ifdef NETINET
14459041Seric 	  case AF_INET:
14559041Seric 		t = sizeof DaemonAddr.sin;
14659041Seric 		break;
14759041Seric # endif
14859041Seric 
14959041Seric # ifdef NETISO
15059041Seric 	  case AF_ISO:
15159041Seric 		t = sizeof DaemonAddr.siso;
15259041Seric 		break;
15359041Seric # endif
15459041Seric 
15559041Seric 	  default:
15659041Seric 		t = sizeof DaemonAddr;
15759041Seric 		break;
15859041Seric 	}
15959041Seric 
16059041Seric 	if (bind(DaemonSocket, &DaemonAddr.sa, t) < 0)
16159041Seric 	{
1629610Seric 		syserr("getrequests: cannot bind");
16310206Seric 		(void) close(DaemonSocket);
1649610Seric 		goto severe;
1659610Seric 	}
1669610Seric 
16724955Seric 	(void) signal(SIGCHLD, reapchild);
16824945Seric 
16958419Seric 	/* write the pid to the log file for posterity */
17058419Seric 	pidf = fopen(PidFile, "w");
17158419Seric 	if (pidf != NULL)
17258419Seric 	{
17358419Seric 		fprintf(pidf, "%d\n", getpid());
17458419Seric 		fclose(pidf);
17558419Seric 	}
17658419Seric 
17758419Seric 
1789610Seric 	if (tTd(15, 1))
17910206Seric 		printf("getrequests: %d\n", DaemonSocket);
1809610Seric 
1814631Seric 	for (;;)
1824631Seric 	{
18314875Seric 		register int pid;
18411147Seric 		auto int lotherend;
18553751Seric 		extern bool refuseconnections();
18611147Seric 
18714875Seric 		/* see if we are rejecting connections */
18853751Seric 		CurrentLA = getla();
18953751Seric 		if (refuseconnections())
19036584Sbostic 		{
19153751Seric 			if (!refusingconnections)
19253751Seric 			{
19353751Seric 				/* don't queue so peer will fail quickly */
19453751Seric 				(void) listen(DaemonSocket, 0);
19553751Seric 				refusingconnections = TRUE;
19653751Seric 			}
19757385Seric 			setproctitle("rejecting connections: load average: %d",
19857385Seric 				CurrentLA);
19914875Seric 			sleep(5);
20053751Seric 			continue;
20136584Sbostic 		}
20214875Seric 
20353751Seric 		if (refusingconnections)
20453751Seric 		{
20553751Seric 			/* start listening again */
20659783Seric 			if (listen(DaemonSocket, ListenQueueSize) < 0)
20753751Seric 			{
20853751Seric 				syserr("getrequests: cannot listen");
20953751Seric 				(void) close(DaemonSocket);
21053751Seric 				goto severe;
21153751Seric 			}
21253751Seric 			setproctitle("accepting connections");
21353751Seric 			refusingconnections = FALSE;
21453751Seric 		}
21553751Seric 
2169610Seric 		/* wait for a connection */
2179610Seric 		do
2189610Seric 		{
2199610Seric 			errno = 0;
22036230Skarels 			lotherend = sizeof RealHostAddr;
22146928Sbostic 			t = accept(DaemonSocket,
22246928Sbostic 			    (struct sockaddr *)&RealHostAddr, &lotherend);
2239610Seric 		} while (t < 0 && errno == EINTR);
2249610Seric 		if (t < 0)
2255978Seric 		{
2269610Seric 			syserr("getrequests: accept");
2279610Seric 			sleep(5);
2289610Seric 			continue;
2295978Seric 		}
2304631Seric 
2315978Seric 		/*
2325978Seric 		**  Create a subprocess to process the mail.
2335978Seric 		*/
2345978Seric 
2357677Seric 		if (tTd(15, 2))
2369610Seric 			printf("getrequests: forking (fd = %d)\n", t);
2375978Seric 
2384636Seric 		pid = fork();
2394636Seric 		if (pid < 0)
2404631Seric 		{
2414636Seric 			syserr("daemon: cannot fork");
2424636Seric 			sleep(10);
2439610Seric 			(void) close(t);
2444636Seric 			continue;
2454631Seric 		}
2464631Seric 
2474636Seric 		if (pid == 0)
2484631Seric 		{
24958951Seric 			extern char *hostnamebyanyaddr();
25011147Seric 
2514636Seric 			/*
2524636Seric 			**  CHILD -- return to caller.
25311147Seric 			**	Collect verified idea of sending host.
2544636Seric 			**	Verify calling user id if possible here.
2554636Seric 			*/
2564631Seric 
25724955Seric 			(void) signal(SIGCHLD, SIG_DFL);
25859156Seric 			OpMode = MD_SMTP;
25924950Seric 
26011147Seric 			/* determine host name */
26158951Seric 			RealHostName = newstr(hostnamebyanyaddr(&RealHostAddr));
26258778Seric 
26355173Seric #ifdef LOG
26457977Seric 			if (LogLevel > 10)
26555173Seric 			{
26655173Seric 				/* log connection information */
26755173Seric 				syslog(LOG_INFO, "connect from %s (%s)",
26858951Seric 					RealHostName, anynet_ntoa(&RealHostAddr));
26955173Seric 			}
27055173Seric #endif
27155173Seric 
27259254Seric 			(void) close(DaemonSocket);
27359254Seric 			InChannel = fdopen(t, "r");
27459254Seric 			OutChannel = fdopen(dup(t), "w");
27559254Seric 
27616884Seric 			/* should we check for illegal connection here? XXX */
27759156Seric #ifdef XLA
27859156Seric 			if (!xla_host_ok(RealHostName))
27959156Seric 			{
28059254Seric 				message("421 Too many SMTP sessions for this host");
28159156Seric 				exit(0);
28259156Seric 			}
28359156Seric #endif
28416884Seric 
2857677Seric 			if (tTd(15, 2))
2865978Seric 				printf("getreq: returning\n");
2874636Seric 			return;
2884631Seric 		}
2894631Seric 
2907117Seric 		/* close the port so that others will hang (for a while) */
2919610Seric 		(void) close(t);
2924631Seric 	}
2939886Seric 	/*NOTREACHED*/
2944631Seric }
2955978Seric /*
29610206Seric **  CLRDAEMON -- reset the daemon connection
29710206Seric **
29810206Seric **	Parameters:
29910206Seric **		none.
30010206Seric **
30110206Seric **	Returns:
30210206Seric **		none.
30310206Seric **
30410206Seric **	Side Effects:
30510206Seric **		releases any resources used by the passive daemon.
30610206Seric */
30710206Seric 
30810206Seric clrdaemon()
30910206Seric {
31010206Seric 	if (DaemonSocket >= 0)
31110206Seric 		(void) close(DaemonSocket);
31210206Seric 	DaemonSocket = -1;
31310206Seric }
31410206Seric /*
31558849Seric **  SETDAEMONOPTIONS -- set options for running the daemon
31658849Seric **
31758849Seric **	Parameters:
31858849Seric **		p -- the options line.
31958849Seric **
32058849Seric **	Returns:
32158849Seric **		none.
32258849Seric */
32358849Seric 
32458849Seric setdaemonoptions(p)
32558849Seric 	register char *p;
32658849Seric {
32758873Seric 	if (DaemonAddr.sa.sa_family == AF_UNSPEC)
32858873Seric 		DaemonAddr.sa.sa_family = AF_INET;
32958873Seric 
33058849Seric 	while (p != NULL)
33158849Seric 	{
33258849Seric 		register char *f;
33358849Seric 		register char *v;
33458849Seric 
33558849Seric 		while (isascii(*p) && isspace(*p))
33658849Seric 			p++;
33758849Seric 		if (*p == '\0')
33858849Seric 			break;
33958849Seric 		f = p;
34058849Seric 		p = strchr(p, ',');
34158849Seric 		if (p != NULL)
34258849Seric 			*p++ = '\0';
34358849Seric 		v = strchr(f, '=');
34458849Seric 		if (v == NULL)
34558849Seric 			continue;
34658849Seric 		while (isascii(*++v) && isspace(*v))
34758849Seric 			continue;
34858849Seric 
34958849Seric 		switch (*f)
35058849Seric 		{
35158873Seric 		  case 'F':		/* address family */
35258849Seric 			if (isascii(*v) && isdigit(*v))
35358873Seric 				DaemonAddr.sa.sa_family = atoi(v);
35458873Seric #ifdef NETINET
35558873Seric 			else if (strcasecmp(v, "inet") == 0)
35658873Seric 				DaemonAddr.sa.sa_family = AF_INET;
35758873Seric #endif
35858873Seric #ifdef NETISO
35958873Seric 			else if (strcasecmp(v, "iso") == 0)
36058873Seric 				DaemonAddr.sa.sa_family = AF_ISO;
36158873Seric #endif
36258873Seric #ifdef NETNS
36358873Seric 			else if (strcasecmp(v, "ns") == 0)
36458873Seric 				DaemonAddr.sa.sa_family = AF_NS;
36558873Seric #endif
36658873Seric #ifdef NETX25
36758873Seric 			else if (strcasecmp(v, "x.25") == 0)
36858873Seric 				DaemonAddr.sa.sa_family = AF_CCITT;
36958873Seric #endif
37058849Seric 			else
37158873Seric 				syserr("554 Unknown address family %s in Family=option", v);
37258873Seric 			break;
37358873Seric 
37458873Seric 		  case 'A':		/* address */
37558873Seric 			switch (DaemonAddr.sa.sa_family)
37658849Seric 			{
37758873Seric #ifdef NETINET
37858873Seric 			  case AF_INET:
37958873Seric 				if (isascii(*v) && isdigit(*v))
38058873Seric 					DaemonAddr.sin.sin_addr.s_addr = inet_network(v);
38158873Seric 				else
38258873Seric 				{
38358873Seric 					register struct netent *np;
38458849Seric 
38558873Seric 					np = getnetbyname(v);
38658873Seric 					if (np == NULL)
38758873Seric 						syserr("554 network \"%s\" unknown", v);
38858873Seric 					else
38958873Seric 						DaemonAddr.sin.sin_addr.s_addr = np->n_net;
39058873Seric 				}
39158873Seric 				break;
39258873Seric #endif
39358873Seric 
39458873Seric 			  default:
39558873Seric 				syserr("554 Address= option unsupported for family %d",
39658873Seric 					DaemonAddr.sa.sa_family);
39758873Seric 				break;
39858849Seric 			}
39958849Seric 			break;
40058849Seric 
40158873Seric 		  case 'P':		/* port */
40258873Seric 			switch (DaemonAddr.sa.sa_family)
40358849Seric 			{
40458873Seric 				short port;
40558849Seric 
40658873Seric #ifdef NETINET
40758873Seric 			  case AF_INET:
40858873Seric 				if (isascii(*v) && isdigit(*v))
40958873Seric 					DaemonAddr.sin.sin_port = atoi(v);
41058849Seric 				else
41158873Seric 				{
41258873Seric 					register struct servent *sp;
41358873Seric 
41458873Seric 					sp = getservbyname(v, "tcp");
41558873Seric 					if (sp == NULL)
41658909Seric 						syserr("554 service \"%s\" unknown", v);
41758873Seric 					else
41858873Seric 						DaemonAddr.sin.sin_port = sp->s_port;
41958873Seric 				}
42058873Seric 				break;
42158873Seric #endif
42258873Seric 
42358873Seric #ifdef NETISO
42458873Seric 			  case AF_ISO:
42558873Seric 				/* assume two byte transport selector */
42658873Seric 				if (isascii(*v) && isdigit(*v))
42758873Seric 					port = atoi(v);
42858873Seric 				else
42958873Seric 				{
43058873Seric 					register struct servent *sp;
43158873Seric 
43258873Seric 					sp = getservbyname(v, "tcp");
43358873Seric 					if (sp == NULL)
43458909Seric 						syserr("554 service \"%s\" unknown", v);
43558873Seric 					else
43658873Seric 						port = sp->s_port;
43758873Seric 				}
43858873Seric 				bcopy((char *) &port, TSEL(&DaemonAddr.siso), 2);
43958873Seric 				break;
44058873Seric #endif
44158873Seric 
44258873Seric 			  default:
44358873Seric 				syserr("554 Port= option unsupported for family %d",
44458873Seric 					DaemonAddr.sa.sa_family);
44558873Seric 				break;
44658849Seric 			}
44758849Seric 			break;
44859783Seric 
44959783Seric 		  case 'L':		/* listen queue size */
45059783Seric 			ListenQueueSize = atoi(v);
45159783Seric 			break;
45258849Seric 		}
45358849Seric 	}
45458849Seric }
45558849Seric /*
4566039Seric **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
4576039Seric **
4586039Seric **	Parameters:
4596039Seric **		host -- the name of the host.
4606633Seric **		port -- the port number to connect to.
46153739Seric **		mci -- a pointer to the mail connection information
46253739Seric **			structure to be filled in.
46352106Seric **		usesecureport -- if set, use a low numbered (reserved)
46452106Seric **			port to provide some rudimentary authentication.
4656039Seric **
4666039Seric **	Returns:
4676039Seric **		An exit code telling whether the connection could be
4686039Seric **			made and if not why not.
4696039Seric **
4706039Seric **	Side Effects:
4716039Seric **		none.
4726039Seric */
4735978Seric 
47458755Seric SOCKADDR	CurHostAddr;		/* address of current host */
47558305Seric 
47654967Seric int
47753739Seric makeconnection(host, port, mci, usesecureport)
4786039Seric 	char *host;
4797286Seric 	u_short port;
48054967Seric 	register MCI *mci;
48152106Seric 	bool usesecureport;
4826039Seric {
48329430Sbloom 	register int i, s;
48429430Sbloom 	register struct hostent *hp = (struct hostent *)NULL;
48558755Seric 	SOCKADDR addr;
48652106Seric 	int sav_errno;
48758755Seric 	int addrlen;
48835651Seric #ifdef NAMED_BIND
48935651Seric 	extern int h_errno;
49035651Seric #endif
4916039Seric 
4926039Seric 	/*
4936039Seric 	**  Set up the address for the mailer.
4949308Seric 	**	Accept "[a.b.c.d]" syntax for host name.
4956039Seric 	*/
4966039Seric 
49735651Seric #ifdef NAMED_BIND
49825475Smiriam 	h_errno = 0;
49935651Seric #endif
50025475Smiriam 	errno = 0;
50158864Seric 	bzero(&CurHostAddr, sizeof CurHostAddr);
50258906Seric 	CurHostName = host;
50325475Smiriam 
5049308Seric 	if (host[0] == '[')
5059308Seric 	{
50611147Seric 		long hid;
50756795Seric 		register char *p = strchr(host, ']');
5089308Seric 
50911147Seric 		if (p != NULL)
5109308Seric 		{
51111147Seric 			*p = '\0';
51259884Seric #ifdef NETINET
51311147Seric 			hid = inet_addr(&host[1]);
51458360Seric 			if (hid == -1)
51559884Seric #endif
51658360Seric 			{
51758360Seric 				/* try it as a host name (avoid MX lookup) */
51858360Seric 				hp = gethostbyname(&host[1]);
51958360Seric 				*p = ']';
52058360Seric 				goto gothostent;
52158360Seric 			}
52211147Seric 			*p = ']';
5239308Seric 		}
52458360Seric 		if (p == NULL)
5259308Seric 		{
52658151Seric 			usrerr("553 Invalid numeric domain spec \"%s\"", host);
5279308Seric 			return (EX_NOHOST);
5289308Seric 		}
52959884Seric #ifdef NETINET
53059884Seric 		addr.sin.sin_family = AF_INET;		/*XXX*/
53158778Seric 		addr.sin.sin_addr.s_addr = hid;
53259884Seric #endif
5339308Seric 	}
5349610Seric 	else
5359610Seric 	{
53629430Sbloom 		hp = gethostbyname(host);
53758360Seric gothostent:
53825475Smiriam 		if (hp == NULL)
53924945Seric 		{
54035651Seric #ifdef NAMED_BIND
54125475Smiriam 			if (errno == ETIMEDOUT || h_errno == TRY_AGAIN)
54225475Smiriam 				return (EX_TEMPFAIL);
54325657Seric 
54435651Seric 			/* if name server is specified, assume temp fail */
54535651Seric 			if (errno == ECONNREFUSED && UseNameServer)
54635651Seric 				return (EX_TEMPFAIL);
54735651Seric #endif
54825475Smiriam 			return (EX_NOHOST);
54924945Seric 		}
55058778Seric 		addr.sa.sa_family = hp->h_addrtype;
55158778Seric 		switch (hp->h_addrtype)
55258778Seric 		{
55358778Seric #ifdef NETINET
55458778Seric 		  case AF_INET:
55558755Seric 			bcopy(hp->h_addr,
55658778Seric 				&addr.sin.sin_addr,
55758755Seric 				hp->h_length);
55858778Seric 			break;
55958778Seric #endif
56058778Seric 
56158778Seric 		  default:
56258755Seric 			bcopy(hp->h_addr,
56358778Seric 				addr.sa.sa_data,
56458755Seric 				hp->h_length);
56558778Seric 			break;
56658778Seric 		}
56729430Sbloom 		i = 1;
5689610Seric 	}
5699610Seric 
5709610Seric 	/*
5719610Seric 	**  Determine the port number.
5729610Seric 	*/
5739610Seric 
57410011Seric 	if (port != 0)
57558755Seric 		port = htons(port);
57610011Seric 	else
5779610Seric 	{
5789610Seric 		register struct servent *sp = getservbyname("smtp", "tcp");
5799610Seric 
5809610Seric 		if (sp == NULL)
5819610Seric 		{
58258909Seric 			syserr("554 makeconnection: service \"smtp\" unknown");
58357977Seric 			return (EX_OSERR);
5849610Seric 		}
58558755Seric 		port = sp->s_port;
5869610Seric 	}
5876039Seric 
58858778Seric 	switch (addr.sa.sa_family)
58958755Seric 	{
59059884Seric #ifdef NETINET
59158755Seric 	  case AF_INET:
59258778Seric 		addr.sin.sin_port = port;
59358755Seric 		addrlen = sizeof (struct sockaddr_in);
59458755Seric 		break;
59559884Seric #endif
59658755Seric 
59758755Seric #ifdef NETISO
59858755Seric 	  case AF_ISO:
59958755Seric 		/* assume two byte transport selector */
60058755Seric 		bcopy((char *) &port, TSEL((struct sockaddr_iso *) &addr), 2);
60158755Seric 		addrlen = sizeof (struct sockaddr_iso);
60258755Seric 		break;
60358755Seric #endif
60458755Seric 
60558755Seric 	  default:
60658778Seric 		syserr("Can't connect to address family %d", addr.sa.sa_family);
60758755Seric 		return (EX_NOHOST);
60858755Seric 	}
60958755Seric 
6106039Seric 	/*
6116039Seric 	**  Try to actually open the connection.
6126039Seric 	*/
6136039Seric 
61459156Seric #ifdef XLA
61559156Seric 	/* if too many connections, don't bother trying */
61659156Seric 	if (!xla_noqueue_ok(host))
61759156Seric 		return EX_TEMPFAIL;
61859156Seric #endif
61959156Seric 
62057736Seric 	for (;;)
62152106Seric 	{
62257736Seric 		if (tTd(16, 1))
62358755Seric 			printf("makeconnection (%s [%s])\n",
62458755Seric 				host, anynet_ntoa(&addr));
62552106Seric 
62658588Seric 		/* save for logging */
62758588Seric 		CurHostAddr = addr;
62858588Seric 
62957736Seric 		if (usesecureport)
63057736Seric 		{
63157736Seric 			int rport = IPPORT_RESERVED - 1;
6326039Seric 
63357736Seric 			s = rresvport(&rport);
63457736Seric 		}
63557736Seric 		else
63657736Seric 		{
63757736Seric 			s = socket(AF_INET, SOCK_STREAM, 0);
63857736Seric 		}
63957736Seric 		if (s < 0)
64057736Seric 		{
64157736Seric 			sav_errno = errno;
64257736Seric 			syserr("makeconnection: no socket");
64357736Seric 			goto failure;
64457736Seric 		}
64510347Seric 
64657736Seric 		if (tTd(16, 1))
64757736Seric 			printf("makeconnection: fd=%d\n", s);
64857736Seric 
64957736Seric 		/* turn on network debugging? */
65057736Seric 		if (tTd(16, 101))
65157736Seric 		{
65257736Seric 			int on = 1;
65357736Seric 			(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG,
65457736Seric 					  (char *)&on, sizeof on);
65557736Seric 		}
65657736Seric 		if (CurEnv->e_xfp != NULL)
65757736Seric 			(void) fflush(CurEnv->e_xfp);		/* for debugging */
65857736Seric 		errno = 0;					/* for debugging */
65958755Seric 		if (connect(s, (struct sockaddr *) &addr, addrlen) >= 0)
66057736Seric 			break;
66157736Seric 
66257736Seric 		/* couldn't connect.... figure out why */
66327744Sbloom 		sav_errno = errno;
66427744Sbloom 		(void) close(s);
66529430Sbloom 		if (hp && hp->h_addr_list[i])
66629430Sbloom 		{
66757736Seric 			if (tTd(16, 1))
66858755Seric 				printf("Connect failed (%s); trying new address....\n",
66958755Seric 					errstring(sav_errno));
67058778Seric 			switch (addr.sa.sa_family)
67158778Seric 			{
67258778Seric #ifdef NETINET
67358778Seric 			  case AF_INET:
67458755Seric 				bcopy(hp->h_addr_list[i++],
67558778Seric 				      &addr.sin.sin_addr,
67658755Seric 				      hp->h_length);
67758778Seric 				break;
67858778Seric #endif
67958778Seric 
68058778Seric 			  default:
68158755Seric 				bcopy(hp->h_addr_list[i++],
68258778Seric 					addr.sa.sa_data,
68352106Seric 					hp->h_length);
68458778Seric 				break;
68558778Seric 			}
68657736Seric 			continue;
68729430Sbloom 		}
68829430Sbloom 
6896039Seric 		/* failure, decide if temporary or not */
6906039Seric 	failure:
69159254Seric #ifdef XLA
69259254Seric 		xla_host_end(host);
69359254Seric #endif
69458542Seric 		if (transienterror(sav_errno))
69558542Seric 			return EX_TEMPFAIL;
69658542Seric 		else
69758542Seric 		{
69858542Seric 			message("%s", errstring(sav_errno));
69958542Seric 			return (EX_UNAVAILABLE);
7006039Seric 		}
7016039Seric 	}
7026039Seric 
7036039Seric 	/* connection ok, put it into canonical form */
70453739Seric 	mci->mci_out = fdopen(s, "w");
70553739Seric 	mci->mci_in = fdopen(dup(s), "r");
7066039Seric 
70710098Seric 	return (EX_OK);
7086039Seric }
70910758Seric /*
71010758Seric **  MYHOSTNAME -- return the name of this host.
71110758Seric **
71210758Seric **	Parameters:
71310758Seric **		hostbuf -- a place to return the name of this host.
71412313Seric **		size -- the size of hostbuf.
71510758Seric **
71610758Seric **	Returns:
71710758Seric **		A list of aliases for this host.
71810758Seric **
71910758Seric **	Side Effects:
72058110Seric **		Sets the MyIpAddrs buffer to a list of my IP addresses.
72110758Seric */
7226039Seric 
72358110Seric struct in_addr	MyIpAddrs[MAXIPADDR + 1];
72458110Seric 
72510758Seric char **
72612313Seric myhostname(hostbuf, size)
72710758Seric 	char hostbuf[];
72812313Seric 	int size;
72910758Seric {
73058110Seric 	register struct hostent *hp;
73110758Seric 	extern struct hostent *gethostbyname();
73210758Seric 
73323120Seric 	if (gethostname(hostbuf, size) < 0)
73423120Seric 	{
73523120Seric 		(void) strcpy(hostbuf, "localhost");
73623120Seric 	}
73711147Seric 	hp = gethostbyname(hostbuf);
73811147Seric 	if (hp != NULL)
73916877Seric 	{
74058110Seric 		(void) strncpy(hostbuf, hp->h_name, size - 1);
74158110Seric 		hostbuf[size - 1] = '\0';
74258110Seric 
74358110Seric 		if (hp->h_addrtype == AF_INET && hp->h_length == 4)
74458110Seric 		{
74558110Seric 			register int i;
74658110Seric 
74758110Seric 			for (i = 0; i < MAXIPADDR; i++)
74858110Seric 			{
74958110Seric 				if (hp->h_addr_list[i] == NULL)
75058110Seric 					break;
75158110Seric 				MyIpAddrs[i].s_addr = *(u_long *) hp->h_addr_list[i];
75258110Seric 			}
75358110Seric 			MyIpAddrs[i].s_addr = 0;
75458110Seric 		}
75558110Seric 
75611147Seric 		return (hp->h_aliases);
75716877Seric 	}
75810758Seric 	else
75910758Seric 		return (NULL);
76010758Seric }
76151315Seric /*
76258951Seric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
76358308Seric **
76458951Seric **	Uses RFC1413 protocol to try to get info from the other end.
76558951Seric **
76658308Seric **	Parameters:
76758308Seric **		fd -- the descriptor
76858308Seric **
76958308Seric **	Returns:
77058951Seric **		The user@host information associated with this descriptor.
77158308Seric **
77258308Seric **	Side Effects:
77358951Seric **		Sets RealHostName to the name of the host at the other end.
77458308Seric */
77558308Seric 
77658951Seric #ifdef IDENTPROTO
77758951Seric 
77858951Seric static jmp_buf	CtxAuthTimeout;
77958951Seric 
78058951Seric static
78158951Seric authtimeout()
78258951Seric {
78358951Seric 	longjmp(CtxAuthTimeout, 1);
78458951Seric }
78558951Seric 
78658951Seric #endif
78758951Seric 
78858308Seric char *
78958951Seric getauthinfo(fd)
79058308Seric 	int fd;
79158308Seric {
79258951Seric 	SOCKADDR fa;
79358951Seric 	int falen;
79459104Seric 	register char *p;
79558951Seric #ifdef IDENTPROTO
79658951Seric 	SOCKADDR la;
79758951Seric 	int lalen;
79858951Seric 	register struct servent *sp;
79958951Seric 	int s;
80058951Seric 	int i;
80158951Seric 	EVENT *ev;
80258951Seric #endif
80358951Seric 	static char hbuf[MAXNAME * 2 + 2];
80458951Seric 	extern char *hostnamebyanyaddr();
80558951Seric 	extern char RealUserName[];			/* main.c */
80658308Seric 
80758951Seric 	falen = sizeof fa;
80858951Seric 	if (getpeername(fd, &fa.sa, &falen) < 0 || falen <= 0)
80958951Seric 	{
81058951Seric 		RealHostName = "localhost";
81158951Seric 		(void) sprintf(hbuf, "%s@localhost", RealUserName);
81258957Seric 		if (tTd(9, 1))
81358951Seric 			printf("getauthinfo: %s\n", hbuf);
81458951Seric 		return hbuf;
81558951Seric 	}
81658951Seric 
81758951Seric 	RealHostName = newstr(hostnamebyanyaddr(&fa));
81858951Seric 	RealHostAddr = fa;
81958951Seric 
82058951Seric #ifdef IDENTPROTO
82158951Seric 	lalen = sizeof la;
82258951Seric 	if (fa.sa.sa_family != AF_INET ||
82358951Seric 	    getsockname(fd, &la.sa, &lalen) < 0 || lalen <= 0 ||
82458951Seric 	    la.sa.sa_family != AF_INET)
82558951Seric 	{
82658951Seric 		/* no ident info */
82758951Seric 		goto noident;
82858951Seric 	}
82958951Seric 
83058951Seric 	/* create ident query */
83158951Seric 	(void) sprintf(hbuf, "%d,%d\r\n", fa.sin.sin_port, la.sin.sin_port);
83258951Seric 
83358951Seric 	/* create local address */
83458951Seric 	bzero(&la, sizeof la);
83558951Seric 
83658951Seric 	/* create foreign address */
83758951Seric 	sp = getservbyname("auth", "tcp");
83858951Seric 	if (sp != NULL)
83958951Seric 		fa.sin.sin_port = sp->s_port;
84058308Seric 	else
84159097Seric 		fa.sin.sin_port = htons(113);
84258951Seric 
84358951Seric 	s = -1;
84458951Seric 	if (setjmp(CtxAuthTimeout) != 0)
84558951Seric 	{
84658951Seric 		if (s >= 0)
84758951Seric 			(void) close(s);
84858951Seric 		goto noident;
84958951Seric 	}
85058951Seric 
85158951Seric 	/* put a timeout around the whole thing */
85258951Seric 	ev = setevent((time_t) 30, authtimeout, 0);
85358951Seric 
85458951Seric 	/* connect to foreign IDENT server */
85558951Seric 	s = socket(AF_INET, SOCK_STREAM, 0);
85658951Seric 	if (s < 0)
85758951Seric 	{
85858951Seric 		clrevent(ev);
85958951Seric 		goto noident;
86058951Seric 	}
86158951Seric 	if (connect(s, &fa.sa, sizeof fa.sin) < 0)
86258951Seric 	{
86358951Seric closeident:
86458951Seric 		(void) close(s);
86558951Seric 		clrevent(ev);
86658951Seric 		goto noident;
86758951Seric 	}
86858951Seric 
86958957Seric 	if (tTd(9, 10))
87058951Seric 		printf("getauthinfo: sent %s", hbuf);
87158951Seric 
87258951Seric 	/* send query */
87358951Seric 	if (write(s, hbuf, strlen(hbuf)) < 0)
87458951Seric 		goto closeident;
87558951Seric 
87658951Seric 	/* get result */
87758951Seric 	i = read(s, hbuf, sizeof hbuf);
87858951Seric 	(void) close(s);
87958951Seric 	clrevent(ev);
88058951Seric 	if (i <= 0)
88158951Seric 		goto noident;
88258951Seric 	if (hbuf[--i] == '\n' && hbuf[--i] == '\r')
88358951Seric 		i--;
88458951Seric 	hbuf[++i] = '\0';
88558951Seric 
88658957Seric 	if (tTd(9, 3))
88758951Seric 		printf("getauthinfo:  got %s\n", hbuf);
88858951Seric 
88958951Seric 	/* parse result */
89058951Seric 	p = strchr(hbuf, ':');
89158951Seric 	if (p == NULL)
89258951Seric 	{
89358951Seric 		/* malformed response */
89458951Seric 		goto noident;
89558951Seric 	}
89658951Seric 	while (isascii(*++p) && isspace(*p))
89758951Seric 		continue;
89858951Seric 	if (strncasecmp(p, "userid", 6) != 0)
89958951Seric 	{
90058951Seric 		/* presumably an error string */
90158951Seric 		goto noident;
90258951Seric 	}
90358951Seric 	p += 6;
90458951Seric 	while (isascii(*p) && isspace(*p))
90558951Seric 		p++;
90658951Seric 	if (*p++ != ':')
90758951Seric 	{
90858951Seric 		/* either useridxx or malformed response */
90958951Seric 		goto noident;
91058951Seric 	}
91158951Seric 
91258951Seric 	/* p now points to the OSTYPE field */
91358951Seric 	p = strchr(p, ':');
91458951Seric 	if (p == NULL)
91558951Seric 	{
91658951Seric 		/* malformed response */
91758951Seric 		goto noident;
91858951Seric 	}
91958951Seric 
92058957Seric 	/* 1413 says don't do this -- but it's broken otherwise */
92158957Seric 	while (isascii(*++p) && isspace(*p))
92258957Seric 		continue;
92358957Seric 
92458951Seric 	/* p now points to the authenticated name */
92558951Seric 	(void) sprintf(hbuf, "%s@%s", p, RealHostName);
92658957Seric 	goto finish;
92758957Seric 
92858957Seric #endif /* IDENTPROTO */
92958957Seric 
93058957Seric noident:
93158957Seric 	(void) strcpy(hbuf, RealHostName);
93258957Seric 
93358957Seric finish:
93458951Seric 	if (RealHostName[0] != '[')
93558951Seric 	{
93658951Seric 		p = &hbuf[strlen(hbuf)];
93758951Seric 		(void) sprintf(p, " [%s]", anynet_ntoa(&RealHostAddr));
93858951Seric 	}
93958957Seric 	if (tTd(9, 1))
94058951Seric 		printf("getauthinfo: %s\n", hbuf);
94158308Seric 	return hbuf;
94258308Seric }
94358308Seric /*
944*60089Seric **  HOST_MAP_LOOKUP -- turn a hostname into canonical form
94553751Seric **
94653751Seric **	Parameters:
94756823Seric **		map -- a pointer to this map (unused).
948*60089Seric **		name -- the (presumably unqualified) hostname.
94955019Seric **		avp -- unused -- for compatibility with other mapping
95055019Seric **			functions.
95159084Seric **		statp -- an exit status (out parameter) -- set to
95259084Seric **			EX_TEMPFAIL if the name server is unavailable.
95353751Seric **
95453751Seric **	Returns:
95553751Seric **		The mapping, if found.
95653751Seric **		NULL if no mapping found.
95753751Seric **
95853751Seric **	Side Effects:
95953751Seric **		Looks up the host specified in hbuf.  If it is not
96053751Seric **		the canonical name for that host, return the canonical
96153751Seric **		name.
96253751Seric */
96351315Seric 
96453751Seric char *
965*60089Seric host_map_lookup(map, name, avp, statp)
96656823Seric 	MAP *map;
967*60089Seric 	char *name;
96853751Seric 	char **avp;
96959084Seric 	int *statp;
97016911Seric {
97116911Seric 	register struct hostent *hp;
97233932Sbostic 	u_long in_addr;
97356823Seric 	char *cp;
97458110Seric 	int i;
97559671Seric 	register STAB *s;
976*60089Seric 	static char hbuf[MAXNAME];
97759671Seric 	extern struct hostent *gethostbyaddr();
97859671Seric 	extern int h_errno;
97916911Seric 
98025574Smiriam 	/*
98159671Seric 	**  See if we have already looked up this name.  If so, just
98259671Seric 	**  return it.
98359671Seric 	*/
98453751Seric 
985*60089Seric 	s = stab(name, ST_NAMECANON, ST_ENTER);
98659671Seric 	if (bitset(NCF_VALID, s->s_namecanon.nc_flags))
98759671Seric 	{
98859986Seric 		if (tTd(9, 1))
989*60089Seric 			printf("host_map_lookup(%s) => CACHE %s\n",
990*60089Seric 				name, s->s_namecanon.nc_cname);
99159671Seric 		errno = s->s_namecanon.nc_errno;
99259671Seric 		h_errno = s->s_namecanon.nc_herrno;
99359671Seric 		*statp = s->s_namecanon.nc_stat;
99459671Seric 		return s->s_namecanon.nc_cname;
99559671Seric 	}
99659671Seric 
99759671Seric 	/*
99859671Seric 	**  If first character is a bracket, then it is an address
99959671Seric 	**  lookup.  Address is copied into a temporary buffer to
1000*60089Seric 	**  strip the brackets and to preserve name if address is
100159671Seric 	**  unknown.
100259671Seric 	*/
100359671Seric 
1004*60089Seric 	if (*name != '[')
100553751Seric 	{
100655019Seric 		extern bool getcanonname();
100755019Seric 
100858798Seric 		if (tTd(9, 1))
1009*60089Seric 			printf("host_map_lookup(%s) => ", name);
101059671Seric 		s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
1011*60089Seric 		(void) strcpy(hbuf, name);
1012*60089Seric 		if (getcanonname(hbuf, sizeof hbuf - 1))
101358796Seric 		{
101458796Seric 			if (tTd(9, 1))
101558796Seric 				printf("%s\n", hbuf);
101659671Seric 			s->s_namecanon.nc_cname = newstr(hbuf);
101753751Seric 			return hbuf;
101858796Seric 		}
101953751Seric 		else
102058796Seric 		{
102159084Seric 			register struct hostent *hp;
102259084Seric 
102358796Seric 			if (tTd(9, 1))
102459084Seric 				printf("FAIL (%d)\n", h_errno);
102559671Seric 			s->s_namecanon.nc_errno = errno;
102659671Seric 			s->s_namecanon.nc_herrno = h_errno;
102759084Seric 			switch (h_errno)
102859084Seric 			{
102959084Seric 			  case TRY_AGAIN:
103059596Seric 				if (UseNameServer)
103159734Seric 				{
103259734Seric 					char *msg = "Recipient domain nameserver timed out";
103359734Seric 
103459734Seric 					message(msg);
103559734Seric 					if (CurEnv->e_message == NULL)
103660009Seric 						CurEnv->e_message = newstr(msg);
103759734Seric 				}
103859084Seric 				*statp = EX_TEMPFAIL;
103959084Seric 				break;
104059084Seric 
104159084Seric 			  case HOST_NOT_FOUND:
104259084Seric 				*statp = EX_NOHOST;
104359084Seric 				break;
104459084Seric 
104559084Seric 			  case NO_RECOVERY:
104659084Seric 				*statp = EX_SOFTWARE;
104759084Seric 				break;
104859084Seric 
104959084Seric 			  default:
105059084Seric 				*statp = EX_UNAVAILABLE;
105159084Seric 				break;
105259084Seric 			}
105359671Seric 			s->s_namecanon.nc_stat = *statp;
105459084Seric 			if (*statp != EX_TEMPFAIL || UseNameServer)
105559084Seric 				return NULL;
105659084Seric 
105759084Seric 			/*
105859084Seric 			**  Try to look it up in /etc/hosts
105959084Seric 			*/
106059084Seric 
1061*60089Seric 			hp = gethostbyname(name);
106259084Seric 			if (hp == NULL)
106359084Seric 			{
106459084Seric 				/* no dice there either */
106559671Seric 				s->s_namecanon.nc_stat = *statp = EX_NOHOST;
106659084Seric 				return NULL;
106759084Seric 			}
106859084Seric 
106959671Seric 			s->s_namecanon.nc_stat = *statp = EX_OK;
107059671Seric 			s->s_namecanon.nc_cname = newstr(hp->h_name);
107159084Seric 			return hp->h_name;
107258796Seric 		}
107353751Seric 	}
1074*60089Seric 	if ((cp = strchr(name, ']')) == NULL)
107553751Seric 		return (NULL);
107640994Sbostic 	*cp = '\0';
1077*60089Seric 	in_addr = inet_addr(&name[1]);
107858110Seric 
107958110Seric 	/* check to see if this is one of our addresses */
108058110Seric 	for (i = 0; MyIpAddrs[i].s_addr != 0; i++)
108158110Seric 	{
108258110Seric 		if (MyIpAddrs[i].s_addr == in_addr)
108358110Seric 		{
1084*60089Seric 			strncpy(hbuf, MyHostName, sizeof hbuf - 1);
1085*60089Seric 			hbuf[sizeof hbuf - 1] = '\0';
108658110Seric 			return hbuf;
108758110Seric 		}
108858110Seric 	}
108958110Seric 
109058110Seric 	/* nope -- ask the name server */
109133932Sbostic 	hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET);
109259671Seric 	s->s_namecanon.nc_errno = errno;
109359671Seric 	s->s_namecanon.nc_herrno = h_errno;
109459671Seric 	s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
109533932Sbostic 	if (hp == NULL)
109659671Seric 	{
109759671Seric 		s->s_namecanon.nc_stat = *statp = EX_NOHOST;
109853751Seric 		return (NULL);
109959671Seric 	}
110053751Seric 
110158110Seric 	/* found a match -- copy out */
110259671Seric 	s->s_namecanon.nc_cname = newstr(hp->h_name);
1103*60089Seric 	if (strlen(hp->h_name) > sizeof hbuf - 1)
1104*60089Seric 		hp->h_name[sizeof hbuf - 1] = '\0';
110553751Seric 	(void) strcpy(hbuf, hp->h_name);
110659671Seric 	s->s_namecanon.nc_stat = *statp = EX_OK;
110753751Seric 	return hbuf;
110833932Sbostic }
110958755Seric /*
111058755Seric **  ANYNET_NTOA -- convert a network address to printable form.
111158755Seric **
111258755Seric **	Parameters:
111358755Seric **		sap -- a pointer to a sockaddr structure.
111458755Seric **
111558755Seric **	Returns:
111658755Seric **		A printable version of that sockaddr.
111758755Seric */
111816911Seric 
111958755Seric char *
112058755Seric anynet_ntoa(sap)
112158755Seric 	register SOCKADDR *sap;
112258755Seric {
112358755Seric 	register char *bp;
112458755Seric 	register char *ap;
112558755Seric 	int l;
112658755Seric 	static char buf[80];
112758755Seric 
112858798Seric 	/* check for null/zero family */
112958798Seric 	if (sap == NULL)
113058798Seric 		return "NULLADDR";
113158798Seric 	if (sap->sa.sa_family == 0)
113258798Seric 		return "0";
113358798Seric 
113458778Seric #ifdef NETINET
113558778Seric 	if (sap->sa.sa_family == AF_INET)
113658755Seric 	{
113758755Seric 		extern char *inet_ntoa();
113858755Seric 
113958755Seric 		return inet_ntoa(((struct sockaddr_in *) sap)->sin_addr);
114058755Seric 	}
114158778Seric #endif
114258755Seric 
114358755Seric 	/* unknown family -- just dump bytes */
114458778Seric 	(void) sprintf(buf, "Family %d: ", sap->sa.sa_family);
114558755Seric 	bp = &buf[strlen(buf)];
114658778Seric 	ap = sap->sa.sa_data;
114758778Seric 	for (l = sizeof sap->sa.sa_data; --l >= 0; )
114858755Seric 	{
114958755Seric 		(void) sprintf(bp, "%02x:", *ap++ & 0377);
115058755Seric 		bp += 3;
115158755Seric 	}
115258755Seric 	*--bp = '\0';
115358755Seric 	return buf;
115458755Seric }
115558951Seric /*
115658951Seric **  HOSTNAMEBYANYADDR -- return name of host based on address
115758951Seric **
115858951Seric **	Parameters:
115958951Seric **		sap -- SOCKADDR pointer
116058951Seric **
116158951Seric **	Returns:
116258951Seric **		text representation of host name.
116358951Seric **
116458951Seric **	Side Effects:
116558951Seric **		none.
116658951Seric */
116758755Seric 
116858951Seric char *
116958951Seric hostnamebyanyaddr(sap)
117058951Seric 	register SOCKADDR *sap;
117158951Seric {
117258951Seric 	register struct hostent *hp;
117358951Seric 
117459042Seric #ifdef NAMED_BIND
117559042Seric 	int saveretry;
117659042Seric 
117759042Seric 	/* shorten name server timeout to avoid higher level timeouts */
117859042Seric 	saveretry = _res.retry;
117959042Seric 	_res.retry = 3;
118059042Seric #endif /* NAMED_BIND */
118159042Seric 
118258951Seric 	switch (sap->sa.sa_family)
118358951Seric 	{
118458951Seric #ifdef NETINET
118558951Seric 	  case AF_INET:
118658951Seric 		hp = gethostbyaddr((char *) &sap->sin.sin_addr,
118758951Seric 			sizeof sap->sin.sin_addr,
118858951Seric 			AF_INET);
118958951Seric 		break;
119058951Seric #endif
119158951Seric 
119258951Seric #ifdef NETISO
119358951Seric 	  case AF_ISO:
119458951Seric 		hp = gethostbyaddr((char *) &sap->siso.siso_addr,
119558951Seric 			sizeof sap->siso.siso_addr,
119658951Seric 			AF_ISO);
119758951Seric 		break;
119858951Seric #endif
119958951Seric 
120058951Seric 	  default:
120158951Seric 		hp = gethostbyaddr(sap->sa.sa_data,
120258951Seric 			   sizeof sap->sa.sa_data,
120358951Seric 			   sap->sa.sa_family);
120458951Seric 		break;
120558951Seric 	}
120658951Seric 
120759042Seric #ifdef NAMED_BIND
120859042Seric 	_res.retry = saveretry;
120959042Seric #endif /* NAMED_BIND */
121059042Seric 
121158951Seric 	if (hp != NULL)
121258951Seric 		return hp->h_name;
121358951Seric 	else
121458951Seric 	{
121558951Seric 		/* produce a dotted quad */
121658951Seric 		static char buf[512];
121758951Seric 
121858951Seric 		(void) sprintf(buf, "[%s]", anynet_ntoa(sap));
121958951Seric 		return buf;
122058951Seric 	}
122158951Seric }
122258951Seric 
122356795Seric # else /* DAEMON */
122416911Seric /* code for systems without sophisticated networking */
122510758Seric 
122610758Seric /*
122710758Seric **  MYHOSTNAME -- stub version for case of no daemon code.
122811297Seric **
122911297Seric **	Can't convert to upper case here because might be a UUCP name.
123012313Seric **
123112313Seric **	Mark, you can change this to be anything you want......
123210758Seric */
123310758Seric 
123410758Seric char **
123512313Seric myhostname(hostbuf, size)
123610758Seric 	char hostbuf[];
123712313Seric 	int size;
123810758Seric {
123910758Seric 	register FILE *f;
124010758Seric 
124110758Seric 	hostbuf[0] = '\0';
124210758Seric 	f = fopen("/usr/include/whoami", "r");
124310758Seric 	if (f != NULL)
124410758Seric 	{
124512313Seric 		(void) fgets(hostbuf, size, f);
124610758Seric 		fixcrlf(hostbuf, TRUE);
124710758Seric 		(void) fclose(f);
124810758Seric 	}
124910758Seric 	return (NULL);
125010758Seric }
125116911Seric /*
125258951Seric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
125358308Seric **
125458308Seric **	Parameters:
125558308Seric **		fd -- the descriptor
125658308Seric **
125758308Seric **	Returns:
125858308Seric **		The host name associated with this descriptor, if it can
125958308Seric **			be determined.
126058308Seric **		NULL otherwise.
126158308Seric **
126258308Seric **	Side Effects:
126358308Seric **		none
126458308Seric */
126558308Seric 
126658308Seric char *
126758951Seric getauthinfo(fd)
126858308Seric 	int fd;
126958308Seric {
127058308Seric 	return NULL;
127158308Seric }
127258308Seric /*
127316911Seric **  MAPHOSTNAME -- turn a hostname into canonical form
127416911Seric **
127516911Seric **	Parameters:
127656823Seric **		map -- a pointer to the database map.
1277*60089Seric **		name -- a buffer containing a hostname.
127853751Seric **		avp -- a pointer to a (cf file defined) argument vector.
127959084Seric **		statp -- an exit status (out parameter).
128016911Seric **
128116911Seric **	Returns:
128253751Seric **		mapped host name
128351315Seric **		FALSE otherwise.
128416911Seric **
128516911Seric **	Side Effects:
1286*60089Seric **		Looks up the host specified in name.  If it is not
128716911Seric **		the canonical name for that host, replace it with
128816911Seric **		the canonical name.  If the name is unknown, or it
128916911Seric **		is already the canonical name, leave it unchanged.
129016911Seric */
129110758Seric 
129216911Seric /*ARGSUSED*/
129353751Seric char *
1294*60089Seric host_map_lookup(map, name, avp, statp)
129556823Seric 	MAP *map;
1296*60089Seric 	char *name;
129753751Seric 	char **avp;
129859084Seric 	char *statp;
129916911Seric {
130059084Seric 	register struct hostent *hp;
130159084Seric 
1302*60089Seric 	hp = gethostbyname(name);
130359084Seric 	if (hp != NULL)
130459084Seric 		return hp->h_name;
130559084Seric 	*statp = EX_NOHOST;
130653751Seric 	return NULL;
130716911Seric }
130816911Seric 
130956795Seric #endif /* DAEMON */
1310