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
|
/*
* Copyright (c) 2010-2016 Balabit
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation, 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 St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/
#include "afmongodb-legacy-uri.h"
#include "afmongodb-legacy-private.h"
#include "host-list.h"
#define DEFAULTHOST "127.0.0.1"
#define SOCKET_TIMEOUT_FOR_MONGO_CONNECTION_IN_MILLISECS 60000
static gboolean
_parse_addr(const char *str, char **host, gint *port)
{
if (!host || !port)
{
msg_debug("Host or port reference should not be NULL");
return FALSE;
}
char *proto_str = g_strdup_printf("mongodb://%s", str);
mongoc_uri_t *uri = mongoc_uri_new(proto_str);
if (!uri)
{
msg_error("Cannot parse MongoDB URI",
evt_tag_str("uri", proto_str));
g_free(proto_str);
return FALSE;
}
const mongoc_host_list_t *hosts = mongoc_uri_get_hosts(uri);
if (!hosts || hosts->next)
{
if (hosts)
msg_error("Multiple hosts found in MongoDB URI",
evt_tag_str("uri", proto_str));
else
msg_error("No host found in MongoDB URI",
evt_tag_str("uri", proto_str));
g_free(proto_str);
mongoc_uri_destroy(uri);
return FALSE;
}
*port = hosts->port;
*host = g_strdup(hosts->host);
mongoc_uri_destroy(uri);
if (!*host)
{
msg_error("NULL hostname",
evt_tag_str("uri", proto_str));
g_free(proto_str);
return FALSE;
}
g_free(proto_str);
return TRUE;
}
static gboolean
_append_legacy_servers(MongoDBDestDriver *self)
{
if (self->port != MONGO_CONN_LOCAL)
{
if (self->address || self->port)
{
gchar *host = NULL;
gint port = 0;
if (!_parse_addr(self->address, &host, &port) || (!host))
{
msg_error("Cannot parse the primary host",
evt_tag_str("primary", self->address),
evt_tag_str("driver", self->super.super.super.id));
return FALSE;
}
g_free(host);
port = self->port ? self->port : MONGOC_DEFAULT_PORT;
const gchar *address = self->address ? self->address : DEFAULTHOST;
gchar *srv = g_strdup_printf("%s:%d", address, port);
self->servers = g_list_prepend(self->servers, srv);
g_free(self->address);
self->address = NULL;
self->port = MONGOC_DEFAULT_PORT;
}
if (self->servers)
{
GList *l;
for (l = self->servers; l; l = g_list_next(l))
{
gchar *host = NULL;
gint port = MONGOC_DEFAULT_PORT;
if (!_parse_addr(l->data, &host, &port))
{
msg_warning("Cannot parse MongoDB server address, ignoring",
evt_tag_str("address", l->data),
evt_tag_str("driver", self->super.super.super.id));
continue;
}
host_list_append(&self->recovery_cache, host, port);
msg_verbose("Added MongoDB server seed",
evt_tag_str("host", host),
evt_tag_int("port", port),
evt_tag_str("driver", self->super.super.super.id));
g_free(host);
}
}
else
{
gchar *localhost = g_strdup_printf(DEFAULTHOST ":%d", MONGOC_DEFAULT_PORT);
self->servers = g_list_append(NULL, localhost);
host_list_append(&self->recovery_cache, DEFAULTHOST, MONGOC_DEFAULT_PORT);
}
if (!_parse_addr(g_list_nth_data(self->servers, 0), &self->address, &self->port))
{
msg_error("Cannot parse the primary host",
evt_tag_str("primary", g_list_nth_data(self->servers, 0)),
evt_tag_str("driver", self->super.super.super.id));
return FALSE;
}
}
else
{
if (!self->address)
{
msg_error("Cannot parse address",
evt_tag_str("primary", g_list_nth_data(self->servers, 0)),
evt_tag_str("driver", self->super.super.super.id));
return FALSE;
}
host_list_append(&self->recovery_cache, self->address, 0);
}
return TRUE;
}
typedef struct _AppendServerState
{
GString *uri_str;
gboolean *have_uri;
gboolean have_path;
} AppendServerState;
static gboolean
_append_server(gpointer user_data, const char *host, gint port)
{
AppendServerState *state = (AppendServerState *)user_data;
if (state->have_path || *state->have_uri)
g_string_append_printf(state->uri_str, ",");
if (port)
{
*state->have_uri = TRUE;
if (state->have_path)
{
msg_warning("Cannot specify both a domain socket and address");
return FALSE;
}
g_string_append_printf(state->uri_str, "%s:%d", host, port);
}
else
{
state->have_path = TRUE;
if (*state->have_uri)
{
msg_warning("Cannot specify both a domain socket and address");
return FALSE;
}
g_string_append_printf(state->uri_str, "%s", host);
}
return TRUE;
}
static gboolean
_append_servers(GString *uri_str, const HostList *host_list, gboolean *have_uri)
{
*have_uri = FALSE;
AppendServerState state = {uri_str, have_uri, FALSE};
return host_list_iterate(host_list, _append_server, &state);
}
static gboolean
_check_auth_options(MongoDBDestDriver *self)
{
if (self->user || self->password)
{
if (!self->user || !self->password)
{
msg_error("Neither the username, nor the password can be empty");
return FALSE;
}
}
return TRUE;
}
gboolean
_build_uri_from_legacy_options(MongoDBDestDriver *self)
{
if (!_append_legacy_servers(self))
return FALSE;
self->uri_str = g_string_new("mongodb://");
if (self->user && self->password)
g_string_append_printf(self->uri_str, "%s:%s@", self->user, self->password);
if (!self->recovery_cache)
{
msg_error("Error in host server list",
evt_tag_str("driver", self->super.super.super.id));
return FALSE;
}
gboolean have_uri;
if (!_append_servers(self->uri_str, self->recovery_cache, &have_uri))
return FALSE;
if (have_uri)
g_string_append_printf(self->uri_str, "/%s", self->db);
if (self->safe_mode)
g_string_append_printf(self->uri_str,
"?wtimeoutMS=%d",
SOCKET_TIMEOUT_FOR_MONGO_CONNECTION_IN_MILLISECS);
else
g_string_append(self->uri_str, "?w=0&safe=false");
g_string_append_printf(self->uri_str,
"&socketTimeoutMS=%d&connectTimeoutMS=%d",
SOCKET_TIMEOUT_FOR_MONGO_CONNECTION_IN_MILLISECS,
SOCKET_TIMEOUT_FOR_MONGO_CONNECTION_IN_MILLISECS);
return TRUE;
}
gboolean
afmongodb_dd_create_uri_from_legacy(MongoDBDestDriver *self)
{
if (!_check_auth_options(self))
return FALSE;
if (self->uri_str && self->is_legacy)
{
msg_error("Error: either specify a MongoDB URI (and optional collection) or only legacy options",
evt_tag_str("driver", self->super.super.super.id));
return FALSE;
}
else if (self->is_legacy)
return _build_uri_from_legacy_options(self);
else
return TRUE;
}
void
afmongodb_dd_init_legacy(MongoDBDestDriver *self)
{
self->db = g_strdup("syslog");
self->safe_mode = TRUE;
}
void
afmongodb_dd_free_legacy(MongoDBDestDriver *self)
{
g_free(self->db);
g_free(self->user);
g_free(self->password);
g_free(self->address);
string_list_free(self->servers);
host_list_free(self->recovery_cache);
self->recovery_cache = NULL;
}
|