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