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