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