xref: /original-bsd/usr.sbin/sendmail/src/daemon.c (revision 482e28bc)
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*482e28bcSeric static char sccsid[] = "@(#)daemon.c	8.79 (Berkeley) 04/11/95 (with daemon mode)";
15d0a9e852Seric #else
16*482e28bcSeric static char sccsid[] = "@(#)daemon.c	8.79 (Berkeley) 04/11/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;
472bfb80540Seric 
473bfb80540Seric 		switch (*f)
474bfb80540Seric 		{
475850144caSeric 		  case 'F':		/* address family */
476850144caSeric 			if (isascii(*v) && isdigit(*v))
477850144caSeric 				DaemonAddr.sa.sa_family = atoi(v);
478850144caSeric #ifdef NETINET
479850144caSeric 			else if (strcasecmp(v, "inet") == 0)
480850144caSeric 				DaemonAddr.sa.sa_family = AF_INET;
481850144caSeric #endif
482850144caSeric #ifdef NETISO
483850144caSeric 			else if (strcasecmp(v, "iso") == 0)
484850144caSeric 				DaemonAddr.sa.sa_family = AF_ISO;
485850144caSeric #endif
486850144caSeric #ifdef NETNS
487850144caSeric 			else if (strcasecmp(v, "ns") == 0)
488850144caSeric 				DaemonAddr.sa.sa_family = AF_NS;
489850144caSeric #endif
490850144caSeric #ifdef NETX25
491850144caSeric 			else if (strcasecmp(v, "x.25") == 0)
492850144caSeric 				DaemonAddr.sa.sa_family = AF_CCITT;
493850144caSeric #endif
494850144caSeric 			else
495850144caSeric 				syserr("554 Unknown address family %s in Family=option", v);
496850144caSeric 			break;
497850144caSeric 
498850144caSeric 		  case 'A':		/* address */
499850144caSeric 			switch (DaemonAddr.sa.sa_family)
500850144caSeric 			{
501850144caSeric #ifdef NETINET
502850144caSeric 			  case AF_INET:
503850144caSeric 				if (isascii(*v) && isdigit(*v))
5048b212fe0Seric 					DaemonAddr.sin.sin_addr.s_addr = htonl(inet_network(v));
505850144caSeric 				else
506850144caSeric 				{
507850144caSeric 					register struct netent *np;
508850144caSeric 
509850144caSeric 					np = getnetbyname(v);
510850144caSeric 					if (np == NULL)
511850144caSeric 						syserr("554 network \"%s\" unknown", v);
512850144caSeric 					else
513850144caSeric 						DaemonAddr.sin.sin_addr.s_addr = np->n_net;
514850144caSeric 				}
515850144caSeric 				break;
516850144caSeric #endif
517850144caSeric 
518850144caSeric 			  default:
519850144caSeric 				syserr("554 Address= option unsupported for family %d",
520850144caSeric 					DaemonAddr.sa.sa_family);
521850144caSeric 				break;
522850144caSeric 			}
523850144caSeric 			break;
524850144caSeric 
525bfb80540Seric 		  case 'P':		/* port */
526850144caSeric 			switch (DaemonAddr.sa.sa_family)
527850144caSeric 			{
528850144caSeric 				short port;
529850144caSeric 
530850144caSeric #ifdef NETINET
531850144caSeric 			  case AF_INET:
532bfb80540Seric 				if (isascii(*v) && isdigit(*v))
53376b70c58Seric 					DaemonAddr.sin.sin_port = htons(atoi(v));
534bfb80540Seric 				else
535bfb80540Seric 				{
536bfb80540Seric 					register struct servent *sp;
537bfb80540Seric 
538bfb80540Seric 					sp = getservbyname(v, "tcp");
539bfb80540Seric 					if (sp == NULL)
540ad977999Seric 						syserr("554 service \"%s\" unknown", v);
541bfb80540Seric 					else
542bfb80540Seric 						DaemonAddr.sin.sin_port = sp->s_port;
543bfb80540Seric 				}
544bfb80540Seric 				break;
545850144caSeric #endif
546bfb80540Seric 
547850144caSeric #ifdef NETISO
548850144caSeric 			  case AF_ISO:
549850144caSeric 				/* assume two byte transport selector */
550bfb80540Seric 				if (isascii(*v) && isdigit(*v))
55176b70c58Seric 					port = htons(atoi(v));
552bfb80540Seric 				else
553bfb80540Seric 				{
554850144caSeric 					register struct servent *sp;
555bfb80540Seric 
556850144caSeric 					sp = getservbyname(v, "tcp");
557850144caSeric 					if (sp == NULL)
558ad977999Seric 						syserr("554 service \"%s\" unknown", v);
559bfb80540Seric 					else
560850144caSeric 						port = sp->s_port;
561850144caSeric 				}
562850144caSeric 				bcopy((char *) &port, TSEL(&DaemonAddr.siso), 2);
563850144caSeric 				break;
564850144caSeric #endif
565850144caSeric 
566850144caSeric 			  default:
567850144caSeric 				syserr("554 Port= option unsupported for family %d",
568850144caSeric 					DaemonAddr.sa.sa_family);
569850144caSeric 				break;
570bfb80540Seric 			}
571bfb80540Seric 			break;
572bfc1eaf8Seric 
573bfc1eaf8Seric 		  case 'L':		/* listen queue size */
574bfc1eaf8Seric 			ListenQueueSize = atoi(v);
575bfc1eaf8Seric 			break;
576b35447dbSeric 
577b35447dbSeric 		  case 'S':		/* send buffer size */
578b35447dbSeric 			TcpSndBufferSize = atoi(v);
579b35447dbSeric 			break;
580b35447dbSeric 
581b35447dbSeric 		  case 'R':		/* receive buffer size */
582b35447dbSeric 			TcpRcvBufferSize = atoi(v);
583b35447dbSeric 			break;
584bfb80540Seric 		}
585bfb80540Seric 	}
586bfb80540Seric }
587bfb80540Seric /*
5887aa493c5Seric **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
5897aa493c5Seric **
5907aa493c5Seric **	Parameters:
5917aa493c5Seric **		host -- the name of the host.
59248ff0a9dSeric **		port -- the port number to connect to.
593655feedbSeric **		mci -- a pointer to the mail connection information
594655feedbSeric **			structure to be filled in.
595914346b1Seric **		usesecureport -- if set, use a low numbered (reserved)
596914346b1Seric **			port to provide some rudimentary authentication.
5977aa493c5Seric **
5987aa493c5Seric **	Returns:
5997aa493c5Seric **		An exit code telling whether the connection could be
6007aa493c5Seric **			made and if not why not.
6017aa493c5Seric **
6027aa493c5Seric **	Side Effects:
6037aa493c5Seric **		none.
6047aa493c5Seric */
6057aa493c5Seric 
606e2f2f828Seric SOCKADDR	CurHostAddr;		/* address of current host */
60771ff6caaSeric 
608b31e7f2bSeric int
609655feedbSeric makeconnection(host, port, mci, usesecureport)
6107aa493c5Seric 	char *host;
611210215eaSeric 	u_short port;
612b31e7f2bSeric 	register MCI *mci;
613914346b1Seric 	bool usesecureport;
6147aa493c5Seric {
6158b212fe0Seric 	register int i = 0;
6168b212fe0Seric 	register int s;
61704344589Sbloom 	register struct hostent *hp = (struct hostent *)NULL;
618e2f2f828Seric 	SOCKADDR addr;
6196286bb75Sbloom 	int sav_errno;
620e2f2f828Seric 	int addrlen;
6218b212fe0Seric 	bool firstconnect;
6229d4a8008Seric #if NAMED_BIND
623134746fbSeric 	extern int h_errno;
624134746fbSeric #endif
6257aa493c5Seric 
6267aa493c5Seric 	/*
6277aa493c5Seric 	**  Set up the address for the mailer.
62871096d12Seric 	**	Accept "[a.b.c.d]" syntax for host name.
6297aa493c5Seric 	*/
6307aa493c5Seric 
6319d4a8008Seric #if NAMED_BIND
632794bdbb9Smiriam 	h_errno = 0;
633134746fbSeric #endif
634794bdbb9Smiriam 	errno = 0;
635967778e2Seric 	bzero(&CurHostAddr, sizeof CurHostAddr);
636c931b82bSeric 	SmtpPhase = mci->mci_phase = "initial connection";
637d945ebe8Seric 	CurHostName = host;
638794bdbb9Smiriam 
63971096d12Seric 	if (host[0] == '[')
64071096d12Seric 	{
641a44d5a5eSeric 		long hid;
6426c2c3107Seric 		register char *p = strchr(host, ']');
64371096d12Seric 
644a44d5a5eSeric 		if (p != NULL)
64571096d12Seric 		{
646a44d5a5eSeric 			*p = '\0';
6474d9c42c2Seric #ifdef NETINET
648a44d5a5eSeric 			hid = inet_addr(&host[1]);
649a7e21fe6Seric 			if (hid == -1)
6504d9c42c2Seric #endif
651a7e21fe6Seric 			{
652a7e21fe6Seric 				/* try it as a host name (avoid MX lookup) */
6538b212fe0Seric 				hp = sm_gethostbyname(&host[1]);
654d8984352Seric 				if (hp == NULL && p[-1] == '.')
655d8984352Seric 				{
6568b212fe0Seric #if NAMED_BIND
6578b212fe0Seric 					int oldopts = _res.options;
6588b212fe0Seric 
6598b212fe0Seric 					_res.options &= ~(RES_DEFNAMES|RES_DNSRCH);
6608b212fe0Seric #endif
661d8984352Seric 					p[-1] = '\0';
6628b212fe0Seric 					hp = sm_gethostbyname(&host[1]);
663d8984352Seric 					p[-1] = '.';
6648b212fe0Seric #if NAMED_BIND
6658b212fe0Seric 					_res.options = oldopts;
6668b212fe0Seric #endif
667d8984352Seric 				}
668a7e21fe6Seric 				*p = ']';
669a7e21fe6Seric 				goto gothostent;
670a7e21fe6Seric 			}
671a44d5a5eSeric 			*p = ']';
67271096d12Seric 		}
673a7e21fe6Seric 		if (p == NULL)
67471096d12Seric 		{
67508b25121Seric 			usrerr("553 Invalid numeric domain spec \"%s\"", host);
67671096d12Seric 			return (EX_NOHOST);
67771096d12Seric 		}
6784d9c42c2Seric #ifdef NETINET
6794d9c42c2Seric 		addr.sin.sin_family = AF_INET;		/*XXX*/
68083c1f4bcSeric 		addr.sin.sin_addr.s_addr = hid;
6814d9c42c2Seric #endif
68271096d12Seric 	}
6831c71e510Seric 	else
6841c71e510Seric 	{
685d8984352Seric 		register char *p = &host[strlen(host) - 1];
686d8984352Seric 
6878b212fe0Seric 		hp = sm_gethostbyname(host);
688d8984352Seric 		if (hp == NULL && *p == '.')
689d8984352Seric 		{
6908b212fe0Seric #if NAMED_BIND
6918b212fe0Seric 			int oldopts = _res.options;
6928b212fe0Seric 
6938b212fe0Seric 			_res.options &= ~(RES_DEFNAMES|RES_DNSRCH);
6948b212fe0Seric #endif
695d8984352Seric 			*p = '\0';
6968b212fe0Seric 			hp = sm_gethostbyname(host);
697d8984352Seric 			*p = '.';
6988b212fe0Seric #if NAMED_BIND
6998b212fe0Seric 			_res.options = oldopts;
7008b212fe0Seric #endif
701d8984352Seric 		}
702a7e21fe6Seric gothostent:
703794bdbb9Smiriam 		if (hp == NULL)
704794bdbb9Smiriam 		{
7059d4a8008Seric #if NAMED_BIND
7068b212fe0Seric 			/* check for name server timeouts */
7078b212fe0Seric 			if (errno == ETIMEDOUT || h_errno == TRY_AGAIN ||
7088b212fe0Seric 			    (errno == ECONNREFUSED && UseNameServer))
7098b212fe0Seric 			{
7108b212fe0Seric 				mci->mci_status = "4.4.3";
71152308a50Seric 				return (EX_TEMPFAIL);
7128b212fe0Seric 			}
713134746fbSeric #endif
7147aa493c5Seric 			return (EX_NOHOST);
715794bdbb9Smiriam 		}
71683c1f4bcSeric 		addr.sa.sa_family = hp->h_addrtype;
71783c1f4bcSeric 		switch (hp->h_addrtype)
71883c1f4bcSeric 		{
71983c1f4bcSeric #ifdef NETINET
72083c1f4bcSeric 		  case AF_INET:
721e2f2f828Seric 			bcopy(hp->h_addr,
72283c1f4bcSeric 				&addr.sin.sin_addr,
7238b212fe0Seric 				INADDRSZ);
72483c1f4bcSeric 			break;
72583c1f4bcSeric #endif
72683c1f4bcSeric 
72783c1f4bcSeric 		  default:
728e2f2f828Seric 			bcopy(hp->h_addr,
72983c1f4bcSeric 				addr.sa.sa_data,
730e2f2f828Seric 				hp->h_length);
73183c1f4bcSeric 			break;
73283c1f4bcSeric 		}
73304344589Sbloom 		i = 1;
7341c71e510Seric 	}
7351c71e510Seric 
7361c71e510Seric 	/*
7371c71e510Seric 	**  Determine the port number.
7381c71e510Seric 	*/
7391c71e510Seric 
740fd7c0790Seric 	if (port != 0)
741e2f2f828Seric 		port = htons(port);
742fd7c0790Seric 	else
7431c71e510Seric 	{
7441c71e510Seric 		register struct servent *sp = getservbyname("smtp", "tcp");
7451c71e510Seric 
7461c71e510Seric 		if (sp == NULL)
7471c71e510Seric 		{
7483edb2e00Seric #ifdef LOG
7493edb2e00Seric 			if (LogLevel > 2)
7503edb2e00Seric 				syslog(LOG_ERR, "makeconnection: service \"smtp\" unknown");
7513edb2e00Seric #endif
752e5311662Seric 			port = htons(25);
7531c71e510Seric 		}
754e5311662Seric 		else
755e2f2f828Seric 			port = sp->s_port;
756e2f2f828Seric 	}
757e2f2f828Seric 
75883c1f4bcSeric 	switch (addr.sa.sa_family)
759e2f2f828Seric 	{
7604d9c42c2Seric #ifdef NETINET
761e2f2f828Seric 	  case AF_INET:
76283c1f4bcSeric 		addr.sin.sin_port = port;
763e2f2f828Seric 		addrlen = sizeof (struct sockaddr_in);
764e2f2f828Seric 		break;
7654d9c42c2Seric #endif
766e2f2f828Seric 
767e2f2f828Seric #ifdef NETISO
768e2f2f828Seric 	  case AF_ISO:
769e2f2f828Seric 		/* assume two byte transport selector */
770e2f2f828Seric 		bcopy((char *) &port, TSEL((struct sockaddr_iso *) &addr), 2);
771e2f2f828Seric 		addrlen = sizeof (struct sockaddr_iso);
772e2f2f828Seric 		break;
773e2f2f828Seric #endif
774e2f2f828Seric 
775e2f2f828Seric 	  default:
77683c1f4bcSeric 		syserr("Can't connect to address family %d", addr.sa.sa_family);
777e2f2f828Seric 		return (EX_NOHOST);
7781c71e510Seric 	}
7797aa493c5Seric 
7807aa493c5Seric 	/*
7817aa493c5Seric 	**  Try to actually open the connection.
7827aa493c5Seric 	*/
7837aa493c5Seric 
784e17a3a5aSeric #ifdef XLA
785e17a3a5aSeric 	/* if too many connections, don't bother trying */
786e17a3a5aSeric 	if (!xla_noqueue_ok(host))
787e17a3a5aSeric 		return EX_TEMPFAIL;
788e17a3a5aSeric #endif
789e17a3a5aSeric 
7908b212fe0Seric 	firstconnect = TRUE;
791aea02ca1Seric 	for (;;)
792aea02ca1Seric 	{
79361e4310fSeric 		if (tTd(16, 1))
794e2f2f828Seric 			printf("makeconnection (%s [%s])\n",
795e2f2f828Seric 				host, anynet_ntoa(&addr));
7967aa493c5Seric 
797226e3022Seric 		/* save for logging */
798226e3022Seric 		CurHostAddr = addr;
799226e3022Seric 
800914346b1Seric 		if (usesecureport)
801914346b1Seric 		{
802914346b1Seric 			int rport = IPPORT_RESERVED - 1;
803914346b1Seric 
804914346b1Seric 			s = rresvport(&rport);
805914346b1Seric 		}
806914346b1Seric 		else
807914346b1Seric 		{
808af5e902cSeric 			s = socket(AF_INET, SOCK_STREAM, 0);
809914346b1Seric 		}
8107aa493c5Seric 		if (s < 0)
8117aa493c5Seric 		{
8126286bb75Sbloom 			sav_errno = errno;
813914346b1Seric 			syserr("makeconnection: no socket");
8147aa493c5Seric 			goto failure;
8157aa493c5Seric 		}
8167aa493c5Seric 
817b35447dbSeric #ifdef SO_SNDBUF
818b35447dbSeric 		if (TcpSndBufferSize > 0)
819b35447dbSeric 		{
820b35447dbSeric 			if (setsockopt(s, SOL_SOCKET, SO_SNDBUF,
821bf217a95Seric 				       (char *) &TcpSndBufferSize,
822b35447dbSeric 				       sizeof(TcpSndBufferSize)) < 0)
823b35447dbSeric 				syserr("makeconnection: setsockopt(SO_SNDBUF)");
824b35447dbSeric 		}
825b35447dbSeric #endif
826b35447dbSeric 
82761e4310fSeric 		if (tTd(16, 1))
828b31e7f2bSeric 			printf("makeconnection: fd=%d\n", s);
8291b6e4a15Seric 
8301b6e4a15Seric 		/* turn on network debugging? */
831a2ef5fa4Seric 		if (tTd(16, 101))
83252308a50Seric 		{
83352308a50Seric 			int on = 1;
8346173568dSeric 			(void) setsockopt(s, SOL_SOCKET, SO_DEBUG,
835aea02ca1Seric 					  (char *)&on, sizeof on);
83652308a50Seric 		}
83787d6e633Srick 		if (CurEnv->e_xfp != NULL)
838877a6142Seric 			(void) fflush(CurEnv->e_xfp);		/* for debugging */
8394bd6a662Seric 		errno = 0;					/* for debugging */
840e2f2f828Seric 		if (connect(s, (struct sockaddr *) &addr, addrlen) >= 0)
841aea02ca1Seric 			break;
842aea02ca1Seric 
8438b212fe0Seric 		/* if running demand-dialed connection, try again */
8448b212fe0Seric 		if (DialDelay > 0 && firstconnect)
8458b212fe0Seric 		{
8468b212fe0Seric 			if (tTd(16, 1))
8478b212fe0Seric 				printf("Connect failed (%s); trying again...\n",
8488b212fe0Seric 					errstring(sav_errno));
8498b212fe0Seric 			firstconnect = FALSE;
8508b212fe0Seric 			sleep(DialDelay);
8518b212fe0Seric 			continue;
8528b212fe0Seric 		}
8538b212fe0Seric 
854aea02ca1Seric 		/* couldn't connect.... figure out why */
8556286bb75Sbloom 		sav_errno = errno;
8566286bb75Sbloom 		(void) close(s);
8578b212fe0Seric 		if (hp != NULL && hp->h_addr_list[i])
85804344589Sbloom 		{
859aea02ca1Seric 			if (tTd(16, 1))
860e2f2f828Seric 				printf("Connect failed (%s); trying new address....\n",
861e2f2f828Seric 					errstring(sav_errno));
86283c1f4bcSeric 			switch (addr.sa.sa_family)
86383c1f4bcSeric 			{
86483c1f4bcSeric #ifdef NETINET
86583c1f4bcSeric 			  case AF_INET:
866e2f2f828Seric 				bcopy(hp->h_addr_list[i++],
86783c1f4bcSeric 				      &addr.sin.sin_addr,
8688b212fe0Seric 				      INADDRSZ);
86983c1f4bcSeric 				break;
87083c1f4bcSeric #endif
87183c1f4bcSeric 
87283c1f4bcSeric 			  default:
873e2f2f828Seric 				bcopy(hp->h_addr_list[i++],
87483c1f4bcSeric 					addr.sa.sa_data,
875914346b1Seric 					hp->h_length);
87683c1f4bcSeric 				break;
87783c1f4bcSeric 			}
878aea02ca1Seric 			continue;
87904344589Sbloom 		}
88004344589Sbloom 
8817aa493c5Seric 		/* failure, decide if temporary or not */
8827aa493c5Seric 	failure:
883244b09d1Seric #ifdef XLA
884244b09d1Seric 		xla_host_end(host);
885244b09d1Seric #endif
886e2de2524Seric 		if (transienterror(sav_errno))
887e2de2524Seric 			return EX_TEMPFAIL;
888e2de2524Seric 		else
88987d6e633Srick 		{
89008b25121Seric 			message("%s", errstring(sav_errno));
8917aa493c5Seric 			return (EX_UNAVAILABLE);
8927aa493c5Seric 		}
8937aa493c5Seric 	}
8947aa493c5Seric 
8957aa493c5Seric 	/* connection ok, put it into canonical form */
896335eae58Seric 	if ((mci->mci_out = fdopen(s, "w")) == NULL ||
897335eae58Seric 	    (s = dup(s)) < 0 ||
898ab81ee53Seric 	    (mci->mci_in = fdopen(s, "r")) == NULL)
899335eae58Seric 	{
900335eae58Seric 		syserr("cannot open SMTP client channel, fd=%d", s);
901335eae58Seric 		return EX_TEMPFAIL;
902335eae58Seric 	}
9037aa493c5Seric 
904dca8e1f7Seric 	return (EX_OK);
9057aa493c5Seric }
906444eaf03Seric /*
907444eaf03Seric **  MYHOSTNAME -- return the name of this host.
908444eaf03Seric **
909444eaf03Seric **	Parameters:
910444eaf03Seric **		hostbuf -- a place to return the name of this host.
911897f1869Seric **		size -- the size of hostbuf.
912444eaf03Seric **
913444eaf03Seric **	Returns:
914444eaf03Seric **		A list of aliases for this host.
915444eaf03Seric **
916444eaf03Seric **	Side Effects:
917d8d0a4aeSeric **		Adds numeric codes to $=w.
918444eaf03Seric */
919444eaf03Seric 
9208b212fe0Seric struct hostent *
921897f1869Seric myhostname(hostbuf, size)
922444eaf03Seric 	char hostbuf[];
923897f1869Seric 	int size;
924444eaf03Seric {
92538ad259dSeric 	register struct hostent *hp;
9268b212fe0Seric 	extern bool getcanonname();
9278b212fe0Seric 	extern int h_errno;
928444eaf03Seric 
929af5e902cSeric 	if (gethostname(hostbuf, size) < 0)
930af5e902cSeric 	{
931af5e902cSeric 		(void) strcpy(hostbuf, "localhost");
932af5e902cSeric 	}
9338b212fe0Seric 	hp = sm_gethostbyname(hostbuf);
9344a5c6430Seric 	if (hp == NULL)
9358b212fe0Seric 		return NULL;
9368b212fe0Seric 	if (strchr(hp->h_name, '.') != NULL || strchr(hostbuf, '.') == NULL)
937423161fbSeric 	{
93835852b23Seric 		(void) strncpy(hostbuf, hp->h_name, size - 1);
93935852b23Seric 		hostbuf[size - 1] = '\0';
9408b212fe0Seric 	}
9414a5c6430Seric 
9424a5c6430Seric #if NAMED_BIND
9438b212fe0Seric 	/*
9448b212fe0Seric 	**  If still no dot, try DNS directly (i.e., avoid NIS problems).
9458b212fe0Seric 	**  This ought to be driven from the configuration file, but
9468b212fe0Seric 	**  we are called before the configuration is read.  We could
9478b212fe0Seric 	**  check for an /etc/resolv.conf file, but that isn't required.
9488b212fe0Seric 	**  All in all, a bit of a mess.
9498b212fe0Seric 	*/
95023668637Seric 
9518b212fe0Seric 	if (strchr(hostbuf, '.') == NULL &&
9528b212fe0Seric 	    !getcanonname(hostbuf, size, TRUE) &&
9538b212fe0Seric 	    h_errno == TRY_AGAIN)
95423668637Seric 	{
9558b212fe0Seric 		/* try twice in case name server not yet started up */
9568b212fe0Seric 		message("My unqualifed host name (%s) unknown to DNS; sleeping for retry",
95723668637Seric 			hostbuf);
9588b212fe0Seric 		sleep(60);
9598b212fe0Seric 		if (!getcanonname(hostbuf, size, TRUE))
9608b212fe0Seric 			errno = h_errno + E_DNSBASE;
961747df804Seric 	}
962747df804Seric #endif
9638b212fe0Seric 	return (hp);
9647364df9fSeric }
965cb452edcSeric /*
9669f8b0eadSeric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
9679f8b0eadSeric **
9689f8b0eadSeric **	Uses RFC1413 protocol to try to get info from the other end.
969320e0d1cSeric **
970320e0d1cSeric **	Parameters:
971320e0d1cSeric **		fd -- the descriptor
972320e0d1cSeric **
973320e0d1cSeric **	Returns:
9749f8b0eadSeric **		The user@host information associated with this descriptor.
975320e0d1cSeric */
976320e0d1cSeric 
9779f8b0eadSeric static jmp_buf	CtxAuthTimeout;
9789f8b0eadSeric 
9798b212fe0Seric static void
9809f8b0eadSeric authtimeout()
9819f8b0eadSeric {
9829f8b0eadSeric 	longjmp(CtxAuthTimeout, 1);
9839f8b0eadSeric }
9849f8b0eadSeric 
985320e0d1cSeric char *
9869f8b0eadSeric getauthinfo(fd)
987320e0d1cSeric 	int fd;
988320e0d1cSeric {
9899f8b0eadSeric 	int falen;
990a5546e24Seric 	register char *p;
9919f8b0eadSeric 	SOCKADDR la;
9929f8b0eadSeric 	int lalen;
9939f8b0eadSeric 	register struct servent *sp;
9949f8b0eadSeric 	int s;
9959f8b0eadSeric 	int i;
9969f8b0eadSeric 	EVENT *ev;
997579270a3Seric 	int nleft;
998f036c8d7Seric 	char ibuf[MAXNAME + 1];
9999f8b0eadSeric 	static char hbuf[MAXNAME * 2 + 2];
10009f8b0eadSeric 	extern char *hostnamebyanyaddr();
10019f8b0eadSeric 	extern char RealUserName[];			/* main.c */
1002320e0d1cSeric 
1003e29a76d1Seric 	falen = sizeof RealHostAddr;
10048b212fe0Seric 	if (isatty(fd) || getpeername(fd, &RealHostAddr.sa, &falen) < 0 ||
10058b212fe0Seric 	    falen <= 0 || RealHostAddr.sa.sa_family == 0)
10069f8b0eadSeric 	{
10079f8b0eadSeric 		(void) sprintf(hbuf, "%s@localhost", RealUserName);
100853853673Seric 		if (tTd(9, 1))
10099f8b0eadSeric 			printf("getauthinfo: %s\n", hbuf);
1010320e0d1cSeric 		return hbuf;
1011320e0d1cSeric 	}
10129f8b0eadSeric 
1013e29a76d1Seric 	if (RealHostName == NULL)
1014e29a76d1Seric 	{
1015e29a76d1Seric 		/* translate that to a host name */
1016e29a76d1Seric 		RealHostName = newstr(hostnamebyanyaddr(&RealHostAddr));
1017e29a76d1Seric 	}
1018e29a76d1Seric 
101993b3215bSeric 	if (TimeOuts.to_ident == 0)
102093b3215bSeric 		goto noident;
102193b3215bSeric 
10229f8b0eadSeric 	lalen = sizeof la;
1023e29a76d1Seric 	if (RealHostAddr.sa.sa_family != AF_INET ||
10249f8b0eadSeric 	    getsockname(fd, &la.sa, &lalen) < 0 || lalen <= 0 ||
10259f8b0eadSeric 	    la.sa.sa_family != AF_INET)
10269f8b0eadSeric 	{
10279f8b0eadSeric 		/* no ident info */
10289f8b0eadSeric 		goto noident;
10299f8b0eadSeric 	}
10309f8b0eadSeric 
10319f8b0eadSeric 	/* create ident query */
103281d2944fSeric 	(void) sprintf(ibuf, "%d,%d\r\n",
1033e29a76d1Seric 		ntohs(RealHostAddr.sin.sin_port), ntohs(la.sin.sin_port));
10349f8b0eadSeric 
10359f8b0eadSeric 	/* create local address */
1036d6af7dadSeric 	la.sin.sin_port = 0;
10379f8b0eadSeric 
10389f8b0eadSeric 	/* create foreign address */
10399f8b0eadSeric 	sp = getservbyname("auth", "tcp");
10409f8b0eadSeric 	if (sp != NULL)
1041e29a76d1Seric 		RealHostAddr.sin.sin_port = sp->s_port;
10429f8b0eadSeric 	else
1043e29a76d1Seric 		RealHostAddr.sin.sin_port = htons(113);
10449f8b0eadSeric 
10459f8b0eadSeric 	s = -1;
10469f8b0eadSeric 	if (setjmp(CtxAuthTimeout) != 0)
10479f8b0eadSeric 	{
10489f8b0eadSeric 		if (s >= 0)
10499f8b0eadSeric 			(void) close(s);
10509f8b0eadSeric 		goto noident;
10519f8b0eadSeric 	}
10529f8b0eadSeric 
10539f8b0eadSeric 	/* put a timeout around the whole thing */
1054a0f780efSeric 	ev = setevent(TimeOuts.to_ident, authtimeout, 0);
10559f8b0eadSeric 
1056d6af7dadSeric 	/* connect to foreign IDENT server using same address as SMTP socket */
10579f8b0eadSeric 	s = socket(AF_INET, SOCK_STREAM, 0);
10589f8b0eadSeric 	if (s < 0)
10599f8b0eadSeric 	{
10609f8b0eadSeric 		clrevent(ev);
10619f8b0eadSeric 		goto noident;
10629f8b0eadSeric 	}
1063d6af7dadSeric 	if (bind(s, &la.sa, sizeof la.sin) < 0 ||
1064e29a76d1Seric 	    connect(s, &RealHostAddr.sa, sizeof RealHostAddr.sin) < 0)
10659f8b0eadSeric 	{
10667c201575Seric 		goto closeident;
10679f8b0eadSeric 	}
10689f8b0eadSeric 
106953853673Seric 	if (tTd(9, 10))
107081d2944fSeric 		printf("getauthinfo: sent %s", ibuf);
10719f8b0eadSeric 
10729f8b0eadSeric 	/* send query */
107381d2944fSeric 	if (write(s, ibuf, strlen(ibuf)) < 0)
10749f8b0eadSeric 		goto closeident;
10759f8b0eadSeric 
10769f8b0eadSeric 	/* get result */
107781d2944fSeric 	p = &ibuf[0];
1078b26156f6Seric 	nleft = sizeof ibuf - 1;
1079579270a3Seric 	while ((i = read(s, p, nleft)) > 0)
1080579270a3Seric 	{
1081579270a3Seric 		p += i;
1082579270a3Seric 		nleft -= i;
1083579270a3Seric 	}
10849f8b0eadSeric 	(void) close(s);
10859f8b0eadSeric 	clrevent(ev);
108681d2944fSeric 	if (i < 0 || p == &ibuf[0])
10879f8b0eadSeric 		goto noident;
1088579270a3Seric 
1089579270a3Seric 	if (*--p == '\n' && *--p == '\r')
1090579270a3Seric 		p--;
1091579270a3Seric 	*++p = '\0';
10929f8b0eadSeric 
109353853673Seric 	if (tTd(9, 3))
109481d2944fSeric 		printf("getauthinfo:  got %s\n", ibuf);
10959f8b0eadSeric 
10969f8b0eadSeric 	/* parse result */
109781d2944fSeric 	p = strchr(ibuf, ':');
10989f8b0eadSeric 	if (p == NULL)
10999f8b0eadSeric 	{
11009f8b0eadSeric 		/* malformed response */
11019f8b0eadSeric 		goto noident;
11029f8b0eadSeric 	}
11039f8b0eadSeric 	while (isascii(*++p) && isspace(*p))
11049f8b0eadSeric 		continue;
11059f8b0eadSeric 	if (strncasecmp(p, "userid", 6) != 0)
11069f8b0eadSeric 	{
11079f8b0eadSeric 		/* presumably an error string */
11089f8b0eadSeric 		goto noident;
11099f8b0eadSeric 	}
11109f8b0eadSeric 	p += 6;
11119f8b0eadSeric 	while (isascii(*p) && isspace(*p))
11129f8b0eadSeric 		p++;
11139f8b0eadSeric 	if (*p++ != ':')
11149f8b0eadSeric 	{
11159f8b0eadSeric 		/* either useridxx or malformed response */
11169f8b0eadSeric 		goto noident;
11179f8b0eadSeric 	}
11189f8b0eadSeric 
11199f8b0eadSeric 	/* p now points to the OSTYPE field */
11208b212fe0Seric 	while (isascii(*p) && isspace(*p))
11218b212fe0Seric 		p++;
11228b212fe0Seric 	if (strncasecmp(p, "other", 5) == 0 &&
11238b212fe0Seric 	    (p[5] == ':' || p[5] == ' ' || p[5] == ',' || p[5] == '\0'))
11248b212fe0Seric 	{
11258b212fe0Seric 		/* not useful information */
11268b212fe0Seric 		goto noident;
11278b212fe0Seric 	}
11289f8b0eadSeric 	p = strchr(p, ':');
11299f8b0eadSeric 	if (p == NULL)
11309f8b0eadSeric 	{
11319f8b0eadSeric 		/* malformed response */
11329f8b0eadSeric 		goto noident;
11339f8b0eadSeric 	}
113453853673Seric 
113553853673Seric 	/* 1413 says don't do this -- but it's broken otherwise */
113653853673Seric 	while (isascii(*++p) && isspace(*p))
113753853673Seric 		continue;
11389f8b0eadSeric 
1139a7763879Seric 	/* p now points to the authenticated name -- copy carefully */
114081d2944fSeric 	cleanstrcpy(hbuf, p, MAXNAME);
1141a7763879Seric 	hbuf[i++] = '@';
1142a7763879Seric 	strcpy(&hbuf[i], RealHostName == NULL ? "localhost" : RealHostName);
114353853673Seric 	goto finish;
114453853673Seric 
11457c201575Seric closeident:
11467c201575Seric 	(void) close(s);
11477c201575Seric 	clrevent(ev);
11487c201575Seric 
114953853673Seric noident:
1150f7869e68Seric 	if (RealHostName == NULL)
1151f7869e68Seric 	{
1152f7869e68Seric 		if (tTd(9, 1))
1153f7869e68Seric 			printf("getauthinfo: NULL\n");
1154f7869e68Seric 		return NULL;
1155f7869e68Seric 	}
115653853673Seric 	(void) strcpy(hbuf, RealHostName);
115753853673Seric 
115853853673Seric finish:
1159f7869e68Seric 	if (RealHostName != NULL && RealHostName[0] != '[')
11609f8b0eadSeric 	{
11619f8b0eadSeric 		p = &hbuf[strlen(hbuf)];
11629f8b0eadSeric 		(void) sprintf(p, " [%s]", anynet_ntoa(&RealHostAddr));
11639f8b0eadSeric 	}
116453853673Seric 	if (tTd(9, 1))
11659f8b0eadSeric 		printf("getauthinfo: %s\n", hbuf);
11669f8b0eadSeric 	return hbuf;
11679f8b0eadSeric }
1168320e0d1cSeric /*
116908de856eSeric **  HOST_MAP_LOOKUP -- turn a hostname into canonical form
117015d084d5Seric **
117115d084d5Seric **	Parameters:
117205b57da8Seric **		map -- a pointer to this map (unused).
117308de856eSeric **		name -- the (presumably unqualified) hostname.
117400b385a9Seric **		av -- unused -- for compatibility with other mapping
1175d798a1deSeric **			functions.
11762d29d43aSeric **		statp -- an exit status (out parameter) -- set to
11772d29d43aSeric **			EX_TEMPFAIL if the name server is unavailable.
117815d084d5Seric **
117915d084d5Seric **	Returns:
118015d084d5Seric **		The mapping, if found.
118115d084d5Seric **		NULL if no mapping found.
118215d084d5Seric **
118315d084d5Seric **	Side Effects:
118415d084d5Seric **		Looks up the host specified in hbuf.  If it is not
118515d084d5Seric **		the canonical name for that host, return the canonical
118615d084d5Seric **		name.
1187f36ede03Sbostic */
1188cb452edcSeric 
118915d084d5Seric char *
119000b385a9Seric host_map_lookup(map, name, av, statp)
119105b57da8Seric 	MAP *map;
119208de856eSeric 	char *name;
119300b385a9Seric 	char **av;
11942d29d43aSeric 	int *statp;
119599f7cf32Seric {
119699f7cf32Seric 	register struct hostent *hp;
11978b212fe0Seric 	struct in_addr in_addr;
119805b57da8Seric 	char *cp;
1199eea91d78Seric 	register STAB *s;
12008b212fe0Seric 	char hbuf[MAXNAME + 1];
12019d4a8008Seric #if NAMED_BIND
1202eea91d78Seric 	extern int h_errno;
1203c304a798Seric #endif
12045f78836eSmiriam 
1205f36ede03Sbostic 	/*
1206eea91d78Seric 	**  See if we have already looked up this name.  If so, just
1207eea91d78Seric 	**  return it.
1208eea91d78Seric 	*/
1209eea91d78Seric 
121008de856eSeric 	s = stab(name, ST_NAMECANON, ST_ENTER);
1211eea91d78Seric 	if (bitset(NCF_VALID, s->s_namecanon.nc_flags))
1212eea91d78Seric 	{
1213f92c3297Seric 		if (tTd(9, 1))
121408de856eSeric 			printf("host_map_lookup(%s) => CACHE %s\n",
121508de856eSeric 				name, s->s_namecanon.nc_cname);
1216eea91d78Seric 		errno = s->s_namecanon.nc_errno;
12179d4a8008Seric #if NAMED_BIND
1218eea91d78Seric 		h_errno = s->s_namecanon.nc_herrno;
1219c304a798Seric #endif
1220eea91d78Seric 		*statp = s->s_namecanon.nc_stat;
122192270fb3Seric 		if (CurEnv->e_message == NULL && *statp == EX_TEMPFAIL)
1222ed63aae0Seric 		{
1223ed63aae0Seric 			sprintf(hbuf, "%s: Name server timeout",
1224ed63aae0Seric 				shortenstring(name, 33));
1225ed63aae0Seric 			CurEnv->e_message = newstr(hbuf);
1226ed63aae0Seric 		}
1227eea91d78Seric 		return s->s_namecanon.nc_cname;
1228eea91d78Seric 	}
1229eea91d78Seric 
1230eea91d78Seric 	/*
1231eea91d78Seric 	**  If first character is a bracket, then it is an address
1232eea91d78Seric 	**  lookup.  Address is copied into a temporary buffer to
123308de856eSeric 	**  strip the brackets and to preserve name if address is
1234eea91d78Seric 	**  unknown.
1235f36ede03Sbostic 	*/
123615d084d5Seric 
123708de856eSeric 	if (*name != '[')
123815d084d5Seric 	{
1239d798a1deSeric 		extern bool getcanonname();
1240d798a1deSeric 
12418cb4653dSeric 		if (tTd(9, 1))
124208de856eSeric 			printf("host_map_lookup(%s) => ", name);
1243eea91d78Seric 		s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
12448b212fe0Seric 		if (strlen(name) < sizeof hbuf)
124508de856eSeric 			(void) strcpy(hbuf, name);
12468b212fe0Seric 		else
12478b212fe0Seric 		{
12488b212fe0Seric 			bcopy(name, hbuf, sizeof hbuf - 1);
12498b212fe0Seric 			hbuf[sizeof hbuf - 1] = '\0';
12508b212fe0Seric 		}
12510183c3f6Seric 		if (getcanonname(hbuf, sizeof hbuf - 1, !NoMXforCanon))
12529040ec4fSeric 		{
12539040ec4fSeric 			if (tTd(9, 1))
12549040ec4fSeric 				printf("%s\n", hbuf);
125500b385a9Seric 			cp = map_rewrite(map, hbuf, strlen(hbuf), av);
125600b385a9Seric 			s->s_namecanon.nc_cname = newstr(cp);
125700b385a9Seric 			return cp;
12589040ec4fSeric 		}
125915d084d5Seric 		else
12609040ec4fSeric 		{
12612d29d43aSeric 			register struct hostent *hp;
12622d29d43aSeric 
1263c304a798Seric 			s->s_namecanon.nc_errno = errno;
12649d4a8008Seric #if NAMED_BIND
1265c304a798Seric 			s->s_namecanon.nc_herrno = h_errno;
12669040ec4fSeric 			if (tTd(9, 1))
12672d29d43aSeric 				printf("FAIL (%d)\n", h_errno);
12682d29d43aSeric 			switch (h_errno)
12692d29d43aSeric 			{
12702d29d43aSeric 			  case TRY_AGAIN:
127189cb2793Seric 				if (UseNameServer)
12728820d51bSeric 				{
1273e0326f4fSeric 					sprintf(hbuf, "%s: Name server timeout",
1274ed63aae0Seric 						shortenstring(name, 33));
1275e0326f4fSeric 					message("%s", hbuf);
12768820d51bSeric 					if (CurEnv->e_message == NULL)
1277e0326f4fSeric 						CurEnv->e_message = newstr(hbuf);
12788820d51bSeric 				}
12792d29d43aSeric 				*statp = EX_TEMPFAIL;
12802d29d43aSeric 				break;
12812d29d43aSeric 
12822d29d43aSeric 			  case HOST_NOT_FOUND:
12832d29d43aSeric 				*statp = EX_NOHOST;
12842d29d43aSeric 				break;
12852d29d43aSeric 
12862d29d43aSeric 			  case NO_RECOVERY:
12872d29d43aSeric 				*statp = EX_SOFTWARE;
12882d29d43aSeric 				break;
12892d29d43aSeric 
12902d29d43aSeric 			  default:
12912d29d43aSeric 				*statp = EX_UNAVAILABLE;
12922d29d43aSeric 				break;
12932d29d43aSeric 			}
1294c304a798Seric #else
1295c304a798Seric 			if (tTd(9, 1))
1296c304a798Seric 				printf("FAIL\n");
1297c304a798Seric 			*statp = EX_NOHOST;
1298c304a798Seric #endif
1299eea91d78Seric 			s->s_namecanon.nc_stat = *statp;
13008b212fe0Seric 			if ((*statp != EX_TEMPFAIL && *statp != EX_NOHOST) ||
13018b212fe0Seric 			    UseNameServer)
130215d084d5Seric 				return NULL;
13032d29d43aSeric 
13042d29d43aSeric 			/*
13052d29d43aSeric 			**  Try to look it up in /etc/hosts
13062d29d43aSeric 			*/
13072d29d43aSeric 
13088b212fe0Seric 			hp = sm_gethostbyname(name);
13092d29d43aSeric 			if (hp == NULL)
13102d29d43aSeric 			{
13112d29d43aSeric 				/* no dice there either */
1312eea91d78Seric 				s->s_namecanon.nc_stat = *statp = EX_NOHOST;
13132d29d43aSeric 				return NULL;
13142d29d43aSeric 			}
13152d29d43aSeric 
1316eea91d78Seric 			s->s_namecanon.nc_stat = *statp = EX_OK;
131700b385a9Seric 			cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
131800b385a9Seric 			s->s_namecanon.nc_cname = newstr(cp);
131900b385a9Seric 			return cp;
132015d084d5Seric 		}
13219040ec4fSeric 	}
132208de856eSeric 	if ((cp = strchr(name, ']')) == NULL)
132315d084d5Seric 		return (NULL);
132434e39927Sbostic 	*cp = '\0';
13258b212fe0Seric 	in_addr.s_addr = inet_addr(&name[1]);
132638ad259dSeric 
132738ad259dSeric 	/* nope -- ask the name server */
13288b212fe0Seric 	hp = sm_gethostbyaddr((char *)&in_addr, INADDRSZ, AF_INET);
1329eea91d78Seric 	s->s_namecanon.nc_errno = errno;
13309d4a8008Seric #if NAMED_BIND
1331eea91d78Seric 	s->s_namecanon.nc_herrno = h_errno;
1332c304a798Seric #endif
1333eea91d78Seric 	s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
13345f78836eSmiriam 	if (hp == NULL)
1335eea91d78Seric 	{
1336eea91d78Seric 		s->s_namecanon.nc_stat = *statp = EX_NOHOST;
133715d084d5Seric 		return (NULL);
1338eea91d78Seric 	}
133915d084d5Seric 
134038ad259dSeric 	/* found a match -- copy out */
134100b385a9Seric 	cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
1342eea91d78Seric 	s->s_namecanon.nc_stat = *statp = EX_OK;
134300b385a9Seric 	s->s_namecanon.nc_cname = newstr(cp);
134400b385a9Seric 	return cp;
134599f7cf32Seric }
1346e2f2f828Seric /*
1347e2f2f828Seric **  ANYNET_NTOA -- convert a network address to printable form.
1348e2f2f828Seric **
1349e2f2f828Seric **	Parameters:
1350e2f2f828Seric **		sap -- a pointer to a sockaddr structure.
1351e2f2f828Seric **
1352e2f2f828Seric **	Returns:
1353e2f2f828Seric **		A printable version of that sockaddr.
1354e2f2f828Seric */
1355e2f2f828Seric 
1356e2f2f828Seric char *
1357e2f2f828Seric anynet_ntoa(sap)
1358e2f2f828Seric 	register SOCKADDR *sap;
1359e2f2f828Seric {
1360e2f2f828Seric 	register char *bp;
1361e2f2f828Seric 	register char *ap;
1362e2f2f828Seric 	int l;
1363e387851eSeric 	static char buf[100];
1364e2f2f828Seric 
13658cb4653dSeric 	/* check for null/zero family */
13668cb4653dSeric 	if (sap == NULL)
13678cb4653dSeric 		return "NULLADDR";
13688cb4653dSeric 	if (sap->sa.sa_family == 0)
13698cb4653dSeric 		return "0";
13708cb4653dSeric 
1371e387851eSeric 	switch (sap->sa.sa_family)
1372e387851eSeric 	{
1373139b52c8Seric #ifdef NETUNIX
1374e387851eSeric 	  case AF_UNIX:
1375c24cf5a4Seric 	  	if (sap->sunix.sun_path[0] != '\0')
1376c24cf5a4Seric 	  		sprintf(buf, "[UNIX: %.64s]", sap->sunix.sun_path);
1377e387851eSeric 	  	else
1378e387851eSeric 	  		sprintf(buf, "[UNIX: localhost]");
1379e387851eSeric 		return buf;
1380e387851eSeric #endif
1381e387851eSeric 
138283c1f4bcSeric #ifdef NETINET
1383e387851eSeric 	  case AF_INET:
1384*482e28bcSeric 		return inet_ntoa(sap->sin.sin_addr);
138583c1f4bcSeric #endif
1386e2f2f828Seric 
1387e387851eSeric 	  default:
1388e387851eSeric 	  	/* this case is only to ensure syntactic correctness */
1389e387851eSeric 	  	break;
1390e387851eSeric 	}
1391e387851eSeric 
1392e2f2f828Seric 	/* unknown family -- just dump bytes */
139383c1f4bcSeric 	(void) sprintf(buf, "Family %d: ", sap->sa.sa_family);
1394e2f2f828Seric 	bp = &buf[strlen(buf)];
139583c1f4bcSeric 	ap = sap->sa.sa_data;
139683c1f4bcSeric 	for (l = sizeof sap->sa.sa_data; --l >= 0; )
1397e2f2f828Seric 	{
1398e2f2f828Seric 		(void) sprintf(bp, "%02x:", *ap++ & 0377);
1399e2f2f828Seric 		bp += 3;
1400e2f2f828Seric 	}
1401e2f2f828Seric 	*--bp = '\0';
1402e2f2f828Seric 	return buf;
1403e2f2f828Seric }
14049f8b0eadSeric /*
14059f8b0eadSeric **  HOSTNAMEBYANYADDR -- return name of host based on address
14069f8b0eadSeric **
14079f8b0eadSeric **	Parameters:
14089f8b0eadSeric **		sap -- SOCKADDR pointer
14099f8b0eadSeric **
14109f8b0eadSeric **	Returns:
14119f8b0eadSeric **		text representation of host name.
14129f8b0eadSeric **
14139f8b0eadSeric **	Side Effects:
14149f8b0eadSeric **		none.
14159f8b0eadSeric */
14169f8b0eadSeric 
14179f8b0eadSeric char *
14189f8b0eadSeric hostnamebyanyaddr(sap)
14199f8b0eadSeric 	register SOCKADDR *sap;
14209f8b0eadSeric {
14219f8b0eadSeric 	register struct hostent *hp;
14223490b9dfSeric 	int saveretry;
14233490b9dfSeric 
14249d4a8008Seric #if NAMED_BIND
14253490b9dfSeric 	/* shorten name server timeout to avoid higher level timeouts */
14263490b9dfSeric 	saveretry = _res.retry;
14273490b9dfSeric 	_res.retry = 3;
14283490b9dfSeric #endif /* NAMED_BIND */
14293490b9dfSeric 
14309f8b0eadSeric 	switch (sap->sa.sa_family)
14319f8b0eadSeric 	{
14329f8b0eadSeric #ifdef NETINET
14339f8b0eadSeric 	  case AF_INET:
14348b212fe0Seric 		hp = sm_gethostbyaddr((char *) &sap->sin.sin_addr,
14358b212fe0Seric 			INADDRSZ,
14369f8b0eadSeric 			AF_INET);
14379f8b0eadSeric 		break;
14389f8b0eadSeric #endif
14399f8b0eadSeric 
14409f8b0eadSeric #ifdef NETISO
14419f8b0eadSeric 	  case AF_ISO:
14428b212fe0Seric 		hp = sm_gethostbyaddr((char *) &sap->siso.siso_addr,
14439f8b0eadSeric 			sizeof sap->siso.siso_addr,
14449f8b0eadSeric 			AF_ISO);
14459f8b0eadSeric 		break;
14469f8b0eadSeric #endif
14479f8b0eadSeric 
1448e387851eSeric 	  case AF_UNIX:
1449e387851eSeric 		hp = NULL;
1450e387851eSeric 		break;
1451e387851eSeric 
14529f8b0eadSeric 	  default:
14538b212fe0Seric 		hp = sm_gethostbyaddr(sap->sa.sa_data,
14549f8b0eadSeric 			   sizeof sap->sa.sa_data,
14559f8b0eadSeric 			   sap->sa.sa_family);
14569f8b0eadSeric 		break;
14579f8b0eadSeric 	}
14589f8b0eadSeric 
14599d4a8008Seric #if NAMED_BIND
14603490b9dfSeric 	_res.retry = saveretry;
14613490b9dfSeric #endif /* NAMED_BIND */
14623490b9dfSeric 
14639f8b0eadSeric 	if (hp != NULL)
14649f8b0eadSeric 		return hp->h_name;
14659f8b0eadSeric 	else
14669f8b0eadSeric 	{
14679f8b0eadSeric 		/* produce a dotted quad */
14689f8b0eadSeric 		static char buf[512];
14699f8b0eadSeric 
14709f8b0eadSeric 		(void) sprintf(buf, "[%s]", anynet_ntoa(sap));
14719f8b0eadSeric 		return buf;
14729f8b0eadSeric 	}
14739f8b0eadSeric }
1474f36ede03Sbostic 
14756c2c3107Seric # else /* DAEMON */
147699f7cf32Seric /* code for systems without sophisticated networking */
1477444eaf03Seric 
1478444eaf03Seric /*
1479444eaf03Seric **  MYHOSTNAME -- stub version for case of no daemon code.
148021e9914dSeric **
148121e9914dSeric **	Can't convert to upper case here because might be a UUCP name.
1482897f1869Seric **
1483897f1869Seric **	Mark, you can change this to be anything you want......
1484444eaf03Seric */
1485444eaf03Seric 
1486444eaf03Seric char **
1487897f1869Seric myhostname(hostbuf, size)
1488444eaf03Seric 	char hostbuf[];
1489897f1869Seric 	int size;
1490444eaf03Seric {
1491444eaf03Seric 	register FILE *f;
1492444eaf03Seric 
1493444eaf03Seric 	hostbuf[0] = '\0';
1494444eaf03Seric 	f = fopen("/usr/include/whoami", "r");
1495444eaf03Seric 	if (f != NULL)
1496444eaf03Seric 	{
1497897f1869Seric 		(void) fgets(hostbuf, size, f);
1498444eaf03Seric 		fixcrlf(hostbuf, TRUE);
1499444eaf03Seric 		(void) fclose(f);
1500444eaf03Seric 	}
1501444eaf03Seric 	return (NULL);
1502444eaf03Seric }
150399f7cf32Seric /*
15049f8b0eadSeric **  GETAUTHINFO -- get the real host name asociated with a file descriptor
1505320e0d1cSeric **
1506320e0d1cSeric **	Parameters:
1507320e0d1cSeric **		fd -- the descriptor
1508320e0d1cSeric **
1509320e0d1cSeric **	Returns:
1510320e0d1cSeric **		The host name associated with this descriptor, if it can
1511320e0d1cSeric **			be determined.
1512320e0d1cSeric **		NULL otherwise.
1513320e0d1cSeric **
1514320e0d1cSeric **	Side Effects:
1515320e0d1cSeric **		none
1516320e0d1cSeric */
1517320e0d1cSeric 
1518320e0d1cSeric char *
15199f8b0eadSeric getauthinfo(fd)
1520320e0d1cSeric 	int fd;
1521320e0d1cSeric {
1522320e0d1cSeric 	return NULL;
1523320e0d1cSeric }
1524320e0d1cSeric /*
152599f7cf32Seric **  MAPHOSTNAME -- turn a hostname into canonical form
152699f7cf32Seric **
152799f7cf32Seric **	Parameters:
152805b57da8Seric **		map -- a pointer to the database map.
152908de856eSeric **		name -- a buffer containing a hostname.
153015d084d5Seric **		avp -- a pointer to a (cf file defined) argument vector.
15312d29d43aSeric **		statp -- an exit status (out parameter).
153299f7cf32Seric **
153399f7cf32Seric **	Returns:
153415d084d5Seric **		mapped host name
1535cb452edcSeric **		FALSE otherwise.
153699f7cf32Seric **
153799f7cf32Seric **	Side Effects:
153808de856eSeric **		Looks up the host specified in name.  If it is not
153999f7cf32Seric **		the canonical name for that host, replace it with
154099f7cf32Seric **		the canonical name.  If the name is unknown, or it
154199f7cf32Seric **		is already the canonical name, leave it unchanged.
154299f7cf32Seric */
154399f7cf32Seric 
154499f7cf32Seric /*ARGSUSED*/
154515d084d5Seric char *
154608de856eSeric host_map_lookup(map, name, avp, statp)
154705b57da8Seric 	MAP *map;
154808de856eSeric 	char *name;
154915d084d5Seric 	char **avp;
15502d29d43aSeric 	char *statp;
155199f7cf32Seric {
15522d29d43aSeric 	register struct hostent *hp;
15532d29d43aSeric 
15548b212fe0Seric 	hp = sm_gethostbyname(name);
15552d29d43aSeric 	if (hp != NULL)
15562d29d43aSeric 		return hp->h_name;
15572d29d43aSeric 	*statp = EX_NOHOST;
155815d084d5Seric 	return NULL;
155999f7cf32Seric }
156099f7cf32Seric 
15616c2c3107Seric #endif /* DAEMON */
1562