xref: /csrg-svn/usr.sbin/sendmail/src/daemon.c (revision 10342)
1 # include <errno.h>
2 # include "sendmail.h"
3 
4 #ifndef DAEMON
5 SCCSID(@(#)daemon.c	3.45		01/16/83	(w/o daemon mode));
6 #else
7 
8 #include <sys/socket.h>
9 #include <netinet/in.h>
10 #include <netdb.h>
11 #include <wait.h>
12 
13 SCCSID(@(#)daemon.c	3.45		01/16/83	(with daemon mode));
14 
15 /*
16 **  DAEMON.C -- routines to use when running as a daemon.
17 **
18 **	This entire file is highly dependent on the 4.2 BSD
19 **	interprocess communication primitives.  No attempt has
20 **	been made to make this file portable to Version 7,
21 **	Version 6, MPX files, etc.  If you should try such a
22 **	thing yourself, I recommend chucking the entire file
23 **	and starting from scratch.  Basic semantics are:
24 **
25 **	getrequests()
26 **		Opens a port and initiates a connection.
27 **		Returns in a child.  Must set InChannel and
28 **		OutChannel appropriately.
29 **	clrdaemon()
30 **		Close any open files associated with getting
31 **		the connection; this is used when running the queue,
32 **		etc., to avoid having extra file descriptors during
33 **		the queue run and to avoid confusing the network
34 **		code (if it cares).
35 **	makeconnection(host, port, outfile, infile)
36 **		Make a connection to the named host on the given
37 **		port.  Set *outfile and *infile to the files
38 **		appropriate for communication.  Returns zero on
39 **		success, else an exit status describing the
40 **		error.
41 **
42 **	The semantics of both of these should be clean.
43 */
44 /*
45 **  GETREQUESTS -- open mail IPC port and get requests.
46 **
47 **	Parameters:
48 **		none.
49 **
50 **	Returns:
51 **		none.
52 **
53 **	Side Effects:
54 **		Waits until some interesting activity occurs.  When
55 **		it does, a child is created to process it, and the
56 **		parent waits for completion.  Return from this
57 **		routine is always in the child.  The file pointers
58 **		"InChannel" and "OutChannel" should be set to point
59 **		to the communication channel.
60 */
61 
62 struct sockaddr_in	SendmailAddress;/* internet address of sendmail */
63 int	DaemonSocket = -1;		/* fd describing socket */
64 int	MaxConnections = 4;		/* maximum simultaneous sendmails */
65 
66 getrequests()
67 {
68 	int t;
69 	union wait status;
70 	int numconnections = 0;
71 	register struct servent *sp;
72 
73 	/*
74 	**  Set up the address for the mailer.
75 	*/
76 
77 	sp = getservbyname("smtp", "tcp");
78 	if (sp == NULL)
79 	{
80 		syserr("server \"smtp\" unknown");
81 		goto severe;
82 	}
83 	SendmailAddress.sin_family = AF_INET;
84 	SendmailAddress.sin_addr.s_addr = INADDR_ANY;
85 	SendmailAddress.sin_port = sp->s_port;
86 
87 	/*
88 	**  Try to actually open the connection.
89 	*/
90 
91 # ifdef DEBUG
92 	if (tTd(15, 1))
93 		printf("getrequests: port 0x%x\n", SendmailAddress.sin_port);
94 # endif DEBUG
95 
96 	/* get a socket for the SMTP connection */
97 	DaemonSocket = socket(AF_INET, SOCK_STREAM, 0, 0);
98 	if (DaemonSocket < 0)
99 	{
100 		/* probably another daemon already */
101 		syserr("getrequests: can't create socket");
102 	  severe:
103 # ifdef LOG
104 		if (LogLevel > 0)
105 			syslog(LOG_SALERT, "cannot get connection");
106 # endif LOG
107 		finis();
108 	}
109 	if (bind(DaemonSocket, &SendmailAddress, sizeof SendmailAddress, 0) < 0)
110 	{
111 		syserr("getrequests: cannot bind");
112 		(void) close(DaemonSocket);
113 		goto severe;
114 	}
115 	listen(DaemonSocket, 10);
116 
117 # ifdef DEBUG
118 	if (tTd(15, 1))
119 		printf("getrequests: %d\n", DaemonSocket);
120 # endif DEBUG
121 
122 	for (;;)
123 	{
124 		/* wait for a connection */
125 		register int pid;
126 
127 		do
128 		{
129 			auto int lotherend;
130 			struct sockaddr otherend;
131 
132 			errno = 0;
133 			lotherend = sizeof otherend;
134 			t = accept(DaemonSocket, &otherend, &lotherend, 0);
135 		} while (t < 0 && errno == EINTR);
136 		if (t < 0)
137 		{
138 			syserr("getrequests: accept");
139 			sleep(5);
140 			continue;
141 		}
142 
143 		/*
144 		**  Create a subprocess to process the mail.
145 		*/
146 
147 # ifdef DEBUG
148 		if (tTd(15, 2))
149 			printf("getrequests: forking (fd = %d)\n", t);
150 # endif DEBUG
151 
152 		pid = fork();
153 		if (pid < 0)
154 		{
155 			syserr("daemon: cannot fork");
156 			sleep(10);
157 			(void) close(t);
158 			continue;
159 		}
160 
161 		if (pid == 0)
162 		{
163 			/*
164 			**  CHILD -- return to caller.
165 			**	Verify calling user id if possible here.
166 			*/
167 
168 			(void) close(DaemonSocket);
169 			InChannel = fdopen(t, "r");
170 			OutChannel = fdopen(t, "w");
171 # ifdef DEBUG
172 			if (tTd(15, 2))
173 				printf("getreq: returning\n");
174 # endif DEBUG
175 # ifdef LOG
176 			if (LogLevel > 11)
177 				syslog(LOG_DEBUG, "connected, pid=%d", getpid());
178 # endif LOG
179 			return;
180 		}
181 
182 		/*
183 		**  PARENT -- wait for child to terminate.
184 		**	Perhaps we should allow concurrent processing?
185 		*/
186 
187 # ifdef DEBUG
188 		if (tTd(15, 2))
189 		{
190 			sleep(2);
191 			printf("getreq: parent waiting\n");
192 		}
193 # endif DEBUG
194 
195 		/* close the port so that others will hang (for a while) */
196 		(void) close(t);
197 
198 		/* pick up old zombies; implement load limiting */
199 		numconnections++;
200 		while (wait3(&status, numconnections < MaxConnections ? WNOHANG : 0, 0) > 0)
201 			numconnections--;
202 	}
203 	/*NOTREACHED*/
204 }
205 /*
206 **  CLRDAEMON -- reset the daemon connection
207 **
208 **	Parameters:
209 **		none.
210 **
211 **	Returns:
212 **		none.
213 **
214 **	Side Effects:
215 **		releases any resources used by the passive daemon.
216 */
217 
218 clrdaemon()
219 {
220 	if (DaemonSocket >= 0)
221 		(void) close(DaemonSocket);
222 	DaemonSocket = -1;
223 }
224 /*
225 **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
226 **
227 **	Parameters:
228 **		host -- the name of the host.
229 **		port -- the port number to connect to.
230 **		outfile -- a pointer to a place to put the outfile
231 **			descriptor.
232 **		infile -- ditto for infile.
233 **
234 **	Returns:
235 **		An exit code telling whether the connection could be
236 **			made and if not why not.
237 **
238 **	Side Effects:
239 **		none.
240 */
241 
242 makeconnection(host, port, outfile, infile)
243 	char *host;
244 	u_short port;
245 	FILE **outfile;
246 	FILE **infile;
247 {
248 	register int s;
249 
250 	/*
251 	**  Set up the address for the mailer.
252 	**	Accept "[a.b.c.d]" syntax for host name.
253 	*/
254 
255 	if (host[0] == '[')
256 	{
257 		long hid = 0;
258 		int i, j;
259 		register char *p = host;
260 
261 		for (i = 3; i >= 0 && *p != ']' && *p != '\0'; i--)
262 		{
263 			j = 0;
264 			while (isdigit(*++p))
265 				j = j * 10 + (*p - '0');
266 			if (*p != (i == 0 ? ']' : '.') || j > 255 || j < 0)
267 				break;
268 			hid |= j << ((3 - i) * 8);
269 		}
270 		if (i >= 0 || *p != ']' || *++p != '\0')
271 		{
272 			usrerr("Invalid numeric domain spec \"%s\"", host);
273 			return (EX_NOHOST);
274 		}
275 		SendmailAddress.sin_addr.s_addr = hid;
276 	}
277 	else
278 	{
279 		register struct hostent *hp = gethostbyname(host);
280 
281 		if (hp == 0)
282 			return (EX_NOHOST);
283 		bmove(hp->h_addr, (char *) &SendmailAddress.sin_addr, hp->h_length);
284 	}
285 
286 	/*
287 	**  Determine the port number.
288 	*/
289 
290 	if (port != 0)
291 		SendmailAddress.sin_port = htons(port);
292 	else
293 	{
294 		register struct servent *sp = getservbyname("smtp", "tcp");
295 
296 		if (sp == NULL)
297 		{
298 			syserr("makeconnection: server \"smtp\" unknown");
299 			return (EX_OSFILE);
300 		}
301 		SendmailAddress.sin_port = sp->s_port;
302 	}
303 
304 	/*
305 	**  Try to actually open the connection.
306 	*/
307 
308 # ifdef DEBUG
309 	if (tTd(16, 1))
310 		printf("makeconnection (%s)\n", host);
311 # endif DEBUG
312 
313 	s = socket(AF_INET, SOCK_STREAM, 0, 0);
314 	if (s < 0)
315 	{
316 		syserr("makeconnection: no socket");
317 		goto failure;
318 	}
319 
320 # ifdef DEBUG
321 	if (tTd(16, 1))
322 		printf("makeconnection: %d\n", s);
323 # endif DEBUG
324 	(void) fflush(CurEnv->e_xfp);			/* for debugging */
325 	SendmailAddress.sin_family = AF_INET;
326 	bind(s, &SendmailAddress, sizeof SendmailAddress, 0);
327 	if (connect(s, &SendmailAddress, sizeof SendmailAddress, 0) < 0)
328 	{
329 		/* failure, decide if temporary or not */
330 	failure:
331 		switch (errno)
332 		{
333 		  case EISCONN:
334 		  case ETIMEDOUT:
335 		  case EINPROGRESS:
336 		  case EALREADY:
337 		  case EADDRINUSE:
338 		  case EHOSTDOWN:
339 		  case ENETDOWN:
340 		  case ENETRESET:
341 		  case ENOBUFS:
342 		  case ECONNREFUSED:
343 		  case EHOSTUNREACH:
344 		  case ENETUNREACH:
345 			/* there are others, I'm sure..... */
346 			return (EX_TEMPFAIL);
347 
348 		  default:
349 			return (EX_UNAVAILABLE);
350 		}
351 	}
352 
353 	/* connection ok, put it into canonical form */
354 	*outfile = fdopen(s, "w");
355 	*infile = fdopen(s, "r");
356 
357 	return (EX_OK);
358 }
359 
360 #endif DAEMON
361