]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/console/authenticate.cpp
Begin adding TLS support to bat.
[bacula/bacula] / bacula / src / qt-console / console / authenticate.cpp
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2001-2007 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version two of the GNU General Public
10    License as published by the Free Software Foundation plus additions
11    that are listed in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of John Walker.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28
29 /*
30  *
31  *   Bacula UA authentication. Provides authentication with
32  *     the Director.
33  *
34  *     Kern Sibbald, June MMI   adapted to bat, Jan MMVI
35  *
36  *     Version $Id$
37  *
38  */
39
40
41 #include "bat.h"
42
43
44 /* Commands sent to Director */
45 static char hello[]    = "Hello %s calling\n";
46
47 /* Response from Director */
48 static char OKhello[]   = "1000 OK:";
49
50 /* Forward referenced functions */
51
52 /*
53  * Authenticate Director
54  */
55 bool Console::authenticate_director(JCR *jcr, DIRRES *director, CONRES *cons)
56 {
57    BSOCK *dir = jcr->dir_bsock;
58    int tls_local_need = BNET_TLS_NONE;
59    int tls_remote_need = BNET_TLS_NONE;
60    int compatible = true;
61    char bashed_name[MAX_NAME_LENGTH];
62    char *password;
63    TLS_CONTEXT *tls_ctx = NULL;
64
65    /*
66     * Send my name to the Director then do authentication
67     */
68    if (cons) {
69       bstrncpy(bashed_name, cons->hdr.name, sizeof(bashed_name));
70       bash_spaces(bashed_name);
71       password = cons->password;
72       /* TLS Requirement */
73       if (cons->tls_enable) {
74          if (cons->tls_require) {
75             tls_local_need = BNET_TLS_REQUIRED;
76          } else {
77             tls_local_need = BNET_TLS_OK;
78          }
79       }
80
81       tls_ctx = cons->tls_ctx;
82    } else {
83       bstrncpy(bashed_name, "*UserAgent*", sizeof(bashed_name));
84       password = director->password;
85       /* TLS Requirement */
86       if (director->tls_enable) {
87          if (director->tls_require) {
88             tls_local_need = BNET_TLS_REQUIRED;
89          } else {
90             tls_local_need = BNET_TLS_OK;
91          }
92       }
93
94       tls_ctx = director->tls_ctx;
95    }
96    /* Timeout Hello after 30 secs */
97    btimer_t *tid = start_bsock_timer(dir, 30);
98    dir->fsend(hello, bashed_name);
99
100    /* respond to Dir challenge */
101    if (!cram_md5_respond(dir, password, &tls_remote_need, &compatible) ||
102        /* Now challenge dir */
103        !cram_md5_challenge(dir, password, tls_local_need, compatible)) {
104       printf(_("%s: Director authorization problem.\n"), my_name);
105       display_text(_("Director authorization problem.\n"));    
106       goto bail_out;
107    }
108
109    /* Verify that the remote host is willing to meet our TLS requirements */
110    if (tls_remote_need < tls_local_need && tls_local_need != BNET_TLS_OK && tls_remote_need != BNET_TLS_OK) {
111       display_text(_("Authorization problem:"
112              " Remote server did not advertise required TLS support.\n"));
113       goto bail_out;
114    }
115
116    /* Verify that we are willing to meet the remote host's requirements */
117    if (tls_remote_need > tls_local_need && tls_local_need != BNET_TLS_OK && tls_remote_need != BNET_TLS_OK) {
118       display_text(_("Authorization problem:"
119                      " Remote server requires TLS.\n"));
120       goto bail_out;
121    }
122
123    /* Is TLS Enabled? */
124    if (have_tls) {
125       if (tls_local_need >= BNET_TLS_OK && tls_remote_need >= BNET_TLS_OK) {
126          /* Engage TLS! Full Speed Ahead! */
127          if (!bnet_tls_client(tls_ctx, dir, NULL)) {
128             display_text(_("TLS negotiation failed\n"));
129             goto bail_out;
130          }
131       }
132    }
133
134    Dmsg1(6, ">dird: %s", dir->msg);
135    if (dir->recv() <= 0) {
136       stop_bsock_timer(tid);
137       display_textf(_("Bad response to Hello command: ERR=%s\n"),
138          dir->bstrerror());
139       printf(_("%s: Bad response to Hello command: ERR=%s\n"),
140          my_name, dir->bstrerror());
141       display_text(_("The Director is probably not running.\n"));
142       return false;
143    }
144
145   stop_bsock_timer(tid);
146    Dmsg1(10, "<dird: %s", dir->msg);
147    if (strncmp(dir->msg, OKhello, sizeof(OKhello)-1) != 0) {
148       display_text(_("Director rejected Hello command\n"));    
149       return false;
150    } else {
151       display_text(dir->msg);
152    }
153    return true;
154
155 bail_out:
156    stop_bsock_timer(tid);
157    display_text(_("Director authorization problem.\n"
158              "Most likely the passwords do not agree.\n"
159              "If you are using TLS, there may have been a certificate validation error during the TLS handshake.\n"
160              "Please see http://www.bacula.org/rel-manual/faq.html#AuthorizationErrors for help.\n"));
161    return false;
162 }