xref: /csrg-svn/usr.sbin/sendmail/src/daemon.c (revision 24950)
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*24950Seric static char	SccsId[] = "@(#)daemon.c	5.10 (Berkeley) 09/19/85	(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*24950Seric static char	SccsId[] = "@(#)daemon.c	5.10 (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;
8724945Seric 	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))
12924945Seric 	{
13024945Seric 		int on = 1;
13124945Seric 		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on);
13224945Seric 	}
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 
14824945Seric 	signal(SIGCHLD, reapchild);
14924945Seric 
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 
210*24950Seric 			signal(SIGCHLD, SIG_DFL);
211*24950Seric 
21211147Seric 			/* determine host name */
21311147Seric 			hp = gethostbyaddr(&otherend.sin_addr, sizeof otherend.sin_addr, AF_INET);
21411147Seric 			if (hp != NULL)
21516890Seric 			{
21623104Seric 				(void) strcpy(buf, hp->h_name);
21716890Seric 				if (NetName != NULL && NetName[0] != '\0' &&
21816894Seric 				    index(hp->h_name, '.') == NULL)
21916890Seric 				{
22023104Seric 					(void) strcat(buf, ".");
22123104Seric 					(void) strcat(buf, NetName);
22216890Seric 				}
22316890Seric 			}
22411147Seric 			else
22516884Seric 			{
22616884Seric 				extern char *inet_ntoa();
22716884Seric 
22816884Seric 				/* produce a dotted quad */
22916884Seric 				(void) sprintf(buf, "[%s]",
23016884Seric 					inet_ntoa(otherend.sin_addr));
23116884Seric 			}
23216884Seric 
23316884Seric 			/* should we check for illegal connection here? XXX */
23416884Seric 
23511147Seric 			RealHostName = newstr(buf);
23611147Seric 
23710206Seric 			(void) close(DaemonSocket);
2389610Seric 			InChannel = fdopen(t, "r");
23921062Seric 			OutChannel = fdopen(dup(t), "w");
2405978Seric # ifdef DEBUG
2417677Seric 			if (tTd(15, 2))
2425978Seric 				printf("getreq: returning\n");
2435978Seric # endif DEBUG
2447876Seric # ifdef LOG
2457876Seric 			if (LogLevel > 11)
2467876Seric 				syslog(LOG_DEBUG, "connected, pid=%d", getpid());
2477876Seric # endif LOG
2484636Seric 			return;
2494631Seric 		}
2504631Seric 
2517117Seric 		/* close the port so that others will hang (for a while) */
2529610Seric 		(void) close(t);
2534631Seric 	}
2549886Seric 	/*NOTREACHED*/
2554631Seric }
2565978Seric /*
25724945Seric **  REAPCHILD -- pick up the body of my child, lest it become a zombie
25824945Seric **
25924945Seric **	Parameters:
26024945Seric **		none.
26124945Seric **
26224945Seric **	Returns:
26324945Seric **		none.
26424945Seric **
26524945Seric **	Side Effects:
26624945Seric **		Picks up zombies.
26724945Seric */
26824945Seric 
26924945Seric reapchild()
27024945Seric {
27124945Seric 	union wait status;
27224945Seric 
27324945Seric 	while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0)
27424945Seric 		continue;
27524945Seric }
27624945Seric /*
27710206Seric **  CLRDAEMON -- reset the daemon connection
27810206Seric **
27910206Seric **	Parameters:
28010206Seric **		none.
28110206Seric **
28210206Seric **	Returns:
28310206Seric **		none.
28410206Seric **
28510206Seric **	Side Effects:
28610206Seric **		releases any resources used by the passive daemon.
28710206Seric */
28810206Seric 
28910206Seric clrdaemon()
29010206Seric {
29110206Seric 	if (DaemonSocket >= 0)
29210206Seric 		(void) close(DaemonSocket);
29310206Seric 	DaemonSocket = -1;
29410206Seric }
29510206Seric /*
2966039Seric **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
2976039Seric **
2986039Seric **	Parameters:
2996039Seric **		host -- the name of the host.
3006633Seric **		port -- the port number to connect to.
3016039Seric **		outfile -- a pointer to a place to put the outfile
3026039Seric **			descriptor.
3036039Seric **		infile -- ditto for infile.
3046039Seric **
3056039Seric **	Returns:
3066039Seric **		An exit code telling whether the connection could be
3076039Seric **			made and if not why not.
3086039Seric **
3096039Seric **	Side Effects:
3106039Seric **		none.
3116039Seric */
3125978Seric 
3136633Seric makeconnection(host, port, outfile, infile)
3146039Seric 	char *host;
3157286Seric 	u_short port;
3166039Seric 	FILE **outfile;
3176039Seric 	FILE **infile;
3186039Seric {
3196039Seric 	register int s;
3206039Seric 
3216039Seric 	/*
3226039Seric 	**  Set up the address for the mailer.
3239308Seric 	**	Accept "[a.b.c.d]" syntax for host name.
3246039Seric 	*/
3256039Seric 
3269308Seric 	if (host[0] == '[')
3279308Seric 	{
32811147Seric 		long hid;
32911147Seric 		register char *p = index(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 		}
3429308Seric 		SendmailAddress.sin_addr.s_addr = hid;
3439308Seric 	}
3449610Seric 	else
3459610Seric 	{
3469610Seric 		register struct hostent *hp = gethostbyname(host);
3479610Seric 
34824945Seric 		if (errno == ETIMEDOUT)
34924945Seric 		{
35024945Seric 			CurEnv->e_flags &= ~EF_FATALERRS;
35124945Seric 			return (EX_TEMPFAIL);
35224945Seric 		}
35311147Seric 		if (hp == NULL)
3549610Seric 			return (EX_NOHOST);
35516884Seric 		bcopy(hp->h_addr, (char *) &SendmailAddress.sin_addr, hp->h_length);
3569610Seric 	}
3579610Seric 
3589610Seric 	/*
3599610Seric 	**  Determine the port number.
3609610Seric 	*/
3619610Seric 
36210011Seric 	if (port != 0)
36310011Seric 		SendmailAddress.sin_port = htons(port);
36410011Seric 	else
3659610Seric 	{
3669610Seric 		register struct servent *sp = getservbyname("smtp", "tcp");
3679610Seric 
3689610Seric 		if (sp == NULL)
3699610Seric 		{
3709610Seric 			syserr("makeconnection: server \"smtp\" unknown");
3719610Seric 			return (EX_OSFILE);
3729610Seric 		}
37310011Seric 		SendmailAddress.sin_port = sp->s_port;
3749610Seric 	}
3756039Seric 
3766039Seric 	/*
3776039Seric 	**  Try to actually open the connection.
3786039Seric 	*/
3796039Seric 
3806039Seric # ifdef DEBUG
3817677Seric 	if (tTd(16, 1))
3826039Seric 		printf("makeconnection (%s)\n", host);
3836039Seric # endif DEBUG
3846039Seric 
38523120Seric 	s = socket(AF_INET, SOCK_STREAM, 0);
3866039Seric 	if (s < 0)
3876039Seric 	{
3886039Seric 		syserr("makeconnection: no socket");
3896039Seric 		goto failure;
3906039Seric 	}
3916039Seric 
3926039Seric # ifdef DEBUG
3937677Seric 	if (tTd(16, 1))
3946039Seric 		printf("makeconnection: %d\n", s);
39510347Seric 
39610347Seric 	/* turn on network debugging? */
39710347Seric 	if (tTd(16, 14))
39824945Seric 	{
39924945Seric 		int on = 1;
40024945Seric 		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on);
40124945Seric 	}
4026039Seric # endif DEBUG
4039546Seric 	(void) fflush(CurEnv->e_xfp);			/* for debugging */
40414383Seric 	errno = 0;					/* for debugging */
4059610Seric 	SendmailAddress.sin_family = AF_INET;
40623120Seric 	if (connect(s, &SendmailAddress, sizeof SendmailAddress) < 0)
4076039Seric 	{
4086039Seric 		/* failure, decide if temporary or not */
4096039Seric 	failure:
4106039Seric 		switch (errno)
4116039Seric 		{
4126039Seric 		  case EISCONN:
4136039Seric 		  case ETIMEDOUT:
4146897Seric 		  case EINPROGRESS:
4156897Seric 		  case EALREADY:
4166897Seric 		  case EADDRINUSE:
41710123Seric 		  case EHOSTDOWN:
4186897Seric 		  case ENETDOWN:
4196897Seric 		  case ENETRESET:
4206897Seric 		  case ENOBUFS:
4217204Seric 		  case ECONNREFUSED:
42211546Seric 		  case ECONNRESET:
42310081Seric 		  case EHOSTUNREACH:
42410098Seric 		  case ENETUNREACH:
4256039Seric 			/* there are others, I'm sure..... */
42616884Seric 			CurEnv->e_flags &= ~EF_FATALERRS;
4276039Seric 			return (EX_TEMPFAIL);
4286039Seric 
42911147Seric 		  case EPERM:
43011147Seric 			/* why is this happening? */
43111147Seric 			syserr("makeconnection: funny failure, addr=%lx, port=%x",
43211147Seric 				SendmailAddress.sin_addr.s_addr, SendmailAddress.sin_port);
43314383Seric 			return (EX_TEMPFAIL);
43411147Seric 
4356039Seric 		  default:
4366039Seric 			return (EX_UNAVAILABLE);
4376039Seric 		}
4386039Seric 	}
4396039Seric 
4406039Seric 	/* connection ok, put it into canonical form */
4416039Seric 	*outfile = fdopen(s, "w");
4426039Seric 	*infile = fdopen(s, "r");
4436039Seric 
44410098Seric 	return (EX_OK);
4456039Seric }
44610758Seric /*
44710758Seric **  MYHOSTNAME -- return the name of this host.
44810758Seric **
44910758Seric **	Parameters:
45010758Seric **		hostbuf -- a place to return the name of this host.
45112313Seric **		size -- the size of hostbuf.
45210758Seric **
45310758Seric **	Returns:
45410758Seric **		A list of aliases for this host.
45510758Seric **
45610758Seric **	Side Effects:
45710758Seric **		none.
45810758Seric */
4596039Seric 
46010758Seric char **
46112313Seric myhostname(hostbuf, size)
46210758Seric 	char hostbuf[];
46312313Seric 	int size;
46410758Seric {
46510758Seric 	extern struct hostent *gethostbyname();
46611147Seric 	struct hostent *hp;
46710758Seric 
46823120Seric 	if (gethostname(hostbuf, size) < 0)
46923120Seric 	{
47023120Seric 		(void) strcpy(hostbuf, "localhost");
47123120Seric 	}
47211147Seric 	hp = gethostbyname(hostbuf);
47311147Seric 	if (hp != NULL)
47416877Seric 	{
47523104Seric 		(void) strcpy(hostbuf, hp->h_name);
47611147Seric 		return (hp->h_aliases);
47716877Seric 	}
47810758Seric 	else
47910758Seric 		return (NULL);
48010758Seric }
48116911Seric /*
48216911Seric **  MAPHOSTNAME -- turn a hostname into canonical form
48316911Seric **
48416911Seric **	Parameters:
48516911Seric **		hbuf -- a buffer containing a hostname.
48616911Seric **		hbsize -- the size of hbuf.
48716911Seric **
48816911Seric **	Returns:
48916911Seric **		none.
49016911Seric **
49116911Seric **	Side Effects:
49216911Seric **		Looks up the host specified in hbuf.  If it is not
49316911Seric **		the canonical name for that host, replace it with
49416911Seric **		the canonical name.  If the name is unknown, or it
49516911Seric **		is already the canonical name, leave it unchanged.
49616911Seric */
49710758Seric 
49816911Seric maphostname(hbuf, hbsize)
49916911Seric 	char *hbuf;
50016911Seric 	int hbsize;
50116911Seric {
50216911Seric 	register struct hostent *hp;
50316911Seric 	extern struct hostent *gethostbyname();
50416911Seric 
50516911Seric 	makelower(hbuf);
50616911Seric 	hp = gethostbyname(hbuf);
50716911Seric 	if (hp != NULL)
50816911Seric 	{
50916911Seric 		int i = strlen(hp->h_name);
51016911Seric 
51116911Seric 		if (i >= hbsize)
51216911Seric 			hp->h_name[--i] = '\0';
51323104Seric 		(void) strcpy(hbuf, hp->h_name);
51416911Seric 	}
51516911Seric }
51616911Seric 
51710758Seric # else DAEMON
51816911Seric /* code for systems without sophisticated networking */
51910758Seric 
52010758Seric /*
52110758Seric **  MYHOSTNAME -- stub version for case of no daemon code.
52211297Seric **
52311297Seric **	Can't convert to upper case here because might be a UUCP name.
52412313Seric **
52512313Seric **	Mark, you can change this to be anything you want......
52610758Seric */
52710758Seric 
52810758Seric char **
52912313Seric myhostname(hostbuf, size)
53010758Seric 	char hostbuf[];
53112313Seric 	int size;
53210758Seric {
53310758Seric 	register FILE *f;
53410758Seric 
53510758Seric 	hostbuf[0] = '\0';
53610758Seric 	f = fopen("/usr/include/whoami", "r");
53710758Seric 	if (f != NULL)
53810758Seric 	{
53912313Seric 		(void) fgets(hostbuf, size, f);
54010758Seric 		fixcrlf(hostbuf, TRUE);
54110758Seric 		(void) fclose(f);
54210758Seric 	}
54310758Seric 	return (NULL);
54410758Seric }
54516911Seric /*
54616911Seric **  MAPHOSTNAME -- turn a hostname into canonical form
54716911Seric **
54816911Seric **	Parameters:
54916911Seric **		hbuf -- a buffer containing a hostname.
55016911Seric **		hbsize -- the size of hbuf.
55116911Seric **
55216911Seric **	Returns:
55316911Seric **		none.
55416911Seric **
55516911Seric **	Side Effects:
55616911Seric **		Looks up the host specified in hbuf.  If it is not
55716911Seric **		the canonical name for that host, replace it with
55816911Seric **		the canonical name.  If the name is unknown, or it
55916911Seric **		is already the canonical name, leave it unchanged.
56016911Seric */
56110758Seric 
56216911Seric /*ARGSUSED*/
56316911Seric maphostname(hbuf, hbsize)
56416911Seric 	char *hbuf;
56516911Seric 	int hbsize;
56616911Seric {
56716911Seric 	return;
56816911Seric }
56916911Seric 
57016911Seric 
5715978Seric #endif DAEMON
572