xref: /csrg-svn/usr.sbin/sendmail/src/daemon.c (revision 58728)
1 /*
2  * Copyright (c) 1983 Eric P. Allman
3  * Copyright (c) 1988 Regents of the University of California.
4  * All rights reserved.
5  *
6  * %sccs.include.redist.c%
7  */
8 
9 #include <errno.h>
10 #include <signal.h>
11 #include "sendmail.h"
12 
13 #ifndef lint
14 #ifdef DAEMON
15 static char sccsid[] = "@(#)daemon.c	6.19 (Berkeley) 03/18/93 (with daemon mode)";
16 #else
17 static char sccsid[] = "@(#)daemon.c	6.19 (Berkeley) 03/18/93 (without daemon mode)";
18 #endif
19 #endif /* not lint */
20 
21 #ifdef DAEMON
22 
23 # include <netdb.h>
24 # include <sys/wait.h>
25 # include <sys/time.h>
26 
27 /*
28 **  DAEMON.C -- routines to use when running as a daemon.
29 **
30 **	This entire file is highly dependent on the 4.2 BSD
31 **	interprocess communication primitives.  No attempt has
32 **	been made to make this file portable to Version 7,
33 **	Version 6, MPX files, etc.  If you should try such a
34 **	thing yourself, I recommend chucking the entire file
35 **	and starting from scratch.  Basic semantics are:
36 **
37 **	getrequests()
38 **		Opens a port and initiates a connection.
39 **		Returns in a child.  Must set InChannel and
40 **		OutChannel appropriately.
41 **	clrdaemon()
42 **		Close any open files associated with getting
43 **		the connection; this is used when running the queue,
44 **		etc., to avoid having extra file descriptors during
45 **		the queue run and to avoid confusing the network
46 **		code (if it cares).
47 **	makeconnection(host, port, outfile, infile, usesecureport)
48 **		Make a connection to the named host on the given
49 **		port.  Set *outfile and *infile to the files
50 **		appropriate for communication.  Returns zero on
51 **		success, else an exit status describing the
52 **		error.
53 **	maphostname(map, hbuf, hbufsiz, avp)
54 **		Convert the entry in hbuf into a canonical form.
55 */
56 /*
57 **  GETREQUESTS -- open mail IPC port and get requests.
58 **
59 **	Parameters:
60 **		none.
61 **
62 **	Returns:
63 **		none.
64 **
65 **	Side Effects:
66 **		Waits until some interesting activity occurs.  When
67 **		it does, a child is created to process it, and the
68 **		parent waits for completion.  Return from this
69 **		routine is always in the child.  The file pointers
70 **		"InChannel" and "OutChannel" should be set to point
71 **		to the communication channel.
72 */
73 
74 int	DaemonSocket	= -1;		/* fd describing socket */
75 
76 getrequests()
77 {
78 	int t;
79 	register struct servent *sp;
80 	int on = 1;
81 	bool refusingconnections = TRUE;
82 	FILE *pidf;
83 	struct sockaddr_in srvraddr;
84 	extern void reapchild();
85 
86 	/*
87 	**  Set up the address for the mailer.
88 	*/
89 
90 	sp = getservbyname("smtp", "tcp");
91 	if (sp == NULL)
92 	{
93 		syserr("554 server \"smtp\" unknown");
94 		goto severe;
95 	}
96 	srvraddr.sin_family = AF_INET;
97 	srvraddr.sin_addr.s_addr = INADDR_ANY;
98 	srvraddr.sin_port = sp->s_port;
99 
100 	/*
101 	**  Try to actually open the connection.
102 	*/
103 
104 	if (tTd(15, 1))
105 		printf("getrequests: port 0x%x\n", srvraddr.sin_port);
106 
107 	/* get a socket for the SMTP connection */
108 	DaemonSocket = socket(AF_INET, SOCK_STREAM, 0);
109 	if (DaemonSocket < 0)
110 	{
111 		/* probably another daemon already */
112 		syserr("getrequests: can't create socket");
113 	  severe:
114 # ifdef LOG
115 		if (LogLevel > 0)
116 			syslog(LOG_ALERT, "problem creating SMTP socket");
117 # endif /* LOG */
118 		finis();
119 	}
120 
121 	/* turn on network debugging? */
122 	if (tTd(15, 101))
123 		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on);
124 
125 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on);
126 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on);
127 
128 	if (bind(DaemonSocket, (struct sockaddr *)&srvraddr, sizeof srvraddr) < 0)
129 	{
130 		syserr("getrequests: cannot bind");
131 		(void) close(DaemonSocket);
132 		goto severe;
133 	}
134 
135 	(void) signal(SIGCHLD, reapchild);
136 
137 	/* write the pid to the log file for posterity */
138 	pidf = fopen(PidFile, "w");
139 	if (pidf != NULL)
140 	{
141 		fprintf(pidf, "%d\n", getpid());
142 		fclose(pidf);
143 	}
144 
145 
146 	if (tTd(15, 1))
147 		printf("getrequests: %d\n", DaemonSocket);
148 
149 	for (;;)
150 	{
151 		register int pid;
152 		auto int lotherend;
153 		extern bool refuseconnections();
154 
155 		/* see if we are rejecting connections */
156 		CurrentLA = getla();
157 		if (refuseconnections())
158 		{
159 			if (!refusingconnections)
160 			{
161 				/* don't queue so peer will fail quickly */
162 				(void) listen(DaemonSocket, 0);
163 				refusingconnections = TRUE;
164 			}
165 			setproctitle("rejecting connections: load average: %d",
166 				CurrentLA);
167 			sleep(5);
168 			continue;
169 		}
170 
171 		if (refusingconnections)
172 		{
173 			/* start listening again */
174 			if (listen(DaemonSocket, 10) < 0)
175 			{
176 				syserr("getrequests: cannot listen");
177 				(void) close(DaemonSocket);
178 				goto severe;
179 			}
180 			setproctitle("accepting connections");
181 			refusingconnections = FALSE;
182 		}
183 
184 		/* wait for a connection */
185 		do
186 		{
187 			errno = 0;
188 			lotherend = sizeof RealHostAddr;
189 			t = accept(DaemonSocket,
190 			    (struct sockaddr *)&RealHostAddr, &lotherend);
191 		} while (t < 0 && errno == EINTR);
192 		if (t < 0)
193 		{
194 			syserr("getrequests: accept");
195 			sleep(5);
196 			continue;
197 		}
198 
199 		/*
200 		**  Create a subprocess to process the mail.
201 		*/
202 
203 		if (tTd(15, 2))
204 			printf("getrequests: forking (fd = %d)\n", t);
205 
206 		pid = fork();
207 		if (pid < 0)
208 		{
209 			syserr("daemon: cannot fork");
210 			sleep(10);
211 			(void) close(t);
212 			continue;
213 		}
214 
215 		if (pid == 0)
216 		{
217 			extern struct hostent *gethostbyaddr();
218 			register struct hostent *hp;
219 			char buf[MAXNAME];
220 			extern char *inet_ntoa();
221 
222 			/*
223 			**  CHILD -- return to caller.
224 			**	Collect verified idea of sending host.
225 			**	Verify calling user id if possible here.
226 			*/
227 
228 			(void) signal(SIGCHLD, SIG_DFL);
229 
230 			/* determine host name */
231 			hp = gethostbyaddr((char *) &RealHostAddr.sin_addr, sizeof RealHostAddr.sin_addr, AF_INET);
232 			if (hp != NULL)
233 				(void) strcpy(buf, hp->h_name);
234 			else
235 			{
236 				/* produce a dotted quad */
237 				(void) sprintf(buf, "[%s]",
238 					inet_ntoa(RealHostAddr.sin_addr));
239 			}
240 
241 #ifdef LOG
242 			if (LogLevel > 10)
243 			{
244 				/* log connection information */
245 				syslog(LOG_INFO, "connect from %s (%s)",
246 					buf, inet_ntoa(RealHostAddr.sin_addr));
247 			}
248 #endif
249 
250 			/* should we check for illegal connection here? XXX */
251 
252 			RealHostName = newstr(buf);
253 
254 			(void) close(DaemonSocket);
255 			InChannel = fdopen(t, "r");
256 			OutChannel = fdopen(dup(t), "w");
257 			if (tTd(15, 2))
258 				printf("getreq: returning\n");
259 			return;
260 		}
261 
262 		/* close the port so that others will hang (for a while) */
263 		(void) close(t);
264 	}
265 	/*NOTREACHED*/
266 }
267 /*
268 **  CLRDAEMON -- reset the daemon connection
269 **
270 **	Parameters:
271 **		none.
272 **
273 **	Returns:
274 **		none.
275 **
276 **	Side Effects:
277 **		releases any resources used by the passive daemon.
278 */
279 
280 clrdaemon()
281 {
282 	if (DaemonSocket >= 0)
283 		(void) close(DaemonSocket);
284 	DaemonSocket = -1;
285 }
286 /*
287 **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
288 **
289 **	Parameters:
290 **		host -- the name of the host.
291 **		port -- the port number to connect to.
292 **		mci -- a pointer to the mail connection information
293 **			structure to be filled in.
294 **		usesecureport -- if set, use a low numbered (reserved)
295 **			port to provide some rudimentary authentication.
296 **
297 **	Returns:
298 **		An exit code telling whether the connection could be
299 **			made and if not why not.
300 **
301 **	Side Effects:
302 **		none.
303 */
304 
305 struct sockaddr_in	CurHostAddr;		/* address of current host */
306 
307 int
308 makeconnection(host, port, mci, usesecureport)
309 	char *host;
310 	u_short port;
311 	register MCI *mci;
312 	bool usesecureport;
313 {
314 	register int i, s;
315 	register struct hostent *hp = (struct hostent *)NULL;
316 	struct sockaddr_in addr;
317 	int sav_errno;
318 	extern char *inet_ntoa();
319 #ifdef NAMED_BIND
320 	extern int h_errno;
321 #endif
322 
323 	/*
324 	**  Set up the address for the mailer.
325 	**	Accept "[a.b.c.d]" syntax for host name.
326 	*/
327 
328 #ifdef NAMED_BIND
329 	h_errno = 0;
330 #endif
331 	errno = 0;
332 
333 	if (host[0] == '[')
334 	{
335 		long hid;
336 		register char *p = strchr(host, ']');
337 
338 		if (p != NULL)
339 		{
340 			*p = '\0';
341 			hid = inet_addr(&host[1]);
342 			if (hid == -1)
343 			{
344 				/* try it as a host name (avoid MX lookup) */
345 				hp = gethostbyname(&host[1]);
346 				*p = ']';
347 				goto gothostent;
348 			}
349 			*p = ']';
350 		}
351 		if (p == NULL)
352 		{
353 			usrerr("553 Invalid numeric domain spec \"%s\"", host);
354 			return (EX_NOHOST);
355 		}
356 		addr.sin_addr.s_addr = hid;
357 	}
358 	else
359 	{
360 		hp = gethostbyname(host);
361 gothostent:
362 		if (hp == NULL)
363 		{
364 #ifdef NAMED_BIND
365 			if (errno == ETIMEDOUT || h_errno == TRY_AGAIN)
366 				return (EX_TEMPFAIL);
367 
368 			/* if name server is specified, assume temp fail */
369 			if (errno == ECONNREFUSED && UseNameServer)
370 				return (EX_TEMPFAIL);
371 #endif
372 			return (EX_NOHOST);
373 		}
374 		bcopy(hp->h_addr, (char *) &addr.sin_addr, hp->h_length);
375 		i = 1;
376 	}
377 
378 	/*
379 	**  Determine the port number.
380 	*/
381 
382 	if (port != 0)
383 		addr.sin_port = htons(port);
384 	else
385 	{
386 		register struct servent *sp = getservbyname("smtp", "tcp");
387 
388 		if (sp == NULL)
389 		{
390 			syserr("554 makeconnection: server \"smtp\" unknown");
391 			return (EX_OSERR);
392 		}
393 		addr.sin_port = sp->s_port;
394 	}
395 
396 	/*
397 	**  Try to actually open the connection.
398 	*/
399 
400 	for (;;)
401 	{
402 		if (tTd(16, 1))
403 			printf("makeconnection (%s [%s])\n", host,
404 			    inet_ntoa(addr.sin_addr));
405 
406 		/* save for logging */
407 		CurHostAddr = addr;
408 
409 		if (usesecureport)
410 		{
411 			int rport = IPPORT_RESERVED - 1;
412 
413 			s = rresvport(&rport);
414 		}
415 		else
416 		{
417 			s = socket(AF_INET, SOCK_STREAM, 0);
418 		}
419 		if (s < 0)
420 		{
421 			sav_errno = errno;
422 			syserr("makeconnection: no socket");
423 			goto failure;
424 		}
425 
426 		if (tTd(16, 1))
427 			printf("makeconnection: fd=%d\n", s);
428 
429 		/* turn on network debugging? */
430 		if (tTd(16, 101))
431 		{
432 			int on = 1;
433 			(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG,
434 					  (char *)&on, sizeof on);
435 		}
436 		if (CurEnv->e_xfp != NULL)
437 			(void) fflush(CurEnv->e_xfp);		/* for debugging */
438 		errno = 0;					/* for debugging */
439 		addr.sin_family = AF_INET;
440 		if (connect(s, (struct sockaddr *) &addr, sizeof addr) >= 0)
441 			break;
442 
443 		/* couldn't connect.... figure out why */
444 		sav_errno = errno;
445 		(void) close(s);
446 		if (hp && hp->h_addr_list[i])
447 		{
448 			if (tTd(16, 1))
449 				printf("Connect failed; trying new address....\n");
450 			bcopy(hp->h_addr_list[i++], (char *) &addr.sin_addr,
451 					hp->h_length);
452 			continue;
453 		}
454 
455 		/* failure, decide if temporary or not */
456 	failure:
457 		if (transienterror(sav_errno))
458 			return EX_TEMPFAIL;
459 		else if (sav_errno == EPERM)
460 		{
461 			/* why is this happening? */
462 			syserr("makeconnection: funny failure, addr=%lx, port=%x",
463 				addr.sin_addr.s_addr, addr.sin_port);
464 			return (EX_TEMPFAIL);
465 		}
466 		else
467 		{
468 			extern char *errstring();
469 
470 			message("%s", errstring(sav_errno));
471 			return (EX_UNAVAILABLE);
472 		}
473 	}
474 
475 	/* connection ok, put it into canonical form */
476 	mci->mci_out = fdopen(s, "w");
477 	mci->mci_in = fdopen(dup(s), "r");
478 
479 	return (EX_OK);
480 }
481 /*
482 **  MYHOSTNAME -- return the name of this host.
483 **
484 **	Parameters:
485 **		hostbuf -- a place to return the name of this host.
486 **		size -- the size of hostbuf.
487 **
488 **	Returns:
489 **		A list of aliases for this host.
490 **
491 **	Side Effects:
492 **		Sets the MyIpAddrs buffer to a list of my IP addresses.
493 */
494 
495 struct in_addr	MyIpAddrs[MAXIPADDR + 1];
496 
497 char **
498 myhostname(hostbuf, size)
499 	char hostbuf[];
500 	int size;
501 {
502 	register struct hostent *hp;
503 	extern struct hostent *gethostbyname();
504 
505 	if (gethostname(hostbuf, size) < 0)
506 	{
507 		(void) strcpy(hostbuf, "localhost");
508 	}
509 	hp = gethostbyname(hostbuf);
510 	if (hp != NULL)
511 	{
512 		(void) strncpy(hostbuf, hp->h_name, size - 1);
513 		hostbuf[size - 1] = '\0';
514 
515 		if (hp->h_addrtype == AF_INET && hp->h_length == 4)
516 		{
517 			register int i;
518 
519 			for (i = 0; i < MAXIPADDR; i++)
520 			{
521 				if (hp->h_addr_list[i] == NULL)
522 					break;
523 				MyIpAddrs[i].s_addr = *(u_long *) hp->h_addr_list[i];
524 			}
525 			MyIpAddrs[i].s_addr = 0;
526 		}
527 
528 		return (hp->h_aliases);
529 	}
530 	else
531 		return (NULL);
532 }
533 /*
534 **  GETREALHOSTNAME -- get the real host name asociated with a file descriptor
535 **
536 **	Parameters:
537 **		fd -- the descriptor
538 **
539 **	Returns:
540 **		The host name associated with this descriptor, if it can
541 **			be determined.
542 **		NULL otherwise.
543 **
544 **	Side Effects:
545 **		none
546 */
547 
548 char *
549 getrealhostname(fd)
550 	int fd;
551 {
552 	register struct hostent *hp;
553 	struct sockaddr_in sin;
554 	int sinlen;
555 	char hbuf[MAXNAME];
556 	extern struct hostent *gethostbyaddr();
557 	extern char *inet_ntoa();
558 
559 	if (getsockname(fd, (struct sockaddr *) &sin, &sinlen) < 0 ||
560 	    sinlen <= 0)
561 		return NULL;
562 	hp = gethostbyaddr((char *) &sin.sin_addr, sizeof sin.sin_addr,
563 			   sin.sin_family);
564 	if (hp != NULL)
565 		(void) strcpy(hbuf, hp->h_name);
566 	else
567 		(void) sprintf(hbuf, "[%s]", inet_ntoa(sin.sin_addr));
568 	return hbuf;
569 }
570 /*
571 **  MAPHOSTNAME -- turn a hostname into canonical form
572 **
573 **	Parameters:
574 **		map -- a pointer to this map (unused).
575 **		hbuf -- a buffer containing a hostname.
576 **		hbsize -- the size of hbuf.
577 **		avp -- unused -- for compatibility with other mapping
578 **			functions.
579 **
580 **	Returns:
581 **		The mapping, if found.
582 **		NULL if no mapping found.
583 **
584 **	Side Effects:
585 **		Looks up the host specified in hbuf.  If it is not
586 **		the canonical name for that host, return the canonical
587 **		name.
588 */
589 
590 char *
591 maphostname(map, hbuf, hbsize, avp)
592 	MAP *map;
593 	char *hbuf;
594 	int hbsize;
595 	char **avp;
596 {
597 	register struct hostent *hp;
598 	u_long in_addr;
599 	char *cp;
600 	int i;
601 	struct hostent *gethostbyaddr();
602 
603 	/* allow room for null */
604 	hbsize--;
605 
606 	/*
607 	 * If first character is a bracket, then it is an address
608 	 * lookup.  Address is copied into a temporary buffer to
609 	 * strip the brackets and to preserve hbuf if address is
610 	 * unknown.
611 	 */
612 
613 	if (*hbuf != '[')
614 	{
615 		extern bool getcanonname();
616 
617 		if (getcanonname(hbuf, hbsize))
618 			return hbuf;
619 		else
620 			return NULL;
621 	}
622 	if ((cp = strchr(hbuf, ']')) == NULL)
623 		return (NULL);
624 	*cp = '\0';
625 	in_addr = inet_addr(&hbuf[1]);
626 
627 	/* check to see if this is one of our addresses */
628 	for (i = 0; MyIpAddrs[i].s_addr != 0; i++)
629 	{
630 		if (MyIpAddrs[i].s_addr == in_addr)
631 		{
632 			strncpy(hbuf, MyHostName, hbsize);
633 			hbuf[hbsize] = '\0';
634 			return hbuf;
635 		}
636 	}
637 
638 	/* nope -- ask the name server */
639 	hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET);
640 	if (hp == NULL)
641 		return (NULL);
642 
643 	/* found a match -- copy out */
644 	if (strlen(hp->h_name) > hbsize)
645 		hp->h_name[hbsize] = '\0';
646 	(void) strcpy(hbuf, hp->h_name);
647 	return hbuf;
648 }
649 
650 # else /* DAEMON */
651 /* code for systems without sophisticated networking */
652 
653 /*
654 **  MYHOSTNAME -- stub version for case of no daemon code.
655 **
656 **	Can't convert to upper case here because might be a UUCP name.
657 **
658 **	Mark, you can change this to be anything you want......
659 */
660 
661 char **
662 myhostname(hostbuf, size)
663 	char hostbuf[];
664 	int size;
665 {
666 	register FILE *f;
667 
668 	hostbuf[0] = '\0';
669 	f = fopen("/usr/include/whoami", "r");
670 	if (f != NULL)
671 	{
672 		(void) fgets(hostbuf, size, f);
673 		fixcrlf(hostbuf, TRUE);
674 		(void) fclose(f);
675 	}
676 	return (NULL);
677 }
678 /*
679 **  GETREALHOSTNAME -- get the real host name asociated with a file descriptor
680 **
681 **	Parameters:
682 **		fd -- the descriptor
683 **
684 **	Returns:
685 **		The host name associated with this descriptor, if it can
686 **			be determined.
687 **		NULL otherwise.
688 **
689 **	Side Effects:
690 **		none
691 */
692 
693 char *
694 getrealhostname(fd)
695 	int fd;
696 {
697 	return NULL;
698 }
699 /*
700 **  MAPHOSTNAME -- turn a hostname into canonical form
701 **
702 **	Parameters:
703 **		map -- a pointer to the database map.
704 **		hbuf -- a buffer containing a hostname.
705 **		avp -- a pointer to a (cf file defined) argument vector.
706 **
707 **	Returns:
708 **		mapped host name
709 **		FALSE otherwise.
710 **
711 **	Side Effects:
712 **		Looks up the host specified in hbuf.  If it is not
713 **		the canonical name for that host, replace it with
714 **		the canonical name.  If the name is unknown, or it
715 **		is already the canonical name, leave it unchanged.
716 */
717 
718 /*ARGSUSED*/
719 char *
720 maphostname(map, hbuf, hbsize, avp)
721 	MAP *map;
722 	char *hbuf;
723 	int hbsize;
724 	char **avp;
725 {
726 	return NULL;
727 }
728 
729 #endif /* DAEMON */
730