new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / analysisd / output / zeromq.c
1 /* Copyright (C) 2015 Trend Micro Inc.
2  * All rights reserved.
3  *
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
7  * Foundation.
8  */
9
10 #ifdef ZEROMQ_OUTPUT_ENABLED
11
12 #include "zeromq.h"
13
14 #include "shared.h"
15 #include "rules.h"
16 #include "format/to_json.h"
17
18
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;
25 zactor_t *auth;
26 #endif
27
28 #if CZMQ_VERSION_MAJOR == 2
29 void zeromq_output_start(const char *uri)
30 {
31     int rc;
32
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);
37         return;
38     }
39
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);
44         return;
45     }
46
47     debug1("%s: DEBUG: Listening on ZeroMQ Socket: %s", ARGV0, uri);
48     rc = zsocket_bind(zeromq_pubsocket, "%s", uri);
49     if (rc) {
50         merror("%s: Unable to bind the ZeroMQ Socket: %s.", ARGV0, uri);
51         return;
52     }
53 }
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     }
65
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
98 void zeromq_output_end()
99 {
100     zsocket_destroy(zeromq_context, zeromq_pubsocket);
101     zctx_destroy(&zeromq_context);
102 }
103 #elif CZMQ_VERSION_MAJOR >= 3
104 void zeromq_output_end()
105 {
106     zsock_destroy(&zeromq_pubsocket);
107     zactor_destroy(&auth);
108 }
109 #endif
110
111 #if CZMQ_VERSION_MAJOR == 2
112 void zeromq_output_event(const Eventinfo *lf)
113 {
114     char *json_alert = Eventinfo_to_jsonstr(lf);
115
116     zmsg_t *msg = zmsg_new();
117     zmsg_addstr(msg, "ossec.alerts");
118     zmsg_addstr(msg, json_alert);
119     zmsg_send(&msg, zeromq_pubsocket);
120     free(json_alert);
121 }
122 #elif ZMQ_VERSION_MAJOR >= 3
123 void zeromq_output_event(const Eventinfo *lf)
124 {
125     char *json_alert = Eventinfo_to_jsonstr(lf);
126
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 }
133 #endif
134
135 #endif