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 @@ -38,7 +38,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 @@ -106,6 +109,11 @@ void _exit_cleanup(int code, const char
62 SIGACTION(SIGUSR1, SIG_IGN);
63 SIGACTION(SIGUSR2, SIG_IGN);
71 rprintf(FINFO,"_exit_cleanup(code=%d, file=%s, line=%d): entered\n",
73 --- old/clientserver.c
74 +++ new/clientserver.c
75 @@ -29,6 +29,9 @@ extern int am_sender;
82 extern int rsync_port;
83 extern int kluge_around_eof;
84 extern int daemon_over_rsh;
85 @@ -105,8 +108,18 @@ int start_socket_client(char *host, char
86 set_socket_options(fd, sockopts);
88 ret = start_inband_exchange(user, path, fd, fd, argc);
94 + int f_in = get_tls_rfd();
95 + int f_out = get_tls_wfd();
96 + return client_run(f_in, f_out, -1, argc, argv);
100 - return ret ? ret : client_run(fd, fd, -1, argc, argv);
101 + return client_run(fd, fd, -1, argc, argv);
104 int start_inband_exchange(char *user, char *path, int f_in, int f_out,
105 @@ -167,6 +180,33 @@ int start_inband_exchange(char *user, ch
107 print_child_argv(sargs);
111 + io_printf(f_out, "#starttls\n");
113 + if (!read_line(f_in, line, sizeof(line)-1)) {
114 + rprintf(FERROR, "rsync: did not receive reply to #starttls\n");
117 + if (strncmp(line, "@ERROR", 6) == 0) {
118 + rprintf(FERROR, "%s\n", line);
121 + if (strcmp(line, "@RSYNCD: starttls") == 0) {
124 + rprintf(FINFO, "%s\n", line);
126 + if (start_tls(f_in, f_out)) {
127 + rprintf(FERROR, "rsync: error during SSL handshake: %s\n",
131 + f_in = get_tls_rfd();
132 + f_out = get_tls_wfd();
136 p = strchr(path,'/');
138 io_printf(f_out, "%s\n", path);
139 @@ -195,6 +235,10 @@ int start_inband_exchange(char *user, ch
140 * server to terminate the listing of modules.
141 * We don't want to go on and transfer
142 * anything; just exit. */
150 @@ -202,6 +246,10 @@ int start_inband_exchange(char *user, ch
151 rprintf(FERROR, "%s\n", line);
152 /* This is always fatal; the server will now
153 * close the socket. */
161 @@ -719,6 +767,7 @@ static void send_listing(int fd)
162 io_printf(fd,"@RSYNCD: EXIT\n");
166 /* this is called when a connection is established to a client
167 and we want to start talking. The setup of the system is done from
169 @@ -777,6 +826,9 @@ int start_daemon(int f_in, int f_out)
170 if (protocol_version > remote_protocol)
171 protocol_version = remote_protocol;
177 if (!read_line(f_in, line, sizeof line - 1))
179 @@ -788,6 +840,20 @@ int start_daemon(int f_in, int f_out)
184 + if (use_ssl && strcmp(line, "#starttls") == 0) {
185 + io_printf(f_out, "@RSYNCD: starttls\n");
186 + if (start_tls(f_in, f_out)) {
187 + rprintf(FLOG, "SSL connection failed: %s\n",
191 + f_in = get_tls_rfd();
192 + f_out = get_tls_wfd();
198 /* it's some sort of command that I don't understand */
199 io_printf(f_out, "@ERROR: Unknown command '%s'\n", line);
202 @@ -282,6 +282,21 @@ if test x"$enable_locale" != x"no"; then
203 AC_DEFINE(CONFIG_LOCALE)
206 +AC_ARG_ENABLE(openssl,
207 + AC_HELP_STRING([--enable-openssl], [compile SSL support with OpenSSL.]))
209 +if test "x$enable_openssl" != xno
212 + AC_CHECK_LIB(ssl, SSL_library_init, , [have_ssl=no])
213 + if test "x$have_ssl" = xyes
215 + AC_DEFINE(HAVE_OPENSSL, 1, [true if you want to use SSL.])
221 AC_MSG_CHECKING([whether to call shutdown on all sockets])
223 *cygwin* ) AC_MSG_RESULT(yes)
226 @@ -172,6 +172,14 @@ int logfile_format_has_o_or_i = 0;
227 int always_checksum = 0;
232 +char *ssl_cert_path = NULL;
233 +char *ssl_key_path = NULL;
234 +char *ssl_key_passwd = NULL;
235 +char *ssl_ca_path = NULL;
238 #define MAX_BATCH_NAME_LEN 256 /* Must be less than MAXPATHLEN-13 */
239 char *batch_name = NULL;
241 @@ -200,6 +208,7 @@ static void print_rsync_version(enum log
242 char const *hardlinks = "no ";
243 char const *links = "no ";
244 char const *ipv6 = "no ";
245 + char const *ssl = "no ";
246 STRUCT_STAT *dumstat;
248 #ifdef HAVE_SOCKETPAIR
249 @@ -222,6 +231,10 @@ static void print_rsync_version(enum log
257 rprintf(f, "%s version %s protocol version %d\n",
258 RSYNC_NAME, RSYNC_VERSION, PROTOCOL_VERSION);
259 rprintf(f, "Copyright (C) 1996-2006 by Andrew Tridgell, Wayne Davison, and others.\n");
260 @@ -234,9 +247,9 @@ static void print_rsync_version(enum log
261 /* Note that this field may not have type ino_t. It depends
262 * on the complicated interaction between largefile feature
264 - rprintf(f, " %sinplace, %sIPv6, "
265 + rprintf(f, " %sinplace, %sIPv6, %sSSL, "
266 "%d-bit system inums, %d-bit internal inums\n",
267 - have_inplace, ipv6,
268 + have_inplace, ipv6, ssl,
269 (int) (sizeof dumstat->st_ino * 8),
270 (int) (sizeof (int64) * 8));
271 #ifdef MAINTAINER_MODE
272 @@ -378,6 +391,13 @@ void usage(enum logcode F)
273 rprintf(F," -4, --ipv4 prefer IPv4\n");
274 rprintf(F," -6, --ipv6 prefer IPv6\n");
277 + rprintf(F," --ssl allow socket connections to use SSL\n");
278 + rprintf(F," --ssl-cert=FILE path to daemon's SSL certificate\n");
279 + rprintf(F," --ssl-key=FILE path to daemon's SSL private key\n");
280 + rprintf(F," --ssl-key-passwd=PASS password for PEM-encoded private key\n");
281 + rprintf(F," --ssl-ca-certs=FILE path to trusted CA certificates\n");
283 rprintf(F," --version print version number\n");
284 rprintf(F,"(-h) --help show this help (-h works with no other options)\n");
286 @@ -390,7 +410,7 @@ enum {OPT_VERSION = 1000, OPT_DAEMON, OP
287 OPT_FILTER, OPT_COMPARE_DEST, OPT_COPY_DEST, OPT_LINK_DEST, OPT_HELP,
288 OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_MODIFY_WINDOW, OPT_MIN_SIZE, OPT_CHMOD,
289 OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_ONLY_WRITE_BATCH, OPT_MAX_SIZE,
291 + OPT_NO_D, OPT_USE_SSL,
292 OPT_SERVER, OPT_REFUSED_BASE = 9000};
294 static struct poptOption long_options[] = {
295 @@ -532,6 +552,13 @@ static struct poptOption long_options[]
296 {"checksum-seed", 0, POPT_ARG_INT, &checksum_seed, 0, 0, 0 },
297 {"server", 0, POPT_ARG_NONE, 0, OPT_SERVER, 0, 0 },
298 {"sender", 0, POPT_ARG_NONE, 0, OPT_SENDER, 0, 0 },
300 + {"ssl", 0, POPT_ARG_NONE, 0, OPT_USE_SSL, 0, 0},
301 + {"ssl-cert", 0, POPT_ARG_STRING, &ssl_cert_path, OPT_USE_SSL, 0, 0},
302 + {"ssl-key", 0, POPT_ARG_STRING, &ssl_key_path, OPT_USE_SSL, 0, 0},
303 + {"ssl-key-passwd", 0, POPT_ARG_STRING, &ssl_key_passwd, OPT_USE_SSL, 0, 0},
304 + {"ssl-ca-certs", 0, POPT_ARG_STRING, &ssl_ca_path, OPT_USE_SSL, 0, 0},
306 /* All the following options switch us into daemon-mode option-parsing. */
307 {"config", 0, POPT_ARG_STRING, 0, OPT_DAEMON, 0, 0 },
308 {"daemon", 0, POPT_ARG_NONE, 0, OPT_DAEMON, 0, 0 },
309 @@ -1080,6 +1107,12 @@ int parse_arguments(int *argc, const cha
320 /* A large opt value means that set_refuse_options()
321 * turned this option off. */
322 @@ -1361,6 +1394,17 @@ int parse_arguments(int *argc, const cha
323 if (delay_updates && !partial_dir)
324 partial_dir = tmp_partialdir;
329 + snprintf(err_buf, sizeof(err_buf),
330 + "Openssl error: %s\n",
338 #ifdef HAVE_FTRUNCATE
340 @@ -1774,11 +1818,28 @@ char *check_for_hostspec(char *s, char *
344 + int url_prefix_len = sizeof URL_PREFIX - 1;
346 - if (port_ptr && strncasecmp(URL_PREFIX, s, strlen(URL_PREFIX)) == 0) {
348 + url_prefix_len = 0;
349 + else if (strncasecmp(URL_PREFIX, s, url_prefix_len) != 0) {
351 + url_prefix_len = sizeof SSL_URL_PREFIX - 1;
352 + if (strncasecmp(SSL_URL_PREFIX, s, url_prefix_len) != 0)
353 + url_prefix_len = 0;
360 + url_prefix_len = 0;
363 + if (url_prefix_len) {
366 - s += strlen(URL_PREFIX);
367 + s += url_prefix_len;
368 if ((p = strchr(s, '/')) != NULL) {
375 #define DEFAULT_LOCK_FILE "/var/run/rsyncd.lock"
376 #define URL_PREFIX "rsync://"
377 +#define SSL_URL_PREFIX "rsyncs://"
379 #define BACKUP_SUFFIX "~"
381 @@ -413,6 +414,11 @@ enum msgcode {
382 # define SIZEOF_INT64 SIZEOF_OFF_T
386 +#include <openssl/ssl.h>
387 +#include <openssl/err.h>
390 /* Starting from protocol version 26, we always use 64-bit
391 * ino_t and dev_t internally, even if this platform does not
392 * allow files to have 64-bit inums. That's because the
396 +/* -*- c-file-style: "linux" -*-
397 + * ssl.c: operations for negotiating SSL rsync connections.
399 + * Copyright (C) 2003 Casey Marshall <rsdio@metastatic.org>
401 + * This program is free software; you can redistribute it and/or modify
402 + * it under the terms of the GNU General Public License as published by
403 + * the Free Software Foundation; either version 2 of the License, or
404 + * (at your option) any later version.
406 + * This program is distributed in the hope that it will be useful,
407 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
408 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
409 + * GNU General Public License for more details.
411 + * You should have received a copy of the GNU General Public License
412 + * along with this program; if not, write to the Free Software
413 + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
418 +#if HAVE_SYS_SELECT_H
419 +#include <sys/select.h>
421 +#include <sys/time.h>
422 +#include <sys/types.h>
427 +#define BUF_SIZE 1024
430 +extern int am_daemon;
431 +extern int am_server;
433 +extern char *ssl_cert_path;
434 +extern char *ssl_key_path;
435 +extern char *ssl_key_passwd;
436 +extern char *ssl_ca_path;
438 +static SSL_CTX *ssl_ctx;
440 +static int tls_read[2] = { -1, -1 };
441 +static int tls_write[2] = { -1, -1 };
442 +static int ssl_running;
443 +static int ssl_pid = -1;
446 + * A non-interactive callback to be passed to SSL_CTX_set_default_password_cb,
447 + * which merely copies the value of ssl_key_passwd into buf. This is
448 + * used for when the private key password is supplied via an option.
450 +static int default_password_cb(char *buf, int n, UNUSED(int f), UNUSED(void *u))
452 + if (ssl_key_passwd == NULL || n < (int)strlen(ssl_key_passwd))
454 + strncpy(buf, ssl_key_passwd, n-1);
455 + return strlen(ssl_key_passwd);
459 + * If verbose, this method traces the status of the SSL handshake.
461 +static void info_callback(const SSL *ssl, int cb, int val)
468 + cbs = "SSL_CB_LOOP";
471 + cbs = "SSL_CB_EXIT";
474 + cbs = "SSL_CB_READ";
477 + cbs = "SSL_CB_WRITE";
480 + cbs = "SSL_CB_ALERT";
482 + case SSL_CB_READ_ALERT:
483 + cbs = "SSL_CB_READ_ALERT";
485 + case SSL_CB_WRITE_ALERT:
486 + cbs = "SSL_CB_WRITE_ALERT";
488 + case SSL_CB_ACCEPT_LOOP:
489 + cbs = "SSL_CB_ACCEPT_LOOP";
491 + case SSL_CB_ACCEPT_EXIT:
492 + cbs = "SSL_CB_ACCEPT_EXIT";
494 + case SSL_CB_CONNECT_LOOP:
495 + cbs = "SSL_CB_CONNECT_LOOP";
497 + case SSL_CB_CONNECT_EXIT:
498 + cbs = "SSL_CB_CONNECT_EXIT";
500 + case SSL_CB_HANDSHAKE_START:
501 + cbs = "SSL_CB_HANDSHAKE_START";
503 + case SSL_CB_HANDSHAKE_DONE:
504 + cbs = "SSL_CB_HANDSHAKE_DONE";
507 + snprintf(buf, sizeof buf, "??? (%d)", cb);
512 + rprintf(FLOG, "SSL: info_callback(%p,%s,%d)\n", ssl, cbs, val);
513 + if (cb == SSL_CB_HANDSHAKE_DONE) {
514 + SSL_CIPHER_description(SSL_get_current_cipher((SSL*)ssl),
516 + rprintf(FLOG, "SSL: cipher: %s", buf);
522 + * Initializes the SSL context for TLSv1 connections; returns zero on
529 + SSL_library_init();
530 + SSL_load_error_strings();
531 + ssl_ctx = SSL_CTX_new(TLSv1_method());
534 + SSL_CTX_set_info_callback(ssl_ctx, info_callback);
536 + /* Sets the certificate sent to the other party. */
537 + if (ssl_cert_path != NULL
538 + && SSL_CTX_use_certificate_file(ssl_ctx, ssl_cert_path,
539 + SSL_FILETYPE_PEM) != 1)
541 + /* Set up the simple non-interactive callback if the password
542 + * was supplied on the command line. */
543 + if (ssl_key_passwd != NULL)
544 + SSL_CTX_set_default_passwd_cb(ssl_ctx, default_password_cb);
545 + /* Sets the private key that matches the public certificate. */
546 + if (ssl_key_path != NULL) {
547 + if (SSL_CTX_use_PrivateKey_file(ssl_ctx, ssl_key_path,
548 + SSL_FILETYPE_PEM) != 1)
550 + if (SSL_CTX_check_private_key(ssl_ctx) != 1)
553 + if (ssl_ca_path != NULL
554 + && !SSL_CTX_load_verify_locations(ssl_ctx, ssl_ca_path, NULL))
561 + * Returns the error string for the current SSL error, if any.
563 +char *get_ssl_error(void)
565 + return ERR_error_string(ERR_get_error(), NULL);
569 + * Returns the input file descriptor for the SSL connection.
571 +int get_tls_rfd(void)
573 + return tls_read[0];
577 + * Returns the output file descriptor for the SSL connection.
579 +int get_tls_wfd(void)
581 + return tls_write[1];
585 + * Signal handler that ends the SSL connection.
587 +static RETSIGTYPE tls_sigusr1(int UNUSED(val))
598 + * Negotiates the TLS connection, creates a socket pair for communicating
599 + * with the rsync process, then forks into a new process that will handle
600 + * the communication.
602 + * 0 is returned on success.
604 +int start_tls(int f_in, int f_out)
608 + unsigned char buf1[BUF_SIZE], buf2[BUF_SIZE];
609 + int avail1 = 0, avail2 = 0, write1 = 0, write2 = 0;
612 + if (fd_pair(tls_read))
614 + if (fd_pair(tls_write))
617 + set_blocking(tls_read[0]);
618 + set_blocking(tls_read[1]);
619 + set_blocking(tls_write[0]);
620 + set_blocking(tls_write[1]);
621 + set_blocking(f_in);
622 + set_blocking(f_out);
624 + ssl_pid = do_fork();
627 + if (ssl_pid != 0) {
628 + close(tls_write[0]);
629 + close(tls_read[1]);
633 + signal(SIGUSR1, tls_sigusr1);
634 + ssl = SSL_new(ssl_ctx);
637 + if (am_daemon || am_server)
638 + SSL_set_accept_state(ssl);
640 + SSL_set_connect_state(ssl);
641 + SSL_set_rfd(ssl, f_in);
642 + SSL_set_wfd(ssl, f_out);
644 + tls_fd = SSL_get_fd(ssl);
646 + n = MAX(tls_read[1], n);
647 + n = MAX(tls_fd, n) + 1;
650 + while (ssl_running) {
653 + FD_SET(tls_write[0], &rd);
654 + FD_SET(tls_read[1], &wd);
655 + FD_SET(tls_fd, &rd);
656 + FD_SET(tls_fd, &wd);
658 + r = select(n, &rd, &wd, NULL, NULL);
660 + if (r == -1 && errno == EINTR)
662 + if (FD_ISSET(tls_write[0], &rd)) {
663 + r = read(tls_write[0], buf1+avail1, BUF_SIZE-avail1);
667 + rprintf(FERROR, "pipe read error: %s\n",
672 + if (FD_ISSET(tls_fd, &rd)) {
673 + r = SSL_read(ssl, buf2+avail2, BUF_SIZE-avail2);
677 + switch (SSL_get_error(ssl, r)) {
678 + case SSL_ERROR_ZERO_RETURN:
680 + case SSL_ERROR_WANT_READ:
681 + case SSL_ERROR_WANT_WRITE:
683 + case SSL_ERROR_SYSCALL:
685 + rprintf(FERROR, "SSL spurious EOF\n");
687 + rprintf(FERROR, "SSL I/O error: %s\n",
690 + case SSL_ERROR_SSL:
691 + rprintf(FERROR, "SSL: %s\n",
692 + ERR_error_string(ERR_get_error(), NULL));
695 + rprintf(FERROR, "unexpected ssl error %d\n", r);
700 + if (FD_ISSET(tls_read[1], &wd) && write2 < avail2) {
701 + r = write(tls_read[1], buf2+write2, avail2-write2);
705 + rprintf(FERROR, "pipe write error: %s\n",
710 + if (FD_ISSET(tls_fd, &wd) && write1 < avail1) {
711 + r = SSL_write(ssl, buf1+write1, avail1-write1);
715 + switch (SSL_get_error(ssl, r)) {
716 + case SSL_ERROR_ZERO_RETURN:
718 + case SSL_ERROR_WANT_READ:
719 + case SSL_ERROR_WANT_WRITE:
721 + case SSL_ERROR_SYSCALL:
723 + rprintf(FERROR, "SSL: spurious EOF\n");
725 + rprintf(FERROR, "SSL: I/O error: %s\n",
728 + case SSL_ERROR_SSL:
729 + rprintf(FERROR, "SSL: %s\n",
730 + ERR_error_string(ERR_get_error(), NULL));
733 + rprintf(FERROR, "unexpected ssl error %d\n", r);
738 + if (avail1 == write1)
739 + avail1 = write1 = 0;
740 + if (avail2 == write2)
741 + avail2 = write2 = 0;
744 + /* XXX I'm pretty sure that there is a lot that I am not considering
745 + here. Bugs? Yes, probably. */
747 + /* We're finished. */
749 + close(tls_read[1]);
750 + close(tls_write[0]);
755 + * Ends the TLS connection.
760 + kill(ssl_pid, SIGUSR1);