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