Debian Codegraph @

manuals/en/misc/stunnel.tex

raw
1 \chapter{Using Stunnel to Encrypt Communications}
2 \label{blb:StunnelChapter}
3 \index[general]{Using Stunnel to Encrypt Communications to Clients}
4
5 Prior to version 1.37, \bacula{} did not have built-in communications encryption.
6 Please see the \bilink{\acs{TLS} chapter}{blb:CommEncryption} if you are using \bacula{}
7 1.37 or greater.
8
9 Without too much effort, it is possible to encrypt the communications
10 between any of the daemons. This chapter will show you how to use
11 \btool{stunnel} to encrypt communications to your client programs. We assume the
12 Director and the Storage daemon are running on one machine that will be called
13 \textbf{server} and the Client or File daemon is running on a different machine
14 called \textbf{client}. Although the details may be slightly different, the same
15 principles apply whether you are encrypting between Unix, Linux, or Win32
16 machines. This example was developed between two Linux machines running
17 stunnel version 4.04-4 on a Red Hat Enterprise 3.0 system.
18
19 \section{Communications Ports Used}
20 \index[general]{Used!Communications Ports}
21 \index[general]{Communications Ports Used}
22
23 First, you must know that with the standard \bacula{} configuration, the Director
24 will contact the File daemon on port 9102. The File daemon then contacts the
25 Storage daemon using the address and port parameters supplied by the Director.
26 The standard port used will be 9103. This is the typical server/client view of
27 the world, the File daemon is a server to the Director (i.e. listens for the
28 Director to contact it), and the Storage daemon is a server to the File
29 daemon.
30
31 \section{Encryption}
32 \index[general]{Encryption}
33
34 The encryption is accomplished between the Director and the File daemon by
35 using an stunnel on the Director's machine (server) to encrypt the data and to
36 contact an stunnel on the File daemon's machine (client), which decrypts the
37 data and passes it to the client.
38
39 Between the File daemon and the Storage daemon, we use an stunnel on the File
40 daemon's machine to encrypt the data and another stunnel on the Storage
41 daemon's machine to decrypt the data.
42
43 As a consequence, there are actually four copies of stunnel running, two on the
44 server and two on the client. This may sound a bit complicated, but it really
45 isn't. To accomplish this, we will need to construct four separate conf files
46 for stunnel, and we will need to make some minor modifications to the
47 Director's conf file. None of the other conf files need to be changed.
48
49 \section{A Picture}
50 \index[general]{Picture}
51
52 Since pictures usually help a lot, here is an overview of what we will be
53 doing. Don't worry about all the details of the port numbers and such for the
54 moment.
55
56 \begin{bVerbatim}
57 File daemon (client):
58 stunnel-fd1.conf
59 |===========|
60 Port 29102 >----| Stunnel 1 |-----> Port 9102
61 |===========|
62 stunnel-fd2.conf
63 |===========|
64 Port 9103 >----| Stunnel 2 |-----> server:29103
65 |===========|
66 Director (server):
67 stunnel-dir.conf
68 |===========|
69 Port 29102 >----| Stunnel 3 |-----> client:29102
70 |===========|
71 stunnel-sd.conf
72 |===========|
73 Port 29103 >----| Stunnel 4 |-----> 9103
74 |===========|
75 \end{bVerbatim}
76
77 \section{Certificates}
78 \index[general]{Certificates}
79
80 In order for stunnel to function as a server, which it does in our diagram for
81 Stunnel 1 and Stunnel 4, you must have a certificate and the key. It is
82 possible to keep the two in separate files, but normally, you keep them in one
83 single \bfilename{.pem} file. You may create this certificate yourself in which case, it
84 will be self-signed, or you may have it signed by a \ac{CA}.
85
86 If you want your clients to verify that the server is in fact valid (Stunnel 2
87 and Stunnel 3), you will need to have the server certificates signed by a \ac{CA}
88 , and you will need to have the CA's public certificate (contains the \ac{CA}'s public key).
89
90 Having a \ac{CA} signed certificate is \textbf{highly} recommended if you are using
91 your client across the Internet, otherwise you are exposed to the man in the
92 middle attack and hence loss of your data.
93
94 See below for how to create a self-signed certificate.
95
96 \section{Securing the Data Channel}
97 \index[general]{Channel!Securing the Data}
98 \index[general]{Securing the Data Channel}
99
100 To simplify things a bit, let's for the moment consider only the data channel.
101 That is the connection between the File daemon and the Storage daemon, which
102 takes place on port 9103. In fact, in a minimalist solution, this is the only
103 connection that needs to be encrypted, because it is the one that transports your
104 data. The connection between the Director and the File daemon is simply a
105 control channel used to start the job and get the job status.
106
107 Normally the File daemon will contact the Storage daemon on port 9103
108 (supplied by the Director), so we need an stunnel that listens on port 9103 on
109 the File daemon's machine, encrypts the data and sends it to the Storage
110 daemon. This is depicted by Stunnel 2 above. Note that this stunnel is
111 listening on port 9103 and sending to server:29103. We use port 29103 on the
112 server because if we would send the data to port 9103, it would go directly to the
113 Storage daemon, which doesn't understand encrypted data. On the server
114 machine, we run Stunnel 4, which listens on port 29103, decrypts the data and
115 sends it to the Storage daemon, which is listening on port 9103.
116
117 \section{Data Channel Configuration}
118 \index[general]{Modification of bacula-dir.conf for the Data Channel}
119 \index[general]{baculoa-dir.conf!Modification for the Data Channel}
120
121 The Storage resource of the \bfilename{bacula-dir.conf} normally looks something like the
122 following:
123
124 \begin{bVerbatim}
125 Storage {
126 Name = File
127 Address = server
128 SDPort = 9103
129 Password = storage_password
130 Device = File
131 Media Type = File
132 }
133 \end{bVerbatim}
134
135 Notice that this is running on the server machine, and it points the File
136 daemon back to server:9103, which is where our Storage daemon is listening. We
137 modify this to be:
138
139 \begin{bVerbatim}
140 Storage {
141 Name = File
142 Address = localhost
143 SDPort = 9103
144 Password = storage_password
145 Device = File
146 Media Type = File
147 }
148 \end{bVerbatim}
149
150 This causes the File daemon to send the data to the stunnel running on
151 \texttt{localhost} (the client machine). We could have used client as the address as
152 well.
153
154 \section{Stunnel Configuration for the Data Channel}
155 \index[general]{Stunnel Configuration for the Data Channel}
156
157 In the diagram above, we see above Stunnel 2 that we use \bfilename{stunnel-fd2.conf} on the
158 client. A pretty much minimal config file would look like the following:
159
160 \begin{bVerbatim}
161 client = yes
162 [29103]
163 accept = localhost:9103
164 connect = server:29103
165 \end{bVerbatim}
166
167 The above config file does encrypt the data but it does not require a
168 certificate, so it is subject to the man in the middle attack. The file I
169 actually used, \bfilename{stunnel-fd2.conf}, looked like this:
170
171 \begin{bVerbatim}
172 #
173 # Stunnel conf for Bacula client -> SD
174 #
175 pid = /home/kern/bacula/bin/working/stunnel.pid
176 #
177 # A cert is not mandatory here. If verify=2, a
178 # cert signed by a CA must be specified, and
179 # either CAfile or CApath must point to the CA's
180 # cert
181 #
182 cert = /home/kern/stunnel/stunnel.pem
183 CAfile = /home/kern/ssl/cacert.pem
184 verify = 2
185 client = yes
186 # debug = 7
187 # foreground = yes
188 [29103]
189 accept = localhost:9103
190 connect = server:29103
191 \end{bVerbatim}
192
193 You will notice that I specified a pid file location because I ran stunnel
194 under my own userid so I could not use the default, which requires root
195 permission. I also specified a certificate that I have as well as verify level
196 2 so that the certificate is required and verified, and I must supply the
197 location of the \ac{CA} certificate so that the stunnel
198 certificate can be verified. Finally, you will see that there are two lines
199 commented out, which when enabled, produce a lot of nice debug info in the
200 command window.
201
202 If you do not have a signed certificate (\bfilename{stunnel.pem}), you need to delete the
203 cert, \ac{CA}file, and verify lines.
204
205 Note that the \bfilename{stunnel.pem}, is actually a private key and a certificate in a
206 single file. These two can be kept and specified individually, but keeping
207 them in one file is more convenient.
208
209 The config file, stunnel-sd.conf, needed for Stunnel 4 on the server machine
210 is:
211
212 \begin{bVerbatim}
213 #
214 # Bacula stunnel conf for Storage daemon
215 #
216 pid = /home/kern/bacula/bin/working/stunnel.pid
217 #
218 # A cert is mandatory here, it may be self signed
219 # If it is self signed, the client may not use
220 # verify
221 #
222 cert = /home/kern/stunnel/stunnel.pem
223 client = no
224 # debug = 7
225 # foreground = yes
226 [29103]
227 accept = 29103
228 connect = 9103
229 \end{bVerbatim}
230
231 \section{Starting and Testing the Data Encryption}
232 \index[general]{Starting and Testing the Data Encryption}
233 \index[general]{Encryption!Starting and Testing the Data}
234
235 It will most likely be the simplest to implement the Data Channel encryption
236 in the following order:
237
238 \begin{bitemize}
239 \item Setup and run \bacula{} backing up some data on your client machine
240 without encryption.
241 \item Stop \bacula{}.
242 \item Modify the Storage resource in the Director's conf file.
243 \item Start \bacula{}
244 \item Start \btool{stunnel} on the server with:
245
246 \begin{bVerbatim}
247 stunnel stunnel-sd.conf
248 \end{bVerbatim}
249
250 \item Start \btool{stunnel} on the client with:
251
252 \begin{bVerbatim}
253 stunnel stunnel-fd2.conf
254 \end{bVerbatim}
255
256 \item Run a job.
257 \item If it doesn't work, turn debug on in both stunnel conf files, restart
258 the stunnels, rerun the job, repeat until it works.
259 \end{bitemize}
260
261 \section{Encrypting the Control Channel}
262 \index[general]{Channel!Encrypting the Control}
263 \index[general]{Encrypting the Control Channel}
264
265 The Job control channel is between the Director and the File daemon, and as
266 mentioned above, it is not really necessary to encrypt, but it is good
267 practice to encrypt it as well. The two stunnels that are used in this case
268 will be Stunnel 1 and Stunnel 3 in the diagram above. Stunnel 3 on the server
269 might normally listen on port 9102, but if you have a local File daemon, this
270 will not work, so we make it listen on port 29102. It then sends the data to
271 client:29102. Again we use port 29102 so that the stunnel on the client
272 machine can decrypt the data before passing it on to port 9102 where the File
273 daemon is listening.
274
275 \section{Control Channel Configuration}
276 \index[general]{Control Channel Configuration}
277
278 We need to modify the standard Client resource, which would normally look
279 something like:
280
281 \begin{bVerbatim}
282 Client {
283 Name = client-fd
284 Address = client
285 FDPort = 9102
286 Catalog = BackupDB
287 Password = "xxx"
288 }
289 \end{bVerbatim}
290
291 to be:
292
293 \begin{bVerbatim}
294 Client {
295 Name = client-fd
296 Address = localhost
297 FDPort = 29102
298 Catalog = BackupDB
299 Password = "xxx"
300 }
301 \end{bVerbatim}
302
303 This will cause the Director to send the control information to
304 localhost:29102 instead of directly to the client.
305
306 \section{Stunnel Configuration for the Control Channel}
307 \index[general]{Config Files for stunnel to Encrypt the Control Channel}
308
309 The stunnel config file, \bfilename{stunnel-dir.conf}, for the Director's machine would
310 look like the following:
311
312 \begin{bVerbatim}
313 #
314 # Bacula stunnel conf for the Directory to contact a client
315 #
316 pid = /home/kern/bacula/bin/working/stunnel.pid
317 #
318 # A cert is not mandatory here. If verify=2, a
319 # cert signed by a CA must be specified, and
320 # either CAfile or CApath must point to the CA's
321 # cert
322 #
323 cert = /home/kern/stunnel/stunnel.pem
324 CAfile = /home/kern/ssl/cacert.pem
325 verify = 2
326 client = yes
327 # debug = 7
328 # foreground = yes
329 [29102]
330 accept = localhost:29102
331 connect = client:29102
332 \end{bVerbatim}
333
334 and the config file, \bfilename{stunnel-fd1.conf}, needed to run stunnel on the Client
335 would be:
336
337 \begin{bVerbatim}
338 #
339 # Bacula stunnel conf for the Directory to contact a client
340 #
341 pid = /home/kern/bacula/bin/working/stunnel.pid
342 #
343 # A cert is not mandatory here. If verify=2, a
344 # cert signed by a CA must be specified, and
345 # either CAfile or CApath must point to the CA's
346 # cert
347 #
348 cert = /home/kern/stunnel/stunnel.pem
349 CAfile = /home/kern/ssl/cacert.pem
350 verify = 2
351 client = yes
352 # debug = 7
353 # foreground = yes
354 [29102]
355 accept = localhost:29102
356 connect = client:29102
357 \end{bVerbatim}
358
359 \section{Starting and Testing the Control Channel}
360 \index[general]{Starting and Testing the Control Channel}
361 \index[general]{Channel!Starting and Testing the Control}
362
363 It will most likely be the simplest to implement the Control Channel
364 encryption in the following order:
365
366 \begin{bitemize}
367 \item Stop \bacula{}.
368 \item Modify the Client resource in the Director's conf file.
369 \item Start \bacula{}
370 \item Start \btool{stunnel} on the server with:
371
372 \begin{bVerbatim}
373 stunnel stunnel-dir.conf
374 \end{bVerbatim}
375
376 \item Start \btool{stunnel} on the client with:
377
378 \begin{bVerbatim}
379 stunnel stunnel-fd1.conf
380 \end{bVerbatim}
381
382 \item Run a job.
383 \item If it doesn't work, turn debug on in both stunnel conf files, restart
384 the stunnels, rerun the job, repeat until it works.
385 \end{bitemize}
386
387 \section{Using stunnel to Encrypt to a Second Client}
388 \index[general]{Using stunnel to Encrypt to a Second Client}
389 \index[general]{Client!Using stunnel to Encrypt to a Second}
390
391 On the client machine, you can just duplicate the setup that you have on the
392 first client file for file and it should work fine.
393
394 In the \bfilename{bacula-dir.conf} file, you will want to create a second client pretty
395 much identical to how you did for the first one, but the port number must be
396 unique. We previously used:
397
398 \begin{bVerbatim}
399 Client {
400 Name = client-fd
401 Address = localhost
402 FDPort = 29102
403 Catalog = BackupDB
404 Password = "xxx"
405 }
406 \end{bVerbatim}
407
408 so for the second client, we will, of course, have a different name, and we
409 will also need a different port. Remember that we used port 29103 for the
410 Storage daemon, so for the second client, we can use port 29104, and the
411 Client resource would look like:
412
413 \begin{bVerbatim}
414 Client {
415 Name = client2-fd
416 Address = localhost
417 FDPort = 29104
418 Catalog = BackupDB
419 Password = "yyy"
420 }
421 \end{bVerbatim}
422
423 Now, fortunately, we do not need a third stunnel to on the Director's machine,
424 we can just add the new port to the config file, \bfilename{stunnel-dir.conf}, to make:
425
426 \begin{bVerbatim}
427 #
428 # Bacula stunnel conf for the Directory to contact a client
429 #
430 pid = /home/kern/bacula/bin/working/stunnel.pid
431 #
432 # A cert is not mandatory here. If verify=2, a
433 # cert signed by a CA must be specified, and
434 # either CAfile or CApath must point to the CA's
435 # cert
436 #
437 cert = /home/kern/stunnel/stunnel.pem
438 CAfile = /home/kern/ssl/cacert.pem
439 verify = 2
440 client = yes
441 # debug = 7
442 # foreground = yes
443 [29102]
444 accept = localhost:29102
445 connect = client:29102
446 [29104]
447 accept = localhost:29102
448 connect = client2:29102
449 \end{bVerbatim}
450
451 There are no changes necessary to the Storage daemon or the other stunnel so
452 that this new client can talk to our Storage daemon.
453
454 \section{Creating a Self-signed Certificate}
455 \index[general]{Creating a Self-signed Certificate}
456 \index[general]{Certificate!Creating a Self-signed}
457
458 You may create a self-signed certificate for use with stunnel that will permit
459 you to make it function, but will not allow certificate validation. The \bfilename{.pem}
460 file containing both the certificate and the key can be made with the
461 following, which I put in a file named \bfilename{makepem}:
462
463 \begin{bVerbatim}
464 #!/bin/sh
465 #
466 # Simple shell script to make a .pem file that can be used
467 # with stunnel and Bacula
468 #
469 OPENSSL=openssl
470 umask 77
471 PEM1="/bin/mktemp openssl.XXXXXX"
472 PEM2="/bin/mktemp openssl.XXXXXX"
473 ${OPENSSL} req -newkey rsa:1024 -keyout $PEM1 -nodes \
474 -x509 -days 365 -out $PEM2
475 cat $PEM1 > stunnel.pem
476 echo "" >>stunnel.pem
477 cat $PEM2 >>stunnel.pem
478 rm $PEM1 $PEM2
479 \end{bVerbatim}
480
481 The above script will ask you a number of questions. You may simply answer
482 each of them by entering a return, or if you wish you may enter your own data.
483
484
485 \section{Getting a \acs{CA} Signed Certificate}
486 \index[general]{Certificate!Getting a \ac{CA} Signed}
487 \index[general]{Getting a \ac{CA} Signed Certificate}
488
489 The process of getting a certificate that is signed by a \ac{CA} is quite a bit
490 more complicated. You can purchase one from quite a number of \acs{PKI} vendors, but
491 that is not at all necessary for use with \bacula{}.
492
493 To get a \ac{CA} signed
494 certificate, you will either need to find a friend that has setup his own \ac{CA}
495 or to become a \ac{CA} yourself, and thus you can sign all your own certificates.
496 The \bfootref{http://ospkibook.sourceforge.net/docs/OSPKI-2.4.7/OSPKI-html/ospki-book.htm}{book OpenSSL by John Viega, Matt Mesier \& Pravir Chandra} from O'Reilly
497 explains how to do it, or you can read the documentation provided in the
498 Open-source \acs{PKI} Book project at Source Forge.
499
500 Note, the link may change.
501
502 \section{Using ssh to Secure the Communications}
503 \index[general]{Communications!Using ssh to Secure the}
504 \index[general]{Using ssh to Secure the Communications}
505
506 Please see the script \btool{ssh-tunnel.sh} in the \bdirectoryname{examples} directory. It
507 was contributed by Stephan Holl.