/
bsock_test.cc
477 lines (378 loc) · 14.5 KB
/
bsock_test.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2018-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.
*/
#include "bsock_test.h"
#include "create_resource.h"
#include "tests/bareos_test_sockets.h"
#include "tests/init_openssl.h"
#include "gtest/gtest.h"
#include <iostream>
#include <fstream>
#include <string>
#include <thread>
#include <future>
#include "include/bareos.h"
#include "console/console.h"
#include "console/console_conf.h"
#include "stored/stored_conf.h"
#include "dird/dird_conf.h"
#include "dird/dird_globals.h"
#include "lib/tls_openssl.h"
#include "lib/bnet.h"
#include "lib/bstringlist.h"
#include "include/jcr.h"
#define CLIENT_AS_A_THREAD 0
class UaContext {
BareosSocket *UA_sock;
};
class StorageResource;
#include "dird/authenticate.h"
#define MIN_MSG_LEN 15
#define MAX_MSG_LEN 175 + 25
#define CONSOLENAME "testconsole"
#define CONSOLEPASSWORD "secret"
static std::string cipher_server, cipher_client;
static std::unique_ptr<directordaemon::ConsoleResource> dir_cons_config;
static std::unique_ptr<directordaemon::DirectorResource> dir_dir_config;
static std::unique_ptr<console::DirectorResource> cons_dir_config;
static std::unique_ptr<console::ConsoleResource> cons_cons_config;
void InitForTest()
{
InitOpenSsl();
dir_cons_config.reset(directordaemon::CreateAndInitializeNewConsoleResource());
dir_dir_config.reset(directordaemon::CreateAndInitializeNewDirectorResource());
cons_dir_config.reset(console::CreateAndInitializeNewDirectorResource());
cons_cons_config.reset(console::CreateAndInitializeNewConsoleResource());
directordaemon::me = dir_dir_config.get();
console::me = cons_cons_config.get();
setlocale(LC_ALL, "");
bindtextdomain("bareos", LOCALEDIR);
textdomain("bareos");
debug_level = 0;
SetTrace(0);
working_directory = "/tmp";
MyNameIs(0, NULL, "bsock_test");
InitMsg(NULL, NULL);
}
static void clone_a_server_socket(BareosSocket* bs)
{
std::unique_ptr<BareosSocket> bs2(bs->clone());
bs2->fsend("cloned-bareos-socket-0987654321");
bs2->close();
bs->fsend("bareos-socket-1234567890");
}
static void start_bareos_server(std::promise<bool> *promise, std::string console_name,
std::string console_password, std::string server_address, int server_port)
{
int newsockfd = create_accepted_server_socket(server_port);
if (newsockfd < 0) {
return;
}
std::unique_ptr<BareosSocket> bs(create_new_bareos_socket(newsockfd));
char *name = (char *)console_name.c_str();
s_password *password = new (s_password);
password->encoding = p_encoding_md5;
password->value = (char *)console_password.c_str();
bool success = false;
if (bs->recv() <= 0) {
Dmsg1(10, _("Connection request from %s failed.\n"), bs->who());
} else if (bs->message_length < MIN_MSG_LEN || bs->message_length > MAX_MSG_LEN) {
Dmsg2(10, _("Invalid connection from %s. Len=%d\n"), bs->who(), bs->message_length);
} else {
Dmsg1(10, "Cons->Dir: %s", bs->msg);
if (!bs->AuthenticateInboundConnection(NULL, "Console", name, *password, dir_cons_config.get())) {
Dmsg0(10, "Server: inbound auth failed\n");
} else {
bs->fsend(_("1000 OK: %s Version: %s (%s)\n"), my_name, VERSION, BDATE);
Dmsg0(10, "Server: inbound auth successful\n");
std::string cipher;
if (bs->tls_conn) {
cipher = bs->tls_conn->TlsCipherGetName();
Dmsg1(10, "Server used cipher: <%s>\n", cipher.c_str());
cipher_server = cipher;
}
if (dir_cons_config->IsTlsConfigured()) {
Dmsg0(10, bs->TlsEstablished() ? "Tls enable\n" : "Tls failed to establish\n");
success = bs->TlsEstablished();
} else {
Dmsg0(10, "Tls disabled by command\n");
if (bs->TlsEstablished()) {
Dmsg0(10, "bs->tls_established_ should be false but is true\n");
}
success = !bs->TlsEstablished();
}
}
}
if (success) {
clone_a_server_socket(bs.get());
}
bs->close();
promise->set_value(success);
}
static void clone_a_client_socket(std::shared_ptr<BareosSocket> UA_sock)
{
std::string received_msg;
std::string orig_msg2("cloned-bareos-socket-0987654321");
std::unique_ptr<BareosSocket> UA_sock2(UA_sock->clone());
UA_sock2->recv();
received_msg = UA_sock2->msg;
EXPECT_STREQ(orig_msg2.c_str(), received_msg.c_str());
UA_sock2->close();
std::string orig_msg("bareos-socket-1234567890");
UA_sock->recv();
received_msg = UA_sock->msg;
EXPECT_STREQ(orig_msg.c_str(), received_msg.c_str());
}
#if CLIENT_AS_A_THREAD
static int connect_to_server(std::string console_name, std::string console_password,
std::string server_address, int server_port)
#else
static bool connect_to_server(std::string console_name, std::string console_password,
std::string server_address, int server_port)
#endif
{
utime_t heart_beat = 0;
JobControlRecord jcr;
memset(&jcr, 0, sizeof(jcr));
char *name = (char *)console_name.c_str();
s_password *password = new (s_password);
password->encoding = p_encoding_md5;
password->value = (char *)console_password.c_str();
std::shared_ptr<BareosSocketTCP> UA_sock(New(BareosSocketTCP));
UA_sock->sleep_time_after_authentication_error = 0;
jcr.dir_bsock = UA_sock.get();
bool success = false;
if (!UA_sock->connect(NULL, 1, 15, heart_beat, "Director daemon", (char *)server_address.c_str(),
NULL, server_port, false)) {
Dmsg0(10, "socket connect failed\n");
} else {
Dmsg0(10, "socket connect OK\n");
uint32_t response_id = kMessageIdUnknown;
BStringList response_args;
if (!UA_sock->ConsoleAuthenticateWithDirector(&jcr, name, *password, cons_dir_config.get(), response_args, response_id)) {
Emsg0(M_ERROR, 0, "Authenticate Failed\n");
} else {
EXPECT_EQ(response_id, kMessageIdOk) << "Received the wrong message id.";
Dmsg0(10, "Authenticate Connect to Server successful!\n");
std::string cipher;
if (UA_sock->tls_conn) {
cipher = UA_sock->tls_conn->TlsCipherGetName();
Dmsg1(10, "Client used cipher: <%s>\n", cipher.c_str());
cipher_client = cipher;
}
if (cons_dir_config->IsTlsConfigured()) {
Dmsg0(10, UA_sock->TlsEstablished() ? "Tls enable\n" : "Tls failed to establish\n");
success = UA_sock->TlsEstablished();
} else {
Dmsg0(10, "Tls disabled by command\n");
if (UA_sock->TlsEstablished()) {
Dmsg0(10, "UA_sock->tls_established_ should be false but is true\n");
}
success = !UA_sock->TlsEstablished();
}
}
}
if (success) {
clone_a_client_socket(UA_sock);
}
if (UA_sock) {
UA_sock->close();
}
return success;
}
std::string client_cons_name;
std::string client_cons_password;
std::string server_cons_name;
std::string server_cons_password;
TEST(bsock, auth_works)
{
listening_server_port_number++;
std::promise<bool> promise;
std::future<bool> future = promise.get_future();
client_cons_name = "clientname";
client_cons_password = "verysecretpassword";
server_cons_name = client_cons_name;
server_cons_password = client_cons_password;
InitForTest();
cons_dir_config->tls_enable_ = false;
dir_cons_config->tls_enable_ = false;
Dmsg0(10, "starting listen thread...\n");
std::thread server_thread(start_bareos_server, &promise, server_cons_name, server_cons_password,
HOST, listening_server_port_number);
Dmsg0(10, "connecting to server\n");
EXPECT_TRUE(connect_to_server(client_cons_name, client_cons_password, HOST, listening_server_port_number));
server_thread.join();
EXPECT_TRUE(future.get());
}
TEST(bsock, auth_works_with_different_names)
{
listening_server_port_number++;
std::promise<bool> promise;
std::future<bool> future = promise.get_future();
client_cons_name = "clientname";
client_cons_password = "verysecretpassword";
server_cons_name = "differentclientname";
server_cons_password = client_cons_password;
InitForTest();
cons_dir_config->tls_enable_ = false;
dir_cons_config->tls_enable_ = false;
Dmsg0(10, "starting listen thread...\n");
std::thread server_thread(start_bareos_server, &promise, server_cons_name, server_cons_password,
HOST, listening_server_port_number);
Dmsg0(10, "connecting to server\n");
EXPECT_TRUE(connect_to_server(client_cons_name, client_cons_password, HOST, listening_server_port_number));
server_thread.join();
EXPECT_TRUE(future.get());
}
TEST(bsock, auth_fails_with_different_passwords)
{
listening_server_port_number++;
std::promise<bool> promise;
std::future<bool> future = promise.get_future();
client_cons_name = "clientname";
client_cons_password = "verysecretpassword";
server_cons_name = client_cons_name;
server_cons_password = "othersecretpassword";
InitForTest();
cons_dir_config->tls_enable_ = false;
dir_cons_config->tls_enable_ = false;
Dmsg0(10, "starting listen thread...\n");
std::thread server_thread(start_bareos_server, &promise, server_cons_name, server_cons_password,
HOST, listening_server_port_number);
Dmsg0(10, "connecting to server\n");
EXPECT_FALSE(connect_to_server(client_cons_name, client_cons_password, HOST, listening_server_port_number));
server_thread.join();
EXPECT_FALSE(future.get());
}
TEST(bsock, auth_works_with_tls_cert)
{
listening_server_port_number++;
std::promise<bool> promise;
std::future<bool> future = promise.get_future();
client_cons_name = "clientname";
client_cons_password = "verysecretpassword";
server_cons_name = client_cons_name;
server_cons_password = client_cons_password;
InitForTest();
cons_dir_config->tls_enable_ = true;
dir_cons_config->tls_enable_ = true;
Dmsg0(10, "starting listen thread...\n");
std::thread server_thread(start_bareos_server, &promise, server_cons_name, server_cons_password,
HOST, listening_server_port_number);
Dmsg0(10, "connecting to server\n");
#if CLIENT_AS_A_THREAD
std::thread client_thread(connect_to_server, client_cons_name, client_cons_password,
HOST, listening_server_port_number, cons_dir_config.get());
client_thread.join();
#else
EXPECT_TRUE(connect_to_server(client_cons_name, client_cons_password, HOST, listening_server_port_number));
#endif
server_thread.join();
EXPECT_TRUE(cipher_server == cipher_client);
EXPECT_TRUE(future.get());
}
class BareosSocketTCPMock : public BareosSocketTCP
{
public:
BareosSocketTCPMock(std::string &t)
: test_variable_(t) {}
virtual ~BareosSocketTCPMock() {
test_variable_ = "Destructor Called";
}
std::string &test_variable_;
};
TEST(bsock, create_bareos_socket_unique_ptr)
{
std::string test_variable;
{
std::unique_ptr<BareosSocketTCPMock,std::function<void(BareosSocket*)>> p;
{
std::unique_ptr<BareosSocketTCPMock,std::function<void(BareosSocket*)>>
p1(New(BareosSocketTCPMock(test_variable)), [](BareosSocket *p) {delete p;});
EXPECT_NE(p1.get(), nullptr);
p = std::move(p1);
EXPECT_EQ(p1.get(), nullptr);
EXPECT_TRUE(test_variable.empty());
}
EXPECT_NE(p.get(), nullptr);
}
EXPECT_STREQ(test_variable.c_str(), "Destructor Called");
}
#if 0
TEST(bsock, auth_works_with_tls_psk)
{
port++;
std::promise<bool> promise;
std::future<bool> future = promise.get_future();
InitForTest();
cons_dir_config->tls_psk.enable = true;
cons_dir_config->tls_cert_.enable = true;
dir_cons_config->tls_psk.enable = true;
Dmsg0(10, "starting listen thread...\n");
std::thread server_thread(start_bareos_server, &promise, dir_cons_config->hdr.name, dir_cons_config->password.value,
HOST, port);
Dmsg0(10, "connecting to server\n");
#if CLIENT_AS_A_THREAD
std::thread client_thread(connect_to_server, console::me->hdr.name, cons_dir_config->password.value, HOST, port, cons_dir_config.get());
client_thread.join();
#else
EXPECT_TRUE(connect_to_server(console::me->hdr.name, cons_dir_config->password.value, HOST, port));
#endif
server_thread.join();
EXPECT_TRUE(cipher_server == cipher_client);
EXPECT_TRUE(future.get());
}
TEST(bsock, auth_fails_with_different_names_with_tls_psk)
{
port++;
std::promise<bool> promise;
std::future<bool> future = promise.get_future();
InitForTest();
dir_cons_config->hdr.name = (char*)"differentclientname";
dir_cons_config->password.value = (char*)"verysecretpassword";
cons_dir_config->tls_psk.enable = true;
cons_dir_config->tls_cert_.enable = true;
dir_cons_config->tls_psk.enable = true;
Dmsg0(10, "starting listen thread...\n");
std::thread server_thread(start_bareos_server, &promise, dir_cons_config->hdr.name, dir_cons_config->password.value,
HOST, port);
Dmsg0(10, "connecting to server\n");
#if CLIENT_AS_A_THREAD
std::thread client_thread(connect_to_server, console::me->hdr.name, cons_dir_config->password.value, HOST, port, cons_dir_config.get());
client_thread.join();
#else
EXPECT_FALSE(connect_to_server(console::me->hdr.name, cons_dir_config->password.value, HOST, port));
#endif
server_thread.join();
EXPECT_TRUE(cipher_server == cipher_client);
EXPECT_FALSE(future.get());
}
#endif
TEST(BNet, FormatAndSendResponseMessage)
{
std::unique_ptr<TestSockets> test_sockets(create_connected_server_and_client_bareos_socket());
EXPECT_NE(test_sockets.get(), nullptr) << "Could not create Bareos test sockets.";
if (!test_sockets) { return; }
std::string m("Test123");
test_sockets->client->FormatAndSendResponseMessage(kMessageIdOk, m);
uint32_t id = kMessageIdUnknown;
BStringList args;
bool ok = test_sockets->server->ReceiveAndEvaluateResponseMessage(id, args);
EXPECT_TRUE(ok) << "ReceiveAndEvaluateResponseMessage errored.";
EXPECT_EQ(id, kMessageIdOk) << "Wrong MessageID received.";
std::string test("1000 Test123");
EXPECT_STREQ(args.JoinReadable().c_str(), test.c_str());
}