Skip to content

Commit f6d60fd

Browse files
committed
Merge pull request #843 from taotetek/master
Problem: we would like to use czmq v3 + encryption and curve authentication
2 parents c88f523 + 0bc8bdf commit f6d60fd

File tree

6 files changed

+91
-2
lines changed

6 files changed

+91
-2
lines changed

src/analysisd/analysisd.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,11 @@ int main_analysisd(int argc, char **argv)
257257
#ifdef ZEROMQ_OUTPUT_ENABLED
258258
/* Start zeromq */
259259
if (Config.zeromq_output) {
260+
#if CZMQ_VERSION_MAJOR == 2
260261
zeromq_output_start(Config.zeromq_output_uri);
262+
#elif CZMQ_VERSION_MAJOR >= 3
263+
zeromq_output_start(Config.zeromq_output_uri, Config.zeromq_output_client_cert, Config.zeromq_output_server_cert);
264+
#endif
261265
}
262266
#endif
263267

src/analysisd/config.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ int GlobalConf(const char *cfgfile)
3232
Config.prelude = 0;
3333
Config.zeromq_output = 0;
3434
Config.zeromq_output_uri = NULL;
35+
Config.zeromq_output_server_cert = NULL;
36+
Config.zeromq_output_client_cert = NULL;
3537
Config.jsonout_output = 0;
3638
Config.memorysize = 1024;
3739
Config.mailnotify = -1;

src/analysisd/output/zeromq.c

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,19 @@
1313

1414
#include "shared.h"
1515
#include "rules.h"
16-
#include "czmq.h"
1716
#include "format/to_json.h"
1817

1918

2019
/* Global variables */
20+
#if CZMQ_VERSION_MAJOR == 2
2121
static zctx_t *zeromq_context;
2222
static void *zeromq_pubsocket;
23+
#elif CZMQ_VERSION_MAJOR >= 3
24+
zsock_t *zeromq_pubsocket;
25+
zactor_t *auth;
26+
#endif
2327

24-
28+
#if CZMQ_VERSION_MAJOR == 2
2529
void zeromq_output_start(const char *uri)
2630
{
2731
int rc;
@@ -47,13 +51,64 @@ void zeromq_output_start(const char *uri)
4751
return;
4852
}
4953
}
54+
#elif CZMQ_VERSION_MAJOR >= 3
55+
void zeromq_output_start(const char *uri, const char *client_cert_path, const char *server_cert_path)
56+
{
57+
int rc;
58+
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);
63+
return;
64+
}
5065

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);
70+
if (!auth) {
71+
merror("%s: Unable to start auth for ZeroMQ Sock", ARGV0);
72+
}
73+
zstr_sendx(auth, "CURVE", client_cert_path, NULL);
74+
zsock_wait(auth);
75+
76+
zcert_t *server_cert = zcert_load(server_cert_path);
77+
if (!server_cert) {
78+
merror("%s: Unable to load server certificate: %s.", ARGV0, server_cert_path);
79+
}
80+
81+
zcert_apply(server_cert, zeromq_pubsocket);
82+
zsock_set_curve_server(zeromq_pubsocket, 1);
83+
84+
zcert_destroy(&server_cert);
85+
}
86+
}
87+
88+
debug1("%s: DEBUG: Listening on ZeroMQ Socket: %s", ARGV0, uri);
89+
rc = zsock_bind(zeromq_pubsocket, "%s", uri);
90+
if (rc) {
91+
merror("%s: Unable to bind the ZeroMQ Socket: %s.", ARGV0, uri);
92+
return;
93+
}
94+
}
95+
#endif
96+
97+
#if CZMQ_VERSION_MAJOR == 2
5198
void zeromq_output_end()
5299
{
53100
zsocket_destroy(zeromq_context, zeromq_pubsocket);
54101
zctx_destroy(&zeromq_context);
55102
}
103+
#elif CZMQ_VERSION_MAJOR >= 3
104+
void zeromq_output_end()
105+
{
106+
zsock_destroy(&zeromq_pubsocket);
107+
zactor_destroy(&auth);
108+
}
109+
#endif
56110

111+
#if CZMQ_VERSION_MAJOR == 2
57112
void zeromq_output_event(const Eventinfo *lf)
58113
{
59114
char *json_alert = Eventinfo_to_jsonstr(lf);
@@ -64,6 +119,17 @@ void zeromq_output_event(const Eventinfo *lf)
64119
zmsg_send(&msg, zeromq_pubsocket);
65120
free(json_alert);
66121
}
122+
#elif ZMQ_VERSION_MAJOR >= 3
123+
void zeromq_output_event(const Eventinfo *lf)
124+
{
125+
char *json_alert = Eventinfo_to_jsonstr(lf);
67126

127+
zmsg_t *msg = zmsg_new();
128+
zmsg_addstr(msg, "ossec.alerts");
129+
zmsg_addstr(msg, json_alert);
130+
zmsg_send(&msg, zeromq_pubsocket);
131+
free(json_alert);
132+
}
68133
#endif
69134

135+
#endif

src/analysisd/output/zeromq.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@
1313
#define _ZEROMQ_H_
1414

1515
#include "eventinfo.h"
16+
#include <czmq.h>
1617

1718
void zeromq_output_event(const Eventinfo *lf);
19+
#if CZMQ_VERSION_MAJOR == 2
1820
void zeromq_output_start(const char *uri);
21+
#elif CZMQ_VERSION_MAJOR >= 3
22+
void zeromq_output_start(const char *uri, const char *client_cert_path, const char *server_cert_path);
23+
#endif
1924
void zeromq_output_end(void);
2025

2126

src/config/global-config.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ int Read_Global(XML_NODE node, void *configp, void *mailp)
105105
const char *xml_prelude_log_level = "prelude_log_level";
106106
const char *xml_zeromq_output = "zeromq_output";
107107
const char *xml_zeromq_output_uri = "zeromq_uri";
108+
const char *xml_zeromq_output_server_cert = "zeromq_server_cert";
109+
const char *xml_zeromq_output_client_cert = "zeromq_client_cert";
108110
const char *xml_jsonout_output = "jsonout_output";
109111
const char *xml_stats = "stats";
110112
const char *xml_memorysize = "memory_size";
@@ -262,6 +264,14 @@ int Read_Global(XML_NODE node, void *configp, void *mailp)
262264
if (Config) {
263265
Config->zeromq_output_uri = strdup(node[i]->content);
264266
}
267+
} else if (strcmp(node[i]->element, xml_zeromq_output_server_cert) == 0) {
268+
if (Config) {
269+
Config->zeromq_output_server_cert = strdup(node[i]->content);
270+
}
271+
} else if (strcmp(node[i]->element, xml_zeromq_output_client_cert) == 0) {
272+
if (Config) {
273+
Config->zeromq_output_client_cert = strdup(node[i]->content);
274+
}
265275
}
266276
/* jsonout output */
267277
else if (strcmp(node[i]->element, xml_jsonout_output) == 0) {

src/config/global-config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ typedef struct __Config {
3535
/* ZEROMQ Export */
3636
u_int8_t zeromq_output;
3737
char *zeromq_output_uri;
38+
char *zeromq_output_server_cert;
39+
char *zeromq_output_client_cert;
3840

3941
/* JSONOUT Export */
4042
u_int8_t jsonout_output;

0 commit comments

Comments
 (0)