xref: /csrg-svn/usr.sbin/sendmail/src/daemon.c (revision 69839)
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.98 (Berkeley) 06/10/95 (with daemon mode)";
15 #else
16 static char sccsid[] = "@(#)daemon.c	8.98 (Berkeley) 06/10/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 #ifdef 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 #ifdef 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 #ifdef 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 # 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 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 #ifdef NETINET
480 			else if (strcasecmp(v, "inet") == 0)
481 				DaemonAddr.sa.sa_family = AF_INET;
482 #endif
483 #ifdef NETISO
484 			else if (strcasecmp(v, "iso") == 0)
485 				DaemonAddr.sa.sa_family = AF_ISO;
486 #endif
487 #ifdef NETNS
488 			else if (strcasecmp(v, "ns") == 0)
489 				DaemonAddr.sa.sa_family = AF_NS;
490 #endif
491 #ifdef 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 #ifdef 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 #ifdef 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 #ifdef 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 #ifdef 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 #ifdef 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 #ifdef 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 		port = htons(port);
747 	else
748 	{
749 		register struct servent *sp = getservbyname("smtp", "tcp");
750 
751 		if (sp == NULL)
752 		{
753 #ifdef LOG
754 			if (LogLevel > 2)
755 				syslog(LOG_ERR, "makeconnection: service \"smtp\" unknown");
756 #endif
757 			port = htons(25);
758 		}
759 		else
760 			port = sp->s_port;
761 	}
762 
763 	switch (addr.sa.sa_family)
764 	{
765 #ifdef NETINET
766 	  case AF_INET:
767 		addr.sin.sin_port = port;
768 		addrlen = sizeof (struct sockaddr_in);
769 		break;
770 #endif
771 
772 #ifdef NETISO
773 	  case AF_ISO:
774 		/* assume two byte transport selector */
775 		bcopy((char *) &port, TSEL((struct sockaddr_iso *) &addr), 2);
776 		addrlen = sizeof (struct sockaddr_iso);
777 		break;
778 #endif
779 
780 	  default:
781 		syserr("Can't connect to address family %d", addr.sa.sa_family);
782 		return (EX_NOHOST);
783 	}
784 
785 	/*
786 	**  Try to actually open the connection.
787 	*/
788 
789 #ifdef XLA
790 	/* if too many connections, don't bother trying */
791 	if (!xla_noqueue_ok(host))
792 		return EX_TEMPFAIL;
793 #endif
794 
795 	firstconnect = TRUE;
796 	for (;;)
797 	{
798 		if (tTd(16, 1))
799 			printf("makeconnection (%s [%s])\n",
800 				host, anynet_ntoa(&addr));
801 
802 		/* save for logging */
803 		CurHostAddr = addr;
804 
805 		if (usesecureport)
806 		{
807 			int rport = IPPORT_RESERVED - 1;
808 
809 			s = rresvport(&rport);
810 		}
811 		else
812 		{
813 			s = socket(AF_INET, SOCK_STREAM, 0);
814 		}
815 		if (s < 0)
816 		{
817 			sav_errno = errno;
818 			syserr("makeconnection: no socket");
819 			goto failure;
820 		}
821 
822 #ifdef SO_SNDBUF
823 		if (TcpSndBufferSize > 0)
824 		{
825 			if (setsockopt(s, SOL_SOCKET, SO_SNDBUF,
826 				       (char *) &TcpSndBufferSize,
827 				       sizeof(TcpSndBufferSize)) < 0)
828 				syserr("makeconnection: setsockopt(SO_SNDBUF)");
829 		}
830 #endif
831 
832 		if (tTd(16, 1))
833 			printf("makeconnection: fd=%d\n", s);
834 
835 		/* turn on network debugging? */
836 		if (tTd(16, 101))
837 		{
838 			int on = 1;
839 			(void) setsockopt(s, SOL_SOCKET, SO_DEBUG,
840 					  (char *)&on, sizeof on);
841 		}
842 		if (CurEnv->e_xfp != NULL)
843 			(void) fflush(CurEnv->e_xfp);		/* for debugging */
844 		errno = 0;					/* for debugging */
845 		if (connect(s, (struct sockaddr *) &addr, addrlen) >= 0)
846 			break;
847 
848 		/* if running demand-dialed connection, try again */
849 		if (DialDelay > 0 && firstconnect)
850 		{
851 			if (tTd(16, 1))
852 				printf("Connect failed (%s); trying again...\n",
853 					errstring(sav_errno));
854 			firstconnect = FALSE;
855 			sleep(DialDelay);
856 			continue;
857 		}
858 
859 		/* couldn't connect.... figure out why */
860 		sav_errno = errno;
861 		(void) close(s);
862 		if (hp != NULL && hp->h_addr_list[i])
863 		{
864 			if (tTd(16, 1))
865 				printf("Connect failed (%s); trying new address....\n",
866 					errstring(sav_errno));
867 			switch (addr.sa.sa_family)
868 			{
869 #ifdef NETINET
870 			  case AF_INET:
871 				bcopy(hp->h_addr_list[i++],
872 				      &addr.sin.sin_addr,
873 				      INADDRSZ);
874 				break;
875 #endif
876 
877 			  default:
878 				bcopy(hp->h_addr_list[i++],
879 					addr.sa.sa_data,
880 					hp->h_length);
881 				break;
882 			}
883 			continue;
884 		}
885 
886 		/* failure, decide if temporary or not */
887 	failure:
888 #ifdef XLA
889 		xla_host_end(host);
890 #endif
891 		if (transienterror(sav_errno))
892 			return EX_TEMPFAIL;
893 		else
894 		{
895 			message("%s", errstring(sav_errno));
896 			return (EX_UNAVAILABLE);
897 		}
898 	}
899 
900 	/* connection ok, put it into canonical form */
901 	if ((mci->mci_out = fdopen(s, "w")) == NULL ||
902 	    (s = dup(s)) < 0 ||
903 	    (mci->mci_in = fdopen(s, "r")) == NULL)
904 	{
905 		syserr("cannot open SMTP client channel, fd=%d", s);
906 		return EX_TEMPFAIL;
907 	}
908 
909 	return (EX_OK);
910 }
911 /*
912 **  MYHOSTNAME -- return the name of this host.
913 **
914 **	Parameters:
915 **		hostbuf -- a place to return the name of this host.
916 **		size -- the size of hostbuf.
917 **
918 **	Returns:
919 **		A list of aliases for this host.
920 **
921 **	Side Effects:
922 **		Adds numeric codes to $=w.
923 */
924 
925 struct hostent *
926 myhostname(hostbuf, size)
927 	char hostbuf[];
928 	int size;
929 {
930 	register struct hostent *hp;
931 	extern bool getcanonname();
932 	extern int h_errno;
933 
934 	if (gethostname(hostbuf, size) < 0)
935 	{
936 		(void) strcpy(hostbuf, "localhost");
937 	}
938 	hp = sm_gethostbyname(hostbuf);
939 	if (hp == NULL)
940 		return NULL;
941 	if (strchr(hp->h_name, '.') != NULL || strchr(hostbuf, '.') == NULL)
942 	{
943 		(void) strncpy(hostbuf, hp->h_name, size - 1);
944 		hostbuf[size - 1] = '\0';
945 	}
946 
947 #if NAMED_BIND
948 	/*
949 	**  If still no dot, try DNS directly (i.e., avoid NIS problems).
950 	**  This ought to be driven from the configuration file, but
951 	**  we are called before the configuration is read.  We could
952 	**  check for an /etc/resolv.conf file, but that isn't required.
953 	**  All in all, a bit of a mess.
954 	*/
955 
956 	if (strchr(hostbuf, '.') == NULL)
957 	{
958 		int nmaps;
959 		int i;
960 		char *maptype[MAXMAPSTACK];
961 		short mapreturn[MAXMAPACTIONS];
962 
963 		nmaps = switch_map_find("hosts", maptype, mapreturn);
964 		for (i = 0; i < nmaps; i++)
965 			if (strcmp(maptype[i], "dns") == 0)
966 				break;
967 		if (i < nmaps &&
968 		    !dns_getcanonname(hostbuf, size, TRUE) &&
969 		    h_errno == TRY_AGAIN)
970 		{
971 			/* try twice in case name server not yet started up */
972 			message("My unqualifed host name (%s) unknown to DNS; sleeping for retry",
973 				hostbuf);
974 			sleep(60);
975 			if (!dns_getcanonname(hostbuf, size, TRUE))
976 				errno = h_errno + E_DNSBASE;
977 		}
978 	}
979 #endif
980 	return (hp);
981 }
982 /*
983 **  GETAUTHINFO -- get the real host name asociated with a file descriptor
984 **
985 **	Uses RFC1413 protocol to try to get info from the other end.
986 **
987 **	Parameters:
988 **		fd -- the descriptor
989 **
990 **	Returns:
991 **		The user@host information associated with this descriptor.
992 */
993 
994 static jmp_buf	CtxAuthTimeout;
995 
996 static void
997 authtimeout()
998 {
999 	longjmp(CtxAuthTimeout, 1);
1000 }
1001 
1002 char *
1003 getauthinfo(fd)
1004 	int fd;
1005 {
1006 	int falen;
1007 	register char *p;
1008 	SOCKADDR la;
1009 	int lalen;
1010 	register struct servent *sp;
1011 	int s;
1012 	int i;
1013 	EVENT *ev;
1014 	int nleft;
1015 	char ibuf[MAXNAME + 1];
1016 	static char hbuf[MAXNAME * 2 + 2];
1017 	extern char *hostnamebyanyaddr();
1018 
1019 	falen = sizeof RealHostAddr;
1020 	if (isatty(fd) || getpeername(fd, &RealHostAddr.sa, &falen) < 0 ||
1021 	    falen <= 0 || RealHostAddr.sa.sa_family == 0)
1022 	{
1023 		(void) sprintf(hbuf, "%s@localhost", RealUserName);
1024 		if (tTd(9, 1))
1025 			printf("getauthinfo: %s\n", hbuf);
1026 		return hbuf;
1027 	}
1028 
1029 	if (RealHostName == NULL)
1030 	{
1031 		/* translate that to a host name */
1032 		RealHostName = newstr(hostnamebyanyaddr(&RealHostAddr));
1033 	}
1034 
1035 	if (TimeOuts.to_ident == 0)
1036 		goto noident;
1037 
1038 	lalen = sizeof la;
1039 	if (RealHostAddr.sa.sa_family != AF_INET ||
1040 	    getsockname(fd, &la.sa, &lalen) < 0 || lalen <= 0 ||
1041 	    la.sa.sa_family != AF_INET)
1042 	{
1043 		/* no ident info */
1044 		goto noident;
1045 	}
1046 
1047 	/* create ident query */
1048 	(void) sprintf(ibuf, "%d,%d\r\n",
1049 		ntohs(RealHostAddr.sin.sin_port), ntohs(la.sin.sin_port));
1050 
1051 	/* create local address */
1052 	la.sin.sin_port = 0;
1053 
1054 	/* create foreign address */
1055 	sp = getservbyname("auth", "tcp");
1056 	if (sp != NULL)
1057 		RealHostAddr.sin.sin_port = sp->s_port;
1058 	else
1059 		RealHostAddr.sin.sin_port = htons(113);
1060 
1061 	s = -1;
1062 	if (setjmp(CtxAuthTimeout) != 0)
1063 	{
1064 		if (s >= 0)
1065 			(void) close(s);
1066 		goto noident;
1067 	}
1068 
1069 	/* put a timeout around the whole thing */
1070 	ev = setevent(TimeOuts.to_ident, authtimeout, 0);
1071 
1072 	/* connect to foreign IDENT server using same address as SMTP socket */
1073 	s = socket(AF_INET, SOCK_STREAM, 0);
1074 	if (s < 0)
1075 	{
1076 		clrevent(ev);
1077 		goto noident;
1078 	}
1079 	if (bind(s, &la.sa, sizeof la.sin) < 0 ||
1080 	    connect(s, &RealHostAddr.sa, sizeof RealHostAddr.sin) < 0)
1081 	{
1082 		goto closeident;
1083 	}
1084 
1085 	if (tTd(9, 10))
1086 		printf("getauthinfo: sent %s", ibuf);
1087 
1088 	/* send query */
1089 	if (write(s, ibuf, strlen(ibuf)) < 0)
1090 		goto closeident;
1091 
1092 	/* get result */
1093 	p = &ibuf[0];
1094 	nleft = sizeof ibuf - 1;
1095 	while ((i = read(s, p, nleft)) > 0)
1096 	{
1097 		p += i;
1098 		nleft -= i;
1099 	}
1100 	(void) close(s);
1101 	clrevent(ev);
1102 	if (i < 0 || p == &ibuf[0])
1103 		goto noident;
1104 
1105 	if (*--p == '\n' && *--p == '\r')
1106 		p--;
1107 	*++p = '\0';
1108 
1109 	if (tTd(9, 3))
1110 		printf("getauthinfo:  got %s\n", ibuf);
1111 
1112 	/* parse result */
1113 	p = strchr(ibuf, ':');
1114 	if (p == NULL)
1115 	{
1116 		/* malformed response */
1117 		goto noident;
1118 	}
1119 	while (isascii(*++p) && isspace(*p))
1120 		continue;
1121 	if (strncasecmp(p, "userid", 6) != 0)
1122 	{
1123 		/* presumably an error string */
1124 		goto noident;
1125 	}
1126 	p += 6;
1127 	while (isascii(*p) && isspace(*p))
1128 		p++;
1129 	if (*p++ != ':')
1130 	{
1131 		/* either useridxx or malformed response */
1132 		goto noident;
1133 	}
1134 
1135 	/* p now points to the OSTYPE field */
1136 	while (isascii(*p) && isspace(*p))
1137 		p++;
1138 	if (strncasecmp(p, "other", 5) == 0 &&
1139 	    (p[5] == ':' || p[5] == ' ' || p[5] == ',' || p[5] == '\0'))
1140 	{
1141 		/* not useful information */
1142 		goto noident;
1143 	}
1144 	p = strchr(p, ':');
1145 	if (p == NULL)
1146 	{
1147 		/* malformed response */
1148 		goto noident;
1149 	}
1150 
1151 	/* 1413 says don't do this -- but it's broken otherwise */
1152 	while (isascii(*++p) && isspace(*p))
1153 		continue;
1154 
1155 	/* p now points to the authenticated name -- copy carefully */
1156 	cleanstrcpy(hbuf, p, MAXNAME);
1157 	i = strlen(hbuf);
1158 	hbuf[i++] = '@';
1159 	strcpy(&hbuf[i], RealHostName == NULL ? "localhost" : RealHostName);
1160 	goto postident;
1161 
1162 closeident:
1163 	(void) close(s);
1164 	clrevent(ev);
1165 
1166 noident:
1167 	if (RealHostName == NULL)
1168 	{
1169 		if (tTd(9, 1))
1170 			printf("getauthinfo: NULL\n");
1171 		return NULL;
1172 	}
1173 	(void) strcpy(hbuf, RealHostName);
1174 
1175 postident:
1176 #if IP_SRCROUTE
1177 	/*
1178 	**  Extract IP source routing information.
1179 	**
1180 	**	Format of output for a connection from site a through b
1181 	**	through c to d:
1182 	**		loose:      @site-c@site-b:site-a
1183 	**		strict:	   !@site-c@site-b:site-a
1184 	**
1185 	**	o - pointer within ipopt_list structure.
1186 	**	q - pointer within ls/ss rr route data
1187 	**	p - pointer to hbuf
1188 	*/
1189 
1190 	if (RealHostAddr.sa.sa_family == AF_INET)
1191 	{
1192 		int ipoptlen, j;
1193 		u_char *q;
1194 		u_char *o;
1195 		struct in_addr addr;
1196 		struct ipoption ipopt;
1197 
1198 		ipoptlen = sizeof ipopt;
1199 		if (getsockopt(fd, IPPROTO_IP, IP_OPTIONS,
1200 			       (char *) &ipopt, &ipoptlen) < 0)
1201 			goto noipsr;
1202 		if (ipoptlen == 0)
1203 			goto noipsr;
1204 		o = (u_char *) ipopt.ipopt_list;
1205 		while (o != NULL && o < (u_char *) &ipopt + ipoptlen)
1206 		{
1207 			switch (*o)
1208 			{
1209 			  case IPOPT_EOL:
1210 				o = NULL;
1211 				break;
1212 
1213 			  case IPOPT_NOP:
1214 				o++;
1215 				break;
1216 
1217 			  case IPOPT_SSRR:
1218 			  case IPOPT_LSRR:
1219 				p = &hbuf[strlen(hbuf)];
1220 				sprintf(p, " [%s@%s",
1221 				    *o == IPOPT_SSRR ? "!" : "",
1222 				    inet_ntoa(ipopt.ipopt_dst));
1223 				p += strlen(p);
1224 
1225 				/* o[1] is option length */
1226 				j = *++o / sizeof(struct in_addr) - 1;
1227 
1228 				/* q skips length and router pointer to data */
1229 				q = o + 2;
1230 				for ( ; j >= 0; j--)
1231 				{
1232 					memcpy(&addr, q, sizeof(addr));
1233 					sprintf(p, "%c%s",
1234 						     j ? '@' : ':',
1235 						     inet_ntoa(addr));
1236 					p += strlen(p);
1237 					q += sizeof(struct in_addr);
1238 				}
1239 				o += *o;
1240 				break;
1241 
1242 			  default:
1243 				/* Skip over option */
1244 				o += o[1];
1245 				break;
1246 			}
1247 		}
1248 		strcat(hbuf,"]");
1249 		goto postipsr;
1250 	}
1251 #endif
1252 
1253 noipsr:
1254 	if (RealHostName != NULL && RealHostName[0] != '[')
1255 	{
1256 		p = &hbuf[strlen(hbuf)];
1257 		(void) sprintf(p, " [%s]", anynet_ntoa(&RealHostAddr));
1258 	}
1259 
1260 postipsr:
1261 	if (tTd(9, 1))
1262 		printf("getauthinfo: %s\n", hbuf);
1263 	return hbuf;
1264 }
1265 /*
1266 **  HOST_MAP_LOOKUP -- turn a hostname into canonical form
1267 **
1268 **	Parameters:
1269 **		map -- a pointer to this map (unused).
1270 **		name -- the (presumably unqualified) hostname.
1271 **		av -- unused -- for compatibility with other mapping
1272 **			functions.
1273 **		statp -- an exit status (out parameter) -- set to
1274 **			EX_TEMPFAIL if the name server is unavailable.
1275 **
1276 **	Returns:
1277 **		The mapping, if found.
1278 **		NULL if no mapping found.
1279 **
1280 **	Side Effects:
1281 **		Looks up the host specified in hbuf.  If it is not
1282 **		the canonical name for that host, return the canonical
1283 **		name.
1284 */
1285 
1286 char *
1287 host_map_lookup(map, name, av, statp)
1288 	MAP *map;
1289 	char *name;
1290 	char **av;
1291 	int *statp;
1292 {
1293 	register struct hostent *hp;
1294 	struct in_addr in_addr;
1295 	char *cp;
1296 	register STAB *s;
1297 	char hbuf[MAXNAME + 1];
1298 #if NAMED_BIND
1299 	extern int h_errno;
1300 #endif
1301 
1302 	/*
1303 	**  See if we have already looked up this name.  If so, just
1304 	**  return it.
1305 	*/
1306 
1307 	s = stab(name, ST_NAMECANON, ST_ENTER);
1308 	if (bitset(NCF_VALID, s->s_namecanon.nc_flags))
1309 	{
1310 		if (tTd(9, 1))
1311 			printf("host_map_lookup(%s) => CACHE %s\n",
1312 			       name,
1313 			       s->s_namecanon.nc_cname == NULL
1314 					? "NULL"
1315 					: s->s_namecanon.nc_cname);
1316 		errno = s->s_namecanon.nc_errno;
1317 #if NAMED_BIND
1318 		h_errno = s->s_namecanon.nc_herrno;
1319 #endif
1320 		*statp = s->s_namecanon.nc_stat;
1321 		if (*statp == EX_TEMPFAIL)
1322 		{
1323 			CurEnv->e_status = "4.4.3";
1324 			usrerr("451 %s: Name server timeout",
1325 				shortenstring(name, 33));
1326 		}
1327 		return s->s_namecanon.nc_cname;
1328 	}
1329 
1330 	/*
1331 	**  If first character is a bracket, then it is an address
1332 	**  lookup.  Address is copied into a temporary buffer to
1333 	**  strip the brackets and to preserve name if address is
1334 	**  unknown.
1335 	*/
1336 
1337 	if (*name != '[')
1338 	{
1339 		extern bool getcanonname();
1340 
1341 		if (tTd(9, 1))
1342 			printf("host_map_lookup(%s) => ", name);
1343 		s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
1344 		if (strlen(name) < sizeof hbuf)
1345 			(void) strcpy(hbuf, name);
1346 		else
1347 		{
1348 			bcopy(name, hbuf, sizeof hbuf - 1);
1349 			hbuf[sizeof hbuf - 1] = '\0';
1350 		}
1351 		if (getcanonname(hbuf, sizeof hbuf - 1, !NoMXforCanon))
1352 		{
1353 			if (tTd(9, 1))
1354 				printf("%s\n", hbuf);
1355 			cp = map_rewrite(map, hbuf, strlen(hbuf), av);
1356 			s->s_namecanon.nc_cname = newstr(cp);
1357 			return cp;
1358 		}
1359 		else
1360 		{
1361 			register struct hostent *hp;
1362 
1363 			s->s_namecanon.nc_errno = errno;
1364 #if NAMED_BIND
1365 			s->s_namecanon.nc_herrno = h_errno;
1366 			if (tTd(9, 1))
1367 				printf("FAIL (%d)\n", h_errno);
1368 			switch (h_errno)
1369 			{
1370 			  case TRY_AGAIN:
1371 				if (UseNameServer)
1372 				{
1373 					CurEnv->e_status = "4.4.3";
1374 					usrerr("451 %s: Name server timeout",
1375 						shortenstring(name, 33));
1376 				}
1377 				*statp = EX_TEMPFAIL;
1378 				break;
1379 
1380 			  case HOST_NOT_FOUND:
1381 			  case NO_DATA:
1382 				*statp = EX_NOHOST;
1383 				break;
1384 
1385 			  case NO_RECOVERY:
1386 				*statp = EX_SOFTWARE;
1387 				break;
1388 
1389 			  default:
1390 				*statp = EX_UNAVAILABLE;
1391 				break;
1392 			}
1393 #else
1394 			if (tTd(9, 1))
1395 				printf("FAIL\n");
1396 			*statp = EX_NOHOST;
1397 #endif
1398 			s->s_namecanon.nc_stat = *statp;
1399 			if ((*statp != EX_TEMPFAIL && *statp != EX_NOHOST) ||
1400 			    UseNameServer)
1401 				return NULL;
1402 
1403 			/*
1404 			**  Try to look it up in /etc/hosts
1405 			*/
1406 
1407 			hp = sm_gethostbyname(name);
1408 			if (hp == NULL)
1409 			{
1410 				/* no dice there either */
1411 				s->s_namecanon.nc_stat = *statp = EX_NOHOST;
1412 				return NULL;
1413 			}
1414 
1415 			s->s_namecanon.nc_stat = *statp = EX_OK;
1416 			cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
1417 			s->s_namecanon.nc_cname = newstr(cp);
1418 			return cp;
1419 		}
1420 	}
1421 	if ((cp = strchr(name, ']')) == NULL)
1422 		return (NULL);
1423 	*cp = '\0';
1424 	in_addr.s_addr = inet_addr(&name[1]);
1425 
1426 	/* nope -- ask the name server */
1427 	hp = sm_gethostbyaddr((char *)&in_addr, INADDRSZ, AF_INET);
1428 	s->s_namecanon.nc_errno = errno;
1429 #if NAMED_BIND
1430 	s->s_namecanon.nc_herrno = h_errno;
1431 #endif
1432 	s->s_namecanon.nc_flags |= NCF_VALID;		/* will be soon */
1433 	if (hp == NULL)
1434 	{
1435 		s->s_namecanon.nc_stat = *statp = EX_NOHOST;
1436 		return (NULL);
1437 	}
1438 
1439 	/* found a match -- copy out */
1440 	cp = map_rewrite(map, hp->h_name, strlen(hp->h_name), av);
1441 	s->s_namecanon.nc_stat = *statp = EX_OK;
1442 	s->s_namecanon.nc_cname = newstr(cp);
1443 	return cp;
1444 }
1445 /*
1446 **  ANYNET_NTOA -- convert a network address to printable form.
1447 **
1448 **	Parameters:
1449 **		sap -- a pointer to a sockaddr structure.
1450 **
1451 **	Returns:
1452 **		A printable version of that sockaddr.
1453 */
1454 
1455 #ifdef NETLINK
1456 # include <net/if_dl.h>
1457 #endif
1458 
1459 char *
1460 anynet_ntoa(sap)
1461 	register SOCKADDR *sap;
1462 {
1463 	register char *bp;
1464 	register char *ap;
1465 	int l;
1466 	static char buf[100];
1467 
1468 	/* check for null/zero family */
1469 	if (sap == NULL)
1470 		return "NULLADDR";
1471 	if (sap->sa.sa_family == 0)
1472 		return "0";
1473 
1474 	switch (sap->sa.sa_family)
1475 	{
1476 #ifdef NETUNIX
1477 	  case AF_UNIX:
1478 	  	if (sap->sunix.sun_path[0] != '\0')
1479 	  		sprintf(buf, "[UNIX: %.64s]", sap->sunix.sun_path);
1480 	  	else
1481 	  		sprintf(buf, "[UNIX: localhost]");
1482 		return buf;
1483 #endif
1484 
1485 #ifdef NETINET
1486 	  case AF_INET:
1487 		return inet_ntoa(sap->sin.sin_addr);
1488 #endif
1489 
1490 #ifdef NETLINK
1491 	  case AF_LINK:
1492 		sprintf(buf, "[LINK: %s]",
1493 			link_ntoa((struct sockaddr_dl *) &sap->sa));
1494 		return buf;
1495 #endif
1496 	  default:
1497 		/* this case is needed when nothing is #defined */
1498 		/* in order to keep the switch syntactically correct */
1499 		break;
1500 	}
1501 
1502 	/* unknown family -- just dump bytes */
1503 	(void) sprintf(buf, "Family %d: ", sap->sa.sa_family);
1504 	bp = &buf[strlen(buf)];
1505 	ap = sap->sa.sa_data;
1506 	for (l = sizeof sap->sa.sa_data; --l >= 0; )
1507 	{
1508 		(void) sprintf(bp, "%02x:", *ap++ & 0377);
1509 		bp += 3;
1510 	}
1511 	*--bp = '\0';
1512 	return buf;
1513 }
1514 /*
1515 **  HOSTNAMEBYANYADDR -- return name of host based on address
1516 **
1517 **	Parameters:
1518 **		sap -- SOCKADDR pointer
1519 **
1520 **	Returns:
1521 **		text representation of host name.
1522 **
1523 **	Side Effects:
1524 **		none.
1525 */
1526 
1527 char *
1528 hostnamebyanyaddr(sap)
1529 	register SOCKADDR *sap;
1530 {
1531 	register struct hostent *hp;
1532 	int saveretry;
1533 
1534 #if NAMED_BIND
1535 	/* shorten name server timeout to avoid higher level timeouts */
1536 	saveretry = _res.retry;
1537 	_res.retry = 3;
1538 #endif /* NAMED_BIND */
1539 
1540 	switch (sap->sa.sa_family)
1541 	{
1542 #ifdef NETINET
1543 	  case AF_INET:
1544 		hp = sm_gethostbyaddr((char *) &sap->sin.sin_addr,
1545 			INADDRSZ,
1546 			AF_INET);
1547 		break;
1548 #endif
1549 
1550 #ifdef NETISO
1551 	  case AF_ISO:
1552 		hp = sm_gethostbyaddr((char *) &sap->siso.siso_addr,
1553 			sizeof sap->siso.siso_addr,
1554 			AF_ISO);
1555 		break;
1556 #endif
1557 
1558 	  case AF_UNIX:
1559 		hp = NULL;
1560 		break;
1561 
1562 	  default:
1563 		hp = sm_gethostbyaddr(sap->sa.sa_data,
1564 			   sizeof sap->sa.sa_data,
1565 			   sap->sa.sa_family);
1566 		break;
1567 	}
1568 
1569 #if NAMED_BIND
1570 	_res.retry = saveretry;
1571 #endif /* NAMED_BIND */
1572 
1573 	if (hp != NULL)
1574 		return hp->h_name;
1575 	else
1576 	{
1577 		/* produce a dotted quad */
1578 		static char buf[512];
1579 
1580 		(void) sprintf(buf, "[%s]", anynet_ntoa(sap));
1581 		return buf;
1582 	}
1583 }
1584 
1585 # else /* DAEMON */
1586 /* code for systems without sophisticated networking */
1587 
1588 /*
1589 **  MYHOSTNAME -- stub version for case of no daemon code.
1590 **
1591 **	Can't convert to upper case here because might be a UUCP name.
1592 **
1593 **	Mark, you can change this to be anything you want......
1594 */
1595 
1596 char **
1597 myhostname(hostbuf, size)
1598 	char hostbuf[];
1599 	int size;
1600 {
1601 	register FILE *f;
1602 
1603 	hostbuf[0] = '\0';
1604 	f = fopen("/usr/include/whoami", "r");
1605 	if (f != NULL)
1606 	{
1607 		(void) fgets(hostbuf, size, f);
1608 		fixcrlf(hostbuf, TRUE);
1609 		(void) fclose(f);
1610 	}
1611 	return (NULL);
1612 }
1613 /*
1614 **  GETAUTHINFO -- get the real host name asociated with a file descriptor
1615 **
1616 **	Parameters:
1617 **		fd -- the descriptor
1618 **
1619 **	Returns:
1620 **		The host name associated with this descriptor, if it can
1621 **			be determined.
1622 **		NULL otherwise.
1623 **
1624 **	Side Effects:
1625 **		none
1626 */
1627 
1628 char *
1629 getauthinfo(fd)
1630 	int fd;
1631 {
1632 	return NULL;
1633 }
1634 /*
1635 **  MAPHOSTNAME -- turn a hostname into canonical form
1636 **
1637 **	Parameters:
1638 **		map -- a pointer to the database map.
1639 **		name -- a buffer containing a hostname.
1640 **		avp -- a pointer to a (cf file defined) argument vector.
1641 **		statp -- an exit status (out parameter).
1642 **
1643 **	Returns:
1644 **		mapped host name
1645 **		FALSE otherwise.
1646 **
1647 **	Side Effects:
1648 **		Looks up the host specified in name.  If it is not
1649 **		the canonical name for that host, replace it with
1650 **		the canonical name.  If the name is unknown, or it
1651 **		is already the canonical name, leave it unchanged.
1652 */
1653 
1654 /*ARGSUSED*/
1655 char *
1656 host_map_lookup(map, name, avp, statp)
1657 	MAP *map;
1658 	char *name;
1659 	char **avp;
1660 	char *statp;
1661 {
1662 	register struct hostent *hp;
1663 
1664 	hp = sm_gethostbyname(name);
1665 	if (hp != NULL)
1666 		return hp->h_name;
1667 	*statp = EX_NOHOST;
1668 	return NULL;
1669 }
1670 
1671 #endif /* DAEMON */
1672