]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/wx-console/authenticate.c
Tweak fix MySQL quoting again :-(
[bacula/bacula] / bacula / src / wx-console / authenticate.c
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 three of the GNU Affero 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 Affero 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 Kern Sibbald.
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  *   Bacula UA authentication. Provides authentication with
31  *     the Director.
32  *
33  *     Kern Sibbald, June MMI
34  *
35  *    This routine runs as a thread and must be thread reentrant.
36  *
37  *  Basic tasks done here:
38  *
39  */
40
41 /* _("...") macro returns a wxChar*, so if we need a char*, we need to convert it with:
42  * wxString(_("...")).mb_str(*wxConvCurrent) */
43
44 /*  Windows debug builds set _DEBUG which is used by wxWidgets to select their
45  *  debug memory allocator.  Unfortunately it conflicts with Bacula's SmartAlloc.
46  * So we turn _DEBUG off since we aren't interested in things it enables.
47  */
48
49 #undef _DEBUG
50
51 #include "bacula.h"
52 #include "console_conf.h"
53 #include "jcr.h"
54
55 #include <wx/intl.h>
56
57 #include "csprint.h"
58
59 void senditf(char *fmt, ...);
60 void sendit(char *buf);
61
62 /* Commands sent to Director */
63 static char hello[]    = "Hello %s calling\n";
64
65 /* Response from Director */
66 static char OKhello[]   = "1000 OK:";
67
68 /* Forward referenced functions */
69
70 /*
71  * Authenticate Director
72  */
73 int authenticate_director(JCR *jcr, DIRRES *director, CONRES *cons)
74 {
75    BSOCK *dir = jcr->dir_bsock;
76    int tls_local_need = BNET_TLS_NONE;
77    int tls_remote_need = BNET_TLS_NONE;
78    int compatible = true;
79    char bashed_name[MAX_NAME_LENGTH];
80    char *password;
81    TLS_CONTEXT *tls_ctx = NULL;
82
83    /*
84     * Send my name to the Director then do authentication
85     */
86    if (cons) {
87       bstrncpy(bashed_name, cons->hdr.name, sizeof(bashed_name));
88       bash_spaces(bashed_name);
89       password = cons->password;
90       /* TLS Requirement */
91       if (cons->tls_enable) {
92          if (cons->tls_require) {
93             tls_local_need = BNET_TLS_REQUIRED;
94          } else {
95             tls_local_need = BNET_TLS_OK;
96          }
97       }
98
99       tls_ctx = cons->tls_ctx;
100    } else {
101       bstrncpy(bashed_name, "*UserAgent*", sizeof(bashed_name));
102       password = director->password;
103       /* TLS Requirement */
104       if (director->tls_enable) {
105          if (director->tls_require) {
106             tls_local_need = BNET_TLS_REQUIRED;
107          } else {
108             tls_local_need = BNET_TLS_OK;
109          }
110       }
111
112       tls_ctx = director->tls_ctx;
113    }
114
115
116    /* Timeout Hello after 5 mins */
117    btimer_t *tid = start_bsock_timer(dir, 60 * 5);
118    bnet_fsend(dir, hello, bashed_name);
119
120    if (!cram_md5_respond(dir, password, &tls_remote_need, &compatible) ||
121        !cram_md5_challenge(dir, password, tls_local_need, compatible)) {
122       goto bail_out;
123    }
124
125    /* Verify that the remote host is willing to meet our TLS requirements */
126    if (tls_remote_need < tls_local_need && tls_local_need != BNET_TLS_OK && tls_remote_need != BNET_TLS_OK) {
127       csprint(_("Authorization problem: Remote server did not advertise required TLS support.\n"));
128       goto bail_out;
129    }
130
131    /* Verify that we are willing to meet the remote host's requirements */
132    if (tls_remote_need > tls_local_need && tls_local_need != BNET_TLS_OK && tls_remote_need != BNET_TLS_OK) {
133       csprint(_("Authorization problem: Remote server requires TLS.\n"));
134       goto bail_out;
135    }
136
137    /* Is TLS Enabled? */
138    if (have_tls) {
139       if (tls_local_need >= BNET_TLS_OK && tls_remote_need >= BNET_TLS_OK) {
140          /* Engage TLS! Full Speed Ahead! */
141          if (!bnet_tls_client(tls_ctx, dir, NULL)) {
142             csprint(_("TLS negotiation failed\n"));
143             goto bail_out;
144          }
145       }
146    }
147
148    Dmsg1(6, ">dird: %s", dir->msg);
149    if (bnet_recv(dir) <= 0) {
150       csprint(_("Bad response to Hello command: ERR="), CS_DATA);
151       csprint(bnet_strerror(dir), CS_DATA);
152       csprint("\n", CS_DATA);
153       goto bail_out;
154    }
155    Dmsg1(10, "<dird: %s", dir->msg);
156    if (strncmp(dir->msg, OKhello, sizeof(OKhello)-1) != 0) {
157       csprint(_("Director rejected Hello command\n"), CS_DATA);
158       goto bail_out;
159    } else {
160       csprint(dir->msg, CS_DATA);
161    }
162    stop_bsock_timer(tid);
163    return 1;
164
165 bail_out:
166    stop_bsock_timer(tid);
167    csprint( _("Director authorization problem.\nMost likely the passwords do not agree.\nIf you are using TLS, there may have been a certificate validation error during the TLS handshake.\nPlease see http://www.bacula.org/en/rel-manual/Bacula_Freque_Asked_Questi.html#SECTION003760000000000000000 for help.\n"));
168    return 0;
169
170 }