1 /* Copyright (C) 2015 Trend Micro Inc.
4 * This program is a free software; you can redistribute it
5 * and/or modify it under the terms of the GNU General Public
6 * License (version 2) as published by the FSF - Free Software
10 #ifdef ZEROMQ_OUTPUT_ENABLED
16 #include "format/to_json.h"
19 /* Global variables */
20 #if CZMQ_VERSION_MAJOR == 2
21 static zctx_t *zeromq_context;
22 static void *zeromq_pubsocket;
23 #elif CZMQ_VERSION_MAJOR >= 3
24 zsock_t *zeromq_pubsocket;
28 #if CZMQ_VERSION_MAJOR == 2
29 void zeromq_output_start(const char *uri)
33 debug1("%s: DEBUG: New ZeroMQ Context", ARGV0);
34 zeromq_context = zctx_new();
35 if (zeromq_context == NULL) {
36 merror("%s: Unable to initialize ZeroMQ library", ARGV0);
40 debug1("%s: DEBUG: New ZeroMQ Socket: ZMQ_PUB", ARGV0);
41 zeromq_pubsocket = zsocket_new(zeromq_context, ZMQ_PUB);
42 if (zeromq_pubsocket == NULL) {
43 merror("%s: Unable to initialize ZeroMQ Socket", ARGV0);
47 debug1("%s: DEBUG: Listening on ZeroMQ Socket: %s", ARGV0, uri);
48 rc = zsocket_bind(zeromq_pubsocket, "%s", uri);
50 merror("%s: Unable to bind the ZeroMQ Socket: %s.", ARGV0, uri);
54 #elif CZMQ_VERSION_MAJOR >= 3
55 void zeromq_output_start(const char *uri, const char *client_cert_path, const char *server_cert_path)
59 debug1("%s: DEBUG: New ZeroMQ Socket: ZMQ_PUB", ARGV0);
60 zeromq_pubsocket = zsock_new(ZMQ_PUB);
61 if (zeromq_pubsocket == NULL) {
62 merror("%s: Unable to initialize ZeroMQ Socket", ARGV0);
66 if (zsys_has_curve()) {
67 if (client_cert_path && server_cert_path) {
68 debug1("%s: DEBUG: Initiating CURVE for ZeroMQ Socket", ARGV0);
69 auth = zactor_new(zauth, NULL);
71 merror("%s: Unable to start auth for ZeroMQ Sock", ARGV0);
73 zstr_sendx(auth, "CURVE", client_cert_path, NULL);
76 zcert_t *server_cert = zcert_load(server_cert_path);
78 merror("%s: Unable to load server certificate: %s.", ARGV0, server_cert_path);
81 zcert_apply(server_cert, zeromq_pubsocket);
82 zsock_set_curve_server(zeromq_pubsocket, 1);
84 zcert_destroy(&server_cert);
88 debug1("%s: DEBUG: Listening on ZeroMQ Socket: %s", ARGV0, uri);
89 rc = zsock_bind(zeromq_pubsocket, "%s", uri);
91 merror("%s: Unable to bind the ZeroMQ Socket: %s.", ARGV0, uri);
97 #if CZMQ_VERSION_MAJOR == 2
98 void zeromq_output_end()
100 zsocket_destroy(zeromq_context, zeromq_pubsocket);
101 zctx_destroy(&zeromq_context);
103 #elif CZMQ_VERSION_MAJOR >= 3
104 void zeromq_output_end()
106 zsock_destroy(&zeromq_pubsocket);
107 zactor_destroy(&auth);
111 #if CZMQ_VERSION_MAJOR == 2
112 void zeromq_output_event(const Eventinfo *lf)
114 char *json_alert = Eventinfo_to_jsonstr(lf);
116 zmsg_t *msg = zmsg_new();
117 zmsg_addstr(msg, "ossec.alerts");
118 zmsg_addstr(msg, json_alert);
119 zmsg_send(&msg, zeromq_pubsocket);
122 #elif ZMQ_VERSION_MAJOR >= 3
123 void zeromq_output_event(const Eventinfo *lf)
125 char *json_alert = Eventinfo_to_jsonstr(lf);
127 zmsg_t *msg = zmsg_new();
128 zmsg_addstr(msg, "ossec.alerts");
129 zmsg_addstr(msg, json_alert);
130 zmsg_send(&msg, zeromq_pubsocket);