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