xref: /original-bsd/usr.sbin/sendmail/src/daemon.c (revision 9607e8bc)
1 /*
2  * Copyright (c) 1983 Eric P. Allman
3  * Copyright (c) 1988, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * %sccs.include.redist.c%
7  */
8 
9 #include <errno.h>
10 #include "sendmail.h"
11 
12 #ifndef lint
13 #ifdef DAEMON
14 static char sccsid[] = "@(#)daemon.c	8.67 (Berkeley) 01/24/95 (with daemon mode)";
15 #else
16 static char sccsid[] = "@(#)daemon.c	8.67 (Berkeley) 01/24/95 (without daemon mode)";
17 #endif
18 #endif /* not lint */
19 
20 #ifdef DAEMON
21 
22 # include <netdb.h>
23 # include <arpa/inet.h>
24 
25 #if NAMED_BIND
26 # include <resolv.h>
27 #endif
28 
29 /*
30 **  DAEMON.C -- routines to use when running as a daemon.
31 **
32 **	This entire file is highly dependent on the 4.2 BSD
33 **	interprocess communication primitives.  No attempt has
34 **	been made to make this file portable to Version 7,
35 **	Version 6, MPX files, etc.  If you should try such a
36 **	thing yourself, I recommend chucking the entire file
37 **	and starting from scratch.  Basic semantics are:
38 **
39 **	getrequests()
40 **		Opens a port and initiates a connection.
41 **		Returns in a child.  Must set InChannel and
42 **		OutChannel appropriately.
43 **	clrdaemon()
44 **		Close any open files associated with getting
45 **		the connection; this is used when running the queue,
46 **		etc., to avoid having extra file descriptors during
47 **		the queue run and to avoid confusing the network
48 **		code (if it cares).
49 **	makeconnection(host, port, outfile, infile, usesecureport)
50 **		Make a connection to the named host on the given
51 **		port.  Set *outfile and *infile to the files
52 **		appropriate for communication.  Returns zero on
53 **		success, else an exit status describing the
54 **		error.
55 **	host_map_lookup(map, hbuf, avp, pstat)
56 **		Convert the entry in hbuf into a canonical form.
57 */
58 /*
59 **  GETREQUESTS -- open mail IPC port and get requests.
60 **
61 **	Parameters:
62 **		none.
63 **
64 **	Returns:
65 **		none.
66 **
67 **	Side Effects:
68 **		Waits until some interesting activity occurs.  When
69 **		it does, a child is created to process it, and the
70 **		parent waits for completion.  Return from this
71 **		routine is always in the child.  The file pointers
72 **		"InChannel" and "OutChannel" should be set to point
73 **		to the communication channel.
74 */
75 
76 int		DaemonSocket	= -1;		/* fd describing socket */
77 SOCKADDR	DaemonAddr;			/* socket for incoming */
78 int		ListenQueueSize = 10;		/* size of listen queue */
79 int		TcpRcvBufferSize = 0;		/* size of TCP receive buffer */
80 int		TcpSndBufferSize = 0;		/* size of TCP send buffer */
81 
82 getrequests()
83 {
84 	int t;
85 	bool refusingconnections = TRUE;
86 	FILE *pidf;
87 	int socksize;
88 #ifdef XDEBUG
89 	bool j_has_dot;
90 #endif
91 	extern void reapchild();
92 
93 	/*
94 	**  Set up the address for the mailer.
95 	*/
96 
97 	if (DaemonAddr.sin.sin_family == 0)
98 		DaemonAddr.sin.sin_family = AF_INET;
99 	if (DaemonAddr.sin.sin_addr.s_addr == 0)
100 		DaemonAddr.sin.sin_addr.s_addr = INADDR_ANY;
101 	if (DaemonAddr.sin.sin_port == 0)
102 	{
103 		register struct servent *sp;
104 
105 		sp = getservbyname("smtp", "tcp");
106 		if (sp == NULL)
107 		{
108 			syserr("554 service \"smtp\" unknown");
109 			DaemonAddr.sin.sin_port = htons(25);
110 		}
111 		else
112 			DaemonAddr.sin.sin_port = sp->s_port;
113 	}
114 
115 	/*
116 	**  Try to actually open the connection.
117 	*/
118 
119 	if (tTd(15, 1))
120 		printf("getrequests: port 0x%x\n", DaemonAddr.sin.sin_port);
121 
122 	/* get a socket for the SMTP connection */
123 	socksize = opendaemonsocket(TRUE);
124 
125 	(void) setsignal(SIGCHLD, reapchild);
126 
127 	/* write the pid to the log file for posterity */
128 	pidf = fopen(PidFile, "w");
129 	if (pidf != NULL)
130 	{
131 		extern char *CommandLineArgs;
132 
133 		/* write the process id on line 1 */
134 		fprintf(pidf, "%d\n", getpid());
135 
136 		/* line 2 contains all command line flags */
137 		fprintf(pidf, "%s\n", CommandLineArgs);
138 
139 		/* flush and close */
140 		fclose(pidf);
141 	}
142 
143 #ifdef XDEBUG
144 	{
145 		char jbuf[MAXHOSTNAMELEN];
146 
147 		expand("\201j", jbuf, &jbuf[sizeof jbuf - 1], CurEnv);
148 		j_has_dot = strchr(jbuf, '.') != NULL;
149 	}
150 #endif
151 
152 	if (tTd(15, 1))
153 		printf("getrequests: %d\n", DaemonSocket);
154 
155 	for (;;)
156 	{
157 		register int pid;
158 		auto int lotherend;
159 		extern bool refuseconnections();
160 
161 		/* see if we are rejecting connections */
162 		CurrentLA = getla();
163 		if (refuseconnections())
164 		{
165 			if (DaemonSocket >= 0)
166 			{
167 				/* close socket so peer will fail quickly */
168 				(void) close(DaemonSocket);
169 				DaemonSocket = -1;
170 			}
171 			refusingconnections = TRUE;
172 			setproctitle("rejecting connections: load average: %d",
173 				CurrentLA);
174 			sleep(15);
175 			continue;
176 		}
177 
178 		/* arrange to (re)open the socket if necessary */
179 		if (refusingconnections)
180 		{
181 			(void) opendaemonsocket(FALSE);
182 			setproctitle("accepting connections");
183 			refusingconnections = FALSE;
184 		}
185 
186 #ifdef XDEBUG
187 		/* check for disaster */
188 		{
189 			char jbuf[MAXHOSTNAMELEN];
190 
191 			expand("\201j", jbuf, &jbuf[sizeof jbuf - 1], CurEnv);
192 			if (!wordinclass(jbuf, 'w'))
193 			{
194 				dumpstate("daemon lost $j");
195 				syslog(LOG_ALERT, "daemon process doesn't have $j in $=w; see syslog");
196 				abort();
197 			}
198 			else if (j_has_dot && strchr(jbuf, '.') == NULL)
199 			{
200 				dumpstate("daemon $j lost dot");
201 				syslog(LOG_ALERT, "daemon process $j lost dot; see syslog");
202 				abort();
203 			}
204 		}
205 #endif
206 
207 		/* wait for a connection */
208 		do
209 		{
210 			errno = 0;
211 			lotherend = socksize;
212 			t = accept(DaemonSocket,
213 			    (struct sockaddr *)&RealHostAddr, &lotherend);
214 		} while (t < 0 && errno == EINTR);
215 		if (t < 0)
216 		{
217 			syserr("getrequests: accept");
218 
219 			/* arrange to re-open the socket next time around */
220 			(void) close(DaemonSocket);
221 			DaemonSocket = -1;
222 			sleep(5);
223 			continue;
224 		}
225 
226 		/*
227 		**  Create a subprocess to process the mail.
228 		*/
229 
230 		if (tTd(15, 2))
231 			printf("getrequests: forking (fd = %d)\n", t);
232 
233 		pid = fork();
234 		if (pid < 0)
235 		{
236 			syserr("daemon: cannot fork");
237 			sleep(10);
238 			(void) close(t);
239 			continue;
240 		}
241 
242 		if (pid == 0)
243 		{
244 			char *p;
245 			extern char *hostnamebyanyaddr();
246 			extern void intsig();
247 
248 			/*
249 			**  CHILD -- return to caller.
250 			**	Collect verified idea of sending host.
251 			**	Verify calling user id if possible here.
252 			*/
253 
254 			(void) setsignal(SIGCHLD, SIG_DFL);
255 			(void) setsignal(SIGHUP, intsig);
256 			(void) close(DaemonSocket);
257 			DisConnected = FALSE;
258 
259 			setproctitle("startup with %s",
260 				anynet_ntoa(&RealHostAddr));
261 
262 			/* determine host name */
263 			p = hostnamebyanyaddr(&RealHostAddr);
264 			RealHostName = newstr(p);
265 			setproctitle("startup with %s", p);
266 
267 #ifdef LOG
268 			if (LogLevel > 11)
269 			{
270 				/* log connection information */
271 				syslog(LOG_INFO, "connect from %s (%s)",
272 					RealHostName, anynet_ntoa(&RealHostAddr));
273 			}
274 #endif
275 
276 			if ((InChannel = fdopen(t, "r")) == NULL ||
277 			    (t = dup(t)) < 0 ||
278 			    (OutChannel = fdopen(t, "w")) == NULL)
279 			{
280 				syserr("cannot open SMTP server channel, fd=%d", t);
281 				exit(0);
282 			}
283 
284 			/* should we check for illegal connection here? XXX */
285 #ifdef XLA
286 			if (!xla_host_ok(RealHostName))
287 			{
288 				message("421 Too many SMTP sessions for this host");
289 				exit(0);
290 			}
291 #endif
292 
293 			if (tTd(15, 2))
294 				printf("getreq: returning\n");
295 			return;
296 		}
297 
298 		/* close the port so that others will hang (for a while) */
299 		(void) close(t);
300 	}
301 	/*NOTREACHED*/
302 }
303 /*
304 **  OPENDAEMONSOCKET -- open the SMTP socket
305 **
306 **	Deals with setting all appropriate options.  DaemonAddr must
307 **	be set up in advance.
308 **
309 **	Parameters:
310 **		firsttime -- set if this is the initial open.
311 **
312 **	Returns:
313 **		Size in bytes of the daemon socket addr.
314 **
315 **	Side Effects:
316 **		Leaves DaemonSocket set to the open socket.
317 **		Exits if the socket cannot be created.
318 */
319 
320 #define MAXOPENTRIES	10	/* maximum number of tries to open connection */
321 
322 int
323 opendaemonsocket(firsttime)
324 	bool firsttime;
325 {
326 	int on = 1;
327 	int socksize;
328 	int ntries = 0;
329 	int saveerrno;
330 
331 	if (tTd(15, 2))
332 		printf("opendaemonsocket()\n");
333 
334 	do
335 	{
336 		if (ntries > 0)
337 			sleep(5);
338 		if (firsttime || DaemonSocket < 0)
339 		{
340 			DaemonSocket = socket(DaemonAddr.sa.sa_family, SOCK_STREAM, 0);
341 			if (DaemonSocket < 0)
342 			{
343 				/* probably another daemon already */
344 				saveerrno = errno;
345 				syserr("opendaemonsocket: can't create server SMTP socket");
346 			  severe:
347 # ifdef LOG
348 				if (LogLevel > 0)
349 					syslog(LOG_ALERT, "problem creating SMTP socket");
350 # endif /* LOG */
351 				DaemonSocket = -1;
352 				continue;
353 			}
354 
355 			/* turn on network debugging? */
356 			if (tTd(15, 101))
357 				(void) setsockopt(DaemonSocket, SOL_SOCKET,
358 						  SO_DEBUG, (char *)&on,
359 						  sizeof on);
360 
361 			(void) setsockopt(DaemonSocket, SOL_SOCKET,
362 					  SO_REUSEADDR, (char *)&on, sizeof on);
363 			(void) setsockopt(DaemonSocket, SOL_SOCKET,
364 					  SO_KEEPALIVE, (char *)&on, sizeof on);
365 
366 #ifdef SO_RCVBUF
367 			if (TcpRcvBufferSize > 0)
368 			{
369 				if (setsockopt(DaemonSocket, SOL_SOCKET,
370 					       SO_RCVBUF,
371 					       (char *) &TcpRcvBufferSize,
372 					       sizeof(TcpRcvBufferSize)) < 0)
373 					syserr("getrequests: setsockopt(SO_RCVBUF)");
374 			}
375 #endif
376 
377 			switch (DaemonAddr.sa.sa_family)
378 			{
379 # ifdef NETINET
380 			  case AF_INET:
381 				socksize = sizeof DaemonAddr.sin;
382 				break;
383 # endif
384 
385 # ifdef NETISO
386 			  case AF_ISO:
387 				socksize = sizeof DaemonAddr.siso;
388 				break;
389 # endif
390 
391 			  default:
392 				socksize = sizeof DaemonAddr;
393 				break;
394 			}
395 
396 			if (bind(DaemonSocket, &DaemonAddr.sa, socksize) < 0)
397 			{
398 				saveerrno = errno;
399 				syserr("getrequests: cannot bind");
400 				(void) close(DaemonSocket);
401 				goto severe;
402 			}
403 		}
404 		if (!firsttime && listen(DaemonSocket, ListenQueueSize) < 0)
405 		{
406 			saveerrno = errno;
407 			syserr("getrequests: cannot listen");
408 			(void) close(DaemonSocket);
409 			goto severe;
410 		}
411 		return socksize;
412 	} while (ntries++ < MAXOPENTRIES && transienterror(saveerrno));
413 	syserr("!opendaemonsocket: server SMTP socket wedged: exiting");
414 	finis();
415 }
416 /*
417 **  CLRDAEMON -- reset the daemon connection
418 **
419 **	Parameters:
420 **		none.
421 **
422 **	Returns:
423 **		none.
424 **
425 **	Side Effects:
426 **		releases any resources used by the passive daemon.
427 */
428 
429 clrdaemon()
430 {
431 	if (DaemonSocket >= 0)
432 		(void) close(DaemonSocket);
433 	DaemonSocket = -1;
434 }
435 /*
436 **  SETDAEMONOPTIONS -- set options for running the daemon
437 **
438 **	Parameters:
439 **		p -- the options line.
440 **
441 **	Returns:
442 **		none.
443 */
444 
445 setdaemonoptions(p)
446 	register char *p;
447 {
448 	if (DaemonAddr.sa.sa_family == AF_UNSPEC)
449 		DaemonAddr.sa.sa_family = AF_INET;
450 
451 	while (p != NULL)
452 	{
453 		register char *f;
454 		register char *v;
455 
456 		while (isascii(*p) && isspace(*p))
457 			p++;
458 		if (*p == '\0')
459 			break;
460 		f = p;
461 		p = strchr(p, ',');
462 		if (p != NULL)
463 			*p++ = '\0';
464 		v = strchr(f, '=');
465 		if (v == NULL)
466 			continue;
467 		while (isascii(*++v) && isspace(*v))
468 			continue;
469 
470 		switch (*f)
471 		{
472 		  case 'F':		/* address family */
473 			if (isascii(*v) && isdigit(*v))
474 				DaemonAddr.sa.sa_family = atoi(v);
475 #ifdef NETINET
476 			else if (strcasecmp(v, "inet") == 0)
477 				DaemonAddr.sa.sa_family = AF_INET;
478 #endif
479 #ifdef NETISO
480 			else if (strcasecmp(v, "iso") == 0)
481 				DaemonAddr.sa.sa_family = AF_ISO;
482 #endif
483 #ifdef NETNS
484 			else if (strcasecmp(v, "ns") == 0)
485 				DaemonAddr.sa.sa_family = AF_NS;
486 #endif
487 #ifdef NETX25
488 			else if (strcasecmp(v, "x.25") == 0)
489 				DaemonAddr.sa.sa_family = AF_CCITT;
490 #endif
491 			else
492 				syserr("554 Unknown address family %s in Family=option", v);
493 			break;
494 
495 		  case 'A':		/* address */
496 			switch (DaemonAddr.sa.sa_family)
497 			{
498 #ifdef NETINET
499 			  case AF_INET:
500 				if (isascii(*v) && isdigit(*v))
501 					DaemonAddr.sin.sin_addr.s_addr = htonl(inet_network(v));
502 				else
503 				{
504 					register struct netent *np;
505 
506 					np = getnetbyname(v);
507 					if (np == NULL)
508 						syserr("554 network \"%s\" unknown", v);
509 					else
510 						DaemonAddr.sin.sin_addr.s_addr = np->n_net;
511 				}
512 				break;
513 #endif
514 
515 			  default:
516 				syserr("554 Address= option unsupported for family %d",
517 					DaemonAddr.sa.sa_family);
518 				break;
519 			}
520 			break;
521 
522 		  case 'P':		/* port */
523 			switch (DaemonAddr.sa.sa_family)
524 			{
525 				short port;
526 
527 #ifdef NETINET
528 			  case AF_INET:
529 				if (isascii(*v) && isdigit(*v))
530 					DaemonAddr.sin.sin_port = htons(atoi(v));
531 				else
532 				{
533 					register struct servent *sp;
534 
535 					sp = getservbyname(v, "tcp");
536 					if (sp == NULL)
537 						syserr("554 service \"%s\" unknown", v);
538 					else
539 						DaemonAddr.sin.sin_port = sp->s_port;
540 				}
541 				break;
542 #endif
543 
544 #ifdef NETISO
545 			  case AF_ISO:
546 				/* assume two byte transport selector */
547 				if (isascii(*v) && isdigit(*v))
548 					port = htons(atoi(v));
549 				else
550 				{
551 					register struct servent *sp;
552 
553 					sp = getservbyname(v, "tcp");
554 					if (sp == NULL)
555 						syserr("554 service \"%s\" unknown", v);
556 					else
557 						port = sp->s_port;
558 				}
559 				bcopy((char *) &port, TSEL(&DaemonAddr.siso), 2);
560 				break;
561 #endif
562 
563 			  default:
564 				syserr("554 Port= option unsupported for family %d",
565 					DaemonAddr.sa.sa_family);
566 				break;
567 			}
568 			break;
569 
570 		  case 'L':		/* listen queue size */
571 			ListenQueueSize = atoi(v);
572 			break;
573 
574 		  case 'S':		/* send buffer size */
575 			TcpSndBufferSize = atoi(v);
576 			break;
577 
578 		  case 'R':		/* receive buffer size */
579 			TcpRcvBufferSize = atoi(v);
580 			break;
581 		}
582 	}
583 }
584 /*
585 **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
586 **
587 **	Parameters:
588 **		host -- the name of the host.
589 **		port -- the port number to connect to.
590 **		mci -- a pointer to the mail connection information
591 **			structure to be filled in.
592 **		usesecureport -- if set, use a low numbered (reserved)
593 **			port to provide some rudimentary authentication.
594 **
595 **	Returns:
596 **		An exit code telling whether the connection could be
597 **			made and if not why not.
598 **
599 **	Side Effects:
600 **		none.
601 */
602 
603 SOCKADDR	CurHostAddr;		/* address of current host */
604 
605 int
606 makeconnection(host, port, mci, usesecureport)
607 	char *host;
608 	u_short port;
609 	register MCI *mci;
610 	bool usesecureport;
611 {
612 	register int i, s;
613 	register struct hostent *hp = (struct hostent *)NULL;
614 	SOCKADDR addr;
615 	int sav_errno;
616 	int addrlen;
617 	bool firstconnect;
618 #if NAMED_BIND
619 	extern int h_errno;
620 #endif
621 
622 	/*
623 	**  Set up the address for the mailer.
624 	**	Accept "[a.b.c.d]" syntax for host name.
625 	*/
626 
627 #if NAMED_BIND
628 	h_errno = 0;
629 #endif
630 	errno = 0;
631 	bzero(&CurHostAddr, sizeof CurHostAddr);
632 	SmtpPhase = mci->mci_phase = "initial connection";
633 	CurHostName = host;
634 
635 	if (host[0] == '[')
636 	{
637 		long hid;
638 		register char *p = strchr(host, ']');
639 
640 		if (p != NULL)
641 		{
642 			*p = '\0';
643 #ifdef NETINET
644 			hid = inet_addr(&host[1]);
645 			if (hid == -1)
646 #endif
647 			{
648 				/* try it as a host name (avoid MX lookup) */
649 				hp = gethostbyname(&host[1]);
650 				if (hp == NULL && p[-1] == '.')
651 				{
652 #if NAMED_BIND
653 					int oldopts = _res.options;
654 
655 					_res.options &= ~(RES_DEFNAMES|RES_DNSRCH);
656 #endif
657 					p[-1] = '\0';
658 					hp = gethostbyname(&host[1]);
659 					p[-1] = '.';
660 #if NAMED_BIND
661 					_res.options = oldopts;
662 #endif
663 				}
664 				*p = ']';
665 				goto gothostent;
666 			}
667 			*p = ']';
668 		}
669 		if (p == NULL)
670 		{
671 			usrerr("553 Invalid numeric domain spec \"%s\"", host);
672 			return (EX_NOHOST);
673 		}
674 #ifdef NETINET
675 		addr.sin.sin_family = AF_INET;		/*XXX*/
676 		addr.sin.sin_addr.s_addr = hid;
677 #endif
678 	}
679 	else
680 	{
681 		register char *p = &host[strlen(host) - 1];
682 
683 		hp = gethostbyname(host);
684 		if (hp == NULL && *p == '.')
685 		{
686 #if NAMED_BIND
687 			int oldopts = _res.options;
688 
689 			_res.options &= ~(RES_DEFNAMES|RES_DNSRCH);
690 #endif
691 			*p = '\0';
692 			hp = gethostbyname(host);
693 			*p = '.';
694 #if NAMED_BIND
695 			_res.options = oldopts;
696 #endif
697 		}
698 gothostent:
699 		if (hp == NULL)
700 		{
701 #if NAMED_BIND
702 			/* check for name server timeouts */
703 			if (errno == ETIMEDOUT || h_errno == TRY_AGAIN ||
704 			    (errno == ECONNREFUSED && UseNameServer))
705 			{
706 				mci->mci_status = "466";
707 				return (EX_TEMPFAIL);
708 			}
709 #endif
710 			return (EX_NOHOST);
711 		}
712 		addr.sa.sa_family = hp->h_addrtype;
713 		switch (hp->h_addrtype)
714 		{
715 #ifdef NETINET
716 		  case AF_INET:
717 			bcopy(hp->h_addr,
718 				&addr.sin.sin_addr,
719 				INADDRSZ);
720 			break;
721 #endif
722 
723 		  default:
724 			bcopy(hp->h_addr,
725 				addr.sa.sa_data,
726 				hp->h_length);
727 			break;
728 		}
729 		i = 1;
730 	}
731 
732 	/*
733 	**  Determine the port number.
734 	*/
735 
736 	if (port != 0)
737 		port = htons(port);
738 	else
739 	{
740 		register struct servent *sp = getservbyname("smtp", "tcp");
741 
742 		if (sp == NULL)
743 		{
744 			syserr("554 makeconnection: service \"smtp\" unknown");
745 			port = htons(25);
746 		}
747 		else
748 			port = sp->s_port;
749 	}
750 
751 	switch (addr.sa.sa_family)
752 	{
753 #ifdef NETINET
754 	  case AF_INET:
755 		addr.sin.sin_port = port;
756 		addrlen = sizeof (struct sockaddr_in);
757 		break;
758 #endif
759 
760 #ifdef NETISO
761 	  case AF_ISO:
762 		/* assume two byte transport selector */
763 		bcopy((char *) &port, TSEL((struct sockaddr_iso *) &addr), 2);
764 		addrlen = sizeof (struct sockaddr_iso);
765 		break;
766 #endif
767 
768 	  default:
769 		syserr("Can't connect to address family %d", addr.sa.sa_family);
770 		return (EX_NOHOST);
771 	}
772 
773 	/*
774 	**  Try to actually open the connection.
775 	*/
776 
777 #ifdef XLA
778 	/* if too many connections, don't bother trying */
779 	if (!xla_noqueue_ok(host))
780 		return EX_TEMPFAIL;
781 #endif
782 
783 	firstconnect = TRUE;
784 	for (;;)
785 	{
786 		if (tTd(16, 1))
787 			printf("makeconnection (%s [%s])\n",
788 				host, anynet_ntoa(&addr));
789 
790 		/* save for logging */
791 		CurHostAddr = addr;
792 
793 		if (usesecureport)
794 		{
795 			int rport = IPPORT_RESERVED - 1;
796 
797 			s = rresvport(&rport);
798 		}
799 		else
800 		{
801 			s = socket(AF_INET, SOCK_STREAM, 0);
802 		}
803 		if (s < 0)
804 		{
805 			sav_errno = errno;
806 			syserr("makeconnection: no socket");
807 			goto failure;
808 		}
809 
810 #ifdef SO_SNDBUF
811 		if (TcpSndBufferSize > 0)
812 		{
813 			if (setsockopt(s, SOL_SOCKET, SO_SNDBUF,
814 				       (char *) &TcpSndBufferSize,
815 				       sizeof(TcpSndBufferSize)) < 0)
816 				syserr("makeconnection: setsockopt(SO_SNDBUF)");
817 		}
818 #endif
819 
820 		if (tTd(16, 1))
821 			printf("makeconnection: fd=%d\n", s);
822 
823 		/* turn on network debugging? */
824 		if (tTd(16, 101))
825 		{
826 			int on = 1;
827 			(void) setsockopt(s, SOL_SOCKET, SO_DEBUG,
828 					  (char *)&on, sizeof on);
829 		}
830 		if (CurEnv->e_xfp != NULL)
831 			(void) fflush(CurEnv->e_xfp);		/* for debugging */
832 		errno = 0;					/* for debugging */
833 		if (connect(s, (struct sockaddr *) &addr, addrlen) >= 0)
834 			break;
835 
836 		/* if running demand-dialed connection, try again */
837 		if (DialDelay > 0 && firstconnect)
838 		{
839 			if (tTd(16, 1))
840 				printf("Connect failed (%s); trying again...\n",
841 					errstring(sav_errno));
842 			firstconnect = FALSE;
843 			sleep(DialDelay);
844 			continue;
845 		}
846 
847 		/* couldn't connect.... figure out why */
848 		sav_errno = errno;
849 		(void) close(s);
850 		if (hp && hp->h_addr_list[i])
851 		{
852 			if (tTd(16, 1))
853 				printf("Connect failed (%s); trying new address....\n",
854 					errstring(sav_errno));
855 			switch (addr.sa.sa_family)
856 			{
857 #ifdef NETINET
858 			  case AF_INET:
859 				bcopy(hp->h_addr_list[i++],
860 				      &addr.sin.sin_addr,
861 				      INADDRSZ);
862 				break;
863 #endif
864 
865 			  default:
866 				bcopy(hp->h_addr_list[i++],
867 					addr.sa.sa_data,
868 					hp->h_length);
869 				break;
870 			}
871 			continue;
872 		}
873 
874 		/* failure, decide if temporary or not */
875 	failure:
876 #ifdef XLA
877 		xla_host_end(host);
878 #endif
879 		if (transienterror(sav_errno))
880 			return EX_TEMPFAIL;
881 		else
882 		{
883 			message("%s", errstring(sav_errno));
884 			return (EX_UNAVAILABLE);
885 		}
886 	}
887 
888 	/* connection ok, put it into canonical form */
889 	if ((mci->mci_out = fdopen(s, "w")) == NULL ||
890 	    (s = dup(s)) < 0 ||
891 	    (mci->mci_in = fdopen(s, "r")) == NULL)
892 	{
893 		syserr("cannot open SMTP client channel, fd=%d", s);
894 		return EX_TEMPFAIL;
895 	}
896 
897 	return (EX_OK);
898 }
899 /*
900 **  MYHOSTNAME -- return the name of this host.
901 **
902 **	Parameters:
903 **		hostbuf -- a place to return the name of this host.
904 **		size -- the size of hostbuf.
905 **
906 **	Returns:
907 **		A list of aliases for this host.
908 **
909 **	Side Effects:
910 **		Adds numeric codes to $=w.
911 */
912 
913 struct hostent *
914 myhostname(hostbuf, size)
915 	char hostbuf[];
916 	int size;
917 {
918 	register struct hostent *hp;
919 	extern struct hostent *gethostbyname();
920 	extern bool getcanonname();
921 	extern int h_errno;
922 
923 	if (gethostname(hostbuf, size) < 0)
924 	{
925 		(void) strcpy(hostbuf, "localhost");
926 	}
927 	hp = gethostbyname(hostbuf);
928 	if (hp == NULL)
929 		return NULL;
930 	if (strchr(hp->h_name, '.') != NULL || strchr(hostbuf, '.') == NULL)
931 	{
932 		(void) strncpy(hostbuf, hp->h_name, size - 1);
933 		hostbuf[size - 1] = '\0';
934 	}
935 
936 #if NAMED_BIND
937 	/*
938 	**  If still no dot, try DNS directly (i.e., avoid NIS problems).
939 	**  This ought to be driven from the configuration file, but
940 	**  we are called before the configuration is read.  We could
941 	**  check for an /etc/resolv.conf file, but that isn't required.
942 	**  All in all, a bit of a mess.
943 	*/
944 
945 	if (strchr(hostbuf, '.') == NULL &&
946 	    !getcanonname(hostbuf, size, TRUE) &&
947 	    h_errno == TRY_AGAIN)
948 	{
949 		struct stat stbuf;
950 
951 		/* try twice in case name server not yet started up */
952 		message("My unqualifed host name (%s) unknown to DNS; sleeping for retry",
953 			hostbuf);
954 		sleep(60);
955 		if (!getcanonname(hostbuf, size, TRUE))
956 			errno = h_errno + E_DNSBASE;
957 	}
958 #endif
959 	return (hp);
960 }
961 /*
962 **  GETAUTHINFO -- get the real host name asociated with a file descriptor
963 **
964 **	Uses RFC1413 protocol to try to get info from the other end.
965 **
966 **	Parameters:
967 **		fd -- the descriptor
968 **
969 **	Returns:
970 **		The user@host information associated with this descriptor.
971 */
972 
973 static jmp_buf	CtxAuthTimeout;
974 
975 static
976 authtimeout()
977 {
978 	longjmp(CtxAuthTimeout, 1);
979 }
980 
981 char *
982 getauthinfo(fd)
983 	int fd;
984 {
985 	int falen;
986 	register char *p;
987 	SOCKADDR la;
988 	int lalen;
989 	register struct servent *sp;
990 	int s;
991 	int i;
992 	EVENT *ev;
993 	static char hbuf[MAXNAME * 2 + 2];
994 	extern char *hostnamebyanyaddr();
995 	extern char RealUserName[];			/* main.c */
996 
997 	falen = sizeof RealHostAddr;
998 	if (isatty(fd) || getpeername(fd, &RealHostAddr.sa, &falen) < 0 ||
999 	    falen <= 0 || RealHostAddr.sa.sa_family == 0)
1000 	{
1001 		(void) sprintf(hbuf, "%s@localhost", RealUserName);
1002 		if (tTd(9, 1))
1003 			printf("getauthinfo: %s\n", hbuf);
1004 		return hbuf;
1005 	}
1006 
1007 	if (RealHostName == NULL)
1008 	{
1009 		/* translate that to a host name */
1010 		RealHostName = newstr(hostnamebyanyaddr(&RealHostAddr));
1011 	}
1012 
1013 	if (TimeOuts.to_ident == 0)
1014 		goto noident;
1015 
1016 	lalen = sizeof la;
1017 	if (RealHostAddr.sa.sa_family != AF_INET ||
1018 	    getsockname(fd, &la.sa, &lalen) < 0 || lalen <= 0 ||
1019 	    la.sa.sa_family != AF_INET)
1020 	{
1021 		/* no ident info */
1022 		goto noident;
1023 	}
1024 
1025 	/* create ident query */
1026 	(void) sprintf(hbuf, "%d,%d\r\n",
1027 		ntohs(RealHostAddr.sin.sin_port), ntohs(la.sin.sin_port));
1028 
1029 	/* create local address */
1030 	la.sin.sin_port = 0;
1031 
1032 	/* create foreign address */
1033 	sp = getservbyname("auth", "tcp");
1034 	if (sp != NULL)
1035 		RealHostAddr.sin.sin_port = sp->s_port;
1036 	else
1037 		RealHostAddr.sin.sin_port = htons(113);
1038 
1039 	s = -1;
1040 	if (setjmp(CtxAuthTimeout) != 0)
1041 	{
1042 		if (s >= 0)
1043 			(void) close(s);
1044 		goto noident;
1045 	}
1046 
1047 	/* put a timeout around the whole thing */
1048 	ev = setevent(TimeOuts.to_ident, authtimeout, 0);
1049 
1050 	/* connect to foreign IDENT server using same address as SMTP socket */
1051 	s = socket(AF_INET, SOCK_STREAM, 0);
1052 	if (s < 0)
1053 	{
1054 		clrevent(ev);
1055 		goto noident;
1056 	}
1057 	if (bind(s, &la.sa, sizeof la.sin) < 0 ||
1058 	    connect(s, &RealHostAddr.sa, sizeof RealHostAddr.sin) < 0)
1059 	{
1060 		goto closeident;
1061 	}
1062 
1063 	if (tTd(9, 10))
1064 		printf("getauthinfo: sent %s", hbuf);
1065 
1066 	/* send query */
1067 	if (write(s, hbuf, strlen(hbuf)) < 0)
1068 		goto closeident;
1069 
1070 	/* get result */
1071 	i = read(s, hbuf, sizeof hbuf);
1072 	(void) close(s);
1073 	clrevent(ev);
1074 	if (i <= 0)
1075 		goto noident;
1076 	if (hbuf[--i] == '\n' && hbuf[--i] == '\r')
1077 		i--;
1078 	hbuf[++i] = '\0';
1079 
1080 	if (tTd(9, 3))
1081 		printf("getauthinfo:  got %s\n", hbuf);
1082 
1083 	/* parse result */
1084 	p = strchr(hbuf, ':');
1085 	if (p == NULL)
1086 	{
1087 		/* malformed response */
1088 		goto noident;
1089 	}
1090 	while (isascii(*++p) && isspace(*p))
1091 		continue;
1092 	if (strncasecmp(p, "userid", 6) != 0)
1093 	{
1094 		/* presumably an error string */
1095 		goto noident;
1096 	}
1097 	p += 6;
1098 	while (isascii(*p) && isspace(*p))
1099 		p++;
1100 	if (*p++ != ':')
1101 	{
1102 		/* either useridxx or malformed response */
1103 		goto noident;
1104 	}
1105 
1106 	/* p now points to the OSTYPE field */
1107 	p = strchr(p, ':');
1108 	if (p == NULL)
1109 	{
1110 		/* malformed response */
1111 		goto noident;
1112 	}
1113 
1114 	/* 1413 says don't do this -- but it's broken otherwise */
1115 	while (isascii(*++p) && isspace(*p))
1116 		continue;
1117 
1118 	/* p now points to the authenticated name -- copy carefully */
1119 	for (i = 0; i < MAXNAME && *p != '\0'; p++)
1120 	{
1121 		if (isascii(*p) &&
1122 		    (isalnum(*p) || strchr("!#$%&'*+-./^_`{|}~", *p) != NULL))
1123 			hbuf[i++] = *p;
1124 	}
1125 	hbuf[i++] = '@';
1126 	strcpy(&hbuf[i], RealHostName == NULL ? "localhost" : RealHostName);
1127 	goto finish;
1128 
1129 closeident:
1130 	(void) close(s);
1131 	clrevent(ev);
1132 
1133 noident:
1134 	if (RealHostName == NULL)
1135 	{
1136 		if (tTd(9, 1))
1137 			printf("getauthinfo: NULL\n");
1138 		return NULL;
1139 	}
1140 	(void) strcpy(hbuf, RealHostName);
1141 
1142 finish:
1143 	if (RealHostName != NULL && RealHostName[0] != '[')
1144 	{
1145 		p = &hbuf[strlen(hbuf)];
1146 		(void) sprintf(p, " [%s]", anynet_ntoa(&RealHostAddr));
1147 	}
1148 	if (tTd(9, 1))
1149 		printf("getauthinfo: %s\n", hbuf);
1150 	return hbuf;
1151 }
1152 /*
1153 **  HOST_MAP_LOOKUP -- turn a hostname into canonical form
1154 **
1155 **	Parameters:
1156 **		map -- a pointer to this map (unused).
1157 **		name -- the (presumably unqualified) hostname.
1158 **		av -- unused -- for compatibility with other mapping
1159 **			functions.
1160 **		statp -- an exit status (out parameter) -- set to
1161 **			EX_TEMPFAIL if the name server is unavailable.
1162 **
1163 **	Returns:
1164 **		The mapping, if found.
1165 **		NULL if no mapping found.
1166 **
1167 **	Side Effects:
1168 **		Looks up the host specified in hbuf.  If it is not
1169 **		the canonical name for that host, return the canonical
1170 **		name.
1171 */
1172 
1173 char *
1174 host_map_lookup(map, name, av, statp)
1175 	MAP *map;
1176 	char *name;
1177 	char **av;
1178 	int *statp;
1179 {
1180 	register struct hostent *hp;
1181 	struct in_addr in_addr;
1182 	char *cp;
1183 	int i;
1184 	register STAB *s;
1185 	char hbuf[MAXNAME];
1186 	extern struct hostent *gethostbyaddr();
1187 #if NAMED_BIND
1188 	extern int h_errno;
1189 #endif
1190 
1191 	/*
1192 	**  See if we have already looked up this name.  If so, just
1193 	**  return it.
1194 	*/
1195 
1196 	s = stab(name, ST_NAMECANON, ST_ENTER);
1197 	if (bitset(NCF_VALID, s->s_namecanon.nc_flags))
1198 	{
1199 		if (tTd(9, 1))
1200 			printf("host_map_lookup(%s) => CACHE %s\n",
1201 				name, s->s_namecanon.nc_cname);
1202 		errno = s->s_namecanon.nc_errno;
1203 #if NAMED_BIND
1204 		h_errno = s->s_namecanon.nc_herrno;
1205 #endif
1206 		*statp = s->s_namecanon.nc_stat;
1207 		if (CurEnv->e_message == NULL && *statp == EX_TEMPFAIL)
1208 		{
1209 			sprintf(hbuf, "%s: Name server timeout",
1210 				shortenstring(name, 33));
1211 			CurEnv->e_message = newstr(hbuf);
1212 		}
1213 		return s->s_namecanon.nc_cname;
1214 	}
1215 
1216 	/*
1217 	**  If first character is a bracket, then it is an address
1218 	**  lookup.  Address is copied into a temporary buffer to
1219 	**  strip the brackets and to preserve name if address is
1220 	**  unknown.
1221 	*/
1222 
1223 	if (*name != '[')
1224 	{
1225 		extern bool getcanonname();
1226 
1227 		if (tTd(9, 1))
1228 			printf("host_map_lookup(%s) => ", name);
1229 		s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
1230 		(void) strcpy(hbuf, name);
1231 		if (getcanonname(hbuf, sizeof hbuf - 1, TRUE))
1232 		{
1233 			if (tTd(9, 1))
1234 				printf("%s\n", hbuf);
1235 			cp = map_rewrite(map, hbuf, strlen(hbuf), av);
1236 			s->s_namecanon.nc_cname = newstr(cp);
1237 			return cp;
1238 		}
1239 		else
1240 		{
1241 			register struct hostent *hp;
1242 
1243 			s->s_namecanon.nc_errno = errno;
1244 #if NAMED_BIND
1245 			s->s_namecanon.nc_herrno = h_errno;
1246 			if (tTd(9, 1))
1247 				printf("FAIL (%d)\n", h_errno);
1248 			switch (h_errno)
1249 			{
1250 			  case TRY_AGAIN:
1251 				if (UseNameServer)
1252 				{
1253 					sprintf(hbuf, "%s: Name server timeout",
1254 						shortenstring(name, 33));
1255 					message("%s", hbuf);
1256 					if (CurEnv->e_message == NULL)
1257 						CurEnv->e_message = newstr(hbuf);
1258 				}
1259 				*statp = EX_TEMPFAIL;
1260 				break;
1261 
1262 			  case HOST_NOT_FOUND:
1263 				*statp = EX_NOHOST;
1264 				break;
1265 
1266 			  case NO_RECOVERY:
1267 				*statp = EX_SOFTWARE;
1268 				break;
1269 
1270 			  default:
1271 				*statp = EX_UNAVAILABLE;
1272 				break;
1273 			}
1274 #else
1275 			if (tTd(9, 1))
1276 				printf("FAIL\n");
1277 			*statp = EX_NOHOST;
1278 #endif
1279 			s->s_namecanon.nc_stat = *statp;
1280 			if ((*statp != EX_TEMPFAIL && *statp != EX_NOHOST) ||
1281 			    UseNameServer)
1282 				return NULL;
1283 
1284 			/*
1285 			**  Try to look it up in /etc/hosts
1286 			*/
1287 
1288 			hp = gethostbyname(name);
1289 			if (hp == NULL)
1290 			{
1291 				/* no dice there either */
1292 				s->s_namecanon.nc_stat = *statp = EX_NOHOST;
1293 				return NULL;
1294 			}
1295 
1296 			s->s_namecanon.nc_stat = *statp = EX_OK;
1297 			cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
1298 			s->s_namecanon.nc_cname = newstr(cp);
1299 			return cp;
1300 		}
1301 	}
1302 	if ((cp = strchr(name, ']')) == NULL)
1303 		return (NULL);
1304 	*cp = '\0';
1305 	in_addr.s_addr = inet_addr(&name[1]);
1306 
1307 	/* nope -- ask the name server */
1308 	hp = gethostbyaddr((char *)&in_addr, INADDRSZ, AF_INET);
1309 	s->s_namecanon.nc_errno = errno;
1310 #if NAMED_BIND
1311 	s->s_namecanon.nc_herrno = h_errno;
1312 #endif
1313 	s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
1314 	if (hp == NULL)
1315 	{
1316 		s->s_namecanon.nc_stat = *statp = EX_NOHOST;
1317 		return (NULL);
1318 	}
1319 
1320 	/* found a match -- copy out */
1321 	cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
1322 	s->s_namecanon.nc_stat = *statp = EX_OK;
1323 	s->s_namecanon.nc_cname = newstr(cp);
1324 	return cp;
1325 }
1326 /*
1327 **  ANYNET_NTOA -- convert a network address to printable form.
1328 **
1329 **	Parameters:
1330 **		sap -- a pointer to a sockaddr structure.
1331 **
1332 **	Returns:
1333 **		A printable version of that sockaddr.
1334 */
1335 
1336 char *
1337 anynet_ntoa(sap)
1338 	register SOCKADDR *sap;
1339 {
1340 	register char *bp;
1341 	register char *ap;
1342 	int l;
1343 	static char buf[100];
1344 
1345 	/* check for null/zero family */
1346 	if (sap == NULL)
1347 		return "NULLADDR";
1348 	if (sap->sa.sa_family == 0)
1349 		return "0";
1350 
1351 	switch (sap->sa.sa_family)
1352 	{
1353 #ifdef NETUNIX
1354 	  case AF_UNIX:
1355 	  	if (sap->sunix.sun_path[0] != '\0')
1356 	  		sprintf(buf, "[UNIX: %.64s]", sap->sunix.sun_path);
1357 	  	else
1358 	  		sprintf(buf, "[UNIX: localhost]");
1359 		return buf;
1360 #endif
1361 
1362 #ifdef NETINET
1363 	  case AF_INET:
1364 		return inet_ntoa(((struct sockaddr_in *) sap)->sin_addr);
1365 #endif
1366 
1367 	  default:
1368 	  	/* this case is only to ensure syntactic correctness */
1369 	  	break;
1370 	}
1371 
1372 	/* unknown family -- just dump bytes */
1373 	(void) sprintf(buf, "Family %d: ", sap->sa.sa_family);
1374 	bp = &buf[strlen(buf)];
1375 	ap = sap->sa.sa_data;
1376 	for (l = sizeof sap->sa.sa_data; --l >= 0; )
1377 	{
1378 		(void) sprintf(bp, "%02x:", *ap++ & 0377);
1379 		bp += 3;
1380 	}
1381 	*--bp = '\0';
1382 	return buf;
1383 }
1384 /*
1385 **  HOSTNAMEBYANYADDR -- return name of host based on address
1386 **
1387 **	Parameters:
1388 **		sap -- SOCKADDR pointer
1389 **
1390 **	Returns:
1391 **		text representation of host name.
1392 **
1393 **	Side Effects:
1394 **		none.
1395 */
1396 
1397 char *
1398 hostnamebyanyaddr(sap)
1399 	register SOCKADDR *sap;
1400 {
1401 	register struct hostent *hp;
1402 	int saveretry;
1403 
1404 #if NAMED_BIND
1405 	/* shorten name server timeout to avoid higher level timeouts */
1406 	saveretry = _res.retry;
1407 	_res.retry = 3;
1408 #endif /* NAMED_BIND */
1409 
1410 	switch (sap->sa.sa_family)
1411 	{
1412 #ifdef NETINET
1413 	  case AF_INET:
1414 		hp = gethostbyaddr((char *) &sap->sin.sin_addr,
1415 			INADDRSZ,
1416 			AF_INET);
1417 		break;
1418 #endif
1419 
1420 #ifdef NETISO
1421 	  case AF_ISO:
1422 		hp = gethostbyaddr((char *) &sap->siso.siso_addr,
1423 			sizeof sap->siso.siso_addr,
1424 			AF_ISO);
1425 		break;
1426 #endif
1427 
1428 	  case AF_UNIX:
1429 		hp = NULL;
1430 		break;
1431 
1432 	  default:
1433 		hp = gethostbyaddr(sap->sa.sa_data,
1434 			   sizeof sap->sa.sa_data,
1435 			   sap->sa.sa_family);
1436 		break;
1437 	}
1438 
1439 #if NAMED_BIND
1440 	_res.retry = saveretry;
1441 #endif /* NAMED_BIND */
1442 
1443 	if (hp != NULL)
1444 		return hp->h_name;
1445 	else
1446 	{
1447 		/* produce a dotted quad */
1448 		static char buf[512];
1449 
1450 		(void) sprintf(buf, "[%s]", anynet_ntoa(sap));
1451 		return buf;
1452 	}
1453 }
1454 
1455 # else /* DAEMON */
1456 /* code for systems without sophisticated networking */
1457 
1458 /*
1459 **  MYHOSTNAME -- stub version for case of no daemon code.
1460 **
1461 **	Can't convert to upper case here because might be a UUCP name.
1462 **
1463 **	Mark, you can change this to be anything you want......
1464 */
1465 
1466 char **
1467 myhostname(hostbuf, size)
1468 	char hostbuf[];
1469 	int size;
1470 {
1471 	register FILE *f;
1472 
1473 	hostbuf[0] = '\0';
1474 	f = fopen("/usr/include/whoami", "r");
1475 	if (f != NULL)
1476 	{
1477 		(void) fgets(hostbuf, size, f);
1478 		fixcrlf(hostbuf, TRUE);
1479 		(void) fclose(f);
1480 	}
1481 	return (NULL);
1482 }
1483 /*
1484 **  GETAUTHINFO -- get the real host name asociated with a file descriptor
1485 **
1486 **	Parameters:
1487 **		fd -- the descriptor
1488 **
1489 **	Returns:
1490 **		The host name associated with this descriptor, if it can
1491 **			be determined.
1492 **		NULL otherwise.
1493 **
1494 **	Side Effects:
1495 **		none
1496 */
1497 
1498 char *
1499 getauthinfo(fd)
1500 	int fd;
1501 {
1502 	return NULL;
1503 }
1504 /*
1505 **  MAPHOSTNAME -- turn a hostname into canonical form
1506 **
1507 **	Parameters:
1508 **		map -- a pointer to the database map.
1509 **		name -- a buffer containing a hostname.
1510 **		avp -- a pointer to a (cf file defined) argument vector.
1511 **		statp -- an exit status (out parameter).
1512 **
1513 **	Returns:
1514 **		mapped host name
1515 **		FALSE otherwise.
1516 **
1517 **	Side Effects:
1518 **		Looks up the host specified in name.  If it is not
1519 **		the canonical name for that host, replace it with
1520 **		the canonical name.  If the name is unknown, or it
1521 **		is already the canonical name, leave it unchanged.
1522 */
1523 
1524 /*ARGSUSED*/
1525 char *
1526 host_map_lookup(map, name, avp, statp)
1527 	MAP *map;
1528 	char *name;
1529 	char **avp;
1530 	char *statp;
1531 {
1532 	register struct hostent *hp;
1533 
1534 	hp = gethostbyname(name);
1535 	if (hp != NULL)
1536 		return hp->h_name;
1537 	*statp = EX_NOHOST;
1538 	return NULL;
1539 }
1540 
1541 #endif /* DAEMON */
1542