summaryrefslogtreecommitdiff
path: root/plugin
diff options
context:
space:
mode:
authorStef Walter <stef@memberwebs.com>2008-06-09 17:22:43 +0000
committerStef Walter <stef@memberwebs.com>2008-06-09 17:22:43 +0000
commitb9ecd6e5e5b87fe1c4dab960e92246772002dd6a (patch)
treeeb05eb48888e391678fa706ec5f62b012df2456e /plugin
parent3add3c0e19b659427a2624ec85daf67019b8ed7f (diff)
Merge the two dns plugins
Diffstat (limited to 'plugin')
-rw-r--r--plugin/Makefile.am6
-rw-r--r--plugin/autoserial.c573
-rw-r--r--plugin/dnsnotify.c (renamed from plugin/slapi-dnsnotify.c)242
-rw-r--r--plugin/plugin.c352
-rw-r--r--plugin/plugin.h72
5 files changed, 1063 insertions, 182 deletions
diff --git a/plugin/Makefile.am b/plugin/Makefile.am
index 9b8f4de..29266b9 100644
--- a/plugin/Makefile.am
+++ b/plugin/Makefile.am
@@ -7,7 +7,11 @@ INCLUDES = \
lib_LTLIBRARIES = slapi-dnsnotify.la
slapi_dnsnotify_la_SOURCES = \
- slapi-dnsnotify.c
+ plugin.c plugin.h \
+ autoserial.c \
+ dnsnotify.c \
+ ../common/soa.c \
+ ../common/strset.c
slapi_dnsnotify_la_LDFLAGS = -module -avoid-version \
-export-symbols-regex 'plugin_init'
diff --git a/plugin/autoserial.c b/plugin/autoserial.c
new file mode 100644
index 0000000..d10f8a5
--- /dev/null
+++ b/plugin/autoserial.c
@@ -0,0 +1,573 @@
+
+#include "config.h"
+
+#include "plugin.h"
+
+#include "common/soa.h"
+#include "common/strset.h"
+
+#include <stdarg.h>
+#include <string.h>
+#include <stdio.h>
+#include <assert.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <unistd.h>
+
+/*----------------------------------------------------------------------------------
+ * DECLARATIONS
+ */
+
+/* TODO: Fill these in from settings */
+static const char *dnsserial_soa_attribute = "sOARecord";
+static const char *dnsserial_soa_base = "";
+
+static Slapi_Mutex *dnsserial_mutex = NULL;
+static strset *dnsserial_soa_cache = NULL;
+
+/* ---------------------------------------------------------------------------------
+ * MEMORY ALLOCATION
+ */
+
+void*
+strset_malloc (size_t length)
+{
+ return slapi_ch_malloc (length);
+}
+
+void
+strset_free (void *ptr)
+{
+ return slapi_ch_free (&ptr);
+}
+
+/* ---------------------------------------------------------------------------------
+ * LDAP OPERATIONS
+ */
+
+static int
+search_for_dns (const char *base, const char *filter, char ***results)
+{
+ Slapi_Entry **entries, **e;
+ Slapi_PBlock *pb;
+ LDAPControl *ctrl;
+ char **dns, **d;
+ char *attr, *dn;
+ int rc, num, code;
+
+ assert (results);
+ assert (filter && filter[0]);
+ assert (base && base[0]);
+
+ ctrl = NULL; /* No controls */
+ attr = NULL; /* No attributes */
+
+ trace ("performing internal search");
+
+ /* Do the actual search */
+ pb = slapi_search_internal ((char*)base, LDAP_SCOPE_SUBTREE, (char*)filter, &ctrl, &attr, 1);
+ return_val_if_fail (pb, -1);
+
+ /* Was it successful? */
+ code = -1;
+ rc = slapi_pblock_get (pb, SLAPI_PLUGIN_INTOP_RESULT, &code);
+ return_val_if_fail (rc >= 0, -1);
+ if (code != LDAP_SUCCESS) {
+ log_plugin ("error performing search on %s for %s (code %d)", base, filter, code);
+ slapi_pblock_destroy (pb);
+ trace ("failure");
+ return -1;
+ }
+
+ /* Dig out all the entries */
+ entries = NULL;
+ rc = slapi_pblock_get (pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
+ return_val_if_fail (rc >= 0, -1);
+
+ /* How many entries? */
+ num = 0;
+ for (e = entries; entries && *e; ++e)
+ ++num;
+
+ /* Allocate memory and copy over all dns found */
+ d = dns = (char**)slapi_ch_calloc (num + 1, sizeof (char*));
+ for (e = entries; entries && *e; ++e) {
+ dn = slapi_entry_get_dn (*e);
+ return_val_if_fail (dn, -1);
+
+ dn = slapi_ch_strdup (dn);
+ dn = slapi_dn_normalize_case (dn);
+ *(d++) = dn;
+ }
+
+ slapi_pblock_destroy (pb);
+ *results = dns;
+
+ trace ("success");
+ return 0;
+}
+
+static int
+populate_soa_cache (void)
+{
+ char **dns, **d;
+ char *filter;
+ int rc, len, have;
+
+ assert (dnsserial_mutex);
+
+ have = 0;
+ slapi_lock_mutex (dnsserial_mutex);
+
+ /* Already populated? */
+ have = dnsserial_soa_cache ? 1 : 0;
+
+ slapi_unlock_mutex (dnsserial_mutex);
+
+ if (have)
+ return 1;
+
+ /* Build up the filter */
+ len = strlen (dnsserial_soa_attribute) +
+ strlen ("(%s=*)");
+ filter = slapi_ch_malloc (len);
+ strcpy (filter, "(");
+ strcat (filter, dnsserial_soa_attribute);
+ strcat (filter, "=*)");
+
+ trace (filter);
+
+ rc = search_for_dns (dnsserial_soa_base, filter, &dns);
+ slapi_ch_free_string (&filter);
+
+ if (rc < 0)
+ return rc;
+
+ /* Normalize all the DNs */
+ for (d = dns; *d; ++d)
+ slapi_dn_normalize_case (*d);
+
+ /* Now we're ready to put them in the cache */
+ assert (dnsserial_mutex);
+ slapi_lock_mutex (dnsserial_mutex);
+
+ /* Already populated? */
+ if (!dnsserial_soa_cache) {
+ dnsserial_soa_cache = strset_create ();
+ for (d = dns; *d; ++d)
+ strset_add (dnsserial_soa_cache, *d);
+ }
+
+ slapi_unlock_mutex (dnsserial_mutex);
+
+ slapi_ch_array_free (dns);
+ return 1;
+}
+
+static int
+present_in_soa_cache (const char *dn)
+{
+ int present;
+
+ assert (dn && dn[0]);
+ assert (dnsserial_mutex);
+
+ if (populate_soa_cache () < 0)
+ return 0;
+
+ slapi_lock_mutex (dnsserial_mutex);
+
+ present = strset_has (dnsserial_soa_cache, dn);
+
+ slapi_unlock_mutex (dnsserial_mutex);
+
+ return present;
+}
+
+static char*
+ancestor_in_soa_cache (const char *dn)
+{
+ Slapi_DN *sdn = NULL;
+ Slapi_DN *parent = NULL;
+ char *ancestor;
+
+ assert (dn && dn[0]);
+ assert (dnsserial_mutex);
+
+ if (populate_soa_cache () < 0)
+ return 0;
+
+ sdn = slapi_sdn_new_dn_byval (dn);
+ return_val_if_fail (sdn, 0);
+
+ ancestor = NULL;
+ slapi_lock_mutex (dnsserial_mutex);
+
+ for (;;) {
+
+ /* Get the parent of the dn */
+ assert (sdn);
+ assert (!parent);
+ parent = slapi_sdn_new ();
+ slapi_sdn_get_parent (sdn, parent);
+
+ /* Reached the root? */
+ if (slapi_sdn_isempty (parent))
+ break;
+
+ /* Reached the top of our SOA block? */
+ dn = slapi_sdn_get_dn (parent);
+ if (strcmp (dn, dnsserial_soa_base) == 0)
+ break;
+
+ /* Found this DN? */
+ if (strset_has (dnsserial_soa_cache, dn)) {
+ ancestor = slapi_ch_strdup (dn);
+ break;
+ }
+
+ slapi_sdn_free (&sdn);
+ sdn = parent;
+ parent = NULL;
+ }
+
+
+ slapi_unlock_mutex (dnsserial_mutex);
+
+ if (sdn)
+ slapi_sdn_free (&sdn);
+ if (parent)
+ slapi_sdn_free (&parent);
+
+ return ancestor;
+}
+
+static int
+add_to_soa_cache (const char *dn)
+{
+ assert (dn && dn[0]);
+ assert (dnsserial_mutex);
+
+ if (populate_soa_cache () < 0)
+ return -1;
+
+ slapi_lock_mutex (dnsserial_mutex);
+
+ strset_add (dnsserial_soa_cache, dn);
+
+ slapi_unlock_mutex (dnsserial_mutex);
+
+ return 0;
+}
+
+static int
+remove_from_soa_cache (const char *dn)
+{
+ assert (dn && dn[0]);
+
+ if (populate_soa_cache () < 0)
+ return -1;
+
+ slapi_lock_mutex (dnsserial_mutex);
+
+ strset_remove (dnsserial_soa_cache, dn);
+
+ slapi_unlock_mutex (dnsserial_mutex);
+
+ return 0;
+}
+
+static char*
+load_soa_attribute (const char *dn)
+{
+ Slapi_Entry **entries;
+ struct berval **values;
+ Slapi_PBlock *pb;
+ Slapi_Attr *attr;
+ LDAPControl *ctrl;
+ char *attrs[2];
+ char *soa;
+ int rc, code;
+
+ trace (dn);
+ assert (dn && dn[0]);
+ assert (dnsserial_soa_attribute);
+
+ ctrl = NULL; /* No controls */
+ attrs[0] = (char*)dnsserial_soa_attribute;
+ attrs[1] = NULL;
+
+ trace ("performing internal search");
+
+ /* Do the actual search */
+ pb = slapi_search_internal ((char*)dn, LDAP_SCOPE_BASE, "(objectClass=*)", &ctrl, attrs, 0);
+ return_val_if_fail (pb, NULL);
+
+ /* Was it successful? */
+ code = -1;
+ rc = slapi_pblock_get (pb, SLAPI_PLUGIN_INTOP_RESULT, &code);
+ return_val_if_fail (rc >= 0, NULL);
+ if (code != LDAP_SUCCESS) {
+ log_plugin ("error loading attribute %s from %s (code %d)", dnsserial_soa_attribute, dn, code);
+ slapi_pblock_destroy (pb);
+ trace ("failure");
+ return NULL;
+ }
+
+ /* Dig out all the entries */
+ entries = NULL;
+ slapi_pblock_get (pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
+ return_val_if_fail (entries, NULL);
+
+ soa = NULL;
+ if (entries[0]) {
+ if (slapi_entry_attr_find (entries[0], (char*)dnsserial_soa_attribute, &attr) >= 0 && attr &&
+ slapi_attr_get_values (attr, &values) >= 0 && values && values[0]) {
+
+ /* Convert the berval into a string */
+ soa = slapi_ch_malloc (values[0]->bv_len + 1);
+ if (values[0]->bv_len)
+ memcpy (soa, values[0]->bv_val, values[0]->bv_len);
+ soa[values[0]->bv_len] = 0;
+ }
+ }
+
+ slapi_pblock_destroy (pb);
+ trace ("success");
+
+ return soa;
+}
+
+static int
+change_soa_attribute (const char *dn, const char *soa_old, const char *soa_new)
+{
+ Slapi_PBlock *pb;
+ LDAPMod *mods[3];
+ char *values_add[2];
+ char *values_remove[2];
+ LDAPMod mod_add;
+ LDAPMod mod_remove;
+ int rc, code;
+
+ trace (dn);
+
+ assert (dn && dn[0]);
+ assert (soa_old);
+ assert (soa_new);
+
+ /* Setup the mods to change */
+ values_add[0] = (char*)soa_new;
+ values_add[1] = NULL;
+ mod_add.mod_op = LDAP_MOD_ADD;
+ mod_add.mod_type = (char*)dnsserial_soa_attribute;
+ mod_add.mod_values = values_add;
+
+ values_remove[0] = (char*)soa_old;
+ values_remove[1] = NULL;
+ mod_remove.mod_op = LDAP_MOD_DELETE;
+ mod_remove.mod_type = (char*)dnsserial_soa_attribute;
+ mod_remove.mod_values = values_remove;
+
+ mods[0] = &mod_remove;
+ mods[1] = &mod_add;
+ mods[2] = NULL;
+
+ trace ("performing modify internal");
+
+ pb = slapi_modify_internal ((char*)dn, mods, NULL, 1);
+ return_val_if_fail (pb, -1);
+
+ code = -1;
+ rc = slapi_pblock_get (pb, SLAPI_PLUGIN_INTOP_RESULT, &code);
+ return_val_if_fail (pb, -1);
+ slapi_pblock_destroy (pb);
+
+ if (code != LDAP_SUCCESS && code != LDAP_TYPE_OR_VALUE_EXISTS && code != LDAP_NO_SUCH_ATTRIBUTE) {
+ log_plugin ("couldn't replace %s on %s with '%s' (code %d)",
+ dnsserial_soa_attribute, dn, soa_new, code);
+ trace ("failure");
+ return -1;
+ }
+
+ trace ("success");
+ return 0;
+}
+
+static int
+increment_soa_dn (const char *dn)
+{
+ char *soa_new;
+ char *soa_old;
+ int len, ret = -1;
+
+ assert (dn && dn[0]);
+
+ soa_old = load_soa_attribute (dn);
+ if (!soa_old) {
+ log_plugin ("couldn't increment SOA record at %s: not found", dn);
+ return -1;
+ }
+
+ len = strlen (soa_old) + 32;
+ soa_new = slapi_ch_malloc (len);
+ if (soa_serial_increment (soa_old, soa_new, len) < 0) {
+ if (errno == EINVAL)
+ log_plugin ("invalid SOA record at %s: %s", dn, soa_old);
+ else
+ log_plugin ("couldn't increment SOA record at %s: %s", dn, strerror (errno));
+ ret = -1;
+ } else {
+ ret = change_soa_attribute (dn, soa_old, soa_new);
+ }
+
+ slapi_ch_free_string (&soa_new);
+ slapi_ch_free_string (&soa_old);
+
+ return ret;
+}
+
+void
+autoserial_post_delete (const char *dn)
+{
+ char *soa_dn;
+
+ return_if_fail (dn && dn[0]);
+
+ /* A DN that is an SOA? */
+ if (present_in_soa_cache (dn)) {
+ remove_from_soa_cache (dn);
+
+ /* A DN underneath an SOA? */
+ } else {
+ soa_dn = ancestor_in_soa_cache (dn);
+ if (soa_dn) {
+ increment_soa_dn (soa_dn);
+ slapi_ch_free_string (&soa_dn);
+ }
+ }
+}
+
+void
+autoserial_post_modify (const char *dn, LDAPMod **mods)
+{
+ char *soa, *soa_dn;
+ int present;
+
+ return_if_fail (dn && dn[0]);
+
+ /* Add or remove from cache as appropriate */
+ soa = load_soa_attribute(dn);
+ if (soa) {
+ add_to_soa_cache (dn);
+ slapi_ch_free_string (&soa);
+ present = 1;
+ } else {
+ remove_from_soa_cache (dn);
+ present = 0;
+ }
+
+ /* A DN underneath an SOA? */
+ if (!present) {
+ soa_dn = ancestor_in_soa_cache (dn);
+ if (soa_dn) {
+ increment_soa_dn (soa_dn);
+ slapi_ch_free_string (&soa_dn);
+ }
+ }
+}
+
+void
+autoserial_post_modrdn (const char *odn, const char *ndn)
+{
+ char *soa_dn;
+
+ return_if_fail (odn && odn[0]);
+ return_if_fail (ndn && ndn[0]);
+
+ /* A DN that is an SOA? */
+ if (present_in_soa_cache (odn)) {
+
+ remove_from_soa_cache (odn);
+ add_to_soa_cache (ndn);
+
+ /* A DN underneath an SOA? */
+ } else {
+ soa_dn = ancestor_in_soa_cache (ndn);
+ if (soa_dn) {
+ increment_soa_dn (soa_dn);
+ slapi_ch_free_string (&soa_dn);
+ }
+ }
+}
+
+void
+autoserial_post_add (const char *dn)
+{
+ char *soa, *soa_dn;
+
+ return_if_fail (dn && dn[0]);
+
+ /* A DN that is an SOA? */
+ soa = load_soa_attribute (dn);
+ if (soa) {
+ add_to_soa_cache (dn);
+ slapi_ch_free_string (&soa);
+
+ /* A DN underneath an SOA? */
+ } else {
+ soa_dn = ancestor_in_soa_cache (dn);
+ if (soa_dn) {
+ increment_soa_dn (soa_dn);
+ slapi_ch_free_string (&soa_dn);
+ }
+ }
+}
+
+int
+autoserial_config (const char *name, const char *value)
+{
+ return_val_if_fail (name, 0);
+
+ if (strcmp ("soa-attribute", name) == 0 && value) {
+ dnsserial_soa_attribute = value;
+ return 1;
+
+
+ } else if (strcmp ("base-dn", name) == 0 && value) {
+ dnsserial_soa_base = value;
+ return 1;
+ }
+
+ return 0;
+}
+
+void
+autoserial_destroy (void)
+{
+ assert (dnsserial_mutex);
+ slapi_lock_mutex (dnsserial_mutex);
+
+ if (dnsserial_soa_cache)
+ strset_destroy (dnsserial_soa_cache);
+ dnsserial_soa_cache = NULL;
+
+ slapi_unlock_mutex (dnsserial_mutex);
+
+ slapi_destroy_mutex (dnsserial_mutex);
+ dnsserial_mutex = NULL;
+}
+
+int
+autoserial_init (void)
+{
+ if (!dnsserial_soa_base || !dnsserial_soa_base[0]) {
+ log_plugin ("'base-dn' argument not specified");
+ return -1;
+ }
+
+ assert (!dnsserial_mutex);
+ dnsserial_mutex = slapi_new_mutex ();
+ return_val_if_fail (dnsserial_mutex, -1);
+
+ return 0;
+}
diff --git a/plugin/slapi-dnsnotify.c b/plugin/dnsnotify.c
index 329b913..132726f 100644
--- a/plugin/slapi-dnsnotify.c
+++ b/plugin/dnsnotify.c
@@ -1,6 +1,8 @@
#include "config.h"
+#include "plugin.h"
+
#include <sys/wait.h>
#include <stdarg.h>
@@ -13,91 +15,19 @@
#include <signal.h>
#include <ctype.h>
-#include <lber.h>
-#include <ldap.h>
-
-#ifdef HAVE_SLAPI_PLUGIN_H
-#include <slapi-plugin.h>
-#else
-#include "include/slapi-plugin.h"
-#endif
-
-#define PLUGIN_NAME "slapi-dnsnotify"
-
/*----------------------------------------------------------------------------------
* DECLARATIONS
*/
static const char *dnsnotify_soa_attribute = "sOARecord";
static const char *dnsnotify_ns_attribute = "nSRecord";
+static int dnsnotify_enable = 1;
+static int dnsnotify_delay = 5;
static Slapi_Mutex *dnsnotify_mutex = NULL;
static int dnsnotify_pipe = -1;
static pid_t dnsnotify_pid = -1;
-/* ---------------------------------------------------------------------
- * LOGGING/TRACING
- */
-
-static void
-log_msg_va (int level, const char* msg, va_list ap)
-{
- size_t len;
- char buf[1024];
-
- assert (level >= 0);
- assert (msg != NULL);
-
- vsnprintf (buf, sizeof (buf) - 2, msg, ap);
-
- len = strlen (buf);
- buf[len] = '\n';
- buf[len + 1] = '\0';
-
-#if _DEBUG
- fprintf (stderr, "%s", buf);
-#endif
- slapi_log_error (level, PLUGIN_NAME, buf);
-}
-
-static void
-log_msg (int level, const char* msg, ...)
-{
- va_list ap;
- va_start (ap, msg);
- log_msg_va (level, msg, ap);
- va_end (ap);
-}
-
-#if _DEBUG
-static void
-log_trace (const char *where, int line, const char *msg)
-{
- log_msg (SLAPI_LOG_TRACE, "*** %s *** %s:%d %s%s",
- PLUGIN_NAME, where, line, msg ? ": " : "", msg ? msg : "");
-}
-#endif
-
-static void
-log_plugin (const char* msg, ...)
-{
- va_list ap;
- va_start (ap, msg);
- log_msg_va (SLAPI_LOG_PLUGIN, msg, ap);
- va_end (ap);
-}
-
-#define return_val_if_fail(expr, ret) \
- do { if (expr) { } else { \
- log_plugin ("*** %s *** check failed: '%s' at %s:%d", PLUGIN_NAME, #expr, __PRETTY_FUNCTION__, __LINE__); \
- return (ret); \
- } } while (0)
-
-#if _DEBUG
-#define trace(x) log_trace (__PRETTY_FUNCTION__, __LINE__, (x))
-#else
-#define trace(x)
-#endif
/* ---------------------------------------------------------------------------------
* OPERATIONS
@@ -214,7 +144,8 @@ fork_notify_process (void)
{
int open_max, fd;
int commpipe[2];
- char *args[3];
+ char *args[5];
+ char delay[16];
pid_t pid;
if (pipe (commpipe) < 0) {
@@ -262,9 +193,12 @@ fork_notify_process (void)
for (fd = 3; fd < open_max; ++fd)
close (fd);
+ snprintf (delay, sizeof (delay), "-w%d", dnsnotify_delay);
+
args[0] = DNS_NOTIFY_PATH;
args[1] = "-s";
- args[2] = NULL;
+ args[2] = delay;
+ args[3] = NULL;
execv (args[0], args);
log_plugin ("couldn't launch '%s' process: %s", DNS_NOTIFY_PATH, strerror (errno));
@@ -371,31 +305,17 @@ notify_dns_slaves (char *soa, char **ns)
slapi_unlock_mutex (dnsnotify_mutex);
}
-static int
-slapi_dnsnotify_modify (Slapi_PBlock *pb)
+void
+dnsnotify_post_modify (const char *dn, LDAPMod **mods)
{
- LDAPMod **mods, **m, *mod;
- char *dn, *soa, **ns;
- int rc, code;
-
- return_val_if_fail (pb, -1);
-
- trace ("detect modify");
-
- /* Make sure it was successful, don't process errors */
- rc = slapi_pblock_get (pb, SLAPI_RESULT_CODE, &code);
- return_val_if_fail (rc >= 0, -1);
- if (code != LDAP_SUCCESS)
- return 0;
+ LDAPMod **m, *mod;
+ char *soa, **ns;
- /* Get out the DN and normalize it */
- rc = slapi_pblock_get (pb, SLAPI_MODIFY_TARGET, &dn);
- return_val_if_fail (rc >= 0 && dn, -1);
- dn = slapi_ch_strdup (dn);
- slapi_dn_normalize_case (dn);
+ return_if_fail (dn && dn[0]);
+ return_if_fail (mods);
- rc = slapi_pblock_get (pb, SLAPI_MODIFY_MODS, &mods);
- return_val_if_fail (rc >= 0 && mods, -1);
+ if (!dnsnotify_enable)
+ return;
for (m = mods; *m; ++m) {
mod = *m;
@@ -409,30 +329,17 @@ slapi_dnsnotify_modify (Slapi_PBlock *pb)
break;
}
}
-
- slapi_ch_free_string (&dn);
- return 0;
}
-static int
-slapi_dnsnotify_add (Slapi_PBlock *pb)
+void
+dnsnotify_post_add (const char *dn)
{
- char *dn, *soa, **ns;
- int rc, code, ret;
-
- return_val_if_fail (pb, -1);
+ char *soa, **ns;
- /* Make sure it was successful, don't process errors */
- rc = slapi_pblock_get (pb, SLAPI_RESULT_CODE, &code);
- return_val_if_fail (rc >= 0, -1);
- if (code != LDAP_SUCCESS)
- return 0;
+ return_if_fail (dn);
- /* Get out the DN and normalize it */
- rc = slapi_pblock_get (pb, SLAPI_ADD_TARGET, &dn);
- return_val_if_fail (rc >= 0 && dn, -1);
- dn = slapi_ch_strdup (dn);
- slapi_dn_normalize_case (dn);
+ if (!dnsnotify_enable)
+ return;
/* A DN that is an SOA? */
if (load_soa_ns_attributes (dn, &soa, &ns)) {
@@ -440,82 +347,41 @@ slapi_dnsnotify_add (Slapi_PBlock *pb)
slapi_ch_free_string (&soa);
slapi_ch_array_free (ns);
}
-
- slapi_ch_free_string (&dn);
- return ret;
}
-static Slapi_PluginDesc plugin_description = {
- PLUGIN_NAME, /* plug-in identifier */
- "stef@memberwebs.com", /* vendor name */
- VERSION, /* plug-in revision number */
- "Notify's DNS slaves when SOA change is made" /* plug-in description */
-};
-
-static int
-plugin_destroy (Slapi_PBlock *pb)
+int
+dnsnotify_config (const char *name, const char *value)
{
- if (dnsnotify_mutex)
- slapi_lock_mutex (dnsnotify_mutex);
+ return_val_if_fail (name, 0);
- kill_notify_process ();
-
- if (dnsnotify_mutex) {
- slapi_unlock_mutex (dnsnotify_mutex);
- slapi_destroy_mutex (dnsnotify_mutex);
- dnsnotify_mutex = NULL;
+ if (strcmp (name, "soa-attribute") == 0 && value) {
+ dnsnotify_soa_attribute = value;
+ return 1;
}
- return 0;
-}
-
-int
-plugin_init (Slapi_PBlock *pb)
-{
- char **argv = NULL;
- int argc = 0;
- int rc;
-
- return_val_if_fail (pb, -1);
-
- rc = slapi_pblock_get (pb, SLAPI_PLUGIN_ARGV, &argv);
- return_val_if_fail (rc >= 0, -1);
- slapi_pblock_get (pb, SLAPI_PLUGIN_ARGC, &argc);
- return_val_if_fail (rc >= 0, -1);
-
- if (argc >= 1)
- dnsnotify_soa_attribute = argv[0];
- if (argc >= 2)
- dnsnotify_ns_attribute = argv[1];
- if (argc > 2)
- log_plugin ("extra arguments present");
-
- if (!dnsnotify_soa_attribute || !dnsnotify_soa_attribute[0]) {
- log_plugin ("DNS SOA attribute specified for plugin is empty");
- return -1;
+ else if (strcmp (name, "ns-attribute") == 0 && value) {
+ dnsnotify_ns_attribute = value;
+ return 1;
}
- if (!dnsnotify_ns_attribute || !dnsnotify_ns_attribute[0]) {
- log_plugin ("DNS SOA attribute specified for plugin is empty");
- return -1;
+ else if (strcmp (name, "notify-delay") == 0 && value) {
+ dnsnotify_delay = atoi (value);
+ return 1;
}
- if (slapi_pblock_set (pb, SLAPI_PLUGIN_VERSION, SLAPI_PLUGIN_VERSION_03) != 0 ||
- slapi_pblock_set (pb, SLAPI_PLUGIN_DESCRIPTION, &plugin_description) != 0 ||
- slapi_pblock_set (pb, SLAPI_PLUGIN_DESTROY_FN, plugin_destroy)) {
- log_plugin ("error registring plugin");
- return -1;
+ else if (strcmp (name, "disable-notify") == 0 && !value) {
+ dnsnotify_enable = 0;
+ return 1;
}
- /*
- * TODO: Add support for changing the default attributes and search uri
- * Accept an argument for this.
- */
+ return 0;
+}
- /* Setup the entry add/mobify functions */
- if (slapi_pblock_set (pb, SLAPI_PLUGIN_POST_ADD_FN, (void*)slapi_dnsnotify_add) != 0 ||
- slapi_pblock_set (pb, SLAPI_PLUGIN_POST_MODIFY_FN, (void*)slapi_dnsnotify_modify)) {
- log_plugin ("error registering plugin hooks");
+int
+dnsnotify_init (void)
+{
+ if (!dnsnotify_ns_attribute || !dnsnotify_ns_attribute[0]) {
+ log_plugin ("SOA or NS attribute specified for plugin is empty");
return -1;
}
@@ -523,7 +389,21 @@ plugin_init (Slapi_PBlock *pb)
dnsnotify_mutex = slapi_new_mutex ();
return_val_if_fail (dnsnotify_mutex, -1);
- log_plugin ("%s initialized", PLUGIN_NAME);
return 0;
}
+
+void
+dnsnotify_destroy (void)
+{
+ if (dnsnotify_mutex)
+ slapi_lock_mutex (dnsnotify_mutex);
+
+ kill_notify_process ();
+
+ if (dnsnotify_mutex) {
+ slapi_unlock_mutex (dnsnotify_mutex);
+ slapi_destroy_mutex (dnsnotify_mutex);
+ dnsnotify_mutex = NULL;
+ }
+}
diff --git a/plugin/plugin.c b/plugin/plugin.c
new file mode 100644
index 0000000..c38eb67
--- /dev/null
+++ b/plugin/plugin.c
@@ -0,0 +1,352 @@
+
+#include "config.h"
+
+#include "plugin.h"
+
+#include <sys/wait.h>
+
+#include <stdarg.h>
+#include <string.h>
+#include <stdio.h>
+#include <assert.h>
+#include <stdlib.h>
+#include <ctype.h>
+
+#define WHITESPACE " \t\r\n\v"
+
+static char **plugin_arguments = NULL;
+
+/* ---------------------------------------------------------------------
+ * LOGGING/TRACING
+ */
+
+static void
+log_msg_va (int level, const char* msg, va_list ap)
+{
+ size_t len;
+ char buf[1024];
+
+ assert (level >= 0);
+ assert (msg != NULL);
+
+ vsnprintf (buf, sizeof (buf) - 2, msg, ap);
+
+ len = strlen (buf);
+ buf[len] = '\n';
+ buf[len + 1] = '\0';
+
+#if _DEBUG
+ fprintf (stderr, "%s", buf);
+#endif
+ slapi_log_error (level, PLUGIN_NAME, buf);
+}
+
+void
+log_msg (int level, const char* msg, ...)
+{
+ va_list ap;
+ va_start (ap, msg);
+ log_msg_va (level, msg, ap);
+ va_end (ap);
+}
+
+#if _DEBUG
+void
+log_trace (const char *where, int line, const char *msg)
+{
+ log_msg (SLAPI_LOG_TRACE, "*** %s *** %s:%d %s%s",
+ PLUGIN_NAME, where, line, msg ? ": " : "", msg ? msg : "");
+}
+#endif
+
+void
+log_plugin (const char* msg, ...)
+{
+ va_list ap;
+ va_start (ap, msg);
+ log_msg_va (SLAPI_LOG_PLUGIN, msg, ap);
+ va_end (ap);
+}
+
+/* --------------------------------------------------------------------------------
+ * HELPERS
+ */
+
+const char*
+ltrim (const char *data)
+{
+ while (*data && isspace (*data))
+ ++data;
+ return data;
+}
+
+void
+rtrim (char *data)
+{
+ char *t = data + strlen (data);
+ while (t > data && isspace (*(t - 1))) {
+ t--;
+ *t = 0;
+ }
+}
+
+char*
+trim (char *data)
+{
+ data = (char*)ltrim (data);
+ rtrim (data);
+ return data;
+}
+
+void
+lowercase (char *data)
+{
+ while (*data) {
+ *data = tolower (*data);
+ ++data;
+ }
+}
+
+/* ---------------------------------------------------------------------------------
+ * OPERATIONS
+ */
+
+static char*
+dn_build_normalize (const char *dn, const char *rdn)
+{
+ char *result;
+ Slapi_DN *sdn;
+ Slapi_DN *parent;
+ Slapi_RDN *srdn;
+
+ /* Get the parent of the dn */
+ sdn = slapi_sdn_new_dn_byval (dn);
+ return_val_if_fail (sdn, NULL);
+ parent = slapi_sdn_new ();
+ slapi_sdn_get_parent (sdn, parent);
+
+ /* Add the rdn to the parent */
+ srdn = slapi_rdn_new_dn (rdn);
+ return_val_if_fail (srdn, NULL);
+ slapi_sdn_add_rdn (parent, srdn);
+
+ /* Get the result out */
+ result = (char*)slapi_sdn_get_dn (parent);
+ return_val_if_fail (result, NULL);
+
+ /* Normalize the result */
+ result = slapi_ch_strdup (result);
+ slapi_dn_normalize_case (result);
+
+ slapi_sdn_free (&sdn);
+ slapi_rdn_free (&srdn);
+ slapi_sdn_free (&parent);
+
+ trace (result);
+ return result;
+}
+
+static int
+post_delete (Slapi_PBlock *pb)
+{
+ int rc, code;
+ char *dn;
+
+ return_val_if_fail (pb, -1);
+
+ /* Make sure it was successful, don't process errors */
+ rc = slapi_pblock_get (pb, SLAPI_RESULT_CODE, &code);
+ return_val_if_fail (rc >= 0, -1);
+ if (code != LDAP_SUCCESS)
+ return 0;
+
+ /* Get out the DN and normalize it */
+ rc = slapi_pblock_get (pb, SLAPI_DELETE_TARGET, &dn);
+ return_val_if_fail (rc >= 0 && dn, -1);
+ dn = slapi_ch_strdup (dn);
+ slapi_dn_normalize_case (dn);
+
+ /* Send off to components */
+ autoserial_post_delete (dn);
+
+ slapi_ch_free_string (&dn);
+ return 0;
+}
+
+static int
+post_modrdn (Slapi_PBlock *pb)
+{
+ char *rdn, *odn, *ndn;
+ int rc, code;
+
+ return_val_if_fail (pb, -1);
+
+ /* Make sure it was successful, don't process errors */
+ rc = slapi_pblock_get (pb, SLAPI_RESULT_CODE, &code);
+ return_val_if_fail (rc >= 0, -1);
+ if (code != LDAP_SUCCESS)
+ return 0;
+
+ /* Get out the DN and normalize it */
+ rc = slapi_pblock_get (pb, SLAPI_MODRDN_TARGET, &odn);
+ return_val_if_fail (rc >= 0 && odn, -1);
+ odn = slapi_ch_strdup (odn);
+ slapi_dn_normalize_case (odn);
+
+ /* Get out the changed DN and calculate it */
+ rc = slapi_pblock_get(pb, SLAPI_MODRDN_NEWRDN, &rdn);
+ return_val_if_fail (rc >= 0 && rdn, -1);
+ ndn = dn_build_normalize (odn, rdn);
+ return_val_if_fail (ndn, -1);
+
+ /* Send off to components */
+ autoserial_post_modrdn (odn, ndn);
+
+ slapi_ch_free_string (&odn);
+ slapi_ch_free_string (&ndn);
+ return 0;
+}
+
+static int
+post_modify (Slapi_PBlock *pb)
+{
+ LDAPMod **mods;
+ char *dn;
+ int rc, code;
+
+ return_val_if_fail (pb, -1);
+
+ /* Make sure it was successful, don't process errors */
+ rc = slapi_pblock_get (pb, SLAPI_RESULT_CODE, &code);
+ return_val_if_fail (rc >= 0, -1);
+ if (code != LDAP_SUCCESS)
+ return 0;
+
+ /* Get out the DN and normalize it */
+ rc = slapi_pblock_get (pb, SLAPI_MODIFY_TARGET, &dn);
+ return_val_if_fail (rc >= 0 && dn, -1);
+ dn = slapi_ch_strdup (dn);
+ slapi_dn_normalize_case (dn);
+
+ rc = slapi_pblock_get (pb, SLAPI_MODIFY_MODS, &mods);
+ return_val_if_fail (rc >= 0 && mods, -1);
+
+ /* Send off to components */
+ autoserial_post_modify (dn, mods);
+ dnsnotify_post_modify (dn, mods);
+
+ slapi_ch_free_string (&dn);
+ return 0;
+}
+
+static int
+post_add (Slapi_PBlock *pb)
+{
+ char *dn;
+ int rc, code;
+
+ return_val_if_fail (pb, -1);
+
+ /* Make sure it was successful, don't process errors */
+ rc = slapi_pblock_get (pb, SLAPI_RESULT_CODE, &code);
+ return_val_if_fail (rc >= 0, -1);
+ if (code != LDAP_SUCCESS)
+ return 0;
+
+ /* Get out the DN and normalize it */
+ rc = slapi_pblock_get (pb, SLAPI_ADD_TARGET, &dn);
+ return_val_if_fail (rc >= 0 && dn, -1);
+ dn = slapi_ch_strdup (dn);
+ slapi_dn_normalize_case (dn);
+
+ /* Send off to components */
+ autoserial_post_add (dn);
+ dnsnotify_post_add (dn);
+
+ slapi_ch_free_string (&dn);
+ return 0;
+}
+
+static Slapi_PluginDesc plugin_description = {
+ PLUGIN_NAME, /* plug-in identifier */
+ "stef@memberwebs.com", /* vendor name */
+ VERSION, /* plug-in revision number */
+ "Notify's DNS slaves when SOA change is made" /* plug-in description */
+};
+
+static int
+plugin_destroy (Slapi_PBlock *pb)
+{
+ autoserial_destroy ();
+ dnsnotify_destroy ();
+
+ slapi_ch_array_free (plugin_arguments);
+ plugin_arguments = NULL;
+
+ return 0;
+}
+
+int
+plugin_init (Slapi_PBlock *pb)
+{
+ char **argv = NULL, *arg, *value;
+ int argc = 0;
+ int rc, i;
+
+ return_val_if_fail (pb, -1);
+
+ rc = slapi_pblock_get (pb, SLAPI_PLUGIN_ARGV, &argv);
+ return_val_if_fail (rc >= 0, -1);
+ slapi_pblock_get (pb, SLAPI_PLUGIN_ARGC, &argc);
+ return_val_if_fail (rc >= 0, -1);
+
+ /*
+ * Copy all the arguments, until we get destroyed, and
+ * send the arguments to the components to configure
+ * themselves.
+ */
+ plugin_arguments = (char**)slapi_ch_calloc (argc + 1, sizeof (char*));
+ for (i = 0; i < argc; ++i) {
+ plugin_arguments[i] = slapi_ch_strdup (argv[i]);
+ arg = trim (plugin_arguments[i]);
+
+ value = strchr (arg, '=');
+ if (value) {
+ *value = 0;
+ value = trim (value + 1);
+ }
+
+ lowercase (arg);
+
+ rc = autoserial_config (arg, value);
+ rc |= dnsnotify_config (arg, value);
+
+ if (!rc)
+ log_plugin ("unrecognized plugin argument: %s", argv[i]);
+ }
+
+ /* Next we initialize all components */
+ if (autoserial_init () < 0 ||
+ dnsnotify_init() < 0)
+ return -1;
+
+ if (slapi_pblock_set (pb, SLAPI_PLUGIN_VERSION, SLAPI_PLUGIN_VERSION_03) != 0 ||
+ slapi_pblock_set (pb, SLAPI_PLUGIN_DESCRIPTION, &plugin_description) != 0 ||
+ slapi_pblock_set (pb, SLAPI_PLUGIN_DESTROY_FN, plugin_destroy)) {
+ log_plugin ("error registring plugin");
+ return -1;
+ }
+
+ /* Setup the entry add/mobify functions */
+ if (slapi_pblock_set (pb, SLAPI_PLUGIN_POST_ADD_FN, (void*)post_add) != 0 ||
+ slapi_pblock_set (pb, SLAPI_PLUGIN_POST_MODIFY_FN, (void*)post_modify) != 0 ||
+ slapi_pblock_set (pb, SLAPI_PLUGIN_POST_DELETE_FN, (void*)post_delete) != 0 ||
+ slapi_pblock_set (pb, SLAPI_PLUGIN_POST_MODRDN_FN, (void*)post_modrdn) != 0) {
+ log_plugin ("error registering plugin hooks");
+ return -1;
+ }
+
+ log_plugin ("%s initialized", PLUGIN_NAME);
+ return 0;
+}
+
diff --git a/plugin/plugin.h b/plugin/plugin.h
new file mode 100644
index 0000000..44080d0
--- /dev/null
+++ b/plugin/plugin.h
@@ -0,0 +1,72 @@
+#ifndef PLUGIN_H_
+#define PLUGIN_H_
+
+#include <lber.h>
+#include <ldap.h>
+
+#ifdef HAVE_SLAPI_PLUGIN_H
+#include <slapi-plugin.h>
+#else
+#include "include/slapi-plugin.h"
+#endif
+
+#define PLUGIN_NAME "slapi-dnsnotify"
+
+/* ---------------------------------------------------------------------
+ * LOGGING
+ */
+
+void log_msg (int level, const char* msg, ...);
+void log_plugin (const char* msg, ...);
+
+#define return_val_if_fail(expr, ret) \
+ do { if (expr) { } else { \
+ log_plugin ("*** %s *** check failed: '%s' at %s:%d", PLUGIN_NAME, #expr, __PRETTY_FUNCTION__, __LINE__); \
+ return (ret); \
+ } } while (0)
+#define return_if_fail(expr) \
+ do { if (expr) { } else { \
+ log_plugin ("*** %s *** check failed: '%s' at %s:%d", PLUGIN_NAME, #expr, __PRETTY_FUNCTION__, __LINE__); \
+ return; \
+ } } while (0)
+
+#if _DEBUG
+void log_trace (const char *where, int line, const char *msg);
+#define trace(x) log_trace (__PRETTY_FUNCTION__, __LINE__, (x))
+#else
+#define trace(x)
+#endif
+
+/* ----------------------------------------------------------------------
+ * HELPERS
+ */
+
+const char* ltrim (const char *data);
+void rtrim (char *data);
+char* trim (char *data);
+void lowercase (char *data);
+
+/* ----------------------------------------------------------------------
+ * AUTO SERIAL
+ */
+
+int autoserial_init (void);
+void autoserial_destroy (void);
+int autoserial_config (const char *name, const char *value);
+void autoserial_post_add (const char *dn);
+void autoserial_post_modify (const char *dn, LDAPMod **mods);
+void autoserial_post_modrdn (const char *odn, const char *ndn);
+void autoserial_post_delete (const char *dn);
+
+/* ----------------------------------------------------------------------
+ * DNS NOTIFY
+ */
+
+int dnsnotify_init (void);
+void dnsnotify_destroy (void);
+int dnsnotify_config (const char *name, const char *value);
+void dnsnotify_post_add (const char *dn);
+void dnsnotify_post_modify (const char *dn, LDAPMod **mods);
+
+
+#endif /*PLUGIN_H_*/