Fixed failing hunks.
[rsync/rsync-patches.git] / openssl-support.diff
CommitLineData
8a529471 1After applying this patch, run these commands for a successful build:
ce06af28 2
27e96866 3 ./prepare-source
8a529471 4 ./configure
8a529471
WD
5 make
6
7Casey Marshall writes:
ce06af28
WD
8
9I've been hacking together a way to use rsync with OpenSSL, and have
10attached my current patch against a recent CVS tree. The details of
11this implementation are:
12
13 1. The SSL code is added as a "layer" that is forked into its own
14 process.
15
16 2. An SSL connection is established by the client issuing the
17 command:
18
19 #starttls
20
37d262c5 21 And, if the daemon allows SSL, it replies with
ce06af28
WD
22
23 @RSYNCD: starttls
24
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.
28
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.
31
32 4. There are a number of details not implemented.
33
34All warnings apply; I don't do C programming all that often, so I
35can't say if I've left any cleanup/compatibility errors in the code.
36
ce06af28 37
9a7eef96
WD
38--- old/Makefile.in
39+++ new/Makefile.in
8330f4aa 40@@ -39,7 +39,7 @@ OBJS3=progress.o pipe.o
ce06af28
WD
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@
46
58a9031f 47 TLS_OBJ = tls.o syscall.o lib/compat.o lib/snprintf.o lib/permstring.o
ce06af28 48
9a7eef96
WD
49--- old/cleanup.c
50+++ new/cleanup.c
1898c899
WD
51@@ -26,6 +26,9 @@
52 extern int am_server;
53 extern int am_daemon;
8a529471 54 extern int io_error;
a7219d20 55+#if HAVE_OPENSSL
8a529471 56+extern int use_ssl;
ce06af28 57+#endif
9c015a83
WD
58 extern int keep_partial;
59 extern int log_got_error;
60 extern char *partial_dir;
1898c899 61@@ -106,6 +109,11 @@ void _exit_cleanup(int code, const char
bc5988ec
WD
62 SIGACTION(SIGUSR1, SIG_IGN);
63 SIGACTION(SIGUSR2, SIG_IGN);
78114162 64
a7219d20 65+#if HAVE_OPENSSL
ce06af28
WD
66+ if (use_ssl)
67+ end_tls();
68+#endif
78114162 69+
8a529471
WD
70 if (verbose > 3) {
71 rprintf(FINFO,"_exit_cleanup(code=%d, file=%s, line=%d): entered\n",
0ca6aebe 72 code, file, line);
9a7eef96
WD
73--- old/clientserver.c
74+++ new/clientserver.c
1898c899 75@@ -29,6 +29,9 @@ extern int am_sender;
d9a67109
WD
76 extern int am_server;
77 extern int am_daemon;
78 extern int am_root;
a7219d20 79+#if HAVE_OPENSSL
ce06af28
WD
80+extern int use_ssl;
81+#endif
d9a67109
WD
82 extern int rsync_port;
83 extern int kluge_around_eof;
84 extern int daemon_over_rsh;
1898c899 85@@ -105,8 +108,18 @@ int start_socket_client(char *host, char
4a65fe72 86 set_socket_options(fd, sockopts);
ce06af28
WD
87
88 ret = start_inband_exchange(user, path, fd, fd, argc);
9d3fe73a 89+ if (ret)
ce06af28
WD
90+ return ret;
91+
a7219d20 92+#if HAVE_OPENSSL
ce06af28
WD
93+ if (use_ssl) {
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);
97+ }
98+#endif
99
9d3fe73a 100- return ret ? ret : client_run(fd, fd, -1, argc, argv);
ce06af28
WD
101+ return client_run(fd, fd, -1, argc, argv);
102 }
103
1898c899
WD
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
be73a66e
WD
106 if (verbose > 1)
107 print_child_argv(sargs);
ce06af28 108
a7219d20 109+#if HAVE_OPENSSL
ce06af28
WD
110+ if (use_ssl) {
111+ io_printf(f_out, "#starttls\n");
112+ while (1) {
113+ if (!read_line(f_in, line, sizeof(line)-1)) {
114+ rprintf(FERROR, "rsync: did not receive reply to #starttls\n");
115+ return -1;
116+ }
117+ if (strncmp(line, "@ERROR", 6) == 0) {
2fd4a7f7 118+ rprintf(FERROR, "%s\n", line);
ce06af28
WD
119+ return -1;
120+ }
121+ if (strcmp(line, "@RSYNCD: starttls") == 0) {
122+ break;
123+ }
124+ rprintf(FINFO, "%s\n", line);
125+ }
126+ if (start_tls(f_in, f_out)) {
127+ rprintf(FERROR, "rsync: error during SSL handshake: %s\n",
128+ get_ssl_error());
129+ return -1;
130+ }
131+ f_in = get_tls_rfd();
132+ f_out = get_tls_wfd();
133+ }
134+#endif
135+
136 p = strchr(path,'/');
137 if (p) *p = 0;
138 io_printf(f_out, "%s\n", path);
1898c899 139@@ -195,6 +235,10 @@ int start_inband_exchange(char *user, ch
ce06af28
WD
140 * server to terminate the listing of modules.
141 * We don't want to go on and transfer
142 * anything; just exit. */
a7219d20 143+#if HAVE_OPENSSL
ce06af28
WD
144+ if (use_ssl)
145+ end_tls();
146+#endif
147 exit(0);
148 }
149
1898c899 150@@ -202,6 +246,10 @@ int start_inband_exchange(char *user, ch
7628f156 151 rprintf(FERROR, "%s\n", line);
ce06af28
WD
152 /* This is always fatal; the server will now
153 * close the socket. */
a7219d20 154+#if HAVE_OPENSSL
ce06af28
WD
155+ if (use_ssl)
156+ end_tls();
157+#endif
9d3fe73a
WD
158 return -1;
159 }
160
8330f4aa 161@@ -718,6 +766,7 @@ static void send_listing(int fd)
ce06af28
WD
162 io_printf(fd,"@RSYNCD: EXIT\n");
163 }
164
165+
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
168 here */
8330f4aa 169@@ -776,6 +825,9 @@ int start_daemon(int f_in, int f_out)
2fd4a7f7
WD
170 if (protocol_version > remote_protocol)
171 protocol_version = remote_protocol;
172
173+#if HAVE_OPENSSL
174+retry:
175+#endif
176 line[0] = 0;
177 if (!read_line(f_in, line, sizeof line - 1))
178 return -1;
8330f4aa 179@@ -787,6 +839,20 @@ int start_daemon(int f_in, int f_out)
5e929029
WD
180 return -1;
181 }
78114162 182
ce06af28 183+#if HAVE_OPENSSL
5e929029
WD
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",
188+ get_ssl_error());
189+ return -1;
ce06af28 190+ }
5e929029
WD
191+ f_in = get_tls_rfd();
192+ f_out = get_tls_wfd();
2fd4a7f7 193+ goto retry;
5e929029 194+ }
ce06af28 195+#endif
78114162 196+
5e929029
WD
197 if (*line == '#') {
198 /* it's some sort of command that I don't understand */
199 io_printf(f_out, "@ERROR: Unknown command '%s'\n", line);
9a7eef96
WD
200--- old/configure.in
201+++ new/configure.in
8330f4aa 202@@ -290,6 +290,21 @@ if test x"$enable_locale" != x"no"; then
0ca6aebe 203 AC_DEFINE(CONFIG_LOCALE)
ce06af28
WD
204 fi
205
206+AC_ARG_ENABLE(openssl,
207+ AC_HELP_STRING([--enable-openssl], [compile SSL support with OpenSSL.]))
208+
209+if test "x$enable_openssl" != xno
210+then
211+ have_ssl=yes
212+ AC_CHECK_LIB(ssl, SSL_library_init, , [have_ssl=no])
213+ if test "x$have_ssl" = xyes
214+ then
215+ AC_DEFINE(HAVE_OPENSSL, 1, [true if you want to use SSL.])
216+ SSL_OBJS=ssl.o
217+ AC_SUBST(SSL_OBJS)
218+ fi
219+fi
220+
221 AC_MSG_CHECKING([whether to call shutdown on all sockets])
222 case $host_os in
223 *cygwin* ) AC_MSG_RESULT(yes)
9a7eef96
WD
224--- old/options.c
225+++ new/options.c
1898c899 226@@ -172,6 +172,14 @@ int logfile_format_has_o_or_i = 0;
ce06af28
WD
227 int always_checksum = 0;
228 int list_only = 0;
229
a7219d20 230+#if HAVE_OPENSSL
ce06af28
WD
231+int use_ssl = 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;
236+#endif
237+
9be39c35
WD
238 #define MAX_BATCH_NAME_LEN 256 /* Must be less than MAXPATHLEN-13 */
239 char *batch_name = NULL;
240
1898c899 241@@ -200,6 +208,7 @@ static void print_rsync_version(enum log
ce06af28
WD
242 char const *hardlinks = "no ";
243 char const *links = "no ";
244 char const *ipv6 = "no ";
245+ char const *ssl = "no ";
246 STRUCT_STAT *dumstat;
247
3d1facaa 248 #ifdef HAVE_SOCKETPAIR
1898c899 249@@ -222,6 +231,10 @@ static void print_rsync_version(enum log
ce06af28
WD
250 ipv6 = "";
251 #endif
252
a7219d20 253+#if HAVE_OPENSSL
ce06af28
WD
254+ ssl = "";
255+#endif
256+
257 rprintf(f, "%s version %s protocol version %d\n",
258 RSYNC_NAME, RSYNC_VERSION, PROTOCOL_VERSION);
4a65fe72 259 rprintf(f, "Copyright (C) 1996-2006 by Andrew Tridgell, Wayne Davison, and others.\n");
1898c899 260@@ -234,9 +247,9 @@ static void print_rsync_version(enum log
ce06af28
WD
261 /* Note that this field may not have type ino_t. It depends
262 * on the complicated interaction between largefile feature
263 * macros. */
bc5988ec
WD
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,
ce06af28 269 (int) (sizeof dumstat->st_ino * 8),
bc5988ec 270 (int) (sizeof (int64) * 8));
ce06af28 271 #ifdef MAINTAINER_MODE
8330f4aa 272@@ -383,6 +396,13 @@ void usage(enum logcode F)
d4e89c6a
WD
273 rprintf(F," -4, --ipv4 prefer IPv4\n");
274 rprintf(F," -6, --ipv6 prefer IPv6\n");
ce06af28 275 #endif
a7219d20 276+#if HAVE_OPENSSL
ce06af28 277+ rprintf(F," --ssl allow socket connections to use SSL\n");
37d262c5
WD
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");
ce06af28
WD
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");
282+#endif
896871f8 283 rprintf(F," --version print version number\n");
d9a67109 284 rprintf(F,"(-h) --help show this help (-h works with no other options)\n");
ce06af28 285
8330f4aa 286@@ -396,7 +416,7 @@ enum {OPT_VERSION = 1000, OPT_DAEMON, OP
6849cd84 287 OPT_FILTER, OPT_COMPARE_DEST, OPT_COPY_DEST, OPT_LINK_DEST, OPT_HELP,
0ca6aebe 288 OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_MODIFY_WINDOW, OPT_MIN_SIZE, OPT_CHMOD,
5398d042 289 OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_ONLY_WRITE_BATCH, OPT_MAX_SIZE,
4a65fe72
WD
290- OPT_NO_D,
291+ OPT_NO_D, OPT_USE_SSL,
0ca6aebe 292 OPT_SERVER, OPT_REFUSED_BASE = 9000};
ce06af28 293
70c5d149 294 static struct poptOption long_options[] = {
8330f4aa 295@@ -541,6 +561,13 @@ static struct poptOption long_options[]
489b0a72 296 {"checksum-seed", 0, POPT_ARG_INT, &checksum_seed, 0, 0, 0 },
0ca6aebe 297 {"server", 0, POPT_ARG_NONE, 0, OPT_SERVER, 0, 0 },
489b0a72 298 {"sender", 0, POPT_ARG_NONE, 0, OPT_SENDER, 0, 0 },
a7219d20 299+#if HAVE_OPENSSL
5388f859
WD
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},
ce06af28 303+ {"ssl-key-passwd", 0, POPT_ARG_STRING, &ssl_key_passwd, OPT_USE_SSL, 0, 0},
5388f859 304+ {"ssl-ca-certs", 0, POPT_ARG_STRING, &ssl_ca_path, OPT_USE_SSL, 0, 0},
ce06af28 305+#endif
489b0a72 306 /* All the following options switch us into daemon-mode option-parsing. */
5388f859 307 {"config", 0, POPT_ARG_STRING, 0, OPT_DAEMON, 0, 0 },
2b06a19d 308 {"daemon", 0, POPT_ARG_NONE, 0, OPT_DAEMON, 0, 0 },
8330f4aa 309@@ -1084,6 +1111,12 @@ int parse_arguments(int *argc, const cha
0ca6aebe
WD
310 usage(FINFO);
311 exit_cleanup(0);
ce06af28
WD
312
313+ case OPT_USE_SSL:
a7219d20 314+#if HAVE_OPENSSL
ce06af28
WD
315+ use_ssl = 1;
316+#endif
317+ break;
318+
319 default:
320 /* A large opt value means that set_refuse_options()
27a7053c 321 * turned this option off. */
8330f4aa 322@@ -1364,6 +1397,17 @@ int parse_arguments(int *argc, const cha
a7219d20 323 if (delay_updates && !partial_dir)
4a65fe72 324 partial_dir = tmp_partialdir;
78114162 325
a7219d20 326+#if HAVE_OPENSSL
ce06af28
WD
327+ if (use_ssl) {
328+ if (init_tls()) {
329+ snprintf(err_buf, sizeof(err_buf),
330+ "Openssl error: %s\n",
331+ get_ssl_error());
332+ return 0;
333+ }
334+ }
335+#endif
78114162 336+
4c1f2ca5 337 if (inplace) {
3d1facaa 338 #ifdef HAVE_FTRUNCATE
4c1f2ca5 339 if (partial_dir) {
8330f4aa 340@@ -1781,10 +1825,27 @@ char *check_for_hostspec(char *s, char *
def2ace9
WD
341 char *p;
342 int not_host;
8330f4aa 343 int hostlen;
def2ace9
WD
344+ int url_prefix_len = sizeof URL_PREFIX - 1;
345
346- if (port_ptr && strncasecmp(URL_PREFIX, s, strlen(URL_PREFIX)) == 0) {
347+ if (!port_ptr)
348+ url_prefix_len = 0;
349+ else if (strncasecmp(URL_PREFIX, s, url_prefix_len) != 0) {
350+#if HAVE_OPENSSL
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;
354+ else {
355+ if (!use_ssl)
356+ init_tls();
357+ use_ssl = 1;
358+ }
359+#else
360+ url_prefix_len = 0;
361+#endif
362+ }
363+ if (url_prefix_len) {
364 char *path;
def2ace9
WD
365- s += strlen(URL_PREFIX);
366+ s += url_prefix_len;
367 if ((p = strchr(s, '/')) != NULL) {
368 hostlen = p - s;
369 path = p + 1;
9a7eef96
WD
370--- old/rsync.h
371+++ new/rsync.h
ce06af28
WD
372@@ -32,6 +32,7 @@
373
374 #define DEFAULT_LOCK_FILE "/var/run/rsyncd.lock"
375 #define URL_PREFIX "rsync://"
376+#define SSL_URL_PREFIX "rsyncs://"
377
378 #define BACKUP_SUFFIX "~"
379
8330f4aa 380@@ -419,6 +420,11 @@ enum msgcode {
a7219d20 381 # define SIZEOF_INT64 SIZEOF_OFF_T
78114162
WD
382 #endif
383
ce06af28
WD
384+#if HAVE_OPENSSL
385+#include <openssl/ssl.h>
386+#include <openssl/err.h>
78114162
WD
387+#endif
388+
ce06af28 389 /* Starting from protocol version 26, we always use 64-bit
78114162
WD
390 * ino_t and dev_t internally, even if this platform does not
391 * allow files to have 64-bit inums. That's because the
9a7eef96
WD
392--- old/ssl.c
393+++ new/ssl.c
ce06af28
WD
394@@ -0,0 +1,366 @@
395+/* -*- c-file-style: "linux" -*-
396+ * ssl.c: operations for negotiating SSL rsync connections.
397+ *
398+ * Copyright (C) 2003 Casey Marshall <rsdio@metastatic.org>
399+ *
400+ * This program is free software; you can redistribute it and/or modify
401+ * it under the terms of the GNU General Public License as published by
402+ * the Free Software Foundation; either version 2 of the License, or
403+ * (at your option) any later version.
404+ *
405+ * This program is distributed in the hope that it will be useful,
406+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
407+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
408+ * GNU General Public License for more details.
409+ *
410+ * You should have received a copy of the GNU General Public License
411+ * along with this program; if not, write to the Free Software
412+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
413+ */
414+
415+#include "rsync.h"
416+
a7219d20 417+#if HAVE_SYS_SELECT_H
ce06af28
WD
418+#include <sys/select.h>
419+#else
420+#include <sys/time.h>
421+#include <sys/types.h>
422+#include <unistd.h>
423+#endif
424+#include <string.h>
425+
426+#define BUF_SIZE 1024
427+
428+extern int verbose;
429+extern int am_daemon;
430+extern int am_server;
431+
432+extern char *ssl_cert_path;
433+extern char *ssl_key_path;
434+extern char *ssl_key_passwd;
435+extern char *ssl_ca_path;
436+
437+static SSL_CTX *ssl_ctx;
438+static SSL *ssl;
439+static int tls_read[2] = { -1, -1 };
440+static int tls_write[2] = { -1, -1 };
441+static int ssl_running;
442+static int ssl_pid = -1;
443+
444+/**
445+ * A non-interactive callback to be passed to SSL_CTX_set_default_password_cb,
446+ * which merely copies the value of ssl_key_passwd into buf. This is
447+ * used for when the private key password is supplied via an option.
448+ */
449+static int default_password_cb(char *buf, int n, UNUSED(int f), UNUSED(void *u))
450+{
2fd4a7f7 451+ if (ssl_key_passwd == NULL || n < (int)strlen(ssl_key_passwd))
ce06af28
WD
452+ return 0;
453+ strncpy(buf, ssl_key_passwd, n-1);
454+ return strlen(ssl_key_passwd);
455+}
456+
457+/**
458+ * If verbose, this method traces the status of the SSL handshake.
459+ */
2fd4a7f7 460+static void info_callback(const SSL *ssl, int cb, int val)
ce06af28
WD
461+{
462+ char buf[128];
463+ char *cbs;
464+
465+ switch (cb) {
466+ case SSL_CB_LOOP:
467+ cbs = "SSL_CB_LOOP";
468+ break;
469+ case SSL_CB_EXIT:
470+ cbs = "SSL_CB_EXIT";
471+ break;
472+ case SSL_CB_READ:
473+ cbs = "SSL_CB_READ";
474+ break;
475+ case SSL_CB_WRITE:
476+ cbs = "SSL_CB_WRITE";
477+ break;
478+ case SSL_CB_ALERT:
479+ cbs = "SSL_CB_ALERT";
480+ break;
481+ case SSL_CB_READ_ALERT:
482+ cbs = "SSL_CB_READ_ALERT";
483+ break;
484+ case SSL_CB_WRITE_ALERT:
485+ cbs = "SSL_CB_WRITE_ALERT";
486+ break;
487+ case SSL_CB_ACCEPT_LOOP:
488+ cbs = "SSL_CB_ACCEPT_LOOP";
489+ break;
490+ case SSL_CB_ACCEPT_EXIT:
491+ cbs = "SSL_CB_ACCEPT_EXIT";
492+ break;
493+ case SSL_CB_CONNECT_LOOP:
494+ cbs = "SSL_CB_CONNECT_LOOP";
495+ break;
496+ case SSL_CB_CONNECT_EXIT:
497+ cbs = "SSL_CB_CONNECT_EXIT";
498+ break;
499+ case SSL_CB_HANDSHAKE_START:
500+ cbs = "SSL_CB_HANDSHAKE_START";
501+ break;
502+ case SSL_CB_HANDSHAKE_DONE:
503+ cbs = "SSL_CB_HANDSHAKE_DONE";
504+ break;
505+ default:
506+ snprintf(buf, sizeof buf, "??? (%d)", cb);
507+ cbs = buf;
508+ break;
509+ }
510+ if (verbose > 2) {
511+ rprintf(FLOG, "SSL: info_callback(%p,%s,%d)\n", ssl, cbs, val);
512+ if (cb == SSL_CB_HANDSHAKE_DONE) {
2fd4a7f7 513+ SSL_CIPHER_description(SSL_get_current_cipher((SSL*)ssl),
ce06af28
WD
514+ buf, sizeof buf);
515+ rprintf(FLOG, "SSL: cipher: %s", buf);
516+ }
517+ }
518+}
519+
520+/**
521+ * Initializes the SSL context for TLSv1 connections; returns zero on
522+ * success.
523+ */
524+int init_tls(void)
525+{
526+ if (ssl_ctx)
527+ return 0;
528+ SSL_library_init();
529+ SSL_load_error_strings();
530+ ssl_ctx = SSL_CTX_new(TLSv1_method());
531+ if (!ssl_ctx)
532+ return 1;
533+ SSL_CTX_set_info_callback(ssl_ctx, info_callback);
534+
535+ /* Sets the certificate sent to the other party. */
536+ if (ssl_cert_path != NULL
537+ && SSL_CTX_use_certificate_file(ssl_ctx, ssl_cert_path,
538+ SSL_FILETYPE_PEM) != 1)
539+ return 1;
540+ /* Set up the simple non-interactive callback if the password
541+ * was supplied on the command line. */
542+ if (ssl_key_passwd != NULL)
543+ SSL_CTX_set_default_passwd_cb(ssl_ctx, default_password_cb);
544+ /* Sets the private key that matches the public certificate. */
545+ if (ssl_key_path != NULL) {
546+ if (SSL_CTX_use_PrivateKey_file(ssl_ctx, ssl_key_path,
547+ SSL_FILETYPE_PEM) != 1)
548+ return 1;
549+ if (SSL_CTX_check_private_key(ssl_ctx) != 1)
550+ return 1;
551+ }
552+ if (ssl_ca_path != NULL
553+ && !SSL_CTX_load_verify_locations(ssl_ctx, ssl_ca_path, NULL))
554+ return 1;
555+
556+ return 0;
557+}
558+
559+/**
560+ * Returns the error string for the current SSL error, if any.
561+ */
562+char *get_ssl_error(void)
563+{
564+ return ERR_error_string(ERR_get_error(), NULL);
565+}
566+
567+/**
568+ * Returns the input file descriptor for the SSL connection.
569+ */
570+int get_tls_rfd(void)
571+{
572+ return tls_read[0];
573+}
574+
575+/**
576+ * Returns the output file descriptor for the SSL connection.
577+ */
578+int get_tls_wfd(void)
579+{
580+ return tls_write[1];
581+}
582+
583+/**
584+ * Signal handler that ends the SSL connection.
585+ */
586+static RETSIGTYPE tls_sigusr1(int UNUSED(val))
587+{
588+ if (ssl) {
589+ SSL_shutdown(ssl);
590+ SSL_free(ssl);
591+ ssl = NULL;
592+ }
593+ ssl_running = 0;
594+}
595+
596+/**
597+ * Negotiates the TLS connection, creates a socket pair for communicating
598+ * with the rsync process, then forks into a new process that will handle
599+ * the communication.
600+ *
601+ * 0 is returned on success.
602+ */
603+int start_tls(int f_in, int f_out)
604+{
605+ int tls_fd;
606+ int n = 0, r;
607+ unsigned char buf1[BUF_SIZE], buf2[BUF_SIZE];
608+ int avail1 = 0, avail2 = 0, write1 = 0, write2 = 0;
609+ fd_set rd, wd;
610+
611+ if (fd_pair(tls_read))
612+ return 1;
613+ if (fd_pair(tls_write))
614+ return 1;
615+
616+ set_blocking(tls_read[0]);
617+ set_blocking(tls_read[1]);
618+ set_blocking(tls_write[0]);
619+ set_blocking(tls_write[1]);
620+ set_blocking(f_in);
621+ set_blocking(f_out);
622+
623+ ssl_pid = do_fork();
624+ if (ssl_pid < 0)
625+ return -1;
626+ if (ssl_pid != 0) {
627+ close(tls_write[0]);
628+ close(tls_read[1]);
629+ return 0;
630+ }
631+
632+ signal(SIGUSR1, tls_sigusr1);
633+ ssl = SSL_new(ssl_ctx);
634+ if (!ssl)
635+ goto closed;
636+ if (am_daemon || am_server)
637+ SSL_set_accept_state(ssl);
638+ else
639+ SSL_set_connect_state(ssl);
640+ SSL_set_rfd(ssl, f_in);
641+ SSL_set_wfd(ssl, f_out);
642+
643+ tls_fd = SSL_get_fd(ssl);
644+ n = tls_write[0];
645+ n = MAX(tls_read[1], n);
646+ n = MAX(tls_fd, n) + 1;
647+
648+ ssl_running = 1;
649+ while (ssl_running) {
650+ FD_ZERO(&rd);
651+ FD_ZERO(&wd);
652+ FD_SET(tls_write[0], &rd);
653+ FD_SET(tls_read[1], &wd);
654+ FD_SET(tls_fd, &rd);
655+ FD_SET(tls_fd, &wd);
656+
657+ r = select(n, &rd, &wd, NULL, NULL);
658+
659+ if (r == -1 && errno == EINTR)
660+ continue;
661+ if (FD_ISSET(tls_write[0], &rd)) {
662+ r = read(tls_write[0], buf1+avail1, BUF_SIZE-avail1);
663+ if (r >= 0)
664+ avail1 += r;
665+ else {
666+ rprintf(FERROR, "pipe read error: %s\n",
667+ strerror(errno));
668+ break;
669+ }
670+ }
671+ if (FD_ISSET(tls_fd, &rd)) {
672+ r = SSL_read(ssl, buf2+avail2, BUF_SIZE-avail2);
673+ if (r > 0)
674+ avail2 += r;
675+ else {
676+ switch (SSL_get_error(ssl, r)) {
677+ case SSL_ERROR_ZERO_RETURN:
678+ goto closed;
679+ case SSL_ERROR_WANT_READ:
680+ case SSL_ERROR_WANT_WRITE:
681+ break;
682+ case SSL_ERROR_SYSCALL:
683+ if (r == 0)
684+ rprintf(FERROR, "SSL spurious EOF\n");
685+ else
686+ rprintf(FERROR, "SSL I/O error: %s\n",
687+ strerror(errno));
688+ goto closed;
689+ case SSL_ERROR_SSL:
690+ rprintf(FERROR, "SSL: %s\n",
691+ ERR_error_string(ERR_get_error(), NULL));
692+ goto closed;
693+ default:
694+ rprintf(FERROR, "unexpected ssl error %d\n", r);
695+ goto closed;
696+ }
697+ }
698+ }
699+ if (FD_ISSET(tls_read[1], &wd) && write2 < avail2) {
700+ r = write(tls_read[1], buf2+write2, avail2-write2);
701+ if (r >= 0)
702+ write2 += r;
703+ else {
704+ rprintf(FERROR, "pipe write error: %s\n",
705+ strerror(errno));
706+ break;
707+ }
708+ }
709+ if (FD_ISSET(tls_fd, &wd) && write1 < avail1) {
710+ r = SSL_write(ssl, buf1+write1, avail1-write1);
711+ if (r > 0)
712+ write1 += r;
713+ else {
714+ switch (SSL_get_error(ssl, r)) {
715+ case SSL_ERROR_ZERO_RETURN:
716+ goto closed;
717+ case SSL_ERROR_WANT_READ:
718+ case SSL_ERROR_WANT_WRITE:
719+ break;
720+ case SSL_ERROR_SYSCALL:
721+ if (r == 0)
722+ rprintf(FERROR, "SSL: spurious EOF\n");
723+ else
724+ rprintf(FERROR, "SSL: I/O error: %s\n",
725+ strerror(errno));
726+ goto closed;
727+ case SSL_ERROR_SSL:
728+ rprintf(FERROR, "SSL: %s\n",
729+ ERR_error_string(ERR_get_error(), NULL));
730+ goto closed;
731+ default:
732+ rprintf(FERROR, "unexpected ssl error %d\n", r);
733+ goto closed;
734+ }
735+ }
736+ }
737+ if (avail1 == write1)
738+ avail1 = write1 = 0;
739+ if (avail2 == write2)
740+ avail2 = write2 = 0;
741+ }
742+
743+ /* XXX I'm pretty sure that there is a lot that I am not considering
744+ here. Bugs? Yes, probably. */
745+
746+ /* We're finished. */
747+ closed:
748+ close(tls_read[1]);
749+ close(tls_write[0]);
750+ exit(0);
751+}
752+
753+/**
754+ * Ends the TLS connection.
755+ */
756+void end_tls(void)
757+{
758+ if (ssl_pid > 0)
759+ kill(ssl_pid, SIGUSR1);
760+}