xref: /csrg-svn/usr.sbin/sendmail/src/daemon.c (revision 51995)
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.41 (Berkeley) 12/17/91 (with daemon mode)";
15 #else
16 static char sccsid[] = "@(#)daemon.c	5.41 (Berkeley) 12/17/91 (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)
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(hbuf, hbufsize)
55 **		Convert the entry in hbuf into a canonical form.  It
56 **		may not be larger than hbufsize.
57 */
58 /*
59 **  GETREQUESTS -- open mail IPC port and get requests.
60 **
61 **	Parameters:
62 **		none.
63 **
64 **	Returns:
65 **		none.
66 **
67 **	Side Effects:
68 **		Waits until some interesting activity occurs.  When
69 **		it does, a child is created to process it, and the
70 **		parent waits for completion.  Return from this
71 **		routine is always in the child.  The file pointers
72 **		"InChannel" and "OutChannel" should be set to point
73 **		to the communication channel.
74 */
75 
76 struct sockaddr_in	SendmailAddress;/* internet address of sendmail */
77 
78 int	DaemonSocket	= -1;		/* fd describing socket */
79 char	*NetName;			/* name of home (local?) network */
80 
81 getrequests()
82 {
83 	int t;
84 	register struct servent *sp;
85 	int on = 1;
86 	extern void reapchild();
87 
88 	/*
89 	**  Set up the address for the mailer.
90 	*/
91 
92 	sp = getservbyname("smtp", "tcp");
93 	if (sp == NULL)
94 	{
95 		syserr("server \"smtp\" unknown");
96 		goto severe;
97 	}
98 	SendmailAddress.sin_family = AF_INET;
99 	SendmailAddress.sin_addr.s_addr = INADDR_ANY;
100 	SendmailAddress.sin_port = sp->s_port;
101 
102 	/*
103 	**  Try to actually open the connection.
104 	*/
105 
106 	if (tTd(15, 1))
107 		printf("getrequests: port 0x%x\n", SendmailAddress.sin_port);
108 
109 	/* get a socket for the SMTP connection */
110 	DaemonSocket = socket(AF_INET, SOCK_STREAM, 0);
111 	if (DaemonSocket < 0)
112 	{
113 		/* probably another daemon already */
114 		syserr("getrequests: can't create socket");
115 	  severe:
116 # ifdef LOG
117 		if (LogLevel > 0)
118 			syslog(LOG_ALERT, "cannot get connection");
119 # endif LOG
120 		finis();
121 	}
122 
123 	/* turn on network debugging? */
124 	if (tTd(15, 15))
125 		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on);
126 
127 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on);
128 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on);
129 
130 	if (bind(DaemonSocket,
131 	    (struct sockaddr *)&SendmailAddress, sizeof SendmailAddress) < 0)
132 	{
133 		syserr("getrequests: cannot bind");
134 		(void) close(DaemonSocket);
135 		goto severe;
136 	}
137 	if (listen(DaemonSocket, 10) < 0)
138 	{
139 		syserr("getrequests: cannot listen");
140 		(void) close(DaemonSocket);
141 		goto severe;
142 	}
143 
144 	(void) signal(SIGCHLD, reapchild);
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 
154 		/* see if we are rejecting connections */
155 		while ((CurrentLA = getla()) > RefuseLA)
156 		{
157 			setproctitle("rejecting connections: load average: %.2f", (double)CurrentLA);
158 			sleep(5);
159 		}
160 
161 		/* wait for a connection */
162 		setproctitle("accepting connections");
163 		do
164 		{
165 			errno = 0;
166 			lotherend = sizeof RealHostAddr;
167 			t = accept(DaemonSocket,
168 			    (struct sockaddr *)&RealHostAddr, &lotherend);
169 		} while (t < 0 && errno == EINTR);
170 		if (t < 0)
171 		{
172 			syserr("getrequests: accept");
173 			sleep(5);
174 			continue;
175 		}
176 
177 		/*
178 		**  Create a subprocess to process the mail.
179 		*/
180 
181 		if (tTd(15, 2))
182 			printf("getrequests: forking (fd = %d)\n", t);
183 
184 		pid = fork();
185 		if (pid < 0)
186 		{
187 			syserr("daemon: cannot fork");
188 			sleep(10);
189 			(void) close(t);
190 			continue;
191 		}
192 
193 		if (pid == 0)
194 		{
195 			extern struct hostent *gethostbyaddr();
196 			register struct hostent *hp;
197 			char buf[MAXNAME];
198 
199 			/*
200 			**  CHILD -- return to caller.
201 			**	Collect verified idea of sending host.
202 			**	Verify calling user id if possible here.
203 			*/
204 
205 			(void) signal(SIGCHLD, SIG_DFL);
206 
207 			/* determine host name */
208 			hp = gethostbyaddr((char *) &RealHostAddr.sin_addr, sizeof RealHostAddr.sin_addr, AF_INET);
209 			if (hp != NULL)
210 				(void) strcpy(buf, hp->h_name);
211 			else
212 			{
213 				extern char *inet_ntoa();
214 
215 				/* produce a dotted quad */
216 				(void) sprintf(buf, "[%s]",
217 					inet_ntoa(RealHostAddr.sin_addr));
218 			}
219 
220 			/* should we check for illegal connection here? XXX */
221 
222 			RealHostName = newstr(buf);
223 
224 			(void) close(DaemonSocket);
225 			InChannel = fdopen(t, "r");
226 			OutChannel = fdopen(dup(t), "w");
227 			if (tTd(15, 2))
228 				printf("getreq: returning\n");
229 # ifdef LOG
230 			if (LogLevel > 11)
231 				syslog(LOG_DEBUG, "connected, pid=%d", getpid());
232 # endif LOG
233 			return;
234 		}
235 
236 		/* close the port so that others will hang (for a while) */
237 		(void) close(t);
238 	}
239 	/*NOTREACHED*/
240 }
241 /*
242 **  CLRDAEMON -- reset the daemon connection
243 **
244 **	Parameters:
245 **		none.
246 **
247 **	Returns:
248 **		none.
249 **
250 **	Side Effects:
251 **		releases any resources used by the passive daemon.
252 */
253 
254 clrdaemon()
255 {
256 	if (DaemonSocket >= 0)
257 		(void) close(DaemonSocket);
258 	DaemonSocket = -1;
259 }
260 /*
261 **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
262 **
263 **	Parameters:
264 **		host -- the name of the host.
265 **		port -- the port number to connect to.
266 **		outfile -- a pointer to a place to put the outfile
267 **			descriptor.
268 **		infile -- ditto for infile.
269 **
270 **	Returns:
271 **		An exit code telling whether the connection could be
272 **			made and if not why not.
273 **
274 **	Side Effects:
275 **		none.
276 */
277 
278 makeconnection(host, port, outfile, infile)
279 	char *host;
280 	u_short port;
281 	FILE **outfile;
282 	FILE **infile;
283 {
284 	register int i, s;
285 	register struct hostent *hp = (struct hostent *)NULL;
286 	extern char *inet_ntoa();
287 	int sav_errno;
288 #ifdef NAMED_BIND
289 	extern int h_errno;
290 #endif
291 
292 	/*
293 	**  Set up the address for the mailer.
294 	**	Accept "[a.b.c.d]" syntax for host name.
295 	*/
296 
297 #ifdef NAMED_BIND
298 	h_errno = 0;
299 #endif
300 	errno = 0;
301 
302 	if (host[0] == '[')
303 	{
304 		long hid;
305 		register char *p = index(host, ']');
306 
307 		if (p != NULL)
308 		{
309 			*p = '\0';
310 			hid = inet_addr(&host[1]);
311 			*p = ']';
312 		}
313 		if (p == NULL || hid == -1)
314 		{
315 			usrerr("Invalid numeric domain spec \"%s\"", host);
316 			return (EX_NOHOST);
317 		}
318 		SendmailAddress.sin_addr.s_addr = hid;
319 	}
320 	else
321 	{
322 		hp = gethostbyname(host);
323 		if (hp == NULL)
324 		{
325 #ifdef NAMED_BIND
326 			if (errno == ETIMEDOUT || h_errno == TRY_AGAIN)
327 				return (EX_TEMPFAIL);
328 
329 			/* if name server is specified, assume temp fail */
330 			if (errno == ECONNREFUSED && UseNameServer)
331 				return (EX_TEMPFAIL);
332 #endif
333 
334 			/*
335 			**  XXX Should look for mail forwarder record here
336 			**  XXX if (h_errno == NO_ADDRESS).
337 			*/
338 
339 			return (EX_NOHOST);
340 		}
341 		bcopy(hp->h_addr, (char *) &SendmailAddress.sin_addr, hp->h_length);
342 		i = 1;
343 	}
344 
345 	/*
346 	**  Determine the port number.
347 	*/
348 
349 	if (port != 0)
350 		SendmailAddress.sin_port = htons(port);
351 	else
352 	{
353 		register struct servent *sp = getservbyname("smtp", "tcp");
354 
355 		if (sp == NULL)
356 		{
357 			syserr("makeconnection: server \"smtp\" unknown");
358 			return (EX_OSFILE);
359 		}
360 		SendmailAddress.sin_port = sp->s_port;
361 	}
362 
363 	/*
364 	**  Try to actually open the connection.
365 	*/
366 
367 again:
368 	if (tTd(16, 1))
369 		printf("makeconnection (%s [%s])\n", host,
370 		    inet_ntoa(SendmailAddress.sin_addr));
371 
372 	s = socket(AF_INET, SOCK_STREAM, 0);
373 	if (s < 0)
374 	{
375 		syserr("makeconnection: no socket");
376 		sav_errno = errno;
377 		goto failure;
378 	}
379 
380 	if (tTd(16, 1))
381 		printf("makeconnection: %d\n", s);
382 
383 	/* turn on network debugging? */
384 	if (tTd(16, 14))
385 	{
386 		int on = 1;
387 		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on);
388 	}
389 	if (CurEnv->e_xfp != NULL)
390 		(void) fflush(CurEnv->e_xfp);		/* for debugging */
391 	errno = 0;					/* for debugging */
392 	SendmailAddress.sin_family = AF_INET;
393 	if (connect(s,
394 	    (struct sockaddr *)&SendmailAddress, sizeof SendmailAddress) < 0)
395 	{
396 		sav_errno = errno;
397 		(void) close(s);
398 		if (hp && hp->h_addr_list[i])
399 		{
400 			bcopy(hp->h_addr_list[i++],
401 			    (char *)&SendmailAddress.sin_addr, hp->h_length);
402 			goto again;
403 		}
404 
405 		/* failure, decide if temporary or not */
406 	failure:
407 		switch (sav_errno)
408 		{
409 		  case EISCONN:
410 		  case ETIMEDOUT:
411 		  case EINPROGRESS:
412 		  case EALREADY:
413 		  case EADDRINUSE:
414 		  case EHOSTDOWN:
415 		  case ENETDOWN:
416 		  case ENETRESET:
417 		  case ENOBUFS:
418 		  case ECONNREFUSED:
419 		  case ECONNRESET:
420 		  case EHOSTUNREACH:
421 		  case ENETUNREACH:
422 #ifdef ENOSR
423 		  case ENOSR:
424 #endif
425 			/* there are others, I'm sure..... */
426 			return (EX_TEMPFAIL);
427 
428 		  case EPERM:
429 			/* why is this happening? */
430 			syserr("makeconnection: funny failure, addr=%lx, port=%x",
431 				SendmailAddress.sin_addr.s_addr, SendmailAddress.sin_port);
432 			return (EX_TEMPFAIL);
433 
434 		  default:
435 			{
436 				extern char *errstring();
437 
438 				message(Arpa_Info, "%s", errstring(sav_errno));
439 				return (EX_UNAVAILABLE);
440 			}
441 		}
442 	}
443 
444 	/* connection ok, put it into canonical form */
445 	*outfile = fdopen(s, "w");
446 	*infile = fdopen(dup(s), "r");
447 
448 	return (EX_OK);
449 }
450 /*
451 **  MYHOSTNAME -- return the name of this host.
452 **
453 **	Parameters:
454 **		hostbuf -- a place to return the name of this host.
455 **		size -- the size of hostbuf.
456 **
457 **	Returns:
458 **		A list of aliases for this host.
459 **
460 **	Side Effects:
461 **		none.
462 */
463 
464 char **
465 myhostname(hostbuf, size)
466 	char hostbuf[];
467 	int size;
468 {
469 	extern struct hostent *gethostbyname();
470 	struct hostent *hp;
471 
472 	if (gethostname(hostbuf, size) < 0)
473 	{
474 		(void) strcpy(hostbuf, "localhost");
475 	}
476 	hp = gethostbyname(hostbuf);
477 	if (hp != NULL)
478 	{
479 		(void) strcpy(hostbuf, hp->h_name);
480 		return (hp->h_aliases);
481 	}
482 	else
483 		return (NULL);
484 }
485 /*
486  *  MAPHOSTNAME -- turn a hostname into canonical form
487  *
488  *	Parameters:
489  *		hbuf -- a buffer containing a hostname.
490  *		hbsize -- the size of hbuf.
491  *
492  *	Returns:
493  *		TRUE if the host name was mapped.
494  *		FALSE otherwise.
495  *
496  *	Side Effects:
497  *		Looks up the host specified in hbuf.  If it is not
498  *		the canonical name for that host, replace it with
499  *		the canonical name.  If the name is unknown, or it
500  *		is already the canonical name, leave it unchanged.
501  */
502 
503 bool
504 maphostname(hbuf, hbsize)
505 	char *hbuf;
506 	int hbsize;
507 {
508 	register struct hostent *hp;
509 	u_long in_addr;
510 	char ptr[256], *cp;
511 	struct hostent *gethostbyaddr();
512 
513 	/*
514 	 * If first character is a bracket, then it is an address
515 	 * lookup.  Address is copied into a temporary buffer to
516 	 * strip the brackets and to preserve hbuf if address is
517 	 * unknown.
518 	 */
519 	if (*hbuf != '[')
520 		return (getcanonname(hbuf, hbsize));
521 	if ((cp = index(strcpy(ptr, hbuf), ']')) == NULL)
522 		return (FALSE);
523 	*cp = '\0';
524 	in_addr = inet_addr(&ptr[1]);
525 	hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET);
526 	if (hp == NULL)
527 		return (FALSE);
528 	if (strlen(hp->h_name) >= hbsize)
529 		hp->h_name[hbsize - 1] = '\0';
530 	(void)strcpy(hbuf, hp->h_name);
531 	return (TRUE);
532 }
533 
534 # else DAEMON
535 /* code for systems without sophisticated networking */
536 
537 /*
538 **  MYHOSTNAME -- stub version for case of no daemon code.
539 **
540 **	Can't convert to upper case here because might be a UUCP name.
541 **
542 **	Mark, you can change this to be anything you want......
543 */
544 
545 char **
546 myhostname(hostbuf, size)
547 	char hostbuf[];
548 	int size;
549 {
550 	register FILE *f;
551 
552 	hostbuf[0] = '\0';
553 	f = fopen("/usr/include/whoami", "r");
554 	if (f != NULL)
555 	{
556 		(void) fgets(hostbuf, size, f);
557 		fixcrlf(hostbuf, TRUE);
558 		(void) fclose(f);
559 	}
560 	return (NULL);
561 }
562 /*
563 **  MAPHOSTNAME -- turn a hostname into canonical form
564 **
565 **	Parameters:
566 **		hbuf -- a buffer containing a hostname.
567 **		hbsize -- the size of hbuf.
568 **
569 **	Returns:
570 **		TRUE if the hostname was mapped.
571 **		FALSE otherwise.
572 **
573 **	Side Effects:
574 **		Looks up the host specified in hbuf.  If it is not
575 **		the canonical name for that host, replace it with
576 **		the canonical name.  If the name is unknown, or it
577 **		is already the canonical name, leave it unchanged.
578 */
579 
580 /*ARGSUSED*/
581 bool
582 maphostname(hbuf, hbsize)
583 	char *hbuf;
584 	int hbsize;
585 {
586 	return (FALSE);
587 }
588 
589 #endif DAEMON
590