xref: /csrg-svn/usr.sbin/sendmail/src/daemon.c (revision 58308)
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>
1058153Seric #include <signal.h>
1140962Sbostic #include "sendmail.h"
124535Seric 
1333780Sbostic #ifndef lint
1433780Sbostic #ifdef DAEMON
15*58308Seric static char sccsid[] = "@(#)daemon.c	6.11 (Berkeley) 02/28/93 (with daemon mode)";
1633780Sbostic #else
17*58308Seric static char sccsid[] = "@(#)daemon.c	6.11 (Berkeley) 02/28/93 (without daemon mode)";
1833780Sbostic #endif
1933780Sbostic #endif /* not lint */
204535Seric 
2133780Sbostic #ifdef DAEMON
2233780Sbostic 
2323120Seric # include <netdb.h>
2423120Seric # include <sys/wait.h>
2523120Seric # include <sys/time.h>
265978Seric 
274535Seric /*
284535Seric **  DAEMON.C -- routines to use when running as a daemon.
297556Seric **
307556Seric **	This entire file is highly dependent on the 4.2 BSD
317556Seric **	interprocess communication primitives.  No attempt has
327556Seric **	been made to make this file portable to Version 7,
337556Seric **	Version 6, MPX files, etc.  If you should try such a
347556Seric **	thing yourself, I recommend chucking the entire file
357556Seric **	and starting from scratch.  Basic semantics are:
367556Seric **
377556Seric **	getrequests()
387556Seric **		Opens a port and initiates a connection.
397556Seric **		Returns in a child.  Must set InChannel and
407556Seric **		OutChannel appropriately.
4110206Seric **	clrdaemon()
4210206Seric **		Close any open files associated with getting
4310206Seric **		the connection; this is used when running the queue,
4410206Seric **		etc., to avoid having extra file descriptors during
4510206Seric **		the queue run and to avoid confusing the network
4610206Seric **		code (if it cares).
4752106Seric **	makeconnection(host, port, outfile, infile, usesecureport)
487556Seric **		Make a connection to the named host on the given
497556Seric **		port.  Set *outfile and *infile to the files
507556Seric **		appropriate for communication.  Returns zero on
517556Seric **		success, else an exit status describing the
527556Seric **		error.
5356823Seric **	maphostname(map, hbuf, hbufsiz, avp)
5456823Seric **		Convert the entry in hbuf into a canonical form.
554535Seric */
564535Seric /*
574535Seric **  GETREQUESTS -- open mail IPC port and get requests.
584535Seric **
594535Seric **	Parameters:
604535Seric **		none.
614535Seric **
624535Seric **	Returns:
634535Seric **		none.
644535Seric **
654535Seric **	Side Effects:
664535Seric **		Waits until some interesting activity occurs.  When
674535Seric **		it does, a child is created to process it, and the
684535Seric **		parent waits for completion.  Return from this
699886Seric **		routine is always in the child.  The file pointers
709886Seric **		"InChannel" and "OutChannel" should be set to point
719886Seric **		to the communication channel.
724535Seric */
734535Seric 
7416144Seric int	DaemonSocket	= -1;		/* fd describing socket */
7516144Seric 
764535Seric getrequests()
774535Seric {
789610Seric 	int t;
799610Seric 	register struct servent *sp;
8025027Seric 	int on = 1;
8153751Seric 	bool refusingconnections = TRUE;
8252106Seric 	struct sockaddr_in srvraddr;
8346928Sbostic 	extern void reapchild();
847117Seric 
859610Seric 	/*
869610Seric 	**  Set up the address for the mailer.
879610Seric 	*/
889610Seric 
899610Seric 	sp = getservbyname("smtp", "tcp");
909610Seric 	if (sp == NULL)
919610Seric 	{
9258151Seric 		syserr("554 server \"smtp\" unknown");
9310167Seric 		goto severe;
949610Seric 	}
9552106Seric 	srvraddr.sin_family = AF_INET;
9652106Seric 	srvraddr.sin_addr.s_addr = INADDR_ANY;
9752106Seric 	srvraddr.sin_port = sp->s_port;
989610Seric 
999610Seric 	/*
1009610Seric 	**  Try to actually open the connection.
1019610Seric 	*/
1029610Seric 
1039610Seric 	if (tTd(15, 1))
10452106Seric 		printf("getrequests: port 0x%x\n", srvraddr.sin_port);
1059610Seric 
1069610Seric 	/* get a socket for the SMTP connection */
10723120Seric 	DaemonSocket = socket(AF_INET, SOCK_STREAM, 0);
10810206Seric 	if (DaemonSocket < 0)
1099610Seric 	{
1109610Seric 		/* probably another daemon already */
1119610Seric 		syserr("getrequests: can't create socket");
1129610Seric 	  severe:
1139610Seric # ifdef LOG
1149610Seric 		if (LogLevel > 0)
11557663Seric 			syslog(LOG_ALERT, "problem creating SMTP socket");
11656795Seric # endif /* LOG */
1179610Seric 		finis();
1189610Seric 	}
11910347Seric 
12010347Seric 	/* turn on network debugging? */
12156328Seric 	if (tTd(15, 101))
12224945Seric 		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on);
12310347Seric 
12425027Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on);
12525027Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on);
12625027Seric 
12752106Seric 	if (bind(DaemonSocket, (struct sockaddr *)&srvraddr, sizeof srvraddr) < 0)
1289610Seric 	{
1299610Seric 		syserr("getrequests: cannot bind");
13010206Seric 		(void) close(DaemonSocket);
1319610Seric 		goto severe;
1329610Seric 	}
1339610Seric 
13424955Seric 	(void) signal(SIGCHLD, reapchild);
13524945Seric 
1369610Seric 	if (tTd(15, 1))
13710206Seric 		printf("getrequests: %d\n", DaemonSocket);
1389610Seric 
1394631Seric 	for (;;)
1404631Seric 	{
14114875Seric 		register int pid;
14211147Seric 		auto int lotherend;
14353751Seric 		extern bool refuseconnections();
14411147Seric 
14514875Seric 		/* see if we are rejecting connections */
14653751Seric 		CurrentLA = getla();
14753751Seric 		if (refuseconnections())
14836584Sbostic 		{
14953751Seric 			if (!refusingconnections)
15053751Seric 			{
15153751Seric 				/* don't queue so peer will fail quickly */
15253751Seric 				(void) listen(DaemonSocket, 0);
15353751Seric 				refusingconnections = TRUE;
15453751Seric 			}
15557385Seric 			setproctitle("rejecting connections: load average: %d",
15657385Seric 				CurrentLA);
15714875Seric 			sleep(5);
15853751Seric 			continue;
15936584Sbostic 		}
16014875Seric 
16153751Seric 		if (refusingconnections)
16253751Seric 		{
16353751Seric 			/* start listening again */
16453751Seric 			if (listen(DaemonSocket, 10) < 0)
16553751Seric 			{
16653751Seric 				syserr("getrequests: cannot listen");
16753751Seric 				(void) close(DaemonSocket);
16853751Seric 				goto severe;
16953751Seric 			}
17053751Seric 			setproctitle("accepting connections");
17153751Seric 			refusingconnections = FALSE;
17253751Seric 		}
17353751Seric 
1749610Seric 		/* wait for a connection */
1759610Seric 		do
1769610Seric 		{
1779610Seric 			errno = 0;
17836230Skarels 			lotherend = sizeof RealHostAddr;
17946928Sbostic 			t = accept(DaemonSocket,
18046928Sbostic 			    (struct sockaddr *)&RealHostAddr, &lotherend);
1819610Seric 		} while (t < 0 && errno == EINTR);
1829610Seric 		if (t < 0)
1835978Seric 		{
1849610Seric 			syserr("getrequests: accept");
1859610Seric 			sleep(5);
1869610Seric 			continue;
1875978Seric 		}
1884631Seric 
1895978Seric 		/*
1905978Seric 		**  Create a subprocess to process the mail.
1915978Seric 		*/
1925978Seric 
1937677Seric 		if (tTd(15, 2))
1949610Seric 			printf("getrequests: forking (fd = %d)\n", t);
1955978Seric 
1964636Seric 		pid = fork();
1974636Seric 		if (pid < 0)
1984631Seric 		{
1994636Seric 			syserr("daemon: cannot fork");
2004636Seric 			sleep(10);
2019610Seric 			(void) close(t);
2024636Seric 			continue;
2034631Seric 		}
2044631Seric 
2054636Seric 		if (pid == 0)
2064631Seric 		{
20711147Seric 			extern struct hostent *gethostbyaddr();
20811147Seric 			register struct hostent *hp;
20911147Seric 			char buf[MAXNAME];
21057135Seric 			extern char *inet_ntoa();
21111147Seric 
2124636Seric 			/*
2134636Seric 			**  CHILD -- return to caller.
21411147Seric 			**	Collect verified idea of sending host.
2154636Seric 			**	Verify calling user id if possible here.
2164636Seric 			*/
2174631Seric 
21824955Seric 			(void) signal(SIGCHLD, SIG_DFL);
21924950Seric 
22011147Seric 			/* determine host name */
22136230Skarels 			hp = gethostbyaddr((char *) &RealHostAddr.sin_addr, sizeof RealHostAddr.sin_addr, AF_INET);
22211147Seric 			if (hp != NULL)
22323104Seric 				(void) strcpy(buf, hp->h_name);
22411147Seric 			else
22516884Seric 			{
22616884Seric 				/* produce a dotted quad */
22716884Seric 				(void) sprintf(buf, "[%s]",
22836230Skarels 					inet_ntoa(RealHostAddr.sin_addr));
22916884Seric 			}
23016884Seric 
23155173Seric #ifdef LOG
23257977Seric 			if (LogLevel > 10)
23355173Seric 			{
23455173Seric 				/* log connection information */
23555173Seric 				syslog(LOG_INFO, "connect from %s (%s)",
23655173Seric 					buf, inet_ntoa(RealHostAddr.sin_addr));
23755173Seric 			}
23855173Seric #endif
23955173Seric 
24016884Seric 			/* should we check for illegal connection here? XXX */
24116884Seric 
24211147Seric 			RealHostName = newstr(buf);
24311147Seric 
24410206Seric 			(void) close(DaemonSocket);
2459610Seric 			InChannel = fdopen(t, "r");
24621062Seric 			OutChannel = fdopen(dup(t), "w");
2477677Seric 			if (tTd(15, 2))
2485978Seric 				printf("getreq: returning\n");
2497876Seric # ifdef LOG
2507876Seric 			if (LogLevel > 11)
2517876Seric 				syslog(LOG_DEBUG, "connected, pid=%d", getpid());
25256795Seric # endif /* LOG */
2534636Seric 			return;
2544631Seric 		}
2554631Seric 
2567117Seric 		/* close the port so that others will hang (for a while) */
2579610Seric 		(void) close(t);
2584631Seric 	}
2599886Seric 	/*NOTREACHED*/
2604631Seric }
2615978Seric /*
26210206Seric **  CLRDAEMON -- reset the daemon connection
26310206Seric **
26410206Seric **	Parameters:
26510206Seric **		none.
26610206Seric **
26710206Seric **	Returns:
26810206Seric **		none.
26910206Seric **
27010206Seric **	Side Effects:
27110206Seric **		releases any resources used by the passive daemon.
27210206Seric */
27310206Seric 
27410206Seric clrdaemon()
27510206Seric {
27610206Seric 	if (DaemonSocket >= 0)
27710206Seric 		(void) close(DaemonSocket);
27810206Seric 	DaemonSocket = -1;
27910206Seric }
28010206Seric /*
2816039Seric **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
2826039Seric **
2836039Seric **	Parameters:
2846039Seric **		host -- the name of the host.
2856633Seric **		port -- the port number to connect to.
28653739Seric **		mci -- a pointer to the mail connection information
28753739Seric **			structure to be filled in.
28852106Seric **		usesecureport -- if set, use a low numbered (reserved)
28952106Seric **			port to provide some rudimentary authentication.
2906039Seric **
2916039Seric **	Returns:
2926039Seric **		An exit code telling whether the connection could be
2936039Seric **			made and if not why not.
2946039Seric **
2956039Seric **	Side Effects:
2966039Seric **		none.
2976039Seric */
2985978Seric 
29958305Seric struct sockaddr_in	CurHostAddr;		/* address of current host */
30058305Seric 
30154967Seric int
30253739Seric makeconnection(host, port, mci, usesecureport)
3036039Seric 	char *host;
3047286Seric 	u_short port;
30554967Seric 	register MCI *mci;
30652106Seric 	bool usesecureport;
3076039Seric {
30829430Sbloom 	register int i, s;
30929430Sbloom 	register struct hostent *hp = (struct hostent *)NULL;
31052106Seric 	struct sockaddr_in addr;
31152106Seric 	int sav_errno;
31229430Sbloom 	extern char *inet_ntoa();
31335651Seric #ifdef NAMED_BIND
31435651Seric 	extern int h_errno;
31535651Seric #endif
3166039Seric 
3176039Seric 	/*
3186039Seric 	**  Set up the address for the mailer.
3199308Seric 	**	Accept "[a.b.c.d]" syntax for host name.
3206039Seric 	*/
3216039Seric 
32235651Seric #ifdef NAMED_BIND
32325475Smiriam 	h_errno = 0;
32435651Seric #endif
32525475Smiriam 	errno = 0;
32625475Smiriam 
3279308Seric 	if (host[0] == '[')
3289308Seric 	{
32911147Seric 		long hid;
33056795Seric 		register char *p = strchr(host, ']');
3319308Seric 
33211147Seric 		if (p != NULL)
3339308Seric 		{
33411147Seric 			*p = '\0';
33511147Seric 			hid = inet_addr(&host[1]);
33611147Seric 			*p = ']';
3379308Seric 		}
33811147Seric 		if (p == NULL || hid == -1)
3399308Seric 		{
34058151Seric 			usrerr("553 Invalid numeric domain spec \"%s\"", host);
3419308Seric 			return (EX_NOHOST);
3429308Seric 		}
34352106Seric 		addr.sin_addr.s_addr = hid;
3449308Seric 	}
3459610Seric 	else
3469610Seric 	{
34729430Sbloom 		hp = gethostbyname(host);
34825475Smiriam 		if (hp == NULL)
34924945Seric 		{
35035651Seric #ifdef NAMED_BIND
35125475Smiriam 			if (errno == ETIMEDOUT || h_errno == TRY_AGAIN)
35225475Smiriam 				return (EX_TEMPFAIL);
35325657Seric 
35435651Seric 			/* if name server is specified, assume temp fail */
35535651Seric 			if (errno == ECONNREFUSED && UseNameServer)
35635651Seric 				return (EX_TEMPFAIL);
35735651Seric #endif
35825475Smiriam 			return (EX_NOHOST);
35924945Seric 		}
36052106Seric 		bcopy(hp->h_addr, (char *) &addr.sin_addr, hp->h_length);
36129430Sbloom 		i = 1;
3629610Seric 	}
3639610Seric 
3649610Seric 	/*
3659610Seric 	**  Determine the port number.
3669610Seric 	*/
3679610Seric 
36810011Seric 	if (port != 0)
36952106Seric 		addr.sin_port = htons(port);
37010011Seric 	else
3719610Seric 	{
3729610Seric 		register struct servent *sp = getservbyname("smtp", "tcp");
3739610Seric 
3749610Seric 		if (sp == NULL)
3759610Seric 		{
37658151Seric 			syserr("554 makeconnection: server \"smtp\" unknown");
37757977Seric 			return (EX_OSERR);
3789610Seric 		}
37952106Seric 		addr.sin_port = sp->s_port;
3809610Seric 	}
3816039Seric 
3826039Seric 	/*
3836039Seric 	**  Try to actually open the connection.
3846039Seric 	*/
3856039Seric 
38657736Seric 	for (;;)
38752106Seric 	{
38857736Seric 		if (tTd(16, 1))
38957736Seric 			printf("makeconnection (%s [%s])\n", host,
39057736Seric 			    inet_ntoa(addr.sin_addr));
39152106Seric 
39257736Seric 		if (usesecureport)
39357736Seric 		{
39457736Seric 			int rport = IPPORT_RESERVED - 1;
3956039Seric 
39657736Seric 			s = rresvport(&rport);
39757736Seric 		}
39857736Seric 		else
39957736Seric 		{
40057736Seric 			s = socket(AF_INET, SOCK_STREAM, 0);
40157736Seric 		}
40257736Seric 		if (s < 0)
40357736Seric 		{
40457736Seric 			sav_errno = errno;
40557736Seric 			syserr("makeconnection: no socket");
40657736Seric 			goto failure;
40757736Seric 		}
40810347Seric 
40957736Seric 		if (tTd(16, 1))
41057736Seric 			printf("makeconnection: fd=%d\n", s);
41157736Seric 
41257736Seric 		/* turn on network debugging? */
41357736Seric 		if (tTd(16, 101))
41457736Seric 		{
41557736Seric 			int on = 1;
41657736Seric 			(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG,
41757736Seric 					  (char *)&on, sizeof on);
41857736Seric 		}
41957736Seric 		if (CurEnv->e_xfp != NULL)
42057736Seric 			(void) fflush(CurEnv->e_xfp);		/* for debugging */
42157736Seric 		errno = 0;					/* for debugging */
42257736Seric 		addr.sin_family = AF_INET;
42357736Seric 		if (connect(s, (struct sockaddr *) &addr, sizeof addr) >= 0)
42457736Seric 			break;
42557736Seric 
42657736Seric 		/* couldn't connect.... figure out why */
42727744Sbloom 		sav_errno = errno;
42827744Sbloom 		(void) close(s);
42929430Sbloom 		if (hp && hp->h_addr_list[i])
43029430Sbloom 		{
43157736Seric 			if (tTd(16, 1))
43257736Seric 				printf("Connect failed; trying new address....\n");
43352106Seric 			bcopy(hp->h_addr_list[i++], (char *) &addr.sin_addr,
43452106Seric 					hp->h_length);
43557736Seric 			continue;
43629430Sbloom 		}
43729430Sbloom 
4386039Seric 		/* failure, decide if temporary or not */
4396039Seric 	failure:
44027744Sbloom 		switch (sav_errno)
4416039Seric 		{
4426039Seric 		  case EISCONN:
4436039Seric 		  case ETIMEDOUT:
4446897Seric 		  case EINPROGRESS:
4456897Seric 		  case EALREADY:
4466897Seric 		  case EADDRINUSE:
44710123Seric 		  case EHOSTDOWN:
4486897Seric 		  case ENETDOWN:
4496897Seric 		  case ENETRESET:
4506897Seric 		  case ENOBUFS:
4517204Seric 		  case ECONNREFUSED:
45211546Seric 		  case ECONNRESET:
45310081Seric 		  case EHOSTUNREACH:
45410098Seric 		  case ENETUNREACH:
45551995Seric #ifdef ENOSR
45651995Seric 		  case ENOSR:
45751995Seric #endif
4586039Seric 			/* there are others, I'm sure..... */
4596039Seric 			return (EX_TEMPFAIL);
4606039Seric 
46111147Seric 		  case EPERM:
46211147Seric 			/* why is this happening? */
46311147Seric 			syserr("makeconnection: funny failure, addr=%lx, port=%x",
46452106Seric 				addr.sin_addr.s_addr, addr.sin_port);
46514383Seric 			return (EX_TEMPFAIL);
46611147Seric 
4676039Seric 		  default:
46840932Srick 			{
46940932Srick 				extern char *errstring();
47040932Srick 
47158151Seric 				message("%s", errstring(sav_errno));
47240932Srick 				return (EX_UNAVAILABLE);
47340932Srick 			}
4746039Seric 		}
4756039Seric 	}
4766039Seric 
4776039Seric 	/* connection ok, put it into canonical form */
47853739Seric 	mci->mci_out = fdopen(s, "w");
47953739Seric 	mci->mci_in = fdopen(dup(s), "r");
4806039Seric 
48158305Seric 	/* save for logging */
48258305Seric 	CurHostAddr = addr;
48358305Seric 
48410098Seric 	return (EX_OK);
4856039Seric }
48610758Seric /*
48710758Seric **  MYHOSTNAME -- return the name of this host.
48810758Seric **
48910758Seric **	Parameters:
49010758Seric **		hostbuf -- a place to return the name of this host.
49112313Seric **		size -- the size of hostbuf.
49210758Seric **
49310758Seric **	Returns:
49410758Seric **		A list of aliases for this host.
49510758Seric **
49610758Seric **	Side Effects:
49758110Seric **		Sets the MyIpAddrs buffer to a list of my IP addresses.
49810758Seric */
4996039Seric 
50058110Seric struct in_addr	MyIpAddrs[MAXIPADDR + 1];
50158110Seric 
50210758Seric char **
50312313Seric myhostname(hostbuf, size)
50410758Seric 	char hostbuf[];
50512313Seric 	int size;
50610758Seric {
50758110Seric 	register struct hostent *hp;
50810758Seric 	extern struct hostent *gethostbyname();
50910758Seric 
51023120Seric 	if (gethostname(hostbuf, size) < 0)
51123120Seric 	{
51223120Seric 		(void) strcpy(hostbuf, "localhost");
51323120Seric 	}
51411147Seric 	hp = gethostbyname(hostbuf);
51511147Seric 	if (hp != NULL)
51616877Seric 	{
51758110Seric 		(void) strncpy(hostbuf, hp->h_name, size - 1);
51858110Seric 		hostbuf[size - 1] = '\0';
51958110Seric 
52058110Seric 		if (hp->h_addrtype == AF_INET && hp->h_length == 4)
52158110Seric 		{
52258110Seric 			register int i;
52358110Seric 
52458110Seric 			for (i = 0; i < MAXIPADDR; i++)
52558110Seric 			{
52658110Seric 				if (hp->h_addr_list[i] == NULL)
52758110Seric 					break;
52858110Seric 				MyIpAddrs[i].s_addr = *(u_long *) hp->h_addr_list[i];
52958110Seric 			}
53058110Seric 			MyIpAddrs[i].s_addr = 0;
53158110Seric 		}
53258110Seric 
53311147Seric 		return (hp->h_aliases);
53416877Seric 	}
53510758Seric 	else
53610758Seric 		return (NULL);
53710758Seric }
53851315Seric /*
539*58308Seric **  GETREALHOSTNAME -- get the real host name asociated with a file descriptor
540*58308Seric **
541*58308Seric **	Parameters:
542*58308Seric **		fd -- the descriptor
543*58308Seric **
544*58308Seric **	Returns:
545*58308Seric **		The host name associated with this descriptor, if it can
546*58308Seric **			be determined.
547*58308Seric **		NULL otherwise.
548*58308Seric **
549*58308Seric **	Side Effects:
550*58308Seric **		none
551*58308Seric */
552*58308Seric 
553*58308Seric char *
554*58308Seric getrealhostname(fd)
555*58308Seric 	int fd;
556*58308Seric {
557*58308Seric 	register struct hostent *hp;
558*58308Seric 	struct sockaddr_in sin;
559*58308Seric 	int sinlen;
560*58308Seric 	char hbuf[MAXNAME];
561*58308Seric 	extern struct hostent *gethostbyaddr();
562*58308Seric 	extern char *inet_ntoa();
563*58308Seric 
564*58308Seric 	if (getsockname(fd, (struct sockaddr *) &sin, &sinlen) < 0)
565*58308Seric 		return NULL;
566*58308Seric 	hp = gethostbyaddr((char *) &sin.sin_addr, sizeof sin.sin_addr,
567*58308Seric 			   sin.sin_family);
568*58308Seric 	if (hp != NULL)
569*58308Seric 		(void) strcpy(hbuf, hp->h_name);
570*58308Seric 	else
571*58308Seric 		(void) sprintf(hbuf, "[%s]", inet_ntoa(sin.sin_addr));
572*58308Seric 	return hbuf;
573*58308Seric }
574*58308Seric /*
57553751Seric **  MAPHOSTNAME -- turn a hostname into canonical form
57653751Seric **
57753751Seric **	Parameters:
57856823Seric **		map -- a pointer to this map (unused).
57953751Seric **		hbuf -- a buffer containing a hostname.
58053751Seric **		hbsize -- the size of hbuf.
58155019Seric **		avp -- unused -- for compatibility with other mapping
58255019Seric **			functions.
58353751Seric **
58453751Seric **	Returns:
58553751Seric **		The mapping, if found.
58653751Seric **		NULL if no mapping found.
58753751Seric **
58853751Seric **	Side Effects:
58953751Seric **		Looks up the host specified in hbuf.  If it is not
59053751Seric **		the canonical name for that host, return the canonical
59153751Seric **		name.
59253751Seric */
59351315Seric 
59453751Seric char *
59556823Seric maphostname(map, hbuf, hbsize, avp)
59656823Seric 	MAP *map;
59716911Seric 	char *hbuf;
59816911Seric 	int hbsize;
59953751Seric 	char **avp;
60016911Seric {
60116911Seric 	register struct hostent *hp;
60233932Sbostic 	u_long in_addr;
60356823Seric 	char *cp;
60458110Seric 	int i;
60533932Sbostic 	struct hostent *gethostbyaddr();
60616911Seric 
60756836Seric 	/* allow room for null */
60856823Seric 	hbsize--;
60953751Seric 
61025574Smiriam 	/*
61133932Sbostic 	 * If first character is a bracket, then it is an address
61233932Sbostic 	 * lookup.  Address is copied into a temporary buffer to
61333932Sbostic 	 * strip the brackets and to preserve hbuf if address is
61433932Sbostic 	 * unknown.
61533932Sbostic 	 */
61653751Seric 
61751315Seric 	if (*hbuf != '[')
61853751Seric 	{
61955019Seric 		extern bool getcanonname();
62055019Seric 
62153751Seric 		if (getcanonname(hbuf, hbsize))
62253751Seric 			return hbuf;
62353751Seric 		else
62453751Seric 			return NULL;
62553751Seric 	}
62656823Seric 	if ((cp = strchr(hbuf, ']')) == NULL)
62753751Seric 		return (NULL);
62840994Sbostic 	*cp = '\0';
62956823Seric 	in_addr = inet_addr(&hbuf[1]);
63058110Seric 
63158110Seric 	/* check to see if this is one of our addresses */
63258110Seric 	for (i = 0; MyIpAddrs[i].s_addr != 0; i++)
63358110Seric 	{
63458110Seric 		if (MyIpAddrs[i].s_addr == in_addr)
63558110Seric 		{
63658110Seric 			strncpy(hbuf, MyHostName, hbsize);
63758110Seric 			hbuf[hbsize] = '\0';
63858110Seric 			return hbuf;
63958110Seric 		}
64058110Seric 	}
64158110Seric 
64258110Seric 	/* nope -- ask the name server */
64333932Sbostic 	hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET);
64433932Sbostic 	if (hp == NULL)
64553751Seric 		return (NULL);
64653751Seric 
64758110Seric 	/* found a match -- copy out */
64856823Seric 	if (strlen(hp->h_name) > hbsize)
64956823Seric 		hp->h_name[hbsize] = '\0';
65053751Seric 	(void) strcpy(hbuf, hp->h_name);
65153751Seric 	return hbuf;
65233932Sbostic }
65316911Seric 
65456795Seric # else /* DAEMON */
65516911Seric /* code for systems without sophisticated networking */
65610758Seric 
65710758Seric /*
65810758Seric **  MYHOSTNAME -- stub version for case of no daemon code.
65911297Seric **
66011297Seric **	Can't convert to upper case here because might be a UUCP name.
66112313Seric **
66212313Seric **	Mark, you can change this to be anything you want......
66310758Seric */
66410758Seric 
66510758Seric char **
66612313Seric myhostname(hostbuf, size)
66710758Seric 	char hostbuf[];
66812313Seric 	int size;
66910758Seric {
67010758Seric 	register FILE *f;
67110758Seric 
67210758Seric 	hostbuf[0] = '\0';
67310758Seric 	f = fopen("/usr/include/whoami", "r");
67410758Seric 	if (f != NULL)
67510758Seric 	{
67612313Seric 		(void) fgets(hostbuf, size, f);
67710758Seric 		fixcrlf(hostbuf, TRUE);
67810758Seric 		(void) fclose(f);
67910758Seric 	}
68010758Seric 	return (NULL);
68110758Seric }
68216911Seric /*
683*58308Seric **  GETREALHOSTNAME -- get the real host name asociated with a file descriptor
684*58308Seric **
685*58308Seric **	Parameters:
686*58308Seric **		fd -- the descriptor
687*58308Seric **
688*58308Seric **	Returns:
689*58308Seric **		The host name associated with this descriptor, if it can
690*58308Seric **			be determined.
691*58308Seric **		NULL otherwise.
692*58308Seric **
693*58308Seric **	Side Effects:
694*58308Seric **		none
695*58308Seric */
696*58308Seric 
697*58308Seric char *
698*58308Seric getrealhostname(fd)
699*58308Seric 	int fd;
700*58308Seric {
701*58308Seric 	return NULL;
702*58308Seric }
703*58308Seric /*
70416911Seric **  MAPHOSTNAME -- turn a hostname into canonical form
70516911Seric **
70616911Seric **	Parameters:
70756823Seric **		map -- a pointer to the database map.
70816911Seric **		hbuf -- a buffer containing a hostname.
70953751Seric **		avp -- a pointer to a (cf file defined) argument vector.
71016911Seric **
71116911Seric **	Returns:
71253751Seric **		mapped host name
71351315Seric **		FALSE otherwise.
71416911Seric **
71516911Seric **	Side Effects:
71616911Seric **		Looks up the host specified in hbuf.  If it is not
71716911Seric **		the canonical name for that host, replace it with
71816911Seric **		the canonical name.  If the name is unknown, or it
71916911Seric **		is already the canonical name, leave it unchanged.
72016911Seric */
72110758Seric 
72216911Seric /*ARGSUSED*/
72353751Seric char *
72456823Seric maphostname(map, hbuf, hbsize, avp)
72556823Seric 	MAP *map;
72616911Seric 	char *hbuf;
72716911Seric 	int hbsize;
72853751Seric 	char **avp;
72916911Seric {
73053751Seric 	return NULL;
73116911Seric }
73216911Seric 
73356795Seric #endif /* DAEMON */
734