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;
46 alist *verify_list = NULL;
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);
53 if (bs->msglen < 25 || bs->msglen > 200) {
54 Dmsg2(50, "Bad Hello command from Director at %s. Len=%d.\n",
56 Emsg2(M_FATAL, 0, _("Bad Hello command from Director at %s. Len=%d.\n"),
60 dirname = get_pool_memory(PM_MESSAGE);
61 dirname = check_pool_memory_size(dirname, bs->msglen);
63 if (sscanf(bs->msg, "Hello Director %s calling\n", dirname) != 1) {
64 free_pool_memory(dirname);
66 Dmsg2(50, "Bad Hello command from Director at %s: %s\n",
68 Emsg2(M_FATAL, 0, _("Bad Hello command from Director at %s: %s\n"),
72 unbash_spaces(dirname);
74 foreach_res(director, R_DIRECTOR) {
75 if (strcmp(director->hdr.name, dirname) == 0)
80 Dmsg2(50, "Connection from unknown Director %s at %s rejected.\n",
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"),
85 free_pool_memory(dirname);
91 if (director->tls_enable) {
92 if (director->tls_require) {
93 tls_local_need = BNET_TLS_REQUIRED;
95 tls_local_need = BNET_TLS_OK;
99 if (director->tls_verify_peer) {
100 verify_list = director->tls_allowed_cns;
104 btimer_t *tid = start_bsock_timer(bs, AUTH_TIMEOUT);
105 auth_success = cram_md5_auth(bs, director->password, tls_local_need);
107 auth_success = cram_md5_get_auth(bs, director->password, &tls_remote_need);
109 Dmsg1(50, "cram_get_auth failed for %s\n", bs->who);
112 Dmsg1(50, "cram_auth failed for %s\n", bs->who);
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"),
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"));
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"));
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"));
149 stop_bsock_timer(tid);
150 free_pool_memory(dirname);
151 jcr->director = director;
152 return (director != NULL);
156 * Inititiate the communications with the Director.
157 * He has made a connection to our server.
159 * Basic tasks done here:
160 * We read Director's initial message and authorize him.
163 int authenticate_director(JCR *jcr)
165 BSOCK *dir = jcr->dir_bsock;
167 if (!authenticate(R_DIRECTOR, dir, jcr)) {
168 bnet_fsend(dir, "%s", Dir_sorry);
169 Emsg0(M_FATAL, 0, _("Unable to authenticate Director\n"));
173 return bnet_fsend(dir, "%s", OK_hello);
177 * First prove our identity to the Storage daemon, then
178 * make him prove his identity.
180 int authenticate_storagedaemon(JCR *jcr)
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;
188 /* TLS Requirement */
189 if (me->tls_enable) {
190 if (me->tls_require) {
191 tls_local_need = BNET_TLS_REQUIRED;
193 tls_local_need = BNET_TLS_OK;
196 #endif /* HAVE_TLS */
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);
201 Dmsg1(50, "cram_get_auth failed for %s\n", sd->who);
203 auth_success = cram_md5_auth(sd, jcr->sd_auth_key, tls_local_need);
205 Dmsg1(50, "cram_auth failed for %s\n", sd->who);
209 /* Destroy session key */
210 memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
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"));
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;
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;
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;
242 #endif /* HAVE_TLS */
245 stop_bsock_timer(tid);