[PATCH (3.4) 0/2]: tfuuid: New template function to generate UUIDs.
Following this mail, two patches will be sent: one that moves the uuid implementation from patternize to lib/uuid.c, and extends it to be able to use libuuid if OpenSSL was not found. And another, that implements a very simple template function, that can be used to generate UUIDs. It uses the wrapper introduced in the first patch. I'm not sure tfuuid warrants an entirely separate module, though: it's so trivial it could easily fit into basicfuncs, for example. If that's more desirable, I'll send an updated version that puts it there, instead of a separate module. -- |8]
The new function, available in "uuid.h" and libsyslog-ng-crypto, can be used to generate UUID strings. It will either use a custom implementation, when OpenSSL is available, or libuuid, if it's present but OpenSSL is not. If neither is present, it falls back to a dumb counter. Signed-off-by: Gergely Nagy <algernon@balabit.hu> --- configure.in | 6 ++- lib/Makefile.am | 7 ++- lib/uuid.c | 91 +++++++++++++++++++++++++++++++++++++++++ lib/uuid.h | 32 ++++++++++++++ modules/dbparser/Makefile.am | 6 +- modules/dbparser/patternize.c | 51 +---------------------- 6 files changed, 137 insertions(+), 56 deletions(-) create mode 100644 lib/uuid.c create mode 100644 lib/uuid.h diff --git a/configure.in b/configure.in index 0a3d730..0b04ae1 100644 --- a/configure.in +++ b/configure.in @@ -921,6 +921,8 @@ if test "x$enable_systemd" = "xyes"; then fi fi +PKG_CHECK_MODULES(UUID, uuid, enable_libuuid="yes", enable_libuuid="no") + dnl *************************************************************************** dnl default modules to be loaded dnl *************************************************************************** @@ -1046,6 +1048,7 @@ AC_DEFINE_UNQUOTED(MODULE_PATH, "$module_path", [module search path]) AC_DEFINE_UNQUOTED(WITH_COMPILE_DATE, $wcmp_date, [Include the compile date in the binary]) AC_DEFINE_UNQUOTED(ENABLE_DEBUG, `enable_value $enable_debug`, [Enable debugging]) AC_DEFINE_UNQUOTED(ENABLE_SSL, `enable_value $enable_ssl`, [Enable SSL support]) +AC_DEFINE_UNQUOTED(ENABLE_LIBUUID, `enable_value $enable_libuuid`, [Enable libuuid support]) AC_DEFINE_UNQUOTED(ENABLE_GPROF, `enable_value $enable_gprof`, [Enable gcc profiling]) AC_DEFINE_UNQUOTED(ENABLE_MEMTRACE, `enable_value $enable_memtrace`, [Enable memtrace]) AC_DEFINE_UNQUOTED(ENABLE_SPOOF_SOURCE, `enable_value $enable_spoof_source`, [Enable spoof source support]) @@ -1103,6 +1106,8 @@ AC_SUBST(JSON_CFLAGS) AC_SUBST(IVYKIS_SUBDIRS) AC_SUBST(RESOLV_LIBS) AC_SUBST(CFLAGS_NOWARN_POINTER_SIGN) +AC_SUBST(UUID_CFLAGS) +AC_SUBST(UUID_LIBS) AC_SUBST(CURRDATE) AC_SUBST(RELEASE_TAG) @@ -1184,4 +1189,3 @@ echo " PACCT module (EXPERIMENTAL) : ${enable_pacct:=no}" echo " MongoDB destination (module): ${enable_mongodb:=no}" echo " JSON support (module) : parser=${enable_json_parse:=no}, formatter=${enable_json_format:=no} (using ${with_json})" - diff --git a/lib/Makefile.am b/lib/Makefile.am index 64b6925..e68c9ae 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -12,7 +12,8 @@ libsyslog_ng_la_LIBADD = @CORE_DEPS_LIBS@ libsyslog_ng_la_LDFLAGS = -no-undefined -release @VERSION@ module_LTLIBRARIES = libsyslog-ng-crypto.la -libsyslog_ng_crypto_la_LIBADD = @CORE_DEPS_LIBS@ @OPENSSL_LIBS@ libsyslog-ng.la +libsyslog_ng_crypto_la_CFLAGS = @UUID_CFLAGS@ +libsyslog_ng_crypto_la_LIBADD = @CORE_DEPS_LIBS@ @OPENSSL_LIBS@ @UUID_LIBS@ libsyslog-ng.la libsyslog_ng_crypto_la_LDFLAGS = -no-undefined -avoid-version # this is intentionally formatted so conflicts are less likely to arise. one name in every line. @@ -77,6 +78,7 @@ pkginclude_HEADERS = \ tlscontext.h \ tlstransport.h \ utils.h \ + uuid.h \ value-pairs.h \ vptransform.h @@ -84,7 +86,8 @@ pkginclude_HEADERS = \ libsyslog_ng_crypto_la_SOURCES = \ crypto.c \ tlscontext.c \ - tlstransport.c + tlstransport.c \ + uuid.c # this is intentionally formatted so conflicts are less likely to arise. one name in every line. libsyslog_ng_la_SOURCES = \ diff --git a/lib/uuid.c b/lib/uuid.c new file mode 100644 index 0000000..2161581 --- /dev/null +++ b/lib/uuid.c @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2010-2012 BalaBit IT Ltd, Budapest, Hungary + * Copyright (c) 2010-2012 Balázs Scheidler + * Copyright (c) 2012 Gergely Nagy <algernon@balabit.hu> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; 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 "uuid.h" + +#if ENABLE_SSL +#include <openssl/rand.h> +#include <arpa/inet.h> + +void +uuid_gen_random(gchar *buf, gsize buflen) +{ + union + { + struct + { + guint32 time_low; + guint16 time_mid; + guint16 time_hi_and_version; + guint8 clk_seq_hi_res; + guint8 clk_seq_low; + guint8 node[6]; + guint16 node_low; + guint32 node_hi; + }; + guchar __rnd[16]; + } uuid; + + RAND_bytes(uuid.__rnd, sizeof(uuid)); + + uuid.clk_seq_hi_res = (uuid.clk_seq_hi_res & ~0xC0) | 0x80; + uuid.time_hi_and_version = htons((uuid.time_hi_and_version & ~0xF000) | 0x4000); + + g_snprintf(buf, buflen, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", + uuid.time_low, uuid.time_mid, uuid.time_hi_and_version, + uuid.clk_seq_hi_res, uuid.clk_seq_low, + uuid.node[0], uuid.node[1], uuid.node[2], + uuid.node[3], uuid.node[4], uuid.node[5]); + +} +#else + +#if ENABLE_LIBUUID +#include <uuid/uuid.h> + +void +uuid_gen_random(gchar *buf, gsize buflen) +{ + uuid_t uuid; + char out[37]; + + uuid_generate(uuid); + uuid_unparse(uuid, out); + + g_strlcpy(buf, out, buflen); +} + +#else /* Neither openssl, nor libuuid */ + +#warning "Neither openssl, nor libuuid was found on your system, UUID generation will be disabled" + +void +uuid_gen_random(gchar *buf, gsize buflen) +{ + static int counter = 1; + + g_snprintf(buf, buflen, "unable-to-generate-uuid-without-random-source-%d", counter++); +} +#endif +#endif diff --git a/lib/uuid.h b/lib/uuid.h new file mode 100644 index 0000000..a66da80 --- /dev/null +++ b/lib/uuid.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2010-2012 BalaBit IT Ltd, Budapest, Hungary + * Copyright (c) 2010-2012 Balázs Scheidler + * Copyright (c) 2012 Gergely Nagy <algernon@balabit.hu> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; 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. + */ + +#ifndef UUID_H_INCLUDED +#define UUID_H_INCLUDED 1 + +#include "syslog-ng.h" + +void uuid_gen_random(gchar *buf, gsize buflen); + +#endif diff --git a/modules/dbparser/Makefile.am b/modules/dbparser/Makefile.am index dbff836..011084b 100644 --- a/modules/dbparser/Makefile.am +++ b/modules/dbparser/Makefile.am @@ -25,13 +25,13 @@ libdbparser_la_SOURCES = \ dbparser-plugin.c $(libsyslog_ng_patterndb_a_SOURCES) libdbparser_la_CPPFLAGS = $(AM_CPPFLAGS) -libdbparser_la_LIBADD = $(MODULE_DEPS_LIBS) @OPENSSL_LIBS@ +libdbparser_la_LIBADD = $(MODULE_DEPS_LIBS) ../../lib/libsyslog-ng-crypto.la libdbparser_la_LDFLAGS = $(MODULE_LDFLAGS) bin_PROGRAMS = pdbtool pdbtool_SOURCES = pdbtool.c -pdbtool_CPPFLAGS = $(AM_CPPFLAGS) @OPENSSL_CFLAGS@ -pdbtool_LDADD = libsyslog-ng-patterndb.a ../../lib/libsyslog-ng.la @TOOL_DEPS_LIBS@ @OPENSSL_LIBS@ +pdbtool_CPPFLAGS = $(AM_CPPFLAGS) +pdbtool_LDADD = libsyslog-ng-patterndb.a ../../lib/libsyslog-ng.la ../../lib/libsyslog-ng-crypto.la @TOOL_DEPS_LIBS@ BUILT_SOURCES = dbparser-grammar.y dbparser-grammar.c dbparser-grammar.h EXTRA_DIST = $(BUILT_SOURCES) radix-find.c dbparser-grammar.ym diff --git a/modules/dbparser/patternize.c b/modules/dbparser/patternize.c index 89b2bcd..86a1d0f 100644 --- a/modules/dbparser/patternize.c +++ b/modules/dbparser/patternize.c @@ -24,6 +24,7 @@ #include "logmsg.h" #include "messages.h" #include "tags.h" +#include "uuid.h" #include <stdlib.h> #include <string.h> @@ -43,56 +44,6 @@ static LogTagId cluster_tag_id; - -#if ENABLE_SSL - -#include <openssl/rand.h> - -static void -uuid_gen_random(gchar *buf, gsize buflen) -{ - union - { - struct - { - guint32 time_low; - guint16 time_mid; - guint16 time_hi_and_version; - guint8 clk_seq_hi_res; - guint8 clk_seq_low; - guint8 node[6]; - guint16 node_low; - guint32 node_hi; - }; - guchar __rnd[16]; - } uuid; - - RAND_bytes(uuid.__rnd, sizeof(uuid)); - - uuid.clk_seq_hi_res = (uuid.clk_seq_hi_res & ~0xC0) | 0x80; - uuid.time_hi_and_version = htons((uuid.time_hi_and_version & ~0xF000) | 0x4000); - - g_snprintf(buf, buflen, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", - uuid.time_low, uuid.time_mid, uuid.time_hi_and_version, - uuid.clk_seq_hi_res, uuid.clk_seq_low, - uuid.node[0], uuid.node[1], uuid.node[2], - uuid.node[3], uuid.node[4], uuid.node[5]); - -} - -#else - -#warning "openssl seems to be missing on your system, UUID generation will be disabled in pdbtool patternize" - -static void -uuid_gen_random(gchar *buf, gsize buflen) -{ - static int counter = 1; - - g_snprintf(buf, buflen, "unable-to-generate-uuid-without-random-source-%d", counter++); -} -#endif - #if 0 static void _ptz_debug_print_word(gpointer key, gpointer value, gpointer dummy) { -- 1.7.8.3
This patch adds a new template function: $(uuid), which can be used to generate Universally Unique IDentifiers. The use case for this is to add an UUID to each message as soon as they're received, so that they can be identified later, accross destinations. This allows me to pre-generate an ID for messages going into mongodb, and send those messages to a different destination too, with the same ID included. Use it like this: rewrite r_add_uuid { set("$(uuid)" value("uuid")); }; log { source(s_network); rewrite(r_add_uuid); destination(d_mongo); destination(d_program); }; Signed-off-by: Gergely Nagy <algernon@balabit.hu> --- modules/Makefile.am | 2 +- modules/tfuuid/Makefile.am | 9 +++++++ modules/tfuuid/tfuuid.c | 57 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 1 deletions(-) create mode 100644 modules/tfuuid/Makefile.am create mode 100644 modules/tfuuid/tfuuid.c diff --git a/modules/Makefile.am b/modules/Makefile.am index 0d0594b..72c005f 100644 --- a/modules/Makefile.am +++ b/modules/Makefile.am @@ -1 +1 @@ -SUBDIRS = afsocket afsql afstreams affile afprog afuser afmongodb csvparser confgen syslogformat pacctformat basicfuncs dbparser tfjson jsonparser dummy +SUBDIRS = afsocket afsql afstreams affile afprog afuser afmongodb csvparser confgen syslogformat pacctformat basicfuncs dbparser tfjson tfuuid jsonparser dummy diff --git a/modules/tfuuid/Makefile.am b/modules/tfuuid/Makefile.am new file mode 100644 index 0000000..47cbeb4 --- /dev/null +++ b/modules/tfuuid/Makefile.am @@ -0,0 +1,9 @@ +moduledir = @moduledir@ +export top_srcdir + +AM_CPPFLAGS = -I$(top_srcdir)/lib -I../../lib +module_LTLIBRARIES = libtfuuid.la + +libtfuuid_la_SOURCES = tfuuid.c +libtfuuid_la_LIBADD = $(MODULE_DEPS_LIBS) ../../lib/libsyslog-ng-crypto.la +libtfuuid_la_LDFLAGS = $(MODULE_LDFLAGS) diff --git a/modules/tfuuid/tfuuid.c b/modules/tfuuid/tfuuid.c new file mode 100644 index 0000000..64643f5 --- /dev/null +++ b/modules/tfuuid/tfuuid.c @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2012 BalaBit IT Ltd, Budapest, Hungary + * Copyright (c) 2012 Gergely Nagy <algernon@balabit.hu> + * + * 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 library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "plugin.h" +#include "templates.h" +#include "cfg.h" +#include "uuid.h" + +#include "config.h" + +static void +tf_uuid(LogMessage *msg, gint argc, GString *argv[], GString *result) +{ + char uuid_str[37]; + + uuid_gen_random(uuid_str, sizeof(uuid_str)); + g_string_append (result, uuid_str); +} + +TEMPLATE_FUNCTION_SIMPLE(tf_uuid); + +static Plugin tfuuid_plugins[] = + { + TEMPLATE_FUNCTION_PLUGIN(tf_uuid, "uuid"), + }; + +gboolean +tfuuid_module_init(GlobalConfig *cfg, CfgArgs *args) +{ + plugin_register(cfg, tfuuid_plugins, G_N_ELEMENTS(tfuuid_plugins)); + return TRUE; +} + +const ModuleInfo module_info = +{ + .canonical_name = "tfuuid", + .version = VERSION, + .description = "The tfuuid module provides a template function to generate UUIDs.", + .core_revision = SOURCE_REVISION, + .plugins = tfuuid_plugins, + .plugins_len = G_N_ELEMENTS(tfuuid_plugins), +}; -- 1.7.8.3
participants (1)
-
Gergely Nagy