Fixed the sending of new files with long xattrs.
[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
ffc18846 51@@ -25,6 +25,9 @@
1898c899
WD
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;
ffc18846 61@@ -116,6 +119,14 @@ NORETURN void _exit_cleanup(int code, co
22585581
WD
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
ffc18846 78@@ -29,6 +29,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 85 extern int rsync_port;
f2376a08 86 extern int ignore_errors;
d9a67109 87 extern int kluge_around_eof;
ffc18846 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
5e3c6c93 107 int start_inband_exchange(const char *user, char *path, int f_in, int f_out,
ffc18846 108@@ -168,6 +181,33 @@ int start_inband_exchange(const char *us
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);
ffc18846 142@@ -196,6 +236,10 @@ int start_inband_exchange(const char *us
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
ffc18846 153@@ -203,6 +247,10 @@ int start_inband_exchange(const char *us
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
f2376a08 164@@ -785,6 +833,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;
f2376a08 174@@ -796,6 +847,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
f2376a08 221@@ -172,6 +172,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
ffc18846
WD
236@@ -202,6 +210,7 @@ static void print_rsync_version(enum log
237 char const *acls = "no ";
ce06af28
WD
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
ffc18846 244@@ -228,6 +237,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);
ffc18846
WD
254 rprintf(f, "Copyright (C) 1996-2007 by Andrew Tridgell, Wayne Davison, and others.\n");
255@@ -238,8 +251,8 @@ static void print_rsync_version(enum log
5e3c6c93
WD
256 (int)(sizeof (int64) * 8));
257 rprintf(f, " %ssocketpairs, %shardlinks, %ssymlinks, %sIPv6, batchfiles, %sinplace,\n",
258 got_socketpair, hardlinks, links, ipv6, have_inplace);
ffc18846
WD
259- rprintf(f, " %sappend, %sACLs\n",
260- have_inplace, acls);
261+ rprintf(f, " %sappend, %sACLs, %sSSL\n",
262+ have_inplace, acls, ssl);
5e3c6c93 263
ce06af28 264 #ifdef MAINTAINER_MODE
5e3c6c93 265 rprintf(f, "Panic Action: \"%s\"\n", get_panic_action());
ffc18846 266@@ -390,6 +403,13 @@ void usage(enum logcode F)
d4e89c6a
WD
267 rprintf(F," -4, --ipv4 prefer IPv4\n");
268 rprintf(F," -6, --ipv6 prefer IPv6\n");
ce06af28 269 #endif
22585581 270+#ifdef HAVE_OPENSSL
ce06af28 271+ rprintf(F," --ssl allow socket connections to use SSL\n");
37d262c5
WD
272+ rprintf(F," --ssl-cert=FILE path to daemon's SSL certificate\n");
273+ rprintf(F," --ssl-key=FILE path to daemon's SSL private key\n");
ce06af28
WD
274+ rprintf(F," --ssl-key-passwd=PASS password for PEM-encoded private key\n");
275+ rprintf(F," --ssl-ca-certs=FILE path to trusted CA certificates\n");
276+#endif
896871f8 277 rprintf(F," --version print version number\n");
d9a67109 278 rprintf(F,"(-h) --help show this help (-h works with no other options)\n");
ce06af28 279
ffc18846 280@@ -403,7 +423,7 @@ enum {OPT_VERSION = 1000, OPT_DAEMON, OP
6849cd84 281 OPT_FILTER, OPT_COMPARE_DEST, OPT_COPY_DEST, OPT_LINK_DEST, OPT_HELP,
0ca6aebe 282 OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_MODIFY_WINDOW, OPT_MIN_SIZE, OPT_CHMOD,
5398d042 283 OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_ONLY_WRITE_BATCH, OPT_MAX_SIZE,
4a65fe72
WD
284- OPT_NO_D,
285+ OPT_NO_D, OPT_USE_SSL,
0ca6aebe 286 OPT_SERVER, OPT_REFUSED_BASE = 9000};
ce06af28 287
70c5d149 288 static struct poptOption long_options[] = {
ffc18846 289@@ -554,6 +574,13 @@ static struct poptOption long_options[]
489b0a72 290 {"checksum-seed", 0, POPT_ARG_INT, &checksum_seed, 0, 0, 0 },
0ca6aebe 291 {"server", 0, POPT_ARG_NONE, 0, OPT_SERVER, 0, 0 },
489b0a72 292 {"sender", 0, POPT_ARG_NONE, 0, OPT_SENDER, 0, 0 },
22585581 293+#ifdef HAVE_OPENSSL
5388f859
WD
294+ {"ssl", 0, POPT_ARG_NONE, 0, OPT_USE_SSL, 0, 0},
295+ {"ssl-cert", 0, POPT_ARG_STRING, &ssl_cert_path, OPT_USE_SSL, 0, 0},
296+ {"ssl-key", 0, POPT_ARG_STRING, &ssl_key_path, OPT_USE_SSL, 0, 0},
ce06af28 297+ {"ssl-key-passwd", 0, POPT_ARG_STRING, &ssl_key_passwd, OPT_USE_SSL, 0, 0},
5388f859 298+ {"ssl-ca-certs", 0, POPT_ARG_STRING, &ssl_ca_path, OPT_USE_SSL, 0, 0},
ce06af28 299+#endif
489b0a72 300 /* All the following options switch us into daemon-mode option-parsing. */
5388f859 301 {"config", 0, POPT_ARG_STRING, 0, OPT_DAEMON, 0, 0 },
2b06a19d 302 {"daemon", 0, POPT_ARG_NONE, 0, OPT_DAEMON, 0, 0 },
ffc18846 303@@ -581,6 +608,13 @@ static void daemon_usage(enum logcode F)
144adbf3
WD
304 rprintf(F," -4, --ipv4 prefer IPv4\n");
305 rprintf(F," -6, --ipv6 prefer IPv6\n");
306 #endif
22585581 307+#ifdef HAVE_OPENSSL
144adbf3
WD
308+ rprintf(F," --ssl allow socket connections to use SSL\n");
309+ rprintf(F," --ssl-cert=FILE path to daemon's SSL certificate\n");
310+ rprintf(F," --ssl-key=FILE path to daemon's SSL private key\n");
311+ rprintf(F," --ssl-key-passwd=PASS password for PEM-encoded private key\n");
312+ rprintf(F," --ssl-ca-certs=FILE path to trusted CA certificates\n");
313+#endif
314 rprintf(F," --help show this help screen\n");
315
316 rprintf(F,"\n");
ffc18846 317@@ -607,6 +641,13 @@ static struct poptOption long_daemon_opt
144adbf3
WD
318 {"protocol", 0, POPT_ARG_INT, &protocol_version, 0, 0, 0 },
319 {"server", 0, POPT_ARG_NONE, &am_server, 0, 0, 0 },
320 {"temp-dir", 'T', POPT_ARG_STRING, &tmpdir, 0, 0, 0 },
22585581 321+#ifdef HAVE_OPENSSL
144adbf3
WD
322+ {"ssl", 0, POPT_ARG_NONE, 0, OPT_USE_SSL, 0, 0},
323+ {"ssl-cert", 0, POPT_ARG_STRING, &ssl_cert_path, OPT_USE_SSL, 0, 0},
324+ {"ssl-key", 0, POPT_ARG_STRING, &ssl_key_path, OPT_USE_SSL, 0, 0},
325+ {"ssl-key-passwd", 0, POPT_ARG_STRING, &ssl_key_passwd, OPT_USE_SSL, 0, 0},
326+ {"ssl-ca-certs", 0, POPT_ARG_STRING, &ssl_ca_path, OPT_USE_SSL, 0, 0},
327+#endif
328 {"verbose", 'v', POPT_ARG_NONE, 0, 'v', 0, 0 },
329 {"no-verbose", 0, POPT_ARG_VAL, &verbose, 0, 0, 0 },
330 {"no-v", 0, POPT_ARG_VAL, &verbose, 0, 0, 0 },
ffc18846 331@@ -866,6 +907,12 @@ int parse_arguments(int *argc, const cha
144adbf3
WD
332 verbose++;
333 break;
334
335+#ifdef HAVE_OPENSSL
336+ case OPT_USE_SSL:
337+ use_ssl = 1;
338+ break;
339+#endif
340+
341 default:
342 rprintf(FERROR,
343 "rsync: %s: %s (in daemon mode)\n",
ffc18846 344@@ -889,6 +936,17 @@ int parse_arguments(int *argc, const cha
144adbf3
WD
345 exit_cleanup(RERR_SYNTAX);
346 }
347
348+#ifdef HAVE_OPENSSL
349+ if (use_ssl) {
350+ if (init_tls()) {
351+ snprintf(err_buf, sizeof(err_buf),
352+ "Openssl error: %s\n",
353+ get_ssl_error());
354+ return 0;
355+ }
356+ }
357+#endif
358+
359 *argv = poptGetArgs(pc);
360 *argc = count_args(*argv);
361 am_starting_up = 0;
ffc18846
WD
362@@ -1122,6 +1180,12 @@ int parse_arguments(int *argc, const cha
363 #endif
364
ce06af28 365
22585581 366+#ifdef HAVE_OPENSSL
144adbf3 367+ case OPT_USE_SSL:
ce06af28 368+ use_ssl = 1;
ce06af28 369+ break;
144adbf3 370+#endif
ce06af28
WD
371+
372 default:
373 /* A large opt value means that set_refuse_options()
27a7053c 374 * turned this option off. */
ffc18846 375@@ -1400,6 +1464,17 @@ int parse_arguments(int *argc, const cha
a7219d20 376 if (delay_updates && !partial_dir)
4a65fe72 377 partial_dir = tmp_partialdir;
78114162 378
22585581 379+#ifdef HAVE_OPENSSL
ce06af28
WD
380+ if (use_ssl) {
381+ if (init_tls()) {
382+ snprintf(err_buf, sizeof(err_buf),
383+ "Openssl error: %s\n",
384+ get_ssl_error());
385+ return 0;
386+ }
387+ }
388+#endif
78114162 389+
4c1f2ca5 390 if (inplace) {
3d1facaa 391 #ifdef HAVE_FTRUNCATE
4c1f2ca5 392 if (partial_dir) {
ffc18846 393@@ -1827,10 +1902,27 @@ char *check_for_hostspec(char *s, char *
def2ace9
WD
394 char *p;
395 int not_host;
8330f4aa 396 int hostlen;
def2ace9
WD
397+ int url_prefix_len = sizeof URL_PREFIX - 1;
398
399- if (port_ptr && strncasecmp(URL_PREFIX, s, strlen(URL_PREFIX)) == 0) {
400+ if (!port_ptr)
401+ url_prefix_len = 0;
402+ else if (strncasecmp(URL_PREFIX, s, url_prefix_len) != 0) {
22585581 403+#ifdef HAVE_OPENSSL
def2ace9
WD
404+ url_prefix_len = sizeof SSL_URL_PREFIX - 1;
405+ if (strncasecmp(SSL_URL_PREFIX, s, url_prefix_len) != 0)
406+ url_prefix_len = 0;
407+ else {
408+ if (!use_ssl)
409+ init_tls();
410+ use_ssl = 1;
411+ }
412+#else
413+ url_prefix_len = 0;
414+#endif
415+ }
416+ if (url_prefix_len) {
417 char *path;
def2ace9
WD
418- s += strlen(URL_PREFIX);
419+ s += url_prefix_len;
420 if ((p = strchr(s, '/')) != NULL) {
421 hostlen = p - s;
422 path = p + 1;
9a7eef96
WD
423--- old/rsync.h
424+++ new/rsync.h
ffc18846 425@@ -31,6 +31,7 @@
ce06af28
WD
426
427 #define DEFAULT_LOCK_FILE "/var/run/rsyncd.lock"
428 #define URL_PREFIX "rsync://"
429+#define SSL_URL_PREFIX "rsyncs://"
430
431 #define BACKUP_SUFFIX "~"
432
ffc18846 433@@ -474,6 +475,11 @@ enum msgcode {
a7219d20 434 # define SIZEOF_INT64 SIZEOF_OFF_T
78114162
WD
435 #endif
436
22585581 437+#ifdef HAVE_OPENSSL
ce06af28
WD
438+#include <openssl/ssl.h>
439+#include <openssl/err.h>
78114162
WD
440+#endif
441+
ce06af28 442 /* Starting from protocol version 26, we always use 64-bit
78114162
WD
443 * ino_t and dev_t internally, even if this platform does not
444 * allow files to have 64-bit inums. That's because the
9a7eef96
WD
445--- old/ssl.c
446+++ new/ssl.c
144adbf3 447@@ -0,0 +1,370 @@
ce06af28
WD
448+/* -*- c-file-style: "linux" -*-
449+ * ssl.c: operations for negotiating SSL rsync connections.
450+ *
451+ * Copyright (C) 2003 Casey Marshall <rsdio@metastatic.org>
452+ *
453+ * This program is free software; you can redistribute it and/or modify
454+ * it under the terms of the GNU General Public License as published by
455+ * the Free Software Foundation; either version 2 of the License, or
456+ * (at your option) any later version.
457+ *
458+ * This program is distributed in the hope that it will be useful,
459+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
460+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
461+ * GNU General Public License for more details.
462+ *
463+ * You should have received a copy of the GNU General Public License
464+ * along with this program; if not, write to the Free Software
465+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
466+ */
467+
468+#include "rsync.h"
469+
22585581 470+#ifdef HAVE_SYS_SELECT_H
ce06af28
WD
471+#include <sys/select.h>
472+#else
473+#include <sys/time.h>
474+#include <sys/types.h>
475+#include <unistd.h>
476+#endif
477+#include <string.h>
478+
479+#define BUF_SIZE 1024
480+
481+extern int verbose;
482+extern int am_daemon;
483+extern int am_server;
484+
485+extern char *ssl_cert_path;
486+extern char *ssl_key_path;
487+extern char *ssl_key_passwd;
488+extern char *ssl_ca_path;
489+
490+static SSL_CTX *ssl_ctx;
491+static SSL *ssl;
492+static int tls_read[2] = { -1, -1 };
493+static int tls_write[2] = { -1, -1 };
494+static int ssl_running;
495+static int ssl_pid = -1;
496+
144adbf3
WD
497+#ifdef HAVE_SIGACTION
498+static struct sigaction sigact;
499+#endif
500+
ce06af28
WD
501+/**
502+ * A non-interactive callback to be passed to SSL_CTX_set_default_password_cb,
503+ * which merely copies the value of ssl_key_passwd into buf. This is
504+ * used for when the private key password is supplied via an option.
505+ */
506+static int default_password_cb(char *buf, int n, UNUSED(int f), UNUSED(void *u))
507+{
2fd4a7f7 508+ if (ssl_key_passwd == NULL || n < (int)strlen(ssl_key_passwd))
ce06af28
WD
509+ return 0;
510+ strncpy(buf, ssl_key_passwd, n-1);
511+ return strlen(ssl_key_passwd);
512+}
513+
514+/**
515+ * If verbose, this method traces the status of the SSL handshake.
516+ */
2fd4a7f7 517+static void info_callback(const SSL *ssl, int cb, int val)
ce06af28
WD
518+{
519+ char buf[128];
520+ char *cbs;
521+
522+ switch (cb) {
523+ case SSL_CB_LOOP:
524+ cbs = "SSL_CB_LOOP";
525+ break;
526+ case SSL_CB_EXIT:
527+ cbs = "SSL_CB_EXIT";
528+ break;
529+ case SSL_CB_READ:
530+ cbs = "SSL_CB_READ";
531+ break;
532+ case SSL_CB_WRITE:
533+ cbs = "SSL_CB_WRITE";
534+ break;
535+ case SSL_CB_ALERT:
536+ cbs = "SSL_CB_ALERT";
537+ break;
538+ case SSL_CB_READ_ALERT:
539+ cbs = "SSL_CB_READ_ALERT";
540+ break;
541+ case SSL_CB_WRITE_ALERT:
542+ cbs = "SSL_CB_WRITE_ALERT";
543+ break;
544+ case SSL_CB_ACCEPT_LOOP:
545+ cbs = "SSL_CB_ACCEPT_LOOP";
546+ break;
547+ case SSL_CB_ACCEPT_EXIT:
548+ cbs = "SSL_CB_ACCEPT_EXIT";
549+ break;
550+ case SSL_CB_CONNECT_LOOP:
551+ cbs = "SSL_CB_CONNECT_LOOP";
552+ break;
553+ case SSL_CB_CONNECT_EXIT:
554+ cbs = "SSL_CB_CONNECT_EXIT";
555+ break;
556+ case SSL_CB_HANDSHAKE_START:
557+ cbs = "SSL_CB_HANDSHAKE_START";
558+ break;
559+ case SSL_CB_HANDSHAKE_DONE:
560+ cbs = "SSL_CB_HANDSHAKE_DONE";
561+ break;
562+ default:
563+ snprintf(buf, sizeof buf, "??? (%d)", cb);
564+ cbs = buf;
565+ break;
566+ }
567+ if (verbose > 2) {
568+ rprintf(FLOG, "SSL: info_callback(%p,%s,%d)\n", ssl, cbs, val);
569+ if (cb == SSL_CB_HANDSHAKE_DONE) {
2fd4a7f7 570+ SSL_CIPHER_description(SSL_get_current_cipher((SSL*)ssl),
ce06af28
WD
571+ buf, sizeof buf);
572+ rprintf(FLOG, "SSL: cipher: %s", buf);
573+ }
574+ }
575+}
576+
577+/**
578+ * Initializes the SSL context for TLSv1 connections; returns zero on
579+ * success.
580+ */
581+int init_tls(void)
582+{
583+ if (ssl_ctx)
584+ return 0;
585+ SSL_library_init();
586+ SSL_load_error_strings();
587+ ssl_ctx = SSL_CTX_new(TLSv1_method());
588+ if (!ssl_ctx)
589+ return 1;
590+ SSL_CTX_set_info_callback(ssl_ctx, info_callback);
591+
592+ /* Sets the certificate sent to the other party. */
593+ if (ssl_cert_path != NULL
594+ && SSL_CTX_use_certificate_file(ssl_ctx, ssl_cert_path,
595+ SSL_FILETYPE_PEM) != 1)
596+ return 1;
597+ /* Set up the simple non-interactive callback if the password
598+ * was supplied on the command line. */
599+ if (ssl_key_passwd != NULL)
600+ SSL_CTX_set_default_passwd_cb(ssl_ctx, default_password_cb);
601+ /* Sets the private key that matches the public certificate. */
602+ if (ssl_key_path != NULL) {
603+ if (SSL_CTX_use_PrivateKey_file(ssl_ctx, ssl_key_path,
604+ SSL_FILETYPE_PEM) != 1)
605+ return 1;
606+ if (SSL_CTX_check_private_key(ssl_ctx) != 1)
607+ return 1;
608+ }
609+ if (ssl_ca_path != NULL
610+ && !SSL_CTX_load_verify_locations(ssl_ctx, ssl_ca_path, NULL))
611+ return 1;
612+
613+ return 0;
614+}
615+
616+/**
617+ * Returns the error string for the current SSL error, if any.
618+ */
619+char *get_ssl_error(void)
620+{
621+ return ERR_error_string(ERR_get_error(), NULL);
622+}
623+
624+/**
625+ * Returns the input file descriptor for the SSL connection.
626+ */
627+int get_tls_rfd(void)
628+{
629+ return tls_read[0];
630+}
631+
632+/**
633+ * Returns the output file descriptor for the SSL connection.
634+ */
635+int get_tls_wfd(void)
636+{
637+ return tls_write[1];
638+}
639+
640+/**
641+ * Signal handler that ends the SSL connection.
642+ */
643+static RETSIGTYPE tls_sigusr1(int UNUSED(val))
644+{
645+ if (ssl) {
646+ SSL_shutdown(ssl);
647+ SSL_free(ssl);
648+ ssl = NULL;
649+ }
650+ ssl_running = 0;
651+}
652+
653+/**
654+ * Negotiates the TLS connection, creates a socket pair for communicating
655+ * with the rsync process, then forks into a new process that will handle
656+ * the communication.
657+ *
658+ * 0 is returned on success.
659+ */
660+int start_tls(int f_in, int f_out)
661+{
662+ int tls_fd;
663+ int n = 0, r;
664+ unsigned char buf1[BUF_SIZE], buf2[BUF_SIZE];
665+ int avail1 = 0, avail2 = 0, write1 = 0, write2 = 0;
666+ fd_set rd, wd;
667+
668+ if (fd_pair(tls_read))
669+ return 1;
670+ if (fd_pair(tls_write))
671+ return 1;
672+
673+ set_blocking(tls_read[0]);
674+ set_blocking(tls_read[1]);
675+ set_blocking(tls_write[0]);
676+ set_blocking(tls_write[1]);
677+ set_blocking(f_in);
678+ set_blocking(f_out);
679+
680+ ssl_pid = do_fork();
681+ if (ssl_pid < 0)
682+ return -1;
683+ if (ssl_pid != 0) {
684+ close(tls_write[0]);
685+ close(tls_read[1]);
686+ return 0;
687+ }
688+
144adbf3 689+ SIGACTION(SIGUSR1, tls_sigusr1);
ce06af28
WD
690+ ssl = SSL_new(ssl_ctx);
691+ if (!ssl)
692+ goto closed;
693+ if (am_daemon || am_server)
694+ SSL_set_accept_state(ssl);
695+ else
696+ SSL_set_connect_state(ssl);
697+ SSL_set_rfd(ssl, f_in);
698+ SSL_set_wfd(ssl, f_out);
699+
700+ tls_fd = SSL_get_fd(ssl);
701+ n = tls_write[0];
702+ n = MAX(tls_read[1], n);
703+ n = MAX(tls_fd, n) + 1;
704+
705+ ssl_running = 1;
706+ while (ssl_running) {
707+ FD_ZERO(&rd);
708+ FD_ZERO(&wd);
709+ FD_SET(tls_write[0], &rd);
710+ FD_SET(tls_read[1], &wd);
711+ FD_SET(tls_fd, &rd);
712+ FD_SET(tls_fd, &wd);
713+
714+ r = select(n, &rd, &wd, NULL, NULL);
715+
716+ if (r == -1 && errno == EINTR)
717+ continue;
718+ if (FD_ISSET(tls_write[0], &rd)) {
719+ r = read(tls_write[0], buf1+avail1, BUF_SIZE-avail1);
720+ if (r >= 0)
721+ avail1 += r;
722+ else {
723+ rprintf(FERROR, "pipe read error: %s\n",
724+ strerror(errno));
725+ break;
726+ }
727+ }
728+ if (FD_ISSET(tls_fd, &rd)) {
729+ r = SSL_read(ssl, buf2+avail2, BUF_SIZE-avail2);
730+ if (r > 0)
731+ avail2 += r;
732+ else {
733+ switch (SSL_get_error(ssl, r)) {
734+ case SSL_ERROR_ZERO_RETURN:
735+ goto closed;
736+ case SSL_ERROR_WANT_READ:
737+ case SSL_ERROR_WANT_WRITE:
738+ break;
739+ case SSL_ERROR_SYSCALL:
740+ if (r == 0)
741+ rprintf(FERROR, "SSL spurious EOF\n");
742+ else
743+ rprintf(FERROR, "SSL I/O error: %s\n",
744+ strerror(errno));
745+ goto closed;
746+ case SSL_ERROR_SSL:
747+ rprintf(FERROR, "SSL: %s\n",
748+ ERR_error_string(ERR_get_error(), NULL));
749+ goto closed;
750+ default:
751+ rprintf(FERROR, "unexpected ssl error %d\n", r);
752+ goto closed;
753+ }
754+ }
755+ }
756+ if (FD_ISSET(tls_read[1], &wd) && write2 < avail2) {
757+ r = write(tls_read[1], buf2+write2, avail2-write2);
758+ if (r >= 0)
759+ write2 += r;
760+ else {
761+ rprintf(FERROR, "pipe write error: %s\n",
762+ strerror(errno));
763+ break;
764+ }
765+ }
766+ if (FD_ISSET(tls_fd, &wd) && write1 < avail1) {
767+ r = SSL_write(ssl, buf1+write1, avail1-write1);
768+ if (r > 0)
769+ write1 += r;
770+ else {
771+ switch (SSL_get_error(ssl, r)) {
772+ case SSL_ERROR_ZERO_RETURN:
773+ goto closed;
774+ case SSL_ERROR_WANT_READ:
775+ case SSL_ERROR_WANT_WRITE:
776+ break;
777+ case SSL_ERROR_SYSCALL:
778+ if (r == 0)
779+ rprintf(FERROR, "SSL: spurious EOF\n");
780+ else
781+ rprintf(FERROR, "SSL: I/O error: %s\n",
782+ strerror(errno));
783+ goto closed;
784+ case SSL_ERROR_SSL:
785+ rprintf(FERROR, "SSL: %s\n",
786+ ERR_error_string(ERR_get_error(), NULL));
787+ goto closed;
788+ default:
789+ rprintf(FERROR, "unexpected ssl error %d\n", r);
790+ goto closed;
791+ }
792+ }
793+ }
794+ if (avail1 == write1)
795+ avail1 = write1 = 0;
796+ if (avail2 == write2)
797+ avail2 = write2 = 0;
798+ }
799+
800+ /* XXX I'm pretty sure that there is a lot that I am not considering
801+ here. Bugs? Yes, probably. */
802+
803+ /* We're finished. */
804+ closed:
805+ close(tls_read[1]);
806+ close(tls_write[0]);
807+ exit(0);
808+}
809+
810+/**
811+ * Ends the TLS connection.
812+ */
813+void end_tls(void)
814+{
815+ if (ssl_pid > 0)
816+ kill(ssl_pid, SIGUSR1);
817+}