/
bsock.cc
688 lines (609 loc) · 20.5 KB
/
bsock.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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2007-2011 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.
*/
/**
* Network Utility Routines
*
* Kern Sibbald
*/
#include "include/bareos.h"
#include "include/jcr.h"
#include "lib/bnet.h"
#include "lib/cram_md5.h"
#include "lib/tls.h"
#include "lib/util.h"
static constexpr int debuglevel = 50;
BareosSocket::BareosSocket()
/* public */
: fd_(-1)
, read_seqno(0)
, msg(GetPoolMemory(PM_BSOCK))
, errmsg(GetPoolMemory(PM_MESSAGE))
, spool_fd_(-1)
, src_addr(nullptr)
, in_msg_no(0)
, out_msg_no(0)
, message_length(0)
, timer_start{0}
, b_errno(0)
, blocking_(1)
, errors(0)
, suppress_error_msgs_(false)
, sleep_time_after_authentication_error(5)
, client_addr{0}
, peer_addr{0}
/* protected: */
, jcr_(nullptr)
, who_(nullptr)
, host_(nullptr)
, port_(-1)
, tid_(nullptr)
, data_end_{0}
, FileIndex_(0)
, timed_out_(false)
, terminated_(false)
, cloned_(false)
, spool_(false)
, use_bursting_(false)
, use_keepalive_(true)
, bwlimit_(0)
, nb_bytes_(0)
, last_tick_{0}
, tls_established_(false)
{
Dmsg0(100, "Contruct BareosSocket\n");
}
BareosSocket::BareosSocket(const BareosSocket &other)
{
Dmsg0(100, "Copy Contructor BareosSocket\n");
fd_ = other.fd_;
read_seqno = other.read_seqno;
msg = other.msg;
errmsg = other.errmsg;
spool_fd_ = other.spool_fd_;
src_addr = other.src_addr;
in_msg_no = other.in_msg_no;
out_msg_no = other.out_msg_no;
message_length = other.message_length;
timer_start = other.timer_start;
b_errno = other.b_errno;
blocking_ = other.blocking_;
errors = other.errors;
suppress_error_msgs_ = other.suppress_error_msgs_;
sleep_time_after_authentication_error = other.sleep_time_after_authentication_error;
client_addr = other.client_addr;
peer_addr = other.peer_addr;
tls_conn = other.tls_conn;
/* protected: */
jcr_ = other.jcr_;
mutex_ = other.mutex_;
who_ = other.who_;
host_ = other.host_;
port_ = other.port_;
tid_ = other.tid_;
data_end_ = other.data_end_;
FileIndex_ = other.FileIndex_;
timed_out_ = other.timed_out_;
terminated_ = other.terminated_;
cloned_ = other.cloned_;
spool_ = other.spool_;
use_bursting_ = other.use_bursting_;
use_keepalive_ = other.use_keepalive_;
bwlimit_ = other.bwlimit_;
nb_bytes_ = other.nb_bytes_;
last_tick_ = other.last_tick_;
tls_established_ = other.tls_established_;
}
BareosSocket::~BareosSocket() { Dmsg0(100, "Destruct BareosSocket\n"); }
void BareosSocket::CloseTlsConnectionAndFreeMemory()
{
if (tls_conn) {
tls_conn->TlsBsockShutdown(this);
tls_conn.reset();
}
}
/**
* Copy the address from the configuration dlist that gets passed in
*/
void BareosSocket::SetSourceAddress(dlist *src_addr_list)
{
char allbuf[256 * 10];
IPADDR *addr = nullptr;
Dmsg1(100, "All source addresses %s\n", BuildAddressesString(src_addr_list, allbuf, sizeof(allbuf)));
/*
* Delete the object we already have, if it's allocated
*/
if (src_addr) {
free(src_addr);
src_addr = nullptr;
}
if (src_addr_list) {
addr = (IPADDR *)src_addr_list->first();
src_addr = New(IPADDR(*addr));
}
}
bool BareosSocket::SetLocking()
{
if (mutex_) {
return true;
}
mutex_ = std::make_shared<std::mutex>();
return true;
}
void BareosSocket::ClearLocking()
{
if (mutex_) {
mutex_.reset();
}
}
/**
* Send a signal
*/
bool BareosSocket::signal(int signal)
{
message_length = signal;
if (signal == BNET_TERMINATE) {
suppress_error_msgs_ = true;
}
return send();
}
/**
* Despool spooled attributes
*/
bool BareosSocket::despool(void UpdateAttrSpoolSize(ssize_t size), ssize_t tsize)
{
int32_t pktsiz;
size_t nbytes;
ssize_t last = 0, size = 0;
int count = 0;
JobControlRecord *jcr = get_jcr();
if (lseek(spool_fd_, 0, SEEK_SET) == -1) {
Qmsg(jcr, M_FATAL, 0, _("attr spool I/O error.\n"));
return false;
}
#if defined(HAVE_POSIX_FADVISE) && defined(POSIX_FADV_WILLNEED)
posix_fadvise(spool_fd_, 0, 0, POSIX_FADV_WILLNEED);
#endif
while ((nbytes = read(spool_fd_, (char *)&pktsiz, sizeof(int32_t))) == sizeof(int32_t)) {
size += sizeof(int32_t);
message_length = ntohl(pktsiz);
if (message_length > 0) {
if (message_length > (int32_t)SizeofPoolMemory(msg)) {
msg = ReallocPoolMemory(msg, message_length + 1);
}
nbytes = read(spool_fd_, msg, message_length);
if (nbytes != (size_t)message_length) {
BErrNo be;
Dmsg2(400, "nbytes=%d message_length=%d\n", nbytes, message_length);
Qmsg1(get_jcr(), M_FATAL, 0, _("read attr spool error. ERR=%s\n"), be.bstrerror());
UpdateAttrSpoolSize(tsize - last);
return false;
}
size += nbytes;
if ((++count & 0x3F) == 0) {
UpdateAttrSpoolSize(size - last);
last = size;
}
}
send();
if (jcr && JobCanceled(jcr)) {
return false;
}
}
UpdateAttrSpoolSize(tsize - last);
return true;
}
/**
* Return the string for the error that occurred
* on the socket. Only the first error is retained.
*/
const char *BareosSocket::bstrerror()
{
BErrNo be;
if (errmsg == nullptr) {
errmsg = GetPoolMemory(PM_MESSAGE);
}
PmStrcpy(errmsg, be.bstrerror(b_errno));
return errmsg;
}
/**
* Format and send a message
* Returns: false on error
* true on success
*/
bool BareosSocket::fsend(const char *fmt, ...)
{
va_list arg_ptr;
int maxlen;
if (errors || IsTerminated()) {
return false;
}
/* This probably won't work, but we vsnprintf, then if we
* get a negative length or a length greater than our buffer
* (depending on which library is used), the printf was truncated, so
* get a bigger buffer and try again.
*/
for (;;) {
maxlen = SizeofPoolMemory(msg) - 1;
va_start(arg_ptr, fmt);
message_length = Bvsnprintf(msg, maxlen, fmt, arg_ptr);
va_end(arg_ptr);
if (message_length >= 0 && message_length < (maxlen - 5)) {
break;
}
msg = ReallocPoolMemory(msg, maxlen + maxlen / 2);
}
return send();
}
void BareosSocket::SetKillable(bool killable)
{
if (jcr_) {
jcr_->SetKillable(killable);
}
}
/** Commands sent to Director */
static char hello[] = "Hello %s calling\n";
/** Response from Director */
static char OKhello[] = "1000 OK:";
/**
* Authenticate with Director
*/
bool BareosSocket::AuthenticateWithDirector(JobControlRecord *jcr,
const char *identity,
s_password &password,
char *response,
int response_len,
TlsResource *tls_configuration)
{
char bashed_name[MAX_NAME_LENGTH];
BareosSocket *dir = this; /* for readability */
response[0] = 0;
/*
* Send my name to the Director then do authentication
*/
bstrncpy(bashed_name, identity, sizeof(bashed_name));
BashSpaces(bashed_name);
/*
* Timeout Hello after 5 mins
*/
dir->StartTimer(60 * 5);
dir->fsend(hello, bashed_name);
if (!AuthenticateOutboundConnection(jcr, "Director", identity, password, tls_configuration)) {
goto bail_out;
}
Dmsg1(6, ">dird: %s", dir->msg);
if (dir->recv() <= 0) {
dir->StopTimer();
Bsnprintf(response, response_len,
_("Bad response to Hello command: ERR=%s\n"
"The Director at \"%s:%d\" is probably not running.\n"),
dir->bstrerror(), dir->host(), dir->port());
return false;
}
dir->StopTimer();
Dmsg1(10, "<dird: %s", dir->msg);
if (!bstrncmp(dir->msg, OKhello, sizeof(OKhello) - 1)) {
Bsnprintf(response, response_len, _("Director at \"%s:%d\" rejected Hello command\n"), dir->host(),
dir->port());
return false;
} else {
Bsnprintf(response, response_len, "%s", dir->msg);
}
return true;
bail_out:
dir->StopTimer();
Bsnprintf(response, response_len,
_("Authorization problem with Director at \"%s:%d\"\n"
"Most likely the passwords do not agree.\n"
"If you are using TLS, there may have been a certificate "
"validation error during the TLS handshake.\n"
"Please see %s for help.\n"),
dir->host(), dir->port(), MANUAL_AUTH_URL);
return false;
}
/**
* Depending on the initiate parameter perform one of the following:
*
* - First make him prove his identity and then prove our identity to the Remote.
* - First prove our identity to the Remote and then make him prove his identity.
*/
bool BareosSocket::TwoWayAuthenticate(JobControlRecord *jcr,
const char *what,
const char *identity,
s_password &password,
TlsResource *tls_configuration,
bool initiated_by_remote)
{
bool auth_success = false;
if (jcr && JobCanceled(jcr)) {
Dmsg0(debuglevel, "Failed, because job is canceled.\n");
} else if (password.encoding != p_encoding_md5) {
Jmsg(jcr, M_FATAL, 0,
_("Password encoding is not MD5. You are probably restoring a NDMP Backup "
"with a restore job not configured for NDMP protocol.\n"));
} else {
uint32_t local_tls_policy = GetLocalTlsPolicyFromConfiguration(tls_configuration);
CramMd5Handshake cram_md5_handshake(this, password.value, local_tls_policy);
btimer_t *tid = StartBsockTimer(this, AUTH_TIMEOUT);
auth_success = cram_md5_handshake.DoHandshake(initiated_by_remote);
if (!auth_success) {
Jmsg(jcr, M_FATAL, 0,
_("Authorization key rejected by %s %s.\n"
"Please see %s for help.\n"),
what, identity, MANUAL_AUTH_URL);
} else if (jcr && JobCanceled(jcr)) {
Dmsg0(debuglevel, "Failed, because job is canceled.\n");
auth_success = false;
} else if (!DoTlsHandshake(cram_md5_handshake.RemoteTlsPolicy(), tls_configuration, initiated_by_remote,
identity, password.value, jcr)) {
auth_success = false;
}
if (tid) {
StopBsockTimer(tid);
}
}
if (jcr) {
jcr->authenticated = auth_success;
}
return auth_success;
}
bool BareosSocket::DoTlsHandshake(uint32_t remote_tls_policy,
TlsResource *tls_configuration,
bool initiated_by_remote,
const char *identity,
const char *password,
JobControlRecord *jcr)
{
if (tls_conn) {
return true;
}
TlsConfigBase *selected_local_tls;
selected_local_tls = SelectTlsFromPolicy(tls_configuration, remote_tls_policy);
if (selected_local_tls->GetPolicy() != TlsConfigBase::BNET_TLS_NONE) { /* no tls configuration is ok */
if (!ParameterizeAndInitTlsConnection(tls_configuration, identity, password, initiated_by_remote)) {
return false;
}
if (initiated_by_remote) {
if (!DoTlsHandshakeWithClient(selected_local_tls, identity, password, jcr)) {
return false;
}
} else {
if (!DoTlsHandshakeWithServer(selected_local_tls, identity, password, jcr)) {
return false;
}
}
if (selected_local_tls->GetAuthenticate()) { /* tls authentication only? */
tls_conn->TlsBsockShutdown(this);
CloseTlsConnectionAndFreeMemory(); /* yes, shutdown tls */
}
}
if (!initiated_by_remote) {
if (tls_conn) {
tls_conn->TlsLogConninfo(jcr, host(), port(), who());
} else {
Qmsg(jcr, M_INFO, 0, _("Cleartext connection to %s at %s:%d established\n"), who(), host(), port());
}
}
return true;
}
bool BareosSocket::ParameterizeAndInitTlsConnection(TlsResource *tls_configuration,
const char *identity,
const char *password,
bool initiated_by_remote)
{
if (!tls_configuration->tls_cert.enable && !tls_configuration->tls_psk.enable) {
return true;
}
tls_conn.reset(Tls::CreateNewTlsContext(Tls::TlsImplementationType::kTlsOpenSsl));
if (!tls_conn) {
Qmsg0(BareosSocket::jcr(), M_FATAL, 0, _("TLS connection initialization failed.\n"));
return false;
}
tls_conn->SetTcpFileDescriptor(fd_);
if (tls_configuration->tls_cert.enable) {
const std::string empty;
tls_conn->SetCaCertfile(tls_configuration->tls_cert.CaCertfile ? *tls_configuration->tls_cert.CaCertfile
: empty);
tls_conn->SetCaCertdir(tls_configuration->tls_cert.CaCertdir ? *tls_configuration->tls_cert.CaCertdir
: empty);
tls_conn->SetCrlfile(tls_configuration->tls_cert.crlfile ? *tls_configuration->tls_cert.crlfile : empty);
tls_conn->SetCertfile(tls_configuration->tls_cert.certfile ? *tls_configuration->tls_cert.certfile
: empty);
tls_conn->SetKeyfile(tls_configuration->tls_cert.keyfile ? *tls_configuration->tls_cert.keyfile : empty);
// tls_conn->SetPemCallback(TlsPemCallback); Ueb: --> Wo kommt der Callback her??
tls_conn->SetPemUserdata(tls_configuration->tls_cert.pem_message);
tls_conn->SetDhFile(tls_configuration->tls_cert.dhfile ? *tls_configuration->tls_cert.dhfile
: empty); /* was never used before */
tls_conn->SetCipherList(tls_configuration->tls_cert.cipherlist ? *tls_configuration->tls_cert.cipherlist
: empty);
tls_conn->SetVerifyPeer(tls_configuration->tls_cert.VerifyPeer);
}
if (tls_configuration->tls_psk.enable) {
if (initiated_by_remote) {
tls_conn->SetTlsPskServerContext(tls_configuration->tls_psk.GetTlsPskByFullyQualifiedResourceNameCb);
} else {
const PskCredentials psk_cred(identity, password);
tls_conn->SetTlsPskClientContext(psk_cred);
}
}
if (!tls_conn->init()) {
return false;
}
return true;
}
bool BareosSocket::DoTlsHandshakeWithClient(TlsConfigBase *selected_local_tls,
const char *identity,
const char *password,
JobControlRecord *jcr)
{
std::vector<std::string> verify_list;
if (selected_local_tls->GetVerifyPeer()) {
verify_list = selected_local_tls->AllowedCertificateCommonNames();
}
if (BnetTlsServer(this, verify_list)) {
return true;
}
Jmsg(jcr, M_FATAL, 0, _("TLS negotiation failed.\n"));
Dmsg0(debuglevel, "TLS negotiation failed.\n");
return false;
}
bool BareosSocket::DoTlsHandshakeWithServer(TlsConfigBase *selected_local_tls,
const char *identity,
const char *password,
JobControlRecord *jcr)
{
if (BnetTlsClient(this, selected_local_tls->GetVerifyPeer(),
selected_local_tls->AllowedCertificateCommonNames())) {
return true;
}
Jmsg(jcr, M_FATAL, 0, _("TLS negotiation failed.\n"));
Dmsg0(debuglevel, "TLS negotiation failed.\n");
return false;
}
bool BareosSocket::AuthenticateOutboundConnection(JobControlRecord *jcr,
const char *what,
const char *identity,
s_password &password,
TlsResource *tls_configuration)
{
return TwoWayAuthenticate(jcr, what, identity, password, tls_configuration, false);
}
bool BareosSocket::AuthenticateInboundConnection(JobControlRecord *jcr,
const char *what,
const char *identity,
s_password &password,
TlsResource *tls_configuration)
{
return TwoWayAuthenticate(jcr, what, identity, password, tls_configuration, true);
}
bool BareosSocket::IsCleartextBareosHello()
{
char buffer[12];
memset(buffer, 0, sizeof(buffer));
int ret = ::recv(fd_, buffer, 10, MSG_PEEK);
if (ret == 10) {
std::string hello("Hello ");
std::string received(&buffer[4]);
if (!hello.compare(received)) {
return true;
}
}
return false;
}
/**
* Try to limit the bandwidth of a network connection
*/
void BareosSocket::ControlBwlimit(int bytes)
{
btime_t now, temp;
int64_t usec_sleep;
/*
* If nothing written or read nothing todo.
*/
if (bytes == 0) {
return;
}
/*
* See if this is the first time we enter here.
*/
now = GetCurrentBtime();
if (last_tick_ == 0) {
nb_bytes_ = bytes;
last_tick_ = now;
return;
}
/*
* Calculate the number of microseconds since the last check.
*/
temp = now - last_tick_;
/*
* Less than 0.1ms since the last call, see the next time
*/
if (temp < 100) {
nb_bytes_ += bytes;
return;
}
/*
* Keep track of how many bytes are written in this timeslice.
*/
nb_bytes_ += bytes;
last_tick_ = now;
if (debug_level >= 400) {
Dmsg3(400, "ControlBwlimit: now = %lld, since = %lld, nb_bytes = %d\n", now, temp, nb_bytes_);
}
/*
* Take care of clock problems (>10s)
*/
if (temp > 10000000) {
return;
}
/*
* Remove what was authorised to be written in temp usecs.
*/
nb_bytes_ -= (int64_t)(temp * ((double)bwlimit_ / 1000000.0));
if (nb_bytes_ < 0) {
/*
* If more was authorized then used but bursting is not enabled
* reset the counter as these bytes cannot be used later on when
* we are exceeding our bandwidth.
*/
if (!use_bursting_) {
nb_bytes_ = 0;
}
return;
}
/*
* What exceed should be converted in sleep time
*/
usec_sleep = (int64_t)(nb_bytes_ / ((double)bwlimit_ / 1000000.0));
if (usec_sleep > 100) {
if (debug_level >= 400) {
Dmsg1(400, "ControlBwlimit: sleeping for %lld usecs\n", usec_sleep);
}
/*
* Sleep the right number of usecs.
*/
while (1) {
Bmicrosleep(0, usec_sleep);
now = GetCurrentBtime();
/*
* See if we slept enough or that Bmicrosleep() returned early.
*/
if ((now - last_tick_) < usec_sleep) {
usec_sleep -= (now - last_tick_);
continue;
} else {
last_tick_ = now;
break;
}
}
/*
* Subtract the number of bytes we could have sent during the sleep
* time given the bandwidth limit set. We only do this when we are
* allowed to burst e.g. use unused bytes from previous timeslices
* to get an overall bandwidth limiting which may sometimes be below
* the bandwidth and sometimes above it but the average will be near
* the set bandwidth.
*/
if (use_bursting_) {
nb_bytes_ -= (int64_t)(usec_sleep * ((double)bwlimit_ / 1000000.0));
} else {
nb_bytes_ = 0;
}
}
}