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