xref: /csrg-svn/usr.sbin/sendmail/src/daemon.c (revision 59783)
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*59783Seric static char sccsid[] = "@(#)daemon.c	6.45 (Berkeley) 05/07/93 (with daemon mode)";
1633780Sbostic #else
17*59783Seric static char sccsid[] = "@(#)daemon.c	6.45 (Berkeley) 05/07/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.
5856823Seric **	maphostname(map, hbuf, hbufsiz, avp)
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 */
83*59783Seric 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 */
206*59783Seric 			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;
448*59783Seric 
449*59783Seric 		  case 'L':		/* listen queue size */
450*59783Seric 			ListenQueueSize = atoi(v);
451*59783Seric 			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';
51211147Seric 			hid = inet_addr(&host[1]);
51358360Seric 			if (hid == -1)
51458360Seric 			{
51558360Seric 				/* try it as a host name (avoid MX lookup) */
51658360Seric 				hp = gethostbyname(&host[1]);
51758360Seric 				*p = ']';
51858360Seric 				goto gothostent;
51958360Seric 			}
52011147Seric 			*p = ']';
5219308Seric 		}
52258360Seric 		if (p == NULL)
5239308Seric 		{
52458151Seric 			usrerr("553 Invalid numeric domain spec \"%s\"", host);
5259308Seric 			return (EX_NOHOST);
5269308Seric 		}
52758778Seric 		addr.sin.sin_family = AF_INET;
52858778Seric 		addr.sin.sin_addr.s_addr = hid;
5299308Seric 	}
5309610Seric 	else
5319610Seric 	{
53229430Sbloom 		hp = gethostbyname(host);
53358360Seric gothostent:
53425475Smiriam 		if (hp == NULL)
53524945Seric 		{
53635651Seric #ifdef NAMED_BIND
53725475Smiriam 			if (errno == ETIMEDOUT || h_errno == TRY_AGAIN)
53825475Smiriam 				return (EX_TEMPFAIL);
53925657Seric 
54035651Seric 			/* if name server is specified, assume temp fail */
54135651Seric 			if (errno == ECONNREFUSED && UseNameServer)
54235651Seric 				return (EX_TEMPFAIL);
54335651Seric #endif
54425475Smiriam 			return (EX_NOHOST);
54524945Seric 		}
54658778Seric 		addr.sa.sa_family = hp->h_addrtype;
54758778Seric 		switch (hp->h_addrtype)
54858778Seric 		{
54958778Seric #ifdef NETINET
55058778Seric 		  case AF_INET:
55158755Seric 			bcopy(hp->h_addr,
55258778Seric 				&addr.sin.sin_addr,
55358755Seric 				hp->h_length);
55458778Seric 			break;
55558778Seric #endif
55658778Seric 
55758778Seric 		  default:
55858755Seric 			bcopy(hp->h_addr,
55958778Seric 				addr.sa.sa_data,
56058755Seric 				hp->h_length);
56158778Seric 			break;
56258778Seric 		}
56329430Sbloom 		i = 1;
5649610Seric 	}
5659610Seric 
5669610Seric 	/*
5679610Seric 	**  Determine the port number.
5689610Seric 	*/
5699610Seric 
57010011Seric 	if (port != 0)
57158755Seric 		port = htons(port);
57210011Seric 	else
5739610Seric 	{
5749610Seric 		register struct servent *sp = getservbyname("smtp", "tcp");
5759610Seric 
5769610Seric 		if (sp == NULL)
5779610Seric 		{
57858909Seric 			syserr("554 makeconnection: service \"smtp\" unknown");
57957977Seric 			return (EX_OSERR);
5809610Seric 		}
58158755Seric 		port = sp->s_port;
5829610Seric 	}
5836039Seric 
58458778Seric 	switch (addr.sa.sa_family)
58558755Seric 	{
58658755Seric 	  case AF_INET:
58758778Seric 		addr.sin.sin_port = port;
58858755Seric 		addrlen = sizeof (struct sockaddr_in);
58958755Seric 		break;
59058755Seric 
59158755Seric #ifdef NETISO
59258755Seric 	  case AF_ISO:
59358755Seric 		/* assume two byte transport selector */
59458755Seric 		bcopy((char *) &port, TSEL((struct sockaddr_iso *) &addr), 2);
59558755Seric 		addrlen = sizeof (struct sockaddr_iso);
59658755Seric 		break;
59758755Seric #endif
59858755Seric 
59958755Seric 	  default:
60058778Seric 		syserr("Can't connect to address family %d", addr.sa.sa_family);
60158755Seric 		return (EX_NOHOST);
60258755Seric 	}
60358755Seric 
6046039Seric 	/*
6056039Seric 	**  Try to actually open the connection.
6066039Seric 	*/
6076039Seric 
60859156Seric #ifdef XLA
60959156Seric 	/* if too many connections, don't bother trying */
61059156Seric 	if (!xla_noqueue_ok(host))
61159156Seric 		return EX_TEMPFAIL;
61259156Seric #endif
61359156Seric 
61457736Seric 	for (;;)
61552106Seric 	{
61657736Seric 		if (tTd(16, 1))
61758755Seric 			printf("makeconnection (%s [%s])\n",
61858755Seric 				host, anynet_ntoa(&addr));
61952106Seric 
62058588Seric 		/* save for logging */
62158588Seric 		CurHostAddr = addr;
62258588Seric 
62357736Seric 		if (usesecureport)
62457736Seric 		{
62557736Seric 			int rport = IPPORT_RESERVED - 1;
6266039Seric 
62757736Seric 			s = rresvport(&rport);
62857736Seric 		}
62957736Seric 		else
63057736Seric 		{
63157736Seric 			s = socket(AF_INET, SOCK_STREAM, 0);
63257736Seric 		}
63357736Seric 		if (s < 0)
63457736Seric 		{
63557736Seric 			sav_errno = errno;
63657736Seric 			syserr("makeconnection: no socket");
63757736Seric 			goto failure;
63857736Seric 		}
63910347Seric 
64057736Seric 		if (tTd(16, 1))
64157736Seric 			printf("makeconnection: fd=%d\n", s);
64257736Seric 
64357736Seric 		/* turn on network debugging? */
64457736Seric 		if (tTd(16, 101))
64557736Seric 		{
64657736Seric 			int on = 1;
64757736Seric 			(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG,
64857736Seric 					  (char *)&on, sizeof on);
64957736Seric 		}
65057736Seric 		if (CurEnv->e_xfp != NULL)
65157736Seric 			(void) fflush(CurEnv->e_xfp);		/* for debugging */
65257736Seric 		errno = 0;					/* for debugging */
65358755Seric 		if (connect(s, (struct sockaddr *) &addr, addrlen) >= 0)
65457736Seric 			break;
65557736Seric 
65657736Seric 		/* couldn't connect.... figure out why */
65727744Sbloom 		sav_errno = errno;
65827744Sbloom 		(void) close(s);
65929430Sbloom 		if (hp && hp->h_addr_list[i])
66029430Sbloom 		{
66158755Seric 			extern char *errstring();
66258755Seric 
66357736Seric 			if (tTd(16, 1))
66458755Seric 				printf("Connect failed (%s); trying new address....\n",
66558755Seric 					errstring(sav_errno));
66658778Seric 			switch (addr.sa.sa_family)
66758778Seric 			{
66858778Seric #ifdef NETINET
66958778Seric 			  case AF_INET:
67058755Seric 				bcopy(hp->h_addr_list[i++],
67158778Seric 				      &addr.sin.sin_addr,
67258755Seric 				      hp->h_length);
67358778Seric 				break;
67458778Seric #endif
67558778Seric 
67658778Seric 			  default:
67758755Seric 				bcopy(hp->h_addr_list[i++],
67858778Seric 					addr.sa.sa_data,
67952106Seric 					hp->h_length);
68058778Seric 				break;
68158778Seric 			}
68257736Seric 			continue;
68329430Sbloom 		}
68429430Sbloom 
6856039Seric 		/* failure, decide if temporary or not */
6866039Seric 	failure:
68759254Seric #ifdef XLA
68859254Seric 		xla_host_end(host);
68959254Seric #endif
69058542Seric 		if (transienterror(sav_errno))
69158542Seric 			return EX_TEMPFAIL;
69258542Seric 		else
69358542Seric 		{
69458542Seric 			extern char *errstring();
69511147Seric 
69658542Seric 			message("%s", errstring(sav_errno));
69758542Seric 			return (EX_UNAVAILABLE);
6986039Seric 		}
6996039Seric 	}
7006039Seric 
7016039Seric 	/* connection ok, put it into canonical form */
70253739Seric 	mci->mci_out = fdopen(s, "w");
70353739Seric 	mci->mci_in = fdopen(dup(s), "r");
7046039Seric 
70510098Seric 	return (EX_OK);
7066039Seric }
70710758Seric /*
70810758Seric **  MYHOSTNAME -- return the name of this host.
70910758Seric **
71010758Seric **	Parameters:
71110758Seric **		hostbuf -- a place to return the name of this host.
71212313Seric **		size -- the size of hostbuf.
71310758Seric **
71410758Seric **	Returns:
71510758Seric **		A list of aliases for this host.
71610758Seric **
71710758Seric **	Side Effects:
71858110Seric **		Sets the MyIpAddrs buffer to a list of my IP addresses.
71910758Seric */
7206039Seric 
72158110Seric struct in_addr	MyIpAddrs[MAXIPADDR + 1];
72258110Seric 
72310758Seric char **
72412313Seric myhostname(hostbuf, size)
72510758Seric 	char hostbuf[];
72612313Seric 	int size;
72710758Seric {
72858110Seric 	register struct hostent *hp;
72910758Seric 	extern struct hostent *gethostbyname();
73010758Seric 
73123120Seric 	if (gethostname(hostbuf, size) < 0)
73223120Seric 	{
73323120Seric 		(void) strcpy(hostbuf, "localhost");
73423120Seric 	}
73511147Seric 	hp = gethostbyname(hostbuf);
73611147Seric 	if (hp != NULL)
73716877Seric 	{
73858110Seric 		(void) strncpy(hostbuf, hp->h_name, size - 1);
73958110Seric 		hostbuf[size - 1] = '\0';
74058110Seric 
74158110Seric 		if (hp->h_addrtype == AF_INET && hp->h_length == 4)
74258110Seric 		{
74358110Seric 			register int i;
74458110Seric 
74558110Seric 			for (i = 0; i < MAXIPADDR; i++)
74658110Seric 			{
74758110Seric 				if (hp->h_addr_list[i] == NULL)
74858110Seric 					break;
74958110Seric 				MyIpAddrs[i].s_addr = *(u_long *) hp->h_addr_list[i];
75058110Seric 			}
75158110Seric 			MyIpAddrs[i].s_addr = 0;
75258110Seric 		}
75358110Seric 
75411147Seric 		return (hp->h_aliases);
75516877Seric 	}
75610758Seric 	else
75710758Seric 		return (NULL);
75810758Seric }
75951315Seric /*
76058951Seric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
76158308Seric **
76258951Seric **	Uses RFC1413 protocol to try to get info from the other end.
76358951Seric **
76458308Seric **	Parameters:
76558308Seric **		fd -- the descriptor
76658308Seric **
76758308Seric **	Returns:
76858951Seric **		The user@host information associated with this descriptor.
76958308Seric **
77058308Seric **	Side Effects:
77158951Seric **		Sets RealHostName to the name of the host at the other end.
77258308Seric */
77358308Seric 
77458951Seric #ifdef IDENTPROTO
77558951Seric 
77658951Seric static jmp_buf	CtxAuthTimeout;
77758951Seric 
77858951Seric static
77958951Seric authtimeout()
78058951Seric {
78158951Seric 	longjmp(CtxAuthTimeout, 1);
78258951Seric }
78358951Seric 
78458951Seric #endif
78558951Seric 
78658308Seric char *
78758951Seric getauthinfo(fd)
78858308Seric 	int fd;
78958308Seric {
79058951Seric 	SOCKADDR fa;
79158951Seric 	int falen;
79259104Seric 	register char *p;
79358951Seric #ifdef IDENTPROTO
79458951Seric 	SOCKADDR la;
79558951Seric 	int lalen;
79658951Seric 	register struct servent *sp;
79758951Seric 	int s;
79858951Seric 	int i;
79958951Seric 	EVENT *ev;
80058951Seric #endif
80158951Seric 	static char hbuf[MAXNAME * 2 + 2];
80258951Seric 	extern char *hostnamebyanyaddr();
80358951Seric 	extern char RealUserName[];			/* main.c */
80458308Seric 
80558951Seric 	falen = sizeof fa;
80658951Seric 	if (getpeername(fd, &fa.sa, &falen) < 0 || falen <= 0)
80758951Seric 	{
80858951Seric 		RealHostName = "localhost";
80958951Seric 		(void) sprintf(hbuf, "%s@localhost", RealUserName);
81058957Seric 		if (tTd(9, 1))
81158951Seric 			printf("getauthinfo: %s\n", hbuf);
81258951Seric 		return hbuf;
81358951Seric 	}
81458951Seric 
81558951Seric 	RealHostName = newstr(hostnamebyanyaddr(&fa));
81658951Seric 	RealHostAddr = fa;
81758951Seric 
81858951Seric #ifdef IDENTPROTO
81958951Seric 	lalen = sizeof la;
82058951Seric 	if (fa.sa.sa_family != AF_INET ||
82158951Seric 	    getsockname(fd, &la.sa, &lalen) < 0 || lalen <= 0 ||
82258951Seric 	    la.sa.sa_family != AF_INET)
82358951Seric 	{
82458951Seric 		/* no ident info */
82558951Seric 		goto noident;
82658951Seric 	}
82758951Seric 
82858951Seric 	/* create ident query */
82958951Seric 	(void) sprintf(hbuf, "%d,%d\r\n", fa.sin.sin_port, la.sin.sin_port);
83058951Seric 
83158951Seric 	/* create local address */
83258951Seric 	bzero(&la, sizeof la);
83358951Seric 
83458951Seric 	/* create foreign address */
83558951Seric 	sp = getservbyname("auth", "tcp");
83658951Seric 	if (sp != NULL)
83758951Seric 		fa.sin.sin_port = sp->s_port;
83858308Seric 	else
83959097Seric 		fa.sin.sin_port = htons(113);
84058951Seric 
84158951Seric 	s = -1;
84258951Seric 	if (setjmp(CtxAuthTimeout) != 0)
84358951Seric 	{
84458951Seric 		if (s >= 0)
84558951Seric 			(void) close(s);
84658951Seric 		goto noident;
84758951Seric 	}
84858951Seric 
84958951Seric 	/* put a timeout around the whole thing */
85058951Seric 	ev = setevent((time_t) 30, authtimeout, 0);
85158951Seric 
85258951Seric 	/* connect to foreign IDENT server */
85358951Seric 	s = socket(AF_INET, SOCK_STREAM, 0);
85458951Seric 	if (s < 0)
85558951Seric 	{
85658951Seric 		clrevent(ev);
85758951Seric 		goto noident;
85858951Seric 	}
85958951Seric 	if (connect(s, &fa.sa, sizeof fa.sin) < 0)
86058951Seric 	{
86158951Seric closeident:
86258951Seric 		(void) close(s);
86358951Seric 		clrevent(ev);
86458951Seric 		goto noident;
86558951Seric 	}
86658951Seric 
86758957Seric 	if (tTd(9, 10))
86858951Seric 		printf("getauthinfo: sent %s", hbuf);
86958951Seric 
87058951Seric 	/* send query */
87158951Seric 	if (write(s, hbuf, strlen(hbuf)) < 0)
87258951Seric 		goto closeident;
87358951Seric 
87458951Seric 	/* get result */
87558951Seric 	i = read(s, hbuf, sizeof hbuf);
87658951Seric 	(void) close(s);
87758951Seric 	clrevent(ev);
87858951Seric 	if (i <= 0)
87958951Seric 		goto noident;
88058951Seric 	if (hbuf[--i] == '\n' && hbuf[--i] == '\r')
88158951Seric 		i--;
88258951Seric 	hbuf[++i] = '\0';
88358951Seric 
88458957Seric 	if (tTd(9, 3))
88558951Seric 		printf("getauthinfo:  got %s\n", hbuf);
88658951Seric 
88758951Seric 	/* parse result */
88858951Seric 	p = strchr(hbuf, ':');
88958951Seric 	if (p == NULL)
89058951Seric 	{
89158951Seric 		/* malformed response */
89258951Seric 		goto noident;
89358951Seric 	}
89458951Seric 	while (isascii(*++p) && isspace(*p))
89558951Seric 		continue;
89658951Seric 	if (strncasecmp(p, "userid", 6) != 0)
89758951Seric 	{
89858951Seric 		/* presumably an error string */
89958951Seric 		goto noident;
90058951Seric 	}
90158951Seric 	p += 6;
90258951Seric 	while (isascii(*p) && isspace(*p))
90358951Seric 		p++;
90458951Seric 	if (*p++ != ':')
90558951Seric 	{
90658951Seric 		/* either useridxx or malformed response */
90758951Seric 		goto noident;
90858951Seric 	}
90958951Seric 
91058951Seric 	/* p now points to the OSTYPE field */
91158951Seric 	p = strchr(p, ':');
91258951Seric 	if (p == NULL)
91358951Seric 	{
91458951Seric 		/* malformed response */
91558951Seric 		goto noident;
91658951Seric 	}
91758951Seric 
91858957Seric 	/* 1413 says don't do this -- but it's broken otherwise */
91958957Seric 	while (isascii(*++p) && isspace(*p))
92058957Seric 		continue;
92158957Seric 
92258951Seric 	/* p now points to the authenticated name */
92358951Seric 	(void) sprintf(hbuf, "%s@%s", p, RealHostName);
92458957Seric 	goto finish;
92558957Seric 
92658957Seric #endif /* IDENTPROTO */
92758957Seric 
92858957Seric noident:
92958957Seric 	(void) strcpy(hbuf, RealHostName);
93058957Seric 
93158957Seric finish:
93258951Seric 	if (RealHostName[0] != '[')
93358951Seric 	{
93458951Seric 		p = &hbuf[strlen(hbuf)];
93558951Seric 		(void) sprintf(p, " [%s]", anynet_ntoa(&RealHostAddr));
93658951Seric 	}
93758957Seric 	if (tTd(9, 1))
93858951Seric 		printf("getauthinfo: %s\n", hbuf);
93958308Seric 	return hbuf;
94058308Seric }
94158308Seric /*
94253751Seric **  MAPHOSTNAME -- turn a hostname into canonical form
94353751Seric **
94453751Seric **	Parameters:
94556823Seric **		map -- a pointer to this map (unused).
94653751Seric **		hbuf -- a buffer containing a hostname.
94753751Seric **		hbsize -- the size of hbuf.
94855019Seric **		avp -- unused -- for compatibility with other mapping
94955019Seric **			functions.
95059084Seric **		statp -- an exit status (out parameter) -- set to
95159084Seric **			EX_TEMPFAIL if the name server is unavailable.
95253751Seric **
95353751Seric **	Returns:
95453751Seric **		The mapping, if found.
95553751Seric **		NULL if no mapping found.
95653751Seric **
95753751Seric **	Side Effects:
95853751Seric **		Looks up the host specified in hbuf.  If it is not
95953751Seric **		the canonical name for that host, return the canonical
96053751Seric **		name.
96153751Seric */
96251315Seric 
96353751Seric char *
96459084Seric maphostname(map, hbuf, hbsize, avp, statp)
96556823Seric 	MAP *map;
96616911Seric 	char *hbuf;
96716911Seric 	int hbsize;
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;
97659671Seric 	extern struct hostent *gethostbyaddr();
97759671Seric 	extern int h_errno;
97816911Seric 
97956836Seric 	/* allow room for null */
98056823Seric 	hbsize--;
98153751Seric 
98225574Smiriam 	/*
98359671Seric 	**  See if we have already looked up this name.  If so, just
98459671Seric 	**  return it.
98559671Seric 	*/
98653751Seric 
98759671Seric 	s = stab(hbuf, ST_NAMECANON, ST_ENTER);
98859671Seric 	if (bitset(NCF_VALID, s->s_namecanon.nc_flags))
98959671Seric 	{
99059671Seric 		errno = s->s_namecanon.nc_errno;
99159671Seric 		h_errno = s->s_namecanon.nc_herrno;
99259671Seric 		*statp = s->s_namecanon.nc_stat;
99359671Seric 		return s->s_namecanon.nc_cname;
99459671Seric 	}
99559671Seric 
99659671Seric 	/*
99759671Seric 	**  If first character is a bracket, then it is an address
99859671Seric 	**  lookup.  Address is copied into a temporary buffer to
99959671Seric 	**  strip the brackets and to preserve hbuf if address is
100059671Seric 	**  unknown.
100159671Seric 	*/
100259671Seric 
100351315Seric 	if (*hbuf != '[')
100453751Seric 	{
100555019Seric 		extern bool getcanonname();
100655019Seric 
100758798Seric 		if (tTd(9, 1))
100858798Seric 			printf("maphostname(%s, %d) => ", hbuf, hbsize);
100959671Seric 		s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
101058674Seric 		if (getcanonname(hbuf, hbsize))
101158796Seric 		{
101258796Seric 			if (tTd(9, 1))
101358796Seric 				printf("%s\n", hbuf);
101459671Seric 			s->s_namecanon.nc_cname = newstr(hbuf);
101553751Seric 			return hbuf;
101658796Seric 		}
101753751Seric 		else
101858796Seric 		{
101959084Seric 			register struct hostent *hp;
102059084Seric 
102158796Seric 			if (tTd(9, 1))
102259084Seric 				printf("FAIL (%d)\n", h_errno);
102359671Seric 			s->s_namecanon.nc_errno = errno;
102459671Seric 			s->s_namecanon.nc_herrno = h_errno;
102559084Seric 			switch (h_errno)
102659084Seric 			{
102759084Seric 			  case TRY_AGAIN:
102859596Seric 				if (UseNameServer)
102959734Seric 				{
103059734Seric 					char *msg = "Recipient domain nameserver timed out";
103159734Seric 
103259734Seric 					message(msg);
103359734Seric 					if (CurEnv->e_message == NULL)
103459734Seric 						CurEnv->e_message = msg;
103559734Seric 				}
103659084Seric 				*statp = EX_TEMPFAIL;
103759084Seric 				break;
103859084Seric 
103959084Seric 			  case HOST_NOT_FOUND:
104059084Seric 				*statp = EX_NOHOST;
104159084Seric 				break;
104259084Seric 
104359084Seric 			  case NO_RECOVERY:
104459084Seric 				*statp = EX_SOFTWARE;
104559084Seric 				break;
104659084Seric 
104759084Seric 			  default:
104859084Seric 				*statp = EX_UNAVAILABLE;
104959084Seric 				break;
105059084Seric 			}
105159671Seric 			s->s_namecanon.nc_stat = *statp;
105259084Seric 			if (*statp != EX_TEMPFAIL || UseNameServer)
105359084Seric 				return NULL;
105459084Seric 
105559084Seric 			/*
105659084Seric 			**  Try to look it up in /etc/hosts
105759084Seric 			*/
105859084Seric 
105959084Seric 			hp = gethostbyname(hbuf);
106059084Seric 			if (hp == NULL)
106159084Seric 			{
106259084Seric 				/* no dice there either */
106359671Seric 				s->s_namecanon.nc_stat = *statp = EX_NOHOST;
106459084Seric 				return NULL;
106559084Seric 			}
106659084Seric 
106759671Seric 			s->s_namecanon.nc_stat = *statp = EX_OK;
106859671Seric 			s->s_namecanon.nc_cname = newstr(hp->h_name);
106959084Seric 			return hp->h_name;
107058796Seric 		}
107153751Seric 	}
107256823Seric 	if ((cp = strchr(hbuf, ']')) == NULL)
107353751Seric 		return (NULL);
107440994Sbostic 	*cp = '\0';
107556823Seric 	in_addr = inet_addr(&hbuf[1]);
107658110Seric 
107758110Seric 	/* check to see if this is one of our addresses */
107858110Seric 	for (i = 0; MyIpAddrs[i].s_addr != 0; i++)
107958110Seric 	{
108058110Seric 		if (MyIpAddrs[i].s_addr == in_addr)
108158110Seric 		{
108258110Seric 			strncpy(hbuf, MyHostName, hbsize);
108358110Seric 			hbuf[hbsize] = '\0';
108458110Seric 			return hbuf;
108558110Seric 		}
108658110Seric 	}
108758110Seric 
108858110Seric 	/* nope -- ask the name server */
108933932Sbostic 	hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET);
109059671Seric 	s->s_namecanon.nc_errno = errno;
109159671Seric 	s->s_namecanon.nc_herrno = h_errno;
109259671Seric 	s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
109333932Sbostic 	if (hp == NULL)
109459671Seric 	{
109559671Seric 		s->s_namecanon.nc_stat = *statp = EX_NOHOST;
109653751Seric 		return (NULL);
109759671Seric 	}
109853751Seric 
109958110Seric 	/* found a match -- copy out */
110059671Seric 	s->s_namecanon.nc_cname = newstr(hp->h_name);
110156823Seric 	if (strlen(hp->h_name) > hbsize)
110256823Seric 		hp->h_name[hbsize] = '\0';
110353751Seric 	(void) strcpy(hbuf, hp->h_name);
110459671Seric 	s->s_namecanon.nc_stat = *statp = EX_OK;
110553751Seric 	return hbuf;
110633932Sbostic }
110758755Seric /*
110858755Seric **  ANYNET_NTOA -- convert a network address to printable form.
110958755Seric **
111058755Seric **	Parameters:
111158755Seric **		sap -- a pointer to a sockaddr structure.
111258755Seric **
111358755Seric **	Returns:
111458755Seric **		A printable version of that sockaddr.
111558755Seric */
111616911Seric 
111758755Seric char *
111858755Seric anynet_ntoa(sap)
111958755Seric 	register SOCKADDR *sap;
112058755Seric {
112158755Seric 	register char *bp;
112258755Seric 	register char *ap;
112358755Seric 	int l;
112458755Seric 	static char buf[80];
112558755Seric 
112658798Seric 	/* check for null/zero family */
112758798Seric 	if (sap == NULL)
112858798Seric 		return "NULLADDR";
112958798Seric 	if (sap->sa.sa_family == 0)
113058798Seric 		return "0";
113158798Seric 
113258778Seric #ifdef NETINET
113358778Seric 	if (sap->sa.sa_family == AF_INET)
113458755Seric 	{
113558755Seric 		extern char *inet_ntoa();
113658755Seric 
113758755Seric 		return inet_ntoa(((struct sockaddr_in *) sap)->sin_addr);
113858755Seric 	}
113958778Seric #endif
114058755Seric 
114158755Seric 	/* unknown family -- just dump bytes */
114258778Seric 	(void) sprintf(buf, "Family %d: ", sap->sa.sa_family);
114358755Seric 	bp = &buf[strlen(buf)];
114458778Seric 	ap = sap->sa.sa_data;
114558778Seric 	for (l = sizeof sap->sa.sa_data; --l >= 0; )
114658755Seric 	{
114758755Seric 		(void) sprintf(bp, "%02x:", *ap++ & 0377);
114858755Seric 		bp += 3;
114958755Seric 	}
115058755Seric 	*--bp = '\0';
115158755Seric 	return buf;
115258755Seric }
115358951Seric /*
115458951Seric **  HOSTNAMEBYANYADDR -- return name of host based on address
115558951Seric **
115658951Seric **	Parameters:
115758951Seric **		sap -- SOCKADDR pointer
115858951Seric **
115958951Seric **	Returns:
116058951Seric **		text representation of host name.
116158951Seric **
116258951Seric **	Side Effects:
116358951Seric **		none.
116458951Seric */
116558755Seric 
116658951Seric char *
116758951Seric hostnamebyanyaddr(sap)
116858951Seric 	register SOCKADDR *sap;
116958951Seric {
117058951Seric 	register struct hostent *hp;
117158951Seric 
117259042Seric #ifdef NAMED_BIND
117359042Seric 	int saveretry;
117459042Seric 
117559042Seric 	/* shorten name server timeout to avoid higher level timeouts */
117659042Seric 	saveretry = _res.retry;
117759042Seric 	_res.retry = 3;
117859042Seric #endif /* NAMED_BIND */
117959042Seric 
118058951Seric 	switch (sap->sa.sa_family)
118158951Seric 	{
118258951Seric #ifdef NETINET
118358951Seric 	  case AF_INET:
118458951Seric 		hp = gethostbyaddr((char *) &sap->sin.sin_addr,
118558951Seric 			sizeof sap->sin.sin_addr,
118658951Seric 			AF_INET);
118758951Seric 		break;
118858951Seric #endif
118958951Seric 
119058951Seric #ifdef NETISO
119158951Seric 	  case AF_ISO:
119258951Seric 		hp = gethostbyaddr((char *) &sap->siso.siso_addr,
119358951Seric 			sizeof sap->siso.siso_addr,
119458951Seric 			AF_ISO);
119558951Seric 		break;
119658951Seric #endif
119758951Seric 
119858951Seric 	  default:
119958951Seric 		hp = gethostbyaddr(sap->sa.sa_data,
120058951Seric 			   sizeof sap->sa.sa_data,
120158951Seric 			   sap->sa.sa_family);
120258951Seric 		break;
120358951Seric 	}
120458951Seric 
120559042Seric #ifdef NAMED_BIND
120659042Seric 	_res.retry = saveretry;
120759042Seric #endif /* NAMED_BIND */
120859042Seric 
120958951Seric 	if (hp != NULL)
121058951Seric 		return hp->h_name;
121158951Seric 	else
121258951Seric 	{
121358951Seric 		/* produce a dotted quad */
121458951Seric 		static char buf[512];
121558951Seric 
121658951Seric 		(void) sprintf(buf, "[%s]", anynet_ntoa(sap));
121758951Seric 		return buf;
121858951Seric 	}
121958951Seric }
122058951Seric 
122156795Seric # else /* DAEMON */
122216911Seric /* code for systems without sophisticated networking */
122310758Seric 
122410758Seric /*
122510758Seric **  MYHOSTNAME -- stub version for case of no daemon code.
122611297Seric **
122711297Seric **	Can't convert to upper case here because might be a UUCP name.
122812313Seric **
122912313Seric **	Mark, you can change this to be anything you want......
123010758Seric */
123110758Seric 
123210758Seric char **
123312313Seric myhostname(hostbuf, size)
123410758Seric 	char hostbuf[];
123512313Seric 	int size;
123610758Seric {
123710758Seric 	register FILE *f;
123810758Seric 
123910758Seric 	hostbuf[0] = '\0';
124010758Seric 	f = fopen("/usr/include/whoami", "r");
124110758Seric 	if (f != NULL)
124210758Seric 	{
124312313Seric 		(void) fgets(hostbuf, size, f);
124410758Seric 		fixcrlf(hostbuf, TRUE);
124510758Seric 		(void) fclose(f);
124610758Seric 	}
124710758Seric 	return (NULL);
124810758Seric }
124916911Seric /*
125058951Seric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
125158308Seric **
125258308Seric **	Parameters:
125358308Seric **		fd -- the descriptor
125458308Seric **
125558308Seric **	Returns:
125658308Seric **		The host name associated with this descriptor, if it can
125758308Seric **			be determined.
125858308Seric **		NULL otherwise.
125958308Seric **
126058308Seric **	Side Effects:
126158308Seric **		none
126258308Seric */
126358308Seric 
126458308Seric char *
126558951Seric getauthinfo(fd)
126658308Seric 	int fd;
126758308Seric {
126858308Seric 	return NULL;
126958308Seric }
127058308Seric /*
127116911Seric **  MAPHOSTNAME -- turn a hostname into canonical form
127216911Seric **
127316911Seric **	Parameters:
127456823Seric **		map -- a pointer to the database map.
127516911Seric **		hbuf -- a buffer containing a hostname.
127659084Seric **		hbsize -- size of hbuf.
127753751Seric **		avp -- a pointer to a (cf file defined) argument vector.
127859084Seric **		statp -- an exit status (out parameter).
127916911Seric **
128016911Seric **	Returns:
128153751Seric **		mapped host name
128251315Seric **		FALSE otherwise.
128316911Seric **
128416911Seric **	Side Effects:
128516911Seric **		Looks up the host specified in hbuf.  If it is not
128616911Seric **		the canonical name for that host, replace it with
128716911Seric **		the canonical name.  If the name is unknown, or it
128816911Seric **		is already the canonical name, leave it unchanged.
128916911Seric */
129010758Seric 
129116911Seric /*ARGSUSED*/
129253751Seric char *
129359084Seric maphostname(map, hbuf, hbsize, avp, statp)
129456823Seric 	MAP *map;
129516911Seric 	char *hbuf;
129616911Seric 	int hbsize;
129753751Seric 	char **avp;
129859084Seric 	char *statp;
129916911Seric {
130059084Seric 	register struct hostent *hp;
130159084Seric 
130259084Seric 	hp = gethostbyname(hbuf);
130359084Seric 	if (hp != NULL)
130459084Seric 		return hp->h_name;
130559084Seric 	*statp = EX_NOHOST;
130653751Seric 	return NULL;
130716911Seric }
130816911Seric 
130956795Seric #endif /* DAEMON */
1310