3 I've been hacking together a way to use rsync with OpenSSL, and have
4 attached my current patch against a recent CVS tree. The details of
5 this implementation are:
7 1. The SSL code is added as a "layer" that is forked into its own
10 2. An SSL connection is established by the client issuing the
15 And, if the daemon allows SSL, it replies with
19 At which point both sides begin negotiating the SSL connection.
20 Servers that can't or don't want to use SSL just treat it as a
21 normal unknown command.
23 3. The SSL code is meant to be unobtrusive, and when this patch is
24 applied the program may still be built with no SSL code.
26 4. There are a number of details not implemented.
28 All warnings apply; I don't do C programming all that often, so I
29 can't say if I've left any cleanup/compatibility errors in the code.
31 To use this patch, run these commands for a successful build:
33 patch -p1 <patches/openssl-support.diff
38 based-on: 40afd365cc8ca968fd16e161d24df5b8a8a520cc
39 diff --git a/Makefile.in b/Makefile.in
42 @@ -40,7 +40,7 @@ OBJS3=progress.o pipe.o
43 DAEMON_OBJ = params.o loadparm.o clientserver.o access.o connection.o authenticate.o
44 popt_OBJS=popt/findme.o popt/popt.o popt/poptconfig.o \
45 popt/popthelp.o popt/poptparse.o
46 -OBJS=$(OBJS1) $(OBJS2) $(OBJS3) $(DAEMON_OBJ) $(LIBOBJ) $(ZLIBOBJ) @BUILD_POPT@
47 +OBJS=$(OBJS1) $(OBJS2) $(OBJS3) $(DAEMON_OBJ) $(LIBOBJ) $(ZLIBOBJ) @BUILD_POPT@ @SSL_OBJS@
49 TLS_OBJ = tls.o syscall.o lib/compat.o lib/snprintf.o lib/permstring.o lib/sysxattrs.o @BUILD_POPT@
51 diff --git a/cleanup.c b/cleanup.c
54 @@ -26,6 +26,9 @@ extern int am_server;
56 extern int am_receiver;
61 extern int keep_partial;
62 extern int got_xfer_error;
63 extern char *partial_dir;
64 @@ -129,6 +132,14 @@ NORETURN void _exit_cleanup(int code, const char *file, int line)
65 who_am_i(), code, file, line);
79 diff --git a/clientserver.c b/clientserver.c
82 @@ -31,6 +31,9 @@ extern int am_sender;
89 extern int rsync_port;
90 extern int protect_args;
91 extern int ignore_errors;
92 @@ -126,8 +129,18 @@ int start_socket_client(char *host, int remote_argc, char *remote_argv[],
95 ret = start_inband_exchange(fd, fd, user, remote_argc, remote_argv);
101 + int f_in = get_tls_rfd();
102 + int f_out = get_tls_wfd();
103 + return client_run(f_in, f_out, -1, argc, argv);
107 - return ret ? ret : client_run(fd, fd, -1, argc, argv);
108 + return client_run(fd, fd, -1, argc, argv);
111 static int exchange_protocols(int f_in, int f_out, char *buf, size_t bufsiz, int am_client)
112 @@ -273,6 +286,32 @@ int start_inband_exchange(int f_in, int f_out, const char *user, int argc, char
114 print_child_argv("sending daemon args:", sargs);
118 + io_printf(f_out, "#starttls\n");
120 + if (!read_line_old(f_in, line, sizeof line)) {
121 + rprintf(FERROR, "rsync: did not receive reply to #starttls\n");
124 + if (strncmp(line, "@ERROR", 6) == 0) {
125 + rprintf(FERROR, "%s\n", line);
128 + if (strcmp(line, "@RSYNCD: starttls") == 0)
130 + rprintf(FINFO, "%s\n", line);
132 + if (start_tls(f_in, f_out)) {
133 + rprintf(FERROR, "rsync: error during SSL handshake: %s\n",
137 + f_in = get_tls_rfd();
138 + f_out = get_tls_wfd();
142 io_printf(f_out, "%.*s\n", modlen, modname);
144 /* Old servers may just drop the connection here,
145 @@ -298,6 +337,10 @@ int start_inband_exchange(int f_in, int f_out, const char *user, int argc, char
146 * server to terminate the listing of modules.
147 * We don't want to go on and transfer
148 * anything; just exit. */
156 @@ -305,6 +348,10 @@ int start_inband_exchange(int f_in, int f_out, const char *user, int argc, char
157 rprintf(FERROR, "%s\n", line);
158 /* This is always fatal; the server will now
159 * close the socket. */
167 @@ -598,10 +645,17 @@ static int rsync_module(int f_in, int f_out, int i, char *addr, char *host)
174 if (asprintf(&p, "RSYNC_PID=%ld", (long)pid) > 0)
176 if (wait_process(pid, &status, 0) < 0)
182 if (asprintf(&p, "RSYNC_RAW_STATUS=%d", status) > 0)
184 if (WIFEXITED(status))
185 @@ -944,6 +998,9 @@ int start_daemon(int f_in, int f_out)
186 if (exchange_protocols(f_in, f_out, line, sizeof line, 0) < 0)
193 if (!read_line_old(f_in, line, sizeof line))
195 @@ -955,6 +1012,20 @@ int start_daemon(int f_in, int f_out)
200 + if (use_ssl && strcmp(line, "#starttls") == 0) {
201 + io_printf(f_out, "@RSYNCD: starttls\n");
202 + if (start_tls(f_in, f_out)) {
203 + rprintf(FLOG, "SSL connection failed: %s\n",
207 + f_in = get_tls_rfd();
208 + f_out = get_tls_wfd();
214 /* it's some sort of command that I don't understand */
215 io_printf(f_out, "@ERROR: Unknown command '%s'\n", line);
216 diff --git a/configure.ac b/configure.ac
219 @@ -312,6 +312,25 @@ if test x"$enable_locale" != x"no"; then
220 AC_DEFINE(CONFIG_LOCALE)
223 +AC_ARG_ENABLE(openssl,
224 + AC_HELP_STRING([--enable-openssl], [compile SSL support with OpenSSL.]))
226 +if test "x$enable_openssl" != xno
229 + LIBS="$LIBS -lcrypto"
231 + AC_CHECK_LIB(ssl, SSL_library_init, , [have_ssl=no])
232 + if test "x$have_ssl" = xyes
234 + AC_DEFINE(HAVE_OPENSSL, 1, [true if you want to use SSL.])
242 AC_MSG_CHECKING([whether to call shutdown on all sockets])
244 *cygwin* ) AC_MSG_RESULT(yes)
245 diff --git a/main.c b/main.c
248 @@ -79,6 +79,9 @@ extern char *password_file;
249 extern char curr_dir[MAXPATHLEN];
250 extern struct file_list *first_flist;
251 extern struct filter_list_struct daemon_filter_list;
257 int am_receiver = 0; /* Only set to 1 after the receiver/generator fork. */
258 @@ -137,6 +140,52 @@ pid_t wait_process(pid_t pid, int *status_ptr, int flags)
262 +/* Sends signal "signo", waits for the process to die, and if it doesn't, sends
263 + * a SIGKILL. If "graceful" is set, the initial "signo" signal is delayed by a
264 + * second to try to let the process exit on its own first. */
265 +pid_t terminate_process(pid_t pid, int *status_ptr, int signo, int graceful)
268 + int timeout = graceful ? 1000 : 3000;
272 + waited_pid = wait_process(pid, status_ptr, timeout >= 0 ? WNOHANG : 0);
275 + if (timeout == 0) {
283 + rprintf(FINFO, "%s:%s shutdown didn't work - sending signal %d\n",
284 + __FUNCTION__, graceful ? " graceful" : "", signo);
289 + /* interruptible wait and calculate the time left for waiting */
290 + struct timeval tval, t1, t2;
292 + gettimeofday(&t1, NULL);
294 + tval.tv_sec = timeout/1000;
295 + tval.tv_usec = (timeout%1000)*1000;
296 + select(0, NULL, NULL, NULL, &tval);
297 + gettimeofday(&t2, NULL);
299 + timeout -= (t2.tv_sec-t1.tv_sec)*1000 + (t2.tv_usec-t1.tv_usec)/1000;
308 /* Wait for a process to exit, calling io_flush while waiting. */
309 static void wait_process_with_flush(pid_t pid, int *exit_code_ptr)
311 @@ -727,6 +776,11 @@ static void do_server_sender(int f_in, int f_out, int argc, char *argv[])
317 + start_tls_buffering();
320 flist = send_file_list(f_out,argc,argv);
321 if (!flist || flist->used == 0)
323 @@ -828,6 +882,10 @@ static int do_recv(int f_in, int f_out, char *local_name)
326 io_start_buffering_out(f_out);
329 + start_tls_buffering();
332 set_msg_fd_in(error_pipe[0]);
333 io_start_buffering_in(error_pipe[0]);
334 @@ -1022,6 +1080,10 @@ int client_run(int f_in, int f_out, pid_t pid, int argc, char *argv[])
335 io_start_buffering_out(f_out);
340 + start_tls_buffering();
342 send_filter_list(f_out);
345 diff --git a/options.c b/options.c
348 @@ -183,6 +183,14 @@ int logfile_format_has_o_or_i = 0;
349 int always_checksum = 0;
354 +char *ssl_cert_path = NULL;
355 +char *ssl_key_path = NULL;
356 +char *ssl_key_passwd = NULL;
357 +char *ssl_ca_path = NULL;
360 #define MAX_BATCH_NAME_LEN 256 /* Must be less than MAXPATHLEN-13 */
361 char *batch_name = NULL;
363 @@ -223,6 +231,7 @@ static void print_rsync_version(enum logcode f)
364 char const *links = "no ";
365 char const *iconv = "no ";
366 char const *ipv6 = "no ";
367 + char const *ssl = "no ";
368 STRUCT_STAT *dumstat;
370 #if SUBPROTOCOL_VERSION != 0
371 @@ -256,6 +265,9 @@ static void print_rsync_version(enum logcode f)
372 #ifdef CAN_SET_SYMLINK_TIMES
379 rprintf(f, "%s version %s protocol version %d%s\n",
380 RSYNC_NAME, RSYNC_VERSION, PROTOCOL_VERSION, subprotocol);
381 @@ -269,8 +281,8 @@ static void print_rsync_version(enum logcode f)
382 (int)(sizeof (int64) * 8));
383 rprintf(f, " %ssocketpairs, %shardlinks, %ssymlinks, %sIPv6, batchfiles, %sinplace,\n",
384 got_socketpair, hardlinks, links, ipv6, have_inplace);
385 - rprintf(f, " %sappend, %sACLs, %sxattrs, %siconv, %ssymtimes\n",
386 - have_inplace, acls, xattrs, iconv, symtimes);
387 + rprintf(f, " %sappend, %sACLs, %sxattrs, %siconv, %ssymtimes, %sSSL\n",
388 + have_inplace, acls, xattrs, iconv, symtimes, ssl);
390 #ifdef MAINTAINER_MODE
391 rprintf(f, "Panic Action: \"%s\"\n", get_panic_action());
392 @@ -432,6 +444,13 @@ void usage(enum logcode F)
394 rprintf(F," -4, --ipv4 prefer IPv4\n");
395 rprintf(F," -6, --ipv6 prefer IPv6\n");
397 + rprintf(F," --ssl allow socket connections to use SSL\n");
398 + rprintf(F," --ssl-cert=FILE path to daemon's SSL certificate\n");
399 + rprintf(F," --ssl-key=FILE path to daemon's SSL private key\n");
400 + rprintf(F," --ssl-key-passwd=PASS password for PEM-encoded private key\n");
401 + rprintf(F," --ssl-ca-certs=FILE path to trusted CA certificates\n");
403 rprintf(F," --version print version number\n");
404 rprintf(F,"(-h) --help show this help (-h is --help only if used alone)\n");
406 @@ -445,7 +464,7 @@ enum {OPT_VERSION = 1000, OPT_DAEMON, OPT_SENDER, OPT_EXCLUDE, OPT_EXCLUDE_FROM,
407 OPT_FILTER, OPT_COMPARE_DEST, OPT_COPY_DEST, OPT_LINK_DEST, OPT_HELP,
408 OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_MODIFY_WINDOW, OPT_MIN_SIZE, OPT_CHMOD,
409 OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_ONLY_WRITE_BATCH, OPT_MAX_SIZE,
410 - OPT_NO_D, OPT_APPEND, OPT_NO_ICONV,
411 + OPT_NO_D, OPT_APPEND, OPT_NO_ICONV, OPT_USE_SSL,
412 OPT_SERVER, OPT_REFUSED_BASE = 9000};
414 static struct poptOption long_options[] = {
415 @@ -648,6 +667,13 @@ static struct poptOption long_options[] = {
416 {"checksum-seed", 0, POPT_ARG_INT, &checksum_seed, 0, 0, 0 },
417 {"server", 0, POPT_ARG_NONE, 0, OPT_SERVER, 0, 0 },
418 {"sender", 0, POPT_ARG_NONE, 0, OPT_SENDER, 0, 0 },
420 + {"ssl", 0, POPT_ARG_NONE, 0, OPT_USE_SSL, 0, 0},
421 + {"ssl-cert", 0, POPT_ARG_STRING, &ssl_cert_path, OPT_USE_SSL, 0, 0},
422 + {"ssl-key", 0, POPT_ARG_STRING, &ssl_key_path, OPT_USE_SSL, 0, 0},
423 + {"ssl-key-passwd", 0, POPT_ARG_STRING, &ssl_key_passwd, OPT_USE_SSL, 0, 0},
424 + {"ssl-ca-certs", 0, POPT_ARG_STRING, &ssl_ca_path, OPT_USE_SSL, 0, 0},
426 /* All the following options switch us into daemon-mode option-parsing. */
427 {"config", 0, POPT_ARG_STRING, 0, OPT_DAEMON, 0, 0 },
428 {"daemon", 0, POPT_ARG_NONE, 0, OPT_DAEMON, 0, 0 },
429 @@ -673,6 +699,13 @@ static void daemon_usage(enum logcode F)
430 rprintf(F," -v, --verbose increase verbosity\n");
431 rprintf(F," -4, --ipv4 prefer IPv4\n");
432 rprintf(F," -6, --ipv6 prefer IPv6\n");
434 + rprintf(F," --ssl allow socket connections to use SSL\n");
435 + rprintf(F," --ssl-cert=FILE path to daemon's SSL certificate\n");
436 + rprintf(F," --ssl-key=FILE path to daemon's SSL private key\n");
437 + rprintf(F," --ssl-key-passwd=PASS password for PEM-encoded private key\n");
438 + rprintf(F," --ssl-ca-certs=FILE path to trusted CA certificates\n");
440 rprintf(F," --help show this help screen\n");
443 @@ -697,6 +730,13 @@ static struct poptOption long_daemon_options[] = {
444 {"protocol", 0, POPT_ARG_INT, &protocol_version, 0, 0, 0 },
445 {"server", 0, POPT_ARG_NONE, &am_server, 0, 0, 0 },
446 {"temp-dir", 'T', POPT_ARG_STRING, &tmpdir, 0, 0, 0 },
448 + {"ssl", 0, POPT_ARG_NONE, 0, OPT_USE_SSL, 0, 0},
449 + {"ssl-cert", 0, POPT_ARG_STRING, &ssl_cert_path, OPT_USE_SSL, 0, 0},
450 + {"ssl-key", 0, POPT_ARG_STRING, &ssl_key_path, OPT_USE_SSL, 0, 0},
451 + {"ssl-key-passwd", 0, POPT_ARG_STRING, &ssl_key_passwd, OPT_USE_SSL, 0, 0},
452 + {"ssl-ca-certs", 0, POPT_ARG_STRING, &ssl_ca_path, OPT_USE_SSL, 0, 0},
454 {"verbose", 'v', POPT_ARG_NONE, 0, 'v', 0, 0 },
455 {"no-verbose", 0, POPT_ARG_VAL, &verbose, 0, 0, 0 },
456 {"no-v", 0, POPT_ARG_VAL, &verbose, 0, 0, 0 },
457 @@ -978,6 +1018,12 @@ int parse_arguments(int *argc_p, const char ***argv_p)
469 "rsync: %s: %s (in daemon mode)\n",
470 @@ -1001,6 +1047,17 @@ int parse_arguments(int *argc_p, const char ***argv_p)
471 exit_cleanup(RERR_SYNTAX);
477 + snprintf(err_buf, sizeof(err_buf),
478 + "Openssl error: %s\n",
485 *argv_p = argv = poptGetArgs(pc);
486 *argc_p = argc = count_args(argv);
488 @@ -1259,6 +1316,12 @@ int parse_arguments(int *argc_p, const char ***argv_p)
499 /* A large opt value means that set_refuse_options()
500 * turned this option off. */
501 @@ -1604,6 +1667,17 @@ int parse_arguments(int *argc_p, const char ***argv_p)
502 if (delay_updates && !partial_dir)
503 partial_dir = tmp_partialdir;
508 + snprintf(err_buf, sizeof(err_buf),
509 + "Openssl error: %s\n",
517 #ifdef HAVE_FTRUNCATE
519 @@ -2152,9 +2226,26 @@ static char *parse_hostspec(char *str, char **path_start_ptr, int *port_ptr)
520 char *check_for_hostspec(char *s, char **host_ptr, int *port_ptr)
524 - if (port_ptr && strncasecmp(URL_PREFIX, s, strlen(URL_PREFIX)) == 0) {
525 - *host_ptr = parse_hostspec(s + strlen(URL_PREFIX), &path, port_ptr);
526 + int url_prefix_len = sizeof URL_PREFIX - 1;
529 + url_prefix_len = 0;
530 + else if (strncasecmp(URL_PREFIX, s, url_prefix_len) != 0) {
532 + url_prefix_len = sizeof SSL_URL_PREFIX - 1;
533 + if (strncasecmp(SSL_URL_PREFIX, s, url_prefix_len) != 0)
534 + url_prefix_len = 0;
541 + url_prefix_len = 0;
544 + if (url_prefix_len) {
545 + *host_ptr = parse_hostspec(s + url_prefix_len, &path, port_ptr);
548 *port_ptr = RSYNC_PORT;
549 diff --git a/rsync.h b/rsync.h
554 #define DEFAULT_LOCK_FILE "/var/run/rsyncd.lock"
555 #define URL_PREFIX "rsync://"
556 +#define SSL_URL_PREFIX "rsyncs://"
558 #define SYMLINK_PREFIX "/rsyncd-munged/" /* This MUST have a trailing slash! */
559 #define SYMLINK_PREFIX_LEN ((int)sizeof SYMLINK_PREFIX - 1)
560 @@ -569,6 +570,11 @@ typedef unsigned int size_t;
561 # define SIZEOF_INT64 SIZEOF_OFF_T
565 +#include <openssl/ssl.h>
566 +#include <openssl/err.h>
572 diff --git a/ssl.c b/ssl.c
577 +/* -*- c-file-style: "linux" -*-
578 + * ssl.c: operations for negotiating SSL rsync connections.
580 + * Copyright (C) 2003 Casey Marshall <rsdio@metastatic.org>
582 + * This program is free software; you can redistribute it and/or modify
583 + * it under the terms of the GNU General Public License as published by
584 + * the Free Software Foundation; either version 2 of the License, or
585 + * (at your option) any later version.
587 + * This program is distributed in the hope that it will be useful,
588 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
589 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
590 + * GNU General Public License for more details.
592 + * You should have received a copy of the GNU General Public License
593 + * along with this program; if not, write to the Free Software
594 + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
599 +#ifdef HAVE_SYS_SELECT_H
600 +#include <sys/select.h>
602 +#include <sys/time.h>
603 +#include <sys/types.h>
608 +#define SSL_MAX_RECORD_SIZE (1024*16) /* TLS record size */
609 +#define MAX_BUFFERING_TIME_MS 100
612 +extern int am_daemon;
613 +extern int am_server;
615 +extern char *ssl_cert_path;
616 +extern char *ssl_key_path;
617 +extern char *ssl_key_passwd;
618 +extern char *ssl_ca_path;
620 +static SSL_CTX *ssl_ctx;
622 +static int tls_read[2] = { -1, -1 };
623 +static int tls_write[2] = { -1, -1 };
624 +static int ssl_running;
625 +static int ssl_min_send_size;
626 +static int ssl_pid = -1;
628 +#ifdef HAVE_SIGACTION
629 +static struct sigaction sigact;
632 +/* copied from progress.c */
633 +static unsigned long msdiff(struct timeval *t1, struct timeval *t2)
635 + return (t2->tv_sec - t1->tv_sec) * 1000L
636 + + (t2->tv_usec - t1->tv_usec) / 1000;
640 + * A non-interactive callback to be passed to SSL_CTX_set_default_password_cb,
641 + * which merely copies the value of ssl_key_passwd into buf. This is
642 + * used for when the private key password is supplied via an option.
644 +static int default_password_cb(char *buf, int n, UNUSED(int f), UNUSED(void *u))
646 + if (ssl_key_passwd == NULL || n < (int)strlen(ssl_key_passwd))
648 + strncpy(buf, ssl_key_passwd, n-1);
649 + return strlen(ssl_key_passwd);
653 + * If verbose, this method traces the status of the SSL handshake.
655 +static void info_callback(const SSL *ssl, int cb, int val)
662 + cbs = "SSL_CB_LOOP";
665 + cbs = "SSL_CB_EXIT";
668 + cbs = "SSL_CB_READ";
671 + cbs = "SSL_CB_WRITE";
674 + cbs = "SSL_CB_ALERT";
676 + case SSL_CB_READ_ALERT:
677 + cbs = "SSL_CB_READ_ALERT";
679 + case SSL_CB_WRITE_ALERT:
680 + cbs = "SSL_CB_WRITE_ALERT";
682 + case SSL_CB_ACCEPT_LOOP:
683 + cbs = "SSL_CB_ACCEPT_LOOP";
685 + case SSL_CB_ACCEPT_EXIT:
686 + cbs = "SSL_CB_ACCEPT_EXIT";
688 + case SSL_CB_CONNECT_LOOP:
689 + cbs = "SSL_CB_CONNECT_LOOP";
691 + case SSL_CB_CONNECT_EXIT:
692 + cbs = "SSL_CB_CONNECT_EXIT";
694 + case SSL_CB_HANDSHAKE_START:
695 + cbs = "SSL_CB_HANDSHAKE_START";
697 + case SSL_CB_HANDSHAKE_DONE:
698 + cbs = "SSL_CB_HANDSHAKE_DONE";
701 + snprintf(buf, sizeof buf, "??? (%d)", cb);
706 + rprintf(FLOG, "SSL: info_callback(%p,%s,%d)\n", ssl, cbs, val);
707 + if (cb == SSL_CB_HANDSHAKE_DONE) {
708 + SSL_CIPHER_description(SSL_get_current_cipher((SSL*)ssl),
710 + rprintf(FLOG, "SSL: cipher: %s", buf);
716 + * Initializes the SSL context for TLSv1 connections; returns zero on
723 + SSL_library_init();
724 + SSL_load_error_strings();
725 + ssl_ctx = SSL_CTX_new(TLSv1_method());
728 + SSL_CTX_set_info_callback(ssl_ctx, info_callback);
730 + SSL_CTX_set_options(ssl_ctx, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS|SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3);
732 + /* Sets the certificate sent to the other party. */
733 + if (ssl_cert_path != NULL
734 + && SSL_CTX_use_certificate_file(ssl_ctx, ssl_cert_path,
735 + SSL_FILETYPE_PEM) != 1)
737 + /* Set up the simple non-interactive callback if the password
738 + * was supplied on the command line. */
739 + if (ssl_key_passwd != NULL)
740 + SSL_CTX_set_default_passwd_cb(ssl_ctx, default_password_cb);
741 + /* Sets the private key that matches the public certificate. */
742 + if (ssl_key_path != NULL) {
743 + if (SSL_CTX_use_PrivateKey_file(ssl_ctx, ssl_key_path,
744 + SSL_FILETYPE_PEM) != 1)
746 + if (SSL_CTX_check_private_key(ssl_ctx) != 1)
749 + if (ssl_ca_path != NULL
750 + && !SSL_CTX_load_verify_locations(ssl_ctx, ssl_ca_path, NULL))
757 + * Returns the error string for the current SSL error, if any.
759 +char *get_ssl_error(void)
761 + return ERR_error_string(ERR_get_error(), NULL);
765 + * Returns the input file descriptor for the SSL connection.
767 +int get_tls_rfd(void)
769 + return tls_read[0];
773 + * Returns the output file descriptor for the SSL connection.
775 +int get_tls_wfd(void)
777 + return tls_write[1];
781 + * Signal handler that ends the SSL connection.
783 +static RETSIGTYPE tls_sigusr1(int UNUSED(val))
788 +/* Signal handler that starts record-size buffering. */
789 +static RETSIGTYPE tls_sigusr2(int UNUSED(val))
791 + ssl_min_send_size = SSL_MAX_RECORD_SIZE;
795 + * Negotiates the TLS connection, creates a socket pair for communicating
796 + * with the rsync process, then forks into a new process that will handle
797 + * the communication.
799 + * 0 is returned on success.
800 + * rsync to network algorithm:
801 + * A buffer ring of two buffers is maintained. Data is read from the local
802 + * socket into the current buffer. The buffer is eligible for sending if
803 + * enough data has been buffered, or enough time has passed. Once the buffer
804 + * becomes eligible for sending it is marked "ready" and buffering continues
805 + * on the other buffer (assuming it's "unready"). This algorithm tries to
806 + * maximize SSL record size.
808 + * network to rsync algorithm:
809 + * One receive buffer twice the size of the SSL max record size. Reading
810 + * from the network is attempted only if there's enough space for a whole
811 + * record (this is done to optimize OpenSSL per-call overhead).
812 + * As bytes enter the buffer sending to rsync begins, when the buffer becomes
813 + * full we wait for sending to rsync to finish.
815 + * A graceful stop is attempted on both directions. So if the pipe from rsync
816 + * is gracefully shut down, we try to send all data to the network before closing
817 + * the connection and if the SSL connection is gracefully shut down we try to
818 + * send everything to rsync before closing the rsync pipe.
820 +int start_tls(int f_in, int f_out)
823 + unsigned char buf_tonetwork[2][SSL_MAX_RECORD_SIZE], buf_fromnetwork[2*SSL_MAX_RECORD_SIZE];
824 + int tonet_ready[2] = {0}, tonet_size[2] = {0}, write_tonet = 0, tonet_fill_idx = 0, tonet_send_idx = 0;
825 + struct timeval buffering_start, now, timeout, *tp;
826 + int avail_fromnet = 0, write_fromnet = 0;
827 + int want_read_fromnet = 1, want_write_tonet = 0;
828 + int want_read_fromrsync = 1, want_write_torsync = 0;
829 + int write_tonet_blocks = 0, read_fromnet_blocks = 0;
830 + int write_torsync_blocks = 0, read_fromrsync_blocks = 0;
831 + int ssl_readfd_blocks = 0, ssl_writefd_blocks = 0;
832 + int ssl_write_count = 0;
834 + int net_closed = 0, rsync_closed = 0;
837 + const char *ssl_ciphers;
839 + if (fd_pair(tls_read))
841 + if (fd_pair(tls_write))
844 + set_blocking(tls_read[0]);
845 + set_nonblocking(tls_read[1]);
846 + set_nonblocking(tls_write[0]);
847 + set_blocking(tls_write[1]);
848 + set_nonblocking(f_in);
849 + set_nonblocking(f_out);
851 + ssl_pid = do_fork();
854 + if (ssl_pid != 0) {
855 + close(tls_write[0]);
856 + close(tls_read[1]);
862 + close(tls_write[1]);
863 + close(tls_read[0]);
865 + SIGACTION(SIGUSR1, tls_sigusr1);
866 + SIGACTION(SIGUSR2, tls_sigusr2);
867 + ssl = SSL_new(ssl_ctx);
870 + if (am_daemon || am_server)
871 + SSL_set_accept_state(ssl);
873 + SSL_set_connect_state(ssl);
874 + ssl_ciphers = getenv("RSYNC_SSL_CIPHERS");
876 + SSL_set_cipher_list(ssl, ssl_ciphers);
879 + SSL_set_rfd(ssl, f_in);
880 + SSL_set_wfd(ssl, f_out);
883 + n = MAX(tls_read[1], n);
885 + n = MAX(f_out, n) + 1;
889 + while (ssl_running) {
890 + ++loopcount; /* the loopcount prevents starvation of one transfer direction by the other */
893 + if (want_read_fromrsync && !read_fromrsync_blocks) {
894 + int cur_buf_size = tonet_size[tonet_fill_idx];
895 + r = read(tls_write[0], buf_tonetwork[tonet_fill_idx]+cur_buf_size, sizeof(buf_tonetwork[0])-cur_buf_size);
899 + if (cur_buf_size == 0)
900 + gettimeofday(&buffering_start, NULL);
902 + tonet_size[tonet_fill_idx] = cur_buf_size;
903 + if (cur_buf_size >= ssl_min_send_size) {
904 + want_write_tonet = 1;
905 + tonet_ready[tonet_fill_idx] = 1;
906 + tonet_fill_idx = !tonet_fill_idx;
907 + if (tonet_ready[tonet_fill_idx])
908 + want_read_fromrsync = 0;
911 + } else if (r < 0 && errno == EWOULDBLOCK)
912 + read_fromrsync_blocks = 1;
914 + rprintf(FERROR, "pipe read error: %s\n",
919 + if (cur_buf_size) {
920 + want_write_tonet = 1;
921 + tonet_ready[tonet_fill_idx] = 1; /* close current buffer */
923 + want_read_fromrsync = 0; /* don't read */
924 + want_write_torsync = 0; /* don't write */
925 + read_fromrsync_blocks = 0; /* don't select */
926 + write_torsync_blocks = 0;
927 + want_read_fromnet = 1; /* purge incoming data from network */
929 + if (net_closed || !want_write_tonet)
934 + if (want_read_fromnet && !read_fromnet_blocks) {
935 + r = SSL_read(ssl, buf_fromnetwork+avail_fromnet, SSL_MAX_RECORD_SIZE);
938 + if (!rsync_closed) {
939 + avail_fromnet += r;
940 + want_write_torsync = 1;
941 + if (avail_fromnet >= (int)(sizeof(buf_fromnetwork)-SSL_MAX_RECORD_SIZE))
942 + want_read_fromnet = 0;
945 + switch (SSL_get_error(ssl, r)) {
946 + case SSL_ERROR_ZERO_RETURN:
948 + want_read_fromnet = 0; /* don't read */
949 + want_write_tonet = 0; /* don't write */
950 + ssl_readfd_blocks = 0; /* and for heaven's sake - don't select() */
951 + ssl_writefd_blocks = 0;
952 + want_read_fromrsync = 1; /* and purge stuff from rsync side */
953 + tonet_size[tonet_fill_idx] = 0;
954 + tonet_ready[tonet_fill_idx] = 0;
955 + if (rsync_closed || !want_write_torsync)
956 + goto graceful_stop;
958 + case SSL_ERROR_WANT_READ:
959 + ssl_readfd_blocks = 1;
960 + read_fromnet_blocks = 1;
962 + case SSL_ERROR_WANT_WRITE:
963 + ssl_writefd_blocks = 1;
964 + read_fromnet_blocks = 1;
966 + case SSL_ERROR_SYSCALL:
968 + rprintf(FERROR, "SSL spurious EOF\n");
970 + rprintf(FERROR, "SSL I/O error: %s\n",
973 + case SSL_ERROR_SSL:
974 + rprintf(FERROR, "SSL %s\n",
975 + ERR_error_string(ERR_get_error(), NULL));
978 + rprintf(FERROR, "unexpected ssl error %d\n", r);
984 + if (want_write_torsync && !write_torsync_blocks) {
985 + r = write(tls_read[1], buf_fromnetwork+write_fromnet, avail_fromnet-write_fromnet);
988 + write_fromnet += r;
989 + if (write_fromnet >= avail_fromnet) {
994 + want_read_fromnet = 1;
995 + want_write_torsync = 0;
998 + else if (errno == EWOULDBLOCK)
999 + write_torsync_blocks = 1;
1001 + if (errno != EPIPE)
1002 + rprintf(FERROR, "pipe write error: %s\n", strerror(errno));
1007 + if (want_write_tonet && !write_tonet_blocks) {
1008 + /* lock the write count, 'cause if SSL_write fails on non-blocking IO, it
1009 + * must be repeated with same parameters. */
1010 + if (ssl_write_count == 0)
1011 + ssl_write_count = tonet_size[tonet_send_idx] - write_tonet;
1012 + r = SSL_write(ssl, buf_tonetwork[tonet_send_idx]+write_tonet, ssl_write_count);
1016 + ssl_write_count = 0;
1017 + if (write_tonet >= tonet_size[tonet_send_idx]) {
1019 + tonet_size[tonet_send_idx] = 0;
1020 + tonet_ready[tonet_send_idx] = 0;
1021 + if (!rsync_closed)
1022 + want_read_fromrsync = 1;
1023 + tonet_send_idx = !tonet_send_idx;
1024 + if (!tonet_ready[tonet_send_idx]) {
1025 + want_write_tonet = 0;
1031 + switch (SSL_get_error(ssl, r)) {
1032 + case SSL_ERROR_ZERO_RETURN:
1033 + goto graceful_stop;
1034 + case SSL_ERROR_WANT_READ:
1035 + ssl_readfd_blocks = 1;
1036 + write_tonet_blocks = 1;
1038 + case SSL_ERROR_WANT_WRITE:
1039 + ssl_writefd_blocks = 1;
1040 + write_tonet_blocks = 1;
1042 + case SSL_ERROR_SYSCALL:
1044 + rprintf(FERROR, "SSL: spurious EOF\n");
1046 + rprintf(FERROR, "SSL: I/O error: %s\n", strerror(errno));
1048 + case SSL_ERROR_SSL:
1049 + rprintf(FERROR, "SSL: %s\n",
1050 + ERR_error_string(ERR_get_error(), NULL));
1053 + rprintf(FERROR, "unexpected ssl error %d\n", r);
1059 + if (!want_more_io || (loopcount&31) == 0) {
1060 + int has_buffered = 0;
1061 + if (!net_closed && !tonet_ready[tonet_fill_idx] && tonet_size[tonet_fill_idx]) {
1063 + unsigned long buffering_time;
1064 + gettimeofday(&now,NULL);
1065 + buffering_time = msdiff(&buffering_start,&now);
1066 + if (buffering_time >= MAX_BUFFERING_TIME_MS) {
1067 + want_write_tonet = 1;
1069 + tonet_ready[tonet_fill_idx] = 1;
1070 + tonet_fill_idx = !tonet_fill_idx;
1071 + if (tonet_ready[tonet_fill_idx])
1072 + want_read_fromrsync = 0;
1076 + int waiting_on_something = 0;
1079 + if (read_fromrsync_blocks) {
1080 + waiting_on_something = 1;
1081 + FD_SET(tls_write[0], &rd);
1083 + if (write_torsync_blocks) {
1084 + waiting_on_something = 1;
1085 + FD_SET(tls_read[1], &wd);
1087 + if (ssl_readfd_blocks) {
1088 + waiting_on_something = 1;
1089 + FD_SET(f_in, &rd);
1091 + if (ssl_writefd_blocks) {
1092 + waiting_on_something = 1;
1093 + FD_SET(f_out, &wd);
1096 + if (want_more_io) {
1097 + /* just poll to see if some sockets became non-blocking*/
1098 + timeout.tv_sec = 0;
1099 + timeout.tv_usec = 0;
1101 + } else if (waiting_on_something && !has_buffered)
1102 + tp = NULL; /*infinite wait until a socket becomes available*/
1104 + timeout.tv_sec = MAX_BUFFERING_TIME_MS/1000;
1105 + timeout.tv_usec = (MAX_BUFFERING_TIME_MS%1000)*1000;
1109 + if (waiting_on_something || !want_more_io) {
1110 + r = select(n, &rd, &wd, NULL, tp);
1113 + if (errno != EINTR) {
1114 + rprintf(FERROR, "select error: %s\n",
1121 + if (FD_ISSET(tls_write[0], &rd))
1122 + read_fromrsync_blocks = 0;
1123 + if (FD_ISSET(f_in,&rd)) {
1124 + read_fromnet_blocks = 0;
1125 + write_tonet_blocks = 0;
1126 + ssl_readfd_blocks = 0;
1128 + if (FD_ISSET(tls_read[1],&wd))
1129 + write_torsync_blocks = 0;
1130 + if (FD_ISSET(f_out,&wd)) {
1131 + read_fromnet_blocks = 0;
1132 + write_tonet_blocks = 0;
1133 + ssl_writefd_blocks = 0;
1141 + /* The SSL shutdown API looks pretty broken with respect to corner cases
1142 + * where the underlying socket is blocking. This is a "best effort". */
1143 + if (SSL_shutdown(ssl) != 1)
1144 + SSL_shutdown(ssl);
1148 + /* We're finished. */
1150 + close(tls_read[1]);
1151 + close(tls_write[0]);
1153 + if (f_out != f_in)
1158 +void start_tls_buffering(void)
1161 + kill(ssl_pid, SIGUSR2);
1165 + * Ends the TLS connection.
1169 + if (ssl_pid > 0) {
1171 + /* try a graceful stop - this makes sure all data is sent, and
1172 + * also causes our side to send a close_notify alert, as required
1173 + * by TLS specs. */
1174 + close(get_tls_rfd());
1175 + if (get_tls_wfd() != get_tls_rfd())
1176 + close(get_tls_wfd());
1177 + terminate_process(ssl_pid, &status, SIGUSR1, 1);