-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_amqp.c
1595 lines (1269 loc) · 42.3 KB
/
mod_amqp.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
/*
* ProFTPD - mod_amqp
* Copyright (c) 2017-2022 TJ Saunders
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
*
* As a special exemption, TJ Saunders and other respective copyright holders
* give permission to link this program with OpenSSL, and distribute the
* resulting executable, without including the source code for OpenSSL in the
* source distribution.
*
* -----DO NOT EDIT BELOW THIS LINE-----
* $Archive: mod_amqp.a$
* $Libraries: -lrabbitmq$
*/
#include "mod_amqp.h"
#include "jot.h"
#include <amqp.h>
#include <amqp_tcp_socket.h>
#ifdef HAVE_AMQP_SSL_SOCKET_H
# include <amqp_ssl_socket.h>
#endif
/* Newer versions of librabbitmq define these delivery mode values via enum. */
#ifndef HAVE_RABBITMQ_DELIVERY_MODE
# define AMQP_DELIVERY_NONPERSISTENT 1
# define AMQP_DELIVERY_PERSISTENT 2
#endif
#define AMQP_DEFAULT_APP_ID "proftpd"
#define AMQP_DEFAULT_MESSAGE_TYPE "log"
#define AMQP_DEFAULT_VHOST "/"
#define AMQP_DEFAULT_USERNAME "guest"
#define AMQP_DEFAULT_PASSWORD "guest"
#define AMQP_DEFAULT_EXCHANGE ""
#define AMQP_DEFAULT_CONNECT_TIMEOUT_MS 2000
extern xaset_t *server_list;
/* From response.c */
extern pr_response_t *resp_list, *resp_err_list;
module amqp_module;
int amqp_logfd = -1;
pool *amqp_pool = NULL;
unsigned long amqp_opts = 0UL;
static int amqp_engine = FALSE;
/* XXX Wrap this in our own struct, for supporting other libs, too. */
static amqp_connection_state_t amqp_conn;
static amqp_channel_t amqp_chan;
static const char *amqp_app_id = NULL;
static const char *amqp_msg_expires = NULL;
static const char *amqp_msg_type = NULL;
/* AMQPOptions */
#define AMQP_OPT_PERSISTENT_DELIVERY 0x0001
static pr_table_t *jot_logfmt2json = NULL;
static const char *trace_channel = "amqp";
/* Necessary function prototypes. */
static int amqp_sess_init(void);
static void amqp_trace_reply(pool *p, amqp_rpc_reply_t reply,
const char *text) {
switch (reply.reply_type) {
case AMQP_RESPONSE_NORMAL:
break;
case AMQP_RESPONSE_NONE:
pr_trace_msg(trace_channel, 3, "%s: missing RPC reply type in response",
text);
break;
case AMQP_RESPONSE_LIBRARY_EXCEPTION:
pr_trace_msg(trace_channel, 3, "%s: %s", text,
amqp_error_string2(reply.library_error));
break;
case AMQP_RESPONSE_SERVER_EXCEPTION: {
switch (reply.reply.id) {
case AMQP_CONNECTION_CLOSE_METHOD: {
amqp_connection_close_t *meth;
meth = reply.reply.decoded;
pr_trace_msg(trace_channel, 3,
"%s: connection exception '%.*s' (%uh)", text,
(int) meth->reply_text.len, (char *) meth->reply_text.bytes,
meth->reply_code);
break;
}
case AMQP_CHANNEL_CLOSE_METHOD: {
amqp_channel_close_t *meth;
meth = reply.reply.decoded;
pr_trace_msg(trace_channel, 3,
"%s: channel exception '%.*s' (%uh)", text,
(int) meth->reply_text.len, (char *) meth->reply_text.bytes,
meth->reply_code);
break;
}
default:
pr_trace_msg(trace_channel, 3,
"%s: unknown broker error: method %s (0x%08x)", text,
amqp_method_name(reply.reply.id), reply.reply.id);
}
break;
}
default:
pr_trace_msg(trace_channel, 3, "%s: unknown reply-type: %d", text,
reply.reply_type);
}
}
static void millis2timeval(struct timeval *tv, unsigned long millis) {
tv->tv_sec = (millis / 1000);
tv->tv_usec = (millis - (tv->tv_sec * 1000)) * 1000;
}
static int amqp_open_conn(pool *p, config_rec *c,
unsigned long connect_millis) {
register unsigned int i;
int connected = FALSE;
array_header *addrs;
amqp_socket_t *sock;
amqp_channel_t chan;
amqp_rpc_reply_t reply;
const char *ip_addr, *vhost, *username = NULL, *password = NULL;
unsigned int port;
amqp_conn = amqp_new_connection();
if (amqp_conn == NULL) {
(void) pr_log_writefile(amqp_logfd, MOD_AMQP_VERSION,
"error allocating memory for RabbitMQ connection");
errno = ENOMEM;
return -1;
}
if (c->argc == 10) {
sock = amqp_ssl_socket_new(amqp_conn);
} else {
sock = amqp_tcp_socket_new(amqp_conn);
}
if (sock == NULL) {
(void) pr_log_writefile(amqp_logfd, MOD_AMQP_VERSION,
"error allocating memory for AMQP socket");
(void) amqp_destroy_connection(amqp_conn);
amqp_conn = NULL;
errno = ENOMEM;
return -1;
}
if (c->argc == 10) {
const char *ssl_cert = NULL, *ssl_key = NULL, *ssl_ca = NULL;
int res, ssl_verify = -1, ssl_verify_hostname = -1, ssl_verify_peer = -1;
ssl_cert = c->argv[4];
ssl_key = c->argv[5];
ssl_ca = c->argv[6];
ssl_verify = *((int *) c->argv[7]);
ssl_verify_hostname = *((int *) c->argv[8]);
ssl_verify_peer = *((int *) c->argv[9]);
if (ssl_cert != NULL &&
ssl_key != NULL) {
pr_trace_msg(trace_channel, 12,
"using SSL client certificate '%s', key '%s'", ssl_cert, ssl_key);
res = amqp_ssl_socket_set_key(sock, ssl_cert, ssl_key);
if (res != AMQP_STATUS_OK) {
pr_trace_msg(trace_channel, 3,
"error setting SSL client certificate: %s", amqp_error_string2(res));
}
}
if (ssl_ca != NULL) {
pr_trace_msg(trace_channel, 12, "using SSL CA '%s'", ssl_ca);
res = amqp_ssl_socket_set_cacert(sock, ssl_ca);
if (res != AMQP_STATUS_OK) {
pr_trace_msg(trace_channel, 3,
"error setting SSL CA: %s", amqp_error_string2(res));
}
}
if (ssl_verify_hostname >= 0 ||
ssl_verify_peer >= 0) {
#ifdef HAVE_AMQP_SSL_SOCKET_SET_VERIFY_HOSTNAME
if (ssl_verify_hostname >= 0) {
pr_trace_msg(trace_channel, 12, "%s SSL hostname verification",
ssl_verify_hostname ? "enabling" : "disabling");
amqp_ssl_socket_set_verify_hostname(sock, ssl_verify_hostname);
}
#endif
#ifdef HAVE_AMQP_SSL_SOCKET_SET_VERIFY_PEER
if (ssl_verify_peer >= 0) {
pr_trace_msg(trace_channel, 12, "%s SSL peer verification",
ssl_verify_peer ? "enabling" : "disabling");
amqp_ssl_socket_set_verify_peer(sock, ssl_verify_peer);
}
#endif
#if !defined(HAVE_AMQP_SSL_SOCKET_SET_VERIFY_HOSTNAME) && \
!defined(HAVE_AMQP_SSL_SOCKET_SET_VERIFY_PEER)
pr_trace_msg(trace_channel, 12, "%s SSL verification",
ssl_verify_peer ? "enabling" : "disabling");
amqp_ssl_socket_set_verify(sock, ssl_verify_peer);
#endif
} else {
pr_trace_msg(trace_channel, 12, "%s SSL verification",
ssl_verify ? "enabling" : "disabling");
#ifdef HAVE_AMQP_SSL_SOCKET_SET_VERIFY_HOSTNAME
amqp_ssl_socket_set_verify_hostname(sock, ssl_verify);
#endif
#ifdef HAVE_AMQP_SSL_SOCKET_SET_VERIFY_PEER
amqp_ssl_socket_set_verify_peer(sock, ssl_verify);
#endif
#if !defined(HAVE_AMQP_SSL_SOCKET_SET_VERIFY_HOSTNAME) && \
!defined(HAVE_AMQP_SSL_SOCKET_SET_VERIFY_PEER)
amqp_ssl_socket_set_verify(sock, ssl_verify);
#endif
}
}
addrs = c->argv[0];
for (i = 0; i < addrs->nelts; i++) {
int res, xerrno;
const pr_netaddr_t *addr;
struct timeval tv;
pr_signals_handle();
addr = ((pr_netaddr_t **) addrs->elts)[i];
ip_addr = pr_netaddr_get_ipstr(addr);
port = ntohs(pr_netaddr_get_port(addr));
millis2timeval(&tv, connect_millis);
pr_trace_msg(trace_channel, 12, "connecting to %s:%u", ip_addr, port);
res = amqp_socket_open_noblock(sock, ip_addr, port, &tv);
xerrno = errno;
switch (res) {
case AMQP_STATUS_OK:
connected = TRUE;
break;
case AMQP_STATUS_SOCKET_ERROR:
pr_trace_msg(trace_channel, 2, "error connecting to %s:%u: %s (%s)",
ip_addr, port, amqp_error_string2(res), strerror(xerrno));
break;
default:
pr_trace_msg(trace_channel, 2, "error connecting to %s:%u: %s", ip_addr,
port, amqp_error_string2(res));
break;
}
if (connected == TRUE) {
break;
}
}
if (connected == FALSE) {
(void) amqp_destroy_connection(amqp_conn);
amqp_conn = NULL;
errno = ENOENT;
return -1;
}
vhost = c->argv[1];
if (vhost == NULL) {
vhost = AMQP_DEFAULT_VHOST;
}
username = c->argv[2];
if (username == NULL) {
username = AMQP_DEFAULT_USERNAME;
}
password = c->argv[3];
if (password == NULL) {
password = AMQP_DEFAULT_PASSWORD;
}
pr_trace_msg(trace_channel, 12,
"logging into %s:%u with vhost '%s', username '%s'", ip_addr, port,
vhost, username);
reply = amqp_login(amqp_conn, vhost, AMQP_DEFAULT_MAX_CHANNELS,
AMQP_DEFAULT_FRAME_SIZE, AMQP_DEFAULT_HEARTBEAT, AMQP_SASL_METHOD_PLAIN,
username, password);
if (reply.reply_type != AMQP_RESPONSE_NORMAL) {
amqp_trace_reply(p, reply, "broker login error");
(void) amqp_connection_close(amqp_conn, AMQP_NOT_ALLOWED);
(void) amqp_destroy_connection(amqp_conn);
amqp_conn = NULL;
errno = EPERM;
return -1;
}
pr_trace_msg(trace_channel, 12,
"authenticated to broker %s:%u with vhost '%s', username '%s'", ip_addr,
port, vhost, username);
chan = 1;
amqp_channel_open(amqp_conn, chan);
reply = amqp_get_rpc_reply(amqp_conn);
if (reply.reply_type != AMQP_RESPONSE_NORMAL) {
amqp_trace_reply(p, reply, "channel opening error");
(void) amqp_connection_close(amqp_conn, AMQP_CHANNEL_ERROR);
(void) amqp_destroy_connection(amqp_conn);
amqp_conn = NULL;
errno = EPERM;
return -1;
}
amqp_chan = chan;
return 0;
}
static int amqp_close_conn(pool *p) {
int res, reason = AMQP_REPLY_SUCCESS;
amqp_rpc_reply_t reply;
if (amqp_conn == NULL) {
return 0;
}
reply = amqp_channel_close(amqp_conn, amqp_chan, reason);
if (reply.reply_type != AMQP_RESPONSE_NORMAL) {
amqp_trace_reply(p, reply, "channel close error");
}
reply = amqp_connection_close(amqp_conn, reason);
if (reply.reply_type != AMQP_RESPONSE_NORMAL) {
amqp_trace_reply(p, reply, "connection close error");
}
res = amqp_destroy_connection(amqp_conn);
if (res != AMQP_STATUS_OK) {
pr_trace_msg(trace_channel, 3, "connection destroy error: %s",
amqp_error_string2(res));
}
amqp_conn = NULL;
amqp_chan = 0;
return 0;
}
static int amqp_send_msg(pool *p, const char *exchange, const char *routing_key,
char *payload, size_t payload_len) {
amqp_basic_properties_t props;
int mandatory, immediate, res;
props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG;
props.content_type = amqp_cstring_bytes("application/json");
props._flags |= AMQP_BASIC_TYPE_FLAG;
props.type = amqp_cstring_bytes(amqp_msg_type);
/* Note: RabbitMQ expects/implements this as a value in millseconds:
* https://www.rabbitmq.com/ttl.html
*/
if (amqp_msg_expires != NULL) {
props._flags |= AMQP_BASIC_EXPIRATION_FLAG;
props.expiration = amqp_cstring_bytes(amqp_msg_expires);
}
props._flags |= AMQP_BASIC_DELIVERY_MODE_FLAG;
if (amqp_opts & AMQP_OPT_PERSISTENT_DELIVERY) {
props.delivery_mode = AMQP_DELIVERY_PERSISTENT;
} else {
props.delivery_mode = AMQP_DELIVERY_NONPERSISTENT;
}
/* Note: While it might be tempting to use AMQP_BASIC_USER_ID_FLAG,
* it will not work as desired. RabbitMQ will verify this user ID againt
* that used for the connection, which is not what we want.
*/
props._flags |= AMQP_BASIC_TIMESTAMP_FLAG;
props.timestamp = time(NULL);
props._flags |= AMQP_BASIC_APP_ID_FLAG;
props.app_id = amqp_cstring_bytes(amqp_app_id);
/* From:
* https://www.rabbitmq.com/amqp-0-9-1-reference.html
*
* mandatory
*
* This flag tells the server how to react if the message cannot be routed * to a queue. If this flag is set, the server will return an unroutable
* message with a Return method. If this flag is zero, the server silently
* drops the message.
*
* immediate
*
* This flag tells the server how to react if the message cannot be routed
* to a queue consumer immediately. If this flag is set, the server will
* return an undeliverable message with a Return method. If this flag is
* zero, the server will queue the message, but with no guarantee that it
* will ever be consumed.
*/
mandatory = TRUE;
immediate = FALSE;
pr_trace_msg(trace_channel, 17,
"publishing AMQP message: channel %u, exchange '%s', routing key '%s'",
(unsigned int) amqp_chan, exchange, routing_key);
res = amqp_basic_publish(amqp_conn, amqp_chan,
amqp_cstring_bytes(exchange),
amqp_cstring_bytes(routing_key),
mandatory,
immediate,
&props,
amqp_cstring_bytes(payload));
if (res != AMQP_STATUS_OK) {
pr_trace_msg(trace_channel, 3, "message publish error: %s",
amqp_error_string2(res));
errno = EIO;
return -1;
}
return 0;
}
/* Logging */
struct amqp_buffer {
char *ptr, *buf;
size_t bufsz, buflen;
};
static void amqp_buffer_append_text(struct amqp_buffer *log, const char *text,
size_t text_len) {
if (text == NULL ||
text_len == 0) {
return;
}
if (text_len > log->buflen) {
text_len = log->buflen;
}
pr_trace_msg(trace_channel, 19, "appending text '%.*s' (%lu) to buffer",
(int) text_len, text, (unsigned long) text_len);
memcpy(log->buf, text, text_len);
log->buf += text_len;
log->buflen -= text_len;
}
static int resolve_on_meta(pool *p, pr_jot_ctx_t *jot_ctx,
unsigned char logfmt_id, const char *jot_hint, const void *val) {
struct amqp_buffer *log;
log = jot_ctx->log;
if (log->buflen > 0) {
const char *text = NULL;
size_t text_len = 0;
char buf[1024];
switch (logfmt_id) {
case LOGFMT_META_MICROSECS: {
unsigned long num;
num = *((double *) val);
text_len = pr_snprintf(buf, sizeof(buf)-1, "%06lu", num);
buf[text_len] = '\0';
text = buf;
break;
}
case LOGFMT_META_MILLISECS: {
unsigned long num;
num = *((double *) val);
text_len = pr_snprintf(buf, sizeof(buf)-1, "%03lu", num);
buf[text_len] = '\0';
text = buf;
break;
}
case LOGFMT_META_LOCAL_PORT:
case LOGFMT_META_REMOTE_PORT:
case LOGFMT_META_RESPONSE_CODE: {
int num;
num = *((double *) val);
text_len = pr_snprintf(buf, sizeof(buf)-1, "%d", num);
buf[text_len] = '\0';
text = buf;
break;
}
case LOGFMT_META_UID: {
uid_t uid;
uid = *((double *) val);
text = pr_uid2str(p, uid);
break;
}
case LOGFMT_META_GID: {
gid_t gid;
gid = *((double *) val);
text = pr_gid2str(p, gid);
break;
}
case LOGFMT_META_BYTES_SENT:
case LOGFMT_META_FILE_OFFSET:
case LOGFMT_META_FILE_SIZE:
case LOGFMT_META_RAW_BYTES_IN:
case LOGFMT_META_RAW_BYTES_OUT:
case LOGFMT_META_RESPONSE_MS:
case LOGFMT_META_XFER_MS: {
off_t num;
num = *((double *) val);
text_len = pr_snprintf(buf, sizeof(buf)-1, "%" PR_LU, (pr_off_t) num);
buf[text_len] = '\0';
text = buf;
break;
}
case LOGFMT_META_EPOCH:
case LOGFMT_META_PID: {
unsigned long num;
num = *((double *) val);
text_len = pr_snprintf(buf, sizeof(buf)-1, "%lu", num);
buf[text_len] = '\0';
text = buf;
break;
}
case LOGFMT_META_FILE_MODIFIED: {
int truth;
truth = *((int *) val);
text = truth ? "true" : "false";
break;
}
case LOGFMT_META_SECONDS: {
float num;
num = *((double *) val);
text_len = pr_snprintf(buf, sizeof(buf)-1, "%0.3f", num);
buf[text_len] = '\0';
text = buf;
break;
}
case LOGFMT_META_ANON_PASS:
case LOGFMT_META_BASENAME:
case LOGFMT_META_CLASS:
case LOGFMT_META_CMD_PARAMS:
case LOGFMT_META_COMMAND:
case LOGFMT_META_DIR_NAME:
case LOGFMT_META_DIR_PATH:
case LOGFMT_META_ENV_VAR:
case LOGFMT_META_EOS_REASON:
case LOGFMT_META_FILENAME:
case LOGFMT_META_GROUP:
case LOGFMT_META_IDENT_USER:
case LOGFMT_META_ISO8601:
case LOGFMT_META_LOCAL_FQDN:
case LOGFMT_META_LOCAL_IP:
case LOGFMT_META_LOCAL_NAME:
case LOGFMT_META_METHOD:
case LOGFMT_META_NOTE_VAR:
case LOGFMT_META_ORIGINAL_USER:
case LOGFMT_META_PROTOCOL:
case LOGFMT_META_REMOTE_HOST:
case LOGFMT_META_REMOTE_IP:
case LOGFMT_META_RENAME_FROM:
case LOGFMT_META_RESPONSE_STR:
case LOGFMT_META_TIME:
case LOGFMT_META_USER:
case LOGFMT_META_VERSION:
case LOGFMT_META_VHOST_IP:
case LOGFMT_META_XFER_FAILURE:
case LOGFMT_META_XFER_PATH:
case LOGFMT_META_XFER_STATUS:
case LOGFMT_META_XFER_TYPE:
default:
text = val;
}
if (text != NULL &&
text_len == 0) {
text_len = strlen(text);
}
amqp_buffer_append_text(log, text, text_len);
}
return 0;
}
static int resolve_on_other(pool *p, pr_jot_ctx_t *jot_ctx, unsigned char *text,
size_t text_len) {
struct amqp_buffer *log;
log = jot_ctx->log;
amqp_buffer_append_text(log, (const char *) text, text_len);
return 0;
}
static void log_event(amqp_channel_t chan, config_rec *c, cmd_rec *cmd) {
pool *tmp_pool;
int res;
pr_jot_ctx_t *jot_ctx;
pr_jot_filters_t *jot_filters;
pr_json_object_t *json;
const char *fmt_name = NULL;
char *payload = NULL;
size_t payload_len = 0;
unsigned char *log_fmt, *exchange_fmt, *routing_fmt;
jot_filters = c->argv[0];
fmt_name = c->argv[1];
log_fmt = c->argv[2];
exchange_fmt = c->argv[3];
routing_fmt = c->argv[4];
if (jot_filters == NULL ||
fmt_name == NULL ||
log_fmt == NULL) {
return;
}
tmp_pool = make_sub_pool(cmd->tmp_pool);
jot_ctx = pcalloc(tmp_pool, sizeof(pr_jot_ctx_t));
json = pr_json_object_alloc(tmp_pool);
jot_ctx->log = json;
jot_ctx->user_data = jot_logfmt2json;
res = pr_jot_resolve_logfmt(tmp_pool, cmd, jot_filters, log_fmt, jot_ctx,
pr_jot_on_json, NULL, NULL);
if (res == 0) {
payload = pr_json_object_to_text(tmp_pool, json, "");
payload_len = strlen(payload);
pr_trace_msg(trace_channel, 8, "generated JSON payload for %s: %.*s",
(char *) cmd->argv[0], (int) payload_len, payload);
} else {
/* EPERM indicates that the message was filtered. */
if (errno != EPERM) {
(void) pr_log_writefile(amqp_logfd, MOD_AMQP_VERSION,
"error generating JSON formatted log message: %s", strerror(errno));
}
payload = NULL;
payload_len = 0;
}
pr_json_object_free(json);
if (payload_len > 0) {
const char *exchange, *routing_key;
if (exchange_fmt != NULL) {
struct amqp_buffer *rb;
char exchange_buf[1024];
rb = pcalloc(tmp_pool, sizeof(struct amqp_buffer));
rb->bufsz = rb->buflen = sizeof(exchange_buf)-1;
rb->ptr = rb->buf = exchange_buf;
jot_ctx->log = rb;
res = pr_jot_resolve_logfmt(tmp_pool, cmd, NULL, exchange_fmt, jot_ctx,
resolve_on_meta, NULL, resolve_on_other);
if (res == 0) {
size_t exchange_buflen;
exchange_buflen = rb->bufsz - rb->buflen;
exchange = pstrndup(tmp_pool, exchange_buf, exchange_buflen);
} else {
(void) pr_log_writefile(amqp_logfd, MOD_AMQP_VERSION,
"error resolving AMQP exchange format '%s': %s", exchange_fmt,
strerror(errno));
exchange = AMQP_DEFAULT_EXCHANGE;
}
} else {
exchange = AMQP_DEFAULT_EXCHANGE;
}
if (routing_fmt != NULL) {
struct amqp_buffer *rb;
char routing_buf[1024];
rb = pcalloc(tmp_pool, sizeof(struct amqp_buffer));
rb->bufsz = rb->buflen = sizeof(routing_buf)-1;
rb->ptr = rb->buf = routing_buf;
jot_ctx->log = rb;
res = pr_jot_resolve_logfmt(tmp_pool, cmd, NULL, routing_fmt, jot_ctx,
resolve_on_meta, NULL, resolve_on_other);
if (res == 0) {
size_t routing_buflen;
routing_buflen = rb->bufsz - rb->buflen;
routing_key = pstrndup(tmp_pool, routing_buf, routing_buflen);
} else {
(void) pr_log_writefile(amqp_logfd, MOD_AMQP_VERSION,
"error resolving AMQP routing key format '%s': %s", routing_fmt,
strerror(errno));
routing_key = fmt_name;
}
} else {
routing_key = fmt_name;
}
res = amqp_send_msg(cmd->tmp_pool, exchange, routing_key, payload,
payload_len);
if (res < 0) {
(void) pr_log_writefile(amqp_logfd, MOD_AMQP_VERSION,
"error publishing log message to '%s#%s': %s", exchange, routing_key,
strerror(errno));
} else {
pr_trace_msg(trace_channel, 17, "published log message to '%s#%s'",
exchange, routing_key);
}
}
destroy_pool(tmp_pool);
}
static void log_events(cmd_rec *cmd) {
config_rec *c;
c = find_config(CURRENT_CONF, CONF_PARAM, "AMQPLogOnEvent", FALSE);
while (c != NULL) {
pr_signals_handle();
log_event(amqp_chan, c, cmd);
c = find_config_next(c, c->next, CONF_PARAM, "AMQPLogOnEvent", FALSE);
}
}
/* Configuration handlers
*/
/* usage: AMQPApplicationID name */
MODRET set_amqpapplicationid(cmd_rec *cmd) {
CHECK_ARGS(cmd, 1);
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
(void) add_config_param_str(cmd->argv[0], 1, cmd->argv[1]);
return PR_HANDLED(cmd);
}
/* usage: AMQPEngine on|off */
MODRET set_amqpengine(cmd_rec *cmd) {
int engine = 1;
config_rec *c;
CHECK_ARGS(cmd, 1);
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
engine = get_boolean(cmd, 1);
if (engine == -1) {
CONF_ERROR(cmd, "expected Boolean parameter");
}
c = add_config_param(cmd->argv[0], 1, NULL);
c->argv[0] = pcalloc(c->pool, sizeof(int));
*((int *) c->argv[0]) = engine;
return PR_HANDLED(cmd);
}
/* usage: AMQPLog path|"none" */
MODRET set_amqplog(cmd_rec *cmd) {
CHECK_ARGS(cmd, 1);
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
(void) add_config_param_str(cmd->argv[0], 1, cmd->argv[1]);
return PR_HANDLED(cmd);
}
static unsigned char *parse_jotted_key(pool *p, const char *text) {
int res;
pool *tmp_pool;
pr_jot_ctx_t *jot_ctx;
pr_jot_parsed_t *jot_parsed;
unsigned char fmtbuf[1024], *fmt = NULL;
size_t fmtlen;
tmp_pool = make_sub_pool(p);
jot_ctx = pcalloc(tmp_pool, sizeof(pr_jot_ctx_t));
jot_parsed = pcalloc(tmp_pool, sizeof(pr_jot_parsed_t));
jot_parsed->bufsz = jot_parsed->buflen = sizeof(fmtbuf)-1;
jot_parsed->ptr = jot_parsed->buf = fmtbuf;
jot_ctx->log = jot_parsed;
res = pr_jot_parse_logfmt(tmp_pool, text, jot_ctx,
pr_jot_parse_on_meta, pr_jot_parse_on_unknown, pr_jot_parse_on_other, 0);
destroy_pool(tmp_pool);
if (res < 0) {
return NULL;
}
fmtlen = jot_parsed->bufsz - jot_parsed->buflen;
fmtbuf[fmtlen] = '\0';
fmt = (unsigned char *) pstrndup(p, (char *) fmtbuf, fmtlen);
return fmt;
}
/* usage: AMQPLogOnEvent "none"|events log-fmt [exchange ...] [routing ...] */
MODRET set_amqplogonevent(cmd_rec *cmd) {
register unsigned int i;
config_rec *c, *logfmt_config;
const char *fmt_name, *rules;
char *exchange = NULL, *routing_key = NULL;
unsigned char *log_fmt = NULL, *exchange_fmt = NULL, *routing_fmt = NULL;
pr_jot_filters_t *jot_filters;
CHECK_CONF(cmd, CONF_ROOT|CONF_GLOBAL|CONF_VIRTUAL|CONF_ANON|CONF_DIR);
if (cmd->argc < 3 ||
cmd->argc > 7) {
if (cmd->argc == 2 &&
strcasecmp(cmd->argv[1], "none") == 0) {
c = add_config_param(cmd->argv[0], 5, NULL, NULL, NULL, NULL, NULL);
c->flags |= CF_MERGEDOWN;
return PR_HANDLED(cmd);
}
CONF_ERROR(cmd, "wrong number of parameters");
}
if ((cmd->argc - 3) % 2 != 0) {
CONF_ERROR(cmd, "wrong number of parameters");
}
c = add_config_param(cmd->argv[0], 5, NULL, NULL, NULL, NULL, NULL);
rules = cmd->argv[1];
jot_filters = pr_jot_filters_create(c->pool, rules,
PR_JOT_FILTER_TYPE_COMMANDS_WITH_CLASSES,
PR_JOT_FILTER_FL_ALL_INCL_ALL);
if (jot_filters == NULL) {
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unable to use events '", rules,
"': ", strerror(errno), NULL));
}
fmt_name = cmd->argv[2];
for (i = 3; i < cmd->argc; i += 2) {
if (strcmp(cmd->argv[i], "exchange") == 0) {
exchange = cmd->argv[i+1];
} else if (strcmp(cmd->argv[i], "routing") == 0) {
routing_key = cmd->argv[i+1];
} else {
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
": unknown AMQPLogOnEvent parameter: ", cmd->argv[i], NULL));
}
}
/* Make sure that the given LogFormat name is known. */
logfmt_config = find_config(cmd->server->conf, CONF_PARAM, "LogFormat",
FALSE);
while (logfmt_config != NULL) {
pr_signals_handle();
if (strcmp(fmt_name, logfmt_config->argv[0]) == 0) {
log_fmt = logfmt_config->argv[1];
break;
}
logfmt_config = find_config_next(logfmt_config, logfmt_config->next,
CONF_PARAM, "LogFormat", FALSE);
}
if (log_fmt == NULL) {
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "no LogFormat '", fmt_name,
"' configured", NULL));
}
if (exchange != NULL) {
exchange_fmt = parse_jotted_key(c->pool, exchange);
if (exchange_fmt == NULL) {
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
"error parsing AMQPLogOnEvent parameter '", exchange, "': ",
strerror(errno), NULL));
}
}
if (routing_key != NULL) {
routing_fmt = parse_jotted_key(c->pool, routing_key);
if (routing_fmt == NULL) {
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
"error parsing AMQPLogEvent parameter '", routing_key, "': ",
strerror(errno), NULL));
}
}
c->argv[0] = jot_filters;
c->argv[1] = pstrdup(c->pool, fmt_name);
c->argv[2] = log_fmt;
c->argv[3] = exchange_fmt;
c->argv[4] = routing_fmt;
return PR_HANDLED(cmd);
}
/* usage: AMQPMessageExpires millis */
MODRET set_amqpmessageexpires(cmd_rec *cmd) {
unsigned long expires = 0;
char *ptr = NULL;
CHECK_ARGS(cmd, 1);
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
expires = strtoul(cmd->argv[1], &ptr, 10);
if (ptr && *ptr) {
CONF_ERROR(cmd, "invalid parameter");
}
/* The librabbitmq API for message expiration uses string, surprisingly;
* we thus just copy the given parameter.
*/
(void) add_config_param_str(cmd->argv[0], 1, cmd->argv[1]);
return PR_HANDLED(cmd);
}
/* usage: AMQPMessageType name */
MODRET set_amqpmessagetype(cmd_rec *cmd) {
CHECK_ARGS(cmd, 1);
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
(void) add_config_param_str(cmd->argv[0], 1, cmd->argv[1]);
return PR_HANDLED(cmd);
}
/* usage: AMQPOptions ... */
MODRET set_amqpoptions(cmd_rec *cmd) {
config_rec *c = NULL;
register unsigned int i = 0;
unsigned long opts = 0UL;
if (cmd->argc-1 == 0) {
CONF_ERROR(cmd, "wrong number of parameters");
}
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);