/
bnet.cc
679 lines (609 loc) · 17.6 KB
/
bnet.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
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2000-2011 Free Software Foundation Europe e.V.
Copyright (C) 2011-2012 Planets Communications B.V.
Copyright (C) 2013-2016 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
*
* by Kern Sibbald
*
* Adapted and enhanced for BAREOS, originally written
* for inclusion in the Apcupsd package
*/
/**
* @file
* Network Utility Routines
*/
#include "include/bareos.h"
#include "jcr.h"
#include "lib/bnet.h"
#include "lib/bsys.h"
#include "lib/ascii_control_characters.h"
#include "lib/bstringlist.h"
#include <netdb.h>
#include "lib/tls.h"
#ifndef INADDR_NONE
#define INADDR_NONE -1
#endif
#ifndef HAVE_GETADDRINFO
static pthread_mutex_t ip_mutex = PTHREAD_MUTEX_INITIALIZER;
#endif
/**
* Receive a message from the other end. Each message consists of
* two packets. The first is a header that contains the size
* of the data that follows in the second packet.
* Returns number of bytes read (may return zero)
* Returns -1 on signal (BNET_SIGNAL)
* Returns -2 on hard end of file (BNET_HARDEOF)
* Returns -3 on error (BNET_ERROR)
*
* Unfortunately, it is a bit complicated because we have these
* four return types:
* 1. Normal data
* 2. Signal including end of data stream
* 3. Hard end of file
* 4. Error
* Using IsBnetStop() and IsBnetError() you can figure this all out.
*/
int32_t BnetRecv(BareosSocket * bsock)
{
return bsock->recv();
}
/**
* Return 1 if there are errors on this bsock or it is closed,
* i.e. stop communicating on this line.
*/
bool IsBnetStop(BareosSocket * bsock)
{
return bsock->IsStop();
}
/**
* Return number of errors on socket
*/
int IsBnetError(BareosSocket * bsock)
{
return bsock->IsError();
}
/**
* Call here after error during closing to suppress error
* messages which are due to the other end shutting down too.
*/
void BnetSuppressErrorMessages(BareosSocket * bsock, bool flag)
{
bsock->suppress_error_msgs_ = flag;
}
/**
* Send a message over the network. The send consists of
* two network packets. The first is sends a 32 bit integer containing
* the length of the data packet which follows.
*
* Returns: false on failure
* true on success
*/
bool BnetSend(BareosSocket *bsock)
{
return bsock->send();
}
/**
* Establish a TLS connection -- server side
* Returns: true on success
* false on failure
*/
#ifdef HAVE_TLS
bool BnetTlsServer(BareosSocket *bsock, const std::vector<std::string> &verify_list)
{
JobControlRecord *jcr = bsock->jcr();
if (!bsock->tls_conn_init) {
Dmsg0(100, "No TLS Connection: Cannot call TlsBsockAccept\n");
goto err;
}
if (!bsock->tls_conn_init->TlsBsockAccept(bsock)) {
Qmsg0(bsock->jcr(), M_FATAL, 0, _("TLS Negotiation failed.\n"));
goto err;
}
if (!verify_list.empty()) {
if (!bsock->tls_conn_init->TlsPostconnectVerifyCn(jcr, verify_list)) {
Qmsg1(bsock->jcr(), M_FATAL, 0, _("TLS certificate verification failed."
" Peer certificate did not match a required commonName\n"),
bsock->host());
goto err;
}
}
bsock->LockMutex();
bsock->tls_conn = std::move(bsock->tls_conn_init);
bsock->UnlockMutex();
Dmsg0(50, "TLS server negotiation established.\n");
return true;
err:
bsock->CloseTlsConnectionAndFreeMemory();
return false;
}
/**
* Establish a TLS connection -- client side
* Returns: true on success
* false on failure
*/
bool BnetTlsClient(BareosSocket *bsock, bool VerifyPeer, const std::vector<std::string> &verify_list)
{
JobControlRecord *jcr = bsock->jcr();
if (!bsock->tls_conn_init) {
Dmsg0(100, "No TLS Connection: Cannot call TlsBsockConnect\n");
goto err;
}
if (!bsock->tls_conn_init->TlsBsockConnect(bsock)) {
goto err;
}
if (VerifyPeer) {
/*
* If there's an Allowed CN verify list, use that to validate the remote
* certificate's CN. Otherwise, we use standard host/CN matching.
*/
if (!verify_list.empty()) {
if (!bsock->tls_conn_init->TlsPostconnectVerifyCn(jcr, verify_list)) {
Qmsg1(bsock->jcr(), M_FATAL, 0, _("TLS certificate verification failed."
" Peer certificate did not match a required commonName\n"),
bsock->host());
goto err;
}
} else {
if (!bsock->tls_conn_init->TlsPostconnectVerifyHost(jcr, bsock->host())) {
Qmsg1(bsock->jcr(), M_FATAL, 0, _("TLS host certificate verification failed. Host name \"%s\" did not match presented certificate\n"),
bsock->host());
goto err;
}
}
}
bsock->LockMutex();
bsock->tls_conn = std::move(bsock->tls_conn_init);
bsock->UnlockMutex();
Dmsg0(50, "TLS client negotiation established.\n");
return true;
err:
bsock->CloseTlsConnectionAndFreeMemory();
return false;
}
#else
bool BnetTlsServer(std::shared_ptr<TlsImplementation> tls_implementation,
BareosSocket *bsock,
const std::vector<std::string> &verify_list)
{
Jmsg(bsock->jcr(), M_ABORT, 0, _("TLS enabled but not configured.\n"));
return false;
}
bool BnetTlsClient(std::shared_ptr<TLS_IMPLEMENTATION> tls_implementation,
BareosSocket *bsock,
bool VerifyPeer,
const std::vector<std::string> &verify_list)
{
Jmsg(bsock->jcr(), M_ABORT, 0, _("TLS enabled but not configured.\n"));
return false;
}
#endif /* HAVE_TLS */
/**
* Wait for a specified time for data to appear on
* the BareosSocket connection.
*
* Returns: 1 if data available
* 0 if timeout
* -1 if error
*/
int BnetWaitData(BareosSocket * bsock, int sec)
{
return bsock->WaitData(sec);
}
/**
* As above, but returns on interrupt
*/
int BnetWaitDataIntr(BareosSocket * bsock, int sec)
{
return bsock->WaitDataIntr(sec);
}
#ifndef NETDB_INTERNAL
#define NETDB_INTERNAL -1 /* See errno. */
#endif
#ifndef NETDB_SUCCESS
#define NETDB_SUCCESS 0 /* No problem. */
#endif
#ifndef HOST_NOT_FOUND
#define HOST_NOT_FOUND 1 /* Authoritative Answer Host not found. */
#endif
#ifndef TRY_AGAIN
#define TRY_AGAIN 2 /* Non-Authoritative Host not found, or SERVERFAIL. */
#endif
#ifndef NO_RECOVERY
#define NO_RECOVERY 3 /* Non recoverable errors, FORMERR, REFUSED, NOTIMP. */
#endif
#ifndef NO_DATA
#define NO_DATA 4 /* Valid name, no data record of requested type. */
#endif
#if HAVE_GETADDRINFO
const char *resolv_host(int family, const char *host, dlist *addr_list)
{
int res;
struct addrinfo hints;
struct addrinfo *ai, *rp;
IPADDR *addr;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = family;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = 0;
res = getaddrinfo(host, NULL, &hints, &ai);
if (res != 0) {
return gai_strerror(res);
}
for (rp = ai; rp != NULL; rp = rp->ai_next) {
switch (rp->ai_addr->sa_family) {
case AF_INET:
addr = New(IPADDR(rp->ai_addr->sa_family));
addr->SetType(IPADDR::R_MULTIPLE);
/*
* Some serious casting to get the struct in_addr *
* rp->ai_addr == struct sockaddr
* as this is AF_INET family we can cast that
* to struct_sockaddr_in. Of that we need the
* address of the sin_addr member which contains a
* struct in_addr
*/
addr->SetAddr4(&(((struct sockaddr_in *)rp->ai_addr)->sin_addr));
break;
#ifdef HAVE_IPV6
case AF_INET6:
addr = New(IPADDR(rp->ai_addr->sa_family));
addr->SetType(IPADDR::R_MULTIPLE);
/*
* Some serious casting to get the struct in6_addr *
* rp->ai_addr == struct sockaddr
* as this is AF_INET6 family we can cast that
* to struct_sockaddr_in6. Of that we need the
* address of the sin6_addr member which contains a
* struct in6_addr
*/
addr->SetAddr6(&(((struct sockaddr_in6 *)rp->ai_addr)->sin6_addr));
break;
#endif
default:
continue;
}
addr_list->append(addr);
}
freeaddrinfo(ai);
return NULL;
}
#else
/**
* Get human readable error for gethostbyname()
*/
static const char *gethost_strerror()
{
const char *msg;
BErrNo be;
switch (h_errno) {
case NETDB_INTERNAL:
msg = be.bstrerror();
break;
case NETDB_SUCCESS:
msg = _("No problem.");
break;
case HOST_NOT_FOUND:
msg = _("Authoritative answer for host not found.");
break;
case TRY_AGAIN:
msg = _("Non-authoritative for host not found, or ServerFail.");
break;
case NO_RECOVERY:
msg = _("Non-recoverable errors, FORMERR, REFUSED, or NOTIMP.");
break;
case NO_DATA:
msg = _("Valid name, no data record of resquested type.");
break;
default:
msg = _("Unknown error.");
}
return msg;
}
static const char *resolv_host(int family, const char *host, dlist *addr_list)
{
struct hostent *hp;
const char *errmsg;
char **p;
IPADDR *addr;
P(ip_mutex); /* gethostbyname() is not thread safe */
#ifdef HAVE_GETHOSTBYNAME2
if ((hp = gethostbyname2(host, family)) == NULL) {
#else
if ((hp = gethostbyname(host)) == NULL) {
#endif
/* may be the strerror give not the right result -:( */
errmsg = gethost_strerror();
V(ip_mutex);
return errmsg;
} else {
for (p = hp->h_addr_list; *p != 0; p++) {
switch (hp->h_addrtype) {
case AF_INET:
addr = New(IPADDR(hp->h_addrtype));
addr->SetType(IPADDR::R_MULTIPLE);
addr->SetAddr4((struct in_addr *)*p);
break;
#ifdef HAVE_IPV6
case AF_INET6:
addr = New(IPADDR(hp->h_addrtype));
addr->SetType(IPADDR::R_MULTIPLE);
addr->SetAddr6((struct in6_addr *)*p);
break;
#endif
default:
continue;
}
addr_list->append(addr);
}
V(ip_mutex);
}
return NULL;
}
#endif
static IPADDR *add_any(int family)
{
IPADDR *addr = New(IPADDR(family));
addr->SetType(IPADDR::R_MULTIPLE);
addr->SetAddrAny();
return addr;
}
/**
* i host = 0 mean INADDR_ANY only ipv4
*/
dlist *BnetHost2IpAddrs(const char *host, int family, const char **errstr)
{
struct in_addr inaddr;
IPADDR *addr = 0;
const char *errmsg;
#ifdef HAVE_IPV6
struct in6_addr inaddr6;
#endif
dlist *addr_list = New(dlist(addr, &addr->link));
if (!host || host[0] == '\0') {
if (family != 0) {
addr_list->append(add_any(family));
} else {
addr_list->append(add_any(AF_INET));
#ifdef HAVE_IPV6
addr_list->append(add_any(AF_INET6));
#endif
}
} else if (inet_aton(host, &inaddr)) { /* MA Bug 4 */
addr = New(IPADDR(AF_INET));
addr->SetType(IPADDR::R_MULTIPLE);
addr->SetAddr4(&inaddr);
addr_list->append(addr);
#ifdef HAVE_IPV6
#ifndef HAVE_WIN32
} else if (inet_pton(AF_INET6, host, &inaddr6) == 1) {
#else
} else if (p_InetPton && p_InetPton(AF_INET6, host, &inaddr6) == 1) {
#endif
addr = New(IPADDR(AF_INET6));
addr->SetType(IPADDR::R_MULTIPLE);
addr->SetAddr6(&inaddr6);
addr_list->append(addr);
#endif
} else {
if (family != 0) {
errmsg = resolv_host(family, host, addr_list);
if (errmsg) {
*errstr = errmsg;
FreeAddresses(addr_list);
return 0;
}
} else {
#ifdef HAVE_IPV6
/* We try to resolv host for ipv6 and ipv4, the connection procedure
* will try to reach the host for each protocols. We report only "Host
* not found" ipv4 message (no need to have ipv6 and ipv4 messages).
*/
resolv_host(AF_INET6, host, addr_list);
#endif
errmsg = resolv_host(AF_INET, host, addr_list);
if (addr_list->size() == 0) {
*errstr = errmsg;
FreeAddresses(addr_list);
return 0;
}
}
}
return addr_list;
}
/**
* Return the string for the error that occurred
* on the socket. Only the first error is retained.
*/
const char *BnetStrerror(BareosSocket * bsock)
{
return bsock->bstrerror();
}
/**
* Format and send a message
* Returns: false on error
* true on success
*/
bool BnetFsend(BareosSocket * bs, const char *fmt, ...)
{
va_list arg_ptr;
int maxlen;
if (bs->errors || bs->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(bs->msg) - 1;
va_start(arg_ptr, fmt);
bs->message_length = Bvsnprintf(bs->msg, maxlen, fmt, arg_ptr);
va_end(arg_ptr);
if (bs->message_length > 0 && bs->message_length < (maxlen - 5)) {
break;
}
bs->msg = ReallocPoolMemory(bs->msg, maxlen + maxlen / 2);
}
return bs->send();
}
int BnetGetPeer(BareosSocket *bs, char *buf, socklen_t buflen)
{
return bs->GetPeer(buf, buflen);
}
/**
* Set the network buffer size, suggested size is in size.
* Actual size obtained is returned in bs->message_length
*
* Returns: 0 on failure
* 1 on success
*/
bool BnetSetBufferSize(BareosSocket * bs, uint32_t size, int rw)
{
return bs->SetBufferSize(size, rw);
}
/**
* Set socket non-blocking
* Returns previous socket flag
*/
int BnetSetNonblocking(BareosSocket *bsock)
{
return bsock->SetNonblocking();
}
/**
* Set socket blocking
* Returns previous socket flags
*/
int BnetSetBlocking(BareosSocket *bsock)
{
return bsock->SetBlocking();
}
/**
* Restores socket flags
*/
void BnetRestoreBlocking (BareosSocket *bsock, int flags)
{
bsock->RestoreBlocking(flags);
}
/**
* Send a network "signal" to the other end
* This consists of sending a negative packet length
*
* Returns: false on failure
* true on success
*/
bool BnetSig(BareosSocket * bs, int signal)
{
return bs->signal(signal);
}
/**
* Convert a network "signal" code into
* human readable ASCII.
*/
const char *BnetSigToAscii(BareosSocket * bs)
{
static char buf[30];
switch (bs->message_length) {
case BNET_EOD:
return "BNET_EOD"; /* end of data stream */
case BNET_EOD_POLL:
return "BNET_EOD_POLL";
case BNET_STATUS:
return "BNET_STATUS";
case BNET_TERMINATE:
return "BNET_TERMINATE"; /* Terminate connection */
case BNET_POLL:
return "BNET_POLL";
case BNET_HEARTBEAT:
return "BNET_HEARTBEAT";
case BNET_HB_RESPONSE:
return "BNET_HB_RESPONSE";
case BNET_SUB_PROMPT:
return "BNET_SUB_PROMPT";
case BNET_TEXT_INPUT:
return "BNET_TEXT_INPUT";
default:
sprintf(buf, _("Unknown sig %d"), (int)bs->message_length);
return buf;
}
}
bool ReadoutCommandIdFromMessage(const BStringList& list_of_arguments, uint32_t &id_out)
{
if (list_of_arguments.size() < 1) {
return false;
}
uint32_t id = kMessageIdUnknown;
try { /* "1000 OK: <director name> ..." */
const std::string &first_argument = list_of_arguments.front();
id = std::stoul(first_argument);
} catch (const std::exception &e) {
id_out = kMessageIdProtokollError;
return false;
}
id_out = id;
return true;
}
bool EvaluateResponseMessageId(const std::string &message, uint32_t &id_out, BStringList &args_out)
{
BStringList list_of_arguments(message, AsciiControlCharacters::RecordSeparator());
uint32_t id = kMessageIdUnknown;
bool ok = ReadoutCommandIdFromMessage(list_of_arguments, id);
if (ok) {
id_out = id;
}
args_out = list_of_arguments;
return ok;
}
bool BareosSocket::ReceiveAndEvaluateResponseMessage(uint32_t &id_out, BStringList &args_out)
{
StartTimer(30); //30 seconds
int ret = recv();
StopTimer();
if (ret <= 0) {
Dmsg1(100, "Error while receiving response message: %s", msg);
return false;
}
std::string message(msg);
if (message.empty()) {
Dmsg0(100, "Received message is empty\n");
return false;
}
return EvaluateResponseMessageId(message, id_out, args_out);
}
bool BareosSocket::FormatAndSendResponseMessage(uint32_t id, const BStringList &list_of_arguments)
{
std::string m = std::to_string(id);
m += AsciiControlCharacters::RecordSeparator();
m += list_of_arguments.Join(AsciiControlCharacters::RecordSeparator());
StartTimer(30); //30 seconds
if (send(m.c_str(), m.size()) <=0 ) {
Dmsg1(100, "Could not send response message: %d\n", m.c_str());
StopTimer();
return false;
}
StopTimer();
return true;
}
bool BareosSocket::FormatAndSendResponseMessage(uint32_t id, const std::string &str)
{
BStringList message;
message << str;
return FormatAndSendResponseMessage(id, message);
}