]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/bcomm/dircomm_auth.cpp
Oops. When I changed the contents of the field earlier, I forgot to make this change
[bacula/bacula] / bacula / src / qt-console / bcomm / dircomm_auth.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 and included
11    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 #include "dircomm.h"
43
44
45 /* Commands sent to Director */
46 static char hello[]    = "Hello %s calling\n";
47
48 /* Response from Director */
49 static char OKhello[]   = "1000 OK:";
50
51 /* Forward referenced functions */
52
53 /*
54  * Authenticate Director
55  */
56 bool DirComm::authenticate_director(JCR *jcr, DIRRES *director, CONRES *cons, 
57                 char *errmsg, int errmsg_len) 
58 {
59    BSOCK *dir = jcr->dir_bsock;
60    int tls_local_need = BNET_TLS_NONE;
61    int tls_remote_need = BNET_TLS_NONE;
62    int compatible = true;
63    char bashed_name[MAX_NAME_LENGTH];
64    char *password;
65    TLS_CONTEXT *tls_ctx = NULL;
66
67    errmsg[0] = 0;
68    /*
69     * Send my name to the Director then do authentication
70     */
71    if (cons) {
72       bstrncpy(bashed_name, cons->hdr.name, sizeof(bashed_name));
73       bash_spaces(bashed_name);
74       password = cons->password;
75       /* TLS Requirement */
76       if (cons->tls_enable) {
77          if (cons->tls_require) {
78             tls_local_need = BNET_TLS_REQUIRED;
79          } else {
80             tls_local_need = BNET_TLS_OK;
81          }
82       }
83       tls_ctx = cons->tls_ctx;
84    } else {
85       bstrncpy(bashed_name, "*UserAgent*", sizeof(bashed_name));
86       password = director->password;
87       /* TLS Requirement */
88       if (director->tls_enable) {
89          if (director->tls_require) {
90             tls_local_need = BNET_TLS_REQUIRED;
91          } else {
92             tls_local_need = BNET_TLS_OK;
93          }
94       }
95
96       tls_ctx = director->tls_ctx;
97    }
98    /* Timeout Hello after 15 secs */
99    dir->start_timer(15);
100    dir->fsend(hello, bashed_name);
101
102    /* respond to Dir challenge */
103    if (!cram_md5_respond(dir, password, &tls_remote_need, &compatible) ||
104        /* Now challenge dir */
105        !cram_md5_challenge(dir, password, tls_local_need, compatible)) {
106       bsnprintf(errmsg, errmsg_len, _("Director authorization problem at \"%s:%d\"\n"),
107          dir->host(), dir->port());
108       goto bail_out;
109    }
110
111    /* Verify that the remote host is willing to meet our TLS requirements */
112    if (tls_remote_need < tls_local_need && tls_local_need != BNET_TLS_OK && tls_remote_need != BNET_TLS_OK) {
113       bsnprintf(errmsg, errmsg_len, _("Authorization problem:"
114              " Remote server at \"%s:%d\" did not advertise required TLS support.\n"),
115              dir->host(), dir->port());
116       goto bail_out;
117    }
118
119    /* Verify that we are willing to meet the remote host's requirements */
120    if (tls_remote_need > tls_local_need && tls_local_need != BNET_TLS_OK && tls_remote_need != BNET_TLS_OK) {
121       bsnprintf(errmsg, errmsg_len, _("Authorization problem with Director at \"%s:%d\":"
122                      " Remote server requires TLS.\n"),
123                      dir->host(), dir->port());
124
125       goto bail_out;
126    }
127
128    /* Is TLS Enabled? */
129    if (have_tls) {
130       if (tls_local_need >= BNET_TLS_OK && tls_remote_need >= BNET_TLS_OK) {
131          /* Engage TLS! Full Speed Ahead! */
132          if (!bnet_tls_client(tls_ctx, dir, NULL)) {
133             bsnprintf(errmsg, errmsg_len, _("TLS negotiation failed with Director at \"%s:%d\"\n"),
134                dir->host(), dir->port());
135             goto bail_out;
136          }
137       }
138    }
139
140    Dmsg1(6, ">dird: %s", dir->msg);
141    if (dir->recv() <= 0) {
142       dir->stop_timer();
143       bsnprintf(errmsg, errmsg_len, _("Bad response to Hello command: ERR=%s\n"
144                       "The Director at \"%s:%d\" is probably not running.\n"),
145                     dir->bstrerror(), dir->host(), dir->port());
146       return false;
147    }
148
149    dir->stop_timer();
150    Dmsg1(10, "<dird: %s", dir->msg);
151    if (strncmp(dir->msg, OKhello, sizeof(OKhello)-1) != 0) {
152       bsnprintf(errmsg, errmsg_len, _("Director at \"%s:%d\" rejected Hello command\n"),
153          dir->host(), dir->port());
154       return false;
155    } else {
156       bsnprintf(errmsg, errmsg_len, "%s", dir->msg);
157    }
158    return true;
159
160 bail_out:
161    dir->stop_timer();
162    bsnprintf(errmsg, errmsg_len, _("Authorization problem with Director at \"%s:%d\"\n"
163              "Most likely the passwords do not agree.\n"
164              "If you are using TLS, there may have been a certificate validation error during the TLS handshake.\n"
165              "Please see http://www.bacula.org/rel-manual/faq.html#AuthorizationErrors for help.\n"), 
166              dir->host(), dir->port());
167    return false;
168 }