/
bnet.c
588 lines (538 loc) · 15 KB
/
bnet.c
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
/*
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-2013 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
*/
#include "bareos.h"
#include "jcr.h"
#include <netdb.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 is_bnet_stop() and is_bnet_error() you can figure this all out.
*/
int32_t bnet_recv(BSOCK * 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 is_bnet_stop(BSOCK * bsock)
{
return bsock->is_stop();
}
/*
* Return number of errors on socket
*/
int is_bnet_error(BSOCK * bsock)
{
return bsock->is_error();
}
/*
* Call here after error during closing to suppress error
* messages which are due to the other end shutting down too.
*/
void bnet_suppress_error_messages(BSOCK * bsock, bool flag)
{
bsock->m_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 bnet_send(BSOCK *bsock)
{
return bsock->send();
}
/*
* Establish a TLS connection -- server side
* Returns: true on success
* false on failure
*/
#ifdef HAVE_TLS
bool bnet_tls_server(TLS_CONTEXT *ctx, BSOCK *bsock, alist *verify_list)
{
TLS_CONNECTION *tls;
JCR *jcr = bsock->jcr();
tls = new_tls_connection(ctx, bsock->m_fd, true);
if (!tls) {
Qmsg0(bsock->jcr(), M_FATAL, 0, _("TLS connection initialization failed.\n"));
return false;
}
bsock->tls = tls;
/*
* Initiate TLS Negotiation
*/
if (!tls_bsock_accept(bsock)) {
Qmsg0(bsock->jcr(), M_FATAL, 0, _("TLS Negotiation failed.\n"));
goto err;
}
if (verify_list) {
if (!tls_postconnect_verify_cn(jcr, tls, 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;
}
}
Dmsg0(50, "TLS server negotiation established.\n");
return true;
err:
free_tls_connection(tls);
bsock->tls = NULL;
return false;
}
/*
* Establish a TLS connection -- client side
* Returns: true on success
* false on failure
*/
bool bnet_tls_client(TLS_CONTEXT *ctx, BSOCK *bsock, alist *verify_list)
{
TLS_CONNECTION *tls;
JCR *jcr = bsock->jcr();
tls = new_tls_connection(ctx, bsock->m_fd, false);
if (!tls) {
Qmsg0(bsock->jcr(), M_FATAL, 0, _("TLS connection initialization failed.\n"));
return false;
}
bsock->tls = tls;
/*
* Initiate TLS Negotiation
*/
if (!tls_bsock_connect(bsock)) {
goto err;
}
/*
* 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) {
if (!tls_postconnect_verify_cn(jcr, tls, 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 (!tls_postconnect_verify_host(jcr, tls, 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;
}
}
Dmsg0(50, "TLS client negotiation established.\n");
return true;
err:
free_tls_connection(tls);
bsock->tls = NULL;
return false;
}
#else
bool bnet_tls_server(TLS_CONTEXT *ctx, BSOCK * bsock, alist *verify_list)
{
Jmsg(bsock->jcr(), M_ABORT, 0, _("TLS enabled but not configured.\n"));
return false;
}
bool bnet_tls_client(TLS_CONTEXT *ctx, BSOCK * bsock, alist *verify_list)
{
Jmsg(bsock->jcr(), M_ABORT, 0, _("TLS enable but not configured.\n"));
return false;
}
#endif /* HAVE_TLS */
/*
* Wait for a specified time for data to appear on
* the BSOCK connection.
*
* Returns: 1 if data available
* 0 if timeout
* -1 if error
*/
int bnet_wait_data(BSOCK * bsock, int sec)
{
return bsock->wait_data(sec);
}
/*
* As above, but returns on interrupt
*/
int bnet_wait_data_intr(BSOCK * bsock, int sec)
{
return bsock->wait_data_intr(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->set_type(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->set_addr4(&(((struct sockaddr_in *)rp->ai_addr)->sin_addr));
break;
#ifdef HAVE_IPV6
case AF_INET6:
addr = New(IPADDR(rp->ai_addr->sa_family));
addr->set_type(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->set_addr6(&(((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->set_type(IPADDR::R_MULTIPLE);
addr->set_addr4((struct in_addr *)*p);
break;
#ifdef HAVE_IPV6
case AF_INET6:
addr = New(IPADDR(hp->h_addrtype));
addr->set_type(IPADDR::R_MULTIPLE);
addr->set_addr6((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->set_type(IPADDR::R_MULTIPLE);
addr->set_addr_any();
return addr;
}
/*
* i host = 0 mean INADDR_ANY only ipv4
*/
dlist *bnet_host2ipaddrs(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->set_type(IPADDR::R_MULTIPLE);
addr->set_addr4(&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->set_type(IPADDR::R_MULTIPLE);
addr->set_addr6(&inaddr6);
addr_list->append(addr);
#endif
} else {
if (family != 0) {
errmsg = resolv_host(family, host, addr_list);
if (errmsg) {
*errstr = errmsg;
free_addresses(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;
free_addresses(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 *bnet_strerror(BSOCK * bsock)
{
return bsock->bstrerror();
}
/*
* Format and send a message
* Returns: false on error
* true on success
*/
bool bnet_fsend(BSOCK * bs, const char *fmt, ...)
{
va_list arg_ptr;
int maxlen;
if (bs->errors || bs->is_terminated()) {
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 = sizeof_pool_memory(bs->msg) - 1;
va_start(arg_ptr, fmt);
bs->msglen = bvsnprintf(bs->msg, maxlen, fmt, arg_ptr);
va_end(arg_ptr);
if (bs->msglen > 0 && bs->msglen < (maxlen - 5)) {
break;
}
bs->msg = realloc_pool_memory(bs->msg, maxlen + maxlen / 2);
}
return bs->send();
}
int bnet_get_peer(BSOCK *bs, char *buf, socklen_t buflen)
{
return bs->get_peer(buf, buflen);
}
/*
* Set the network buffer size, suggested size is in size.
* Actual size obtained is returned in bs->msglen
*
* Returns: 0 on failure
* 1 on success
*/
bool bnet_set_buffer_size(BSOCK * bs, uint32_t size, int rw)
{
return bs->set_buffer_size(size, rw);
}
/*
* Set socket non-blocking
* Returns previous socket flag
*/
int bnet_set_nonblocking(BSOCK *bsock)
{
return bsock->set_nonblocking();
}
/*
* Set socket blocking
* Returns previous socket flags
*/
int bnet_set_blocking(BSOCK *bsock)
{
return bsock->set_blocking();
}
/*
* Restores socket flags
*/
void bnet_restore_blocking (BSOCK *bsock, int flags)
{
bsock->restore_blocking(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 bnet_sig(BSOCK * bs, int signal)
{
return bs->signal(signal);
}
/*
* Convert a network "signal" code into
* human readable ASCII.
*/
const char *bnet_sig_to_ascii(BSOCK * bs)
{
static char buf[30];
switch (bs->msglen) {
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->msglen);
return buf;
}
}