xref: /csrg-svn/usr.sbin/sendmail/src/daemon.c (revision 14875)
16039Seric # include <errno.h>
24535Seric # include "sendmail.h"
34535Seric 
45978Seric #ifndef DAEMON
5*14875Seric SCCSID(@(#)daemon.c	4.4		08/28/83	(w/o daemon mode));
65978Seric #else
74535Seric 
89610Seric #include <sys/socket.h>
99610Seric #include <netinet/in.h>
109610Seric #include <netdb.h>
1113586Swnj #include <sys/wait.h>
125978Seric 
13*14875Seric SCCSID(@(#)daemon.c	4.4		08/28/83	(with daemon mode));
145978Seric 
154535Seric /*
164535Seric **  DAEMON.C -- routines to use when running as a daemon.
177556Seric **
187556Seric **	This entire file is highly dependent on the 4.2 BSD
197556Seric **	interprocess communication primitives.  No attempt has
207556Seric **	been made to make this file portable to Version 7,
217556Seric **	Version 6, MPX files, etc.  If you should try such a
227556Seric **	thing yourself, I recommend chucking the entire file
237556Seric **	and starting from scratch.  Basic semantics are:
247556Seric **
257556Seric **	getrequests()
267556Seric **		Opens a port and initiates a connection.
277556Seric **		Returns in a child.  Must set InChannel and
287556Seric **		OutChannel appropriately.
2910206Seric **	clrdaemon()
3010206Seric **		Close any open files associated with getting
3110206Seric **		the connection; this is used when running the queue,
3210206Seric **		etc., to avoid having extra file descriptors during
3310206Seric **		the queue run and to avoid confusing the network
3410206Seric **		code (if it cares).
357556Seric **	makeconnection(host, port, outfile, infile)
367556Seric **		Make a connection to the named host on the given
377556Seric **		port.  Set *outfile and *infile to the files
387556Seric **		appropriate for communication.  Returns zero on
397556Seric **		success, else an exit status describing the
407556Seric **		error.
417556Seric **
427556Seric **	The semantics of both of these should be clean.
434535Seric */
444535Seric /*
454535Seric **  GETREQUESTS -- open mail IPC port and get requests.
464535Seric **
474535Seric **	Parameters:
484535Seric **		none.
494535Seric **
504535Seric **	Returns:
514535Seric **		none.
524535Seric **
534535Seric **	Side Effects:
544535Seric **		Waits until some interesting activity occurs.  When
554535Seric **		it does, a child is created to process it, and the
564535Seric **		parent waits for completion.  Return from this
579886Seric **		routine is always in the child.  The file pointers
589886Seric **		"InChannel" and "OutChannel" should be set to point
599886Seric **		to the communication channel.
604535Seric */
614535Seric 
6210206Seric struct sockaddr_in	SendmailAddress;/* internet address of sendmail */
6310206Seric int	DaemonSocket = -1;		/* fd describing socket */
649610Seric 
654535Seric getrequests()
664535Seric {
679610Seric 	int t;
687117Seric 	union wait status;
699610Seric 	register struct servent *sp;
707117Seric 
719610Seric 	/*
729610Seric 	**  Set up the address for the mailer.
739610Seric 	*/
749610Seric 
759610Seric 	sp = getservbyname("smtp", "tcp");
769610Seric 	if (sp == NULL)
779610Seric 	{
789610Seric 		syserr("server \"smtp\" unknown");
7910167Seric 		goto severe;
809610Seric 	}
819610Seric 	SendmailAddress.sin_family = AF_INET;
829610Seric 	SendmailAddress.sin_addr.s_addr = INADDR_ANY;
839740Ssam 	SendmailAddress.sin_port = sp->s_port;
849610Seric 
859610Seric 	/*
869610Seric 	**  Try to actually open the connection.
879610Seric 	*/
889610Seric 
899610Seric # ifdef DEBUG
909610Seric 	if (tTd(15, 1))
919610Seric 		printf("getrequests: port 0x%x\n", SendmailAddress.sin_port);
929610Seric # endif DEBUG
939610Seric 
949610Seric 	/* get a socket for the SMTP connection */
9510206Seric 	DaemonSocket = socket(AF_INET, SOCK_STREAM, 0, 0);
9610206Seric 	if (DaemonSocket < 0)
979610Seric 	{
989610Seric 		/* probably another daemon already */
999610Seric 		syserr("getrequests: can't create socket");
1009610Seric 	  severe:
1019610Seric # ifdef LOG
1029610Seric 		if (LogLevel > 0)
1039610Seric 			syslog(LOG_SALERT, "cannot get connection");
1049610Seric # endif LOG
1059610Seric 		finis();
1069610Seric 	}
10710347Seric 
10810347Seric #ifdef DEBUG
10910347Seric 	/* turn on network debugging? */
11010347Seric 	if (tTd(15, 15))
11110347Seric 		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, 0, 0);
11210347Seric #endif DEBUG
11310347Seric 
11410206Seric 	if (bind(DaemonSocket, &SendmailAddress, sizeof SendmailAddress, 0) < 0)
1159610Seric 	{
1169610Seric 		syserr("getrequests: cannot bind");
11710206Seric 		(void) close(DaemonSocket);
1189610Seric 		goto severe;
1199610Seric 	}
12010206Seric 	listen(DaemonSocket, 10);
1219610Seric 
1229610Seric # ifdef DEBUG
1239610Seric 	if (tTd(15, 1))
12410206Seric 		printf("getrequests: %d\n", DaemonSocket);
1259610Seric # endif DEBUG
1269610Seric 
1274631Seric 	for (;;)
1284631Seric 	{
129*14875Seric 		register int pid;
13011147Seric 		auto int lotherend;
13111147Seric 		struct sockaddr_in otherend;
132*14875Seric 		extern int RefuseLA;
13311147Seric 
134*14875Seric 		/* see if we are rejecting connections */
135*14875Seric 		while (getla() > RefuseLA)
136*14875Seric 			sleep(5);
137*14875Seric 
1389610Seric 		/* wait for a connection */
1399610Seric 		do
1409610Seric 		{
1419610Seric 			errno = 0;
1429610Seric 			lotherend = sizeof otherend;
14310206Seric 			t = accept(DaemonSocket, &otherend, &lotherend, 0);
1449610Seric 		} while (t < 0 && errno == EINTR);
1459610Seric 		if (t < 0)
1465978Seric 		{
1479610Seric 			syserr("getrequests: accept");
1489610Seric 			sleep(5);
1499610Seric 			continue;
1505978Seric 		}
1514631Seric 
1525978Seric 		/*
1535978Seric 		**  Create a subprocess to process the mail.
1545978Seric 		*/
1555978Seric 
1565978Seric # ifdef DEBUG
1577677Seric 		if (tTd(15, 2))
1589610Seric 			printf("getrequests: forking (fd = %d)\n", t);
1595978Seric # endif DEBUG
1605978Seric 
1614636Seric 		pid = fork();
1624636Seric 		if (pid < 0)
1634631Seric 		{
1644636Seric 			syserr("daemon: cannot fork");
1654636Seric 			sleep(10);
1669610Seric 			(void) close(t);
1674636Seric 			continue;
1684631Seric 		}
1694631Seric 
1704636Seric 		if (pid == 0)
1714631Seric 		{
17211147Seric 			extern struct hostent *gethostbyaddr();
17311147Seric 			register struct hostent *hp;
17411147Seric 			extern char *RealHostName;	/* srvrsmtp.c */
17511147Seric 			char buf[MAXNAME];
17611147Seric 
1774636Seric 			/*
1784636Seric 			**  CHILD -- return to caller.
17911147Seric 			**	Collect verified idea of sending host.
1804636Seric 			**	Verify calling user id if possible here.
1814636Seric 			*/
1824631Seric 
18311147Seric 			/* determine host name */
18411147Seric 			hp = gethostbyaddr(&otherend.sin_addr, sizeof otherend.sin_addr, AF_INET);
18511147Seric 			if (hp != NULL)
18611147Seric 				(void) sprintf(buf, "%s.ARPA", hp->h_name);
18711147Seric 			else
18811147Seric 				/* this should produce a dotted quad */
18911147Seric 				(void) sprintf(buf, "%lx", otherend.sin_addr.s_addr);
19011147Seric 			RealHostName = newstr(buf);
19111147Seric 
19210206Seric 			(void) close(DaemonSocket);
1939610Seric 			InChannel = fdopen(t, "r");
1949610Seric 			OutChannel = fdopen(t, "w");
1955978Seric # ifdef DEBUG
1967677Seric 			if (tTd(15, 2))
1975978Seric 				printf("getreq: returning\n");
1985978Seric # endif DEBUG
1997876Seric # ifdef LOG
2007876Seric 			if (LogLevel > 11)
2017876Seric 				syslog(LOG_DEBUG, "connected, pid=%d", getpid());
2027876Seric # endif LOG
2034636Seric 			return;
2044631Seric 		}
2054631Seric 
2064636Seric 		/*
2074636Seric 		**  PARENT -- wait for child to terminate.
2084636Seric 		**	Perhaps we should allow concurrent processing?
2094636Seric 		*/
2104631Seric 
2115978Seric # ifdef DEBUG
2127677Seric 		if (tTd(15, 2))
2135978Seric 		{
2145978Seric 			sleep(2);
2155978Seric 			printf("getreq: parent waiting\n");
2165978Seric 		}
2175978Seric # endif DEBUG
2185978Seric 
2197117Seric 		/* close the port so that others will hang (for a while) */
2209610Seric 		(void) close(t);
2217117Seric 
22213933Seric 		/* pick up old zombies */
22313933Seric 		while (wait3(&status, WNOHANG, 0) > 0)
22413933Seric 			continue;
2254631Seric 	}
2269886Seric 	/*NOTREACHED*/
2274631Seric }
2285978Seric /*
22910206Seric **  CLRDAEMON -- reset the daemon connection
23010206Seric **
23110206Seric **	Parameters:
23210206Seric **		none.
23310206Seric **
23410206Seric **	Returns:
23510206Seric **		none.
23610206Seric **
23710206Seric **	Side Effects:
23810206Seric **		releases any resources used by the passive daemon.
23910206Seric */
24010206Seric 
24110206Seric clrdaemon()
24210206Seric {
24310206Seric 	if (DaemonSocket >= 0)
24410206Seric 		(void) close(DaemonSocket);
24510206Seric 	DaemonSocket = -1;
24610206Seric }
24710206Seric /*
2486039Seric **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
2496039Seric **
2506039Seric **	Parameters:
2516039Seric **		host -- the name of the host.
2526633Seric **		port -- the port number to connect to.
2536039Seric **		outfile -- a pointer to a place to put the outfile
2546039Seric **			descriptor.
2556039Seric **		infile -- ditto for infile.
2566039Seric **
2576039Seric **	Returns:
2586039Seric **		An exit code telling whether the connection could be
2596039Seric **			made and if not why not.
2606039Seric **
2616039Seric **	Side Effects:
2626039Seric **		none.
2636039Seric */
2645978Seric 
2656633Seric makeconnection(host, port, outfile, infile)
2666039Seric 	char *host;
2677286Seric 	u_short port;
2686039Seric 	FILE **outfile;
2696039Seric 	FILE **infile;
2706039Seric {
2716039Seric 	register int s;
2726039Seric 
2736039Seric 	/*
2746039Seric 	**  Set up the address for the mailer.
2759308Seric 	**	Accept "[a.b.c.d]" syntax for host name.
2766039Seric 	*/
2776039Seric 
2789308Seric 	if (host[0] == '[')
2799308Seric 	{
28011147Seric 		long hid;
28111147Seric 		register char *p = index(host, ']');
2829308Seric 
28311147Seric 		if (p != NULL)
2849308Seric 		{
28511147Seric 			*p = '\0';
28611147Seric 			hid = inet_addr(&host[1]);
28711147Seric 			*p = ']';
2889308Seric 		}
28911147Seric 		if (p == NULL || hid == -1)
2909308Seric 		{
2919308Seric 			usrerr("Invalid numeric domain spec \"%s\"", host);
2929308Seric 			return (EX_NOHOST);
2939308Seric 		}
2949308Seric 		SendmailAddress.sin_addr.s_addr = hid;
2959308Seric 	}
2969610Seric 	else
2979610Seric 	{
2989610Seric 		register struct hostent *hp = gethostbyname(host);
2999610Seric 
30011147Seric 		if (hp == NULL)
3019610Seric 			return (EX_NOHOST);
3029610Seric 		bmove(hp->h_addr, (char *) &SendmailAddress.sin_addr, hp->h_length);
3039610Seric 	}
3049610Seric 
3059610Seric 	/*
3069610Seric 	**  Determine the port number.
3079610Seric 	*/
3089610Seric 
30910011Seric 	if (port != 0)
31010011Seric 		SendmailAddress.sin_port = htons(port);
31110011Seric 	else
3129610Seric 	{
3139610Seric 		register struct servent *sp = getservbyname("smtp", "tcp");
3149610Seric 
3159610Seric 		if (sp == NULL)
3169610Seric 		{
3179610Seric 			syserr("makeconnection: server \"smtp\" unknown");
3189610Seric 			return (EX_OSFILE);
3199610Seric 		}
32010011Seric 		SendmailAddress.sin_port = sp->s_port;
3219610Seric 	}
3226039Seric 
3236039Seric 	/*
3246039Seric 	**  Try to actually open the connection.
3256039Seric 	*/
3266039Seric 
3276039Seric # ifdef DEBUG
3287677Seric 	if (tTd(16, 1))
3296039Seric 		printf("makeconnection (%s)\n", host);
3306039Seric # endif DEBUG
3316039Seric 
3329310Seric 	s = socket(AF_INET, SOCK_STREAM, 0, 0);
3336039Seric 	if (s < 0)
3346039Seric 	{
3356039Seric 		syserr("makeconnection: no socket");
3366039Seric 		goto failure;
3376039Seric 	}
3386039Seric 
3396039Seric # ifdef DEBUG
3407677Seric 	if (tTd(16, 1))
3416039Seric 		printf("makeconnection: %d\n", s);
34210347Seric 
34310347Seric 	/* turn on network debugging? */
34410347Seric 	if (tTd(16, 14))
34510347Seric 		(void) setsockopt(s, SOL_SOCKET, SO_DEBUG, 0, 0);
3466039Seric # endif DEBUG
3479546Seric 	(void) fflush(CurEnv->e_xfp);			/* for debugging */
34814383Seric 	errno = 0;					/* for debugging */
3499610Seric 	SendmailAddress.sin_family = AF_INET;
3509310Seric 	if (connect(s, &SendmailAddress, sizeof SendmailAddress, 0) < 0)
3516039Seric 	{
3526039Seric 		/* failure, decide if temporary or not */
3536039Seric 	failure:
3546039Seric 		switch (errno)
3556039Seric 		{
3566039Seric 		  case EISCONN:
3576039Seric 		  case ETIMEDOUT:
3586897Seric 		  case EINPROGRESS:
3596897Seric 		  case EALREADY:
3606897Seric 		  case EADDRINUSE:
36110123Seric 		  case EHOSTDOWN:
3626897Seric 		  case ENETDOWN:
3636897Seric 		  case ENETRESET:
3646897Seric 		  case ENOBUFS:
3657204Seric 		  case ECONNREFUSED:
36611546Seric 		  case ECONNRESET:
36710081Seric 		  case EHOSTUNREACH:
36810098Seric 		  case ENETUNREACH:
3696039Seric 			/* there are others, I'm sure..... */
3706039Seric 			return (EX_TEMPFAIL);
3716039Seric 
37211147Seric 		  case EPERM:
37311147Seric 			/* why is this happening? */
37411147Seric 			syserr("makeconnection: funny failure, addr=%lx, port=%x",
37511147Seric 				SendmailAddress.sin_addr.s_addr, SendmailAddress.sin_port);
37614383Seric 			return (EX_TEMPFAIL);
37711147Seric 
3786039Seric 		  default:
3796039Seric 			return (EX_UNAVAILABLE);
3806039Seric 		}
3816039Seric 	}
3826039Seric 
3836039Seric 	/* connection ok, put it into canonical form */
3846039Seric 	*outfile = fdopen(s, "w");
3856039Seric 	*infile = fdopen(s, "r");
3866039Seric 
38710098Seric 	return (EX_OK);
3886039Seric }
38910758Seric /*
39010758Seric **  MYHOSTNAME -- return the name of this host.
39110758Seric **
39210758Seric **	Parameters:
39310758Seric **		hostbuf -- a place to return the name of this host.
39412313Seric **		size -- the size of hostbuf.
39510758Seric **
39610758Seric **	Returns:
39710758Seric **		A list of aliases for this host.
39810758Seric **
39910758Seric **	Side Effects:
40010758Seric **		none.
40110758Seric */
4026039Seric 
40310758Seric char **
40412313Seric myhostname(hostbuf, size)
40510758Seric 	char hostbuf[];
40612313Seric 	int size;
40710758Seric {
40810758Seric 	extern struct hostent *gethostbyname();
40911147Seric 	struct hostent *hp;
41012313Seric 	auto int i = size;
41110758Seric 
41211276Seric 	gethostname(hostbuf, &i);
41311147Seric 	hp = gethostbyname(hostbuf);
41411147Seric 	if (hp != NULL)
41511147Seric 		return (hp->h_aliases);
41610758Seric 	else
41710758Seric 		return (NULL);
41810758Seric }
41910758Seric 
42010758Seric # else DAEMON
42110758Seric 
42210758Seric /*
42310758Seric **  MYHOSTNAME -- stub version for case of no daemon code.
42411297Seric **
42511297Seric **	Can't convert to upper case here because might be a UUCP name.
42612313Seric **
42712313Seric **	Mark, you can change this to be anything you want......
42810758Seric */
42910758Seric 
43010758Seric char **
43112313Seric myhostname(hostbuf, size)
43210758Seric 	char hostbuf[];
43312313Seric 	int size;
43410758Seric {
43510758Seric 	register FILE *f;
43610758Seric 
43710758Seric 	hostbuf[0] = '\0';
43810758Seric 	f = fopen("/usr/include/whoami", "r");
43910758Seric 	if (f != NULL)
44010758Seric 	{
44112313Seric 		(void) fgets(hostbuf, size, f);
44210758Seric 		fixcrlf(hostbuf, TRUE);
44310758Seric 		(void) fclose(f);
44410758Seric 	}
44510758Seric 	return (NULL);
44610758Seric }
44710758Seric 
4485978Seric #endif DAEMON
449