xref: /original-bsd/usr.sbin/sendmail/src/daemon.c (revision 3a099713)
17aa493c5Seric # include <errno.h>
27fa39d90Seric # include "sendmail.h"
37fa39d90Seric 
4d0a9e852Seric #ifndef DAEMON
5*3a099713Seric SCCSID(@(#)daemon.c	4.4		08/28/83	(w/o daemon mode));
6d0a9e852Seric #else
7d0a9e852Seric 
8d0a9e852Seric #include <sys/socket.h>
91c71e510Seric #include <netinet/in.h>
101c71e510Seric #include <netdb.h>
111e1663f7Swnj #include <sys/wait.h>
12d0a9e852Seric 
13*3a099713Seric SCCSID(@(#)daemon.c	4.4		08/28/83	(with daemon mode));
147fa39d90Seric 
157fa39d90Seric /*
167fa39d90Seric **  DAEMON.C -- routines to use when running as a daemon.
1747b12ae1Seric **
1847b12ae1Seric **	This entire file is highly dependent on the 4.2 BSD
1947b12ae1Seric **	interprocess communication primitives.  No attempt has
2047b12ae1Seric **	been made to make this file portable to Version 7,
2147b12ae1Seric **	Version 6, MPX files, etc.  If you should try such a
2247b12ae1Seric **	thing yourself, I recommend chucking the entire file
2347b12ae1Seric **	and starting from scratch.  Basic semantics are:
2447b12ae1Seric **
2547b12ae1Seric **	getrequests()
2647b12ae1Seric **		Opens a port and initiates a connection.
2747b12ae1Seric **		Returns in a child.  Must set InChannel and
2847b12ae1Seric **		OutChannel appropriately.
29b7d7afcbSeric **	clrdaemon()
30b7d7afcbSeric **		Close any open files associated with getting
31b7d7afcbSeric **		the connection; this is used when running the queue,
32b7d7afcbSeric **		etc., to avoid having extra file descriptors during
33b7d7afcbSeric **		the queue run and to avoid confusing the network
34b7d7afcbSeric **		code (if it cares).
3547b12ae1Seric **	makeconnection(host, port, outfile, infile)
3647b12ae1Seric **		Make a connection to the named host on the given
3747b12ae1Seric **		port.  Set *outfile and *infile to the files
3847b12ae1Seric **		appropriate for communication.  Returns zero on
3947b12ae1Seric **		success, else an exit status describing the
4047b12ae1Seric **		error.
4147b12ae1Seric **
4247b12ae1Seric **	The semantics of both of these should be clean.
437fa39d90Seric */
447fa39d90Seric /*
457fa39d90Seric **  GETREQUESTS -- open mail IPC port and get requests.
467fa39d90Seric **
477fa39d90Seric **	Parameters:
487fa39d90Seric **		none.
497fa39d90Seric **
507fa39d90Seric **	Returns:
517fa39d90Seric **		none.
527fa39d90Seric **
537fa39d90Seric **	Side Effects:
547fa39d90Seric **		Waits until some interesting activity occurs.  When
557fa39d90Seric **		it does, a child is created to process it, and the
567fa39d90Seric **		parent waits for completion.  Return from this
57147303b1Seric **		routine is always in the child.  The file pointers
58147303b1Seric **		"InChannel" and "OutChannel" should be set to point
59147303b1Seric **		to the communication channel.
607fa39d90Seric */
617fa39d90Seric 
62b7d7afcbSeric struct sockaddr_in	SendmailAddress;/* internet address of sendmail */
63b7d7afcbSeric int	DaemonSocket = -1;		/* fd describing socket */
641c71e510Seric 
657fa39d90Seric getrequests()
667fa39d90Seric {
671c71e510Seric 	int t;
682554d49fSeric 	union wait status;
691c71e510Seric 	register struct servent *sp;
70eb889047Seric 
71a8268164Seric 	/*
721c71e510Seric 	**  Set up the address for the mailer.
73eb889047Seric 	*/
74eb889047Seric 
751c71e510Seric 	sp = getservbyname("smtp", "tcp");
761c71e510Seric 	if (sp == NULL)
77d0a9e852Seric 	{
781c71e510Seric 		syserr("server \"smtp\" unknown");
79a1961f2aSeric 		goto severe;
801c71e510Seric 	}
811c71e510Seric 	SendmailAddress.sin_family = AF_INET;
821c71e510Seric 	SendmailAddress.sin_addr.s_addr = INADDR_ANY;
83f43fa3c2Ssam 	SendmailAddress.sin_port = sp->s_port;
841c71e510Seric 
851c71e510Seric 	/*
861c71e510Seric 	**  Try to actually open the connection.
871c71e510Seric 	*/
881c71e510Seric 
891c71e510Seric # ifdef DEBUG
901c71e510Seric 	if (tTd(15, 1))
911c71e510Seric 		printf("getrequests: port 0x%x\n", SendmailAddress.sin_port);
921c71e510Seric # endif DEBUG
931c71e510Seric 
941c71e510Seric 	/* get a socket for the SMTP connection */
95b7d7afcbSeric 	DaemonSocket = socket(AF_INET, SOCK_STREAM, 0, 0);
96b7d7afcbSeric 	if (DaemonSocket < 0)
971c71e510Seric 	{
981c71e510Seric 		/* probably another daemon already */
991c71e510Seric 		syserr("getrequests: can't create socket");
1001c71e510Seric 	  severe:
101b0ba8827Seric # ifdef LOG
102b0ba8827Seric 		if (LogLevel > 0)
103b0ba8827Seric 			syslog(LOG_SALERT, "cannot get connection");
104b0ba8827Seric # endif LOG
10547b12ae1Seric 		finis();
106d0a9e852Seric 	}
1071b6e4a15Seric 
1081b6e4a15Seric #ifdef DEBUG
1091b6e4a15Seric 	/* turn on network debugging? */
1101b6e4a15Seric 	if (tTd(15, 15))
1111b6e4a15Seric 		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, 0, 0);
1121b6e4a15Seric #endif DEBUG
1131b6e4a15Seric 
114b7d7afcbSeric 	if (bind(DaemonSocket, &SendmailAddress, sizeof SendmailAddress, 0) < 0)
1151c71e510Seric 	{
1161c71e510Seric 		syserr("getrequests: cannot bind");
117b7d7afcbSeric 		(void) close(DaemonSocket);
1181c71e510Seric 		goto severe;
1191c71e510Seric 	}
120b7d7afcbSeric 	listen(DaemonSocket, 10);
1211c71e510Seric 
1221c71e510Seric # ifdef DEBUG
1231c71e510Seric 	if (tTd(15, 1))
124b7d7afcbSeric 		printf("getrequests: %d\n", DaemonSocket);
1251c71e510Seric # endif DEBUG
1261c71e510Seric 
1271c71e510Seric 	for (;;)
1281c71e510Seric 	{
129*3a099713Seric 		register int pid;
130a44d5a5eSeric 		auto int lotherend;
131a44d5a5eSeric 		struct sockaddr_in otherend;
132*3a099713Seric 		extern int RefuseLA;
133*3a099713Seric 
134*3a099713Seric 		/* see if we are rejecting connections */
135*3a099713Seric 		while (getla() > RefuseLA)
136*3a099713Seric 			sleep(5);
137a44d5a5eSeric 
1381c71e510Seric 		/* wait for a connection */
1391c71e510Seric 		do
1401c71e510Seric 		{
1411c71e510Seric 			errno = 0;
1421c71e510Seric 			lotherend = sizeof otherend;
143b7d7afcbSeric 			t = accept(DaemonSocket, &otherend, &lotherend, 0);
1441c71e510Seric 		} while (t < 0 && errno == EINTR);
1451c71e510Seric 		if (t < 0)
1461c71e510Seric 		{
1471c71e510Seric 			syserr("getrequests: accept");
1481c71e510Seric 			sleep(5);
1491c71e510Seric 			continue;
1501c71e510Seric 		}
151d0a9e852Seric 
152d0a9e852Seric 		/*
153d0a9e852Seric 		**  Create a subprocess to process the mail.
154d0a9e852Seric 		*/
155d0a9e852Seric 
156d0a9e852Seric # ifdef DEBUG
15761e4310fSeric 		if (tTd(15, 2))
1581c71e510Seric 			printf("getrequests: forking (fd = %d)\n", t);
159d0a9e852Seric # endif DEBUG
160eb889047Seric 
161a8268164Seric 		pid = fork();
162a8268164Seric 		if (pid < 0)
163a8268164Seric 		{
164a8268164Seric 			syserr("daemon: cannot fork");
165a8268164Seric 			sleep(10);
1661c71e510Seric 			(void) close(t);
167a8268164Seric 			continue;
168a8268164Seric 		}
169a8268164Seric 
170a8268164Seric 		if (pid == 0)
171a8268164Seric 		{
172a44d5a5eSeric 			extern struct hostent *gethostbyaddr();
173a44d5a5eSeric 			register struct hostent *hp;
174a44d5a5eSeric 			extern char *RealHostName;	/* srvrsmtp.c */
175a44d5a5eSeric 			char buf[MAXNAME];
176a44d5a5eSeric 
177a8268164Seric 			/*
178a8268164Seric 			**  CHILD -- return to caller.
179a44d5a5eSeric 			**	Collect verified idea of sending host.
180a8268164Seric 			**	Verify calling user id if possible here.
181a8268164Seric 			*/
182a8268164Seric 
183a44d5a5eSeric 			/* determine host name */
184a44d5a5eSeric 			hp = gethostbyaddr(&otherend.sin_addr, sizeof otherend.sin_addr, AF_INET);
185a44d5a5eSeric 			if (hp != NULL)
186a44d5a5eSeric 				(void) sprintf(buf, "%s.ARPA", hp->h_name);
187a44d5a5eSeric 			else
188a44d5a5eSeric 				/* this should produce a dotted quad */
189a44d5a5eSeric 				(void) sprintf(buf, "%lx", otherend.sin_addr.s_addr);
190a44d5a5eSeric 			RealHostName = newstr(buf);
191a44d5a5eSeric 
192b7d7afcbSeric 			(void) close(DaemonSocket);
1931c71e510Seric 			InChannel = fdopen(t, "r");
1941c71e510Seric 			OutChannel = fdopen(t, "w");
195d0a9e852Seric # ifdef DEBUG
19661e4310fSeric 			if (tTd(15, 2))
197d0a9e852Seric 				printf("getreq: returning\n");
198d0a9e852Seric # endif DEBUG
199252c8a22Seric # ifdef LOG
200252c8a22Seric 			if (LogLevel > 11)
201252c8a22Seric 				syslog(LOG_DEBUG, "connected, pid=%d", getpid());
202252c8a22Seric # endif LOG
203a8268164Seric 			return;
204a8268164Seric 		}
205a8268164Seric 
206a8268164Seric 		/*
207a8268164Seric 		**  PARENT -- wait for child to terminate.
208a8268164Seric 		**	Perhaps we should allow concurrent processing?
209a8268164Seric 		*/
210a8268164Seric 
211d0a9e852Seric # ifdef DEBUG
21261e4310fSeric 		if (tTd(15, 2))
213d0a9e852Seric 		{
214d0a9e852Seric 			sleep(2);
215d0a9e852Seric 			printf("getreq: parent waiting\n");
216d0a9e852Seric 		}
217d0a9e852Seric # endif DEBUG
218d0a9e852Seric 
2192554d49fSeric 		/* close the port so that others will hang (for a while) */
2201c71e510Seric 		(void) close(t);
2212554d49fSeric 
222105e901cSeric 		/* pick up old zombies */
223105e901cSeric 		while (wait3(&status, WNOHANG, 0) > 0)
224105e901cSeric 			continue;
225a8268164Seric 	}
226147303b1Seric 	/*NOTREACHED*/
2277fa39d90Seric }
228d0a9e852Seric /*
229b7d7afcbSeric **  CLRDAEMON -- reset the daemon connection
230b7d7afcbSeric **
231b7d7afcbSeric **	Parameters:
232b7d7afcbSeric **		none.
233b7d7afcbSeric **
234b7d7afcbSeric **	Returns:
235b7d7afcbSeric **		none.
236b7d7afcbSeric **
237b7d7afcbSeric **	Side Effects:
238b7d7afcbSeric **		releases any resources used by the passive daemon.
239b7d7afcbSeric */
240b7d7afcbSeric 
241b7d7afcbSeric clrdaemon()
242b7d7afcbSeric {
243b7d7afcbSeric 	if (DaemonSocket >= 0)
244b7d7afcbSeric 		(void) close(DaemonSocket);
245b7d7afcbSeric 	DaemonSocket = -1;
246b7d7afcbSeric }
247b7d7afcbSeric /*
2487aa493c5Seric **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
2497aa493c5Seric **
2507aa493c5Seric **	Parameters:
2517aa493c5Seric **		host -- the name of the host.
25248ff0a9dSeric **		port -- the port number to connect to.
2537aa493c5Seric **		outfile -- a pointer to a place to put the outfile
2547aa493c5Seric **			descriptor.
2557aa493c5Seric **		infile -- ditto for infile.
2567aa493c5Seric **
2577aa493c5Seric **	Returns:
2587aa493c5Seric **		An exit code telling whether the connection could be
2597aa493c5Seric **			made and if not why not.
2607aa493c5Seric **
2617aa493c5Seric **	Side Effects:
2627aa493c5Seric **		none.
2637aa493c5Seric */
2647aa493c5Seric 
26548ff0a9dSeric makeconnection(host, port, outfile, infile)
2667aa493c5Seric 	char *host;
267210215eaSeric 	u_short port;
2687aa493c5Seric 	FILE **outfile;
2697aa493c5Seric 	FILE **infile;
2707aa493c5Seric {
2717aa493c5Seric 	register int s;
2727aa493c5Seric 
2737aa493c5Seric 	/*
2747aa493c5Seric 	**  Set up the address for the mailer.
27571096d12Seric 	**	Accept "[a.b.c.d]" syntax for host name.
2767aa493c5Seric 	*/
2777aa493c5Seric 
27871096d12Seric 	if (host[0] == '[')
27971096d12Seric 	{
280a44d5a5eSeric 		long hid;
281a44d5a5eSeric 		register char *p = index(host, ']');
28271096d12Seric 
283a44d5a5eSeric 		if (p != NULL)
28471096d12Seric 		{
285a44d5a5eSeric 			*p = '\0';
286a44d5a5eSeric 			hid = inet_addr(&host[1]);
287a44d5a5eSeric 			*p = ']';
28871096d12Seric 		}
289a44d5a5eSeric 		if (p == NULL || hid == -1)
29071096d12Seric 		{
29171096d12Seric 			usrerr("Invalid numeric domain spec \"%s\"", host);
29271096d12Seric 			return (EX_NOHOST);
29371096d12Seric 		}
29471096d12Seric 		SendmailAddress.sin_addr.s_addr = hid;
29571096d12Seric 	}
2961c71e510Seric 	else
2971c71e510Seric 	{
2981c71e510Seric 		register struct hostent *hp = gethostbyname(host);
2991c71e510Seric 
300a44d5a5eSeric 		if (hp == NULL)
3017aa493c5Seric 			return (EX_NOHOST);
3021c71e510Seric 		bmove(hp->h_addr, (char *) &SendmailAddress.sin_addr, hp->h_length);
3031c71e510Seric 	}
3041c71e510Seric 
3051c71e510Seric 	/*
3061c71e510Seric 	**  Determine the port number.
3071c71e510Seric 	*/
3081c71e510Seric 
309fd7c0790Seric 	if (port != 0)
310fd7c0790Seric 		SendmailAddress.sin_port = htons(port);
311fd7c0790Seric 	else
3121c71e510Seric 	{
3131c71e510Seric 		register struct servent *sp = getservbyname("smtp", "tcp");
3141c71e510Seric 
3151c71e510Seric 		if (sp == NULL)
3161c71e510Seric 		{
3171c71e510Seric 			syserr("makeconnection: server \"smtp\" unknown");
3181c71e510Seric 			return (EX_OSFILE);
3191c71e510Seric 		}
320fd7c0790Seric 		SendmailAddress.sin_port = sp->s_port;
3211c71e510Seric 	}
3227aa493c5Seric 
3237aa493c5Seric 	/*
3247aa493c5Seric 	**  Try to actually open the connection.
3257aa493c5Seric 	*/
3267aa493c5Seric 
3277aa493c5Seric # ifdef DEBUG
32861e4310fSeric 	if (tTd(16, 1))
3297aa493c5Seric 		printf("makeconnection (%s)\n", host);
3307aa493c5Seric # endif DEBUG
3317aa493c5Seric 
33210d42158Seric 	s = socket(AF_INET, SOCK_STREAM, 0, 0);
3337aa493c5Seric 	if (s < 0)
3347aa493c5Seric 	{
3357aa493c5Seric 		syserr("makeconnection: no socket");
3367aa493c5Seric 		goto failure;
3377aa493c5Seric 	}
3387aa493c5Seric 
3397aa493c5Seric # ifdef DEBUG
34061e4310fSeric 	if (tTd(16, 1))
3417aa493c5Seric 		printf("makeconnection: %d\n", s);
3421b6e4a15Seric 
3431b6e4a15Seric 	/* turn on network debugging? */
3441b6e4a15Seric 	if (tTd(16, 14))
3451b6e4a15Seric 		(void) setsockopt(s, SOL_SOCKET, SO_DEBUG, 0, 0);
3467aa493c5Seric # endif DEBUG
347877a6142Seric 	(void) fflush(CurEnv->e_xfp);			/* for debugging */
3484bd6a662Seric 	errno = 0;					/* for debugging */
3491c71e510Seric 	SendmailAddress.sin_family = AF_INET;
35010d42158Seric 	if (connect(s, &SendmailAddress, sizeof SendmailAddress, 0) < 0)
3517aa493c5Seric 	{
3527aa493c5Seric 		/* failure, decide if temporary or not */
3537aa493c5Seric 	failure:
3547aa493c5Seric 		switch (errno)
3557aa493c5Seric 		{
3567aa493c5Seric 		  case EISCONN:
3577aa493c5Seric 		  case ETIMEDOUT:
358292668b9Seric 		  case EINPROGRESS:
359292668b9Seric 		  case EALREADY:
360292668b9Seric 		  case EADDRINUSE:
36136bd23a8Seric 		  case EHOSTDOWN:
362292668b9Seric 		  case ENETDOWN:
363292668b9Seric 		  case ENETRESET:
364292668b9Seric 		  case ENOBUFS:
365a1645df8Seric 		  case ECONNREFUSED:
366efe49624Seric 		  case ECONNRESET:
367cae8261aSeric 		  case EHOSTUNREACH:
368dca8e1f7Seric 		  case ENETUNREACH:
3697aa493c5Seric 			/* there are others, I'm sure..... */
3707aa493c5Seric 			return (EX_TEMPFAIL);
3717aa493c5Seric 
372a44d5a5eSeric 		  case EPERM:
373a44d5a5eSeric 			/* why is this happening? */
374a44d5a5eSeric 			syserr("makeconnection: funny failure, addr=%lx, port=%x",
375a44d5a5eSeric 				SendmailAddress.sin_addr.s_addr, SendmailAddress.sin_port);
3764bd6a662Seric 			return (EX_TEMPFAIL);
377a44d5a5eSeric 
3787aa493c5Seric 		  default:
3797aa493c5Seric 			return (EX_UNAVAILABLE);
3807aa493c5Seric 		}
3817aa493c5Seric 	}
3827aa493c5Seric 
3837aa493c5Seric 	/* connection ok, put it into canonical form */
3847aa493c5Seric 	*outfile = fdopen(s, "w");
3857aa493c5Seric 	*infile = fdopen(s, "r");
3867aa493c5Seric 
387dca8e1f7Seric 	return (EX_OK);
3887aa493c5Seric }
389444eaf03Seric /*
390444eaf03Seric **  MYHOSTNAME -- return the name of this host.
391444eaf03Seric **
392444eaf03Seric **	Parameters:
393444eaf03Seric **		hostbuf -- a place to return the name of this host.
394897f1869Seric **		size -- the size of hostbuf.
395444eaf03Seric **
396444eaf03Seric **	Returns:
397444eaf03Seric **		A list of aliases for this host.
398444eaf03Seric **
399444eaf03Seric **	Side Effects:
400444eaf03Seric **		none.
401444eaf03Seric */
402444eaf03Seric 
403444eaf03Seric char **
404897f1869Seric myhostname(hostbuf, size)
405444eaf03Seric 	char hostbuf[];
406897f1869Seric 	int size;
407444eaf03Seric {
408444eaf03Seric 	extern struct hostent *gethostbyname();
409a44d5a5eSeric 	struct hostent *hp;
410897f1869Seric 	auto int i = size;
411444eaf03Seric 
412107faed7Seric 	gethostname(hostbuf, &i);
413a44d5a5eSeric 	hp = gethostbyname(hostbuf);
414a44d5a5eSeric 	if (hp != NULL)
415a44d5a5eSeric 		return (hp->h_aliases);
416444eaf03Seric 	else
417444eaf03Seric 		return (NULL);
418444eaf03Seric }
419444eaf03Seric 
420444eaf03Seric # else DAEMON
421444eaf03Seric 
422444eaf03Seric /*
423444eaf03Seric **  MYHOSTNAME -- stub version for case of no daemon code.
42421e9914dSeric **
42521e9914dSeric **	Can't convert to upper case here because might be a UUCP name.
426897f1869Seric **
427897f1869Seric **	Mark, you can change this to be anything you want......
428444eaf03Seric */
429444eaf03Seric 
430444eaf03Seric char **
431897f1869Seric myhostname(hostbuf, size)
432444eaf03Seric 	char hostbuf[];
433897f1869Seric 	int size;
434444eaf03Seric {
435444eaf03Seric 	register FILE *f;
436444eaf03Seric 
437444eaf03Seric 	hostbuf[0] = '\0';
438444eaf03Seric 	f = fopen("/usr/include/whoami", "r");
439444eaf03Seric 	if (f != NULL)
440444eaf03Seric 	{
441897f1869Seric 		(void) fgets(hostbuf, size, f);
442444eaf03Seric 		fixcrlf(hostbuf, TRUE);
443444eaf03Seric 		(void) fclose(f);
444444eaf03Seric 	}
445444eaf03Seric 	return (NULL);
446444eaf03Seric }
447d0a9e852Seric 
448d0a9e852Seric #endif DAEMON
449