]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/authenticate.c
1c173ae7dfd82e37c48e9556fcf8c76e33163d46
[bacula/bacula] / bacula / src / filed / authenticate.c
1 /*
2  * Authenticate Director who is attempting to connect.
3  *
4  *   Kern Sibbald, October 2000
5  *
6  *   Version $Id$
7  *
8  */
9 /*
10    Copyright (C) 2000-2004 Kern Sibbald and John Walker
11
12    This program is free software; you can redistribute it and/or
13    modify it under the terms of the GNU General Public License as
14    published by the Free Software Foundation; either version 2 of
15    the License, or (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20    General Public License for more details.
21
22    You should have received a copy of the GNU General Public
23    License along with this program; if not, write to the Free
24    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25    MA 02111-1307, USA.
26
27  */
28
29 #include "bacula.h"
30 #include "filed.h"
31
32 static char OK_hello[]  = "2000 OK Hello\n";
33 static char Dir_sorry[] = "2999 No go\n";
34
35
36 /*********************************************************************
37  *
38  */
39 static int authenticate(int rcode, BSOCK *bs, JCR* jcr)
40 {
41    POOLMEM *dirname;
42    DIRRES *director;
43    int tls_local_need = BNET_TLS_NONE;
44    int tls_remote_need = BNET_TLS_NONE;
45    bool auth_success = false;
46 #ifdef HAVE_TLS
47    alist *verify_list = NULL;
48 #endif /* HAVE_TLS */
49
50    if (rcode != R_DIRECTOR) {
51       Dmsg1(50, _("I only authenticate directors, not %d\n"), rcode);
52       Emsg1(M_FATAL, 0, _("I only authenticate directors, not %d\n"), rcode);
53       return 0;
54    }
55    if (bs->msglen < 25 || bs->msglen > 200) {
56       Dmsg2(50, _("Bad Hello command from Director at %s. Len=%d.\n"),
57             bs->who, bs->msglen);
58       Emsg2(M_FATAL, 0, _("Bad Hello command from Director at %s. Len=%d.\n"),
59             bs->who, bs->msglen);
60       return 0;
61    }
62    dirname = get_pool_memory(PM_MESSAGE);
63    dirname = check_pool_memory_size(dirname, bs->msglen);
64
65    if (sscanf(bs->msg, "Hello Director %s calling\n", dirname) != 1) {
66       free_pool_memory(dirname);
67       bs->msg[100] = 0;
68       Dmsg2(50, _("Bad Hello command from Director at %s: %s\n"),
69             bs->who, bs->msg);
70       Emsg2(M_FATAL, 0, _("Bad Hello command from Director at %s: %s\n"),
71             bs->who, bs->msg);
72       return 0;
73    }
74    unbash_spaces(dirname);
75    LockRes();
76    foreach_res(director, R_DIRECTOR) {
77       if (strcmp(director->hdr.name, dirname) == 0)
78          break;
79    }
80    UnlockRes();
81    if (!director) {
82       Dmsg2(50, _("Connection from unknown Director %s at %s rejected.\n"),
83             dirname, bs->who);
84       Emsg2(M_FATAL, 0, _("Connection from unknown Director %s at %s rejected.\n"
85        "Please see http://www.bacula.org/rel-manual/faq.html#AuthorizationErrors for help.\n"),
86             dirname, bs->who);
87       free_pool_memory(dirname);
88       return 0;
89    }
90
91 #ifdef HAVE_TLS
92    /* TLS Requirement */
93    if (director->tls_enable) {
94       if (director->tls_require) {
95          tls_local_need = BNET_TLS_REQUIRED;
96       } else {
97          tls_local_need = BNET_TLS_OK;
98       }
99    }
100
101    if (director->tls_verify_peer) {
102       verify_list = director->tls_allowed_cns;
103    }
104 #endif /* HAVE_TLS */
105
106    btimer_t *tid = start_bsock_timer(bs, AUTH_TIMEOUT);
107    auth_success = cram_md5_auth(bs, director->password, tls_local_need);
108    if (auth_success) {
109       auth_success = cram_md5_get_auth(bs, director->password, &tls_remote_need);
110       if (!auth_success) {
111          Dmsg1(50, "cram_get_auth failed for %s\n", bs->who);
112       }
113    } else {
114       Dmsg1(50, "cram_auth failed for %s\n", bs->who);
115    }
116    if (!auth_success) {
117       Emsg1(M_FATAL, 0, _("Incorrect password given by Director at %s.\n"
118        "Please see http://www.bacula.org/rel-manual/faq.html#AuthorizationErrors for help.\n"),
119             bs->who);
120       director = NULL;
121       goto auth_fatal;
122    }
123
124    /* Verify that the remote host is willing to meet our TLS requirements */
125    if (tls_remote_need < tls_local_need && tls_local_need != BNET_TLS_OK && tls_remote_need != BNET_TLS_OK) {
126       Emsg0(M_FATAL, 0, _("Authorization problem: Remote server did not"
127            " advertise required TLS support.\n"));
128       director = NULL;
129       goto auth_fatal;
130    }
131
132    /* Verify that we are willing to meet the remote host's requirements */
133    if (tls_remote_need > tls_local_need && tls_local_need != BNET_TLS_OK && tls_remote_need != BNET_TLS_OK) {
134       Emsg0(M_FATAL, 0, _("Authorization problem: Remote server requires TLS.\n"));
135       director = NULL;
136       goto auth_fatal;
137    }
138
139 #ifdef HAVE_TLS
140    if (tls_local_need >= BNET_TLS_OK && tls_remote_need >= BNET_TLS_OK) {
141       /* Engage TLS! Full Speed Ahead! */
142       if (!bnet_tls_server(director->tls_ctx, bs, verify_list)) {
143          Emsg0(M_FATAL, 0, "TLS negotiation failed.\n");
144          director = NULL;
145          goto auth_fatal;
146       }
147    }
148 #endif /* HAVE_TLS */
149
150 auth_fatal:
151    stop_bsock_timer(tid);
152    free_pool_memory(dirname);
153    jcr->director = director;
154    return (director != NULL);
155 }
156
157 /*
158  * Inititiate the communications with the Director.
159  * He has made a connection to our server.
160  *
161  * Basic tasks done here:
162  *   We read Director's initial message and authorize him.
163  *
164  */
165 int authenticate_director(JCR *jcr)
166 {
167    BSOCK *dir = jcr->dir_bsock;
168
169    if (!authenticate(R_DIRECTOR, dir, jcr)) {
170       bnet_fsend(dir, "%s", Dir_sorry);
171       Emsg0(M_FATAL, 0, _("Unable to authenticate Director\n"));
172       bmicrosleep(5, 0);
173       return 0;
174    }
175    return bnet_fsend(dir, "%s", OK_hello);
176 }
177
178 /*
179  * First prove our identity to the Storage daemon, then
180  * make him prove his identity.
181  */
182 int authenticate_storagedaemon(JCR *jcr)
183 {
184    BSOCK *sd = jcr->store_bsock;
185    int tls_local_need = BNET_TLS_NONE;
186    int tls_remote_need = BNET_TLS_NONE;
187    bool auth_success = false;
188
189 #ifdef HAVE_TLS
190    /* TLS Requirement */
191    if (me->tls_enable) {
192       if (me->tls_require) {
193          tls_local_need = BNET_TLS_REQUIRED;
194       } else {
195          tls_local_need = BNET_TLS_OK;
196       }
197    }
198 #endif /* HAVE_TLS */
199
200    btimer_t *tid = start_bsock_timer(sd, AUTH_TIMEOUT);
201    auth_success = cram_md5_get_auth(sd, jcr->sd_auth_key, &tls_remote_need);
202    if (!auth_success) {
203       Dmsg1(50, "cram_get_auth failed for %s\n", sd->who);
204    } else {
205       auth_success = cram_md5_auth(sd, jcr->sd_auth_key, tls_local_need);
206       if (!auth_success) {
207          Dmsg1(50, "cram_auth failed for %s\n", sd->who);
208       }
209    }
210
211    /* Destroy session key */
212    memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
213
214    if (!auth_success) {
215       Jmsg(jcr, M_FATAL, 0, _("Authorization key rejected by Storage daemon.\n"
216        "Please see http://www.bacula.org/rel-manual/faq.html#AuthorizationErrors for help.\n"));
217       goto auth_fatal;
218    }
219
220    /* Verify that the remote host is willing to meet our TLS requirements */
221    if (tls_remote_need < tls_local_need && tls_local_need != BNET_TLS_OK && tls_remote_need != BNET_TLS_OK) {
222       Jmsg(jcr, M_FATAL, 0, _("Authorization problem: Remote server did not" 
223            " advertise required TLS support.\n"));
224       auth_success = false;
225       goto auth_fatal;
226    }
227
228    /* Verify that we are willing to meet the remote host's requirements */
229    if (tls_remote_need > tls_local_need && tls_local_need != BNET_TLS_OK && tls_remote_need != BNET_TLS_OK) {
230       Jmsg(jcr, M_FATAL, 0, _("Authorization problem: Remote server requires TLS.\n"));
231       auth_success = false;
232       goto auth_fatal;
233    }
234
235 #ifdef HAVE_TLS
236    if (tls_local_need >= BNET_TLS_OK && tls_remote_need >= BNET_TLS_OK) {
237       /* Engage TLS! Full Speed Ahead! */
238       if (!bnet_tls_client(me->tls_ctx, sd)) {
239          Jmsg(jcr, M_FATAL, 0, "TLS negotiation failed.\n");
240          auth_success = false;
241          goto auth_fatal;
242       }
243    }
244 #endif /* HAVE_TLS */
245
246 auth_fatal:
247    stop_bsock_timer(tid);
248    return auth_success;
249 }