3a7434
From 3db35ad73ec57c8af499a0dcef96ffd4da914236 Mon Sep 17 00:00:00 2001
3a7434
From: Stef Walter <stefw@redhat.com>
3a7434
Date: Mon, 7 Sep 2015 13:49:10 +0200
3a7434
Subject: [PATCH 2/2] service: Fully qualify --computer-ou DN before passing to
3a7434
 adcli
3a7434
3a7434
This allows us to have a similar behavior for both the Samba and
3a7434
adcli membership software.
3a7434
---
3a7434
 service/Makefile.am          |   4 +-
3a7434
 service/realm-adcli-enroll.c |  11 +-
3a7434
 service/realm-dn-util.c      | 239 +++++++++++++++++++++++++++++++++++++++++++
3a7434
 service/realm-dn-util.h      |  32 ++++++
3a7434
 service/realm-samba-enroll.c |   4 +-
3a7434
 service/realm-samba-util.c   | 172 -------------------------------
3a7434
 service/realm-samba-util.h   |  29 ------
3a7434
 tests/Makefile.am            |  16 +--
3a7434
 tests/test-dn-util.c         | 129 +++++++++++++++++++++++
3a7434
 tests/test-samba-ou-format.c |  89 ----------------
3a7434
 11 files changed, 422 insertions(+), 305 deletions(-)
3a7434
 create mode 100644 service/realm-dn-util.c
3a7434
 create mode 100644 service/realm-dn-util.h
3a7434
 delete mode 100644 service/realm-samba-util.c
3a7434
 delete mode 100644 service/realm-samba-util.h
3a7434
 create mode 100644 tests/test-dn-util.c
3a7434
 delete mode 100644 tests/test-samba-ou-format.c
3a7434
3a7434
diff --git a/service/Makefile.am b/service/Makefile.am
3a7434
index 06a95ef..88ee780 100644
3a7434
--- a/service/Makefile.am
3a7434
+++ b/service/Makefile.am
3a7434
@@ -43,6 +43,8 @@ realmd_SOURCES = \
3a7434
 	service/realm-disco-mscldap.h \
3a7434
 	service/realm-disco-rootdse.c \
3a7434
 	service/realm-disco-rootdse.h \
3a7434
+	service/realm-dn-util.c \
3a7434
+	service/realm-dn-util.h \
3a7434
 	service/realm-errors.c \
3a7434
 	service/realm-errors.h \
3a7434
 	service/realm-example.c \
3a7434
@@ -79,8 +81,6 @@ realmd_SOURCES = \
3a7434
 	service/realm-samba-enroll.h \
3a7434
 	service/realm-samba-provider.c \
3a7434
 	service/realm-samba-provider.h \
3a7434
-	service/realm-samba-util.c \
3a7434
-	service/realm-samba-util.h \
3a7434
 	service/realm-samba-winbind.c \
3a7434
 	service/realm-samba-winbind.h \
3a7434
 	service/realm-service.c \
3a7434
diff --git a/service/realm-adcli-enroll.c b/service/realm-adcli-enroll.c
3a7434
index 7448647..ef1b563 100644
3a7434
--- a/service/realm-adcli-enroll.c
3a7434
+++ b/service/realm-adcli-enroll.c
3a7434
@@ -18,6 +18,7 @@
3a7434
 #include "realm-command.h"
3a7434
 #include "realm-daemon.h"
3a7434
 #include "realm-diagnostics.h"
3a7434
+#include "realm-dn-util.h"
3a7434
 #include "realm-errors.h"
3a7434
 #include "realm-ini-config.h"
3a7434
 #include "realm-options.h"
