197
199
transport_write_block(int fd, uschar *block, int len)
199
201
int i, rc, save_errno;
202
int local_timeout = transport_write_timeout;
204
/* This loop is for handling incomplete writes and other retries. In most
205
normal cases, it is only ever executed once. */
201
207
for (i = 0; i < 100; i++)
203
209
DEBUG(D_transport)
204
210
debug_printf("writing data block fd=%d size=%d timeout=%d\n",
205
fd, len, transport_write_timeout);
206
if (transport_write_timeout > 0) alarm(transport_write_timeout);
209
if (tls_active == fd) rc = tls_write(block, len); else
212
rc = write(fd, block, len);
215
/* Cancel the alarm and deal with a timeout */
217
if (transport_write_timeout > 0)
211
fd, len, local_timeout);
213
/* This code makes use of alarm() in order to implement the timeout. This
214
isn't a very tidy way of doing things. Using non-blocking I/O with select()
215
provides a neater approach. However, I don't know how to do this when TLS is
218
if (transport_write_timeout <= 0) /* No timeout wanted */
221
if (tls_active == fd) rc = tls_write(block, len); else
223
rc = write(fd, block, len);
227
/* Timeout wanted. */
231
alarm(local_timeout);
233
if (tls_active == fd) rc = tls_write(block, len); else
235
rc = write(fd, block, len);
237
local_timeout = alarm(0);
220
238
if (sigalrm_seen)
222
240
errno = ETIMEDOUT;
941
#ifdef EXPERIMENTAL_DOMAINKEYS
943
/**********************************************************************************
944
* External interface to write the message, while signing it with domainkeys *
945
**********************************************************************************/
947
/* This function is a wrapper around transport_write_message(). It is only called
948
from the smtp transport if
949
(1) Domainkeys support is compiled in.
950
(2) The dk_private_key option on the smtp transport is set.
951
The function sets up a replacement fd into a -K file, then calls the normal
952
function. This way, the exact bits that exim would have put "on the wire" will
953
end up in the file (except for TLS encapsulation, which is the very
954
very last thing). When we are done signing the file, send the
955
signed message down the original fd (or TLS fd).
957
Arguments: as for internal_transport_write_message() above, with additional
959
uschar *dk_private_key The private key to use (filename or plain data)
960
uschar *dk_domain Override domain (normally NULL)
961
uschar *dk_selector The selector to use.
962
uschar *dk_canon The canonalization scheme to use, "simple" or "nofws"
963
uschar *dk_headers Colon-separated header list to include in the signing
965
uschar *dk_strict What to do if signing fails: 1/true => throw error
966
0/false => send anyway
968
Returns: TRUE on success; FALSE (with errno) for any failure
972
dk_transport_write_message(address_item *addr, int fd, int options,
973
int size_limit, uschar *add_headers, uschar *remove_headers,
974
uschar *check_string, uschar *escape_string, rewrite_rule *rewrite_rules,
975
int rewrite_existflags, uschar *dk_private_key, uschar *dk_domain,
976
uschar *dk_selector, uschar *dk_canon, uschar *dk_headers, uschar *dk_strict)
981
uschar dk_spool_name[256];
985
uschar *dk_signature = NULL;
987
snprintf(CS dk_spool_name, 256, "%s/input/%s/%s-K",
988
spool_directory, message_subdir, message_id);
989
dk_fd = Uopen(dk_spool_name, O_RDWR|O_CREAT|O_EXCL, SPOOL_MODE);
992
/* Can't create spool file. Ugh. */
998
/* Call original function */
999
rc = transport_write_message(addr, dk_fd, options,
1000
size_limit, add_headers, remove_headers,
1001
check_string, escape_string, rewrite_rules,
1002
rewrite_existflags);
1004
/* Save error state. We must clean up before returning. */
1011
/* Rewind file and feed it to the goats^W DK lib */
1012
lseek(dk_fd, 0, SEEK_SET);
1013
dk_signature = dk_exim_sign(dk_fd,
1019
if (dk_signature != NULL)
1021
/* Send the signature first */
1022
int siglen = Ustrlen(dk_signature);
1026
if (tls_active == fd) wwritten = tls_write(dk_signature, siglen); else
1028
wwritten = write(fd,dk_signature,siglen);
1031
/* error, bail out */
1037
dk_signature += wwritten;
1040
else if (dk_strict != NULL)
1042
uschar *dk_strict_result = expand_string(dk_strict);
1043
if (dk_strict_result != NULL)
1045
if ( (strcmpic(dk_strict,US"1") == 0) ||
1046
(strcmpic(dk_strict,US"true") == 0) )
1055
/* Rewind file and send it down the original fd. */
1056
lseek(dk_fd, 0, SEEK_SET);
1058
while((sread = read(dk_fd,sbuf,2048)) > 0)
1061
/* write the chunk */
1064
if (tls_active == fd) wwritten = tls_write(US p, sread); else
1066
wwritten = write(fd,p,sread);
1069
/* error, bail out */
1074
if (wwritten < sread)
1076
/* short write, try again */
1092
/* unlink -K file */
1094
Uunlink(dk_spool_name);
906
1101
/*************************************************
968
1165
write_pid = (pid_t)(-1);
970
fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
1167
(void)fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
971
1168
filter_pid = child_open(transport_filter_argv, NULL, 077, &fd_write, &fd_read,
973
fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) & ~FD_CLOEXEC);
1170
(void)fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) & ~FD_CLOEXEC);
974
1171
if (filter_pid < 0) goto TIDY_UP; /* errno set */
976
1173
DEBUG(D_transport)
985
1182
if ((write_pid = fork()) == 0)
989
close(pfd[pipe_read]);
1185
(void)close(fd_read);
1186
(void)close(pfd[pipe_read]);
990
1187
nl_check_length = nl_escape_length = 0;
991
1188
rc = internal_transport_write_message(addr, fd_write,
992
1189
(options & ~(topt_use_crlf | topt_end_dot)),
993
1190
size_limit, add_headers, remove_headers, NULL, NULL,
994
1191
rewrite_rules, rewrite_existflags);
995
1192
save_errno = errno;
996
write(pfd[pipe_write], (void *)&rc, sizeof(BOOL));
997
write(pfd[pipe_write], (void *)&save_errno, sizeof(int));
998
write(pfd[pipe_write], (void *)&(addr->more_errno), sizeof(int));
1193
(void)write(pfd[pipe_write], (void *)&rc, sizeof(BOOL));
1194
(void)write(pfd[pipe_write], (void *)&save_errno, sizeof(int));
1195
(void)write(pfd[pipe_write], (void *)&(addr->more_errno), sizeof(int));
1001
1198
save_errno = errno;
1003
1200
/* Parent process: close our copy of the writing subprocess' pipes. */
1005
close(pfd[pipe_write]);
1202
(void)close(pfd[pipe_write]);
1203
(void)close(fd_write);
1009
1206
/* Writing process creation failed */
1095
1293
/* Wait for the writing process to complete. If it ends successfully,
1096
read the results from its pipe. */
1294
read the results from its pipe, provided we haven't already had a filter
1098
1297
DEBUG(D_transport) debug_printf("waiting for writing process\n");
1099
1298
if (write_pid > 0)
1101
if ((rc = child_close(write_pid, 30)) == 0)
1300
rc = child_close(write_pid, 30);
1104
read(pfd[pipe_read], (void *)&ok, sizeof(BOOL));
1107
read(pfd[pipe_read], (void *)&save_errno, sizeof(int));
1108
read(pfd[pipe_read], (void *)&(addr->more_errno), sizeof(int));
1306
(void)read(pfd[pipe_read], (void *)&ok, sizeof(BOOL));
1309
(void)read(pfd[pipe_read], (void *)&save_errno, sizeof(int));
1310
(void)read(pfd[pipe_read], (void *)&(addr->more_errno), sizeof(int));
1317
save_errno = ERRNO_FILTER_FAIL;
1318
addr->more_errno = rc;
1319
DEBUG(D_transport) debug_printf("writing process returned %d\n", rc);
1115
save_errno = ERRNO_FILTER_FAIL;
1116
addr->more_errno = rc;
1117
DEBUG(D_transport) debug_printf("writing process returned %d\n", rc);
1120
close(pfd[pipe_read]);
1323
(void)close(pfd[pipe_read]);
1122
1325
/* If there have been no problems we can now add the terminating "." if this is
1123
1326
SMTP output, turning off escaping beforehand. If the last character from the