Updated to work with the latest g2r-basis-filename patch.
[rsync/rsync-patches.git] / openssl-support.diff
CommitLineData
8a529471 1After applying this patch, run these commands for a successful build:
ce06af28 2
8a529471
WD
3 autoconf
4 autoheader
5 ./configure
6 make proto
7 make
8
9Casey Marshall writes:
ce06af28
WD
10
11I've been hacking together a way to use rsync with OpenSSL, and have
12attached my current patch against a recent CVS tree. The details of
13this implementation are:
14
15 1. The SSL code is added as a "layer" that is forked into its own
16 process.
17
18 2. An SSL connection is established by the client issuing the
19 command:
20
21 #starttls
22
23 And, if the server allows SSL, it replies with
24
25 @RSYNCD: starttls
26
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.
30
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.
33
34 4. There are a number of details not implemented.
35
36All warnings apply; I don't do C programming all that often, so I
37can't say if I've left any cleanup/compatibility errors in the code.
38
ce06af28 39
9be39c35 40--- orig/Makefile.in 2004-07-04 08:59:17
13bed3dd 41+++ Makefile.in 2004-07-03 20:22:28
ce06af28
WD
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@
48
49 TLS_OBJ = tls.o syscall.o lib/permstring.o
50
2f5fa77e 51--- orig/cleanup.c 2004-07-20 21:36:07
13bed3dd 52+++ cleanup.c 2004-07-03 20:22:28
8a529471
WD
53@@ -24,6 +24,9 @@
54 extern int io_error;
55 extern int keep_partial;
56 extern int log_got_error;
ce06af28 57+#ifdef HAVE_OPENSSL
8a529471 58+extern int use_ssl;
ce06af28 59+#endif
ce06af28 60
8a529471
WD
61 /**
62 * Close all open sockets and files, allowing a (somewhat) graceful
2f5fa77e 63@@ -97,6 +100,11 @@ void _exit_cleanup(int code, const char
ce06af28
WD
64 signal(SIGUSR1, SIG_IGN);
65 signal(SIGUSR2, SIG_IGN);
78114162 66
ce06af28
WD
67+#ifdef HAVE_OPENSSL
68+ if (use_ssl)
69+ end_tls();
70+#endif
78114162 71+
8a529471
WD
72 if (verbose > 3) {
73 rprintf(FINFO,"_exit_cleanup(code=%d, file=%s, line=%d): entered\n",
78114162 74 code, file, line);
2f5fa77e 75--- orig/clientserver.c 2004-07-17 15:20:05
13bed3dd 76+++ clientserver.c 2004-07-03 20:22:28
ce06af28
WD
77@@ -46,6 +46,9 @@ extern int io_timeout;
78 extern int orig_umask;
79 extern int no_detach;
80 extern int default_af_hint;
81+#ifdef HAVE_OPENSSL
82+extern int use_ssl;
83+#endif
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);
89
90 ret = start_inband_exchange(user, path, fd, fd, argc);
91+ if (ret < 0)
92+ return ret;
93+
94+#ifdef HAVE_OPENSSL
95+ if (use_ssl) {
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);
99+ }
100+#endif
101
102- return ret < 0? ret : client_run(fd, fd, -1, argc, argv);
103+ return client_run(fd, fd, -1, argc, argv);
104 }
105
9be39c35
WD
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
ce06af28
WD
108 if (protocol_version > remote_protocol)
109 protocol_version = remote_protocol;
110
111+#ifdef HAVE_OPENSSL
112+ if (use_ssl) {
113+ io_printf(f_out, "#starttls\n");
114+ while (1) {
115+ if (!read_line(f_in, line, sizeof(line)-1)) {
116+ rprintf(FERROR, "rsync: did not receive reply to #starttls\n");
117+ return -1;
118+ }
119+ if (strncmp(line, "@ERROR", 6) == 0) {
120+ rprintf(FERROR, "rsync: ssl connection denied\n");
121+ return -1;
122+ }
123+ if (strcmp(line, "@RSYNCD: starttls") == 0) {
124+ break;
125+ }
126+ rprintf(FINFO, "%s\n", line);
127+ }
128+ if (start_tls(f_in, f_out)) {
129+ rprintf(FERROR, "rsync: error during SSL handshake: %s\n",
130+ get_ssl_error());
131+ return -1;
132+ }
133+ f_in = get_tls_rfd();
134+ f_out = get_tls_wfd();
135+ }
136+#endif
137+
138 p = strchr(path,'/');
139 if (p) *p = 0;
140 io_printf(f_out, "%s\n", path);
9be39c35 141@@ -177,6 +217,10 @@ int start_inband_exchange(char *user, ch
ce06af28
WD
142 * server to terminate the listing of modules.
143 * We don't want to go on and transfer
144 * anything; just exit. */
145+#ifdef HAVE_OPENSSL
146+ if (use_ssl)
147+ end_tls();
148+#endif
149 exit(0);
150 }
151
9be39c35 152@@ -184,6 +228,10 @@ int start_inband_exchange(char *user, ch
7628f156 153 rprintf(FERROR, "%s\n", line);
ce06af28
WD
154 /* This is always fatal; the server will now
155 * close the socket. */
156+#ifdef HAVE_OPENSSL
157+ if (use_ssl)
158+ end_tls();
159+#endif
160 return RERR_STARTCLIENT;
161 } else {
162 rprintf(FINFO,"%s\n", line);
9be39c35 163@@ -488,6 +536,7 @@ static void send_listing(int fd)
ce06af28
WD
164 io_printf(fd,"@RSYNCD: EXIT\n");
165 }
166
167+
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
170 here */
2f5fa77e 171@@ -547,6 +596,20 @@ int start_daemon(int f_in, int f_out)
ce06af28
WD
172 return -1;
173 }
78114162 174
ce06af28
WD
175+#if HAVE_OPENSSL
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",
180+ get_ssl_error());
181+ return -1;
182+ }
183+ f_in = get_tls_rfd();
184+ f_out = get_tls_wfd();
185+ continue;
186+ }
187+#endif
78114162 188+
ce06af28
WD
189 if (*line == '#') {
190 /* it's some sort of command that I don't understand */
78114162 191 io_printf(f_out, "@ERROR: Unknown command '%s'\n", line);
f6c3b300 192--- orig/configure.in 2004-07-16 20:07:22
13bed3dd 193+++ configure.in 2004-07-03 20:22:28
ea238f1c 194@@ -271,6 +271,21 @@ yes
ce06af28
WD
195 AC_SEARCH_LIBS(getaddrinfo, inet6)
196 fi
197
198+AC_ARG_ENABLE(openssl,
199+ AC_HELP_STRING([--enable-openssl], [compile SSL support with OpenSSL.]))
200+
201+if test "x$enable_openssl" != xno
202+then
203+ have_ssl=yes
204+ AC_CHECK_LIB(ssl, SSL_library_init, , [have_ssl=no])
205+ if test "x$have_ssl" = xyes
206+ then
207+ AC_DEFINE(HAVE_OPENSSL, 1, [true if you want to use SSL.])
208+ SSL_OBJS=ssl.o
209+ AC_SUBST(SSL_OBJS)
210+ fi
211+fi
212+
213 AC_MSG_CHECKING([whether to call shutdown on all sockets])
214 case $host_os in
215 *cygwin* ) AC_MSG_RESULT(yes)
2f5fa77e 216--- orig/main.c 2004-07-21 23:59:35
9be39c35 217+++ main.c 2004-07-15 02:40:51
2f5fa77e 218@@ -55,6 +55,9 @@ extern int write_batch;
9be39c35 219 extern int batch_fd;
2f5fa77e 220 extern int batch_gen_fd;
ce06af28
WD
221 extern int filesfrom_fd;
222+#ifdef HAVE_OPENSSL
223+extern int use_ssl;
224+#endif
225 extern pid_t cleanup_child_pid;
226 extern char *files_from;
227 extern char *remote_filesfrom_file;
2f5fa77e 228@@ -750,18 +753,32 @@ static int start_client(int argc, char *
ce06af28
WD
229 pid_t pid;
230 int f_in,f_out;
231 int rc;
232+ int url_prefix = strlen(URL_PREFIX);
233
234 /* Don't clobber argv[] so that ps(1) can still show the right
235 * command line. */
236 if ((rc = copy_argv(argv)))
237 return rc;
238
9be39c35 239+ if (strncasecmp(URL_PREFIX, argv[0], url_prefix) != 0 && !read_batch) {
ce06af28
WD
240+#ifdef HAVE_OPENSSL
241+ url_prefix = strlen(SSL_URL_PREFIX);
242+ if (strncasecmp(SSL_URL_PREFIX, argv[0], url_prefix) != 0)
243+ url_prefix = 0;
244+ else {
245+ if (!use_ssl)
246+ init_tls();
247+ use_ssl = 1;
248+ }
249+#else
250+ url_prefix = 0;
251+#endif
252+ }
253 /* rsync:// always uses rsync server over direct socket connection */
9be39c35
WD
254- if (strncasecmp(URL_PREFIX, argv[0], strlen(URL_PREFIX)) == 0
255- && !read_batch) {
ce06af28
WD
256+ if (url_prefix) {
257 char *host, *path;
258
259- host = argv[0] + strlen(URL_PREFIX);
260+ host = argv[0] + url_prefix;
261 p = strchr(host,'/');
262 if (p) {
263 *p = 0;
2f5fa77e 264@@ -809,12 +826,27 @@ static int start_client(int argc, char *
ce06af28 265 argv++;
7628f156 266 } else { /* source is local */
ce06af28
WD
267 am_sender = 1;
268+ url_prefix = strlen(URL_PREFIX);
269+ if (strncasecmp(URL_PREFIX, argv[0], url_prefix) != 0) {
270+#ifdef HAVE_OPENSSL
271+ url_prefix = strlen(SSL_URL_PREFIX);
272+ if (strncasecmp(SSL_URL_PREFIX, argv[0], url_prefix) != 0)
273+ url_prefix = 0;
274+ else {
275+ if (!use_ssl)
276+ init_tls();
277+ use_ssl = 1;
278+ }
279+#else
280+ url_prefix = 0;
281+#endif
282+ }
283
284 /* rsync:// destination uses rsync server over direct socket */
285- if (strncasecmp(URL_PREFIX, argv[argc-1], strlen(URL_PREFIX)) == 0) {
286+ if (url_prefix) {
287 char *host, *path;
288
289- host = argv[argc-1] + strlen(URL_PREFIX);
290+ host = argv[argc-1] + url_prefix;
291 p = strchr(host,'/');
292 if (p) {
293 *p = 0;
2f5fa77e 294--- orig/options.c 2004-07-21 23:59:35
f6c3b300
WD
295+++ options.c 2004-07-16 20:19:50
296@@ -134,6 +134,14 @@ int quiet = 0;
ce06af28
WD
297 int always_checksum = 0;
298 int list_only = 0;
299
300+#ifdef HAVE_OPENSSL
301+int use_ssl = 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;
306+#endif
307+
9be39c35
WD
308 #define MAX_BATCH_NAME_LEN 256 /* Must be less than MAXPATHLEN-13 */
309 char *batch_name = NULL;
310
f6c3b300 311@@ -153,6 +161,7 @@ static void print_rsync_version(enum log
ce06af28
WD
312 char const *hardlinks = "no ";
313 char const *links = "no ";
314 char const *ipv6 = "no ";
315+ char const *ssl = "no ";
316 STRUCT_STAT *dumstat;
317
318 #ifdef HAVE_SOCKETPAIR
f6c3b300 319@@ -175,6 +184,10 @@ static void print_rsync_version(enum log
ce06af28
WD
320 ipv6 = "";
321 #endif
322
323+#ifdef HAVE_OPENSSL
324+ ssl = "";
325+#endif
326+
327 rprintf(f, "%s version %s protocol version %d\n",
328 RSYNC_NAME, RSYNC_VERSION, PROTOCOL_VERSION);
329 rprintf(f,
f6c3b300 330@@ -188,10 +201,10 @@ static void print_rsync_version(enum log
ce06af28
WD
331 /* Note that this field may not have type ino_t. It depends
332 * on the complicated interaction between largefile feature
333 * macros. */
f6c3b300
WD
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",
336 have_inplace, ipv6,
ce06af28
WD
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",
342 get_panic_action());
f6c3b300 343@@ -305,6 +318,13 @@ void usage(enum logcode F)
d4e89c6a
WD
344 rprintf(F," -4, --ipv4 prefer IPv4\n");
345 rprintf(F," -6, --ipv6 prefer IPv6\n");
ce06af28
WD
346 #endif
347+#ifdef HAVE_OPENSSL
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");
353+#endif
ea238f1c 354 rprintf(F," -h, --help show this help screen\n");
ce06af28
WD
355
356 rprintf(F,"\n");
f6c3b300 357@@ -316,7 +336,7 @@ void usage(enum logcode F)
ce06af28
WD
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,
125d7fca
WD
361- OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_TIMEOUT,
362+ OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_TIMEOUT, OPT_USE_SSL,
ce06af28
WD
363 OPT_REFUSED_BASE = 9000};
364
365 static struct poptOption long_options[] = {
f6c3b300 366@@ -404,6 +424,13 @@ static struct poptOption long_options[]
ea238f1c
WD
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 },
ce06af28
WD
369 #endif
370+#ifdef HAVE_OPENSSL
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},
376+#endif
377 {0,0,0,0, 0, 0, 0}
378 };
379
f6c3b300 380@@ -601,6 +628,12 @@ int parse_arguments(int *argc, const cha
ce06af28
WD
381 return 0;
382 #endif
383
384+ case OPT_USE_SSL:
385+#ifdef HAVE_OPENSSL
386+ use_ssl = 1;
387+#endif
388+ break;
389+
390 default:
391 /* A large opt value means that set_refuse_options()
392 * turned this option off (opt-BASE is its index). */
2f5fa77e 393@@ -761,6 +794,17 @@ int parse_arguments(int *argc, const cha
ce06af28
WD
394 if (do_progress && !verbose)
395 verbose = 1;
78114162 396
ce06af28
WD
397+#ifdef HAVE_OPENSSL
398+ if (use_ssl) {
399+ if (init_tls()) {
400+ snprintf(err_buf, sizeof(err_buf),
401+ "Openssl error: %s\n",
402+ get_ssl_error());
403+ return 0;
404+ }
405+ }
406+#endif
78114162
WD
407+
408 if (bwlimit) {
409 bwlimit_writemax = (size_t)bwlimit * 128;
410 if (bwlimit_writemax < 512)
2f5fa77e 411--- orig/rsync.h 2004-07-20 21:36:08
13bed3dd 412+++ rsync.h 2004-07-03 20:22:28
ce06af28
WD
413@@ -32,6 +32,7 @@
414
415 #define DEFAULT_LOCK_FILE "/var/run/rsyncd.lock"
416 #define URL_PREFIX "rsync://"
417+#define SSL_URL_PREFIX "rsyncs://"
418
419 #define BACKUP_SUFFIX "~"
420
78114162 421@@ -326,6 +327,11 @@ enum msgcode {
ce06af28 422 #define uint64 unsigned off_t
78114162
WD
423 #endif
424
ce06af28
WD
425+#if HAVE_OPENSSL
426+#include <openssl/ssl.h>
427+#include <openssl/err.h>
78114162
WD
428+#endif
429+
ce06af28 430 /* Starting from protocol version 26, we always use 64-bit
78114162
WD
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
13bed3dd
WD
433--- orig/ssl.c 2004-07-02 21:44:19
434+++ ssl.c 2004-07-02 21:44:19
ce06af28
WD
435@@ -0,0 +1,366 @@
436+/* -*- c-file-style: "linux" -*-
437+ * ssl.c: operations for negotiating SSL rsync connections.
438+ *
439+ * Copyright (C) 2003 Casey Marshall <rsdio@metastatic.org>
440+ *
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.
445+ *
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.
450+ *
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.
454+ */
455+
456+#include "rsync.h"
457+
458+#ifdef HAVE_SYS_SELECT_H
459+#include <sys/select.h>
460+#else
461+#include <sys/time.h>
462+#include <sys/types.h>
463+#include <unistd.h>
464+#endif
465+#include <string.h>
466+
467+#define BUF_SIZE 1024
468+
469+extern int verbose;
470+extern int am_daemon;
471+extern int am_server;
472+
473+extern char *ssl_cert_path;
474+extern char *ssl_key_path;
475+extern char *ssl_key_passwd;
476+extern char *ssl_ca_path;
477+
478+static SSL_CTX *ssl_ctx;
479+static SSL *ssl;
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;
484+
485+/**
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.
489+ */
490+static int default_password_cb(char *buf, int n, UNUSED(int f), UNUSED(void *u))
491+{
492+ if (ssl_key_passwd == NULL || n < strlen(ssl_key_passwd))
493+ return 0;
494+ strncpy(buf, ssl_key_passwd, n-1);
495+ return strlen(ssl_key_passwd);
496+}
497+
498+/**
499+ * If verbose, this method traces the status of the SSL handshake.
500+ */
501+static void info_callback(SSL *ssl, int cb, int val)
502+{
503+ char buf[128];
504+ char *cbs;
505+
506+ switch (cb) {
507+ case SSL_CB_LOOP:
508+ cbs = "SSL_CB_LOOP";
509+ break;
510+ case SSL_CB_EXIT:
511+ cbs = "SSL_CB_EXIT";
512+ break;
513+ case SSL_CB_READ:
514+ cbs = "SSL_CB_READ";
515+ break;
516+ case SSL_CB_WRITE:
517+ cbs = "SSL_CB_WRITE";
518+ break;
519+ case SSL_CB_ALERT:
520+ cbs = "SSL_CB_ALERT";
521+ break;
522+ case SSL_CB_READ_ALERT:
523+ cbs = "SSL_CB_READ_ALERT";
524+ break;
525+ case SSL_CB_WRITE_ALERT:
526+ cbs = "SSL_CB_WRITE_ALERT";
527+ break;
528+ case SSL_CB_ACCEPT_LOOP:
529+ cbs = "SSL_CB_ACCEPT_LOOP";
530+ break;
531+ case SSL_CB_ACCEPT_EXIT:
532+ cbs = "SSL_CB_ACCEPT_EXIT";
533+ break;
534+ case SSL_CB_CONNECT_LOOP:
535+ cbs = "SSL_CB_CONNECT_LOOP";
536+ break;
537+ case SSL_CB_CONNECT_EXIT:
538+ cbs = "SSL_CB_CONNECT_EXIT";
539+ break;
540+ case SSL_CB_HANDSHAKE_START:
541+ cbs = "SSL_CB_HANDSHAKE_START";
542+ break;
543+ case SSL_CB_HANDSHAKE_DONE:
544+ cbs = "SSL_CB_HANDSHAKE_DONE";
545+ break;
546+ default:
547+ snprintf(buf, sizeof buf, "??? (%d)", cb);
548+ cbs = buf;
549+ break;
550+ }
551+ if (verbose > 2) {
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),
555+ buf, sizeof buf);
556+ rprintf(FLOG, "SSL: cipher: %s", buf);
557+ }
558+ }
559+}
560+
561+/**
562+ * Initializes the SSL context for TLSv1 connections; returns zero on
563+ * success.
564+ */
565+int init_tls(void)
566+{
567+ if (ssl_ctx)
568+ return 0;
569+ SSL_library_init();
570+ SSL_load_error_strings();
571+ ssl_ctx = SSL_CTX_new(TLSv1_method());
572+ if (!ssl_ctx)
573+ return 1;
574+ SSL_CTX_set_info_callback(ssl_ctx, info_callback);
575+
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)
580+ return 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)
589+ return 1;
590+ if (SSL_CTX_check_private_key(ssl_ctx) != 1)
591+ return 1;
592+ }
593+ if (ssl_ca_path != NULL
594+ && !SSL_CTX_load_verify_locations(ssl_ctx, ssl_ca_path, NULL))
595+ return 1;
596+
597+ return 0;
598+}
599+
600+/**
601+ * Returns the error string for the current SSL error, if any.
602+ */
603+char *get_ssl_error(void)
604+{
605+ return ERR_error_string(ERR_get_error(), NULL);
606+}
607+
608+/**
609+ * Returns the input file descriptor for the SSL connection.
610+ */
611+int get_tls_rfd(void)
612+{
613+ return tls_read[0];
614+}
615+
616+/**
617+ * Returns the output file descriptor for the SSL connection.
618+ */
619+int get_tls_wfd(void)
620+{
621+ return tls_write[1];
622+}
623+
624+/**
625+ * Signal handler that ends the SSL connection.
626+ */
627+static RETSIGTYPE tls_sigusr1(int UNUSED(val))
628+{
629+ if (ssl) {
630+ SSL_shutdown(ssl);
631+ SSL_free(ssl);
632+ ssl = NULL;
633+ }
634+ ssl_running = 0;
635+}
636+
637+/**
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.
641+ *
642+ * 0 is returned on success.
643+ */
644+int start_tls(int f_in, int f_out)
645+{
646+ int tls_fd;
647+ int n = 0, r;
648+ unsigned char buf1[BUF_SIZE], buf2[BUF_SIZE];
649+ int avail1 = 0, avail2 = 0, write1 = 0, write2 = 0;
650+ fd_set rd, wd;
651+
652+ if (fd_pair(tls_read))
653+ return 1;
654+ if (fd_pair(tls_write))
655+ return 1;
656+
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);
663+
664+ ssl_pid = do_fork();
665+ if (ssl_pid < 0)
666+ return -1;
667+ if (ssl_pid != 0) {
668+ close(tls_write[0]);
669+ close(tls_read[1]);
670+ return 0;
671+ }
672+
673+ signal(SIGUSR1, tls_sigusr1);
674+ ssl = SSL_new(ssl_ctx);
675+ if (!ssl)
676+ goto closed;
677+ if (am_daemon || am_server)
678+ SSL_set_accept_state(ssl);
679+ else
680+ SSL_set_connect_state(ssl);
681+ SSL_set_rfd(ssl, f_in);
682+ SSL_set_wfd(ssl, f_out);
683+
684+ tls_fd = SSL_get_fd(ssl);
685+ n = tls_write[0];
686+ n = MAX(tls_read[1], n);
687+ n = MAX(tls_fd, n) + 1;
688+
689+ ssl_running = 1;
690+ while (ssl_running) {
691+ FD_ZERO(&rd);
692+ FD_ZERO(&wd);
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);
697+
698+ r = select(n, &rd, &wd, NULL, NULL);
699+
700+ if (r == -1 && errno == EINTR)
701+ continue;
702+ if (FD_ISSET(tls_write[0], &rd)) {
703+ r = read(tls_write[0], buf1+avail1, BUF_SIZE-avail1);
704+ if (r >= 0)
705+ avail1 += r;
706+ else {
707+ rprintf(FERROR, "pipe read error: %s\n",
708+ strerror(errno));
709+ break;
710+ }
711+ }
712+ if (FD_ISSET(tls_fd, &rd)) {
713+ r = SSL_read(ssl, buf2+avail2, BUF_SIZE-avail2);
714+ if (r > 0)
715+ avail2 += r;
716+ else {
717+ switch (SSL_get_error(ssl, r)) {
718+ case SSL_ERROR_ZERO_RETURN:
719+ goto closed;
720+ case SSL_ERROR_WANT_READ:
721+ case SSL_ERROR_WANT_WRITE:
722+ break;
723+ case SSL_ERROR_SYSCALL:
724+ if (r == 0)
725+ rprintf(FERROR, "SSL spurious EOF\n");
726+ else
727+ rprintf(FERROR, "SSL I/O error: %s\n",
728+ strerror(errno));
729+ goto closed;
730+ case SSL_ERROR_SSL:
731+ rprintf(FERROR, "SSL: %s\n",
732+ ERR_error_string(ERR_get_error(), NULL));
733+ goto closed;
734+ default:
735+ rprintf(FERROR, "unexpected ssl error %d\n", r);
736+ goto closed;
737+ }
738+ }
739+ }
740+ if (FD_ISSET(tls_read[1], &wd) && write2 < avail2) {
741+ r = write(tls_read[1], buf2+write2, avail2-write2);
742+ if (r >= 0)
743+ write2 += r;
744+ else {
745+ rprintf(FERROR, "pipe write error: %s\n",
746+ strerror(errno));
747+ break;
748+ }
749+ }
750+ if (FD_ISSET(tls_fd, &wd) && write1 < avail1) {
751+ r = SSL_write(ssl, buf1+write1, avail1-write1);
752+ if (r > 0)
753+ write1 += r;
754+ else {
755+ switch (SSL_get_error(ssl, r)) {
756+ case SSL_ERROR_ZERO_RETURN:
757+ goto closed;
758+ case SSL_ERROR_WANT_READ:
759+ case SSL_ERROR_WANT_WRITE:
760+ break;
761+ case SSL_ERROR_SYSCALL:
762+ if (r == 0)
763+ rprintf(FERROR, "SSL: spurious EOF\n");
764+ else
765+ rprintf(FERROR, "SSL: I/O error: %s\n",
766+ strerror(errno));
767+ goto closed;
768+ case SSL_ERROR_SSL:
769+ rprintf(FERROR, "SSL: %s\n",
770+ ERR_error_string(ERR_get_error(), NULL));
771+ goto closed;
772+ default:
773+ rprintf(FERROR, "unexpected ssl error %d\n", r);
774+ goto closed;
775+ }
776+ }
777+ }
778+ if (avail1 == write1)
779+ avail1 = write1 = 0;
780+ if (avail2 == write2)
781+ avail2 = write2 = 0;
782+ }
783+
784+ /* XXX I'm pretty sure that there is a lot that I am not considering
785+ here. Bugs? Yes, probably. */
786+
787+ /* We're finished. */
788+ closed:
789+ close(tls_read[1]);
790+ close(tls_write[0]);
791+ exit(0);
792+}
793+
794+/**
795+ * Ends the TLS connection.
796+ */
797+void end_tls(void)
798+{
799+ if (ssl_pid > 0)
800+ kill(ssl_pid, SIGUSR1);
801+}