2 * Authenticate Director who is attempting to connect.
4 * Kern Sibbald, October 2000
10 Copyright (C) 2000-2004 Kern Sibbald and John Walker
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.
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.
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,
32 static char OK_hello[] = "2000 OK Hello\n";
33 static char Dir_sorry[] = "2999 No go\n";
36 /*********************************************************************
39 static int authenticate(int rcode, BSOCK *bs, JCR* jcr)
43 int tls_local_need = BNET_TLS_NONE;
44 int tls_remote_need = BNET_TLS_NONE;
45 bool auth_success = false;
47 alist *verify_list = NULL;
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);
55 if (bs->msglen < 25 || bs->msglen > 200) {
56 Dmsg2(50, _("Bad Hello command from Director at %s. Len=%d.\n"),
58 Emsg2(M_FATAL, 0, _("Bad Hello command from Director at %s. Len=%d.\n"),
62 dirname = get_pool_memory(PM_MESSAGE);
63 dirname = check_pool_memory_size(dirname, bs->msglen);
65 if (sscanf(bs->msg, "Hello Director %s calling\n", dirname) != 1) {
66 free_pool_memory(dirname);
68 Dmsg2(50, _("Bad Hello command from Director at %s: %s\n"),
70 Emsg2(M_FATAL, 0, _("Bad Hello command from Director at %s: %s\n"),
74 unbash_spaces(dirname);
76 foreach_res(director, R_DIRECTOR) {
77 if (strcmp(director->hdr.name, dirname) == 0)
82 Dmsg2(50, _("Connection from unknown Director %s at %s rejected.\n"),
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"),
87 free_pool_memory(dirname);
93 if (director->tls_enable) {
94 if (director->tls_require) {
95 tls_local_need = BNET_TLS_REQUIRED;
97 tls_local_need = BNET_TLS_OK;
101 if (director->tls_verify_peer) {
102 verify_list = director->tls_allowed_cns;
104 #endif /* HAVE_TLS */
106 btimer_t *tid = start_bsock_timer(bs, AUTH_TIMEOUT);
107 auth_success = cram_md5_auth(bs, director->password, tls_local_need);
109 auth_success = cram_md5_get_auth(bs, director->password, &tls_remote_need);
111 Dmsg1(50, "cram_get_auth failed for %s\n", bs->who);
114 Dmsg1(50, "cram_auth failed for %s\n", bs->who);
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"),
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"));
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"));
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");
148 #endif /* HAVE_TLS */
151 stop_bsock_timer(tid);
152 free_pool_memory(dirname);
153 jcr->director = director;
154 return (director != NULL);
158 * Inititiate the communications with the Director.
159 * He has made a connection to our server.
161 * Basic tasks done here:
162 * We read Director's initial message and authorize him.
165 int authenticate_director(JCR *jcr)
167 BSOCK *dir = jcr->dir_bsock;
169 if (!authenticate(R_DIRECTOR, dir, jcr)) {
170 bnet_fsend(dir, "%s", Dir_sorry);
171 Emsg0(M_FATAL, 0, _("Unable to authenticate Director\n"));
175 return bnet_fsend(dir, "%s", OK_hello);
179 * First prove our identity to the Storage daemon, then
180 * make him prove his identity.
182 int authenticate_storagedaemon(JCR *jcr)
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;
190 /* TLS Requirement */
191 if (me->tls_enable) {
192 if (me->tls_require) {
193 tls_local_need = BNET_TLS_REQUIRED;
195 tls_local_need = BNET_TLS_OK;
198 #endif /* HAVE_TLS */
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);
203 Dmsg1(50, "cram_get_auth failed for %s\n", sd->who);
205 auth_success = cram_md5_auth(sd, jcr->sd_auth_key, tls_local_need);
207 Dmsg1(50, "cram_auth failed for %s\n", sd->who);
211 /* Destroy session key */
212 memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
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"));
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;
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;
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;
244 #endif /* HAVE_TLS */
247 stop_bsock_timer(tid);