xref: /original-bsd/usr.sbin/sendmail/src/daemon.c (revision e7988623)
1939f5b94Sdist /*
2759969d8Seric  * Copyright (c) 1983, 1995 Eric P. Allman
3c9d2fa25Sbostic  * Copyright (c) 1988, 1993
4c9d2fa25Sbostic  *	The Regents of the University of California.  All rights reserved.
5da1c6175Sbostic  *
63bc94712Sbostic  * %sccs.include.redist.c%
7939f5b94Sdist  */
8939f5b94Sdist 
97aa493c5Seric #include <errno.h>
106c05f684Sbostic #include "sendmail.h"
117fa39d90Seric 
12af5e902cSeric #ifndef lint
13da1c6175Sbostic #ifdef DAEMON
14*e7988623Seric static char sccsid[] = "@(#)daemon.c	8.85 (Berkeley) 05/13/95 (with daemon mode)";
15d0a9e852Seric #else
16*e7988623Seric static char sccsid[] = "@(#)daemon.c	8.85 (Berkeley) 05/13/95 (without daemon mode)";
17da1c6175Sbostic #endif
18da1c6175Sbostic #endif /* not lint */
19da1c6175Sbostic 
20da1c6175Sbostic #ifdef DAEMON
21d0a9e852Seric 
22d8d0a4aeSeric # include <arpa/inet.h>
23d0a9e852Seric 
249d4a8008Seric #if NAMED_BIND
253490b9dfSeric # include <resolv.h>
263490b9dfSeric #endif
273490b9dfSeric 
287fa39d90Seric /*
297fa39d90Seric **  DAEMON.C -- routines to use when running as a daemon.
3047b12ae1Seric **
3147b12ae1Seric **	This entire file is highly dependent on the 4.2 BSD
3247b12ae1Seric **	interprocess communication primitives.  No attempt has
3347b12ae1Seric **	been made to make this file portable to Version 7,
3447b12ae1Seric **	Version 6, MPX files, etc.  If you should try such a
3547b12ae1Seric **	thing yourself, I recommend chucking the entire file
3647b12ae1Seric **	and starting from scratch.  Basic semantics are:
3747b12ae1Seric **
3847b12ae1Seric **	getrequests()
3947b12ae1Seric **		Opens a port and initiates a connection.
4047b12ae1Seric **		Returns in a child.  Must set InChannel and
4147b12ae1Seric **		OutChannel appropriately.
42b7d7afcbSeric **	clrdaemon()
43b7d7afcbSeric **		Close any open files associated with getting
44b7d7afcbSeric **		the connection; this is used when running the queue,
45b7d7afcbSeric **		etc., to avoid having extra file descriptors during
46b7d7afcbSeric **		the queue run and to avoid confusing the network
47b7d7afcbSeric **		code (if it cares).
48914346b1Seric **	makeconnection(host, port, outfile, infile, usesecureport)
4947b12ae1Seric **		Make a connection to the named host on the given
5047b12ae1Seric **		port.  Set *outfile and *infile to the files
5147b12ae1Seric **		appropriate for communication.  Returns zero on
5247b12ae1Seric **		success, else an exit status describing the
5347b12ae1Seric **		error.
5408de856eSeric **	host_map_lookup(map, hbuf, avp, pstat)
5505b57da8Seric **		Convert the entry in hbuf into a canonical form.
567fa39d90Seric */
577fa39d90Seric /*
587fa39d90Seric **  GETREQUESTS -- open mail IPC port and get requests.
597fa39d90Seric **
607fa39d90Seric **	Parameters:
617fa39d90Seric **		none.
627fa39d90Seric **
637fa39d90Seric **	Returns:
647fa39d90Seric **		none.
657fa39d90Seric **
667fa39d90Seric **	Side Effects:
677fa39d90Seric **		Waits until some interesting activity occurs.  When
687fa39d90Seric **		it does, a child is created to process it, and the
697fa39d90Seric **		parent waits for completion.  Return from this
70147303b1Seric **		routine is always in the child.  The file pointers
71147303b1Seric **		"InChannel" and "OutChannel" should be set to point
72147303b1Seric **		to the communication channel.
737fa39d90Seric */
747fa39d90Seric 
75b7d7afcbSeric int		DaemonSocket	= -1;		/* fd describing socket */
76bfb80540Seric SOCKADDR	DaemonAddr;			/* socket for incoming */
77bfc1eaf8Seric int		ListenQueueSize = 10;		/* size of listen queue */
78b35447dbSeric int		TcpRcvBufferSize = 0;		/* size of TCP receive buffer */
79b35447dbSeric int		TcpSndBufferSize = 0;		/* size of TCP send buffer */
801c71e510Seric 
818b212fe0Seric void
827fa39d90Seric getrequests()
837fa39d90Seric {
841c71e510Seric 	int t;
8515d084d5Seric 	bool refusingconnections = TRUE;
860aae1086Seric 	FILE *pidf;
87dadb8687Seric 	int socksize;
88dfe840b2Seric #ifdef XDEBUG
89dfe840b2Seric 	bool j_has_dot;
90dfe840b2Seric #endif
919b100374Sbostic 	extern void reapchild();
92eb889047Seric 
93a8268164Seric 	/*
941c71e510Seric 	**  Set up the address for the mailer.
95eb889047Seric 	*/
96eb889047Seric 
97bfb80540Seric 	if (DaemonAddr.sin.sin_family == 0)
98bfb80540Seric 		DaemonAddr.sin.sin_family = AF_INET;
99bfb80540Seric 	if (DaemonAddr.sin.sin_addr.s_addr == 0)
100bfb80540Seric 		DaemonAddr.sin.sin_addr.s_addr = INADDR_ANY;
101bfb80540Seric 	if (DaemonAddr.sin.sin_port == 0)
102bfb80540Seric 	{
103e5311662Seric 		register struct servent *sp;
104e5311662Seric 
1051c71e510Seric 		sp = getservbyname("smtp", "tcp");
1061c71e510Seric 		if (sp == NULL)
107d0a9e852Seric 		{
108ad977999Seric 			syserr("554 service \"smtp\" unknown");
109e5311662Seric 			DaemonAddr.sin.sin_port = htons(25);
1101c71e510Seric 		}
111e5311662Seric 		else
112bfb80540Seric 			DaemonAddr.sin.sin_port = sp->s_port;
113bfb80540Seric 	}
1141c71e510Seric 
1151c71e510Seric 	/*
1161c71e510Seric 	**  Try to actually open the connection.
1171c71e510Seric 	*/
1181c71e510Seric 
1191c71e510Seric 	if (tTd(15, 1))
120bfb80540Seric 		printf("getrequests: port 0x%x\n", DaemonAddr.sin.sin_port);
1211c71e510Seric 
1221c71e510Seric 	/* get a socket for the SMTP connection */
12388ea5609Seric 	socksize = opendaemonsocket(TRUE);
1241c71e510Seric 
1252b9178d3Seric 	(void) setsignal(SIGCHLD, reapchild);
12652308a50Seric 
1270aae1086Seric 	/* write the pid to the log file for posterity */
1280aae1086Seric 	pidf = fopen(PidFile, "w");
1290aae1086Seric 	if (pidf != NULL)
1300aae1086Seric 	{
13137950f67Seric 		extern char *CommandLineArgs;
13237950f67Seric 
13337950f67Seric 		/* write the process id on line 1 */
1340aae1086Seric 		fprintf(pidf, "%d\n", getpid());
13537950f67Seric 
13637950f67Seric 		/* line 2 contains all command line flags */
13737950f67Seric 		fprintf(pidf, "%s\n", CommandLineArgs);
13837950f67Seric 
13937950f67Seric 		/* flush and close */
1400aae1086Seric 		fclose(pidf);
1410aae1086Seric 	}
1420aae1086Seric 
143dfe840b2Seric #ifdef XDEBUG
144dfe840b2Seric 	{
14535852b23Seric 		char jbuf[MAXHOSTNAMELEN];
146dfe840b2Seric 
1478b212fe0Seric 		expand("\201j", jbuf, sizeof jbuf, CurEnv);
14835852b23Seric 		j_has_dot = strchr(jbuf, '.') != NULL;
149dfe840b2Seric 	}
150dfe840b2Seric #endif
1510aae1086Seric 
1521c71e510Seric 	if (tTd(15, 1))
153b7d7afcbSeric 		printf("getrequests: %d\n", DaemonSocket);
1541c71e510Seric 
1551c71e510Seric 	for (;;)
1561c71e510Seric 	{
1573a099713Seric 		register int pid;
158a44d5a5eSeric 		auto int lotherend;
15915d084d5Seric 		extern bool refuseconnections();
1608b212fe0Seric 		extern int getla();
1613a099713Seric 
1623a099713Seric 		/* see if we are rejecting connections */
16315d084d5Seric 		CurrentLA = getla();
16415d084d5Seric 		if (refuseconnections())
1656775ec03Sbostic 		{
1660084e6f6Seric 			if (DaemonSocket >= 0)
16715d084d5Seric 			{
1680084e6f6Seric 				/* close socket so peer will fail quickly */
1690084e6f6Seric 				(void) close(DaemonSocket);
1700084e6f6Seric 				DaemonSocket = -1;
17115d084d5Seric 			}
1720084e6f6Seric 			refusingconnections = TRUE;
17371e5e267Seric 			setproctitle("rejecting connections: load average: %d",
17471e5e267Seric 				CurrentLA);
1750084e6f6Seric 			sleep(15);
17615d084d5Seric 			continue;
17715d084d5Seric 		}
17815d084d5Seric 
1798b212fe0Seric 		/* arrange to (re)open the socket if necessary */
18015d084d5Seric 		if (refusingconnections)
18115d084d5Seric 		{
1828a0cb579Seric 			(void) opendaemonsocket(FALSE);
18315d084d5Seric 			setproctitle("accepting connections");
18415d084d5Seric 			refusingconnections = FALSE;
1856775ec03Sbostic 		}
186a44d5a5eSeric 
187dfe840b2Seric #ifdef XDEBUG
188dfe840b2Seric 		/* check for disaster */
189dfe840b2Seric 		{
19035852b23Seric 			char jbuf[MAXHOSTNAMELEN];
191dfe840b2Seric 
1928b212fe0Seric 			expand("\201j", jbuf, sizeof jbuf, CurEnv);
1938b212fe0Seric 			if (!wordinclass(jbuf, 'w'))
194dfe840b2Seric 			{
195dfe840b2Seric 				dumpstate("daemon lost $j");
196dfe840b2Seric 				syslog(LOG_ALERT, "daemon process doesn't have $j in $=w; see syslog");
197dfe840b2Seric 				abort();
198dfe840b2Seric 			}
19935852b23Seric 			else if (j_has_dot && strchr(jbuf, '.') == NULL)
200dfe840b2Seric 			{
201dfe840b2Seric 				dumpstate("daemon $j lost dot");
202dfe840b2Seric 				syslog(LOG_ALERT, "daemon process $j lost dot; see syslog");
203dfe840b2Seric 				abort();
204dfe840b2Seric 			}
205dfe840b2Seric 		}
206dfe840b2Seric #endif
207dfe840b2Seric 
2081c71e510Seric 		/* wait for a connection */
2091c71e510Seric 		do
2101c71e510Seric 		{
2111c71e510Seric 			errno = 0;
212dadb8687Seric 			lotherend = socksize;
2139b100374Sbostic 			t = accept(DaemonSocket,
2149b100374Sbostic 			    (struct sockaddr *)&RealHostAddr, &lotherend);
2151c71e510Seric 		} while (t < 0 && errno == EINTR);
2161c71e510Seric 		if (t < 0)
2171c71e510Seric 		{
2181c71e510Seric 			syserr("getrequests: accept");
2198b212fe0Seric 
2208b212fe0Seric 			/* arrange to re-open the socket next time around */
2218b212fe0Seric 			(void) close(DaemonSocket);
2228b212fe0Seric 			DaemonSocket = -1;
2231c71e510Seric 			sleep(5);
2241c71e510Seric 			continue;
2251c71e510Seric 		}
226d0a9e852Seric 
227d0a9e852Seric 		/*
228d0a9e852Seric 		**  Create a subprocess to process the mail.
229d0a9e852Seric 		*/
230d0a9e852Seric 
23161e4310fSeric 		if (tTd(15, 2))
2321c71e510Seric 			printf("getrequests: forking (fd = %d)\n", t);
233eb889047Seric 
234a8268164Seric 		pid = fork();
235a8268164Seric 		if (pid < 0)
236a8268164Seric 		{
237a8268164Seric 			syserr("daemon: cannot fork");
238a8268164Seric 			sleep(10);
2391c71e510Seric 			(void) close(t);
240a8268164Seric 			continue;
241a8268164Seric 		}
242a8268164Seric 
243a8268164Seric 		if (pid == 0)
244a8268164Seric 		{
245da662164Seric 			char *p;
2469f8b0eadSeric 			extern char *hostnamebyanyaddr();
2478b212fe0Seric 			extern void intsig();
248a44d5a5eSeric 
249a8268164Seric 			/*
250a8268164Seric 			**  CHILD -- return to caller.
251a44d5a5eSeric 			**	Collect verified idea of sending host.
252a8268164Seric 			**	Verify calling user id if possible here.
253a8268164Seric 			*/
254a8268164Seric 
2552b9178d3Seric 			(void) setsignal(SIGCHLD, SIG_DFL);
2568b212fe0Seric 			(void) setsignal(SIGHUP, intsig);
2578b212fe0Seric 			(void) close(DaemonSocket);
2589f9b003eSeric 			DisConnected = FALSE;
259779ac194Seric 
2604dd09a90Seric 			setproctitle("startup with %s",
2614dd09a90Seric 				anynet_ntoa(&RealHostAddr));
2624dd09a90Seric 
263a44d5a5eSeric 			/* determine host name */
264da662164Seric 			p = hostnamebyanyaddr(&RealHostAddr);
265da662164Seric 			RealHostName = newstr(p);
2664dd09a90Seric 			setproctitle("startup with %s", p);
26729dcf4baSeric 
2682a6bc25bSeric #ifdef LOG
2691f2ff1a4Seric 			if (LogLevel > 11)
2702a6bc25bSeric 			{
2712a6bc25bSeric 				/* log connection information */
2722a6bc25bSeric 				syslog(LOG_INFO, "connect from %s (%s)",
2739f8b0eadSeric 					RealHostName, anynet_ntoa(&RealHostAddr));
2742a6bc25bSeric 			}
2752a6bc25bSeric #endif
2762a6bc25bSeric 
277335eae58Seric 			if ((InChannel = fdopen(t, "r")) == NULL ||
278335eae58Seric 			    (t = dup(t)) < 0 ||
279335eae58Seric 			    (OutChannel = fdopen(t, "w")) == NULL)
280335eae58Seric 			{
281335eae58Seric 				syserr("cannot open SMTP server channel, fd=%d", t);
282335eae58Seric 				exit(0);
283335eae58Seric 			}
284244b09d1Seric 
28529dcf4baSeric 			/* should we check for illegal connection here? XXX */
286e17a3a5aSeric #ifdef XLA
287e17a3a5aSeric 			if (!xla_host_ok(RealHostName))
288e17a3a5aSeric 			{
289244b09d1Seric 				message("421 Too many SMTP sessions for this host");
290e17a3a5aSeric 				exit(0);
291e17a3a5aSeric 			}
292e17a3a5aSeric #endif
293a44d5a5eSeric 
29461e4310fSeric 			if (tTd(15, 2))
295d0a9e852Seric 				printf("getreq: returning\n");
296a8268164Seric 			return;
297a8268164Seric 		}
298a8268164Seric 
2993c154354Seric 		/* close the port so that others will hang (for a while) */
3003c154354Seric 		(void) close(t);
3018e3e4b17Seric 	}
3023c154354Seric 	/*NOTREACHED*/
3033c154354Seric }
3048e3e4b17Seric /*
3050084e6f6Seric **  OPENDAEMONSOCKET -- open the SMTP socket
3060084e6f6Seric **
3070084e6f6Seric **	Deals with setting all appropriate options.  DaemonAddr must
3080084e6f6Seric **	be set up in advance.
3090084e6f6Seric **
3100084e6f6Seric **	Parameters:
31188ea5609Seric **		firsttime -- set if this is the initial open.
3120084e6f6Seric **
3130084e6f6Seric **	Returns:
3140084e6f6Seric **		Size in bytes of the daemon socket addr.
3150084e6f6Seric **
3160084e6f6Seric **	Side Effects:
3170084e6f6Seric **		Leaves DaemonSocket set to the open socket.
3180084e6f6Seric **		Exits if the socket cannot be created.
3190084e6f6Seric */
3200084e6f6Seric 
3216173568dSeric #define MAXOPENTRIES	10	/* maximum number of tries to open connection */
3226173568dSeric 
3230084e6f6Seric int
32488ea5609Seric opendaemonsocket(firsttime)
32588ea5609Seric 	bool firsttime;
3260084e6f6Seric {
3270084e6f6Seric 	int on = 1;
3288b212fe0Seric 	int socksize = 0;
3296173568dSeric 	int ntries = 0;
3306173568dSeric 	int saveerrno;
3310084e6f6Seric 
3320084e6f6Seric 	if (tTd(15, 2))
3330084e6f6Seric 		printf("opendaemonsocket()\n");
3340084e6f6Seric 
3356173568dSeric 	do
3366173568dSeric 	{
337254b93c3Seric 		if (ntries > 0)
338254b93c3Seric 			sleep(5);
33988ea5609Seric 		if (firsttime || DaemonSocket < 0)
34088ea5609Seric 		{
3410084e6f6Seric 			DaemonSocket = socket(DaemonAddr.sa.sa_family, SOCK_STREAM, 0);
3420084e6f6Seric 			if (DaemonSocket < 0)
3430084e6f6Seric 			{
3440084e6f6Seric 				/* probably another daemon already */
3456173568dSeric 				saveerrno = errno;
3460084e6f6Seric 				syserr("opendaemonsocket: can't create server SMTP socket");
3470084e6f6Seric 			  severe:
3480084e6f6Seric # ifdef LOG
3490084e6f6Seric 				if (LogLevel > 0)
3500084e6f6Seric 					syslog(LOG_ALERT, "problem creating SMTP socket");
3510084e6f6Seric # endif /* LOG */
3526173568dSeric 				DaemonSocket = -1;
3536173568dSeric 				continue;
3540084e6f6Seric 			}
3550084e6f6Seric 
3560084e6f6Seric 			/* turn on network debugging? */
3570084e6f6Seric 			if (tTd(15, 101))
3586173568dSeric 				(void) setsockopt(DaemonSocket, SOL_SOCKET,
3596173568dSeric 						  SO_DEBUG, (char *)&on,
3606173568dSeric 						  sizeof on);
3610084e6f6Seric 
3626173568dSeric 			(void) setsockopt(DaemonSocket, SOL_SOCKET,
3636173568dSeric 					  SO_REUSEADDR, (char *)&on, sizeof on);
3646173568dSeric 			(void) setsockopt(DaemonSocket, SOL_SOCKET,
3656173568dSeric 					  SO_KEEPALIVE, (char *)&on, sizeof on);
3660084e6f6Seric 
3670084e6f6Seric #ifdef SO_RCVBUF
3680084e6f6Seric 			if (TcpRcvBufferSize > 0)
3690084e6f6Seric 			{
3706173568dSeric 				if (setsockopt(DaemonSocket, SOL_SOCKET,
3716173568dSeric 					       SO_RCVBUF,
3720084e6f6Seric 					       (char *) &TcpRcvBufferSize,
3730084e6f6Seric 					       sizeof(TcpRcvBufferSize)) < 0)
3740084e6f6Seric 					syserr("getrequests: setsockopt(SO_RCVBUF)");
3750084e6f6Seric 			}
3760084e6f6Seric #endif
3770084e6f6Seric 
3780084e6f6Seric 			switch (DaemonAddr.sa.sa_family)
3790084e6f6Seric 			{
3800084e6f6Seric # ifdef NETINET
3810084e6f6Seric 			  case AF_INET:
3820084e6f6Seric 				socksize = sizeof DaemonAddr.sin;
3830084e6f6Seric 				break;
3840084e6f6Seric # endif
3850084e6f6Seric 
3860084e6f6Seric # ifdef NETISO
3870084e6f6Seric 			  case AF_ISO:
3880084e6f6Seric 				socksize = sizeof DaemonAddr.siso;
3890084e6f6Seric 				break;
3900084e6f6Seric # endif
3910084e6f6Seric 
3920084e6f6Seric 			  default:
3930084e6f6Seric 				socksize = sizeof DaemonAddr;
3940084e6f6Seric 				break;
3950084e6f6Seric 			}
3960084e6f6Seric 
3970084e6f6Seric 			if (bind(DaemonSocket, &DaemonAddr.sa, socksize) < 0)
3980084e6f6Seric 			{
3996173568dSeric 				saveerrno = errno;
4000084e6f6Seric 				syserr("getrequests: cannot bind");
4010084e6f6Seric 				(void) close(DaemonSocket);
4020084e6f6Seric 				goto severe;
4030084e6f6Seric 			}
40488ea5609Seric 		}
40588ea5609Seric 		if (!firsttime && listen(DaemonSocket, ListenQueueSize) < 0)
4060084e6f6Seric 		{
4076173568dSeric 			saveerrno = errno;
4080084e6f6Seric 			syserr("getrequests: cannot listen");
4090084e6f6Seric 			(void) close(DaemonSocket);
4100084e6f6Seric 			goto severe;
4110084e6f6Seric 		}
4120084e6f6Seric 		return socksize;
4136173568dSeric 	} while (ntries++ < MAXOPENTRIES && transienterror(saveerrno));
4148b212fe0Seric 	syserr("!opendaemonsocket: server SMTP socket wedged: exiting");
4156173568dSeric 	finis();
4160084e6f6Seric }
4170084e6f6Seric /*
418b7d7afcbSeric **  CLRDAEMON -- reset the daemon connection
419b7d7afcbSeric **
420b7d7afcbSeric **	Parameters:
421b7d7afcbSeric **		none.
422b7d7afcbSeric **
423b7d7afcbSeric **	Returns:
424b7d7afcbSeric **		none.
425b7d7afcbSeric **
426b7d7afcbSeric **	Side Effects:
427b7d7afcbSeric **		releases any resources used by the passive daemon.
428b7d7afcbSeric */
429b7d7afcbSeric 
4308b212fe0Seric void
431b7d7afcbSeric clrdaemon()
432b7d7afcbSeric {
433b7d7afcbSeric 	if (DaemonSocket >= 0)
434b7d7afcbSeric 		(void) close(DaemonSocket);
435b7d7afcbSeric 	DaemonSocket = -1;
436b7d7afcbSeric }
437b7d7afcbSeric /*
438bfb80540Seric **  SETDAEMONOPTIONS -- set options for running the daemon
439bfb80540Seric **
440bfb80540Seric **	Parameters:
441bfb80540Seric **		p -- the options line.
442bfb80540Seric **
443bfb80540Seric **	Returns:
444bfb80540Seric **		none.
445bfb80540Seric */
446bfb80540Seric 
4478b212fe0Seric void
448bfb80540Seric setdaemonoptions(p)
449bfb80540Seric 	register char *p;
450bfb80540Seric {
451850144caSeric 	if (DaemonAddr.sa.sa_family == AF_UNSPEC)
452850144caSeric 		DaemonAddr.sa.sa_family = AF_INET;
453850144caSeric 
454bfb80540Seric 	while (p != NULL)
455bfb80540Seric 	{
456bfb80540Seric 		register char *f;
457bfb80540Seric 		register char *v;
458bfb80540Seric 
459bfb80540Seric 		while (isascii(*p) && isspace(*p))
460bfb80540Seric 			p++;
461bfb80540Seric 		if (*p == '\0')
462bfb80540Seric 			break;
463bfb80540Seric 		f = p;
464bfb80540Seric 		p = strchr(p, ',');
465bfb80540Seric 		if (p != NULL)
466bfb80540Seric 			*p++ = '\0';
467bfb80540Seric 		v = strchr(f, '=');
468bfb80540Seric 		if (v == NULL)
469bfb80540Seric 			continue;
470bfb80540Seric 		while (isascii(*++v) && isspace(*v))
471bfb80540Seric 			continue;
472*e7988623Seric 		if (isascii(*f) && isupper(*f))
473*e7988623Seric 			*f = tolower(*f);
474bfb80540Seric 
475bfb80540Seric 		switch (*f)
476bfb80540Seric 		{
477850144caSeric 		  case 'F':		/* address family */
478850144caSeric 			if (isascii(*v) && isdigit(*v))
479850144caSeric 				DaemonAddr.sa.sa_family = atoi(v);
480850144caSeric #ifdef NETINET
481850144caSeric 			else if (strcasecmp(v, "inet") == 0)
482850144caSeric 				DaemonAddr.sa.sa_family = AF_INET;
483850144caSeric #endif
484850144caSeric #ifdef NETISO
485850144caSeric 			else if (strcasecmp(v, "iso") == 0)
486850144caSeric 				DaemonAddr.sa.sa_family = AF_ISO;
487850144caSeric #endif
488850144caSeric #ifdef NETNS
489850144caSeric 			else if (strcasecmp(v, "ns") == 0)
490850144caSeric 				DaemonAddr.sa.sa_family = AF_NS;
491850144caSeric #endif
492850144caSeric #ifdef NETX25
493850144caSeric 			else if (strcasecmp(v, "x.25") == 0)
494850144caSeric 				DaemonAddr.sa.sa_family = AF_CCITT;
495850144caSeric #endif
496850144caSeric 			else
497850144caSeric 				syserr("554 Unknown address family %s in Family=option", v);
498850144caSeric 			break;
499850144caSeric 
500850144caSeric 		  case 'A':		/* address */
501850144caSeric 			switch (DaemonAddr.sa.sa_family)
502850144caSeric 			{
503850144caSeric #ifdef NETINET
504850144caSeric 			  case AF_INET:
505850144caSeric 				if (isascii(*v) && isdigit(*v))
5068b212fe0Seric 					DaemonAddr.sin.sin_addr.s_addr = htonl(inet_network(v));
507850144caSeric 				else
508850144caSeric 				{
509850144caSeric 					register struct netent *np;
510850144caSeric 
511850144caSeric 					np = getnetbyname(v);
512850144caSeric 					if (np == NULL)
513850144caSeric 						syserr("554 network \"%s\" unknown", v);
514850144caSeric 					else
515850144caSeric 						DaemonAddr.sin.sin_addr.s_addr = np->n_net;
516850144caSeric 				}
517850144caSeric 				break;
518850144caSeric #endif
519850144caSeric 
520850144caSeric 			  default:
521850144caSeric 				syserr("554 Address= option unsupported for family %d",
522850144caSeric 					DaemonAddr.sa.sa_family);
523850144caSeric 				break;
524850144caSeric 			}
525850144caSeric 			break;
526850144caSeric 
527bfb80540Seric 		  case 'P':		/* port */
528850144caSeric 			switch (DaemonAddr.sa.sa_family)
529850144caSeric 			{
530850144caSeric 				short port;
531850144caSeric 
532850144caSeric #ifdef NETINET
533850144caSeric 			  case AF_INET:
534bfb80540Seric 				if (isascii(*v) && isdigit(*v))
53576b70c58Seric 					DaemonAddr.sin.sin_port = htons(atoi(v));
536bfb80540Seric 				else
537bfb80540Seric 				{
538bfb80540Seric 					register struct servent *sp;
539bfb80540Seric 
540bfb80540Seric 					sp = getservbyname(v, "tcp");
541bfb80540Seric 					if (sp == NULL)
542ad977999Seric 						syserr("554 service \"%s\" unknown", v);
543bfb80540Seric 					else
544bfb80540Seric 						DaemonAddr.sin.sin_port = sp->s_port;
545bfb80540Seric 				}
546bfb80540Seric 				break;
547850144caSeric #endif
548bfb80540Seric 
549850144caSeric #ifdef NETISO
550850144caSeric 			  case AF_ISO:
551850144caSeric 				/* assume two byte transport selector */
552bfb80540Seric 				if (isascii(*v) && isdigit(*v))
55376b70c58Seric 					port = htons(atoi(v));
554bfb80540Seric 				else
555bfb80540Seric 				{
556850144caSeric 					register struct servent *sp;
557bfb80540Seric 
558850144caSeric 					sp = getservbyname(v, "tcp");
559850144caSeric 					if (sp == NULL)
560ad977999Seric 						syserr("554 service \"%s\" unknown", v);
561bfb80540Seric 					else
562850144caSeric 						port = sp->s_port;
563850144caSeric 				}
564850144caSeric 				bcopy((char *) &port, TSEL(&DaemonAddr.siso), 2);
565850144caSeric 				break;
566850144caSeric #endif
567850144caSeric 
568850144caSeric 			  default:
569850144caSeric 				syserr("554 Port= option unsupported for family %d",
570850144caSeric 					DaemonAddr.sa.sa_family);
571850144caSeric 				break;
572bfb80540Seric 			}
573bfb80540Seric 			break;
574bfc1eaf8Seric 
575bfc1eaf8Seric 		  case 'L':		/* listen queue size */
576bfc1eaf8Seric 			ListenQueueSize = atoi(v);
577bfc1eaf8Seric 			break;
578b35447dbSeric 
579b35447dbSeric 		  case 'S':		/* send buffer size */
580b35447dbSeric 			TcpSndBufferSize = atoi(v);
581b35447dbSeric 			break;
582b35447dbSeric 
583b35447dbSeric 		  case 'R':		/* receive buffer size */
584b35447dbSeric 			TcpRcvBufferSize = atoi(v);
585b35447dbSeric 			break;
586*e7988623Seric 
587*e7988623Seric 		  default:
588*e7988623Seric 			syserr("554 DaemonPortOptions parameter \"%s\" unknown", f);
589bfb80540Seric 		}
590bfb80540Seric 	}
591bfb80540Seric }
592bfb80540Seric /*
5937aa493c5Seric **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
5947aa493c5Seric **
5957aa493c5Seric **	Parameters:
5967aa493c5Seric **		host -- the name of the host.
59748ff0a9dSeric **		port -- the port number to connect to.
598655feedbSeric **		mci -- a pointer to the mail connection information
599655feedbSeric **			structure to be filled in.
600914346b1Seric **		usesecureport -- if set, use a low numbered (reserved)
601914346b1Seric **			port to provide some rudimentary authentication.
6027aa493c5Seric **
6037aa493c5Seric **	Returns:
6047aa493c5Seric **		An exit code telling whether the connection could be
6057aa493c5Seric **			made and if not why not.
6067aa493c5Seric **
6077aa493c5Seric **	Side Effects:
6087aa493c5Seric **		none.
6097aa493c5Seric */
6107aa493c5Seric 
611e2f2f828Seric SOCKADDR	CurHostAddr;		/* address of current host */
61271ff6caaSeric 
613b31e7f2bSeric int
614655feedbSeric makeconnection(host, port, mci, usesecureport)
6157aa493c5Seric 	char *host;
616210215eaSeric 	u_short port;
617b31e7f2bSeric 	register MCI *mci;
618914346b1Seric 	bool usesecureport;
6197aa493c5Seric {
6208b212fe0Seric 	register int i = 0;
6218b212fe0Seric 	register int s;
62204344589Sbloom 	register struct hostent *hp = (struct hostent *)NULL;
623e2f2f828Seric 	SOCKADDR addr;
6246286bb75Sbloom 	int sav_errno;
625e2f2f828Seric 	int addrlen;
6268b212fe0Seric 	bool firstconnect;
6279d4a8008Seric #if NAMED_BIND
628134746fbSeric 	extern int h_errno;
629134746fbSeric #endif
6307aa493c5Seric 
6317aa493c5Seric 	/*
6327aa493c5Seric 	**  Set up the address for the mailer.
63371096d12Seric 	**	Accept "[a.b.c.d]" syntax for host name.
6347aa493c5Seric 	*/
6357aa493c5Seric 
6369d4a8008Seric #if NAMED_BIND
637794bdbb9Smiriam 	h_errno = 0;
638134746fbSeric #endif
639794bdbb9Smiriam 	errno = 0;
640967778e2Seric 	bzero(&CurHostAddr, sizeof CurHostAddr);
641c931b82bSeric 	SmtpPhase = mci->mci_phase = "initial connection";
642d945ebe8Seric 	CurHostName = host;
643794bdbb9Smiriam 
64471096d12Seric 	if (host[0] == '[')
64571096d12Seric 	{
646a44d5a5eSeric 		long hid;
6476c2c3107Seric 		register char *p = strchr(host, ']');
64871096d12Seric 
649a44d5a5eSeric 		if (p != NULL)
65071096d12Seric 		{
651a44d5a5eSeric 			*p = '\0';
6524d9c42c2Seric #ifdef NETINET
653a44d5a5eSeric 			hid = inet_addr(&host[1]);
654a7e21fe6Seric 			if (hid == -1)
6554d9c42c2Seric #endif
656a7e21fe6Seric 			{
657a7e21fe6Seric 				/* try it as a host name (avoid MX lookup) */
6588b212fe0Seric 				hp = sm_gethostbyname(&host[1]);
659d8984352Seric 				if (hp == NULL && p[-1] == '.')
660d8984352Seric 				{
6618b212fe0Seric #if NAMED_BIND
6628b212fe0Seric 					int oldopts = _res.options;
6638b212fe0Seric 
6648b212fe0Seric 					_res.options &= ~(RES_DEFNAMES|RES_DNSRCH);
6658b212fe0Seric #endif
666d8984352Seric 					p[-1] = '\0';
6678b212fe0Seric 					hp = sm_gethostbyname(&host[1]);
668d8984352Seric 					p[-1] = '.';
6698b212fe0Seric #if NAMED_BIND
6708b212fe0Seric 					_res.options = oldopts;
6718b212fe0Seric #endif
672d8984352Seric 				}
673a7e21fe6Seric 				*p = ']';
674a7e21fe6Seric 				goto gothostent;
675a7e21fe6Seric 			}
676a44d5a5eSeric 			*p = ']';
67771096d12Seric 		}
678a7e21fe6Seric 		if (p == NULL)
67971096d12Seric 		{
68008b25121Seric 			usrerr("553 Invalid numeric domain spec \"%s\"", host);
6815b068d51Seric 			mci->mci_status = "5.1.2";
68271096d12Seric 			return (EX_NOHOST);
68371096d12Seric 		}
6844d9c42c2Seric #ifdef NETINET
6854d9c42c2Seric 		addr.sin.sin_family = AF_INET;		/*XXX*/
68683c1f4bcSeric 		addr.sin.sin_addr.s_addr = hid;
6874d9c42c2Seric #endif
68871096d12Seric 	}
6891c71e510Seric 	else
6901c71e510Seric 	{
691d8984352Seric 		register char *p = &host[strlen(host) - 1];
692d8984352Seric 
6938b212fe0Seric 		hp = sm_gethostbyname(host);
694d8984352Seric 		if (hp == NULL && *p == '.')
695d8984352Seric 		{
6968b212fe0Seric #if NAMED_BIND
6978b212fe0Seric 			int oldopts = _res.options;
6988b212fe0Seric 
6998b212fe0Seric 			_res.options &= ~(RES_DEFNAMES|RES_DNSRCH);
7008b212fe0Seric #endif
701d8984352Seric 			*p = '\0';
7028b212fe0Seric 			hp = sm_gethostbyname(host);
703d8984352Seric 			*p = '.';
7048b212fe0Seric #if NAMED_BIND
7058b212fe0Seric 			_res.options = oldopts;
7068b212fe0Seric #endif
707d8984352Seric 		}
708a7e21fe6Seric gothostent:
709794bdbb9Smiriam 		if (hp == NULL)
710794bdbb9Smiriam 		{
7119d4a8008Seric #if NAMED_BIND
7128b212fe0Seric 			/* check for name server timeouts */
7138b212fe0Seric 			if (errno == ETIMEDOUT || h_errno == TRY_AGAIN ||
7148b212fe0Seric 			    (errno == ECONNREFUSED && UseNameServer))
7158b212fe0Seric 			{
7168b212fe0Seric 				mci->mci_status = "4.4.3";
71752308a50Seric 				return (EX_TEMPFAIL);
7188b212fe0Seric 			}
719134746fbSeric #endif
7207aa493c5Seric 			return (EX_NOHOST);
721794bdbb9Smiriam 		}
72283c1f4bcSeric 		addr.sa.sa_family = hp->h_addrtype;
72383c1f4bcSeric 		switch (hp->h_addrtype)
72483c1f4bcSeric 		{
72583c1f4bcSeric #ifdef NETINET
72683c1f4bcSeric 		  case AF_INET:
727e2f2f828Seric 			bcopy(hp->h_addr,
72883c1f4bcSeric 				&addr.sin.sin_addr,
7298b212fe0Seric 				INADDRSZ);
73083c1f4bcSeric 			break;
73183c1f4bcSeric #endif
73283c1f4bcSeric 
73383c1f4bcSeric 		  default:
734e2f2f828Seric 			bcopy(hp->h_addr,
73583c1f4bcSeric 				addr.sa.sa_data,
736e2f2f828Seric 				hp->h_length);
73783c1f4bcSeric 			break;
73883c1f4bcSeric 		}
73904344589Sbloom 		i = 1;
7401c71e510Seric 	}
7411c71e510Seric 
7421c71e510Seric 	/*
7431c71e510Seric 	**  Determine the port number.
7441c71e510Seric 	*/
7451c71e510Seric 
746fd7c0790Seric 	if (port != 0)
747e2f2f828Seric 		port = htons(port);
748fd7c0790Seric 	else
7491c71e510Seric 	{
7501c71e510Seric 		register struct servent *sp = getservbyname("smtp", "tcp");
7511c71e510Seric 
7521c71e510Seric 		if (sp == NULL)
7531c71e510Seric 		{
7543edb2e00Seric #ifdef LOG
7553edb2e00Seric 			if (LogLevel > 2)
7563edb2e00Seric 				syslog(LOG_ERR, "makeconnection: service \"smtp\" unknown");
7573edb2e00Seric #endif
758e5311662Seric 			port = htons(25);
7591c71e510Seric 		}
760e5311662Seric 		else
761e2f2f828Seric 			port = sp->s_port;
762e2f2f828Seric 	}
763e2f2f828Seric 
76483c1f4bcSeric 	switch (addr.sa.sa_family)
765e2f2f828Seric 	{
7664d9c42c2Seric #ifdef NETINET
767e2f2f828Seric 	  case AF_INET:
76883c1f4bcSeric 		addr.sin.sin_port = port;
769e2f2f828Seric 		addrlen = sizeof (struct sockaddr_in);
770e2f2f828Seric 		break;
7714d9c42c2Seric #endif
772e2f2f828Seric 
773e2f2f828Seric #ifdef NETISO
774e2f2f828Seric 	  case AF_ISO:
775e2f2f828Seric 		/* assume two byte transport selector */
776e2f2f828Seric 		bcopy((char *) &port, TSEL((struct sockaddr_iso *) &addr), 2);
777e2f2f828Seric 		addrlen = sizeof (struct sockaddr_iso);
778e2f2f828Seric 		break;
779e2f2f828Seric #endif
780e2f2f828Seric 
781e2f2f828Seric 	  default:
78283c1f4bcSeric 		syserr("Can't connect to address family %d", addr.sa.sa_family);
783e2f2f828Seric 		return (EX_NOHOST);
7841c71e510Seric 	}
7857aa493c5Seric 
7867aa493c5Seric 	/*
7877aa493c5Seric 	**  Try to actually open the connection.
7887aa493c5Seric 	*/
7897aa493c5Seric 
790e17a3a5aSeric #ifdef XLA
791e17a3a5aSeric 	/* if too many connections, don't bother trying */
792e17a3a5aSeric 	if (!xla_noqueue_ok(host))
793e17a3a5aSeric 		return EX_TEMPFAIL;
794e17a3a5aSeric #endif
795e17a3a5aSeric 
7968b212fe0Seric 	firstconnect = TRUE;
797aea02ca1Seric 	for (;;)
798aea02ca1Seric 	{
79961e4310fSeric 		if (tTd(16, 1))
800e2f2f828Seric 			printf("makeconnection (%s [%s])\n",
801e2f2f828Seric 				host, anynet_ntoa(&addr));
8027aa493c5Seric 
803226e3022Seric 		/* save for logging */
804226e3022Seric 		CurHostAddr = addr;
805226e3022Seric 
806914346b1Seric 		if (usesecureport)
807914346b1Seric 		{
808914346b1Seric 			int rport = IPPORT_RESERVED - 1;
809914346b1Seric 
810914346b1Seric 			s = rresvport(&rport);
811914346b1Seric 		}
812914346b1Seric 		else
813914346b1Seric 		{
814af5e902cSeric 			s = socket(AF_INET, SOCK_STREAM, 0);
815914346b1Seric 		}
8167aa493c5Seric 		if (s < 0)
8177aa493c5Seric 		{
8186286bb75Sbloom 			sav_errno = errno;
819914346b1Seric 			syserr("makeconnection: no socket");
8207aa493c5Seric 			goto failure;
8217aa493c5Seric 		}
8227aa493c5Seric 
823b35447dbSeric #ifdef SO_SNDBUF
824b35447dbSeric 		if (TcpSndBufferSize > 0)
825b35447dbSeric 		{
826b35447dbSeric 			if (setsockopt(s, SOL_SOCKET, SO_SNDBUF,
827bf217a95Seric 				       (char *) &TcpSndBufferSize,
828b35447dbSeric 				       sizeof(TcpSndBufferSize)) < 0)
829b35447dbSeric 				syserr("makeconnection: setsockopt(SO_SNDBUF)");
830b35447dbSeric 		}
831b35447dbSeric #endif
832b35447dbSeric 
83361e4310fSeric 		if (tTd(16, 1))
834b31e7f2bSeric 			printf("makeconnection: fd=%d\n", s);
8351b6e4a15Seric 
8361b6e4a15Seric 		/* turn on network debugging? */
837a2ef5fa4Seric 		if (tTd(16, 101))
83852308a50Seric 		{
83952308a50Seric 			int on = 1;
8406173568dSeric 			(void) setsockopt(s, SOL_SOCKET, SO_DEBUG,
841aea02ca1Seric 					  (char *)&on, sizeof on);
84252308a50Seric 		}
84387d6e633Srick 		if (CurEnv->e_xfp != NULL)
844877a6142Seric 			(void) fflush(CurEnv->e_xfp);		/* for debugging */
8454bd6a662Seric 		errno = 0;					/* for debugging */
846e2f2f828Seric 		if (connect(s, (struct sockaddr *) &addr, addrlen) >= 0)
847aea02ca1Seric 			break;
848aea02ca1Seric 
8498b212fe0Seric 		/* if running demand-dialed connection, try again */
8508b212fe0Seric 		if (DialDelay > 0 && firstconnect)
8518b212fe0Seric 		{
8528b212fe0Seric 			if (tTd(16, 1))
8538b212fe0Seric 				printf("Connect failed (%s); trying again...\n",
8548b212fe0Seric 					errstring(sav_errno));
8558b212fe0Seric 			firstconnect = FALSE;
8568b212fe0Seric 			sleep(DialDelay);
8578b212fe0Seric 			continue;
8588b212fe0Seric 		}
8598b212fe0Seric 
860aea02ca1Seric 		/* couldn't connect.... figure out why */
8616286bb75Sbloom 		sav_errno = errno;
8626286bb75Sbloom 		(void) close(s);
8638b212fe0Seric 		if (hp != NULL && hp->h_addr_list[i])
86404344589Sbloom 		{
865aea02ca1Seric 			if (tTd(16, 1))
866e2f2f828Seric 				printf("Connect failed (%s); trying new address....\n",
867e2f2f828Seric 					errstring(sav_errno));
86883c1f4bcSeric 			switch (addr.sa.sa_family)
86983c1f4bcSeric 			{
87083c1f4bcSeric #ifdef NETINET
87183c1f4bcSeric 			  case AF_INET:
872e2f2f828Seric 				bcopy(hp->h_addr_list[i++],
87383c1f4bcSeric 				      &addr.sin.sin_addr,
8748b212fe0Seric 				      INADDRSZ);
87583c1f4bcSeric 				break;
87683c1f4bcSeric #endif
87783c1f4bcSeric 
87883c1f4bcSeric 			  default:
879e2f2f828Seric 				bcopy(hp->h_addr_list[i++],
88083c1f4bcSeric 					addr.sa.sa_data,
881914346b1Seric 					hp->h_length);
88283c1f4bcSeric 				break;
88383c1f4bcSeric 			}
884aea02ca1Seric 			continue;
88504344589Sbloom 		}
88604344589Sbloom 
8877aa493c5Seric 		/* failure, decide if temporary or not */
8887aa493c5Seric 	failure:
889244b09d1Seric #ifdef XLA
890244b09d1Seric 		xla_host_end(host);
891244b09d1Seric #endif
892e2de2524Seric 		if (transienterror(sav_errno))
893e2de2524Seric 			return EX_TEMPFAIL;
894e2de2524Seric 		else
89587d6e633Srick 		{
89608b25121Seric 			message("%s", errstring(sav_errno));
8977aa493c5Seric 			return (EX_UNAVAILABLE);
8987aa493c5Seric 		}
8997aa493c5Seric 	}
9007aa493c5Seric 
9017aa493c5Seric 	/* connection ok, put it into canonical form */
902335eae58Seric 	if ((mci->mci_out = fdopen(s, "w")) == NULL ||
903335eae58Seric 	    (s = dup(s)) < 0 ||
904ab81ee53Seric 	    (mci->mci_in = fdopen(s, "r")) == NULL)
905335eae58Seric 	{
906335eae58Seric 		syserr("cannot open SMTP client channel, fd=%d", s);
907335eae58Seric 		return EX_TEMPFAIL;
908335eae58Seric 	}
9097aa493c5Seric 
910dca8e1f7Seric 	return (EX_OK);
9117aa493c5Seric }
912444eaf03Seric /*
913444eaf03Seric **  MYHOSTNAME -- return the name of this host.
914444eaf03Seric **
915444eaf03Seric **	Parameters:
916444eaf03Seric **		hostbuf -- a place to return the name of this host.
917897f1869Seric **		size -- the size of hostbuf.
918444eaf03Seric **
919444eaf03Seric **	Returns:
920444eaf03Seric **		A list of aliases for this host.
921444eaf03Seric **
922444eaf03Seric **	Side Effects:
923d8d0a4aeSeric **		Adds numeric codes to $=w.
924444eaf03Seric */
925444eaf03Seric 
9268b212fe0Seric struct hostent *
927897f1869Seric myhostname(hostbuf, size)
928444eaf03Seric 	char hostbuf[];
929897f1869Seric 	int size;
930444eaf03Seric {
93138ad259dSeric 	register struct hostent *hp;
9328b212fe0Seric 	extern bool getcanonname();
9338b212fe0Seric 	extern int h_errno;
934444eaf03Seric 
935af5e902cSeric 	if (gethostname(hostbuf, size) < 0)
936af5e902cSeric 	{
937af5e902cSeric 		(void) strcpy(hostbuf, "localhost");
938af5e902cSeric 	}
9398b212fe0Seric 	hp = sm_gethostbyname(hostbuf);
9404a5c6430Seric 	if (hp == NULL)
9418b212fe0Seric 		return NULL;
9428b212fe0Seric 	if (strchr(hp->h_name, '.') != NULL || strchr(hostbuf, '.') == NULL)
943423161fbSeric 	{
94435852b23Seric 		(void) strncpy(hostbuf, hp->h_name, size - 1);
94535852b23Seric 		hostbuf[size - 1] = '\0';
9468b212fe0Seric 	}
9474a5c6430Seric 
9484a5c6430Seric #if NAMED_BIND
9498b212fe0Seric 	/*
9508b212fe0Seric 	**  If still no dot, try DNS directly (i.e., avoid NIS problems).
9518b212fe0Seric 	**  This ought to be driven from the configuration file, but
9528b212fe0Seric 	**  we are called before the configuration is read.  We could
9538b212fe0Seric 	**  check for an /etc/resolv.conf file, but that isn't required.
9548b212fe0Seric 	**  All in all, a bit of a mess.
9558b212fe0Seric 	*/
95623668637Seric 
9578b212fe0Seric 	if (strchr(hostbuf, '.') == NULL &&
9588b212fe0Seric 	    !getcanonname(hostbuf, size, TRUE) &&
9598b212fe0Seric 	    h_errno == TRY_AGAIN)
96023668637Seric 	{
9618b212fe0Seric 		/* try twice in case name server not yet started up */
9628b212fe0Seric 		message("My unqualifed host name (%s) unknown to DNS; sleeping for retry",
96323668637Seric 			hostbuf);
9648b212fe0Seric 		sleep(60);
9658b212fe0Seric 		if (!getcanonname(hostbuf, size, TRUE))
9668b212fe0Seric 			errno = h_errno + E_DNSBASE;
967747df804Seric 	}
968747df804Seric #endif
9698b212fe0Seric 	return (hp);
9707364df9fSeric }
971cb452edcSeric /*
9729f8b0eadSeric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
9739f8b0eadSeric **
9749f8b0eadSeric **	Uses RFC1413 protocol to try to get info from the other end.
975320e0d1cSeric **
976320e0d1cSeric **	Parameters:
977320e0d1cSeric **		fd -- the descriptor
978320e0d1cSeric **
979320e0d1cSeric **	Returns:
9809f8b0eadSeric **		The user@host information associated with this descriptor.
981320e0d1cSeric */
982320e0d1cSeric 
9839f8b0eadSeric static jmp_buf	CtxAuthTimeout;
9849f8b0eadSeric 
9858b212fe0Seric static void
9869f8b0eadSeric authtimeout()
9879f8b0eadSeric {
9889f8b0eadSeric 	longjmp(CtxAuthTimeout, 1);
9899f8b0eadSeric }
9909f8b0eadSeric 
991320e0d1cSeric char *
9929f8b0eadSeric getauthinfo(fd)
993320e0d1cSeric 	int fd;
994320e0d1cSeric {
9959f8b0eadSeric 	int falen;
996a5546e24Seric 	register char *p;
9979f8b0eadSeric 	SOCKADDR la;
9989f8b0eadSeric 	int lalen;
9999f8b0eadSeric 	register struct servent *sp;
10009f8b0eadSeric 	int s;
10019f8b0eadSeric 	int i;
10029f8b0eadSeric 	EVENT *ev;
1003579270a3Seric 	int nleft;
1004f036c8d7Seric 	char ibuf[MAXNAME + 1];
10059f8b0eadSeric 	static char hbuf[MAXNAME * 2 + 2];
10069f8b0eadSeric 	extern char *hostnamebyanyaddr();
10079f8b0eadSeric 	extern char RealUserName[];			/* main.c */
1008320e0d1cSeric 
1009e29a76d1Seric 	falen = sizeof RealHostAddr;
10108b212fe0Seric 	if (isatty(fd) || getpeername(fd, &RealHostAddr.sa, &falen) < 0 ||
10118b212fe0Seric 	    falen <= 0 || RealHostAddr.sa.sa_family == 0)
10129f8b0eadSeric 	{
10139f8b0eadSeric 		(void) sprintf(hbuf, "%s@localhost", RealUserName);
101453853673Seric 		if (tTd(9, 1))
10159f8b0eadSeric 			printf("getauthinfo: %s\n", hbuf);
1016320e0d1cSeric 		return hbuf;
1017320e0d1cSeric 	}
10189f8b0eadSeric 
1019e29a76d1Seric 	if (RealHostName == NULL)
1020e29a76d1Seric 	{
1021e29a76d1Seric 		/* translate that to a host name */
1022e29a76d1Seric 		RealHostName = newstr(hostnamebyanyaddr(&RealHostAddr));
1023e29a76d1Seric 	}
1024e29a76d1Seric 
102593b3215bSeric 	if (TimeOuts.to_ident == 0)
102693b3215bSeric 		goto noident;
102793b3215bSeric 
10289f8b0eadSeric 	lalen = sizeof la;
1029e29a76d1Seric 	if (RealHostAddr.sa.sa_family != AF_INET ||
10309f8b0eadSeric 	    getsockname(fd, &la.sa, &lalen) < 0 || lalen <= 0 ||
10319f8b0eadSeric 	    la.sa.sa_family != AF_INET)
10329f8b0eadSeric 	{
10339f8b0eadSeric 		/* no ident info */
10349f8b0eadSeric 		goto noident;
10359f8b0eadSeric 	}
10369f8b0eadSeric 
10379f8b0eadSeric 	/* create ident query */
103881d2944fSeric 	(void) sprintf(ibuf, "%d,%d\r\n",
1039e29a76d1Seric 		ntohs(RealHostAddr.sin.sin_port), ntohs(la.sin.sin_port));
10409f8b0eadSeric 
10419f8b0eadSeric 	/* create local address */
1042d6af7dadSeric 	la.sin.sin_port = 0;
10439f8b0eadSeric 
10449f8b0eadSeric 	/* create foreign address */
10459f8b0eadSeric 	sp = getservbyname("auth", "tcp");
10469f8b0eadSeric 	if (sp != NULL)
1047e29a76d1Seric 		RealHostAddr.sin.sin_port = sp->s_port;
10489f8b0eadSeric 	else
1049e29a76d1Seric 		RealHostAddr.sin.sin_port = htons(113);
10509f8b0eadSeric 
10519f8b0eadSeric 	s = -1;
10529f8b0eadSeric 	if (setjmp(CtxAuthTimeout) != 0)
10539f8b0eadSeric 	{
10549f8b0eadSeric 		if (s >= 0)
10559f8b0eadSeric 			(void) close(s);
10569f8b0eadSeric 		goto noident;
10579f8b0eadSeric 	}
10589f8b0eadSeric 
10599f8b0eadSeric 	/* put a timeout around the whole thing */
1060a0f780efSeric 	ev = setevent(TimeOuts.to_ident, authtimeout, 0);
10619f8b0eadSeric 
1062d6af7dadSeric 	/* connect to foreign IDENT server using same address as SMTP socket */
10639f8b0eadSeric 	s = socket(AF_INET, SOCK_STREAM, 0);
10649f8b0eadSeric 	if (s < 0)
10659f8b0eadSeric 	{
10669f8b0eadSeric 		clrevent(ev);
10679f8b0eadSeric 		goto noident;
10689f8b0eadSeric 	}
1069d6af7dadSeric 	if (bind(s, &la.sa, sizeof la.sin) < 0 ||
1070e29a76d1Seric 	    connect(s, &RealHostAddr.sa, sizeof RealHostAddr.sin) < 0)
10719f8b0eadSeric 	{
10727c201575Seric 		goto closeident;
10739f8b0eadSeric 	}
10749f8b0eadSeric 
107553853673Seric 	if (tTd(9, 10))
107681d2944fSeric 		printf("getauthinfo: sent %s", ibuf);
10779f8b0eadSeric 
10789f8b0eadSeric 	/* send query */
107981d2944fSeric 	if (write(s, ibuf, strlen(ibuf)) < 0)
10809f8b0eadSeric 		goto closeident;
10819f8b0eadSeric 
10829f8b0eadSeric 	/* get result */
108381d2944fSeric 	p = &ibuf[0];
1084b26156f6Seric 	nleft = sizeof ibuf - 1;
1085579270a3Seric 	while ((i = read(s, p, nleft)) > 0)
1086579270a3Seric 	{
1087579270a3Seric 		p += i;
1088579270a3Seric 		nleft -= i;
1089579270a3Seric 	}
10909f8b0eadSeric 	(void) close(s);
10919f8b0eadSeric 	clrevent(ev);
109281d2944fSeric 	if (i < 0 || p == &ibuf[0])
10939f8b0eadSeric 		goto noident;
1094579270a3Seric 
1095579270a3Seric 	if (*--p == '\n' && *--p == '\r')
1096579270a3Seric 		p--;
1097579270a3Seric 	*++p = '\0';
10989f8b0eadSeric 
109953853673Seric 	if (tTd(9, 3))
110081d2944fSeric 		printf("getauthinfo:  got %s\n", ibuf);
11019f8b0eadSeric 
11029f8b0eadSeric 	/* parse result */
110381d2944fSeric 	p = strchr(ibuf, ':');
11049f8b0eadSeric 	if (p == NULL)
11059f8b0eadSeric 	{
11069f8b0eadSeric 		/* malformed response */
11079f8b0eadSeric 		goto noident;
11089f8b0eadSeric 	}
11099f8b0eadSeric 	while (isascii(*++p) && isspace(*p))
11109f8b0eadSeric 		continue;
11119f8b0eadSeric 	if (strncasecmp(p, "userid", 6) != 0)
11129f8b0eadSeric 	{
11139f8b0eadSeric 		/* presumably an error string */
11149f8b0eadSeric 		goto noident;
11159f8b0eadSeric 	}
11169f8b0eadSeric 	p += 6;
11179f8b0eadSeric 	while (isascii(*p) && isspace(*p))
11189f8b0eadSeric 		p++;
11199f8b0eadSeric 	if (*p++ != ':')
11209f8b0eadSeric 	{
11219f8b0eadSeric 		/* either useridxx or malformed response */
11229f8b0eadSeric 		goto noident;
11239f8b0eadSeric 	}
11249f8b0eadSeric 
11259f8b0eadSeric 	/* p now points to the OSTYPE field */
11268b212fe0Seric 	while (isascii(*p) && isspace(*p))
11278b212fe0Seric 		p++;
11288b212fe0Seric 	if (strncasecmp(p, "other", 5) == 0 &&
11298b212fe0Seric 	    (p[5] == ':' || p[5] == ' ' || p[5] == ',' || p[5] == '\0'))
11308b212fe0Seric 	{
11318b212fe0Seric 		/* not useful information */
11328b212fe0Seric 		goto noident;
11338b212fe0Seric 	}
11349f8b0eadSeric 	p = strchr(p, ':');
11359f8b0eadSeric 	if (p == NULL)
11369f8b0eadSeric 	{
11379f8b0eadSeric 		/* malformed response */
11389f8b0eadSeric 		goto noident;
11399f8b0eadSeric 	}
114053853673Seric 
114153853673Seric 	/* 1413 says don't do this -- but it's broken otherwise */
114253853673Seric 	while (isascii(*++p) && isspace(*p))
114353853673Seric 		continue;
11449f8b0eadSeric 
1145a7763879Seric 	/* p now points to the authenticated name -- copy carefully */
114681d2944fSeric 	cleanstrcpy(hbuf, p, MAXNAME);
114793433f31Seric 	i = strlen(hbuf);
1148a7763879Seric 	hbuf[i++] = '@';
1149a7763879Seric 	strcpy(&hbuf[i], RealHostName == NULL ? "localhost" : RealHostName);
115053853673Seric 	goto finish;
115153853673Seric 
11527c201575Seric closeident:
11537c201575Seric 	(void) close(s);
11547c201575Seric 	clrevent(ev);
11557c201575Seric 
115653853673Seric noident:
1157f7869e68Seric 	if (RealHostName == NULL)
1158f7869e68Seric 	{
1159f7869e68Seric 		if (tTd(9, 1))
1160f7869e68Seric 			printf("getauthinfo: NULL\n");
1161f7869e68Seric 		return NULL;
1162f7869e68Seric 	}
116353853673Seric 	(void) strcpy(hbuf, RealHostName);
116453853673Seric 
116553853673Seric finish:
1166f7869e68Seric 	if (RealHostName != NULL && RealHostName[0] != '[')
11679f8b0eadSeric 	{
11689f8b0eadSeric 		p = &hbuf[strlen(hbuf)];
11699f8b0eadSeric 		(void) sprintf(p, " [%s]", anynet_ntoa(&RealHostAddr));
11709f8b0eadSeric 	}
117153853673Seric 	if (tTd(9, 1))
11729f8b0eadSeric 		printf("getauthinfo: %s\n", hbuf);
11739f8b0eadSeric 	return hbuf;
11749f8b0eadSeric }
1175320e0d1cSeric /*
117608de856eSeric **  HOST_MAP_LOOKUP -- turn a hostname into canonical form
117715d084d5Seric **
117815d084d5Seric **	Parameters:
117905b57da8Seric **		map -- a pointer to this map (unused).
118008de856eSeric **		name -- the (presumably unqualified) hostname.
118100b385a9Seric **		av -- unused -- for compatibility with other mapping
1182d798a1deSeric **			functions.
11832d29d43aSeric **		statp -- an exit status (out parameter) -- set to
11842d29d43aSeric **			EX_TEMPFAIL if the name server is unavailable.
118515d084d5Seric **
118615d084d5Seric **	Returns:
118715d084d5Seric **		The mapping, if found.
118815d084d5Seric **		NULL if no mapping found.
118915d084d5Seric **
119015d084d5Seric **	Side Effects:
119115d084d5Seric **		Looks up the host specified in hbuf.  If it is not
119215d084d5Seric **		the canonical name for that host, return the canonical
119315d084d5Seric **		name.
1194f36ede03Sbostic */
1195cb452edcSeric 
119615d084d5Seric char *
119700b385a9Seric host_map_lookup(map, name, av, statp)
119805b57da8Seric 	MAP *map;
119908de856eSeric 	char *name;
120000b385a9Seric 	char **av;
12012d29d43aSeric 	int *statp;
120299f7cf32Seric {
120399f7cf32Seric 	register struct hostent *hp;
12048b212fe0Seric 	struct in_addr in_addr;
120505b57da8Seric 	char *cp;
1206eea91d78Seric 	register STAB *s;
12078b212fe0Seric 	char hbuf[MAXNAME + 1];
12089d4a8008Seric #if NAMED_BIND
1209eea91d78Seric 	extern int h_errno;
1210c304a798Seric #endif
12115f78836eSmiriam 
1212f36ede03Sbostic 	/*
1213eea91d78Seric 	**  See if we have already looked up this name.  If so, just
1214eea91d78Seric 	**  return it.
1215eea91d78Seric 	*/
1216eea91d78Seric 
121708de856eSeric 	s = stab(name, ST_NAMECANON, ST_ENTER);
1218eea91d78Seric 	if (bitset(NCF_VALID, s->s_namecanon.nc_flags))
1219eea91d78Seric 	{
1220f92c3297Seric 		if (tTd(9, 1))
122108de856eSeric 			printf("host_map_lookup(%s) => CACHE %s\n",
122208de856eSeric 				name, s->s_namecanon.nc_cname);
1223eea91d78Seric 		errno = s->s_namecanon.nc_errno;
12249d4a8008Seric #if NAMED_BIND
1225eea91d78Seric 		h_errno = s->s_namecanon.nc_herrno;
1226c304a798Seric #endif
1227eea91d78Seric 		*statp = s->s_namecanon.nc_stat;
1228fcfb36d9Seric 		if (*statp == EX_TEMPFAIL)
1229ed63aae0Seric 		{
12305b068d51Seric 			CurEnv->e_status = "4.4.3";
1231fcfb36d9Seric 			usrerr("451 %s: Name server timeout",
1232ed63aae0Seric 				shortenstring(name, 33));
1233ed63aae0Seric 		}
1234eea91d78Seric 		return s->s_namecanon.nc_cname;
1235eea91d78Seric 	}
1236eea91d78Seric 
1237eea91d78Seric 	/*
1238eea91d78Seric 	**  If first character is a bracket, then it is an address
1239eea91d78Seric 	**  lookup.  Address is copied into a temporary buffer to
124008de856eSeric 	**  strip the brackets and to preserve name if address is
1241eea91d78Seric 	**  unknown.
1242f36ede03Sbostic 	*/
124315d084d5Seric 
124408de856eSeric 	if (*name != '[')
124515d084d5Seric 	{
1246d798a1deSeric 		extern bool getcanonname();
1247d798a1deSeric 
12488cb4653dSeric 		if (tTd(9, 1))
124908de856eSeric 			printf("host_map_lookup(%s) => ", name);
1250eea91d78Seric 		s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
12518b212fe0Seric 		if (strlen(name) < sizeof hbuf)
125208de856eSeric 			(void) strcpy(hbuf, name);
12538b212fe0Seric 		else
12548b212fe0Seric 		{
12558b212fe0Seric 			bcopy(name, hbuf, sizeof hbuf - 1);
12568b212fe0Seric 			hbuf[sizeof hbuf - 1] = '\0';
12578b212fe0Seric 		}
12580183c3f6Seric 		if (getcanonname(hbuf, sizeof hbuf - 1, !NoMXforCanon))
12599040ec4fSeric 		{
12609040ec4fSeric 			if (tTd(9, 1))
12619040ec4fSeric 				printf("%s\n", hbuf);
126200b385a9Seric 			cp = map_rewrite(map, hbuf, strlen(hbuf), av);
126300b385a9Seric 			s->s_namecanon.nc_cname = newstr(cp);
126400b385a9Seric 			return cp;
12659040ec4fSeric 		}
126615d084d5Seric 		else
12679040ec4fSeric 		{
12682d29d43aSeric 			register struct hostent *hp;
12692d29d43aSeric 
1270c304a798Seric 			s->s_namecanon.nc_errno = errno;
12719d4a8008Seric #if NAMED_BIND
1272c304a798Seric 			s->s_namecanon.nc_herrno = h_errno;
12739040ec4fSeric 			if (tTd(9, 1))
12742d29d43aSeric 				printf("FAIL (%d)\n", h_errno);
12752d29d43aSeric 			switch (h_errno)
12762d29d43aSeric 			{
12772d29d43aSeric 			  case TRY_AGAIN:
127889cb2793Seric 				if (UseNameServer)
12798820d51bSeric 				{
12805b068d51Seric 					CurEnv->e_status = "4.4.3";
1281fcfb36d9Seric 					usrerr("451 %s: Name server timeout",
1282ed63aae0Seric 						shortenstring(name, 33));
12838820d51bSeric 				}
12842d29d43aSeric 				*statp = EX_TEMPFAIL;
12852d29d43aSeric 				break;
12862d29d43aSeric 
12872d29d43aSeric 			  case HOST_NOT_FOUND:
128880027ae6Seric 			  case NO_DATA:
12892d29d43aSeric 				*statp = EX_NOHOST;
12902d29d43aSeric 				break;
12912d29d43aSeric 
12922d29d43aSeric 			  case NO_RECOVERY:
12932d29d43aSeric 				*statp = EX_SOFTWARE;
12942d29d43aSeric 				break;
12952d29d43aSeric 
12962d29d43aSeric 			  default:
12972d29d43aSeric 				*statp = EX_UNAVAILABLE;
12982d29d43aSeric 				break;
12992d29d43aSeric 			}
1300c304a798Seric #else
1301c304a798Seric 			if (tTd(9, 1))
1302c304a798Seric 				printf("FAIL\n");
1303c304a798Seric 			*statp = EX_NOHOST;
1304c304a798Seric #endif
1305eea91d78Seric 			s->s_namecanon.nc_stat = *statp;
13068b212fe0Seric 			if ((*statp != EX_TEMPFAIL && *statp != EX_NOHOST) ||
13078b212fe0Seric 			    UseNameServer)
130815d084d5Seric 				return NULL;
13092d29d43aSeric 
13102d29d43aSeric 			/*
13112d29d43aSeric 			**  Try to look it up in /etc/hosts
13122d29d43aSeric 			*/
13132d29d43aSeric 
13148b212fe0Seric 			hp = sm_gethostbyname(name);
13152d29d43aSeric 			if (hp == NULL)
13162d29d43aSeric 			{
13172d29d43aSeric 				/* no dice there either */
1318eea91d78Seric 				s->s_namecanon.nc_stat = *statp = EX_NOHOST;
13192d29d43aSeric 				return NULL;
13202d29d43aSeric 			}
13212d29d43aSeric 
1322eea91d78Seric 			s->s_namecanon.nc_stat = *statp = EX_OK;
132300b385a9Seric 			cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
132400b385a9Seric 			s->s_namecanon.nc_cname = newstr(cp);
132500b385a9Seric 			return cp;
132615d084d5Seric 		}
13279040ec4fSeric 	}
132808de856eSeric 	if ((cp = strchr(name, ']')) == NULL)
132915d084d5Seric 		return (NULL);
133034e39927Sbostic 	*cp = '\0';
13318b212fe0Seric 	in_addr.s_addr = inet_addr(&name[1]);
133238ad259dSeric 
133338ad259dSeric 	/* nope -- ask the name server */
13348b212fe0Seric 	hp = sm_gethostbyaddr((char *)&in_addr, INADDRSZ, AF_INET);
1335eea91d78Seric 	s->s_namecanon.nc_errno = errno;
13369d4a8008Seric #if NAMED_BIND
1337eea91d78Seric 	s->s_namecanon.nc_herrno = h_errno;
1338c304a798Seric #endif
1339eea91d78Seric 	s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
13405f78836eSmiriam 	if (hp == NULL)
1341eea91d78Seric 	{
1342eea91d78Seric 		s->s_namecanon.nc_stat = *statp = EX_NOHOST;
134315d084d5Seric 		return (NULL);
1344eea91d78Seric 	}
134515d084d5Seric 
134638ad259dSeric 	/* found a match -- copy out */
134700b385a9Seric 	cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
1348eea91d78Seric 	s->s_namecanon.nc_stat = *statp = EX_OK;
134900b385a9Seric 	s->s_namecanon.nc_cname = newstr(cp);
135000b385a9Seric 	return cp;
135199f7cf32Seric }
1352e2f2f828Seric /*
1353e2f2f828Seric **  ANYNET_NTOA -- convert a network address to printable form.
1354e2f2f828Seric **
1355e2f2f828Seric **	Parameters:
1356e2f2f828Seric **		sap -- a pointer to a sockaddr structure.
1357e2f2f828Seric **
1358e2f2f828Seric **	Returns:
1359e2f2f828Seric **		A printable version of that sockaddr.
1360e2f2f828Seric */
1361e2f2f828Seric 
1362e2f2f828Seric char *
1363e2f2f828Seric anynet_ntoa(sap)
1364e2f2f828Seric 	register SOCKADDR *sap;
1365e2f2f828Seric {
1366e2f2f828Seric 	register char *bp;
1367e2f2f828Seric 	register char *ap;
1368e2f2f828Seric 	int l;
1369e387851eSeric 	static char buf[100];
1370e2f2f828Seric 
13718cb4653dSeric 	/* check for null/zero family */
13728cb4653dSeric 	if (sap == NULL)
13738cb4653dSeric 		return "NULLADDR";
13748cb4653dSeric 	if (sap->sa.sa_family == 0)
13758cb4653dSeric 		return "0";
13768cb4653dSeric 
1377e387851eSeric 	switch (sap->sa.sa_family)
1378e387851eSeric 	{
1379139b52c8Seric #ifdef NETUNIX
1380e387851eSeric 	  case AF_UNIX:
1381c24cf5a4Seric 	  	if (sap->sunix.sun_path[0] != '\0')
1382c24cf5a4Seric 	  		sprintf(buf, "[UNIX: %.64s]", sap->sunix.sun_path);
1383e387851eSeric 	  	else
1384e387851eSeric 	  		sprintf(buf, "[UNIX: localhost]");
1385e387851eSeric 		return buf;
1386e387851eSeric #endif
1387e387851eSeric 
138883c1f4bcSeric #ifdef NETINET
1389e387851eSeric 	  case AF_INET:
1390482e28bcSeric 		return inet_ntoa(sap->sin.sin_addr);
139183c1f4bcSeric #endif
1392e2f2f828Seric 
1393e387851eSeric 	  default:
1394e387851eSeric 	  	/* this case is only to ensure syntactic correctness */
1395e387851eSeric 	  	break;
1396e387851eSeric 	}
1397e387851eSeric 
1398e2f2f828Seric 	/* unknown family -- just dump bytes */
139983c1f4bcSeric 	(void) sprintf(buf, "Family %d: ", sap->sa.sa_family);
1400e2f2f828Seric 	bp = &buf[strlen(buf)];
140183c1f4bcSeric 	ap = sap->sa.sa_data;
140283c1f4bcSeric 	for (l = sizeof sap->sa.sa_data; --l >= 0; )
1403e2f2f828Seric 	{
1404e2f2f828Seric 		(void) sprintf(bp, "%02x:", *ap++ & 0377);
1405e2f2f828Seric 		bp += 3;
1406e2f2f828Seric 	}
1407e2f2f828Seric 	*--bp = '\0';
1408e2f2f828Seric 	return buf;
1409e2f2f828Seric }
14109f8b0eadSeric /*
14119f8b0eadSeric **  HOSTNAMEBYANYADDR -- return name of host based on address
14129f8b0eadSeric **
14139f8b0eadSeric **	Parameters:
14149f8b0eadSeric **		sap -- SOCKADDR pointer
14159f8b0eadSeric **
14169f8b0eadSeric **	Returns:
14179f8b0eadSeric **		text representation of host name.
14189f8b0eadSeric **
14199f8b0eadSeric **	Side Effects:
14209f8b0eadSeric **		none.
14219f8b0eadSeric */
14229f8b0eadSeric 
14239f8b0eadSeric char *
14249f8b0eadSeric hostnamebyanyaddr(sap)
14259f8b0eadSeric 	register SOCKADDR *sap;
14269f8b0eadSeric {
14279f8b0eadSeric 	register struct hostent *hp;
14283490b9dfSeric 	int saveretry;
14293490b9dfSeric 
14309d4a8008Seric #if NAMED_BIND
14313490b9dfSeric 	/* shorten name server timeout to avoid higher level timeouts */
14323490b9dfSeric 	saveretry = _res.retry;
14333490b9dfSeric 	_res.retry = 3;
14343490b9dfSeric #endif /* NAMED_BIND */
14353490b9dfSeric 
14369f8b0eadSeric 	switch (sap->sa.sa_family)
14379f8b0eadSeric 	{
14389f8b0eadSeric #ifdef NETINET
14399f8b0eadSeric 	  case AF_INET:
14408b212fe0Seric 		hp = sm_gethostbyaddr((char *) &sap->sin.sin_addr,
14418b212fe0Seric 			INADDRSZ,
14429f8b0eadSeric 			AF_INET);
14439f8b0eadSeric 		break;
14449f8b0eadSeric #endif
14459f8b0eadSeric 
14469f8b0eadSeric #ifdef NETISO
14479f8b0eadSeric 	  case AF_ISO:
14488b212fe0Seric 		hp = sm_gethostbyaddr((char *) &sap->siso.siso_addr,
14499f8b0eadSeric 			sizeof sap->siso.siso_addr,
14509f8b0eadSeric 			AF_ISO);
14519f8b0eadSeric 		break;
14529f8b0eadSeric #endif
14539f8b0eadSeric 
1454e387851eSeric 	  case AF_UNIX:
1455e387851eSeric 		hp = NULL;
1456e387851eSeric 		break;
1457e387851eSeric 
14589f8b0eadSeric 	  default:
14598b212fe0Seric 		hp = sm_gethostbyaddr(sap->sa.sa_data,
14609f8b0eadSeric 			   sizeof sap->sa.sa_data,
14619f8b0eadSeric 			   sap->sa.sa_family);
14629f8b0eadSeric 		break;
14639f8b0eadSeric 	}
14649f8b0eadSeric 
14659d4a8008Seric #if NAMED_BIND
14663490b9dfSeric 	_res.retry = saveretry;
14673490b9dfSeric #endif /* NAMED_BIND */
14683490b9dfSeric 
14699f8b0eadSeric 	if (hp != NULL)
14709f8b0eadSeric 		return hp->h_name;
14719f8b0eadSeric 	else
14729f8b0eadSeric 	{
14739f8b0eadSeric 		/* produce a dotted quad */
14749f8b0eadSeric 		static char buf[512];
14759f8b0eadSeric 
14769f8b0eadSeric 		(void) sprintf(buf, "[%s]", anynet_ntoa(sap));
14779f8b0eadSeric 		return buf;
14789f8b0eadSeric 	}
14799f8b0eadSeric }
1480f36ede03Sbostic 
14816c2c3107Seric # else /* DAEMON */
148299f7cf32Seric /* code for systems without sophisticated networking */
1483444eaf03Seric 
1484444eaf03Seric /*
1485444eaf03Seric **  MYHOSTNAME -- stub version for case of no daemon code.
148621e9914dSeric **
148721e9914dSeric **	Can't convert to upper case here because might be a UUCP name.
1488897f1869Seric **
1489897f1869Seric **	Mark, you can change this to be anything you want......
1490444eaf03Seric */
1491444eaf03Seric 
1492444eaf03Seric char **
1493897f1869Seric myhostname(hostbuf, size)
1494444eaf03Seric 	char hostbuf[];
1495897f1869Seric 	int size;
1496444eaf03Seric {
1497444eaf03Seric 	register FILE *f;
1498444eaf03Seric 
1499444eaf03Seric 	hostbuf[0] = '\0';
1500444eaf03Seric 	f = fopen("/usr/include/whoami", "r");
1501444eaf03Seric 	if (f != NULL)
1502444eaf03Seric 	{
1503897f1869Seric 		(void) fgets(hostbuf, size, f);
1504444eaf03Seric 		fixcrlf(hostbuf, TRUE);
1505444eaf03Seric 		(void) fclose(f);
1506444eaf03Seric 	}
1507444eaf03Seric 	return (NULL);
1508444eaf03Seric }
150999f7cf32Seric /*
15109f8b0eadSeric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
1511320e0d1cSeric **
1512320e0d1cSeric **	Parameters:
1513320e0d1cSeric **		fd -- the descriptor
1514320e0d1cSeric **
1515320e0d1cSeric **	Returns:
1516320e0d1cSeric **		The host name associated with this descriptor, if it can
1517320e0d1cSeric **			be determined.
1518320e0d1cSeric **		NULL otherwise.
1519320e0d1cSeric **
1520320e0d1cSeric **	Side Effects:
1521320e0d1cSeric **		none
1522320e0d1cSeric */
1523320e0d1cSeric 
1524320e0d1cSeric char *
15259f8b0eadSeric getauthinfo(fd)
1526320e0d1cSeric 	int fd;
1527320e0d1cSeric {
1528320e0d1cSeric 	return NULL;
1529320e0d1cSeric }
1530320e0d1cSeric /*
153199f7cf32Seric **  MAPHOSTNAME -- turn a hostname into canonical form
153299f7cf32Seric **
153399f7cf32Seric **	Parameters:
153405b57da8Seric **		map -- a pointer to the database map.
153508de856eSeric **		name -- a buffer containing a hostname.
153615d084d5Seric **		avp -- a pointer to a (cf file defined) argument vector.
15372d29d43aSeric **		statp -- an exit status (out parameter).
153899f7cf32Seric **
153999f7cf32Seric **	Returns:
154015d084d5Seric **		mapped host name
1541cb452edcSeric **		FALSE otherwise.
154299f7cf32Seric **
154399f7cf32Seric **	Side Effects:
154408de856eSeric **		Looks up the host specified in name.  If it is not
154599f7cf32Seric **		the canonical name for that host, replace it with
154699f7cf32Seric **		the canonical name.  If the name is unknown, or it
154799f7cf32Seric **		is already the canonical name, leave it unchanged.
154899f7cf32Seric */
154999f7cf32Seric 
155099f7cf32Seric /*ARGSUSED*/
155115d084d5Seric char *
155208de856eSeric host_map_lookup(map, name, avp, statp)
155305b57da8Seric 	MAP *map;
155408de856eSeric 	char *name;
155515d084d5Seric 	char **avp;
15562d29d43aSeric 	char *statp;
155799f7cf32Seric {
15582d29d43aSeric 	register struct hostent *hp;
15592d29d43aSeric 
15608b212fe0Seric 	hp = sm_gethostbyname(name);
15612d29d43aSeric 	if (hp != NULL)
15622d29d43aSeric 		return hp->h_name;
15632d29d43aSeric 	*statp = EX_NOHOST;
156415d084d5Seric 	return NULL;
156599f7cf32Seric }
156699f7cf32Seric 
15676c2c3107Seric #endif /* DAEMON */
1568