]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/authenticate.c
186c0afc74b76aae8703c23d6dc2920efda7cb77
[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    alist *verify_list = NULL;
47
48    if (rcode != R_DIRECTOR) {
49       Dmsg1(50, "I only authenticate directors, not %d\n", rcode);
50       Emsg1(M_FATAL, 0, _("I only authenticate directors, not %d\n"), rcode);
51       return 0;
52    }
53    if (bs->msglen < 25 || bs->msglen > 200) {
54       Dmsg2(50, "Bad Hello command from Director at %s. Len=%d.\n",
55             bs->who, bs->msglen);
56       Emsg2(M_FATAL, 0, _("Bad Hello command from Director at %s. Len=%d.\n"),
57             bs->who, bs->msglen);
58       return 0;
59    }
60    dirname = get_pool_memory(PM_MESSAGE);
61    dirname = check_pool_memory_size(dirname, bs->msglen);
62
63    if (sscanf(bs->msg, "Hello Director %s calling\n", dirname) != 1) {
64       free_pool_memory(dirname);
65       bs->msg[100] = 0;
66       Dmsg2(50, "Bad Hello command from Director at %s: %s\n",
67             bs->who, bs->msg);
68       Emsg2(M_FATAL, 0, _("Bad Hello command from Director at %s: %s\n"),
69             bs->who, bs->msg);
70       return 0;
71    }
72    unbash_spaces(dirname);
73    LockRes();
74    foreach_res(director, R_DIRECTOR) {
75       if (strcmp(director->hdr.name, dirname) == 0)
76          break;
77    }
78    UnlockRes();
79    if (!director) {
80       Dmsg2(50, "Connection from unknown Director %s at %s rejected.\n",
81             dirname, bs->who);
82       Emsg2(M_FATAL, 0, _("Connection from unknown Director %s at %s rejected.\n"
83        "Please see http://www.bacula.org/rel-manual/faq.html#AuthorizationErrors for help.\n"),
84             dirname, bs->who);
85       free_pool_memory(dirname);
86       return 0;
87    }
88
89    if (have_tls) {
90       /* TLS Requirement */
91       if (director->tls_enable) {
92          if (director->tls_require) {
93             tls_local_need = BNET_TLS_REQUIRED;
94          } else {
95             tls_local_need = BNET_TLS_OK;
96          }
97       }
98
99       if (director->tls_verify_peer) {
100          verify_list = director->tls_allowed_cns;
101       }
102    }
103
104    btimer_t *tid = start_bsock_timer(bs, AUTH_TIMEOUT);
105    auth_success = cram_md5_auth(bs, director->password, tls_local_need);
106    if (auth_success) {
107       auth_success = cram_md5_get_auth(bs, director->password, &tls_remote_need);
108       if (!auth_success) {
109          Dmsg1(50, "cram_get_auth failed for %s\n", bs->who);
110       }
111    } else {
112       Dmsg1(50, "cram_auth failed for %s\n", bs->who);
113    }
114    if (!auth_success) {
115       Emsg1(M_FATAL, 0, _("Incorrect password given by Director at %s.\n"
116        "Please see http://www.bacula.org/rel-manual/faq.html#AuthorizationErrors for help.\n"),
117             bs->who);
118       director = NULL;
119       goto auth_fatal;
120    }
121
122    /* Verify that the remote host is willing to meet our TLS requirements */
123    if (tls_remote_need < tls_local_need && tls_local_need != BNET_TLS_OK && tls_remote_need != BNET_TLS_OK) {
124       Emsg0(M_FATAL, 0, _("Authorization problem: Remote server did not"
125            " advertise required TLS support.\n"));
126       director = NULL;
127       goto auth_fatal;
128    }
129
130    /* Verify that we are willing to meet the remote host's requirements */
131    if (tls_remote_need > tls_local_need && tls_local_need != BNET_TLS_OK && tls_remote_need != BNET_TLS_OK) {
132       Emsg0(M_FATAL, 0, _("Authorization problem: Remote server requires TLS.\n"));
133       director = NULL;
134       goto auth_fatal;
135    }
136
137    if (have_tls) {
138       if (tls_local_need >= BNET_TLS_OK && tls_remote_need >= BNET_TLS_OK) {
139          /* Engage TLS! Full Speed Ahead! */
140          if (!bnet_tls_server(director->tls_ctx, bs, verify_list)) {
141             Emsg0(M_FATAL, 0, _("TLS negotiation failed.\n"));
142             director = NULL;
143             goto auth_fatal;
144          }
145       }
146    }
147
148 auth_fatal:
149    stop_bsock_timer(tid);
150    free_pool_memory(dirname);
151    jcr->director = director;
152    return (director != NULL);
153 }
154
155 /*
156  * Inititiate the communications with the Director.
157  * He has made a connection to our server.
158  *
159  * Basic tasks done here:
160  *   We read Director's initial message and authorize him.
161  *
162  */
163 int authenticate_director(JCR *jcr)
164 {
165    BSOCK *dir = jcr->dir_bsock;
166
167    if (!authenticate(R_DIRECTOR, dir, jcr)) {
168       bnet_fsend(dir, "%s", Dir_sorry);
169       Emsg0(M_FATAL, 0, _("Unable to authenticate Director\n"));
170       bmicrosleep(5, 0);
171       return 0;
172    }
173    return bnet_fsend(dir, "%s", OK_hello);
174 }
175
176 /*
177  * First prove our identity to the Storage daemon, then
178  * make him prove his identity.
179  */
180 int authenticate_storagedaemon(JCR *jcr)
181 {
182    BSOCK *sd = jcr->store_bsock;
183    int tls_local_need = BNET_TLS_NONE;
184    int tls_remote_need = BNET_TLS_NONE;
185    bool auth_success = false;
186
187 #ifdef HAVE_TLS
188    /* TLS Requirement */
189    if (me->tls_enable) {
190       if (me->tls_require) {
191          tls_local_need = BNET_TLS_REQUIRED;
192       } else {
193          tls_local_need = BNET_TLS_OK;
194       }
195    }
196 #endif /* HAVE_TLS */
197
198    btimer_t *tid = start_bsock_timer(sd, AUTH_TIMEOUT);
199    auth_success = cram_md5_get_auth(sd, jcr->sd_auth_key, &tls_remote_need);
200    if (!auth_success) {
201       Dmsg1(50, "cram_get_auth failed for %s\n", sd->who);
202    } else {
203       auth_success = cram_md5_auth(sd, jcr->sd_auth_key, tls_local_need);
204       if (!auth_success) {
205          Dmsg1(50, "cram_auth failed for %s\n", sd->who);
206       }
207    }
208
209    /* Destroy session key */
210    memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
211
212    if (!auth_success) {
213       Jmsg(jcr, M_FATAL, 0, _("Authorization key rejected by Storage daemon.\n"
214        "Please see http://www.bacula.org/rel-manual/faq.html#AuthorizationErrors for help.\n"));
215       goto auth_fatal;
216    }
217
218    /* Verify that the remote host is willing to meet our TLS requirements */
219    if (tls_remote_need < tls_local_need && tls_local_need != BNET_TLS_OK && tls_remote_need != BNET_TLS_OK) {
220       Jmsg(jcr, M_FATAL, 0, _("Authorization problem: Remote server did not" 
221            " advertise required TLS support.\n"));
222       auth_success = false;
223       goto auth_fatal;
224    }
225
226    /* Verify that we are willing to meet the remote host's requirements */
227    if (tls_remote_need > tls_local_need && tls_local_need != BNET_TLS_OK && tls_remote_need != BNET_TLS_OK) {
228       Jmsg(jcr, M_FATAL, 0, _("Authorization problem: Remote server requires TLS.\n"));
229       auth_success = false;
230       goto auth_fatal;
231    }
232
233 #ifdef HAVE_TLS
234    if (tls_local_need >= BNET_TLS_OK && tls_remote_need >= BNET_TLS_OK) {
235       /* Engage TLS! Full Speed Ahead! */
236       if (!bnet_tls_client(me->tls_ctx, sd)) {
237          Jmsg(jcr, M_FATAL, 0, _("TLS negotiation failed.\n"));
238          auth_success = false;
239          goto auth_fatal;
240       }
241    }
242 #endif /* HAVE_TLS */
243
244 auth_fatal:
245    stop_bsock_timer(tid);
246    return auth_success;
247 }