-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
forward.c
1826 lines (1532 loc) · 51.6 KB
/
forward.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
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
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* Fluent Bit
* ==========
* Copyright (C) 2015-2024 The Fluent Bit Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <fluent-bit/flb_output_plugin.h>
#include <fluent-bit/flb_utils.h>
#include <fluent-bit/flb_network.h>
#include <fluent-bit/flb_time.h>
#include <fluent-bit/flb_upstream.h>
#include <fluent-bit/flb_upstream_ha.h>
#include <fluent-bit/flb_hash.h>
#include <fluent-bit/flb_crypto.h>
#include <fluent-bit/flb_config_map.h>
#include <fluent-bit/flb_random.h>
#include <fluent-bit/flb_gzip.h>
#include <fluent-bit/flb_log_event.h>
#include <msgpack.h>
#include "forward.h"
#include "forward_format.h"
#ifdef FLB_HAVE_UNIX_SOCKET
#include <sys/socket.h>
#include <sys/un.h>
#endif
#define SECURED_BY "Fluent Bit"
pthread_once_t uds_connection_tls_slot_init_once_control = PTHREAD_ONCE_INIT;
FLB_TLS_DEFINE(struct flb_forward_uds_connection, uds_connection);
void initialize_uds_connection_tls_slot()
{
FLB_TLS_INIT(uds_connection);
}
#ifdef FLB_HAVE_UNIX_SOCKET
static flb_sockfd_t forward_unix_connect(struct flb_forward_config *config,
struct flb_forward *ctx)
{
flb_sockfd_t fd = -1;
struct sockaddr_un address;
if (sizeof(address.sun_path) <= flb_sds_len(config->unix_path)) {
flb_plg_error(ctx->ins, "unix_path is too long");
return -1;
}
memset(&address, 0, sizeof(struct sockaddr_un));
fd = flb_net_socket_create(AF_UNIX, FLB_FALSE);
if (fd < 0) {
flb_plg_error(ctx->ins, "flb_net_socket_create error");
return -1;
}
address.sun_family = AF_UNIX;
strncpy(address.sun_path, config->unix_path, flb_sds_len(config->unix_path));
if(connect(fd, (const struct sockaddr*) &address, sizeof(address)) < 0) {
flb_errno();
close(fd);
return -1;
}
return fd;
}
static flb_sockfd_t forward_uds_get_conn(struct flb_forward_config *config,
struct flb_forward *ctx)
{
struct flb_forward_uds_connection *connection_entry;
flb_sockfd_t connection;
connection_entry = FLB_TLS_GET(uds_connection);
/* We need to allow the code to try to get the value from the TLS
* regardless of if it's provided with a config and context because
* when we establish the connection we do have both of them but those
* are not passed along to the functions in charge of doing IO.
*/
if (connection_entry == NULL) {
if (config == NULL ||
ctx == NULL) {
return -1;
}
connection_entry = flb_calloc(1, sizeof(struct flb_forward_uds_connection));
if (connection_entry == NULL) {
flb_errno();
return -1;
}
connection = forward_unix_connect(config, ctx);
if (connection == -1) {
flb_free(connection_entry);
return -1;
}
connection_entry->descriptor = connection;
pthread_mutex_lock(&ctx->uds_connection_list_mutex);
cfl_list_add(&connection_entry->_head, &ctx->uds_connection_list);
pthread_mutex_unlock(&ctx->uds_connection_list_mutex);
FLB_TLS_SET(uds_connection, connection_entry);
}
return connection_entry->descriptor;
}
static void forward_uds_drop_conn(struct flb_forward *ctx,
flb_sockfd_t connection)
{
struct flb_forward_uds_connection *connection_entry;
if (ctx != NULL) {
connection_entry = FLB_TLS_GET(uds_connection);
if (connection_entry != NULL) {
pthread_mutex_lock(&ctx->uds_connection_list_mutex);
if (connection == connection_entry->descriptor) {
close(connection);
if (!cfl_list_entry_is_orphan(&connection_entry->_head)) {
cfl_list_del(&connection_entry->_head);
}
free(connection_entry);
FLB_TLS_SET(uds_connection, NULL);
}
pthread_mutex_unlock(&ctx->uds_connection_list_mutex);
}
}
}
static void forward_uds_drop_all(struct flb_forward *ctx)
{
struct flb_forward_uds_connection *connection_entry;
struct cfl_list *head;
struct cfl_list *tmp;
if (ctx != NULL) {
pthread_mutex_lock(&ctx->uds_connection_list_mutex);
cfl_list_foreach_safe(head, tmp, &ctx->uds_connection_list) {
connection_entry = cfl_list_entry(head,
struct flb_forward_uds_connection,
_head);
if (connection_entry->descriptor != -1) {
close(connection_entry->descriptor);
connection_entry->descriptor = -1;
}
if (!cfl_list_entry_is_orphan(&connection_entry->_head)) {
cfl_list_del(&connection_entry->_head);
}
free(connection_entry);
}
pthread_mutex_unlock(&ctx->uds_connection_list_mutex);
}
}
/* In these functions forward_uds_get_conn
* should not return -1 because it should have been
* called earlier with a proper context and it should
* have saved a file descriptor to the TLS.
*/
static int io_unix_write(struct flb_connection *unused, int deprecated_fd, const void* data,
size_t len, size_t *out_len)
{
flb_sockfd_t uds_conn;
uds_conn = forward_uds_get_conn(NULL, NULL);
return flb_io_fd_write(uds_conn, data, len, out_len);
}
static int io_unix_read(struct flb_connection *unused, int deprecated_fd, void* buf,size_t len)
{
flb_sockfd_t uds_conn;
uds_conn = forward_uds_get_conn(NULL, NULL);
return flb_io_fd_read(uds_conn, buf, len);
}
#else
static flb_sockfd_t forward_uds_get_conn(struct flb_forward_config *config,
struct flb_forward *ctx)
{
(void) config;
(void) ctx;
return -1;
}
static void forward_uds_drop_conn(struct flb_forward *ctx,
flb_sockfd_t connection)
{
(void) ctx;
(void) connection;
}
static void forward_uds_drop_all(struct flb_forward *ctx)
{
(void) ctx;
}
#endif
#ifdef FLB_HAVE_TLS
static int io_net_write(struct flb_connection *conn, int unused_fd,
const void* data, size_t len, size_t *out_len)
{
return flb_io_net_write(conn, data, len, out_len);
}
static int io_net_read(struct flb_connection *conn, int unused_fd,
void* buf, size_t len)
{
return flb_io_net_read(conn, buf, len);
}
static int secure_forward_init(struct flb_forward *ctx,
struct flb_forward_config *fc)
{
return 0;
}
#endif
static inline void print_msgpack_status(struct flb_forward *ctx,
int ret, char *context)
{
switch (ret) {
case MSGPACK_UNPACK_EXTRA_BYTES:
flb_plg_error(ctx->ins, "%s MSGPACK_UNPACK_EXTRA_BYTES", context);
break;
case MSGPACK_UNPACK_CONTINUE:
flb_plg_trace(ctx->ins, "%s MSGPACK_UNPACK_CONTINUE", context);
break;
case MSGPACK_UNPACK_PARSE_ERROR:
flb_plg_error(ctx->ins, "%s MSGPACK_UNPACK_PARSE_ERROR", context);
break;
case MSGPACK_UNPACK_NOMEM_ERROR:
flb_plg_error(ctx->ins, "%s MSGPACK_UNPACK_NOMEM_ERROR", context);
break;
}
}
/* Read a secure forward msgpack message */
static int secure_forward_read(struct flb_forward *ctx,
struct flb_connection *u_conn,
struct flb_forward_config *fc,
char *buf, size_t size, size_t *out_len)
{
int ret;
size_t off;
size_t avail;
size_t buf_off = 0;
msgpack_unpacked result;
msgpack_unpacked_init(&result);
while (1) {
avail = size - buf_off;
if (avail < 1) {
goto error;
}
/* Read the message */
ret = fc->io_read(u_conn, fc->unix_fd, buf + buf_off, size - buf_off);
if (ret <= 0) {
goto error;
}
buf_off += ret;
/* Validate */
off = 0;
ret = msgpack_unpack_next(&result, buf, buf_off, &off);
switch (ret) {
case MSGPACK_UNPACK_SUCCESS:
msgpack_unpacked_destroy(&result);
*out_len = buf_off;
return 0;
default:
print_msgpack_status(ctx, ret, "handshake");
goto error;
};
}
error:
msgpack_unpacked_destroy(&result);
return -1;
}
static void secure_forward_set_ping(struct flb_forward_ping *ping,
msgpack_object *map)
{
int i;
msgpack_object key;
msgpack_object val;
const char *ptr;
int len;
memset(ping, 0, sizeof(struct flb_forward_ping));
ping->keepalive = 1; /* default, as per spec */
for (i = 0; i < map->via.map.size; i++) {
key = map->via.map.ptr[i].key;
val = map->via.map.ptr[i].val;
ptr = key.via.str.ptr;
len = key.via.str.size;
if (len == 5 && memcmp(ptr, "nonce", len) == 0) {
ping->nonce = val.via.bin.ptr;
ping->nonce_len = val.via.bin.size;
}
else if (len == 4 && memcmp(ptr, "auth", len) == 0) {
ping->auth = val.via.bin.ptr;
ping->auth_len = val.via.bin.size;
}
else if (len == 9 && memcmp(ptr, "keepalive", len) == 0) {
ping->keepalive = val.via.boolean;
}
}
}
static int secure_forward_hash_shared_key(struct flb_forward_config *fc,
struct flb_forward_ping *ping,
char *buf, int buflen)
{
size_t length_entries[4];
unsigned char *data_entries[4];
uint8_t hash[64];
int result;
if (buflen < 128) {
return -1;
}
data_entries[0] = (unsigned char *) fc->shared_key_salt;
length_entries[0] = 16;
data_entries[1] = (unsigned char *) fc->self_hostname;
length_entries[1] = strlen(fc->self_hostname);
data_entries[2] = (unsigned char *) ping->nonce;
length_entries[2] = ping->nonce_len;
data_entries[3] = (unsigned char *) fc->shared_key;
length_entries[3] = strlen(fc->shared_key);
result = flb_hash_simple_batch(FLB_HASH_SHA512,
4,
data_entries,
length_entries,
hash,
sizeof(hash));
if (result != FLB_CRYPTO_SUCCESS) {
return -1;
}
flb_forward_format_bin_to_hex(hash, 64, buf);
return 0;
}
static int secure_forward_hash_password(struct flb_forward_config *fc,
struct flb_forward_ping *ping,
char *buf, int buflen)
{
size_t length_entries[3];
unsigned char *data_entries[3];
uint8_t hash[64];
int result;
if (buflen < 128) {
return -1;
}
data_entries[0] = (unsigned char *) ping->auth;
length_entries[0] = ping->auth_len;
data_entries[1] = (unsigned char *) fc->username;
length_entries[1] = strlen(fc->username);
data_entries[2] = (unsigned char *) fc->password;
length_entries[2] = strlen(fc->password);
result = flb_hash_simple_batch(FLB_HASH_SHA512,
3,
data_entries,
length_entries,
hash,
sizeof(hash));
if (result != FLB_CRYPTO_SUCCESS) {
return -1;
}
flb_forward_format_bin_to_hex(hash, 64, buf);
return 0;
}
static int secure_forward_ping(struct flb_connection *u_conn,
msgpack_object map,
struct flb_forward_config *fc,
struct flb_forward *ctx)
{
int ret;
size_t bytes_sent;
char shared_key_hexdigest[128];
char password_hexdigest[128];
msgpack_sbuffer mp_sbuf;
msgpack_packer mp_pck;
struct flb_forward_ping ping;
secure_forward_set_ping(&ping, &map);
if (ping.nonce == NULL) {
flb_plg_error(ctx->ins, "nonce not found");
return -1;
}
if (secure_forward_hash_shared_key(fc, &ping, shared_key_hexdigest, 128)) {
flb_plg_error(ctx->ins, "failed to hash shared_key");
return -1;
}
if (ping.auth != NULL) {
if (secure_forward_hash_password(fc, &ping, password_hexdigest, 128)) {
flb_plg_error(ctx->ins, "failed to hash password");
return -1;
}
}
/* Prepare outgoing msgpack PING */
msgpack_sbuffer_init(&mp_sbuf);
msgpack_packer_init(&mp_pck, &mp_sbuf, msgpack_sbuffer_write);
msgpack_pack_array(&mp_pck, 6);
/* [0] PING */
msgpack_pack_str(&mp_pck, 4);
msgpack_pack_str_body(&mp_pck, "PING", 4);
/* [1] Hostname */
msgpack_pack_str(&mp_pck, flb_sds_len(fc->self_hostname));
msgpack_pack_str_body(&mp_pck, fc->self_hostname,
flb_sds_len(fc->self_hostname));
/* [2] Shared key salt */
msgpack_pack_str(&mp_pck, 16);
msgpack_pack_str_body(&mp_pck, fc->shared_key_salt, 16);
/* [3] Shared key in Hexdigest format */
msgpack_pack_str(&mp_pck, 128);
msgpack_pack_str_body(&mp_pck, shared_key_hexdigest, 128);
/* [4] Username and password (optional) */
if (ping.auth != NULL) {
msgpack_pack_str(&mp_pck, strlen(fc->username));
msgpack_pack_str_body(&mp_pck, fc->username, strlen(fc->username));
msgpack_pack_str(&mp_pck, 128);
msgpack_pack_str_body(&mp_pck, password_hexdigest, 128);
}
else {
msgpack_pack_str(&mp_pck, 0);
msgpack_pack_str_body(&mp_pck, "", 0);
msgpack_pack_str(&mp_pck, 0);
msgpack_pack_str_body(&mp_pck, "", 0);
}
ret = fc->io_write(u_conn, fc->unix_fd, mp_sbuf.data, mp_sbuf.size, &bytes_sent);
flb_plg_debug(ctx->ins, "PING sent: ret=%i bytes sent=%lu", ret, bytes_sent);
msgpack_sbuffer_destroy(&mp_sbuf);
if (ret > -1 && bytes_sent > 0) {
return 0;
}
return -1;
}
static int secure_forward_pong(struct flb_forward *ctx, char *buf, int buf_size)
{
int ret;
char msg[32] = {0};
size_t off = 0;
msgpack_unpacked result;
msgpack_object root;
msgpack_object o;
msgpack_unpacked_init(&result);
ret = msgpack_unpack_next(&result, buf, buf_size, &off);
if (ret != MSGPACK_UNPACK_SUCCESS) {
return -1;
}
root = result.data;
if (root.type != MSGPACK_OBJECT_ARRAY) {
goto error;
}
if (root.via.array.size < 4) {
goto error;
}
o = root.via.array.ptr[0];
if (o.type != MSGPACK_OBJECT_STR) {
goto error;
}
if (strncmp(o.via.str.ptr, "PONG", 4) != 0 || o.via.str.size != 4) {
goto error;
}
o = root.via.array.ptr[1];
if (o.type != MSGPACK_OBJECT_BOOLEAN) {
goto error;
}
if (o.via.boolean) {
msgpack_unpacked_destroy(&result);
return 0;
}
else {
o = root.via.array.ptr[2];
memcpy(msg, o.via.str.ptr, o.via.str.size);
flb_plg_error(ctx->ins, "failed authorization: %s", msg);
}
error:
msgpack_unpacked_destroy(&result);
return -1;
}
static int secure_forward_handshake(struct flb_connection *u_conn,
struct flb_forward_config *fc,
struct flb_forward *ctx)
{
int ret;
char buf[1024];
size_t out_len;
size_t off;
msgpack_unpacked result;
msgpack_object root;
msgpack_object o;
/* Wait for server HELO */
ret = secure_forward_read(ctx, u_conn, fc, buf, sizeof(buf) - 1, &out_len);
if (ret == -1) {
flb_plg_error(ctx->ins, "handshake error expecting HELO");
return -1;
}
/* Unpack message and validate */
off = 0;
msgpack_unpacked_init(&result);
ret = msgpack_unpack_next(&result, buf, out_len, &off);
if (ret != MSGPACK_UNPACK_SUCCESS) {
print_msgpack_status(ctx, ret, "HELO");
return -1;
}
/* Parse HELO message */
root = result.data;
if (root.via.array.size < 2) {
flb_plg_error(ctx->ins, "Invalid HELO message");
msgpack_unpacked_destroy(&result);
return -1;
}
o = root.via.array.ptr[0];
if (o.type != MSGPACK_OBJECT_STR) {
flb_plg_error(ctx->ins, "Invalid HELO type message");
msgpack_unpacked_destroy(&result);
return -1;
}
if (strncmp(o.via.str.ptr, "HELO", 4) != 0 || o.via.str.size != 4) {
flb_plg_error(ctx->ins, "Invalid HELO content message");
msgpack_unpacked_destroy(&result);
return -1;
}
flb_plg_debug(ctx->ins, "protocol: received HELO");
/* Compose and send PING message */
o = root.via.array.ptr[1];
ret = secure_forward_ping(u_conn, o, fc, ctx);
if (ret == -1) {
flb_plg_error(ctx->ins, "Failed PING");
msgpack_unpacked_destroy(&result);
return -1;
}
/* Expect a PONG */
ret = secure_forward_read(ctx, u_conn, fc, buf, sizeof(buf) - 1, &out_len);
if (ret == -1) {
flb_plg_error(ctx->ins, "handshake error expecting HELO");
msgpack_unpacked_destroy(&result);
return -1;
}
/* Process PONG */
ret = secure_forward_pong(ctx, buf, out_len);
if (ret == -1) {
msgpack_unpacked_destroy(&result);
return -1;
}
msgpack_unpacked_destroy(&result);
return 0;
}
static int forward_read_ack(struct flb_forward *ctx,
struct flb_forward_config *fc,
struct flb_connection *u_conn,
char *chunk, int chunk_len)
{
int ret;
int i;
size_t out_len;
size_t off;
const char *ack;
size_t ack_len;
msgpack_unpacked result;
msgpack_object root;
msgpack_object_map map;
msgpack_object key;
msgpack_object val;
char buf[512]; /* ack should never be bigger */
flb_plg_trace(ctx->ins, "wait ACK (%.*s)", chunk_len, chunk);
/* Wait for server ACK */
ret = secure_forward_read(ctx, u_conn, fc, buf, sizeof(buf) - 1, &out_len);
if (ret == -1) {
flb_plg_error(ctx->ins, "cannot get ack");
return -1;
}
/* Unpack message and validate */
off = 0;
msgpack_unpacked_init(&result);
ret = msgpack_unpack_next(&result, buf, out_len, &off);
if (ret != MSGPACK_UNPACK_SUCCESS) {
print_msgpack_status(ctx, ret, "ACK");
goto error;
}
/* Parse ACK message */
root = result.data;
if (root.type != MSGPACK_OBJECT_MAP) {
flb_plg_error(ctx->ins, "ACK response not MAP (type:%d)", root.type);
goto error;
}
map = root.via.map;
ack = NULL;
/* Lookup ack field */
for (i = 0; i < map.size; i++) {
key = map.ptr[i].key;
if (key.via.str.size == 3 && strncmp(key.via.str.ptr, "ack", 3) == 0) {
val = map.ptr[i].val;
ack_len = val.via.str.size;
ack = val.via.str.ptr;
break;
}
}
if (!ack) {
flb_plg_error(ctx->ins, "ack: ack not found");
goto error;
}
if (ack_len != chunk_len) {
flb_plg_error(ctx->ins,
"ack: ack len does not match ack(%ld)(%.*s) chunk(%d)(%.*s)",
ack_len, (int) ack_len, ack,
chunk_len, (int) chunk_len, chunk);
goto error;
}
if (strncmp(ack, chunk, ack_len) != 0) {
flb_plg_error(ctx->ins, "ACK: mismatch received=%s, expected=(%.*s)",
ack, chunk_len, chunk);
goto error;
}
flb_plg_debug(ctx->ins, "protocol: received ACK %.*s", (int)ack_len, ack);
msgpack_unpacked_destroy(&result);
return 0;
error:
msgpack_unpacked_destroy(&result);
return -1;
}
static int forward_config_init(struct flb_forward_config *fc,
struct flb_forward *ctx)
{
if (fc->io_read == NULL || fc->io_write == NULL) {
flb_plg_error(ctx->ins, "io_read/io_write is NULL");
return -1;
}
#ifdef FLB_HAVE_TLS
/* Initialize Secure Forward mode */
if (fc->secured == FLB_TRUE) {
secure_forward_init(ctx, fc);
}
#endif
/* Generate the shared key salt */
if (flb_random_bytes(fc->shared_key_salt, 16)) {
flb_plg_error(ctx->ins, "cannot generate shared key salt");
return -1;
}
mk_list_add(&fc->_head, &ctx->configs);
return 0;
}
static flb_sds_t config_get_property(char *prop,
struct flb_upstream_node *node,
struct flb_forward *ctx)
{
if (node) {
return (flb_sds_t) flb_upstream_node_get_property(prop, node);
}
else {
return (flb_sds_t) flb_output_get_property(prop, ctx->ins);
}
}
static int config_set_properties(struct flb_upstream_node *node,
struct flb_forward_config *fc,
struct flb_forward *ctx)
{
flb_sds_t tmp;
/* Shared Key */
tmp = config_get_property("empty_shared_key", node, ctx);
if (tmp && flb_utils_bool(tmp)) {
fc->empty_shared_key = FLB_TRUE;
}
else {
fc->empty_shared_key = FLB_FALSE;
}
tmp = config_get_property("shared_key", node, ctx);
if (fc->empty_shared_key) {
fc->shared_key = flb_sds_create("");
}
else if (tmp) {
fc->shared_key = flb_sds_create(tmp);
}
else {
fc->shared_key = NULL;
}
tmp = config_get_property("username", node, ctx);
if (tmp) {
fc->username = tmp;
}
else {
fc->username = "";
}
tmp = config_get_property("password", node, ctx);
if (tmp) {
fc->password = tmp;
}
else {
fc->password = "";
}
/* Self Hostname */
tmp = config_get_property("self_hostname", node, ctx);
if (tmp) {
fc->self_hostname = flb_sds_create(tmp);
}
else {
fc->self_hostname = flb_sds_create("localhost");
}
/* Backward compatible timing mode */
tmp = config_get_property("time_as_integer", node, ctx);
if (tmp) {
fc->time_as_integer = flb_utils_bool(tmp);
}
else {
fc->time_as_integer = FLB_FALSE;
}
/* send always options (with size) */
tmp = config_get_property("send_options", node, ctx);
if (tmp) {
fc->send_options = flb_utils_bool(tmp);
}
/* add_option -> extra_options: if the user has defined 'add_option'
* we need to enable the 'send_options' flag
*/
if (fc->extra_options && mk_list_size(fc->extra_options) > 0) {
fc->send_options = FLB_TRUE;
}
/* require ack response (implies send_options) */
tmp = config_get_property("require_ack_response", node, ctx);
if (tmp) {
fc->require_ack_response = flb_utils_bool(tmp);
if (fc->require_ack_response) {
fc->send_options = FLB_TRUE;
}
}
/* Tag Overwrite */
tmp = config_get_property("tag", node, ctx);
if (tmp) {
/* Set the tag */
fc->tag = flb_sds_create(tmp);
if (!fc->tag) {
flb_plg_error(ctx->ins, "cannot allocate tag");
return -1;
}
#ifdef FLB_HAVE_RECORD_ACCESSOR
/* Record Accessor */
fc->ra_tag = flb_ra_create(fc->tag, FLB_TRUE);
if (!fc->ra_tag) {
flb_plg_error(ctx->ins, "cannot create record accessor for tag: %s",
fc->tag);
return -1;
}
/* Static record accessor ? (no dynamic values from map) */
fc->ra_static = flb_ra_is_static(fc->ra_tag);
#endif
}
else {
fc->tag = NULL;
}
/* compress (implies send_options) */
tmp = config_get_property("compress", node, ctx);
if (tmp) {
if (!strcasecmp(tmp, "text")) {
fc->compress = COMPRESS_NONE;
}
else if (!strcasecmp(tmp, "gzip")) {
fc->compress = COMPRESS_GZIP;
fc->send_options = FLB_TRUE;
}
else {
flb_plg_error(ctx->ins, "invalid compress mode: %s", tmp);
return -1;
}
}
else {
fc->compress = COMPRESS_NONE;
}
if (fc->compress != COMPRESS_NONE && fc->time_as_integer == FLB_TRUE) {
flb_plg_error(ctx->ins, "compress mode %s is incompatible with "
"time_as_integer", tmp);
return -1;
}
#ifdef FLB_HAVE_RECORD_ACCESSOR
if (fc->compress != COMPRESS_NONE &&
(fc->ra_tag && fc->ra_static == FLB_FALSE) ) {
flb_plg_error(ctx->ins, "compress mode %s is incompatible with dynamic "
"tags", tmp);
return -1;
}
#endif
return 0;
}
static void forward_config_destroy(struct flb_forward_config *fc)
{
flb_sds_destroy(fc->shared_key);
flb_sds_destroy(fc->self_hostname);
flb_sds_destroy(fc->tag);
#ifdef FLB_HAVE_RECORD_ACCESSOR
if (fc->ra_tag) {
flb_ra_destroy(fc->ra_tag);
}
#endif
flb_free(fc);
}
/* Configure in HA mode */
static int forward_config_ha(const char *upstream_file,
struct flb_forward *ctx,
struct flb_config *config)
{
int ret;
struct mk_list *head;
struct flb_upstream_node *node;
struct flb_forward_config *fc = NULL;
ctx->ha_mode = FLB_TRUE;
ctx->ha = flb_upstream_ha_from_file(upstream_file, config);
if (!ctx->ha) {
flb_plg_error(ctx->ins, "cannot load Upstream file");
return -1;
}
/* Iterate nodes and create a forward_config context */
mk_list_foreach(head, &ctx->ha->nodes) {
node = mk_list_entry(head, struct flb_upstream_node, _head);
/* create forward_config context */
fc = flb_calloc(1, sizeof(struct flb_forward_config));
if (!fc) {
flb_errno();
flb_plg_error(ctx->ins, "failed config allocation");
continue;
}
fc->unix_fd = -1;
fc->secured = FLB_FALSE;
fc->io_write = io_net_write;
fc->io_read = io_net_read;
/* Is TLS enabled ? */
if (node->tls_enabled == FLB_TRUE) {
fc->secured = FLB_TRUE;
}
/* Read properties into 'fc' context */
config_set_properties(node, fc, ctx);
/* Initialize and validate forward_config context */
ret = forward_config_init(fc, ctx);
if (ret == -1) {
if (fc) {
forward_config_destroy(fc);
}
return -1;
}
/* Set our forward_config context into the node */
flb_upstream_node_set_data(fc, node);
}
flb_output_upstream_ha_set(ctx->ha, ctx->ins);
return 0;
}
static int forward_config_simple(struct flb_forward *ctx,
struct flb_output_instance *ins,
struct flb_config *config)
{