1 After applying this patch, run these commands for a successful build:
11 I've been hacking together a way to use rsync with OpenSSL, and have
12 attached my current patch against a recent CVS tree. The details of
13 this implementation are:
15 1. The SSL code is added as a "layer" that is forked into its own
18 2. An SSL connection is established by the client issuing the
23 And, if the server allows SSL, it replies with
27 At which point both sides begin negotiating the SSL connection.
28 Servers that can't or don't want to use SSL just treat it as a
29 normal unknown command.
31 3. The SSL code is meant to be unobtrusive, and when this patch is
32 applied the program may still be built with no SSL code.
34 4. There are a number of details not implemented.
36 All warnings apply; I don't do C programming all that often, so I
37 can't say if I've left any cleanup/compatibility errors in the code.
40 --- orig/Makefile.in 2004-07-04 08:59:17
41 +++ Makefile.in 2004-07-03 20:22:28
42 @@ -39,7 +39,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/permstring.o
51 --- orig/cleanup.c 2004-07-20 21:36:07
52 +++ cleanup.c 2004-07-03 20:22:28
55 extern int keep_partial;
56 extern int log_got_error;
62 * Close all open sockets and files, allowing a (somewhat) graceful
63 @@ -97,6 +100,11 @@ void _exit_cleanup(int code, const char
64 signal(SIGUSR1, SIG_IGN);
65 signal(SIGUSR2, SIG_IGN);
73 rprintf(FINFO,"_exit_cleanup(code=%d, file=%s, line=%d): entered\n",
75 --- orig/clientserver.c 2004-07-17 15:20:05
76 +++ clientserver.c 2004-07-03 20:22:28
77 @@ -46,6 +46,9 @@ extern int io_timeout;
78 extern int orig_umask;
80 extern int default_af_hint;
84 extern char *bind_address;
85 extern struct exclude_list_struct server_exclude_list;
86 extern char *exclude_path_prefix;
87 @@ -93,8 +96,18 @@ int start_socket_client(char *host, char
88 exit_cleanup(RERR_SOCKETIO);
90 ret = start_inband_exchange(user, path, fd, fd, argc);
96 + int f_in = get_tls_rfd();
97 + int f_out = get_tls_wfd();
98 + return client_run(f_in, f_out, -1, argc, argv);
102 - return ret < 0? ret : client_run(fd, fd, -1, argc, argv);
103 + return client_run(fd, fd, -1, argc, argv);
106 int start_inband_exchange(char *user, char *path, int f_in, int f_out,
107 @@ -149,6 +162,33 @@ int start_inband_exchange(char *user, ch
108 if (protocol_version > remote_protocol)
109 protocol_version = remote_protocol;
113 + io_printf(f_out, "#starttls\n");
115 + if (!read_line(f_in, line, sizeof(line)-1)) {
116 + rprintf(FERROR, "rsync: did not receive reply to #starttls\n");
119 + if (strncmp(line, "@ERROR", 6) == 0) {
120 + rprintf(FERROR, "rsync: ssl connection denied\n");
123 + if (strcmp(line, "@RSYNCD: starttls") == 0) {
126 + rprintf(FINFO, "%s\n", line);
128 + if (start_tls(f_in, f_out)) {
129 + rprintf(FERROR, "rsync: error during SSL handshake: %s\n",
133 + f_in = get_tls_rfd();
134 + f_out = get_tls_wfd();
138 p = strchr(path,'/');
140 io_printf(f_out, "%s\n", path);
141 @@ -177,6 +217,10 @@ int start_inband_exchange(char *user, ch
142 * server to terminate the listing of modules.
143 * We don't want to go on and transfer
144 * anything; just exit. */
152 @@ -184,6 +228,10 @@ int start_inband_exchange(char *user, ch
153 rprintf(FERROR, "%s\n", line);
154 /* This is always fatal; the server will now
155 * close the socket. */
160 return RERR_STARTCLIENT;
162 rprintf(FINFO,"%s\n", line);
163 @@ -488,6 +536,7 @@ static void send_listing(int fd)
164 io_printf(fd,"@RSYNCD: EXIT\n");
168 /* this is called when a connection is established to a client
169 and we want to start talking. The setup of the system is done from
171 @@ -547,6 +596,20 @@ int start_daemon(int f_in, int f_out)
176 + if (use_ssl && strcmp(line, "#starttls") == 0) {
177 + io_printf(f_out, "@RSYNCD: starttls\n");
178 + if (start_tls(f_in, f_out)) {
179 + rprintf(FLOG, "SSL connection failed: %s\n",
183 + f_in = get_tls_rfd();
184 + f_out = get_tls_wfd();
190 /* it's some sort of command that I don't understand */
191 io_printf(f_out, "@ERROR: Unknown command '%s'\n", line);
192 --- orig/configure.in 2004-07-16 20:07:22
193 +++ configure.in 2004-07-03 20:22:28
194 @@ -271,6 +271,21 @@ yes
195 AC_SEARCH_LIBS(getaddrinfo, inet6)
198 +AC_ARG_ENABLE(openssl,
199 + AC_HELP_STRING([--enable-openssl], [compile SSL support with OpenSSL.]))
201 +if test "x$enable_openssl" != xno
204 + AC_CHECK_LIB(ssl, SSL_library_init, , [have_ssl=no])
205 + if test "x$have_ssl" = xyes
207 + AC_DEFINE(HAVE_OPENSSL, 1, [true if you want to use SSL.])
213 AC_MSG_CHECKING([whether to call shutdown on all sockets])
215 *cygwin* ) AC_MSG_RESULT(yes)
216 --- orig/main.c 2004-07-21 23:59:35
217 +++ main.c 2004-07-15 02:40:51
218 @@ -55,6 +55,9 @@ extern int write_batch;
220 extern int batch_gen_fd;
221 extern int filesfrom_fd;
225 extern pid_t cleanup_child_pid;
226 extern char *files_from;
227 extern char *remote_filesfrom_file;
228 @@ -750,18 +753,32 @@ static int start_client(int argc, char *
232 + int url_prefix = strlen(URL_PREFIX);
234 /* Don't clobber argv[] so that ps(1) can still show the right
236 if ((rc = copy_argv(argv)))
239 + if (strncasecmp(URL_PREFIX, argv[0], url_prefix) != 0 && !read_batch) {
241 + url_prefix = strlen(SSL_URL_PREFIX);
242 + if (strncasecmp(SSL_URL_PREFIX, argv[0], url_prefix) != 0)
253 /* rsync:// always uses rsync server over direct socket connection */
254 - if (strncasecmp(URL_PREFIX, argv[0], strlen(URL_PREFIX)) == 0
259 - host = argv[0] + strlen(URL_PREFIX);
260 + host = argv[0] + url_prefix;
261 p = strchr(host,'/');
264 @@ -809,12 +826,27 @@ static int start_client(int argc, char *
266 } else { /* source is local */
268 + url_prefix = strlen(URL_PREFIX);
269 + if (strncasecmp(URL_PREFIX, argv[0], url_prefix) != 0) {
271 + url_prefix = strlen(SSL_URL_PREFIX);
272 + if (strncasecmp(SSL_URL_PREFIX, argv[0], url_prefix) != 0)
284 /* rsync:// destination uses rsync server over direct socket */
285 - if (strncasecmp(URL_PREFIX, argv[argc-1], strlen(URL_PREFIX)) == 0) {
289 - host = argv[argc-1] + strlen(URL_PREFIX);
290 + host = argv[argc-1] + url_prefix;
291 p = strchr(host,'/');
294 --- orig/options.c 2004-07-21 23:59:35
295 +++ options.c 2004-07-16 20:19:50
296 @@ -134,6 +134,14 @@ int quiet = 0;
297 int always_checksum = 0;
302 +char *ssl_cert_path = NULL;
303 +char *ssl_key_path = NULL;
304 +char *ssl_key_passwd = NULL;
305 +char *ssl_ca_path = NULL;
308 #define MAX_BATCH_NAME_LEN 256 /* Must be less than MAXPATHLEN-13 */
309 char *batch_name = NULL;
311 @@ -153,6 +161,7 @@ static void print_rsync_version(enum log
312 char const *hardlinks = "no ";
313 char const *links = "no ";
314 char const *ipv6 = "no ";
315 + char const *ssl = "no ";
316 STRUCT_STAT *dumstat;
318 #ifdef HAVE_SOCKETPAIR
319 @@ -175,6 +184,10 @@ static void print_rsync_version(enum log
327 rprintf(f, "%s version %s protocol version %d\n",
328 RSYNC_NAME, RSYNC_VERSION, PROTOCOL_VERSION);
330 @@ -188,10 +201,10 @@ static void print_rsync_version(enum log
331 /* Note that this field may not have type ino_t. It depends
332 * on the complicated interaction between largefile feature
334 - rprintf(f, " %sinplace, %sIPv6, %d-bit system inums, %d-bit internal inums\n",
335 + rprintf(f, " %sinplace, %sIPv6, %d-bit system inums, %d-bit internal inums, %sssl\n",
337 (int) (sizeof dumstat->st_ino * 8),
338 - (int) (sizeof (uint64) * 8));
339 + (int) (sizeof (uint64) * 8), ssl);
340 #ifdef MAINTAINER_MODE
341 rprintf(f, " panic action: \"%s\"\n",
343 @@ -305,6 +318,13 @@ void usage(enum logcode F)
344 rprintf(F," -4, --ipv4 prefer IPv4\n");
345 rprintf(F," -6, --ipv6 prefer IPv6\n");
348 + rprintf(F," --ssl allow socket connections to use SSL\n");
349 + rprintf(F," --ssl-cert=FILE path to server's SSL certificate\n");
350 + rprintf(F," --ssl-key=FILE path to server's SSL private key\n");
351 + rprintf(F," --ssl-key-passwd=PASS password for PEM-encoded private key\n");
352 + rprintf(F," --ssl-ca-certs=FILE path to trusted CA certificates\n");
354 rprintf(F," -h, --help show this help screen\n");
357 @@ -316,7 +336,7 @@ void usage(enum logcode F)
358 enum {OPT_VERSION = 1000, OPT_SENDER, OPT_EXCLUDE, OPT_EXCLUDE_FROM,
359 OPT_DELETE_AFTER, OPT_DELETE_EXCLUDED, OPT_LINK_DEST,
360 OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_MODIFY_WINDOW,
361 - OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_TIMEOUT,
362 + OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_TIMEOUT, OPT_USE_SSL,
363 OPT_REFUSED_BASE = 9000};
365 static struct poptOption long_options[] = {
366 @@ -404,6 +424,13 @@ static struct poptOption long_options[]
367 {"ipv4", '4', POPT_ARG_VAL, &default_af_hint, AF_INET, 0, 0 },
368 {"ipv6", '6', POPT_ARG_VAL, &default_af_hint, AF_INET6, 0, 0 },
371 + {"ssl", 0, POPT_ARG_NONE, 0, OPT_USE_SSL, 0, 0},
372 + {"ssl-cert", 0, POPT_ARG_STRING, &ssl_cert_path, OPT_USE_SSL, 0, 0},
373 + {"ssl-key", 0, POPT_ARG_STRING, &ssl_key_path, OPT_USE_SSL, 0, 0},
374 + {"ssl-key-passwd", 0, POPT_ARG_STRING, &ssl_key_passwd, OPT_USE_SSL, 0, 0},
375 + {"ssl-ca-certs", 0, POPT_ARG_STRING, &ssl_ca_path, OPT_USE_SSL, 0, 0},
380 @@ -601,6 +628,12 @@ int parse_arguments(int *argc, const cha
391 /* A large opt value means that set_refuse_options()
392 * turned this option off (opt-BASE is its index). */
393 @@ -761,6 +794,17 @@ int parse_arguments(int *argc, const cha
394 if (do_progress && !verbose)
400 + snprintf(err_buf, sizeof(err_buf),
401 + "Openssl error: %s\n",
409 bwlimit_writemax = (size_t)bwlimit * 128;
410 if (bwlimit_writemax < 512)
411 --- orig/rsync.h 2004-07-20 21:36:08
412 +++ rsync.h 2004-07-03 20:22:28
415 #define DEFAULT_LOCK_FILE "/var/run/rsyncd.lock"
416 #define URL_PREFIX "rsync://"
417 +#define SSL_URL_PREFIX "rsyncs://"
419 #define BACKUP_SUFFIX "~"
421 @@ -326,6 +327,11 @@ enum msgcode {
422 #define uint64 unsigned off_t
426 +#include <openssl/ssl.h>
427 +#include <openssl/err.h>
430 /* Starting from protocol version 26, we always use 64-bit
431 * ino_t and dev_t internally, even if this platform does not
432 * allow files to have 64-bit inums. That's because the
433 --- orig/ssl.c 2004-07-02 21:44:19
434 +++ ssl.c 2004-07-02 21:44:19
436 +/* -*- c-file-style: "linux" -*-
437 + * ssl.c: operations for negotiating SSL rsync connections.
439 + * Copyright (C) 2003 Casey Marshall <rsdio@metastatic.org>
441 + * This program is free software; you can redistribute it and/or modify
442 + * it under the terms of the GNU General Public License as published by
443 + * the Free Software Foundation; either version 2 of the License, or
444 + * (at your option) any later version.
446 + * This program is distributed in the hope that it will be useful,
447 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
448 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
449 + * GNU General Public License for more details.
451 + * You should have received a copy of the GNU General Public License
452 + * along with this program; if not, write to the Free Software
453 + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
458 +#ifdef HAVE_SYS_SELECT_H
459 +#include <sys/select.h>
461 +#include <sys/time.h>
462 +#include <sys/types.h>
467 +#define BUF_SIZE 1024
470 +extern int am_daemon;
471 +extern int am_server;
473 +extern char *ssl_cert_path;
474 +extern char *ssl_key_path;
475 +extern char *ssl_key_passwd;
476 +extern char *ssl_ca_path;
478 +static SSL_CTX *ssl_ctx;
480 +static int tls_read[2] = { -1, -1 };
481 +static int tls_write[2] = { -1, -1 };
482 +static int ssl_running;
483 +static int ssl_pid = -1;
486 + * A non-interactive callback to be passed to SSL_CTX_set_default_password_cb,
487 + * which merely copies the value of ssl_key_passwd into buf. This is
488 + * used for when the private key password is supplied via an option.
490 +static int default_password_cb(char *buf, int n, UNUSED(int f), UNUSED(void *u))
492 + if (ssl_key_passwd == NULL || n < strlen(ssl_key_passwd))
494 + strncpy(buf, ssl_key_passwd, n-1);
495 + return strlen(ssl_key_passwd);
499 + * If verbose, this method traces the status of the SSL handshake.
501 +static void info_callback(SSL *ssl, int cb, int val)
508 + cbs = "SSL_CB_LOOP";
511 + cbs = "SSL_CB_EXIT";
514 + cbs = "SSL_CB_READ";
517 + cbs = "SSL_CB_WRITE";
520 + cbs = "SSL_CB_ALERT";
522 + case SSL_CB_READ_ALERT:
523 + cbs = "SSL_CB_READ_ALERT";
525 + case SSL_CB_WRITE_ALERT:
526 + cbs = "SSL_CB_WRITE_ALERT";
528 + case SSL_CB_ACCEPT_LOOP:
529 + cbs = "SSL_CB_ACCEPT_LOOP";
531 + case SSL_CB_ACCEPT_EXIT:
532 + cbs = "SSL_CB_ACCEPT_EXIT";
534 + case SSL_CB_CONNECT_LOOP:
535 + cbs = "SSL_CB_CONNECT_LOOP";
537 + case SSL_CB_CONNECT_EXIT:
538 + cbs = "SSL_CB_CONNECT_EXIT";
540 + case SSL_CB_HANDSHAKE_START:
541 + cbs = "SSL_CB_HANDSHAKE_START";
543 + case SSL_CB_HANDSHAKE_DONE:
544 + cbs = "SSL_CB_HANDSHAKE_DONE";
547 + snprintf(buf, sizeof buf, "??? (%d)", cb);
552 + rprintf(FLOG, "SSL: info_callback(%p,%s,%d)\n", ssl, cbs, val);
553 + if (cb == SSL_CB_HANDSHAKE_DONE) {
554 + SSL_CIPHER_description(SSL_get_current_cipher(ssl),
556 + rprintf(FLOG, "SSL: cipher: %s", buf);
562 + * Initializes the SSL context for TLSv1 connections; returns zero on
569 + SSL_library_init();
570 + SSL_load_error_strings();
571 + ssl_ctx = SSL_CTX_new(TLSv1_method());
574 + SSL_CTX_set_info_callback(ssl_ctx, info_callback);
576 + /* Sets the certificate sent to the other party. */
577 + if (ssl_cert_path != NULL
578 + && SSL_CTX_use_certificate_file(ssl_ctx, ssl_cert_path,
579 + SSL_FILETYPE_PEM) != 1)
581 + /* Set up the simple non-interactive callback if the password
582 + * was supplied on the command line. */
583 + if (ssl_key_passwd != NULL)
584 + SSL_CTX_set_default_passwd_cb(ssl_ctx, default_password_cb);
585 + /* Sets the private key that matches the public certificate. */
586 + if (ssl_key_path != NULL) {
587 + if (SSL_CTX_use_PrivateKey_file(ssl_ctx, ssl_key_path,
588 + SSL_FILETYPE_PEM) != 1)
590 + if (SSL_CTX_check_private_key(ssl_ctx) != 1)
593 + if (ssl_ca_path != NULL
594 + && !SSL_CTX_load_verify_locations(ssl_ctx, ssl_ca_path, NULL))
601 + * Returns the error string for the current SSL error, if any.
603 +char *get_ssl_error(void)
605 + return ERR_error_string(ERR_get_error(), NULL);
609 + * Returns the input file descriptor for the SSL connection.
611 +int get_tls_rfd(void)
613 + return tls_read[0];
617 + * Returns the output file descriptor for the SSL connection.
619 +int get_tls_wfd(void)
621 + return tls_write[1];
625 + * Signal handler that ends the SSL connection.
627 +static RETSIGTYPE tls_sigusr1(int UNUSED(val))
638 + * Negotiates the TLS connection, creates a socket pair for communicating
639 + * with the rsync process, then forks into a new process that will handle
640 + * the communication.
642 + * 0 is returned on success.
644 +int start_tls(int f_in, int f_out)
648 + unsigned char buf1[BUF_SIZE], buf2[BUF_SIZE];
649 + int avail1 = 0, avail2 = 0, write1 = 0, write2 = 0;
652 + if (fd_pair(tls_read))
654 + if (fd_pair(tls_write))
657 + set_blocking(tls_read[0]);
658 + set_blocking(tls_read[1]);
659 + set_blocking(tls_write[0]);
660 + set_blocking(tls_write[1]);
661 + set_blocking(f_in);
662 + set_blocking(f_out);
664 + ssl_pid = do_fork();
667 + if (ssl_pid != 0) {
668 + close(tls_write[0]);
669 + close(tls_read[1]);
673 + signal(SIGUSR1, tls_sigusr1);
674 + ssl = SSL_new(ssl_ctx);
677 + if (am_daemon || am_server)
678 + SSL_set_accept_state(ssl);
680 + SSL_set_connect_state(ssl);
681 + SSL_set_rfd(ssl, f_in);
682 + SSL_set_wfd(ssl, f_out);
684 + tls_fd = SSL_get_fd(ssl);
686 + n = MAX(tls_read[1], n);
687 + n = MAX(tls_fd, n) + 1;
690 + while (ssl_running) {
693 + FD_SET(tls_write[0], &rd);
694 + FD_SET(tls_read[1], &wd);
695 + FD_SET(tls_fd, &rd);
696 + FD_SET(tls_fd, &wd);
698 + r = select(n, &rd, &wd, NULL, NULL);
700 + if (r == -1 && errno == EINTR)
702 + if (FD_ISSET(tls_write[0], &rd)) {
703 + r = read(tls_write[0], buf1+avail1, BUF_SIZE-avail1);
707 + rprintf(FERROR, "pipe read error: %s\n",
712 + if (FD_ISSET(tls_fd, &rd)) {
713 + r = SSL_read(ssl, buf2+avail2, BUF_SIZE-avail2);
717 + switch (SSL_get_error(ssl, r)) {
718 + case SSL_ERROR_ZERO_RETURN:
720 + case SSL_ERROR_WANT_READ:
721 + case SSL_ERROR_WANT_WRITE:
723 + case SSL_ERROR_SYSCALL:
725 + rprintf(FERROR, "SSL spurious EOF\n");
727 + rprintf(FERROR, "SSL I/O error: %s\n",
730 + case SSL_ERROR_SSL:
731 + rprintf(FERROR, "SSL: %s\n",
732 + ERR_error_string(ERR_get_error(), NULL));
735 + rprintf(FERROR, "unexpected ssl error %d\n", r);
740 + if (FD_ISSET(tls_read[1], &wd) && write2 < avail2) {
741 + r = write(tls_read[1], buf2+write2, avail2-write2);
745 + rprintf(FERROR, "pipe write error: %s\n",
750 + if (FD_ISSET(tls_fd, &wd) && write1 < avail1) {
751 + r = SSL_write(ssl, buf1+write1, avail1-write1);
755 + switch (SSL_get_error(ssl, r)) {
756 + case SSL_ERROR_ZERO_RETURN:
758 + case SSL_ERROR_WANT_READ:
759 + case SSL_ERROR_WANT_WRITE:
761 + case SSL_ERROR_SYSCALL:
763 + rprintf(FERROR, "SSL: spurious EOF\n");
765 + rprintf(FERROR, "SSL: I/O error: %s\n",
768 + case SSL_ERROR_SSL:
769 + rprintf(FERROR, "SSL: %s\n",
770 + ERR_error_string(ERR_get_error(), NULL));
773 + rprintf(FERROR, "unexpected ssl error %d\n", r);
778 + if (avail1 == write1)
779 + avail1 = write1 = 0;
780 + if (avail2 == write2)
781 + avail2 = write2 = 0;
784 + /* XXX I'm pretty sure that there is a lot that I am not considering
785 + here. Bugs? Yes, probably. */
787 + /* We're finished. */
789 + close(tls_read[1]);
790 + close(tls_write[0]);
795 + * Ends the TLS connection.
800 + kill(ssl_pid, SIGUSR1);