xref: /csrg-svn/usr.sbin/sendmail/src/daemon.c (revision 27744)
122700Sdist /*
222700Sdist **  Sendmail
322700Sdist **  Copyright (c) 1983  Eric P. Allman
422700Sdist **  Berkeley, California
522700Sdist **
622700Sdist **  Copyright (c) 1983 Regents of the University of California.
722700Sdist **  All rights reserved.  The Berkeley software License Agreement
822700Sdist **  specifies the terms and conditions for redistribution.
922700Sdist */
1022700Sdist 
1122700Sdist 
126039Seric # include <errno.h>
134535Seric # include "sendmail.h"
144535Seric 
1523120Seric # ifndef DAEMON
1623120Seric # ifndef lint
17*27744Sbloom static char	SccsId[] = "@(#)daemon.c	5.19 (Berkeley) 05/06/86	(w/o daemon mode)";
1823120Seric # endif not lint
1923120Seric # else
204535Seric 
2123120Seric # include <netdb.h>
2224945Seric # include <sys/signal.h>
2323120Seric # include <sys/wait.h>
2423120Seric # include <sys/time.h>
2523120Seric # include <sys/resource.h>
265978Seric 
2723120Seric # ifndef lint
28*27744Sbloom static char	SccsId[] = "@(#)daemon.c	5.19 (Berkeley) 05/06/86 (with daemon mode)";
2923120Seric # endif not lint
305978Seric 
314535Seric /*
324535Seric **  DAEMON.C -- routines to use when running as a daemon.
337556Seric **
347556Seric **	This entire file is highly dependent on the 4.2 BSD
357556Seric **	interprocess communication primitives.  No attempt has
367556Seric **	been made to make this file portable to Version 7,
377556Seric **	Version 6, MPX files, etc.  If you should try such a
387556Seric **	thing yourself, I recommend chucking the entire file
397556Seric **	and starting from scratch.  Basic semantics are:
407556Seric **
417556Seric **	getrequests()
427556Seric **		Opens a port and initiates a connection.
437556Seric **		Returns in a child.  Must set InChannel and
447556Seric **		OutChannel appropriately.
4510206Seric **	clrdaemon()
4610206Seric **		Close any open files associated with getting
4710206Seric **		the connection; this is used when running the queue,
4810206Seric **		etc., to avoid having extra file descriptors during
4910206Seric **		the queue run and to avoid confusing the network
5010206Seric **		code (if it cares).
517556Seric **	makeconnection(host, port, outfile, infile)
527556Seric **		Make a connection to the named host on the given
537556Seric **		port.  Set *outfile and *infile to the files
547556Seric **		appropriate for communication.  Returns zero on
557556Seric **		success, else an exit status describing the
567556Seric **		error.
5725699Seric **	maphostname(hbuf, hbufsize)
5825699Seric **		Convert the entry in hbuf into a canonical form.  It
5925699Seric **		may not be larger than hbufsize.
604535Seric */
614535Seric /*
624535Seric **  GETREQUESTS -- open mail IPC port and get requests.
634535Seric **
644535Seric **	Parameters:
654535Seric **		none.
664535Seric **
674535Seric **	Returns:
684535Seric **		none.
694535Seric **
704535Seric **	Side Effects:
714535Seric **		Waits until some interesting activity occurs.  When
724535Seric **		it does, a child is created to process it, and the
734535Seric **		parent waits for completion.  Return from this
749886Seric **		routine is always in the child.  The file pointers
759886Seric **		"InChannel" and "OutChannel" should be set to point
769886Seric **		to the communication channel.
774535Seric */
784535Seric 
7910206Seric struct sockaddr_in	SendmailAddress;/* internet address of sendmail */
809610Seric 
8116144Seric int	DaemonSocket	= -1;		/* fd describing socket */
8216890Seric char	*NetName;			/* name of home (local?) network */
8316144Seric 
844535Seric getrequests()
854535Seric {
869610Seric 	int t;
879610Seric 	register struct servent *sp;
8825027Seric 	int on = 1;
8924945Seric 	extern reapchild();
907117Seric 
919610Seric 	/*
929610Seric 	**  Set up the address for the mailer.
939610Seric 	*/
949610Seric 
959610Seric 	sp = getservbyname("smtp", "tcp");
969610Seric 	if (sp == NULL)
979610Seric 	{
989610Seric 		syserr("server \"smtp\" unknown");
9910167Seric 		goto severe;
1009610Seric 	}
1019610Seric 	SendmailAddress.sin_family = AF_INET;
1029610Seric 	SendmailAddress.sin_addr.s_addr = INADDR_ANY;
1039740Ssam 	SendmailAddress.sin_port = sp->s_port;
1049610Seric 
1059610Seric 	/*
1069610Seric 	**  Try to actually open the connection.
1079610Seric 	*/
1089610Seric 
1099610Seric # ifdef DEBUG
1109610Seric 	if (tTd(15, 1))
1119610Seric 		printf("getrequests: port 0x%x\n", SendmailAddress.sin_port);
1129610Seric # endif DEBUG
1139610Seric 
1149610Seric 	/* get a socket for the SMTP connection */
11523120Seric 	DaemonSocket = socket(AF_INET, SOCK_STREAM, 0);
11610206Seric 	if (DaemonSocket < 0)
1179610Seric 	{
1189610Seric 		/* probably another daemon already */
1199610Seric 		syserr("getrequests: can't create socket");
1209610Seric 	  severe:
1219610Seric # ifdef LOG
1229610Seric 		if (LogLevel > 0)
12324858Seric 			syslog(LOG_ALERT, "cannot get connection");
1249610Seric # endif LOG
1259610Seric 		finis();
1269610Seric 	}
12710347Seric 
12810347Seric #ifdef DEBUG
12910347Seric 	/* turn on network debugging? */
13010347Seric 	if (tTd(15, 15))
13124945Seric 		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on);
13210347Seric #endif DEBUG
13310347Seric 
13425027Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on);
13525027Seric 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on);
13625027Seric 
13723120Seric 	if (bind(DaemonSocket, &SendmailAddress, sizeof SendmailAddress) < 0)
1389610Seric 	{
1399610Seric 		syserr("getrequests: cannot bind");
14010206Seric 		(void) close(DaemonSocket);
1419610Seric 		goto severe;
1429610Seric 	}
14323120Seric 	if (listen(DaemonSocket, 10) < 0)
14423120Seric 	{
14523120Seric 		syserr("getrequests: cannot listen");
14623120Seric 		(void) close(DaemonSocket);
14723120Seric 		goto severe;
14823120Seric 	}
1499610Seric 
15024955Seric 	(void) signal(SIGCHLD, reapchild);
15124945Seric 
1529610Seric # ifdef DEBUG
1539610Seric 	if (tTd(15, 1))
15410206Seric 		printf("getrequests: %d\n", DaemonSocket);
1559610Seric # endif DEBUG
1569610Seric 
1574631Seric 	for (;;)
1584631Seric 	{
15914875Seric 		register int pid;
16011147Seric 		auto int lotherend;
16111147Seric 		struct sockaddr_in otherend;
16214875Seric 		extern int RefuseLA;
16311147Seric 
16414875Seric 		/* see if we are rejecting connections */
16514875Seric 		while (getla() > RefuseLA)
16614875Seric 			sleep(5);
16714875Seric 
1689610Seric 		/* wait for a connection */
1699610Seric 		do
1709610Seric 		{
1719610Seric 			errno = 0;
1729610Seric 			lotherend = sizeof otherend;
17323120Seric 			t = accept(DaemonSocket, &otherend, &lotherend);
1749610Seric 		} while (t < 0 && errno == EINTR);
1759610Seric 		if (t < 0)
1765978Seric 		{
1779610Seric 			syserr("getrequests: accept");
1789610Seric 			sleep(5);
1799610Seric 			continue;
1805978Seric 		}
1814631Seric 
1825978Seric 		/*
1835978Seric 		**  Create a subprocess to process the mail.
1845978Seric 		*/
1855978Seric 
1865978Seric # ifdef DEBUG
1877677Seric 		if (tTd(15, 2))
1889610Seric 			printf("getrequests: forking (fd = %d)\n", t);
1895978Seric # endif DEBUG
1905978Seric 
1914636Seric 		pid = fork();
1924636Seric 		if (pid < 0)
1934631Seric 		{
1944636Seric 			syserr("daemon: cannot fork");
1954636Seric 			sleep(10);
1969610Seric 			(void) close(t);
1974636Seric 			continue;
1984631Seric 		}
1994631Seric 
2004636Seric 		if (pid == 0)
2014631Seric 		{
20211147Seric 			extern struct hostent *gethostbyaddr();
20311147Seric 			register struct hostent *hp;
20411147Seric 			char buf[MAXNAME];
20511147Seric 
2064636Seric 			/*
2074636Seric 			**  CHILD -- return to caller.
20811147Seric 			**	Collect verified idea of sending host.
2094636Seric 			**	Verify calling user id if possible here.
2104636Seric 			*/
2114631Seric 
21224955Seric 			(void) signal(SIGCHLD, SIG_DFL);
21324950Seric 
21411147Seric 			/* determine host name */
21525616Seric 			hp = gethostbyaddr((char *) &otherend.sin_addr, sizeof otherend.sin_addr, AF_INET);
21611147Seric 			if (hp != NULL)
21716890Seric 			{
21823104Seric 				(void) strcpy(buf, hp->h_name);
21916890Seric 				if (NetName != NULL && NetName[0] != '\0' &&
22016894Seric 				    index(hp->h_name, '.') == NULL)
22116890Seric 				{
22223104Seric 					(void) strcat(buf, ".");
22323104Seric 					(void) strcat(buf, NetName);
22416890Seric 				}
22516890Seric 			}
22611147Seric 			else
22716884Seric 			{
22816884Seric 				extern char *inet_ntoa();
22916884Seric 
23016884Seric 				/* produce a dotted quad */
23116884Seric 				(void) sprintf(buf, "[%s]",
23216884Seric 					inet_ntoa(otherend.sin_addr));
23316884Seric 			}
23416884Seric 
23516884Seric 			/* should we check for illegal connection here? XXX */
23616884Seric 
23711147Seric 			RealHostName = newstr(buf);
23811147Seric 
23910206Seric 			(void) close(DaemonSocket);
2409610Seric 			InChannel = fdopen(t, "r");
24121062Seric 			OutChannel = fdopen(dup(t), "w");
2425978Seric # ifdef DEBUG
2437677Seric 			if (tTd(15, 2))
2445978Seric 				printf("getreq: returning\n");
2455978Seric # endif DEBUG
2467876Seric # ifdef LOG
2477876Seric 			if (LogLevel > 11)
2487876Seric 				syslog(LOG_DEBUG, "connected, pid=%d", getpid());
2497876Seric # endif LOG
2504636Seric 			return;
2514631Seric 		}
2524631Seric 
2537117Seric 		/* close the port so that others will hang (for a while) */
2549610Seric 		(void) close(t);
2554631Seric 	}
2569886Seric 	/*NOTREACHED*/
2574631Seric }
2585978Seric /*
25910206Seric **  CLRDAEMON -- reset the daemon connection
26010206Seric **
26110206Seric **	Parameters:
26210206Seric **		none.
26310206Seric **
26410206Seric **	Returns:
26510206Seric **		none.
26610206Seric **
26710206Seric **	Side Effects:
26810206Seric **		releases any resources used by the passive daemon.
26910206Seric */
27010206Seric 
27110206Seric clrdaemon()
27210206Seric {
27310206Seric 	if (DaemonSocket >= 0)
27410206Seric 		(void) close(DaemonSocket);
27510206Seric 	DaemonSocket = -1;
27610206Seric }
27710206Seric /*
2786039Seric **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
2796039Seric **
2806039Seric **	Parameters:
2816039Seric **		host -- the name of the host.
2826633Seric **		port -- the port number to connect to.
2836039Seric **		outfile -- a pointer to a place to put the outfile
2846039Seric **			descriptor.
2856039Seric **		infile -- ditto for infile.
2866039Seric **
2876039Seric **	Returns:
2886039Seric **		An exit code telling whether the connection could be
2896039Seric **			made and if not why not.
2906039Seric **
2916039Seric **	Side Effects:
2926039Seric **		none.
2936039Seric */
2945978Seric 
29525475Smiriam int	h_errno;	/*this will go away when code implemented*/
29625475Smiriam 
2976633Seric makeconnection(host, port, outfile, infile)
2986039Seric 	char *host;
2997286Seric 	u_short port;
3006039Seric 	FILE **outfile;
3016039Seric 	FILE **infile;
3026039Seric {
3036039Seric 	register int s;
304*27744Sbloom 	int sav_errno;
3056039Seric 
3066039Seric 	/*
3076039Seric 	**  Set up the address for the mailer.
3089308Seric 	**	Accept "[a.b.c.d]" syntax for host name.
3096039Seric 	*/
3106039Seric 
31125475Smiriam 	h_errno = 0;
31225475Smiriam 	errno = 0;
31325475Smiriam 
3149308Seric 	if (host[0] == '[')
3159308Seric 	{
31611147Seric 		long hid;
31711147Seric 		register char *p = index(host, ']');
3189308Seric 
31911147Seric 		if (p != NULL)
3209308Seric 		{
32111147Seric 			*p = '\0';
32211147Seric 			hid = inet_addr(&host[1]);
32311147Seric 			*p = ']';
3249308Seric 		}
32511147Seric 		if (p == NULL || hid == -1)
3269308Seric 		{
3279308Seric 			usrerr("Invalid numeric domain spec \"%s\"", host);
3289308Seric 			return (EX_NOHOST);
3299308Seric 		}
3309308Seric 		SendmailAddress.sin_addr.s_addr = hid;
3319308Seric 	}
3329610Seric 	else
3339610Seric 	{
3349610Seric 		register struct hostent *hp = gethostbyname(host);
3359610Seric 
33625475Smiriam 		if (hp == NULL)
33724945Seric 		{
33825475Smiriam 			if (errno == ETIMEDOUT || h_errno == TRY_AGAIN)
33925475Smiriam 				return (EX_TEMPFAIL);
34025657Seric 
34125657Seric 			/*
34225657Seric 			**  XXX Should look for mail forwarder record here
34325657Seric 			**  XXX if (h_errno == NO_ADDRESS).
34425657Seric 			*/
34525657Seric 
34625475Smiriam 			return (EX_NOHOST);
34724945Seric 		}
34816884Seric 		bcopy(hp->h_addr, (char *) &SendmailAddress.sin_addr, hp->h_length);
3499610Seric 	}
3509610Seric 
3519610Seric 	/*
3529610Seric 	**  Determine the port number.
3539610Seric 	*/
3549610Seric 
35510011Seric 	if (port != 0)
35610011Seric 		SendmailAddress.sin_port = htons(port);
35710011Seric 	else
3589610Seric 	{
3599610Seric 		register struct servent *sp = getservbyname("smtp", "tcp");
3609610Seric 
3619610Seric 		if (sp == NULL)
3629610Seric 		{
3639610Seric 			syserr("makeconnection: server \"smtp\" unknown");
3649610Seric 			return (EX_OSFILE);
3659610Seric 		}
36610011Seric 		SendmailAddress.sin_port = sp->s_port;
3679610Seric 	}
3686039Seric 
3696039Seric 	/*
3706039Seric 	**  Try to actually open the connection.
3716039Seric 	*/
3726039Seric 
3736039Seric # ifdef DEBUG
3747677Seric 	if (tTd(16, 1))
3756039Seric 		printf("makeconnection (%s)\n", host);
3766039Seric # endif DEBUG
3776039Seric 
37823120Seric 	s = socket(AF_INET, SOCK_STREAM, 0);
3796039Seric 	if (s < 0)
3806039Seric 	{
3816039Seric 		syserr("makeconnection: no socket");
382*27744Sbloom 		sav_errno = errno;
3836039Seric 		goto failure;
3846039Seric 	}
3856039Seric 
3866039Seric # ifdef DEBUG
3877677Seric 	if (tTd(16, 1))
3886039Seric 		printf("makeconnection: %d\n", s);
38910347Seric 
39010347Seric 	/* turn on network debugging? */
39110347Seric 	if (tTd(16, 14))
39224945Seric 	{
39324945Seric 		int on = 1;
39424945Seric 		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on);
39524945Seric 	}
3966039Seric # endif DEBUG
3979546Seric 	(void) fflush(CurEnv->e_xfp);			/* for debugging */
39814383Seric 	errno = 0;					/* for debugging */
3999610Seric 	SendmailAddress.sin_family = AF_INET;
40023120Seric 	if (connect(s, &SendmailAddress, sizeof SendmailAddress) < 0)
4016039Seric 	{
402*27744Sbloom 		sav_errno = errno;
403*27744Sbloom 		(void) close(s);
4046039Seric 		/* failure, decide if temporary or not */
4056039Seric 	failure:
406*27744Sbloom 		switch (sav_errno)
4076039Seric 		{
4086039Seric 		  case EISCONN:
4096039Seric 		  case ETIMEDOUT:
4106897Seric 		  case EINPROGRESS:
4116897Seric 		  case EALREADY:
4126897Seric 		  case EADDRINUSE:
41310123Seric 		  case EHOSTDOWN:
4146897Seric 		  case ENETDOWN:
4156897Seric 		  case ENETRESET:
4166897Seric 		  case ENOBUFS:
4177204Seric 		  case ECONNREFUSED:
41811546Seric 		  case ECONNRESET:
41910081Seric 		  case EHOSTUNREACH:
42010098Seric 		  case ENETUNREACH:
4216039Seric 			/* there are others, I'm sure..... */
4226039Seric 			return (EX_TEMPFAIL);
4236039Seric 
42411147Seric 		  case EPERM:
42511147Seric 			/* why is this happening? */
42611147Seric 			syserr("makeconnection: funny failure, addr=%lx, port=%x",
42711147Seric 				SendmailAddress.sin_addr.s_addr, SendmailAddress.sin_port);
42814383Seric 			return (EX_TEMPFAIL);
42911147Seric 
4306039Seric 		  default:
4316039Seric 			return (EX_UNAVAILABLE);
4326039Seric 		}
4336039Seric 	}
4346039Seric 
4356039Seric 	/* connection ok, put it into canonical form */
4366039Seric 	*outfile = fdopen(s, "w");
4376039Seric 	*infile = fdopen(s, "r");
4386039Seric 
43910098Seric 	return (EX_OK);
4406039Seric }
44110758Seric /*
44210758Seric **  MYHOSTNAME -- return the name of this host.
44310758Seric **
44410758Seric **	Parameters:
44510758Seric **		hostbuf -- a place to return the name of this host.
44612313Seric **		size -- the size of hostbuf.
44710758Seric **
44810758Seric **	Returns:
44910758Seric **		A list of aliases for this host.
45010758Seric **
45110758Seric **	Side Effects:
45210758Seric **		none.
45310758Seric */
4546039Seric 
45510758Seric char **
45612313Seric myhostname(hostbuf, size)
45710758Seric 	char hostbuf[];
45812313Seric 	int size;
45910758Seric {
46010758Seric 	extern struct hostent *gethostbyname();
46111147Seric 	struct hostent *hp;
46210758Seric 
46323120Seric 	if (gethostname(hostbuf, size) < 0)
46423120Seric 	{
46523120Seric 		(void) strcpy(hostbuf, "localhost");
46623120Seric 	}
46711147Seric 	hp = gethostbyname(hostbuf);
46811147Seric 	if (hp != NULL)
46916877Seric 	{
47023104Seric 		(void) strcpy(hostbuf, hp->h_name);
47111147Seric 		return (hp->h_aliases);
47216877Seric 	}
47310758Seric 	else
47410758Seric 		return (NULL);
47510758Seric }
47616911Seric /*
47716911Seric **  MAPHOSTNAME -- turn a hostname into canonical form
47816911Seric **
47916911Seric **	Parameters:
48016911Seric **		hbuf -- a buffer containing a hostname.
48116911Seric **		hbsize -- the size of hbuf.
48216911Seric **
48316911Seric **	Returns:
48416911Seric **		none.
48516911Seric **
48616911Seric **	Side Effects:
48716911Seric **		Looks up the host specified in hbuf.  If it is not
48816911Seric **		the canonical name for that host, replace it with
48916911Seric **		the canonical name.  If the name is unknown, or it
49016911Seric **		is already the canonical name, leave it unchanged.
49116911Seric */
49210758Seric 
49316911Seric maphostname(hbuf, hbsize)
49416911Seric 	char *hbuf;
49516911Seric 	int hbsize;
49616911Seric {
49716911Seric 	register struct hostent *hp;
49816911Seric 	extern struct hostent *gethostbyname();
49916911Seric 
50025574Smiriam 	/*
50125616Seric 	**  If first character is a bracket, then it is an address
50225616Seric 	**  lookup.  Address is copied into a temporary buffer to
50325616Seric 	**  strip the brackets and to preserve hbuf if address is
50425616Seric 	**  unknown.
50525616Seric 	*/
50625574Smiriam 
50725574Smiriam 	if (*hbuf == '[')
50825574Smiriam 	{
50925574Smiriam 		extern struct hostent *gethostbyaddr();
51025574Smiriam 		u_long in_addr;
51125574Smiriam 		char ptr[256];
51225574Smiriam 		char *bptr;
51325574Smiriam 
51425616Seric 		(void) strcpy(ptr, hbuf);
51525574Smiriam 		bptr = index(ptr,']');
51625574Smiriam 		*bptr = '\0';
51725574Smiriam 		in_addr = inet_addr(&ptr[1]);
51825616Seric 		hp = gethostbyaddr((char *) &in_addr, sizeof(struct in_addr), AF_INET);
51925616Seric 		if (hp == NULL)
52025616Seric 			return;
52125574Smiriam 	}
52225574Smiriam 	else
52325574Smiriam 	{
52425574Smiriam 		makelower(hbuf);
52525574Smiriam 		hp = gethostbyname(hbuf);
52625574Smiriam 	}
52716911Seric 	if (hp != NULL)
52816911Seric 	{
52916911Seric 		int i = strlen(hp->h_name);
53016911Seric 
53116911Seric 		if (i >= hbsize)
53216911Seric 			hp->h_name[--i] = '\0';
53323104Seric 		(void) strcpy(hbuf, hp->h_name);
53416911Seric 	}
53516911Seric }
53616911Seric 
53710758Seric # else DAEMON
53816911Seric /* code for systems without sophisticated networking */
53910758Seric 
54010758Seric /*
54110758Seric **  MYHOSTNAME -- stub version for case of no daemon code.
54211297Seric **
54311297Seric **	Can't convert to upper case here because might be a UUCP name.
54412313Seric **
54512313Seric **	Mark, you can change this to be anything you want......
54610758Seric */
54710758Seric 
54810758Seric char **
54912313Seric myhostname(hostbuf, size)
55010758Seric 	char hostbuf[];
55112313Seric 	int size;
55210758Seric {
55310758Seric 	register FILE *f;
55410758Seric 
55510758Seric 	hostbuf[0] = '\0';
55610758Seric 	f = fopen("/usr/include/whoami", "r");
55710758Seric 	if (f != NULL)
55810758Seric 	{
55912313Seric 		(void) fgets(hostbuf, size, f);
56010758Seric 		fixcrlf(hostbuf, TRUE);
56110758Seric 		(void) fclose(f);
56210758Seric 	}
56310758Seric 	return (NULL);
56410758Seric }
56516911Seric /*
56616911Seric **  MAPHOSTNAME -- turn a hostname into canonical form
56716911Seric **
56816911Seric **	Parameters:
56916911Seric **		hbuf -- a buffer containing a hostname.
57016911Seric **		hbsize -- the size of hbuf.
57116911Seric **
57216911Seric **	Returns:
57316911Seric **		none.
57416911Seric **
57516911Seric **	Side Effects:
57616911Seric **		Looks up the host specified in hbuf.  If it is not
57716911Seric **		the canonical name for that host, replace it with
57816911Seric **		the canonical name.  If the name is unknown, or it
57916911Seric **		is already the canonical name, leave it unchanged.
58016911Seric */
58110758Seric 
58216911Seric /*ARGSUSED*/
58316911Seric maphostname(hbuf, hbsize)
58416911Seric 	char *hbuf;
58516911Seric 	int hbsize;
58616911Seric {
58716911Seric 	return;
58816911Seric }
58916911Seric 
5905978Seric #endif DAEMON
591