xref: /original-bsd/usr.sbin/sendmail/src/daemon.c (revision d4ebb9ab)
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*d4ebb9abSeric static char sccsid[] = "@(#)daemon.c	8.101 (Berkeley) 06/12/95 (with daemon mode)";
15d0a9e852Seric #else
16*d4ebb9abSeric static char sccsid[] = "@(#)daemon.c	8.101 (Berkeley) 06/12/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;
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 
190dfe840b2Seric #ifdef 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
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 			{
3790084e6f6Seric # ifdef NETINET
3800084e6f6Seric 			  case AF_INET:
3810084e6f6Seric 				socksize = sizeof DaemonAddr.sin;
3820084e6f6Seric 				break;
3830084e6f6Seric # endif
3840084e6f6Seric 
3850084e6f6Seric # ifdef 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
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
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);
479850144caSeric #ifdef NETINET
480850144caSeric 			else if (strcasecmp(v, "inet") == 0)
481850144caSeric 				DaemonAddr.sa.sa_family = AF_INET;
482850144caSeric #endif
483850144caSeric #ifdef NETISO
484850144caSeric 			else if (strcasecmp(v, "iso") == 0)
485850144caSeric 				DaemonAddr.sa.sa_family = AF_ISO;
486850144caSeric #endif
487850144caSeric #ifdef NETNS
488850144caSeric 			else if (strcasecmp(v, "ns") == 0)
489850144caSeric 				DaemonAddr.sa.sa_family = AF_NS;
490850144caSeric #endif
491850144caSeric #ifdef 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 			{
502850144caSeric #ifdef 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 
531850144caSeric #ifdef 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 
548850144caSeric #ifdef 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
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;
6269d4a8008Seric #if NAMED_BIND
627134746fbSeric 	extern int h_errno;
628134746fbSeric #endif
6297aa493c5Seric 
6307aa493c5Seric 	/*
6317aa493c5Seric 	**  Set up the address for the mailer.
63271096d12Seric 	**	Accept "[a.b.c.d]" syntax for host name.
6337aa493c5Seric 	*/
6347aa493c5Seric 
6359d4a8008Seric #if NAMED_BIND
636794bdbb9Smiriam 	h_errno = 0;
637134746fbSeric #endif
638794bdbb9Smiriam 	errno = 0;
639967778e2Seric 	bzero(&CurHostAddr, sizeof CurHostAddr);
640c931b82bSeric 	SmtpPhase = mci->mci_phase = "initial connection";
641d945ebe8Seric 	CurHostName = host;
642794bdbb9Smiriam 
64371096d12Seric 	if (host[0] == '[')
64471096d12Seric 	{
645a44d5a5eSeric 		long hid;
6466c2c3107Seric 		register char *p = strchr(host, ']');
64771096d12Seric 
648a44d5a5eSeric 		if (p != NULL)
64971096d12Seric 		{
650a44d5a5eSeric 			*p = '\0';
6514d9c42c2Seric #ifdef NETINET
652a44d5a5eSeric 			hid = inet_addr(&host[1]);
653a7e21fe6Seric 			if (hid == -1)
6544d9c42c2Seric #endif
655a7e21fe6Seric 			{
656a7e21fe6Seric 				/* try it as a host name (avoid MX lookup) */
6578b212fe0Seric 				hp = sm_gethostbyname(&host[1]);
658d8984352Seric 				if (hp == NULL && p[-1] == '.')
659d8984352Seric 				{
6608b212fe0Seric #if NAMED_BIND
6618b212fe0Seric 					int oldopts = _res.options;
6628b212fe0Seric 
6638b212fe0Seric 					_res.options &= ~(RES_DEFNAMES|RES_DNSRCH);
6648b212fe0Seric #endif
665d8984352Seric 					p[-1] = '\0';
6668b212fe0Seric 					hp = sm_gethostbyname(&host[1]);
667d8984352Seric 					p[-1] = '.';
6688b212fe0Seric #if NAMED_BIND
6698b212fe0Seric 					_res.options = oldopts;
6708b212fe0Seric #endif
671d8984352Seric 				}
672a7e21fe6Seric 				*p = ']';
673a7e21fe6Seric 				goto gothostent;
674a7e21fe6Seric 			}
675a44d5a5eSeric 			*p = ']';
67671096d12Seric 		}
677a7e21fe6Seric 		if (p == NULL)
67871096d12Seric 		{
67908b25121Seric 			usrerr("553 Invalid numeric domain spec \"%s\"", host);
6805b068d51Seric 			mci->mci_status = "5.1.2";
68171096d12Seric 			return (EX_NOHOST);
68271096d12Seric 		}
6834d9c42c2Seric #ifdef NETINET
6844d9c42c2Seric 		addr.sin.sin_family = AF_INET;		/*XXX*/
68583c1f4bcSeric 		addr.sin.sin_addr.s_addr = hid;
6864d9c42c2Seric #endif
68771096d12Seric 	}
6881c71e510Seric 	else
6891c71e510Seric 	{
690d8984352Seric 		register char *p = &host[strlen(host) - 1];
691d8984352Seric 
6928b212fe0Seric 		hp = sm_gethostbyname(host);
693d8984352Seric 		if (hp == NULL && *p == '.')
694d8984352Seric 		{
6958b212fe0Seric #if NAMED_BIND
6968b212fe0Seric 			int oldopts = _res.options;
6978b212fe0Seric 
6988b212fe0Seric 			_res.options &= ~(RES_DEFNAMES|RES_DNSRCH);
6998b212fe0Seric #endif
700d8984352Seric 			*p = '\0';
7018b212fe0Seric 			hp = sm_gethostbyname(host);
702d8984352Seric 			*p = '.';
7038b212fe0Seric #if NAMED_BIND
7048b212fe0Seric 			_res.options = oldopts;
7058b212fe0Seric #endif
706d8984352Seric 		}
707a7e21fe6Seric gothostent:
708794bdbb9Smiriam 		if (hp == NULL)
709794bdbb9Smiriam 		{
7109d4a8008Seric #if NAMED_BIND
7118b212fe0Seric 			/* check for name server timeouts */
7128b212fe0Seric 			if (errno == ETIMEDOUT || h_errno == TRY_AGAIN ||
7138b212fe0Seric 			    (errno == ECONNREFUSED && UseNameServer))
7148b212fe0Seric 			{
7158b212fe0Seric 				mci->mci_status = "4.4.3";
71652308a50Seric 				return (EX_TEMPFAIL);
7178b212fe0Seric 			}
718134746fbSeric #endif
7197aa493c5Seric 			return (EX_NOHOST);
720794bdbb9Smiriam 		}
72183c1f4bcSeric 		addr.sa.sa_family = hp->h_addrtype;
72283c1f4bcSeric 		switch (hp->h_addrtype)
72383c1f4bcSeric 		{
72483c1f4bcSeric #ifdef NETINET
72583c1f4bcSeric 		  case AF_INET:
726e2f2f828Seric 			bcopy(hp->h_addr,
72783c1f4bcSeric 				&addr.sin.sin_addr,
7288b212fe0Seric 				INADDRSZ);
72983c1f4bcSeric 			break;
73083c1f4bcSeric #endif
73183c1f4bcSeric 
73283c1f4bcSeric 		  default:
733e2f2f828Seric 			bcopy(hp->h_addr,
73483c1f4bcSeric 				addr.sa.sa_data,
735e2f2f828Seric 				hp->h_length);
73683c1f4bcSeric 			break;
73783c1f4bcSeric 		}
73804344589Sbloom 		i = 1;
7391c71e510Seric 	}
7401c71e510Seric 
7411c71e510Seric 	/*
7421c71e510Seric 	**  Determine the port number.
7431c71e510Seric 	*/
7441c71e510Seric 
7450784fccbSeric 	if (port == 0)
7461c71e510Seric 	{
7471c71e510Seric 		register struct servent *sp = getservbyname("smtp", "tcp");
7481c71e510Seric 
7491c71e510Seric 		if (sp == NULL)
7501c71e510Seric 		{
7513edb2e00Seric #ifdef LOG
7523edb2e00Seric 			if (LogLevel > 2)
7533edb2e00Seric 				syslog(LOG_ERR, "makeconnection: service \"smtp\" unknown");
7543edb2e00Seric #endif
755e5311662Seric 			port = htons(25);
7561c71e510Seric 		}
757e5311662Seric 		else
758e2f2f828Seric 			port = sp->s_port;
759e2f2f828Seric 	}
760e2f2f828Seric 
76183c1f4bcSeric 	switch (addr.sa.sa_family)
762e2f2f828Seric 	{
7634d9c42c2Seric #ifdef NETINET
764e2f2f828Seric 	  case AF_INET:
76583c1f4bcSeric 		addr.sin.sin_port = port;
766e2f2f828Seric 		addrlen = sizeof (struct sockaddr_in);
767e2f2f828Seric 		break;
7684d9c42c2Seric #endif
769e2f2f828Seric 
770e2f2f828Seric #ifdef NETISO
771e2f2f828Seric 	  case AF_ISO:
772e2f2f828Seric 		/* assume two byte transport selector */
773e2f2f828Seric 		bcopy((char *) &port, TSEL((struct sockaddr_iso *) &addr), 2);
774e2f2f828Seric 		addrlen = sizeof (struct sockaddr_iso);
775e2f2f828Seric 		break;
776e2f2f828Seric #endif
777e2f2f828Seric 
778e2f2f828Seric 	  default:
77983c1f4bcSeric 		syserr("Can't connect to address family %d", addr.sa.sa_family);
780e2f2f828Seric 		return (EX_NOHOST);
7811c71e510Seric 	}
7827aa493c5Seric 
7837aa493c5Seric 	/*
7847aa493c5Seric 	**  Try to actually open the connection.
7857aa493c5Seric 	*/
7867aa493c5Seric 
787e17a3a5aSeric #ifdef XLA
788e17a3a5aSeric 	/* if too many connections, don't bother trying */
789e17a3a5aSeric 	if (!xla_noqueue_ok(host))
790e17a3a5aSeric 		return EX_TEMPFAIL;
791e17a3a5aSeric #endif
792e17a3a5aSeric 
7938b212fe0Seric 	firstconnect = TRUE;
794aea02ca1Seric 	for (;;)
795aea02ca1Seric 	{
79661e4310fSeric 		if (tTd(16, 1))
797e2f2f828Seric 			printf("makeconnection (%s [%s])\n",
798e2f2f828Seric 				host, anynet_ntoa(&addr));
7997aa493c5Seric 
800226e3022Seric 		/* save for logging */
801226e3022Seric 		CurHostAddr = addr;
802226e3022Seric 
803914346b1Seric 		if (usesecureport)
804914346b1Seric 		{
805914346b1Seric 			int rport = IPPORT_RESERVED - 1;
806914346b1Seric 
807914346b1Seric 			s = rresvport(&rport);
808914346b1Seric 		}
809914346b1Seric 		else
810914346b1Seric 		{
811af5e902cSeric 			s = socket(AF_INET, SOCK_STREAM, 0);
812914346b1Seric 		}
8137aa493c5Seric 		if (s < 0)
8147aa493c5Seric 		{
8156286bb75Sbloom 			sav_errno = errno;
816914346b1Seric 			syserr("makeconnection: no socket");
8177aa493c5Seric 			goto failure;
8187aa493c5Seric 		}
8197aa493c5Seric 
820b35447dbSeric #ifdef SO_SNDBUF
821b35447dbSeric 		if (TcpSndBufferSize > 0)
822b35447dbSeric 		{
823b35447dbSeric 			if (setsockopt(s, SOL_SOCKET, SO_SNDBUF,
824bf217a95Seric 				       (char *) &TcpSndBufferSize,
825b35447dbSeric 				       sizeof(TcpSndBufferSize)) < 0)
826b35447dbSeric 				syserr("makeconnection: setsockopt(SO_SNDBUF)");
827b35447dbSeric 		}
828b35447dbSeric #endif
829b35447dbSeric 
83061e4310fSeric 		if (tTd(16, 1))
831b31e7f2bSeric 			printf("makeconnection: fd=%d\n", s);
8321b6e4a15Seric 
8331b6e4a15Seric 		/* turn on network debugging? */
834a2ef5fa4Seric 		if (tTd(16, 101))
83552308a50Seric 		{
83652308a50Seric 			int on = 1;
8376173568dSeric 			(void) setsockopt(s, SOL_SOCKET, SO_DEBUG,
838aea02ca1Seric 					  (char *)&on, sizeof on);
83952308a50Seric 		}
84087d6e633Srick 		if (CurEnv->e_xfp != NULL)
841877a6142Seric 			(void) fflush(CurEnv->e_xfp);		/* for debugging */
8424bd6a662Seric 		errno = 0;					/* for debugging */
843e2f2f828Seric 		if (connect(s, (struct sockaddr *) &addr, addrlen) >= 0)
844aea02ca1Seric 			break;
845aea02ca1Seric 
8468b212fe0Seric 		/* if running demand-dialed connection, try again */
8478b212fe0Seric 		if (DialDelay > 0 && firstconnect)
8488b212fe0Seric 		{
8498b212fe0Seric 			if (tTd(16, 1))
8508b212fe0Seric 				printf("Connect failed (%s); trying again...\n",
8518b212fe0Seric 					errstring(sav_errno));
8528b212fe0Seric 			firstconnect = FALSE;
8538b212fe0Seric 			sleep(DialDelay);
8548b212fe0Seric 			continue;
8558b212fe0Seric 		}
8568b212fe0Seric 
857aea02ca1Seric 		/* couldn't connect.... figure out why */
8586286bb75Sbloom 		sav_errno = errno;
8596286bb75Sbloom 		(void) close(s);
8608b212fe0Seric 		if (hp != NULL && hp->h_addr_list[i])
86104344589Sbloom 		{
862aea02ca1Seric 			if (tTd(16, 1))
863e2f2f828Seric 				printf("Connect failed (%s); trying new address....\n",
864e2f2f828Seric 					errstring(sav_errno));
86583c1f4bcSeric 			switch (addr.sa.sa_family)
86683c1f4bcSeric 			{
86783c1f4bcSeric #ifdef NETINET
86883c1f4bcSeric 			  case AF_INET:
869e2f2f828Seric 				bcopy(hp->h_addr_list[i++],
87083c1f4bcSeric 				      &addr.sin.sin_addr,
8718b212fe0Seric 				      INADDRSZ);
87283c1f4bcSeric 				break;
87383c1f4bcSeric #endif
87483c1f4bcSeric 
87583c1f4bcSeric 			  default:
876e2f2f828Seric 				bcopy(hp->h_addr_list[i++],
87783c1f4bcSeric 					addr.sa.sa_data,
878914346b1Seric 					hp->h_length);
87983c1f4bcSeric 				break;
88083c1f4bcSeric 			}
881aea02ca1Seric 			continue;
88204344589Sbloom 		}
88304344589Sbloom 
8847aa493c5Seric 		/* failure, decide if temporary or not */
8857aa493c5Seric 	failure:
886244b09d1Seric #ifdef XLA
887244b09d1Seric 		xla_host_end(host);
888244b09d1Seric #endif
889e2de2524Seric 		if (transienterror(sav_errno))
890e2de2524Seric 			return EX_TEMPFAIL;
891e2de2524Seric 		else
89287d6e633Srick 		{
89308b25121Seric 			message("%s", errstring(sav_errno));
8947aa493c5Seric 			return (EX_UNAVAILABLE);
8957aa493c5Seric 		}
8967aa493c5Seric 	}
8977aa493c5Seric 
8987aa493c5Seric 	/* connection ok, put it into canonical form */
899335eae58Seric 	if ((mci->mci_out = fdopen(s, "w")) == NULL ||
900335eae58Seric 	    (s = dup(s)) < 0 ||
901ab81ee53Seric 	    (mci->mci_in = fdopen(s, "r")) == NULL)
902335eae58Seric 	{
903335eae58Seric 		syserr("cannot open SMTP client channel, fd=%d", s);
904335eae58Seric 		return EX_TEMPFAIL;
905335eae58Seric 	}
9067aa493c5Seric 
907dca8e1f7Seric 	return (EX_OK);
9087aa493c5Seric }
909444eaf03Seric /*
910444eaf03Seric **  MYHOSTNAME -- return the name of this host.
911444eaf03Seric **
912444eaf03Seric **	Parameters:
913444eaf03Seric **		hostbuf -- a place to return the name of this host.
914897f1869Seric **		size -- the size of hostbuf.
915444eaf03Seric **
916444eaf03Seric **	Returns:
917444eaf03Seric **		A list of aliases for this host.
918444eaf03Seric **
919444eaf03Seric **	Side Effects:
920d8d0a4aeSeric **		Adds numeric codes to $=w.
921444eaf03Seric */
922444eaf03Seric 
9238b212fe0Seric struct hostent *
924897f1869Seric myhostname(hostbuf, size)
925444eaf03Seric 	char hostbuf[];
926897f1869Seric 	int size;
927444eaf03Seric {
92838ad259dSeric 	register struct hostent *hp;
9298b212fe0Seric 	extern bool getcanonname();
9308b212fe0Seric 	extern int h_errno;
931444eaf03Seric 
932af5e902cSeric 	if (gethostname(hostbuf, size) < 0)
933af5e902cSeric 	{
934af5e902cSeric 		(void) strcpy(hostbuf, "localhost");
935af5e902cSeric 	}
9368b212fe0Seric 	hp = sm_gethostbyname(hostbuf);
9374a5c6430Seric 	if (hp == NULL)
9388b212fe0Seric 		return NULL;
9398b212fe0Seric 	if (strchr(hp->h_name, '.') != NULL || strchr(hostbuf, '.') == NULL)
940423161fbSeric 	{
94135852b23Seric 		(void) strncpy(hostbuf, hp->h_name, size - 1);
94235852b23Seric 		hostbuf[size - 1] = '\0';
9438b212fe0Seric 	}
9444a5c6430Seric 
9458b212fe0Seric 	/*
946*d4ebb9abSeric 	**  If there is still no dot in the name, try looking for a
947*d4ebb9abSeric 	**  dotted alias.
9488b212fe0Seric 	*/
94923668637Seric 
9508868a898Seric 	if (strchr(hostbuf, '.') == NULL)
9518868a898Seric 	{
952*d4ebb9abSeric 		char **ha;
9538868a898Seric 
954*d4ebb9abSeric 		for (ha = hp->h_aliases; *ha != NULL; ha++)
95523668637Seric 		{
956*d4ebb9abSeric 			if (strchr(*ha, '.') != NULL)
957*d4ebb9abSeric 			{
958*d4ebb9abSeric 				(void) strncpy(hostbuf, *ha, size - 1);
959*d4ebb9abSeric 				hostbuf[size - 1] = '\0';
960*d4ebb9abSeric 				break;
961*d4ebb9abSeric 			}
962*d4ebb9abSeric 		}
963*d4ebb9abSeric 	}
964*d4ebb9abSeric 
965*d4ebb9abSeric 	/*
966*d4ebb9abSeric 	**  If _still_ no dot, wait for a while and try again -- it is
967*d4ebb9abSeric 	**  possible that some service is starting up.  This can result
968*d4ebb9abSeric 	**  in excessive delays if the system is badly configured, but
969*d4ebb9abSeric 	**  there really isn't a way around that, particularly given that
970*d4ebb9abSeric 	**  the config file hasn't been read at this point.
971*d4ebb9abSeric 	**  All in all, a bit of a mess.
972*d4ebb9abSeric 	*/
973*d4ebb9abSeric 
974*d4ebb9abSeric 	if (strchr(hostbuf, '.') == NULL &&
975*d4ebb9abSeric 	    !getcanonname(hostbuf, size, TRUE))
976*d4ebb9abSeric 	{
977*d4ebb9abSeric 		message("My unqualifed host name (%s) unknown; sleeping for retry",
97823668637Seric 			hostbuf);
9798b212fe0Seric 		sleep(60);
980*d4ebb9abSeric 		(void) getcanonname(hostbuf, size, TRUE);
981747df804Seric 	}
9828b212fe0Seric 	return (hp);
9837364df9fSeric }
984cb452edcSeric /*
9859f8b0eadSeric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
9869f8b0eadSeric **
9879f8b0eadSeric **	Uses RFC1413 protocol to try to get info from the other end.
988320e0d1cSeric **
989320e0d1cSeric **	Parameters:
990320e0d1cSeric **		fd -- the descriptor
991320e0d1cSeric **
992320e0d1cSeric **	Returns:
9939f8b0eadSeric **		The user@host information associated with this descriptor.
994320e0d1cSeric */
995320e0d1cSeric 
9969f8b0eadSeric static jmp_buf	CtxAuthTimeout;
9979f8b0eadSeric 
9988b212fe0Seric static void
9999f8b0eadSeric authtimeout()
10009f8b0eadSeric {
10019f8b0eadSeric 	longjmp(CtxAuthTimeout, 1);
10029f8b0eadSeric }
10039f8b0eadSeric 
1004320e0d1cSeric char *
10059f8b0eadSeric getauthinfo(fd)
1006320e0d1cSeric 	int fd;
1007320e0d1cSeric {
10089f8b0eadSeric 	int falen;
1009a5546e24Seric 	register char *p;
10109f8b0eadSeric 	SOCKADDR la;
10119f8b0eadSeric 	int lalen;
10129f8b0eadSeric 	register struct servent *sp;
10139f8b0eadSeric 	int s;
10149f8b0eadSeric 	int i;
10159f8b0eadSeric 	EVENT *ev;
1016579270a3Seric 	int nleft;
1017f036c8d7Seric 	char ibuf[MAXNAME + 1];
10189f8b0eadSeric 	static char hbuf[MAXNAME * 2 + 2];
10199f8b0eadSeric 	extern char *hostnamebyanyaddr();
1020320e0d1cSeric 
1021e29a76d1Seric 	falen = sizeof RealHostAddr;
10228b212fe0Seric 	if (isatty(fd) || getpeername(fd, &RealHostAddr.sa, &falen) < 0 ||
10238b212fe0Seric 	    falen <= 0 || RealHostAddr.sa.sa_family == 0)
10249f8b0eadSeric 	{
10259f8b0eadSeric 		(void) sprintf(hbuf, "%s@localhost", RealUserName);
102653853673Seric 		if (tTd(9, 1))
10279f8b0eadSeric 			printf("getauthinfo: %s\n", hbuf);
1028320e0d1cSeric 		return hbuf;
1029320e0d1cSeric 	}
10309f8b0eadSeric 
1031e29a76d1Seric 	if (RealHostName == NULL)
1032e29a76d1Seric 	{
1033e29a76d1Seric 		/* translate that to a host name */
1034e29a76d1Seric 		RealHostName = newstr(hostnamebyanyaddr(&RealHostAddr));
1035e29a76d1Seric 	}
1036e29a76d1Seric 
103793b3215bSeric 	if (TimeOuts.to_ident == 0)
103893b3215bSeric 		goto noident;
103993b3215bSeric 
10409f8b0eadSeric 	lalen = sizeof la;
1041e29a76d1Seric 	if (RealHostAddr.sa.sa_family != AF_INET ||
10429f8b0eadSeric 	    getsockname(fd, &la.sa, &lalen) < 0 || lalen <= 0 ||
10439f8b0eadSeric 	    la.sa.sa_family != AF_INET)
10449f8b0eadSeric 	{
10459f8b0eadSeric 		/* no ident info */
10469f8b0eadSeric 		goto noident;
10479f8b0eadSeric 	}
10489f8b0eadSeric 
10499f8b0eadSeric 	/* create ident query */
105081d2944fSeric 	(void) sprintf(ibuf, "%d,%d\r\n",
1051e29a76d1Seric 		ntohs(RealHostAddr.sin.sin_port), ntohs(la.sin.sin_port));
10529f8b0eadSeric 
10539f8b0eadSeric 	/* create local address */
1054d6af7dadSeric 	la.sin.sin_port = 0;
10559f8b0eadSeric 
10569f8b0eadSeric 	/* create foreign address */
10579f8b0eadSeric 	sp = getservbyname("auth", "tcp");
10589f8b0eadSeric 	if (sp != NULL)
1059e29a76d1Seric 		RealHostAddr.sin.sin_port = sp->s_port;
10609f8b0eadSeric 	else
1061e29a76d1Seric 		RealHostAddr.sin.sin_port = htons(113);
10629f8b0eadSeric 
10639f8b0eadSeric 	s = -1;
10649f8b0eadSeric 	if (setjmp(CtxAuthTimeout) != 0)
10659f8b0eadSeric 	{
10669f8b0eadSeric 		if (s >= 0)
10679f8b0eadSeric 			(void) close(s);
10689f8b0eadSeric 		goto noident;
10699f8b0eadSeric 	}
10709f8b0eadSeric 
10719f8b0eadSeric 	/* put a timeout around the whole thing */
1072a0f780efSeric 	ev = setevent(TimeOuts.to_ident, authtimeout, 0);
10739f8b0eadSeric 
1074d6af7dadSeric 	/* connect to foreign IDENT server using same address as SMTP socket */
10759f8b0eadSeric 	s = socket(AF_INET, SOCK_STREAM, 0);
10769f8b0eadSeric 	if (s < 0)
10779f8b0eadSeric 	{
10789f8b0eadSeric 		clrevent(ev);
10799f8b0eadSeric 		goto noident;
10809f8b0eadSeric 	}
1081d6af7dadSeric 	if (bind(s, &la.sa, sizeof la.sin) < 0 ||
1082e29a76d1Seric 	    connect(s, &RealHostAddr.sa, sizeof RealHostAddr.sin) < 0)
10839f8b0eadSeric 	{
10847c201575Seric 		goto closeident;
10859f8b0eadSeric 	}
10869f8b0eadSeric 
108753853673Seric 	if (tTd(9, 10))
108881d2944fSeric 		printf("getauthinfo: sent %s", ibuf);
10899f8b0eadSeric 
10909f8b0eadSeric 	/* send query */
109181d2944fSeric 	if (write(s, ibuf, strlen(ibuf)) < 0)
10929f8b0eadSeric 		goto closeident;
10939f8b0eadSeric 
10949f8b0eadSeric 	/* get result */
109581d2944fSeric 	p = &ibuf[0];
1096b26156f6Seric 	nleft = sizeof ibuf - 1;
1097579270a3Seric 	while ((i = read(s, p, nleft)) > 0)
1098579270a3Seric 	{
1099579270a3Seric 		p += i;
1100579270a3Seric 		nleft -= i;
1101579270a3Seric 	}
11029f8b0eadSeric 	(void) close(s);
11039f8b0eadSeric 	clrevent(ev);
110481d2944fSeric 	if (i < 0 || p == &ibuf[0])
11059f8b0eadSeric 		goto noident;
1106579270a3Seric 
1107579270a3Seric 	if (*--p == '\n' && *--p == '\r')
1108579270a3Seric 		p--;
1109579270a3Seric 	*++p = '\0';
11109f8b0eadSeric 
111153853673Seric 	if (tTd(9, 3))
111281d2944fSeric 		printf("getauthinfo:  got %s\n", ibuf);
11139f8b0eadSeric 
11149f8b0eadSeric 	/* parse result */
111581d2944fSeric 	p = strchr(ibuf, ':');
11169f8b0eadSeric 	if (p == NULL)
11179f8b0eadSeric 	{
11189f8b0eadSeric 		/* malformed response */
11199f8b0eadSeric 		goto noident;
11209f8b0eadSeric 	}
11219f8b0eadSeric 	while (isascii(*++p) && isspace(*p))
11229f8b0eadSeric 		continue;
11239f8b0eadSeric 	if (strncasecmp(p, "userid", 6) != 0)
11249f8b0eadSeric 	{
11259f8b0eadSeric 		/* presumably an error string */
11269f8b0eadSeric 		goto noident;
11279f8b0eadSeric 	}
11289f8b0eadSeric 	p += 6;
11299f8b0eadSeric 	while (isascii(*p) && isspace(*p))
11309f8b0eadSeric 		p++;
11319f8b0eadSeric 	if (*p++ != ':')
11329f8b0eadSeric 	{
11339f8b0eadSeric 		/* either useridxx or malformed response */
11349f8b0eadSeric 		goto noident;
11359f8b0eadSeric 	}
11369f8b0eadSeric 
11379f8b0eadSeric 	/* p now points to the OSTYPE field */
11388b212fe0Seric 	while (isascii(*p) && isspace(*p))
11398b212fe0Seric 		p++;
11408b212fe0Seric 	if (strncasecmp(p, "other", 5) == 0 &&
11418b212fe0Seric 	    (p[5] == ':' || p[5] == ' ' || p[5] == ',' || p[5] == '\0'))
11428b212fe0Seric 	{
11438b212fe0Seric 		/* not useful information */
11448b212fe0Seric 		goto noident;
11458b212fe0Seric 	}
11469f8b0eadSeric 	p = strchr(p, ':');
11479f8b0eadSeric 	if (p == NULL)
11489f8b0eadSeric 	{
11499f8b0eadSeric 		/* malformed response */
11509f8b0eadSeric 		goto noident;
11519f8b0eadSeric 	}
115253853673Seric 
115353853673Seric 	/* 1413 says don't do this -- but it's broken otherwise */
115453853673Seric 	while (isascii(*++p) && isspace(*p))
115553853673Seric 		continue;
11569f8b0eadSeric 
1157a7763879Seric 	/* p now points to the authenticated name -- copy carefully */
115881d2944fSeric 	cleanstrcpy(hbuf, p, MAXNAME);
115993433f31Seric 	i = strlen(hbuf);
1160a7763879Seric 	hbuf[i++] = '@';
1161a7763879Seric 	strcpy(&hbuf[i], RealHostName == NULL ? "localhost" : RealHostName);
116211af795cSeric 	goto postident;
116353853673Seric 
11647c201575Seric closeident:
11657c201575Seric 	(void) close(s);
11667c201575Seric 	clrevent(ev);
11677c201575Seric 
116853853673Seric noident:
1169f7869e68Seric 	if (RealHostName == NULL)
1170f7869e68Seric 	{
1171f7869e68Seric 		if (tTd(9, 1))
1172f7869e68Seric 			printf("getauthinfo: NULL\n");
1173f7869e68Seric 		return NULL;
1174f7869e68Seric 	}
117553853673Seric 	(void) strcpy(hbuf, RealHostName);
117653853673Seric 
117711af795cSeric postident:
117811af795cSeric #if IP_SRCROUTE
117911af795cSeric 	/*
118011af795cSeric 	**  Extract IP source routing information.
118111af795cSeric 	**
118211af795cSeric 	**	Format of output for a connection from site a through b
118311af795cSeric 	**	through c to d:
118411af795cSeric 	**		loose:      @site-c@site-b:site-a
118511af795cSeric 	**		strict:	   !@site-c@site-b:site-a
118611af795cSeric 	**
118711af795cSeric 	**	o - pointer within ipopt_list structure.
118811af795cSeric 	**	q - pointer within ls/ss rr route data
118911af795cSeric 	**	p - pointer to hbuf
119011af795cSeric 	*/
119111af795cSeric 
119211af795cSeric 	if (RealHostAddr.sa.sa_family == AF_INET)
119311af795cSeric 	{
119411af795cSeric 		int ipoptlen, j;
11954d6b8580Seric 		u_char *q;
119611af795cSeric 		u_char *o;
119711af795cSeric 		struct in_addr addr;
119811af795cSeric 		struct ipoption ipopt;
119911af795cSeric 
120011af795cSeric 		ipoptlen = sizeof ipopt;
120111af795cSeric 		if (getsockopt(fd, IPPROTO_IP, IP_OPTIONS,
120211af795cSeric 			       (char *) &ipopt, &ipoptlen) < 0)
120311af795cSeric 			goto noipsr;
120411af795cSeric 		if (ipoptlen == 0)
120511af795cSeric 			goto noipsr;
12064d6b8580Seric 		o = (u_char *) ipopt.ipopt_list;
12075db00695Seric 		while (o != NULL && o < (u_char *) &ipopt + ipoptlen)
120811af795cSeric 		{
120911af795cSeric 			switch (*o)
121011af795cSeric 			{
121111af795cSeric 			  case IPOPT_EOL:
121211af795cSeric 				o = NULL;
121311af795cSeric 				break;
121411af795cSeric 
121511af795cSeric 			  case IPOPT_NOP:
121611af795cSeric 				o++;
121711af795cSeric 				break;
121811af795cSeric 
121911af795cSeric 			  case IPOPT_SSRR:
122011af795cSeric 			  case IPOPT_LSRR:
122111af795cSeric 				p = &hbuf[strlen(hbuf)];
122211af795cSeric 				sprintf(p, " [%s@%s",
122311af795cSeric 				    *o == IPOPT_SSRR ? "!" : "",
122411af795cSeric 				    inet_ntoa(ipopt.ipopt_dst));
122511af795cSeric 				p += strlen(p);
122611af795cSeric 
122711af795cSeric 				/* o[1] is option length */
122811af795cSeric 				j = *++o / sizeof(struct in_addr) - 1;
122911af795cSeric 
123011af795cSeric 				/* q skips length and router pointer to data */
123111af795cSeric 				q = o + 2;
123211af795cSeric 				for ( ; j >= 0; j--)
123311af795cSeric 				{
123411af795cSeric 					memcpy(&addr, q, sizeof(addr));
12354d6b8580Seric 					sprintf(p, "%c%s",
123611af795cSeric 						     j ? '@' : ':',
123711af795cSeric 						     inet_ntoa(addr));
12384d6b8580Seric 					p += strlen(p);
123911af795cSeric 					q += sizeof(struct in_addr);
124011af795cSeric 				}
124111af795cSeric 				o += *o;
124211af795cSeric 				break;
124311af795cSeric 
124411af795cSeric 			  default:
124511af795cSeric 				/* Skip over option */
124611af795cSeric 				o += o[1];
124711af795cSeric 				break;
124811af795cSeric 			}
124911af795cSeric 		}
125011af795cSeric 		strcat(hbuf,"]");
125111af795cSeric 		goto postipsr;
125211af795cSeric 	}
125311af795cSeric #endif
125411af795cSeric 
125511af795cSeric noipsr:
1256f7869e68Seric 	if (RealHostName != NULL && RealHostName[0] != '[')
12579f8b0eadSeric 	{
12589f8b0eadSeric 		p = &hbuf[strlen(hbuf)];
12599f8b0eadSeric 		(void) sprintf(p, " [%s]", anynet_ntoa(&RealHostAddr));
12609f8b0eadSeric 	}
126111af795cSeric 
126211af795cSeric postipsr:
126353853673Seric 	if (tTd(9, 1))
12649f8b0eadSeric 		printf("getauthinfo: %s\n", hbuf);
12659f8b0eadSeric 	return hbuf;
12669f8b0eadSeric }
1267320e0d1cSeric /*
126808de856eSeric **  HOST_MAP_LOOKUP -- turn a hostname into canonical form
126915d084d5Seric **
127015d084d5Seric **	Parameters:
127105b57da8Seric **		map -- a pointer to this map (unused).
127208de856eSeric **		name -- the (presumably unqualified) hostname.
127300b385a9Seric **		av -- unused -- for compatibility with other mapping
1274d798a1deSeric **			functions.
12752d29d43aSeric **		statp -- an exit status (out parameter) -- set to
12762d29d43aSeric **			EX_TEMPFAIL if the name server is unavailable.
127715d084d5Seric **
127815d084d5Seric **	Returns:
127915d084d5Seric **		The mapping, if found.
128015d084d5Seric **		NULL if no mapping found.
128115d084d5Seric **
128215d084d5Seric **	Side Effects:
128315d084d5Seric **		Looks up the host specified in hbuf.  If it is not
128415d084d5Seric **		the canonical name for that host, return the canonical
128515d084d5Seric **		name.
1286f36ede03Sbostic */
1287cb452edcSeric 
128815d084d5Seric char *
128900b385a9Seric host_map_lookup(map, name, av, statp)
129005b57da8Seric 	MAP *map;
129108de856eSeric 	char *name;
129200b385a9Seric 	char **av;
12932d29d43aSeric 	int *statp;
129499f7cf32Seric {
129599f7cf32Seric 	register struct hostent *hp;
12968b212fe0Seric 	struct in_addr in_addr;
129705b57da8Seric 	char *cp;
1298eea91d78Seric 	register STAB *s;
12998b212fe0Seric 	char hbuf[MAXNAME + 1];
13009d4a8008Seric #if NAMED_BIND
1301eea91d78Seric 	extern int h_errno;
1302c304a798Seric #endif
13035f78836eSmiriam 
1304f36ede03Sbostic 	/*
1305eea91d78Seric 	**  See if we have already looked up this name.  If so, just
1306eea91d78Seric 	**  return it.
1307eea91d78Seric 	*/
1308eea91d78Seric 
130908de856eSeric 	s = stab(name, ST_NAMECANON, ST_ENTER);
1310eea91d78Seric 	if (bitset(NCF_VALID, s->s_namecanon.nc_flags))
1311eea91d78Seric 	{
1312f92c3297Seric 		if (tTd(9, 1))
131308de856eSeric 			printf("host_map_lookup(%s) => CACHE %s\n",
131498826a83Seric 			       name,
131598826a83Seric 			       s->s_namecanon.nc_cname == NULL
131698826a83Seric 					? "NULL"
13175f08d413Seric 					: s->s_namecanon.nc_cname);
1318eea91d78Seric 		errno = s->s_namecanon.nc_errno;
13199d4a8008Seric #if NAMED_BIND
1320eea91d78Seric 		h_errno = s->s_namecanon.nc_herrno;
1321c304a798Seric #endif
1322eea91d78Seric 		*statp = s->s_namecanon.nc_stat;
1323fcfb36d9Seric 		if (*statp == EX_TEMPFAIL)
1324ed63aae0Seric 		{
13255b068d51Seric 			CurEnv->e_status = "4.4.3";
1326fcfb36d9Seric 			usrerr("451 %s: Name server timeout",
1327ed63aae0Seric 				shortenstring(name, 33));
1328ed63aae0Seric 		}
1329eea91d78Seric 		return s->s_namecanon.nc_cname;
1330eea91d78Seric 	}
1331eea91d78Seric 
1332eea91d78Seric 	/*
1333eea91d78Seric 	**  If first character is a bracket, then it is an address
1334eea91d78Seric 	**  lookup.  Address is copied into a temporary buffer to
133508de856eSeric 	**  strip the brackets and to preserve name if address is
1336eea91d78Seric 	**  unknown.
1337f36ede03Sbostic 	*/
133815d084d5Seric 
133908de856eSeric 	if (*name != '[')
134015d084d5Seric 	{
1341d798a1deSeric 		extern bool getcanonname();
1342d798a1deSeric 
13438cb4653dSeric 		if (tTd(9, 1))
134408de856eSeric 			printf("host_map_lookup(%s) => ", name);
1345eea91d78Seric 		s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
13468b212fe0Seric 		if (strlen(name) < sizeof hbuf)
134708de856eSeric 			(void) strcpy(hbuf, name);
13488b212fe0Seric 		else
13498b212fe0Seric 		{
13508b212fe0Seric 			bcopy(name, hbuf, sizeof hbuf - 1);
13518b212fe0Seric 			hbuf[sizeof hbuf - 1] = '\0';
13528b212fe0Seric 		}
13530183c3f6Seric 		if (getcanonname(hbuf, sizeof hbuf - 1, !NoMXforCanon))
13549040ec4fSeric 		{
13559040ec4fSeric 			if (tTd(9, 1))
13569040ec4fSeric 				printf("%s\n", hbuf);
135700b385a9Seric 			cp = map_rewrite(map, hbuf, strlen(hbuf), av);
135800b385a9Seric 			s->s_namecanon.nc_cname = newstr(cp);
135900b385a9Seric 			return cp;
13609040ec4fSeric 		}
136115d084d5Seric 		else
13629040ec4fSeric 		{
13632d29d43aSeric 			register struct hostent *hp;
13642d29d43aSeric 
1365c304a798Seric 			s->s_namecanon.nc_errno = errno;
13669d4a8008Seric #if NAMED_BIND
1367c304a798Seric 			s->s_namecanon.nc_herrno = h_errno;
13689040ec4fSeric 			if (tTd(9, 1))
13692d29d43aSeric 				printf("FAIL (%d)\n", h_errno);
13702d29d43aSeric 			switch (h_errno)
13712d29d43aSeric 			{
13722d29d43aSeric 			  case TRY_AGAIN:
137389cb2793Seric 				if (UseNameServer)
13748820d51bSeric 				{
13755b068d51Seric 					CurEnv->e_status = "4.4.3";
1376fcfb36d9Seric 					usrerr("451 %s: Name server timeout",
1377ed63aae0Seric 						shortenstring(name, 33));
13788820d51bSeric 				}
13792d29d43aSeric 				*statp = EX_TEMPFAIL;
13802d29d43aSeric 				break;
13812d29d43aSeric 
13822d29d43aSeric 			  case HOST_NOT_FOUND:
138380027ae6Seric 			  case NO_DATA:
13842d29d43aSeric 				*statp = EX_NOHOST;
13852d29d43aSeric 				break;
13862d29d43aSeric 
13872d29d43aSeric 			  case NO_RECOVERY:
13882d29d43aSeric 				*statp = EX_SOFTWARE;
13892d29d43aSeric 				break;
13902d29d43aSeric 
13912d29d43aSeric 			  default:
13922d29d43aSeric 				*statp = EX_UNAVAILABLE;
13932d29d43aSeric 				break;
13942d29d43aSeric 			}
1395c304a798Seric #else
1396c304a798Seric 			if (tTd(9, 1))
1397c304a798Seric 				printf("FAIL\n");
1398c304a798Seric 			*statp = EX_NOHOST;
1399c304a798Seric #endif
1400eea91d78Seric 			s->s_namecanon.nc_stat = *statp;
14018b212fe0Seric 			if ((*statp != EX_TEMPFAIL && *statp != EX_NOHOST) ||
14028b212fe0Seric 			    UseNameServer)
140315d084d5Seric 				return NULL;
14042d29d43aSeric 
14052d29d43aSeric 			/*
14062d29d43aSeric 			**  Try to look it up in /etc/hosts
14072d29d43aSeric 			*/
14082d29d43aSeric 
14098b212fe0Seric 			hp = sm_gethostbyname(name);
14102d29d43aSeric 			if (hp == NULL)
14112d29d43aSeric 			{
14122d29d43aSeric 				/* no dice there either */
1413eea91d78Seric 				s->s_namecanon.nc_stat = *statp = EX_NOHOST;
14142d29d43aSeric 				return NULL;
14152d29d43aSeric 			}
14162d29d43aSeric 
1417eea91d78Seric 			s->s_namecanon.nc_stat = *statp = EX_OK;
141800b385a9Seric 			cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
141900b385a9Seric 			s->s_namecanon.nc_cname = newstr(cp);
142000b385a9Seric 			return cp;
142115d084d5Seric 		}
14229040ec4fSeric 	}
142308de856eSeric 	if ((cp = strchr(name, ']')) == NULL)
142415d084d5Seric 		return (NULL);
142534e39927Sbostic 	*cp = '\0';
14268b212fe0Seric 	in_addr.s_addr = inet_addr(&name[1]);
142738ad259dSeric 
142838ad259dSeric 	/* nope -- ask the name server */
14298b212fe0Seric 	hp = sm_gethostbyaddr((char *)&in_addr, INADDRSZ, AF_INET);
1430eea91d78Seric 	s->s_namecanon.nc_errno = errno;
14319d4a8008Seric #if NAMED_BIND
1432eea91d78Seric 	s->s_namecanon.nc_herrno = h_errno;
1433c304a798Seric #endif
1434eea91d78Seric 	s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
14355f78836eSmiriam 	if (hp == NULL)
1436eea91d78Seric 	{
1437eea91d78Seric 		s->s_namecanon.nc_stat = *statp = EX_NOHOST;
143815d084d5Seric 		return (NULL);
1439eea91d78Seric 	}
144015d084d5Seric 
144138ad259dSeric 	/* found a match -- copy out */
144200b385a9Seric 	cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
1443eea91d78Seric 	s->s_namecanon.nc_stat = *statp = EX_OK;
144400b385a9Seric 	s->s_namecanon.nc_cname = newstr(cp);
144500b385a9Seric 	return cp;
144699f7cf32Seric }
1447e2f2f828Seric /*
1448e2f2f828Seric **  ANYNET_NTOA -- convert a network address to printable form.
1449e2f2f828Seric **
1450e2f2f828Seric **	Parameters:
1451e2f2f828Seric **		sap -- a pointer to a sockaddr structure.
1452e2f2f828Seric **
1453e2f2f828Seric **	Returns:
1454e2f2f828Seric **		A printable version of that sockaddr.
1455e2f2f828Seric */
1456e2f2f828Seric 
1457543d9d93Seric #ifdef NETLINK
145806cc5485Seric # include <net/if_dl.h>
145906cc5485Seric #endif
146006cc5485Seric 
1461e2f2f828Seric char *
1462e2f2f828Seric anynet_ntoa(sap)
1463e2f2f828Seric 	register SOCKADDR *sap;
1464e2f2f828Seric {
1465e2f2f828Seric 	register char *bp;
1466e2f2f828Seric 	register char *ap;
1467e2f2f828Seric 	int l;
1468e387851eSeric 	static char buf[100];
1469e2f2f828Seric 
14708cb4653dSeric 	/* check for null/zero family */
14718cb4653dSeric 	if (sap == NULL)
14728cb4653dSeric 		return "NULLADDR";
14738cb4653dSeric 	if (sap->sa.sa_family == 0)
14748cb4653dSeric 		return "0";
14758cb4653dSeric 
1476e387851eSeric 	switch (sap->sa.sa_family)
1477e387851eSeric 	{
1478139b52c8Seric #ifdef NETUNIX
1479e387851eSeric 	  case AF_UNIX:
1480c24cf5a4Seric 	  	if (sap->sunix.sun_path[0] != '\0')
1481c24cf5a4Seric 	  		sprintf(buf, "[UNIX: %.64s]", sap->sunix.sun_path);
1482e387851eSeric 	  	else
1483e387851eSeric 	  		sprintf(buf, "[UNIX: localhost]");
1484e387851eSeric 		return buf;
1485e387851eSeric #endif
1486e387851eSeric 
148783c1f4bcSeric #ifdef NETINET
1488e387851eSeric 	  case AF_INET:
1489482e28bcSeric 		return inet_ntoa(sap->sin.sin_addr);
149083c1f4bcSeric #endif
1491e2f2f828Seric 
1492543d9d93Seric #ifdef NETLINK
149306cc5485Seric 	  case AF_LINK:
149406cc5485Seric 		sprintf(buf, "[LINK: %s]",
149506cc5485Seric 			link_ntoa((struct sockaddr_dl *) &sap->sa));
149606cc5485Seric 		return buf;
149706cc5485Seric #endif
1498e387851eSeric 	  default:
149906cc5485Seric 		/* this case is needed when nothing is #defined */
150006cc5485Seric 		/* in order to keep the switch syntactically correct */
1501e387851eSeric 		break;
1502e387851eSeric 	}
1503e387851eSeric 
1504e2f2f828Seric 	/* unknown family -- just dump bytes */
150583c1f4bcSeric 	(void) sprintf(buf, "Family %d: ", sap->sa.sa_family);
1506e2f2f828Seric 	bp = &buf[strlen(buf)];
150783c1f4bcSeric 	ap = sap->sa.sa_data;
150883c1f4bcSeric 	for (l = sizeof sap->sa.sa_data; --l >= 0; )
1509e2f2f828Seric 	{
1510e2f2f828Seric 		(void) sprintf(bp, "%02x:", *ap++ & 0377);
1511e2f2f828Seric 		bp += 3;
1512e2f2f828Seric 	}
1513e2f2f828Seric 	*--bp = '\0';
1514e2f2f828Seric 	return buf;
1515e2f2f828Seric }
15169f8b0eadSeric /*
15179f8b0eadSeric **  HOSTNAMEBYANYADDR -- return name of host based on address
15189f8b0eadSeric **
15199f8b0eadSeric **	Parameters:
15209f8b0eadSeric **		sap -- SOCKADDR pointer
15219f8b0eadSeric **
15229f8b0eadSeric **	Returns:
15239f8b0eadSeric **		text representation of host name.
15249f8b0eadSeric **
15259f8b0eadSeric **	Side Effects:
15269f8b0eadSeric **		none.
15279f8b0eadSeric */
15289f8b0eadSeric 
15299f8b0eadSeric char *
15309f8b0eadSeric hostnamebyanyaddr(sap)
15319f8b0eadSeric 	register SOCKADDR *sap;
15329f8b0eadSeric {
15339f8b0eadSeric 	register struct hostent *hp;
15343490b9dfSeric 	int saveretry;
15353490b9dfSeric 
15369d4a8008Seric #if NAMED_BIND
15373490b9dfSeric 	/* shorten name server timeout to avoid higher level timeouts */
15383490b9dfSeric 	saveretry = _res.retry;
15393490b9dfSeric 	_res.retry = 3;
15403490b9dfSeric #endif /* NAMED_BIND */
15413490b9dfSeric 
15429f8b0eadSeric 	switch (sap->sa.sa_family)
15439f8b0eadSeric 	{
15449f8b0eadSeric #ifdef NETINET
15459f8b0eadSeric 	  case AF_INET:
15468b212fe0Seric 		hp = sm_gethostbyaddr((char *) &sap->sin.sin_addr,
15478b212fe0Seric 			INADDRSZ,
15489f8b0eadSeric 			AF_INET);
15499f8b0eadSeric 		break;
15509f8b0eadSeric #endif
15519f8b0eadSeric 
15529f8b0eadSeric #ifdef NETISO
15539f8b0eadSeric 	  case AF_ISO:
15548b212fe0Seric 		hp = sm_gethostbyaddr((char *) &sap->siso.siso_addr,
15559f8b0eadSeric 			sizeof sap->siso.siso_addr,
15569f8b0eadSeric 			AF_ISO);
15579f8b0eadSeric 		break;
15589f8b0eadSeric #endif
15599f8b0eadSeric 
1560e387851eSeric 	  case AF_UNIX:
1561e387851eSeric 		hp = NULL;
1562e387851eSeric 		break;
1563e387851eSeric 
15649f8b0eadSeric 	  default:
15658b212fe0Seric 		hp = sm_gethostbyaddr(sap->sa.sa_data,
15669f8b0eadSeric 			   sizeof sap->sa.sa_data,
15679f8b0eadSeric 			   sap->sa.sa_family);
15689f8b0eadSeric 		break;
15699f8b0eadSeric 	}
15709f8b0eadSeric 
15719d4a8008Seric #if NAMED_BIND
15723490b9dfSeric 	_res.retry = saveretry;
15733490b9dfSeric #endif /* NAMED_BIND */
15743490b9dfSeric 
15759f8b0eadSeric 	if (hp != NULL)
15769f8b0eadSeric 		return hp->h_name;
15779f8b0eadSeric 	else
15789f8b0eadSeric 	{
15799f8b0eadSeric 		/* produce a dotted quad */
15809f8b0eadSeric 		static char buf[512];
15819f8b0eadSeric 
15829f8b0eadSeric 		(void) sprintf(buf, "[%s]", anynet_ntoa(sap));
15839f8b0eadSeric 		return buf;
15849f8b0eadSeric 	}
15859f8b0eadSeric }
1586f36ede03Sbostic 
15876c2c3107Seric # else /* DAEMON */
158899f7cf32Seric /* code for systems without sophisticated networking */
1589444eaf03Seric 
1590444eaf03Seric /*
1591444eaf03Seric **  MYHOSTNAME -- stub version for case of no daemon code.
159221e9914dSeric **
159321e9914dSeric **	Can't convert to upper case here because might be a UUCP name.
1594897f1869Seric **
1595897f1869Seric **	Mark, you can change this to be anything you want......
1596444eaf03Seric */
1597444eaf03Seric 
1598444eaf03Seric char **
1599897f1869Seric myhostname(hostbuf, size)
1600444eaf03Seric 	char hostbuf[];
1601897f1869Seric 	int size;
1602444eaf03Seric {
1603444eaf03Seric 	register FILE *f;
1604444eaf03Seric 
1605444eaf03Seric 	hostbuf[0] = '\0';
1606444eaf03Seric 	f = fopen("/usr/include/whoami", "r");
1607444eaf03Seric 	if (f != NULL)
1608444eaf03Seric 	{
1609897f1869Seric 		(void) fgets(hostbuf, size, f);
1610444eaf03Seric 		fixcrlf(hostbuf, TRUE);
1611444eaf03Seric 		(void) fclose(f);
1612444eaf03Seric 	}
1613444eaf03Seric 	return (NULL);
1614444eaf03Seric }
161599f7cf32Seric /*
16169f8b0eadSeric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
1617320e0d1cSeric **
1618320e0d1cSeric **	Parameters:
1619320e0d1cSeric **		fd -- the descriptor
1620320e0d1cSeric **
1621320e0d1cSeric **	Returns:
1622320e0d1cSeric **		The host name associated with this descriptor, if it can
1623320e0d1cSeric **			be determined.
1624320e0d1cSeric **		NULL otherwise.
1625320e0d1cSeric **
1626320e0d1cSeric **	Side Effects:
1627320e0d1cSeric **		none
1628320e0d1cSeric */
1629320e0d1cSeric 
1630320e0d1cSeric char *
16319f8b0eadSeric getauthinfo(fd)
1632320e0d1cSeric 	int fd;
1633320e0d1cSeric {
1634320e0d1cSeric 	return NULL;
1635320e0d1cSeric }
1636320e0d1cSeric /*
163799f7cf32Seric **  MAPHOSTNAME -- turn a hostname into canonical form
163899f7cf32Seric **
163999f7cf32Seric **	Parameters:
164005b57da8Seric **		map -- a pointer to the database map.
164108de856eSeric **		name -- a buffer containing a hostname.
164215d084d5Seric **		avp -- a pointer to a (cf file defined) argument vector.
16432d29d43aSeric **		statp -- an exit status (out parameter).
164499f7cf32Seric **
164599f7cf32Seric **	Returns:
164615d084d5Seric **		mapped host name
1647cb452edcSeric **		FALSE otherwise.
164899f7cf32Seric **
164999f7cf32Seric **	Side Effects:
165008de856eSeric **		Looks up the host specified in name.  If it is not
165199f7cf32Seric **		the canonical name for that host, replace it with
165299f7cf32Seric **		the canonical name.  If the name is unknown, or it
165399f7cf32Seric **		is already the canonical name, leave it unchanged.
165499f7cf32Seric */
165599f7cf32Seric 
165699f7cf32Seric /*ARGSUSED*/
165715d084d5Seric char *
165808de856eSeric host_map_lookup(map, name, avp, statp)
165905b57da8Seric 	MAP *map;
166008de856eSeric 	char *name;
166115d084d5Seric 	char **avp;
16622d29d43aSeric 	char *statp;
166399f7cf32Seric {
16642d29d43aSeric 	register struct hostent *hp;
16652d29d43aSeric 
16668b212fe0Seric 	hp = sm_gethostbyname(name);
16672d29d43aSeric 	if (hp != NULL)
16682d29d43aSeric 		return hp->h_name;
16692d29d43aSeric 	*statp = EX_NOHOST;
167015d084d5Seric 	return NULL;
167199f7cf32Seric }
167299f7cf32Seric 
16736c2c3107Seric #endif /* DAEMON */
1674