xref: /csrg-svn/usr.sbin/sendmail/src/daemon.c (revision 64366)
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*64366Seric static char sccsid[] = "@(#)daemon.c	8.11 (Berkeley) 08/27/93 (with daemon mode)";
1533780Sbostic #else
16*64366Seric static char sccsid[] = "@(#)daemon.c	8.11 (Berkeley) 08/27/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>
2564338Seric # include <arpa/inet.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.
5860089Seric **	host_map_lookup(map, hbuf, avp, pstat)
5956823Seric **		Convert the entry in hbuf into a canonical form.
604535Seric */
614535Seric /*
624535Seric **  GETREQUESTS -- open mail IPC port and get requests.
634535Seric **
644535Seric **	Parameters:
654535Seric **		none.
664535Seric **
674535Seric **	Returns:
684535Seric **		none.
694535Seric **
704535Seric **	Side Effects:
714535Seric **		Waits until some interesting activity occurs.  When
724535Seric **		it does, a child is created to process it, and the
734535Seric **		parent waits for completion.  Return from this
749886Seric **		routine is always in the child.  The file pointers
759886Seric **		"InChannel" and "OutChannel" should be set to point
769886Seric **		to the communication channel.
774535Seric */
784535Seric 
7958849Seric int		DaemonSocket	= -1;		/* fd describing socket */
8058849Seric SOCKADDR	DaemonAddr;			/* socket for incoming */
8159783Seric int		ListenQueueSize = 10;		/* size of listen queue */
8216144Seric 
834535Seric getrequests()
844535Seric {
859610Seric 	int t;
869610Seric 	register struct servent *sp;
8725027Seric 	int on = 1;
8853751Seric 	bool refusingconnections = TRUE;
8958419Seric 	FILE *pidf;
9046928Sbostic 	extern void reapchild();
917117Seric 
929610Seric 	/*
939610Seric 	**  Set up the address for the mailer.
949610Seric 	*/
959610Seric 
9658849Seric 	if (DaemonAddr.sin.sin_family == 0)
9758849Seric 		DaemonAddr.sin.sin_family = AF_INET;
9858849Seric 	if (DaemonAddr.sin.sin_addr.s_addr == 0)
9958849Seric 		DaemonAddr.sin.sin_addr.s_addr = INADDR_ANY;
10058849Seric 	if (DaemonAddr.sin.sin_port == 0)
1019610Seric 	{
10258849Seric 		sp = getservbyname("smtp", "tcp");
10358849Seric 		if (sp == NULL)
10458849Seric 		{
10558909Seric 			syserr("554 service \"smtp\" unknown");
10658849Seric 			goto severe;
10758849Seric 		}
10858849Seric 		DaemonAddr.sin.sin_port = sp->s_port;
1099610Seric 	}
1109610Seric 
1119610Seric 	/*
1129610Seric 	**  Try to actually open the connection.
1139610Seric 	*/
1149610Seric 
1159610Seric 	if (tTd(15, 1))
11658849Seric 		printf("getrequests: port 0x%x\n", DaemonAddr.sin.sin_port);
1179610Seric 
1189610Seric 	/* get a socket for the SMTP connection */
11959041Seric 	DaemonSocket = socket(DaemonAddr.sa.sa_family, SOCK_STREAM, 0);
12010206Seric 	if (DaemonSocket < 0)
1219610Seric 	{
1229610Seric 		/* probably another daemon already */
1239610Seric 		syserr("getrequests: can't create socket");
1249610Seric 	  severe:
1259610Seric # ifdef LOG
1269610Seric 		if (LogLevel > 0)
12757663Seric 			syslog(LOG_ALERT, "problem creating SMTP socket");
12856795Seric # endif /* LOG */
1299610Seric 		finis();
1309610Seric 	}
13110347Seric 
13210347Seric 	/* turn on network debugging? */
13356328Seric 	if (tTd(15, 101))
13424945Seric 		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on);
13510347Seric 
13625027Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on);
13725027Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on);
13825027Seric 
13959041Seric 	switch (DaemonAddr.sa.sa_family)
1409610Seric 	{
14159041Seric # ifdef NETINET
14259041Seric 	  case AF_INET:
14359041Seric 		t = sizeof DaemonAddr.sin;
14459041Seric 		break;
14559041Seric # endif
14659041Seric 
14759041Seric # ifdef NETISO
14859041Seric 	  case AF_ISO:
14959041Seric 		t = sizeof DaemonAddr.siso;
15059041Seric 		break;
15159041Seric # endif
15259041Seric 
15359041Seric 	  default:
15459041Seric 		t = sizeof DaemonAddr;
15559041Seric 		break;
15659041Seric 	}
15759041Seric 
15859041Seric 	if (bind(DaemonSocket, &DaemonAddr.sa, t) < 0)
15959041Seric 	{
1609610Seric 		syserr("getrequests: cannot bind");
16110206Seric 		(void) close(DaemonSocket);
1629610Seric 		goto severe;
1639610Seric 	}
1649610Seric 
16564035Seric 	(void) setsignal(SIGCHLD, reapchild);
16624945Seric 
16758419Seric 	/* write the pid to the log file for posterity */
16858419Seric 	pidf = fopen(PidFile, "w");
16958419Seric 	if (pidf != NULL)
17058419Seric 	{
17163863Seric 		extern char *CommandLineArgs;
17263863Seric 
17363863Seric 		/* write the process id on line 1 */
17458419Seric 		fprintf(pidf, "%d\n", getpid());
17563863Seric 
17663863Seric 		/* line 2 contains all command line flags */
17763863Seric 		fprintf(pidf, "%s\n", CommandLineArgs);
17863863Seric 
17963863Seric 		/* flush and close */
18058419Seric 		fclose(pidf);
18158419Seric 	}
18258419Seric 
18358419Seric 
1849610Seric 	if (tTd(15, 1))
18510206Seric 		printf("getrequests: %d\n", DaemonSocket);
1869610Seric 
1874631Seric 	for (;;)
1884631Seric 	{
18914875Seric 		register int pid;
19011147Seric 		auto int lotherend;
19153751Seric 		extern bool refuseconnections();
19211147Seric 
19314875Seric 		/* see if we are rejecting connections */
19453751Seric 		CurrentLA = getla();
19553751Seric 		if (refuseconnections())
19636584Sbostic 		{
19753751Seric 			if (!refusingconnections)
19853751Seric 			{
19953751Seric 				/* don't queue so peer will fail quickly */
20053751Seric 				(void) listen(DaemonSocket, 0);
20153751Seric 				refusingconnections = TRUE;
20253751Seric 			}
20357385Seric 			setproctitle("rejecting connections: load average: %d",
20457385Seric 				CurrentLA);
20514875Seric 			sleep(5);
20653751Seric 			continue;
20736584Sbostic 		}
20814875Seric 
20953751Seric 		if (refusingconnections)
21053751Seric 		{
21153751Seric 			/* start listening again */
21259783Seric 			if (listen(DaemonSocket, ListenQueueSize) < 0)
21353751Seric 			{
21453751Seric 				syserr("getrequests: cannot listen");
21553751Seric 				(void) close(DaemonSocket);
21653751Seric 				goto severe;
21753751Seric 			}
21853751Seric 			setproctitle("accepting connections");
21953751Seric 			refusingconnections = FALSE;
22053751Seric 		}
22153751Seric 
2229610Seric 		/* wait for a connection */
2239610Seric 		do
2249610Seric 		{
2259610Seric 			errno = 0;
22636230Skarels 			lotherend = sizeof RealHostAddr;
22746928Sbostic 			t = accept(DaemonSocket,
22846928Sbostic 			    (struct sockaddr *)&RealHostAddr, &lotherend);
2299610Seric 		} while (t < 0 && errno == EINTR);
2309610Seric 		if (t < 0)
2315978Seric 		{
2329610Seric 			syserr("getrequests: accept");
2339610Seric 			sleep(5);
2349610Seric 			continue;
2355978Seric 		}
2364631Seric 
2375978Seric 		/*
2385978Seric 		**  Create a subprocess to process the mail.
2395978Seric 		*/
2405978Seric 
2417677Seric 		if (tTd(15, 2))
2429610Seric 			printf("getrequests: forking (fd = %d)\n", t);
2435978Seric 
2444636Seric 		pid = fork();
2454636Seric 		if (pid < 0)
2464631Seric 		{
2474636Seric 			syserr("daemon: cannot fork");
2484636Seric 			sleep(10);
2499610Seric 			(void) close(t);
2504636Seric 			continue;
2514631Seric 		}
2524631Seric 
2534636Seric 		if (pid == 0)
2544631Seric 		{
25564086Seric 			char *p;
25658951Seric 			extern char *hostnamebyanyaddr();
25711147Seric 
2584636Seric 			/*
2594636Seric 			**  CHILD -- return to caller.
26011147Seric 			**	Collect verified idea of sending host.
2614636Seric 			**	Verify calling user id if possible here.
2624636Seric 			*/
2634631Seric 
26464035Seric 			(void) setsignal(SIGCHLD, SIG_DFL);
26559156Seric 			OpMode = MD_SMTP;
26624950Seric 
26711147Seric 			/* determine host name */
26864086Seric 			p = hostnamebyanyaddr(&RealHostAddr);
26964086Seric 			RealHostName = newstr(p);
27058778Seric 
27155173Seric #ifdef LOG
27263842Seric 			if (LogLevel > 11)
27355173Seric 			{
27455173Seric 				/* log connection information */
27555173Seric 				syslog(LOG_INFO, "connect from %s (%s)",
27658951Seric 					RealHostName, anynet_ntoa(&RealHostAddr));
27755173Seric 			}
27855173Seric #endif
27955173Seric 
28059254Seric 			(void) close(DaemonSocket);
28159254Seric 			InChannel = fdopen(t, "r");
28259254Seric 			OutChannel = fdopen(dup(t), "w");
28359254Seric 
28416884Seric 			/* should we check for illegal connection here? XXX */
28559156Seric #ifdef XLA
28659156Seric 			if (!xla_host_ok(RealHostName))
28759156Seric 			{
28859254Seric 				message("421 Too many SMTP sessions for this host");
28959156Seric 				exit(0);
29059156Seric 			}
29159156Seric #endif
29216884Seric 
2937677Seric 			if (tTd(15, 2))
2945978Seric 				printf("getreq: returning\n");
2954636Seric 			return;
2964631Seric 		}
2974631Seric 
2987117Seric 		/* close the port so that others will hang (for a while) */
2999610Seric 		(void) close(t);
3004631Seric 	}
3019886Seric 	/*NOTREACHED*/
3024631Seric }
3035978Seric /*
30410206Seric **  CLRDAEMON -- reset the daemon connection
30510206Seric **
30610206Seric **	Parameters:
30710206Seric **		none.
30810206Seric **
30910206Seric **	Returns:
31010206Seric **		none.
31110206Seric **
31210206Seric **	Side Effects:
31310206Seric **		releases any resources used by the passive daemon.
31410206Seric */
31510206Seric 
31610206Seric clrdaemon()
31710206Seric {
31810206Seric 	if (DaemonSocket >= 0)
31910206Seric 		(void) close(DaemonSocket);
32010206Seric 	DaemonSocket = -1;
32110206Seric }
32210206Seric /*
32358849Seric **  SETDAEMONOPTIONS -- set options for running the daemon
32458849Seric **
32558849Seric **	Parameters:
32658849Seric **		p -- the options line.
32758849Seric **
32858849Seric **	Returns:
32958849Seric **		none.
33058849Seric */
33158849Seric 
33258849Seric setdaemonoptions(p)
33358849Seric 	register char *p;
33458849Seric {
33558873Seric 	if (DaemonAddr.sa.sa_family == AF_UNSPEC)
33658873Seric 		DaemonAddr.sa.sa_family = AF_INET;
33758873Seric 
33858849Seric 	while (p != NULL)
33958849Seric 	{
34058849Seric 		register char *f;
34158849Seric 		register char *v;
34258849Seric 
34358849Seric 		while (isascii(*p) && isspace(*p))
34458849Seric 			p++;
34558849Seric 		if (*p == '\0')
34658849Seric 			break;
34758849Seric 		f = p;
34858849Seric 		p = strchr(p, ',');
34958849Seric 		if (p != NULL)
35058849Seric 			*p++ = '\0';
35158849Seric 		v = strchr(f, '=');
35258849Seric 		if (v == NULL)
35358849Seric 			continue;
35458849Seric 		while (isascii(*++v) && isspace(*v))
35558849Seric 			continue;
35658849Seric 
35758849Seric 		switch (*f)
35858849Seric 		{
35958873Seric 		  case 'F':		/* address family */
36058849Seric 			if (isascii(*v) && isdigit(*v))
36158873Seric 				DaemonAddr.sa.sa_family = atoi(v);
36258873Seric #ifdef NETINET
36358873Seric 			else if (strcasecmp(v, "inet") == 0)
36458873Seric 				DaemonAddr.sa.sa_family = AF_INET;
36558873Seric #endif
36658873Seric #ifdef NETISO
36758873Seric 			else if (strcasecmp(v, "iso") == 0)
36858873Seric 				DaemonAddr.sa.sa_family = AF_ISO;
36958873Seric #endif
37058873Seric #ifdef NETNS
37158873Seric 			else if (strcasecmp(v, "ns") == 0)
37258873Seric 				DaemonAddr.sa.sa_family = AF_NS;
37358873Seric #endif
37458873Seric #ifdef NETX25
37558873Seric 			else if (strcasecmp(v, "x.25") == 0)
37658873Seric 				DaemonAddr.sa.sa_family = AF_CCITT;
37758873Seric #endif
37858849Seric 			else
37958873Seric 				syserr("554 Unknown address family %s in Family=option", v);
38058873Seric 			break;
38158873Seric 
38258873Seric 		  case 'A':		/* address */
38358873Seric 			switch (DaemonAddr.sa.sa_family)
38458849Seric 			{
38558873Seric #ifdef NETINET
38658873Seric 			  case AF_INET:
38758873Seric 				if (isascii(*v) && isdigit(*v))
38858873Seric 					DaemonAddr.sin.sin_addr.s_addr = inet_network(v);
38958873Seric 				else
39058873Seric 				{
39158873Seric 					register struct netent *np;
39258849Seric 
39358873Seric 					np = getnetbyname(v);
39458873Seric 					if (np == NULL)
39558873Seric 						syserr("554 network \"%s\" unknown", v);
39658873Seric 					else
39758873Seric 						DaemonAddr.sin.sin_addr.s_addr = np->n_net;
39858873Seric 				}
39958873Seric 				break;
40058873Seric #endif
40158873Seric 
40258873Seric 			  default:
40358873Seric 				syserr("554 Address= option unsupported for family %d",
40458873Seric 					DaemonAddr.sa.sa_family);
40558873Seric 				break;
40658849Seric 			}
40758849Seric 			break;
40858849Seric 
40958873Seric 		  case 'P':		/* port */
41058873Seric 			switch (DaemonAddr.sa.sa_family)
41158849Seric 			{
41258873Seric 				short port;
41358849Seric 
41458873Seric #ifdef NETINET
41558873Seric 			  case AF_INET:
41658873Seric 				if (isascii(*v) && isdigit(*v))
417*64366Seric 					DaemonAddr.sin.sin_port = htons(atoi(v));
41858849Seric 				else
41958873Seric 				{
42058873Seric 					register struct servent *sp;
42158873Seric 
42258873Seric 					sp = getservbyname(v, "tcp");
42358873Seric 					if (sp == NULL)
42458909Seric 						syserr("554 service \"%s\" unknown", v);
42558873Seric 					else
42658873Seric 						DaemonAddr.sin.sin_port = sp->s_port;
42758873Seric 				}
42858873Seric 				break;
42958873Seric #endif
43058873Seric 
43158873Seric #ifdef NETISO
43258873Seric 			  case AF_ISO:
43358873Seric 				/* assume two byte transport selector */
43458873Seric 				if (isascii(*v) && isdigit(*v))
435*64366Seric 					port = htons(atoi(v));
43658873Seric 				else
43758873Seric 				{
43858873Seric 					register struct servent *sp;
43958873Seric 
44058873Seric 					sp = getservbyname(v, "tcp");
44158873Seric 					if (sp == NULL)
44258909Seric 						syserr("554 service \"%s\" unknown", v);
44358873Seric 					else
44458873Seric 						port = sp->s_port;
44558873Seric 				}
44658873Seric 				bcopy((char *) &port, TSEL(&DaemonAddr.siso), 2);
44758873Seric 				break;
44858873Seric #endif
44958873Seric 
45058873Seric 			  default:
45158873Seric 				syserr("554 Port= option unsupported for family %d",
45258873Seric 					DaemonAddr.sa.sa_family);
45358873Seric 				break;
45458849Seric 			}
45558849Seric 			break;
45659783Seric 
45759783Seric 		  case 'L':		/* listen queue size */
45859783Seric 			ListenQueueSize = atoi(v);
45959783Seric 			break;
46058849Seric 		}
46158849Seric 	}
46258849Seric }
46358849Seric /*
4646039Seric **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
4656039Seric **
4666039Seric **	Parameters:
4676039Seric **		host -- the name of the host.
4686633Seric **		port -- the port number to connect to.
46953739Seric **		mci -- a pointer to the mail connection information
47053739Seric **			structure to be filled in.
47152106Seric **		usesecureport -- if set, use a low numbered (reserved)
47252106Seric **			port to provide some rudimentary authentication.
4736039Seric **
4746039Seric **	Returns:
4756039Seric **		An exit code telling whether the connection could be
4766039Seric **			made and if not why not.
4776039Seric **
4786039Seric **	Side Effects:
4796039Seric **		none.
4806039Seric */
4815978Seric 
48258755Seric SOCKADDR	CurHostAddr;		/* address of current host */
48358305Seric 
48454967Seric int
48553739Seric makeconnection(host, port, mci, usesecureport)
4866039Seric 	char *host;
4877286Seric 	u_short port;
48854967Seric 	register MCI *mci;
48952106Seric 	bool usesecureport;
4906039Seric {
49129430Sbloom 	register int i, s;
49229430Sbloom 	register struct hostent *hp = (struct hostent *)NULL;
49358755Seric 	SOCKADDR addr;
49452106Seric 	int sav_errno;
49558755Seric 	int addrlen;
49635651Seric #ifdef NAMED_BIND
49735651Seric 	extern int h_errno;
49835651Seric #endif
4996039Seric 
5006039Seric 	/*
5016039Seric 	**  Set up the address for the mailer.
5029308Seric 	**	Accept "[a.b.c.d]" syntax for host name.
5036039Seric 	*/
5046039Seric 
50535651Seric #ifdef NAMED_BIND
50625475Smiriam 	h_errno = 0;
50735651Seric #endif
50825475Smiriam 	errno = 0;
50958864Seric 	bzero(&CurHostAddr, sizeof CurHostAddr);
51064334Seric 	SmtpPhase = mci->mci_phase = "initial connection";
51158906Seric 	CurHostName = host;
51225475Smiriam 
5139308Seric 	if (host[0] == '[')
5149308Seric 	{
51511147Seric 		long hid;
51656795Seric 		register char *p = strchr(host, ']');
5179308Seric 
51811147Seric 		if (p != NULL)
5199308Seric 		{
52011147Seric 			*p = '\0';
52159884Seric #ifdef NETINET
52211147Seric 			hid = inet_addr(&host[1]);
52358360Seric 			if (hid == -1)
52459884Seric #endif
52558360Seric 			{
52658360Seric 				/* try it as a host name (avoid MX lookup) */
52758360Seric 				hp = gethostbyname(&host[1]);
52858360Seric 				*p = ']';
52958360Seric 				goto gothostent;
53058360Seric 			}
53111147Seric 			*p = ']';
5329308Seric 		}
53358360Seric 		if (p == NULL)
5349308Seric 		{
53558151Seric 			usrerr("553 Invalid numeric domain spec \"%s\"", host);
5369308Seric 			return (EX_NOHOST);
5379308Seric 		}
53859884Seric #ifdef NETINET
53959884Seric 		addr.sin.sin_family = AF_INET;		/*XXX*/
54058778Seric 		addr.sin.sin_addr.s_addr = hid;
54159884Seric #endif
5429308Seric 	}
5439610Seric 	else
5449610Seric 	{
54529430Sbloom 		hp = gethostbyname(host);
54658360Seric gothostent:
54725475Smiriam 		if (hp == NULL)
54824945Seric 		{
54935651Seric #ifdef NAMED_BIND
55025475Smiriam 			if (errno == ETIMEDOUT || h_errno == TRY_AGAIN)
55125475Smiriam 				return (EX_TEMPFAIL);
55225657Seric 
55335651Seric 			/* if name server is specified, assume temp fail */
55435651Seric 			if (errno == ECONNREFUSED && UseNameServer)
55535651Seric 				return (EX_TEMPFAIL);
55635651Seric #endif
55725475Smiriam 			return (EX_NOHOST);
55824945Seric 		}
55958778Seric 		addr.sa.sa_family = hp->h_addrtype;
56058778Seric 		switch (hp->h_addrtype)
56158778Seric 		{
56258778Seric #ifdef NETINET
56358778Seric 		  case AF_INET:
56458755Seric 			bcopy(hp->h_addr,
56558778Seric 				&addr.sin.sin_addr,
56658755Seric 				hp->h_length);
56758778Seric 			break;
56858778Seric #endif
56958778Seric 
57058778Seric 		  default:
57158755Seric 			bcopy(hp->h_addr,
57258778Seric 				addr.sa.sa_data,
57358755Seric 				hp->h_length);
57458778Seric 			break;
57558778Seric 		}
57629430Sbloom 		i = 1;
5779610Seric 	}
5789610Seric 
5799610Seric 	/*
5809610Seric 	**  Determine the port number.
5819610Seric 	*/
5829610Seric 
58310011Seric 	if (port != 0)
58458755Seric 		port = htons(port);
58510011Seric 	else
5869610Seric 	{
5879610Seric 		register struct servent *sp = getservbyname("smtp", "tcp");
5889610Seric 
5899610Seric 		if (sp == NULL)
5909610Seric 		{
59158909Seric 			syserr("554 makeconnection: service \"smtp\" unknown");
59257977Seric 			return (EX_OSERR);
5939610Seric 		}
59458755Seric 		port = sp->s_port;
5959610Seric 	}
5966039Seric 
59758778Seric 	switch (addr.sa.sa_family)
59858755Seric 	{
59959884Seric #ifdef NETINET
60058755Seric 	  case AF_INET:
60158778Seric 		addr.sin.sin_port = port;
60258755Seric 		addrlen = sizeof (struct sockaddr_in);
60358755Seric 		break;
60459884Seric #endif
60558755Seric 
60658755Seric #ifdef NETISO
60758755Seric 	  case AF_ISO:
60858755Seric 		/* assume two byte transport selector */
60958755Seric 		bcopy((char *) &port, TSEL((struct sockaddr_iso *) &addr), 2);
61058755Seric 		addrlen = sizeof (struct sockaddr_iso);
61158755Seric 		break;
61258755Seric #endif
61358755Seric 
61458755Seric 	  default:
61558778Seric 		syserr("Can't connect to address family %d", addr.sa.sa_family);
61658755Seric 		return (EX_NOHOST);
61758755Seric 	}
61858755Seric 
6196039Seric 	/*
6206039Seric 	**  Try to actually open the connection.
6216039Seric 	*/
6226039Seric 
62359156Seric #ifdef XLA
62459156Seric 	/* if too many connections, don't bother trying */
62559156Seric 	if (!xla_noqueue_ok(host))
62659156Seric 		return EX_TEMPFAIL;
62759156Seric #endif
62859156Seric 
62957736Seric 	for (;;)
63052106Seric 	{
63157736Seric 		if (tTd(16, 1))
63258755Seric 			printf("makeconnection (%s [%s])\n",
63358755Seric 				host, anynet_ntoa(&addr));
63452106Seric 
63558588Seric 		/* save for logging */
63658588Seric 		CurHostAddr = addr;
63758588Seric 
63857736Seric 		if (usesecureport)
63957736Seric 		{
64057736Seric 			int rport = IPPORT_RESERVED - 1;
6416039Seric 
64257736Seric 			s = rresvport(&rport);
64357736Seric 		}
64457736Seric 		else
64557736Seric 		{
64657736Seric 			s = socket(AF_INET, SOCK_STREAM, 0);
64757736Seric 		}
64857736Seric 		if (s < 0)
64957736Seric 		{
65057736Seric 			sav_errno = errno;
65157736Seric 			syserr("makeconnection: no socket");
65257736Seric 			goto failure;
65357736Seric 		}
65410347Seric 
65557736Seric 		if (tTd(16, 1))
65657736Seric 			printf("makeconnection: fd=%d\n", s);
65757736Seric 
65857736Seric 		/* turn on network debugging? */
65957736Seric 		if (tTd(16, 101))
66057736Seric 		{
66157736Seric 			int on = 1;
66257736Seric 			(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG,
66357736Seric 					  (char *)&on, sizeof on);
66457736Seric 		}
66557736Seric 		if (CurEnv->e_xfp != NULL)
66657736Seric 			(void) fflush(CurEnv->e_xfp);		/* for debugging */
66757736Seric 		errno = 0;					/* for debugging */
66858755Seric 		if (connect(s, (struct sockaddr *) &addr, addrlen) >= 0)
66957736Seric 			break;
67057736Seric 
67157736Seric 		/* couldn't connect.... figure out why */
67227744Sbloom 		sav_errno = errno;
67327744Sbloom 		(void) close(s);
67429430Sbloom 		if (hp && hp->h_addr_list[i])
67529430Sbloom 		{
67657736Seric 			if (tTd(16, 1))
67758755Seric 				printf("Connect failed (%s); trying new address....\n",
67858755Seric 					errstring(sav_errno));
67958778Seric 			switch (addr.sa.sa_family)
68058778Seric 			{
68158778Seric #ifdef NETINET
68258778Seric 			  case AF_INET:
68358755Seric 				bcopy(hp->h_addr_list[i++],
68458778Seric 				      &addr.sin.sin_addr,
68558755Seric 				      hp->h_length);
68658778Seric 				break;
68758778Seric #endif
68858778Seric 
68958778Seric 			  default:
69058755Seric 				bcopy(hp->h_addr_list[i++],
69158778Seric 					addr.sa.sa_data,
69252106Seric 					hp->h_length);
69358778Seric 				break;
69458778Seric 			}
69557736Seric 			continue;
69629430Sbloom 		}
69729430Sbloom 
6986039Seric 		/* failure, decide if temporary or not */
6996039Seric 	failure:
70059254Seric #ifdef XLA
70159254Seric 		xla_host_end(host);
70259254Seric #endif
70358542Seric 		if (transienterror(sav_errno))
70458542Seric 			return EX_TEMPFAIL;
70558542Seric 		else
70658542Seric 		{
70758542Seric 			message("%s", errstring(sav_errno));
70858542Seric 			return (EX_UNAVAILABLE);
7096039Seric 		}
7106039Seric 	}
7116039Seric 
7126039Seric 	/* connection ok, put it into canonical form */
71353739Seric 	mci->mci_out = fdopen(s, "w");
71453739Seric 	mci->mci_in = fdopen(dup(s), "r");
7156039Seric 
71610098Seric 	return (EX_OK);
7176039Seric }
71810758Seric /*
71910758Seric **  MYHOSTNAME -- return the name of this host.
72010758Seric **
72110758Seric **	Parameters:
72210758Seric **		hostbuf -- a place to return the name of this host.
72312313Seric **		size -- the size of hostbuf.
72410758Seric **
72510758Seric **	Returns:
72610758Seric **		A list of aliases for this host.
72710758Seric **
72810758Seric **	Side Effects:
72964338Seric **		Adds numeric codes to $=w.
73010758Seric */
7316039Seric 
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 
75464338Seric 			for (i = 0; hp->h_addr_list[i] != NULL; i++)
75558110Seric 			{
75664338Seric 				char ipbuf[100];
75764338Seric 
75864338Seric 				sprintf(ipbuf, "[%s]",
75964338Seric 					inet_ntoa(*((struct in_addr *) hp->h_addr_list[i])));
76064338Seric 				setclass('w', ipbuf);
76158110Seric 			}
76258110Seric 		}
76358110Seric 
76411147Seric 		return (hp->h_aliases);
76516877Seric 	}
76610758Seric 	else
76710758Seric 		return (NULL);
76810758Seric }
76951315Seric /*
77058951Seric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
77158308Seric **
77258951Seric **	Uses RFC1413 protocol to try to get info from the other end.
77358951Seric **
77458308Seric **	Parameters:
77558308Seric **		fd -- the descriptor
77658308Seric **
77758308Seric **	Returns:
77858951Seric **		The user@host information associated with this descriptor.
77958308Seric **
78058308Seric **	Side Effects:
78158951Seric **		Sets RealHostName to the name of the host at the other end.
78258308Seric */
78358308Seric 
78458951Seric #ifdef IDENTPROTO
78558951Seric 
78658951Seric static jmp_buf	CtxAuthTimeout;
78758951Seric 
78858951Seric static
78958951Seric authtimeout()
79058951Seric {
79158951Seric 	longjmp(CtxAuthTimeout, 1);
79258951Seric }
79358951Seric 
79458951Seric #endif
79558951Seric 
79658308Seric char *
79758951Seric getauthinfo(fd)
79858308Seric 	int fd;
79958308Seric {
80058951Seric 	SOCKADDR fa;
80158951Seric 	int falen;
80259104Seric 	register char *p;
80358951Seric #ifdef IDENTPROTO
80458951Seric 	SOCKADDR la;
80558951Seric 	int lalen;
80658951Seric 	register struct servent *sp;
80758951Seric 	int s;
80858951Seric 	int i;
80958951Seric 	EVENT *ev;
81058951Seric #endif
81158951Seric 	static char hbuf[MAXNAME * 2 + 2];
81258951Seric 	extern char *hostnamebyanyaddr();
81358951Seric 	extern char RealUserName[];			/* main.c */
81458308Seric 
81558951Seric 	falen = sizeof fa;
81658951Seric 	if (getpeername(fd, &fa.sa, &falen) < 0 || falen <= 0)
81758951Seric 	{
81858951Seric 		RealHostName = "localhost";
81958951Seric 		(void) sprintf(hbuf, "%s@localhost", RealUserName);
82058957Seric 		if (tTd(9, 1))
82158951Seric 			printf("getauthinfo: %s\n", hbuf);
82258951Seric 		return hbuf;
82358951Seric 	}
82458951Seric 
82564086Seric 	p = hostnamebyanyaddr(&fa);
82664086Seric 	RealHostName = newstr(p);
82758951Seric 	RealHostAddr = fa;
82858951Seric 
82958951Seric #ifdef IDENTPROTO
83058951Seric 	lalen = sizeof la;
83158951Seric 	if (fa.sa.sa_family != AF_INET ||
83258951Seric 	    getsockname(fd, &la.sa, &lalen) < 0 || lalen <= 0 ||
83358951Seric 	    la.sa.sa_family != AF_INET)
83458951Seric 	{
83558951Seric 		/* no ident info */
83658951Seric 		goto noident;
83758951Seric 	}
83858951Seric 
83958951Seric 	/* create ident query */
84060489Seric 	(void) sprintf(hbuf, "%d,%d\r\n",
84160489Seric 		ntohs(fa.sin.sin_port), ntohs(la.sin.sin_port));
84258951Seric 
84358951Seric 	/* create local address */
84458951Seric 	bzero(&la, sizeof la);
84558951Seric 
84658951Seric 	/* create foreign address */
84758951Seric 	sp = getservbyname("auth", "tcp");
84858951Seric 	if (sp != NULL)
84958951Seric 		fa.sin.sin_port = sp->s_port;
85058308Seric 	else
85159097Seric 		fa.sin.sin_port = htons(113);
85258951Seric 
85358951Seric 	s = -1;
85458951Seric 	if (setjmp(CtxAuthTimeout) != 0)
85558951Seric 	{
85658951Seric 		if (s >= 0)
85758951Seric 			(void) close(s);
85858951Seric 		goto noident;
85958951Seric 	}
86058951Seric 
86158951Seric 	/* put a timeout around the whole thing */
86264255Seric 	ev = setevent(TimeOuts.to_ident, authtimeout, 0);
86358951Seric 
86458951Seric 	/* connect to foreign IDENT server */
86558951Seric 	s = socket(AF_INET, SOCK_STREAM, 0);
86658951Seric 	if (s < 0)
86758951Seric 	{
86858951Seric 		clrevent(ev);
86958951Seric 		goto noident;
87058951Seric 	}
87158951Seric 	if (connect(s, &fa.sa, sizeof fa.sin) < 0)
87258951Seric 	{
87358951Seric closeident:
87458951Seric 		(void) close(s);
87558951Seric 		clrevent(ev);
87658951Seric 		goto noident;
87758951Seric 	}
87858951Seric 
87958957Seric 	if (tTd(9, 10))
88058951Seric 		printf("getauthinfo: sent %s", hbuf);
88158951Seric 
88258951Seric 	/* send query */
88358951Seric 	if (write(s, hbuf, strlen(hbuf)) < 0)
88458951Seric 		goto closeident;
88558951Seric 
88658951Seric 	/* get result */
88758951Seric 	i = read(s, hbuf, sizeof hbuf);
88858951Seric 	(void) close(s);
88958951Seric 	clrevent(ev);
89058951Seric 	if (i <= 0)
89158951Seric 		goto noident;
89258951Seric 	if (hbuf[--i] == '\n' && hbuf[--i] == '\r')
89358951Seric 		i--;
89458951Seric 	hbuf[++i] = '\0';
89558951Seric 
89658957Seric 	if (tTd(9, 3))
89758951Seric 		printf("getauthinfo:  got %s\n", hbuf);
89858951Seric 
89958951Seric 	/* parse result */
90058951Seric 	p = strchr(hbuf, ':');
90158951Seric 	if (p == NULL)
90258951Seric 	{
90358951Seric 		/* malformed response */
90458951Seric 		goto noident;
90558951Seric 	}
90658951Seric 	while (isascii(*++p) && isspace(*p))
90758951Seric 		continue;
90858951Seric 	if (strncasecmp(p, "userid", 6) != 0)
90958951Seric 	{
91058951Seric 		/* presumably an error string */
91158951Seric 		goto noident;
91258951Seric 	}
91358951Seric 	p += 6;
91458951Seric 	while (isascii(*p) && isspace(*p))
91558951Seric 		p++;
91658951Seric 	if (*p++ != ':')
91758951Seric 	{
91858951Seric 		/* either useridxx or malformed response */
91958951Seric 		goto noident;
92058951Seric 	}
92158951Seric 
92258951Seric 	/* p now points to the OSTYPE field */
92358951Seric 	p = strchr(p, ':');
92458951Seric 	if (p == NULL)
92558951Seric 	{
92658951Seric 		/* malformed response */
92758951Seric 		goto noident;
92858951Seric 	}
92958951Seric 
93058957Seric 	/* 1413 says don't do this -- but it's broken otherwise */
93158957Seric 	while (isascii(*++p) && isspace(*p))
93258957Seric 		continue;
93358957Seric 
93458951Seric 	/* p now points to the authenticated name */
93558951Seric 	(void) sprintf(hbuf, "%s@%s", p, RealHostName);
93658957Seric 	goto finish;
93758957Seric 
93858957Seric #endif /* IDENTPROTO */
93958957Seric 
94058957Seric noident:
94158957Seric 	(void) strcpy(hbuf, RealHostName);
94258957Seric 
94358957Seric finish:
94458951Seric 	if (RealHostName[0] != '[')
94558951Seric 	{
94658951Seric 		p = &hbuf[strlen(hbuf)];
94758951Seric 		(void) sprintf(p, " [%s]", anynet_ntoa(&RealHostAddr));
94858951Seric 	}
94958957Seric 	if (tTd(9, 1))
95058951Seric 		printf("getauthinfo: %s\n", hbuf);
95158308Seric 	return hbuf;
95258308Seric }
95358308Seric /*
95460089Seric **  HOST_MAP_LOOKUP -- turn a hostname into canonical form
95553751Seric **
95653751Seric **	Parameters:
95756823Seric **		map -- a pointer to this map (unused).
95860089Seric **		name -- the (presumably unqualified) hostname.
95960257Seric **		av -- unused -- for compatibility with other mapping
96055019Seric **			functions.
96159084Seric **		statp -- an exit status (out parameter) -- set to
96259084Seric **			EX_TEMPFAIL if the name server is unavailable.
96353751Seric **
96453751Seric **	Returns:
96553751Seric **		The mapping, if found.
96653751Seric **		NULL if no mapping found.
96753751Seric **
96853751Seric **	Side Effects:
96953751Seric **		Looks up the host specified in hbuf.  If it is not
97053751Seric **		the canonical name for that host, return the canonical
97153751Seric **		name.
97253751Seric */
97351315Seric 
97453751Seric char *
97560257Seric host_map_lookup(map, name, av, statp)
97656823Seric 	MAP *map;
97760089Seric 	char *name;
97860257Seric 	char **av;
97959084Seric 	int *statp;
98016911Seric {
98116911Seric 	register struct hostent *hp;
98233932Sbostic 	u_long in_addr;
98356823Seric 	char *cp;
98458110Seric 	int i;
98559671Seric 	register STAB *s;
98660257Seric 	char hbuf[MAXNAME];
98759671Seric 	extern struct hostent *gethostbyaddr();
98859671Seric 	extern int h_errno;
98916911Seric 
99025574Smiriam 	/*
99159671Seric 	**  See if we have already looked up this name.  If so, just
99259671Seric 	**  return it.
99359671Seric 	*/
99453751Seric 
99560089Seric 	s = stab(name, ST_NAMECANON, ST_ENTER);
99659671Seric 	if (bitset(NCF_VALID, s->s_namecanon.nc_flags))
99759671Seric 	{
99859986Seric 		if (tTd(9, 1))
99960089Seric 			printf("host_map_lookup(%s) => CACHE %s\n",
100060089Seric 				name, s->s_namecanon.nc_cname);
100159671Seric 		errno = s->s_namecanon.nc_errno;
100259671Seric 		h_errno = s->s_namecanon.nc_herrno;
100359671Seric 		*statp = s->s_namecanon.nc_stat;
100459671Seric 		return s->s_namecanon.nc_cname;
100559671Seric 	}
100659671Seric 
100759671Seric 	/*
100859671Seric 	**  If first character is a bracket, then it is an address
100959671Seric 	**  lookup.  Address is copied into a temporary buffer to
101060089Seric 	**  strip the brackets and to preserve name if address is
101159671Seric 	**  unknown.
101259671Seric 	*/
101359671Seric 
101460089Seric 	if (*name != '[')
101553751Seric 	{
101655019Seric 		extern bool getcanonname();
101755019Seric 
101858798Seric 		if (tTd(9, 1))
101960089Seric 			printf("host_map_lookup(%s) => ", name);
102059671Seric 		s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
102160089Seric 		(void) strcpy(hbuf, name);
102263842Seric 		if (getcanonname(hbuf, sizeof hbuf - 1, TRUE))
102358796Seric 		{
102458796Seric 			if (tTd(9, 1))
102558796Seric 				printf("%s\n", hbuf);
102660257Seric 			cp = map_rewrite(map, hbuf, strlen(hbuf), av);
102760257Seric 			s->s_namecanon.nc_cname = newstr(cp);
102860257Seric 			return cp;
102958796Seric 		}
103053751Seric 		else
103158796Seric 		{
103259084Seric 			register struct hostent *hp;
103359084Seric 
103458796Seric 			if (tTd(9, 1))
103559084Seric 				printf("FAIL (%d)\n", h_errno);
103659671Seric 			s->s_namecanon.nc_errno = errno;
103759671Seric 			s->s_namecanon.nc_herrno = h_errno;
103859084Seric 			switch (h_errno)
103959084Seric 			{
104059084Seric 			  case TRY_AGAIN:
104159596Seric 				if (UseNameServer)
104259734Seric 				{
104359734Seric 					char *msg = "Recipient domain nameserver timed out";
104459734Seric 
104559734Seric 					message(msg);
104659734Seric 					if (CurEnv->e_message == NULL)
104760009Seric 						CurEnv->e_message = newstr(msg);
104859734Seric 				}
104959084Seric 				*statp = EX_TEMPFAIL;
105059084Seric 				break;
105159084Seric 
105259084Seric 			  case HOST_NOT_FOUND:
105359084Seric 				*statp = EX_NOHOST;
105459084Seric 				break;
105559084Seric 
105659084Seric 			  case NO_RECOVERY:
105759084Seric 				*statp = EX_SOFTWARE;
105859084Seric 				break;
105959084Seric 
106059084Seric 			  default:
106159084Seric 				*statp = EX_UNAVAILABLE;
106259084Seric 				break;
106359084Seric 			}
106459671Seric 			s->s_namecanon.nc_stat = *statp;
106559084Seric 			if (*statp != EX_TEMPFAIL || UseNameServer)
106659084Seric 				return NULL;
106759084Seric 
106859084Seric 			/*
106959084Seric 			**  Try to look it up in /etc/hosts
107059084Seric 			*/
107159084Seric 
107260089Seric 			hp = gethostbyname(name);
107359084Seric 			if (hp == NULL)
107459084Seric 			{
107559084Seric 				/* no dice there either */
107659671Seric 				s->s_namecanon.nc_stat = *statp = EX_NOHOST;
107759084Seric 				return NULL;
107859084Seric 			}
107959084Seric 
108059671Seric 			s->s_namecanon.nc_stat = *statp = EX_OK;
108160257Seric 			cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
108260257Seric 			s->s_namecanon.nc_cname = newstr(cp);
108360257Seric 			return cp;
108458796Seric 		}
108553751Seric 	}
108660089Seric 	if ((cp = strchr(name, ']')) == NULL)
108753751Seric 		return (NULL);
108840994Sbostic 	*cp = '\0';
108960089Seric 	in_addr = inet_addr(&name[1]);
109058110Seric 
109158110Seric 	/* nope -- ask the name server */
109233932Sbostic 	hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET);
109359671Seric 	s->s_namecanon.nc_errno = errno;
109459671Seric 	s->s_namecanon.nc_herrno = h_errno;
109559671Seric 	s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
109633932Sbostic 	if (hp == NULL)
109759671Seric 	{
109859671Seric 		s->s_namecanon.nc_stat = *statp = EX_NOHOST;
109953751Seric 		return (NULL);
110059671Seric 	}
110153751Seric 
110258110Seric 	/* found a match -- copy out */
110360257Seric 	cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
110459671Seric 	s->s_namecanon.nc_stat = *statp = EX_OK;
110560257Seric 	s->s_namecanon.nc_cname = newstr(cp);
110660257Seric 	return cp;
110733932Sbostic }
110858755Seric /*
110958755Seric **  ANYNET_NTOA -- convert a network address to printable form.
111058755Seric **
111158755Seric **	Parameters:
111258755Seric **		sap -- a pointer to a sockaddr structure.
111358755Seric **
111458755Seric **	Returns:
111558755Seric **		A printable version of that sockaddr.
111658755Seric */
111716911Seric 
111858755Seric char *
111958755Seric anynet_ntoa(sap)
112058755Seric 	register SOCKADDR *sap;
112158755Seric {
112258755Seric 	register char *bp;
112358755Seric 	register char *ap;
112458755Seric 	int l;
112558755Seric 	static char buf[80];
112658755Seric 
112758798Seric 	/* check for null/zero family */
112858798Seric 	if (sap == NULL)
112958798Seric 		return "NULLADDR";
113058798Seric 	if (sap->sa.sa_family == 0)
113158798Seric 		return "0";
113258798Seric 
113358778Seric #ifdef NETINET
113458778Seric 	if (sap->sa.sa_family == AF_INET)
113558755Seric 		return inet_ntoa(((struct sockaddr_in *) sap)->sin_addr);
113658778Seric #endif
113758755Seric 
113858755Seric 	/* unknown family -- just dump bytes */
113958778Seric 	(void) sprintf(buf, "Family %d: ", sap->sa.sa_family);
114058755Seric 	bp = &buf[strlen(buf)];
114158778Seric 	ap = sap->sa.sa_data;
114258778Seric 	for (l = sizeof sap->sa.sa_data; --l >= 0; )
114358755Seric 	{
114458755Seric 		(void) sprintf(bp, "%02x:", *ap++ & 0377);
114558755Seric 		bp += 3;
114658755Seric 	}
114758755Seric 	*--bp = '\0';
114858755Seric 	return buf;
114958755Seric }
115058951Seric /*
115158951Seric **  HOSTNAMEBYANYADDR -- return name of host based on address
115258951Seric **
115358951Seric **	Parameters:
115458951Seric **		sap -- SOCKADDR pointer
115558951Seric **
115658951Seric **	Returns:
115758951Seric **		text representation of host name.
115858951Seric **
115958951Seric **	Side Effects:
116058951Seric **		none.
116158951Seric */
116258755Seric 
116358951Seric char *
116458951Seric hostnamebyanyaddr(sap)
116558951Seric 	register SOCKADDR *sap;
116658951Seric {
116758951Seric 	register struct hostent *hp;
116858951Seric 
116959042Seric #ifdef NAMED_BIND
117059042Seric 	int saveretry;
117159042Seric 
117259042Seric 	/* shorten name server timeout to avoid higher level timeouts */
117359042Seric 	saveretry = _res.retry;
117459042Seric 	_res.retry = 3;
117559042Seric #endif /* NAMED_BIND */
117659042Seric 
117758951Seric 	switch (sap->sa.sa_family)
117858951Seric 	{
117958951Seric #ifdef NETINET
118058951Seric 	  case AF_INET:
118158951Seric 		hp = gethostbyaddr((char *) &sap->sin.sin_addr,
118258951Seric 			sizeof sap->sin.sin_addr,
118358951Seric 			AF_INET);
118458951Seric 		break;
118558951Seric #endif
118658951Seric 
118758951Seric #ifdef NETISO
118858951Seric 	  case AF_ISO:
118958951Seric 		hp = gethostbyaddr((char *) &sap->siso.siso_addr,
119058951Seric 			sizeof sap->siso.siso_addr,
119158951Seric 			AF_ISO);
119258951Seric 		break;
119358951Seric #endif
119458951Seric 
119558951Seric 	  default:
119658951Seric 		hp = gethostbyaddr(sap->sa.sa_data,
119758951Seric 			   sizeof sap->sa.sa_data,
119858951Seric 			   sap->sa.sa_family);
119958951Seric 		break;
120058951Seric 	}
120158951Seric 
120259042Seric #ifdef NAMED_BIND
120359042Seric 	_res.retry = saveretry;
120459042Seric #endif /* NAMED_BIND */
120559042Seric 
120658951Seric 	if (hp != NULL)
120758951Seric 		return hp->h_name;
120858951Seric 	else
120958951Seric 	{
121058951Seric 		/* produce a dotted quad */
121158951Seric 		static char buf[512];
121258951Seric 
121358951Seric 		(void) sprintf(buf, "[%s]", anynet_ntoa(sap));
121458951Seric 		return buf;
121558951Seric 	}
121658951Seric }
121758951Seric 
121856795Seric # else /* DAEMON */
121916911Seric /* code for systems without sophisticated networking */
122010758Seric 
122110758Seric /*
122210758Seric **  MYHOSTNAME -- stub version for case of no daemon code.
122311297Seric **
122411297Seric **	Can't convert to upper case here because might be a UUCP name.
122512313Seric **
122612313Seric **	Mark, you can change this to be anything you want......
122710758Seric */
122810758Seric 
122910758Seric char **
123012313Seric myhostname(hostbuf, size)
123110758Seric 	char hostbuf[];
123212313Seric 	int size;
123310758Seric {
123410758Seric 	register FILE *f;
123510758Seric 
123610758Seric 	hostbuf[0] = '\0';
123710758Seric 	f = fopen("/usr/include/whoami", "r");
123810758Seric 	if (f != NULL)
123910758Seric 	{
124012313Seric 		(void) fgets(hostbuf, size, f);
124110758Seric 		fixcrlf(hostbuf, TRUE);
124210758Seric 		(void) fclose(f);
124310758Seric 	}
124410758Seric 	return (NULL);
124510758Seric }
124616911Seric /*
124758951Seric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
124858308Seric **
124958308Seric **	Parameters:
125058308Seric **		fd -- the descriptor
125158308Seric **
125258308Seric **	Returns:
125358308Seric **		The host name associated with this descriptor, if it can
125458308Seric **			be determined.
125558308Seric **		NULL otherwise.
125658308Seric **
125758308Seric **	Side Effects:
125858308Seric **		none
125958308Seric */
126058308Seric 
126158308Seric char *
126258951Seric getauthinfo(fd)
126358308Seric 	int fd;
126458308Seric {
126558308Seric 	return NULL;
126658308Seric }
126758308Seric /*
126816911Seric **  MAPHOSTNAME -- turn a hostname into canonical form
126916911Seric **
127016911Seric **	Parameters:
127156823Seric **		map -- a pointer to the database map.
127260089Seric **		name -- a buffer containing a hostname.
127353751Seric **		avp -- a pointer to a (cf file defined) argument vector.
127459084Seric **		statp -- an exit status (out parameter).
127516911Seric **
127616911Seric **	Returns:
127753751Seric **		mapped host name
127851315Seric **		FALSE otherwise.
127916911Seric **
128016911Seric **	Side Effects:
128160089Seric **		Looks up the host specified in name.  If it is not
128216911Seric **		the canonical name for that host, replace it with
128316911Seric **		the canonical name.  If the name is unknown, or it
128416911Seric **		is already the canonical name, leave it unchanged.
128516911Seric */
128610758Seric 
128716911Seric /*ARGSUSED*/
128853751Seric char *
128960089Seric host_map_lookup(map, name, avp, statp)
129056823Seric 	MAP *map;
129160089Seric 	char *name;
129253751Seric 	char **avp;
129359084Seric 	char *statp;
129416911Seric {
129559084Seric 	register struct hostent *hp;
129659084Seric 
129760089Seric 	hp = gethostbyname(name);
129859084Seric 	if (hp != NULL)
129959084Seric 		return hp->h_name;
130059084Seric 	*statp = EX_NOHOST;
130153751Seric 	return NULL;
130216911Seric }
130316911Seric 
130456795Seric #endif /* DAEMON */
1305