xref: /original-bsd/usr.sbin/sendmail/src/daemon.c (revision 00b385a9)
1939f5b94Sdist /*
20942ea6aSbostic  * Copyright (c) 1983 Eric P. Allman
3da1c6175Sbostic  * Copyright (c) 1988 Regents of the University of California.
4da1c6175Sbostic  * All rights reserved.
5da1c6175Sbostic  *
63bc94712Sbostic  * %sccs.include.redist.c%
7939f5b94Sdist  */
8939f5b94Sdist 
97aa493c5Seric #include <errno.h>
10a8c080f0Seric #include <signal.h>
116c05f684Sbostic #include "sendmail.h"
127fa39d90Seric 
13af5e902cSeric #ifndef lint
14da1c6175Sbostic #ifdef DAEMON
15*00b385a9Seric static char sccsid[] = "@(#)daemon.c	6.50 (Berkeley) 05/24/93 (with daemon mode)";
16d0a9e852Seric #else
17*00b385a9Seric static char sccsid[] = "@(#)daemon.c	6.50 (Berkeley) 05/24/93 (without daemon mode)";
18da1c6175Sbostic #endif
19da1c6175Sbostic #endif /* not lint */
20da1c6175Sbostic 
21da1c6175Sbostic #ifdef DAEMON
22d0a9e852Seric 
231c71e510Seric # include <netdb.h>
241e1663f7Swnj # include <sys/wait.h>
25af5e902cSeric # include <sys/time.h>
26d0a9e852Seric 
273490b9dfSeric #ifdef NAMED_BIND
283490b9dfSeric # include <arpa/nameser.h>
293490b9dfSeric # include <resolv.h>
303490b9dfSeric #endif
313490b9dfSeric 
327fa39d90Seric /*
337fa39d90Seric **  DAEMON.C -- routines to use when running as a daemon.
3447b12ae1Seric **
3547b12ae1Seric **	This entire file is highly dependent on the 4.2 BSD
3647b12ae1Seric **	interprocess communication primitives.  No attempt has
3747b12ae1Seric **	been made to make this file portable to Version 7,
3847b12ae1Seric **	Version 6, MPX files, etc.  If you should try such a
3947b12ae1Seric **	thing yourself, I recommend chucking the entire file
4047b12ae1Seric **	and starting from scratch.  Basic semantics are:
4147b12ae1Seric **
4247b12ae1Seric **	getrequests()
4347b12ae1Seric **		Opens a port and initiates a connection.
4447b12ae1Seric **		Returns in a child.  Must set InChannel and
4547b12ae1Seric **		OutChannel appropriately.
46b7d7afcbSeric **	clrdaemon()
47b7d7afcbSeric **		Close any open files associated with getting
48b7d7afcbSeric **		the connection; this is used when running the queue,
49b7d7afcbSeric **		etc., to avoid having extra file descriptors during
50b7d7afcbSeric **		the queue run and to avoid confusing the network
51b7d7afcbSeric **		code (if it cares).
52914346b1Seric **	makeconnection(host, port, outfile, infile, usesecureport)
5347b12ae1Seric **		Make a connection to the named host on the given
5447b12ae1Seric **		port.  Set *outfile and *infile to the files
5547b12ae1Seric **		appropriate for communication.  Returns zero on
5647b12ae1Seric **		success, else an exit status describing the
5747b12ae1Seric **		error.
5808de856eSeric **	host_map_lookup(map, hbuf, avp, pstat)
5905b57da8Seric **		Convert the entry in hbuf into a canonical form.
607fa39d90Seric */
61e2f2f828Seric 
62e2f2f828Seric extern char	*anynet_ntoa();
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 */
841c71e510Seric 
857fa39d90Seric getrequests()
867fa39d90Seric {
871c71e510Seric 	int t;
881c71e510Seric 	register struct servent *sp;
897868dfc2Seric 	int on = 1;
9015d084d5Seric 	bool refusingconnections = TRUE;
910aae1086Seric 	FILE *pidf;
929b100374Sbostic 	extern void reapchild();
93eb889047Seric 
94a8268164Seric 	/*
951c71e510Seric 	**  Set up the address for the mailer.
96eb889047Seric 	*/
97eb889047Seric 
98bfb80540Seric 	if (DaemonAddr.sin.sin_family == 0)
99bfb80540Seric 		DaemonAddr.sin.sin_family = AF_INET;
100bfb80540Seric 	if (DaemonAddr.sin.sin_addr.s_addr == 0)
101bfb80540Seric 		DaemonAddr.sin.sin_addr.s_addr = INADDR_ANY;
102bfb80540Seric 	if (DaemonAddr.sin.sin_port == 0)
103bfb80540Seric 	{
1041c71e510Seric 		sp = getservbyname("smtp", "tcp");
1051c71e510Seric 		if (sp == NULL)
106d0a9e852Seric 		{
107ad977999Seric 			syserr("554 service \"smtp\" unknown");
108a1961f2aSeric 			goto severe;
1091c71e510Seric 		}
110bfb80540Seric 		DaemonAddr.sin.sin_port = sp->s_port;
111bfb80540Seric 	}
1121c71e510Seric 
1131c71e510Seric 	/*
1141c71e510Seric 	**  Try to actually open the connection.
1151c71e510Seric 	*/
1161c71e510Seric 
1171c71e510Seric 	if (tTd(15, 1))
118bfb80540Seric 		printf("getrequests: port 0x%x\n", DaemonAddr.sin.sin_port);
1191c71e510Seric 
1201c71e510Seric 	/* get a socket for the SMTP connection */
12198e28903Seric 	DaemonSocket = socket(DaemonAddr.sa.sa_family, SOCK_STREAM, 0);
122b7d7afcbSeric 	if (DaemonSocket < 0)
1231c71e510Seric 	{
1241c71e510Seric 		/* probably another daemon already */
1251c71e510Seric 		syserr("getrequests: can't create socket");
1261c71e510Seric 	  severe:
127b0ba8827Seric # ifdef LOG
128b0ba8827Seric 		if (LogLevel > 0)
1290c034190Seric 			syslog(LOG_ALERT, "problem creating SMTP socket");
1306c2c3107Seric # endif /* LOG */
13147b12ae1Seric 		finis();
132d0a9e852Seric 	}
1331b6e4a15Seric 
1341b6e4a15Seric 	/* turn on network debugging? */
135a2ef5fa4Seric 	if (tTd(15, 101))
13652308a50Seric 		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on);
1371b6e4a15Seric 
1387868dfc2Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on);
1397868dfc2Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on);
1407868dfc2Seric 
14198e28903Seric 	switch (DaemonAddr.sa.sa_family)
14298e28903Seric 	{
14398e28903Seric # ifdef NETINET
14498e28903Seric 	  case AF_INET:
14598e28903Seric 		t = sizeof DaemonAddr.sin;
14698e28903Seric 		break;
14798e28903Seric # endif
14898e28903Seric 
14998e28903Seric # ifdef NETISO
15098e28903Seric 	  case AF_ISO:
15198e28903Seric 		t = sizeof DaemonAddr.siso;
15298e28903Seric 		break;
15398e28903Seric # endif
15498e28903Seric 
15598e28903Seric 	  default:
15698e28903Seric 		t = sizeof DaemonAddr;
15798e28903Seric 		break;
15898e28903Seric 	}
15998e28903Seric 
16098e28903Seric 	if (bind(DaemonSocket, &DaemonAddr.sa, t) < 0)
1611c71e510Seric 	{
1621c71e510Seric 		syserr("getrequests: cannot bind");
163b7d7afcbSeric 		(void) close(DaemonSocket);
1641c71e510Seric 		goto severe;
1651c71e510Seric 	}
1661c71e510Seric 
1671cd247eeSeric 	(void) signal(SIGCHLD, reapchild);
16852308a50Seric 
1690aae1086Seric 	/* write the pid to the log file for posterity */
1700aae1086Seric 	pidf = fopen(PidFile, "w");
1710aae1086Seric 	if (pidf != NULL)
1720aae1086Seric 	{
1730aae1086Seric 		fprintf(pidf, "%d\n", getpid());
1740aae1086Seric 		fclose(pidf);
1750aae1086Seric 	}
1760aae1086Seric 
1770aae1086Seric 
1781c71e510Seric 	if (tTd(15, 1))
179b7d7afcbSeric 		printf("getrequests: %d\n", DaemonSocket);
1801c71e510Seric 
1811c71e510Seric 	for (;;)
1821c71e510Seric 	{
1833a099713Seric 		register int pid;
184a44d5a5eSeric 		auto int lotherend;
18515d084d5Seric 		extern bool refuseconnections();
1863a099713Seric 
1873a099713Seric 		/* see if we are rejecting connections */
18815d084d5Seric 		CurrentLA = getla();
18915d084d5Seric 		if (refuseconnections())
1906775ec03Sbostic 		{
19115d084d5Seric 			if (!refusingconnections)
19215d084d5Seric 			{
19315d084d5Seric 				/* don't queue so peer will fail quickly */
19415d084d5Seric 				(void) listen(DaemonSocket, 0);
19515d084d5Seric 				refusingconnections = TRUE;
19615d084d5Seric 			}
19771e5e267Seric 			setproctitle("rejecting connections: load average: %d",
19871e5e267Seric 				CurrentLA);
1993a099713Seric 			sleep(5);
20015d084d5Seric 			continue;
20115d084d5Seric 		}
20215d084d5Seric 
20315d084d5Seric 		if (refusingconnections)
20415d084d5Seric 		{
20515d084d5Seric 			/* start listening again */
206bfc1eaf8Seric 			if (listen(DaemonSocket, ListenQueueSize) < 0)
20715d084d5Seric 			{
20815d084d5Seric 				syserr("getrequests: cannot listen");
20915d084d5Seric 				(void) close(DaemonSocket);
21015d084d5Seric 				goto severe;
21115d084d5Seric 			}
21215d084d5Seric 			setproctitle("accepting connections");
21315d084d5Seric 			refusingconnections = FALSE;
2146775ec03Sbostic 		}
215a44d5a5eSeric 
2161c71e510Seric 		/* wait for a connection */
2171c71e510Seric 		do
2181c71e510Seric 		{
2191c71e510Seric 			errno = 0;
2209f9a15b6Skarels 			lotherend = sizeof RealHostAddr;
2219b100374Sbostic 			t = accept(DaemonSocket,
2229b100374Sbostic 			    (struct sockaddr *)&RealHostAddr, &lotherend);
2231c71e510Seric 		} while (t < 0 && errno == EINTR);
2241c71e510Seric 		if (t < 0)
2251c71e510Seric 		{
2261c71e510Seric 			syserr("getrequests: accept");
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 		{
2499f8b0eadSeric 			extern char *hostnamebyanyaddr();
250a44d5a5eSeric 
251a8268164Seric 			/*
252a8268164Seric 			**  CHILD -- return to caller.
253a44d5a5eSeric 			**	Collect verified idea of sending host.
254a8268164Seric 			**	Verify calling user id if possible here.
255a8268164Seric 			*/
256a8268164Seric 
2571cd247eeSeric 			(void) signal(SIGCHLD, SIG_DFL);
258e17a3a5aSeric 			OpMode = MD_SMTP;
259779ac194Seric 
260a44d5a5eSeric 			/* determine host name */
2619f8b0eadSeric 			RealHostName = newstr(hostnamebyanyaddr(&RealHostAddr));
26229dcf4baSeric 
2632a6bc25bSeric #ifdef LOG
264845e533cSeric 			if (LogLevel > 10)
2652a6bc25bSeric 			{
2662a6bc25bSeric 				/* log connection information */
2672a6bc25bSeric 				syslog(LOG_INFO, "connect from %s (%s)",
2689f8b0eadSeric 					RealHostName, anynet_ntoa(&RealHostAddr));
2692a6bc25bSeric 			}
2702a6bc25bSeric #endif
2712a6bc25bSeric 
272244b09d1Seric 			(void) close(DaemonSocket);
273244b09d1Seric 			InChannel = fdopen(t, "r");
274244b09d1Seric 			OutChannel = fdopen(dup(t), "w");
275244b09d1Seric 
27629dcf4baSeric 			/* should we check for illegal connection here? XXX */
277e17a3a5aSeric #ifdef XLA
278e17a3a5aSeric 			if (!xla_host_ok(RealHostName))
279e17a3a5aSeric 			{
280244b09d1Seric 				message("421 Too many SMTP sessions for this host");
281e17a3a5aSeric 				exit(0);
282e17a3a5aSeric 			}
283e17a3a5aSeric #endif
284a44d5a5eSeric 
28561e4310fSeric 			if (tTd(15, 2))
286d0a9e852Seric 				printf("getreq: returning\n");
287a8268164Seric 			return;
288a8268164Seric 		}
289a8268164Seric 
2903c154354Seric 		/* close the port so that others will hang (for a while) */
2913c154354Seric 		(void) close(t);
2928e3e4b17Seric 	}
2933c154354Seric 	/*NOTREACHED*/
2943c154354Seric }
2958e3e4b17Seric /*
296b7d7afcbSeric **  CLRDAEMON -- reset the daemon connection
297b7d7afcbSeric **
298b7d7afcbSeric **	Parameters:
299b7d7afcbSeric **		none.
300b7d7afcbSeric **
301b7d7afcbSeric **	Returns:
302b7d7afcbSeric **		none.
303b7d7afcbSeric **
304b7d7afcbSeric **	Side Effects:
305b7d7afcbSeric **		releases any resources used by the passive daemon.
306b7d7afcbSeric */
307b7d7afcbSeric 
308b7d7afcbSeric clrdaemon()
309b7d7afcbSeric {
310b7d7afcbSeric 	if (DaemonSocket >= 0)
311b7d7afcbSeric 		(void) close(DaemonSocket);
312b7d7afcbSeric 	DaemonSocket = -1;
313b7d7afcbSeric }
314b7d7afcbSeric /*
315bfb80540Seric **  SETDAEMONOPTIONS -- set options for running the daemon
316bfb80540Seric **
317bfb80540Seric **	Parameters:
318bfb80540Seric **		p -- the options line.
319bfb80540Seric **
320bfb80540Seric **	Returns:
321bfb80540Seric **		none.
322bfb80540Seric */
323bfb80540Seric 
324bfb80540Seric setdaemonoptions(p)
325bfb80540Seric 	register char *p;
326bfb80540Seric {
327850144caSeric 	if (DaemonAddr.sa.sa_family == AF_UNSPEC)
328850144caSeric 		DaemonAddr.sa.sa_family = AF_INET;
329850144caSeric 
330bfb80540Seric 	while (p != NULL)
331bfb80540Seric 	{
332bfb80540Seric 		register char *f;
333bfb80540Seric 		register char *v;
334bfb80540Seric 
335bfb80540Seric 		while (isascii(*p) && isspace(*p))
336bfb80540Seric 			p++;
337bfb80540Seric 		if (*p == '\0')
338bfb80540Seric 			break;
339bfb80540Seric 		f = p;
340bfb80540Seric 		p = strchr(p, ',');
341bfb80540Seric 		if (p != NULL)
342bfb80540Seric 			*p++ = '\0';
343bfb80540Seric 		v = strchr(f, '=');
344bfb80540Seric 		if (v == NULL)
345bfb80540Seric 			continue;
346bfb80540Seric 		while (isascii(*++v) && isspace(*v))
347bfb80540Seric 			continue;
348bfb80540Seric 
349bfb80540Seric 		switch (*f)
350bfb80540Seric 		{
351850144caSeric 		  case 'F':		/* address family */
352850144caSeric 			if (isascii(*v) && isdigit(*v))
353850144caSeric 				DaemonAddr.sa.sa_family = atoi(v);
354850144caSeric #ifdef NETINET
355850144caSeric 			else if (strcasecmp(v, "inet") == 0)
356850144caSeric 				DaemonAddr.sa.sa_family = AF_INET;
357850144caSeric #endif
358850144caSeric #ifdef NETISO
359850144caSeric 			else if (strcasecmp(v, "iso") == 0)
360850144caSeric 				DaemonAddr.sa.sa_family = AF_ISO;
361850144caSeric #endif
362850144caSeric #ifdef NETNS
363850144caSeric 			else if (strcasecmp(v, "ns") == 0)
364850144caSeric 				DaemonAddr.sa.sa_family = AF_NS;
365850144caSeric #endif
366850144caSeric #ifdef NETX25
367850144caSeric 			else if (strcasecmp(v, "x.25") == 0)
368850144caSeric 				DaemonAddr.sa.sa_family = AF_CCITT;
369850144caSeric #endif
370850144caSeric 			else
371850144caSeric 				syserr("554 Unknown address family %s in Family=option", v);
372850144caSeric 			break;
373850144caSeric 
374850144caSeric 		  case 'A':		/* address */
375850144caSeric 			switch (DaemonAddr.sa.sa_family)
376850144caSeric 			{
377850144caSeric #ifdef NETINET
378850144caSeric 			  case AF_INET:
379850144caSeric 				if (isascii(*v) && isdigit(*v))
380850144caSeric 					DaemonAddr.sin.sin_addr.s_addr = inet_network(v);
381850144caSeric 				else
382850144caSeric 				{
383850144caSeric 					register struct netent *np;
384850144caSeric 
385850144caSeric 					np = getnetbyname(v);
386850144caSeric 					if (np == NULL)
387850144caSeric 						syserr("554 network \"%s\" unknown", v);
388850144caSeric 					else
389850144caSeric 						DaemonAddr.sin.sin_addr.s_addr = np->n_net;
390850144caSeric 				}
391850144caSeric 				break;
392850144caSeric #endif
393850144caSeric 
394850144caSeric 			  default:
395850144caSeric 				syserr("554 Address= option unsupported for family %d",
396850144caSeric 					DaemonAddr.sa.sa_family);
397850144caSeric 				break;
398850144caSeric 			}
399850144caSeric 			break;
400850144caSeric 
401bfb80540Seric 		  case 'P':		/* port */
402850144caSeric 			switch (DaemonAddr.sa.sa_family)
403850144caSeric 			{
404850144caSeric 				short port;
405850144caSeric 
406850144caSeric #ifdef NETINET
407850144caSeric 			  case AF_INET:
408bfb80540Seric 				if (isascii(*v) && isdigit(*v))
409bfb80540Seric 					DaemonAddr.sin.sin_port = atoi(v);
410bfb80540Seric 				else
411bfb80540Seric 				{
412bfb80540Seric 					register struct servent *sp;
413bfb80540Seric 
414bfb80540Seric 					sp = getservbyname(v, "tcp");
415bfb80540Seric 					if (sp == NULL)
416ad977999Seric 						syserr("554 service \"%s\" unknown", v);
417bfb80540Seric 					else
418bfb80540Seric 						DaemonAddr.sin.sin_port = sp->s_port;
419bfb80540Seric 				}
420bfb80540Seric 				break;
421850144caSeric #endif
422bfb80540Seric 
423850144caSeric #ifdef NETISO
424850144caSeric 			  case AF_ISO:
425850144caSeric 				/* assume two byte transport selector */
426bfb80540Seric 				if (isascii(*v) && isdigit(*v))
427850144caSeric 					port = atoi(v);
428bfb80540Seric 				else
429bfb80540Seric 				{
430850144caSeric 					register struct servent *sp;
431bfb80540Seric 
432850144caSeric 					sp = getservbyname(v, "tcp");
433850144caSeric 					if (sp == NULL)
434ad977999Seric 						syserr("554 service \"%s\" unknown", v);
435bfb80540Seric 					else
436850144caSeric 						port = sp->s_port;
437850144caSeric 				}
438850144caSeric 				bcopy((char *) &port, TSEL(&DaemonAddr.siso), 2);
439850144caSeric 				break;
440850144caSeric #endif
441850144caSeric 
442850144caSeric 			  default:
443850144caSeric 				syserr("554 Port= option unsupported for family %d",
444850144caSeric 					DaemonAddr.sa.sa_family);
445850144caSeric 				break;
446bfb80540Seric 			}
447bfb80540Seric 			break;
448bfc1eaf8Seric 
449bfc1eaf8Seric 		  case 'L':		/* listen queue size */
450bfc1eaf8Seric 			ListenQueueSize = atoi(v);
451bfc1eaf8Seric 			break;
452bfb80540Seric 		}
453bfb80540Seric 	}
454bfb80540Seric }
455bfb80540Seric /*
4567aa493c5Seric **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
4577aa493c5Seric **
4587aa493c5Seric **	Parameters:
4597aa493c5Seric **		host -- the name of the host.
46048ff0a9dSeric **		port -- the port number to connect to.
461655feedbSeric **		mci -- a pointer to the mail connection information
462655feedbSeric **			structure to be filled in.
463914346b1Seric **		usesecureport -- if set, use a low numbered (reserved)
464914346b1Seric **			port to provide some rudimentary authentication.
4657aa493c5Seric **
4667aa493c5Seric **	Returns:
4677aa493c5Seric **		An exit code telling whether the connection could be
4687aa493c5Seric **			made and if not why not.
4697aa493c5Seric **
4707aa493c5Seric **	Side Effects:
4717aa493c5Seric **		none.
4727aa493c5Seric */
4737aa493c5Seric 
474e2f2f828Seric SOCKADDR	CurHostAddr;		/* address of current host */
47571ff6caaSeric 
476b31e7f2bSeric int
477655feedbSeric makeconnection(host, port, mci, usesecureport)
4787aa493c5Seric 	char *host;
479210215eaSeric 	u_short port;
480b31e7f2bSeric 	register MCI *mci;
481914346b1Seric 	bool usesecureport;
4827aa493c5Seric {
48304344589Sbloom 	register int i, s;
48404344589Sbloom 	register struct hostent *hp = (struct hostent *)NULL;
485e2f2f828Seric 	SOCKADDR addr;
4866286bb75Sbloom 	int sav_errno;
487e2f2f828Seric 	int addrlen;
488134746fbSeric #ifdef NAMED_BIND
489134746fbSeric 	extern int h_errno;
490134746fbSeric #endif
4917aa493c5Seric 
4927aa493c5Seric 	/*
4937aa493c5Seric 	**  Set up the address for the mailer.
49471096d12Seric 	**	Accept "[a.b.c.d]" syntax for host name.
4957aa493c5Seric 	*/
4967aa493c5Seric 
497134746fbSeric #ifdef NAMED_BIND
498794bdbb9Smiriam 	h_errno = 0;
499134746fbSeric #endif
500794bdbb9Smiriam 	errno = 0;
501967778e2Seric 	bzero(&CurHostAddr, sizeof CurHostAddr);
502d945ebe8Seric 	CurHostName = host;
503794bdbb9Smiriam 
50471096d12Seric 	if (host[0] == '[')
50571096d12Seric 	{
506a44d5a5eSeric 		long hid;
5076c2c3107Seric 		register char *p = strchr(host, ']');
50871096d12Seric 
509a44d5a5eSeric 		if (p != NULL)
51071096d12Seric 		{
511a44d5a5eSeric 			*p = '\0';
5124d9c42c2Seric #ifdef NETINET
513a44d5a5eSeric 			hid = inet_addr(&host[1]);
514a7e21fe6Seric 			if (hid == -1)
5154d9c42c2Seric #endif
516a7e21fe6Seric 			{
517a7e21fe6Seric 				/* try it as a host name (avoid MX lookup) */
518a7e21fe6Seric 				hp = gethostbyname(&host[1]);
519a7e21fe6Seric 				*p = ']';
520a7e21fe6Seric 				goto gothostent;
521a7e21fe6Seric 			}
522a44d5a5eSeric 			*p = ']';
52371096d12Seric 		}
524a7e21fe6Seric 		if (p == NULL)
52571096d12Seric 		{
52608b25121Seric 			usrerr("553 Invalid numeric domain spec \"%s\"", host);
52771096d12Seric 			return (EX_NOHOST);
52871096d12Seric 		}
5294d9c42c2Seric #ifdef NETINET
5304d9c42c2Seric 		addr.sin.sin_family = AF_INET;		/*XXX*/
53183c1f4bcSeric 		addr.sin.sin_addr.s_addr = hid;
5324d9c42c2Seric #endif
53371096d12Seric 	}
5341c71e510Seric 	else
5351c71e510Seric 	{
53604344589Sbloom 		hp = gethostbyname(host);
537a7e21fe6Seric gothostent:
538794bdbb9Smiriam 		if (hp == NULL)
539794bdbb9Smiriam 		{
540134746fbSeric #ifdef NAMED_BIND
541794bdbb9Smiriam 			if (errno == ETIMEDOUT || h_errno == TRY_AGAIN)
54252308a50Seric 				return (EX_TEMPFAIL);
54382e5d8ddSeric 
544134746fbSeric 			/* if name server is specified, assume temp fail */
545134746fbSeric 			if (errno == ECONNREFUSED && UseNameServer)
546134746fbSeric 				return (EX_TEMPFAIL);
547134746fbSeric #endif
5487aa493c5Seric 			return (EX_NOHOST);
549794bdbb9Smiriam 		}
55083c1f4bcSeric 		addr.sa.sa_family = hp->h_addrtype;
55183c1f4bcSeric 		switch (hp->h_addrtype)
55283c1f4bcSeric 		{
55383c1f4bcSeric #ifdef NETINET
55483c1f4bcSeric 		  case AF_INET:
555e2f2f828Seric 			bcopy(hp->h_addr,
55683c1f4bcSeric 				&addr.sin.sin_addr,
557e2f2f828Seric 				hp->h_length);
55883c1f4bcSeric 			break;
55983c1f4bcSeric #endif
56083c1f4bcSeric 
56183c1f4bcSeric 		  default:
562e2f2f828Seric 			bcopy(hp->h_addr,
56383c1f4bcSeric 				addr.sa.sa_data,
564e2f2f828Seric 				hp->h_length);
56583c1f4bcSeric 			break;
56683c1f4bcSeric 		}
56704344589Sbloom 		i = 1;
5681c71e510Seric 	}
5691c71e510Seric 
5701c71e510Seric 	/*
5711c71e510Seric 	**  Determine the port number.
5721c71e510Seric 	*/
5731c71e510Seric 
574fd7c0790Seric 	if (port != 0)
575e2f2f828Seric 		port = htons(port);
576fd7c0790Seric 	else
5771c71e510Seric 	{
5781c71e510Seric 		register struct servent *sp = getservbyname("smtp", "tcp");
5791c71e510Seric 
5801c71e510Seric 		if (sp == NULL)
5811c71e510Seric 		{
582ad977999Seric 			syserr("554 makeconnection: service \"smtp\" unknown");
583845e533cSeric 			return (EX_OSERR);
5841c71e510Seric 		}
585e2f2f828Seric 		port = sp->s_port;
586e2f2f828Seric 	}
587e2f2f828Seric 
58883c1f4bcSeric 	switch (addr.sa.sa_family)
589e2f2f828Seric 	{
5904d9c42c2Seric #ifdef NETINET
591e2f2f828Seric 	  case AF_INET:
59283c1f4bcSeric 		addr.sin.sin_port = port;
593e2f2f828Seric 		addrlen = sizeof (struct sockaddr_in);
594e2f2f828Seric 		break;
5954d9c42c2Seric #endif
596e2f2f828Seric 
597e2f2f828Seric #ifdef NETISO
598e2f2f828Seric 	  case AF_ISO:
599e2f2f828Seric 		/* assume two byte transport selector */
600e2f2f828Seric 		bcopy((char *) &port, TSEL((struct sockaddr_iso *) &addr), 2);
601e2f2f828Seric 		addrlen = sizeof (struct sockaddr_iso);
602e2f2f828Seric 		break;
603e2f2f828Seric #endif
604e2f2f828Seric 
605e2f2f828Seric 	  default:
60683c1f4bcSeric 		syserr("Can't connect to address family %d", addr.sa.sa_family);
607e2f2f828Seric 		return (EX_NOHOST);
6081c71e510Seric 	}
6097aa493c5Seric 
6107aa493c5Seric 	/*
6117aa493c5Seric 	**  Try to actually open the connection.
6127aa493c5Seric 	*/
6137aa493c5Seric 
614e17a3a5aSeric #ifdef XLA
615e17a3a5aSeric 	/* if too many connections, don't bother trying */
616e17a3a5aSeric 	if (!xla_noqueue_ok(host))
617e17a3a5aSeric 		return EX_TEMPFAIL;
618e17a3a5aSeric #endif
619e17a3a5aSeric 
620aea02ca1Seric 	for (;;)
621aea02ca1Seric 	{
62261e4310fSeric 		if (tTd(16, 1))
623e2f2f828Seric 			printf("makeconnection (%s [%s])\n",
624e2f2f828Seric 				host, anynet_ntoa(&addr));
6257aa493c5Seric 
626226e3022Seric 		/* save for logging */
627226e3022Seric 		CurHostAddr = addr;
628226e3022Seric 
629914346b1Seric 		if (usesecureport)
630914346b1Seric 		{
631914346b1Seric 			int rport = IPPORT_RESERVED - 1;
632914346b1Seric 
633914346b1Seric 			s = rresvport(&rport);
634914346b1Seric 		}
635914346b1Seric 		else
636914346b1Seric 		{
637af5e902cSeric 			s = socket(AF_INET, SOCK_STREAM, 0);
638914346b1Seric 		}
6397aa493c5Seric 		if (s < 0)
6407aa493c5Seric 		{
6416286bb75Sbloom 			sav_errno = errno;
642914346b1Seric 			syserr("makeconnection: no socket");
6437aa493c5Seric 			goto failure;
6447aa493c5Seric 		}
6457aa493c5Seric 
64661e4310fSeric 		if (tTd(16, 1))
647b31e7f2bSeric 			printf("makeconnection: fd=%d\n", s);
6481b6e4a15Seric 
6491b6e4a15Seric 		/* turn on network debugging? */
650a2ef5fa4Seric 		if (tTd(16, 101))
65152308a50Seric 		{
65252308a50Seric 			int on = 1;
653aea02ca1Seric 			(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG,
654aea02ca1Seric 					  (char *)&on, sizeof on);
65552308a50Seric 		}
65687d6e633Srick 		if (CurEnv->e_xfp != NULL)
657877a6142Seric 			(void) fflush(CurEnv->e_xfp);		/* for debugging */
6584bd6a662Seric 		errno = 0;					/* for debugging */
659e2f2f828Seric 		if (connect(s, (struct sockaddr *) &addr, addrlen) >= 0)
660aea02ca1Seric 			break;
661aea02ca1Seric 
662aea02ca1Seric 		/* couldn't connect.... figure out why */
6636286bb75Sbloom 		sav_errno = errno;
6646286bb75Sbloom 		(void) close(s);
66504344589Sbloom 		if (hp && hp->h_addr_list[i])
66604344589Sbloom 		{
667aea02ca1Seric 			if (tTd(16, 1))
668e2f2f828Seric 				printf("Connect failed (%s); trying new address....\n",
669e2f2f828Seric 					errstring(sav_errno));
67083c1f4bcSeric 			switch (addr.sa.sa_family)
67183c1f4bcSeric 			{
67283c1f4bcSeric #ifdef NETINET
67383c1f4bcSeric 			  case AF_INET:
674e2f2f828Seric 				bcopy(hp->h_addr_list[i++],
67583c1f4bcSeric 				      &addr.sin.sin_addr,
676e2f2f828Seric 				      hp->h_length);
67783c1f4bcSeric 				break;
67883c1f4bcSeric #endif
67983c1f4bcSeric 
68083c1f4bcSeric 			  default:
681e2f2f828Seric 				bcopy(hp->h_addr_list[i++],
68283c1f4bcSeric 					addr.sa.sa_data,
683914346b1Seric 					hp->h_length);
68483c1f4bcSeric 				break;
68583c1f4bcSeric 			}
686aea02ca1Seric 			continue;
68704344589Sbloom 		}
68804344589Sbloom 
6897aa493c5Seric 		/* failure, decide if temporary or not */
6907aa493c5Seric 	failure:
691244b09d1Seric #ifdef XLA
692244b09d1Seric 		xla_host_end(host);
693244b09d1Seric #endif
694e2de2524Seric 		if (transienterror(sav_errno))
695e2de2524Seric 			return EX_TEMPFAIL;
696e2de2524Seric 		else
69787d6e633Srick 		{
69808b25121Seric 			message("%s", errstring(sav_errno));
6997aa493c5Seric 			return (EX_UNAVAILABLE);
7007aa493c5Seric 		}
7017aa493c5Seric 	}
7027aa493c5Seric 
7037aa493c5Seric 	/* connection ok, put it into canonical form */
704655feedbSeric 	mci->mci_out = fdopen(s, "w");
705655feedbSeric 	mci->mci_in = fdopen(dup(s), "r");
7067aa493c5Seric 
707dca8e1f7Seric 	return (EX_OK);
7087aa493c5Seric }
709444eaf03Seric /*
710444eaf03Seric **  MYHOSTNAME -- return the name of this host.
711444eaf03Seric **
712444eaf03Seric **	Parameters:
713444eaf03Seric **		hostbuf -- a place to return the name of this host.
714897f1869Seric **		size -- the size of hostbuf.
715444eaf03Seric **
716444eaf03Seric **	Returns:
717444eaf03Seric **		A list of aliases for this host.
718444eaf03Seric **
719444eaf03Seric **	Side Effects:
72038ad259dSeric **		Sets the MyIpAddrs buffer to a list of my IP addresses.
721444eaf03Seric */
722444eaf03Seric 
72338ad259dSeric struct in_addr	MyIpAddrs[MAXIPADDR + 1];
72438ad259dSeric 
725444eaf03Seric char **
726897f1869Seric myhostname(hostbuf, size)
727444eaf03Seric 	char hostbuf[];
728897f1869Seric 	int size;
729444eaf03Seric {
73038ad259dSeric 	register struct hostent *hp;
731444eaf03Seric 	extern struct hostent *gethostbyname();
732444eaf03Seric 
733af5e902cSeric 	if (gethostname(hostbuf, size) < 0)
734af5e902cSeric 	{
735af5e902cSeric 		(void) strcpy(hostbuf, "localhost");
736af5e902cSeric 	}
737a44d5a5eSeric 	hp = gethostbyname(hostbuf);
738a44d5a5eSeric 	if (hp != NULL)
7397364df9fSeric 	{
74038ad259dSeric 		(void) strncpy(hostbuf, hp->h_name, size - 1);
74138ad259dSeric 		hostbuf[size - 1] = '\0';
74238ad259dSeric 
74338ad259dSeric 		if (hp->h_addrtype == AF_INET && hp->h_length == 4)
74438ad259dSeric 		{
74538ad259dSeric 			register int i;
74638ad259dSeric 
74738ad259dSeric 			for (i = 0; i < MAXIPADDR; i++)
74838ad259dSeric 			{
74938ad259dSeric 				if (hp->h_addr_list[i] == NULL)
75038ad259dSeric 					break;
75138ad259dSeric 				MyIpAddrs[i].s_addr = *(u_long *) hp->h_addr_list[i];
75238ad259dSeric 			}
75338ad259dSeric 			MyIpAddrs[i].s_addr = 0;
75438ad259dSeric 		}
75538ad259dSeric 
756a44d5a5eSeric 		return (hp->h_aliases);
7577364df9fSeric 	}
758444eaf03Seric 	else
759444eaf03Seric 		return (NULL);
760444eaf03Seric }
761cb452edcSeric /*
7629f8b0eadSeric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
7639f8b0eadSeric **
7649f8b0eadSeric **	Uses RFC1413 protocol to try to get info from the other end.
765320e0d1cSeric **
766320e0d1cSeric **	Parameters:
767320e0d1cSeric **		fd -- the descriptor
768320e0d1cSeric **
769320e0d1cSeric **	Returns:
7709f8b0eadSeric **		The user@host information associated with this descriptor.
771320e0d1cSeric **
772320e0d1cSeric **	Side Effects:
7739f8b0eadSeric **		Sets RealHostName to the name of the host at the other end.
774320e0d1cSeric */
775320e0d1cSeric 
7769f8b0eadSeric #ifdef IDENTPROTO
7779f8b0eadSeric 
7789f8b0eadSeric static jmp_buf	CtxAuthTimeout;
7799f8b0eadSeric 
7809f8b0eadSeric static
7819f8b0eadSeric authtimeout()
7829f8b0eadSeric {
7839f8b0eadSeric 	longjmp(CtxAuthTimeout, 1);
7849f8b0eadSeric }
7859f8b0eadSeric 
7869f8b0eadSeric #endif
7879f8b0eadSeric 
788320e0d1cSeric char *
7899f8b0eadSeric getauthinfo(fd)
790320e0d1cSeric 	int fd;
791320e0d1cSeric {
7929f8b0eadSeric 	SOCKADDR fa;
7939f8b0eadSeric 	int falen;
794a5546e24Seric 	register char *p;
7959f8b0eadSeric #ifdef IDENTPROTO
7969f8b0eadSeric 	SOCKADDR la;
7979f8b0eadSeric 	int lalen;
7989f8b0eadSeric 	register struct servent *sp;
7999f8b0eadSeric 	int s;
8009f8b0eadSeric 	int i;
8019f8b0eadSeric 	EVENT *ev;
8029f8b0eadSeric #endif
8039f8b0eadSeric 	static char hbuf[MAXNAME * 2 + 2];
8049f8b0eadSeric 	extern char *hostnamebyanyaddr();
8059f8b0eadSeric 	extern char RealUserName[];			/* main.c */
806320e0d1cSeric 
8079f8b0eadSeric 	falen = sizeof fa;
8089f8b0eadSeric 	if (getpeername(fd, &fa.sa, &falen) < 0 || falen <= 0)
8099f8b0eadSeric 	{
8109f8b0eadSeric 		RealHostName = "localhost";
8119f8b0eadSeric 		(void) sprintf(hbuf, "%s@localhost", RealUserName);
81253853673Seric 		if (tTd(9, 1))
8139f8b0eadSeric 			printf("getauthinfo: %s\n", hbuf);
814320e0d1cSeric 		return hbuf;
815320e0d1cSeric 	}
8169f8b0eadSeric 
8179f8b0eadSeric 	RealHostName = newstr(hostnamebyanyaddr(&fa));
8189f8b0eadSeric 	RealHostAddr = fa;
8199f8b0eadSeric 
8209f8b0eadSeric #ifdef IDENTPROTO
8219f8b0eadSeric 	lalen = sizeof la;
8229f8b0eadSeric 	if (fa.sa.sa_family != AF_INET ||
8239f8b0eadSeric 	    getsockname(fd, &la.sa, &lalen) < 0 || lalen <= 0 ||
8249f8b0eadSeric 	    la.sa.sa_family != AF_INET)
8259f8b0eadSeric 	{
8269f8b0eadSeric 		/* no ident info */
8279f8b0eadSeric 		goto noident;
8289f8b0eadSeric 	}
8299f8b0eadSeric 
8309f8b0eadSeric 	/* create ident query */
8319f8b0eadSeric 	(void) sprintf(hbuf, "%d,%d\r\n", fa.sin.sin_port, la.sin.sin_port);
8329f8b0eadSeric 
8339f8b0eadSeric 	/* create local address */
8349f8b0eadSeric 	bzero(&la, sizeof la);
8359f8b0eadSeric 
8369f8b0eadSeric 	/* create foreign address */
8379f8b0eadSeric 	sp = getservbyname("auth", "tcp");
8389f8b0eadSeric 	if (sp != NULL)
8399f8b0eadSeric 		fa.sin.sin_port = sp->s_port;
8409f8b0eadSeric 	else
8411038598cSeric 		fa.sin.sin_port = htons(113);
8429f8b0eadSeric 
8439f8b0eadSeric 	s = -1;
8449f8b0eadSeric 	if (setjmp(CtxAuthTimeout) != 0)
8459f8b0eadSeric 	{
8469f8b0eadSeric 		if (s >= 0)
8479f8b0eadSeric 			(void) close(s);
8489f8b0eadSeric 		goto noident;
8499f8b0eadSeric 	}
8509f8b0eadSeric 
8519f8b0eadSeric 	/* put a timeout around the whole thing */
8529f8b0eadSeric 	ev = setevent((time_t) 30, authtimeout, 0);
8539f8b0eadSeric 
8549f8b0eadSeric 	/* connect to foreign IDENT server */
8559f8b0eadSeric 	s = socket(AF_INET, SOCK_STREAM, 0);
8569f8b0eadSeric 	if (s < 0)
8579f8b0eadSeric 	{
8589f8b0eadSeric 		clrevent(ev);
8599f8b0eadSeric 		goto noident;
8609f8b0eadSeric 	}
8619f8b0eadSeric 	if (connect(s, &fa.sa, sizeof fa.sin) < 0)
8629f8b0eadSeric 	{
8639f8b0eadSeric closeident:
8649f8b0eadSeric 		(void) close(s);
8659f8b0eadSeric 		clrevent(ev);
8669f8b0eadSeric 		goto noident;
8679f8b0eadSeric 	}
8689f8b0eadSeric 
86953853673Seric 	if (tTd(9, 10))
8709f8b0eadSeric 		printf("getauthinfo: sent %s", hbuf);
8719f8b0eadSeric 
8729f8b0eadSeric 	/* send query */
8739f8b0eadSeric 	if (write(s, hbuf, strlen(hbuf)) < 0)
8749f8b0eadSeric 		goto closeident;
8759f8b0eadSeric 
8769f8b0eadSeric 	/* get result */
8779f8b0eadSeric 	i = read(s, hbuf, sizeof hbuf);
8789f8b0eadSeric 	(void) close(s);
8799f8b0eadSeric 	clrevent(ev);
8809f8b0eadSeric 	if (i <= 0)
8819f8b0eadSeric 		goto noident;
8829f8b0eadSeric 	if (hbuf[--i] == '\n' && hbuf[--i] == '\r')
8839f8b0eadSeric 		i--;
8849f8b0eadSeric 	hbuf[++i] = '\0';
8859f8b0eadSeric 
88653853673Seric 	if (tTd(9, 3))
8879f8b0eadSeric 		printf("getauthinfo:  got %s\n", hbuf);
8889f8b0eadSeric 
8899f8b0eadSeric 	/* parse result */
8909f8b0eadSeric 	p = strchr(hbuf, ':');
8919f8b0eadSeric 	if (p == NULL)
8929f8b0eadSeric 	{
8939f8b0eadSeric 		/* malformed response */
8949f8b0eadSeric 		goto noident;
8959f8b0eadSeric 	}
8969f8b0eadSeric 	while (isascii(*++p) && isspace(*p))
8979f8b0eadSeric 		continue;
8989f8b0eadSeric 	if (strncasecmp(p, "userid", 6) != 0)
8999f8b0eadSeric 	{
9009f8b0eadSeric 		/* presumably an error string */
9019f8b0eadSeric 		goto noident;
9029f8b0eadSeric 	}
9039f8b0eadSeric 	p += 6;
9049f8b0eadSeric 	while (isascii(*p) && isspace(*p))
9059f8b0eadSeric 		p++;
9069f8b0eadSeric 	if (*p++ != ':')
9079f8b0eadSeric 	{
9089f8b0eadSeric 		/* either useridxx or malformed response */
9099f8b0eadSeric 		goto noident;
9109f8b0eadSeric 	}
9119f8b0eadSeric 
9129f8b0eadSeric 	/* p now points to the OSTYPE field */
9139f8b0eadSeric 	p = strchr(p, ':');
9149f8b0eadSeric 	if (p == NULL)
9159f8b0eadSeric 	{
9169f8b0eadSeric 		/* malformed response */
9179f8b0eadSeric 		goto noident;
9189f8b0eadSeric 	}
91953853673Seric 
92053853673Seric 	/* 1413 says don't do this -- but it's broken otherwise */
92153853673Seric 	while (isascii(*++p) && isspace(*p))
92253853673Seric 		continue;
9239f8b0eadSeric 
9249f8b0eadSeric 	/* p now points to the authenticated name */
9259f8b0eadSeric 	(void) sprintf(hbuf, "%s@%s", p, RealHostName);
92653853673Seric 	goto finish;
92753853673Seric 
92853853673Seric #endif /* IDENTPROTO */
92953853673Seric 
93053853673Seric noident:
93153853673Seric 	(void) strcpy(hbuf, RealHostName);
93253853673Seric 
93353853673Seric finish:
9349f8b0eadSeric 	if (RealHostName[0] != '[')
9359f8b0eadSeric 	{
9369f8b0eadSeric 		p = &hbuf[strlen(hbuf)];
9379f8b0eadSeric 		(void) sprintf(p, " [%s]", anynet_ntoa(&RealHostAddr));
9389f8b0eadSeric 	}
93953853673Seric 	if (tTd(9, 1))
9409f8b0eadSeric 		printf("getauthinfo: %s\n", hbuf);
9419f8b0eadSeric 	return hbuf;
9429f8b0eadSeric }
943320e0d1cSeric /*
94408de856eSeric **  HOST_MAP_LOOKUP -- turn a hostname into canonical form
94515d084d5Seric **
94615d084d5Seric **	Parameters:
94705b57da8Seric **		map -- a pointer to this map (unused).
94808de856eSeric **		name -- the (presumably unqualified) hostname.
949*00b385a9Seric **		av -- unused -- for compatibility with other mapping
950d798a1deSeric **			functions.
9512d29d43aSeric **		statp -- an exit status (out parameter) -- set to
9522d29d43aSeric **			EX_TEMPFAIL if the name server is unavailable.
95315d084d5Seric **
95415d084d5Seric **	Returns:
95515d084d5Seric **		The mapping, if found.
95615d084d5Seric **		NULL if no mapping found.
95715d084d5Seric **
95815d084d5Seric **	Side Effects:
95915d084d5Seric **		Looks up the host specified in hbuf.  If it is not
96015d084d5Seric **		the canonical name for that host, return the canonical
96115d084d5Seric **		name.
962f36ede03Sbostic */
963cb452edcSeric 
96415d084d5Seric char *
965*00b385a9Seric host_map_lookup(map, name, av, statp)
96605b57da8Seric 	MAP *map;
96708de856eSeric 	char *name;
968*00b385a9Seric 	char **av;
9692d29d43aSeric 	int *statp;
97099f7cf32Seric {
97199f7cf32Seric 	register struct hostent *hp;
9725f78836eSmiriam 	u_long in_addr;
97305b57da8Seric 	char *cp;
97438ad259dSeric 	int i;
975eea91d78Seric 	register STAB *s;
976*00b385a9Seric 	char hbuf[MAXNAME];
977eea91d78Seric 	extern struct hostent *gethostbyaddr();
978eea91d78Seric 	extern int h_errno;
979*00b385a9Seric 	extern char *map_rewrite();
9805f78836eSmiriam 
981f36ede03Sbostic 	/*
982eea91d78Seric 	**  See if we have already looked up this name.  If so, just
983eea91d78Seric 	**  return it.
984eea91d78Seric 	*/
985eea91d78Seric 
98608de856eSeric 	s = stab(name, ST_NAMECANON, ST_ENTER);
987eea91d78Seric 	if (bitset(NCF_VALID, s->s_namecanon.nc_flags))
988eea91d78Seric 	{
989f92c3297Seric 		if (tTd(9, 1))
99008de856eSeric 			printf("host_map_lookup(%s) => CACHE %s\n",
99108de856eSeric 				name, s->s_namecanon.nc_cname);
992eea91d78Seric 		errno = s->s_namecanon.nc_errno;
993eea91d78Seric 		h_errno = s->s_namecanon.nc_herrno;
994eea91d78Seric 		*statp = s->s_namecanon.nc_stat;
995eea91d78Seric 		return s->s_namecanon.nc_cname;
996eea91d78Seric 	}
997eea91d78Seric 
998eea91d78Seric 	/*
999eea91d78Seric 	**  If first character is a bracket, then it is an address
1000eea91d78Seric 	**  lookup.  Address is copied into a temporary buffer to
100108de856eSeric 	**  strip the brackets and to preserve name if address is
1002eea91d78Seric 	**  unknown.
1003f36ede03Sbostic 	*/
100415d084d5Seric 
100508de856eSeric 	if (*name != '[')
100615d084d5Seric 	{
1007d798a1deSeric 		extern bool getcanonname();
1008d798a1deSeric 
10098cb4653dSeric 		if (tTd(9, 1))
101008de856eSeric 			printf("host_map_lookup(%s) => ", name);
1011eea91d78Seric 		s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
101208de856eSeric 		(void) strcpy(hbuf, name);
101308de856eSeric 		if (getcanonname(hbuf, sizeof hbuf - 1))
10149040ec4fSeric 		{
10159040ec4fSeric 			if (tTd(9, 1))
10169040ec4fSeric 				printf("%s\n", hbuf);
1017*00b385a9Seric 			cp = map_rewrite(map, hbuf, strlen(hbuf), av);
1018*00b385a9Seric 			s->s_namecanon.nc_cname = newstr(cp);
1019*00b385a9Seric 			return cp;
10209040ec4fSeric 		}
102115d084d5Seric 		else
10229040ec4fSeric 		{
10232d29d43aSeric 			register struct hostent *hp;
10242d29d43aSeric 
10259040ec4fSeric 			if (tTd(9, 1))
10262d29d43aSeric 				printf("FAIL (%d)\n", h_errno);
1027eea91d78Seric 			s->s_namecanon.nc_errno = errno;
1028eea91d78Seric 			s->s_namecanon.nc_herrno = h_errno;
10292d29d43aSeric 			switch (h_errno)
10302d29d43aSeric 			{
10312d29d43aSeric 			  case TRY_AGAIN:
103289cb2793Seric 				if (UseNameServer)
10338820d51bSeric 				{
10348820d51bSeric 					char *msg = "Recipient domain nameserver timed out";
10358820d51bSeric 
10368820d51bSeric 					message(msg);
10378820d51bSeric 					if (CurEnv->e_message == NULL)
103898da658aSeric 						CurEnv->e_message = newstr(msg);
10398820d51bSeric 				}
10402d29d43aSeric 				*statp = EX_TEMPFAIL;
10412d29d43aSeric 				break;
10422d29d43aSeric 
10432d29d43aSeric 			  case HOST_NOT_FOUND:
10442d29d43aSeric 				*statp = EX_NOHOST;
10452d29d43aSeric 				break;
10462d29d43aSeric 
10472d29d43aSeric 			  case NO_RECOVERY:
10482d29d43aSeric 				*statp = EX_SOFTWARE;
10492d29d43aSeric 				break;
10502d29d43aSeric 
10512d29d43aSeric 			  default:
10522d29d43aSeric 				*statp = EX_UNAVAILABLE;
10532d29d43aSeric 				break;
10542d29d43aSeric 			}
1055eea91d78Seric 			s->s_namecanon.nc_stat = *statp;
10562d29d43aSeric 			if (*statp != EX_TEMPFAIL || UseNameServer)
105715d084d5Seric 				return NULL;
10582d29d43aSeric 
10592d29d43aSeric 			/*
10602d29d43aSeric 			**  Try to look it up in /etc/hosts
10612d29d43aSeric 			*/
10622d29d43aSeric 
106308de856eSeric 			hp = gethostbyname(name);
10642d29d43aSeric 			if (hp == NULL)
10652d29d43aSeric 			{
10662d29d43aSeric 				/* no dice there either */
1067eea91d78Seric 				s->s_namecanon.nc_stat = *statp = EX_NOHOST;
10682d29d43aSeric 				return NULL;
10692d29d43aSeric 			}
10702d29d43aSeric 
1071eea91d78Seric 			s->s_namecanon.nc_stat = *statp = EX_OK;
1072*00b385a9Seric 			cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
1073*00b385a9Seric 			s->s_namecanon.nc_cname = newstr(cp);
1074*00b385a9Seric 			return cp;
107515d084d5Seric 		}
10769040ec4fSeric 	}
107708de856eSeric 	if ((cp = strchr(name, ']')) == NULL)
107815d084d5Seric 		return (NULL);
107934e39927Sbostic 	*cp = '\0';
108008de856eSeric 	in_addr = inet_addr(&name[1]);
108138ad259dSeric 
108238ad259dSeric 	/* check to see if this is one of our addresses */
108338ad259dSeric 	for (i = 0; MyIpAddrs[i].s_addr != 0; i++)
108438ad259dSeric 	{
108538ad259dSeric 		if (MyIpAddrs[i].s_addr == in_addr)
108638ad259dSeric 		{
1087*00b385a9Seric 			return map_rewrite(map, MyHostName, strlen(MyHostName), av);
108838ad259dSeric 		}
108938ad259dSeric 	}
109038ad259dSeric 
109138ad259dSeric 	/* nope -- ask the name server */
109231601fa7Seric 	hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET);
1093eea91d78Seric 	s->s_namecanon.nc_errno = errno;
1094eea91d78Seric 	s->s_namecanon.nc_herrno = h_errno;
1095eea91d78Seric 	s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
10965f78836eSmiriam 	if (hp == NULL)
1097eea91d78Seric 	{
1098eea91d78Seric 		s->s_namecanon.nc_stat = *statp = EX_NOHOST;
109915d084d5Seric 		return (NULL);
1100eea91d78Seric 	}
110115d084d5Seric 
110238ad259dSeric 	/* found a match -- copy out */
1103*00b385a9Seric 	cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
1104eea91d78Seric 	s->s_namecanon.nc_stat = *statp = EX_OK;
1105*00b385a9Seric 	s->s_namecanon.nc_cname = newstr(cp);
1106*00b385a9Seric 	return cp;
110799f7cf32Seric }
1108e2f2f828Seric /*
1109e2f2f828Seric **  ANYNET_NTOA -- convert a network address to printable form.
1110e2f2f828Seric **
1111e2f2f828Seric **	Parameters:
1112e2f2f828Seric **		sap -- a pointer to a sockaddr structure.
1113e2f2f828Seric **
1114e2f2f828Seric **	Returns:
1115e2f2f828Seric **		A printable version of that sockaddr.
1116e2f2f828Seric */
1117e2f2f828Seric 
1118e2f2f828Seric char *
1119e2f2f828Seric anynet_ntoa(sap)
1120e2f2f828Seric 	register SOCKADDR *sap;
1121e2f2f828Seric {
1122e2f2f828Seric 	register char *bp;
1123e2f2f828Seric 	register char *ap;
1124e2f2f828Seric 	int l;
1125e2f2f828Seric 	static char buf[80];
1126e2f2f828Seric 
11278cb4653dSeric 	/* check for null/zero family */
11288cb4653dSeric 	if (sap == NULL)
11298cb4653dSeric 		return "NULLADDR";
11308cb4653dSeric 	if (sap->sa.sa_family == 0)
11318cb4653dSeric 		return "0";
11328cb4653dSeric 
113383c1f4bcSeric #ifdef NETINET
113483c1f4bcSeric 	if (sap->sa.sa_family == AF_INET)
1135e2f2f828Seric 	{
1136e2f2f828Seric 		extern char *inet_ntoa();
1137e2f2f828Seric 
1138e2f2f828Seric 		return inet_ntoa(((struct sockaddr_in *) sap)->sin_addr);
1139e2f2f828Seric 	}
114083c1f4bcSeric #endif
1141e2f2f828Seric 
1142e2f2f828Seric 	/* unknown family -- just dump bytes */
114383c1f4bcSeric 	(void) sprintf(buf, "Family %d: ", sap->sa.sa_family);
1144e2f2f828Seric 	bp = &buf[strlen(buf)];
114583c1f4bcSeric 	ap = sap->sa.sa_data;
114683c1f4bcSeric 	for (l = sizeof sap->sa.sa_data; --l >= 0; )
1147e2f2f828Seric 	{
1148e2f2f828Seric 		(void) sprintf(bp, "%02x:", *ap++ & 0377);
1149e2f2f828Seric 		bp += 3;
1150e2f2f828Seric 	}
1151e2f2f828Seric 	*--bp = '\0';
1152e2f2f828Seric 	return buf;
1153e2f2f828Seric }
11549f8b0eadSeric /*
11559f8b0eadSeric **  HOSTNAMEBYANYADDR -- return name of host based on address
11569f8b0eadSeric **
11579f8b0eadSeric **	Parameters:
11589f8b0eadSeric **		sap -- SOCKADDR pointer
11599f8b0eadSeric **
11609f8b0eadSeric **	Returns:
11619f8b0eadSeric **		text representation of host name.
11629f8b0eadSeric **
11639f8b0eadSeric **	Side Effects:
11649f8b0eadSeric **		none.
11659f8b0eadSeric */
11669f8b0eadSeric 
11679f8b0eadSeric char *
11689f8b0eadSeric hostnamebyanyaddr(sap)
11699f8b0eadSeric 	register SOCKADDR *sap;
11709f8b0eadSeric {
11719f8b0eadSeric 	register struct hostent *hp;
11729f8b0eadSeric 
11733490b9dfSeric #ifdef NAMED_BIND
11743490b9dfSeric 	int saveretry;
11753490b9dfSeric 
11763490b9dfSeric 	/* shorten name server timeout to avoid higher level timeouts */
11773490b9dfSeric 	saveretry = _res.retry;
11783490b9dfSeric 	_res.retry = 3;
11793490b9dfSeric #endif /* NAMED_BIND */
11803490b9dfSeric 
11819f8b0eadSeric 	switch (sap->sa.sa_family)
11829f8b0eadSeric 	{
11839f8b0eadSeric #ifdef NETINET
11849f8b0eadSeric 	  case AF_INET:
11859f8b0eadSeric 		hp = gethostbyaddr((char *) &sap->sin.sin_addr,
11869f8b0eadSeric 			sizeof sap->sin.sin_addr,
11879f8b0eadSeric 			AF_INET);
11889f8b0eadSeric 		break;
11899f8b0eadSeric #endif
11909f8b0eadSeric 
11919f8b0eadSeric #ifdef NETISO
11929f8b0eadSeric 	  case AF_ISO:
11939f8b0eadSeric 		hp = gethostbyaddr((char *) &sap->siso.siso_addr,
11949f8b0eadSeric 			sizeof sap->siso.siso_addr,
11959f8b0eadSeric 			AF_ISO);
11969f8b0eadSeric 		break;
11979f8b0eadSeric #endif
11989f8b0eadSeric 
11999f8b0eadSeric 	  default:
12009f8b0eadSeric 		hp = gethostbyaddr(sap->sa.sa_data,
12019f8b0eadSeric 			   sizeof sap->sa.sa_data,
12029f8b0eadSeric 			   sap->sa.sa_family);
12039f8b0eadSeric 		break;
12049f8b0eadSeric 	}
12059f8b0eadSeric 
12063490b9dfSeric #ifdef NAMED_BIND
12073490b9dfSeric 	_res.retry = saveretry;
12083490b9dfSeric #endif /* NAMED_BIND */
12093490b9dfSeric 
12109f8b0eadSeric 	if (hp != NULL)
12119f8b0eadSeric 		return hp->h_name;
12129f8b0eadSeric 	else
12139f8b0eadSeric 	{
12149f8b0eadSeric 		/* produce a dotted quad */
12159f8b0eadSeric 		static char buf[512];
12169f8b0eadSeric 
12179f8b0eadSeric 		(void) sprintf(buf, "[%s]", anynet_ntoa(sap));
12189f8b0eadSeric 		return buf;
12199f8b0eadSeric 	}
12209f8b0eadSeric }
1221f36ede03Sbostic 
12226c2c3107Seric # else /* DAEMON */
122399f7cf32Seric /* code for systems without sophisticated networking */
1224444eaf03Seric 
1225444eaf03Seric /*
1226444eaf03Seric **  MYHOSTNAME -- stub version for case of no daemon code.
122721e9914dSeric **
122821e9914dSeric **	Can't convert to upper case here because might be a UUCP name.
1229897f1869Seric **
1230897f1869Seric **	Mark, you can change this to be anything you want......
1231444eaf03Seric */
1232444eaf03Seric 
1233444eaf03Seric char **
1234897f1869Seric myhostname(hostbuf, size)
1235444eaf03Seric 	char hostbuf[];
1236897f1869Seric 	int size;
1237444eaf03Seric {
1238444eaf03Seric 	register FILE *f;
1239444eaf03Seric 
1240444eaf03Seric 	hostbuf[0] = '\0';
1241444eaf03Seric 	f = fopen("/usr/include/whoami", "r");
1242444eaf03Seric 	if (f != NULL)
1243444eaf03Seric 	{
1244897f1869Seric 		(void) fgets(hostbuf, size, f);
1245444eaf03Seric 		fixcrlf(hostbuf, TRUE);
1246444eaf03Seric 		(void) fclose(f);
1247444eaf03Seric 	}
1248444eaf03Seric 	return (NULL);
1249444eaf03Seric }
125099f7cf32Seric /*
12519f8b0eadSeric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
1252320e0d1cSeric **
1253320e0d1cSeric **	Parameters:
1254320e0d1cSeric **		fd -- the descriptor
1255320e0d1cSeric **
1256320e0d1cSeric **	Returns:
1257320e0d1cSeric **		The host name associated with this descriptor, if it can
1258320e0d1cSeric **			be determined.
1259320e0d1cSeric **		NULL otherwise.
1260320e0d1cSeric **
1261320e0d1cSeric **	Side Effects:
1262320e0d1cSeric **		none
1263320e0d1cSeric */
1264320e0d1cSeric 
1265320e0d1cSeric char *
12669f8b0eadSeric getauthinfo(fd)
1267320e0d1cSeric 	int fd;
1268320e0d1cSeric {
1269320e0d1cSeric 	return NULL;
1270320e0d1cSeric }
1271320e0d1cSeric /*
127299f7cf32Seric **  MAPHOSTNAME -- turn a hostname into canonical form
127399f7cf32Seric **
127499f7cf32Seric **	Parameters:
127505b57da8Seric **		map -- a pointer to the database map.
127608de856eSeric **		name -- a buffer containing a hostname.
127715d084d5Seric **		avp -- a pointer to a (cf file defined) argument vector.
12782d29d43aSeric **		statp -- an exit status (out parameter).
127999f7cf32Seric **
128099f7cf32Seric **	Returns:
128115d084d5Seric **		mapped host name
1282cb452edcSeric **		FALSE otherwise.
128399f7cf32Seric **
128499f7cf32Seric **	Side Effects:
128508de856eSeric **		Looks up the host specified in name.  If it is not
128699f7cf32Seric **		the canonical name for that host, replace it with
128799f7cf32Seric **		the canonical name.  If the name is unknown, or it
128899f7cf32Seric **		is already the canonical name, leave it unchanged.
128999f7cf32Seric */
129099f7cf32Seric 
129199f7cf32Seric /*ARGSUSED*/
129215d084d5Seric char *
129308de856eSeric host_map_lookup(map, name, avp, statp)
129405b57da8Seric 	MAP *map;
129508de856eSeric 	char *name;
129615d084d5Seric 	char **avp;
12972d29d43aSeric 	char *statp;
129899f7cf32Seric {
12992d29d43aSeric 	register struct hostent *hp;
13002d29d43aSeric 
130108de856eSeric 	hp = gethostbyname(name);
13022d29d43aSeric 	if (hp != NULL)
13032d29d43aSeric 		return hp->h_name;
13042d29d43aSeric 	*statp = EX_NOHOST;
130515d084d5Seric 	return NULL;
130699f7cf32Seric }
130799f7cf32Seric 
13086c2c3107Seric #endif /* DAEMON */
1309