Debian Codegraph @

rsync-ssl

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 #!/usr/bin/env bash
2
3 # This script uses openssl, gnutls, or stunnel to secure an rsync daemon connection.
4
5 # By default this script takes rsync args and hands them off to the actual
6 # rsync command with an --rsh option that makes it open an SSL connection to an
7 # rsync daemon. See the rsync-ssl manpage for usage details and env variables.
8
9 # When the first arg is --HELPER, we are being used by rsync as an --rsh helper
10 # script, and the args are (note the trailing dot):
11 #
12 # rsync-ssl --HELPER HOSTNAME rsync --server --daemon .
13 #
14 # --HELPER is not a user-facing option, so it is not documented in the manpage.
15
16 # The first SSL setup was based on: http://dozzie.jarowit.net/trac/wiki/RsyncSSL
17 # Note that an stunnel connection requires at least version 4.x of stunnel.
18
19 function rsync_ssl_run {
20 case "$*" in
21 *rsync://*) ;;
22 *::*) ;;
23 *)
24 echo "You must use rsync-ssl with a daemon-style hostname." 1>&2
25 exit 1
26 ;;
27 esac
28
29 exec rsync --rsh="'$0' --HELPER" "${@}"
30 }
31
32 function rsync_ssl_helper {
33 if [[ -z "$RSYNC_SSL_TYPE" ]]; then
34 found=`path_search openssl stunnel4 stunnel` || exit 1
35 if [[ "$found" == */openssl ]]; then
36 RSYNC_SSL_TYPE=openssl
37 RSYNC_SSL_OPENSSL="$found"
38 elif [[ "$found" == */gnutls-cli ]]; then
39 RSYNC_SSL_TYPE=gnutls
40 RSYNC_SSL_GNUTLS="$found"
41 else
42 RSYNC_SSL_TYPE=stunnel
43 RSYNC_SSL_STUNNEL="$found"
44 fi
45 fi
46
47 case "$RSYNC_SSL_TYPE" in
48 openssl)
49 if [[ -z "$RSYNC_SSL_OPENSSL" ]]; then
50 RSYNC_SSL_OPENSSL=`path_search openssl` || exit 1
51 fi
52 optsep=' '
53 ;;
54 gnutls)
55 if [[ -z "$RSYNC_SSL_GNUTLS" ]]; then
56 RSYNC_SSL_GNUTLS=`path_search gnutls-cli` || exit 1
57 fi
58 optsep=' '
59 ;;
60 stunnel)
61 if [[ -z "$RSYNC_SSL_STUNNEL" ]]; then
62 RSYNC_SSL_STUNNEL=`path_search stunnel4 stunnel` || exit 1
63 fi
64 optsep=' = '
65 ;;
66 *)
67 echo "The RSYNC_SSL_TYPE specifies an unknown type: $RSYNC_SSL_TYPE" 1>&2
68 exit 1
69 ;;
70 esac
71
72 if [[ -z "$RSYNC_SSL_CERT" ]]; then
73 certopt=""
74 gnutls_cert_opt=""
75 else
76 certopt="-cert$optsep$RSYNC_SSL_CERT"
77 gnutls_cert_opt="--x509certfile=$RSYNC_SSL_CERT"
78 fi
79
80 if [[ -z "$RSYNC_SSL_KEY" ]]; then
81 keyopt=""
82 gnutls_key_opt=""
83 else
84 keyopt="-key$optsep$RSYNC_SSL_KEY"
85 gnutls_key_opt="--x509keyfile=$RSYNC_SSL_KEY"
86 fi
87
88 if [[ -z ${RSYNC_SSL_CA_CERT+x} ]]; then
89 # RSYNC_SSL_CA_CERT unset - default CA set AND verify:
90 # openssl:
91 caopt="-verify_return_error -verify 4"
92 # gnutls:
93 # gnutls-cli has no reliable "use the system trust store AND make
94 # verification fatal" switch across versions, so the gnutls path
95 # refuses below unless RSYNC_SSL_ALLOW_INSECURE_GNUTLS is set.
96 gnutls_opts=""
97 # stunnel:
98 # Since there is no way of using the default CA certificate collection,
99 # we cannot do any verification. Thus, stunnel should really only be
100 # used if nothing else is available.
101 cafile=""
102 verify=""
103 elif [[ "$RSYNC_SSL_CA_CERT" == "" ]]; then
104 # RSYNC_SSL_CA_CERT set but empty -do NO verifications:
105 # openssl:
106 caopt="-verify 1"
107 # gnutls:
108 gnutls_opts="--insecure"
109 # stunnel:
110 cafile=""
111 verify="verifyChain = no"
112 else
113 # RSYNC_SSL_CA_CERT set - use CA AND verify:
114 # openssl:
115 caopt="-CAfile $RSYNC_SSL_CA_CERT -verify_return_error -verify 4"
116 # gnutls:
117 gnutls_opts="--x509cafile=$RSYNC_SSL_CA_CERT"
118 # stunnel:
119 cafile="CAfile = $RSYNC_SSL_CA_CERT"
120 verify="verifyChain = yes"
121 fi
122
123 port="${RSYNC_PORT:-0}"
124 if [[ "$port" == 0 ]]; then
125 port="${RSYNC_SSL_PORT:-874}"
126 fi
127
128 # If the user specified USER@HOSTNAME::module, then rsync passes us
129 # the -l USER option too, so we must be prepared to ignore it.
130 if [[ "$1" == "-l" ]]; then
131 shift 2
132 fi
133
134 hostname="$1"
135 shift
136
137 if [[ -z "$hostname" || "$1" != rsync || "$2" != --server || "$3" != --daemon ]]; then
138 echo "Usage: rsync-ssl --HELPER HOSTNAME rsync --server --daemon ." 1>&2
139 exit 1
140 fi
141 validate_ssl_hostname "$hostname"
142
143 if [[ $RSYNC_SSL_TYPE == stunnel && -z ${RSYNC_SSL_CA_CERT+x} && "$RSYNC_SSL_ALLOW_INSECURE_STUNNEL" != 1 ]]; then
144 echo "stunnel requires RSYNC_SSL_CA_CERT for server certificate validation (or set RSYNC_SSL_ALLOW_INSECURE_STUNNEL=1 to opt out)" 1>&2
145 exit 1
146 fi
147
148 # Bind the server certificate to the requested host (openssl -verify_hostname,
149 # stunnel checkHost) so a cert that is CA-valid for a *different* name can't be
150 # used to MITM. Set RSYNC_SSL_SKIP_HOSTNAME_CHECK=1 to keep CA chain
151 # verification but skip the identity check (e.g. bare-IP or internal-CA setups).
152 # checkHost only applies while the chain is being verified.
153 openssl_host_opt="-verify_hostname $hostname"
154 stunnel_host_opt=""
155 [[ "$verify" == "verifyChain = yes" ]] && stunnel_host_opt="checkHost = $hostname"
156 if [[ "$RSYNC_SSL_SKIP_HOSTNAME_CHECK" == 1 ]]; then
157 openssl_host_opt=""
158 stunnel_host_opt=""
159 fi
160
161 if [[ $RSYNC_SSL_TYPE == gnutls && -z ${RSYNC_SSL_CA_CERT+x} && "$RSYNC_SSL_ALLOW_INSECURE_GNUTLS" != 1 ]]; then
162 echo "gnutls-cli requires RSYNC_SSL_CA_CERT for server certificate validation (or set RSYNC_SSL_ALLOW_INSECURE_GNUTLS=1 to opt out)" 1>&2
163 exit 1
164 fi
165
166 if [[ $RSYNC_SSL_TYPE == openssl ]]; then
167 exec "$RSYNC_SSL_OPENSSL" s_client $caopt $certopt $keyopt -quiet -verify_quiet -servername $hostname $openssl_host_opt -connect $hostname:$port
168 elif [[ $RSYNC_SSL_TYPE == gnutls ]]; then
169 exec "$RSYNC_SSL_GNUTLS" --logfile=/dev/null $gnutls_cert_opt $gnutls_key_opt $gnutls_opts $hostname:$port
170 else
171 # [email protected] came up with this no-tmpfile calling syntax:
172 exec "$RSYNC_SSL_STUNNEL" -fd 10 11<&0 <<EOF 10<&0 0<&11 11<&-
173 foreground = yes
174 debug = crit
175 connect = $hostname:$port
176 client = yes
177 TIMEOUTclose = 0
178 $verify
179 $stunnel_host_opt
180 $certopt
181 $cafile
182 EOF
183 fi
184 }
185
186 function validate_ssl_hostname {
187 local host="$1"
188 if [[ -z "$host" || "$host" == -* || "$host" =~ [^A-Za-z0-9._:-] ]]; then
189 echo "invalid rsync-ssl hostname: $host" 1>&2
190 exit 1
191 fi
192 }
193
194 function path_search {
195 IFS_SAVE="$IFS"
196 IFS=:
197 for prog in "${@}"; do
198 for dir in $PATH; do
199 [[ -z "$dir" ]] && dir=.
200 if [[ -f "$dir/$prog" && -x "$dir/$prog" ]]; then
201 echo "$dir/$prog"
202 IFS="$IFS_SAVE"
203 return 0
204 fi
205 done
206 done
207
208 IFS="$IFS_SAVE"
209 echo "Failed to find on your path: $*" 1>&2
210 echo "See the rsync-ssl manpage for configuration assistance." 1>&2
211 return 1
212 }
213
214 if [[ "$#" == 0 ]]; then
215 echo "Usage: rsync-ssl [--type=SSL_TYPE] RSYNC_ARG [...]" 1>&2
216 echo "The SSL_TYPE can be openssl or stunnel"
217 exit 1
218 fi
219
220 if [[ "$1" = --help || "$1" = -h ]]; then
221 exec rsync --help
222 fi
223
224 if [[ "$1" == --HELPER ]]; then
225 shift
226 rsync_ssl_helper "${@}"
227 fi
228
229 if [[ "$1" == --type=* ]]; then
230 export RSYNC_SSL_TYPE="${1/--type=/}"
231 shift
232 fi
233
234 rsync_ssl_run "${@}"