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