3a7434
@@ -82,6 +83,7 @@ realm_adcli_enroll_join_async (RealmDisco *disco,
3a7434
 	gchar *ccache_arg = NULL;
3a7434
 	gchar *upn_arg = NULL;
3a7434
 	gchar *server_arg = NULL;
3a7434
+	gchar *ou_arg = NULL;
3a7434
 
3a7434
 	g_return_if_fail (cred != NULL);
3a7434
 	g_return_if_fail (disco != NULL);
3a7434
@@ -120,9 +122,13 @@ realm_adcli_enroll_join_async (RealmDisco *disco,
3a7434
 	}
3a7434
 
3a7434
 	computer_ou = realm_options_computer_ou (options, disco->domain_name);
3a7434
-	if (computer_ou) {
3a7434
+	if (computer_ou != NULL) {
3a7434
+		ou_arg = realm_dn_util_build_qualified (computer_ou, disco->domain_name);
3a7434
 		g_ptr_array_add (args, "--computer-ou");
3a7434
-		g_ptr_array_add (args, (gpointer)computer_ou);
3a7434
+		if (ou_arg)
3a7434
+			g_ptr_array_add (args, ou_arg);
3a7434
+		else
3a7434
+			g_ptr_array_add (args, (gpointer)computer_ou);
3a7434
 	}
3a7434
 
3a7434
 	os = realm_settings_value ("active-directory", "os-name");
3a7434
@@ -190,6 +196,7 @@ realm_adcli_enroll_join_async (RealmDisco *disco,
3a7434
 	free (ccache_arg);
3a7434
 	free (upn_arg);
3a7434
 	free (server_arg);
3a7434
+	free (ou_arg);
3a7434
 }
3a7434
 
3a7434
 gboolean
3a7434
diff --git a/service/realm-dn-util.c b/service/realm-dn-util.c
3a7434
new file mode 100644
3a7434
index 0000000..85bcdb9
3a7434
--- /dev/null
3a7434
+++ b/service/realm-dn-util.c
3a7434
@@ -0,0 +1,239 @@
3a7434
+/* realmd -- Realm configuration service
3a7434
+ *
3a7434
+ * Copyright 2012 Red Hat Inc
3a7434
+ *
3a7434
+ * This program is free software: you can redistribute it and/or modify
3a7434
+ * it under the terms of the GNU Lesser General Public License as published
3a7434
+ * by the Free Software Foundation; either version 2 of the licence or (at
3a7434
+ * your option) any later version.
3a7434
+ *
3a7434
+ * See the included COPYING file for more information.
3a7434
+ *
3a7434
+ * Author: Stef Walter <stefw@gnome.org>
3a7434
+ */
3a7434
+
3a7434
+#include "config.h"
3a7434
+
3a7434
+#include "realm-dn-util.h"
3a7434
+
3a7434
+#include <glib.h>
3a7434
+
3a7434
+#include <ldap.h>
3a7434
+
3a7434
+static gboolean
3a7434
+berval_is_string (const struct berval *bv,
3a7434
+                  const gchar *string,
3a7434
+                  gsize length)
3a7434
+{
3a7434
+	return (bv->bv_len == length &&
3a7434
+	        g_ascii_strncasecmp (bv->bv_val, string, length) == 0);
3a7434
+
3a7434
+}
3a7434
+
3a7434
+static gboolean
3a7434
+berval_case_equals (const struct berval *v1,
3a7434
+                    const struct berval *v2)
3a7434
+{
3a7434
+	return (v1->bv_len == v2->bv_len &&
3a7434
+	        g_ascii_strncasecmp (v1->bv_val, v2->bv_val, v1->bv_len) == 0);
3a7434
+}
3a7434
+
3a7434
+static gboolean
3a7434
+dn_equals_domain (LDAPDN dn,
3a7434
+                  const gchar *domain_dn_str,
3a7434
+                  const gchar *domain)
3a7434
+{
3a7434
+	LDAPDN domain_dn;
3a7434
+	gboolean ret;
3a7434
+	int rc;
3a7434
+	gint i, j;
3a7434
+
3a7434
+	rc = ldap_str2dn (domain_dn_str, &domain_dn, LDAP_DN_FORMAT_LDAPV3);
3a7434
+	g_return_val_if_fail (rc == LDAP_SUCCESS, FALSE);
3a7434
+
3a7434
+	for (i = 0; dn[i] != NULL && domain_dn[i] != NULL; i++) {
3a7434
+		for (j = 0; dn[i][j] != NULL && domain_dn[i][j] != NULL; j++) {
3a7434
+			if (!berval_case_equals (&(dn[i][j]->la_attr), &(domain_dn[i][j]->la_attr)) ||
3a7434
+			    !berval_case_equals (&(dn[i][j]->la_value), &(domain_dn[i][j]->la_value)))
3a7434
+				break;
3a7434
+		}
3a7434
+
3a7434
+		if (dn[i][j] != NULL && domain_dn[i][j] != NULL)
3a7434
+			break;
3a7434
+	}
3a7434
+
3a7434
+	/* Did we reach end of both DNs? */
3a7434
+	ret = (dn[i] == NULL && domain_dn[i] == NULL);
3a7434
+
3a7434
+	ldap_dnfree (domain_dn);
3a7434
+
3a7434
+	return ret;
3a7434
+}
3a7434
+
3a7434
+gchar *
3a7434
+realm_dn_util_build_samba_ou (const gchar *ldap_dn,
3a7434
+                              const gchar *domain)
3a7434
+{
3a7434
+	gchar *domain_dn_str = NULL;
3a7434
+	GArray *parts;
3a7434
+	GString *part;
3a7434
+	gchar **strv;
3a7434
+	gchar *str;
3a7434
+	LDAPAVA* ava;
3a7434
+	gboolean ret;
3a7434
+	LDAPDN dn;
3a7434
+	int rc;
3a7434
+	gint i, j;
3a7434
+
3a7434
+	/*
3a7434
+	 * Here we convert a standard LDAP DN to the strange samba net format,
3a7434
+	 * as "documented" here:
3a7434
+	 *
3a7434
+	 * createcomputer=OU  Precreate the computer account in a specific OU.
3a7434
+	 *                    The OU string read from top to bottom without RDNs and delimited by a '/'.
3a7434
+	 *                    E.g. "createcomputer=Computers/Servers/Unix"
3a7434
+	 *                    NB: A backslash '\' is used as escape at multiple levels and may
3a7434
+	 *                        need to be doubled or even quadrupled.  It is not used as a separator.
3a7434
+	 */
3a7434
+
3a7434
+	/* ldap_str2dn doesn't like empty strings */
3a7434
+	while (g_ascii_isspace (ldap_dn[0]))
3a7434
+		ldap_dn++;
3a7434
+	if (g_str_equal (ldap_dn, ""))
3a7434
+		return NULL;
3a7434
+
3a7434
+	rc = ldap_str2dn (ldap_dn, &dn, LDAP_DN_FORMAT_LDAPV3);
3a7434
+	if (rc != LDAP_SUCCESS)
3a7434
+		return NULL;
3a7434
+
3a7434
+	ret = TRUE;
3a7434
+	parts = g_array_new (TRUE, TRUE, sizeof (gchar *));
3a7434
+
3a7434
+	for (i = 0; dn[i] != NULL; i++) {
3a7434
+		ava = dn[i][0];
3a7434
+
3a7434
+		/*
3a7434
+		 * Make sure this is a valid DN, we only support one value per
3a7434
+		 * RDN, string values, and must be an OU. DC values are allowed
3a7434
+		 * but only at the end of the DN.
3a7434
+		 */
3a7434
+
3a7434
+		if (ava == NULL || dn[i][1] != NULL || !(ava->la_flags & LDAP_AVA_STRING)) {
3a7434
+			ret = FALSE;
3a7434
+			break;
3a7434
+
3a7434
+		/* A DC, remainder must match the domain */
3a7434
+		} else if (berval_is_string (&ava->la_attr, "DC", 2)) {
3a7434
+			rc = ldap_domain2dn (domain, &domain_dn_str);
3a7434
+			if (rc != LDAP_SUCCESS)
3a7434
+				ret = FALSE;
3a7434
+			else
3a7434
+				ret = dn_equals_domain (dn + i, domain_dn_str, domain);
3a7434
+			break;
3a7434
+
3a7434
+		/* An OU, include */
3a7434
+		} else if (berval_is_string (&ava->la_attr, "OU", 2)) {
3a7434
+			part = g_string_sized_new (ava->la_value.bv_len);
3a7434
+			for (j = 0; j < ava->la_value.bv_len; j++) {
3a7434
+				switch (ava->la_value.bv_val[j]) {
3a7434
+				case '\\':
3a7434
+					g_string_append (part, "\\\\");
3a7434
+					break;
3a7434
+				case '/':
3a7434
+					g_string_append (part, "\\/");
3a7434
+					break;
3a7434
+				default:
3a7434
+					g_string_append_c (part, ava->la_value.bv_val[j]);
3a7434
+					break;
3a7434
+				}
3a7434
+			}
3a7434
+			str = g_string_free (part, FALSE);
3a7434
+			g_array_insert_val (parts, 0, str);
3a7434
+
3a7434
+		/* Invalid, stop */
3a7434
+		} else {
3a7434
+			ret = FALSE;
3a7434
+			break;
3a7434
+		}
3a7434
+	}
3a7434
+
3a7434
+	ldap_dnfree (dn);
3a7434
+	if (domain_dn_str)
3a7434
+		ldap_memfree (domain_dn_str);
3a7434
+
3a7434
+	strv = (gchar **)g_array_free (parts, FALSE);
3a7434
+	str = NULL;
3a7434
+
3a7434
+	/* Loop completed successfully */
3a7434
+	if (ret)
3a7434
+		str = g_strjoinv ("/", strv);
3a7434
+
3a7434
+	g_strfreev (strv);
3a7434
+
3a7434
+	return str;
3a7434
+}
3a7434
+
3a7434
+gchar *
3a7434
+realm_dn_util_build_qualified (const gchar *ldap_dn,
3a7434
+                               const gchar *domain)
3a7434
+{
3a7434
+	gchar *domain_dn_str = NULL;
3a7434
+	gboolean had_dc = FALSE;
3a7434
+	gchar *str;
3a7434
+	LDAPAVA* ava;
3a7434
+	gboolean ret;
3a7434
+	LDAPDN dn;
3a7434
+	int rc;
3a7434
+	gint i;
3a7434
+
3a7434
+	/* ldap_str2dn doesn't like empty strings */
3a7434
+	while (g_ascii_isspace (ldap_dn[0]))
3a7434
+		ldap_dn++;
3a7434
+	if (g_str_equal (ldap_dn, ""))
3a7434
+		return NULL;
3a7434
+
3a7434
+	rc = ldap_str2dn (ldap_dn, &dn, LDAP_DN_FORMAT_LDAPV3);
3a7434
+	if (rc != LDAP_SUCCESS)
3a7434
+		return NULL;
3a7434
+
3a7434
+	rc = ldap_domain2dn (domain, &domain_dn_str);
3a7434
+	if (rc != LDAP_SUCCESS) {
3a7434
+		ldap_dnfree (dn);
3a7434
+		return NULL;
3a7434
+	}
3a7434
+
3a7434
+	ret = TRUE;
3a7434
+
3a7434
+	for (i = 0; dn[i] != NULL; i++) {
3a7434
+		ava = dn[i][0];
3a7434
+
3a7434
+		/*
3a7434
+		 * Make sure this is a valid DN, we only support one value per
3a7434
+		 * RDN, string values. DC values are allowed but only at the end of the DN.
3a7434
+		 */
3a7434
+
3a7434
+		if (ava == NULL || dn[i][1] != NULL || !(ava->la_flags & LDAP_AVA_STRING)) {
3a7434
+			ret = FALSE;
3a7434
+			break;
3a7434
+
3a7434
+		/* A DC, remainder must match the domain */
3a7434
+		} else if (berval_is_string (&ava->la_attr, "DC", 2)) {
3a7434
+			had_dc = TRUE;
3a7434
+			ret = dn_equals_domain (dn + i, domain_dn_str, domain);
3a7434
+			break;
3a7434
+		}
3a7434
+	}
3a7434
+
3a7434
+	ldap_dnfree (dn);
3a7434
+
3a7434
+	if (!ret)
3a7434
+		return NULL;
3a7434
+
3a7434
+	if (had_dc)
3a7434
+		str = g_strdup (ldap_dn);
3a7434
+	else
3a7434
+		str = g_strdup_printf ("%s,%s", ldap_dn, domain_dn_str);
3a7434
+
3a7434
+	ldap_memfree (domain_dn_str);
3a7434
+	return str;
3a7434
+}
3a7434
diff --git a/service/realm-dn-util.h b/service/realm-dn-util.h
3a7434
new file mode 100644
3a7434
index 0000000..f5e5e69
3a7434
--- /dev/null
3a7434
+++ b/service/realm-dn-util.h
3a7434
@@ -0,0 +1,32 @@
3a7434
+/* realmd -- Realm configuration service
3a7434
+ *
3a7434
+ * Copyright 2012 Red Hat Inc
3a7434
+ *
3a7434
+ * This program is free software: you can redistribute it and/or modify
3a7434
+ * it under the terms of the GNU Lesser General Public License as published
3a7434
+ * by the Free Software Foundation; either version 2 of the licence or (at
3a7434
+ * your option) any later version.
3a7434
+ *
3a7434
+ * See the included COPYING file for more information.
3a7434
+ *
3a7434
+ * Author: Stef Walter <stefw@gnome.org>
3a7434
+ */
3a7434
+
3a7434
+#include "config.h"
3a7434
+
3a7434
+#ifndef __REALM_DN_UTIL_H__
3a7434
+#define __REALM_DN_UTIL_H__
3a7434
+
3a7434
+#include <gio/gio.h>
3a7434
+
3a7434
+G_BEGIN_DECLS
3a7434
+
3a7434
+gchar *           realm_dn_util_build_samba_ou     (const gchar *ldap_dn,
3a7434
+                                                    const gchar *domain);
3a7434
+
3a7434
+gchar *           realm_dn_util_build_qualified    (const gchar *ldap_dn,
3a7434
+                                                    const gchar *domain);
3a7434
+
3a7434
+G_END_DECLS
3a7434
+
3a7434
+#endif /* __REALM_DN_UTIL_H__ */
3a7434
diff --git a/service/realm-samba-enroll.c b/service/realm-samba-enroll.c
3a7434
index e8739d7..e749764 100644
3a7434
--- a/service/realm-samba-enroll.c
3a7434
+++ b/service/realm-samba-enroll.c
3a7434
@@ -18,12 +18,12 @@
3a7434
 #include "realm-daemon.h"
3a7434
 #include "realm-dbus-constants.h"
3a7434
 #include "realm-diagnostics.h"
3a7434
+#include "realm-dn-util.h"
3a7434
 #include "realm-errors.h"
3a7434
 #include "realm-options.h"
3a7434
 #include "realm-samba-config.h"
3a7434
 #include "realm-samba-enroll.h"
3a7434
 #include "realm-samba-provider.h"
3a7434
-#include "realm-samba-util.h"
3a7434
 #include "realm-settings.h"
3a7434
 
3a7434
 #include <glib/gstdio.h>
3a7434
@@ -314,7 +314,7 @@ begin_join (GTask *task,
3a7434
 
3a7434
 	computer_ou = realm_options_computer_ou (options, join->disco->domain_name);
3a7434
 	if (computer_ou != NULL) {
3a7434
-		strange_ou = realm_samba_util_build_strange_ou (computer_ou, join->disco->domain_name);
3a7434
+		strange_ou = realm_dn_util_build_samba_ou (computer_ou, join->disco->domain_name);
3a7434
 		if (strange_ou) {
3a7434
 			if (!g_str_equal (strange_ou, ""))
3a7434
 				join->join_args[at++] = g_strdup_printf ("createcomputer=%s", strange_ou);
3a7434
diff --git a/service/realm-samba-util.c b/service/realm-samba-util.c
3a7434
deleted file mode 100644
3a7434
index 3f6a53e..0000000
3a7434
--- a/service/realm-samba-util.c
3a7434
+++ /dev/null
3a7434
@@ -1,172 +0,0 @@
3a7434
-/* realmd -- Realm configuration service
3a7434
- *
3a7434
- * Copyright 2012 Red Hat Inc
3a7434
- *
3a7434
- * This program is free software: you can redistribute it and/or modify
3a7434
- * it under the terms of the GNU Lesser General Public License as published
3a7434
- * by the Free Software Foundation; either version 2 of the licence or (at
3a7434
- * your option) any later version.
3a7434
- *
3a7434
- * See the included COPYING file for more information.
3a7434
- *
3a7434
- * Author: Stef Walter <stefw@gnome.org>
3a7434
- */
3a7434
-
3a7434
-#include "config.h"
3a7434
-
3a7434
-#include "realm-samba-util.h"
3a7434
-
3a7434
-#include <glib.h>
3a7434
-
3a7434
-#include <ldap.h>
3a7434
-
3a7434
-static gboolean
3a7434
-berval_is_string (const struct berval *bv,
3a7434
-                  const gchar *string,
3a7434
-                  gsize length)
3a7434
-{
3a7434
-	return (bv->bv_len == length &&
3a7434
-	        g_ascii_strncasecmp (bv->bv_val, string, length) == 0);
3a7434
-
3a7434
-}
3a7434
-
3a7434
-static gboolean
3a7434
-berval_case_equals (const struct berval *v1,
3a7434
-                    const struct berval *v2)
3a7434
-{
3a7434
-	return (v1->bv_len == v2->bv_len &&
3a7434
-	        g_ascii_strncasecmp (v1->bv_val, v2->bv_val, v1->bv_len) == 0);
3a7434
-}
3a7434
-
3a7434
-static gboolean
3a7434
-dn_equals_domain (LDAPDN dn,
3a7434
-                  const gchar *domain)
3a7434
-{
3a7434
-	LDAPDN domain_dn;
3a7434
-	gchar *domain_dn_str;
3a7434
-	gboolean ret;
3a7434
-	int rc;
3a7434
-	gint i, j;
3a7434
-
3a7434
-	rc = ldap_domain2dn (domain, &domain_dn_str);
3a7434
-	g_return_val_if_fail (rc == LDAP_SUCCESS, FALSE);
3a7434
-
3a7434
-	rc = ldap_str2dn (domain_dn_str, &domain_dn, LDAP_DN_FORMAT_LDAPV3);
3a7434
-	g_return_val_if_fail (rc == LDAP_SUCCESS, FALSE);
3a7434
-
3a7434
-	ldap_memfree (domain_dn_str);
3a7434
-
3a7434
-	for (i = 0; dn[i] != NULL && domain_dn[i] != NULL; i++) {
3a7434
-		for (j = 0; dn[i][j] != NULL && domain_dn[i][j] != NULL; j++) {
3a7434
-			if (!berval_case_equals (&(dn[i][j]->la_attr), &(domain_dn[i][j]->la_attr)) ||
3a7434
-			    !berval_case_equals (&(dn[i][j]->la_value), &(domain_dn[i][j]->la_value)))
3a7434
-				break;
3a7434
-		}
3a7434
-
3a7434
-		if (dn[i][j] != NULL && domain_dn[i][j] != NULL)
3a7434
-			break;
3a7434
-	}
3a7434
-
3a7434
-	/* Did we reach end of both DNs? */
3a7434
-	ret = (dn[i] == NULL && domain_dn[i] == NULL);
3a7434
-
3a7434
-	ldap_dnfree (domain_dn);
3a7434
-
3a7434
-	return ret;
3a7434
-}
3a7434
-
3a7434
-gchar *
3a7434
-realm_samba_util_build_strange_ou (const gchar *ldap_dn,
3a7434
-                                   const gchar *domain)
3a7434
-{
3a7434
-	GArray *parts;
3a7434
-	GString *part;
3a7434
-	gchar **strv;
3a7434
-	gchar *str;
3a7434
-	LDAPAVA* ava;
3a7434
-	gboolean ret;
3a7434
-	LDAPDN dn;
3a7434
-	int rc;
3a7434
-	gint i, j;
3a7434
-
3a7434
-	/*
3a7434
-	 * Here we convert a standard LDAP DN to the strange samba net format,
3a7434
-	 * as "documented" here:
3a7434
-	 *
3a7434
-	 * createcomputer=OU  Precreate the computer account in a specific OU.
3a7434
-	 *                    The OU string read from top to bottom without RDNs and delimited by a '/'.
3a7434
-	 *                    E.g. "createcomputer=Computers/Servers/Unix"
3a7434
-	 *                    NB: A backslash '\' is used as escape at multiple levels and may
3a7434
-	 *                        need to be doubled or even quadrupled.  It is not used as a separator.
3a7434
-	 */
3a7434
-
3a7434
-	/* ldap_str2dn doesn't like empty strings */
3a7434
-	while (g_ascii_isspace (ldap_dn[0]))
3a7434
-		ldap_dn++;
3a7434
-	if (g_str_equal (ldap_dn, ""))
3a7434
-		return NULL;
3a7434
-
3a7434
-	rc = ldap_str2dn (ldap_dn, &dn, LDAP_DN_FORMAT_LDAPV3);
3a7434
-	if (rc != LDAP_SUCCESS)
3a7434
-		return NULL;
3a7434
-
3a7434
-	ret = TRUE;
3a7434
-	parts = g_array_new (TRUE, TRUE, sizeof (gchar *));
3a7434
-
3a7434
-	for (i = 0; dn[i] != NULL; i++) {
3a7434
-		ava = dn[i][0];
3a7434
-
3a7434
-		/*
3a7434
-		 * Make sure this is a valid DN, we only support one value per
3a7434
-		 * RDN, string values, and must be an OU. DC values are allowed
3a7434
-		 * but only at the end of the DN.
3a7434
-		 */
3a7434
-
3a7434
-		if (ava == NULL || dn[i][1] != NULL || !(ava->la_flags & LDAP_AVA_STRING)) {
3a7434
-			ret = FALSE;
3a7434
-			break;
3a7434
-
3a7434
-		/* A DC, remainder must match the domain */
3a7434
-		} else if (berval_is_string (&ava->la_attr, "DC", 2)) {
3a7434
-			ret = dn_equals_domain (dn + i, domain);
3a7434
-			break;
3a7434
-
3a7434
-		/* An OU, include */
3a7434
-		} else if (berval_is_string (&ava->la_attr, "OU", 2)) {
3a7434
-			part = g_string_sized_new (ava->la_value.bv_len);
3a7434
-			for (j = 0; j < ava->la_value.bv_len; j++) {
3a7434
-				switch (ava->la_value.bv_val[j]) {
3a7434
-				case '\\':
3a7434
-					g_string_append (part, "\\\\");
3a7434
-					break;
3a7434
-				case '/':
3a7434
-					g_string_append (part, "\\/");
3a7434
-					break;
3a7434
-				default:
3a7434
-					g_string_append_c (part, ava->la_value.bv_val[j]);
3a7434
-					break;
3a7434
-				}
3a7434
-			}
3a7434
-			str = g_string_free (part, FALSE);
3a7434
-			g_array_insert_val (parts, 0, str);
3a7434
-
3a7434
-		/* Invalid, stop */
3a7434
-		} else {
3a7434
-			ret = FALSE;
3a7434
-			break;
3a7434
-		}
3a7434
-	}
3a7434
-
3a7434
-	ldap_dnfree (dn);
3a7434
-
3a7434
-	strv = (gchar **)g_array_free (parts, FALSE);
3a7434
-	str = NULL;
3a7434
-
3a7434
-	/* Loop completed successfully */
3a7434
-	if (ret)
3a7434
-		str = g_strjoinv ("/", strv);
3a7434
-
3a7434
-	g_strfreev (strv);
3a7434
-
3a7434
-	return str;
3a7434
-}
3a7434
diff --git a/service/realm-samba-util.h b/service/realm-samba-util.h
3a7434
deleted file mode 100644
3a7434
index 2a680e7..0000000
3a7434
--- a/service/realm-samba-util.h
3a7434
+++ /dev/null
3a7434
@@ -1,29 +0,0 @@
3a7434
-/* realmd -- Realm configuration service
3a7434
- *
3a7434
- * Copyright 2012 Red Hat Inc
3a7434
- *
3a7434
- * This program is free software: you can redistribute it and/or modify
3a7434
- * it under the terms of the GNU Lesser General Public License as published
3a7434
- * by the Free Software Foundation; either version 2 of the licence or (at
3a7434
- * your option) any later version.
3a7434
- *
3a7434
- * See the included COPYING file for more information.
3a7434
- *
3a7434
- * Author: Stef Walter <stefw@gnome.org>
3a7434
- */
3a7434
-
3a7434
-#include "config.h"
3a7434
-
3a7434
-#ifndef __REALM_SAMBA_UTIL_H__
3a7434
-#define __REALM_SAMBA_UTIL_H__
3a7434
-
3a7434
-#include <gio/gio.h>
3a7434
-
3a7434
-G_BEGIN_DECLS
3a7434
-
3a7434
-gchar *           realm_samba_util_build_strange_ou   (const gchar *ldap_dn,
3a7434
-                                                       const gchar *suffix_dn);
3a7434
-
3a7434
-G_END_DECLS
3a7434
-
3a7434
-#endif /* __REALM_SAMBA_UTIL_H__ */
3a7434
diff --git a/tests/Makefile.am b/tests/Makefile.am
3a7434
index ddeba4d..3b05066 100644
3a7434
--- a/tests/Makefile.am
3a7434
+++ b/tests/Makefile.am
3a7434
@@ -12,11 +12,11 @@ TEST_LIBS = \
3a7434
 	$(GLIB_LIBS)
3a7434
 
3a7434
 TEST_PROGS = \
3a7434
+	test-dn-util \
3a7434
 	test-ini-config \
3a7434
 	test-sssd-config \
3a7434
 	test-safe-format \
3a7434
 	test-login-name \
3a7434
-	test-samba-ou-format \
3a7434
 	test-settings \
3a7434
 	$(NULL)
3a7434
 
3a7434
@@ -27,6 +27,13 @@ noinst_PROGRAMS +=  \
3a7434
 	frob-install-packages \
3a7434
 	$(NULL)
3a7434
 
3a7434
+test_dn_util_SOURCES = \
3a7434
+	tests/test-dn-util.c \
3a7434
+	service/realm-dn-util.c \
3a7434
+	$(NULL)
3a7434
+test_dn_util_LDADD = $(TEST_LIBS)
3a7434
+test_dn_util_CFLAGS = $(TEST_CFLAGS)
3a7434
+
3a7434
 test_ini_config_SOURCES = \
3a7434
 	tests/test-ini-config.c \
3a7434
 	service/realm-ini-config.c \
3a7434
@@ -59,13 +66,6 @@ test_login_name_SOURCES = \
3a7434
 test_login_name_LDADD = $(TEST_LIBS)
3a7434
 test_login_name_CFLAGS = $(TEST_CFLAGS)
3a7434
 
3a7434
-test_samba_ou_format_SOURCES = \
3a7434
-	tests/test-samba-ou-format.c \
3a7434
-	service/realm-samba-util.c \
3a7434
-	$(NULL)
3a7434
-test_samba_ou_format_LDADD = $(TEST_LIBS)
3a7434
-test_samba_ou_format_CFLAGS = $(TEST_CFLAGS)
3a7434
-
3a7434
 test_settings_SOURCES = \
3a7434
 	tests/test-settings.c \
3a7434
 	service/realm-settings.c \
3a7434
diff --git a/tests/test-dn-util.c b/tests/test-dn-util.c
3a7434
new file mode 100644
3a7434
index 0000000..c62a40f
3a7434
--- /dev/null
3a7434
+++ b/tests/test-dn-util.c
3a7434
@@ -0,0 +1,129 @@
3a7434
+/* realmd -- Realm configuration service
3a7434
+ *
3a7434
+ * Copyright 2012 Red Hat Inc
3a7434
+ *
3a7434
+ * This program is free software: you can redistribute it and/or modify
3a7434
+ * it under the terms of the GNU Lesser General Public License as published
3a7434
+ * by the Free Software Foundation; either version 2 of the licence or (at
3a7434
+ * your option) any later version.
3a7434
+ *
3a7434
+ * See the included COPYING file for more information.
3a7434
+ *
3a7434
+ * Author: Stef Walter <stefw@gnome.org>
3a7434
+ */
3a7434
+
3a7434
+#include "config.h"
3a7434
+
3a7434
+#include "service/realm-dn-util.h"
3a7434
+
3a7434
+#include <glib/gstdio.h>
3a7434
+
3a7434
+#include <string.h>
3a7434
+
3a7434
+typedef struct {
3a7434
+	const gchar *ldap_dn;
3a7434
+	const gchar *domain;
3a7434
+	const gchar *result;
3a7434
+} Fixture;
3a7434
+
3a7434
+static void
3a7434
+test_samba_ou_format (gconstpointer user_data)
3a7434
+{
3a7434
+	const Fixture *fixture = user_data;
3a7434
+	gchar *result;
3a7434
+
3a7434
+	result = realm_dn_util_build_samba_ou (fixture->ldap_dn, fixture->domain);
3a7434
+	g_assert_cmpstr (result, ==, fixture->result);
3a7434
+	g_free (result);
3a7434
+}
3a7434
+
3a7434
+static const Fixture samba_ou_fixtures[] = {
3a7434
+	{ "OU=One", "domain.example.com", "One" },
3a7434
+	{ "OU=One,ou=two", "domain.example.com", "two/One" },
3a7434
+	{ "Ou=One Long,OU=two", "domain.example.com", "two/One Long" },
3a7434
+	{ "Ou=One,OU=two, ou=Three", "domain.example.com", "Three/two/One" },
3a7434
+	{ "Ou=Test/Escape,Ou=Two", "domain.example.com", "Two/Test\\/Escape" },
3a7434
+	{ "Ou=Test\\\\Escape,Ou=Two", "domain.example.com", "Two/Test\\\\Escape" },
3a7434
+	{ "OU=One,DC=domain,dc=example,Dc=COM", "domain.example.com", "One" },
3a7434
+	{ "OU=One,OU=Two Here,DC=domain,dc=example,Dc=COM", "domain.example.com", "Two Here/One" },
3a7434
+	{ "OU=One,OU=Two Here,DC=invalid,Dc=COM", "domain.example.com", NULL },
3a7434
+	{ " ", "domain.example.com", NULL },
3a7434
+	{ "", "domain.example.com", NULL },
3a7434
+	{ "OU", "domain.example.com", NULL },
3a7434
+	{ "OU=One,", "domain.example.com", NULL },
3a7434
+	{ "CN=Unsupported", "domain.example.com", NULL },
3a7434
+	{ "OU=One+CN=Unsupported", "domain.example.com", NULL },
3a7434
+	{ "DC=radi07, DC=segad, DC=lab, DC=sjc, DC=redhat, DC=com", "radi08.segad.lab.sjc.redhat.com", NULL },
3a7434
+
3a7434
+};
3a7434
+
3a7434
+static void
3a7434
+test_qualify_dn (gconstpointer user_data)
3a7434
+{
3a7434
+	const Fixture *fixture = user_data;
3a7434
+	gchar *result;
3a7434
+
3a7434
+	result = realm_dn_util_build_qualified (fixture->ldap_dn, fixture->domain);
3a7434
+	g_assert_cmpstr (result, ==, fixture->result);
3a7434
+	g_free (result);
3a7434
+}
3a7434
+
3a7434
+static const Fixture qualify_fixtures[] = {
3a7434
+	{ "OU=One", "domain.example.com", "OU=One,dc=domain,dc=example,dc=com" },
3a7434
+	{ "OU=One,ou=two", "domain.example.com", "OU=One,ou=two,dc=domain,dc=example,dc=com" },
3a7434
+	{ "Ou=One Long,OU=two", "domain.example.com", "Ou=One Long,OU=two,dc=domain,dc=example,dc=com" },
3a7434
+	{ "OU=One,DC=domain,dc=example,Dc=COM", "domain.example.com", "OU=One,DC=domain,dc=example,Dc=COM" },
3a7434
+	{ "OU=One,OU=Two Here,DC=domain,dc=example,Dc=COM", "domain.example.com", "OU=One,OU=Two Here,DC=domain,dc=example,Dc=COM" },
3a7434
+	{ "OU=One,OU=Two Here,DC=invalid,Dc=COM", "domain.example.com", NULL },
3a7434
+	{ " ", "domain.example.com", NULL },
3a7434
+	{ "", "domain.example.com", NULL },
3a7434
+	{ "OU", "domain.example.com", NULL },
3a7434
+	{ "OU=One,", "domain.example.com", NULL },
3a7434
+	{ "CN=Test", "domain.example.com", "CN=Test,dc=domain,dc=example,dc=com" },
3a7434
+	{ "OU=One+CN=Unsupported", "domain.example.com", NULL },
3a7434
+	{ "DC=radi07, DC=segad, DC=lab, DC=sjc, DC=redhat, DC=com", "radi08.segad.lab.sjc.redhat.com", NULL },
3a7434
+};
3a7434
+
3a7434
+int
3a7434
+main (int argc,
3a7434
+      char **argv)
3a7434
+{
3a7434
+	gchar *escaped;
3a7434
+	gchar *name;
3a7434
+	gint i;
3a7434
+
3a7434
+#if !GLIB_CHECK_VERSION(2, 36, 0)
3a7434
+	g_type_init ();
3a7434
+#endif
3a7434
+
3a7434
+	g_test_init (&argc, &argv, NULL);
3a7434
+	g_set_prgname ("test-dn-util");
3a7434
+
3a7434
+	for (i = 0; i < G_N_ELEMENTS (samba_ou_fixtures); i++) {
3a7434
+		if (g_str_equal (samba_ou_fixtures[i].ldap_dn, ""))
3a7434
+			escaped = g_strdup ("_empty_");
3a7434
+		else
3a7434
+			escaped = g_strdup (samba_ou_fixtures[i].ldap_dn);
3a7434
+		g_strdelimit (escaped, ", =\\/", '_');
3a7434
+		name = g_strdup_printf ("/realmd/samba-ou-format/%s", escaped);
3a7434
+		g_free (escaped);
3a7434
+
3a7434
+		g_test_add_data_func (name, samba_ou_fixtures + i, test_samba_ou_format);
3a7434
+		g_free (name);
3a7434
+	}
3a7434
+
3a7434
+	for (i = 0; i < G_N_ELEMENTS (qualify_fixtures); i++) {
3a7434
+		if (g_str_equal (qualify_fixtures[i].ldap_dn, ""))
3a7434
+			escaped = g_strdup ("_empty_");
3a7434
+		else
3a7434
+			escaped = g_strdup (qualify_fixtures[i].ldap_dn);
3a7434
+		g_strdelimit (escaped, ", =\\/", '_');
3a7434
+		name = g_strdup_printf ("/realmd/qualify-dn/%s", escaped);
3a7434
+		g_free (escaped);
3a7434
+
3a7434
+		g_test_add_data_func (name, qualify_fixtures + i, test_qualify_dn);
3a7434
+		g_free (name);
3a7434
+	}
3a7434
+
3a7434
+	return g_test_run ();
3a7434
+}
3a7434
diff --git a/tests/test-samba-ou-format.c b/tests/test-samba-ou-format.c
3a7434
deleted file mode 100644
3a7434
index 0a482ee..0000000
3a7434
--- a/tests/test-samba-ou-format.c
3a7434
+++ /dev/null
3a7434
@@ -1,89 +0,0 @@
3a7434
-/* realmd -- Realm configuration service
3a7434
- *
3a7434
- * Copyright 2012 Red Hat Inc
3a7434
- *
3a7434
- * This program is free software: you can redistribute it and/or modify
3a7434
- * it under the terms of the GNU Lesser General Public License as published
3a7434
- * by the Free Software Foundation; either version 2 of the licence or (at
3a7434
- * your option) any later version.
3a7434
- *
3a7434
- * See the included COPYING file for more information.
3a7434
- *
3a7434
- * Author: Stef Walter <stefw@gnome.org>
3a7434
- */
3a7434
-
3a7434
-#include "config.h"
3a7434
-
3a7434
-#include "service/realm-samba-util.h"
3a7434
-
3a7434
-#include <glib/gstdio.h>
3a7434
-
3a7434
-#include <string.h>
3a7434
-
3a7434
-typedef struct {
3a7434
-	const gchar *ldap_dn;
3a7434
-	const gchar *domain;
3a7434
-	const gchar *ou_format;
3a7434
-} Fixture;
3a7434
-
3a7434
-static void
3a7434
-test_samba_ou_format (gconstpointer user_data)
3a7434
-{
3a7434
-	const Fixture *fixture = user_data;
3a7434
-	gchar *result;
3a7434
-
3a7434
-	result = realm_samba_util_build_strange_ou (fixture->ldap_dn, fixture->domain);
3a7434
-	g_assert_cmpstr (result, ==, fixture->ou_format);
3a7434
-	g_free (result);
3a7434
-}
3a7434
-
3a7434
-static const Fixture samba_ou_fixtures[] = {
3a7434
-	{ "OU=One", "domain.example.com", "One" },
3a7434
-	{ "OU=One,ou=two", "domain.example.com", "two/One" },
3a7434
-	{ "Ou=One Long,OU=two", "domain.example.com", "two/One Long" },
3a7434
-	{ "Ou=One,OU=two, ou=Three", "domain.example.com", "Three/two/One" },
3a7434
-	{ "Ou=Test/Escape,Ou=Two", "domain.example.com", "Two/Test\\/Escape" },
3a7434
-	{ "Ou=Test\\\\Escape,Ou=Two", "domain.example.com", "Two/Test\\\\Escape" },
3a7434
-	{ "OU=One,DC=domain,dc=example,Dc=COM", "domain.example.com", "One" },
3a7434
-	{ "OU=One,OU=Two Here,DC=domain,dc=example,Dc=COM", "domain.example.com", "Two Here/One" },
3a7434
-	{ "OU=One,OU=Two Here,DC=invalid,Dc=COM", "domain.example.com", NULL },
3a7434
-	{ " ", "domain.example.com", NULL },
3a7434
-	{ "", "domain.example.com", NULL },
3a7434
-	{ "OU", "domain.example.com", NULL },
3a7434
-	{ "OU=One,", "domain.example.com", NULL },
3a7434
-	{ "CN=Unsupported", "domain.example.com", NULL },
3a7434
-	{ "OU=One+CN=Unsupported", "domain.example.com", NULL },
3a7434
-	{ "DC=radi07, DC=segad, DC=lab, DC=sjc, DC=redhat, DC=com", "radi08.segad.lab.sjc.redhat.com", NULL },
3a7434
-
3a7434
-};
3a7434
-
3a7434
-int
3a7434
-main (int argc,
3a7434
-      char **argv)
3a7434
-{
3a7434
-	gchar *escaped;
3a7434
-	gchar *name;
3a7434
-	gint i;
3a7434
-
3a7434
-#if !GLIB_CHECK_VERSION(2, 36, 0)
3a7434
-	g_type_init ();
3a7434
-#endif
3a7434
-
3a7434
-	g_test_init (&argc, &argv, NULL);
3a7434
-	g_set_prgname ("test-samba-ou-format");
3a7434
-
3a7434
-	for (i = 0; i < G_N_ELEMENTS (samba_ou_fixtures); i++) {
3a7434
-		if (g_str_equal (samba_ou_fixtures[i].ldap_dn, ""))
3a7434
-			escaped = g_strdup ("_empty_");
3a7434
-		else
3a7434
-			escaped = g_strdup (samba_ou_fixtures[i].ldap_dn);
3a7434
-		g_strdelimit (escaped, ", =\\/", '_');
3a7434
-		name = g_strdup_printf ("/realmd/samba-ou-format/%s", escaped);
3a7434
-		g_free (escaped);
3a7434
-
3a7434
-		g_test_add_data_func (name, samba_ou_fixtures + i, test_samba_ou_format);
3a7434
-		g_free (name);
3a7434
-	}
3a7434
-
3a7434
-	return g_test_run ();
3a7434
-}
3a7434
-- 
3a7434
2.7.4
3a7434