-
Notifications
You must be signed in to change notification settings - Fork 256
/
authenticate_console.cc
442 lines (387 loc) · 13.6 KB
/
authenticate_console.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*
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-2023 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.
*/
#include "include/bareos.h"
#include "dird/authenticate_console.h"
#include "dird/dird.h"
#if defined(HAVE_PAM)
# include "dird/auth_pam.h"
#endif
#include "dird/dird_globals.h"
#include "lib/bnet.h"
#include "lib/bsock.h"
#include "lib/qualified_resource_name_type_converter.h"
#include "lib/bstringlist.h"
#include "lib/jcr.h"
#include "lib/parse_conf.h"
#include "lib/util.h"
#include "lib/version.h"
#include "include/version_numbers.h"
#include "console_connection_lease.h"
namespace directordaemon {
enum class OptionResult
{
Completed,
Skipped
};
class ConsoleAuthenticator {
public:
ConsoleAuthenticator(UaContext* ua, const std::string& console_name);
virtual ~ConsoleAuthenticator() = default;
ConsoleAuthenticator(const ConsoleAuthenticator& other) = delete;
virtual OptionResult AuthenticateDefaultConsole() = 0;
virtual void AuthenticateNamedConsole() = 0;
virtual OptionResult AuthenticatePamUser() = 0;
virtual bool SendInfoMessage() { return false; }
bool auth_success_;
std::string console_name_;
protected:
OptionResult DoDefaultAuthentication();
void DoNamedAuthentication();
UaContext* ua_;
ConsoleResource* optional_console_resource_;
};
ConsoleAuthenticator::ConsoleAuthenticator(UaContext* ua,
const std::string& console_name)
: auth_success_(false), console_name_(console_name), ua_(ua)
{
optional_console_resource_ = (ConsoleResource*)my_config->GetResWithName(
R_CONSOLE, console_name_.c_str());
}
OptionResult ConsoleAuthenticator::DoDefaultAuthentication()
{
const std::string default_console_name{"*UserAgent*"};
if (console_name_ != default_console_name) { return OptionResult::Skipped; }
auth_success_ = ua_->UA_sock->AuthenticateInboundConnection(
NULL, my_config, default_console_name.c_str(), me->password_, me);
ua_->user_acl = nullptr;
return OptionResult::Completed;
}
void ConsoleAuthenticator::DoNamedAuthentication()
{
if (!optional_console_resource_) {
Dmsg1(200, "Could not find resource for console %s\n",
console_name_.c_str());
auth_success_ = false;
return;
}
auth_success_ = ua_->UA_sock->AuthenticateInboundConnection(
NULL, my_config, console_name_.c_str(),
optional_console_resource_->password_, optional_console_resource_);
if (auth_success_) {
ua_->user_acl = &optional_console_resource_->user_acl;
} else {
ua_->user_acl = nullptr;
Dmsg1(200, "Could not authenticate console %s\n", console_name_.c_str());
}
}
class ConsoleAuthenticatorBefore_18_2 : public ConsoleAuthenticator {
public:
ConsoleAuthenticatorBefore_18_2(UaContext* ua,
const std::string& console_name)
: ConsoleAuthenticator(ua, console_name)
{
}
OptionResult AuthenticateDefaultConsole() override;
void AuthenticateNamedConsole() override;
OptionResult AuthenticatePamUser() override;
private:
bool SendOkMessageWithSpaceSeparators()
{
if (ua_) {
std::string cipher = ua_->UA_sock->GetCipherMessageString();
if (ua_->UA_sock) {
return ua_->UA_sock->fsend(_("1000 OK: %s Version: %s (%s) "
"-- %s\n"),
my_name, kBareosVersionStrings.Full,
kBareosVersionStrings.Date, cipher.c_str());
}
}
return false;
}
bool SendNotAuthorizedMessageWithSpaceSeparators()
{
if (ua_ && ua_->UA_sock) {
return ua_->UA_sock->fsend("%s", "1999 You are not authorized.\n");
}
return false;
}
};
OptionResult ConsoleAuthenticatorBefore_18_2::AuthenticateDefaultConsole()
{
if (DoDefaultAuthentication() == OptionResult::Skipped) {
return OptionResult::Skipped;
}
if (auth_success_) {
auth_success_ = SendOkMessageWithSpaceSeparators();
} else {
SendNotAuthorizedMessageWithSpaceSeparators();
}
if (!auth_success_) {
Dmsg1(200, "Could not authenticate outdated console %s\n",
console_name_.c_str());
}
return OptionResult::Completed;
}
void ConsoleAuthenticatorBefore_18_2::AuthenticateNamedConsole()
{
DoNamedAuthentication();
if (auth_success_) {
auth_success_ = SendOkMessageWithSpaceSeparators();
} else {
SendNotAuthorizedMessageWithSpaceSeparators();
}
}
OptionResult ConsoleAuthenticatorBefore_18_2::AuthenticatePamUser()
{
if (!optional_console_resource_) { return OptionResult::Skipped; }
if (optional_console_resource_->use_pam_authentication_) {
Dmsg1(200, "PAM authentication using outdated Bareos console denied: %s\n",
console_name_.c_str());
auth_success_
= false; /* console before 18_2 cannot do pam authentication */
return OptionResult::Completed;
}
return OptionResult::Skipped;
}
class ConsoleAuthenticatorFrom_18_2 : public ConsoleAuthenticator {
public:
ConsoleAuthenticatorFrom_18_2(UaContext* ua, const std::string& console_name)
: ConsoleAuthenticator(ua, console_name)
{
}
OptionResult AuthenticateDefaultConsole() override;
void AuthenticateNamedConsole() override;
OptionResult AuthenticatePamUser() override;
bool SendInfoMessage() override;
private:
bool SendResponseMessage(uint32_t response_id, bool send_version_info);
};
bool ConsoleAuthenticatorFrom_18_2::SendResponseMessage(uint32_t response_id,
bool send_version_info)
{
std::string message;
if (send_version_info) {
char version_info[128];
::snprintf(version_info, 100, "OK: %s Version: %s (%s)", my_name,
kBareosVersionStrings.Full, kBareosVersionStrings.Date);
message = version_info;
}
return ua_->UA_sock->FormatAndSendResponseMessage(response_id, message);
}
bool ConsoleAuthenticatorFrom_18_2::SendInfoMessage()
{
std::string message;
message += kBareosVersionStrings.ServicesMessage;
message += "\n";
message += "You are ";
if (ua_->user_acl) {
message += "logged in as: ";
message += ua_->user_acl->corresponding_resource->resource_name_;
} else {
message += "connected using the default console";
}
auth_success_ = ua_->UA_sock->FormatAndSendResponseMessage(
kMessageIdInfoMessage, message);
return true;
}
OptionResult ConsoleAuthenticatorFrom_18_2::AuthenticateDefaultConsole()
{
if (DoDefaultAuthentication() == OptionResult::Skipped) {
return OptionResult::Skipped;
}
if (auth_success_) {
if (!SendResponseMessage(kMessageIdOk, true)) { auth_success_ = false; }
}
if (!auth_success_) {
Dmsg1(200, "Could not authenticate console %s\n", console_name_.c_str());
}
return OptionResult::Completed;
}
void ConsoleAuthenticatorFrom_18_2::AuthenticateNamedConsole()
{
DoNamedAuthentication();
if (!auth_success_) { return; }
uint32_t response_id = kMessageIdOk;
bool send_version = true;
if (optional_console_resource_
&& optional_console_resource_->use_pam_authentication_) {
response_id = kMessageIdPamRequired;
send_version = false;
}
if (!SendResponseMessage(response_id, send_version)) {
Dmsg1(200, "Send of response message failed %s\n", console_name_.c_str());
auth_success_ = false;
}
}
OptionResult ConsoleAuthenticatorFrom_18_2::AuthenticatePamUser()
{
#if !defined(HAVE_PAM)
{
if (optional_console_resource_
&& optional_console_resource_->use_pam_authentication_) {
Emsg0(M_ERROR, 0, _("PAM is not available on this director\n"));
auth_success_ = false;
return OptionResult::Completed;
} else {
return OptionResult::Skipped;
}
}
#else /* HAVE_PAM */
{
if (!optional_console_resource_) {
auth_success_ = false;
return OptionResult::Completed;
}
if (!optional_console_resource_->use_pam_authentication_) {
return OptionResult::Skipped;
}
uint32_t response_id;
BStringList message_arguments;
if (!ua_->UA_sock->ReceiveAndEvaluateResponseMessage(response_id,
message_arguments)) {
Dmsg2(100, "Could not evaluate response_id: %d - %d\n", response_id,
message_arguments.JoinReadable().c_str());
auth_success_ = false;
return OptionResult::Completed;
}
std::string pam_username;
std::string pam_password;
if (response_id == kMessageIdPamUserCredentials) {
Dmsg0(200, "Console chooses PAM direct credentials\n");
if (message_arguments.size() < 3) {
Dmsg0(200, "Console sent wrong number of credentials\n");
auth_success_ = false;
return OptionResult::Completed;
} else {
pam_username = message_arguments.at(1);
pam_password = message_arguments.at(2);
}
} else if (response_id == kMessageIdPamInteractive) {
Dmsg0(200, "Console chooses PAM interactive\n");
}
Dmsg1(200, "Try to authenticate user using PAM:%s\n", pam_username.c_str());
std::string authenticated_username;
if (!PamAuthenticateUser(ua_->UA_sock, pam_username, pam_password,
authenticated_username)) {
ua_->user_acl = nullptr;
auth_success_ = false;
} else {
UserResource* user = (UserResource*)my_config->GetResWithName(
R_USER, authenticated_username.c_str());
if (!user) {
Dmsg1(200, "No user config found for user %s\n",
authenticated_username.c_str());
ua_->user_acl = nullptr;
auth_success_ = false;
} else {
ua_->user_acl = &user->user_acl;
auth_success_ = true;
}
}
if (auth_success_) {
if (!SendResponseMessage(kMessageIdOk, true)) { auth_success_ = false; }
}
return OptionResult::Completed;
} /* HAVE PAM */
#endif /* !HAVE_PAM */
}
static void LogErrorMessage(std::string console_name, UaContext* ua)
{
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());
}
static bool GetConsoleNameAndVersion(BareosSocket* ua_sock,
std::string& name_out,
BareosVersionNumber& version_out)
{
std::string name;
std::string r_code_str_unused;
BareosVersionNumber version = BareosVersionNumber::kUndefined;
if (GetNameAndResourceTypeAndVersionFromHello(ua_sock->msg, name,
r_code_str_unused, version)) {
name_out = name;
version_out = version;
return true;
} else {
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;
}
}
static ConsoleAuthenticator* CreateConsoleAuthenticator(UaContext* ua)
{
std::string console_name;
BareosVersionNumber version = BareosVersionNumber::kUndefined;
if (!GetConsoleNameAndVersion(ua->UA_sock, console_name, version)) {
return nullptr;
}
ua->UA_sock->connected_daemon_version_
= version; /* this is redundant if using cleartext handshake */
if (version < BareosVersionNumber::kRelease_18_2) {
return new ConsoleAuthenticatorBefore_18_2(ua, console_name);
} else /* == kRelease_18_2 */ {
return new ConsoleAuthenticatorFrom_18_2(ua, console_name);
}
}
bool AuthenticateConsole(UaContext* ua)
{
auto num_leases = ConsoleConnectionLease::get_num_leases();
std::unique_ptr<ConsoleAuthenticator> console_authenticator(
CreateConsoleAuthenticator(ua));
if (!console_authenticator) { return false; }
if (console_authenticator->AuthenticateDefaultConsole()
== OptionResult::Completed) {
if (!console_authenticator->auth_success_) {
LogErrorMessage(console_authenticator->console_name_, ua);
return false;
}
if (num_leases > me->MaxConsoleConnections) {
Emsg0(M_INFO, 0,
_("Number of console connections exceeded\n"
"Maximum: %u, Current: %zu\n"),
me->MaxConsoleConnections, num_leases);
}
} else {
if (num_leases > me->MaxConsoleConnections) {
Emsg0(M_ERROR, 0,
_("Number of console connections exceeded "
"Maximum :%u, Current: %zu\n"),
me->MaxConsoleConnections, num_leases);
return false;
}
console_authenticator->AuthenticateNamedConsole();
if (!console_authenticator->auth_success_) {
LogErrorMessage(console_authenticator->console_name_, ua);
return false;
}
if (console_authenticator->AuthenticatePamUser()
== OptionResult::Completed) {
if (!console_authenticator->auth_success_) {
LogErrorMessage(console_authenticator->console_name_, ua);
return false;
}
}
}
if (console_authenticator->SendInfoMessage()) {
if (!console_authenticator->auth_success_) { return false; }
}
return true;
}
} /* namespace directordaemon */