xref: /csrg-svn/usr.sbin/sendmail/src/daemon.c (revision 56795)
122700Sdist /*
234920Sbostic  * Copyright (c) 1983 Eric P. Allman
333780Sbostic  * Copyright (c) 1988 Regents of the University of California.
433780Sbostic  * All rights reserved.
533780Sbostic  *
642825Sbostic  * %sccs.include.redist.c%
733780Sbostic  */
822700Sdist 
933932Sbostic #include <errno.h>
1040962Sbostic #include "sendmail.h"
114535Seric 
1233780Sbostic #ifndef lint
1333780Sbostic #ifdef DAEMON
14*56795Seric static char sccsid[] = "@(#)daemon.c	5.51 (Berkeley) 11/14/92 (with daemon mode)";
1533780Sbostic #else
16*56795Seric static char sccsid[] = "@(#)daemon.c	5.51 (Berkeley) 11/14/92 (without daemon mode)";
1733780Sbostic #endif
1833780Sbostic #endif /* not lint */
194535Seric 
2033780Sbostic #ifdef DAEMON
2133780Sbostic 
2223120Seric # include <netdb.h>
2324945Seric # include <sys/signal.h>
2423120Seric # include <sys/wait.h>
2523120Seric # include <sys/time.h>
2623120Seric # include <sys/resource.h>
275978Seric 
284535Seric /*
294535Seric **  DAEMON.C -- routines to use when running as a daemon.
307556Seric **
317556Seric **	This entire file is highly dependent on the 4.2 BSD
327556Seric **	interprocess communication primitives.  No attempt has
337556Seric **	been made to make this file portable to Version 7,
347556Seric **	Version 6, MPX files, etc.  If you should try such a
357556Seric **	thing yourself, I recommend chucking the entire file
367556Seric **	and starting from scratch.  Basic semantics are:
377556Seric **
387556Seric **	getrequests()
397556Seric **		Opens a port and initiates a connection.
407556Seric **		Returns in a child.  Must set InChannel and
417556Seric **		OutChannel appropriately.
4210206Seric **	clrdaemon()
4310206Seric **		Close any open files associated with getting
4410206Seric **		the connection; this is used when running the queue,
4510206Seric **		etc., to avoid having extra file descriptors during
4610206Seric **		the queue run and to avoid confusing the network
4710206Seric **		code (if it cares).
4852106Seric **	makeconnection(host, port, outfile, infile, usesecureport)
497556Seric **		Make a connection to the named host on the given
507556Seric **		port.  Set *outfile and *infile to the files
517556Seric **		appropriate for communication.  Returns zero on
527556Seric **		success, else an exit status describing the
537556Seric **		error.
5455019Seric **	maphostname(hbuf, hbufsize, avp)
5525699Seric **		Convert the entry in hbuf into a canonical form.  It
5625699Seric **		may not be larger than hbufsize.
574535Seric */
584535Seric /*
594535Seric **  GETREQUESTS -- open mail IPC port and get requests.
604535Seric **
614535Seric **	Parameters:
624535Seric **		none.
634535Seric **
644535Seric **	Returns:
654535Seric **		none.
664535Seric **
674535Seric **	Side Effects:
684535Seric **		Waits until some interesting activity occurs.  When
694535Seric **		it does, a child is created to process it, and the
704535Seric **		parent waits for completion.  Return from this
719886Seric **		routine is always in the child.  The file pointers
729886Seric **		"InChannel" and "OutChannel" should be set to point
739886Seric **		to the communication channel.
744535Seric */
754535Seric 
7616144Seric int	DaemonSocket	= -1;		/* fd describing socket */
7716144Seric 
784535Seric getrequests()
794535Seric {
809610Seric 	int t;
819610Seric 	register struct servent *sp;
8225027Seric 	int on = 1;
8353751Seric 	bool refusingconnections = TRUE;
8452106Seric 	struct sockaddr_in srvraddr;
8546928Sbostic 	extern void reapchild();
867117Seric 
879610Seric 	/*
889610Seric 	**  Set up the address for the mailer.
899610Seric 	*/
909610Seric 
919610Seric 	sp = getservbyname("smtp", "tcp");
929610Seric 	if (sp == NULL)
939610Seric 	{
949610Seric 		syserr("server \"smtp\" unknown");
9510167Seric 		goto severe;
969610Seric 	}
9752106Seric 	srvraddr.sin_family = AF_INET;
9852106Seric 	srvraddr.sin_addr.s_addr = INADDR_ANY;
9952106Seric 	srvraddr.sin_port = sp->s_port;
1009610Seric 
1019610Seric 	/*
1029610Seric 	**  Try to actually open the connection.
1039610Seric 	*/
1049610Seric 
1059610Seric 	if (tTd(15, 1))
10652106Seric 		printf("getrequests: port 0x%x\n", srvraddr.sin_port);
1079610Seric 
1089610Seric 	/* get a socket for the SMTP connection */
10923120Seric 	DaemonSocket = socket(AF_INET, SOCK_STREAM, 0);
11010206Seric 	if (DaemonSocket < 0)
1119610Seric 	{
1129610Seric 		/* probably another daemon already */
1139610Seric 		syserr("getrequests: can't create socket");
1149610Seric 	  severe:
1159610Seric # ifdef LOG
1169610Seric 		if (LogLevel > 0)
11724858Seric 			syslog(LOG_ALERT, "cannot get connection");
118*56795Seric # endif /* LOG */
1199610Seric 		finis();
1209610Seric 	}
12110347Seric 
12210347Seric 	/* turn on network debugging? */
12356328Seric 	if (tTd(15, 101))
12424945Seric 		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on);
12510347Seric 
12625027Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on);
12725027Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on);
12825027Seric 
12952106Seric 	if (bind(DaemonSocket, (struct sockaddr *)&srvraddr, sizeof srvraddr) < 0)
1309610Seric 	{
1319610Seric 		syserr("getrequests: cannot bind");
13210206Seric 		(void) close(DaemonSocket);
1339610Seric 		goto severe;
1349610Seric 	}
1359610Seric 
13624955Seric 	(void) signal(SIGCHLD, reapchild);
13724945Seric 
1389610Seric 	if (tTd(15, 1))
13910206Seric 		printf("getrequests: %d\n", DaemonSocket);
1409610Seric 
1414631Seric 	for (;;)
1424631Seric 	{
14314875Seric 		register int pid;
14411147Seric 		auto int lotherend;
14553751Seric 		extern bool refuseconnections();
14611147Seric 
14714875Seric 		/* see if we are rejecting connections */
14853751Seric 		CurrentLA = getla();
14953751Seric 		if (refuseconnections())
15036584Sbostic 		{
15153751Seric 			if (!refusingconnections)
15253751Seric 			{
15353751Seric 				/* don't queue so peer will fail quickly */
15453751Seric 				(void) listen(DaemonSocket, 0);
15553751Seric 				refusingconnections = TRUE;
15653751Seric 			}
15753751Seric 			setproctitle("rejecting connections: load average: %.2f",
15853751Seric 				(double)CurrentLA);
15914875Seric 			sleep(5);
16053751Seric 			continue;
16136584Sbostic 		}
16214875Seric 
16353751Seric 		if (refusingconnections)
16453751Seric 		{
16553751Seric 			/* start listening again */
16653751Seric 			if (listen(DaemonSocket, 10) < 0)
16753751Seric 			{
16853751Seric 				syserr("getrequests: cannot listen");
16953751Seric 				(void) close(DaemonSocket);
17053751Seric 				goto severe;
17153751Seric 			}
17253751Seric 			setproctitle("accepting connections");
17353751Seric 			refusingconnections = FALSE;
17453751Seric 		}
17553751Seric 
1769610Seric 		/* wait for a connection */
1779610Seric 		do
1789610Seric 		{
1799610Seric 			errno = 0;
18036230Skarels 			lotherend = sizeof RealHostAddr;
18146928Sbostic 			t = accept(DaemonSocket,
18246928Sbostic 			    (struct sockaddr *)&RealHostAddr, &lotherend);
1839610Seric 		} while (t < 0 && errno == EINTR);
1849610Seric 		if (t < 0)
1855978Seric 		{
1869610Seric 			syserr("getrequests: accept");
1879610Seric 			sleep(5);
1889610Seric 			continue;
1895978Seric 		}
1904631Seric 
1915978Seric 		/*
1925978Seric 		**  Create a subprocess to process the mail.
1935978Seric 		*/
1945978Seric 
1957677Seric 		if (tTd(15, 2))
1969610Seric 			printf("getrequests: forking (fd = %d)\n", t);
1975978Seric 
1984636Seric 		pid = fork();
1994636Seric 		if (pid < 0)
2004631Seric 		{
2014636Seric 			syserr("daemon: cannot fork");
2024636Seric 			sleep(10);
2039610Seric 			(void) close(t);
2044636Seric 			continue;
2054631Seric 		}
2064631Seric 
2074636Seric 		if (pid == 0)
2084631Seric 		{
20911147Seric 			extern struct hostent *gethostbyaddr();
21011147Seric 			register struct hostent *hp;
21111147Seric 			char buf[MAXNAME];
21211147Seric 
2134636Seric 			/*
2144636Seric 			**  CHILD -- return to caller.
21511147Seric 			**	Collect verified idea of sending host.
2164636Seric 			**	Verify calling user id if possible here.
2174636Seric 			*/
2184631Seric 
21924955Seric 			(void) signal(SIGCHLD, SIG_DFL);
22024950Seric 
22111147Seric 			/* determine host name */
22236230Skarels 			hp = gethostbyaddr((char *) &RealHostAddr.sin_addr, sizeof RealHostAddr.sin_addr, AF_INET);
22311147Seric 			if (hp != NULL)
22423104Seric 				(void) strcpy(buf, hp->h_name);
22511147Seric 			else
22616884Seric 			{
22716884Seric 				extern char *inet_ntoa();
22816884Seric 
22916884Seric 				/* produce a dotted quad */
23016884Seric 				(void) sprintf(buf, "[%s]",
23136230Skarels 					inet_ntoa(RealHostAddr.sin_addr));
23216884Seric 			}
23316884Seric 
23455173Seric #ifdef LOG
23555173Seric 			if (LogLevel > 9)
23655173Seric 			{
23755173Seric 				/* log connection information */
23855173Seric 				syslog(LOG_INFO, "connect from %s (%s)",
23955173Seric 					buf, inet_ntoa(RealHostAddr.sin_addr));
24055173Seric 			}
24155173Seric #endif
24255173Seric 
24316884Seric 			/* should we check for illegal connection here? XXX */
24416884Seric 
24511147Seric 			RealHostName = newstr(buf);
24611147Seric 
24710206Seric 			(void) close(DaemonSocket);
2489610Seric 			InChannel = fdopen(t, "r");
24921062Seric 			OutChannel = fdopen(dup(t), "w");
2507677Seric 			if (tTd(15, 2))
2515978Seric 				printf("getreq: returning\n");
2527876Seric # ifdef LOG
2537876Seric 			if (LogLevel > 11)
2547876Seric 				syslog(LOG_DEBUG, "connected, pid=%d", getpid());
255*56795Seric # endif /* LOG */
2564636Seric 			return;
2574631Seric 		}
2584631Seric 
2597117Seric 		/* close the port so that others will hang (for a while) */
2609610Seric 		(void) close(t);
2614631Seric 	}
2629886Seric 	/*NOTREACHED*/
2634631Seric }
2645978Seric /*
26510206Seric **  CLRDAEMON -- reset the daemon connection
26610206Seric **
26710206Seric **	Parameters:
26810206Seric **		none.
26910206Seric **
27010206Seric **	Returns:
27110206Seric **		none.
27210206Seric **
27310206Seric **	Side Effects:
27410206Seric **		releases any resources used by the passive daemon.
27510206Seric */
27610206Seric 
27710206Seric clrdaemon()
27810206Seric {
27910206Seric 	if (DaemonSocket >= 0)
28010206Seric 		(void) close(DaemonSocket);
28110206Seric 	DaemonSocket = -1;
28210206Seric }
28310206Seric /*
2846039Seric **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
2856039Seric **
2866039Seric **	Parameters:
2876039Seric **		host -- the name of the host.
2886633Seric **		port -- the port number to connect to.
28953739Seric **		mci -- a pointer to the mail connection information
29053739Seric **			structure to be filled in.
29152106Seric **		usesecureport -- if set, use a low numbered (reserved)
29252106Seric **			port to provide some rudimentary authentication.
2936039Seric **
2946039Seric **	Returns:
2956039Seric **		An exit code telling whether the connection could be
2966039Seric **			made and if not why not.
2976039Seric **
2986039Seric **	Side Effects:
2996039Seric **		none.
3006039Seric */
3015978Seric 
30254967Seric int
30353739Seric makeconnection(host, port, mci, usesecureport)
3046039Seric 	char *host;
3057286Seric 	u_short port;
30654967Seric 	register MCI *mci;
30752106Seric 	bool usesecureport;
3086039Seric {
30929430Sbloom 	register int i, s;
31029430Sbloom 	register struct hostent *hp = (struct hostent *)NULL;
31152106Seric 	struct sockaddr_in addr;
31252106Seric 	int sav_errno;
31329430Sbloom 	extern char *inet_ntoa();
31435651Seric #ifdef NAMED_BIND
31535651Seric 	extern int h_errno;
31635651Seric #endif
3176039Seric 
3186039Seric 	/*
3196039Seric 	**  Set up the address for the mailer.
3209308Seric 	**	Accept "[a.b.c.d]" syntax for host name.
3216039Seric 	*/
3226039Seric 
32335651Seric #ifdef NAMED_BIND
32425475Smiriam 	h_errno = 0;
32535651Seric #endif
32625475Smiriam 	errno = 0;
32725475Smiriam 
3289308Seric 	if (host[0] == '[')
3299308Seric 	{
33011147Seric 		long hid;
331*56795Seric 		register char *p = strchr(host, ']');
3329308Seric 
33311147Seric 		if (p != NULL)
3349308Seric 		{
33511147Seric 			*p = '\0';
33611147Seric 			hid = inet_addr(&host[1]);
33711147Seric 			*p = ']';
3389308Seric 		}
33911147Seric 		if (p == NULL || hid == -1)
3409308Seric 		{
3419308Seric 			usrerr("Invalid numeric domain spec \"%s\"", host);
3429308Seric 			return (EX_NOHOST);
3439308Seric 		}
34452106Seric 		addr.sin_addr.s_addr = hid;
3459308Seric 	}
3469610Seric 	else
3479610Seric 	{
34829430Sbloom 		hp = gethostbyname(host);
34925475Smiriam 		if (hp == NULL)
35024945Seric 		{
35135651Seric #ifdef NAMED_BIND
35225475Smiriam 			if (errno == ETIMEDOUT || h_errno == TRY_AGAIN)
35325475Smiriam 				return (EX_TEMPFAIL);
35425657Seric 
35535651Seric 			/* if name server is specified, assume temp fail */
35635651Seric 			if (errno == ECONNREFUSED && UseNameServer)
35735651Seric 				return (EX_TEMPFAIL);
35835651Seric #endif
35935651Seric 
36025657Seric 			/*
36125657Seric 			**  XXX Should look for mail forwarder record here
36225657Seric 			**  XXX if (h_errno == NO_ADDRESS).
36325657Seric 			*/
36425657Seric 
36525475Smiriam 			return (EX_NOHOST);
36624945Seric 		}
36752106Seric 		bcopy(hp->h_addr, (char *) &addr.sin_addr, hp->h_length);
36829430Sbloom 		i = 1;
3699610Seric 	}
3709610Seric 
3719610Seric 	/*
3729610Seric 	**  Determine the port number.
3739610Seric 	*/
3749610Seric 
37510011Seric 	if (port != 0)
37652106Seric 		addr.sin_port = htons(port);
37710011Seric 	else
3789610Seric 	{
3799610Seric 		register struct servent *sp = getservbyname("smtp", "tcp");
3809610Seric 
3819610Seric 		if (sp == NULL)
3829610Seric 		{
3839610Seric 			syserr("makeconnection: server \"smtp\" unknown");
3849610Seric 			return (EX_OSFILE);
3859610Seric 		}
38652106Seric 		addr.sin_port = sp->s_port;
3879610Seric 	}
3886039Seric 
3896039Seric 	/*
3906039Seric 	**  Try to actually open the connection.
3916039Seric 	*/
3926039Seric 
39329430Sbloom again:
3947677Seric 	if (tTd(16, 1))
39529430Sbloom 		printf("makeconnection (%s [%s])\n", host,
39652106Seric 		    inet_ntoa(addr.sin_addr));
3976039Seric 
39852106Seric 	if (usesecureport)
39952106Seric 	{
40052106Seric 		int rport = IPPORT_RESERVED - 1;
40152106Seric 
40252106Seric 		s = rresvport(&rport);
40352106Seric 	}
40452106Seric 	else
40552106Seric 	{
40652106Seric 		s = socket(AF_INET, SOCK_STREAM, 0);
40752106Seric 	}
4086039Seric 	if (s < 0)
4096039Seric 	{
41052106Seric 		sav_errno = errno;
4116039Seric 		syserr("makeconnection: no socket");
4126039Seric 		goto failure;
4136039Seric 	}
4146039Seric 
4157677Seric 	if (tTd(16, 1))
41654967Seric 		printf("makeconnection: fd=%d\n", s);
41710347Seric 
41810347Seric 	/* turn on network debugging? */
41956328Seric 	if (tTd(16, 101))
42024945Seric 	{
42124945Seric 		int on = 1;
42224945Seric 		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on);
42324945Seric 	}
42440932Srick 	if (CurEnv->e_xfp != NULL)
42540932Srick 		(void) fflush(CurEnv->e_xfp);		/* for debugging */
42614383Seric 	errno = 0;					/* for debugging */
42752106Seric 	addr.sin_family = AF_INET;
42852106Seric 	if (connect(s, (struct sockaddr *) &addr, sizeof addr) < 0)
4296039Seric 	{
43027744Sbloom 		sav_errno = errno;
43127744Sbloom 		(void) close(s);
43229430Sbloom 		if (hp && hp->h_addr_list[i])
43329430Sbloom 		{
43452106Seric 			bcopy(hp->h_addr_list[i++], (char *) &addr.sin_addr,
43552106Seric 					hp->h_length);
43629430Sbloom 			goto again;
43729430Sbloom 		}
43829430Sbloom 
4396039Seric 		/* failure, decide if temporary or not */
4406039Seric 	failure:
44127744Sbloom 		switch (sav_errno)
4426039Seric 		{
4436039Seric 		  case EISCONN:
4446039Seric 		  case ETIMEDOUT:
4456897Seric 		  case EINPROGRESS:
4466897Seric 		  case EALREADY:
4476897Seric 		  case EADDRINUSE:
44810123Seric 		  case EHOSTDOWN:
4496897Seric 		  case ENETDOWN:
4506897Seric 		  case ENETRESET:
4516897Seric 		  case ENOBUFS:
4527204Seric 		  case ECONNREFUSED:
45311546Seric 		  case ECONNRESET:
45410081Seric 		  case EHOSTUNREACH:
45510098Seric 		  case ENETUNREACH:
45651995Seric #ifdef ENOSR
45751995Seric 		  case ENOSR:
45851995Seric #endif
4596039Seric 			/* there are others, I'm sure..... */
4606039Seric 			return (EX_TEMPFAIL);
4616039Seric 
46211147Seric 		  case EPERM:
46311147Seric 			/* why is this happening? */
46411147Seric 			syserr("makeconnection: funny failure, addr=%lx, port=%x",
46552106Seric 				addr.sin_addr.s_addr, addr.sin_port);
46614383Seric 			return (EX_TEMPFAIL);
46711147Seric 
4686039Seric 		  default:
46940932Srick 			{
47040932Srick 				extern char *errstring();
47140932Srick 
47240932Srick 				message(Arpa_Info, "%s", errstring(sav_errno));
47340932Srick 				return (EX_UNAVAILABLE);
47440932Srick 			}
4756039Seric 		}
4766039Seric 	}
4776039Seric 
4786039Seric 	/* connection ok, put it into canonical form */
47953739Seric 	mci->mci_out = fdopen(s, "w");
48053739Seric 	mci->mci_in = fdopen(dup(s), "r");
4816039Seric 
48210098Seric 	return (EX_OK);
4836039Seric }
48410758Seric /*
48510758Seric **  MYHOSTNAME -- return the name of this host.
48610758Seric **
48710758Seric **	Parameters:
48810758Seric **		hostbuf -- a place to return the name of this host.
48912313Seric **		size -- the size of hostbuf.
49010758Seric **
49110758Seric **	Returns:
49210758Seric **		A list of aliases for this host.
49310758Seric **
49410758Seric **	Side Effects:
49510758Seric **		none.
49610758Seric */
4976039Seric 
49810758Seric char **
49912313Seric myhostname(hostbuf, size)
50010758Seric 	char hostbuf[];
50112313Seric 	int size;
50210758Seric {
50310758Seric 	extern struct hostent *gethostbyname();
50411147Seric 	struct hostent *hp;
50510758Seric 
50623120Seric 	if (gethostname(hostbuf, size) < 0)
50723120Seric 	{
50823120Seric 		(void) strcpy(hostbuf, "localhost");
50923120Seric 	}
51011147Seric 	hp = gethostbyname(hostbuf);
51111147Seric 	if (hp != NULL)
51216877Seric 	{
51323104Seric 		(void) strcpy(hostbuf, hp->h_name);
51411147Seric 		return (hp->h_aliases);
51516877Seric 	}
51610758Seric 	else
51710758Seric 		return (NULL);
51810758Seric }
51951315Seric /*
52053751Seric **  MAPHOSTNAME -- turn a hostname into canonical form
52153751Seric **
52253751Seric **	Parameters:
52353751Seric **		hbuf -- a buffer containing a hostname.
52453751Seric **		hbsize -- the size of hbuf.
52555019Seric **		avp -- unused -- for compatibility with other mapping
52655019Seric **			functions.
52753751Seric **
52853751Seric **	Returns:
52953751Seric **		The mapping, if found.
53053751Seric **		NULL if no mapping found.
53153751Seric **
53253751Seric **	Side Effects:
53353751Seric **		Looks up the host specified in hbuf.  If it is not
53453751Seric **		the canonical name for that host, return the canonical
53553751Seric **		name.
53653751Seric */
53751315Seric 
53853751Seric char *
53953751Seric maphostname(hbuf, hbsize, avp)
54016911Seric 	char *hbuf;
54116911Seric 	int hbsize;
54253751Seric 	char **avp;
54316911Seric {
54416911Seric 	register struct hostent *hp;
54533932Sbostic 	u_long in_addr;
54640994Sbostic 	char ptr[256], *cp;
54733932Sbostic 	struct hostent *gethostbyaddr();
54816911Seric 
54953751Seric 	/* allow room for trailing dot on correct match */
55053751Seric 	if (ConfigLevel >= 2)
55153751Seric 		hbsize--;
55253751Seric 
55325574Smiriam 	/*
55433932Sbostic 	 * If first character is a bracket, then it is an address
55533932Sbostic 	 * lookup.  Address is copied into a temporary buffer to
55633932Sbostic 	 * strip the brackets and to preserve hbuf if address is
55733932Sbostic 	 * unknown.
55833932Sbostic 	 */
55953751Seric 
56051315Seric 	if (*hbuf != '[')
56153751Seric 	{
56255019Seric 		extern bool getcanonname();
56355019Seric 
56453751Seric 		if (getcanonname(hbuf, hbsize))
56553751Seric 		{
56653751Seric 			/* found a match -- add the trailing dot */
56753751Seric 			if (ConfigLevel >= 2)
56856678Seric 			{
56956678Seric 				int i = strlen(hbuf) - 1;
57056678Seric 
57156678Seric 				if (hbuf[i] != '.')
57256678Seric 					(void) strcpy(&hbuf[++i], ".");
57356678Seric 			}
57453751Seric 			return hbuf;
57553751Seric 		}
57653751Seric 		else
57753751Seric 			return NULL;
57853751Seric 	}
579*56795Seric 	if ((cp = strchr(strcpy(ptr, hbuf), ']')) == NULL)
58053751Seric 		return (NULL);
58140994Sbostic 	*cp = '\0';
58233932Sbostic 	in_addr = inet_addr(&ptr[1]);
58333932Sbostic 	hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET);
58433932Sbostic 	if (hp == NULL)
58553751Seric 		return (NULL);
58653751Seric 
58753751Seric 	/* found a match -- copy and dot terminate */
58833932Sbostic 	if (strlen(hp->h_name) >= hbsize)
58933932Sbostic 		hp->h_name[hbsize - 1] = '\0';
59053751Seric 	(void) strcpy(hbuf, hp->h_name);
59153751Seric 	if (ConfigLevel >= 2)
59253751Seric 		(void) strcat(hbuf, ".");
59353751Seric 	return hbuf;
59433932Sbostic }
59516911Seric 
596*56795Seric # else /* DAEMON */
59716911Seric /* code for systems without sophisticated networking */
59810758Seric 
59910758Seric /*
60010758Seric **  MYHOSTNAME -- stub version for case of no daemon code.
60111297Seric **
60211297Seric **	Can't convert to upper case here because might be a UUCP name.
60312313Seric **
60412313Seric **	Mark, you can change this to be anything you want......
60510758Seric */
60610758Seric 
60710758Seric char **
60812313Seric myhostname(hostbuf, size)
60910758Seric 	char hostbuf[];
61012313Seric 	int size;
61110758Seric {
61210758Seric 	register FILE *f;
61310758Seric 
61410758Seric 	hostbuf[0] = '\0';
61510758Seric 	f = fopen("/usr/include/whoami", "r");
61610758Seric 	if (f != NULL)
61710758Seric 	{
61812313Seric 		(void) fgets(hostbuf, size, f);
61910758Seric 		fixcrlf(hostbuf, TRUE);
62010758Seric 		(void) fclose(f);
62110758Seric 	}
62210758Seric 	return (NULL);
62310758Seric }
62416911Seric /*
62516911Seric **  MAPHOSTNAME -- turn a hostname into canonical form
62616911Seric **
62716911Seric **	Parameters:
62816911Seric **		hbuf -- a buffer containing a hostname.
62916911Seric **		hbsize -- the size of hbuf.
63053751Seric **		avp -- a pointer to a (cf file defined) argument vector.
63116911Seric **
63216911Seric **	Returns:
63353751Seric **		mapped host name
63451315Seric **		FALSE otherwise.
63516911Seric **
63616911Seric **	Side Effects:
63716911Seric **		Looks up the host specified in hbuf.  If it is not
63816911Seric **		the canonical name for that host, replace it with
63916911Seric **		the canonical name.  If the name is unknown, or it
64016911Seric **		is already the canonical name, leave it unchanged.
64116911Seric */
64210758Seric 
64316911Seric /*ARGSUSED*/
64453751Seric char *
64553751Seric maphostname(hbuf, hbsize, avp)
64616911Seric 	char *hbuf;
64716911Seric 	int hbsize;
64853751Seric 	char **avp;
64916911Seric {
65053751Seric 	return NULL;
65116911Seric }
65216911Seric 
653*56795Seric #endif /* DAEMON */
654