xref: /csrg-svn/usr.sbin/sendmail/src/daemon.c (revision 24945)
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*24945Seric static char	SccsId[] = "@(#)daemon.c	5.9 (Berkeley) 09/19/85	(w/o daemon mode)";
1823120Seric # endif not lint
1923120Seric # else
204535Seric 
2123120Seric # include <netdb.h>
22*24945Seric # include <sys/signal.h>
2323120Seric # include <sys/wait.h>
2423120Seric # include <sys/time.h>
2523120Seric # include <sys/resource.h>
265978Seric 
2723120Seric # ifndef lint
28*24945Seric static char	SccsId[] = "@(#)daemon.c	5.9 (Berkeley) 09/19/85 (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.
577556Seric **
587556Seric **	The semantics of both of these should be clean.
594535Seric */
604535Seric /*
614535Seric **  GETREQUESTS -- open mail IPC port and get requests.
624535Seric **
634535Seric **	Parameters:
644535Seric **		none.
654535Seric **
664535Seric **	Returns:
674535Seric **		none.
684535Seric **
694535Seric **	Side Effects:
704535Seric **		Waits until some interesting activity occurs.  When
714535Seric **		it does, a child is created to process it, and the
724535Seric **		parent waits for completion.  Return from this
739886Seric **		routine is always in the child.  The file pointers
749886Seric **		"InChannel" and "OutChannel" should be set to point
759886Seric **		to the communication channel.
764535Seric */
774535Seric 
7810206Seric struct sockaddr_in	SendmailAddress;/* internet address of sendmail */
799610Seric 
8016144Seric int	DaemonSocket	= -1;		/* fd describing socket */
8116890Seric char	*NetName;			/* name of home (local?) network */
8216144Seric 
834535Seric getrequests()
844535Seric {
859610Seric 	int t;
869610Seric 	register struct servent *sp;
87*24945Seric 	extern reapchild();
887117Seric 
899610Seric 	/*
909610Seric 	**  Set up the address for the mailer.
919610Seric 	*/
929610Seric 
939610Seric 	sp = getservbyname("smtp", "tcp");
949610Seric 	if (sp == NULL)
959610Seric 	{
969610Seric 		syserr("server \"smtp\" unknown");
9710167Seric 		goto severe;
989610Seric 	}
999610Seric 	SendmailAddress.sin_family = AF_INET;
1009610Seric 	SendmailAddress.sin_addr.s_addr = INADDR_ANY;
1019740Ssam 	SendmailAddress.sin_port = sp->s_port;
1029610Seric 
1039610Seric 	/*
1049610Seric 	**  Try to actually open the connection.
1059610Seric 	*/
1069610Seric 
1079610Seric # ifdef DEBUG
1089610Seric 	if (tTd(15, 1))
1099610Seric 		printf("getrequests: port 0x%x\n", SendmailAddress.sin_port);
1109610Seric # endif DEBUG
1119610Seric 
1129610Seric 	/* get a socket for the SMTP connection */
11323120Seric 	DaemonSocket = socket(AF_INET, SOCK_STREAM, 0);
11410206Seric 	if (DaemonSocket < 0)
1159610Seric 	{
1169610Seric 		/* probably another daemon already */
1179610Seric 		syserr("getrequests: can't create socket");
1189610Seric 	  severe:
1199610Seric # ifdef LOG
1209610Seric 		if (LogLevel > 0)
12124858Seric 			syslog(LOG_ALERT, "cannot get connection");
1229610Seric # endif LOG
1239610Seric 		finis();
1249610Seric 	}
12510347Seric 
12610347Seric #ifdef DEBUG
12710347Seric 	/* turn on network debugging? */
12810347Seric 	if (tTd(15, 15))
129*24945Seric 	{
130*24945Seric 		int on = 1;
131*24945Seric 		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on);
132*24945Seric 	}
13310347Seric #endif DEBUG
13410347Seric 
13523120Seric 	if (bind(DaemonSocket, &SendmailAddress, sizeof SendmailAddress) < 0)
1369610Seric 	{
1379610Seric 		syserr("getrequests: cannot bind");
13810206Seric 		(void) close(DaemonSocket);
1399610Seric 		goto severe;
1409610Seric 	}
14123120Seric 	if (listen(DaemonSocket, 10) < 0)
14223120Seric 	{
14323120Seric 		syserr("getrequests: cannot listen");
14423120Seric 		(void) close(DaemonSocket);
14523120Seric 		goto severe;
14623120Seric 	}
1479610Seric 
148*24945Seric 	signal(SIGCHLD, reapchild);
149*24945Seric 
1509610Seric # ifdef DEBUG
1519610Seric 	if (tTd(15, 1))
15210206Seric 		printf("getrequests: %d\n", DaemonSocket);
1539610Seric # endif DEBUG
1549610Seric 
1554631Seric 	for (;;)
1564631Seric 	{
15714875Seric 		register int pid;
15811147Seric 		auto int lotherend;
15911147Seric 		struct sockaddr_in otherend;
16014875Seric 		extern int RefuseLA;
16111147Seric 
16214875Seric 		/* see if we are rejecting connections */
16314875Seric 		while (getla() > RefuseLA)
16414875Seric 			sleep(5);
16514875Seric 
1669610Seric 		/* wait for a connection */
1679610Seric 		do
1689610Seric 		{
1699610Seric 			errno = 0;
1709610Seric 			lotherend = sizeof otherend;
17123120Seric 			t = accept(DaemonSocket, &otherend, &lotherend);
1729610Seric 		} while (t < 0 && errno == EINTR);
1739610Seric 		if (t < 0)
1745978Seric 		{
1759610Seric 			syserr("getrequests: accept");
1769610Seric 			sleep(5);
1779610Seric 			continue;
1785978Seric 		}
1794631Seric 
1805978Seric 		/*
1815978Seric 		**  Create a subprocess to process the mail.
1825978Seric 		*/
1835978Seric 
1845978Seric # ifdef DEBUG
1857677Seric 		if (tTd(15, 2))
1869610Seric 			printf("getrequests: forking (fd = %d)\n", t);
1875978Seric # endif DEBUG
1885978Seric 
1894636Seric 		pid = fork();
1904636Seric 		if (pid < 0)
1914631Seric 		{
1924636Seric 			syserr("daemon: cannot fork");
1934636Seric 			sleep(10);
1949610Seric 			(void) close(t);
1954636Seric 			continue;
1964631Seric 		}
1974631Seric 
1984636Seric 		if (pid == 0)
1994631Seric 		{
20011147Seric 			extern struct hostent *gethostbyaddr();
20111147Seric 			register struct hostent *hp;
20211147Seric 			char buf[MAXNAME];
20311147Seric 
2044636Seric 			/*
2054636Seric 			**  CHILD -- return to caller.
20611147Seric 			**	Collect verified idea of sending host.
2074636Seric 			**	Verify calling user id if possible here.
2084636Seric 			*/
2094631Seric 
21011147Seric 			/* determine host name */
21111147Seric 			hp = gethostbyaddr(&otherend.sin_addr, sizeof otherend.sin_addr, AF_INET);
21211147Seric 			if (hp != NULL)
21316890Seric 			{
21423104Seric 				(void) strcpy(buf, hp->h_name);
21516890Seric 				if (NetName != NULL && NetName[0] != '\0' &&
21616894Seric 				    index(hp->h_name, '.') == NULL)
21716890Seric 				{
21823104Seric 					(void) strcat(buf, ".");
21923104Seric 					(void) strcat(buf, NetName);
22016890Seric 				}
22116890Seric 			}
22211147Seric 			else
22316884Seric 			{
22416884Seric 				extern char *inet_ntoa();
22516884Seric 
22616884Seric 				/* produce a dotted quad */
22716884Seric 				(void) sprintf(buf, "[%s]",
22816884Seric 					inet_ntoa(otherend.sin_addr));
22916884Seric 			}
23016884Seric 
23116884Seric 			/* should we check for illegal connection here? XXX */
23216884Seric 
23311147Seric 			RealHostName = newstr(buf);
23411147Seric 
23510206Seric 			(void) close(DaemonSocket);
2369610Seric 			InChannel = fdopen(t, "r");
23721062Seric 			OutChannel = fdopen(dup(t), "w");
2385978Seric # ifdef DEBUG
2397677Seric 			if (tTd(15, 2))
2405978Seric 				printf("getreq: returning\n");
2415978Seric # endif DEBUG
2427876Seric # ifdef LOG
2437876Seric 			if (LogLevel > 11)
2447876Seric 				syslog(LOG_DEBUG, "connected, pid=%d", getpid());
2457876Seric # endif LOG
2464636Seric 			return;
2474631Seric 		}
2484631Seric 
2497117Seric 		/* close the port so that others will hang (for a while) */
2509610Seric 		(void) close(t);
2514631Seric 	}
2529886Seric 	/*NOTREACHED*/
2534631Seric }
2545978Seric /*
255*24945Seric **  REAPCHILD -- pick up the body of my child, lest it become a zombie
256*24945Seric **
257*24945Seric **	Parameters:
258*24945Seric **		none.
259*24945Seric **
260*24945Seric **	Returns:
261*24945Seric **		none.
262*24945Seric **
263*24945Seric **	Side Effects:
264*24945Seric **		Picks up zombies.
265*24945Seric */
266*24945Seric 
267*24945Seric reapchild()
268*24945Seric {
269*24945Seric 	union wait status;
270*24945Seric 
271*24945Seric 	while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0)
272*24945Seric 		continue;
273*24945Seric }
274*24945Seric /*
27510206Seric **  CLRDAEMON -- reset the daemon connection
27610206Seric **
27710206Seric **	Parameters:
27810206Seric **		none.
27910206Seric **
28010206Seric **	Returns:
28110206Seric **		none.
28210206Seric **
28310206Seric **	Side Effects:
28410206Seric **		releases any resources used by the passive daemon.
28510206Seric */
28610206Seric 
28710206Seric clrdaemon()
28810206Seric {
28910206Seric 	if (DaemonSocket >= 0)
29010206Seric 		(void) close(DaemonSocket);
29110206Seric 	DaemonSocket = -1;
29210206Seric }
29310206Seric /*
2946039Seric **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
2956039Seric **
2966039Seric **	Parameters:
2976039Seric **		host -- the name of the host.
2986633Seric **		port -- the port number to connect to.
2996039Seric **		outfile -- a pointer to a place to put the outfile
3006039Seric **			descriptor.
3016039Seric **		infile -- ditto for infile.
3026039Seric **
3036039Seric **	Returns:
3046039Seric **		An exit code telling whether the connection could be
3056039Seric **			made and if not why not.
3066039Seric **
3076039Seric **	Side Effects:
3086039Seric **		none.
3096039Seric */
3105978Seric 
3116633Seric makeconnection(host, port, outfile, infile)
3126039Seric 	char *host;
3137286Seric 	u_short port;
3146039Seric 	FILE **outfile;
3156039Seric 	FILE **infile;
3166039Seric {
3176039Seric 	register int s;
3186039Seric 
3196039Seric 	/*
3206039Seric 	**  Set up the address for the mailer.
3219308Seric 	**	Accept "[a.b.c.d]" syntax for host name.
3226039Seric 	*/
3236039Seric 
3249308Seric 	if (host[0] == '[')
3259308Seric 	{
32611147Seric 		long hid;
32711147Seric 		register char *p = index(host, ']');
3289308Seric 
32911147Seric 		if (p != NULL)
3309308Seric 		{
33111147Seric 			*p = '\0';
33211147Seric 			hid = inet_addr(&host[1]);
33311147Seric 			*p = ']';
3349308Seric 		}
33511147Seric 		if (p == NULL || hid == -1)
3369308Seric 		{
3379308Seric 			usrerr("Invalid numeric domain spec \"%s\"", host);
3389308Seric 			return (EX_NOHOST);
3399308Seric 		}
3409308Seric 		SendmailAddress.sin_addr.s_addr = hid;
3419308Seric 	}
3429610Seric 	else
3439610Seric 	{
3449610Seric 		register struct hostent *hp = gethostbyname(host);
3459610Seric 
346*24945Seric 		if (errno == ETIMEDOUT)
347*24945Seric 		{
348*24945Seric 			CurEnv->e_flags &= ~EF_FATALERRS;
349*24945Seric 			return (EX_TEMPFAIL);
350*24945Seric 		}
35111147Seric 		if (hp == NULL)
3529610Seric 			return (EX_NOHOST);
35316884Seric 		bcopy(hp->h_addr, (char *) &SendmailAddress.sin_addr, hp->h_length);
3549610Seric 	}
3559610Seric 
3569610Seric 	/*
3579610Seric 	**  Determine the port number.
3589610Seric 	*/
3599610Seric 
36010011Seric 	if (port != 0)
36110011Seric 		SendmailAddress.sin_port = htons(port);
36210011Seric 	else
3639610Seric 	{
3649610Seric 		register struct servent *sp = getservbyname("smtp", "tcp");
3659610Seric 
3669610Seric 		if (sp == NULL)
3679610Seric 		{
3689610Seric 			syserr("makeconnection: server \"smtp\" unknown");
3699610Seric 			return (EX_OSFILE);
3709610Seric 		}
37110011Seric 		SendmailAddress.sin_port = sp->s_port;
3729610Seric 	}
3736039Seric 
3746039Seric 	/*
3756039Seric 	**  Try to actually open the connection.
3766039Seric 	*/
3776039Seric 
3786039Seric # ifdef DEBUG
3797677Seric 	if (tTd(16, 1))
3806039Seric 		printf("makeconnection (%s)\n", host);
3816039Seric # endif DEBUG
3826039Seric 
38323120Seric 	s = socket(AF_INET, SOCK_STREAM, 0);
3846039Seric 	if (s < 0)
3856039Seric 	{
3866039Seric 		syserr("makeconnection: no socket");
3876039Seric 		goto failure;
3886039Seric 	}
3896039Seric 
3906039Seric # ifdef DEBUG
3917677Seric 	if (tTd(16, 1))
3926039Seric 		printf("makeconnection: %d\n", s);
39310347Seric 
39410347Seric 	/* turn on network debugging? */
39510347Seric 	if (tTd(16, 14))
396*24945Seric 	{
397*24945Seric 		int on = 1;
398*24945Seric 		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on);
399*24945Seric 	}
4006039Seric # endif DEBUG
4019546Seric 	(void) fflush(CurEnv->e_xfp);			/* for debugging */
40214383Seric 	errno = 0;					/* for debugging */
4039610Seric 	SendmailAddress.sin_family = AF_INET;
40423120Seric 	if (connect(s, &SendmailAddress, sizeof SendmailAddress) < 0)
4056039Seric 	{
4066039Seric 		/* failure, decide if temporary or not */
4076039Seric 	failure:
4086039Seric 		switch (errno)
4096039Seric 		{
4106039Seric 		  case EISCONN:
4116039Seric 		  case ETIMEDOUT:
4126897Seric 		  case EINPROGRESS:
4136897Seric 		  case EALREADY:
4146897Seric 		  case EADDRINUSE:
41510123Seric 		  case EHOSTDOWN:
4166897Seric 		  case ENETDOWN:
4176897Seric 		  case ENETRESET:
4186897Seric 		  case ENOBUFS:
4197204Seric 		  case ECONNREFUSED:
42011546Seric 		  case ECONNRESET:
42110081Seric 		  case EHOSTUNREACH:
42210098Seric 		  case ENETUNREACH:
4236039Seric 			/* there are others, I'm sure..... */
42416884Seric 			CurEnv->e_flags &= ~EF_FATALERRS;
4256039Seric 			return (EX_TEMPFAIL);
4266039Seric 
42711147Seric 		  case EPERM:
42811147Seric 			/* why is this happening? */
42911147Seric 			syserr("makeconnection: funny failure, addr=%lx, port=%x",
43011147Seric 				SendmailAddress.sin_addr.s_addr, SendmailAddress.sin_port);
43114383Seric 			return (EX_TEMPFAIL);
43211147Seric 
4336039Seric 		  default:
4346039Seric 			return (EX_UNAVAILABLE);
4356039Seric 		}
4366039Seric 	}
4376039Seric 
4386039Seric 	/* connection ok, put it into canonical form */
4396039Seric 	*outfile = fdopen(s, "w");
4406039Seric 	*infile = fdopen(s, "r");
4416039Seric 
44210098Seric 	return (EX_OK);
4436039Seric }
44410758Seric /*
44510758Seric **  MYHOSTNAME -- return the name of this host.
44610758Seric **
44710758Seric **	Parameters:
44810758Seric **		hostbuf -- a place to return the name of this host.
44912313Seric **		size -- the size of hostbuf.
45010758Seric **
45110758Seric **	Returns:
45210758Seric **		A list of aliases for this host.
45310758Seric **
45410758Seric **	Side Effects:
45510758Seric **		none.
45610758Seric */
4576039Seric 
45810758Seric char **
45912313Seric myhostname(hostbuf, size)
46010758Seric 	char hostbuf[];
46112313Seric 	int size;
46210758Seric {
46310758Seric 	extern struct hostent *gethostbyname();
46411147Seric 	struct hostent *hp;
46510758Seric 
46623120Seric 	if (gethostname(hostbuf, size) < 0)
46723120Seric 	{
46823120Seric 		(void) strcpy(hostbuf, "localhost");
46923120Seric 	}
47011147Seric 	hp = gethostbyname(hostbuf);
47111147Seric 	if (hp != NULL)
47216877Seric 	{
47323104Seric 		(void) strcpy(hostbuf, hp->h_name);
47411147Seric 		return (hp->h_aliases);
47516877Seric 	}
47610758Seric 	else
47710758Seric 		return (NULL);
47810758Seric }
47916911Seric /*
48016911Seric **  MAPHOSTNAME -- turn a hostname into canonical form
48116911Seric **
48216911Seric **	Parameters:
48316911Seric **		hbuf -- a buffer containing a hostname.
48416911Seric **		hbsize -- the size of hbuf.
48516911Seric **
48616911Seric **	Returns:
48716911Seric **		none.
48816911Seric **
48916911Seric **	Side Effects:
49016911Seric **		Looks up the host specified in hbuf.  If it is not
49116911Seric **		the canonical name for that host, replace it with
49216911Seric **		the canonical name.  If the name is unknown, or it
49316911Seric **		is already the canonical name, leave it unchanged.
49416911Seric */
49510758Seric 
49616911Seric maphostname(hbuf, hbsize)
49716911Seric 	char *hbuf;
49816911Seric 	int hbsize;
49916911Seric {
50016911Seric 	register struct hostent *hp;
50116911Seric 	extern struct hostent *gethostbyname();
50216911Seric 
50316911Seric 	makelower(hbuf);
50416911Seric 	hp = gethostbyname(hbuf);
50516911Seric 	if (hp != NULL)
50616911Seric 	{
50716911Seric 		int i = strlen(hp->h_name);
50816911Seric 
50916911Seric 		if (i >= hbsize)
51016911Seric 			hp->h_name[--i] = '\0';
51123104Seric 		(void) strcpy(hbuf, hp->h_name);
51216911Seric 	}
51316911Seric }
51416911Seric 
51510758Seric # else DAEMON
51616911Seric /* code for systems without sophisticated networking */
51710758Seric 
51810758Seric /*
51910758Seric **  MYHOSTNAME -- stub version for case of no daemon code.
52011297Seric **
52111297Seric **	Can't convert to upper case here because might be a UUCP name.
52212313Seric **
52312313Seric **	Mark, you can change this to be anything you want......
52410758Seric */
52510758Seric 
52610758Seric char **
52712313Seric myhostname(hostbuf, size)
52810758Seric 	char hostbuf[];
52912313Seric 	int size;
53010758Seric {
53110758Seric 	register FILE *f;
53210758Seric 
53310758Seric 	hostbuf[0] = '\0';
53410758Seric 	f = fopen("/usr/include/whoami", "r");
53510758Seric 	if (f != NULL)
53610758Seric 	{
53712313Seric 		(void) fgets(hostbuf, size, f);
53810758Seric 		fixcrlf(hostbuf, TRUE);
53910758Seric 		(void) fclose(f);
54010758Seric 	}
54110758Seric 	return (NULL);
54210758Seric }
54316911Seric /*
54416911Seric **  MAPHOSTNAME -- turn a hostname into canonical form
54516911Seric **
54616911Seric **	Parameters:
54716911Seric **		hbuf -- a buffer containing a hostname.
54816911Seric **		hbsize -- the size of hbuf.
54916911Seric **
55016911Seric **	Returns:
55116911Seric **		none.
55216911Seric **
55316911Seric **	Side Effects:
55416911Seric **		Looks up the host specified in hbuf.  If it is not
55516911Seric **		the canonical name for that host, replace it with
55616911Seric **		the canonical name.  If the name is unknown, or it
55716911Seric **		is already the canonical name, leave it unchanged.
55816911Seric */
55910758Seric 
56016911Seric /*ARGSUSED*/
56116911Seric maphostname(hbuf, hbsize)
56216911Seric 	char *hbuf;
56316911Seric 	int hbsize;
56416911Seric {
56516911Seric 	return;
56616911Seric }
56716911Seric 
56816911Seric 
5695978Seric #endif DAEMON
570