xref: /original-bsd/usr.sbin/sendmail/src/daemon.c (revision ab81ee53)
1939f5b94Sdist /*
20942ea6aSbostic  * Copyright (c) 1983 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*ab81ee53Seric static char sccsid[] = "@(#)daemon.c	8.17 (Berkeley) 10/17/93 (with daemon mode)";
15d0a9e852Seric #else
16*ab81ee53Seric static char sccsid[] = "@(#)daemon.c	8.17 (Berkeley) 10/17/93 (without daemon mode)";
17da1c6175Sbostic #endif
18da1c6175Sbostic #endif /* not lint */
19da1c6175Sbostic 
20da1c6175Sbostic #ifdef DAEMON
21d0a9e852Seric 
221c71e510Seric # include <netdb.h>
23af5e902cSeric # include <sys/time.h>
24d8d0a4aeSeric # include <arpa/inet.h>
25d0a9e852Seric 
263490b9dfSeric #ifdef NAMED_BIND
273490b9dfSeric # include <arpa/nameser.h>
283490b9dfSeric # include <resolv.h>
293490b9dfSeric #endif
303490b9dfSeric 
317fa39d90Seric /*
327fa39d90Seric **  DAEMON.C -- routines to use when running as a daemon.
3347b12ae1Seric **
3447b12ae1Seric **	This entire file is highly dependent on the 4.2 BSD
3547b12ae1Seric **	interprocess communication primitives.  No attempt has
3647b12ae1Seric **	been made to make this file portable to Version 7,
3747b12ae1Seric **	Version 6, MPX files, etc.  If you should try such a
3847b12ae1Seric **	thing yourself, I recommend chucking the entire file
3947b12ae1Seric **	and starting from scratch.  Basic semantics are:
4047b12ae1Seric **
4147b12ae1Seric **	getrequests()
4247b12ae1Seric **		Opens a port and initiates a connection.
4347b12ae1Seric **		Returns in a child.  Must set InChannel and
4447b12ae1Seric **		OutChannel appropriately.
45b7d7afcbSeric **	clrdaemon()
46b7d7afcbSeric **		Close any open files associated with getting
47b7d7afcbSeric **		the connection; this is used when running the queue,
48b7d7afcbSeric **		etc., to avoid having extra file descriptors during
49b7d7afcbSeric **		the queue run and to avoid confusing the network
50b7d7afcbSeric **		code (if it cares).
51914346b1Seric **	makeconnection(host, port, outfile, infile, usesecureport)
5247b12ae1Seric **		Make a connection to the named host on the given
5347b12ae1Seric **		port.  Set *outfile and *infile to the files
5447b12ae1Seric **		appropriate for communication.  Returns zero on
5547b12ae1Seric **		success, else an exit status describing the
5647b12ae1Seric **		error.
5708de856eSeric **	host_map_lookup(map, hbuf, avp, pstat)
5805b57da8Seric **		Convert the entry in hbuf into a canonical form.
597fa39d90Seric */
607fa39d90Seric /*
617fa39d90Seric **  GETREQUESTS -- open mail IPC port and get requests.
627fa39d90Seric **
637fa39d90Seric **	Parameters:
647fa39d90Seric **		none.
657fa39d90Seric **
667fa39d90Seric **	Returns:
677fa39d90Seric **		none.
687fa39d90Seric **
697fa39d90Seric **	Side Effects:
707fa39d90Seric **		Waits until some interesting activity occurs.  When
717fa39d90Seric **		it does, a child is created to process it, and the
727fa39d90Seric **		parent waits for completion.  Return from this
73147303b1Seric **		routine is always in the child.  The file pointers
74147303b1Seric **		"InChannel" and "OutChannel" should be set to point
75147303b1Seric **		to the communication channel.
767fa39d90Seric */
777fa39d90Seric 
78b7d7afcbSeric int		DaemonSocket	= -1;		/* fd describing socket */
79bfb80540Seric SOCKADDR	DaemonAddr;			/* socket for incoming */
80bfc1eaf8Seric int		ListenQueueSize = 10;		/* size of listen queue */
81b35447dbSeric int		TcpRcvBufferSize = 0;		/* size of TCP receive buffer */
82b35447dbSeric int		TcpSndBufferSize = 0;		/* size of TCP send buffer */
831c71e510Seric 
847fa39d90Seric getrequests()
857fa39d90Seric {
861c71e510Seric 	int t;
871c71e510Seric 	register struct servent *sp;
887868dfc2Seric 	int on = 1;
8915d084d5Seric 	bool refusingconnections = TRUE;
900aae1086Seric 	FILE *pidf;
919b100374Sbostic 	extern void reapchild();
92eb889047Seric 
93a8268164Seric 	/*
941c71e510Seric 	**  Set up the address for the mailer.
95eb889047Seric 	*/
96eb889047Seric 
97bfb80540Seric 	if (DaemonAddr.sin.sin_family == 0)
98bfb80540Seric 		DaemonAddr.sin.sin_family = AF_INET;
99bfb80540Seric 	if (DaemonAddr.sin.sin_addr.s_addr == 0)
100bfb80540Seric 		DaemonAddr.sin.sin_addr.s_addr = INADDR_ANY;
101bfb80540Seric 	if (DaemonAddr.sin.sin_port == 0)
102bfb80540Seric 	{
1031c71e510Seric 		sp = getservbyname("smtp", "tcp");
1041c71e510Seric 		if (sp == NULL)
105d0a9e852Seric 		{
106ad977999Seric 			syserr("554 service \"smtp\" unknown");
107a1961f2aSeric 			goto severe;
1081c71e510Seric 		}
109bfb80540Seric 		DaemonAddr.sin.sin_port = sp->s_port;
110bfb80540Seric 	}
1111c71e510Seric 
1121c71e510Seric 	/*
1131c71e510Seric 	**  Try to actually open the connection.
1141c71e510Seric 	*/
1151c71e510Seric 
1161c71e510Seric 	if (tTd(15, 1))
117bfb80540Seric 		printf("getrequests: port 0x%x\n", DaemonAddr.sin.sin_port);
1181c71e510Seric 
1191c71e510Seric 	/* get a socket for the SMTP connection */
12098e28903Seric 	DaemonSocket = socket(DaemonAddr.sa.sa_family, SOCK_STREAM, 0);
121b7d7afcbSeric 	if (DaemonSocket < 0)
1221c71e510Seric 	{
1231c71e510Seric 		/* probably another daemon already */
1241c71e510Seric 		syserr("getrequests: can't create socket");
1251c71e510Seric 	  severe:
126b0ba8827Seric # ifdef LOG
127b0ba8827Seric 		if (LogLevel > 0)
1280c034190Seric 			syslog(LOG_ALERT, "problem creating SMTP socket");
1296c2c3107Seric # endif /* LOG */
13047b12ae1Seric 		finis();
131d0a9e852Seric 	}
1321b6e4a15Seric 
1331b6e4a15Seric 	/* turn on network debugging? */
134a2ef5fa4Seric 	if (tTd(15, 101))
13552308a50Seric 		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on);
1361b6e4a15Seric 
1377868dfc2Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on);
1387868dfc2Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on);
1397868dfc2Seric 
14008feafeaSeric #ifdef SO_RCVBUF
14108feafeaSeric 	if (TcpRcvBufferSize > 0)
14208feafeaSeric 	{
14308feafeaSeric 		if (setsockopt(DaemonSocket, SOL_SOCKET, SO_RCVBUF,
144bf217a95Seric 			       (char *) &TcpRcvBufferSize,
14508feafeaSeric 			       sizeof(TcpRcvBufferSize)) < 0)
14608feafeaSeric 			syserr("getrequests: setsockopt(SO_RCVBUF)");
14708feafeaSeric 	}
14808feafeaSeric #endif
14908feafeaSeric 
15098e28903Seric 	switch (DaemonAddr.sa.sa_family)
15198e28903Seric 	{
15298e28903Seric # ifdef NETINET
15398e28903Seric 	  case AF_INET:
15498e28903Seric 		t = sizeof DaemonAddr.sin;
15598e28903Seric 		break;
15698e28903Seric # endif
15798e28903Seric 
15898e28903Seric # ifdef NETISO
15998e28903Seric 	  case AF_ISO:
16098e28903Seric 		t = sizeof DaemonAddr.siso;
16198e28903Seric 		break;
16298e28903Seric # endif
16398e28903Seric 
16498e28903Seric 	  default:
16598e28903Seric 		t = sizeof DaemonAddr;
16698e28903Seric 		break;
16798e28903Seric 	}
16898e28903Seric 
16998e28903Seric 	if (bind(DaemonSocket, &DaemonAddr.sa, t) < 0)
1701c71e510Seric 	{
1711c71e510Seric 		syserr("getrequests: cannot bind");
172b7d7afcbSeric 		(void) close(DaemonSocket);
1731c71e510Seric 		goto severe;
1741c71e510Seric 	}
1751c71e510Seric 
1762b9178d3Seric 	(void) setsignal(SIGCHLD, reapchild);
17752308a50Seric 
1780aae1086Seric 	/* write the pid to the log file for posterity */
1790aae1086Seric 	pidf = fopen(PidFile, "w");
1800aae1086Seric 	if (pidf != NULL)
1810aae1086Seric 	{
18237950f67Seric 		extern char *CommandLineArgs;
18337950f67Seric 
18437950f67Seric 		/* write the process id on line 1 */
1850aae1086Seric 		fprintf(pidf, "%d\n", getpid());
18637950f67Seric 
18737950f67Seric 		/* line 2 contains all command line flags */
18837950f67Seric 		fprintf(pidf, "%s\n", CommandLineArgs);
18937950f67Seric 
19037950f67Seric 		/* flush and close */
1910aae1086Seric 		fclose(pidf);
1920aae1086Seric 	}
1930aae1086Seric 
1940aae1086Seric 
1951c71e510Seric 	if (tTd(15, 1))
196b7d7afcbSeric 		printf("getrequests: %d\n", DaemonSocket);
1971c71e510Seric 
1981c71e510Seric 	for (;;)
1991c71e510Seric 	{
2003a099713Seric 		register int pid;
201a44d5a5eSeric 		auto int lotherend;
20215d084d5Seric 		extern bool refuseconnections();
2033a099713Seric 
2043a099713Seric 		/* see if we are rejecting connections */
20515d084d5Seric 		CurrentLA = getla();
20615d084d5Seric 		if (refuseconnections())
2076775ec03Sbostic 		{
20815d084d5Seric 			if (!refusingconnections)
20915d084d5Seric 			{
21015d084d5Seric 				/* don't queue so peer will fail quickly */
21115d084d5Seric 				(void) listen(DaemonSocket, 0);
21215d084d5Seric 				refusingconnections = TRUE;
21315d084d5Seric 			}
21471e5e267Seric 			setproctitle("rejecting connections: load average: %d",
21571e5e267Seric 				CurrentLA);
2163a099713Seric 			sleep(5);
21715d084d5Seric 			continue;
21815d084d5Seric 		}
21915d084d5Seric 
22015d084d5Seric 		if (refusingconnections)
22115d084d5Seric 		{
22215d084d5Seric 			/* start listening again */
223bfc1eaf8Seric 			if (listen(DaemonSocket, ListenQueueSize) < 0)
22415d084d5Seric 			{
22515d084d5Seric 				syserr("getrequests: cannot listen");
22615d084d5Seric 				(void) close(DaemonSocket);
22715d084d5Seric 				goto severe;
22815d084d5Seric 			}
22915d084d5Seric 			setproctitle("accepting connections");
23015d084d5Seric 			refusingconnections = FALSE;
2316775ec03Sbostic 		}
232a44d5a5eSeric 
2331c71e510Seric 		/* wait for a connection */
2341c71e510Seric 		do
2351c71e510Seric 		{
2361c71e510Seric 			errno = 0;
2379f9a15b6Skarels 			lotherend = sizeof RealHostAddr;
2389b100374Sbostic 			t = accept(DaemonSocket,
2399b100374Sbostic 			    (struct sockaddr *)&RealHostAddr, &lotherend);
2401c71e510Seric 		} while (t < 0 && errno == EINTR);
2411c71e510Seric 		if (t < 0)
2421c71e510Seric 		{
2431c71e510Seric 			syserr("getrequests: accept");
2441c71e510Seric 			sleep(5);
2451c71e510Seric 			continue;
2461c71e510Seric 		}
247d0a9e852Seric 
248d0a9e852Seric 		/*
249d0a9e852Seric 		**  Create a subprocess to process the mail.
250d0a9e852Seric 		*/
251d0a9e852Seric 
25261e4310fSeric 		if (tTd(15, 2))
2531c71e510Seric 			printf("getrequests: forking (fd = %d)\n", t);
254eb889047Seric 
255a8268164Seric 		pid = fork();
256a8268164Seric 		if (pid < 0)
257a8268164Seric 		{
258a8268164Seric 			syserr("daemon: cannot fork");
259a8268164Seric 			sleep(10);
2601c71e510Seric 			(void) close(t);
261a8268164Seric 			continue;
262a8268164Seric 		}
263a8268164Seric 
264a8268164Seric 		if (pid == 0)
265a8268164Seric 		{
266da662164Seric 			char *p;
2679f8b0eadSeric 			extern char *hostnamebyanyaddr();
268a44d5a5eSeric 
269a8268164Seric 			/*
270a8268164Seric 			**  CHILD -- return to caller.
271a44d5a5eSeric 			**	Collect verified idea of sending host.
272a8268164Seric 			**	Verify calling user id if possible here.
273a8268164Seric 			*/
274a8268164Seric 
2752b9178d3Seric 			(void) setsignal(SIGCHLD, SIG_DFL);
276e17a3a5aSeric 			OpMode = MD_SMTP;
277779ac194Seric 
278a44d5a5eSeric 			/* determine host name */
279da662164Seric 			p = hostnamebyanyaddr(&RealHostAddr);
280da662164Seric 			RealHostName = newstr(p);
28129dcf4baSeric 
2822a6bc25bSeric #ifdef LOG
2831f2ff1a4Seric 			if (LogLevel > 11)
2842a6bc25bSeric 			{
2852a6bc25bSeric 				/* log connection information */
2862a6bc25bSeric 				syslog(LOG_INFO, "connect from %s (%s)",
2879f8b0eadSeric 					RealHostName, anynet_ntoa(&RealHostAddr));
2882a6bc25bSeric 			}
2892a6bc25bSeric #endif
2902a6bc25bSeric 
291244b09d1Seric 			(void) close(DaemonSocket);
292335eae58Seric 			if ((InChannel = fdopen(t, "r")) == NULL ||
293335eae58Seric 			    (t = dup(t)) < 0 ||
294335eae58Seric 			    (OutChannel = fdopen(t, "w")) == NULL)
295335eae58Seric 			{
296335eae58Seric 				syserr("cannot open SMTP server channel, fd=%d", t);
297335eae58Seric 				exit(0);
298335eae58Seric 			}
299244b09d1Seric 
30029dcf4baSeric 			/* should we check for illegal connection here? XXX */
301e17a3a5aSeric #ifdef XLA
302e17a3a5aSeric 			if (!xla_host_ok(RealHostName))
303e17a3a5aSeric 			{
304244b09d1Seric 				message("421 Too many SMTP sessions for this host");
305e17a3a5aSeric 				exit(0);
306e17a3a5aSeric 			}
307e17a3a5aSeric #endif
308a44d5a5eSeric 
30961e4310fSeric 			if (tTd(15, 2))
310d0a9e852Seric 				printf("getreq: returning\n");
311a8268164Seric 			return;
312a8268164Seric 		}
313a8268164Seric 
3143c154354Seric 		/* close the port so that others will hang (for a while) */
3153c154354Seric 		(void) close(t);
3168e3e4b17Seric 	}
3173c154354Seric 	/*NOTREACHED*/
3183c154354Seric }
3198e3e4b17Seric /*
320b7d7afcbSeric **  CLRDAEMON -- reset the daemon connection
321b7d7afcbSeric **
322b7d7afcbSeric **	Parameters:
323b7d7afcbSeric **		none.
324b7d7afcbSeric **
325b7d7afcbSeric **	Returns:
326b7d7afcbSeric **		none.
327b7d7afcbSeric **
328b7d7afcbSeric **	Side Effects:
329b7d7afcbSeric **		releases any resources used by the passive daemon.
330b7d7afcbSeric */
331b7d7afcbSeric 
332b7d7afcbSeric clrdaemon()
333b7d7afcbSeric {
334b7d7afcbSeric 	if (DaemonSocket >= 0)
335b7d7afcbSeric 		(void) close(DaemonSocket);
336b7d7afcbSeric 	DaemonSocket = -1;
337b7d7afcbSeric }
338b7d7afcbSeric /*
339bfb80540Seric **  SETDAEMONOPTIONS -- set options for running the daemon
340bfb80540Seric **
341bfb80540Seric **	Parameters:
342bfb80540Seric **		p -- the options line.
343bfb80540Seric **
344bfb80540Seric **	Returns:
345bfb80540Seric **		none.
346bfb80540Seric */
347bfb80540Seric 
348bfb80540Seric setdaemonoptions(p)
349bfb80540Seric 	register char *p;
350bfb80540Seric {
351850144caSeric 	if (DaemonAddr.sa.sa_family == AF_UNSPEC)
352850144caSeric 		DaemonAddr.sa.sa_family = AF_INET;
353850144caSeric 
354bfb80540Seric 	while (p != NULL)
355bfb80540Seric 	{
356bfb80540Seric 		register char *f;
357bfb80540Seric 		register char *v;
358bfb80540Seric 
359bfb80540Seric 		while (isascii(*p) && isspace(*p))
360bfb80540Seric 			p++;
361bfb80540Seric 		if (*p == '\0')
362bfb80540Seric 			break;
363bfb80540Seric 		f = p;
364bfb80540Seric 		p = strchr(p, ',');
365bfb80540Seric 		if (p != NULL)
366bfb80540Seric 			*p++ = '\0';
367bfb80540Seric 		v = strchr(f, '=');
368bfb80540Seric 		if (v == NULL)
369bfb80540Seric 			continue;
370bfb80540Seric 		while (isascii(*++v) && isspace(*v))
371bfb80540Seric 			continue;
372bfb80540Seric 
373bfb80540Seric 		switch (*f)
374bfb80540Seric 		{
375850144caSeric 		  case 'F':		/* address family */
376850144caSeric 			if (isascii(*v) && isdigit(*v))
377850144caSeric 				DaemonAddr.sa.sa_family = atoi(v);
378850144caSeric #ifdef NETINET
379850144caSeric 			else if (strcasecmp(v, "inet") == 0)
380850144caSeric 				DaemonAddr.sa.sa_family = AF_INET;
381850144caSeric #endif
382850144caSeric #ifdef NETISO
383850144caSeric 			else if (strcasecmp(v, "iso") == 0)
384850144caSeric 				DaemonAddr.sa.sa_family = AF_ISO;
385850144caSeric #endif
386850144caSeric #ifdef NETNS
387850144caSeric 			else if (strcasecmp(v, "ns") == 0)
388850144caSeric 				DaemonAddr.sa.sa_family = AF_NS;
389850144caSeric #endif
390850144caSeric #ifdef NETX25
391850144caSeric 			else if (strcasecmp(v, "x.25") == 0)
392850144caSeric 				DaemonAddr.sa.sa_family = AF_CCITT;
393850144caSeric #endif
394850144caSeric 			else
395850144caSeric 				syserr("554 Unknown address family %s in Family=option", v);
396850144caSeric 			break;
397850144caSeric 
398850144caSeric 		  case 'A':		/* address */
399850144caSeric 			switch (DaemonAddr.sa.sa_family)
400850144caSeric 			{
401850144caSeric #ifdef NETINET
402850144caSeric 			  case AF_INET:
403850144caSeric 				if (isascii(*v) && isdigit(*v))
404850144caSeric 					DaemonAddr.sin.sin_addr.s_addr = inet_network(v);
405850144caSeric 				else
406850144caSeric 				{
407850144caSeric 					register struct netent *np;
408850144caSeric 
409850144caSeric 					np = getnetbyname(v);
410850144caSeric 					if (np == NULL)
411850144caSeric 						syserr("554 network \"%s\" unknown", v);
412850144caSeric 					else
413850144caSeric 						DaemonAddr.sin.sin_addr.s_addr = np->n_net;
414850144caSeric 				}
415850144caSeric 				break;
416850144caSeric #endif
417850144caSeric 
418850144caSeric 			  default:
419850144caSeric 				syserr("554 Address= option unsupported for family %d",
420850144caSeric 					DaemonAddr.sa.sa_family);
421850144caSeric 				break;
422850144caSeric 			}
423850144caSeric 			break;
424850144caSeric 
425bfb80540Seric 		  case 'P':		/* port */
426850144caSeric 			switch (DaemonAddr.sa.sa_family)
427850144caSeric 			{
428850144caSeric 				short port;
429850144caSeric 
430850144caSeric #ifdef NETINET
431850144caSeric 			  case AF_INET:
432bfb80540Seric 				if (isascii(*v) && isdigit(*v))
43376b70c58Seric 					DaemonAddr.sin.sin_port = htons(atoi(v));
434bfb80540Seric 				else
435bfb80540Seric 				{
436bfb80540Seric 					register struct servent *sp;
437bfb80540Seric 
438bfb80540Seric 					sp = getservbyname(v, "tcp");
439bfb80540Seric 					if (sp == NULL)
440ad977999Seric 						syserr("554 service \"%s\" unknown", v);
441bfb80540Seric 					else
442bfb80540Seric 						DaemonAddr.sin.sin_port = sp->s_port;
443bfb80540Seric 				}
444bfb80540Seric 				break;
445850144caSeric #endif
446bfb80540Seric 
447850144caSeric #ifdef NETISO
448850144caSeric 			  case AF_ISO:
449850144caSeric 				/* assume two byte transport selector */
450bfb80540Seric 				if (isascii(*v) && isdigit(*v))
45176b70c58Seric 					port = htons(atoi(v));
452bfb80540Seric 				else
453bfb80540Seric 				{
454850144caSeric 					register struct servent *sp;
455bfb80540Seric 
456850144caSeric 					sp = getservbyname(v, "tcp");
457850144caSeric 					if (sp == NULL)
458ad977999Seric 						syserr("554 service \"%s\" unknown", v);
459bfb80540Seric 					else
460850144caSeric 						port = sp->s_port;
461850144caSeric 				}
462850144caSeric 				bcopy((char *) &port, TSEL(&DaemonAddr.siso), 2);
463850144caSeric 				break;
464850144caSeric #endif
465850144caSeric 
466850144caSeric 			  default:
467850144caSeric 				syserr("554 Port= option unsupported for family %d",
468850144caSeric 					DaemonAddr.sa.sa_family);
469850144caSeric 				break;
470bfb80540Seric 			}
471bfb80540Seric 			break;
472bfc1eaf8Seric 
473bfc1eaf8Seric 		  case 'L':		/* listen queue size */
474bfc1eaf8Seric 			ListenQueueSize = atoi(v);
475bfc1eaf8Seric 			break;
476b35447dbSeric 
477b35447dbSeric 		  case 'S':		/* send buffer size */
478b35447dbSeric 			TcpSndBufferSize = atoi(v);
479b35447dbSeric 			break;
480b35447dbSeric 
481b35447dbSeric 		  case 'R':		/* receive buffer size */
482b35447dbSeric 			TcpRcvBufferSize = atoi(v);
483b35447dbSeric 			break;
484bfb80540Seric 		}
485bfb80540Seric 	}
486bfb80540Seric }
487bfb80540Seric /*
4887aa493c5Seric **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
4897aa493c5Seric **
4907aa493c5Seric **	Parameters:
4917aa493c5Seric **		host -- the name of the host.
49248ff0a9dSeric **		port -- the port number to connect to.
493655feedbSeric **		mci -- a pointer to the mail connection information
494655feedbSeric **			structure to be filled in.
495914346b1Seric **		usesecureport -- if set, use a low numbered (reserved)
496914346b1Seric **			port to provide some rudimentary authentication.
4977aa493c5Seric **
4987aa493c5Seric **	Returns:
4997aa493c5Seric **		An exit code telling whether the connection could be
5007aa493c5Seric **			made and if not why not.
5017aa493c5Seric **
5027aa493c5Seric **	Side Effects:
5037aa493c5Seric **		none.
5047aa493c5Seric */
5057aa493c5Seric 
506e2f2f828Seric SOCKADDR	CurHostAddr;		/* address of current host */
50771ff6caaSeric 
508b31e7f2bSeric int
509655feedbSeric makeconnection(host, port, mci, usesecureport)
5107aa493c5Seric 	char *host;
511210215eaSeric 	u_short port;
512b31e7f2bSeric 	register MCI *mci;
513914346b1Seric 	bool usesecureport;
5147aa493c5Seric {
51504344589Sbloom 	register int i, s;
51604344589Sbloom 	register struct hostent *hp = (struct hostent *)NULL;
517e2f2f828Seric 	SOCKADDR addr;
5186286bb75Sbloom 	int sav_errno;
519e2f2f828Seric 	int addrlen;
520134746fbSeric #ifdef NAMED_BIND
521134746fbSeric 	extern int h_errno;
522134746fbSeric #endif
5237aa493c5Seric 
5247aa493c5Seric 	/*
5257aa493c5Seric 	**  Set up the address for the mailer.
52671096d12Seric 	**	Accept "[a.b.c.d]" syntax for host name.
5277aa493c5Seric 	*/
5287aa493c5Seric 
529134746fbSeric #ifdef NAMED_BIND
530794bdbb9Smiriam 	h_errno = 0;
531134746fbSeric #endif
532794bdbb9Smiriam 	errno = 0;
533967778e2Seric 	bzero(&CurHostAddr, sizeof CurHostAddr);
534c931b82bSeric 	SmtpPhase = mci->mci_phase = "initial connection";
535d945ebe8Seric 	CurHostName = host;
536794bdbb9Smiriam 
53771096d12Seric 	if (host[0] == '[')
53871096d12Seric 	{
539a44d5a5eSeric 		long hid;
5406c2c3107Seric 		register char *p = strchr(host, ']');
54171096d12Seric 
542a44d5a5eSeric 		if (p != NULL)
54371096d12Seric 		{
544a44d5a5eSeric 			*p = '\0';
5454d9c42c2Seric #ifdef NETINET
546a44d5a5eSeric 			hid = inet_addr(&host[1]);
547a7e21fe6Seric 			if (hid == -1)
5484d9c42c2Seric #endif
549a7e21fe6Seric 			{
550a7e21fe6Seric 				/* try it as a host name (avoid MX lookup) */
551a7e21fe6Seric 				hp = gethostbyname(&host[1]);
552a7e21fe6Seric 				*p = ']';
553a7e21fe6Seric 				goto gothostent;
554a7e21fe6Seric 			}
555a44d5a5eSeric 			*p = ']';
55671096d12Seric 		}
557a7e21fe6Seric 		if (p == NULL)
55871096d12Seric 		{
55908b25121Seric 			usrerr("553 Invalid numeric domain spec \"%s\"", host);
56071096d12Seric 			return (EX_NOHOST);
56171096d12Seric 		}
5624d9c42c2Seric #ifdef NETINET
5634d9c42c2Seric 		addr.sin.sin_family = AF_INET;		/*XXX*/
56483c1f4bcSeric 		addr.sin.sin_addr.s_addr = hid;
5654d9c42c2Seric #endif
56671096d12Seric 	}
5671c71e510Seric 	else
5681c71e510Seric 	{
56904344589Sbloom 		hp = gethostbyname(host);
570a7e21fe6Seric gothostent:
571794bdbb9Smiriam 		if (hp == NULL)
572794bdbb9Smiriam 		{
573134746fbSeric #ifdef NAMED_BIND
574794bdbb9Smiriam 			if (errno == ETIMEDOUT || h_errno == TRY_AGAIN)
57552308a50Seric 				return (EX_TEMPFAIL);
57682e5d8ddSeric 
577134746fbSeric 			/* if name server is specified, assume temp fail */
578134746fbSeric 			if (errno == ECONNREFUSED && UseNameServer)
579134746fbSeric 				return (EX_TEMPFAIL);
580134746fbSeric #endif
5817aa493c5Seric 			return (EX_NOHOST);
582794bdbb9Smiriam 		}
58383c1f4bcSeric 		addr.sa.sa_family = hp->h_addrtype;
58483c1f4bcSeric 		switch (hp->h_addrtype)
58583c1f4bcSeric 		{
58683c1f4bcSeric #ifdef NETINET
58783c1f4bcSeric 		  case AF_INET:
588e2f2f828Seric 			bcopy(hp->h_addr,
58983c1f4bcSeric 				&addr.sin.sin_addr,
590e2f2f828Seric 				hp->h_length);
59183c1f4bcSeric 			break;
59283c1f4bcSeric #endif
59383c1f4bcSeric 
59483c1f4bcSeric 		  default:
595e2f2f828Seric 			bcopy(hp->h_addr,
59683c1f4bcSeric 				addr.sa.sa_data,
597e2f2f828Seric 				hp->h_length);
59883c1f4bcSeric 			break;
59983c1f4bcSeric 		}
60004344589Sbloom 		i = 1;
6011c71e510Seric 	}
6021c71e510Seric 
6031c71e510Seric 	/*
6041c71e510Seric 	**  Determine the port number.
6051c71e510Seric 	*/
6061c71e510Seric 
607fd7c0790Seric 	if (port != 0)
608e2f2f828Seric 		port = htons(port);
609fd7c0790Seric 	else
6101c71e510Seric 	{
6111c71e510Seric 		register struct servent *sp = getservbyname("smtp", "tcp");
6121c71e510Seric 
6131c71e510Seric 		if (sp == NULL)
6141c71e510Seric 		{
615ad977999Seric 			syserr("554 makeconnection: service \"smtp\" unknown");
616845e533cSeric 			return (EX_OSERR);
6171c71e510Seric 		}
618e2f2f828Seric 		port = sp->s_port;
619e2f2f828Seric 	}
620e2f2f828Seric 
62183c1f4bcSeric 	switch (addr.sa.sa_family)
622e2f2f828Seric 	{
6234d9c42c2Seric #ifdef NETINET
624e2f2f828Seric 	  case AF_INET:
62583c1f4bcSeric 		addr.sin.sin_port = port;
626e2f2f828Seric 		addrlen = sizeof (struct sockaddr_in);
627e2f2f828Seric 		break;
6284d9c42c2Seric #endif
629e2f2f828Seric 
630e2f2f828Seric #ifdef NETISO
631e2f2f828Seric 	  case AF_ISO:
632e2f2f828Seric 		/* assume two byte transport selector */
633e2f2f828Seric 		bcopy((char *) &port, TSEL((struct sockaddr_iso *) &addr), 2);
634e2f2f828Seric 		addrlen = sizeof (struct sockaddr_iso);
635e2f2f828Seric 		break;
636e2f2f828Seric #endif
637e2f2f828Seric 
638e2f2f828Seric 	  default:
63983c1f4bcSeric 		syserr("Can't connect to address family %d", addr.sa.sa_family);
640e2f2f828Seric 		return (EX_NOHOST);
6411c71e510Seric 	}
6427aa493c5Seric 
6437aa493c5Seric 	/*
6447aa493c5Seric 	**  Try to actually open the connection.
6457aa493c5Seric 	*/
6467aa493c5Seric 
647e17a3a5aSeric #ifdef XLA
648e17a3a5aSeric 	/* if too many connections, don't bother trying */
649e17a3a5aSeric 	if (!xla_noqueue_ok(host))
650e17a3a5aSeric 		return EX_TEMPFAIL;
651e17a3a5aSeric #endif
652e17a3a5aSeric 
653aea02ca1Seric 	for (;;)
654aea02ca1Seric 	{
65561e4310fSeric 		if (tTd(16, 1))
656e2f2f828Seric 			printf("makeconnection (%s [%s])\n",
657e2f2f828Seric 				host, anynet_ntoa(&addr));
6587aa493c5Seric 
659226e3022Seric 		/* save for logging */
660226e3022Seric 		CurHostAddr = addr;
661226e3022Seric 
662914346b1Seric 		if (usesecureport)
663914346b1Seric 		{
664914346b1Seric 			int rport = IPPORT_RESERVED - 1;
665914346b1Seric 
666914346b1Seric 			s = rresvport(&rport);
667914346b1Seric 		}
668914346b1Seric 		else
669914346b1Seric 		{
670af5e902cSeric 			s = socket(AF_INET, SOCK_STREAM, 0);
671914346b1Seric 		}
6727aa493c5Seric 		if (s < 0)
6737aa493c5Seric 		{
6746286bb75Sbloom 			sav_errno = errno;
675914346b1Seric 			syserr("makeconnection: no socket");
6767aa493c5Seric 			goto failure;
6777aa493c5Seric 		}
6787aa493c5Seric 
679b35447dbSeric #ifdef SO_SNDBUF
680b35447dbSeric 		if (TcpSndBufferSize > 0)
681b35447dbSeric 		{
682b35447dbSeric 			if (setsockopt(s, SOL_SOCKET, SO_SNDBUF,
683bf217a95Seric 				       (char *) &TcpSndBufferSize,
684b35447dbSeric 				       sizeof(TcpSndBufferSize)) < 0)
685b35447dbSeric 				syserr("makeconnection: setsockopt(SO_SNDBUF)");
686b35447dbSeric 		}
687b35447dbSeric #endif
688b35447dbSeric 
68961e4310fSeric 		if (tTd(16, 1))
690b31e7f2bSeric 			printf("makeconnection: fd=%d\n", s);
6911b6e4a15Seric 
6921b6e4a15Seric 		/* turn on network debugging? */
693a2ef5fa4Seric 		if (tTd(16, 101))
69452308a50Seric 		{
69552308a50Seric 			int on = 1;
696aea02ca1Seric 			(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG,
697aea02ca1Seric 					  (char *)&on, sizeof on);
69852308a50Seric 		}
69987d6e633Srick 		if (CurEnv->e_xfp != NULL)
700877a6142Seric 			(void) fflush(CurEnv->e_xfp);		/* for debugging */
7014bd6a662Seric 		errno = 0;					/* for debugging */
702e2f2f828Seric 		if (connect(s, (struct sockaddr *) &addr, addrlen) >= 0)
703aea02ca1Seric 			break;
704aea02ca1Seric 
705aea02ca1Seric 		/* couldn't connect.... figure out why */
7066286bb75Sbloom 		sav_errno = errno;
7076286bb75Sbloom 		(void) close(s);
70804344589Sbloom 		if (hp && hp->h_addr_list[i])
70904344589Sbloom 		{
710aea02ca1Seric 			if (tTd(16, 1))
711e2f2f828Seric 				printf("Connect failed (%s); trying new address....\n",
712e2f2f828Seric 					errstring(sav_errno));
71383c1f4bcSeric 			switch (addr.sa.sa_family)
71483c1f4bcSeric 			{
71583c1f4bcSeric #ifdef NETINET
71683c1f4bcSeric 			  case AF_INET:
717e2f2f828Seric 				bcopy(hp->h_addr_list[i++],
71883c1f4bcSeric 				      &addr.sin.sin_addr,
719e2f2f828Seric 				      hp->h_length);
72083c1f4bcSeric 				break;
72183c1f4bcSeric #endif
72283c1f4bcSeric 
72383c1f4bcSeric 			  default:
724e2f2f828Seric 				bcopy(hp->h_addr_list[i++],
72583c1f4bcSeric 					addr.sa.sa_data,
726914346b1Seric 					hp->h_length);
72783c1f4bcSeric 				break;
72883c1f4bcSeric 			}
729aea02ca1Seric 			continue;
73004344589Sbloom 		}
73104344589Sbloom 
7327aa493c5Seric 		/* failure, decide if temporary or not */
7337aa493c5Seric 	failure:
734244b09d1Seric #ifdef XLA
735244b09d1Seric 		xla_host_end(host);
736244b09d1Seric #endif
737e2de2524Seric 		if (transienterror(sav_errno))
738e2de2524Seric 			return EX_TEMPFAIL;
739e2de2524Seric 		else
74087d6e633Srick 		{
74108b25121Seric 			message("%s", errstring(sav_errno));
7427aa493c5Seric 			return (EX_UNAVAILABLE);
7437aa493c5Seric 		}
7447aa493c5Seric 	}
7457aa493c5Seric 
7467aa493c5Seric 	/* connection ok, put it into canonical form */
747335eae58Seric 	if ((mci->mci_out = fdopen(s, "w")) == NULL ||
748335eae58Seric 	    (s = dup(s)) < 0 ||
749*ab81ee53Seric 	    (mci->mci_in = fdopen(s, "r")) == NULL)
750335eae58Seric 	{
751335eae58Seric 		syserr("cannot open SMTP client channel, fd=%d", s);
752335eae58Seric 		return EX_TEMPFAIL;
753335eae58Seric 	}
7547aa493c5Seric 
755dca8e1f7Seric 	return (EX_OK);
7567aa493c5Seric }
757444eaf03Seric /*
758444eaf03Seric **  MYHOSTNAME -- return the name of this host.
759444eaf03Seric **
760444eaf03Seric **	Parameters:
761444eaf03Seric **		hostbuf -- a place to return the name of this host.
762897f1869Seric **		size -- the size of hostbuf.
763444eaf03Seric **
764444eaf03Seric **	Returns:
765444eaf03Seric **		A list of aliases for this host.
766444eaf03Seric **
767444eaf03Seric **	Side Effects:
768d8d0a4aeSeric **		Adds numeric codes to $=w.
769444eaf03Seric */
770444eaf03Seric 
771444eaf03Seric char **
772897f1869Seric myhostname(hostbuf, size)
773444eaf03Seric 	char hostbuf[];
774897f1869Seric 	int size;
775444eaf03Seric {
77638ad259dSeric 	register struct hostent *hp;
777444eaf03Seric 	extern struct hostent *gethostbyname();
778444eaf03Seric 
779af5e902cSeric 	if (gethostname(hostbuf, size) < 0)
780af5e902cSeric 	{
781af5e902cSeric 		(void) strcpy(hostbuf, "localhost");
782af5e902cSeric 	}
783a44d5a5eSeric 	hp = gethostbyname(hostbuf);
784a44d5a5eSeric 	if (hp != NULL)
7857364df9fSeric 	{
78638ad259dSeric 		(void) strncpy(hostbuf, hp->h_name, size - 1);
78738ad259dSeric 		hostbuf[size - 1] = '\0';
78838ad259dSeric 
78938ad259dSeric 		if (hp->h_addrtype == AF_INET && hp->h_length == 4)
79038ad259dSeric 		{
79138ad259dSeric 			register int i;
79238ad259dSeric 
793d8d0a4aeSeric 			for (i = 0; hp->h_addr_list[i] != NULL; i++)
79438ad259dSeric 			{
795d8d0a4aeSeric 				char ipbuf[100];
796d8d0a4aeSeric 
797d8d0a4aeSeric 				sprintf(ipbuf, "[%s]",
798d8d0a4aeSeric 					inet_ntoa(*((struct in_addr *) hp->h_addr_list[i])));
799d8d0a4aeSeric 				setclass('w', ipbuf);
80038ad259dSeric 			}
80138ad259dSeric 		}
80238ad259dSeric 
803a44d5a5eSeric 		return (hp->h_aliases);
8047364df9fSeric 	}
805444eaf03Seric 	else
806444eaf03Seric 		return (NULL);
807444eaf03Seric }
808cb452edcSeric /*
8099f8b0eadSeric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
8109f8b0eadSeric **
8119f8b0eadSeric **	Uses RFC1413 protocol to try to get info from the other end.
812320e0d1cSeric **
813320e0d1cSeric **	Parameters:
814320e0d1cSeric **		fd -- the descriptor
815320e0d1cSeric **
816320e0d1cSeric **	Returns:
8179f8b0eadSeric **		The user@host information associated with this descriptor.
818320e0d1cSeric **
819320e0d1cSeric **	Side Effects:
8209f8b0eadSeric **		Sets RealHostName to the name of the host at the other end.
821320e0d1cSeric */
822320e0d1cSeric 
8239f8b0eadSeric #ifdef IDENTPROTO
8249f8b0eadSeric 
8259f8b0eadSeric static jmp_buf	CtxAuthTimeout;
8269f8b0eadSeric 
8279f8b0eadSeric static
8289f8b0eadSeric authtimeout()
8299f8b0eadSeric {
8309f8b0eadSeric 	longjmp(CtxAuthTimeout, 1);
8319f8b0eadSeric }
8329f8b0eadSeric 
8339f8b0eadSeric #endif
8349f8b0eadSeric 
835320e0d1cSeric char *
8369f8b0eadSeric getauthinfo(fd)
837320e0d1cSeric 	int fd;
838320e0d1cSeric {
8399f8b0eadSeric 	SOCKADDR fa;
8409f8b0eadSeric 	int falen;
841a5546e24Seric 	register char *p;
8429f8b0eadSeric #ifdef IDENTPROTO
8439f8b0eadSeric 	SOCKADDR la;
8449f8b0eadSeric 	int lalen;
8459f8b0eadSeric 	register struct servent *sp;
8469f8b0eadSeric 	int s;
8479f8b0eadSeric 	int i;
8489f8b0eadSeric 	EVENT *ev;
8499f8b0eadSeric #endif
8509f8b0eadSeric 	static char hbuf[MAXNAME * 2 + 2];
8519f8b0eadSeric 	extern char *hostnamebyanyaddr();
8529f8b0eadSeric 	extern char RealUserName[];			/* main.c */
853320e0d1cSeric 
8549f8b0eadSeric 	falen = sizeof fa;
8559f8b0eadSeric 	if (getpeername(fd, &fa.sa, &falen) < 0 || falen <= 0)
8569f8b0eadSeric 	{
8579f8b0eadSeric 		RealHostName = "localhost";
8589f8b0eadSeric 		(void) sprintf(hbuf, "%s@localhost", RealUserName);
85953853673Seric 		if (tTd(9, 1))
8609f8b0eadSeric 			printf("getauthinfo: %s\n", hbuf);
861320e0d1cSeric 		return hbuf;
862320e0d1cSeric 	}
8639f8b0eadSeric 
864da662164Seric 	p = hostnamebyanyaddr(&fa);
865da662164Seric 	RealHostName = newstr(p);
8669f8b0eadSeric 	RealHostAddr = fa;
8679f8b0eadSeric 
8689f8b0eadSeric #ifdef IDENTPROTO
8699f8b0eadSeric 	lalen = sizeof la;
8709f8b0eadSeric 	if (fa.sa.sa_family != AF_INET ||
8719f8b0eadSeric 	    getsockname(fd, &la.sa, &lalen) < 0 || lalen <= 0 ||
8729f8b0eadSeric 	    la.sa.sa_family != AF_INET)
8739f8b0eadSeric 	{
8749f8b0eadSeric 		/* no ident info */
8759f8b0eadSeric 		goto noident;
8769f8b0eadSeric 	}
8779f8b0eadSeric 
8789f8b0eadSeric 	/* create ident query */
879f2d880b6Seric 	(void) sprintf(hbuf, "%d,%d\r\n",
880f2d880b6Seric 		ntohs(fa.sin.sin_port), ntohs(la.sin.sin_port));
8819f8b0eadSeric 
8829f8b0eadSeric 	/* create local address */
8839f8b0eadSeric 	bzero(&la, sizeof la);
8849f8b0eadSeric 
8859f8b0eadSeric 	/* create foreign address */
8869f8b0eadSeric 	sp = getservbyname("auth", "tcp");
8879f8b0eadSeric 	if (sp != NULL)
8889f8b0eadSeric 		fa.sin.sin_port = sp->s_port;
8899f8b0eadSeric 	else
8901038598cSeric 		fa.sin.sin_port = htons(113);
8919f8b0eadSeric 
8929f8b0eadSeric 	s = -1;
8939f8b0eadSeric 	if (setjmp(CtxAuthTimeout) != 0)
8949f8b0eadSeric 	{
8959f8b0eadSeric 		if (s >= 0)
8969f8b0eadSeric 			(void) close(s);
8979f8b0eadSeric 		goto noident;
8989f8b0eadSeric 	}
8999f8b0eadSeric 
9009f8b0eadSeric 	/* put a timeout around the whole thing */
901a0f780efSeric 	ev = setevent(TimeOuts.to_ident, authtimeout, 0);
9029f8b0eadSeric 
9039f8b0eadSeric 	/* connect to foreign IDENT server */
9049f8b0eadSeric 	s = socket(AF_INET, SOCK_STREAM, 0);
9059f8b0eadSeric 	if (s < 0)
9069f8b0eadSeric 	{
9079f8b0eadSeric 		clrevent(ev);
9089f8b0eadSeric 		goto noident;
9099f8b0eadSeric 	}
9109f8b0eadSeric 	if (connect(s, &fa.sa, sizeof fa.sin) < 0)
9119f8b0eadSeric 	{
9129f8b0eadSeric closeident:
9139f8b0eadSeric 		(void) close(s);
9149f8b0eadSeric 		clrevent(ev);
9159f8b0eadSeric 		goto noident;
9169f8b0eadSeric 	}
9179f8b0eadSeric 
91853853673Seric 	if (tTd(9, 10))
9199f8b0eadSeric 		printf("getauthinfo: sent %s", hbuf);
9209f8b0eadSeric 
9219f8b0eadSeric 	/* send query */
9229f8b0eadSeric 	if (write(s, hbuf, strlen(hbuf)) < 0)
9239f8b0eadSeric 		goto closeident;
9249f8b0eadSeric 
9259f8b0eadSeric 	/* get result */
9269f8b0eadSeric 	i = read(s, hbuf, sizeof hbuf);
9279f8b0eadSeric 	(void) close(s);
9289f8b0eadSeric 	clrevent(ev);
9299f8b0eadSeric 	if (i <= 0)
9309f8b0eadSeric 		goto noident;
9319f8b0eadSeric 	if (hbuf[--i] == '\n' && hbuf[--i] == '\r')
9329f8b0eadSeric 		i--;
9339f8b0eadSeric 	hbuf[++i] = '\0';
9349f8b0eadSeric 
93553853673Seric 	if (tTd(9, 3))
9369f8b0eadSeric 		printf("getauthinfo:  got %s\n", hbuf);
9379f8b0eadSeric 
9389f8b0eadSeric 	/* parse result */
9399f8b0eadSeric 	p = strchr(hbuf, ':');
9409f8b0eadSeric 	if (p == NULL)
9419f8b0eadSeric 	{
9429f8b0eadSeric 		/* malformed response */
9439f8b0eadSeric 		goto noident;
9449f8b0eadSeric 	}
9459f8b0eadSeric 	while (isascii(*++p) && isspace(*p))
9469f8b0eadSeric 		continue;
9479f8b0eadSeric 	if (strncasecmp(p, "userid", 6) != 0)
9489f8b0eadSeric 	{
9499f8b0eadSeric 		/* presumably an error string */
9509f8b0eadSeric 		goto noident;
9519f8b0eadSeric 	}
9529f8b0eadSeric 	p += 6;
9539f8b0eadSeric 	while (isascii(*p) && isspace(*p))
9549f8b0eadSeric 		p++;
9559f8b0eadSeric 	if (*p++ != ':')
9569f8b0eadSeric 	{
9579f8b0eadSeric 		/* either useridxx or malformed response */
9589f8b0eadSeric 		goto noident;
9599f8b0eadSeric 	}
9609f8b0eadSeric 
9619f8b0eadSeric 	/* p now points to the OSTYPE field */
9629f8b0eadSeric 	p = strchr(p, ':');
9639f8b0eadSeric 	if (p == NULL)
9649f8b0eadSeric 	{
9659f8b0eadSeric 		/* malformed response */
9669f8b0eadSeric 		goto noident;
9679f8b0eadSeric 	}
96853853673Seric 
96953853673Seric 	/* 1413 says don't do this -- but it's broken otherwise */
97053853673Seric 	while (isascii(*++p) && isspace(*p))
97153853673Seric 		continue;
9729f8b0eadSeric 
9739f8b0eadSeric 	/* p now points to the authenticated name */
9749f8b0eadSeric 	(void) sprintf(hbuf, "%s@%s", p, RealHostName);
97553853673Seric 	goto finish;
97653853673Seric 
97753853673Seric #endif /* IDENTPROTO */
97853853673Seric 
97953853673Seric noident:
98053853673Seric 	(void) strcpy(hbuf, RealHostName);
98153853673Seric 
98253853673Seric finish:
9839f8b0eadSeric 	if (RealHostName[0] != '[')
9849f8b0eadSeric 	{
9859f8b0eadSeric 		p = &hbuf[strlen(hbuf)];
9869f8b0eadSeric 		(void) sprintf(p, " [%s]", anynet_ntoa(&RealHostAddr));
9879f8b0eadSeric 	}
98853853673Seric 	if (tTd(9, 1))
9899f8b0eadSeric 		printf("getauthinfo: %s\n", hbuf);
9909f8b0eadSeric 	return hbuf;
9919f8b0eadSeric }
992320e0d1cSeric /*
99308de856eSeric **  HOST_MAP_LOOKUP -- turn a hostname into canonical form
99415d084d5Seric **
99515d084d5Seric **	Parameters:
99605b57da8Seric **		map -- a pointer to this map (unused).
99708de856eSeric **		name -- the (presumably unqualified) hostname.
99800b385a9Seric **		av -- unused -- for compatibility with other mapping
999d798a1deSeric **			functions.
10002d29d43aSeric **		statp -- an exit status (out parameter) -- set to
10012d29d43aSeric **			EX_TEMPFAIL if the name server is unavailable.
100215d084d5Seric **
100315d084d5Seric **	Returns:
100415d084d5Seric **		The mapping, if found.
100515d084d5Seric **		NULL if no mapping found.
100615d084d5Seric **
100715d084d5Seric **	Side Effects:
100815d084d5Seric **		Looks up the host specified in hbuf.  If it is not
100915d084d5Seric **		the canonical name for that host, return the canonical
101015d084d5Seric **		name.
1011f36ede03Sbostic */
1012cb452edcSeric 
101315d084d5Seric char *
101400b385a9Seric host_map_lookup(map, name, av, statp)
101505b57da8Seric 	MAP *map;
101608de856eSeric 	char *name;
101700b385a9Seric 	char **av;
10182d29d43aSeric 	int *statp;
101999f7cf32Seric {
102099f7cf32Seric 	register struct hostent *hp;
10215f78836eSmiriam 	u_long in_addr;
102205b57da8Seric 	char *cp;
102338ad259dSeric 	int i;
1024eea91d78Seric 	register STAB *s;
102500b385a9Seric 	char hbuf[MAXNAME];
1026eea91d78Seric 	extern struct hostent *gethostbyaddr();
1027eea91d78Seric 	extern int h_errno;
10285f78836eSmiriam 
1029f36ede03Sbostic 	/*
1030eea91d78Seric 	**  See if we have already looked up this name.  If so, just
1031eea91d78Seric 	**  return it.
1032eea91d78Seric 	*/
1033eea91d78Seric 
103408de856eSeric 	s = stab(name, ST_NAMECANON, ST_ENTER);
1035eea91d78Seric 	if (bitset(NCF_VALID, s->s_namecanon.nc_flags))
1036eea91d78Seric 	{
1037f92c3297Seric 		if (tTd(9, 1))
103808de856eSeric 			printf("host_map_lookup(%s) => CACHE %s\n",
103908de856eSeric 				name, s->s_namecanon.nc_cname);
1040eea91d78Seric 		errno = s->s_namecanon.nc_errno;
1041eea91d78Seric 		h_errno = s->s_namecanon.nc_herrno;
1042eea91d78Seric 		*statp = s->s_namecanon.nc_stat;
1043eea91d78Seric 		return s->s_namecanon.nc_cname;
1044eea91d78Seric 	}
1045eea91d78Seric 
1046eea91d78Seric 	/*
1047eea91d78Seric 	**  If first character is a bracket, then it is an address
1048eea91d78Seric 	**  lookup.  Address is copied into a temporary buffer to
104908de856eSeric 	**  strip the brackets and to preserve name if address is
1050eea91d78Seric 	**  unknown.
1051f36ede03Sbostic 	*/
105215d084d5Seric 
105308de856eSeric 	if (*name != '[')
105415d084d5Seric 	{
1055d798a1deSeric 		extern bool getcanonname();
1056d798a1deSeric 
10578cb4653dSeric 		if (tTd(9, 1))
105808de856eSeric 			printf("host_map_lookup(%s) => ", name);
1059eea91d78Seric 		s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
106008de856eSeric 		(void) strcpy(hbuf, name);
10611f2ff1a4Seric 		if (getcanonname(hbuf, sizeof hbuf - 1, TRUE))
10629040ec4fSeric 		{
10639040ec4fSeric 			if (tTd(9, 1))
10649040ec4fSeric 				printf("%s\n", hbuf);
106500b385a9Seric 			cp = map_rewrite(map, hbuf, strlen(hbuf), av);
106600b385a9Seric 			s->s_namecanon.nc_cname = newstr(cp);
106700b385a9Seric 			return cp;
10689040ec4fSeric 		}
106915d084d5Seric 		else
10709040ec4fSeric 		{
10712d29d43aSeric 			register struct hostent *hp;
10722d29d43aSeric 
10739040ec4fSeric 			if (tTd(9, 1))
10742d29d43aSeric 				printf("FAIL (%d)\n", h_errno);
1075eea91d78Seric 			s->s_namecanon.nc_errno = errno;
1076eea91d78Seric 			s->s_namecanon.nc_herrno = h_errno;
10772d29d43aSeric 			switch (h_errno)
10782d29d43aSeric 			{
10792d29d43aSeric 			  case TRY_AGAIN:
108089cb2793Seric 				if (UseNameServer)
10818820d51bSeric 				{
10828820d51bSeric 					char *msg = "Recipient domain nameserver timed out";
10838820d51bSeric 
10848820d51bSeric 					message(msg);
10858820d51bSeric 					if (CurEnv->e_message == NULL)
108698da658aSeric 						CurEnv->e_message = newstr(msg);
10878820d51bSeric 				}
10882d29d43aSeric 				*statp = EX_TEMPFAIL;
10892d29d43aSeric 				break;
10902d29d43aSeric 
10912d29d43aSeric 			  case HOST_NOT_FOUND:
10922d29d43aSeric 				*statp = EX_NOHOST;
10932d29d43aSeric 				break;
10942d29d43aSeric 
10952d29d43aSeric 			  case NO_RECOVERY:
10962d29d43aSeric 				*statp = EX_SOFTWARE;
10972d29d43aSeric 				break;
10982d29d43aSeric 
10992d29d43aSeric 			  default:
11002d29d43aSeric 				*statp = EX_UNAVAILABLE;
11012d29d43aSeric 				break;
11022d29d43aSeric 			}
1103eea91d78Seric 			s->s_namecanon.nc_stat = *statp;
11042d29d43aSeric 			if (*statp != EX_TEMPFAIL || UseNameServer)
110515d084d5Seric 				return NULL;
11062d29d43aSeric 
11072d29d43aSeric 			/*
11082d29d43aSeric 			**  Try to look it up in /etc/hosts
11092d29d43aSeric 			*/
11102d29d43aSeric 
111108de856eSeric 			hp = gethostbyname(name);
11122d29d43aSeric 			if (hp == NULL)
11132d29d43aSeric 			{
11142d29d43aSeric 				/* no dice there either */
1115eea91d78Seric 				s->s_namecanon.nc_stat = *statp = EX_NOHOST;
11162d29d43aSeric 				return NULL;
11172d29d43aSeric 			}
11182d29d43aSeric 
1119eea91d78Seric 			s->s_namecanon.nc_stat = *statp = EX_OK;
112000b385a9Seric 			cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
112100b385a9Seric 			s->s_namecanon.nc_cname = newstr(cp);
112200b385a9Seric 			return cp;
112315d084d5Seric 		}
11249040ec4fSeric 	}
112508de856eSeric 	if ((cp = strchr(name, ']')) == NULL)
112615d084d5Seric 		return (NULL);
112734e39927Sbostic 	*cp = '\0';
112808de856eSeric 	in_addr = inet_addr(&name[1]);
112938ad259dSeric 
113038ad259dSeric 	/* nope -- ask the name server */
113131601fa7Seric 	hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET);
1132eea91d78Seric 	s->s_namecanon.nc_errno = errno;
1133eea91d78Seric 	s->s_namecanon.nc_herrno = h_errno;
1134eea91d78Seric 	s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
11355f78836eSmiriam 	if (hp == NULL)
1136eea91d78Seric 	{
1137eea91d78Seric 		s->s_namecanon.nc_stat = *statp = EX_NOHOST;
113815d084d5Seric 		return (NULL);
1139eea91d78Seric 	}
114015d084d5Seric 
114138ad259dSeric 	/* found a match -- copy out */
114200b385a9Seric 	cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
1143eea91d78Seric 	s->s_namecanon.nc_stat = *statp = EX_OK;
114400b385a9Seric 	s->s_namecanon.nc_cname = newstr(cp);
114500b385a9Seric 	return cp;
114699f7cf32Seric }
1147e2f2f828Seric /*
1148e2f2f828Seric **  ANYNET_NTOA -- convert a network address to printable form.
1149e2f2f828Seric **
1150e2f2f828Seric **	Parameters:
1151e2f2f828Seric **		sap -- a pointer to a sockaddr structure.
1152e2f2f828Seric **
1153e2f2f828Seric **	Returns:
1154e2f2f828Seric **		A printable version of that sockaddr.
1155e2f2f828Seric */
1156e2f2f828Seric 
1157e2f2f828Seric char *
1158e2f2f828Seric anynet_ntoa(sap)
1159e2f2f828Seric 	register SOCKADDR *sap;
1160e2f2f828Seric {
1161e2f2f828Seric 	register char *bp;
1162e2f2f828Seric 	register char *ap;
1163e2f2f828Seric 	int l;
1164e2f2f828Seric 	static char buf[80];
1165e2f2f828Seric 
11668cb4653dSeric 	/* check for null/zero family */
11678cb4653dSeric 	if (sap == NULL)
11688cb4653dSeric 		return "NULLADDR";
11698cb4653dSeric 	if (sap->sa.sa_family == 0)
11708cb4653dSeric 		return "0";
11718cb4653dSeric 
117283c1f4bcSeric #ifdef NETINET
117383c1f4bcSeric 	if (sap->sa.sa_family == AF_INET)
1174e2f2f828Seric 		return inet_ntoa(((struct sockaddr_in *) sap)->sin_addr);
117583c1f4bcSeric #endif
1176e2f2f828Seric 
1177e2f2f828Seric 	/* unknown family -- just dump bytes */
117883c1f4bcSeric 	(void) sprintf(buf, "Family %d: ", sap->sa.sa_family);
1179e2f2f828Seric 	bp = &buf[strlen(buf)];
118083c1f4bcSeric 	ap = sap->sa.sa_data;
118183c1f4bcSeric 	for (l = sizeof sap->sa.sa_data; --l >= 0; )
1182e2f2f828Seric 	{
1183e2f2f828Seric 		(void) sprintf(bp, "%02x:", *ap++ & 0377);
1184e2f2f828Seric 		bp += 3;
1185e2f2f828Seric 	}
1186e2f2f828Seric 	*--bp = '\0';
1187e2f2f828Seric 	return buf;
1188e2f2f828Seric }
11899f8b0eadSeric /*
11909f8b0eadSeric **  HOSTNAMEBYANYADDR -- return name of host based on address
11919f8b0eadSeric **
11929f8b0eadSeric **	Parameters:
11939f8b0eadSeric **		sap -- SOCKADDR pointer
11949f8b0eadSeric **
11959f8b0eadSeric **	Returns:
11969f8b0eadSeric **		text representation of host name.
11979f8b0eadSeric **
11989f8b0eadSeric **	Side Effects:
11999f8b0eadSeric **		none.
12009f8b0eadSeric */
12019f8b0eadSeric 
12029f8b0eadSeric char *
12039f8b0eadSeric hostnamebyanyaddr(sap)
12049f8b0eadSeric 	register SOCKADDR *sap;
12059f8b0eadSeric {
12069f8b0eadSeric 	register struct hostent *hp;
12079f8b0eadSeric 
12083490b9dfSeric #ifdef NAMED_BIND
12093490b9dfSeric 	int saveretry;
12103490b9dfSeric 
12113490b9dfSeric 	/* shorten name server timeout to avoid higher level timeouts */
12123490b9dfSeric 	saveretry = _res.retry;
12133490b9dfSeric 	_res.retry = 3;
12143490b9dfSeric #endif /* NAMED_BIND */
12153490b9dfSeric 
12169f8b0eadSeric 	switch (sap->sa.sa_family)
12179f8b0eadSeric 	{
12189f8b0eadSeric #ifdef NETINET
12199f8b0eadSeric 	  case AF_INET:
12209f8b0eadSeric 		hp = gethostbyaddr((char *) &sap->sin.sin_addr,
12219f8b0eadSeric 			sizeof sap->sin.sin_addr,
12229f8b0eadSeric 			AF_INET);
12239f8b0eadSeric 		break;
12249f8b0eadSeric #endif
12259f8b0eadSeric 
12269f8b0eadSeric #ifdef NETISO
12279f8b0eadSeric 	  case AF_ISO:
12289f8b0eadSeric 		hp = gethostbyaddr((char *) &sap->siso.siso_addr,
12299f8b0eadSeric 			sizeof sap->siso.siso_addr,
12309f8b0eadSeric 			AF_ISO);
12319f8b0eadSeric 		break;
12329f8b0eadSeric #endif
12339f8b0eadSeric 
12349f8b0eadSeric 	  default:
12359f8b0eadSeric 		hp = gethostbyaddr(sap->sa.sa_data,
12369f8b0eadSeric 			   sizeof sap->sa.sa_data,
12379f8b0eadSeric 			   sap->sa.sa_family);
12389f8b0eadSeric 		break;
12399f8b0eadSeric 	}
12409f8b0eadSeric 
12413490b9dfSeric #ifdef NAMED_BIND
12423490b9dfSeric 	_res.retry = saveretry;
12433490b9dfSeric #endif /* NAMED_BIND */
12443490b9dfSeric 
12459f8b0eadSeric 	if (hp != NULL)
12469f8b0eadSeric 		return hp->h_name;
12479f8b0eadSeric 	else
12489f8b0eadSeric 	{
12499f8b0eadSeric 		/* produce a dotted quad */
12509f8b0eadSeric 		static char buf[512];
12519f8b0eadSeric 
12529f8b0eadSeric 		(void) sprintf(buf, "[%s]", anynet_ntoa(sap));
12539f8b0eadSeric 		return buf;
12549f8b0eadSeric 	}
12559f8b0eadSeric }
1256f36ede03Sbostic 
12576c2c3107Seric # else /* DAEMON */
125899f7cf32Seric /* code for systems without sophisticated networking */
1259444eaf03Seric 
1260444eaf03Seric /*
1261444eaf03Seric **  MYHOSTNAME -- stub version for case of no daemon code.
126221e9914dSeric **
126321e9914dSeric **	Can't convert to upper case here because might be a UUCP name.
1264897f1869Seric **
1265897f1869Seric **	Mark, you can change this to be anything you want......
1266444eaf03Seric */
1267444eaf03Seric 
1268444eaf03Seric char **
1269897f1869Seric myhostname(hostbuf, size)
1270444eaf03Seric 	char hostbuf[];
1271897f1869Seric 	int size;
1272444eaf03Seric {
1273444eaf03Seric 	register FILE *f;
1274444eaf03Seric 
1275444eaf03Seric 	hostbuf[0] = '\0';
1276444eaf03Seric 	f = fopen("/usr/include/whoami", "r");
1277444eaf03Seric 	if (f != NULL)
1278444eaf03Seric 	{
1279897f1869Seric 		(void) fgets(hostbuf, size, f);
1280444eaf03Seric 		fixcrlf(hostbuf, TRUE);
1281444eaf03Seric 		(void) fclose(f);
1282444eaf03Seric 	}
1283444eaf03Seric 	return (NULL);
1284444eaf03Seric }
128599f7cf32Seric /*
12869f8b0eadSeric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
1287320e0d1cSeric **
1288320e0d1cSeric **	Parameters:
1289320e0d1cSeric **		fd -- the descriptor
1290320e0d1cSeric **
1291320e0d1cSeric **	Returns:
1292320e0d1cSeric **		The host name associated with this descriptor, if it can
1293320e0d1cSeric **			be determined.
1294320e0d1cSeric **		NULL otherwise.
1295320e0d1cSeric **
1296320e0d1cSeric **	Side Effects:
1297320e0d1cSeric **		none
1298320e0d1cSeric */
1299320e0d1cSeric 
1300320e0d1cSeric char *
13019f8b0eadSeric getauthinfo(fd)
1302320e0d1cSeric 	int fd;
1303320e0d1cSeric {
1304320e0d1cSeric 	return NULL;
1305320e0d1cSeric }
1306320e0d1cSeric /*
130799f7cf32Seric **  MAPHOSTNAME -- turn a hostname into canonical form
130899f7cf32Seric **
130999f7cf32Seric **	Parameters:
131005b57da8Seric **		map -- a pointer to the database map.
131108de856eSeric **		name -- a buffer containing a hostname.
131215d084d5Seric **		avp -- a pointer to a (cf file defined) argument vector.
13132d29d43aSeric **		statp -- an exit status (out parameter).
131499f7cf32Seric **
131599f7cf32Seric **	Returns:
131615d084d5Seric **		mapped host name
1317cb452edcSeric **		FALSE otherwise.
131899f7cf32Seric **
131999f7cf32Seric **	Side Effects:
132008de856eSeric **		Looks up the host specified in name.  If it is not
132199f7cf32Seric **		the canonical name for that host, replace it with
132299f7cf32Seric **		the canonical name.  If the name is unknown, or it
132399f7cf32Seric **		is already the canonical name, leave it unchanged.
132499f7cf32Seric */
132599f7cf32Seric 
132699f7cf32Seric /*ARGSUSED*/
132715d084d5Seric char *
132808de856eSeric host_map_lookup(map, name, avp, statp)
132905b57da8Seric 	MAP *map;
133008de856eSeric 	char *name;
133115d084d5Seric 	char **avp;
13322d29d43aSeric 	char *statp;
133399f7cf32Seric {
13342d29d43aSeric 	register struct hostent *hp;
13352d29d43aSeric 
133608de856eSeric 	hp = gethostbyname(name);
13372d29d43aSeric 	if (hp != NULL)
13382d29d43aSeric 		return hp->h_name;
13392d29d43aSeric 	*statp = EX_NOHOST;
134015d084d5Seric 	return NULL;
134199f7cf32Seric }
134299f7cf32Seric 
13436c2c3107Seric #endif /* DAEMON */
1344