179
186
goto ERROR_RETURN;
189
/* Get the data for the local interface address. */
191
if (getsockname(accept_socket, (struct sockaddr *)(&interface_sockaddr),
194
log_write(0, LOG_MAIN|LOG_PANIC, "getsockname() failed: %s",
196
smtp_printf("421 Local problem: getsockname() failed; please try again later\r\n");
200
interface_address = host_ntoa(-1, &interface_sockaddr, NULL, &interface_port);
201
DEBUG(D_interface) debug_printf("interface address=%s port=%d\n",
202
interface_address, interface_port);
204
/* Build a string identifying the remote host and, if requested, the port and
205
the local interface data. This is for logging; at the end of this function the
206
memory is reclaimed. */
208
whofrom = string_append(whofrom, &wfsize, &wfptr, 3, "[", sender_host_address, "]");
210
if ((log_extra_selector & LX_incoming_port) != 0)
211
whofrom = string_append(whofrom, &wfsize, &wfptr, 2, ":", string_sprintf("%d",
214
if ((log_extra_selector & LX_incoming_interface) != 0)
215
whofrom = string_append(whofrom, &wfsize, &wfptr, 4, " I=[",
216
interface_address, "]:", string_sprintf("%d", interface_port));
218
whofrom[wfptr] = 0; /* Terminate the newly-built string */
182
220
/* Check maximum number of connections. We do not check for reserved
183
221
connections or unacceptable hosts here. That is done in the subprocess because
184
222
it might take some time. */
283
321
"from this IP address; please try again later.\r\n");
284
322
log_write(L_connection_reject,
285
323
LOG_MAIN, "Connection from %s refused: too many connections "
286
"from that IP address", sender_host_address);
324
"from that IP address", whofrom);
287
325
goto ERROR_RETURN;
291
329
/* OK, the connection count checks have been passed. Before we can fork the
292
330
accepting process, we must first log the connection if requested. This logging
293
used to happen in the subprocess, but doing that that means that the value of
331
used to happen in the subprocess, but doing that means that the value of
294
332
smtp_accept_count can be out of step by the time it is logged. So we have to do
295
333
the logging here and accept the performance cost. Note that smtp_accept_count
296
334
hasn't yet been incremented to take account of this connection.
298
336
In order to minimize the cost (because this is going to happen for every
299
connection), do a selector test here. This saves ploughing through the
300
generalized logging code each time when the selector is false. */
337
connection), do a preliminary selector test here. This saves ploughing through
338
the generalized logging code each time when the selector is false. If the
339
selector is set, check whether the host is on the list for logging. If not,
340
arrange to unset the selector in the subprocess. */
302
342
if ((log_write_selector & L_smtp_connection) != 0)
304
if ((log_extra_selector & LX_incoming_port) == 0)
305
log_write(0, LOG_MAIN, "SMTP connection from [%s] "
306
"(TCP/IP connection count = %d)",
307
sender_host_address, smtp_accept_count + 1);
344
uschar *list = hosts_connection_nolog;
345
if (list != NULL && verify_check_host(&list) == OK)
346
use_log_write_selector &= ~L_smtp_connection;
309
log_write(0, LOG_MAIN, "SMTP connection from [%s]:%d "
310
"(TCP/IP connection count = %d)",
311
sender_host_address, sender_host_port, smtp_accept_count + 1);
314
/* Get the data for the local interface address. We do this in the
315
main process so that we can easily give up if it fails. */
317
if (getsockname(accept_socket, (struct sockaddr *)(&interface_sockaddr),
320
log_write(0, LOG_MAIN|LOG_PANIC, "getsockname() failed: %s",
322
smtp_printf("421 Local problem: getsockname() failed; please try again later\r\n");
348
log_write(L_smtp_connection, LOG_MAIN, "SMTP connection from %s "
349
"(TCP/IP connection count = %d)", whofrom, smtp_accept_count + 1);
326
352
/* Now we can fork the accepting process; do a lookup tidy, just in case any
337
363
int queue_only_reason = 0;
364
int old_pool = store_pool;
365
int save_debug_selector = debug_selector;
338
366
BOOL local_queue_only;
339
367
#ifdef SA_NOCLDWAIT
340
368
struct sigaction act;
343
/* Get the local interface address into some permanent store, and also set
346
uschar *buffer = store_get_perm(sizeof(ip_address_item));
347
interface_address = host_ntoa(-1, &interface_sockaddr, buffer,
349
DEBUG(D_interface) debug_printf("interface address=%s port=%d\n",
350
interface_address, interface_port);
371
/* May have been modified for the subprocess */
373
log_write_selector = use_log_write_selector;
375
/* Get the local interface address into permanent store */
377
store_pool = POOL_PERM;
378
interface_address = string_copy(interface_address);
379
store_pool = old_pool;
381
/* Check for a tls-on-connect port */
383
if (host_is_tls_on_connect_port(interface_port)) tls_on_connect = TRUE;
352
385
/* Expand smtp_active_hostname if required. We do not do this any earlier,
353
386
because it may depend on the local interface address (indeed, that is most
400
433
/* Attempt to get an id from the sending machine via the RFC 1413
401
434
protocol. We do this in the sub-process in order not to hold up the
402
435
main process if there is any delay. Then set up the fullhost information
403
in case there is no HELO/EHLO. */
436
in case there is no HELO/EHLO.
438
If debugging is enabled only for the daemon, we must turn if off while
439
finding the id, but turn it on again afterwards so that information about the
440
incoming connection is output. */
442
if (debug_daemon) debug_selector = 0;
405
443
verify_get_ident(IDENT_PORT);
406
444
host_build_sender_fullhost();
445
debug_selector = save_debug_selector;
409
448
debug_printf("Process %d is handling incoming connection from %s\n",
410
449
(int)getpid(), sender_fullhost);
451
/* Now disable debugging permanently if it's required only for the daemon
454
if (debug_daemon) debug_selector = 0;
412
456
/* If there are too many child processes for immediate delivery,
413
457
set the local_queue_only flag, which is initialized from the
414
458
configured value and may therefore already be TRUE. Leave logging
779
/*************************************************
780
* Handle terminating subprocesses *
781
*************************************************/
783
/* Handle the termination of child processes. Theoretically, this need be done
784
only when sigchld_seen is TRUE, but rumour has it that some systems lose
785
SIGCHLD signals at busy times, so to be on the safe side, this function is
786
called each time round. It shouldn't be too expensive.
793
handle_ending_processes(void)
798
while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
801
DEBUG(D_any) debug_printf("child %d ended: status=0x%x\n", (int)pid,
804
/* If it's a listening daemon for which we are keeping track of individual
805
subprocesses, deal with an accepting process that has terminated. */
807
if (smtp_slots != NULL)
809
for (i = 0; i < smtp_accept_max; i++)
811
if (smtp_slots[i].pid == pid)
813
if (smtp_slots[i].host_address != NULL)
814
store_free(smtp_slots[i].host_address);
815
smtp_slots[i] = empty_smtp_slot;
816
if (--smtp_accept_count < 0) smtp_accept_count = 0;
817
DEBUG(D_any) debug_printf("%d SMTP accept process%s now running\n",
818
smtp_accept_count, (smtp_accept_count == 1)? "" : "es");
822
if (i < smtp_accept_max) continue; /* Found an accepting process */
825
/* If it wasn't an accepting process, see if it was a queue-runner
826
process that we are tracking. */
828
if (queue_pid_slots != NULL)
830
for (i = 0; i < queue_run_max; i++)
832
if (queue_pid_slots[i] == pid)
834
queue_pid_slots[i] = 0;
835
if (--queue_run_count < 0) queue_run_count = 0;
836
DEBUG(D_any) debug_printf("%d queue-runner process%s now running\n",
837
queue_run_count, (queue_run_count == 1)? "" : "es");
733
847
/*************************************************
1077
/* We now close all open file descriptors that we know about, and disconnect
1078
from the controlling terminal, unless background_daemon is unset. This is
1079
always unset when debugging, but can also be forced. Most modern Unixes seem to
1080
have setsid() for getting rid of the controlling terminal. For any OS that
1081
doesn't, setsid() can be #defined as a no-op, or as something else. */
1193
/* The variable background_daemon is always false when debugging, but
1194
can also be forced false in order to keep a non-debugging daemon in the
1195
foreground. If background_daemon is true, close all open file descriptors that
1196
we know about, but then re-open stdin, stdout, and stderr to /dev/null.
1198
This is protection against any called functions (in libraries, or in
1199
Perl, or whatever) that think they can write to stderr (or stdout). Before this
1200
was added, it was quite likely that an SMTP connection would use one of these
1201
file descriptors, in which case writing random stuff to it caused chaos.
1203
Then disconnect from the controlling terminal, Most modern Unixes seem to have
1204
setsid() for getting rid of the controlling terminal. For any OS that doesn't,
1205
setsid() can be #defined as a no-op, or as something else. */
1083
1207
if (background_daemon)
1085
log_close_all(); /* Just in case anything was logged earlier */
1086
search_tidyup(); /* Just in case any were used in reading the config. */
1087
close(0); /* Get rid of stdin/stdout/stderr */
1209
log_close_all(); /* Just in case anything was logged earlier */
1210
search_tidyup(); /* Just in case any were used in reading the config. */
1211
(void)close(0); /* Get rid of stdin/stdout/stderr */
1214
exim_nullstd(); /* Connect stdin/stdout/stderr to /dev/null */
1090
1215
log_stderr = NULL; /* So no attempt to copy paniclog output */
1092
1217
/* If the parent process of this one has pid == 1, we are re-initializing the
1093
daemon as the result of a SIGHUP. In this case, there is no need to do any
1094
forking, because the controlling terminal has long gone. Otherwise, fork,
1095
in case current process is a process group leader (see 'man setsid' for an
1218
daemon as the result of a SIGHUP. In this case, there is no need to do
1219
anything, because the controlling terminal has long gone. Otherwise, fork, in
1220
case current process is a process group leader (see 'man setsid' for an
1221
explanation) before calling setsid(). */
1098
1223
if (getppid() != 1)
1197
1321
DEBUG(D_any) debug_printf("wildcard IPv4 bind() failed after IPv6 "
1198
1322
"listen() success; EADDRINUSE ignored\n");
1199
close(listen_sockets[sk]);
1323
(void)close(listen_sockets[sk]);
1200
1324
goto SKIP_SOCKET;
1202
1326
msg = US strerror(errno);
1203
1327
addr = wildcard? ((af == AF_INET6)? US"(any IPv6)" : US"(any IPv4)") :
1329
if (daemon_startup_retries <= 0)
1206
1330
log_write(0, LOG_MAIN|LOG_PANIC_DIE,
1207
1331
"socket bind() to port %d for address %s failed: %s: "
1208
1332
"daemon abandoned", ipa->port, addr, msg);
1209
1333
log_write(0, LOG_MAIN, "socket bind() to port %d for address %s "
1210
"failed: %s: waiting before trying again", ipa->port, addr, msg);
1334
"failed: %s: waiting %s before trying again (%d more %s)",
1335
ipa->port, addr, msg, readconf_printtime(daemon_startup_sleep),
1336
daemon_startup_retries, (daemon_startup_retries > 1)? "tries" : "try");
1337
daemon_startup_retries--;
1338
sleep(daemon_startup_sleep);
1350
1479
US"no queue runs";
1353
uschar *sinfo = tls_on_connect ?
1354
US"listening for SMTPS on" : US"listening for SMTP on";
1356
uschar *sinfo = US"listening for SMTP on";
1359
1481
/* Build a list of listening addresses in big_buffer, but limit it to 10
1360
items. The style is for backwards compatibility. */
1362
for (i = 0, ipa = addresses; i < 10 && ipa != NULL; i++, ipa = ipa->next)
1364
if (ipa->address[0] == ':' && ipa->address[1] == 0)
1482
items. The style is for backwards compatibility.
1484
It is now possible to have some ports listening for SMTPS (the old,
1485
deprecated protocol that starts TLS without using STARTTLS), and others
1486
listening for standard SMTP. Keep their listings separate. */
1488
for (j = 0; j < 2; j++)
1490
for (i = 0, ipa = addresses; i < 10 && ipa != NULL; i++, ipa = ipa->next)
1366
if (ipa->next != NULL && ipa->next->address[0] == 0 &&
1367
ipa->next->port == ipa->port)
1492
/* First time round, look for SMTP ports; second time round, look for
1493
SMTPS ports. For the first one of each, insert leading text. */
1495
if (host_is_tls_on_connect_port(ipa->port) == (j > 0))
1369
(void)sprintf(CS p, " port %d (IPv6 and IPv4)", ipa->port);
1499
if (smtp_ports++ == 0)
1501
memcpy(p, "SMTP on", 8);
1507
if (smtps_ports++ == 0)
1509
(void)sprintf(CS p, "%sSMTPS on",
1510
(smtp_ports == 0)? "":" and for ");
1511
while (*p != 0) p++;
1515
/* Now the information about the port (and sometimes interface) */
1517
if (ipa->address[0] == ':' && ipa->address[1] == 0)
1519
if (ipa->next != NULL && ipa->next->address[0] == 0 &&
1520
ipa->next->port == ipa->port)
1522
(void)sprintf(CS p, " port %d (IPv6 and IPv4)", ipa->port);
1525
else if (ipa->v6_include_v4)
1526
(void)sprintf(CS p, " port %d (IPv6 with IPv4)", ipa->port);
1528
(void)sprintf(CS p, " port %d (IPv6)", ipa->port);
1530
else if (ipa->address[0] == 0)
1531
(void)sprintf(CS p, " port %d (IPv4)", ipa->port);
1533
(void)sprintf(CS p, " [%s]:%d", ipa->address, ipa->port);
1534
while (*p != 0) p++;
1372
else if (ipa->v6_include_v4)
1373
(void)sprintf(CS p, " port %d (IPv6 with IPv4)", ipa->port);
1375
(void)sprintf(CS p, " port %d (IPv6)", ipa->port);
1377
else if (ipa->address[0] == 0)
1378
(void)sprintf(CS p, " port %d (IPv4)", ipa->port);
1380
(void)sprintf(CS p, " [%s]:%d", ipa->address, ipa->port);
1381
while (*p != 0) p++;
1384
if (ipa != NULL) memcpy(p, " ...", 5);
1540
memcpy(p, " ...", 5);
1386
1545
log_write(0, LOG_MAIN,
1387
"exim %s daemon started: pid=%d, %s, %s%s",
1388
version_string, getpid(), qinfo, sinfo, big_buffer);
1389
set_process_info("daemon: %s, %s%s", qinfo, sinfo, big_buffer);
1546
"exim %s daemon started: pid=%d, %s, listening for %s",
1547
version_string, getpid(), qinfo, big_buffer);
1548
set_process_info("daemon: %s, listening for %s", qinfo, big_buffer);
1538
1705
DEBUG(D_any) debug_printf("Listening...\n");
1540
if ((lcount = select(max_socket + 1, (SELECT_ARG2_TYPE *)&select_listen,
1541
NULL, NULL, NULL)) < 0)
1707
/* In rare cases we may have had a SIGCHLD signal in the time between
1708
setting the handler (below) and getting back here. If so, pretend that the
1709
select() was interrupted so that we reap the child. This might still leave
1710
a small window when a SIGCHLD could get lost. However, since we use SIGCHLD
1711
only to do the reaping more quickly, it shouldn't result in anything other
1712
than a delay until something else causes a wake-up. */
1721
lcount = select(max_socket + 1, (SELECT_ARG2_TYPE *)&select_listen,
1543
1727
select_failed = TRUE;
1731
/* Clean up any subprocesses that may have terminated. We need to do this
1732
here so that smtp_accept_max_per_host works when a connection to that host
1733
has completed, and we are about to accept a new one. When this code was
1734
later in the sequence, a new connection could be rejected, even though an
1735
old one had just finished. Preserve the errno from any select() failure for
1736
the use of the common select/accept error processing below. */
1738
select_errno = errno;
1739
handle_ending_processes();
1740
errno = select_errno;
1547
1742
/* Loop for all the sockets that are currently ready to go. If select
1548
actually failed, we have set the count to 1 and a flag, so as to use the
1549
common error code for select/accept below. */
1743
actually failed, we have set the count to 1 and select_failed=TRUE, so as
1744
to use the common error code for select/accept below. */
1551
1746
while (lcount-- > 0)
1636
1833
tv.tv_sec = queue_interval;
1637
1834
tv.tv_usec = 0;
1638
1835
select(0, NULL, NULL, NULL, &tv);
1641
/* Handle the termination of a child process. Theoretically, this need
1642
be done only when sigchld_seen is TRUE, but rumour has it that some systems
1643
lose SIGCHLD signals at busy times, so to be on the safe side, just
1644
do it each time round. It shouldn't be too expensive. */
1646
while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
1649
DEBUG(D_any) debug_printf("child %d ended: status=0x%x\n", (int)pid,
1652
/* If it's a listening daemon, deal with an accepting process that has
1657
for (i = 0; i < smtp_accept_max; i++)
1659
if (smtp_slots[i].pid == pid)
1661
if (smtp_slots[i].host_address != NULL)
1662
store_free(smtp_slots[i].host_address);
1663
smtp_slots[i] = empty_smtp_slot;
1664
if (--smtp_accept_count < 0) smtp_accept_count = 0;
1665
DEBUG(D_any) debug_printf("%d SMTP accept process%s now running\n",
1666
smtp_accept_count, (smtp_accept_count == 1)? "" : "es");
1670
if (i < smtp_accept_max) continue; /* Found an accepting process */
1673
/* If it wasn't an accepting process, see if it was a queue-runner
1674
process, if we are keeping track of them. */
1676
if (queue_interval > 0)
1678
for (i = 0; i < queue_run_max; i++)
1680
if (queue_pid_slots[i] == pid)
1682
queue_pid_slots[i] = 0;
1683
if (--queue_run_count < 0) queue_run_count = 0;
1684
DEBUG(D_any) debug_printf("%d queue-runner process%s now running\n",
1685
queue_run_count, (queue_run_count == 1)? "" : "es");
1836
handle_ending_processes();
1692
1839
/* Re-enable the SIGCHLD handler if it has been run. It can't do it
1703
1850
closing the listening sockets so that they can be reused. Cancel any pending
1704
1851
alarm in case it is just about to go off, and set SIGHUP to be ignored so
1705
1852
that another HUP in quick succession doesn't clobber the new daemon before it
1706
gets going. All log files get closed by the close-on-exec flag. */
1853
gets going. All log files get closed by the close-on-exec flag; however, if
1854
the exec fails, we need to close the logs. */
1708
1856
if (sighup_seen)
1711
1859
log_write(0, LOG_MAIN, "pid %d: SIGHUP received: re-exec daemon",
1713
for (sk = 0; sk < listen_socket_count; sk++) close(listen_sockets[sk]);
1861
for (sk = 0; sk < listen_socket_count; sk++)
1862
(void)close(listen_sockets[sk]);
1715
1864
signal(SIGHUP, SIG_IGN);
1716
1865
sighup_argv[0] = exim_path;