/
authenticate.cc
412 lines (353 loc) · 13.2 KB
/
authenticate.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2001-2008 Free Software Foundation Europe e.V.
Copyright (C) 2011-2012 Planets Communications B.V.
Copyright (C) 2013-2018 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
/*
* Kern Sibbald, May MMI
*/
/**
* @file
* handles authorization of Consoles, Storage and File daemons.
*
* This routine runs as a thread and must be thread reentrant.
*/
#include "include/bareos.h"
#include "dird.h"
#include "dird/authenticate.h"
#if defined(HAVE_PAM)
#include "dird/auth_pam.h"
#endif
#include "dird/fd_cmds.h"
#include "dird/client_connection_handshake_mode.h"
#include "dird/dird_globals.h"
#include "lib/bnet.h"
#include "lib/qualified_resource_name_type_converter.h"
namespace directordaemon {
static const int debuglevel = 50;
/*
* Commands sent to Storage daemon and File daemon and received from the User Agent
*/
static char hello[] = "Hello Director %s calling\n";
/*
* Response from Storage daemon
*/
static char OKhello[] = "3000 OK Hello\n";
static char FDOKhello[] = "2000 OK Hello\n";
static char FDOKnewHello[] = "2000 OK Hello %d\n";
static char dir_not_authorized_message[] = "1999 You are not authorized.\n";
bool AuthenticateWithStorageDaemon(BareosSocket* sd, JobControlRecord *jcr, StorageResource *store)
{
std::string qualified_resource_name;
if (!my_config->GetQualifiedResourceNameTypeConverter()->ResourceToString(me->hdr.name, my_config->r_own_,
qualified_resource_name)) {
Dmsg0(100, "Could not generate qualified resource name for a storage resource\n");
return false;
}
if (!sd->DoTlsHandshake(TlsConfigBase::BNET_TLS_AUTO, store, false, qualified_resource_name.c_str(),
store->password.value, jcr)) {
Dmsg0(100, "Could not DoTlsHandshake() with a storage daemon\n");
return false;
}
char dirname[MAX_NAME_LENGTH];
bstrncpy(dirname, me->hdr.name, sizeof(dirname));
BashSpaces(dirname);
if (!sd->fsend(hello, dirname)) {
Dmsg1(debuglevel, _("Error sending Hello to Storage daemon. ERR=%s\n"), BnetStrerror(sd));
Jmsg(jcr, M_FATAL, 0, _("Error sending Hello to Storage daemon. ERR=%s\n"), BnetStrerror(sd));
return false;
}
bool auth_success = false;
auth_success = sd->AuthenticateOutboundConnection(jcr, "Storage daemon", dirname, store->password, store);
if (!auth_success) {
Dmsg2(debuglevel, "Director unable to authenticate with Storage daemon at \"%s:%d\"\n", sd->host(),
sd->port());
Jmsg(jcr, M_FATAL, 0,
_("Director unable to authenticate with Storage daemon at \"%s:%d\". Possible causes:\n"
"Passwords or names not the same or\n"
"TLS negotiation problem or\n"
"Maximum Concurrent Jobs exceeded on the SD or\n"
"SD networking messed up (restart daemon).\n"
"Please see %s for help.\n"),
sd->host(), sd->port(), MANUAL_AUTH_URL);
return false;
}
Dmsg1(116, ">stored: %s", sd->msg);
if (sd->recv() <= 0) {
Jmsg3(jcr, M_FATAL, 0, _("dir<stored: \"%s:%s\" bad response to Hello command: ERR=%s\n"), sd->who(),
sd->host(), sd->bstrerror());
return false;
}
Dmsg1(110, "<stored: %s", sd->msg);
if (!bstrncmp(sd->msg, OKhello, sizeof(OKhello))) {
Dmsg0(debuglevel, _("Storage daemon rejected Hello command\n"));
Jmsg2(jcr, M_FATAL, 0, _("Storage daemon at \"%s:%d\" rejected Hello command\n"), sd->host(), sd->port());
return false;
}
return true;
}
bool AuthenticateWithFileDaemon(JobControlRecord *jcr)
{
if (jcr->authenticated) { return true; }
BareosSocket *fd = jcr->file_bsock;
ClientResource *client = jcr->res.client;
if (jcr->connection_handshake_try_ == ClientConnectionHandshakeMode::kTlsFirst) {
std::string qualified_resource_name;
if (!my_config->GetQualifiedResourceNameTypeConverter()->ResourceToString(me->hdr.name, my_config->r_own_,
qualified_resource_name)) {
Dmsg0(100, "Could not generate qualified resource name for a client resource\n");
return false;
}
if (!fd->DoTlsHandshake(TlsConfigBase::BNET_TLS_AUTO, client, false,
qualified_resource_name.c_str(), client->password.value, jcr)) {
Dmsg0(100, "Could not DoTlsHandshake() with a file daemon\n");
return false;
}
}
char dirname[MAX_NAME_LENGTH];
bstrncpy(dirname, me->name(), sizeof(dirname));
BashSpaces(dirname);
if (!fd->fsend(hello, dirname)) {
Jmsg(jcr, M_FATAL, 0, _("Error sending Hello to File daemon at \"%s:%d\". ERR=%s\n"), fd->host(),
fd->port(), fd->bstrerror());
return false;
}
Dmsg1(debuglevel, "Sent: %s", fd->msg);
bool auth_success;
auth_success = fd->AuthenticateOutboundConnection(jcr, "File Daemon", dirname, client->password, client);
if (!auth_success) {
Dmsg2(debuglevel, "Unable to authenticate with File daemon at \"%s:%d\"\n", fd->host(), fd->port());
Jmsg(jcr, M_FATAL, 0,
_("Unable to authenticate with File daemon at \"%s:%d\". Possible causes:\n"
"Passwords or names not the same or\n"
"TLS negotiation failed or\n"
"Maximum Concurrent Jobs exceeded on the FD or\n"
"FD networking messed up (restart daemon).\n"
"Please see %s for help.\n"),
fd->host(), fd->port(), MANUAL_AUTH_URL);
return false;
}
Dmsg1(116, ">filed: %s", fd->msg);
if (fd->recv() <= 0) {
Dmsg1(debuglevel, _("Bad response from File daemon to Hello command: ERR=%s\n"), BnetStrerror(fd));
Jmsg(jcr, M_FATAL, 0, _("Bad response from File daemon at \"%s:%d\" to Hello command: ERR=%s\n"),
fd->host(), fd->port(), fd->bstrerror());
return false;
}
Dmsg1(110, "<filed: %s", fd->msg);
jcr->FDVersion = 0;
if (!bstrncmp(fd->msg, FDOKhello, sizeof(FDOKhello)) &&
sscanf(fd->msg, FDOKnewHello, &jcr->FDVersion) != 1) {
Dmsg0(debuglevel, _("File daemon rejected Hello command\n"));
Jmsg(jcr, M_FATAL, 0, _("File daemon at \"%s:%d\" rejected Hello command\n"), fd->host(), fd->port());
return false;
}
return true;
}
bool AuthenticateFileDaemon(BareosSocket *fd, char *client_name)
{
ClientResource *client;
bool auth_success = false;
UnbashSpaces(client_name);
client = (ClientResource *)my_config->GetResWithName(R_CLIENT, client_name);
if (client) {
if (IsConnectFromClientAllowed(client)) {
auth_success =
fd->AuthenticateInboundConnection(NULL, "File Daemon", client_name, client->password, client);
}
}
/*
* Authorization Completed
*/
if (!auth_success) {
fd->fsend("%s", _(dir_not_authorized_message));
Emsg4(M_ERROR, 0, _("Unable to authenticate client \"%s\" at %s:%s:%d.\n"), client_name, fd->who(),
fd->host(), fd->port());
sleep(5);
return false;
}
fd->fsend("1000 OK: %s Version: %s (%s)\n", my_name, VERSION, BDATE);
return true;
}
static bool NumberOfConsoleConnectionsExceeded()
{
JobControlRecord *jcr;
unsigned int cnt = 0;
foreach_jcr(jcr)
{
if (jcr->is_JobType(JT_CONSOLE)) { cnt++; }
}
endeach_jcr(jcr);
return (cnt >= me->MaxConsoleConnections) ? true : false;
}
static bool GetConsoleName(BareosSocket *ua_sock, std::string &name)
{
char buffer[MAX_NAME_LENGTH]; /* zero terminated C-string */
if (sscanf(ua_sock->msg, "Hello %127s calling\n", buffer) != 1) {
Emsg4(M_ERROR, 0, _("UA Hello from %s:%s:%d is invalid. Got: %s\n"), ua_sock->who(), ua_sock->host(), ua_sock->port(),
ua_sock->msg);
return false;
}
UnbashSpaces(buffer);
name = buffer;
return true;
}
static void SendErrorMessage(std::string console_name, UaContext *ua)
{
ua->UA_sock->fsend("%s", _(dir_not_authorized_message));
Emsg4(M_ERROR, 0, _("Unable to authenticate console \"%s\" at %s:%s:%d.\n"), console_name.c_str(),
ua->UA_sock->who(), ua->UA_sock->host(), ua->UA_sock->port());
sleep(5);
}
static void SendOkMessage(UaContext *ua)
{
ua->UA_sock->fsend(_("1000 OK: %s Version: %s (%s)\n"), my_name, VERSION, BDATE);
}
static bool OptionalAuthenticateRootConsole(std::string console_name, UaContext *ua, bool &auth_success)
{
const std::string root_console_name { "*UserAgent*" };
if (console_name != root_console_name) {
return false; /* no need to evaluate auth_success */
}
auth_success = ua->UA_sock->AuthenticateInboundConnection(NULL, "Console", root_console_name.c_str(), me->password, me);
return true;
}
static void AuthenticateNamedConsole(std::string console_name, UaContext *ua, bool &auth_success)
{
ConsoleResource *cons;
cons = (ConsoleResource *)my_config->GetResWithName(R_CONSOLE, console_name.c_str());
if (!cons) { /* if console resource cannot be obtained is treated as an error */
auth_success = false;
return;
}
if (!ua->UA_sock->AuthenticateInboundConnection(NULL, "Console", console_name.c_str(), cons->password, cons)) {
ua->cons = nullptr;
auth_success = false;
} else {
ua->cons = cons;
auth_success = true;
}
}
#if defined(HAVE_PAM)
static void LookupTokenFromSocketStream(BareosSocket *ua_sock, const std::string& token, std::string& output)
{
std::unique_ptr<char> buffer(new char[token.size()]);
memset(buffer.get(), 0, token.size());
int flags = ua_sock->SetNonblocking();
int tries = 3;
bool ready = false;
do {
Bmicrosleep(1,0);
int ret = ::recv(ua_sock->fd_, buffer.get(), token.size(), MSG_PEEK);
if (ret == (int)token.size()) {
if (ua_sock->recv() <= 0) { return; }
std::string temp(ua_sock->msg);
output = temp.substr(temp.find(':')+1);
ready = true;
}
} while (tries-- && !ready);
ua_sock->RestoreBlocking(flags);
}
static void LookupOptionalUsername(BareosSocket *ua_sock, std::string& pam_username)
{
const std::string token {"@@username:"};
LookupTokenFromSocketStream(ua_sock, token, pam_username);
}
static void LookupOptionalPassword(BareosSocket *ua_sock, std::string& pam_password)
{
const std::string token {"@@password:"};
LookupTokenFromSocketStream(ua_sock, token, pam_password);
}
#endif /* HAVE PAM */
static bool OptionalAuthenticatePamUser(std::string console_name, UaContext *ua, bool &auth_success)
{
ConsoleResource *cons = (ConsoleResource *)my_config->GetResWithName(R_CONSOLE, console_name.c_str());
#if defined(HAVE_PAM)
{
if (!cons) { /* if console resource cannot be obtained is treated as an error */
auth_success = false;
return true;
}
/* no need to evaluate auth_success if no pam is required */
if (!cons->use_pam_authentication_) { return false; }
std::string pam_username;
std::string pam_password;
LookupOptionalUsername(ua->UA_sock, pam_username);
LookupOptionalPassword(ua->UA_sock, pam_password);
std::string authenticated_username;
if (!PamAuthenticateUser(ua->UA_sock, pam_username, pam_password, authenticated_username)) {
ua->cons = nullptr;
auth_success = false;
} else {
ConsoleResource *user = (ConsoleResource *)my_config->GetResWithName(R_CONSOLE,
authenticated_username.c_str());
if (!user) {
ua->cons = nullptr;
auth_success = false;
} else {
ua->cons = user;
auth_success = true;
}
}
return true;
} /* HAVE PAM */
#else /* !HAVE_PAM */
if (cons && cons->use_pam_authentication_) {
Emsg0(M_ERROR, 0, _("PAM is not available on this director\n"));
auth_success = false;
return true;
} else {
return false; /* auth_success can be ignored */
}
#endif /* !HAVE_PAM */
}
bool AuthenticateUserAgent(UaContext *ua)
{
std::string console_name;
if (!GetConsoleName(ua->UA_sock, console_name)) {
return false;
}
if (NumberOfConsoleConnectionsExceeded()) {
ua->UA_sock->fsend("%s", _(dir_not_authorized_message));
Emsg0(M_ERROR, 0, _("Number of console connections exceeded MaximumConsoleConnections\n"));
return false;
}
bool auth_success = false;
if (OptionalAuthenticateRootConsole(console_name, ua, auth_success)) {
if (!auth_success) {
SendErrorMessage(console_name, ua);
return false;
} else {
SendOkMessage(ua);
}
} else {
AuthenticateNamedConsole(console_name, ua, auth_success);
if (!auth_success) {
SendErrorMessage(console_name, ua);
return false;
} else {
SendOkMessage(ua);
}
if (OptionalAuthenticatePamUser(console_name, ua, auth_success)) {
if (!auth_success) {
SendErrorMessage(console_name, ua);
return false;
}
}
}
return true;
}
} /* namespace directordaemon */