1 After applying this patch, run these commands for a successful build:
9 I've been hacking together a way to use rsync with OpenSSL, and have
10 attached my current patch against a recent CVS tree. The details of
11 this implementation are:
13 1. The SSL code is added as a "layer" that is forked into its own
16 2. An SSL connection is established by the client issuing the
21 And, if the daemon allows SSL, it replies with
25 At which point both sides begin negotiating the SSL connection.
26 Servers that can't or don't want to use SSL just treat it as a
27 normal unknown command.
29 3. The SSL code is meant to be unobtrusive, and when this patch is
30 applied the program may still be built with no SSL code.
32 4. There are a number of details not implemented.
34 All warnings apply; I don't do C programming all that often, so I
35 can't say if I've left any cleanup/compatibility errors in the code.
40 @@ -39,7 +39,7 @@ OBJS3=progress.o pipe.o
41 DAEMON_OBJ = params.o loadparm.o clientserver.o access.o connection.o authenticate.o
42 popt_OBJS=popt/findme.o popt/popt.o popt/poptconfig.o \
43 popt/popthelp.o popt/poptparse.o
44 -OBJS=$(OBJS1) $(OBJS2) $(OBJS3) $(DAEMON_OBJ) $(LIBOBJ) $(ZLIBOBJ) @BUILD_POPT@
45 +OBJS=$(OBJS1) $(OBJS2) $(OBJS3) $(DAEMON_OBJ) $(LIBOBJ) $(ZLIBOBJ) @BUILD_POPT@ @SSL_OBJS@
47 TLS_OBJ = tls.o syscall.o lib/compat.o lib/snprintf.o lib/permstring.o
58 extern int keep_partial;
59 extern int log_got_error;
60 extern char *partial_dir;
61 @@ -117,6 +120,14 @@ NORETURN void _exit_cleanup(int code, co
76 --- old/clientserver.c
77 +++ new/clientserver.c
78 @@ -29,6 +29,9 @@ extern int am_sender;
85 extern int rsync_port;
86 extern int kluge_around_eof;
87 extern int daemon_over_rsh;
88 @@ -105,8 +108,18 @@ int start_socket_client(char *host, char
89 set_socket_options(fd, sockopts);
91 ret = start_inband_exchange(user, path, fd, fd, argc);
97 + int f_in = get_tls_rfd();
98 + int f_out = get_tls_wfd();
99 + return client_run(f_in, f_out, -1, argc, argv);
103 - return ret ? ret : client_run(fd, fd, -1, argc, argv);
104 + return client_run(fd, fd, -1, argc, argv);
107 int start_inband_exchange(char *user, char *path, int f_in, int f_out,
108 @@ -167,6 +180,33 @@ int start_inband_exchange(char *user, ch
110 print_child_argv(sargs);
114 + io_printf(f_out, "#starttls\n");
116 + if (!read_line(f_in, line, sizeof(line)-1)) {
117 + rprintf(FERROR, "rsync: did not receive reply to #starttls\n");
120 + if (strncmp(line, "@ERROR", 6) == 0) {
121 + rprintf(FERROR, "%s\n", line);
124 + if (strcmp(line, "@RSYNCD: starttls") == 0) {
127 + rprintf(FINFO, "%s\n", line);
129 + if (start_tls(f_in, f_out)) {
130 + rprintf(FERROR, "rsync: error during SSL handshake: %s\n",
134 + f_in = get_tls_rfd();
135 + f_out = get_tls_wfd();
139 p = strchr(path,'/');
141 io_printf(f_out, "%s\n", path);
142 @@ -195,6 +235,10 @@ int start_inband_exchange(char *user, ch
143 * server to terminate the listing of modules.
144 * We don't want to go on and transfer
145 * anything; just exit. */
153 @@ -202,6 +246,10 @@ int start_inband_exchange(char *user, ch
154 rprintf(FERROR, "%s\n", line);
155 /* This is always fatal; the server will now
156 * close the socket. */
164 @@ -718,6 +766,7 @@ static void send_listing(int fd)
165 io_printf(fd,"@RSYNCD: EXIT\n");
169 /* this is called when a connection is established to a client
170 and we want to start talking. The setup of the system is done from
172 @@ -776,6 +825,9 @@ int start_daemon(int f_in, int f_out)
173 if (protocol_version > remote_protocol)
174 protocol_version = remote_protocol;
180 if (!read_line(f_in, line, sizeof line - 1))
182 @@ -787,6 +839,20 @@ int start_daemon(int f_in, int f_out)
187 + if (use_ssl && strcmp(line, "#starttls") == 0) {
188 + io_printf(f_out, "@RSYNCD: starttls\n");
189 + if (start_tls(f_in, f_out)) {
190 + rprintf(FLOG, "SSL connection failed: %s\n",
194 + f_in = get_tls_rfd();
195 + f_out = get_tls_wfd();
201 /* it's some sort of command that I don't understand */
202 io_printf(f_out, "@ERROR: Unknown command '%s'\n", line);
205 @@ -290,6 +290,21 @@ if test x"$enable_locale" != x"no"; then
206 AC_DEFINE(CONFIG_LOCALE)
209 +AC_ARG_ENABLE(openssl,
210 + AC_HELP_STRING([--enable-openssl], [compile SSL support with OpenSSL.]))
212 +if test "x$enable_openssl" != xno
215 + AC_CHECK_LIB(ssl, SSL_library_init, , [have_ssl=no])
216 + if test "x$have_ssl" = xyes
218 + AC_DEFINE(HAVE_OPENSSL, 1, [true if you want to use SSL.])
224 AC_MSG_CHECKING([whether to call shutdown on all sockets])
226 *cygwin* ) AC_MSG_RESULT(yes)
229 @@ -172,6 +172,14 @@ int logfile_format_has_o_or_i = 0;
230 int always_checksum = 0;
235 +char *ssl_cert_path = NULL;
236 +char *ssl_key_path = NULL;
237 +char *ssl_key_passwd = NULL;
238 +char *ssl_ca_path = NULL;
241 #define MAX_BATCH_NAME_LEN 256 /* Must be less than MAXPATHLEN-13 */
242 char *batch_name = NULL;
244 @@ -200,6 +208,7 @@ static void print_rsync_version(enum log
245 char const *hardlinks = "no ";
246 char const *links = "no ";
247 char const *ipv6 = "no ";
248 + char const *ssl = "no ";
249 STRUCT_STAT *dumstat;
251 #ifdef HAVE_SOCKETPAIR
252 @@ -222,6 +231,10 @@ static void print_rsync_version(enum log
260 rprintf(f, "%s version %s protocol version %d\n",
261 RSYNC_NAME, RSYNC_VERSION, PROTOCOL_VERSION);
262 rprintf(f, "Copyright (C) 1996-2006 by Andrew Tridgell, Wayne Davison, and others.\n");
263 @@ -234,9 +247,9 @@ static void print_rsync_version(enum log
264 /* Note that this field may not have type ino_t. It depends
265 * on the complicated interaction between largefile feature
267 - rprintf(f, " %sinplace, %sIPv6, "
268 + rprintf(f, " %sinplace, %sIPv6, %sSSL, "
269 "%d-bit system inums, %d-bit internal inums\n",
270 - have_inplace, ipv6,
271 + have_inplace, ipv6, ssl,
272 (int) (sizeof dumstat->st_ino * 8),
273 (int) (sizeof (int64) * 8));
274 #ifdef MAINTAINER_MODE
275 @@ -383,6 +396,13 @@ void usage(enum logcode F)
276 rprintf(F," -4, --ipv4 prefer IPv4\n");
277 rprintf(F," -6, --ipv6 prefer IPv6\n");
280 + rprintf(F," --ssl allow socket connections to use SSL\n");
281 + rprintf(F," --ssl-cert=FILE path to daemon's SSL certificate\n");
282 + rprintf(F," --ssl-key=FILE path to daemon's SSL private key\n");
283 + rprintf(F," --ssl-key-passwd=PASS password for PEM-encoded private key\n");
284 + rprintf(F," --ssl-ca-certs=FILE path to trusted CA certificates\n");
286 rprintf(F," --version print version number\n");
287 rprintf(F,"(-h) --help show this help (-h works with no other options)\n");
289 @@ -396,7 +416,7 @@ enum {OPT_VERSION = 1000, OPT_DAEMON, OP
290 OPT_FILTER, OPT_COMPARE_DEST, OPT_COPY_DEST, OPT_LINK_DEST, OPT_HELP,
291 OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_MODIFY_WINDOW, OPT_MIN_SIZE, OPT_CHMOD,
292 OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_ONLY_WRITE_BATCH, OPT_MAX_SIZE,
294 + OPT_NO_D, OPT_USE_SSL,
295 OPT_SERVER, OPT_REFUSED_BASE = 9000};
297 static struct poptOption long_options[] = {
298 @@ -541,6 +561,13 @@ static struct poptOption long_options[]
299 {"checksum-seed", 0, POPT_ARG_INT, &checksum_seed, 0, 0, 0 },
300 {"server", 0, POPT_ARG_NONE, 0, OPT_SERVER, 0, 0 },
301 {"sender", 0, POPT_ARG_NONE, 0, OPT_SENDER, 0, 0 },
303 + {"ssl", 0, POPT_ARG_NONE, 0, OPT_USE_SSL, 0, 0},
304 + {"ssl-cert", 0, POPT_ARG_STRING, &ssl_cert_path, OPT_USE_SSL, 0, 0},
305 + {"ssl-key", 0, POPT_ARG_STRING, &ssl_key_path, OPT_USE_SSL, 0, 0},
306 + {"ssl-key-passwd", 0, POPT_ARG_STRING, &ssl_key_passwd, OPT_USE_SSL, 0, 0},
307 + {"ssl-ca-certs", 0, POPT_ARG_STRING, &ssl_ca_path, OPT_USE_SSL, 0, 0},
309 /* All the following options switch us into daemon-mode option-parsing. */
310 {"config", 0, POPT_ARG_STRING, 0, OPT_DAEMON, 0, 0 },
311 {"daemon", 0, POPT_ARG_NONE, 0, OPT_DAEMON, 0, 0 },
312 @@ -568,6 +595,13 @@ static void daemon_usage(enum logcode F)
313 rprintf(F," -4, --ipv4 prefer IPv4\n");
314 rprintf(F," -6, --ipv6 prefer IPv6\n");
317 + rprintf(F," --ssl allow socket connections to use SSL\n");
318 + rprintf(F," --ssl-cert=FILE path to daemon's SSL certificate\n");
319 + rprintf(F," --ssl-key=FILE path to daemon's SSL private key\n");
320 + rprintf(F," --ssl-key-passwd=PASS password for PEM-encoded private key\n");
321 + rprintf(F," --ssl-ca-certs=FILE path to trusted CA certificates\n");
323 rprintf(F," --help show this help screen\n");
326 @@ -594,6 +628,13 @@ static struct poptOption long_daemon_opt
327 {"protocol", 0, POPT_ARG_INT, &protocol_version, 0, 0, 0 },
328 {"server", 0, POPT_ARG_NONE, &am_server, 0, 0, 0 },
329 {"temp-dir", 'T', POPT_ARG_STRING, &tmpdir, 0, 0, 0 },
331 + {"ssl", 0, POPT_ARG_NONE, 0, OPT_USE_SSL, 0, 0},
332 + {"ssl-cert", 0, POPT_ARG_STRING, &ssl_cert_path, OPT_USE_SSL, 0, 0},
333 + {"ssl-key", 0, POPT_ARG_STRING, &ssl_key_path, OPT_USE_SSL, 0, 0},
334 + {"ssl-key-passwd", 0, POPT_ARG_STRING, &ssl_key_passwd, OPT_USE_SSL, 0, 0},
335 + {"ssl-ca-certs", 0, POPT_ARG_STRING, &ssl_ca_path, OPT_USE_SSL, 0, 0},
337 {"verbose", 'v', POPT_ARG_NONE, 0, 'v', 0, 0 },
338 {"no-verbose", 0, POPT_ARG_VAL, &verbose, 0, 0, 0 },
339 {"no-v", 0, POPT_ARG_VAL, &verbose, 0, 0, 0 },
340 @@ -851,6 +892,12 @@ int parse_arguments(int *argc, const cha
352 "rsync: %s: %s (in daemon mode)\n",
353 @@ -874,6 +921,17 @@ int parse_arguments(int *argc, const cha
354 exit_cleanup(RERR_SYNTAX);
360 + snprintf(err_buf, sizeof(err_buf),
361 + "Openssl error: %s\n",
368 *argv = poptGetArgs(pc);
369 *argc = count_args(*argv);
371 @@ -1085,6 +1143,12 @@ int parse_arguments(int *argc, const cha
382 /* A large opt value means that set_refuse_options()
383 * turned this option off. */
384 @@ -1361,6 +1425,17 @@ int parse_arguments(int *argc, const cha
385 if (delay_updates && !partial_dir)
386 partial_dir = tmp_partialdir;
391 + snprintf(err_buf, sizeof(err_buf),
392 + "Openssl error: %s\n",
400 #ifdef HAVE_FTRUNCATE
402 @@ -1778,10 +1853,27 @@ char *check_for_hostspec(char *s, char *
406 + int url_prefix_len = sizeof URL_PREFIX - 1;
408 - if (port_ptr && strncasecmp(URL_PREFIX, s, strlen(URL_PREFIX)) == 0) {
410 + url_prefix_len = 0;
411 + else if (strncasecmp(URL_PREFIX, s, url_prefix_len) != 0) {
413 + url_prefix_len = sizeof SSL_URL_PREFIX - 1;
414 + if (strncasecmp(SSL_URL_PREFIX, s, url_prefix_len) != 0)
415 + url_prefix_len = 0;
422 + url_prefix_len = 0;
425 + if (url_prefix_len) {
427 - s += strlen(URL_PREFIX);
428 + s += url_prefix_len;
429 if ((p = strchr(s, '/')) != NULL) {
436 #define DEFAULT_LOCK_FILE "/var/run/rsyncd.lock"
437 #define URL_PREFIX "rsync://"
438 +#define SSL_URL_PREFIX "rsyncs://"
440 #define BACKUP_SUFFIX "~"
442 @@ -419,6 +420,11 @@ enum msgcode {
443 # define SIZEOF_INT64 SIZEOF_OFF_T
447 +#include <openssl/ssl.h>
448 +#include <openssl/err.h>
451 /* Starting from protocol version 26, we always use 64-bit
452 * ino_t and dev_t internally, even if this platform does not
453 * allow files to have 64-bit inums. That's because the
457 +/* -*- c-file-style: "linux" -*-
458 + * ssl.c: operations for negotiating SSL rsync connections.
460 + * Copyright (C) 2003 Casey Marshall <rsdio@metastatic.org>
462 + * This program is free software; you can redistribute it and/or modify
463 + * it under the terms of the GNU General Public License as published by
464 + * the Free Software Foundation; either version 2 of the License, or
465 + * (at your option) any later version.
467 + * This program is distributed in the hope that it will be useful,
468 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
469 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
470 + * GNU General Public License for more details.
472 + * You should have received a copy of the GNU General Public License
473 + * along with this program; if not, write to the Free Software
474 + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
479 +#ifdef HAVE_SYS_SELECT_H
480 +#include <sys/select.h>
482 +#include <sys/time.h>
483 +#include <sys/types.h>
488 +#define BUF_SIZE 1024
491 +extern int am_daemon;
492 +extern int am_server;
494 +extern char *ssl_cert_path;
495 +extern char *ssl_key_path;
496 +extern char *ssl_key_passwd;
497 +extern char *ssl_ca_path;
499 +static SSL_CTX *ssl_ctx;
501 +static int tls_read[2] = { -1, -1 };
502 +static int tls_write[2] = { -1, -1 };
503 +static int ssl_running;
504 +static int ssl_pid = -1;
506 +#ifdef HAVE_SIGACTION
507 +static struct sigaction sigact;
511 + * A non-interactive callback to be passed to SSL_CTX_set_default_password_cb,
512 + * which merely copies the value of ssl_key_passwd into buf. This is
513 + * used for when the private key password is supplied via an option.
515 +static int default_password_cb(char *buf, int n, UNUSED(int f), UNUSED(void *u))
517 + if (ssl_key_passwd == NULL || n < (int)strlen(ssl_key_passwd))
519 + strncpy(buf, ssl_key_passwd, n-1);
520 + return strlen(ssl_key_passwd);
524 + * If verbose, this method traces the status of the SSL handshake.
526 +static void info_callback(const SSL *ssl, int cb, int val)
533 + cbs = "SSL_CB_LOOP";
536 + cbs = "SSL_CB_EXIT";
539 + cbs = "SSL_CB_READ";
542 + cbs = "SSL_CB_WRITE";
545 + cbs = "SSL_CB_ALERT";
547 + case SSL_CB_READ_ALERT:
548 + cbs = "SSL_CB_READ_ALERT";
550 + case SSL_CB_WRITE_ALERT:
551 + cbs = "SSL_CB_WRITE_ALERT";
553 + case SSL_CB_ACCEPT_LOOP:
554 + cbs = "SSL_CB_ACCEPT_LOOP";
556 + case SSL_CB_ACCEPT_EXIT:
557 + cbs = "SSL_CB_ACCEPT_EXIT";
559 + case SSL_CB_CONNECT_LOOP:
560 + cbs = "SSL_CB_CONNECT_LOOP";
562 + case SSL_CB_CONNECT_EXIT:
563 + cbs = "SSL_CB_CONNECT_EXIT";
565 + case SSL_CB_HANDSHAKE_START:
566 + cbs = "SSL_CB_HANDSHAKE_START";
568 + case SSL_CB_HANDSHAKE_DONE:
569 + cbs = "SSL_CB_HANDSHAKE_DONE";
572 + snprintf(buf, sizeof buf, "??? (%d)", cb);
577 + rprintf(FLOG, "SSL: info_callback(%p,%s,%d)\n", ssl, cbs, val);
578 + if (cb == SSL_CB_HANDSHAKE_DONE) {
579 + SSL_CIPHER_description(SSL_get_current_cipher((SSL*)ssl),
581 + rprintf(FLOG, "SSL: cipher: %s", buf);
587 + * Initializes the SSL context for TLSv1 connections; returns zero on
594 + SSL_library_init();
595 + SSL_load_error_strings();
596 + ssl_ctx = SSL_CTX_new(TLSv1_method());
599 + SSL_CTX_set_info_callback(ssl_ctx, info_callback);
601 + /* Sets the certificate sent to the other party. */
602 + if (ssl_cert_path != NULL
603 + && SSL_CTX_use_certificate_file(ssl_ctx, ssl_cert_path,
604 + SSL_FILETYPE_PEM) != 1)
606 + /* Set up the simple non-interactive callback if the password
607 + * was supplied on the command line. */
608 + if (ssl_key_passwd != NULL)
609 + SSL_CTX_set_default_passwd_cb(ssl_ctx, default_password_cb);
610 + /* Sets the private key that matches the public certificate. */
611 + if (ssl_key_path != NULL) {
612 + if (SSL_CTX_use_PrivateKey_file(ssl_ctx, ssl_key_path,
613 + SSL_FILETYPE_PEM) != 1)
615 + if (SSL_CTX_check_private_key(ssl_ctx) != 1)
618 + if (ssl_ca_path != NULL
619 + && !SSL_CTX_load_verify_locations(ssl_ctx, ssl_ca_path, NULL))
626 + * Returns the error string for the current SSL error, if any.
628 +char *get_ssl_error(void)
630 + return ERR_error_string(ERR_get_error(), NULL);
634 + * Returns the input file descriptor for the SSL connection.
636 +int get_tls_rfd(void)
638 + return tls_read[0];
642 + * Returns the output file descriptor for the SSL connection.
644 +int get_tls_wfd(void)
646 + return tls_write[1];
650 + * Signal handler that ends the SSL connection.
652 +static RETSIGTYPE tls_sigusr1(int UNUSED(val))
663 + * Negotiates the TLS connection, creates a socket pair for communicating
664 + * with the rsync process, then forks into a new process that will handle
665 + * the communication.
667 + * 0 is returned on success.
669 +int start_tls(int f_in, int f_out)
673 + unsigned char buf1[BUF_SIZE], buf2[BUF_SIZE];
674 + int avail1 = 0, avail2 = 0, write1 = 0, write2 = 0;
677 + if (fd_pair(tls_read))
679 + if (fd_pair(tls_write))
682 + set_blocking(tls_read[0]);
683 + set_blocking(tls_read[1]);
684 + set_blocking(tls_write[0]);
685 + set_blocking(tls_write[1]);
686 + set_blocking(f_in);
687 + set_blocking(f_out);
689 + ssl_pid = do_fork();
692 + if (ssl_pid != 0) {
693 + close(tls_write[0]);
694 + close(tls_read[1]);
698 + SIGACTION(SIGUSR1, tls_sigusr1);
699 + ssl = SSL_new(ssl_ctx);
702 + if (am_daemon || am_server)
703 + SSL_set_accept_state(ssl);
705 + SSL_set_connect_state(ssl);
706 + SSL_set_rfd(ssl, f_in);
707 + SSL_set_wfd(ssl, f_out);
709 + tls_fd = SSL_get_fd(ssl);
711 + n = MAX(tls_read[1], n);
712 + n = MAX(tls_fd, n) + 1;
715 + while (ssl_running) {
718 + FD_SET(tls_write[0], &rd);
719 + FD_SET(tls_read[1], &wd);
720 + FD_SET(tls_fd, &rd);
721 + FD_SET(tls_fd, &wd);
723 + r = select(n, &rd, &wd, NULL, NULL);
725 + if (r == -1 && errno == EINTR)
727 + if (FD_ISSET(tls_write[0], &rd)) {
728 + r = read(tls_write[0], buf1+avail1, BUF_SIZE-avail1);
732 + rprintf(FERROR, "pipe read error: %s\n",
737 + if (FD_ISSET(tls_fd, &rd)) {
738 + r = SSL_read(ssl, buf2+avail2, BUF_SIZE-avail2);
742 + switch (SSL_get_error(ssl, r)) {
743 + case SSL_ERROR_ZERO_RETURN:
745 + case SSL_ERROR_WANT_READ:
746 + case SSL_ERROR_WANT_WRITE:
748 + case SSL_ERROR_SYSCALL:
750 + rprintf(FERROR, "SSL spurious EOF\n");
752 + rprintf(FERROR, "SSL I/O error: %s\n",
755 + case SSL_ERROR_SSL:
756 + rprintf(FERROR, "SSL: %s\n",
757 + ERR_error_string(ERR_get_error(), NULL));
760 + rprintf(FERROR, "unexpected ssl error %d\n", r);
765 + if (FD_ISSET(tls_read[1], &wd) && write2 < avail2) {
766 + r = write(tls_read[1], buf2+write2, avail2-write2);
770 + rprintf(FERROR, "pipe write error: %s\n",
775 + if (FD_ISSET(tls_fd, &wd) && write1 < avail1) {
776 + r = SSL_write(ssl, buf1+write1, avail1-write1);
780 + switch (SSL_get_error(ssl, r)) {
781 + case SSL_ERROR_ZERO_RETURN:
783 + case SSL_ERROR_WANT_READ:
784 + case SSL_ERROR_WANT_WRITE:
786 + case SSL_ERROR_SYSCALL:
788 + rprintf(FERROR, "SSL: spurious EOF\n");
790 + rprintf(FERROR, "SSL: I/O error: %s\n",
793 + case SSL_ERROR_SSL:
794 + rprintf(FERROR, "SSL: %s\n",
795 + ERR_error_string(ERR_get_error(), NULL));
798 + rprintf(FERROR, "unexpected ssl error %d\n", r);
803 + if (avail1 == write1)
804 + avail1 = write1 = 0;
805 + if (avail2 == write2)
806 + avail2 = write2 = 0;
809 + /* XXX I'm pretty sure that there is a lot that I am not considering
810 + here. Bugs? Yes, probably. */
812 + /* We're finished. */
814 + close(tls_read[1]);
815 + close(tls_write[0]);
820 + * Ends the TLS connection.
825 + kill(ssl_pid, SIGUSR1);