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