From b3266fde4664f391ad1621d121e7b2c6024d5d9e Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Fri, 19 Jul 2013 17:11:44 -0700 Subject: [PATCH] cname::dns: Added first version of a plugin to query DNS for cnames. --- configure.ac | 2 + src/Makefile.am | 10 +++++ src/plugins/cname/dns.c | 97 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 src/plugins/cname/dns.c diff --git a/configure.ac b/configure.ac index 6847c08..9377501 100644 --- a/configure.ac +++ b/configure.ac @@ -246,6 +246,8 @@ AC_SDB_PLUGIN([puppet-storeconfigs], [$puppet_storeconfigs_default], [backend accessing the Puppet stored configuration database]) AC_SDB_PLUGIN([syslog], [yes], [plugin logging to syslog]) +AC_SDB_PLUGIN([cname-dns], [yes], + [canonicalize hostnames by querying DNS]) AM_CONDITIONAL([BUILD_DOCUMENTATION], test "x$build_documentation" = "xyes") diff --git a/src/Makefile.am b/src/Makefile.am index 09b81b7..3ab6bc6 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -54,11 +54,13 @@ dist_sdbconf_DATA = daemon/sysdbd.conf.sample pkgbackendlibdir = $(pkglibdir)/backend pkgbackendcollectdlibdir = $(pkgbackendlibdir)/collectd pkgbackendpuppetlibdir = $(pkgbackendlibdir)/puppet +pkgcnamelibdir = $(pkglibdir)/cname pkglib_LTLIBRARIES = pkgbackendlib_LTLIBRARIES = pkgbackendcollectdlib_LTLIBRARIES = pkgbackendpuppetlib_LTLIBRARIES = +pkgcnamelib_LTLIBRARIES = if BUILD_PLUGIN_COLLECTD pkgbackendcollectdlib_LTLIBRARIES += backend/collectd/unixsock.la @@ -92,6 +94,14 @@ libsysdb_la_LIBADD += -dlopen plugins/syslog.la libsysdb_la_DEPENDENCIES += plugins/syslog.la endif +if BUILD_PLUGIN_CNAMEDNS +pkgcnamelib_LTLIBRARIES += plugins/cname/dns.la +plugins_cname_dns_la_SOURCE = plugins/cname/dns.c +plugins_cname_dns_la_LDFLAGS = -module -avoid-version +libsysdb_la_LIBADD += -dlopen plugins/cname/dns.la +libsysdb_la_DEPENDENCIES += plugins/cname/dns.la +endif + include/sysdb.h: include/sysdb.h.in ../version source ../version; sed \ -e "s/@SDB_VERSION_MAJOR@/$$VERSION_MAJOR/g" \ diff --git a/src/plugins/cname/dns.c b/src/plugins/cname/dns.c new file mode 100644 index 0000000..d989e52 --- /dev/null +++ b/src/plugins/cname/dns.c @@ -0,0 +1,97 @@ +/* + * SysDB - src/plugins/cname/dns.c + * Copyright (C) 2013 Sebastian 'tokkee' Harl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "sysdb.h" +#include "core/plugin.h" +#include "core/error.h" + +#include +#include +#include + +#include +#include +#include + +/* make it a no-op if necessary */ +#ifndef AI_IDN +# define AI_IDN 0 +#endif + +SDB_PLUGIN_MAGIC; + +/* + * plugin API + */ + +static char * +sdb_dns_cname(const char *name, + sdb_object_t __attribute__((unused)) *user_data) +{ + struct addrinfo hints; + struct addrinfo *ret = NULL; + char *cname; + int err; + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_UNSPEC; + hints.ai_flags = AI_CANONNAME | AI_IDN; + + err = getaddrinfo(name, /* service = */ NULL, &hints, &ret); + if (err) { + /* XXX: what to do about EAI_AGAIN? */ + sdb_plugin_logf(SDB_LOG_ERR, "cname::dns: Failed to resolve '%s': %s", + name, gai_strerror(err)); + return NULL; + } + + if ((! ret) || (! ret->ai_canonname)) + return NULL; + + cname = strdup(ret->ai_canonname); + freeaddrinfo(ret); + return cname; +} /* sdb_dns_cname */ + +int +sdb_module_init(sdb_plugin_info_t *info) +{ + sdb_plugin_set_info(info, SDB_PLUGIN_INFO_NAME, "cname::dns"); + sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC, + "canonicalize hostnames by querying DNS"); + sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT, + "Copyright (C) 2013 Sebastian 'tokkee' Harl "); + sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD"); + sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION); + sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION); + + sdb_plugin_register_cname("cname::dns", sdb_dns_cname, NULL); + return 0; +} /* sdb_module_init */ + +/* vim: set tw=78 sw=4 ts=4 noexpandtab : */ + -- 2.30.2