xref: /csrg-svn/usr.sbin/sendmail/src/daemon.c (revision 58110)
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*58110Seric static char sccsid[] = "@(#)daemon.c	6.7 (Berkeley) 02/21/93 (with daemon mode)";
1533780Sbostic #else
16*58110Seric static char sccsid[] = "@(#)daemon.c	6.7 (Berkeley) 02/21/93 (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.
5456823Seric **	maphostname(map, hbuf, hbufsiz, avp)
5556823Seric **		Convert the entry in hbuf into a canonical form.
564535Seric */
574535Seric /*
584535Seric **  GETREQUESTS -- open mail IPC port and get requests.
594535Seric **
604535Seric **	Parameters:
614535Seric **		none.
624535Seric **
634535Seric **	Returns:
644535Seric **		none.
654535Seric **
664535Seric **	Side Effects:
674535Seric **		Waits until some interesting activity occurs.  When
684535Seric **		it does, a child is created to process it, and the
694535Seric **		parent waits for completion.  Return from this
709886Seric **		routine is always in the child.  The file pointers
719886Seric **		"InChannel" and "OutChannel" should be set to point
729886Seric **		to the communication channel.
734535Seric */
744535Seric 
7516144Seric int	DaemonSocket	= -1;		/* fd describing socket */
7616144Seric 
774535Seric getrequests()
784535Seric {
799610Seric 	int t;
809610Seric 	register struct servent *sp;
8125027Seric 	int on = 1;
8253751Seric 	bool refusingconnections = TRUE;
8352106Seric 	struct sockaddr_in srvraddr;
8446928Sbostic 	extern void reapchild();
857117Seric 
869610Seric 	/*
879610Seric 	**  Set up the address for the mailer.
889610Seric 	*/
899610Seric 
909610Seric 	sp = getservbyname("smtp", "tcp");
919610Seric 	if (sp == NULL)
929610Seric 	{
939610Seric 		syserr("server \"smtp\" unknown");
9410167Seric 		goto severe;
959610Seric 	}
9652106Seric 	srvraddr.sin_family = AF_INET;
9752106Seric 	srvraddr.sin_addr.s_addr = INADDR_ANY;
9852106Seric 	srvraddr.sin_port = sp->s_port;
999610Seric 
1009610Seric 	/*
1019610Seric 	**  Try to actually open the connection.
1029610Seric 	*/
1039610Seric 
1049610Seric 	if (tTd(15, 1))
10552106Seric 		printf("getrequests: port 0x%x\n", srvraddr.sin_port);
1069610Seric 
1079610Seric 	/* get a socket for the SMTP connection */
10823120Seric 	DaemonSocket = socket(AF_INET, SOCK_STREAM, 0);
10910206Seric 	if (DaemonSocket < 0)
1109610Seric 	{
1119610Seric 		/* probably another daemon already */
1129610Seric 		syserr("getrequests: can't create socket");
1139610Seric 	  severe:
1149610Seric # ifdef LOG
1159610Seric 		if (LogLevel > 0)
11657663Seric 			syslog(LOG_ALERT, "problem creating SMTP socket");
11756795Seric # endif /* LOG */
1189610Seric 		finis();
1199610Seric 	}
12010347Seric 
12110347Seric 	/* turn on network debugging? */
12256328Seric 	if (tTd(15, 101))
12324945Seric 		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on);
12410347Seric 
12525027Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on);
12625027Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on);
12725027Seric 
12852106Seric 	if (bind(DaemonSocket, (struct sockaddr *)&srvraddr, sizeof srvraddr) < 0)
1299610Seric 	{
1309610Seric 		syserr("getrequests: cannot bind");
13110206Seric 		(void) close(DaemonSocket);
1329610Seric 		goto severe;
1339610Seric 	}
1349610Seric 
13524955Seric 	(void) signal(SIGCHLD, reapchild);
13624945Seric 
1379610Seric 	if (tTd(15, 1))
13810206Seric 		printf("getrequests: %d\n", DaemonSocket);
1399610Seric 
1404631Seric 	for (;;)
1414631Seric 	{
14214875Seric 		register int pid;
14311147Seric 		auto int lotherend;
14453751Seric 		extern bool refuseconnections();
14511147Seric 
14614875Seric 		/* see if we are rejecting connections */
14753751Seric 		CurrentLA = getla();
14853751Seric 		if (refuseconnections())
14936584Sbostic 		{
15053751Seric 			if (!refusingconnections)
15153751Seric 			{
15253751Seric 				/* don't queue so peer will fail quickly */
15353751Seric 				(void) listen(DaemonSocket, 0);
15453751Seric 				refusingconnections = TRUE;
15553751Seric 			}
15657385Seric 			setproctitle("rejecting connections: load average: %d",
15757385Seric 				CurrentLA);
15814875Seric 			sleep(5);
15953751Seric 			continue;
16036584Sbostic 		}
16114875Seric 
16253751Seric 		if (refusingconnections)
16353751Seric 		{
16453751Seric 			/* start listening again */
16553751Seric 			if (listen(DaemonSocket, 10) < 0)
16653751Seric 			{
16753751Seric 				syserr("getrequests: cannot listen");
16853751Seric 				(void) close(DaemonSocket);
16953751Seric 				goto severe;
17053751Seric 			}
17153751Seric 			setproctitle("accepting connections");
17253751Seric 			refusingconnections = FALSE;
17353751Seric 		}
17453751Seric 
1759610Seric 		/* wait for a connection */
1769610Seric 		do
1779610Seric 		{
1789610Seric 			errno = 0;
17936230Skarels 			lotherend = sizeof RealHostAddr;
18046928Sbostic 			t = accept(DaemonSocket,
18146928Sbostic 			    (struct sockaddr *)&RealHostAddr, &lotherend);
1829610Seric 		} while (t < 0 && errno == EINTR);
1839610Seric 		if (t < 0)
1845978Seric 		{
1859610Seric 			syserr("getrequests: accept");
1869610Seric 			sleep(5);
1879610Seric 			continue;
1885978Seric 		}
1894631Seric 
1905978Seric 		/*
1915978Seric 		**  Create a subprocess to process the mail.
1925978Seric 		*/
1935978Seric 
1947677Seric 		if (tTd(15, 2))
1959610Seric 			printf("getrequests: forking (fd = %d)\n", t);
1965978Seric 
1974636Seric 		pid = fork();
1984636Seric 		if (pid < 0)
1994631Seric 		{
2004636Seric 			syserr("daemon: cannot fork");
2014636Seric 			sleep(10);
2029610Seric 			(void) close(t);
2034636Seric 			continue;
2044631Seric 		}
2054631Seric 
2064636Seric 		if (pid == 0)
2074631Seric 		{
20811147Seric 			extern struct hostent *gethostbyaddr();
20911147Seric 			register struct hostent *hp;
21011147Seric 			char buf[MAXNAME];
21157135Seric 			extern char *inet_ntoa();
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 				/* produce a dotted quad */
22816884Seric 				(void) sprintf(buf, "[%s]",
22936230Skarels 					inet_ntoa(RealHostAddr.sin_addr));
23016884Seric 			}
23116884Seric 
23255173Seric #ifdef LOG
23357977Seric 			if (LogLevel > 10)
23455173Seric 			{
23555173Seric 				/* log connection information */
23655173Seric 				syslog(LOG_INFO, "connect from %s (%s)",
23755173Seric 					buf, inet_ntoa(RealHostAddr.sin_addr));
23855173Seric 			}
23955173Seric #endif
24055173Seric 
24116884Seric 			/* should we check for illegal connection here? XXX */
24216884Seric 
24311147Seric 			RealHostName = newstr(buf);
24411147Seric 
24510206Seric 			(void) close(DaemonSocket);
2469610Seric 			InChannel = fdopen(t, "r");
24721062Seric 			OutChannel = fdopen(dup(t), "w");
2487677Seric 			if (tTd(15, 2))
2495978Seric 				printf("getreq: returning\n");
2507876Seric # ifdef LOG
2517876Seric 			if (LogLevel > 11)
2527876Seric 				syslog(LOG_DEBUG, "connected, pid=%d", getpid());
25356795Seric # endif /* LOG */
2544636Seric 			return;
2554631Seric 		}
2564631Seric 
2577117Seric 		/* close the port so that others will hang (for a while) */
2589610Seric 		(void) close(t);
2594631Seric 	}
2609886Seric 	/*NOTREACHED*/
2614631Seric }
2625978Seric /*
26310206Seric **  CLRDAEMON -- reset the daemon connection
26410206Seric **
26510206Seric **	Parameters:
26610206Seric **		none.
26710206Seric **
26810206Seric **	Returns:
26910206Seric **		none.
27010206Seric **
27110206Seric **	Side Effects:
27210206Seric **		releases any resources used by the passive daemon.
27310206Seric */
27410206Seric 
27510206Seric clrdaemon()
27610206Seric {
27710206Seric 	if (DaemonSocket >= 0)
27810206Seric 		(void) close(DaemonSocket);
27910206Seric 	DaemonSocket = -1;
28010206Seric }
28110206Seric /*
2826039Seric **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
2836039Seric **
2846039Seric **	Parameters:
2856039Seric **		host -- the name of the host.
2866633Seric **		port -- the port number to connect to.
28753739Seric **		mci -- a pointer to the mail connection information
28853739Seric **			structure to be filled in.
28952106Seric **		usesecureport -- if set, use a low numbered (reserved)
29052106Seric **			port to provide some rudimentary authentication.
2916039Seric **
2926039Seric **	Returns:
2936039Seric **		An exit code telling whether the connection could be
2946039Seric **			made and if not why not.
2956039Seric **
2966039Seric **	Side Effects:
2976039Seric **		none.
2986039Seric */
2995978Seric 
30054967Seric int
30153739Seric makeconnection(host, port, mci, usesecureport)
3026039Seric 	char *host;
3037286Seric 	u_short port;
30454967Seric 	register MCI *mci;
30552106Seric 	bool usesecureport;
3066039Seric {
30729430Sbloom 	register int i, s;
30829430Sbloom 	register struct hostent *hp = (struct hostent *)NULL;
30952106Seric 	struct sockaddr_in addr;
31052106Seric 	int sav_errno;
31129430Sbloom 	extern char *inet_ntoa();
31235651Seric #ifdef NAMED_BIND
31335651Seric 	extern int h_errno;
31435651Seric #endif
3156039Seric 
3166039Seric 	/*
3176039Seric 	**  Set up the address for the mailer.
3189308Seric 	**	Accept "[a.b.c.d]" syntax for host name.
3196039Seric 	*/
3206039Seric 
32135651Seric #ifdef NAMED_BIND
32225475Smiriam 	h_errno = 0;
32335651Seric #endif
32425475Smiriam 	errno = 0;
32525475Smiriam 
3269308Seric 	if (host[0] == '[')
3279308Seric 	{
32811147Seric 		long hid;
32956795Seric 		register char *p = strchr(host, ']');
3309308Seric 
33111147Seric 		if (p != NULL)
3329308Seric 		{
33311147Seric 			*p = '\0';
33411147Seric 			hid = inet_addr(&host[1]);
33511147Seric 			*p = ']';
3369308Seric 		}
33711147Seric 		if (p == NULL || hid == -1)
3389308Seric 		{
3399308Seric 			usrerr("Invalid numeric domain spec \"%s\"", host);
3409308Seric 			return (EX_NOHOST);
3419308Seric 		}
34252106Seric 		addr.sin_addr.s_addr = hid;
3439308Seric 	}
3449610Seric 	else
3459610Seric 	{
34629430Sbloom 		hp = gethostbyname(host);
34725475Smiriam 		if (hp == NULL)
34824945Seric 		{
34935651Seric #ifdef NAMED_BIND
35025475Smiriam 			if (errno == ETIMEDOUT || h_errno == TRY_AGAIN)
35125475Smiriam 				return (EX_TEMPFAIL);
35225657Seric 
35335651Seric 			/* if name server is specified, assume temp fail */
35435651Seric 			if (errno == ECONNREFUSED && UseNameServer)
35535651Seric 				return (EX_TEMPFAIL);
35635651Seric #endif
35725475Smiriam 			return (EX_NOHOST);
35824945Seric 		}
35952106Seric 		bcopy(hp->h_addr, (char *) &addr.sin_addr, hp->h_length);
36029430Sbloom 		i = 1;
3619610Seric 	}
3629610Seric 
3639610Seric 	/*
3649610Seric 	**  Determine the port number.
3659610Seric 	*/
3669610Seric 
36710011Seric 	if (port != 0)
36852106Seric 		addr.sin_port = htons(port);
36910011Seric 	else
3709610Seric 	{
3719610Seric 		register struct servent *sp = getservbyname("smtp", "tcp");
3729610Seric 
3739610Seric 		if (sp == NULL)
3749610Seric 		{
3759610Seric 			syserr("makeconnection: server \"smtp\" unknown");
37657977Seric 			return (EX_OSERR);
3779610Seric 		}
37852106Seric 		addr.sin_port = sp->s_port;
3799610Seric 	}
3806039Seric 
3816039Seric 	/*
3826039Seric 	**  Try to actually open the connection.
3836039Seric 	*/
3846039Seric 
38557736Seric 	for (;;)
38652106Seric 	{
38757736Seric 		if (tTd(16, 1))
38857736Seric 			printf("makeconnection (%s [%s])\n", host,
38957736Seric 			    inet_ntoa(addr.sin_addr));
39052106Seric 
39157736Seric 		if (usesecureport)
39257736Seric 		{
39357736Seric 			int rport = IPPORT_RESERVED - 1;
3946039Seric 
39557736Seric 			s = rresvport(&rport);
39657736Seric 		}
39757736Seric 		else
39857736Seric 		{
39957736Seric 			s = socket(AF_INET, SOCK_STREAM, 0);
40057736Seric 		}
40157736Seric 		if (s < 0)
40257736Seric 		{
40357736Seric 			sav_errno = errno;
40457736Seric 			syserr("makeconnection: no socket");
40557736Seric 			goto failure;
40657736Seric 		}
40710347Seric 
40857736Seric 		if (tTd(16, 1))
40957736Seric 			printf("makeconnection: fd=%d\n", s);
41057736Seric 
41157736Seric 		/* turn on network debugging? */
41257736Seric 		if (tTd(16, 101))
41357736Seric 		{
41457736Seric 			int on = 1;
41557736Seric 			(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG,
41657736Seric 					  (char *)&on, sizeof on);
41757736Seric 		}
41857736Seric 		if (CurEnv->e_xfp != NULL)
41957736Seric 			(void) fflush(CurEnv->e_xfp);		/* for debugging */
42057736Seric 		errno = 0;					/* for debugging */
42157736Seric 		addr.sin_family = AF_INET;
42257736Seric 		if (connect(s, (struct sockaddr *) &addr, sizeof addr) >= 0)
42357736Seric 			break;
42457736Seric 
42557736Seric 		/* couldn't connect.... figure out why */
42627744Sbloom 		sav_errno = errno;
42727744Sbloom 		(void) close(s);
42829430Sbloom 		if (hp && hp->h_addr_list[i])
42929430Sbloom 		{
43057736Seric 			if (tTd(16, 1))
43157736Seric 				printf("Connect failed; trying new address....\n");
43252106Seric 			bcopy(hp->h_addr_list[i++], (char *) &addr.sin_addr,
43352106Seric 					hp->h_length);
43457736Seric 			continue;
43529430Sbloom 		}
43629430Sbloom 
4376039Seric 		/* failure, decide if temporary or not */
4386039Seric 	failure:
43927744Sbloom 		switch (sav_errno)
4406039Seric 		{
4416039Seric 		  case EISCONN:
4426039Seric 		  case ETIMEDOUT:
4436897Seric 		  case EINPROGRESS:
4446897Seric 		  case EALREADY:
4456897Seric 		  case EADDRINUSE:
44610123Seric 		  case EHOSTDOWN:
4476897Seric 		  case ENETDOWN:
4486897Seric 		  case ENETRESET:
4496897Seric 		  case ENOBUFS:
4507204Seric 		  case ECONNREFUSED:
45111546Seric 		  case ECONNRESET:
45210081Seric 		  case EHOSTUNREACH:
45310098Seric 		  case ENETUNREACH:
45451995Seric #ifdef ENOSR
45551995Seric 		  case ENOSR:
45651995Seric #endif
4576039Seric 			/* there are others, I'm sure..... */
4586039Seric 			return (EX_TEMPFAIL);
4596039Seric 
46011147Seric 		  case EPERM:
46111147Seric 			/* why is this happening? */
46211147Seric 			syserr("makeconnection: funny failure, addr=%lx, port=%x",
46352106Seric 				addr.sin_addr.s_addr, addr.sin_port);
46414383Seric 			return (EX_TEMPFAIL);
46511147Seric 
4666039Seric 		  default:
46740932Srick 			{
46840932Srick 				extern char *errstring();
46940932Srick 
47040932Srick 				message(Arpa_Info, "%s", errstring(sav_errno));
47140932Srick 				return (EX_UNAVAILABLE);
47240932Srick 			}
4736039Seric 		}
4746039Seric 	}
4756039Seric 
4766039Seric 	/* connection ok, put it into canonical form */
47753739Seric 	mci->mci_out = fdopen(s, "w");
47853739Seric 	mci->mci_in = fdopen(dup(s), "r");
4796039Seric 
48010098Seric 	return (EX_OK);
4816039Seric }
48210758Seric /*
48310758Seric **  MYHOSTNAME -- return the name of this host.
48410758Seric **
48510758Seric **	Parameters:
48610758Seric **		hostbuf -- a place to return the name of this host.
48712313Seric **		size -- the size of hostbuf.
48810758Seric **
48910758Seric **	Returns:
49010758Seric **		A list of aliases for this host.
49110758Seric **
49210758Seric **	Side Effects:
493*58110Seric **		Sets the MyIpAddrs buffer to a list of my IP addresses.
49410758Seric */
4956039Seric 
496*58110Seric struct in_addr	MyIpAddrs[MAXIPADDR + 1];
497*58110Seric 
49810758Seric char **
49912313Seric myhostname(hostbuf, size)
50010758Seric 	char hostbuf[];
50112313Seric 	int size;
50210758Seric {
503*58110Seric 	register struct hostent *hp;
50410758Seric 	extern struct hostent *gethostbyname();
50510758Seric 
50623120Seric 	if (gethostname(hostbuf, size) < 0)
50723120Seric 	{
50823120Seric 		(void) strcpy(hostbuf, "localhost");
50923120Seric 	}
51011147Seric 	hp = gethostbyname(hostbuf);
51111147Seric 	if (hp != NULL)
51216877Seric 	{
513*58110Seric 		(void) strncpy(hostbuf, hp->h_name, size - 1);
514*58110Seric 		hostbuf[size - 1] = '\0';
515*58110Seric 
516*58110Seric 		if (hp->h_addrtype == AF_INET && hp->h_length == 4)
517*58110Seric 		{
518*58110Seric 			register int i;
519*58110Seric 
520*58110Seric 			for (i = 0; i < MAXIPADDR; i++)
521*58110Seric 			{
522*58110Seric 				if (hp->h_addr_list[i] == NULL)
523*58110Seric 					break;
524*58110Seric 				MyIpAddrs[i].s_addr = *(u_long *) hp->h_addr_list[i];
525*58110Seric 			}
526*58110Seric 			MyIpAddrs[i].s_addr = 0;
527*58110Seric 		}
528*58110Seric 
52911147Seric 		return (hp->h_aliases);
53016877Seric 	}
53110758Seric 	else
53210758Seric 		return (NULL);
53310758Seric }
53451315Seric /*
53553751Seric **  MAPHOSTNAME -- turn a hostname into canonical form
53653751Seric **
53753751Seric **	Parameters:
53856823Seric **		map -- a pointer to this map (unused).
53953751Seric **		hbuf -- a buffer containing a hostname.
54053751Seric **		hbsize -- the size of hbuf.
54155019Seric **		avp -- unused -- for compatibility with other mapping
54255019Seric **			functions.
54353751Seric **
54453751Seric **	Returns:
54553751Seric **		The mapping, if found.
54653751Seric **		NULL if no mapping found.
54753751Seric **
54853751Seric **	Side Effects:
54953751Seric **		Looks up the host specified in hbuf.  If it is not
55053751Seric **		the canonical name for that host, return the canonical
55153751Seric **		name.
55253751Seric */
55351315Seric 
55453751Seric char *
55556823Seric maphostname(map, hbuf, hbsize, avp)
55656823Seric 	MAP *map;
55716911Seric 	char *hbuf;
55816911Seric 	int hbsize;
55953751Seric 	char **avp;
56016911Seric {
56116911Seric 	register struct hostent *hp;
56233932Sbostic 	u_long in_addr;
56356823Seric 	char *cp;
564*58110Seric 	int i;
56533932Sbostic 	struct hostent *gethostbyaddr();
56616911Seric 
56756836Seric 	/* allow room for null */
56856823Seric 	hbsize--;
56953751Seric 
57025574Smiriam 	/*
57133932Sbostic 	 * If first character is a bracket, then it is an address
57233932Sbostic 	 * lookup.  Address is copied into a temporary buffer to
57333932Sbostic 	 * strip the brackets and to preserve hbuf if address is
57433932Sbostic 	 * unknown.
57533932Sbostic 	 */
57653751Seric 
57751315Seric 	if (*hbuf != '[')
57853751Seric 	{
57955019Seric 		extern bool getcanonname();
58055019Seric 
58153751Seric 		if (getcanonname(hbuf, hbsize))
58253751Seric 			return hbuf;
58353751Seric 		else
58453751Seric 			return NULL;
58553751Seric 	}
58656823Seric 	if ((cp = strchr(hbuf, ']')) == NULL)
58753751Seric 		return (NULL);
58840994Sbostic 	*cp = '\0';
58956823Seric 	in_addr = inet_addr(&hbuf[1]);
590*58110Seric 
591*58110Seric 	/* check to see if this is one of our addresses */
592*58110Seric 	for (i = 0; MyIpAddrs[i].s_addr != 0; i++)
593*58110Seric 	{
594*58110Seric 		if (MyIpAddrs[i].s_addr == in_addr)
595*58110Seric 		{
596*58110Seric 			strncpy(hbuf, MyHostName, hbsize);
597*58110Seric 			hbuf[hbsize] = '\0';
598*58110Seric 			return hbuf;
599*58110Seric 		}
600*58110Seric 	}
601*58110Seric 
602*58110Seric 	/* nope -- ask the name server */
60333932Sbostic 	hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET);
60433932Sbostic 	if (hp == NULL)
60553751Seric 		return (NULL);
60653751Seric 
607*58110Seric 	/* found a match -- copy out */
60856823Seric 	if (strlen(hp->h_name) > hbsize)
60956823Seric 		hp->h_name[hbsize] = '\0';
61053751Seric 	(void) strcpy(hbuf, hp->h_name);
61153751Seric 	return hbuf;
61233932Sbostic }
61316911Seric 
61456795Seric # else /* DAEMON */
61516911Seric /* code for systems without sophisticated networking */
61610758Seric 
61710758Seric /*
61810758Seric **  MYHOSTNAME -- stub version for case of no daemon code.
61911297Seric **
62011297Seric **	Can't convert to upper case here because might be a UUCP name.
62112313Seric **
62212313Seric **	Mark, you can change this to be anything you want......
62310758Seric */
62410758Seric 
62510758Seric char **
62612313Seric myhostname(hostbuf, size)
62710758Seric 	char hostbuf[];
62812313Seric 	int size;
62910758Seric {
63010758Seric 	register FILE *f;
63110758Seric 
63210758Seric 	hostbuf[0] = '\0';
63310758Seric 	f = fopen("/usr/include/whoami", "r");
63410758Seric 	if (f != NULL)
63510758Seric 	{
63612313Seric 		(void) fgets(hostbuf, size, f);
63710758Seric 		fixcrlf(hostbuf, TRUE);
63810758Seric 		(void) fclose(f);
63910758Seric 	}
64010758Seric 	return (NULL);
64110758Seric }
64216911Seric /*
64316911Seric **  MAPHOSTNAME -- turn a hostname into canonical form
64416911Seric **
64516911Seric **	Parameters:
64656823Seric **		map -- a pointer to the database map.
64716911Seric **		hbuf -- a buffer containing a hostname.
64853751Seric **		avp -- a pointer to a (cf file defined) argument vector.
64916911Seric **
65016911Seric **	Returns:
65153751Seric **		mapped host name
65251315Seric **		FALSE otherwise.
65316911Seric **
65416911Seric **	Side Effects:
65516911Seric **		Looks up the host specified in hbuf.  If it is not
65616911Seric **		the canonical name for that host, replace it with
65716911Seric **		the canonical name.  If the name is unknown, or it
65816911Seric **		is already the canonical name, leave it unchanged.
65916911Seric */
66010758Seric 
66116911Seric /*ARGSUSED*/
66253751Seric char *
66356823Seric maphostname(map, hbuf, hbsize, avp)
66456823Seric 	MAP *map;
66516911Seric 	char *hbuf;
66616911Seric 	int hbsize;
66753751Seric 	char **avp;
66816911Seric {
66953751Seric 	return NULL;
67016911Seric }
67116911Seric 
67256795Seric #endif /* DAEMON */
673