xref: /original-bsd/usr.sbin/sendmail/src/daemon.c (revision 9cb4faa5)
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*9cb4faa5Seric static char sccsid[] = "@(#)daemon.c	8.105 (Berkeley) 06/20/95 (with daemon mode)";
15d0a9e852Seric #else
16*9cb4faa5Seric static char sccsid[] = "@(#)daemon.c	8.105 (Berkeley) 06/20/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
getrequests()887fa39d90Seric getrequests()
897fa39d90Seric {
901c71e510Seric 	int t;
9115d084d5Seric 	bool refusingconnections = TRUE;
920aae1086Seric 	FILE *pidf;
93dadb8687Seric 	int socksize;
9486aab53cSeric #if 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 
14986aab53cSeric #if 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;
1790084e6f6Seric 			sleep(15);
18015d084d5Seric 			continue;
18115d084d5Seric 		}
18215d084d5Seric 
1838b212fe0Seric 		/* arrange to (re)open the socket if necessary */
18415d084d5Seric 		if (refusingconnections)
18515d084d5Seric 		{
1868a0cb579Seric 			(void) opendaemonsocket(FALSE);
18715d084d5Seric 			refusingconnections = FALSE;
1886775ec03Sbostic 		}
189a44d5a5eSeric 
19086aab53cSeric #if XDEBUG
191dfe840b2Seric 		/* check for disaster */
192dfe840b2Seric 		{
19335852b23Seric 			char jbuf[MAXHOSTNAMELEN];
194dfe840b2Seric 
1958b212fe0Seric 			expand("\201j", jbuf, sizeof jbuf, CurEnv);
1968b212fe0Seric 			if (!wordinclass(jbuf, 'w'))
197dfe840b2Seric 			{
198dfe840b2Seric 				dumpstate("daemon lost $j");
199dfe840b2Seric 				syslog(LOG_ALERT, "daemon process doesn't have $j in $=w; see syslog");
200dfe840b2Seric 				abort();
201dfe840b2Seric 			}
20235852b23Seric 			else if (j_has_dot && strchr(jbuf, '.') == NULL)
203dfe840b2Seric 			{
204dfe840b2Seric 				dumpstate("daemon $j lost dot");
205dfe840b2Seric 				syslog(LOG_ALERT, "daemon process $j lost dot; see syslog");
206dfe840b2Seric 				abort();
207dfe840b2Seric 			}
208dfe840b2Seric 		}
209dfe840b2Seric #endif
210dfe840b2Seric 
2111c71e510Seric 		/* wait for a connection */
212b28779c8Seric 		setproctitle("accepting connections");
2131c71e510Seric 		do
2141c71e510Seric 		{
2151c71e510Seric 			errno = 0;
216dadb8687Seric 			lotherend = socksize;
2179b100374Sbostic 			t = accept(DaemonSocket,
2189b100374Sbostic 			    (struct sockaddr *)&RealHostAddr, &lotherend);
2191c71e510Seric 		} while (t < 0 && errno == EINTR);
2201c71e510Seric 		if (t < 0)
2211c71e510Seric 		{
2221c71e510Seric 			syserr("getrequests: accept");
2238b212fe0Seric 
2248b212fe0Seric 			/* arrange to re-open the socket next time around */
2258b212fe0Seric 			(void) close(DaemonSocket);
2268b212fe0Seric 			DaemonSocket = -1;
2271c71e510Seric 			sleep(5);
2281c71e510Seric 			continue;
2291c71e510Seric 		}
230d0a9e852Seric 
231d0a9e852Seric 		/*
232d0a9e852Seric 		**  Create a subprocess to process the mail.
233d0a9e852Seric 		*/
234d0a9e852Seric 
23561e4310fSeric 		if (tTd(15, 2))
2361c71e510Seric 			printf("getrequests: forking (fd = %d)\n", t);
237eb889047Seric 
238a8268164Seric 		pid = fork();
239a8268164Seric 		if (pid < 0)
240a8268164Seric 		{
241a8268164Seric 			syserr("daemon: cannot fork");
242a8268164Seric 			sleep(10);
2431c71e510Seric 			(void) close(t);
244a8268164Seric 			continue;
245a8268164Seric 		}
246a8268164Seric 
247a8268164Seric 		if (pid == 0)
248a8268164Seric 		{
249da662164Seric 			char *p;
2509f8b0eadSeric 			extern char *hostnamebyanyaddr();
2518b212fe0Seric 			extern void intsig();
252a44d5a5eSeric 
253a8268164Seric 			/*
254a8268164Seric 			**  CHILD -- return to caller.
255a44d5a5eSeric 			**	Collect verified idea of sending host.
256a8268164Seric 			**	Verify calling user id if possible here.
257a8268164Seric 			*/
258a8268164Seric 
2592b9178d3Seric 			(void) setsignal(SIGCHLD, SIG_DFL);
2608b212fe0Seric 			(void) setsignal(SIGHUP, intsig);
2618b212fe0Seric 			(void) close(DaemonSocket);
2629f9b003eSeric 			DisConnected = FALSE;
263779ac194Seric 
2644dd09a90Seric 			setproctitle("startup with %s",
2654dd09a90Seric 				anynet_ntoa(&RealHostAddr));
2664dd09a90Seric 
267a44d5a5eSeric 			/* determine host name */
268da662164Seric 			p = hostnamebyanyaddr(&RealHostAddr);
26937b4af40Seric 			if (strlen(p) > MAXNAME)
27037b4af40Seric 				p[MAXNAME] = '\0';
271da662164Seric 			RealHostName = newstr(p);
2724dd09a90Seric 			setproctitle("startup with %s", p);
27329dcf4baSeric 
274335eae58Seric 			if ((InChannel = fdopen(t, "r")) == NULL ||
275335eae58Seric 			    (t = dup(t)) < 0 ||
276335eae58Seric 			    (OutChannel = fdopen(t, "w")) == NULL)
277335eae58Seric 			{
278335eae58Seric 				syserr("cannot open SMTP server channel, fd=%d", t);
279335eae58Seric 				exit(0);
280335eae58Seric 			}
281244b09d1Seric 
28229dcf4baSeric 			/* should we check for illegal connection here? XXX */
283e17a3a5aSeric #ifdef XLA
284e17a3a5aSeric 			if (!xla_host_ok(RealHostName))
285e17a3a5aSeric 			{
286244b09d1Seric 				message("421 Too many SMTP sessions for this host");
287e17a3a5aSeric 				exit(0);
288e17a3a5aSeric 			}
289e17a3a5aSeric #endif
290a44d5a5eSeric 
29161e4310fSeric 			if (tTd(15, 2))
292d0a9e852Seric 				printf("getreq: returning\n");
293a8268164Seric 			return;
294a8268164Seric 		}
295a8268164Seric 
2963f1260a1Seric 		CurChildren++;
2973f1260a1Seric 
2983c154354Seric 		/* close the port so that others will hang (for a while) */
2993c154354Seric 		(void) close(t);
3008e3e4b17Seric 	}
3013c154354Seric 	/*NOTREACHED*/
3023c154354Seric }
3038e3e4b17Seric /*
3040084e6f6Seric **  OPENDAEMONSOCKET -- open the SMTP socket
3050084e6f6Seric **
3060084e6f6Seric **	Deals with setting all appropriate options.  DaemonAddr must
3070084e6f6Seric **	be set up in advance.
3080084e6f6Seric **
3090084e6f6Seric **	Parameters:
31088ea5609Seric **		firsttime -- set if this is the initial open.
3110084e6f6Seric **
3120084e6f6Seric **	Returns:
3130084e6f6Seric **		Size in bytes of the daemon socket addr.
3140084e6f6Seric **
3150084e6f6Seric **	Side Effects:
3160084e6f6Seric **		Leaves DaemonSocket set to the open socket.
3170084e6f6Seric **		Exits if the socket cannot be created.
3180084e6f6Seric */
3190084e6f6Seric 
3206173568dSeric #define MAXOPENTRIES	10	/* maximum number of tries to open connection */
3216173568dSeric 
3220084e6f6Seric int
opendaemonsocket(firsttime)32388ea5609Seric opendaemonsocket(firsttime)
32488ea5609Seric 	bool firsttime;
3250084e6f6Seric {
3260084e6f6Seric 	int on = 1;
3278b212fe0Seric 	int socksize = 0;
3286173568dSeric 	int ntries = 0;
3296173568dSeric 	int saveerrno;
3300084e6f6Seric 
3310084e6f6Seric 	if (tTd(15, 2))
3320084e6f6Seric 		printf("opendaemonsocket()\n");
3330084e6f6Seric 
3346173568dSeric 	do
3356173568dSeric 	{
336254b93c3Seric 		if (ntries > 0)
337254b93c3Seric 			sleep(5);
33888ea5609Seric 		if (firsttime || DaemonSocket < 0)
33988ea5609Seric 		{
3400084e6f6Seric 			DaemonSocket = socket(DaemonAddr.sa.sa_family, SOCK_STREAM, 0);
3410084e6f6Seric 			if (DaemonSocket < 0)
3420084e6f6Seric 			{
3430084e6f6Seric 				/* probably another daemon already */
3446173568dSeric 				saveerrno = errno;
3450084e6f6Seric 				syserr("opendaemonsocket: can't create server SMTP socket");
3460084e6f6Seric 			  severe:
3470084e6f6Seric # ifdef LOG
3480084e6f6Seric 				if (LogLevel > 0)
3490084e6f6Seric 					syslog(LOG_ALERT, "problem creating SMTP socket");
3500084e6f6Seric # endif /* LOG */
3516173568dSeric 				DaemonSocket = -1;
3526173568dSeric 				continue;
3530084e6f6Seric 			}
3540084e6f6Seric 
3550084e6f6Seric 			/* turn on network debugging? */
3560084e6f6Seric 			if (tTd(15, 101))
3576173568dSeric 				(void) setsockopt(DaemonSocket, SOL_SOCKET,
3586173568dSeric 						  SO_DEBUG, (char *)&on,
3596173568dSeric 						  sizeof on);
3600084e6f6Seric 
3616173568dSeric 			(void) setsockopt(DaemonSocket, SOL_SOCKET,
3626173568dSeric 					  SO_REUSEADDR, (char *)&on, sizeof on);
3636173568dSeric 			(void) setsockopt(DaemonSocket, SOL_SOCKET,
3646173568dSeric 					  SO_KEEPALIVE, (char *)&on, sizeof on);
3650084e6f6Seric 
3660084e6f6Seric #ifdef SO_RCVBUF
3670084e6f6Seric 			if (TcpRcvBufferSize > 0)
3680084e6f6Seric 			{
3696173568dSeric 				if (setsockopt(DaemonSocket, SOL_SOCKET,
3706173568dSeric 					       SO_RCVBUF,
3710084e6f6Seric 					       (char *) &TcpRcvBufferSize,
3720084e6f6Seric 					       sizeof(TcpRcvBufferSize)) < 0)
3730084e6f6Seric 					syserr("getrequests: setsockopt(SO_RCVBUF)");
3740084e6f6Seric 			}
3750084e6f6Seric #endif
3760084e6f6Seric 
3770084e6f6Seric 			switch (DaemonAddr.sa.sa_family)
3780084e6f6Seric 			{
37986aab53cSeric # if NETINET
3800084e6f6Seric 			  case AF_INET:
3810084e6f6Seric 				socksize = sizeof DaemonAddr.sin;
3820084e6f6Seric 				break;
3830084e6f6Seric # endif
3840084e6f6Seric 
38586aab53cSeric # if NETISO
3860084e6f6Seric 			  case AF_ISO:
3870084e6f6Seric 				socksize = sizeof DaemonAddr.siso;
3880084e6f6Seric 				break;
3890084e6f6Seric # endif
3900084e6f6Seric 
3910084e6f6Seric 			  default:
3920084e6f6Seric 				socksize = sizeof DaemonAddr;
3930084e6f6Seric 				break;
3940084e6f6Seric 			}
3950084e6f6Seric 
3960084e6f6Seric 			if (bind(DaemonSocket, &DaemonAddr.sa, socksize) < 0)
3970084e6f6Seric 			{
3986173568dSeric 				saveerrno = errno;
3990084e6f6Seric 				syserr("getrequests: cannot bind");
4000084e6f6Seric 				(void) close(DaemonSocket);
4010084e6f6Seric 				goto severe;
4020084e6f6Seric 			}
40388ea5609Seric 		}
40488ea5609Seric 		if (!firsttime && listen(DaemonSocket, ListenQueueSize) < 0)
4050084e6f6Seric 		{
4066173568dSeric 			saveerrno = errno;
4070084e6f6Seric 			syserr("getrequests: cannot listen");
4080084e6f6Seric 			(void) close(DaemonSocket);
4090084e6f6Seric 			goto severe;
4100084e6f6Seric 		}
4110084e6f6Seric 		return socksize;
4126173568dSeric 	} while (ntries++ < MAXOPENTRIES && transienterror(saveerrno));
4138b212fe0Seric 	syserr("!opendaemonsocket: server SMTP socket wedged: exiting");
4146173568dSeric 	finis();
4150084e6f6Seric }
4160084e6f6Seric /*
417b7d7afcbSeric **  CLRDAEMON -- reset the daemon connection
418b7d7afcbSeric **
419b7d7afcbSeric **	Parameters:
420b7d7afcbSeric **		none.
421b7d7afcbSeric **
422b7d7afcbSeric **	Returns:
423b7d7afcbSeric **		none.
424b7d7afcbSeric **
425b7d7afcbSeric **	Side Effects:
426b7d7afcbSeric **		releases any resources used by the passive daemon.
427b7d7afcbSeric */
428b7d7afcbSeric 
4298b212fe0Seric void
clrdaemon()430b7d7afcbSeric clrdaemon()
431b7d7afcbSeric {
432b7d7afcbSeric 	if (DaemonSocket >= 0)
433b7d7afcbSeric 		(void) close(DaemonSocket);
434b7d7afcbSeric 	DaemonSocket = -1;
435b7d7afcbSeric }
436b7d7afcbSeric /*
437bfb80540Seric **  SETDAEMONOPTIONS -- set options for running the daemon
438bfb80540Seric **
439bfb80540Seric **	Parameters:
440bfb80540Seric **		p -- the options line.
441bfb80540Seric **
442bfb80540Seric **	Returns:
443bfb80540Seric **		none.
444bfb80540Seric */
445bfb80540Seric 
4468b212fe0Seric void
setdaemonoptions(p)447bfb80540Seric setdaemonoptions(p)
448bfb80540Seric 	register char *p;
449bfb80540Seric {
450850144caSeric 	if (DaemonAddr.sa.sa_family == AF_UNSPEC)
451850144caSeric 		DaemonAddr.sa.sa_family = AF_INET;
452850144caSeric 
453bfb80540Seric 	while (p != NULL)
454bfb80540Seric 	{
455bfb80540Seric 		register char *f;
456bfb80540Seric 		register char *v;
457bfb80540Seric 
458bfb80540Seric 		while (isascii(*p) && isspace(*p))
459bfb80540Seric 			p++;
460bfb80540Seric 		if (*p == '\0')
461bfb80540Seric 			break;
462bfb80540Seric 		f = p;
463bfb80540Seric 		p = strchr(p, ',');
464bfb80540Seric 		if (p != NULL)
465bfb80540Seric 			*p++ = '\0';
466bfb80540Seric 		v = strchr(f, '=');
467bfb80540Seric 		if (v == NULL)
468bfb80540Seric 			continue;
469bfb80540Seric 		while (isascii(*++v) && isspace(*v))
470bfb80540Seric 			continue;
471e7988623Seric 		if (isascii(*f) && isupper(*f))
472e7988623Seric 			*f = tolower(*f);
473bfb80540Seric 
474bfb80540Seric 		switch (*f)
475bfb80540Seric 		{
476850144caSeric 		  case 'F':		/* address family */
477850144caSeric 			if (isascii(*v) && isdigit(*v))
478850144caSeric 				DaemonAddr.sa.sa_family = atoi(v);
47986aab53cSeric #if NETINET
480850144caSeric 			else if (strcasecmp(v, "inet") == 0)
481850144caSeric 				DaemonAddr.sa.sa_family = AF_INET;
482850144caSeric #endif
48386aab53cSeric #if NETISO
484850144caSeric 			else if (strcasecmp(v, "iso") == 0)
485850144caSeric 				DaemonAddr.sa.sa_family = AF_ISO;
486850144caSeric #endif
48786aab53cSeric #if NETNS
488850144caSeric 			else if (strcasecmp(v, "ns") == 0)
489850144caSeric 				DaemonAddr.sa.sa_family = AF_NS;
490850144caSeric #endif
49186aab53cSeric #if NETX25
492850144caSeric 			else if (strcasecmp(v, "x.25") == 0)
493850144caSeric 				DaemonAddr.sa.sa_family = AF_CCITT;
494850144caSeric #endif
495850144caSeric 			else
496850144caSeric 				syserr("554 Unknown address family %s in Family=option", v);
497850144caSeric 			break;
498850144caSeric 
499850144caSeric 		  case 'A':		/* address */
500850144caSeric 			switch (DaemonAddr.sa.sa_family)
501850144caSeric 			{
50286aab53cSeric #if NETINET
503850144caSeric 			  case AF_INET:
504850144caSeric 				if (isascii(*v) && isdigit(*v))
5058b212fe0Seric 					DaemonAddr.sin.sin_addr.s_addr = htonl(inet_network(v));
506850144caSeric 				else
507850144caSeric 				{
508850144caSeric 					register struct netent *np;
509850144caSeric 
510850144caSeric 					np = getnetbyname(v);
511850144caSeric 					if (np == NULL)
512850144caSeric 						syserr("554 network \"%s\" unknown", v);
513850144caSeric 					else
514850144caSeric 						DaemonAddr.sin.sin_addr.s_addr = np->n_net;
515850144caSeric 				}
516850144caSeric 				break;
517850144caSeric #endif
518850144caSeric 
519850144caSeric 			  default:
520850144caSeric 				syserr("554 Address= option unsupported for family %d",
521850144caSeric 					DaemonAddr.sa.sa_family);
522850144caSeric 				break;
523850144caSeric 			}
524850144caSeric 			break;
525850144caSeric 
526bfb80540Seric 		  case 'P':		/* port */
527850144caSeric 			switch (DaemonAddr.sa.sa_family)
528850144caSeric 			{
529850144caSeric 				short port;
530850144caSeric 
53186aab53cSeric #if NETINET
532850144caSeric 			  case AF_INET:
533bfb80540Seric 				if (isascii(*v) && isdigit(*v))
53476b70c58Seric 					DaemonAddr.sin.sin_port = htons(atoi(v));
535bfb80540Seric 				else
536bfb80540Seric 				{
537bfb80540Seric 					register struct servent *sp;
538bfb80540Seric 
539bfb80540Seric 					sp = getservbyname(v, "tcp");
540bfb80540Seric 					if (sp == NULL)
541ad977999Seric 						syserr("554 service \"%s\" unknown", v);
542bfb80540Seric 					else
543bfb80540Seric 						DaemonAddr.sin.sin_port = sp->s_port;
544bfb80540Seric 				}
545bfb80540Seric 				break;
546850144caSeric #endif
547bfb80540Seric 
54886aab53cSeric #if NETISO
549850144caSeric 			  case AF_ISO:
550850144caSeric 				/* assume two byte transport selector */
551bfb80540Seric 				if (isascii(*v) && isdigit(*v))
55276b70c58Seric 					port = htons(atoi(v));
553bfb80540Seric 				else
554bfb80540Seric 				{
555850144caSeric 					register struct servent *sp;
556bfb80540Seric 
557850144caSeric 					sp = getservbyname(v, "tcp");
558850144caSeric 					if (sp == NULL)
559ad977999Seric 						syserr("554 service \"%s\" unknown", v);
560bfb80540Seric 					else
561850144caSeric 						port = sp->s_port;
562850144caSeric 				}
563850144caSeric 				bcopy((char *) &port, TSEL(&DaemonAddr.siso), 2);
564850144caSeric 				break;
565850144caSeric #endif
566850144caSeric 
567850144caSeric 			  default:
568850144caSeric 				syserr("554 Port= option unsupported for family %d",
569850144caSeric 					DaemonAddr.sa.sa_family);
570850144caSeric 				break;
571bfb80540Seric 			}
572bfb80540Seric 			break;
573bfc1eaf8Seric 
574bfc1eaf8Seric 		  case 'L':		/* listen queue size */
575bfc1eaf8Seric 			ListenQueueSize = atoi(v);
576bfc1eaf8Seric 			break;
577b35447dbSeric 
578b35447dbSeric 		  case 'S':		/* send buffer size */
579b35447dbSeric 			TcpSndBufferSize = atoi(v);
580b35447dbSeric 			break;
581b35447dbSeric 
582b35447dbSeric 		  case 'R':		/* receive buffer size */
583b35447dbSeric 			TcpRcvBufferSize = atoi(v);
584b35447dbSeric 			break;
585e7988623Seric 
586e7988623Seric 		  default:
587e7988623Seric 			syserr("554 DaemonPortOptions parameter \"%s\" unknown", f);
588bfb80540Seric 		}
589bfb80540Seric 	}
590bfb80540Seric }
591bfb80540Seric /*
5927aa493c5Seric **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
5937aa493c5Seric **
5947aa493c5Seric **	Parameters:
5957aa493c5Seric **		host -- the name of the host.
59648ff0a9dSeric **		port -- the port number to connect to.
597655feedbSeric **		mci -- a pointer to the mail connection information
598655feedbSeric **			structure to be filled in.
599914346b1Seric **		usesecureport -- if set, use a low numbered (reserved)
600914346b1Seric **			port to provide some rudimentary authentication.
6017aa493c5Seric **
6027aa493c5Seric **	Returns:
6037aa493c5Seric **		An exit code telling whether the connection could be
6047aa493c5Seric **			made and if not why not.
6057aa493c5Seric **
6067aa493c5Seric **	Side Effects:
6077aa493c5Seric **		none.
6087aa493c5Seric */
6097aa493c5Seric 
610e2f2f828Seric SOCKADDR	CurHostAddr;		/* address of current host */
61171ff6caaSeric 
612b31e7f2bSeric int
makeconnection(host,port,mci,usesecureport)613655feedbSeric makeconnection(host, port, mci, usesecureport)
6147aa493c5Seric 	char *host;
615210215eaSeric 	u_short port;
616b31e7f2bSeric 	register MCI *mci;
617914346b1Seric 	bool usesecureport;
6187aa493c5Seric {
6198b212fe0Seric 	register int i = 0;
6208b212fe0Seric 	register int s;
62104344589Sbloom 	register struct hostent *hp = (struct hostent *)NULL;
622e2f2f828Seric 	SOCKADDR addr;
6236286bb75Sbloom 	int sav_errno;
624e2f2f828Seric 	int addrlen;
6258b212fe0Seric 	bool firstconnect;
6267aa493c5Seric 
6277aa493c5Seric 	/*
6287aa493c5Seric 	**  Set up the address for the mailer.
62971096d12Seric 	**	Accept "[a.b.c.d]" syntax for host name.
6307aa493c5Seric 	*/
6317aa493c5Seric 
6329d4a8008Seric #if NAMED_BIND
633794bdbb9Smiriam 	h_errno = 0;
634134746fbSeric #endif
635794bdbb9Smiriam 	errno = 0;
636967778e2Seric 	bzero(&CurHostAddr, sizeof CurHostAddr);
637c931b82bSeric 	SmtpPhase = mci->mci_phase = "initial connection";
638d945ebe8Seric 	CurHostName = host;
639794bdbb9Smiriam 
64071096d12Seric 	if (host[0] == '[')
64171096d12Seric 	{
642a44d5a5eSeric 		long hid;
6436c2c3107Seric 		register char *p = strchr(host, ']');
64471096d12Seric 
645a44d5a5eSeric 		if (p != NULL)
64671096d12Seric 		{
647a44d5a5eSeric 			*p = '\0';
64886aab53cSeric #if NETINET
649a44d5a5eSeric 			hid = inet_addr(&host[1]);
650a7e21fe6Seric 			if (hid == -1)
6514d9c42c2Seric #endif
652a7e21fe6Seric 			{
653a7e21fe6Seric 				/* try it as a host name (avoid MX lookup) */
6548b212fe0Seric 				hp = sm_gethostbyname(&host[1]);
655d8984352Seric 				if (hp == NULL && p[-1] == '.')
656d8984352Seric 				{
6578b212fe0Seric #if NAMED_BIND
6588b212fe0Seric 					int oldopts = _res.options;
6598b212fe0Seric 
6608b212fe0Seric 					_res.options &= ~(RES_DEFNAMES|RES_DNSRCH);
6618b212fe0Seric #endif
662d8984352Seric 					p[-1] = '\0';
6638b212fe0Seric 					hp = sm_gethostbyname(&host[1]);
664d8984352Seric 					p[-1] = '.';
6658b212fe0Seric #if NAMED_BIND
6668b212fe0Seric 					_res.options = oldopts;
6678b212fe0Seric #endif
668d8984352Seric 				}
669a7e21fe6Seric 				*p = ']';
670a7e21fe6Seric 				goto gothostent;
671a7e21fe6Seric 			}
672a44d5a5eSeric 			*p = ']';
67371096d12Seric 		}
674a7e21fe6Seric 		if (p == NULL)
67571096d12Seric 		{
67608b25121Seric 			usrerr("553 Invalid numeric domain spec \"%s\"", host);
6775b068d51Seric 			mci->mci_status = "5.1.2";
67871096d12Seric 			return (EX_NOHOST);
67971096d12Seric 		}
68086aab53cSeric #if NETINET
6814d9c42c2Seric 		addr.sin.sin_family = AF_INET;		/*XXX*/
68283c1f4bcSeric 		addr.sin.sin_addr.s_addr = hid;
6834d9c42c2Seric #endif
68471096d12Seric 	}
6851c71e510Seric 	else
6861c71e510Seric 	{
687d8984352Seric 		register char *p = &host[strlen(host) - 1];
688d8984352Seric 
6898b212fe0Seric 		hp = sm_gethostbyname(host);
690d8984352Seric 		if (hp == NULL && *p == '.')
691d8984352Seric 		{
6928b212fe0Seric #if NAMED_BIND
6938b212fe0Seric 			int oldopts = _res.options;
6948b212fe0Seric 
6958b212fe0Seric 			_res.options &= ~(RES_DEFNAMES|RES_DNSRCH);
6968b212fe0Seric #endif
697d8984352Seric 			*p = '\0';
6988b212fe0Seric 			hp = sm_gethostbyname(host);
699d8984352Seric 			*p = '.';
7008b212fe0Seric #if NAMED_BIND
7018b212fe0Seric 			_res.options = oldopts;
7028b212fe0Seric #endif
703d8984352Seric 		}
704a7e21fe6Seric gothostent:
705794bdbb9Smiriam 		if (hp == NULL)
706794bdbb9Smiriam 		{
7079d4a8008Seric #if NAMED_BIND
7088b212fe0Seric 			/* check for name server timeouts */
7098b212fe0Seric 			if (errno == ETIMEDOUT || h_errno == TRY_AGAIN ||
7108b212fe0Seric 			    (errno == ECONNREFUSED && UseNameServer))
7118b212fe0Seric 			{
7128b212fe0Seric 				mci->mci_status = "4.4.3";
71352308a50Seric 				return (EX_TEMPFAIL);
7148b212fe0Seric 			}
715134746fbSeric #endif
7167aa493c5Seric 			return (EX_NOHOST);
717794bdbb9Smiriam 		}
71883c1f4bcSeric 		addr.sa.sa_family = hp->h_addrtype;
71983c1f4bcSeric 		switch (hp->h_addrtype)
72083c1f4bcSeric 		{
72186aab53cSeric #if NETINET
72283c1f4bcSeric 		  case AF_INET:
723e2f2f828Seric 			bcopy(hp->h_addr,
72483c1f4bcSeric 				&addr.sin.sin_addr,
7258b212fe0Seric 				INADDRSZ);
72683c1f4bcSeric 			break;
72783c1f4bcSeric #endif
72883c1f4bcSeric 
72983c1f4bcSeric 		  default:
730e2f2f828Seric 			bcopy(hp->h_addr,
73183c1f4bcSeric 				addr.sa.sa_data,
732e2f2f828Seric 				hp->h_length);
73383c1f4bcSeric 			break;
73483c1f4bcSeric 		}
73504344589Sbloom 		i = 1;
7361c71e510Seric 	}
7371c71e510Seric 
7381c71e510Seric 	/*
7391c71e510Seric 	**  Determine the port number.
7401c71e510Seric 	*/
7411c71e510Seric 
7420784fccbSeric 	if (port == 0)
7431c71e510Seric 	{
7441c71e510Seric 		register struct servent *sp = getservbyname("smtp", "tcp");
7451c71e510Seric 
7461c71e510Seric 		if (sp == NULL)
7471c71e510Seric 		{
7483edb2e00Seric #ifdef LOG
7493edb2e00Seric 			if (LogLevel > 2)
7503edb2e00Seric 				syslog(LOG_ERR, "makeconnection: service \"smtp\" unknown");
7513edb2e00Seric #endif
752e5311662Seric 			port = htons(25);
7531c71e510Seric 		}
754e5311662Seric 		else
755e2f2f828Seric 			port = sp->s_port;
756e2f2f828Seric 	}
757e2f2f828Seric 
75883c1f4bcSeric 	switch (addr.sa.sa_family)
759e2f2f828Seric 	{
76086aab53cSeric #if NETINET
761e2f2f828Seric 	  case AF_INET:
76283c1f4bcSeric 		addr.sin.sin_port = port;
763e2f2f828Seric 		addrlen = sizeof (struct sockaddr_in);
764e2f2f828Seric 		break;
7654d9c42c2Seric #endif
766e2f2f828Seric 
76786aab53cSeric #if NETISO
768e2f2f828Seric 	  case AF_ISO:
769e2f2f828Seric 		/* assume two byte transport selector */
770e2f2f828Seric 		bcopy((char *) &port, TSEL((struct sockaddr_iso *) &addr), 2);
771e2f2f828Seric 		addrlen = sizeof (struct sockaddr_iso);
772e2f2f828Seric 		break;
773e2f2f828Seric #endif
774e2f2f828Seric 
775e2f2f828Seric 	  default:
77683c1f4bcSeric 		syserr("Can't connect to address family %d", addr.sa.sa_family);
777e2f2f828Seric 		return (EX_NOHOST);
7781c71e510Seric 	}
7797aa493c5Seric 
7807aa493c5Seric 	/*
7817aa493c5Seric 	**  Try to actually open the connection.
7827aa493c5Seric 	*/
7837aa493c5Seric 
784e17a3a5aSeric #ifdef XLA
785e17a3a5aSeric 	/* if too many connections, don't bother trying */
786e17a3a5aSeric 	if (!xla_noqueue_ok(host))
787e17a3a5aSeric 		return EX_TEMPFAIL;
788e17a3a5aSeric #endif
789e17a3a5aSeric 
7908b212fe0Seric 	firstconnect = TRUE;
791aea02ca1Seric 	for (;;)
792aea02ca1Seric 	{
79361e4310fSeric 		if (tTd(16, 1))
794e2f2f828Seric 			printf("makeconnection (%s [%s])\n",
795e2f2f828Seric 				host, anynet_ntoa(&addr));
7967aa493c5Seric 
797226e3022Seric 		/* save for logging */
798226e3022Seric 		CurHostAddr = addr;
799226e3022Seric 
800914346b1Seric 		if (usesecureport)
801914346b1Seric 		{
802914346b1Seric 			int rport = IPPORT_RESERVED - 1;
803914346b1Seric 
804914346b1Seric 			s = rresvport(&rport);
805914346b1Seric 		}
806914346b1Seric 		else
807914346b1Seric 		{
808af5e902cSeric 			s = socket(AF_INET, SOCK_STREAM, 0);
809914346b1Seric 		}
8107aa493c5Seric 		if (s < 0)
8117aa493c5Seric 		{
8126286bb75Sbloom 			sav_errno = errno;
813d1b5947aSeric 			syserr("makeconnection: cannot create socket");
8147aa493c5Seric 			goto failure;
8157aa493c5Seric 		}
8167aa493c5Seric 
817b35447dbSeric #ifdef SO_SNDBUF
818b35447dbSeric 		if (TcpSndBufferSize > 0)
819b35447dbSeric 		{
820b35447dbSeric 			if (setsockopt(s, SOL_SOCKET, SO_SNDBUF,
821bf217a95Seric 				       (char *) &TcpSndBufferSize,
822b35447dbSeric 				       sizeof(TcpSndBufferSize)) < 0)
823b35447dbSeric 				syserr("makeconnection: setsockopt(SO_SNDBUF)");
824b35447dbSeric 		}
825b35447dbSeric #endif
826b35447dbSeric 
82761e4310fSeric 		if (tTd(16, 1))
828b31e7f2bSeric 			printf("makeconnection: fd=%d\n", s);
8291b6e4a15Seric 
8301b6e4a15Seric 		/* turn on network debugging? */
831a2ef5fa4Seric 		if (tTd(16, 101))
83252308a50Seric 		{
83352308a50Seric 			int on = 1;
8346173568dSeric 			(void) setsockopt(s, SOL_SOCKET, SO_DEBUG,
835aea02ca1Seric 					  (char *)&on, sizeof on);
83652308a50Seric 		}
83787d6e633Srick 		if (CurEnv->e_xfp != NULL)
838877a6142Seric 			(void) fflush(CurEnv->e_xfp);		/* for debugging */
8394bd6a662Seric 		errno = 0;					/* for debugging */
840e2f2f828Seric 		if (connect(s, (struct sockaddr *) &addr, addrlen) >= 0)
841aea02ca1Seric 			break;
842aea02ca1Seric 
8438b212fe0Seric 		/* if running demand-dialed connection, try again */
8448b212fe0Seric 		if (DialDelay > 0 && firstconnect)
8458b212fe0Seric 		{
8468b212fe0Seric 			if (tTd(16, 1))
8478b212fe0Seric 				printf("Connect failed (%s); trying again...\n",
8488b212fe0Seric 					errstring(sav_errno));
8498b212fe0Seric 			firstconnect = FALSE;
8508b212fe0Seric 			sleep(DialDelay);
8518b212fe0Seric 			continue;
8528b212fe0Seric 		}
8538b212fe0Seric 
854aea02ca1Seric 		/* couldn't connect.... figure out why */
8556286bb75Sbloom 		sav_errno = errno;
8566286bb75Sbloom 		(void) close(s);
8578b212fe0Seric 		if (hp != NULL && hp->h_addr_list[i])
85804344589Sbloom 		{
859aea02ca1Seric 			if (tTd(16, 1))
860e2f2f828Seric 				printf("Connect failed (%s); trying new address....\n",
861e2f2f828Seric 					errstring(sav_errno));
86283c1f4bcSeric 			switch (addr.sa.sa_family)
86383c1f4bcSeric 			{
86486aab53cSeric #if NETINET
86583c1f4bcSeric 			  case AF_INET:
866e2f2f828Seric 				bcopy(hp->h_addr_list[i++],
86783c1f4bcSeric 				      &addr.sin.sin_addr,
8688b212fe0Seric 				      INADDRSZ);
86983c1f4bcSeric 				break;
87083c1f4bcSeric #endif
87183c1f4bcSeric 
87283c1f4bcSeric 			  default:
873e2f2f828Seric 				bcopy(hp->h_addr_list[i++],
87483c1f4bcSeric 					addr.sa.sa_data,
875914346b1Seric 					hp->h_length);
87683c1f4bcSeric 				break;
87783c1f4bcSeric 			}
878aea02ca1Seric 			continue;
87904344589Sbloom 		}
88004344589Sbloom 
8817aa493c5Seric 		/* failure, decide if temporary or not */
8827aa493c5Seric 	failure:
883244b09d1Seric #ifdef XLA
884244b09d1Seric 		xla_host_end(host);
885244b09d1Seric #endif
886e2de2524Seric 		if (transienterror(sav_errno))
887e2de2524Seric 			return EX_TEMPFAIL;
888e2de2524Seric 		else
88987d6e633Srick 		{
89008b25121Seric 			message("%s", errstring(sav_errno));
8917aa493c5Seric 			return (EX_UNAVAILABLE);
8927aa493c5Seric 		}
8937aa493c5Seric 	}
8947aa493c5Seric 
8957aa493c5Seric 	/* connection ok, put it into canonical form */
896335eae58Seric 	if ((mci->mci_out = fdopen(s, "w")) == NULL ||
897335eae58Seric 	    (s = dup(s)) < 0 ||
898ab81ee53Seric 	    (mci->mci_in = fdopen(s, "r")) == NULL)
899335eae58Seric 	{
900335eae58Seric 		syserr("cannot open SMTP client channel, fd=%d", s);
901335eae58Seric 		return EX_TEMPFAIL;
902335eae58Seric 	}
9037aa493c5Seric 
904dca8e1f7Seric 	return (EX_OK);
9057aa493c5Seric }
906444eaf03Seric /*
907444eaf03Seric **  MYHOSTNAME -- return the name of this host.
908444eaf03Seric **
909444eaf03Seric **	Parameters:
910444eaf03Seric **		hostbuf -- a place to return the name of this host.
911897f1869Seric **		size -- the size of hostbuf.
912444eaf03Seric **
913444eaf03Seric **	Returns:
914444eaf03Seric **		A list of aliases for this host.
915444eaf03Seric **
916444eaf03Seric **	Side Effects:
917d8d0a4aeSeric **		Adds numeric codes to $=w.
918444eaf03Seric */
919444eaf03Seric 
9208b212fe0Seric struct hostent *
myhostname(hostbuf,size)921897f1869Seric myhostname(hostbuf, size)
922444eaf03Seric 	char hostbuf[];
923897f1869Seric 	int size;
924444eaf03Seric {
92538ad259dSeric 	register struct hostent *hp;
9268b212fe0Seric 	extern bool getcanonname();
927444eaf03Seric 
928af5e902cSeric 	if (gethostname(hostbuf, size) < 0)
929af5e902cSeric 	{
930af5e902cSeric 		(void) strcpy(hostbuf, "localhost");
931af5e902cSeric 	}
9328b212fe0Seric 	hp = sm_gethostbyname(hostbuf);
9334a5c6430Seric 	if (hp == NULL)
9348b212fe0Seric 		return NULL;
9358b212fe0Seric 	if (strchr(hp->h_name, '.') != NULL || strchr(hostbuf, '.') == NULL)
936423161fbSeric 	{
93735852b23Seric 		(void) strncpy(hostbuf, hp->h_name, size - 1);
93835852b23Seric 		hostbuf[size - 1] = '\0';
9398b212fe0Seric 	}
9404a5c6430Seric 
9418b212fe0Seric 	/*
942d4ebb9abSeric 	**  If there is still no dot in the name, try looking for a
943d4ebb9abSeric 	**  dotted alias.
9448b212fe0Seric 	*/
94523668637Seric 
9468868a898Seric 	if (strchr(hostbuf, '.') == NULL)
9478868a898Seric 	{
948d4ebb9abSeric 		char **ha;
9498868a898Seric 
950d4ebb9abSeric 		for (ha = hp->h_aliases; *ha != NULL; ha++)
95123668637Seric 		{
952d4ebb9abSeric 			if (strchr(*ha, '.') != NULL)
953d4ebb9abSeric 			{
954d4ebb9abSeric 				(void) strncpy(hostbuf, *ha, size - 1);
955d4ebb9abSeric 				hostbuf[size - 1] = '\0';
956d4ebb9abSeric 				break;
957d4ebb9abSeric 			}
958d4ebb9abSeric 		}
959d4ebb9abSeric 	}
960d4ebb9abSeric 
961d4ebb9abSeric 	/*
962d4ebb9abSeric 	**  If _still_ no dot, wait for a while and try again -- it is
963d4ebb9abSeric 	**  possible that some service is starting up.  This can result
964d4ebb9abSeric 	**  in excessive delays if the system is badly configured, but
965d4ebb9abSeric 	**  there really isn't a way around that, particularly given that
966d4ebb9abSeric 	**  the config file hasn't been read at this point.
967d4ebb9abSeric 	**  All in all, a bit of a mess.
968d4ebb9abSeric 	*/
969d4ebb9abSeric 
970d4ebb9abSeric 	if (strchr(hostbuf, '.') == NULL &&
971d4ebb9abSeric 	    !getcanonname(hostbuf, size, TRUE))
972d4ebb9abSeric 	{
973d4ebb9abSeric 		message("My unqualifed host name (%s) unknown; sleeping for retry",
97423668637Seric 			hostbuf);
9758b212fe0Seric 		sleep(60);
976d4ebb9abSeric 		(void) getcanonname(hostbuf, size, TRUE);
977747df804Seric 	}
9788b212fe0Seric 	return (hp);
9797364df9fSeric }
980cb452edcSeric /*
9819f8b0eadSeric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
9829f8b0eadSeric **
9839f8b0eadSeric **	Uses RFC1413 protocol to try to get info from the other end.
984320e0d1cSeric **
985320e0d1cSeric **	Parameters:
986320e0d1cSeric **		fd -- the descriptor
987320e0d1cSeric **
988320e0d1cSeric **	Returns:
9899f8b0eadSeric **		The user@host information associated with this descriptor.
990320e0d1cSeric */
991320e0d1cSeric 
9929f8b0eadSeric static jmp_buf	CtxAuthTimeout;
9939f8b0eadSeric 
9948b212fe0Seric static void
authtimeout()9959f8b0eadSeric authtimeout()
9969f8b0eadSeric {
9979f8b0eadSeric 	longjmp(CtxAuthTimeout, 1);
9989f8b0eadSeric }
9999f8b0eadSeric 
1000320e0d1cSeric char *
getauthinfo(fd)10019f8b0eadSeric getauthinfo(fd)
1002320e0d1cSeric 	int fd;
1003320e0d1cSeric {
10049f8b0eadSeric 	int falen;
1005a5546e24Seric 	register char *p;
10069f8b0eadSeric 	SOCKADDR la;
10079f8b0eadSeric 	int lalen;
10089f8b0eadSeric 	register struct servent *sp;
10099f8b0eadSeric 	int s;
10109f8b0eadSeric 	int i;
10119f8b0eadSeric 	EVENT *ev;
1012579270a3Seric 	int nleft;
1013f036c8d7Seric 	char ibuf[MAXNAME + 1];
10149f8b0eadSeric 	static char hbuf[MAXNAME * 2 + 2];
10159f8b0eadSeric 	extern char *hostnamebyanyaddr();
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;
11914d6b8580Seric 		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;
12024d6b8580Seric 		o = (u_char *) ipopt.ipopt_list;
12035db00695Seric 		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));
12314d6b8580Seric 					sprintf(p, "%c%s",
123211af795cSeric 						     j ? '@' : ':',
123311af795cSeric 						     inet_ntoa(addr));
12344d6b8580Seric 					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 *
host_map_lookup(map,name,av,statp)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];
12965f78836eSmiriam 
1297f36ede03Sbostic 	/*
1298eea91d78Seric 	**  See if we have already looked up this name.  If so, just
1299eea91d78Seric 	**  return it.
1300eea91d78Seric 	*/
1301eea91d78Seric 
130208de856eSeric 	s = stab(name, ST_NAMECANON, ST_ENTER);
1303eea91d78Seric 	if (bitset(NCF_VALID, s->s_namecanon.nc_flags))
1304eea91d78Seric 	{
1305f92c3297Seric 		if (tTd(9, 1))
130608de856eSeric 			printf("host_map_lookup(%s) => CACHE %s\n",
130798826a83Seric 			       name,
130898826a83Seric 			       s->s_namecanon.nc_cname == NULL
130998826a83Seric 					? "NULL"
13105f08d413Seric 					: s->s_namecanon.nc_cname);
1311eea91d78Seric 		errno = s->s_namecanon.nc_errno;
13129d4a8008Seric #if NAMED_BIND
1313eea91d78Seric 		h_errno = s->s_namecanon.nc_herrno;
1314c304a798Seric #endif
1315eea91d78Seric 		*statp = s->s_namecanon.nc_stat;
1316fcfb36d9Seric 		if (*statp == EX_TEMPFAIL)
1317ed63aae0Seric 		{
13185b068d51Seric 			CurEnv->e_status = "4.4.3";
1319fcfb36d9Seric 			usrerr("451 %s: Name server timeout",
1320ed63aae0Seric 				shortenstring(name, 33));
1321ed63aae0Seric 		}
1322eea91d78Seric 		return s->s_namecanon.nc_cname;
1323eea91d78Seric 	}
1324eea91d78Seric 
1325eea91d78Seric 	/*
1326eea91d78Seric 	**  If first character is a bracket, then it is an address
1327eea91d78Seric 	**  lookup.  Address is copied into a temporary buffer to
132808de856eSeric 	**  strip the brackets and to preserve name if address is
1329eea91d78Seric 	**  unknown.
1330f36ede03Sbostic 	*/
133115d084d5Seric 
133208de856eSeric 	if (*name != '[')
133315d084d5Seric 	{
1334d798a1deSeric 		extern bool getcanonname();
1335d798a1deSeric 
13368cb4653dSeric 		if (tTd(9, 1))
133708de856eSeric 			printf("host_map_lookup(%s) => ", name);
1338eea91d78Seric 		s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
13398b212fe0Seric 		if (strlen(name) < sizeof hbuf)
134008de856eSeric 			(void) strcpy(hbuf, name);
13418b212fe0Seric 		else
13428b212fe0Seric 		{
13438b212fe0Seric 			bcopy(name, hbuf, sizeof hbuf - 1);
13448b212fe0Seric 			hbuf[sizeof hbuf - 1] = '\0';
13458b212fe0Seric 		}
13462387cae6Seric 		if (getcanonname(hbuf, sizeof hbuf - 1, !HasWildcardMX))
13479040ec4fSeric 		{
13489040ec4fSeric 			if (tTd(9, 1))
13499040ec4fSeric 				printf("%s\n", hbuf);
135000b385a9Seric 			cp = map_rewrite(map, hbuf, strlen(hbuf), av);
135100b385a9Seric 			s->s_namecanon.nc_cname = newstr(cp);
135200b385a9Seric 			return cp;
13539040ec4fSeric 		}
135415d084d5Seric 		else
13559040ec4fSeric 		{
13562d29d43aSeric 			register struct hostent *hp;
13572d29d43aSeric 
1358c304a798Seric 			s->s_namecanon.nc_errno = errno;
13599d4a8008Seric #if NAMED_BIND
1360c304a798Seric 			s->s_namecanon.nc_herrno = h_errno;
13619040ec4fSeric 			if (tTd(9, 1))
13622d29d43aSeric 				printf("FAIL (%d)\n", h_errno);
13632d29d43aSeric 			switch (h_errno)
13642d29d43aSeric 			{
13652d29d43aSeric 			  case TRY_AGAIN:
136689cb2793Seric 				if (UseNameServer)
13678820d51bSeric 				{
13685b068d51Seric 					CurEnv->e_status = "4.4.3";
1369fcfb36d9Seric 					usrerr("451 %s: Name server timeout",
1370ed63aae0Seric 						shortenstring(name, 33));
13718820d51bSeric 				}
13722d29d43aSeric 				*statp = EX_TEMPFAIL;
13732d29d43aSeric 				break;
13742d29d43aSeric 
13752d29d43aSeric 			  case HOST_NOT_FOUND:
137680027ae6Seric 			  case NO_DATA:
13772d29d43aSeric 				*statp = EX_NOHOST;
13782d29d43aSeric 				break;
13792d29d43aSeric 
13802d29d43aSeric 			  case NO_RECOVERY:
13812d29d43aSeric 				*statp = EX_SOFTWARE;
13822d29d43aSeric 				break;
13832d29d43aSeric 
13842d29d43aSeric 			  default:
13852d29d43aSeric 				*statp = EX_UNAVAILABLE;
13862d29d43aSeric 				break;
13872d29d43aSeric 			}
1388c304a798Seric #else
1389c304a798Seric 			if (tTd(9, 1))
1390c304a798Seric 				printf("FAIL\n");
1391c304a798Seric 			*statp = EX_NOHOST;
1392c304a798Seric #endif
1393eea91d78Seric 			s->s_namecanon.nc_stat = *statp;
13948b212fe0Seric 			if ((*statp != EX_TEMPFAIL && *statp != EX_NOHOST) ||
13958b212fe0Seric 			    UseNameServer)
139615d084d5Seric 				return NULL;
13972d29d43aSeric 
13982d29d43aSeric 			/*
13992d29d43aSeric 			**  Try to look it up in /etc/hosts
14002d29d43aSeric 			*/
14012d29d43aSeric 
14028b212fe0Seric 			hp = sm_gethostbyname(name);
14032d29d43aSeric 			if (hp == NULL)
14042d29d43aSeric 			{
14052d29d43aSeric 				/* no dice there either */
1406eea91d78Seric 				s->s_namecanon.nc_stat = *statp = EX_NOHOST;
14072d29d43aSeric 				return NULL;
14082d29d43aSeric 			}
14092d29d43aSeric 
1410eea91d78Seric 			s->s_namecanon.nc_stat = *statp = EX_OK;
141100b385a9Seric 			cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
141200b385a9Seric 			s->s_namecanon.nc_cname = newstr(cp);
141300b385a9Seric 			return cp;
141415d084d5Seric 		}
14159040ec4fSeric 	}
141608de856eSeric 	if ((cp = strchr(name, ']')) == NULL)
141715d084d5Seric 		return (NULL);
141834e39927Sbostic 	*cp = '\0';
14198b212fe0Seric 	in_addr.s_addr = inet_addr(&name[1]);
142038ad259dSeric 
142138ad259dSeric 	/* nope -- ask the name server */
14228b212fe0Seric 	hp = sm_gethostbyaddr((char *)&in_addr, INADDRSZ, AF_INET);
1423eea91d78Seric 	s->s_namecanon.nc_errno = errno;
14249d4a8008Seric #if NAMED_BIND
1425eea91d78Seric 	s->s_namecanon.nc_herrno = h_errno;
1426c304a798Seric #endif
1427eea91d78Seric 	s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
14285f78836eSmiriam 	if (hp == NULL)
1429eea91d78Seric 	{
1430eea91d78Seric 		s->s_namecanon.nc_stat = *statp = EX_NOHOST;
143115d084d5Seric 		return (NULL);
1432eea91d78Seric 	}
143315d084d5Seric 
143438ad259dSeric 	/* found a match -- copy out */
143500b385a9Seric 	cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
1436eea91d78Seric 	s->s_namecanon.nc_stat = *statp = EX_OK;
143700b385a9Seric 	s->s_namecanon.nc_cname = newstr(cp);
143800b385a9Seric 	return cp;
143999f7cf32Seric }
1440e2f2f828Seric /*
1441e2f2f828Seric **  ANYNET_NTOA -- convert a network address to printable form.
1442e2f2f828Seric **
1443e2f2f828Seric **	Parameters:
1444e2f2f828Seric **		sap -- a pointer to a sockaddr structure.
1445e2f2f828Seric **
1446e2f2f828Seric **	Returns:
1447e2f2f828Seric **		A printable version of that sockaddr.
1448e2f2f828Seric */
1449e2f2f828Seric 
145086aab53cSeric #if NETLINK
145106cc5485Seric # include <net/if_dl.h>
145206cc5485Seric #endif
145306cc5485Seric 
1454e2f2f828Seric char *
anynet_ntoa(sap)1455e2f2f828Seric anynet_ntoa(sap)
1456e2f2f828Seric 	register SOCKADDR *sap;
1457e2f2f828Seric {
1458e2f2f828Seric 	register char *bp;
1459e2f2f828Seric 	register char *ap;
1460e2f2f828Seric 	int l;
1461e387851eSeric 	static char buf[100];
1462e2f2f828Seric 
14638cb4653dSeric 	/* check for null/zero family */
14648cb4653dSeric 	if (sap == NULL)
14658cb4653dSeric 		return "NULLADDR";
14668cb4653dSeric 	if (sap->sa.sa_family == 0)
14678cb4653dSeric 		return "0";
14688cb4653dSeric 
1469e387851eSeric 	switch (sap->sa.sa_family)
1470e387851eSeric 	{
147186aab53cSeric #if NETUNIX
1472e387851eSeric 	  case AF_UNIX:
1473c24cf5a4Seric 	  	if (sap->sunix.sun_path[0] != '\0')
1474c24cf5a4Seric 	  		sprintf(buf, "[UNIX: %.64s]", sap->sunix.sun_path);
1475e387851eSeric 	  	else
1476e387851eSeric 	  		sprintf(buf, "[UNIX: localhost]");
1477e387851eSeric 		return buf;
1478e387851eSeric #endif
1479e387851eSeric 
148086aab53cSeric #if NETINET
1481e387851eSeric 	  case AF_INET:
1482482e28bcSeric 		return inet_ntoa(sap->sin.sin_addr);
148383c1f4bcSeric #endif
1484e2f2f828Seric 
148586aab53cSeric #if NETLINK
148606cc5485Seric 	  case AF_LINK:
148706cc5485Seric 		sprintf(buf, "[LINK: %s]",
148806cc5485Seric 			link_ntoa((struct sockaddr_dl *) &sap->sa));
148906cc5485Seric 		return buf;
149006cc5485Seric #endif
1491e387851eSeric 	  default:
149206cc5485Seric 		/* this case is needed when nothing is #defined */
149306cc5485Seric 		/* in order to keep the switch syntactically correct */
1494e387851eSeric 		break;
1495e387851eSeric 	}
1496e387851eSeric 
1497e2f2f828Seric 	/* unknown family -- just dump bytes */
149883c1f4bcSeric 	(void) sprintf(buf, "Family %d: ", sap->sa.sa_family);
1499e2f2f828Seric 	bp = &buf[strlen(buf)];
150083c1f4bcSeric 	ap = sap->sa.sa_data;
150183c1f4bcSeric 	for (l = sizeof sap->sa.sa_data; --l >= 0; )
1502e2f2f828Seric 	{
1503e2f2f828Seric 		(void) sprintf(bp, "%02x:", *ap++ & 0377);
1504e2f2f828Seric 		bp += 3;
1505e2f2f828Seric 	}
1506e2f2f828Seric 	*--bp = '\0';
1507e2f2f828Seric 	return buf;
1508e2f2f828Seric }
15099f8b0eadSeric /*
15109f8b0eadSeric **  HOSTNAMEBYANYADDR -- return name of host based on address
15119f8b0eadSeric **
15129f8b0eadSeric **	Parameters:
15139f8b0eadSeric **		sap -- SOCKADDR pointer
15149f8b0eadSeric **
15159f8b0eadSeric **	Returns:
15169f8b0eadSeric **		text representation of host name.
15179f8b0eadSeric **
15189f8b0eadSeric **	Side Effects:
15199f8b0eadSeric **		none.
15209f8b0eadSeric */
15219f8b0eadSeric 
15229f8b0eadSeric char *
hostnamebyanyaddr(sap)15239f8b0eadSeric hostnamebyanyaddr(sap)
15249f8b0eadSeric 	register SOCKADDR *sap;
15259f8b0eadSeric {
15269f8b0eadSeric 	register struct hostent *hp;
15273490b9dfSeric 	int saveretry;
15283490b9dfSeric 
15299d4a8008Seric #if NAMED_BIND
15303490b9dfSeric 	/* shorten name server timeout to avoid higher level timeouts */
15313490b9dfSeric 	saveretry = _res.retry;
15323490b9dfSeric 	_res.retry = 3;
15333490b9dfSeric #endif /* NAMED_BIND */
15343490b9dfSeric 
15359f8b0eadSeric 	switch (sap->sa.sa_family)
15369f8b0eadSeric 	{
153786aab53cSeric #if NETINET
15389f8b0eadSeric 	  case AF_INET:
15398b212fe0Seric 		hp = sm_gethostbyaddr((char *) &sap->sin.sin_addr,
15408b212fe0Seric 			INADDRSZ,
15419f8b0eadSeric 			AF_INET);
15429f8b0eadSeric 		break;
15439f8b0eadSeric #endif
15449f8b0eadSeric 
154586aab53cSeric #if NETISO
15469f8b0eadSeric 	  case AF_ISO:
15478b212fe0Seric 		hp = sm_gethostbyaddr((char *) &sap->siso.siso_addr,
15489f8b0eadSeric 			sizeof sap->siso.siso_addr,
15499f8b0eadSeric 			AF_ISO);
15509f8b0eadSeric 		break;
15519f8b0eadSeric #endif
15529f8b0eadSeric 
1553e387851eSeric 	  case AF_UNIX:
1554e387851eSeric 		hp = NULL;
1555e387851eSeric 		break;
1556e387851eSeric 
15579f8b0eadSeric 	  default:
15588b212fe0Seric 		hp = sm_gethostbyaddr(sap->sa.sa_data,
15599f8b0eadSeric 			   sizeof sap->sa.sa_data,
15609f8b0eadSeric 			   sap->sa.sa_family);
15619f8b0eadSeric 		break;
15629f8b0eadSeric 	}
15639f8b0eadSeric 
15649d4a8008Seric #if NAMED_BIND
15653490b9dfSeric 	_res.retry = saveretry;
15663490b9dfSeric #endif /* NAMED_BIND */
15673490b9dfSeric 
15689f8b0eadSeric 	if (hp != NULL)
15699f8b0eadSeric 		return hp->h_name;
15709f8b0eadSeric 	else
15719f8b0eadSeric 	{
15729f8b0eadSeric 		/* produce a dotted quad */
15739f8b0eadSeric 		static char buf[512];
15749f8b0eadSeric 
15759f8b0eadSeric 		(void) sprintf(buf, "[%s]", anynet_ntoa(sap));
15769f8b0eadSeric 		return buf;
15779f8b0eadSeric 	}
15789f8b0eadSeric }
1579f36ede03Sbostic 
15806c2c3107Seric # else /* DAEMON */
158199f7cf32Seric /* code for systems without sophisticated networking */
1582444eaf03Seric 
1583444eaf03Seric /*
1584444eaf03Seric **  MYHOSTNAME -- stub version for case of no daemon code.
158521e9914dSeric **
158621e9914dSeric **	Can't convert to upper case here because might be a UUCP name.
1587897f1869Seric **
1588897f1869Seric **	Mark, you can change this to be anything you want......
1589444eaf03Seric */
1590444eaf03Seric 
1591444eaf03Seric char **
myhostname(hostbuf,size)1592897f1869Seric myhostname(hostbuf, size)
1593444eaf03Seric 	char hostbuf[];
1594897f1869Seric 	int size;
1595444eaf03Seric {
1596444eaf03Seric 	register FILE *f;
1597444eaf03Seric 
1598444eaf03Seric 	hostbuf[0] = '\0';
1599444eaf03Seric 	f = fopen("/usr/include/whoami", "r");
1600444eaf03Seric 	if (f != NULL)
1601444eaf03Seric 	{
1602897f1869Seric 		(void) fgets(hostbuf, size, f);
1603444eaf03Seric 		fixcrlf(hostbuf, TRUE);
1604444eaf03Seric 		(void) fclose(f);
1605444eaf03Seric 	}
1606444eaf03Seric 	return (NULL);
1607444eaf03Seric }
160899f7cf32Seric /*
16099f8b0eadSeric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
1610320e0d1cSeric **
1611320e0d1cSeric **	Parameters:
1612320e0d1cSeric **		fd -- the descriptor
1613320e0d1cSeric **
1614320e0d1cSeric **	Returns:
1615320e0d1cSeric **		The host name associated with this descriptor, if it can
1616320e0d1cSeric **			be determined.
1617320e0d1cSeric **		NULL otherwise.
1618320e0d1cSeric **
1619320e0d1cSeric **	Side Effects:
1620320e0d1cSeric **		none
1621320e0d1cSeric */
1622320e0d1cSeric 
1623320e0d1cSeric char *
getauthinfo(fd)16249f8b0eadSeric getauthinfo(fd)
1625320e0d1cSeric 	int fd;
1626320e0d1cSeric {
1627320e0d1cSeric 	return NULL;
1628320e0d1cSeric }
1629320e0d1cSeric /*
163099f7cf32Seric **  MAPHOSTNAME -- turn a hostname into canonical form
163199f7cf32Seric **
163299f7cf32Seric **	Parameters:
163305b57da8Seric **		map -- a pointer to the database map.
163408de856eSeric **		name -- a buffer containing a hostname.
163515d084d5Seric **		avp -- a pointer to a (cf file defined) argument vector.
16362d29d43aSeric **		statp -- an exit status (out parameter).
163799f7cf32Seric **
163899f7cf32Seric **	Returns:
163915d084d5Seric **		mapped host name
1640cb452edcSeric **		FALSE otherwise.
164199f7cf32Seric **
164299f7cf32Seric **	Side Effects:
164308de856eSeric **		Looks up the host specified in name.  If it is not
164499f7cf32Seric **		the canonical name for that host, replace it with
164599f7cf32Seric **		the canonical name.  If the name is unknown, or it
164699f7cf32Seric **		is already the canonical name, leave it unchanged.
164799f7cf32Seric */
164899f7cf32Seric 
164999f7cf32Seric /*ARGSUSED*/
165015d084d5Seric char *
host_map_lookup(map,name,avp,statp)165108de856eSeric host_map_lookup(map, name, avp, statp)
165205b57da8Seric 	MAP *map;
165308de856eSeric 	char *name;
165415d084d5Seric 	char **avp;
16552d29d43aSeric 	char *statp;
165699f7cf32Seric {
16572d29d43aSeric 	register struct hostent *hp;
16582d29d43aSeric 
16598b212fe0Seric 	hp = sm_gethostbyname(name);
16602d29d43aSeric 	if (hp != NULL)
16612d29d43aSeric 		return hp->h_name;
16622d29d43aSeric 	*statp = EX_NOHOST;
166315d084d5Seric 	return NULL;
166499f7cf32Seric }
166599f7cf32Seric 
16666c2c3107Seric #endif /* DAEMON */
1667