xref: /original-bsd/usr.sbin/sendmail/src/daemon.c (revision 9d4a8008)
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*9d4a8008Seric static char sccsid[] = "@(#)daemon.c	8.38 (Berkeley) 03/11/94 (with daemon mode)";
15d0a9e852Seric #else
16*9d4a8008Seric static char sccsid[] = "@(#)daemon.c	8.38 (Berkeley) 03/11/94 (without daemon mode)";
17da1c6175Sbostic #endif
18da1c6175Sbostic #endif /* not lint */
19da1c6175Sbostic 
20da1c6175Sbostic #ifdef DAEMON
21d0a9e852Seric 
221c71e510Seric # include <netdb.h>
23d8d0a4aeSeric # include <arpa/inet.h>
24d0a9e852Seric 
25*9d4a8008Seric #if NAMED_BIND
263490b9dfSeric # include <arpa/nameser.h>
273490b9dfSeric # include <resolv.h>
283490b9dfSeric #endif
293490b9dfSeric 
307fa39d90Seric /*
317fa39d90Seric **  DAEMON.C -- routines to use when running as a daemon.
3247b12ae1Seric **
3347b12ae1Seric **	This entire file is highly dependent on the 4.2 BSD
3447b12ae1Seric **	interprocess communication primitives.  No attempt has
3547b12ae1Seric **	been made to make this file portable to Version 7,
3647b12ae1Seric **	Version 6, MPX files, etc.  If you should try such a
3747b12ae1Seric **	thing yourself, I recommend chucking the entire file
3847b12ae1Seric **	and starting from scratch.  Basic semantics are:
3947b12ae1Seric **
4047b12ae1Seric **	getrequests()
4147b12ae1Seric **		Opens a port and initiates a connection.
4247b12ae1Seric **		Returns in a child.  Must set InChannel and
4347b12ae1Seric **		OutChannel appropriately.
44b7d7afcbSeric **	clrdaemon()
45b7d7afcbSeric **		Close any open files associated with getting
46b7d7afcbSeric **		the connection; this is used when running the queue,
47b7d7afcbSeric **		etc., to avoid having extra file descriptors during
48b7d7afcbSeric **		the queue run and to avoid confusing the network
49b7d7afcbSeric **		code (if it cares).
50914346b1Seric **	makeconnection(host, port, outfile, infile, usesecureport)
5147b12ae1Seric **		Make a connection to the named host on the given
5247b12ae1Seric **		port.  Set *outfile and *infile to the files
5347b12ae1Seric **		appropriate for communication.  Returns zero on
5447b12ae1Seric **		success, else an exit status describing the
5547b12ae1Seric **		error.
5608de856eSeric **	host_map_lookup(map, hbuf, avp, pstat)
5705b57da8Seric **		Convert the entry in hbuf into a canonical form.
587fa39d90Seric */
597fa39d90Seric /*
607fa39d90Seric **  GETREQUESTS -- open mail IPC port and get requests.
617fa39d90Seric **
627fa39d90Seric **	Parameters:
637fa39d90Seric **		none.
647fa39d90Seric **
657fa39d90Seric **	Returns:
667fa39d90Seric **		none.
677fa39d90Seric **
687fa39d90Seric **	Side Effects:
697fa39d90Seric **		Waits until some interesting activity occurs.  When
707fa39d90Seric **		it does, a child is created to process it, and the
717fa39d90Seric **		parent waits for completion.  Return from this
72147303b1Seric **		routine is always in the child.  The file pointers
73147303b1Seric **		"InChannel" and "OutChannel" should be set to point
74147303b1Seric **		to the communication channel.
757fa39d90Seric */
767fa39d90Seric 
77b7d7afcbSeric int		DaemonSocket	= -1;		/* fd describing socket */
78bfb80540Seric SOCKADDR	DaemonAddr;			/* socket for incoming */
79bfc1eaf8Seric int		ListenQueueSize = 10;		/* size of listen queue */
80b35447dbSeric int		TcpRcvBufferSize = 0;		/* size of TCP receive buffer */
81b35447dbSeric int		TcpSndBufferSize = 0;		/* size of TCP send buffer */
821c71e510Seric 
837fa39d90Seric getrequests()
847fa39d90Seric {
851c71e510Seric 	int t;
867868dfc2Seric 	int on = 1;
8715d084d5Seric 	bool refusingconnections = TRUE;
880aae1086Seric 	FILE *pidf;
89dadb8687Seric 	int socksize;
909b100374Sbostic 	extern void reapchild();
91eb889047Seric 
92a8268164Seric 	/*
931c71e510Seric 	**  Set up the address for the mailer.
94eb889047Seric 	*/
95eb889047Seric 
96bfb80540Seric 	if (DaemonAddr.sin.sin_family == 0)
97bfb80540Seric 		DaemonAddr.sin.sin_family = AF_INET;
98bfb80540Seric 	if (DaemonAddr.sin.sin_addr.s_addr == 0)
99bfb80540Seric 		DaemonAddr.sin.sin_addr.s_addr = INADDR_ANY;
100bfb80540Seric 	if (DaemonAddr.sin.sin_port == 0)
101bfb80540Seric 	{
102e5311662Seric 		register struct servent *sp;
103e5311662Seric 
1041c71e510Seric 		sp = getservbyname("smtp", "tcp");
1051c71e510Seric 		if (sp == NULL)
106d0a9e852Seric 		{
107ad977999Seric 			syserr("554 service \"smtp\" unknown");
108e5311662Seric 			DaemonAddr.sin.sin_port = htons(25);
1091c71e510Seric 		}
110e5311662Seric 		else
111bfb80540Seric 			DaemonAddr.sin.sin_port = sp->s_port;
112bfb80540Seric 	}
1131c71e510Seric 
1141c71e510Seric 	/*
1151c71e510Seric 	**  Try to actually open the connection.
1161c71e510Seric 	*/
1171c71e510Seric 
1181c71e510Seric 	if (tTd(15, 1))
119bfb80540Seric 		printf("getrequests: port 0x%x\n", DaemonAddr.sin.sin_port);
1201c71e510Seric 
1211c71e510Seric 	/* get a socket for the SMTP connection */
12298e28903Seric 	DaemonSocket = socket(DaemonAddr.sa.sa_family, SOCK_STREAM, 0);
123b7d7afcbSeric 	if (DaemonSocket < 0)
1241c71e510Seric 	{
1251c71e510Seric 		/* probably another daemon already */
1261c71e510Seric 		syserr("getrequests: can't create socket");
1271c71e510Seric 	  severe:
128b0ba8827Seric # ifdef LOG
129b0ba8827Seric 		if (LogLevel > 0)
1300c034190Seric 			syslog(LOG_ALERT, "problem creating SMTP socket");
1316c2c3107Seric # endif /* LOG */
13247b12ae1Seric 		finis();
133d0a9e852Seric 	}
1341b6e4a15Seric 
1351b6e4a15Seric 	/* turn on network debugging? */
136a2ef5fa4Seric 	if (tTd(15, 101))
13752308a50Seric 		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on);
1381b6e4a15Seric 
1397868dfc2Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on);
1407868dfc2Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on);
1417868dfc2Seric 
14208feafeaSeric #ifdef SO_RCVBUF
14308feafeaSeric 	if (TcpRcvBufferSize > 0)
14408feafeaSeric 	{
14508feafeaSeric 		if (setsockopt(DaemonSocket, SOL_SOCKET, SO_RCVBUF,
146bf217a95Seric 			       (char *) &TcpRcvBufferSize,
14708feafeaSeric 			       sizeof(TcpRcvBufferSize)) < 0)
14808feafeaSeric 			syserr("getrequests: setsockopt(SO_RCVBUF)");
14908feafeaSeric 	}
15008feafeaSeric #endif
15108feafeaSeric 
15298e28903Seric 	switch (DaemonAddr.sa.sa_family)
15398e28903Seric 	{
15498e28903Seric # ifdef NETINET
15598e28903Seric 	  case AF_INET:
156dadb8687Seric 		socksize = sizeof DaemonAddr.sin;
15798e28903Seric 		break;
15898e28903Seric # endif
15998e28903Seric 
16098e28903Seric # ifdef NETISO
16198e28903Seric 	  case AF_ISO:
162dadb8687Seric 		socksize = sizeof DaemonAddr.siso;
16398e28903Seric 		break;
16498e28903Seric # endif
16598e28903Seric 
16698e28903Seric 	  default:
167dadb8687Seric 		socksize = sizeof DaemonAddr;
16898e28903Seric 		break;
16998e28903Seric 	}
17098e28903Seric 
171dadb8687Seric 	if (bind(DaemonSocket, &DaemonAddr.sa, socksize) < 0)
1721c71e510Seric 	{
1731c71e510Seric 		syserr("getrequests: cannot bind");
174b7d7afcbSeric 		(void) close(DaemonSocket);
1751c71e510Seric 		goto severe;
1761c71e510Seric 	}
1771c71e510Seric 
1782b9178d3Seric 	(void) setsignal(SIGCHLD, reapchild);
17952308a50Seric 
1800aae1086Seric 	/* write the pid to the log file for posterity */
1810aae1086Seric 	pidf = fopen(PidFile, "w");
1820aae1086Seric 	if (pidf != NULL)
1830aae1086Seric 	{
18437950f67Seric 		extern char *CommandLineArgs;
18537950f67Seric 
18637950f67Seric 		/* write the process id on line 1 */
1870aae1086Seric 		fprintf(pidf, "%d\n", getpid());
18837950f67Seric 
18937950f67Seric 		/* line 2 contains all command line flags */
19037950f67Seric 		fprintf(pidf, "%s\n", CommandLineArgs);
19137950f67Seric 
19237950f67Seric 		/* flush and close */
1930aae1086Seric 		fclose(pidf);
1940aae1086Seric 	}
1950aae1086Seric 
1960aae1086Seric 
1971c71e510Seric 	if (tTd(15, 1))
198b7d7afcbSeric 		printf("getrequests: %d\n", DaemonSocket);
1991c71e510Seric 
2001c71e510Seric 	for (;;)
2011c71e510Seric 	{
2023a099713Seric 		register int pid;
203a44d5a5eSeric 		auto int lotherend;
20415d084d5Seric 		extern bool refuseconnections();
2053a099713Seric 
2063a099713Seric 		/* see if we are rejecting connections */
20715d084d5Seric 		CurrentLA = getla();
20815d084d5Seric 		if (refuseconnections())
2096775ec03Sbostic 		{
21015d084d5Seric 			if (!refusingconnections)
21115d084d5Seric 			{
21215d084d5Seric 				/* don't queue so peer will fail quickly */
21315d084d5Seric 				(void) listen(DaemonSocket, 0);
21415d084d5Seric 				refusingconnections = TRUE;
21515d084d5Seric 			}
21671e5e267Seric 			setproctitle("rejecting connections: load average: %d",
21771e5e267Seric 				CurrentLA);
2183a099713Seric 			sleep(5);
21915d084d5Seric 			continue;
22015d084d5Seric 		}
22115d084d5Seric 
22215d084d5Seric 		if (refusingconnections)
22315d084d5Seric 		{
22415d084d5Seric 			/* start listening again */
225bfc1eaf8Seric 			if (listen(DaemonSocket, ListenQueueSize) < 0)
22615d084d5Seric 			{
22715d084d5Seric 				syserr("getrequests: cannot listen");
22815d084d5Seric 				(void) close(DaemonSocket);
22915d084d5Seric 				goto severe;
23015d084d5Seric 			}
23115d084d5Seric 			setproctitle("accepting connections");
23215d084d5Seric 			refusingconnections = FALSE;
2336775ec03Sbostic 		}
234a44d5a5eSeric 
2351c71e510Seric 		/* wait for a connection */
2361c71e510Seric 		do
2371c71e510Seric 		{
2381c71e510Seric 			errno = 0;
239dadb8687Seric 			lotherend = socksize;
2409b100374Sbostic 			t = accept(DaemonSocket,
2419b100374Sbostic 			    (struct sockaddr *)&RealHostAddr, &lotherend);
2421c71e510Seric 		} while (t < 0 && errno == EINTR);
2431c71e510Seric 		if (t < 0)
2441c71e510Seric 		{
2451c71e510Seric 			syserr("getrequests: accept");
2461c71e510Seric 			sleep(5);
2471c71e510Seric 			continue;
2481c71e510Seric 		}
249d0a9e852Seric 
250d0a9e852Seric 		/*
251d0a9e852Seric 		**  Create a subprocess to process the mail.
252d0a9e852Seric 		*/
253d0a9e852Seric 
25461e4310fSeric 		if (tTd(15, 2))
2551c71e510Seric 			printf("getrequests: forking (fd = %d)\n", t);
256eb889047Seric 
257a8268164Seric 		pid = fork();
258a8268164Seric 		if (pid < 0)
259a8268164Seric 		{
260a8268164Seric 			syserr("daemon: cannot fork");
261a8268164Seric 			sleep(10);
2621c71e510Seric 			(void) close(t);
263a8268164Seric 			continue;
264a8268164Seric 		}
265a8268164Seric 
266a8268164Seric 		if (pid == 0)
267a8268164Seric 		{
268da662164Seric 			char *p;
2699f8b0eadSeric 			extern char *hostnamebyanyaddr();
270a44d5a5eSeric 
271a8268164Seric 			/*
272a8268164Seric 			**  CHILD -- return to caller.
273a44d5a5eSeric 			**	Collect verified idea of sending host.
274a8268164Seric 			**	Verify calling user id if possible here.
275a8268164Seric 			*/
276a8268164Seric 
2772b9178d3Seric 			(void) setsignal(SIGCHLD, SIG_DFL);
2789f9b003eSeric 			DisConnected = FALSE;
279779ac194Seric 
2804dd09a90Seric 			setproctitle("startup with %s",
2814dd09a90Seric 				anynet_ntoa(&RealHostAddr));
2824dd09a90Seric 
283a44d5a5eSeric 			/* determine host name */
284da662164Seric 			p = hostnamebyanyaddr(&RealHostAddr);
285da662164Seric 			RealHostName = newstr(p);
2864dd09a90Seric 			setproctitle("startup with %s", p);
28729dcf4baSeric 
2882a6bc25bSeric #ifdef LOG
2891f2ff1a4Seric 			if (LogLevel > 11)
2902a6bc25bSeric 			{
2912a6bc25bSeric 				/* log connection information */
2922a6bc25bSeric 				syslog(LOG_INFO, "connect from %s (%s)",
2939f8b0eadSeric 					RealHostName, anynet_ntoa(&RealHostAddr));
2942a6bc25bSeric 			}
2952a6bc25bSeric #endif
2962a6bc25bSeric 
297244b09d1Seric 			(void) close(DaemonSocket);
298335eae58Seric 			if ((InChannel = fdopen(t, "r")) == NULL ||
299335eae58Seric 			    (t = dup(t)) < 0 ||
300335eae58Seric 			    (OutChannel = fdopen(t, "w")) == NULL)
301335eae58Seric 			{
302335eae58Seric 				syserr("cannot open SMTP server channel, fd=%d", t);
303335eae58Seric 				exit(0);
304335eae58Seric 			}
305244b09d1Seric 
30629dcf4baSeric 			/* should we check for illegal connection here? XXX */
307e17a3a5aSeric #ifdef XLA
308e17a3a5aSeric 			if (!xla_host_ok(RealHostName))
309e17a3a5aSeric 			{
310244b09d1Seric 				message("421 Too many SMTP sessions for this host");
311e17a3a5aSeric 				exit(0);
312e17a3a5aSeric 			}
313e17a3a5aSeric #endif
314a44d5a5eSeric 
31561e4310fSeric 			if (tTd(15, 2))
316d0a9e852Seric 				printf("getreq: returning\n");
317a8268164Seric 			return;
318a8268164Seric 		}
319a8268164Seric 
3203c154354Seric 		/* close the port so that others will hang (for a while) */
3213c154354Seric 		(void) close(t);
3228e3e4b17Seric 	}
3233c154354Seric 	/*NOTREACHED*/
3243c154354Seric }
3258e3e4b17Seric /*
326b7d7afcbSeric **  CLRDAEMON -- reset the daemon connection
327b7d7afcbSeric **
328b7d7afcbSeric **	Parameters:
329b7d7afcbSeric **		none.
330b7d7afcbSeric **
331b7d7afcbSeric **	Returns:
332b7d7afcbSeric **		none.
333b7d7afcbSeric **
334b7d7afcbSeric **	Side Effects:
335b7d7afcbSeric **		releases any resources used by the passive daemon.
336b7d7afcbSeric */
337b7d7afcbSeric 
338b7d7afcbSeric clrdaemon()
339b7d7afcbSeric {
340b7d7afcbSeric 	if (DaemonSocket >= 0)
341b7d7afcbSeric 		(void) close(DaemonSocket);
342b7d7afcbSeric 	DaemonSocket = -1;
343b7d7afcbSeric }
344b7d7afcbSeric /*
345bfb80540Seric **  SETDAEMONOPTIONS -- set options for running the daemon
346bfb80540Seric **
347bfb80540Seric **	Parameters:
348bfb80540Seric **		p -- the options line.
349bfb80540Seric **
350bfb80540Seric **	Returns:
351bfb80540Seric **		none.
352bfb80540Seric */
353bfb80540Seric 
354bfb80540Seric setdaemonoptions(p)
355bfb80540Seric 	register char *p;
356bfb80540Seric {
357850144caSeric 	if (DaemonAddr.sa.sa_family == AF_UNSPEC)
358850144caSeric 		DaemonAddr.sa.sa_family = AF_INET;
359850144caSeric 
360bfb80540Seric 	while (p != NULL)
361bfb80540Seric 	{
362bfb80540Seric 		register char *f;
363bfb80540Seric 		register char *v;
364bfb80540Seric 
365bfb80540Seric 		while (isascii(*p) && isspace(*p))
366bfb80540Seric 			p++;
367bfb80540Seric 		if (*p == '\0')
368bfb80540Seric 			break;
369bfb80540Seric 		f = p;
370bfb80540Seric 		p = strchr(p, ',');
371bfb80540Seric 		if (p != NULL)
372bfb80540Seric 			*p++ = '\0';
373bfb80540Seric 		v = strchr(f, '=');
374bfb80540Seric 		if (v == NULL)
375bfb80540Seric 			continue;
376bfb80540Seric 		while (isascii(*++v) && isspace(*v))
377bfb80540Seric 			continue;
378bfb80540Seric 
379bfb80540Seric 		switch (*f)
380bfb80540Seric 		{
381850144caSeric 		  case 'F':		/* address family */
382850144caSeric 			if (isascii(*v) && isdigit(*v))
383850144caSeric 				DaemonAddr.sa.sa_family = atoi(v);
384850144caSeric #ifdef NETINET
385850144caSeric 			else if (strcasecmp(v, "inet") == 0)
386850144caSeric 				DaemonAddr.sa.sa_family = AF_INET;
387850144caSeric #endif
388850144caSeric #ifdef NETISO
389850144caSeric 			else if (strcasecmp(v, "iso") == 0)
390850144caSeric 				DaemonAddr.sa.sa_family = AF_ISO;
391850144caSeric #endif
392850144caSeric #ifdef NETNS
393850144caSeric 			else if (strcasecmp(v, "ns") == 0)
394850144caSeric 				DaemonAddr.sa.sa_family = AF_NS;
395850144caSeric #endif
396850144caSeric #ifdef NETX25
397850144caSeric 			else if (strcasecmp(v, "x.25") == 0)
398850144caSeric 				DaemonAddr.sa.sa_family = AF_CCITT;
399850144caSeric #endif
400850144caSeric 			else
401850144caSeric 				syserr("554 Unknown address family %s in Family=option", v);
402850144caSeric 			break;
403850144caSeric 
404850144caSeric 		  case 'A':		/* address */
405850144caSeric 			switch (DaemonAddr.sa.sa_family)
406850144caSeric 			{
407850144caSeric #ifdef NETINET
408850144caSeric 			  case AF_INET:
409850144caSeric 				if (isascii(*v) && isdigit(*v))
410850144caSeric 					DaemonAddr.sin.sin_addr.s_addr = inet_network(v);
411850144caSeric 				else
412850144caSeric 				{
413850144caSeric 					register struct netent *np;
414850144caSeric 
415850144caSeric 					np = getnetbyname(v);
416850144caSeric 					if (np == NULL)
417850144caSeric 						syserr("554 network \"%s\" unknown", v);
418850144caSeric 					else
419850144caSeric 						DaemonAddr.sin.sin_addr.s_addr = np->n_net;
420850144caSeric 				}
421850144caSeric 				break;
422850144caSeric #endif
423850144caSeric 
424850144caSeric 			  default:
425850144caSeric 				syserr("554 Address= option unsupported for family %d",
426850144caSeric 					DaemonAddr.sa.sa_family);
427850144caSeric 				break;
428850144caSeric 			}
429850144caSeric 			break;
430850144caSeric 
431bfb80540Seric 		  case 'P':		/* port */
432850144caSeric 			switch (DaemonAddr.sa.sa_family)
433850144caSeric 			{
434850144caSeric 				short port;
435850144caSeric 
436850144caSeric #ifdef NETINET
437850144caSeric 			  case AF_INET:
438bfb80540Seric 				if (isascii(*v) && isdigit(*v))
43976b70c58Seric 					DaemonAddr.sin.sin_port = htons(atoi(v));
440bfb80540Seric 				else
441bfb80540Seric 				{
442bfb80540Seric 					register struct servent *sp;
443bfb80540Seric 
444bfb80540Seric 					sp = getservbyname(v, "tcp");
445bfb80540Seric 					if (sp == NULL)
446ad977999Seric 						syserr("554 service \"%s\" unknown", v);
447bfb80540Seric 					else
448bfb80540Seric 						DaemonAddr.sin.sin_port = sp->s_port;
449bfb80540Seric 				}
450bfb80540Seric 				break;
451850144caSeric #endif
452bfb80540Seric 
453850144caSeric #ifdef NETISO
454850144caSeric 			  case AF_ISO:
455850144caSeric 				/* assume two byte transport selector */
456bfb80540Seric 				if (isascii(*v) && isdigit(*v))
45776b70c58Seric 					port = htons(atoi(v));
458bfb80540Seric 				else
459bfb80540Seric 				{
460850144caSeric 					register struct servent *sp;
461bfb80540Seric 
462850144caSeric 					sp = getservbyname(v, "tcp");
463850144caSeric 					if (sp == NULL)
464ad977999Seric 						syserr("554 service \"%s\" unknown", v);
465bfb80540Seric 					else
466850144caSeric 						port = sp->s_port;
467850144caSeric 				}
468850144caSeric 				bcopy((char *) &port, TSEL(&DaemonAddr.siso), 2);
469850144caSeric 				break;
470850144caSeric #endif
471850144caSeric 
472850144caSeric 			  default:
473850144caSeric 				syserr("554 Port= option unsupported for family %d",
474850144caSeric 					DaemonAddr.sa.sa_family);
475850144caSeric 				break;
476bfb80540Seric 			}
477bfb80540Seric 			break;
478bfc1eaf8Seric 
479bfc1eaf8Seric 		  case 'L':		/* listen queue size */
480bfc1eaf8Seric 			ListenQueueSize = atoi(v);
481bfc1eaf8Seric 			break;
482b35447dbSeric 
483b35447dbSeric 		  case 'S':		/* send buffer size */
484b35447dbSeric 			TcpSndBufferSize = atoi(v);
485b35447dbSeric 			break;
486b35447dbSeric 
487b35447dbSeric 		  case 'R':		/* receive buffer size */
488b35447dbSeric 			TcpRcvBufferSize = atoi(v);
489b35447dbSeric 			break;
490bfb80540Seric 		}
491bfb80540Seric 	}
492bfb80540Seric }
493bfb80540Seric /*
4947aa493c5Seric **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
4957aa493c5Seric **
4967aa493c5Seric **	Parameters:
4977aa493c5Seric **		host -- the name of the host.
49848ff0a9dSeric **		port -- the port number to connect to.
499655feedbSeric **		mci -- a pointer to the mail connection information
500655feedbSeric **			structure to be filled in.
501914346b1Seric **		usesecureport -- if set, use a low numbered (reserved)
502914346b1Seric **			port to provide some rudimentary authentication.
5037aa493c5Seric **
5047aa493c5Seric **	Returns:
5057aa493c5Seric **		An exit code telling whether the connection could be
5067aa493c5Seric **			made and if not why not.
5077aa493c5Seric **
5087aa493c5Seric **	Side Effects:
5097aa493c5Seric **		none.
5107aa493c5Seric */
5117aa493c5Seric 
512e2f2f828Seric SOCKADDR	CurHostAddr;		/* address of current host */
51371ff6caaSeric 
514b31e7f2bSeric int
515655feedbSeric makeconnection(host, port, mci, usesecureport)
5167aa493c5Seric 	char *host;
517210215eaSeric 	u_short port;
518b31e7f2bSeric 	register MCI *mci;
519914346b1Seric 	bool usesecureport;
5207aa493c5Seric {
52104344589Sbloom 	register int i, s;
52204344589Sbloom 	register struct hostent *hp = (struct hostent *)NULL;
523e2f2f828Seric 	SOCKADDR addr;
5246286bb75Sbloom 	int sav_errno;
525e2f2f828Seric 	int addrlen;
526*9d4a8008Seric #if NAMED_BIND
527134746fbSeric 	extern int h_errno;
528134746fbSeric #endif
5297aa493c5Seric 
5307aa493c5Seric 	/*
5317aa493c5Seric 	**  Set up the address for the mailer.
53271096d12Seric 	**	Accept "[a.b.c.d]" syntax for host name.
5337aa493c5Seric 	*/
5347aa493c5Seric 
535*9d4a8008Seric #if NAMED_BIND
536794bdbb9Smiriam 	h_errno = 0;
537134746fbSeric #endif
538794bdbb9Smiriam 	errno = 0;
539967778e2Seric 	bzero(&CurHostAddr, sizeof CurHostAddr);
540c931b82bSeric 	SmtpPhase = mci->mci_phase = "initial connection";
541d945ebe8Seric 	CurHostName = host;
542794bdbb9Smiriam 
54371096d12Seric 	if (host[0] == '[')
54471096d12Seric 	{
545a44d5a5eSeric 		long hid;
5466c2c3107Seric 		register char *p = strchr(host, ']');
54771096d12Seric 
548a44d5a5eSeric 		if (p != NULL)
54971096d12Seric 		{
550a44d5a5eSeric 			*p = '\0';
5514d9c42c2Seric #ifdef NETINET
552a44d5a5eSeric 			hid = inet_addr(&host[1]);
553a7e21fe6Seric 			if (hid == -1)
5544d9c42c2Seric #endif
555a7e21fe6Seric 			{
556a7e21fe6Seric 				/* try it as a host name (avoid MX lookup) */
557a7e21fe6Seric 				hp = gethostbyname(&host[1]);
558a7e21fe6Seric 				*p = ']';
559a7e21fe6Seric 				goto gothostent;
560a7e21fe6Seric 			}
561a44d5a5eSeric 			*p = ']';
56271096d12Seric 		}
563a7e21fe6Seric 		if (p == NULL)
56471096d12Seric 		{
56508b25121Seric 			usrerr("553 Invalid numeric domain spec \"%s\"", host);
56671096d12Seric 			return (EX_NOHOST);
56771096d12Seric 		}
5684d9c42c2Seric #ifdef NETINET
5694d9c42c2Seric 		addr.sin.sin_family = AF_INET;		/*XXX*/
57083c1f4bcSeric 		addr.sin.sin_addr.s_addr = hid;
5714d9c42c2Seric #endif
57271096d12Seric 	}
5731c71e510Seric 	else
5741c71e510Seric 	{
57504344589Sbloom 		hp = gethostbyname(host);
576a7e21fe6Seric gothostent:
577794bdbb9Smiriam 		if (hp == NULL)
578794bdbb9Smiriam 		{
579*9d4a8008Seric #if NAMED_BIND
580794bdbb9Smiriam 			if (errno == ETIMEDOUT || h_errno == TRY_AGAIN)
58152308a50Seric 				return (EX_TEMPFAIL);
58282e5d8ddSeric 
583134746fbSeric 			/* if name server is specified, assume temp fail */
584134746fbSeric 			if (errno == ECONNREFUSED && UseNameServer)
585134746fbSeric 				return (EX_TEMPFAIL);
586134746fbSeric #endif
5877aa493c5Seric 			return (EX_NOHOST);
588794bdbb9Smiriam 		}
58983c1f4bcSeric 		addr.sa.sa_family = hp->h_addrtype;
59083c1f4bcSeric 		switch (hp->h_addrtype)
59183c1f4bcSeric 		{
59283c1f4bcSeric #ifdef NETINET
59383c1f4bcSeric 		  case AF_INET:
594e2f2f828Seric 			bcopy(hp->h_addr,
59583c1f4bcSeric 				&addr.sin.sin_addr,
596859d5010Seric 				sizeof addr.sin.sin_addr);
59783c1f4bcSeric 			break;
59883c1f4bcSeric #endif
59983c1f4bcSeric 
60083c1f4bcSeric 		  default:
601e2f2f828Seric 			bcopy(hp->h_addr,
60283c1f4bcSeric 				addr.sa.sa_data,
603e2f2f828Seric 				hp->h_length);
60483c1f4bcSeric 			break;
60583c1f4bcSeric 		}
60604344589Sbloom 		i = 1;
6071c71e510Seric 	}
6081c71e510Seric 
6091c71e510Seric 	/*
6101c71e510Seric 	**  Determine the port number.
6111c71e510Seric 	*/
6121c71e510Seric 
613fd7c0790Seric 	if (port != 0)
614e2f2f828Seric 		port = htons(port);
615fd7c0790Seric 	else
6161c71e510Seric 	{
6171c71e510Seric 		register struct servent *sp = getservbyname("smtp", "tcp");
6181c71e510Seric 
6191c71e510Seric 		if (sp == NULL)
6201c71e510Seric 		{
621ad977999Seric 			syserr("554 makeconnection: service \"smtp\" unknown");
622e5311662Seric 			port = htons(25);
6231c71e510Seric 		}
624e5311662Seric 		else
625e2f2f828Seric 			port = sp->s_port;
626e2f2f828Seric 	}
627e2f2f828Seric 
62883c1f4bcSeric 	switch (addr.sa.sa_family)
629e2f2f828Seric 	{
6304d9c42c2Seric #ifdef NETINET
631e2f2f828Seric 	  case AF_INET:
63283c1f4bcSeric 		addr.sin.sin_port = port;
633e2f2f828Seric 		addrlen = sizeof (struct sockaddr_in);
634e2f2f828Seric 		break;
6354d9c42c2Seric #endif
636e2f2f828Seric 
637e2f2f828Seric #ifdef NETISO
638e2f2f828Seric 	  case AF_ISO:
639e2f2f828Seric 		/* assume two byte transport selector */
640e2f2f828Seric 		bcopy((char *) &port, TSEL((struct sockaddr_iso *) &addr), 2);
641e2f2f828Seric 		addrlen = sizeof (struct sockaddr_iso);
642e2f2f828Seric 		break;
643e2f2f828Seric #endif
644e2f2f828Seric 
645e2f2f828Seric 	  default:
64683c1f4bcSeric 		syserr("Can't connect to address family %d", addr.sa.sa_family);
647e2f2f828Seric 		return (EX_NOHOST);
6481c71e510Seric 	}
6497aa493c5Seric 
6507aa493c5Seric 	/*
6517aa493c5Seric 	**  Try to actually open the connection.
6527aa493c5Seric 	*/
6537aa493c5Seric 
654e17a3a5aSeric #ifdef XLA
655e17a3a5aSeric 	/* if too many connections, don't bother trying */
656e17a3a5aSeric 	if (!xla_noqueue_ok(host))
657e17a3a5aSeric 		return EX_TEMPFAIL;
658e17a3a5aSeric #endif
659e17a3a5aSeric 
660aea02ca1Seric 	for (;;)
661aea02ca1Seric 	{
66261e4310fSeric 		if (tTd(16, 1))
663e2f2f828Seric 			printf("makeconnection (%s [%s])\n",
664e2f2f828Seric 				host, anynet_ntoa(&addr));
6657aa493c5Seric 
666226e3022Seric 		/* save for logging */
667226e3022Seric 		CurHostAddr = addr;
668226e3022Seric 
669914346b1Seric 		if (usesecureport)
670914346b1Seric 		{
671914346b1Seric 			int rport = IPPORT_RESERVED - 1;
672914346b1Seric 
673914346b1Seric 			s = rresvport(&rport);
674914346b1Seric 		}
675914346b1Seric 		else
676914346b1Seric 		{
677af5e902cSeric 			s = socket(AF_INET, SOCK_STREAM, 0);
678914346b1Seric 		}
6797aa493c5Seric 		if (s < 0)
6807aa493c5Seric 		{
6816286bb75Sbloom 			sav_errno = errno;
682914346b1Seric 			syserr("makeconnection: no socket");
6837aa493c5Seric 			goto failure;
6847aa493c5Seric 		}
6857aa493c5Seric 
686b35447dbSeric #ifdef SO_SNDBUF
687b35447dbSeric 		if (TcpSndBufferSize > 0)
688b35447dbSeric 		{
689b35447dbSeric 			if (setsockopt(s, SOL_SOCKET, SO_SNDBUF,
690bf217a95Seric 				       (char *) &TcpSndBufferSize,
691b35447dbSeric 				       sizeof(TcpSndBufferSize)) < 0)
692b35447dbSeric 				syserr("makeconnection: setsockopt(SO_SNDBUF)");
693b35447dbSeric 		}
694b35447dbSeric #endif
695b35447dbSeric 
69661e4310fSeric 		if (tTd(16, 1))
697b31e7f2bSeric 			printf("makeconnection: fd=%d\n", s);
6981b6e4a15Seric 
6991b6e4a15Seric 		/* turn on network debugging? */
700a2ef5fa4Seric 		if (tTd(16, 101))
70152308a50Seric 		{
70252308a50Seric 			int on = 1;
703aea02ca1Seric 			(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG,
704aea02ca1Seric 					  (char *)&on, sizeof on);
70552308a50Seric 		}
70687d6e633Srick 		if (CurEnv->e_xfp != NULL)
707877a6142Seric 			(void) fflush(CurEnv->e_xfp);		/* for debugging */
7084bd6a662Seric 		errno = 0;					/* for debugging */
709e2f2f828Seric 		if (connect(s, (struct sockaddr *) &addr, addrlen) >= 0)
710aea02ca1Seric 			break;
711aea02ca1Seric 
712aea02ca1Seric 		/* couldn't connect.... figure out why */
7136286bb75Sbloom 		sav_errno = errno;
7146286bb75Sbloom 		(void) close(s);
71504344589Sbloom 		if (hp && hp->h_addr_list[i])
71604344589Sbloom 		{
717aea02ca1Seric 			if (tTd(16, 1))
718e2f2f828Seric 				printf("Connect failed (%s); trying new address....\n",
719e2f2f828Seric 					errstring(sav_errno));
72083c1f4bcSeric 			switch (addr.sa.sa_family)
72183c1f4bcSeric 			{
72283c1f4bcSeric #ifdef NETINET
72383c1f4bcSeric 			  case AF_INET:
724e2f2f828Seric 				bcopy(hp->h_addr_list[i++],
72583c1f4bcSeric 				      &addr.sin.sin_addr,
726859d5010Seric 				      sizeof addr.sin.sin_addr);
72783c1f4bcSeric 				break;
72883c1f4bcSeric #endif
72983c1f4bcSeric 
73083c1f4bcSeric 			  default:
731e2f2f828Seric 				bcopy(hp->h_addr_list[i++],
73283c1f4bcSeric 					addr.sa.sa_data,
733914346b1Seric 					hp->h_length);
73483c1f4bcSeric 				break;
73583c1f4bcSeric 			}
736aea02ca1Seric 			continue;
73704344589Sbloom 		}
73804344589Sbloom 
7397aa493c5Seric 		/* failure, decide if temporary or not */
7407aa493c5Seric 	failure:
741244b09d1Seric #ifdef XLA
742244b09d1Seric 		xla_host_end(host);
743244b09d1Seric #endif
744e2de2524Seric 		if (transienterror(sav_errno))
745e2de2524Seric 			return EX_TEMPFAIL;
746e2de2524Seric 		else
74787d6e633Srick 		{
74808b25121Seric 			message("%s", errstring(sav_errno));
7497aa493c5Seric 			return (EX_UNAVAILABLE);
7507aa493c5Seric 		}
7517aa493c5Seric 	}
7527aa493c5Seric 
7537aa493c5Seric 	/* connection ok, put it into canonical form */
754335eae58Seric 	if ((mci->mci_out = fdopen(s, "w")) == NULL ||
755335eae58Seric 	    (s = dup(s)) < 0 ||
756ab81ee53Seric 	    (mci->mci_in = fdopen(s, "r")) == NULL)
757335eae58Seric 	{
758335eae58Seric 		syserr("cannot open SMTP client channel, fd=%d", s);
759335eae58Seric 		return EX_TEMPFAIL;
760335eae58Seric 	}
7617aa493c5Seric 
762dca8e1f7Seric 	return (EX_OK);
7637aa493c5Seric }
764444eaf03Seric /*
765444eaf03Seric **  MYHOSTNAME -- return the name of this host.
766444eaf03Seric **
767444eaf03Seric **	Parameters:
768444eaf03Seric **		hostbuf -- a place to return the name of this host.
769897f1869Seric **		size -- the size of hostbuf.
770444eaf03Seric **
771444eaf03Seric **	Returns:
772444eaf03Seric **		A list of aliases for this host.
773444eaf03Seric **
774444eaf03Seric **	Side Effects:
775d8d0a4aeSeric **		Adds numeric codes to $=w.
776444eaf03Seric */
777444eaf03Seric 
778444eaf03Seric char **
779897f1869Seric myhostname(hostbuf, size)
780444eaf03Seric 	char hostbuf[];
781897f1869Seric 	int size;
782444eaf03Seric {
78338ad259dSeric 	register struct hostent *hp;
784444eaf03Seric 	extern struct hostent *gethostbyname();
785444eaf03Seric 
786af5e902cSeric 	if (gethostname(hostbuf, size) < 0)
787af5e902cSeric 	{
788af5e902cSeric 		(void) strcpy(hostbuf, "localhost");
789af5e902cSeric 	}
790a44d5a5eSeric 	hp = gethostbyname(hostbuf);
791a44d5a5eSeric 	if (hp != NULL)
7927364df9fSeric 	{
79338ad259dSeric 		(void) strncpy(hostbuf, hp->h_name, size - 1);
79438ad259dSeric 		hostbuf[size - 1] = '\0';
79538ad259dSeric 
79638ad259dSeric 		if (hp->h_addrtype == AF_INET && hp->h_length == 4)
79738ad259dSeric 		{
79838ad259dSeric 			register int i;
79938ad259dSeric 
800d8d0a4aeSeric 			for (i = 0; hp->h_addr_list[i] != NULL; i++)
80138ad259dSeric 			{
802d8d0a4aeSeric 				char ipbuf[100];
803d8d0a4aeSeric 
804d8d0a4aeSeric 				sprintf(ipbuf, "[%s]",
805d8d0a4aeSeric 					inet_ntoa(*((struct in_addr *) hp->h_addr_list[i])));
806d8d0a4aeSeric 				setclass('w', ipbuf);
80738ad259dSeric 			}
80838ad259dSeric 		}
80938ad259dSeric 
810a44d5a5eSeric 		return (hp->h_aliases);
8117364df9fSeric 	}
812444eaf03Seric 	else
813444eaf03Seric 		return (NULL);
814444eaf03Seric }
815cb452edcSeric /*
8169f8b0eadSeric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
8179f8b0eadSeric **
8189f8b0eadSeric **	Uses RFC1413 protocol to try to get info from the other end.
819320e0d1cSeric **
820320e0d1cSeric **	Parameters:
821320e0d1cSeric **		fd -- the descriptor
822320e0d1cSeric **
823320e0d1cSeric **	Returns:
8249f8b0eadSeric **		The user@host information associated with this descriptor.
825320e0d1cSeric */
826320e0d1cSeric 
827c73f2aa4Seric #if IDENTPROTO
8289f8b0eadSeric 
8299f8b0eadSeric static jmp_buf	CtxAuthTimeout;
8309f8b0eadSeric 
8319f8b0eadSeric static
8329f8b0eadSeric authtimeout()
8339f8b0eadSeric {
8349f8b0eadSeric 	longjmp(CtxAuthTimeout, 1);
8359f8b0eadSeric }
8369f8b0eadSeric 
8379f8b0eadSeric #endif
8389f8b0eadSeric 
839320e0d1cSeric char *
8409f8b0eadSeric getauthinfo(fd)
841320e0d1cSeric 	int fd;
842320e0d1cSeric {
8439f8b0eadSeric 	SOCKADDR fa;
8449f8b0eadSeric 	int falen;
845a5546e24Seric 	register char *p;
846c73f2aa4Seric #if IDENTPROTO
8479f8b0eadSeric 	SOCKADDR la;
8489f8b0eadSeric 	int lalen;
8499f8b0eadSeric 	register struct servent *sp;
8509f8b0eadSeric 	int s;
8519f8b0eadSeric 	int i;
8529f8b0eadSeric 	EVENT *ev;
8539f8b0eadSeric #endif
8549f8b0eadSeric 	static char hbuf[MAXNAME * 2 + 2];
8559f8b0eadSeric 	extern char *hostnamebyanyaddr();
8569f8b0eadSeric 	extern char RealUserName[];			/* main.c */
857320e0d1cSeric 
8589f8b0eadSeric 	falen = sizeof fa;
8593940f49dSeric 	if (getpeername(fd, &fa.sa, &falen) < 0 || falen <= 0 ||
8603940f49dSeric 	    fa.sa.sa_family == 0)
8619f8b0eadSeric 	{
8629f8b0eadSeric 		(void) sprintf(hbuf, "%s@localhost", RealUserName);
86353853673Seric 		if (tTd(9, 1))
8649f8b0eadSeric 			printf("getauthinfo: %s\n", hbuf);
865320e0d1cSeric 		return hbuf;
866320e0d1cSeric 	}
8679f8b0eadSeric 
868c73f2aa4Seric #if IDENTPROTO
86993b3215bSeric 	if (TimeOuts.to_ident == 0)
87093b3215bSeric 		goto noident;
87193b3215bSeric 
8729f8b0eadSeric 	lalen = sizeof la;
8739f8b0eadSeric 	if (fa.sa.sa_family != AF_INET ||
8749f8b0eadSeric 	    getsockname(fd, &la.sa, &lalen) < 0 || lalen <= 0 ||
8759f8b0eadSeric 	    la.sa.sa_family != AF_INET)
8769f8b0eadSeric 	{
8779f8b0eadSeric 		/* no ident info */
8789f8b0eadSeric 		goto noident;
8799f8b0eadSeric 	}
8809f8b0eadSeric 
8819f8b0eadSeric 	/* create ident query */
882f2d880b6Seric 	(void) sprintf(hbuf, "%d,%d\r\n",
883f2d880b6Seric 		ntohs(fa.sin.sin_port), ntohs(la.sin.sin_port));
8849f8b0eadSeric 
8859f8b0eadSeric 	/* create local address */
886d6af7dadSeric 	la.sin.sin_port = 0;
8879f8b0eadSeric 
8889f8b0eadSeric 	/* create foreign address */
8899f8b0eadSeric 	sp = getservbyname("auth", "tcp");
8909f8b0eadSeric 	if (sp != NULL)
8919f8b0eadSeric 		fa.sin.sin_port = sp->s_port;
8929f8b0eadSeric 	else
8931038598cSeric 		fa.sin.sin_port = htons(113);
8949f8b0eadSeric 
8959f8b0eadSeric 	s = -1;
8969f8b0eadSeric 	if (setjmp(CtxAuthTimeout) != 0)
8979f8b0eadSeric 	{
8989f8b0eadSeric 		if (s >= 0)
8999f8b0eadSeric 			(void) close(s);
9009f8b0eadSeric 		goto noident;
9019f8b0eadSeric 	}
9029f8b0eadSeric 
9039f8b0eadSeric 	/* put a timeout around the whole thing */
904a0f780efSeric 	ev = setevent(TimeOuts.to_ident, authtimeout, 0);
9059f8b0eadSeric 
906d6af7dadSeric 	/* connect to foreign IDENT server using same address as SMTP socket */
9079f8b0eadSeric 	s = socket(AF_INET, SOCK_STREAM, 0);
9089f8b0eadSeric 	if (s < 0)
9099f8b0eadSeric 	{
9109f8b0eadSeric 		clrevent(ev);
9119f8b0eadSeric 		goto noident;
9129f8b0eadSeric 	}
913d6af7dadSeric 	if (bind(s, &la.sa, sizeof la.sin) < 0 ||
914d6af7dadSeric 	    connect(s, &fa.sa, sizeof fa.sin) < 0)
9159f8b0eadSeric 	{
9167c201575Seric 		goto closeident;
9179f8b0eadSeric 	}
9189f8b0eadSeric 
91953853673Seric 	if (tTd(9, 10))
9209f8b0eadSeric 		printf("getauthinfo: sent %s", hbuf);
9219f8b0eadSeric 
9229f8b0eadSeric 	/* send query */
9239f8b0eadSeric 	if (write(s, hbuf, strlen(hbuf)) < 0)
9249f8b0eadSeric 		goto closeident;
9259f8b0eadSeric 
9269f8b0eadSeric 	/* get result */
9279f8b0eadSeric 	i = read(s, hbuf, sizeof hbuf);
9289f8b0eadSeric 	(void) close(s);
9299f8b0eadSeric 	clrevent(ev);
9309f8b0eadSeric 	if (i <= 0)
9319f8b0eadSeric 		goto noident;
9329f8b0eadSeric 	if (hbuf[--i] == '\n' && hbuf[--i] == '\r')
9339f8b0eadSeric 		i--;
9349f8b0eadSeric 	hbuf[++i] = '\0';
9359f8b0eadSeric 
93653853673Seric 	if (tTd(9, 3))
9379f8b0eadSeric 		printf("getauthinfo:  got %s\n", hbuf);
9389f8b0eadSeric 
9399f8b0eadSeric 	/* parse result */
9409f8b0eadSeric 	p = strchr(hbuf, ':');
9419f8b0eadSeric 	if (p == NULL)
9429f8b0eadSeric 	{
9439f8b0eadSeric 		/* malformed response */
9449f8b0eadSeric 		goto noident;
9459f8b0eadSeric 	}
9469f8b0eadSeric 	while (isascii(*++p) && isspace(*p))
9479f8b0eadSeric 		continue;
9489f8b0eadSeric 	if (strncasecmp(p, "userid", 6) != 0)
9499f8b0eadSeric 	{
9509f8b0eadSeric 		/* presumably an error string */
9519f8b0eadSeric 		goto noident;
9529f8b0eadSeric 	}
9539f8b0eadSeric 	p += 6;
9549f8b0eadSeric 	while (isascii(*p) && isspace(*p))
9559f8b0eadSeric 		p++;
9569f8b0eadSeric 	if (*p++ != ':')
9579f8b0eadSeric 	{
9589f8b0eadSeric 		/* either useridxx or malformed response */
9599f8b0eadSeric 		goto noident;
9609f8b0eadSeric 	}
9619f8b0eadSeric 
9629f8b0eadSeric 	/* p now points to the OSTYPE field */
9639f8b0eadSeric 	p = strchr(p, ':');
9649f8b0eadSeric 	if (p == NULL)
9659f8b0eadSeric 	{
9669f8b0eadSeric 		/* malformed response */
9679f8b0eadSeric 		goto noident;
9689f8b0eadSeric 	}
96953853673Seric 
97053853673Seric 	/* 1413 says don't do this -- but it's broken otherwise */
97153853673Seric 	while (isascii(*++p) && isspace(*p))
97253853673Seric 		continue;
9739f8b0eadSeric 
9749f8b0eadSeric 	/* p now points to the authenticated name */
975f7869e68Seric 	(void) sprintf(hbuf, "%s@%s",
976f7869e68Seric 		p, RealHostName == NULL ? "localhost" : RealHostName);
97753853673Seric 	goto finish;
97853853673Seric 
9797c201575Seric closeident:
9807c201575Seric 	(void) close(s);
9817c201575Seric 	clrevent(ev);
9827c201575Seric 
98353853673Seric #endif /* IDENTPROTO */
98453853673Seric 
98553853673Seric noident:
986f7869e68Seric 	if (RealHostName == NULL)
987f7869e68Seric 	{
988f7869e68Seric 		if (tTd(9, 1))
989f7869e68Seric 			printf("getauthinfo: NULL\n");
990f7869e68Seric 		return NULL;
991f7869e68Seric 	}
99253853673Seric 	(void) strcpy(hbuf, RealHostName);
99353853673Seric 
99453853673Seric finish:
995f7869e68Seric 	if (RealHostName != NULL && RealHostName[0] != '[')
9969f8b0eadSeric 	{
9979f8b0eadSeric 		p = &hbuf[strlen(hbuf)];
9989f8b0eadSeric 		(void) sprintf(p, " [%s]", anynet_ntoa(&RealHostAddr));
9999f8b0eadSeric 	}
100053853673Seric 	if (tTd(9, 1))
10019f8b0eadSeric 		printf("getauthinfo: %s\n", hbuf);
10029f8b0eadSeric 	return hbuf;
10039f8b0eadSeric }
1004320e0d1cSeric /*
100508de856eSeric **  HOST_MAP_LOOKUP -- turn a hostname into canonical form
100615d084d5Seric **
100715d084d5Seric **	Parameters:
100805b57da8Seric **		map -- a pointer to this map (unused).
100908de856eSeric **		name -- the (presumably unqualified) hostname.
101000b385a9Seric **		av -- unused -- for compatibility with other mapping
1011d798a1deSeric **			functions.
10122d29d43aSeric **		statp -- an exit status (out parameter) -- set to
10132d29d43aSeric **			EX_TEMPFAIL if the name server is unavailable.
101415d084d5Seric **
101515d084d5Seric **	Returns:
101615d084d5Seric **		The mapping, if found.
101715d084d5Seric **		NULL if no mapping found.
101815d084d5Seric **
101915d084d5Seric **	Side Effects:
102015d084d5Seric **		Looks up the host specified in hbuf.  If it is not
102115d084d5Seric **		the canonical name for that host, return the canonical
102215d084d5Seric **		name.
1023f36ede03Sbostic */
1024cb452edcSeric 
102515d084d5Seric char *
102600b385a9Seric host_map_lookup(map, name, av, statp)
102705b57da8Seric 	MAP *map;
102808de856eSeric 	char *name;
102900b385a9Seric 	char **av;
10302d29d43aSeric 	int *statp;
103199f7cf32Seric {
103299f7cf32Seric 	register struct hostent *hp;
10335f78836eSmiriam 	u_long in_addr;
103405b57da8Seric 	char *cp;
103538ad259dSeric 	int i;
1036eea91d78Seric 	register STAB *s;
103700b385a9Seric 	char hbuf[MAXNAME];
1038eea91d78Seric 	extern struct hostent *gethostbyaddr();
1039*9d4a8008Seric #if NAMED_BIND
1040eea91d78Seric 	extern int h_errno;
1041c304a798Seric #endif
10425f78836eSmiriam 
1043f36ede03Sbostic 	/*
1044eea91d78Seric 	**  See if we have already looked up this name.  If so, just
1045eea91d78Seric 	**  return it.
1046eea91d78Seric 	*/
1047eea91d78Seric 
104808de856eSeric 	s = stab(name, ST_NAMECANON, ST_ENTER);
1049eea91d78Seric 	if (bitset(NCF_VALID, s->s_namecanon.nc_flags))
1050eea91d78Seric 	{
1051f92c3297Seric 		if (tTd(9, 1))
105208de856eSeric 			printf("host_map_lookup(%s) => CACHE %s\n",
105308de856eSeric 				name, s->s_namecanon.nc_cname);
1054eea91d78Seric 		errno = s->s_namecanon.nc_errno;
1055*9d4a8008Seric #if NAMED_BIND
1056eea91d78Seric 		h_errno = s->s_namecanon.nc_herrno;
1057c304a798Seric #endif
1058eea91d78Seric 		*statp = s->s_namecanon.nc_stat;
105992270fb3Seric 		if (CurEnv->e_message == NULL && *statp == EX_TEMPFAIL)
1060ed63aae0Seric 		{
1061ed63aae0Seric 			sprintf(hbuf, "%s: Name server timeout",
1062ed63aae0Seric 				shortenstring(name, 33));
1063ed63aae0Seric 			CurEnv->e_message = newstr(hbuf);
1064ed63aae0Seric 		}
1065eea91d78Seric 		return s->s_namecanon.nc_cname;
1066eea91d78Seric 	}
1067eea91d78Seric 
1068eea91d78Seric 	/*
1069eea91d78Seric 	**  If first character is a bracket, then it is an address
1070eea91d78Seric 	**  lookup.  Address is copied into a temporary buffer to
107108de856eSeric 	**  strip the brackets and to preserve name if address is
1072eea91d78Seric 	**  unknown.
1073f36ede03Sbostic 	*/
107415d084d5Seric 
107508de856eSeric 	if (*name != '[')
107615d084d5Seric 	{
1077d798a1deSeric 		extern bool getcanonname();
1078d798a1deSeric 
10798cb4653dSeric 		if (tTd(9, 1))
108008de856eSeric 			printf("host_map_lookup(%s) => ", name);
1081eea91d78Seric 		s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
108208de856eSeric 		(void) strcpy(hbuf, name);
10831f2ff1a4Seric 		if (getcanonname(hbuf, sizeof hbuf - 1, TRUE))
10849040ec4fSeric 		{
10859040ec4fSeric 			if (tTd(9, 1))
10869040ec4fSeric 				printf("%s\n", hbuf);
108700b385a9Seric 			cp = map_rewrite(map, hbuf, strlen(hbuf), av);
108800b385a9Seric 			s->s_namecanon.nc_cname = newstr(cp);
108900b385a9Seric 			return cp;
10909040ec4fSeric 		}
109115d084d5Seric 		else
10929040ec4fSeric 		{
10932d29d43aSeric 			register struct hostent *hp;
10942d29d43aSeric 
1095c304a798Seric 			s->s_namecanon.nc_errno = errno;
1096*9d4a8008Seric #if NAMED_BIND
1097c304a798Seric 			s->s_namecanon.nc_herrno = h_errno;
10989040ec4fSeric 			if (tTd(9, 1))
10992d29d43aSeric 				printf("FAIL (%d)\n", h_errno);
11002d29d43aSeric 			switch (h_errno)
11012d29d43aSeric 			{
11022d29d43aSeric 			  case TRY_AGAIN:
110389cb2793Seric 				if (UseNameServer)
11048820d51bSeric 				{
1105e0326f4fSeric 					sprintf(hbuf, "%s: Name server timeout",
1106ed63aae0Seric 						shortenstring(name, 33));
1107e0326f4fSeric 					message("%s", hbuf);
11088820d51bSeric 					if (CurEnv->e_message == NULL)
1109e0326f4fSeric 						CurEnv->e_message = newstr(hbuf);
11108820d51bSeric 				}
11112d29d43aSeric 				*statp = EX_TEMPFAIL;
11122d29d43aSeric 				break;
11132d29d43aSeric 
11142d29d43aSeric 			  case HOST_NOT_FOUND:
11152d29d43aSeric 				*statp = EX_NOHOST;
11162d29d43aSeric 				break;
11172d29d43aSeric 
11182d29d43aSeric 			  case NO_RECOVERY:
11192d29d43aSeric 				*statp = EX_SOFTWARE;
11202d29d43aSeric 				break;
11212d29d43aSeric 
11222d29d43aSeric 			  default:
11232d29d43aSeric 				*statp = EX_UNAVAILABLE;
11242d29d43aSeric 				break;
11252d29d43aSeric 			}
1126c304a798Seric #else
1127c304a798Seric 			if (tTd(9, 1))
1128c304a798Seric 				printf("FAIL\n");
1129c304a798Seric 			*statp = EX_NOHOST;
1130c304a798Seric #endif
1131eea91d78Seric 			s->s_namecanon.nc_stat = *statp;
11322d29d43aSeric 			if (*statp != EX_TEMPFAIL || UseNameServer)
113315d084d5Seric 				return NULL;
11342d29d43aSeric 
11352d29d43aSeric 			/*
11362d29d43aSeric 			**  Try to look it up in /etc/hosts
11372d29d43aSeric 			*/
11382d29d43aSeric 
113908de856eSeric 			hp = gethostbyname(name);
11402d29d43aSeric 			if (hp == NULL)
11412d29d43aSeric 			{
11422d29d43aSeric 				/* no dice there either */
1143eea91d78Seric 				s->s_namecanon.nc_stat = *statp = EX_NOHOST;
11442d29d43aSeric 				return NULL;
11452d29d43aSeric 			}
11462d29d43aSeric 
1147eea91d78Seric 			s->s_namecanon.nc_stat = *statp = EX_OK;
114800b385a9Seric 			cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
114900b385a9Seric 			s->s_namecanon.nc_cname = newstr(cp);
115000b385a9Seric 			return cp;
115115d084d5Seric 		}
11529040ec4fSeric 	}
115308de856eSeric 	if ((cp = strchr(name, ']')) == NULL)
115415d084d5Seric 		return (NULL);
115534e39927Sbostic 	*cp = '\0';
115608de856eSeric 	in_addr = inet_addr(&name[1]);
115738ad259dSeric 
115838ad259dSeric 	/* nope -- ask the name server */
115931601fa7Seric 	hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET);
1160eea91d78Seric 	s->s_namecanon.nc_errno = errno;
1161*9d4a8008Seric #if NAMED_BIND
1162eea91d78Seric 	s->s_namecanon.nc_herrno = h_errno;
1163c304a798Seric #endif
1164eea91d78Seric 	s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
11655f78836eSmiriam 	if (hp == NULL)
1166eea91d78Seric 	{
1167eea91d78Seric 		s->s_namecanon.nc_stat = *statp = EX_NOHOST;
116815d084d5Seric 		return (NULL);
1169eea91d78Seric 	}
117015d084d5Seric 
117138ad259dSeric 	/* found a match -- copy out */
117200b385a9Seric 	cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
1173eea91d78Seric 	s->s_namecanon.nc_stat = *statp = EX_OK;
117400b385a9Seric 	s->s_namecanon.nc_cname = newstr(cp);
117500b385a9Seric 	return cp;
117699f7cf32Seric }
1177e2f2f828Seric /*
1178e2f2f828Seric **  ANYNET_NTOA -- convert a network address to printable form.
1179e2f2f828Seric **
1180e2f2f828Seric **	Parameters:
1181e2f2f828Seric **		sap -- a pointer to a sockaddr structure.
1182e2f2f828Seric **
1183e2f2f828Seric **	Returns:
1184e2f2f828Seric **		A printable version of that sockaddr.
1185e2f2f828Seric */
1186e2f2f828Seric 
1187e2f2f828Seric char *
1188e2f2f828Seric anynet_ntoa(sap)
1189e2f2f828Seric 	register SOCKADDR *sap;
1190e2f2f828Seric {
1191e2f2f828Seric 	register char *bp;
1192e2f2f828Seric 	register char *ap;
1193e2f2f828Seric 	int l;
1194e387851eSeric 	static char buf[100];
1195e2f2f828Seric 
11968cb4653dSeric 	/* check for null/zero family */
11978cb4653dSeric 	if (sap == NULL)
11988cb4653dSeric 		return "NULLADDR";
11998cb4653dSeric 	if (sap->sa.sa_family == 0)
12008cb4653dSeric 		return "0";
12018cb4653dSeric 
1202e387851eSeric 	switch (sap->sa.sa_family)
1203e387851eSeric 	{
1204e387851eSeric #ifdef MAYBENEXTRELEASE		/*** UNTESTED *** UNTESTED *** UNTESTED ***/
1205139b52c8Seric #ifdef NETUNIX
1206e387851eSeric 	  case AF_UNIX:
1207c24cf5a4Seric 	  	if (sap->sunix.sun_path[0] != '\0')
1208c24cf5a4Seric 	  		sprintf(buf, "[UNIX: %.64s]", sap->sunix.sun_path);
1209e387851eSeric 	  	else
1210e387851eSeric 	  		sprintf(buf, "[UNIX: localhost]");
1211e387851eSeric 		return buf;
1212e387851eSeric #endif
1213139b52c8Seric #endif
1214e387851eSeric 
121583c1f4bcSeric #ifdef NETINET
1216e387851eSeric 	  case AF_INET:
1217e2f2f828Seric 		return inet_ntoa(((struct sockaddr_in *) sap)->sin_addr);
121883c1f4bcSeric #endif
1219e2f2f828Seric 
1220e387851eSeric 	  default:
1221e387851eSeric 	  	/* this case is only to ensure syntactic correctness */
1222e387851eSeric 	  	break;
1223e387851eSeric 	}
1224e387851eSeric 
1225e2f2f828Seric 	/* unknown family -- just dump bytes */
122683c1f4bcSeric 	(void) sprintf(buf, "Family %d: ", sap->sa.sa_family);
1227e2f2f828Seric 	bp = &buf[strlen(buf)];
122883c1f4bcSeric 	ap = sap->sa.sa_data;
122983c1f4bcSeric 	for (l = sizeof sap->sa.sa_data; --l >= 0; )
1230e2f2f828Seric 	{
1231e2f2f828Seric 		(void) sprintf(bp, "%02x:", *ap++ & 0377);
1232e2f2f828Seric 		bp += 3;
1233e2f2f828Seric 	}
1234e2f2f828Seric 	*--bp = '\0';
1235e2f2f828Seric 	return buf;
1236e2f2f828Seric }
12379f8b0eadSeric /*
12389f8b0eadSeric **  HOSTNAMEBYANYADDR -- return name of host based on address
12399f8b0eadSeric **
12409f8b0eadSeric **	Parameters:
12419f8b0eadSeric **		sap -- SOCKADDR pointer
12429f8b0eadSeric **
12439f8b0eadSeric **	Returns:
12449f8b0eadSeric **		text representation of host name.
12459f8b0eadSeric **
12469f8b0eadSeric **	Side Effects:
12479f8b0eadSeric **		none.
12489f8b0eadSeric */
12499f8b0eadSeric 
12509f8b0eadSeric char *
12519f8b0eadSeric hostnamebyanyaddr(sap)
12529f8b0eadSeric 	register SOCKADDR *sap;
12539f8b0eadSeric {
12549f8b0eadSeric 	register struct hostent *hp;
12553490b9dfSeric 	int saveretry;
12563490b9dfSeric 
1257*9d4a8008Seric #if NAMED_BIND
12583490b9dfSeric 	/* shorten name server timeout to avoid higher level timeouts */
12593490b9dfSeric 	saveretry = _res.retry;
12603490b9dfSeric 	_res.retry = 3;
12613490b9dfSeric #endif /* NAMED_BIND */
12623490b9dfSeric 
12639f8b0eadSeric 	switch (sap->sa.sa_family)
12649f8b0eadSeric 	{
12659f8b0eadSeric #ifdef NETINET
12669f8b0eadSeric 	  case AF_INET:
12679f8b0eadSeric 		hp = gethostbyaddr((char *) &sap->sin.sin_addr,
12689f8b0eadSeric 			sizeof sap->sin.sin_addr,
12699f8b0eadSeric 			AF_INET);
12709f8b0eadSeric 		break;
12719f8b0eadSeric #endif
12729f8b0eadSeric 
12739f8b0eadSeric #ifdef NETISO
12749f8b0eadSeric 	  case AF_ISO:
12759f8b0eadSeric 		hp = gethostbyaddr((char *) &sap->siso.siso_addr,
12769f8b0eadSeric 			sizeof sap->siso.siso_addr,
12779f8b0eadSeric 			AF_ISO);
12789f8b0eadSeric 		break;
12799f8b0eadSeric #endif
12809f8b0eadSeric 
1281e387851eSeric #ifdef MAYBENEXTRELEASE		/*** UNTESTED *** UNTESTED *** UNTESTED ***/
1282e387851eSeric 	  case AF_UNIX:
1283e387851eSeric 		hp = NULL;
1284e387851eSeric 		break;
1285e387851eSeric #endif
1286e387851eSeric 
12879f8b0eadSeric 	  default:
12889f8b0eadSeric 		hp = gethostbyaddr(sap->sa.sa_data,
12899f8b0eadSeric 			   sizeof sap->sa.sa_data,
12909f8b0eadSeric 			   sap->sa.sa_family);
12919f8b0eadSeric 		break;
12929f8b0eadSeric 	}
12939f8b0eadSeric 
1294*9d4a8008Seric #if NAMED_BIND
12953490b9dfSeric 	_res.retry = saveretry;
12963490b9dfSeric #endif /* NAMED_BIND */
12973490b9dfSeric 
12989f8b0eadSeric 	if (hp != NULL)
12999f8b0eadSeric 		return hp->h_name;
13009f8b0eadSeric 	else
13019f8b0eadSeric 	{
13029f8b0eadSeric 		/* produce a dotted quad */
13039f8b0eadSeric 		static char buf[512];
13049f8b0eadSeric 
13059f8b0eadSeric 		(void) sprintf(buf, "[%s]", anynet_ntoa(sap));
13069f8b0eadSeric 		return buf;
13079f8b0eadSeric 	}
13089f8b0eadSeric }
1309f36ede03Sbostic 
13106c2c3107Seric # else /* DAEMON */
131199f7cf32Seric /* code for systems without sophisticated networking */
1312444eaf03Seric 
1313444eaf03Seric /*
1314444eaf03Seric **  MYHOSTNAME -- stub version for case of no daemon code.
131521e9914dSeric **
131621e9914dSeric **	Can't convert to upper case here because might be a UUCP name.
1317897f1869Seric **
1318897f1869Seric **	Mark, you can change this to be anything you want......
1319444eaf03Seric */
1320444eaf03Seric 
1321444eaf03Seric char **
1322897f1869Seric myhostname(hostbuf, size)
1323444eaf03Seric 	char hostbuf[];
1324897f1869Seric 	int size;
1325444eaf03Seric {
1326444eaf03Seric 	register FILE *f;
1327444eaf03Seric 
1328444eaf03Seric 	hostbuf[0] = '\0';
1329444eaf03Seric 	f = fopen("/usr/include/whoami", "r");
1330444eaf03Seric 	if (f != NULL)
1331444eaf03Seric 	{
1332897f1869Seric 		(void) fgets(hostbuf, size, f);
1333444eaf03Seric 		fixcrlf(hostbuf, TRUE);
1334444eaf03Seric 		(void) fclose(f);
1335444eaf03Seric 	}
1336444eaf03Seric 	return (NULL);
1337444eaf03Seric }
133899f7cf32Seric /*
13399f8b0eadSeric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
1340320e0d1cSeric **
1341320e0d1cSeric **	Parameters:
1342320e0d1cSeric **		fd -- the descriptor
1343320e0d1cSeric **
1344320e0d1cSeric **	Returns:
1345320e0d1cSeric **		The host name associated with this descriptor, if it can
1346320e0d1cSeric **			be determined.
1347320e0d1cSeric **		NULL otherwise.
1348320e0d1cSeric **
1349320e0d1cSeric **	Side Effects:
1350320e0d1cSeric **		none
1351320e0d1cSeric */
1352320e0d1cSeric 
1353320e0d1cSeric char *
13549f8b0eadSeric getauthinfo(fd)
1355320e0d1cSeric 	int fd;
1356320e0d1cSeric {
1357320e0d1cSeric 	return NULL;
1358320e0d1cSeric }
1359320e0d1cSeric /*
136099f7cf32Seric **  MAPHOSTNAME -- turn a hostname into canonical form
136199f7cf32Seric **
136299f7cf32Seric **	Parameters:
136305b57da8Seric **		map -- a pointer to the database map.
136408de856eSeric **		name -- a buffer containing a hostname.
136515d084d5Seric **		avp -- a pointer to a (cf file defined) argument vector.
13662d29d43aSeric **		statp -- an exit status (out parameter).
136799f7cf32Seric **
136899f7cf32Seric **	Returns:
136915d084d5Seric **		mapped host name
1370cb452edcSeric **		FALSE otherwise.
137199f7cf32Seric **
137299f7cf32Seric **	Side Effects:
137308de856eSeric **		Looks up the host specified in name.  If it is not
137499f7cf32Seric **		the canonical name for that host, replace it with
137599f7cf32Seric **		the canonical name.  If the name is unknown, or it
137699f7cf32Seric **		is already the canonical name, leave it unchanged.
137799f7cf32Seric */
137899f7cf32Seric 
137999f7cf32Seric /*ARGSUSED*/
138015d084d5Seric char *
138108de856eSeric host_map_lookup(map, name, avp, statp)
138205b57da8Seric 	MAP *map;
138308de856eSeric 	char *name;
138415d084d5Seric 	char **avp;
13852d29d43aSeric 	char *statp;
138699f7cf32Seric {
13872d29d43aSeric 	register struct hostent *hp;
13882d29d43aSeric 
138908de856eSeric 	hp = gethostbyname(name);
13902d29d43aSeric 	if (hp != NULL)
13912d29d43aSeric 		return hp->h_name;
13922d29d43aSeric 	*statp = EX_NOHOST;
139315d084d5Seric 	return NULL;
139499f7cf32Seric }
139599f7cf32Seric 
13966c2c3107Seric #endif /* DAEMON */
1397