[syslog-ng] [PATCH (3.4) 1/2] uuid: Introduce a new function to generate uuids.

Gergely Nagy algernon at balabit.hu
Fri Feb 3 13:46:01 CET 2012


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 at 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 at 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 at 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




More information about the syslog-ng mailing list