]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/console/authenticate.c
- Integrated TLS network encryption
[bacula/bacula] / bacula / src / 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-2004 Kern Sibbald and John Walker
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    as published by the Free Software Foundation; either version 2
19    of the License, or (at your option) any later version.
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    GNU General Public License for more details.
25
26    You should have received a copy of the GNU General Public
27    License along with this program; if not, write to the Free
28    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
29    MA 02111-1307, USA.
30  */
31
32 #include "bacula.h"
33 #include "console_conf.h"
34 #include "jcr.h"
35
36
37 void senditf(const char *fmt, ...);
38 void sendit(const char *buf);
39
40 /* Commands sent to Director */
41 static char hello[]    = "Hello %s calling\n";
42
43 /* Response from Director */
44 static char OKhello[]   = "1000 OK:";
45
46 /* Forward referenced functions */
47
48 /*
49  * Authenticate Director
50  */
51 int authenticate_director(JCR *jcr, DIRRES *director, CONRES *cons)
52 {
53    BSOCK *dir = jcr->dir_bsock;
54    int tls_local_need = BNET_TLS_NONE;
55    int tls_remote_need = BNET_TLS_NONE;
56    char bashed_name[MAX_NAME_LENGTH];
57    bool auth_success = false;
58    char *password;
59 #ifdef HAVE_TLS
60    TLS_CONTEXT *tls_ctx = NULL;
61 #endif /* HAVE_TLS */
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 #ifdef HAVE_TLS
71       /* TLS Requirement */
72       if (cons->tls_enable) {
73          if (cons->tls_require) {
74             tls_local_need = BNET_TLS_REQUIRED;
75          } else {
76             tls_local_need = BNET_TLS_OK;
77          }
78       }
79
80       tls_ctx = cons->tls_ctx;
81 #endif /* HAVE_TLS */
82    } else {
83       bstrncpy(bashed_name, "*UserAgent*", sizeof(bashed_name));
84       password = director->password;
85 #ifdef HAVE_TLS
86       /* TLS Requirement */
87       if (director->tls_enable) {
88          if (director->tls_require) {
89             tls_local_need = BNET_TLS_REQUIRED;
90          } else {
91             tls_local_need = BNET_TLS_OK;
92          }
93       }
94
95       tls_ctx = director->tls_ctx;
96 #endif /* HAVE_TLS */
97    }
98
99    
100    /* Timeout Hello after 5 mins */
101    btimer_t *tid = start_bsock_timer(dir, 60 * 5);
102    bnet_fsend(dir, hello, bashed_name);
103
104    if (!cram_md5_get_auth(dir, password, &tls_remote_need) ||
105        !cram_md5_auth(dir, password, tls_local_need)) {
106       auth_success = false;
107       goto auth_done;
108    } else {
109       auth_success = true;
110       /* Continue on, soldier ... */
111    }
112
113    /* Verify that the remote host is willing to meet our TLS requirements */
114    if (tls_remote_need < tls_local_need && tls_local_need != BNET_TLS_OK && tls_remote_need != BNET_TLS_OK) {
115       sendit(_("Authorization problem:"
116              " Remote server did not advertise required TLS support.\n"));
117       auth_success = false;
118       goto auth_done;
119    }
120
121    /* Verify that we are willing to meet the remote host's requirements */
122    if (tls_remote_need > tls_local_need && tls_local_need != BNET_TLS_OK && tls_remote_need != BNET_TLS_OK) {
123       sendit(_("Authorization problem:"
124              " Remote server requires TLS.\n"));
125       auth_success = false;
126       goto auth_done;
127    }
128
129 #ifdef HAVE_TLS
130    /* Is TLS Enabled? */
131    if (tls_local_need >= BNET_TLS_OK && tls_remote_need >= BNET_TLS_OK) {
132       /* Engage TLS! Full Speed Ahead! */
133       if (!bnet_tls_client(tls_ctx, dir)) {
134          sendit(_("TLS negotiation failed\n"));
135          auth_success = false;
136          goto auth_done;
137       }
138       auth_success = true;
139    }
140 #endif /* HAVE_TLS */
141
142 /* Authorization Completed */
143 auth_done:
144    if (!auth_success) {
145       stop_bsock_timer(tid);
146       sendit( _("Director authorization problem.\n"
147              "Most likely the passwords do not agree.\n"
148              "If you are using TLS, there may have been a certificate validation error during the TLS handshake.\n"
149              "Please see http://www.bacula.org/html-manual/faq.html#AuthorizationErrors for help.\n"));
150       return 0;
151    }
152
153    /*
154     * It's possible that the TLS connection will
155     * be dropped here if an invalid client certificate was presented
156     */
157    Dmsg1(6, ">dird: %s", dir->msg);
158    if (bnet_recv(dir) <= 0) {
159       stop_bsock_timer(tid);
160       senditf(_("Bad response to Hello command: ERR=%s\n"),
161          bnet_strerror(dir));
162       senditf(_("If you are using TLS, it is possible that your client"
163               " certificate was not accepted. Check the server messages.\n"));
164       return 0;
165    }
166    Dmsg1(10, "<dird: %s", dir->msg);
167    stop_bsock_timer(tid);
168    if (strncmp(dir->msg, OKhello, sizeof(OKhello)-1) != 0) {
169       sendit(_("Director rejected Hello command\n"));
170       return 0;
171    } else {
172       sendit(dir->msg);
173    }
174    return 1;
175 }