xref: /csrg-svn/usr.sbin/sendmail/src/daemon.c (revision 66349)
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*66349Seric static char sccsid[] = "@(#)daemon.c	8.39 (Berkeley) 03/13/94 (with daemon mode)";
1533780Sbostic #else
16*66349Seric static char sccsid[] = "@(#)daemon.c	8.39 (Berkeley) 03/13/94 (without daemon mode)";
1733780Sbostic #endif
1833780Sbostic #endif /* not lint */
194535Seric 
2033780Sbostic #ifdef DAEMON
2133780Sbostic 
2223120Seric # include <netdb.h>
2364338Seric # include <arpa/inet.h>
245978Seric 
2566334Seric #if NAMED_BIND
2659042Seric # include <arpa/nameser.h>
2759042Seric # include <resolv.h>
2859042Seric #endif
2959042Seric 
304535Seric /*
314535Seric **  DAEMON.C -- routines to use when running as a daemon.
327556Seric **
337556Seric **	This entire file is highly dependent on the 4.2 BSD
347556Seric **	interprocess communication primitives.  No attempt has
357556Seric **	been made to make this file portable to Version 7,
367556Seric **	Version 6, MPX files, etc.  If you should try such a
377556Seric **	thing yourself, I recommend chucking the entire file
387556Seric **	and starting from scratch.  Basic semantics are:
397556Seric **
407556Seric **	getrequests()
417556Seric **		Opens a port and initiates a connection.
427556Seric **		Returns in a child.  Must set InChannel and
437556Seric **		OutChannel appropriately.
4410206Seric **	clrdaemon()
4510206Seric **		Close any open files associated with getting
4610206Seric **		the connection; this is used when running the queue,
4710206Seric **		etc., to avoid having extra file descriptors during
4810206Seric **		the queue run and to avoid confusing the network
4910206Seric **		code (if it cares).
5052106Seric **	makeconnection(host, port, outfile, infile, usesecureport)
517556Seric **		Make a connection to the named host on the given
527556Seric **		port.  Set *outfile and *infile to the files
537556Seric **		appropriate for communication.  Returns zero on
547556Seric **		success, else an exit status describing the
557556Seric **		error.
5660089Seric **	host_map_lookup(map, hbuf, avp, pstat)
5756823Seric **		Convert the entry in hbuf into a canonical form.
584535Seric */
594535Seric /*
604535Seric **  GETREQUESTS -- open mail IPC port and get requests.
614535Seric **
624535Seric **	Parameters:
634535Seric **		none.
644535Seric **
654535Seric **	Returns:
664535Seric **		none.
674535Seric **
684535Seric **	Side Effects:
694535Seric **		Waits until some interesting activity occurs.  When
704535Seric **		it does, a child is created to process it, and the
714535Seric **		parent waits for completion.  Return from this
729886Seric **		routine is always in the child.  The file pointers
739886Seric **		"InChannel" and "OutChannel" should be set to point
749886Seric **		to the communication channel.
754535Seric */
764535Seric 
7758849Seric int		DaemonSocket	= -1;		/* fd describing socket */
7858849Seric SOCKADDR	DaemonAddr;			/* socket for incoming */
7959783Seric int		ListenQueueSize = 10;		/* size of listen queue */
8064381Seric int		TcpRcvBufferSize = 0;		/* size of TCP receive buffer */
8164381Seric int		TcpSndBufferSize = 0;		/* size of TCP send buffer */
8216144Seric 
834535Seric getrequests()
844535Seric {
859610Seric 	int t;
8625027Seric 	int on = 1;
8753751Seric 	bool refusingconnections = TRUE;
8858419Seric 	FILE *pidf;
8964828Seric 	int socksize;
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 	{
10265169Seric 		register struct servent *sp;
10365169Seric 
10458849Seric 		sp = getservbyname("smtp", "tcp");
10558849Seric 		if (sp == NULL)
10658849Seric 		{
10758909Seric 			syserr("554 service \"smtp\" unknown");
10865169Seric 			DaemonAddr.sin.sin_port = htons(25);
10958849Seric 		}
11065169Seric 		else
11165169Seric 			DaemonAddr.sin.sin_port = sp->s_port;
1129610Seric 	}
1139610Seric 
1149610Seric 	/*
1159610Seric 	**  Try to actually open the connection.
1169610Seric 	*/
1179610Seric 
1189610Seric 	if (tTd(15, 1))
11958849Seric 		printf("getrequests: port 0x%x\n", DaemonAddr.sin.sin_port);
1209610Seric 
1219610Seric 	/* get a socket for the SMTP connection */
12259041Seric 	DaemonSocket = socket(DaemonAddr.sa.sa_family, SOCK_STREAM, 0);
12310206Seric 	if (DaemonSocket < 0)
1249610Seric 	{
1259610Seric 		/* probably another daemon already */
1269610Seric 		syserr("getrequests: can't create socket");
1279610Seric 	  severe:
1289610Seric # ifdef LOG
1299610Seric 		if (LogLevel > 0)
13057663Seric 			syslog(LOG_ALERT, "problem creating SMTP socket");
13156795Seric # endif /* LOG */
1329610Seric 		finis();
1339610Seric 	}
13410347Seric 
13510347Seric 	/* turn on network debugging? */
13656328Seric 	if (tTd(15, 101))
13724945Seric 		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on);
13810347Seric 
13925027Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on);
14025027Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on);
14125027Seric 
14264391Seric #ifdef SO_RCVBUF
14364391Seric 	if (TcpRcvBufferSize > 0)
14464391Seric 	{
14564391Seric 		if (setsockopt(DaemonSocket, SOL_SOCKET, SO_RCVBUF,
14664561Seric 			       (char *) &TcpRcvBufferSize,
14764391Seric 			       sizeof(TcpRcvBufferSize)) < 0)
14864391Seric 			syserr("getrequests: setsockopt(SO_RCVBUF)");
14964391Seric 	}
15064391Seric #endif
15164391Seric 
15259041Seric 	switch (DaemonAddr.sa.sa_family)
1539610Seric 	{
15459041Seric # ifdef NETINET
15559041Seric 	  case AF_INET:
15664828Seric 		socksize = sizeof DaemonAddr.sin;
15759041Seric 		break;
15859041Seric # endif
15959041Seric 
16059041Seric # ifdef NETISO
16159041Seric 	  case AF_ISO:
16264828Seric 		socksize = sizeof DaemonAddr.siso;
16359041Seric 		break;
16459041Seric # endif
16559041Seric 
16659041Seric 	  default:
16764828Seric 		socksize = sizeof DaemonAddr;
16859041Seric 		break;
16959041Seric 	}
17059041Seric 
17164828Seric 	if (bind(DaemonSocket, &DaemonAddr.sa, socksize) < 0)
17259041Seric 	{
1739610Seric 		syserr("getrequests: cannot bind");
17410206Seric 		(void) close(DaemonSocket);
1759610Seric 		goto severe;
1769610Seric 	}
1779610Seric 
17864035Seric 	(void) setsignal(SIGCHLD, reapchild);
17924945Seric 
18058419Seric 	/* write the pid to the log file for posterity */
18158419Seric 	pidf = fopen(PidFile, "w");
18258419Seric 	if (pidf != NULL)
18358419Seric 	{
18463863Seric 		extern char *CommandLineArgs;
18563863Seric 
18663863Seric 		/* write the process id on line 1 */
18758419Seric 		fprintf(pidf, "%d\n", getpid());
18863863Seric 
18963863Seric 		/* line 2 contains all command line flags */
19063863Seric 		fprintf(pidf, "%s\n", CommandLineArgs);
19163863Seric 
19263863Seric 		/* flush and close */
19358419Seric 		fclose(pidf);
19458419Seric 	}
19558419Seric 
19658419Seric 
1979610Seric 	if (tTd(15, 1))
19810206Seric 		printf("getrequests: %d\n", DaemonSocket);
1999610Seric 
2004631Seric 	for (;;)
2014631Seric 	{
20214875Seric 		register int pid;
20311147Seric 		auto int lotherend;
20453751Seric 		extern bool refuseconnections();
20511147Seric 
20614875Seric 		/* see if we are rejecting connections */
20753751Seric 		CurrentLA = getla();
20853751Seric 		if (refuseconnections())
20936584Sbostic 		{
21053751Seric 			if (!refusingconnections)
21153751Seric 			{
21253751Seric 				/* don't queue so peer will fail quickly */
21353751Seric 				(void) listen(DaemonSocket, 0);
21453751Seric 				refusingconnections = TRUE;
21553751Seric 			}
21657385Seric 			setproctitle("rejecting connections: load average: %d",
21757385Seric 				CurrentLA);
21814875Seric 			sleep(5);
21953751Seric 			continue;
22036584Sbostic 		}
22114875Seric 
22253751Seric 		if (refusingconnections)
22353751Seric 		{
22453751Seric 			/* start listening again */
22559783Seric 			if (listen(DaemonSocket, ListenQueueSize) < 0)
22653751Seric 			{
22753751Seric 				syserr("getrequests: cannot listen");
22853751Seric 				(void) close(DaemonSocket);
22953751Seric 				goto severe;
23053751Seric 			}
23153751Seric 			setproctitle("accepting connections");
23253751Seric 			refusingconnections = FALSE;
23353751Seric 		}
23453751Seric 
2359610Seric 		/* wait for a connection */
2369610Seric 		do
2379610Seric 		{
2389610Seric 			errno = 0;
23964828Seric 			lotherend = socksize;
24046928Sbostic 			t = accept(DaemonSocket,
24146928Sbostic 			    (struct sockaddr *)&RealHostAddr, &lotherend);
2429610Seric 		} while (t < 0 && errno == EINTR);
2439610Seric 		if (t < 0)
2445978Seric 		{
2459610Seric 			syserr("getrequests: accept");
2469610Seric 			sleep(5);
2479610Seric 			continue;
2485978Seric 		}
2494631Seric 
2505978Seric 		/*
2515978Seric 		**  Create a subprocess to process the mail.
2525978Seric 		*/
2535978Seric 
2547677Seric 		if (tTd(15, 2))
2559610Seric 			printf("getrequests: forking (fd = %d)\n", t);
2565978Seric 
2574636Seric 		pid = fork();
2584636Seric 		if (pid < 0)
2594631Seric 		{
2604636Seric 			syserr("daemon: cannot fork");
2614636Seric 			sleep(10);
2629610Seric 			(void) close(t);
2634636Seric 			continue;
2644631Seric 		}
2654631Seric 
2664636Seric 		if (pid == 0)
2674631Seric 		{
26864086Seric 			char *p;
26958951Seric 			extern char *hostnamebyanyaddr();
27011147Seric 
2714636Seric 			/*
2724636Seric 			**  CHILD -- return to caller.
27311147Seric 			**	Collect verified idea of sending host.
2744636Seric 			**	Verify calling user id if possible here.
2754636Seric 			*/
2764631Seric 
27764035Seric 			(void) setsignal(SIGCHLD, SIG_DFL);
27866017Seric 			DisConnected = FALSE;
27924950Seric 
28066032Seric 			setproctitle("startup with %s",
28166032Seric 				anynet_ntoa(&RealHostAddr));
28266032Seric 
28311147Seric 			/* determine host name */
28464086Seric 			p = hostnamebyanyaddr(&RealHostAddr);
28564086Seric 			RealHostName = newstr(p);
28666032Seric 			setproctitle("startup with %s", p);
28758778Seric 
28855173Seric #ifdef LOG
28963842Seric 			if (LogLevel > 11)
29055173Seric 			{
29155173Seric 				/* log connection information */
29255173Seric 				syslog(LOG_INFO, "connect from %s (%s)",
29358951Seric 					RealHostName, anynet_ntoa(&RealHostAddr));
29455173Seric 			}
29555173Seric #endif
29655173Seric 
29759254Seric 			(void) close(DaemonSocket);
29864724Seric 			if ((InChannel = fdopen(t, "r")) == NULL ||
29964724Seric 			    (t = dup(t)) < 0 ||
30064724Seric 			    (OutChannel = fdopen(t, "w")) == NULL)
30164724Seric 			{
30264724Seric 				syserr("cannot open SMTP server channel, fd=%d", t);
30364724Seric 				exit(0);
30464724Seric 			}
30559254Seric 
30616884Seric 			/* should we check for illegal connection here? XXX */
30759156Seric #ifdef XLA
30859156Seric 			if (!xla_host_ok(RealHostName))
30959156Seric 			{
31059254Seric 				message("421 Too many SMTP sessions for this host");
31159156Seric 				exit(0);
31259156Seric 			}
31359156Seric #endif
31416884Seric 
3157677Seric 			if (tTd(15, 2))
3165978Seric 				printf("getreq: returning\n");
3174636Seric 			return;
3184631Seric 		}
3194631Seric 
3207117Seric 		/* close the port so that others will hang (for a while) */
3219610Seric 		(void) close(t);
3224631Seric 	}
3239886Seric 	/*NOTREACHED*/
3244631Seric }
3255978Seric /*
32610206Seric **  CLRDAEMON -- reset the daemon connection
32710206Seric **
32810206Seric **	Parameters:
32910206Seric **		none.
33010206Seric **
33110206Seric **	Returns:
33210206Seric **		none.
33310206Seric **
33410206Seric **	Side Effects:
33510206Seric **		releases any resources used by the passive daemon.
33610206Seric */
33710206Seric 
33810206Seric clrdaemon()
33910206Seric {
34010206Seric 	if (DaemonSocket >= 0)
34110206Seric 		(void) close(DaemonSocket);
34210206Seric 	DaemonSocket = -1;
34310206Seric }
34410206Seric /*
34558849Seric **  SETDAEMONOPTIONS -- set options for running the daemon
34658849Seric **
34758849Seric **	Parameters:
34858849Seric **		p -- the options line.
34958849Seric **
35058849Seric **	Returns:
35158849Seric **		none.
35258849Seric */
35358849Seric 
35458849Seric setdaemonoptions(p)
35558849Seric 	register char *p;
35658849Seric {
35758873Seric 	if (DaemonAddr.sa.sa_family == AF_UNSPEC)
35858873Seric 		DaemonAddr.sa.sa_family = AF_INET;
35958873Seric 
36058849Seric 	while (p != NULL)
36158849Seric 	{
36258849Seric 		register char *f;
36358849Seric 		register char *v;
36458849Seric 
36558849Seric 		while (isascii(*p) && isspace(*p))
36658849Seric 			p++;
36758849Seric 		if (*p == '\0')
36858849Seric 			break;
36958849Seric 		f = p;
37058849Seric 		p = strchr(p, ',');
37158849Seric 		if (p != NULL)
37258849Seric 			*p++ = '\0';
37358849Seric 		v = strchr(f, '=');
37458849Seric 		if (v == NULL)
37558849Seric 			continue;
37658849Seric 		while (isascii(*++v) && isspace(*v))
37758849Seric 			continue;
37858849Seric 
37958849Seric 		switch (*f)
38058849Seric 		{
38158873Seric 		  case 'F':		/* address family */
38258849Seric 			if (isascii(*v) && isdigit(*v))
38358873Seric 				DaemonAddr.sa.sa_family = atoi(v);
38458873Seric #ifdef NETINET
38558873Seric 			else if (strcasecmp(v, "inet") == 0)
38658873Seric 				DaemonAddr.sa.sa_family = AF_INET;
38758873Seric #endif
38858873Seric #ifdef NETISO
38958873Seric 			else if (strcasecmp(v, "iso") == 0)
39058873Seric 				DaemonAddr.sa.sa_family = AF_ISO;
39158873Seric #endif
39258873Seric #ifdef NETNS
39358873Seric 			else if (strcasecmp(v, "ns") == 0)
39458873Seric 				DaemonAddr.sa.sa_family = AF_NS;
39558873Seric #endif
39658873Seric #ifdef NETX25
39758873Seric 			else if (strcasecmp(v, "x.25") == 0)
39858873Seric 				DaemonAddr.sa.sa_family = AF_CCITT;
39958873Seric #endif
40058849Seric 			else
40158873Seric 				syserr("554 Unknown address family %s in Family=option", v);
40258873Seric 			break;
40358873Seric 
40458873Seric 		  case 'A':		/* address */
40558873Seric 			switch (DaemonAddr.sa.sa_family)
40658849Seric 			{
40758873Seric #ifdef NETINET
40858873Seric 			  case AF_INET:
40958873Seric 				if (isascii(*v) && isdigit(*v))
41058873Seric 					DaemonAddr.sin.sin_addr.s_addr = inet_network(v);
41158873Seric 				else
41258873Seric 				{
41358873Seric 					register struct netent *np;
41458849Seric 
41558873Seric 					np = getnetbyname(v);
41658873Seric 					if (np == NULL)
41758873Seric 						syserr("554 network \"%s\" unknown", v);
41858873Seric 					else
41958873Seric 						DaemonAddr.sin.sin_addr.s_addr = np->n_net;
42058873Seric 				}
42158873Seric 				break;
42258873Seric #endif
42358873Seric 
42458873Seric 			  default:
42558873Seric 				syserr("554 Address= option unsupported for family %d",
42658873Seric 					DaemonAddr.sa.sa_family);
42758873Seric 				break;
42858849Seric 			}
42958849Seric 			break;
43058849Seric 
43158873Seric 		  case 'P':		/* port */
43258873Seric 			switch (DaemonAddr.sa.sa_family)
43358849Seric 			{
43458873Seric 				short port;
43558849Seric 
43658873Seric #ifdef NETINET
43758873Seric 			  case AF_INET:
43858873Seric 				if (isascii(*v) && isdigit(*v))
43964366Seric 					DaemonAddr.sin.sin_port = htons(atoi(v));
44058849Seric 				else
44158873Seric 				{
44258873Seric 					register struct servent *sp;
44358873Seric 
44458873Seric 					sp = getservbyname(v, "tcp");
44558873Seric 					if (sp == NULL)
44658909Seric 						syserr("554 service \"%s\" unknown", v);
44758873Seric 					else
44858873Seric 						DaemonAddr.sin.sin_port = sp->s_port;
44958873Seric 				}
45058873Seric 				break;
45158873Seric #endif
45258873Seric 
45358873Seric #ifdef NETISO
45458873Seric 			  case AF_ISO:
45558873Seric 				/* assume two byte transport selector */
45658873Seric 				if (isascii(*v) && isdigit(*v))
45764366Seric 					port = htons(atoi(v));
45858873Seric 				else
45958873Seric 				{
46058873Seric 					register struct servent *sp;
46158873Seric 
46258873Seric 					sp = getservbyname(v, "tcp");
46358873Seric 					if (sp == NULL)
46458909Seric 						syserr("554 service \"%s\" unknown", v);
46558873Seric 					else
46658873Seric 						port = sp->s_port;
46758873Seric 				}
46858873Seric 				bcopy((char *) &port, TSEL(&DaemonAddr.siso), 2);
46958873Seric 				break;
47058873Seric #endif
47158873Seric 
47258873Seric 			  default:
47358873Seric 				syserr("554 Port= option unsupported for family %d",
47458873Seric 					DaemonAddr.sa.sa_family);
47558873Seric 				break;
47658849Seric 			}
47758849Seric 			break;
47859783Seric 
47959783Seric 		  case 'L':		/* listen queue size */
48059783Seric 			ListenQueueSize = atoi(v);
48159783Seric 			break;
48264381Seric 
48364381Seric 		  case 'S':		/* send buffer size */
48464381Seric 			TcpSndBufferSize = atoi(v);
48564381Seric 			break;
48664381Seric 
48764381Seric 		  case 'R':		/* receive buffer size */
48864381Seric 			TcpRcvBufferSize = atoi(v);
48964381Seric 			break;
49058849Seric 		}
49158849Seric 	}
49258849Seric }
49358849Seric /*
4946039Seric **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
4956039Seric **
4966039Seric **	Parameters:
4976039Seric **		host -- the name of the host.
4986633Seric **		port -- the port number to connect to.
49953739Seric **		mci -- a pointer to the mail connection information
50053739Seric **			structure to be filled in.
50152106Seric **		usesecureport -- if set, use a low numbered (reserved)
50252106Seric **			port to provide some rudimentary authentication.
5036039Seric **
5046039Seric **	Returns:
5056039Seric **		An exit code telling whether the connection could be
5066039Seric **			made and if not why not.
5076039Seric **
5086039Seric **	Side Effects:
5096039Seric **		none.
5106039Seric */
5115978Seric 
51258755Seric SOCKADDR	CurHostAddr;		/* address of current host */
51358305Seric 
51454967Seric int
51553739Seric makeconnection(host, port, mci, usesecureport)
5166039Seric 	char *host;
5177286Seric 	u_short port;
51854967Seric 	register MCI *mci;
51952106Seric 	bool usesecureport;
5206039Seric {
52129430Sbloom 	register int i, s;
52229430Sbloom 	register struct hostent *hp = (struct hostent *)NULL;
52358755Seric 	SOCKADDR addr;
52452106Seric 	int sav_errno;
52558755Seric 	int addrlen;
52666334Seric #if NAMED_BIND
52735651Seric 	extern int h_errno;
52835651Seric #endif
5296039Seric 
5306039Seric 	/*
5316039Seric 	**  Set up the address for the mailer.
5329308Seric 	**	Accept "[a.b.c.d]" syntax for host name.
5336039Seric 	*/
5346039Seric 
53566334Seric #if NAMED_BIND
53625475Smiriam 	h_errno = 0;
53735651Seric #endif
53825475Smiriam 	errno = 0;
53958864Seric 	bzero(&CurHostAddr, sizeof CurHostAddr);
54064334Seric 	SmtpPhase = mci->mci_phase = "initial connection";
54158906Seric 	CurHostName = host;
54225475Smiriam 
5439308Seric 	if (host[0] == '[')
5449308Seric 	{
54511147Seric 		long hid;
54656795Seric 		register char *p = strchr(host, ']');
5479308Seric 
54811147Seric 		if (p != NULL)
5499308Seric 		{
55011147Seric 			*p = '\0';
55159884Seric #ifdef NETINET
55211147Seric 			hid = inet_addr(&host[1]);
55358360Seric 			if (hid == -1)
55459884Seric #endif
55558360Seric 			{
55658360Seric 				/* try it as a host name (avoid MX lookup) */
55758360Seric 				hp = gethostbyname(&host[1]);
558*66349Seric 				if (hp == NULL && p[-1] == '.')
559*66349Seric 				{
560*66349Seric 					p[-1] = '\0';
561*66349Seric 					hp = gethostbyname(&host[1]);
562*66349Seric 					p[-1] = '.';
563*66349Seric 				}
56458360Seric 				*p = ']';
56558360Seric 				goto gothostent;
56658360Seric 			}
56711147Seric 			*p = ']';
5689308Seric 		}
56958360Seric 		if (p == NULL)
5709308Seric 		{
57158151Seric 			usrerr("553 Invalid numeric domain spec \"%s\"", host);
5729308Seric 			return (EX_NOHOST);
5739308Seric 		}
57459884Seric #ifdef NETINET
57559884Seric 		addr.sin.sin_family = AF_INET;		/*XXX*/
57658778Seric 		addr.sin.sin_addr.s_addr = hid;
57759884Seric #endif
5789308Seric 	}
5799610Seric 	else
5809610Seric 	{
581*66349Seric 		register char *p = &host[strlen(host) - 1];
582*66349Seric 
58329430Sbloom 		hp = gethostbyname(host);
584*66349Seric 		if (hp == NULL && *p == '.')
585*66349Seric 		{
586*66349Seric 			*p = '\0';
587*66349Seric 			hp = gethostbyname(host);
588*66349Seric 			*p = '.';
589*66349Seric 		}
59058360Seric gothostent:
59125475Smiriam 		if (hp == NULL)
59224945Seric 		{
59366334Seric #if NAMED_BIND
59425475Smiriam 			if (errno == ETIMEDOUT || h_errno == TRY_AGAIN)
59525475Smiriam 				return (EX_TEMPFAIL);
59625657Seric 
59735651Seric 			/* if name server is specified, assume temp fail */
59835651Seric 			if (errno == ECONNREFUSED && UseNameServer)
59935651Seric 				return (EX_TEMPFAIL);
60035651Seric #endif
60125475Smiriam 			return (EX_NOHOST);
60224945Seric 		}
60358778Seric 		addr.sa.sa_family = hp->h_addrtype;
60458778Seric 		switch (hp->h_addrtype)
60558778Seric 		{
60658778Seric #ifdef NETINET
60758778Seric 		  case AF_INET:
60858755Seric 			bcopy(hp->h_addr,
60958778Seric 				&addr.sin.sin_addr,
61064943Seric 				sizeof addr.sin.sin_addr);
61158778Seric 			break;
61258778Seric #endif
61358778Seric 
61458778Seric 		  default:
61558755Seric 			bcopy(hp->h_addr,
61658778Seric 				addr.sa.sa_data,
61758755Seric 				hp->h_length);
61858778Seric 			break;
61958778Seric 		}
62029430Sbloom 		i = 1;
6219610Seric 	}
6229610Seric 
6239610Seric 	/*
6249610Seric 	**  Determine the port number.
6259610Seric 	*/
6269610Seric 
62710011Seric 	if (port != 0)
62858755Seric 		port = htons(port);
62910011Seric 	else
6309610Seric 	{
6319610Seric 		register struct servent *sp = getservbyname("smtp", "tcp");
6329610Seric 
6339610Seric 		if (sp == NULL)
6349610Seric 		{
63558909Seric 			syserr("554 makeconnection: service \"smtp\" unknown");
63665169Seric 			port = htons(25);
6379610Seric 		}
63865169Seric 		else
63965169Seric 			port = sp->s_port;
6409610Seric 	}
6416039Seric 
64258778Seric 	switch (addr.sa.sa_family)
64358755Seric 	{
64459884Seric #ifdef NETINET
64558755Seric 	  case AF_INET:
64658778Seric 		addr.sin.sin_port = port;
64758755Seric 		addrlen = sizeof (struct sockaddr_in);
64858755Seric 		break;
64959884Seric #endif
65058755Seric 
65158755Seric #ifdef NETISO
65258755Seric 	  case AF_ISO:
65358755Seric 		/* assume two byte transport selector */
65458755Seric 		bcopy((char *) &port, TSEL((struct sockaddr_iso *) &addr), 2);
65558755Seric 		addrlen = sizeof (struct sockaddr_iso);
65658755Seric 		break;
65758755Seric #endif
65858755Seric 
65958755Seric 	  default:
66058778Seric 		syserr("Can't connect to address family %d", addr.sa.sa_family);
66158755Seric 		return (EX_NOHOST);
66258755Seric 	}
66358755Seric 
6646039Seric 	/*
6656039Seric 	**  Try to actually open the connection.
6666039Seric 	*/
6676039Seric 
66859156Seric #ifdef XLA
66959156Seric 	/* if too many connections, don't bother trying */
67059156Seric 	if (!xla_noqueue_ok(host))
67159156Seric 		return EX_TEMPFAIL;
67259156Seric #endif
67359156Seric 
67457736Seric 	for (;;)
67552106Seric 	{
67657736Seric 		if (tTd(16, 1))
67758755Seric 			printf("makeconnection (%s [%s])\n",
67858755Seric 				host, anynet_ntoa(&addr));
67952106Seric 
68058588Seric 		/* save for logging */
68158588Seric 		CurHostAddr = addr;
68258588Seric 
68357736Seric 		if (usesecureport)
68457736Seric 		{
68557736Seric 			int rport = IPPORT_RESERVED - 1;
6866039Seric 
68757736Seric 			s = rresvport(&rport);
68857736Seric 		}
68957736Seric 		else
69057736Seric 		{
69157736Seric 			s = socket(AF_INET, SOCK_STREAM, 0);
69257736Seric 		}
69357736Seric 		if (s < 0)
69457736Seric 		{
69557736Seric 			sav_errno = errno;
69657736Seric 			syserr("makeconnection: no socket");
69757736Seric 			goto failure;
69857736Seric 		}
69910347Seric 
70064381Seric #ifdef SO_SNDBUF
70164381Seric 		if (TcpSndBufferSize > 0)
70264381Seric 		{
70364381Seric 			if (setsockopt(s, SOL_SOCKET, SO_SNDBUF,
70464561Seric 				       (char *) &TcpSndBufferSize,
70564381Seric 				       sizeof(TcpSndBufferSize)) < 0)
70664381Seric 				syserr("makeconnection: setsockopt(SO_SNDBUF)");
70764381Seric 		}
70864381Seric #endif
70964381Seric 
71057736Seric 		if (tTd(16, 1))
71157736Seric 			printf("makeconnection: fd=%d\n", s);
71257736Seric 
71357736Seric 		/* turn on network debugging? */
71457736Seric 		if (tTd(16, 101))
71557736Seric 		{
71657736Seric 			int on = 1;
71757736Seric 			(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG,
71857736Seric 					  (char *)&on, sizeof on);
71957736Seric 		}
72057736Seric 		if (CurEnv->e_xfp != NULL)
72157736Seric 			(void) fflush(CurEnv->e_xfp);		/* for debugging */
72257736Seric 		errno = 0;					/* for debugging */
72358755Seric 		if (connect(s, (struct sockaddr *) &addr, addrlen) >= 0)
72457736Seric 			break;
72557736Seric 
72657736Seric 		/* couldn't connect.... figure out why */
72727744Sbloom 		sav_errno = errno;
72827744Sbloom 		(void) close(s);
72929430Sbloom 		if (hp && hp->h_addr_list[i])
73029430Sbloom 		{
73157736Seric 			if (tTd(16, 1))
73258755Seric 				printf("Connect failed (%s); trying new address....\n",
73358755Seric 					errstring(sav_errno));
73458778Seric 			switch (addr.sa.sa_family)
73558778Seric 			{
73658778Seric #ifdef NETINET
73758778Seric 			  case AF_INET:
73858755Seric 				bcopy(hp->h_addr_list[i++],
73958778Seric 				      &addr.sin.sin_addr,
74064943Seric 				      sizeof addr.sin.sin_addr);
74158778Seric 				break;
74258778Seric #endif
74358778Seric 
74458778Seric 			  default:
74558755Seric 				bcopy(hp->h_addr_list[i++],
74658778Seric 					addr.sa.sa_data,
74752106Seric 					hp->h_length);
74858778Seric 				break;
74958778Seric 			}
75057736Seric 			continue;
75129430Sbloom 		}
75229430Sbloom 
7536039Seric 		/* failure, decide if temporary or not */
7546039Seric 	failure:
75559254Seric #ifdef XLA
75659254Seric 		xla_host_end(host);
75759254Seric #endif
75858542Seric 		if (transienterror(sav_errno))
75958542Seric 			return EX_TEMPFAIL;
76058542Seric 		else
76158542Seric 		{
76258542Seric 			message("%s", errstring(sav_errno));
76358542Seric 			return (EX_UNAVAILABLE);
7646039Seric 		}
7656039Seric 	}
7666039Seric 
7676039Seric 	/* connection ok, put it into canonical form */
76864724Seric 	if ((mci->mci_out = fdopen(s, "w")) == NULL ||
76964724Seric 	    (s = dup(s)) < 0 ||
77064725Seric 	    (mci->mci_in = fdopen(s, "r")) == NULL)
77164724Seric 	{
77264724Seric 		syserr("cannot open SMTP client channel, fd=%d", s);
77364724Seric 		return EX_TEMPFAIL;
77464724Seric 	}
7756039Seric 
77610098Seric 	return (EX_OK);
7776039Seric }
77810758Seric /*
77910758Seric **  MYHOSTNAME -- return the name of this host.
78010758Seric **
78110758Seric **	Parameters:
78210758Seric **		hostbuf -- a place to return the name of this host.
78312313Seric **		size -- the size of hostbuf.
78410758Seric **
78510758Seric **	Returns:
78610758Seric **		A list of aliases for this host.
78710758Seric **
78810758Seric **	Side Effects:
78964338Seric **		Adds numeric codes to $=w.
79010758Seric */
7916039Seric 
79210758Seric char **
79312313Seric myhostname(hostbuf, size)
79410758Seric 	char hostbuf[];
79512313Seric 	int size;
79610758Seric {
79758110Seric 	register struct hostent *hp;
79810758Seric 	extern struct hostent *gethostbyname();
79910758Seric 
80023120Seric 	if (gethostname(hostbuf, size) < 0)
80123120Seric 	{
80223120Seric 		(void) strcpy(hostbuf, "localhost");
80323120Seric 	}
80411147Seric 	hp = gethostbyname(hostbuf);
80511147Seric 	if (hp != NULL)
80616877Seric 	{
80758110Seric 		(void) strncpy(hostbuf, hp->h_name, size - 1);
80858110Seric 		hostbuf[size - 1] = '\0';
80958110Seric 
81058110Seric 		if (hp->h_addrtype == AF_INET && hp->h_length == 4)
81158110Seric 		{
81258110Seric 			register int i;
81358110Seric 
81464338Seric 			for (i = 0; hp->h_addr_list[i] != NULL; i++)
81558110Seric 			{
81664338Seric 				char ipbuf[100];
81764338Seric 
81864338Seric 				sprintf(ipbuf, "[%s]",
81964338Seric 					inet_ntoa(*((struct in_addr *) hp->h_addr_list[i])));
82064338Seric 				setclass('w', ipbuf);
82158110Seric 			}
82258110Seric 		}
82358110Seric 
82411147Seric 		return (hp->h_aliases);
82516877Seric 	}
82610758Seric 	else
82710758Seric 		return (NULL);
82810758Seric }
82951315Seric /*
83058951Seric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
83158308Seric **
83258951Seric **	Uses RFC1413 protocol to try to get info from the other end.
83358951Seric **
83458308Seric **	Parameters:
83558308Seric **		fd -- the descriptor
83658308Seric **
83758308Seric **	Returns:
83858951Seric **		The user@host information associated with this descriptor.
83958308Seric */
84058308Seric 
84164927Seric #if IDENTPROTO
84258951Seric 
84358951Seric static jmp_buf	CtxAuthTimeout;
84458951Seric 
84558951Seric static
84658951Seric authtimeout()
84758951Seric {
84858951Seric 	longjmp(CtxAuthTimeout, 1);
84958951Seric }
85058951Seric 
85158951Seric #endif
85258951Seric 
85358308Seric char *
85458951Seric getauthinfo(fd)
85558308Seric 	int fd;
85658308Seric {
85758951Seric 	SOCKADDR fa;
85858951Seric 	int falen;
85959104Seric 	register char *p;
86064927Seric #if IDENTPROTO
86158951Seric 	SOCKADDR la;
86258951Seric 	int lalen;
86358951Seric 	register struct servent *sp;
86458951Seric 	int s;
86558951Seric 	int i;
86658951Seric 	EVENT *ev;
86758951Seric #endif
86858951Seric 	static char hbuf[MAXNAME * 2 + 2];
86958951Seric 	extern char *hostnamebyanyaddr();
87058951Seric 	extern char RealUserName[];			/* main.c */
87158308Seric 
87258951Seric 	falen = sizeof fa;
87364845Seric 	if (getpeername(fd, &fa.sa, &falen) < 0 || falen <= 0 ||
87464845Seric 	    fa.sa.sa_family == 0)
87558951Seric 	{
87658951Seric 		(void) sprintf(hbuf, "%s@localhost", RealUserName);
87758957Seric 		if (tTd(9, 1))
87858951Seric 			printf("getauthinfo: %s\n", hbuf);
87958951Seric 		return hbuf;
88058951Seric 	}
88158951Seric 
88264927Seric #if IDENTPROTO
88365831Seric 	if (TimeOuts.to_ident == 0)
88465831Seric 		goto noident;
88565831Seric 
88658951Seric 	lalen = sizeof la;
88758951Seric 	if (fa.sa.sa_family != AF_INET ||
88858951Seric 	    getsockname(fd, &la.sa, &lalen) < 0 || lalen <= 0 ||
88958951Seric 	    la.sa.sa_family != AF_INET)
89058951Seric 	{
89158951Seric 		/* no ident info */
89258951Seric 		goto noident;
89358951Seric 	}
89458951Seric 
89558951Seric 	/* create ident query */
89660489Seric 	(void) sprintf(hbuf, "%d,%d\r\n",
89760489Seric 		ntohs(fa.sin.sin_port), ntohs(la.sin.sin_port));
89858951Seric 
89958951Seric 	/* create local address */
90064747Seric 	la.sin.sin_port = 0;
90158951Seric 
90258951Seric 	/* create foreign address */
90358951Seric 	sp = getservbyname("auth", "tcp");
90458951Seric 	if (sp != NULL)
90558951Seric 		fa.sin.sin_port = sp->s_port;
90658308Seric 	else
90759097Seric 		fa.sin.sin_port = htons(113);
90858951Seric 
90958951Seric 	s = -1;
91058951Seric 	if (setjmp(CtxAuthTimeout) != 0)
91158951Seric 	{
91258951Seric 		if (s >= 0)
91358951Seric 			(void) close(s);
91458951Seric 		goto noident;
91558951Seric 	}
91658951Seric 
91758951Seric 	/* put a timeout around the whole thing */
91864255Seric 	ev = setevent(TimeOuts.to_ident, authtimeout, 0);
91958951Seric 
92064747Seric 	/* connect to foreign IDENT server using same address as SMTP socket */
92158951Seric 	s = socket(AF_INET, SOCK_STREAM, 0);
92258951Seric 	if (s < 0)
92358951Seric 	{
92458951Seric 		clrevent(ev);
92558951Seric 		goto noident;
92658951Seric 	}
92764747Seric 	if (bind(s, &la.sa, sizeof la.sin) < 0 ||
92864747Seric 	    connect(s, &fa.sa, sizeof fa.sin) < 0)
92958951Seric 	{
93066011Seric 		goto closeident;
93158951Seric 	}
93258951Seric 
93358957Seric 	if (tTd(9, 10))
93458951Seric 		printf("getauthinfo: sent %s", hbuf);
93558951Seric 
93658951Seric 	/* send query */
93758951Seric 	if (write(s, hbuf, strlen(hbuf)) < 0)
93858951Seric 		goto closeident;
93958951Seric 
94058951Seric 	/* get result */
94158951Seric 	i = read(s, hbuf, sizeof hbuf);
94258951Seric 	(void) close(s);
94358951Seric 	clrevent(ev);
94458951Seric 	if (i <= 0)
94558951Seric 		goto noident;
94658951Seric 	if (hbuf[--i] == '\n' && hbuf[--i] == '\r')
94758951Seric 		i--;
94858951Seric 	hbuf[++i] = '\0';
94958951Seric 
95058957Seric 	if (tTd(9, 3))
95158951Seric 		printf("getauthinfo:  got %s\n", hbuf);
95258951Seric 
95358951Seric 	/* parse result */
95458951Seric 	p = strchr(hbuf, ':');
95558951Seric 	if (p == NULL)
95658951Seric 	{
95758951Seric 		/* malformed response */
95858951Seric 		goto noident;
95958951Seric 	}
96058951Seric 	while (isascii(*++p) && isspace(*p))
96158951Seric 		continue;
96258951Seric 	if (strncasecmp(p, "userid", 6) != 0)
96358951Seric 	{
96458951Seric 		/* presumably an error string */
96558951Seric 		goto noident;
96658951Seric 	}
96758951Seric 	p += 6;
96858951Seric 	while (isascii(*p) && isspace(*p))
96958951Seric 		p++;
97058951Seric 	if (*p++ != ':')
97158951Seric 	{
97258951Seric 		/* either useridxx or malformed response */
97358951Seric 		goto noident;
97458951Seric 	}
97558951Seric 
97658951Seric 	/* p now points to the OSTYPE field */
97758951Seric 	p = strchr(p, ':');
97858951Seric 	if (p == NULL)
97958951Seric 	{
98058951Seric 		/* malformed response */
98158951Seric 		goto noident;
98258951Seric 	}
98358951Seric 
98458957Seric 	/* 1413 says don't do this -- but it's broken otherwise */
98558957Seric 	while (isascii(*++p) && isspace(*p))
98658957Seric 		continue;
98758957Seric 
98858951Seric 	/* p now points to the authenticated name */
98966003Seric 	(void) sprintf(hbuf, "%s@%s",
99066003Seric 		p, RealHostName == NULL ? "localhost" : RealHostName);
99158957Seric 	goto finish;
99258957Seric 
99366011Seric closeident:
99466011Seric 	(void) close(s);
99566011Seric 	clrevent(ev);
99666011Seric 
99758957Seric #endif /* IDENTPROTO */
99858957Seric 
99958957Seric noident:
100066003Seric 	if (RealHostName == NULL)
100166003Seric 	{
100266003Seric 		if (tTd(9, 1))
100366003Seric 			printf("getauthinfo: NULL\n");
100466003Seric 		return NULL;
100566003Seric 	}
100658957Seric 	(void) strcpy(hbuf, RealHostName);
100758957Seric 
100858957Seric finish:
100966003Seric 	if (RealHostName != NULL && RealHostName[0] != '[')
101058951Seric 	{
101158951Seric 		p = &hbuf[strlen(hbuf)];
101258951Seric 		(void) sprintf(p, " [%s]", anynet_ntoa(&RealHostAddr));
101358951Seric 	}
101458957Seric 	if (tTd(9, 1))
101558951Seric 		printf("getauthinfo: %s\n", hbuf);
101658308Seric 	return hbuf;
101758308Seric }
101858308Seric /*
101960089Seric **  HOST_MAP_LOOKUP -- turn a hostname into canonical form
102053751Seric **
102153751Seric **	Parameters:
102256823Seric **		map -- a pointer to this map (unused).
102360089Seric **		name -- the (presumably unqualified) hostname.
102460257Seric **		av -- unused -- for compatibility with other mapping
102555019Seric **			functions.
102659084Seric **		statp -- an exit status (out parameter) -- set to
102759084Seric **			EX_TEMPFAIL if the name server is unavailable.
102853751Seric **
102953751Seric **	Returns:
103053751Seric **		The mapping, if found.
103153751Seric **		NULL if no mapping found.
103253751Seric **
103353751Seric **	Side Effects:
103453751Seric **		Looks up the host specified in hbuf.  If it is not
103553751Seric **		the canonical name for that host, return the canonical
103653751Seric **		name.
103753751Seric */
103851315Seric 
103953751Seric char *
104060257Seric host_map_lookup(map, name, av, statp)
104156823Seric 	MAP *map;
104260089Seric 	char *name;
104360257Seric 	char **av;
104459084Seric 	int *statp;
104516911Seric {
104616911Seric 	register struct hostent *hp;
104733932Sbostic 	u_long in_addr;
104856823Seric 	char *cp;
104958110Seric 	int i;
105059671Seric 	register STAB *s;
105160257Seric 	char hbuf[MAXNAME];
105259671Seric 	extern struct hostent *gethostbyaddr();
105366334Seric #if NAMED_BIND
105459671Seric 	extern int h_errno;
105566029Seric #endif
105616911Seric 
105725574Smiriam 	/*
105859671Seric 	**  See if we have already looked up this name.  If so, just
105959671Seric 	**  return it.
106059671Seric 	*/
106153751Seric 
106260089Seric 	s = stab(name, ST_NAMECANON, ST_ENTER);
106359671Seric 	if (bitset(NCF_VALID, s->s_namecanon.nc_flags))
106459671Seric 	{
106559986Seric 		if (tTd(9, 1))
106660089Seric 			printf("host_map_lookup(%s) => CACHE %s\n",
106760089Seric 				name, s->s_namecanon.nc_cname);
106859671Seric 		errno = s->s_namecanon.nc_errno;
106966334Seric #if NAMED_BIND
107059671Seric 		h_errno = s->s_namecanon.nc_herrno;
107166029Seric #endif
107259671Seric 		*statp = s->s_namecanon.nc_stat;
107364797Seric 		if (CurEnv->e_message == NULL && *statp == EX_TEMPFAIL)
107465199Seric 		{
107565199Seric 			sprintf(hbuf, "%s: Name server timeout",
107665199Seric 				shortenstring(name, 33));
107765199Seric 			CurEnv->e_message = newstr(hbuf);
107865199Seric 		}
107959671Seric 		return s->s_namecanon.nc_cname;
108059671Seric 	}
108159671Seric 
108259671Seric 	/*
108359671Seric 	**  If first character is a bracket, then it is an address
108459671Seric 	**  lookup.  Address is copied into a temporary buffer to
108560089Seric 	**  strip the brackets and to preserve name if address is
108659671Seric 	**  unknown.
108759671Seric 	*/
108859671Seric 
108960089Seric 	if (*name != '[')
109053751Seric 	{
109155019Seric 		extern bool getcanonname();
109255019Seric 
109358798Seric 		if (tTd(9, 1))
109460089Seric 			printf("host_map_lookup(%s) => ", name);
109559671Seric 		s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
109660089Seric 		(void) strcpy(hbuf, name);
109763842Seric 		if (getcanonname(hbuf, sizeof hbuf - 1, TRUE))
109858796Seric 		{
109958796Seric 			if (tTd(9, 1))
110058796Seric 				printf("%s\n", hbuf);
110160257Seric 			cp = map_rewrite(map, hbuf, strlen(hbuf), av);
110260257Seric 			s->s_namecanon.nc_cname = newstr(cp);
110360257Seric 			return cp;
110458796Seric 		}
110553751Seric 		else
110658796Seric 		{
110759084Seric 			register struct hostent *hp;
110859084Seric 
110966029Seric 			s->s_namecanon.nc_errno = errno;
111066334Seric #if NAMED_BIND
111166029Seric 			s->s_namecanon.nc_herrno = h_errno;
111258796Seric 			if (tTd(9, 1))
111359084Seric 				printf("FAIL (%d)\n", h_errno);
111459084Seric 			switch (h_errno)
111559084Seric 			{
111659084Seric 			  case TRY_AGAIN:
111759596Seric 				if (UseNameServer)
111859734Seric 				{
111965202Seric 					sprintf(hbuf, "%s: Name server timeout",
112065199Seric 						shortenstring(name, 33));
112165202Seric 					message("%s", hbuf);
112259734Seric 					if (CurEnv->e_message == NULL)
112365202Seric 						CurEnv->e_message = newstr(hbuf);
112459734Seric 				}
112559084Seric 				*statp = EX_TEMPFAIL;
112659084Seric 				break;
112759084Seric 
112859084Seric 			  case HOST_NOT_FOUND:
112959084Seric 				*statp = EX_NOHOST;
113059084Seric 				break;
113159084Seric 
113259084Seric 			  case NO_RECOVERY:
113359084Seric 				*statp = EX_SOFTWARE;
113459084Seric 				break;
113559084Seric 
113659084Seric 			  default:
113759084Seric 				*statp = EX_UNAVAILABLE;
113859084Seric 				break;
113959084Seric 			}
114066029Seric #else
114166029Seric 			if (tTd(9, 1))
114266029Seric 				printf("FAIL\n");
114366029Seric 			*statp = EX_NOHOST;
114466029Seric #endif
114559671Seric 			s->s_namecanon.nc_stat = *statp;
114659084Seric 			if (*statp != EX_TEMPFAIL || UseNameServer)
114759084Seric 				return NULL;
114859084Seric 
114959084Seric 			/*
115059084Seric 			**  Try to look it up in /etc/hosts
115159084Seric 			*/
115259084Seric 
115360089Seric 			hp = gethostbyname(name);
115459084Seric 			if (hp == NULL)
115559084Seric 			{
115659084Seric 				/* no dice there either */
115759671Seric 				s->s_namecanon.nc_stat = *statp = EX_NOHOST;
115859084Seric 				return NULL;
115959084Seric 			}
116059084Seric 
116159671Seric 			s->s_namecanon.nc_stat = *statp = EX_OK;
116260257Seric 			cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
116360257Seric 			s->s_namecanon.nc_cname = newstr(cp);
116460257Seric 			return cp;
116558796Seric 		}
116653751Seric 	}
116760089Seric 	if ((cp = strchr(name, ']')) == NULL)
116853751Seric 		return (NULL);
116940994Sbostic 	*cp = '\0';
117060089Seric 	in_addr = inet_addr(&name[1]);
117158110Seric 
117258110Seric 	/* nope -- ask the name server */
117333932Sbostic 	hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET);
117459671Seric 	s->s_namecanon.nc_errno = errno;
117566334Seric #if NAMED_BIND
117659671Seric 	s->s_namecanon.nc_herrno = h_errno;
117766029Seric #endif
117859671Seric 	s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
117933932Sbostic 	if (hp == NULL)
118059671Seric 	{
118159671Seric 		s->s_namecanon.nc_stat = *statp = EX_NOHOST;
118253751Seric 		return (NULL);
118359671Seric 	}
118453751Seric 
118558110Seric 	/* found a match -- copy out */
118660257Seric 	cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
118759671Seric 	s->s_namecanon.nc_stat = *statp = EX_OK;
118860257Seric 	s->s_namecanon.nc_cname = newstr(cp);
118960257Seric 	return cp;
119033932Sbostic }
119158755Seric /*
119258755Seric **  ANYNET_NTOA -- convert a network address to printable form.
119358755Seric **
119458755Seric **	Parameters:
119558755Seric **		sap -- a pointer to a sockaddr structure.
119658755Seric **
119758755Seric **	Returns:
119858755Seric **		A printable version of that sockaddr.
119958755Seric */
120016911Seric 
120158755Seric char *
120258755Seric anynet_ntoa(sap)
120358755Seric 	register SOCKADDR *sap;
120458755Seric {
120558755Seric 	register char *bp;
120658755Seric 	register char *ap;
120758755Seric 	int l;
120864734Seric 	static char buf[100];
120958755Seric 
121058798Seric 	/* check for null/zero family */
121158798Seric 	if (sap == NULL)
121258798Seric 		return "NULLADDR";
121358798Seric 	if (sap->sa.sa_family == 0)
121458798Seric 		return "0";
121558798Seric 
121664734Seric 	switch (sap->sa.sa_family)
121764734Seric 	{
121864734Seric #ifdef MAYBENEXTRELEASE		/*** UNTESTED *** UNTESTED *** UNTESTED ***/
121964821Seric #ifdef NETUNIX
122064734Seric 	  case AF_UNIX:
122164758Seric 	  	if (sap->sunix.sun_path[0] != '\0')
122264758Seric 	  		sprintf(buf, "[UNIX: %.64s]", sap->sunix.sun_path);
122364734Seric 	  	else
122464734Seric 	  		sprintf(buf, "[UNIX: localhost]");
122564734Seric 		return buf;
122664734Seric #endif
122764821Seric #endif
122864734Seric 
122958778Seric #ifdef NETINET
123064734Seric 	  case AF_INET:
123158755Seric 		return inet_ntoa(((struct sockaddr_in *) sap)->sin_addr);
123258778Seric #endif
123358755Seric 
123464734Seric 	  default:
123564734Seric 	  	/* this case is only to ensure syntactic correctness */
123664734Seric 	  	break;
123764734Seric 	}
123864734Seric 
123958755Seric 	/* unknown family -- just dump bytes */
124058778Seric 	(void) sprintf(buf, "Family %d: ", sap->sa.sa_family);
124158755Seric 	bp = &buf[strlen(buf)];
124258778Seric 	ap = sap->sa.sa_data;
124358778Seric 	for (l = sizeof sap->sa.sa_data; --l >= 0; )
124458755Seric 	{
124558755Seric 		(void) sprintf(bp, "%02x:", *ap++ & 0377);
124658755Seric 		bp += 3;
124758755Seric 	}
124858755Seric 	*--bp = '\0';
124958755Seric 	return buf;
125058755Seric }
125158951Seric /*
125258951Seric **  HOSTNAMEBYANYADDR -- return name of host based on address
125358951Seric **
125458951Seric **	Parameters:
125558951Seric **		sap -- SOCKADDR pointer
125658951Seric **
125758951Seric **	Returns:
125858951Seric **		text representation of host name.
125958951Seric **
126058951Seric **	Side Effects:
126158951Seric **		none.
126258951Seric */
126358755Seric 
126458951Seric char *
126558951Seric hostnamebyanyaddr(sap)
126658951Seric 	register SOCKADDR *sap;
126758951Seric {
126858951Seric 	register struct hostent *hp;
126964734Seric 	int saveretry;
127058951Seric 
127166334Seric #if NAMED_BIND
127259042Seric 	/* shorten name server timeout to avoid higher level timeouts */
127359042Seric 	saveretry = _res.retry;
127459042Seric 	_res.retry = 3;
127559042Seric #endif /* NAMED_BIND */
127659042Seric 
127758951Seric 	switch (sap->sa.sa_family)
127858951Seric 	{
127958951Seric #ifdef NETINET
128058951Seric 	  case AF_INET:
128158951Seric 		hp = gethostbyaddr((char *) &sap->sin.sin_addr,
128258951Seric 			sizeof sap->sin.sin_addr,
128358951Seric 			AF_INET);
128458951Seric 		break;
128558951Seric #endif
128658951Seric 
128758951Seric #ifdef NETISO
128858951Seric 	  case AF_ISO:
128958951Seric 		hp = gethostbyaddr((char *) &sap->siso.siso_addr,
129058951Seric 			sizeof sap->siso.siso_addr,
129158951Seric 			AF_ISO);
129258951Seric 		break;
129358951Seric #endif
129458951Seric 
129564734Seric #ifdef MAYBENEXTRELEASE		/*** UNTESTED *** UNTESTED *** UNTESTED ***/
129664734Seric 	  case AF_UNIX:
129764734Seric 		hp = NULL;
129864734Seric 		break;
129964734Seric #endif
130064734Seric 
130158951Seric 	  default:
130258951Seric 		hp = gethostbyaddr(sap->sa.sa_data,
130358951Seric 			   sizeof sap->sa.sa_data,
130458951Seric 			   sap->sa.sa_family);
130558951Seric 		break;
130658951Seric 	}
130758951Seric 
130866334Seric #if NAMED_BIND
130959042Seric 	_res.retry = saveretry;
131059042Seric #endif /* NAMED_BIND */
131159042Seric 
131258951Seric 	if (hp != NULL)
131358951Seric 		return hp->h_name;
131458951Seric 	else
131558951Seric 	{
131658951Seric 		/* produce a dotted quad */
131758951Seric 		static char buf[512];
131858951Seric 
131958951Seric 		(void) sprintf(buf, "[%s]", anynet_ntoa(sap));
132058951Seric 		return buf;
132158951Seric 	}
132258951Seric }
132358951Seric 
132456795Seric # else /* DAEMON */
132516911Seric /* code for systems without sophisticated networking */
132610758Seric 
132710758Seric /*
132810758Seric **  MYHOSTNAME -- stub version for case of no daemon code.
132911297Seric **
133011297Seric **	Can't convert to upper case here because might be a UUCP name.
133112313Seric **
133212313Seric **	Mark, you can change this to be anything you want......
133310758Seric */
133410758Seric 
133510758Seric char **
133612313Seric myhostname(hostbuf, size)
133710758Seric 	char hostbuf[];
133812313Seric 	int size;
133910758Seric {
134010758Seric 	register FILE *f;
134110758Seric 
134210758Seric 	hostbuf[0] = '\0';
134310758Seric 	f = fopen("/usr/include/whoami", "r");
134410758Seric 	if (f != NULL)
134510758Seric 	{
134612313Seric 		(void) fgets(hostbuf, size, f);
134710758Seric 		fixcrlf(hostbuf, TRUE);
134810758Seric 		(void) fclose(f);
134910758Seric 	}
135010758Seric 	return (NULL);
135110758Seric }
135216911Seric /*
135358951Seric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
135458308Seric **
135558308Seric **	Parameters:
135658308Seric **		fd -- the descriptor
135758308Seric **
135858308Seric **	Returns:
135958308Seric **		The host name associated with this descriptor, if it can
136058308Seric **			be determined.
136158308Seric **		NULL otherwise.
136258308Seric **
136358308Seric **	Side Effects:
136458308Seric **		none
136558308Seric */
136658308Seric 
136758308Seric char *
136858951Seric getauthinfo(fd)
136958308Seric 	int fd;
137058308Seric {
137158308Seric 	return NULL;
137258308Seric }
137358308Seric /*
137416911Seric **  MAPHOSTNAME -- turn a hostname into canonical form
137516911Seric **
137616911Seric **	Parameters:
137756823Seric **		map -- a pointer to the database map.
137860089Seric **		name -- a buffer containing a hostname.
137953751Seric **		avp -- a pointer to a (cf file defined) argument vector.
138059084Seric **		statp -- an exit status (out parameter).
138116911Seric **
138216911Seric **	Returns:
138353751Seric **		mapped host name
138451315Seric **		FALSE otherwise.
138516911Seric **
138616911Seric **	Side Effects:
138760089Seric **		Looks up the host specified in name.  If it is not
138816911Seric **		the canonical name for that host, replace it with
138916911Seric **		the canonical name.  If the name is unknown, or it
139016911Seric **		is already the canonical name, leave it unchanged.
139116911Seric */
139210758Seric 
139316911Seric /*ARGSUSED*/
139453751Seric char *
139560089Seric host_map_lookup(map, name, avp, statp)
139656823Seric 	MAP *map;
139760089Seric 	char *name;
139853751Seric 	char **avp;
139959084Seric 	char *statp;
140016911Seric {
140159084Seric 	register struct hostent *hp;
140259084Seric 
140360089Seric 	hp = gethostbyname(name);
140459084Seric 	if (hp != NULL)
140559084Seric 		return hp->h_name;
140659084Seric 	*statp = EX_NOHOST;
140753751Seric 	return NULL;
140816911Seric }
140916911Seric 
141056795Seric #endif /* DAEMON */
1411