]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/wx-console/authenticate.c
Change Groupbox name to Remote Director in Configuration page if Director is not...
[bacula/bacula] / bacula / src / wx-console / authenticate.c
1 /*
2  *
3  *   Bacula UA authentication. Provides authentication with
4  *     the Director.
5  *
6  *     Kern Sibbald, June MMI
7  *
8  *    This routine runs as a thread and must be thread reentrant.
9  *
10  *  Basic tasks done here:
11  *
12  */
13 /*
14    Copyright (C) 2001-2005 Kern Sibbald
15
16    This program is free software; you can redistribute it and/or
17    modify it under the terms of the GNU General Public License
18    version 2 as amended with additional clauses defined in the
19    file LICENSE in the main source directory.
20
21    This program is distributed in the hope that it will be useful,
22    but WITHOUT ANY WARRANTY; without even the implied warranty of
23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
24    the file LICENSE for additional details.
25
26  */
27
28 /* _("...") macro returns a wxChar*, so if we need a char*, we need to convert it with:
29  * wxString(_("...")).mb_str(*wxConvCurrent) */
30
31 /*  Windows debug builds set _DEBUG which is used by wxWidgets to select their
32  *  debug memory allocator.  Unfortunately it conflicts with Bacula's SmartAlloc.
33  * So we turn _DEBUG off since we aren't interested in things it enables.
34  */
35
36 #undef _DEBUG
37
38 #include "bacula.h"
39 #include "console_conf.h"
40 #include "jcr.h"
41
42 #include <wx/intl.h>
43
44 #include "csprint.h"
45
46 void senditf(char *fmt, ...);
47 void sendit(char *buf);
48
49 /* Commands sent to Director */
50 static char hello[]    = "Hello %s calling\n";
51
52 /* Response from Director */
53 static char OKhello[]   = "1000 OK:";
54
55 /* Forward referenced functions */
56
57 /*
58  * Authenticate Director
59  */
60 int authenticate_director(JCR *jcr, DIRRES *director, CONRES *cons)
61 {
62    BSOCK *dir = jcr->dir_bsock;
63    int tls_local_need = BNET_TLS_NONE;
64    int tls_remote_need = BNET_TLS_NONE;
65    int compatible = true;
66    char bashed_name[MAX_NAME_LENGTH];
67    char *password;
68    TLS_CONTEXT *tls_ctx = NULL;
69
70    /*
71     * Send my name to the Director then do authentication
72     */
73    if (cons) {
74       bstrncpy(bashed_name, cons->hdr.name, sizeof(bashed_name));
75       bash_spaces(bashed_name);
76       password = cons->password;
77       /* TLS Requirement */
78       if (cons->tls_enable) {
79          if (cons->tls_require) {
80             tls_local_need = BNET_TLS_REQUIRED;
81          } else {
82             tls_local_need = BNET_TLS_OK;
83          }
84       }
85
86       tls_ctx = cons->tls_ctx;
87    } else {
88       bstrncpy(bashed_name, "*UserAgent*", sizeof(bashed_name));
89       password = director->password;
90       /* TLS Requirement */
91       if (director->tls_enable) {
92          if (director->tls_require) {
93             tls_local_need = BNET_TLS_REQUIRED;
94          } else {
95             tls_local_need = BNET_TLS_OK;
96          }
97       }
98
99       tls_ctx = director->tls_ctx;
100    }
101
102
103    /* Timeout Hello after 5 mins */
104    btimer_t *tid = start_bsock_timer(dir, 60 * 5);
105    bnet_fsend(dir, hello, bashed_name);
106
107    if (!cram_md5_respond(dir, password, &tls_remote_need, &compatible) ||
108        !cram_md5_challenge(dir, password, tls_local_need, compatible)) {
109       goto bail_out;
110    }
111
112    /* Verify that the remote host is willing to meet our TLS requirements */
113    if (tls_remote_need < tls_local_need && tls_local_need != BNET_TLS_OK && tls_remote_need != BNET_TLS_OK) {
114       csprint(_("Authorization problem: Remote server did not advertise required TLS support.\n"));
115       goto bail_out;
116    }
117
118    /* Verify that we are willing to meet the remote host's requirements */
119    if (tls_remote_need > tls_local_need && tls_local_need != BNET_TLS_OK && tls_remote_need != BNET_TLS_OK) {
120       csprint(_("Authorization problem: Remote server requires TLS.\n"));
121       goto bail_out;
122    }
123
124    /* Is TLS Enabled? */
125    if (have_tls) {
126       if (tls_local_need >= BNET_TLS_OK && tls_remote_need >= BNET_TLS_OK) {
127          /* Engage TLS! Full Speed Ahead! */
128          if (!bnet_tls_client(tls_ctx, dir)) {
129             csprint(_("TLS negotiation failed\n"));
130             goto bail_out;
131          }
132       }
133    }
134
135    Dmsg1(6, ">dird: %s", dir->msg);
136    if (bnet_recv(dir) <= 0) {
137       csprint(_("Bad response to Hello command: ERR="), CS_DATA);
138       csprint(bnet_strerror(dir), CS_DATA);
139       csprint("\n", CS_DATA);
140       goto bail_out;
141    }
142    Dmsg1(10, "<dird: %s", dir->msg);
143    if (strncmp(dir->msg, OKhello, sizeof(OKhello)-1) != 0) {
144       csprint(_("Director rejected Hello command\n"), CS_DATA);
145       goto bail_out;
146    } else {
147       csprint(dir->msg, CS_DATA);
148    }
149    stop_bsock_timer(tid);
150    return 1;
151
152 bail_out:
153    stop_bsock_timer(tid);
154    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/rel-manual/faq.html#AuthorizationErrors for help.\n"));
155    return 0;
156
157 }