]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/wx-console/authenticate.c
======================= Warning ==========================
[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 #include "bacula.h"
32 #include "console_conf.h"
33 #include "jcr.h"
34
35 #include <wx/intl.h>
36
37 #include "csprint.h"
38
39 void senditf(char *fmt, ...);
40 void sendit(char *buf);
41
42 /* Commands sent to Director */
43 static char hello[]    = "Hello %s calling\n";
44
45 /* Response from Director */
46 static char OKhello[]   = "1000 OK:";
47
48 /* Forward referenced functions */
49
50 /*
51  * Authenticate Director
52  */
53 int authenticate_director(JCR *jcr, DIRRES *director, CONRES *cons)
54 {
55    BSOCK *dir = jcr->dir_bsock;
56    int tls_local_need = BNET_TLS_NONE;
57    int tls_remote_need = BNET_TLS_NONE;
58    int compatible = true;
59    char bashed_name[MAX_NAME_LENGTH];
60    char *password;
61    TLS_CONTEXT *tls_ctx = NULL;
62
63    /*
64     * Send my name to the Director then do authentication
65     */
66    if (cons) {
67       bstrncpy(bashed_name, cons->hdr.name, sizeof(bashed_name));
68       bash_spaces(bashed_name);
69       password = cons->password;
70       /* TLS Requirement */
71       if (cons->tls_enable) {
72          if (cons->tls_require) {
73             tls_local_need = BNET_TLS_REQUIRED;
74          } else {
75             tls_local_need = BNET_TLS_OK;
76          }
77       }
78
79       tls_ctx = cons->tls_ctx;
80    } else {
81       bstrncpy(bashed_name, "*UserAgent*", sizeof(bashed_name));
82       password = director->password;
83       /* TLS Requirement */
84       if (director->tls_enable) {
85          if (director->tls_require) {
86             tls_local_need = BNET_TLS_REQUIRED;
87          } else {
88             tls_local_need = BNET_TLS_OK;
89          }
90       }
91
92       tls_ctx = director->tls_ctx;
93    }
94
95
96    /* Timeout Hello after 5 mins */
97    btimer_t *tid = start_bsock_timer(dir, 60 * 5);
98    bnet_fsend(dir, hello, bashed_name);
99
100    if (!cram_md5_respond(dir, password, &tls_remote_need, &compatible) ||
101        !cram_md5_challenge(dir, password, tls_local_need, compatible)) {
102       goto bail_out;
103    }       
104
105    /* Verify that the remote host is willing to meet our TLS requirements */
106    if (tls_remote_need < tls_local_need && tls_local_need != BNET_TLS_OK && tls_remote_need != BNET_TLS_OK) {
107       csprint(_("Authorization problem: Remote server did not advertise required TLS support.\n"));
108       goto bail_out;
109    }
110
111    /* Verify that we are willing to meet the remote host's requirements */
112    if (tls_remote_need > tls_local_need && tls_local_need != BNET_TLS_OK && tls_remote_need != BNET_TLS_OK) {
113       csprint(_("Authorization problem: Remote server requires TLS.\n"));
114       goto bail_out;
115    }
116
117    /* Is TLS Enabled? */
118    if (have_tls) {
119       if (tls_local_need >= BNET_TLS_OK && tls_remote_need >= BNET_TLS_OK) {
120          /* Engage TLS! Full Speed Ahead! */
121          if (!bnet_tls_client(tls_ctx, dir)) {
122             csprint(_("TLS negotiation failed\n"));
123             goto bail_out;
124          }
125       }
126    }
127
128    Dmsg1(6, ">dird: %s", dir->msg);
129    if (bnet_recv(dir) <= 0) {
130       csprint(_("Bad response to Hello command: ERR="), CS_DATA);
131       csprint(bnet_strerror(dir), CS_DATA);
132       csprint("\n", CS_DATA);
133       goto bail_out;
134    }
135    Dmsg1(10, "<dird: %s", dir->msg);
136    if (strncmp(dir->msg, OKhello, sizeof(OKhello)-1) != 0) {
137       csprint(_("Director rejected Hello command\n"), CS_DATA);
138       goto bail_out;
139    } else {
140       csprint(dir->msg, CS_DATA);
141    }
142    stop_bsock_timer(tid);
143    return 1;
144
145 bail_out:
146    stop_bsock_timer(tid);
147    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"));
148    return 0;
149
150 }