]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/authenticate.c
- Fix bug in changing tape pools after first backup. Reported
[bacula/bacula] / bacula / src / stored / authenticate.c
1 /*
2  * Authenticate caller
3  *
4  *   Kern Sibbald, October 2000
5  *
6  *   Version $Id$
7  *
8  */
9 /*
10    Copyright (C) 2000, 2001, 2002 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 "stored.h"
31
32 static char Dir_sorry[] = "3999 No go\n";
33 static char OK_hello[]  = "3000 OK Hello\n";
34
35
36 /*********************************************************************
37  *
38  *
39  */
40 static int authenticate(int rcode, BSOCK *bs, JCR* jcr)
41 {
42    POOLMEM *dirname;
43    DIRRES *director = NULL;
44    int tls_local_need = BNET_TLS_NONE;
45    int tls_remote_need = BNET_TLS_NONE;
46    bool auth_success = false;
47 #ifdef HAVE_TLS
48    alist *verify_list = NULL;
49 #endif
50
51    if (rcode != R_DIRECTOR) {
52       Dmsg1(50, _("I only authenticate Directors, not %d\n"), rcode);
53       Emsg1(M_FATAL, 0, _("I only authenticate Directors, not %d\n"), rcode);
54       return 0;
55    }
56    if (bs->msglen < 25 || bs->msglen > 200) {
57       Dmsg2(50, _("Bad Hello command from Director at %s. Len=%d.\n"),
58             bs->who, bs->msglen);
59       Emsg2(M_FATAL, 0, _("Bad Hello command from Director at %s. Len=%d.\n"),
60             bs->who, bs->msglen);
61       return 0;
62    }
63    dirname = get_pool_memory(PM_MESSAGE);
64    dirname = check_pool_memory_size(dirname, bs->msglen);
65
66    if (sscanf(bs->msg, "Hello Director %127s calling\n", dirname) != 1) {
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    director = NULL;
75    unbash_spaces(dirname);
76 // LockRes();
77    foreach_res(director, rcode) {
78       if (strcmp(director->hdr.name, dirname) == 0)
79          break;
80    }
81 // UnlockRes();
82    if (!director) {
83       Dmsg2(50, _("Connection from unknown Director %s at %s rejected.\n"),
84             dirname, bs->who);
85       Emsg2(M_FATAL, 0, _("Connection from unknown Director %s at %s rejected.\n"
86        "Please see http://www.bacula.org/html-manual/faq.html#AuthorizationErrors for help.\n"),
87             dirname, bs->who);
88       free_pool_memory(dirname);
89       return 0;
90    }
91
92 #ifdef HAVE_TLS
93    /* TLS Requirement */
94    if (director->tls_enable) {
95       if (director->tls_require) {
96          tls_local_need = BNET_TLS_REQUIRED;
97       } else {
98          tls_local_need = BNET_TLS_OK;
99       }
100    }
101
102    if (director->tls_verify_peer) {
103       verify_list = director->tls_allowed_cns;
104    }
105 #endif /* HAVE_TLS */
106
107    /* Timeout Hello after 10 mins */
108    btimer_t *tid = start_bsock_timer(bs, AUTH_TIMEOUT);
109    auth_success = cram_md5_auth(bs, director->password, tls_local_need);
110    if (auth_success) {
111       auth_success = cram_md5_get_auth(bs, director->password, &tls_remote_need);
112       if (!auth_success) {
113          Dmsg1(50, "cram_get_auth failed with %s\n", bs->who);
114       }
115    } else {
116       Dmsg1(50, "cram_auth failed with %s\n", bs->who);
117    }
118
119    if (!auth_success) {
120       Emsg0(M_FATAL, 0, _("Incorrect password given by Director.\n"
121        "Please see http://www.bacula.org/html-manual/faq.html#AuthorizationErrors for help.\n"));
122       auth_success = false;
123       goto auth_fatal;
124    }
125
126    /* Verify that the remote host is willing to meet our TLS requirements */
127    if (tls_remote_need < tls_local_need && tls_local_need != BNET_TLS_OK && tls_remote_need != BNET_TLS_OK) {
128       Emsg0(M_FATAL, 0, _("Authorization problem: Remote server did not" 
129            " advertise required TLS support.\n"));
130       auth_success = false;
131       goto auth_fatal;
132    }
133
134    /* Verify that we are willing to meet the remote host's requirements */
135    if (tls_remote_need > tls_local_need && tls_local_need != BNET_TLS_OK && tls_remote_need != BNET_TLS_OK) {
136       Emsg0(M_FATAL, 0, _("Authorization problem: Remote server requires TLS.\n"));
137       auth_success = false;
138       goto auth_fatal;
139    }
140
141 #ifdef HAVE_TLS
142    if (tls_local_need >= BNET_TLS_OK && tls_remote_need >= BNET_TLS_OK) {
143       /* Engage TLS! Full Speed Ahead! */
144       if (!bnet_tls_server(director->tls_ctx, bs, verify_list)) {
145          Emsg0(M_FATAL, 0, "TLS negotiation failed.\n");
146          auth_success = false;
147          goto auth_fatal;
148       }
149    }
150 #endif /* HAVE_TLS */
151
152 auth_fatal:
153    stop_bsock_timer(tid);
154    free_pool_memory(dirname);
155    jcr->director = director;
156    return auth_success;
157 }
158
159 /*
160  * Inititiate the message channel with the Director.
161  * He has made a connection to our server.
162  *
163  * Basic tasks done here:
164  *   Assume the Hello message is already in the input
165  *     buffer.  We then authenticate him.
166  *   Get device, media, and pool information from Director
167  *
168  *   This is the channel across which we will send error
169  *     messages and job status information.
170  */
171 int authenticate_director(JCR *jcr)
172 {
173    BSOCK *dir = jcr->dir_bsock;
174
175    if (!authenticate(R_DIRECTOR, dir, jcr)) {
176       bnet_fsend(dir, "%s", Dir_sorry);
177       Dmsg1(50, _("Unable to authenticate Director at %s.\n"), dir->who);
178       Emsg1(M_ERROR, 0, _("Unable to authenticate Director at %s.\n"), dir->who);
179       bmicrosleep(5, 0);
180       return 0;
181    }
182    return bnet_fsend(dir, "%s", OK_hello);
183 }
184
185 int authenticate_filed(JCR *jcr)
186 {
187    BSOCK *fd = jcr->file_bsock;
188    int tls_local_need = BNET_TLS_NONE;
189    int tls_remote_need = BNET_TLS_NONE;
190    bool auth_success = false;
191 #ifdef HAVE_TLS
192    alist *verify_list = NULL;
193 #endif
194
195 #ifdef HAVE_TLS
196    /* TLS Requirement */
197    if (me->tls_enable) {
198       if (me->tls_require) {
199          tls_local_need = BNET_TLS_REQUIRED;
200       } else {
201          tls_local_need = BNET_TLS_OK;
202       }
203    }
204
205    if (me->tls_verify_peer) {
206       verify_list = me->tls_allowed_cns;
207    }
208 #endif /* HAVE_TLS */
209
210    /* Timeout Hello after 5 mins */
211    btimer_t *tid = start_bsock_timer(fd, AUTH_TIMEOUT);
212    auth_success = cram_md5_auth(fd, jcr->sd_auth_key, tls_local_need);
213    if (auth_success) {
214        auth_success = cram_md5_get_auth(fd, jcr->sd_auth_key, &tls_remote_need);
215        if (!auth_success) {
216           Dmsg1(50, "cram-get-auth failed with %s\n", fd->who);
217        }
218    } else {
219       Dmsg1(50, "cram-auth failed with %s\n", fd->who);
220    }
221
222    if (!auth_success) {
223       Jmsg(jcr, M_FATAL, 0, _("Incorrect authorization key from File daemon at %s rejected.\n"
224        "Please see http://www.bacula.org/html-manual/faq.html#AuthorizationErrors for help.\n"),
225            fd->who);
226       auth_success = false;
227       goto auth_fatal;
228    }
229
230    /* Verify that the remote host is willing to meet our TLS requirements */
231    if (tls_remote_need < tls_local_need && tls_local_need != BNET_TLS_OK && tls_remote_need != BNET_TLS_OK) {
232       Jmsg(jcr, M_FATAL, 0, _("Authorization problem: Remote server did not" 
233            " advertise required TLS support.\n"));
234       auth_success = false;
235       goto auth_fatal;
236    }
237
238    /* Verify that we are willing to meet the remote host's requirements */
239    if (tls_remote_need > tls_local_need && tls_local_need != BNET_TLS_OK && tls_remote_need != BNET_TLS_OK) {
240       Jmsg(jcr, M_FATAL, 0, _("Authorization problem: Remote server requires TLS.\n"));
241       auth_success = false;
242       goto auth_fatal;
243    }
244
245 #ifdef HAVE_TLS
246    if (tls_local_need >= BNET_TLS_OK && tls_remote_need >= BNET_TLS_OK) {
247       /* Engage TLS! Full Speed Ahead! */
248       if (!bnet_tls_server(me->tls_ctx, fd, verify_list)) {
249          Jmsg(jcr, M_FATAL, 0, "TLS negotiation failed.\n");
250          auth_success = false;
251          goto auth_fatal;
252       }
253    }
254 #endif /* HAVE_TLS */
255
256 auth_fatal:
257    stop_bsock_timer(tid);
258    if (!auth_success) {
259       Jmsg(jcr, M_FATAL, 0, _("Incorrect authorization key from File daemon at %s rejected.\n"
260        "Please see http://www.bacula.org/html-manual/faq.html#AuthorizationErrors for help.\n"),
261            fd->who);
262    }
263    jcr->authenticated = auth_success;
264    return auth_success;
265 }