16039Seric # include <errno.h>
24535Seric # include "sendmail.h"
34535Seric 
45978Seric #ifndef DAEMON
5*7556Seric SCCSID(@(#)daemon.c	3.20		07/27/82	(w/o daemon mode));
65978Seric #else
74535Seric 
85978Seric # include <sys/socket.h>
95978Seric # include <net/in.h>
107117Seric # include <wait.h>
115978Seric 
12*7556Seric SCCSID(@(#)daemon.c	3.20		07/27/82	(with daemon mode));
135978Seric 
144535Seric /*
154535Seric **  DAEMON.C -- routines to use when running as a daemon.
16*7556Seric **
17*7556Seric **	This entire file is highly dependent on the 4.2 BSD
18*7556Seric **	interprocess communication primitives.  No attempt has
19*7556Seric **	been made to make this file portable to Version 7,
20*7556Seric **	Version 6, MPX files, etc.  If you should try such a
21*7556Seric **	thing yourself, I recommend chucking the entire file
22*7556Seric **	and starting from scratch.  Basic semantics are:
23*7556Seric **
24*7556Seric **	getrequests()
25*7556Seric **		Opens a port and initiates a connection.
26*7556Seric **		Returns in a child.  Must set InChannel and
27*7556Seric **		OutChannel appropriately.
28*7556Seric **	makeconnection(host, port, outfile, infile)
29*7556Seric **		Make a connection to the named host on the given
30*7556Seric **		port.  Set *outfile and *infile to the files
31*7556Seric **		appropriate for communication.  Returns zero on
32*7556Seric **		success, else an exit status describing the
33*7556Seric **		error.
34*7556Seric **
35*7556Seric **	The semantics of both of these should be clean.
364535Seric */
374535Seric /*
384535Seric **  GETREQUESTS -- open mail IPC port and get requests.
394535Seric **
404535Seric **	Parameters:
414535Seric **		none.
424535Seric **
434535Seric **	Returns:
444535Seric **		none.
454535Seric **
464535Seric **	Side Effects:
474535Seric **		Waits until some interesting activity occurs.  When
484535Seric **		it does, a child is created to process it, and the
494535Seric **		parent waits for completion.  Return from this
504535Seric **		routine is always in the child.
514535Seric */
524535Seric 
537117Seric # define MAXCONNS	4	/* maximum simultaneous sendmails */
547117Seric 
554535Seric getrequests()
564535Seric {
577117Seric 	union wait status;
587117Seric 	int numconnections = 0;
597117Seric 
604631Seric 	for (;;)
614631Seric 	{
624636Seric 		register int pid;
635978Seric 		register int port;
644631Seric 
654636Seric 		/*
664636Seric 		**  Wait for a connection.
674636Seric 		*/
684631Seric 
695978Seric 		while ((port = getconnection()) < 0)
705978Seric 		{
715978Seric 			syserr("getrequests: getconnection failed");
72*7556Seric 			finis();
735978Seric 		}
744631Seric 
755978Seric 		/*
765978Seric 		**  Create a subprocess to process the mail.
775978Seric 		*/
785978Seric 
795978Seric # ifdef DEBUG
805978Seric 		if (Debug > 1)
815978Seric 			printf("getrequests: forking (port = %d)\n", port);
825978Seric # endif DEBUG
835978Seric 
844636Seric 		pid = fork();
854636Seric 		if (pid < 0)
864631Seric 		{
874636Seric 			syserr("daemon: cannot fork");
884636Seric 			sleep(10);
897009Seric 			(void) close(port);
904636Seric 			continue;
914631Seric 		}
924631Seric 
934636Seric 		if (pid == 0)
944631Seric 		{
954636Seric 			/*
964636Seric 			**  CHILD -- return to caller.
974636Seric 			**	Verify calling user id if possible here.
984636Seric 			*/
994631Seric 
1005978Seric 			InChannel = fdopen(port, "r");
1015978Seric 			OutChannel = fdopen(port, "w");
1025978Seric # ifdef DEBUG
1035978Seric 			if (Debug > 1)
1045978Seric 				printf("getreq: returning\n");
1055978Seric # endif DEBUG
1064636Seric 			return;
1074631Seric 		}
1084631Seric 
1094636Seric 		/*
1104636Seric 		**  PARENT -- wait for child to terminate.
1114636Seric 		**	Perhaps we should allow concurrent processing?
1124636Seric 		*/
1134631Seric 
1145978Seric # ifdef DEBUG
1155978Seric 		if (Debug > 1)
1165978Seric 		{
1175978Seric 			sleep(2);
1185978Seric 			printf("getreq: parent waiting\n");
1195978Seric 		}
1205978Seric # endif DEBUG
1215978Seric 
1227117Seric 		/* close the port so that others will hang (for a while) */
1237009Seric 		(void) close(port);
1247117Seric 
1257117Seric 		/* pick up old zombies; implement load limiting */
1267117Seric 		numconnections++;
1277117Seric 		while (wait3(&status, numconnections < MAXCONNS ? WNOHANG : 0, 0) > 0)
1287117Seric 			numconnections--;
1294631Seric 	}
1304631Seric }
1315978Seric /*
1325978Seric **  GETCONNECTION -- make a connection with the outside world
1335978Seric **
1345978Seric **	Parameters:
1355978Seric **		none.
1365978Seric **
1375978Seric **	Returns:
1385978Seric **		The port for mail traffic.
1395978Seric **
1405978Seric **	Side Effects:
1415978Seric **		Waits for a connection.
1425978Seric */
1435978Seric 
1447117Seric #define IPPORT_PLAYPORT	3055		/* random number */
1457117Seric 
1465979Seric struct sockaddr_in SendmailAddress = { AF_INET, IPPORT_SMTP };
1475978Seric 
1485978Seric getconnection()
1495978Seric {
1505978Seric 	register int s;
1515978Seric 	struct sockaddr otherend;
1525978Seric 
1535978Seric 	/*
1545978Seric 	**  Set up the address for the mailer.
1555978Seric 	*/
1565978Seric 
1576058Seric 	SendmailAddress.sin_addr.s_addr = 0;
1586633Seric 	SendmailAddress.sin_port = IPPORT_SMTP;
1597117Seric # ifdef DEBUG
1607117Seric 	if (Debug > 0)
1617117Seric 		SendmailAddress.sin_port = IPPORT_PLAYPORT;
1627117Seric # endif DEBUG
1637117Seric 	SendmailAddress.sin_port = htons(SendmailAddress.sin_port);
1645978Seric 
1655978Seric 	/*
1665978Seric 	**  Try to actually open the connection.
1675978Seric 	*/
1685978Seric 
1695978Seric # ifdef DEBUG
1705978Seric 	if (Debug)
1716058Seric 		printf("getconnection\n");
1725978Seric # endif DEBUG
1735978Seric 
174*7556Seric 	for (;;)
1757117Seric 	{
176*7556Seric 		/* get a socket for the SMTP connection */
177*7556Seric 		s = socket(SOCK_STREAM, 0, &SendmailAddress, SO_ACCEPTCONN);
178*7556Seric 		if (s < 0)
179*7556Seric 		{
180*7556Seric 			/* probably another daemon already */
181*7556Seric 			syserr("getconnection: can't create socket");
182*7556Seric 			break;
183*7556Seric 		}
1845978Seric 
1855978Seric # ifdef DEBUG
186*7556Seric 		if (Debug)
187*7556Seric 			printf("getconnection: %d\n", s);
1885978Seric # endif DEBUG
189*7556Seric 
190*7556Seric 		/* wait for a connection */
191*7556Seric 		if (accept(s, &otherend) >= 0)
192*7556Seric 			break;
193*7556Seric 
194*7556Seric 		/* probably innocuous -- retry */
195*7556Seric 		syserr("getconnection: accept");
1967286Seric 		(void) close(s);
197*7556Seric 		sleep(20);
1987117Seric 	}
1995978Seric 
2005978Seric 	return (s);
2015978Seric }
2026039Seric /*
2036039Seric **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
2046039Seric **
2056039Seric **	Parameters:
2066039Seric **		host -- the name of the host.
2076633Seric **		port -- the port number to connect to.
2086039Seric **		outfile -- a pointer to a place to put the outfile
2096039Seric **			descriptor.
2106039Seric **		infile -- ditto for infile.
2116039Seric **
2126039Seric **	Returns:
2136039Seric **		An exit code telling whether the connection could be
2146039Seric **			made and if not why not.
2156039Seric **
2166039Seric **	Side Effects:
2176039Seric **		none.
2186039Seric */
2195978Seric 
2206633Seric makeconnection(host, port, outfile, infile)
2216039Seric 	char *host;
2227286Seric 	u_short port;
2236039Seric 	FILE **outfile;
2246039Seric 	FILE **infile;
2256039Seric {
2266039Seric 	register int s;
2276039Seric 
2286039Seric 	/*
2296039Seric 	**  Set up the address for the mailer.
2306039Seric 	*/
2316039Seric 
2326039Seric 	if ((SendmailAddress.sin_addr.s_addr = rhost(&host)) == -1)
2336039Seric 		return (EX_NOHOST);
2346633Seric 	if (port == 0)
2356633Seric 		port = IPPORT_SMTP;
2367117Seric 	SendmailAddress.sin_port = htons(port);
2376039Seric 
2386039Seric 	/*
2396039Seric 	**  Try to actually open the connection.
2406039Seric 	*/
2416039Seric 
2426039Seric # ifdef DEBUG
2436039Seric 	if (Debug)
2446039Seric 		printf("makeconnection (%s)\n", host);
2456039Seric # endif DEBUG
2466039Seric 
2477009Seric 	s = socket(SOCK_STREAM, 0, (struct sockaddr_in *) 0, 0);
2486039Seric 	if (s < 0)
2496039Seric 	{
2506039Seric 		syserr("makeconnection: no socket");
2516039Seric 		goto failure;
2526039Seric 	}
2536039Seric 
2546039Seric # ifdef DEBUG
2556039Seric 	if (Debug)
2566039Seric 		printf("makeconnection: %d\n", s);
2576039Seric # endif DEBUG
2587372Seric 	fflush(Xscript);				/* for debugging */
2596039Seric 	if (connect(s, &SendmailAddress) < 0)
2606039Seric 	{
2616039Seric 		/* failure, decide if temporary or not */
2626039Seric 	failure:
2636039Seric 		switch (errno)
2646039Seric 		{
2656039Seric 		  case EISCONN:
2666039Seric 		  case ETIMEDOUT:
2676897Seric 		  case EINPROGRESS:
2686897Seric 		  case EALREADY:
2696897Seric 		  case EADDRINUSE:
2706897Seric 		  case ENETDOWN:
2716897Seric 		  case ENETRESET:
2726897Seric 		  case ENOBUFS:
2737204Seric 		  case ECONNREFUSED:
2746039Seric 			/* there are others, I'm sure..... */
2756039Seric 			return (EX_TEMPFAIL);
2766039Seric 
2776039Seric 		  default:
2786039Seric 			return (EX_UNAVAILABLE);
2796039Seric 		}
2806039Seric 	}
2816039Seric 
2826039Seric 	/* connection ok, put it into canonical form */
2836039Seric 	*outfile = fdopen(s, "w");
2846039Seric 	*infile = fdopen(s, "r");
2856039Seric 
2866039Seric 	return (0);
2876039Seric }
2886039Seric 
2895978Seric #endif DAEMON
290