xref: /csrg-svn/usr.sbin/sendmail/src/daemon.c (revision 16911)
16039Seric # include <errno.h>
24535Seric # include "sendmail.h"
34535Seric 
45978Seric #ifndef DAEMON
5*16911Seric SCCSID(@(#)daemon.c	4.11		08/11/84	(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*16911Seric SCCSID(@(#)daemon.c	4.11		08/11/84	(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 */
639610Seric 
6416144Seric int	DaemonSocket	= -1;		/* fd describing socket */
6516890Seric char	*NetName;			/* name of home (local?) network */
6616144Seric 
674535Seric getrequests()
684535Seric {
699610Seric 	int t;
707117Seric 	union wait status;
719610Seric 	register struct servent *sp;
727117Seric 
739610Seric 	/*
749610Seric 	**  Set up the address for the mailer.
759610Seric 	*/
769610Seric 
779610Seric 	sp = getservbyname("smtp", "tcp");
789610Seric 	if (sp == NULL)
799610Seric 	{
809610Seric 		syserr("server \"smtp\" unknown");
8110167Seric 		goto severe;
829610Seric 	}
839610Seric 	SendmailAddress.sin_family = AF_INET;
849610Seric 	SendmailAddress.sin_addr.s_addr = INADDR_ANY;
859740Ssam 	SendmailAddress.sin_port = sp->s_port;
869610Seric 
879610Seric 	/*
889610Seric 	**  Try to actually open the connection.
899610Seric 	*/
909610Seric 
919610Seric # ifdef DEBUG
929610Seric 	if (tTd(15, 1))
939610Seric 		printf("getrequests: port 0x%x\n", SendmailAddress.sin_port);
949610Seric # endif DEBUG
959610Seric 
969610Seric 	/* get a socket for the SMTP connection */
9710206Seric 	DaemonSocket = socket(AF_INET, SOCK_STREAM, 0, 0);
9810206Seric 	if (DaemonSocket < 0)
999610Seric 	{
1009610Seric 		/* probably another daemon already */
1019610Seric 		syserr("getrequests: can't create socket");
1029610Seric 	  severe:
1039610Seric # ifdef LOG
1049610Seric 		if (LogLevel > 0)
1059610Seric 			syslog(LOG_SALERT, "cannot get connection");
1069610Seric # endif LOG
1079610Seric 		finis();
1089610Seric 	}
10910347Seric 
11010347Seric #ifdef DEBUG
11110347Seric 	/* turn on network debugging? */
11210347Seric 	if (tTd(15, 15))
11310347Seric 		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, 0, 0);
11410347Seric #endif DEBUG
11510347Seric 
11610206Seric 	if (bind(DaemonSocket, &SendmailAddress, sizeof SendmailAddress, 0) < 0)
1179610Seric 	{
1189610Seric 		syserr("getrequests: cannot bind");
11910206Seric 		(void) close(DaemonSocket);
1209610Seric 		goto severe;
1219610Seric 	}
12210206Seric 	listen(DaemonSocket, 10);
1239610Seric 
1249610Seric # ifdef DEBUG
1259610Seric 	if (tTd(15, 1))
12610206Seric 		printf("getrequests: %d\n", DaemonSocket);
1279610Seric # endif DEBUG
1289610Seric 
1294631Seric 	for (;;)
1304631Seric 	{
13114875Seric 		register int pid;
13211147Seric 		auto int lotherend;
13311147Seric 		struct sockaddr_in otherend;
13414875Seric 		extern int RefuseLA;
13511147Seric 
13614875Seric 		/* see if we are rejecting connections */
13714875Seric 		while (getla() > RefuseLA)
13814875Seric 			sleep(5);
13914875Seric 
1409610Seric 		/* wait for a connection */
1419610Seric 		do
1429610Seric 		{
1439610Seric 			errno = 0;
1449610Seric 			lotherend = sizeof otherend;
14510206Seric 			t = accept(DaemonSocket, &otherend, &lotherend, 0);
1469610Seric 		} while (t < 0 && errno == EINTR);
1479610Seric 		if (t < 0)
1485978Seric 		{
1499610Seric 			syserr("getrequests: accept");
1509610Seric 			sleep(5);
1519610Seric 			continue;
1525978Seric 		}
1534631Seric 
1545978Seric 		/*
1555978Seric 		**  Create a subprocess to process the mail.
1565978Seric 		*/
1575978Seric 
1585978Seric # ifdef DEBUG
1597677Seric 		if (tTd(15, 2))
1609610Seric 			printf("getrequests: forking (fd = %d)\n", t);
1615978Seric # endif DEBUG
1625978Seric 
1634636Seric 		pid = fork();
1644636Seric 		if (pid < 0)
1654631Seric 		{
1664636Seric 			syserr("daemon: cannot fork");
1674636Seric 			sleep(10);
1689610Seric 			(void) close(t);
1694636Seric 			continue;
1704631Seric 		}
1714631Seric 
1724636Seric 		if (pid == 0)
1734631Seric 		{
17411147Seric 			extern struct hostent *gethostbyaddr();
17511147Seric 			register struct hostent *hp;
17611147Seric 			extern char *RealHostName;	/* srvrsmtp.c */
17711147Seric 			char buf[MAXNAME];
17811147Seric 
1794636Seric 			/*
1804636Seric 			**  CHILD -- return to caller.
18111147Seric 			**	Collect verified idea of sending host.
1824636Seric 			**	Verify calling user id if possible here.
1834636Seric 			*/
1844631Seric 
18511147Seric 			/* determine host name */
18611147Seric 			hp = gethostbyaddr(&otherend.sin_addr, sizeof otherend.sin_addr, AF_INET);
18711147Seric 			if (hp != NULL)
18816890Seric 			{
18916890Seric 				strcpy(buf, hp->h_name);
19016890Seric 				if (NetName != NULL && NetName[0] != '\0' &&
19116894Seric 				    index(hp->h_name, '.') == NULL)
19216890Seric 				{
19316890Seric 					strcat(buf, ".");
19416890Seric 					strcat(buf, NetName);
19516890Seric 				}
19616890Seric 			}
19711147Seric 			else
19816884Seric 			{
19916884Seric 				extern char *inet_ntoa();
20016884Seric 
20116884Seric 				/* produce a dotted quad */
20216884Seric 				(void) sprintf(buf, "[%s]",
20316884Seric 					inet_ntoa(otherend.sin_addr));
20416884Seric 			}
20516884Seric 
20616884Seric 			/* should we check for illegal connection here? XXX */
20716884Seric 
20811147Seric 			RealHostName = newstr(buf);
20911147Seric 
21010206Seric 			(void) close(DaemonSocket);
2119610Seric 			InChannel = fdopen(t, "r");
2129610Seric 			OutChannel = fdopen(t, "w");
2135978Seric # ifdef DEBUG
2147677Seric 			if (tTd(15, 2))
2155978Seric 				printf("getreq: returning\n");
2165978Seric # endif DEBUG
2177876Seric # ifdef LOG
2187876Seric 			if (LogLevel > 11)
2197876Seric 				syslog(LOG_DEBUG, "connected, pid=%d", getpid());
2207876Seric # endif LOG
2214636Seric 			return;
2224631Seric 		}
2234631Seric 
2244636Seric 		/*
2254636Seric 		**  PARENT -- wait for child to terminate.
2264636Seric 		**	Perhaps we should allow concurrent processing?
2274636Seric 		*/
2284631Seric 
2295978Seric # ifdef DEBUG
2307677Seric 		if (tTd(15, 2))
2315978Seric 		{
2325978Seric 			sleep(2);
2335978Seric 			printf("getreq: parent waiting\n");
2345978Seric 		}
2355978Seric # endif DEBUG
2365978Seric 
2377117Seric 		/* close the port so that others will hang (for a while) */
2389610Seric 		(void) close(t);
2397117Seric 
24013933Seric 		/* pick up old zombies */
24113933Seric 		while (wait3(&status, WNOHANG, 0) > 0)
24213933Seric 			continue;
2434631Seric 	}
2449886Seric 	/*NOTREACHED*/
2454631Seric }
2465978Seric /*
24710206Seric **  CLRDAEMON -- reset the daemon connection
24810206Seric **
24910206Seric **	Parameters:
25010206Seric **		none.
25110206Seric **
25210206Seric **	Returns:
25310206Seric **		none.
25410206Seric **
25510206Seric **	Side Effects:
25610206Seric **		releases any resources used by the passive daemon.
25710206Seric */
25810206Seric 
25910206Seric clrdaemon()
26010206Seric {
26110206Seric 	if (DaemonSocket >= 0)
26210206Seric 		(void) close(DaemonSocket);
26310206Seric 	DaemonSocket = -1;
26410206Seric }
26510206Seric /*
2666039Seric **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
2676039Seric **
2686039Seric **	Parameters:
2696039Seric **		host -- the name of the host.
2706633Seric **		port -- the port number to connect to.
2716039Seric **		outfile -- a pointer to a place to put the outfile
2726039Seric **			descriptor.
2736039Seric **		infile -- ditto for infile.
2746039Seric **
2756039Seric **	Returns:
2766039Seric **		An exit code telling whether the connection could be
2776039Seric **			made and if not why not.
2786039Seric **
2796039Seric **	Side Effects:
2806039Seric **		none.
2816039Seric */
2825978Seric 
2836633Seric makeconnection(host, port, outfile, infile)
2846039Seric 	char *host;
2857286Seric 	u_short port;
2866039Seric 	FILE **outfile;
2876039Seric 	FILE **infile;
2886039Seric {
2896039Seric 	register int s;
2906039Seric 
2916039Seric 	/*
2926039Seric 	**  Set up the address for the mailer.
2939308Seric 	**	Accept "[a.b.c.d]" syntax for host name.
2946039Seric 	*/
2956039Seric 
2969308Seric 	if (host[0] == '[')
2979308Seric 	{
29811147Seric 		long hid;
29911147Seric 		register char *p = index(host, ']');
3009308Seric 
30111147Seric 		if (p != NULL)
3029308Seric 		{
30311147Seric 			*p = '\0';
30411147Seric 			hid = inet_addr(&host[1]);
30511147Seric 			*p = ']';
3069308Seric 		}
30711147Seric 		if (p == NULL || hid == -1)
3089308Seric 		{
3099308Seric 			usrerr("Invalid numeric domain spec \"%s\"", host);
3109308Seric 			return (EX_NOHOST);
3119308Seric 		}
3129308Seric 		SendmailAddress.sin_addr.s_addr = hid;
3139308Seric 	}
3149610Seric 	else
3159610Seric 	{
3169610Seric 		register struct hostent *hp = gethostbyname(host);
3179610Seric 
31811147Seric 		if (hp == NULL)
3199610Seric 			return (EX_NOHOST);
32016884Seric 		bcopy(hp->h_addr, (char *) &SendmailAddress.sin_addr, hp->h_length);
3219610Seric 	}
3229610Seric 
3239610Seric 	/*
3249610Seric 	**  Determine the port number.
3259610Seric 	*/
3269610Seric 
32710011Seric 	if (port != 0)
32810011Seric 		SendmailAddress.sin_port = htons(port);
32910011Seric 	else
3309610Seric 	{
3319610Seric 		register struct servent *sp = getservbyname("smtp", "tcp");
3329610Seric 
3339610Seric 		if (sp == NULL)
3349610Seric 		{
3359610Seric 			syserr("makeconnection: server \"smtp\" unknown");
3369610Seric 			return (EX_OSFILE);
3379610Seric 		}
33810011Seric 		SendmailAddress.sin_port = sp->s_port;
3399610Seric 	}
3406039Seric 
3416039Seric 	/*
3426039Seric 	**  Try to actually open the connection.
3436039Seric 	*/
3446039Seric 
3456039Seric # ifdef DEBUG
3467677Seric 	if (tTd(16, 1))
3476039Seric 		printf("makeconnection (%s)\n", host);
3486039Seric # endif DEBUG
3496039Seric 
3509310Seric 	s = socket(AF_INET, SOCK_STREAM, 0, 0);
3516039Seric 	if (s < 0)
3526039Seric 	{
3536039Seric 		syserr("makeconnection: no socket");
3546039Seric 		goto failure;
3556039Seric 	}
3566039Seric 
3576039Seric # ifdef DEBUG
3587677Seric 	if (tTd(16, 1))
3596039Seric 		printf("makeconnection: %d\n", s);
36010347Seric 
36110347Seric 	/* turn on network debugging? */
36210347Seric 	if (tTd(16, 14))
36310347Seric 		(void) setsockopt(s, SOL_SOCKET, SO_DEBUG, 0, 0);
3646039Seric # endif DEBUG
3659546Seric 	(void) fflush(CurEnv->e_xfp);			/* for debugging */
36614383Seric 	errno = 0;					/* for debugging */
3679610Seric 	SendmailAddress.sin_family = AF_INET;
3689310Seric 	if (connect(s, &SendmailAddress, sizeof SendmailAddress, 0) < 0)
3696039Seric 	{
3706039Seric 		/* failure, decide if temporary or not */
3716039Seric 	failure:
3726039Seric 		switch (errno)
3736039Seric 		{
3746039Seric 		  case EISCONN:
3756039Seric 		  case ETIMEDOUT:
3766897Seric 		  case EINPROGRESS:
3776897Seric 		  case EALREADY:
3786897Seric 		  case EADDRINUSE:
37910123Seric 		  case EHOSTDOWN:
3806897Seric 		  case ENETDOWN:
3816897Seric 		  case ENETRESET:
3826897Seric 		  case ENOBUFS:
3837204Seric 		  case ECONNREFUSED:
38411546Seric 		  case ECONNRESET:
38510081Seric 		  case EHOSTUNREACH:
38610098Seric 		  case ENETUNREACH:
3876039Seric 			/* there are others, I'm sure..... */
38816884Seric 			CurEnv->e_flags &= ~EF_FATALERRS;
3896039Seric 			return (EX_TEMPFAIL);
3906039Seric 
39111147Seric 		  case EPERM:
39211147Seric 			/* why is this happening? */
39311147Seric 			syserr("makeconnection: funny failure, addr=%lx, port=%x",
39411147Seric 				SendmailAddress.sin_addr.s_addr, SendmailAddress.sin_port);
39514383Seric 			return (EX_TEMPFAIL);
39611147Seric 
3976039Seric 		  default:
3986039Seric 			return (EX_UNAVAILABLE);
3996039Seric 		}
4006039Seric 	}
4016039Seric 
4026039Seric 	/* connection ok, put it into canonical form */
4036039Seric 	*outfile = fdopen(s, "w");
4046039Seric 	*infile = fdopen(s, "r");
4056039Seric 
40610098Seric 	return (EX_OK);
4076039Seric }
40810758Seric /*
40910758Seric **  MYHOSTNAME -- return the name of this host.
41010758Seric **
41110758Seric **	Parameters:
41210758Seric **		hostbuf -- a place to return the name of this host.
41312313Seric **		size -- the size of hostbuf.
41410758Seric **
41510758Seric **	Returns:
41610758Seric **		A list of aliases for this host.
41710758Seric **
41810758Seric **	Side Effects:
41910758Seric **		none.
42010758Seric */
4216039Seric 
42210758Seric char **
42312313Seric myhostname(hostbuf, size)
42410758Seric 	char hostbuf[];
42512313Seric 	int size;
42610758Seric {
42710758Seric 	extern struct hostent *gethostbyname();
42811147Seric 	struct hostent *hp;
42910758Seric 
43016163Seric 	gethostname(hostbuf, size);
43111147Seric 	hp = gethostbyname(hostbuf);
43211147Seric 	if (hp != NULL)
43316877Seric 	{
43416877Seric 		strcpy(hostbuf, hp->h_name);
43511147Seric 		return (hp->h_aliases);
43616877Seric 	}
43710758Seric 	else
43810758Seric 		return (NULL);
43910758Seric }
440*16911Seric /*
441*16911Seric **  MAPHOSTNAME -- turn a hostname into canonical form
442*16911Seric **
443*16911Seric **	Parameters:
444*16911Seric **		hbuf -- a buffer containing a hostname.
445*16911Seric **		hbsize -- the size of hbuf.
446*16911Seric **
447*16911Seric **	Returns:
448*16911Seric **		none.
449*16911Seric **
450*16911Seric **	Side Effects:
451*16911Seric **		Looks up the host specified in hbuf.  If it is not
452*16911Seric **		the canonical name for that host, replace it with
453*16911Seric **		the canonical name.  If the name is unknown, or it
454*16911Seric **		is already the canonical name, leave it unchanged.
455*16911Seric */
45610758Seric 
457*16911Seric maphostname(hbuf, hbsize)
458*16911Seric 	char *hbuf;
459*16911Seric 	int hbsize;
460*16911Seric {
461*16911Seric 	register struct hostent *hp;
462*16911Seric 	extern struct hostent *gethostbyname();
463*16911Seric 
464*16911Seric 	makelower(hbuf);
465*16911Seric 	hp = gethostbyname(hbuf);
466*16911Seric 	if (hp != NULL)
467*16911Seric 	{
468*16911Seric 		int i = strlen(hp->h_name);
469*16911Seric 
470*16911Seric 		if (i >= hbsize)
471*16911Seric 			hp->h_name[--i] = '\0';
472*16911Seric 		strcpy(hbuf, hp->h_name);
473*16911Seric 	}
474*16911Seric }
475*16911Seric 
47610758Seric # else DAEMON
477*16911Seric /* code for systems without sophisticated networking */
47810758Seric 
47910758Seric /*
48010758Seric **  MYHOSTNAME -- stub version for case of no daemon code.
48111297Seric **
48211297Seric **	Can't convert to upper case here because might be a UUCP name.
48312313Seric **
48412313Seric **	Mark, you can change this to be anything you want......
48510758Seric */
48610758Seric 
48710758Seric char **
48812313Seric myhostname(hostbuf, size)
48910758Seric 	char hostbuf[];
49012313Seric 	int size;
49110758Seric {
49210758Seric 	register FILE *f;
49310758Seric 
49410758Seric 	hostbuf[0] = '\0';
49510758Seric 	f = fopen("/usr/include/whoami", "r");
49610758Seric 	if (f != NULL)
49710758Seric 	{
49812313Seric 		(void) fgets(hostbuf, size, f);
49910758Seric 		fixcrlf(hostbuf, TRUE);
50010758Seric 		(void) fclose(f);
50110758Seric 	}
50210758Seric 	return (NULL);
50310758Seric }
504*16911Seric /*
505*16911Seric **  MAPHOSTNAME -- turn a hostname into canonical form
506*16911Seric **
507*16911Seric **	Parameters:
508*16911Seric **		hbuf -- a buffer containing a hostname.
509*16911Seric **		hbsize -- the size of hbuf.
510*16911Seric **
511*16911Seric **	Returns:
512*16911Seric **		none.
513*16911Seric **
514*16911Seric **	Side Effects:
515*16911Seric **		Looks up the host specified in hbuf.  If it is not
516*16911Seric **		the canonical name for that host, replace it with
517*16911Seric **		the canonical name.  If the name is unknown, or it
518*16911Seric **		is already the canonical name, leave it unchanged.
519*16911Seric */
52010758Seric 
521*16911Seric /*ARGSUSED*/
522*16911Seric maphostname(hbuf, hbsize)
523*16911Seric 	char *hbuf;
524*16911Seric 	int hbsize;
525*16911Seric {
526*16911Seric 	return;
527*16911Seric }
528*16911Seric 
529*16911Seric 
5305978Seric #endif DAEMON
531