xref: /original-bsd/usr.sbin/sendmail/src/daemon.c (revision 4d6b8580)
1939f5b94Sdist /*
2759969d8Seric  * Copyright (c) 1983, 1995 Eric P. Allman
3c9d2fa25Sbostic  * Copyright (c) 1988, 1993
4c9d2fa25Sbostic  *	The Regents of the University of California.  All rights reserved.
5da1c6175Sbostic  *
63bc94712Sbostic  * %sccs.include.redist.c%
7939f5b94Sdist  */
8939f5b94Sdist 
97aa493c5Seric #include <errno.h>
106c05f684Sbostic #include "sendmail.h"
117fa39d90Seric 
12af5e902cSeric #ifndef lint
13da1c6175Sbostic #ifdef DAEMON
14*4d6b8580Seric static char sccsid[] = "@(#)daemon.c	8.92 (Berkeley) 05/23/95 (with daemon mode)";
15d0a9e852Seric #else
16*4d6b8580Seric static char sccsid[] = "@(#)daemon.c	8.92 (Berkeley) 05/23/95 (without daemon mode)";
17da1c6175Sbostic #endif
18da1c6175Sbostic #endif /* not lint */
19da1c6175Sbostic 
20da1c6175Sbostic #ifdef DAEMON
21d0a9e852Seric 
22d8d0a4aeSeric # include <arpa/inet.h>
23d0a9e852Seric 
249d4a8008Seric #if NAMED_BIND
253490b9dfSeric # include <resolv.h>
263490b9dfSeric #endif
273490b9dfSeric 
2811af795cSeric #if IP_SRCROUTE
2911af795cSeric # include <netinet/in_systm.h>
3011af795cSeric # include <netinet/ip.h>
3111af795cSeric # include <netinet/ip_var.h>
3211af795cSeric #endif
3311af795cSeric 
347fa39d90Seric /*
357fa39d90Seric **  DAEMON.C -- routines to use when running as a daemon.
3647b12ae1Seric **
3747b12ae1Seric **	This entire file is highly dependent on the 4.2 BSD
3847b12ae1Seric **	interprocess communication primitives.  No attempt has
3947b12ae1Seric **	been made to make this file portable to Version 7,
4047b12ae1Seric **	Version 6, MPX files, etc.  If you should try such a
4147b12ae1Seric **	thing yourself, I recommend chucking the entire file
4247b12ae1Seric **	and starting from scratch.  Basic semantics are:
4347b12ae1Seric **
4447b12ae1Seric **	getrequests()
4547b12ae1Seric **		Opens a port and initiates a connection.
4647b12ae1Seric **		Returns in a child.  Must set InChannel and
4747b12ae1Seric **		OutChannel appropriately.
48b7d7afcbSeric **	clrdaemon()
49b7d7afcbSeric **		Close any open files associated with getting
50b7d7afcbSeric **		the connection; this is used when running the queue,
51b7d7afcbSeric **		etc., to avoid having extra file descriptors during
52b7d7afcbSeric **		the queue run and to avoid confusing the network
53b7d7afcbSeric **		code (if it cares).
54914346b1Seric **	makeconnection(host, port, outfile, infile, usesecureport)
5547b12ae1Seric **		Make a connection to the named host on the given
5647b12ae1Seric **		port.  Set *outfile and *infile to the files
5747b12ae1Seric **		appropriate for communication.  Returns zero on
5847b12ae1Seric **		success, else an exit status describing the
5947b12ae1Seric **		error.
6008de856eSeric **	host_map_lookup(map, hbuf, avp, pstat)
6105b57da8Seric **		Convert the entry in hbuf into a canonical form.
627fa39d90Seric */
637fa39d90Seric /*
647fa39d90Seric **  GETREQUESTS -- open mail IPC port and get requests.
657fa39d90Seric **
667fa39d90Seric **	Parameters:
677fa39d90Seric **		none.
687fa39d90Seric **
697fa39d90Seric **	Returns:
707fa39d90Seric **		none.
717fa39d90Seric **
727fa39d90Seric **	Side Effects:
737fa39d90Seric **		Waits until some interesting activity occurs.  When
747fa39d90Seric **		it does, a child is created to process it, and the
757fa39d90Seric **		parent waits for completion.  Return from this
76147303b1Seric **		routine is always in the child.  The file pointers
77147303b1Seric **		"InChannel" and "OutChannel" should be set to point
78147303b1Seric **		to the communication channel.
797fa39d90Seric */
807fa39d90Seric 
81b7d7afcbSeric int		DaemonSocket	= -1;		/* fd describing socket */
82bfb80540Seric SOCKADDR	DaemonAddr;			/* socket for incoming */
83bfc1eaf8Seric int		ListenQueueSize = 10;		/* size of listen queue */
84b35447dbSeric int		TcpRcvBufferSize = 0;		/* size of TCP receive buffer */
85b35447dbSeric int		TcpSndBufferSize = 0;		/* size of TCP send buffer */
861c71e510Seric 
878b212fe0Seric void
887fa39d90Seric getrequests()
897fa39d90Seric {
901c71e510Seric 	int t;
9115d084d5Seric 	bool refusingconnections = TRUE;
920aae1086Seric 	FILE *pidf;
93dadb8687Seric 	int socksize;
94dfe840b2Seric #ifdef XDEBUG
95dfe840b2Seric 	bool j_has_dot;
96dfe840b2Seric #endif
979b100374Sbostic 	extern void reapchild();
98eb889047Seric 
99a8268164Seric 	/*
1001c71e510Seric 	**  Set up the address for the mailer.
101eb889047Seric 	*/
102eb889047Seric 
103bfb80540Seric 	if (DaemonAddr.sin.sin_family == 0)
104bfb80540Seric 		DaemonAddr.sin.sin_family = AF_INET;
105bfb80540Seric 	if (DaemonAddr.sin.sin_addr.s_addr == 0)
106bfb80540Seric 		DaemonAddr.sin.sin_addr.s_addr = INADDR_ANY;
107bfb80540Seric 	if (DaemonAddr.sin.sin_port == 0)
108bfb80540Seric 	{
109e5311662Seric 		register struct servent *sp;
110e5311662Seric 
1111c71e510Seric 		sp = getservbyname("smtp", "tcp");
1121c71e510Seric 		if (sp == NULL)
113d0a9e852Seric 		{
114ad977999Seric 			syserr("554 service \"smtp\" unknown");
115e5311662Seric 			DaemonAddr.sin.sin_port = htons(25);
1161c71e510Seric 		}
117e5311662Seric 		else
118bfb80540Seric 			DaemonAddr.sin.sin_port = sp->s_port;
119bfb80540Seric 	}
1201c71e510Seric 
1211c71e510Seric 	/*
1221c71e510Seric 	**  Try to actually open the connection.
1231c71e510Seric 	*/
1241c71e510Seric 
1251c71e510Seric 	if (tTd(15, 1))
126bfb80540Seric 		printf("getrequests: port 0x%x\n", DaemonAddr.sin.sin_port);
1271c71e510Seric 
1281c71e510Seric 	/* get a socket for the SMTP connection */
12988ea5609Seric 	socksize = opendaemonsocket(TRUE);
1301c71e510Seric 
1312b9178d3Seric 	(void) setsignal(SIGCHLD, reapchild);
13252308a50Seric 
1330aae1086Seric 	/* write the pid to the log file for posterity */
1340aae1086Seric 	pidf = fopen(PidFile, "w");
1350aae1086Seric 	if (pidf != NULL)
1360aae1086Seric 	{
13737950f67Seric 		extern char *CommandLineArgs;
13837950f67Seric 
13937950f67Seric 		/* write the process id on line 1 */
1400aae1086Seric 		fprintf(pidf, "%d\n", getpid());
14137950f67Seric 
14237950f67Seric 		/* line 2 contains all command line flags */
14337950f67Seric 		fprintf(pidf, "%s\n", CommandLineArgs);
14437950f67Seric 
14537950f67Seric 		/* flush and close */
1460aae1086Seric 		fclose(pidf);
1470aae1086Seric 	}
1480aae1086Seric 
149dfe840b2Seric #ifdef XDEBUG
150dfe840b2Seric 	{
15135852b23Seric 		char jbuf[MAXHOSTNAMELEN];
152dfe840b2Seric 
1538b212fe0Seric 		expand("\201j", jbuf, sizeof jbuf, CurEnv);
15435852b23Seric 		j_has_dot = strchr(jbuf, '.') != NULL;
155dfe840b2Seric 	}
156dfe840b2Seric #endif
1570aae1086Seric 
1581c71e510Seric 	if (tTd(15, 1))
159b7d7afcbSeric 		printf("getrequests: %d\n", DaemonSocket);
1601c71e510Seric 
1611c71e510Seric 	for (;;)
1621c71e510Seric 	{
1633a099713Seric 		register int pid;
164a44d5a5eSeric 		auto int lotherend;
16515d084d5Seric 		extern bool refuseconnections();
1668b212fe0Seric 		extern int getla();
1673a099713Seric 
1683a099713Seric 		/* see if we are rejecting connections */
16915d084d5Seric 		CurrentLA = getla();
17015d084d5Seric 		if (refuseconnections())
1716775ec03Sbostic 		{
1720084e6f6Seric 			if (DaemonSocket >= 0)
17315d084d5Seric 			{
1740084e6f6Seric 				/* close socket so peer will fail quickly */
1750084e6f6Seric 				(void) close(DaemonSocket);
1760084e6f6Seric 				DaemonSocket = -1;
17715d084d5Seric 			}
1780084e6f6Seric 			refusingconnections = TRUE;
17971e5e267Seric 			setproctitle("rejecting connections: load average: %d",
18071e5e267Seric 				CurrentLA);
1810084e6f6Seric 			sleep(15);
18215d084d5Seric 			continue;
18315d084d5Seric 		}
18415d084d5Seric 
1858b212fe0Seric 		/* arrange to (re)open the socket if necessary */
18615d084d5Seric 		if (refusingconnections)
18715d084d5Seric 		{
1888a0cb579Seric 			(void) opendaemonsocket(FALSE);
18915d084d5Seric 			setproctitle("accepting connections");
19015d084d5Seric 			refusingconnections = FALSE;
1916775ec03Sbostic 		}
192a44d5a5eSeric 
193dfe840b2Seric #ifdef XDEBUG
194dfe840b2Seric 		/* check for disaster */
195dfe840b2Seric 		{
19635852b23Seric 			char jbuf[MAXHOSTNAMELEN];
197dfe840b2Seric 
1988b212fe0Seric 			expand("\201j", jbuf, sizeof jbuf, CurEnv);
1998b212fe0Seric 			if (!wordinclass(jbuf, 'w'))
200dfe840b2Seric 			{
201dfe840b2Seric 				dumpstate("daemon lost $j");
202dfe840b2Seric 				syslog(LOG_ALERT, "daemon process doesn't have $j in $=w; see syslog");
203dfe840b2Seric 				abort();
204dfe840b2Seric 			}
20535852b23Seric 			else if (j_has_dot && strchr(jbuf, '.') == NULL)
206dfe840b2Seric 			{
207dfe840b2Seric 				dumpstate("daemon $j lost dot");
208dfe840b2Seric 				syslog(LOG_ALERT, "daemon process $j lost dot; see syslog");
209dfe840b2Seric 				abort();
210dfe840b2Seric 			}
211dfe840b2Seric 		}
212dfe840b2Seric #endif
213dfe840b2Seric 
2141c71e510Seric 		/* wait for a connection */
2151c71e510Seric 		do
2161c71e510Seric 		{
2171c71e510Seric 			errno = 0;
218dadb8687Seric 			lotherend = socksize;
2199b100374Sbostic 			t = accept(DaemonSocket,
2209b100374Sbostic 			    (struct sockaddr *)&RealHostAddr, &lotherend);
2211c71e510Seric 		} while (t < 0 && errno == EINTR);
2221c71e510Seric 		if (t < 0)
2231c71e510Seric 		{
2241c71e510Seric 			syserr("getrequests: accept");
2258b212fe0Seric 
2268b212fe0Seric 			/* arrange to re-open the socket next time around */
2278b212fe0Seric 			(void) close(DaemonSocket);
2288b212fe0Seric 			DaemonSocket = -1;
2291c71e510Seric 			sleep(5);
2301c71e510Seric 			continue;
2311c71e510Seric 		}
232d0a9e852Seric 
233d0a9e852Seric 		/*
234d0a9e852Seric 		**  Create a subprocess to process the mail.
235d0a9e852Seric 		*/
236d0a9e852Seric 
23761e4310fSeric 		if (tTd(15, 2))
2381c71e510Seric 			printf("getrequests: forking (fd = %d)\n", t);
239eb889047Seric 
240a8268164Seric 		pid = fork();
241a8268164Seric 		if (pid < 0)
242a8268164Seric 		{
243a8268164Seric 			syserr("daemon: cannot fork");
244a8268164Seric 			sleep(10);
2451c71e510Seric 			(void) close(t);
246a8268164Seric 			continue;
247a8268164Seric 		}
248a8268164Seric 
249a8268164Seric 		if (pid == 0)
250a8268164Seric 		{
251da662164Seric 			char *p;
2529f8b0eadSeric 			extern char *hostnamebyanyaddr();
2538b212fe0Seric 			extern void intsig();
254a44d5a5eSeric 
255a8268164Seric 			/*
256a8268164Seric 			**  CHILD -- return to caller.
257a44d5a5eSeric 			**	Collect verified idea of sending host.
258a8268164Seric 			**	Verify calling user id if possible here.
259a8268164Seric 			*/
260a8268164Seric 
2612b9178d3Seric 			(void) setsignal(SIGCHLD, SIG_DFL);
2628b212fe0Seric 			(void) setsignal(SIGHUP, intsig);
2638b212fe0Seric 			(void) close(DaemonSocket);
2649f9b003eSeric 			DisConnected = FALSE;
265779ac194Seric 
2664dd09a90Seric 			setproctitle("startup with %s",
2674dd09a90Seric 				anynet_ntoa(&RealHostAddr));
2684dd09a90Seric 
269a44d5a5eSeric 			/* determine host name */
270da662164Seric 			p = hostnamebyanyaddr(&RealHostAddr);
27137b4af40Seric 			if (strlen(p) > MAXNAME)
27237b4af40Seric 				p[MAXNAME] = '\0';
273da662164Seric 			RealHostName = newstr(p);
2744dd09a90Seric 			setproctitle("startup with %s", p);
27529dcf4baSeric 
2762a6bc25bSeric #ifdef LOG
2771f2ff1a4Seric 			if (LogLevel > 11)
2782a6bc25bSeric 			{
2792a6bc25bSeric 				/* log connection information */
2802a6bc25bSeric 				syslog(LOG_INFO, "connect from %s (%s)",
2819f8b0eadSeric 					RealHostName, anynet_ntoa(&RealHostAddr));
2822a6bc25bSeric 			}
2832a6bc25bSeric #endif
2842a6bc25bSeric 
285335eae58Seric 			if ((InChannel = fdopen(t, "r")) == NULL ||
286335eae58Seric 			    (t = dup(t)) < 0 ||
287335eae58Seric 			    (OutChannel = fdopen(t, "w")) == NULL)
288335eae58Seric 			{
289335eae58Seric 				syserr("cannot open SMTP server channel, fd=%d", t);
290335eae58Seric 				exit(0);
291335eae58Seric 			}
292244b09d1Seric 
29329dcf4baSeric 			/* should we check for illegal connection here? XXX */
294e17a3a5aSeric #ifdef XLA
295e17a3a5aSeric 			if (!xla_host_ok(RealHostName))
296e17a3a5aSeric 			{
297244b09d1Seric 				message("421 Too many SMTP sessions for this host");
298e17a3a5aSeric 				exit(0);
299e17a3a5aSeric 			}
300e17a3a5aSeric #endif
301a44d5a5eSeric 
30261e4310fSeric 			if (tTd(15, 2))
303d0a9e852Seric 				printf("getreq: returning\n");
304a8268164Seric 			return;
305a8268164Seric 		}
306a8268164Seric 
3073c154354Seric 		/* close the port so that others will hang (for a while) */
3083c154354Seric 		(void) close(t);
3098e3e4b17Seric 	}
3103c154354Seric 	/*NOTREACHED*/
3113c154354Seric }
3128e3e4b17Seric /*
3130084e6f6Seric **  OPENDAEMONSOCKET -- open the SMTP socket
3140084e6f6Seric **
3150084e6f6Seric **	Deals with setting all appropriate options.  DaemonAddr must
3160084e6f6Seric **	be set up in advance.
3170084e6f6Seric **
3180084e6f6Seric **	Parameters:
31988ea5609Seric **		firsttime -- set if this is the initial open.
3200084e6f6Seric **
3210084e6f6Seric **	Returns:
3220084e6f6Seric **		Size in bytes of the daemon socket addr.
3230084e6f6Seric **
3240084e6f6Seric **	Side Effects:
3250084e6f6Seric **		Leaves DaemonSocket set to the open socket.
3260084e6f6Seric **		Exits if the socket cannot be created.
3270084e6f6Seric */
3280084e6f6Seric 
3296173568dSeric #define MAXOPENTRIES	10	/* maximum number of tries to open connection */
3306173568dSeric 
3310084e6f6Seric int
33288ea5609Seric opendaemonsocket(firsttime)
33388ea5609Seric 	bool firsttime;
3340084e6f6Seric {
3350084e6f6Seric 	int on = 1;
3368b212fe0Seric 	int socksize = 0;
3376173568dSeric 	int ntries = 0;
3386173568dSeric 	int saveerrno;
3390084e6f6Seric 
3400084e6f6Seric 	if (tTd(15, 2))
3410084e6f6Seric 		printf("opendaemonsocket()\n");
3420084e6f6Seric 
3436173568dSeric 	do
3446173568dSeric 	{
345254b93c3Seric 		if (ntries > 0)
346254b93c3Seric 			sleep(5);
34788ea5609Seric 		if (firsttime || DaemonSocket < 0)
34888ea5609Seric 		{
3490084e6f6Seric 			DaemonSocket = socket(DaemonAddr.sa.sa_family, SOCK_STREAM, 0);
3500084e6f6Seric 			if (DaemonSocket < 0)
3510084e6f6Seric 			{
3520084e6f6Seric 				/* probably another daemon already */
3536173568dSeric 				saveerrno = errno;
3540084e6f6Seric 				syserr("opendaemonsocket: can't create server SMTP socket");
3550084e6f6Seric 			  severe:
3560084e6f6Seric # ifdef LOG
3570084e6f6Seric 				if (LogLevel > 0)
3580084e6f6Seric 					syslog(LOG_ALERT, "problem creating SMTP socket");
3590084e6f6Seric # endif /* LOG */
3606173568dSeric 				DaemonSocket = -1;
3616173568dSeric 				continue;
3620084e6f6Seric 			}
3630084e6f6Seric 
3640084e6f6Seric 			/* turn on network debugging? */
3650084e6f6Seric 			if (tTd(15, 101))
3666173568dSeric 				(void) setsockopt(DaemonSocket, SOL_SOCKET,
3676173568dSeric 						  SO_DEBUG, (char *)&on,
3686173568dSeric 						  sizeof on);
3690084e6f6Seric 
3706173568dSeric 			(void) setsockopt(DaemonSocket, SOL_SOCKET,
3716173568dSeric 					  SO_REUSEADDR, (char *)&on, sizeof on);
3726173568dSeric 			(void) setsockopt(DaemonSocket, SOL_SOCKET,
3736173568dSeric 					  SO_KEEPALIVE, (char *)&on, sizeof on);
3740084e6f6Seric 
3750084e6f6Seric #ifdef SO_RCVBUF
3760084e6f6Seric 			if (TcpRcvBufferSize > 0)
3770084e6f6Seric 			{
3786173568dSeric 				if (setsockopt(DaemonSocket, SOL_SOCKET,
3796173568dSeric 					       SO_RCVBUF,
3800084e6f6Seric 					       (char *) &TcpRcvBufferSize,
3810084e6f6Seric 					       sizeof(TcpRcvBufferSize)) < 0)
3820084e6f6Seric 					syserr("getrequests: setsockopt(SO_RCVBUF)");
3830084e6f6Seric 			}
3840084e6f6Seric #endif
3850084e6f6Seric 
3860084e6f6Seric 			switch (DaemonAddr.sa.sa_family)
3870084e6f6Seric 			{
3880084e6f6Seric # ifdef NETINET
3890084e6f6Seric 			  case AF_INET:
3900084e6f6Seric 				socksize = sizeof DaemonAddr.sin;
3910084e6f6Seric 				break;
3920084e6f6Seric # endif
3930084e6f6Seric 
3940084e6f6Seric # ifdef NETISO
3950084e6f6Seric 			  case AF_ISO:
3960084e6f6Seric 				socksize = sizeof DaemonAddr.siso;
3970084e6f6Seric 				break;
3980084e6f6Seric # endif
3990084e6f6Seric 
4000084e6f6Seric 			  default:
4010084e6f6Seric 				socksize = sizeof DaemonAddr;
4020084e6f6Seric 				break;
4030084e6f6Seric 			}
4040084e6f6Seric 
4050084e6f6Seric 			if (bind(DaemonSocket, &DaemonAddr.sa, socksize) < 0)
4060084e6f6Seric 			{
4076173568dSeric 				saveerrno = errno;
4080084e6f6Seric 				syserr("getrequests: cannot bind");
4090084e6f6Seric 				(void) close(DaemonSocket);
4100084e6f6Seric 				goto severe;
4110084e6f6Seric 			}
41288ea5609Seric 		}
41388ea5609Seric 		if (!firsttime && listen(DaemonSocket, ListenQueueSize) < 0)
4140084e6f6Seric 		{
4156173568dSeric 			saveerrno = errno;
4160084e6f6Seric 			syserr("getrequests: cannot listen");
4170084e6f6Seric 			(void) close(DaemonSocket);
4180084e6f6Seric 			goto severe;
4190084e6f6Seric 		}
4200084e6f6Seric 		return socksize;
4216173568dSeric 	} while (ntries++ < MAXOPENTRIES && transienterror(saveerrno));
4228b212fe0Seric 	syserr("!opendaemonsocket: server SMTP socket wedged: exiting");
4236173568dSeric 	finis();
4240084e6f6Seric }
4250084e6f6Seric /*
426b7d7afcbSeric **  CLRDAEMON -- reset the daemon connection
427b7d7afcbSeric **
428b7d7afcbSeric **	Parameters:
429b7d7afcbSeric **		none.
430b7d7afcbSeric **
431b7d7afcbSeric **	Returns:
432b7d7afcbSeric **		none.
433b7d7afcbSeric **
434b7d7afcbSeric **	Side Effects:
435b7d7afcbSeric **		releases any resources used by the passive daemon.
436b7d7afcbSeric */
437b7d7afcbSeric 
4388b212fe0Seric void
439b7d7afcbSeric clrdaemon()
440b7d7afcbSeric {
441b7d7afcbSeric 	if (DaemonSocket >= 0)
442b7d7afcbSeric 		(void) close(DaemonSocket);
443b7d7afcbSeric 	DaemonSocket = -1;
444b7d7afcbSeric }
445b7d7afcbSeric /*
446bfb80540Seric **  SETDAEMONOPTIONS -- set options for running the daemon
447bfb80540Seric **
448bfb80540Seric **	Parameters:
449bfb80540Seric **		p -- the options line.
450bfb80540Seric **
451bfb80540Seric **	Returns:
452bfb80540Seric **		none.
453bfb80540Seric */
454bfb80540Seric 
4558b212fe0Seric void
456bfb80540Seric setdaemonoptions(p)
457bfb80540Seric 	register char *p;
458bfb80540Seric {
459850144caSeric 	if (DaemonAddr.sa.sa_family == AF_UNSPEC)
460850144caSeric 		DaemonAddr.sa.sa_family = AF_INET;
461850144caSeric 
462bfb80540Seric 	while (p != NULL)
463bfb80540Seric 	{
464bfb80540Seric 		register char *f;
465bfb80540Seric 		register char *v;
466bfb80540Seric 
467bfb80540Seric 		while (isascii(*p) && isspace(*p))
468bfb80540Seric 			p++;
469bfb80540Seric 		if (*p == '\0')
470bfb80540Seric 			break;
471bfb80540Seric 		f = p;
472bfb80540Seric 		p = strchr(p, ',');
473bfb80540Seric 		if (p != NULL)
474bfb80540Seric 			*p++ = '\0';
475bfb80540Seric 		v = strchr(f, '=');
476bfb80540Seric 		if (v == NULL)
477bfb80540Seric 			continue;
478bfb80540Seric 		while (isascii(*++v) && isspace(*v))
479bfb80540Seric 			continue;
480e7988623Seric 		if (isascii(*f) && isupper(*f))
481e7988623Seric 			*f = tolower(*f);
482bfb80540Seric 
483bfb80540Seric 		switch (*f)
484bfb80540Seric 		{
485850144caSeric 		  case 'F':		/* address family */
486850144caSeric 			if (isascii(*v) && isdigit(*v))
487850144caSeric 				DaemonAddr.sa.sa_family = atoi(v);
488850144caSeric #ifdef NETINET
489850144caSeric 			else if (strcasecmp(v, "inet") == 0)
490850144caSeric 				DaemonAddr.sa.sa_family = AF_INET;
491850144caSeric #endif
492850144caSeric #ifdef NETISO
493850144caSeric 			else if (strcasecmp(v, "iso") == 0)
494850144caSeric 				DaemonAddr.sa.sa_family = AF_ISO;
495850144caSeric #endif
496850144caSeric #ifdef NETNS
497850144caSeric 			else if (strcasecmp(v, "ns") == 0)
498850144caSeric 				DaemonAddr.sa.sa_family = AF_NS;
499850144caSeric #endif
500850144caSeric #ifdef NETX25
501850144caSeric 			else if (strcasecmp(v, "x.25") == 0)
502850144caSeric 				DaemonAddr.sa.sa_family = AF_CCITT;
503850144caSeric #endif
504850144caSeric 			else
505850144caSeric 				syserr("554 Unknown address family %s in Family=option", v);
506850144caSeric 			break;
507850144caSeric 
508850144caSeric 		  case 'A':		/* address */
509850144caSeric 			switch (DaemonAddr.sa.sa_family)
510850144caSeric 			{
511850144caSeric #ifdef NETINET
512850144caSeric 			  case AF_INET:
513850144caSeric 				if (isascii(*v) && isdigit(*v))
5148b212fe0Seric 					DaemonAddr.sin.sin_addr.s_addr = htonl(inet_network(v));
515850144caSeric 				else
516850144caSeric 				{
517850144caSeric 					register struct netent *np;
518850144caSeric 
519850144caSeric 					np = getnetbyname(v);
520850144caSeric 					if (np == NULL)
521850144caSeric 						syserr("554 network \"%s\" unknown", v);
522850144caSeric 					else
523850144caSeric 						DaemonAddr.sin.sin_addr.s_addr = np->n_net;
524850144caSeric 				}
525850144caSeric 				break;
526850144caSeric #endif
527850144caSeric 
528850144caSeric 			  default:
529850144caSeric 				syserr("554 Address= option unsupported for family %d",
530850144caSeric 					DaemonAddr.sa.sa_family);
531850144caSeric 				break;
532850144caSeric 			}
533850144caSeric 			break;
534850144caSeric 
535bfb80540Seric 		  case 'P':		/* port */
536850144caSeric 			switch (DaemonAddr.sa.sa_family)
537850144caSeric 			{
538850144caSeric 				short port;
539850144caSeric 
540850144caSeric #ifdef NETINET
541850144caSeric 			  case AF_INET:
542bfb80540Seric 				if (isascii(*v) && isdigit(*v))
54376b70c58Seric 					DaemonAddr.sin.sin_port = htons(atoi(v));
544bfb80540Seric 				else
545bfb80540Seric 				{
546bfb80540Seric 					register struct servent *sp;
547bfb80540Seric 
548bfb80540Seric 					sp = getservbyname(v, "tcp");
549bfb80540Seric 					if (sp == NULL)
550ad977999Seric 						syserr("554 service \"%s\" unknown", v);
551bfb80540Seric 					else
552bfb80540Seric 						DaemonAddr.sin.sin_port = sp->s_port;
553bfb80540Seric 				}
554bfb80540Seric 				break;
555850144caSeric #endif
556bfb80540Seric 
557850144caSeric #ifdef NETISO
558850144caSeric 			  case AF_ISO:
559850144caSeric 				/* assume two byte transport selector */
560bfb80540Seric 				if (isascii(*v) && isdigit(*v))
56176b70c58Seric 					port = htons(atoi(v));
562bfb80540Seric 				else
563bfb80540Seric 				{
564850144caSeric 					register struct servent *sp;
565bfb80540Seric 
566850144caSeric 					sp = getservbyname(v, "tcp");
567850144caSeric 					if (sp == NULL)
568ad977999Seric 						syserr("554 service \"%s\" unknown", v);
569bfb80540Seric 					else
570850144caSeric 						port = sp->s_port;
571850144caSeric 				}
572850144caSeric 				bcopy((char *) &port, TSEL(&DaemonAddr.siso), 2);
573850144caSeric 				break;
574850144caSeric #endif
575850144caSeric 
576850144caSeric 			  default:
577850144caSeric 				syserr("554 Port= option unsupported for family %d",
578850144caSeric 					DaemonAddr.sa.sa_family);
579850144caSeric 				break;
580bfb80540Seric 			}
581bfb80540Seric 			break;
582bfc1eaf8Seric 
583bfc1eaf8Seric 		  case 'L':		/* listen queue size */
584bfc1eaf8Seric 			ListenQueueSize = atoi(v);
585bfc1eaf8Seric 			break;
586b35447dbSeric 
587b35447dbSeric 		  case 'S':		/* send buffer size */
588b35447dbSeric 			TcpSndBufferSize = atoi(v);
589b35447dbSeric 			break;
590b35447dbSeric 
591b35447dbSeric 		  case 'R':		/* receive buffer size */
592b35447dbSeric 			TcpRcvBufferSize = atoi(v);
593b35447dbSeric 			break;
594e7988623Seric 
595e7988623Seric 		  default:
596e7988623Seric 			syserr("554 DaemonPortOptions parameter \"%s\" unknown", f);
597bfb80540Seric 		}
598bfb80540Seric 	}
599bfb80540Seric }
600bfb80540Seric /*
6017aa493c5Seric **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
6027aa493c5Seric **
6037aa493c5Seric **	Parameters:
6047aa493c5Seric **		host -- the name of the host.
60548ff0a9dSeric **		port -- the port number to connect to.
606655feedbSeric **		mci -- a pointer to the mail connection information
607655feedbSeric **			structure to be filled in.
608914346b1Seric **		usesecureport -- if set, use a low numbered (reserved)
609914346b1Seric **			port to provide some rudimentary authentication.
6107aa493c5Seric **
6117aa493c5Seric **	Returns:
6127aa493c5Seric **		An exit code telling whether the connection could be
6137aa493c5Seric **			made and if not why not.
6147aa493c5Seric **
6157aa493c5Seric **	Side Effects:
6167aa493c5Seric **		none.
6177aa493c5Seric */
6187aa493c5Seric 
619e2f2f828Seric SOCKADDR	CurHostAddr;		/* address of current host */
62071ff6caaSeric 
621b31e7f2bSeric int
622655feedbSeric makeconnection(host, port, mci, usesecureport)
6237aa493c5Seric 	char *host;
624210215eaSeric 	u_short port;
625b31e7f2bSeric 	register MCI *mci;
626914346b1Seric 	bool usesecureport;
6277aa493c5Seric {
6288b212fe0Seric 	register int i = 0;
6298b212fe0Seric 	register int s;
63004344589Sbloom 	register struct hostent *hp = (struct hostent *)NULL;
631e2f2f828Seric 	SOCKADDR addr;
6326286bb75Sbloom 	int sav_errno;
633e2f2f828Seric 	int addrlen;
6348b212fe0Seric 	bool firstconnect;
6359d4a8008Seric #if NAMED_BIND
636134746fbSeric 	extern int h_errno;
637134746fbSeric #endif
6387aa493c5Seric 
6397aa493c5Seric 	/*
6407aa493c5Seric 	**  Set up the address for the mailer.
64171096d12Seric 	**	Accept "[a.b.c.d]" syntax for host name.
6427aa493c5Seric 	*/
6437aa493c5Seric 
6449d4a8008Seric #if NAMED_BIND
645794bdbb9Smiriam 	h_errno = 0;
646134746fbSeric #endif
647794bdbb9Smiriam 	errno = 0;
648967778e2Seric 	bzero(&CurHostAddr, sizeof CurHostAddr);
649c931b82bSeric 	SmtpPhase = mci->mci_phase = "initial connection";
650d945ebe8Seric 	CurHostName = host;
651794bdbb9Smiriam 
65271096d12Seric 	if (host[0] == '[')
65371096d12Seric 	{
654a44d5a5eSeric 		long hid;
6556c2c3107Seric 		register char *p = strchr(host, ']');
65671096d12Seric 
657a44d5a5eSeric 		if (p != NULL)
65871096d12Seric 		{
659a44d5a5eSeric 			*p = '\0';
6604d9c42c2Seric #ifdef NETINET
661a44d5a5eSeric 			hid = inet_addr(&host[1]);
662a7e21fe6Seric 			if (hid == -1)
6634d9c42c2Seric #endif
664a7e21fe6Seric 			{
665a7e21fe6Seric 				/* try it as a host name (avoid MX lookup) */
6668b212fe0Seric 				hp = sm_gethostbyname(&host[1]);
667d8984352Seric 				if (hp == NULL && p[-1] == '.')
668d8984352Seric 				{
6698b212fe0Seric #if NAMED_BIND
6708b212fe0Seric 					int oldopts = _res.options;
6718b212fe0Seric 
6728b212fe0Seric 					_res.options &= ~(RES_DEFNAMES|RES_DNSRCH);
6738b212fe0Seric #endif
674d8984352Seric 					p[-1] = '\0';
6758b212fe0Seric 					hp = sm_gethostbyname(&host[1]);
676d8984352Seric 					p[-1] = '.';
6778b212fe0Seric #if NAMED_BIND
6788b212fe0Seric 					_res.options = oldopts;
6798b212fe0Seric #endif
680d8984352Seric 				}
681a7e21fe6Seric 				*p = ']';
682a7e21fe6Seric 				goto gothostent;
683a7e21fe6Seric 			}
684a44d5a5eSeric 			*p = ']';
68571096d12Seric 		}
686a7e21fe6Seric 		if (p == NULL)
68771096d12Seric 		{
68808b25121Seric 			usrerr("553 Invalid numeric domain spec \"%s\"", host);
6895b068d51Seric 			mci->mci_status = "5.1.2";
69071096d12Seric 			return (EX_NOHOST);
69171096d12Seric 		}
6924d9c42c2Seric #ifdef NETINET
6934d9c42c2Seric 		addr.sin.sin_family = AF_INET;		/*XXX*/
69483c1f4bcSeric 		addr.sin.sin_addr.s_addr = hid;
6954d9c42c2Seric #endif
69671096d12Seric 	}
6971c71e510Seric 	else
6981c71e510Seric 	{
699d8984352Seric 		register char *p = &host[strlen(host) - 1];
700d8984352Seric 
7018b212fe0Seric 		hp = sm_gethostbyname(host);
702d8984352Seric 		if (hp == NULL && *p == '.')
703d8984352Seric 		{
7048b212fe0Seric #if NAMED_BIND
7058b212fe0Seric 			int oldopts = _res.options;
7068b212fe0Seric 
7078b212fe0Seric 			_res.options &= ~(RES_DEFNAMES|RES_DNSRCH);
7088b212fe0Seric #endif
709d8984352Seric 			*p = '\0';
7108b212fe0Seric 			hp = sm_gethostbyname(host);
711d8984352Seric 			*p = '.';
7128b212fe0Seric #if NAMED_BIND
7138b212fe0Seric 			_res.options = oldopts;
7148b212fe0Seric #endif
715d8984352Seric 		}
716a7e21fe6Seric gothostent:
717794bdbb9Smiriam 		if (hp == NULL)
718794bdbb9Smiriam 		{
7199d4a8008Seric #if NAMED_BIND
7208b212fe0Seric 			/* check for name server timeouts */
7218b212fe0Seric 			if (errno == ETIMEDOUT || h_errno == TRY_AGAIN ||
7228b212fe0Seric 			    (errno == ECONNREFUSED && UseNameServer))
7238b212fe0Seric 			{
7248b212fe0Seric 				mci->mci_status = "4.4.3";
72552308a50Seric 				return (EX_TEMPFAIL);
7268b212fe0Seric 			}
727134746fbSeric #endif
7287aa493c5Seric 			return (EX_NOHOST);
729794bdbb9Smiriam 		}
73083c1f4bcSeric 		addr.sa.sa_family = hp->h_addrtype;
73183c1f4bcSeric 		switch (hp->h_addrtype)
73283c1f4bcSeric 		{
73383c1f4bcSeric #ifdef NETINET
73483c1f4bcSeric 		  case AF_INET:
735e2f2f828Seric 			bcopy(hp->h_addr,
73683c1f4bcSeric 				&addr.sin.sin_addr,
7378b212fe0Seric 				INADDRSZ);
73883c1f4bcSeric 			break;
73983c1f4bcSeric #endif
74083c1f4bcSeric 
74183c1f4bcSeric 		  default:
742e2f2f828Seric 			bcopy(hp->h_addr,
74383c1f4bcSeric 				addr.sa.sa_data,
744e2f2f828Seric 				hp->h_length);
74583c1f4bcSeric 			break;
74683c1f4bcSeric 		}
74704344589Sbloom 		i = 1;
7481c71e510Seric 	}
7491c71e510Seric 
7501c71e510Seric 	/*
7511c71e510Seric 	**  Determine the port number.
7521c71e510Seric 	*/
7531c71e510Seric 
754fd7c0790Seric 	if (port != 0)
755e2f2f828Seric 		port = htons(port);
756fd7c0790Seric 	else
7571c71e510Seric 	{
7581c71e510Seric 		register struct servent *sp = getservbyname("smtp", "tcp");
7591c71e510Seric 
7601c71e510Seric 		if (sp == NULL)
7611c71e510Seric 		{
7623edb2e00Seric #ifdef LOG
7633edb2e00Seric 			if (LogLevel > 2)
7643edb2e00Seric 				syslog(LOG_ERR, "makeconnection: service \"smtp\" unknown");
7653edb2e00Seric #endif
766e5311662Seric 			port = htons(25);
7671c71e510Seric 		}
768e5311662Seric 		else
769e2f2f828Seric 			port = sp->s_port;
770e2f2f828Seric 	}
771e2f2f828Seric 
77283c1f4bcSeric 	switch (addr.sa.sa_family)
773e2f2f828Seric 	{
7744d9c42c2Seric #ifdef NETINET
775e2f2f828Seric 	  case AF_INET:
77683c1f4bcSeric 		addr.sin.sin_port = port;
777e2f2f828Seric 		addrlen = sizeof (struct sockaddr_in);
778e2f2f828Seric 		break;
7794d9c42c2Seric #endif
780e2f2f828Seric 
781e2f2f828Seric #ifdef NETISO
782e2f2f828Seric 	  case AF_ISO:
783e2f2f828Seric 		/* assume two byte transport selector */
784e2f2f828Seric 		bcopy((char *) &port, TSEL((struct sockaddr_iso *) &addr), 2);
785e2f2f828Seric 		addrlen = sizeof (struct sockaddr_iso);
786e2f2f828Seric 		break;
787e2f2f828Seric #endif
788e2f2f828Seric 
789e2f2f828Seric 	  default:
79083c1f4bcSeric 		syserr("Can't connect to address family %d", addr.sa.sa_family);
791e2f2f828Seric 		return (EX_NOHOST);
7921c71e510Seric 	}
7937aa493c5Seric 
7947aa493c5Seric 	/*
7957aa493c5Seric 	**  Try to actually open the connection.
7967aa493c5Seric 	*/
7977aa493c5Seric 
798e17a3a5aSeric #ifdef XLA
799e17a3a5aSeric 	/* if too many connections, don't bother trying */
800e17a3a5aSeric 	if (!xla_noqueue_ok(host))
801e17a3a5aSeric 		return EX_TEMPFAIL;
802e17a3a5aSeric #endif
803e17a3a5aSeric 
8048b212fe0Seric 	firstconnect = TRUE;
805aea02ca1Seric 	for (;;)
806aea02ca1Seric 	{
80761e4310fSeric 		if (tTd(16, 1))
808e2f2f828Seric 			printf("makeconnection (%s [%s])\n",
809e2f2f828Seric 				host, anynet_ntoa(&addr));
8107aa493c5Seric 
811226e3022Seric 		/* save for logging */
812226e3022Seric 		CurHostAddr = addr;
813226e3022Seric 
814914346b1Seric 		if (usesecureport)
815914346b1Seric 		{
816914346b1Seric 			int rport = IPPORT_RESERVED - 1;
817914346b1Seric 
818914346b1Seric 			s = rresvport(&rport);
819914346b1Seric 		}
820914346b1Seric 		else
821914346b1Seric 		{
822af5e902cSeric 			s = socket(AF_INET, SOCK_STREAM, 0);
823914346b1Seric 		}
8247aa493c5Seric 		if (s < 0)
8257aa493c5Seric 		{
8266286bb75Sbloom 			sav_errno = errno;
827914346b1Seric 			syserr("makeconnection: no socket");
8287aa493c5Seric 			goto failure;
8297aa493c5Seric 		}
8307aa493c5Seric 
831b35447dbSeric #ifdef SO_SNDBUF
832b35447dbSeric 		if (TcpSndBufferSize > 0)
833b35447dbSeric 		{
834b35447dbSeric 			if (setsockopt(s, SOL_SOCKET, SO_SNDBUF,
835bf217a95Seric 				       (char *) &TcpSndBufferSize,
836b35447dbSeric 				       sizeof(TcpSndBufferSize)) < 0)
837b35447dbSeric 				syserr("makeconnection: setsockopt(SO_SNDBUF)");
838b35447dbSeric 		}
839b35447dbSeric #endif
840b35447dbSeric 
84161e4310fSeric 		if (tTd(16, 1))
842b31e7f2bSeric 			printf("makeconnection: fd=%d\n", s);
8431b6e4a15Seric 
8441b6e4a15Seric 		/* turn on network debugging? */
845a2ef5fa4Seric 		if (tTd(16, 101))
84652308a50Seric 		{
84752308a50Seric 			int on = 1;
8486173568dSeric 			(void) setsockopt(s, SOL_SOCKET, SO_DEBUG,
849aea02ca1Seric 					  (char *)&on, sizeof on);
85052308a50Seric 		}
85187d6e633Srick 		if (CurEnv->e_xfp != NULL)
852877a6142Seric 			(void) fflush(CurEnv->e_xfp);		/* for debugging */
8534bd6a662Seric 		errno = 0;					/* for debugging */
854e2f2f828Seric 		if (connect(s, (struct sockaddr *) &addr, addrlen) >= 0)
855aea02ca1Seric 			break;
856aea02ca1Seric 
8578b212fe0Seric 		/* if running demand-dialed connection, try again */
8588b212fe0Seric 		if (DialDelay > 0 && firstconnect)
8598b212fe0Seric 		{
8608b212fe0Seric 			if (tTd(16, 1))
8618b212fe0Seric 				printf("Connect failed (%s); trying again...\n",
8628b212fe0Seric 					errstring(sav_errno));
8638b212fe0Seric 			firstconnect = FALSE;
8648b212fe0Seric 			sleep(DialDelay);
8658b212fe0Seric 			continue;
8668b212fe0Seric 		}
8678b212fe0Seric 
868aea02ca1Seric 		/* couldn't connect.... figure out why */
8696286bb75Sbloom 		sav_errno = errno;
8706286bb75Sbloom 		(void) close(s);
8718b212fe0Seric 		if (hp != NULL && hp->h_addr_list[i])
87204344589Sbloom 		{
873aea02ca1Seric 			if (tTd(16, 1))
874e2f2f828Seric 				printf("Connect failed (%s); trying new address....\n",
875e2f2f828Seric 					errstring(sav_errno));
87683c1f4bcSeric 			switch (addr.sa.sa_family)
87783c1f4bcSeric 			{
87883c1f4bcSeric #ifdef NETINET
87983c1f4bcSeric 			  case AF_INET:
880e2f2f828Seric 				bcopy(hp->h_addr_list[i++],
88183c1f4bcSeric 				      &addr.sin.sin_addr,
8828b212fe0Seric 				      INADDRSZ);
88383c1f4bcSeric 				break;
88483c1f4bcSeric #endif
88583c1f4bcSeric 
88683c1f4bcSeric 			  default:
887e2f2f828Seric 				bcopy(hp->h_addr_list[i++],
88883c1f4bcSeric 					addr.sa.sa_data,
889914346b1Seric 					hp->h_length);
89083c1f4bcSeric 				break;
89183c1f4bcSeric 			}
892aea02ca1Seric 			continue;
89304344589Sbloom 		}
89404344589Sbloom 
8957aa493c5Seric 		/* failure, decide if temporary or not */
8967aa493c5Seric 	failure:
897244b09d1Seric #ifdef XLA
898244b09d1Seric 		xla_host_end(host);
899244b09d1Seric #endif
900e2de2524Seric 		if (transienterror(sav_errno))
901e2de2524Seric 			return EX_TEMPFAIL;
902e2de2524Seric 		else
90387d6e633Srick 		{
90408b25121Seric 			message("%s", errstring(sav_errno));
9057aa493c5Seric 			return (EX_UNAVAILABLE);
9067aa493c5Seric 		}
9077aa493c5Seric 	}
9087aa493c5Seric 
9097aa493c5Seric 	/* connection ok, put it into canonical form */
910335eae58Seric 	if ((mci->mci_out = fdopen(s, "w")) == NULL ||
911335eae58Seric 	    (s = dup(s)) < 0 ||
912ab81ee53Seric 	    (mci->mci_in = fdopen(s, "r")) == NULL)
913335eae58Seric 	{
914335eae58Seric 		syserr("cannot open SMTP client channel, fd=%d", s);
915335eae58Seric 		return EX_TEMPFAIL;
916335eae58Seric 	}
9177aa493c5Seric 
918dca8e1f7Seric 	return (EX_OK);
9197aa493c5Seric }
920444eaf03Seric /*
921444eaf03Seric **  MYHOSTNAME -- return the name of this host.
922444eaf03Seric **
923444eaf03Seric **	Parameters:
924444eaf03Seric **		hostbuf -- a place to return the name of this host.
925897f1869Seric **		size -- the size of hostbuf.
926444eaf03Seric **
927444eaf03Seric **	Returns:
928444eaf03Seric **		A list of aliases for this host.
929444eaf03Seric **
930444eaf03Seric **	Side Effects:
931d8d0a4aeSeric **		Adds numeric codes to $=w.
932444eaf03Seric */
933444eaf03Seric 
9348b212fe0Seric struct hostent *
935897f1869Seric myhostname(hostbuf, size)
936444eaf03Seric 	char hostbuf[];
937897f1869Seric 	int size;
938444eaf03Seric {
93938ad259dSeric 	register struct hostent *hp;
9408b212fe0Seric 	extern bool getcanonname();
9418b212fe0Seric 	extern int h_errno;
942444eaf03Seric 
943af5e902cSeric 	if (gethostname(hostbuf, size) < 0)
944af5e902cSeric 	{
945af5e902cSeric 		(void) strcpy(hostbuf, "localhost");
946af5e902cSeric 	}
9478b212fe0Seric 	hp = sm_gethostbyname(hostbuf);
9484a5c6430Seric 	if (hp == NULL)
9498b212fe0Seric 		return NULL;
9508b212fe0Seric 	if (strchr(hp->h_name, '.') != NULL || strchr(hostbuf, '.') == NULL)
951423161fbSeric 	{
95235852b23Seric 		(void) strncpy(hostbuf, hp->h_name, size - 1);
95335852b23Seric 		hostbuf[size - 1] = '\0';
9548b212fe0Seric 	}
9554a5c6430Seric 
9564a5c6430Seric #if NAMED_BIND
9578b212fe0Seric 	/*
9588b212fe0Seric 	**  If still no dot, try DNS directly (i.e., avoid NIS problems).
9598b212fe0Seric 	**  This ought to be driven from the configuration file, but
9608b212fe0Seric 	**  we are called before the configuration is read.  We could
9618b212fe0Seric 	**  check for an /etc/resolv.conf file, but that isn't required.
9628b212fe0Seric 	**  All in all, a bit of a mess.
9638b212fe0Seric 	*/
96423668637Seric 
9658b212fe0Seric 	if (strchr(hostbuf, '.') == NULL &&
9668b212fe0Seric 	    !getcanonname(hostbuf, size, TRUE) &&
9678b212fe0Seric 	    h_errno == TRY_AGAIN)
96823668637Seric 	{
9698b212fe0Seric 		/* try twice in case name server not yet started up */
9708b212fe0Seric 		message("My unqualifed host name (%s) unknown to DNS; sleeping for retry",
97123668637Seric 			hostbuf);
9728b212fe0Seric 		sleep(60);
9738b212fe0Seric 		if (!getcanonname(hostbuf, size, TRUE))
9748b212fe0Seric 			errno = h_errno + E_DNSBASE;
975747df804Seric 	}
976747df804Seric #endif
9778b212fe0Seric 	return (hp);
9787364df9fSeric }
979cb452edcSeric /*
9809f8b0eadSeric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
9819f8b0eadSeric **
9829f8b0eadSeric **	Uses RFC1413 protocol to try to get info from the other end.
983320e0d1cSeric **
984320e0d1cSeric **	Parameters:
985320e0d1cSeric **		fd -- the descriptor
986320e0d1cSeric **
987320e0d1cSeric **	Returns:
9889f8b0eadSeric **		The user@host information associated with this descriptor.
989320e0d1cSeric */
990320e0d1cSeric 
9919f8b0eadSeric static jmp_buf	CtxAuthTimeout;
9929f8b0eadSeric 
9938b212fe0Seric static void
9949f8b0eadSeric authtimeout()
9959f8b0eadSeric {
9969f8b0eadSeric 	longjmp(CtxAuthTimeout, 1);
9979f8b0eadSeric }
9989f8b0eadSeric 
999320e0d1cSeric char *
10009f8b0eadSeric getauthinfo(fd)
1001320e0d1cSeric 	int fd;
1002320e0d1cSeric {
10039f8b0eadSeric 	int falen;
1004a5546e24Seric 	register char *p;
10059f8b0eadSeric 	SOCKADDR la;
10069f8b0eadSeric 	int lalen;
10079f8b0eadSeric 	register struct servent *sp;
10089f8b0eadSeric 	int s;
10099f8b0eadSeric 	int i;
10109f8b0eadSeric 	EVENT *ev;
1011579270a3Seric 	int nleft;
1012f036c8d7Seric 	char ibuf[MAXNAME + 1];
10139f8b0eadSeric 	static char hbuf[MAXNAME * 2 + 2];
10149f8b0eadSeric 	extern char *hostnamebyanyaddr();
10159f8b0eadSeric 	extern char RealUserName[];			/* main.c */
1016320e0d1cSeric 
1017e29a76d1Seric 	falen = sizeof RealHostAddr;
10188b212fe0Seric 	if (isatty(fd) || getpeername(fd, &RealHostAddr.sa, &falen) < 0 ||
10198b212fe0Seric 	    falen <= 0 || RealHostAddr.sa.sa_family == 0)
10209f8b0eadSeric 	{
10219f8b0eadSeric 		(void) sprintf(hbuf, "%s@localhost", RealUserName);
102253853673Seric 		if (tTd(9, 1))
10239f8b0eadSeric 			printf("getauthinfo: %s\n", hbuf);
1024320e0d1cSeric 		return hbuf;
1025320e0d1cSeric 	}
10269f8b0eadSeric 
1027e29a76d1Seric 	if (RealHostName == NULL)
1028e29a76d1Seric 	{
1029e29a76d1Seric 		/* translate that to a host name */
1030e29a76d1Seric 		RealHostName = newstr(hostnamebyanyaddr(&RealHostAddr));
1031e29a76d1Seric 	}
1032e29a76d1Seric 
103393b3215bSeric 	if (TimeOuts.to_ident == 0)
103493b3215bSeric 		goto noident;
103593b3215bSeric 
10369f8b0eadSeric 	lalen = sizeof la;
1037e29a76d1Seric 	if (RealHostAddr.sa.sa_family != AF_INET ||
10389f8b0eadSeric 	    getsockname(fd, &la.sa, &lalen) < 0 || lalen <= 0 ||
10399f8b0eadSeric 	    la.sa.sa_family != AF_INET)
10409f8b0eadSeric 	{
10419f8b0eadSeric 		/* no ident info */
10429f8b0eadSeric 		goto noident;
10439f8b0eadSeric 	}
10449f8b0eadSeric 
10459f8b0eadSeric 	/* create ident query */
104681d2944fSeric 	(void) sprintf(ibuf, "%d,%d\r\n",
1047e29a76d1Seric 		ntohs(RealHostAddr.sin.sin_port), ntohs(la.sin.sin_port));
10489f8b0eadSeric 
10499f8b0eadSeric 	/* create local address */
1050d6af7dadSeric 	la.sin.sin_port = 0;
10519f8b0eadSeric 
10529f8b0eadSeric 	/* create foreign address */
10539f8b0eadSeric 	sp = getservbyname("auth", "tcp");
10549f8b0eadSeric 	if (sp != NULL)
1055e29a76d1Seric 		RealHostAddr.sin.sin_port = sp->s_port;
10569f8b0eadSeric 	else
1057e29a76d1Seric 		RealHostAddr.sin.sin_port = htons(113);
10589f8b0eadSeric 
10599f8b0eadSeric 	s = -1;
10609f8b0eadSeric 	if (setjmp(CtxAuthTimeout) != 0)
10619f8b0eadSeric 	{
10629f8b0eadSeric 		if (s >= 0)
10639f8b0eadSeric 			(void) close(s);
10649f8b0eadSeric 		goto noident;
10659f8b0eadSeric 	}
10669f8b0eadSeric 
10679f8b0eadSeric 	/* put a timeout around the whole thing */
1068a0f780efSeric 	ev = setevent(TimeOuts.to_ident, authtimeout, 0);
10699f8b0eadSeric 
1070d6af7dadSeric 	/* connect to foreign IDENT server using same address as SMTP socket */
10719f8b0eadSeric 	s = socket(AF_INET, SOCK_STREAM, 0);
10729f8b0eadSeric 	if (s < 0)
10739f8b0eadSeric 	{
10749f8b0eadSeric 		clrevent(ev);
10759f8b0eadSeric 		goto noident;
10769f8b0eadSeric 	}
1077d6af7dadSeric 	if (bind(s, &la.sa, sizeof la.sin) < 0 ||
1078e29a76d1Seric 	    connect(s, &RealHostAddr.sa, sizeof RealHostAddr.sin) < 0)
10799f8b0eadSeric 	{
10807c201575Seric 		goto closeident;
10819f8b0eadSeric 	}
10829f8b0eadSeric 
108353853673Seric 	if (tTd(9, 10))
108481d2944fSeric 		printf("getauthinfo: sent %s", ibuf);
10859f8b0eadSeric 
10869f8b0eadSeric 	/* send query */
108781d2944fSeric 	if (write(s, ibuf, strlen(ibuf)) < 0)
10889f8b0eadSeric 		goto closeident;
10899f8b0eadSeric 
10909f8b0eadSeric 	/* get result */
109181d2944fSeric 	p = &ibuf[0];
1092b26156f6Seric 	nleft = sizeof ibuf - 1;
1093579270a3Seric 	while ((i = read(s, p, nleft)) > 0)
1094579270a3Seric 	{
1095579270a3Seric 		p += i;
1096579270a3Seric 		nleft -= i;
1097579270a3Seric 	}
10989f8b0eadSeric 	(void) close(s);
10999f8b0eadSeric 	clrevent(ev);
110081d2944fSeric 	if (i < 0 || p == &ibuf[0])
11019f8b0eadSeric 		goto noident;
1102579270a3Seric 
1103579270a3Seric 	if (*--p == '\n' && *--p == '\r')
1104579270a3Seric 		p--;
1105579270a3Seric 	*++p = '\0';
11069f8b0eadSeric 
110753853673Seric 	if (tTd(9, 3))
110881d2944fSeric 		printf("getauthinfo:  got %s\n", ibuf);
11099f8b0eadSeric 
11109f8b0eadSeric 	/* parse result */
111181d2944fSeric 	p = strchr(ibuf, ':');
11129f8b0eadSeric 	if (p == NULL)
11139f8b0eadSeric 	{
11149f8b0eadSeric 		/* malformed response */
11159f8b0eadSeric 		goto noident;
11169f8b0eadSeric 	}
11179f8b0eadSeric 	while (isascii(*++p) && isspace(*p))
11189f8b0eadSeric 		continue;
11199f8b0eadSeric 	if (strncasecmp(p, "userid", 6) != 0)
11209f8b0eadSeric 	{
11219f8b0eadSeric 		/* presumably an error string */
11229f8b0eadSeric 		goto noident;
11239f8b0eadSeric 	}
11249f8b0eadSeric 	p += 6;
11259f8b0eadSeric 	while (isascii(*p) && isspace(*p))
11269f8b0eadSeric 		p++;
11279f8b0eadSeric 	if (*p++ != ':')
11289f8b0eadSeric 	{
11299f8b0eadSeric 		/* either useridxx or malformed response */
11309f8b0eadSeric 		goto noident;
11319f8b0eadSeric 	}
11329f8b0eadSeric 
11339f8b0eadSeric 	/* p now points to the OSTYPE field */
11348b212fe0Seric 	while (isascii(*p) && isspace(*p))
11358b212fe0Seric 		p++;
11368b212fe0Seric 	if (strncasecmp(p, "other", 5) == 0 &&
11378b212fe0Seric 	    (p[5] == ':' || p[5] == ' ' || p[5] == ',' || p[5] == '\0'))
11388b212fe0Seric 	{
11398b212fe0Seric 		/* not useful information */
11408b212fe0Seric 		goto noident;
11418b212fe0Seric 	}
11429f8b0eadSeric 	p = strchr(p, ':');
11439f8b0eadSeric 	if (p == NULL)
11449f8b0eadSeric 	{
11459f8b0eadSeric 		/* malformed response */
11469f8b0eadSeric 		goto noident;
11479f8b0eadSeric 	}
114853853673Seric 
114953853673Seric 	/* 1413 says don't do this -- but it's broken otherwise */
115053853673Seric 	while (isascii(*++p) && isspace(*p))
115153853673Seric 		continue;
11529f8b0eadSeric 
1153a7763879Seric 	/* p now points to the authenticated name -- copy carefully */
115481d2944fSeric 	cleanstrcpy(hbuf, p, MAXNAME);
115593433f31Seric 	i = strlen(hbuf);
1156a7763879Seric 	hbuf[i++] = '@';
1157a7763879Seric 	strcpy(&hbuf[i], RealHostName == NULL ? "localhost" : RealHostName);
115811af795cSeric 	goto postident;
115953853673Seric 
11607c201575Seric closeident:
11617c201575Seric 	(void) close(s);
11627c201575Seric 	clrevent(ev);
11637c201575Seric 
116453853673Seric noident:
1165f7869e68Seric 	if (RealHostName == NULL)
1166f7869e68Seric 	{
1167f7869e68Seric 		if (tTd(9, 1))
1168f7869e68Seric 			printf("getauthinfo: NULL\n");
1169f7869e68Seric 		return NULL;
1170f7869e68Seric 	}
117153853673Seric 	(void) strcpy(hbuf, RealHostName);
117253853673Seric 
117311af795cSeric postident:
117411af795cSeric #if IP_SRCROUTE
117511af795cSeric 	/*
117611af795cSeric 	**  Extract IP source routing information.
117711af795cSeric 	**
117811af795cSeric 	**	Format of output for a connection from site a through b
117911af795cSeric 	**	through c to d:
118011af795cSeric 	**		loose:      @site-c@site-b:site-a
118111af795cSeric 	**		strict:	   !@site-c@site-b:site-a
118211af795cSeric 	**
118311af795cSeric 	**	o - pointer within ipopt_list structure.
118411af795cSeric 	**	q - pointer within ls/ss rr route data
118511af795cSeric 	**	p - pointer to hbuf
118611af795cSeric 	*/
118711af795cSeric 
118811af795cSeric 	if (RealHostAddr.sa.sa_family == AF_INET)
118911af795cSeric 	{
119011af795cSeric 		int ipoptlen, j;
1191*4d6b8580Seric 		u_char *q;
119211af795cSeric 		u_char *o;
119311af795cSeric 		struct in_addr addr;
119411af795cSeric 		struct ipoption ipopt;
119511af795cSeric 
119611af795cSeric 		ipoptlen = sizeof ipopt;
119711af795cSeric 		if (getsockopt(fd, IPPROTO_IP, IP_OPTIONS,
119811af795cSeric 			       (char *) &ipopt, &ipoptlen) < 0)
119911af795cSeric 			goto noipsr;
120011af795cSeric 		if (ipoptlen == 0)
120111af795cSeric 			goto noipsr;
1202*4d6b8580Seric 		o = (u_char *) ipopt.ipopt_list;
120311af795cSeric 		while (o != NULL && o < (u_char *) (&ipopt + ipoptlen))
120411af795cSeric 		{
120511af795cSeric 			switch (*o)
120611af795cSeric 			{
120711af795cSeric 			  case IPOPT_EOL:
120811af795cSeric 				o = NULL;
120911af795cSeric 				break;
121011af795cSeric 
121111af795cSeric 			  case IPOPT_NOP:
121211af795cSeric 				o++;
121311af795cSeric 				break;
121411af795cSeric 
121511af795cSeric 			  case IPOPT_SSRR:
121611af795cSeric 			  case IPOPT_LSRR:
121711af795cSeric 				p = &hbuf[strlen(hbuf)];
121811af795cSeric 				sprintf(p, " [%s@%s",
121911af795cSeric 				    *o == IPOPT_SSRR ? "!" : "",
122011af795cSeric 				    inet_ntoa(ipopt.ipopt_dst));
122111af795cSeric 				p += strlen(p);
122211af795cSeric 
122311af795cSeric 				/* o[1] is option length */
122411af795cSeric 				j = *++o / sizeof(struct in_addr) - 1;
122511af795cSeric 
122611af795cSeric 				/* q skips length and router pointer to data */
122711af795cSeric 				q = o + 2;
122811af795cSeric 				for ( ; j >= 0; j--)
122911af795cSeric 				{
123011af795cSeric 					memcpy(&addr, q, sizeof(addr));
1231*4d6b8580Seric 					sprintf(p, "%c%s",
123211af795cSeric 						     j ? '@' : ':',
123311af795cSeric 						     inet_ntoa(addr));
1234*4d6b8580Seric 					p += strlen(p);
123511af795cSeric 					q += sizeof(struct in_addr);
123611af795cSeric 				}
123711af795cSeric 				o += *o;
123811af795cSeric 				break;
123911af795cSeric 
124011af795cSeric 			  default:
124111af795cSeric 				/* Skip over option */
124211af795cSeric 				o += o[1];
124311af795cSeric 				break;
124411af795cSeric 			}
124511af795cSeric 		}
124611af795cSeric 		strcat(hbuf,"]");
124711af795cSeric 		goto postipsr;
124811af795cSeric 	}
124911af795cSeric #endif
125011af795cSeric 
125111af795cSeric noipsr:
1252f7869e68Seric 	if (RealHostName != NULL && RealHostName[0] != '[')
12539f8b0eadSeric 	{
12549f8b0eadSeric 		p = &hbuf[strlen(hbuf)];
12559f8b0eadSeric 		(void) sprintf(p, " [%s]", anynet_ntoa(&RealHostAddr));
12569f8b0eadSeric 	}
125711af795cSeric 
125811af795cSeric postipsr:
125953853673Seric 	if (tTd(9, 1))
12609f8b0eadSeric 		printf("getauthinfo: %s\n", hbuf);
12619f8b0eadSeric 	return hbuf;
12629f8b0eadSeric }
1263320e0d1cSeric /*
126408de856eSeric **  HOST_MAP_LOOKUP -- turn a hostname into canonical form
126515d084d5Seric **
126615d084d5Seric **	Parameters:
126705b57da8Seric **		map -- a pointer to this map (unused).
126808de856eSeric **		name -- the (presumably unqualified) hostname.
126900b385a9Seric **		av -- unused -- for compatibility with other mapping
1270d798a1deSeric **			functions.
12712d29d43aSeric **		statp -- an exit status (out parameter) -- set to
12722d29d43aSeric **			EX_TEMPFAIL if the name server is unavailable.
127315d084d5Seric **
127415d084d5Seric **	Returns:
127515d084d5Seric **		The mapping, if found.
127615d084d5Seric **		NULL if no mapping found.
127715d084d5Seric **
127815d084d5Seric **	Side Effects:
127915d084d5Seric **		Looks up the host specified in hbuf.  If it is not
128015d084d5Seric **		the canonical name for that host, return the canonical
128115d084d5Seric **		name.
1282f36ede03Sbostic */
1283cb452edcSeric 
128415d084d5Seric char *
128500b385a9Seric host_map_lookup(map, name, av, statp)
128605b57da8Seric 	MAP *map;
128708de856eSeric 	char *name;
128800b385a9Seric 	char **av;
12892d29d43aSeric 	int *statp;
129099f7cf32Seric {
129199f7cf32Seric 	register struct hostent *hp;
12928b212fe0Seric 	struct in_addr in_addr;
129305b57da8Seric 	char *cp;
1294eea91d78Seric 	register STAB *s;
12958b212fe0Seric 	char hbuf[MAXNAME + 1];
12969d4a8008Seric #if NAMED_BIND
1297eea91d78Seric 	extern int h_errno;
1298c304a798Seric #endif
12995f78836eSmiriam 
1300f36ede03Sbostic 	/*
1301eea91d78Seric 	**  See if we have already looked up this name.  If so, just
1302eea91d78Seric 	**  return it.
1303eea91d78Seric 	*/
1304eea91d78Seric 
130508de856eSeric 	s = stab(name, ST_NAMECANON, ST_ENTER);
1306eea91d78Seric 	if (bitset(NCF_VALID, s->s_namecanon.nc_flags))
1307eea91d78Seric 	{
1308f92c3297Seric 		if (tTd(9, 1))
130908de856eSeric 			printf("host_map_lookup(%s) => CACHE %s\n",
131098826a83Seric 			       name,
131198826a83Seric 			       s->s_namecanon.nc_cname == NULL
131298826a83Seric 					? "NULL"
13135f08d413Seric 					: s->s_namecanon.nc_cname);
1314eea91d78Seric 		errno = s->s_namecanon.nc_errno;
13159d4a8008Seric #if NAMED_BIND
1316eea91d78Seric 		h_errno = s->s_namecanon.nc_herrno;
1317c304a798Seric #endif
1318eea91d78Seric 		*statp = s->s_namecanon.nc_stat;
1319fcfb36d9Seric 		if (*statp == EX_TEMPFAIL)
1320ed63aae0Seric 		{
13215b068d51Seric 			CurEnv->e_status = "4.4.3";
1322fcfb36d9Seric 			usrerr("451 %s: Name server timeout",
1323ed63aae0Seric 				shortenstring(name, 33));
1324ed63aae0Seric 		}
1325eea91d78Seric 		return s->s_namecanon.nc_cname;
1326eea91d78Seric 	}
1327eea91d78Seric 
1328eea91d78Seric 	/*
1329eea91d78Seric 	**  If first character is a bracket, then it is an address
1330eea91d78Seric 	**  lookup.  Address is copied into a temporary buffer to
133108de856eSeric 	**  strip the brackets and to preserve name if address is
1332eea91d78Seric 	**  unknown.
1333f36ede03Sbostic 	*/
133415d084d5Seric 
133508de856eSeric 	if (*name != '[')
133615d084d5Seric 	{
1337d798a1deSeric 		extern bool getcanonname();
1338d798a1deSeric 
13398cb4653dSeric 		if (tTd(9, 1))
134008de856eSeric 			printf("host_map_lookup(%s) => ", name);
1341eea91d78Seric 		s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
13428b212fe0Seric 		if (strlen(name) < sizeof hbuf)
134308de856eSeric 			(void) strcpy(hbuf, name);
13448b212fe0Seric 		else
13458b212fe0Seric 		{
13468b212fe0Seric 			bcopy(name, hbuf, sizeof hbuf - 1);
13478b212fe0Seric 			hbuf[sizeof hbuf - 1] = '\0';
13488b212fe0Seric 		}
13490183c3f6Seric 		if (getcanonname(hbuf, sizeof hbuf - 1, !NoMXforCanon))
13509040ec4fSeric 		{
13519040ec4fSeric 			if (tTd(9, 1))
13529040ec4fSeric 				printf("%s\n", hbuf);
135300b385a9Seric 			cp = map_rewrite(map, hbuf, strlen(hbuf), av);
135400b385a9Seric 			s->s_namecanon.nc_cname = newstr(cp);
135500b385a9Seric 			return cp;
13569040ec4fSeric 		}
135715d084d5Seric 		else
13589040ec4fSeric 		{
13592d29d43aSeric 			register struct hostent *hp;
13602d29d43aSeric 
1361c304a798Seric 			s->s_namecanon.nc_errno = errno;
13629d4a8008Seric #if NAMED_BIND
1363c304a798Seric 			s->s_namecanon.nc_herrno = h_errno;
13649040ec4fSeric 			if (tTd(9, 1))
13652d29d43aSeric 				printf("FAIL (%d)\n", h_errno);
13662d29d43aSeric 			switch (h_errno)
13672d29d43aSeric 			{
13682d29d43aSeric 			  case TRY_AGAIN:
136989cb2793Seric 				if (UseNameServer)
13708820d51bSeric 				{
13715b068d51Seric 					CurEnv->e_status = "4.4.3";
1372fcfb36d9Seric 					usrerr("451 %s: Name server timeout",
1373ed63aae0Seric 						shortenstring(name, 33));
13748820d51bSeric 				}
13752d29d43aSeric 				*statp = EX_TEMPFAIL;
13762d29d43aSeric 				break;
13772d29d43aSeric 
13782d29d43aSeric 			  case HOST_NOT_FOUND:
137980027ae6Seric 			  case NO_DATA:
13802d29d43aSeric 				*statp = EX_NOHOST;
13812d29d43aSeric 				break;
13822d29d43aSeric 
13832d29d43aSeric 			  case NO_RECOVERY:
13842d29d43aSeric 				*statp = EX_SOFTWARE;
13852d29d43aSeric 				break;
13862d29d43aSeric 
13872d29d43aSeric 			  default:
13882d29d43aSeric 				*statp = EX_UNAVAILABLE;
13892d29d43aSeric 				break;
13902d29d43aSeric 			}
1391c304a798Seric #else
1392c304a798Seric 			if (tTd(9, 1))
1393c304a798Seric 				printf("FAIL\n");
1394c304a798Seric 			*statp = EX_NOHOST;
1395c304a798Seric #endif
1396eea91d78Seric 			s->s_namecanon.nc_stat = *statp;
13978b212fe0Seric 			if ((*statp != EX_TEMPFAIL && *statp != EX_NOHOST) ||
13988b212fe0Seric 			    UseNameServer)
139915d084d5Seric 				return NULL;
14002d29d43aSeric 
14012d29d43aSeric 			/*
14022d29d43aSeric 			**  Try to look it up in /etc/hosts
14032d29d43aSeric 			*/
14042d29d43aSeric 
14058b212fe0Seric 			hp = sm_gethostbyname(name);
14062d29d43aSeric 			if (hp == NULL)
14072d29d43aSeric 			{
14082d29d43aSeric 				/* no dice there either */
1409eea91d78Seric 				s->s_namecanon.nc_stat = *statp = EX_NOHOST;
14102d29d43aSeric 				return NULL;
14112d29d43aSeric 			}
14122d29d43aSeric 
1413eea91d78Seric 			s->s_namecanon.nc_stat = *statp = EX_OK;
141400b385a9Seric 			cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
141500b385a9Seric 			s->s_namecanon.nc_cname = newstr(cp);
141600b385a9Seric 			return cp;
141715d084d5Seric 		}
14189040ec4fSeric 	}
141908de856eSeric 	if ((cp = strchr(name, ']')) == NULL)
142015d084d5Seric 		return (NULL);
142134e39927Sbostic 	*cp = '\0';
14228b212fe0Seric 	in_addr.s_addr = inet_addr(&name[1]);
142338ad259dSeric 
142438ad259dSeric 	/* nope -- ask the name server */
14258b212fe0Seric 	hp = sm_gethostbyaddr((char *)&in_addr, INADDRSZ, AF_INET);
1426eea91d78Seric 	s->s_namecanon.nc_errno = errno;
14279d4a8008Seric #if NAMED_BIND
1428eea91d78Seric 	s->s_namecanon.nc_herrno = h_errno;
1429c304a798Seric #endif
1430eea91d78Seric 	s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
14315f78836eSmiriam 	if (hp == NULL)
1432eea91d78Seric 	{
1433eea91d78Seric 		s->s_namecanon.nc_stat = *statp = EX_NOHOST;
143415d084d5Seric 		return (NULL);
1435eea91d78Seric 	}
143615d084d5Seric 
143738ad259dSeric 	/* found a match -- copy out */
143800b385a9Seric 	cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
1439eea91d78Seric 	s->s_namecanon.nc_stat = *statp = EX_OK;
144000b385a9Seric 	s->s_namecanon.nc_cname = newstr(cp);
144100b385a9Seric 	return cp;
144299f7cf32Seric }
1443e2f2f828Seric /*
1444e2f2f828Seric **  ANYNET_NTOA -- convert a network address to printable form.
1445e2f2f828Seric **
1446e2f2f828Seric **	Parameters:
1447e2f2f828Seric **		sap -- a pointer to a sockaddr structure.
1448e2f2f828Seric **
1449e2f2f828Seric **	Returns:
1450e2f2f828Seric **		A printable version of that sockaddr.
1451e2f2f828Seric */
1452e2f2f828Seric 
1453543d9d93Seric #ifdef NETLINK
145406cc5485Seric # include <net/if_dl.h>
145506cc5485Seric #endif
145606cc5485Seric 
1457e2f2f828Seric char *
1458e2f2f828Seric anynet_ntoa(sap)
1459e2f2f828Seric 	register SOCKADDR *sap;
1460e2f2f828Seric {
1461e2f2f828Seric 	register char *bp;
1462e2f2f828Seric 	register char *ap;
1463e2f2f828Seric 	int l;
1464e387851eSeric 	static char buf[100];
1465e2f2f828Seric 
14668cb4653dSeric 	/* check for null/zero family */
14678cb4653dSeric 	if (sap == NULL)
14688cb4653dSeric 		return "NULLADDR";
14698cb4653dSeric 	if (sap->sa.sa_family == 0)
14708cb4653dSeric 		return "0";
14718cb4653dSeric 
1472e387851eSeric 	switch (sap->sa.sa_family)
1473e387851eSeric 	{
1474139b52c8Seric #ifdef NETUNIX
1475e387851eSeric 	  case AF_UNIX:
1476c24cf5a4Seric 	  	if (sap->sunix.sun_path[0] != '\0')
1477c24cf5a4Seric 	  		sprintf(buf, "[UNIX: %.64s]", sap->sunix.sun_path);
1478e387851eSeric 	  	else
1479e387851eSeric 	  		sprintf(buf, "[UNIX: localhost]");
1480e387851eSeric 		return buf;
1481e387851eSeric #endif
1482e387851eSeric 
148383c1f4bcSeric #ifdef NETINET
1484e387851eSeric 	  case AF_INET:
1485482e28bcSeric 		return inet_ntoa(sap->sin.sin_addr);
148683c1f4bcSeric #endif
1487e2f2f828Seric 
1488543d9d93Seric #ifdef NETLINK
148906cc5485Seric 	  case AF_LINK:
149006cc5485Seric 		sprintf(buf, "[LINK: %s]",
149106cc5485Seric 			link_ntoa((struct sockaddr_dl *) &sap->sa));
149206cc5485Seric 		return buf;
149306cc5485Seric #endif
1494e387851eSeric 	  default:
149506cc5485Seric 		/* this case is needed when nothing is #defined */
149606cc5485Seric 		/* in order to keep the switch syntactically correct */
1497e387851eSeric 		break;
1498e387851eSeric 	}
1499e387851eSeric 
1500e2f2f828Seric 	/* unknown family -- just dump bytes */
150183c1f4bcSeric 	(void) sprintf(buf, "Family %d: ", sap->sa.sa_family);
1502e2f2f828Seric 	bp = &buf[strlen(buf)];
150383c1f4bcSeric 	ap = sap->sa.sa_data;
150483c1f4bcSeric 	for (l = sizeof sap->sa.sa_data; --l >= 0; )
1505e2f2f828Seric 	{
1506e2f2f828Seric 		(void) sprintf(bp, "%02x:", *ap++ & 0377);
1507e2f2f828Seric 		bp += 3;
1508e2f2f828Seric 	}
1509e2f2f828Seric 	*--bp = '\0';
1510e2f2f828Seric 	return buf;
1511e2f2f828Seric }
15129f8b0eadSeric /*
15139f8b0eadSeric **  HOSTNAMEBYANYADDR -- return name of host based on address
15149f8b0eadSeric **
15159f8b0eadSeric **	Parameters:
15169f8b0eadSeric **		sap -- SOCKADDR pointer
15179f8b0eadSeric **
15189f8b0eadSeric **	Returns:
15199f8b0eadSeric **		text representation of host name.
15209f8b0eadSeric **
15219f8b0eadSeric **	Side Effects:
15229f8b0eadSeric **		none.
15239f8b0eadSeric */
15249f8b0eadSeric 
15259f8b0eadSeric char *
15269f8b0eadSeric hostnamebyanyaddr(sap)
15279f8b0eadSeric 	register SOCKADDR *sap;
15289f8b0eadSeric {
15299f8b0eadSeric 	register struct hostent *hp;
15303490b9dfSeric 	int saveretry;
15313490b9dfSeric 
15329d4a8008Seric #if NAMED_BIND
15333490b9dfSeric 	/* shorten name server timeout to avoid higher level timeouts */
15343490b9dfSeric 	saveretry = _res.retry;
15353490b9dfSeric 	_res.retry = 3;
15363490b9dfSeric #endif /* NAMED_BIND */
15373490b9dfSeric 
15389f8b0eadSeric 	switch (sap->sa.sa_family)
15399f8b0eadSeric 	{
15409f8b0eadSeric #ifdef NETINET
15419f8b0eadSeric 	  case AF_INET:
15428b212fe0Seric 		hp = sm_gethostbyaddr((char *) &sap->sin.sin_addr,
15438b212fe0Seric 			INADDRSZ,
15449f8b0eadSeric 			AF_INET);
15459f8b0eadSeric 		break;
15469f8b0eadSeric #endif
15479f8b0eadSeric 
15489f8b0eadSeric #ifdef NETISO
15499f8b0eadSeric 	  case AF_ISO:
15508b212fe0Seric 		hp = sm_gethostbyaddr((char *) &sap->siso.siso_addr,
15519f8b0eadSeric 			sizeof sap->siso.siso_addr,
15529f8b0eadSeric 			AF_ISO);
15539f8b0eadSeric 		break;
15549f8b0eadSeric #endif
15559f8b0eadSeric 
1556e387851eSeric 	  case AF_UNIX:
1557e387851eSeric 		hp = NULL;
1558e387851eSeric 		break;
1559e387851eSeric 
15609f8b0eadSeric 	  default:
15618b212fe0Seric 		hp = sm_gethostbyaddr(sap->sa.sa_data,
15629f8b0eadSeric 			   sizeof sap->sa.sa_data,
15639f8b0eadSeric 			   sap->sa.sa_family);
15649f8b0eadSeric 		break;
15659f8b0eadSeric 	}
15669f8b0eadSeric 
15679d4a8008Seric #if NAMED_BIND
15683490b9dfSeric 	_res.retry = saveretry;
15693490b9dfSeric #endif /* NAMED_BIND */
15703490b9dfSeric 
15719f8b0eadSeric 	if (hp != NULL)
15729f8b0eadSeric 		return hp->h_name;
15739f8b0eadSeric 	else
15749f8b0eadSeric 	{
15759f8b0eadSeric 		/* produce a dotted quad */
15769f8b0eadSeric 		static char buf[512];
15779f8b0eadSeric 
15789f8b0eadSeric 		(void) sprintf(buf, "[%s]", anynet_ntoa(sap));
15799f8b0eadSeric 		return buf;
15809f8b0eadSeric 	}
15819f8b0eadSeric }
1582f36ede03Sbostic 
15836c2c3107Seric # else /* DAEMON */
158499f7cf32Seric /* code for systems without sophisticated networking */
1585444eaf03Seric 
1586444eaf03Seric /*
1587444eaf03Seric **  MYHOSTNAME -- stub version for case of no daemon code.
158821e9914dSeric **
158921e9914dSeric **	Can't convert to upper case here because might be a UUCP name.
1590897f1869Seric **
1591897f1869Seric **	Mark, you can change this to be anything you want......
1592444eaf03Seric */
1593444eaf03Seric 
1594444eaf03Seric char **
1595897f1869Seric myhostname(hostbuf, size)
1596444eaf03Seric 	char hostbuf[];
1597897f1869Seric 	int size;
1598444eaf03Seric {
1599444eaf03Seric 	register FILE *f;
1600444eaf03Seric 
1601444eaf03Seric 	hostbuf[0] = '\0';
1602444eaf03Seric 	f = fopen("/usr/include/whoami", "r");
1603444eaf03Seric 	if (f != NULL)
1604444eaf03Seric 	{
1605897f1869Seric 		(void) fgets(hostbuf, size, f);
1606444eaf03Seric 		fixcrlf(hostbuf, TRUE);
1607444eaf03Seric 		(void) fclose(f);
1608444eaf03Seric 	}
1609444eaf03Seric 	return (NULL);
1610444eaf03Seric }
161199f7cf32Seric /*
16129f8b0eadSeric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
1613320e0d1cSeric **
1614320e0d1cSeric **	Parameters:
1615320e0d1cSeric **		fd -- the descriptor
1616320e0d1cSeric **
1617320e0d1cSeric **	Returns:
1618320e0d1cSeric **		The host name associated with this descriptor, if it can
1619320e0d1cSeric **			be determined.
1620320e0d1cSeric **		NULL otherwise.
1621320e0d1cSeric **
1622320e0d1cSeric **	Side Effects:
1623320e0d1cSeric **		none
1624320e0d1cSeric */
1625320e0d1cSeric 
1626320e0d1cSeric char *
16279f8b0eadSeric getauthinfo(fd)
1628320e0d1cSeric 	int fd;
1629320e0d1cSeric {
1630320e0d1cSeric 	return NULL;
1631320e0d1cSeric }
1632320e0d1cSeric /*
163399f7cf32Seric **  MAPHOSTNAME -- turn a hostname into canonical form
163499f7cf32Seric **
163599f7cf32Seric **	Parameters:
163605b57da8Seric **		map -- a pointer to the database map.
163708de856eSeric **		name -- a buffer containing a hostname.
163815d084d5Seric **		avp -- a pointer to a (cf file defined) argument vector.
16392d29d43aSeric **		statp -- an exit status (out parameter).
164099f7cf32Seric **
164199f7cf32Seric **	Returns:
164215d084d5Seric **		mapped host name
1643cb452edcSeric **		FALSE otherwise.
164499f7cf32Seric **
164599f7cf32Seric **	Side Effects:
164608de856eSeric **		Looks up the host specified in name.  If it is not
164799f7cf32Seric **		the canonical name for that host, replace it with
164899f7cf32Seric **		the canonical name.  If the name is unknown, or it
164999f7cf32Seric **		is already the canonical name, leave it unchanged.
165099f7cf32Seric */
165199f7cf32Seric 
165299f7cf32Seric /*ARGSUSED*/
165315d084d5Seric char *
165408de856eSeric host_map_lookup(map, name, avp, statp)
165505b57da8Seric 	MAP *map;
165608de856eSeric 	char *name;
165715d084d5Seric 	char **avp;
16582d29d43aSeric 	char *statp;
165999f7cf32Seric {
16602d29d43aSeric 	register struct hostent *hp;
16612d29d43aSeric 
16628b212fe0Seric 	hp = sm_gethostbyname(name);
16632d29d43aSeric 	if (hp != NULL)
16642d29d43aSeric 		return hp->h_name;
16652d29d43aSeric 	*statp = EX_NOHOST;
166615d084d5Seric 	return NULL;
166799f7cf32Seric }
166899f7cf32Seric 
16696c2c3107Seric #endif /* DAEMON */
1670