Updated to apply to latest source.
[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
13bed3dd
WD
51--- orig/cleanup.c 2004-07-02 18:11:26
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
13bed3dd 63@@ -98,6 +101,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);
9be39c35 75--- orig/clientserver.c 2004-07-15 02:21:10
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 */
9be39c35 171@@ -545,6 +594,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);
13bed3dd
WD
192--- orig/configure.in 2004-04-30 18:04:07
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)
9be39c35
WD
216--- orig/main.c 2004-07-15 17:02:03
217+++ main.c 2004-07-15 02:40:51
218@@ -54,6 +54,9 @@ extern int read_batch;
ce06af28 219 extern int write_batch;
9be39c35 220 extern int batch_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;
9be39c35 228@@ -748,18 +751,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;
9be39c35 264@@ -807,12 +824,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;
9be39c35
WD
294--- orig/options.c 2004-07-15 16:51:50
295+++ options.c 2004-07-15 02:41:12
125d7fca 296@@ -133,6 +133,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
311@@ -144,13 +152,13 @@ static int modify_window_set;
ce06af28
WD
312 * address, or a hostname. **/
313 char *bind_address;
314
315-
316 static void print_rsync_version(enum logcode f)
317 {
318 char const *got_socketpair = "no ";
319 char const *hardlinks = "no ";
320 char const *links = "no ";
321 char const *ipv6 = "no ";
322+ char const *ssl = "no ";
323 STRUCT_STAT *dumstat;
324
325 #ifdef HAVE_SOCKETPAIR
9be39c35 326@@ -169,6 +177,10 @@ static void print_rsync_version(enum log
ce06af28
WD
327 ipv6 = "";
328 #endif
329
330+#ifdef HAVE_OPENSSL
331+ ssl = "";
332+#endif
333+
334 rprintf(f, "%s version %s protocol version %d\n",
335 RSYNC_NAME, RSYNC_VERSION, PROTOCOL_VERSION);
336 rprintf(f,
9be39c35 337@@ -182,10 +194,10 @@ static void print_rsync_version(enum log
ce06af28
WD
338 /* Note that this field may not have type ino_t. It depends
339 * on the complicated interaction between largefile feature
340 * macros. */
341- rprintf(f, " %sIPv6, %d-bit system inums, %d-bit internal inums\n",
342+ rprintf(f, " %sIPv6, %d-bit system inums, %d-bit internal inums, %sssl\n",
343 ipv6,
344 (int) (sizeof dumstat->st_ino * 8),
345- (int) (sizeof (uint64) * 8));
346+ (int) (sizeof (uint64) * 8), ssl);
347 #ifdef MAINTAINER_MODE
348 rprintf(f, " panic action: \"%s\"\n",
349 get_panic_action());
9be39c35 350@@ -298,6 +310,13 @@ void usage(enum logcode F)
ea238f1c
WD
351 rprintf(F," -4 --ipv4 prefer IPv4\n");
352 rprintf(F," -6 --ipv6 prefer IPv6\n");
ce06af28
WD
353 #endif
354+#ifdef HAVE_OPENSSL
355+ rprintf(F," --ssl allow socket connections to use SSL\n");
356+ rprintf(F," --ssl-cert=FILE path to server's SSL certificate\n");
357+ rprintf(F," --ssl-key=FILE path to server's SSL private key\n");
358+ rprintf(F," --ssl-key-passwd=PASS password for PEM-encoded private key\n");
359+ rprintf(F," --ssl-ca-certs=FILE path to trusted CA certificates\n");
360+#endif
ea238f1c 361 rprintf(F," -h, --help show this help screen\n");
ce06af28
WD
362
363 rprintf(F,"\n");
9be39c35 364@@ -309,7 +328,7 @@ void usage(enum logcode F)
ce06af28
WD
365 enum {OPT_VERSION = 1000, OPT_SENDER, OPT_EXCLUDE, OPT_EXCLUDE_FROM,
366 OPT_DELETE_AFTER, OPT_DELETE_EXCLUDED, OPT_LINK_DEST,
367 OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_MODIFY_WINDOW,
125d7fca
WD
368- OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_TIMEOUT,
369+ OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_TIMEOUT, OPT_USE_SSL,
ce06af28
WD
370 OPT_REFUSED_BASE = 9000};
371
372 static struct poptOption long_options[] = {
9be39c35 373@@ -396,6 +415,13 @@ static struct poptOption long_options[]
ea238f1c
WD
374 {"ipv4", '4', POPT_ARG_VAL, &default_af_hint, AF_INET, 0, 0 },
375 {"ipv6", '6', POPT_ARG_VAL, &default_af_hint, AF_INET6, 0, 0 },
ce06af28
WD
376 #endif
377+#ifdef HAVE_OPENSSL
378+ {"ssl", 0, POPT_ARG_NONE, 0, OPT_USE_SSL, 0, 0},
379+ {"ssl-cert", 0, POPT_ARG_STRING, &ssl_cert_path, OPT_USE_SSL, 0, 0},
380+ {"ssl-key", 0, POPT_ARG_STRING, &ssl_key_path, OPT_USE_SSL, 0, 0},
381+ {"ssl-key-passwd", 0, POPT_ARG_STRING, &ssl_key_passwd, OPT_USE_SSL, 0, 0},
382+ {"ssl-ca-certs", 0, POPT_ARG_STRING, &ssl_ca_path, OPT_USE_SSL, 0, 0},
383+#endif
384 {0,0,0,0, 0, 0, 0}
385 };
386
9be39c35 387@@ -593,6 +619,12 @@ int parse_arguments(int *argc, const cha
ce06af28
WD
388 return 0;
389 #endif
390
391+ case OPT_USE_SSL:
392+#ifdef HAVE_OPENSSL
393+ use_ssl = 1;
394+#endif
395+ break;
396+
397 default:
398 /* A large opt value means that set_refuse_options()
399 * turned this option off (opt-BASE is its index). */
9be39c35 400@@ -732,6 +764,17 @@ int parse_arguments(int *argc, const cha
ce06af28
WD
401 if (do_progress && !verbose)
402 verbose = 1;
78114162 403
ce06af28
WD
404+#ifdef HAVE_OPENSSL
405+ if (use_ssl) {
406+ if (init_tls()) {
407+ snprintf(err_buf, sizeof(err_buf),
408+ "Openssl error: %s\n",
409+ get_ssl_error());
410+ return 0;
411+ }
412+ }
413+#endif
78114162
WD
414+
415 if (bwlimit) {
416 bwlimit_writemax = (size_t)bwlimit * 128;
417 if (bwlimit_writemax < 512)
9be39c35 418--- orig/rsync.h 2004-07-07 08:27:00
13bed3dd 419+++ rsync.h 2004-07-03 20:22:28
ce06af28
WD
420@@ -32,6 +32,7 @@
421
422 #define DEFAULT_LOCK_FILE "/var/run/rsyncd.lock"
423 #define URL_PREFIX "rsync://"
424+#define SSL_URL_PREFIX "rsyncs://"
425
426 #define BACKUP_SUFFIX "~"
427
78114162 428@@ -326,6 +327,11 @@ enum msgcode {
ce06af28 429 #define uint64 unsigned off_t
78114162
WD
430 #endif
431
ce06af28
WD
432+#if HAVE_OPENSSL
433+#include <openssl/ssl.h>
434+#include <openssl/err.h>
78114162
WD
435+#endif
436+
ce06af28 437 /* Starting from protocol version 26, we always use 64-bit
78114162
WD
438 * ino_t and dev_t internally, even if this platform does not
439 * allow files to have 64-bit inums. That's because the
13bed3dd
WD
440--- orig/ssl.c 2004-07-02 21:44:19
441+++ ssl.c 2004-07-02 21:44:19
ce06af28
WD
442@@ -0,0 +1,366 @@
443+/* -*- c-file-style: "linux" -*-
444+ * ssl.c: operations for negotiating SSL rsync connections.
445+ *
446+ * Copyright (C) 2003 Casey Marshall <rsdio@metastatic.org>
447+ *
448+ * This program is free software; you can redistribute it and/or modify
449+ * it under the terms of the GNU General Public License as published by
450+ * the Free Software Foundation; either version 2 of the License, or
451+ * (at your option) any later version.
452+ *
453+ * This program is distributed in the hope that it will be useful,
454+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
455+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
456+ * GNU General Public License for more details.
457+ *
458+ * You should have received a copy of the GNU General Public License
459+ * along with this program; if not, write to the Free Software
460+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
461+ */
462+
463+#include "rsync.h"
464+
465+#ifdef HAVE_SYS_SELECT_H
466+#include <sys/select.h>
467+#else
468+#include <sys/time.h>
469+#include <sys/types.h>
470+#include <unistd.h>
471+#endif
472+#include <string.h>
473+
474+#define BUF_SIZE 1024
475+
476+extern int verbose;
477+extern int am_daemon;
478+extern int am_server;
479+
480+extern char *ssl_cert_path;
481+extern char *ssl_key_path;
482+extern char *ssl_key_passwd;
483+extern char *ssl_ca_path;
484+
485+static SSL_CTX *ssl_ctx;
486+static SSL *ssl;
487+static int tls_read[2] = { -1, -1 };
488+static int tls_write[2] = { -1, -1 };
489+static int ssl_running;
490+static int ssl_pid = -1;
491+
492+/**
493+ * A non-interactive callback to be passed to SSL_CTX_set_default_password_cb,
494+ * which merely copies the value of ssl_key_passwd into buf. This is
495+ * used for when the private key password is supplied via an option.
496+ */
497+static int default_password_cb(char *buf, int n, UNUSED(int f), UNUSED(void *u))
498+{
499+ if (ssl_key_passwd == NULL || n < strlen(ssl_key_passwd))
500+ return 0;
501+ strncpy(buf, ssl_key_passwd, n-1);
502+ return strlen(ssl_key_passwd);
503+}
504+
505+/**
506+ * If verbose, this method traces the status of the SSL handshake.
507+ */
508+static void info_callback(SSL *ssl, int cb, int val)
509+{
510+ char buf[128];
511+ char *cbs;
512+
513+ switch (cb) {
514+ case SSL_CB_LOOP:
515+ cbs = "SSL_CB_LOOP";
516+ break;
517+ case SSL_CB_EXIT:
518+ cbs = "SSL_CB_EXIT";
519+ break;
520+ case SSL_CB_READ:
521+ cbs = "SSL_CB_READ";
522+ break;
523+ case SSL_CB_WRITE:
524+ cbs = "SSL_CB_WRITE";
525+ break;
526+ case SSL_CB_ALERT:
527+ cbs = "SSL_CB_ALERT";
528+ break;
529+ case SSL_CB_READ_ALERT:
530+ cbs = "SSL_CB_READ_ALERT";
531+ break;
532+ case SSL_CB_WRITE_ALERT:
533+ cbs = "SSL_CB_WRITE_ALERT";
534+ break;
535+ case SSL_CB_ACCEPT_LOOP:
536+ cbs = "SSL_CB_ACCEPT_LOOP";
537+ break;
538+ case SSL_CB_ACCEPT_EXIT:
539+ cbs = "SSL_CB_ACCEPT_EXIT";
540+ break;
541+ case SSL_CB_CONNECT_LOOP:
542+ cbs = "SSL_CB_CONNECT_LOOP";
543+ break;
544+ case SSL_CB_CONNECT_EXIT:
545+ cbs = "SSL_CB_CONNECT_EXIT";
546+ break;
547+ case SSL_CB_HANDSHAKE_START:
548+ cbs = "SSL_CB_HANDSHAKE_START";
549+ break;
550+ case SSL_CB_HANDSHAKE_DONE:
551+ cbs = "SSL_CB_HANDSHAKE_DONE";
552+ break;
553+ default:
554+ snprintf(buf, sizeof buf, "??? (%d)", cb);
555+ cbs = buf;
556+ break;
557+ }
558+ if (verbose > 2) {
559+ rprintf(FLOG, "SSL: info_callback(%p,%s,%d)\n", ssl, cbs, val);
560+ if (cb == SSL_CB_HANDSHAKE_DONE) {
561+ SSL_CIPHER_description(SSL_get_current_cipher(ssl),
562+ buf, sizeof buf);
563+ rprintf(FLOG, "SSL: cipher: %s", buf);
564+ }
565+ }
566+}
567+
568+/**
569+ * Initializes the SSL context for TLSv1 connections; returns zero on
570+ * success.
571+ */
572+int init_tls(void)
573+{
574+ if (ssl_ctx)
575+ return 0;
576+ SSL_library_init();
577+ SSL_load_error_strings();
578+ ssl_ctx = SSL_CTX_new(TLSv1_method());
579+ if (!ssl_ctx)
580+ return 1;
581+ SSL_CTX_set_info_callback(ssl_ctx, info_callback);
582+
583+ /* Sets the certificate sent to the other party. */
584+ if (ssl_cert_path != NULL
585+ && SSL_CTX_use_certificate_file(ssl_ctx, ssl_cert_path,
586+ SSL_FILETYPE_PEM) != 1)
587+ return 1;
588+ /* Set up the simple non-interactive callback if the password
589+ * was supplied on the command line. */
590+ if (ssl_key_passwd != NULL)
591+ SSL_CTX_set_default_passwd_cb(ssl_ctx, default_password_cb);
592+ /* Sets the private key that matches the public certificate. */
593+ if (ssl_key_path != NULL) {
594+ if (SSL_CTX_use_PrivateKey_file(ssl_ctx, ssl_key_path,
595+ SSL_FILETYPE_PEM) != 1)
596+ return 1;
597+ if (SSL_CTX_check_private_key(ssl_ctx) != 1)
598+ return 1;
599+ }
600+ if (ssl_ca_path != NULL
601+ && !SSL_CTX_load_verify_locations(ssl_ctx, ssl_ca_path, NULL))
602+ return 1;
603+
604+ return 0;
605+}
606+
607+/**
608+ * Returns the error string for the current SSL error, if any.
609+ */
610+char *get_ssl_error(void)
611+{
612+ return ERR_error_string(ERR_get_error(), NULL);
613+}
614+
615+/**
616+ * Returns the input file descriptor for the SSL connection.
617+ */
618+int get_tls_rfd(void)
619+{
620+ return tls_read[0];
621+}
622+
623+/**
624+ * Returns the output file descriptor for the SSL connection.
625+ */
626+int get_tls_wfd(void)
627+{
628+ return tls_write[1];
629+}
630+
631+/**
632+ * Signal handler that ends the SSL connection.
633+ */
634+static RETSIGTYPE tls_sigusr1(int UNUSED(val))
635+{
636+ if (ssl) {
637+ SSL_shutdown(ssl);
638+ SSL_free(ssl);
639+ ssl = NULL;
640+ }
641+ ssl_running = 0;
642+}
643+
644+/**
645+ * Negotiates the TLS connection, creates a socket pair for communicating
646+ * with the rsync process, then forks into a new process that will handle
647+ * the communication.
648+ *
649+ * 0 is returned on success.
650+ */
651+int start_tls(int f_in, int f_out)
652+{
653+ int tls_fd;
654+ int n = 0, r;
655+ unsigned char buf1[BUF_SIZE], buf2[BUF_SIZE];
656+ int avail1 = 0, avail2 = 0, write1 = 0, write2 = 0;
657+ fd_set rd, wd;
658+
659+ if (fd_pair(tls_read))
660+ return 1;
661+ if (fd_pair(tls_write))
662+ return 1;
663+
664+ set_blocking(tls_read[0]);
665+ set_blocking(tls_read[1]);
666+ set_blocking(tls_write[0]);
667+ set_blocking(tls_write[1]);
668+ set_blocking(f_in);
669+ set_blocking(f_out);
670+
671+ ssl_pid = do_fork();
672+ if (ssl_pid < 0)
673+ return -1;
674+ if (ssl_pid != 0) {
675+ close(tls_write[0]);
676+ close(tls_read[1]);
677+ return 0;
678+ }
679+
680+ signal(SIGUSR1, tls_sigusr1);
681+ ssl = SSL_new(ssl_ctx);
682+ if (!ssl)
683+ goto closed;
684+ if (am_daemon || am_server)
685+ SSL_set_accept_state(ssl);
686+ else
687+ SSL_set_connect_state(ssl);
688+ SSL_set_rfd(ssl, f_in);
689+ SSL_set_wfd(ssl, f_out);
690+
691+ tls_fd = SSL_get_fd(ssl);
692+ n = tls_write[0];
693+ n = MAX(tls_read[1], n);
694+ n = MAX(tls_fd, n) + 1;
695+
696+ ssl_running = 1;
697+ while (ssl_running) {
698+ FD_ZERO(&rd);
699+ FD_ZERO(&wd);
700+ FD_SET(tls_write[0], &rd);
701+ FD_SET(tls_read[1], &wd);
702+ FD_SET(tls_fd, &rd);
703+ FD_SET(tls_fd, &wd);
704+
705+ r = select(n, &rd, &wd, NULL, NULL);
706+
707+ if (r == -1 && errno == EINTR)
708+ continue;
709+ if (FD_ISSET(tls_write[0], &rd)) {
710+ r = read(tls_write[0], buf1+avail1, BUF_SIZE-avail1);
711+ if (r >= 0)
712+ avail1 += r;
713+ else {
714+ rprintf(FERROR, "pipe read error: %s\n",
715+ strerror(errno));
716+ break;
717+ }
718+ }
719+ if (FD_ISSET(tls_fd, &rd)) {
720+ r = SSL_read(ssl, buf2+avail2, BUF_SIZE-avail2);
721+ if (r > 0)
722+ avail2 += r;
723+ else {
724+ switch (SSL_get_error(ssl, r)) {
725+ case SSL_ERROR_ZERO_RETURN:
726+ goto closed;
727+ case SSL_ERROR_WANT_READ:
728+ case SSL_ERROR_WANT_WRITE:
729+ break;
730+ case SSL_ERROR_SYSCALL:
731+ if (r == 0)
732+ rprintf(FERROR, "SSL spurious EOF\n");
733+ else
734+ rprintf(FERROR, "SSL I/O error: %s\n",
735+ strerror(errno));
736+ goto closed;
737+ case SSL_ERROR_SSL:
738+ rprintf(FERROR, "SSL: %s\n",
739+ ERR_error_string(ERR_get_error(), NULL));
740+ goto closed;
741+ default:
742+ rprintf(FERROR, "unexpected ssl error %d\n", r);
743+ goto closed;
744+ }
745+ }
746+ }
747+ if (FD_ISSET(tls_read[1], &wd) && write2 < avail2) {
748+ r = write(tls_read[1], buf2+write2, avail2-write2);
749+ if (r >= 0)
750+ write2 += r;
751+ else {
752+ rprintf(FERROR, "pipe write error: %s\n",
753+ strerror(errno));
754+ break;
755+ }
756+ }
757+ if (FD_ISSET(tls_fd, &wd) && write1 < avail1) {
758+ r = SSL_write(ssl, buf1+write1, avail1-write1);
759+ if (r > 0)
760+ write1 += r;
761+ else {
762+ switch (SSL_get_error(ssl, r)) {
763+ case SSL_ERROR_ZERO_RETURN:
764+ goto closed;
765+ case SSL_ERROR_WANT_READ:
766+ case SSL_ERROR_WANT_WRITE:
767+ break;
768+ case SSL_ERROR_SYSCALL:
769+ if (r == 0)
770+ rprintf(FERROR, "SSL: spurious EOF\n");
771+ else
772+ rprintf(FERROR, "SSL: I/O error: %s\n",
773+ strerror(errno));
774+ goto closed;
775+ case SSL_ERROR_SSL:
776+ rprintf(FERROR, "SSL: %s\n",
777+ ERR_error_string(ERR_get_error(), NULL));
778+ goto closed;
779+ default:
780+ rprintf(FERROR, "unexpected ssl error %d\n", r);
781+ goto closed;
782+ }
783+ }
784+ }
785+ if (avail1 == write1)
786+ avail1 = write1 = 0;
787+ if (avail2 == write2)
788+ avail2 = write2 = 0;
789+ }
790+
791+ /* XXX I'm pretty sure that there is a lot that I am not considering
792+ here. Bugs? Yes, probably. */
793+
794+ /* We're finished. */
795+ closed:
796+ close(tls_read[1]);
797+ close(tls_write[0]);
798+ exit(0);
799+}
800+
801+/**
802+ * Ends the TLS connection.
803+ */
804+void end_tls(void)
805+{
806+ if (ssl_pid > 0)
807+ kill(ssl_pid, SIGUSR1);
808+}