xref: /csrg-svn/usr.sbin/sendmail/src/daemon.c (revision 25027)
1 /*
2 **  Sendmail
3 **  Copyright (c) 1983  Eric P. Allman
4 **  Berkeley, California
5 **
6 **  Copyright (c) 1983 Regents of the University of California.
7 **  All rights reserved.  The Berkeley software License Agreement
8 **  specifies the terms and conditions for redistribution.
9 */
10 
11 
12 # include <errno.h>
13 # include "sendmail.h"
14 
15 # ifndef DAEMON
16 # ifndef lint
17 static char	SccsId[] = "@(#)daemon.c	5.12 (Berkeley) 09/24/85	(w/o daemon mode)";
18 # endif not lint
19 # else
20 
21 # include <netdb.h>
22 # include <sys/signal.h>
23 # include <sys/wait.h>
24 # include <sys/time.h>
25 # include <sys/resource.h>
26 
27 # ifndef lint
28 static char	SccsId[] = "@(#)daemon.c	5.12 (Berkeley) 09/24/85 (with daemon mode)";
29 # endif not lint
30 
31 /*
32 **  DAEMON.C -- routines to use when running as a daemon.
33 **
34 **	This entire file is highly dependent on the 4.2 BSD
35 **	interprocess communication primitives.  No attempt has
36 **	been made to make this file portable to Version 7,
37 **	Version 6, MPX files, etc.  If you should try such a
38 **	thing yourself, I recommend chucking the entire file
39 **	and starting from scratch.  Basic semantics are:
40 **
41 **	getrequests()
42 **		Opens a port and initiates a connection.
43 **		Returns in a child.  Must set InChannel and
44 **		OutChannel appropriately.
45 **	clrdaemon()
46 **		Close any open files associated with getting
47 **		the connection; this is used when running the queue,
48 **		etc., to avoid having extra file descriptors during
49 **		the queue run and to avoid confusing the network
50 **		code (if it cares).
51 **	makeconnection(host, port, outfile, infile)
52 **		Make a connection to the named host on the given
53 **		port.  Set *outfile and *infile to the files
54 **		appropriate for communication.  Returns zero on
55 **		success, else an exit status describing the
56 **		error.
57 **
58 **	The semantics of both of these should be clean.
59 */
60 /*
61 **  GETREQUESTS -- open mail IPC port and get requests.
62 **
63 **	Parameters:
64 **		none.
65 **
66 **	Returns:
67 **		none.
68 **
69 **	Side Effects:
70 **		Waits until some interesting activity occurs.  When
71 **		it does, a child is created to process it, and the
72 **		parent waits for completion.  Return from this
73 **		routine is always in the child.  The file pointers
74 **		"InChannel" and "OutChannel" should be set to point
75 **		to the communication channel.
76 */
77 
78 struct sockaddr_in	SendmailAddress;/* internet address of sendmail */
79 
80 int	DaemonSocket	= -1;		/* fd describing socket */
81 char	*NetName;			/* name of home (local?) network */
82 
83 getrequests()
84 {
85 	int t;
86 	register struct servent *sp;
87 	int on = 1;
88 	extern reapchild();
89 
90 	/*
91 	**  Set up the address for the mailer.
92 	*/
93 
94 	sp = getservbyname("smtp", "tcp");
95 	if (sp == NULL)
96 	{
97 		syserr("server \"smtp\" unknown");
98 		goto severe;
99 	}
100 	SendmailAddress.sin_family = AF_INET;
101 	SendmailAddress.sin_addr.s_addr = INADDR_ANY;
102 	SendmailAddress.sin_port = sp->s_port;
103 
104 	/*
105 	**  Try to actually open the connection.
106 	*/
107 
108 # ifdef DEBUG
109 	if (tTd(15, 1))
110 		printf("getrequests: port 0x%x\n", SendmailAddress.sin_port);
111 # endif DEBUG
112 
113 	/* get a socket for the SMTP connection */
114 	DaemonSocket = socket(AF_INET, SOCK_STREAM, 0);
115 	if (DaemonSocket < 0)
116 	{
117 		/* probably another daemon already */
118 		syserr("getrequests: can't create socket");
119 	  severe:
120 # ifdef LOG
121 		if (LogLevel > 0)
122 			syslog(LOG_ALERT, "cannot get connection");
123 # endif LOG
124 		finis();
125 	}
126 
127 #ifdef DEBUG
128 	/* turn on network debugging? */
129 	if (tTd(15, 15))
130 		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on);
131 #endif DEBUG
132 
133 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on);
134 	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on);
135 
136 	if (bind(DaemonSocket, &SendmailAddress, sizeof SendmailAddress) < 0)
137 	{
138 		syserr("getrequests: cannot bind");
139 		(void) close(DaemonSocket);
140 		goto severe;
141 	}
142 	if (listen(DaemonSocket, 10) < 0)
143 	{
144 		syserr("getrequests: cannot listen");
145 		(void) close(DaemonSocket);
146 		goto severe;
147 	}
148 
149 	(void) signal(SIGCHLD, reapchild);
150 
151 # ifdef DEBUG
152 	if (tTd(15, 1))
153 		printf("getrequests: %d\n", DaemonSocket);
154 # endif DEBUG
155 
156 	for (;;)
157 	{
158 		register int pid;
159 		auto int lotherend;
160 		struct sockaddr_in otherend;
161 		extern int RefuseLA;
162 
163 		/* see if we are rejecting connections */
164 		while (getla() > RefuseLA)
165 			sleep(5);
166 
167 		/* wait for a connection */
168 		do
169 		{
170 			errno = 0;
171 			lotherend = sizeof otherend;
172 			t = accept(DaemonSocket, &otherend, &lotherend);
173 		} while (t < 0 && errno == EINTR);
174 		if (t < 0)
175 		{
176 			syserr("getrequests: accept");
177 			sleep(5);
178 			continue;
179 		}
180 
181 		/*
182 		**  Create a subprocess to process the mail.
183 		*/
184 
185 # ifdef DEBUG
186 		if (tTd(15, 2))
187 			printf("getrequests: forking (fd = %d)\n", t);
188 # endif DEBUG
189 
190 		pid = fork();
191 		if (pid < 0)
192 		{
193 			syserr("daemon: cannot fork");
194 			sleep(10);
195 			(void) close(t);
196 			continue;
197 		}
198 
199 		if (pid == 0)
200 		{
201 			extern struct hostent *gethostbyaddr();
202 			register struct hostent *hp;
203 			char buf[MAXNAME];
204 
205 			/*
206 			**  CHILD -- return to caller.
207 			**	Collect verified idea of sending host.
208 			**	Verify calling user id if possible here.
209 			*/
210 
211 			(void) signal(SIGCHLD, SIG_DFL);
212 
213 			/* determine host name */
214 			hp = gethostbyaddr(&otherend.sin_addr, sizeof otherend.sin_addr, AF_INET);
215 			if (hp != NULL)
216 			{
217 				(void) strcpy(buf, hp->h_name);
218 				if (NetName != NULL && NetName[0] != '\0' &&
219 				    index(hp->h_name, '.') == NULL)
220 				{
221 					(void) strcat(buf, ".");
222 					(void) strcat(buf, NetName);
223 				}
224 			}
225 			else
226 			{
227 				extern char *inet_ntoa();
228 
229 				/* produce a dotted quad */
230 				(void) sprintf(buf, "[%s]",
231 					inet_ntoa(otherend.sin_addr));
232 			}
233 
234 			/* should we check for illegal connection here? XXX */
235 
236 			RealHostName = newstr(buf);
237 
238 			(void) close(DaemonSocket);
239 			InChannel = fdopen(t, "r");
240 			OutChannel = fdopen(dup(t), "w");
241 # ifdef DEBUG
242 			if (tTd(15, 2))
243 				printf("getreq: returning\n");
244 # endif DEBUG
245 # ifdef LOG
246 			if (LogLevel > 11)
247 				syslog(LOG_DEBUG, "connected, pid=%d", getpid());
248 # endif LOG
249 			return;
250 		}
251 
252 		/* close the port so that others will hang (for a while) */
253 		(void) close(t);
254 	}
255 	/*NOTREACHED*/
256 }
257 /*
258 **  REAPCHILD -- pick up the body of my child, lest it become a zombie
259 **
260 **	Parameters:
261 **		none.
262 **
263 **	Returns:
264 **		none.
265 **
266 **	Side Effects:
267 **		Picks up zombies.
268 */
269 
270 reapchild()
271 {
272 	union wait status;
273 
274 	while (wait3(&status, WNOHANG, (struct rusage *) NULL) > 0)
275 		continue;
276 }
277 /*
278 **  CLRDAEMON -- reset the daemon connection
279 **
280 **	Parameters:
281 **		none.
282 **
283 **	Returns:
284 **		none.
285 **
286 **	Side Effects:
287 **		releases any resources used by the passive daemon.
288 */
289 
290 clrdaemon()
291 {
292 	if (DaemonSocket >= 0)
293 		(void) close(DaemonSocket);
294 	DaemonSocket = -1;
295 }
296 /*
297 **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
298 **
299 **	Parameters:
300 **		host -- the name of the host.
301 **		port -- the port number to connect to.
302 **		outfile -- a pointer to a place to put the outfile
303 **			descriptor.
304 **		infile -- ditto for infile.
305 **
306 **	Returns:
307 **		An exit code telling whether the connection could be
308 **			made and if not why not.
309 **
310 **	Side Effects:
311 **		none.
312 */
313 
314 makeconnection(host, port, outfile, infile)
315 	char *host;
316 	u_short port;
317 	FILE **outfile;
318 	FILE **infile;
319 {
320 	register int s;
321 
322 	/*
323 	**  Set up the address for the mailer.
324 	**	Accept "[a.b.c.d]" syntax for host name.
325 	*/
326 
327 	if (host[0] == '[')
328 	{
329 		long hid;
330 		register char *p = index(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 		SendmailAddress.sin_addr.s_addr = hid;
344 	}
345 	else
346 	{
347 		register struct hostent *hp = gethostbyname(host);
348 
349 		if (errno == ETIMEDOUT)
350 		{
351 			CurEnv->e_flags &= ~EF_FATALERRS;
352 			return (EX_TEMPFAIL);
353 		}
354 		if (hp == NULL)
355 			return (EX_NOHOST);
356 		bcopy(hp->h_addr, (char *) &SendmailAddress.sin_addr, hp->h_length);
357 	}
358 
359 	/*
360 	**  Determine the port number.
361 	*/
362 
363 	if (port != 0)
364 		SendmailAddress.sin_port = htons(port);
365 	else
366 	{
367 		register struct servent *sp = getservbyname("smtp", "tcp");
368 
369 		if (sp == NULL)
370 		{
371 			syserr("makeconnection: server \"smtp\" unknown");
372 			return (EX_OSFILE);
373 		}
374 		SendmailAddress.sin_port = sp->s_port;
375 	}
376 
377 	/*
378 	**  Try to actually open the connection.
379 	*/
380 
381 # ifdef DEBUG
382 	if (tTd(16, 1))
383 		printf("makeconnection (%s)\n", host);
384 # endif DEBUG
385 
386 	s = socket(AF_INET, SOCK_STREAM, 0);
387 	if (s < 0)
388 	{
389 		syserr("makeconnection: no socket");
390 		goto failure;
391 	}
392 
393 # ifdef DEBUG
394 	if (tTd(16, 1))
395 		printf("makeconnection: %d\n", s);
396 
397 	/* turn on network debugging? */
398 	if (tTd(16, 14))
399 	{
400 		int on = 1;
401 		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on);
402 	}
403 # endif DEBUG
404 	(void) fflush(CurEnv->e_xfp);			/* for debugging */
405 	errno = 0;					/* for debugging */
406 	SendmailAddress.sin_family = AF_INET;
407 	if (connect(s, &SendmailAddress, sizeof SendmailAddress) < 0)
408 	{
409 		/* failure, decide if temporary or not */
410 	failure:
411 		switch (errno)
412 		{
413 		  case EISCONN:
414 		  case ETIMEDOUT:
415 		  case EINPROGRESS:
416 		  case EALREADY:
417 		  case EADDRINUSE:
418 		  case EHOSTDOWN:
419 		  case ENETDOWN:
420 		  case ENETRESET:
421 		  case ENOBUFS:
422 		  case ECONNREFUSED:
423 		  case ECONNRESET:
424 		  case EHOSTUNREACH:
425 		  case ENETUNREACH:
426 			/* there are others, I'm sure..... */
427 			CurEnv->e_flags &= ~EF_FATALERRS;
428 			return (EX_TEMPFAIL);
429 
430 		  case EPERM:
431 			/* why is this happening? */
432 			syserr("makeconnection: funny failure, addr=%lx, port=%x",
433 				SendmailAddress.sin_addr.s_addr, SendmailAddress.sin_port);
434 			return (EX_TEMPFAIL);
435 
436 		  default:
437 			return (EX_UNAVAILABLE);
438 		}
439 	}
440 
441 	/* connection ok, put it into canonical form */
442 	*outfile = fdopen(s, "w");
443 	*infile = fdopen(s, "r");
444 
445 	return (EX_OK);
446 }
447 /*
448 **  MYHOSTNAME -- return the name of this host.
449 **
450 **	Parameters:
451 **		hostbuf -- a place to return the name of this host.
452 **		size -- the size of hostbuf.
453 **
454 **	Returns:
455 **		A list of aliases for this host.
456 **
457 **	Side Effects:
458 **		none.
459 */
460 
461 char **
462 myhostname(hostbuf, size)
463 	char hostbuf[];
464 	int size;
465 {
466 	extern struct hostent *gethostbyname();
467 	struct hostent *hp;
468 
469 	if (gethostname(hostbuf, size) < 0)
470 	{
471 		(void) strcpy(hostbuf, "localhost");
472 	}
473 	hp = gethostbyname(hostbuf);
474 	if (hp != NULL)
475 	{
476 		(void) strcpy(hostbuf, hp->h_name);
477 		return (hp->h_aliases);
478 	}
479 	else
480 		return (NULL);
481 }
482 /*
483 **  MAPHOSTNAME -- turn a hostname into canonical form
484 **
485 **	Parameters:
486 **		hbuf -- a buffer containing a hostname.
487 **		hbsize -- the size of hbuf.
488 **
489 **	Returns:
490 **		none.
491 **
492 **	Side Effects:
493 **		Looks up the host specified in hbuf.  If it is not
494 **		the canonical name for that host, replace it with
495 **		the canonical name.  If the name is unknown, or it
496 **		is already the canonical name, leave it unchanged.
497 */
498 
499 maphostname(hbuf, hbsize)
500 	char *hbuf;
501 	int hbsize;
502 {
503 	register struct hostent *hp;
504 	extern struct hostent *gethostbyname();
505 
506 	makelower(hbuf);
507 	hp = gethostbyname(hbuf);
508 	if (hp != NULL)
509 	{
510 		int i = strlen(hp->h_name);
511 
512 		if (i >= hbsize)
513 			hp->h_name[--i] = '\0';
514 		(void) strcpy(hbuf, hp->h_name);
515 	}
516 }
517 
518 # else DAEMON
519 /* code for systems without sophisticated networking */
520 
521 /*
522 **  MYHOSTNAME -- stub version for case of no daemon code.
523 **
524 **	Can't convert to upper case here because might be a UUCP name.
525 **
526 **	Mark, you can change this to be anything you want......
527 */
528 
529 char **
530 myhostname(hostbuf, size)
531 	char hostbuf[];
532 	int size;
533 {
534 	register FILE *f;
535 
536 	hostbuf[0] = '\0';
537 	f = fopen("/usr/include/whoami", "r");
538 	if (f != NULL)
539 	{
540 		(void) fgets(hostbuf, size, f);
541 		fixcrlf(hostbuf, TRUE);
542 		(void) fclose(f);
543 	}
544 	return (NULL);
545 }
546 /*
547 **  MAPHOSTNAME -- turn a hostname into canonical form
548 **
549 **	Parameters:
550 **		hbuf -- a buffer containing a hostname.
551 **		hbsize -- the size of hbuf.
552 **
553 **	Returns:
554 **		none.
555 **
556 **	Side Effects:
557 **		Looks up the host specified in hbuf.  If it is not
558 **		the canonical name for that host, replace it with
559 **		the canonical name.  If the name is unknown, or it
560 **		is already the canonical name, leave it unchanged.
561 */
562 
563 /*ARGSUSED*/
564 maphostname(hbuf, hbsize)
565 	char *hbuf;
566 	int hbsize;
567 {
568 	return;
569 }
570 
571 
572 #endif DAEMON
573