4 * Kern Sibbald, October 2000
10 Copyright (C) 2000, 2001, 2002 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 Dir_sorry[] = "3999 No go\n";
33 static char OK_hello[] = "3000 OK Hello\n";
36 /*********************************************************************
40 static int authenticate(int rcode, BSOCK *bs, JCR* jcr)
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;
48 alist *verify_list = NULL;
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);
56 if (bs->msglen < 25 || bs->msglen > 200) {
57 Dmsg2(50, _("Bad Hello command from Director at %s. Len=%d.\n"),
59 Emsg2(M_FATAL, 0, _("Bad Hello command from Director at %s. Len=%d.\n"),
63 dirname = get_pool_memory(PM_MESSAGE);
64 dirname = check_pool_memory_size(dirname, bs->msglen);
66 if (sscanf(bs->msg, "Hello Director %127s calling\n", dirname) != 1) {
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"),
75 unbash_spaces(dirname);
77 foreach_res(director, rcode) {
78 if (strcmp(director->hdr.name, dirname) == 0)
83 Dmsg2(50, _("Connection from unknown Director %s at %s rejected.\n"),
85 Emsg2(M_FATAL, 0, _("Connection from unknown Director %s at %s rejected.\n"
86 "Please see http://www.bacula.org/rel-manual/faq.html#AuthorizationErrors for help.\n"),
88 free_pool_memory(dirname);
94 if (director->tls_enable) {
95 if (director->tls_require) {
96 tls_local_need = BNET_TLS_REQUIRED;
98 tls_local_need = BNET_TLS_OK;
102 if (director->tls_verify_peer) {
103 verify_list = director->tls_allowed_cns;
105 #endif /* HAVE_TLS */
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);
111 auth_success = cram_md5_get_auth(bs, director->password, &tls_remote_need);
113 Dmsg1(50, "cram_get_auth failed with %s\n", bs->who);
116 Dmsg1(50, "cram_auth failed with %s\n", bs->who);
120 Emsg0(M_FATAL, 0, _("Incorrect password given by Director.\n"
121 "Please see http://www.bacula.org/rel-manual/faq.html#AuthorizationErrors for help.\n"));
122 auth_success = false;
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;
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;
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;
150 #endif /* HAVE_TLS */
153 stop_bsock_timer(tid);
154 free_pool_memory(dirname);
155 jcr->director = director;
160 * Inititiate the message channel with the Director.
161 * He has made a connection to our server.
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
168 * This is the channel across which we will send error
169 * messages and job status information.
171 int authenticate_director(JCR *jcr)
173 BSOCK *dir = jcr->dir_bsock;
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);
182 return bnet_fsend(dir, "%s", OK_hello);
185 int authenticate_filed(JCR *jcr)
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;
192 alist *verify_list = NULL;
196 /* TLS Requirement */
197 if (me->tls_enable) {
198 if (me->tls_require) {
199 tls_local_need = BNET_TLS_REQUIRED;
201 tls_local_need = BNET_TLS_OK;
205 if (me->tls_verify_peer) {
206 verify_list = me->tls_allowed_cns;
208 #endif /* HAVE_TLS */
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);
214 auth_success = cram_md5_get_auth(fd, jcr->sd_auth_key, &tls_remote_need);
216 Dmsg1(50, "cram-get-auth failed with %s\n", fd->who);
219 Dmsg1(50, "cram-auth failed with %s\n", fd->who);
223 Jmsg(jcr, M_FATAL, 0, _("Incorrect authorization key from File daemon at %s rejected.\n"
224 "Please see http://www.bacula.org/rel-manual/faq.html#AuthorizationErrors for help.\n"),
226 auth_success = false;
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;
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;
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;
254 #endif /* HAVE_TLS */
257 stop_bsock_timer(tid);
259 Jmsg(jcr, M_FATAL, 0, _("Incorrect authorization key from File daemon at %s rejected.\n"
260 "Please see http://www.bacula.org/rel-manual/faq.html#AuthorizationErrors for help.\n"),
263 jcr->authenticated = auth_success;