Blame SOURCES/0002-create-user-try-to-find-NIS-domain-if-needed.patch

59dcbd
From 408880a11879b1a57a450e25c77ef2e310bdffd5 Mon Sep 17 00:00:00 2001
59dcbd
From: Sumit Bose <sbose@redhat.com>
59dcbd
Date: Mon, 18 Mar 2019 16:45:54 +0100
59dcbd
Subject: [PATCH 2/2] create-user: try to find NIS domain if needed
59dcbd
59dcbd
Related to https://gitlab.freedesktop.org/realmd/adcli/issues/2
59dcbd
---
59dcbd
 doc/adcli.xml     |  4 +++-
59dcbd
 library/adentry.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
59dcbd
 library/adentry.h |  2 ++
59dcbd
 tools/entry.c     | 16 ++++++++++++++++
59dcbd
 4 files changed, 65 insertions(+), 1 deletion(-)
59dcbd
59dcbd
diff --git a/doc/adcli.xml b/doc/adcli.xml
59dcbd
index 18620c0..af73433 100644
59dcbd
--- a/doc/adcli.xml
59dcbd
+++ b/doc/adcli.xml
59dcbd
@@ -537,7 +537,9 @@ $ adcli create-user Fry --domain=domain.example.com \
59dcbd
 			the new created user account, which should be the user's
59dcbd
 			NIS domain is the NIS/YP service of Active Directory's Services for Unix (SFU)
59dcbd
 			are used. This is needed to let the 'UNIX attributes' tab of older Active
59dcbd
-			Directoy versions show the set UNIX specific attributes.</para></listitem>
59dcbd
+			Directoy versions show the set UNIX specific attributes. If not specified
59dcbd
+			adcli will try to determine the NIS domain automatically if needed.
59dcbd
+			</para></listitem>
59dcbd
 		</varlistentry>
59dcbd
 	</variablelist>
59dcbd
 
59dcbd
diff --git a/library/adentry.c b/library/adentry.c
59dcbd
index 9b9e1c6..1cc0518 100644
59dcbd
--- a/library/adentry.c
59dcbd
+++ b/library/adentry.c
59dcbd
@@ -484,3 +484,47 @@ adcli_entry_new_group (adcli_conn *conn,
59dcbd
 	return_val_if_fail (sam_name != NULL, NULL);
59dcbd
 	return entry_new (conn, "group", group_entry_builder, sam_name);
59dcbd
 }
59dcbd
+
59dcbd
+adcli_result
59dcbd
+adcli_get_nis_domain (adcli_entry *entry,
59dcbd
+                      adcli_attrs *attrs)
59dcbd
+{
59dcbd
+	LDAP *ldap;
59dcbd
+	const char *ldap_attrs[] = { "cn", NULL };
59dcbd
+	LDAPMessage *results;
59dcbd
+	LDAPMessage *ldap_entry;
59dcbd
+	char *base;
59dcbd
+	const char *filter = "objectClass=msSFU30DomainInfo";
59dcbd
+	char *cn;
59dcbd
+	int ret;
59dcbd
+
59dcbd
+	ldap = adcli_conn_get_ldap_connection (entry->conn);
59dcbd
+	return_unexpected_if_fail (ldap != NULL);
59dcbd
+
59dcbd
+	if (asprintf (&base, "CN=ypservers,CN=ypServ30,CN=RpcServices,CN=System,%s",
59dcbd
+	              adcli_conn_get_default_naming_context (entry->conn)) < 0) {
59dcbd
+		return_unexpected_if_reached ();
59dcbd
+	}
59dcbd
+
59dcbd
+	ret = ldap_search_ext_s (ldap, base, LDAP_SCOPE_SUB, filter, (char **)ldap_attrs,
59dcbd
+	                         0, NULL, NULL, NULL, -1, &results);
59dcbd
+
59dcbd
+	free (base);
59dcbd
+
59dcbd
+	if (ret != LDAP_SUCCESS) {
59dcbd
+		/* No NIS domain available */
59dcbd
+		ldap_msgfree (results);
59dcbd
+		return ADCLI_SUCCESS;
59dcbd
+	}
59dcbd
+
59dcbd
+	ldap_entry = ldap_first_entry (ldap, results);
59dcbd
+	if (ldap_entry != NULL) {
59dcbd
+		cn = _adcli_ldap_parse_value (ldap, ldap_entry, "cn");
59dcbd
+		return_unexpected_if_fail (cn != NULL);
59dcbd
+
59dcbd
+		adcli_attrs_add (attrs, "msSFU30NisDomain", cn, NULL);
59dcbd
+	}
59dcbd
+	ldap_msgfree (results);
59dcbd
+
59dcbd
+	return ADCLI_SUCCESS;
59dcbd
+}
59dcbd
diff --git a/library/adentry.h b/library/adentry.h
59dcbd
index eb8bc00..ae90689 100644
59dcbd
--- a/library/adentry.h
59dcbd
+++ b/library/adentry.h
59dcbd
@@ -58,4 +58,6 @@ const char *       adcli_entry_get_sam_name             (adcli_entry *entry);
59dcbd
 
59dcbd
 const char *       adcli_entry_get_dn                   (adcli_entry *entry);
59dcbd
 
59dcbd
+adcli_result       adcli_get_nis_domain                 (adcli_entry *entry,
59dcbd
+                                                         adcli_attrs *attrs);
59dcbd
 #endif /* ADENTRY_H_ */
59dcbd
diff --git a/tools/entry.c b/tools/entry.c
59dcbd
index 69ce62c..de56586 100644
59dcbd
--- a/tools/entry.c
59dcbd
+++ b/tools/entry.c
59dcbd
@@ -153,6 +153,8 @@ adcli_tool_user_create (adcli_conn *conn,
59dcbd
 	adcli_attrs *attrs;
59dcbd
 	const char *ou = NULL;
59dcbd
 	int opt;
59dcbd
+	bool has_unix_attr = false;
59dcbd
+	bool has_nis_domain = false;
59dcbd
 
59dcbd
 	struct option options[] = {
59dcbd
 		{ "display-name", required_argument, NULL, opt_display_name },
59dcbd
@@ -193,18 +195,23 @@ adcli_tool_user_create (adcli_conn *conn,
59dcbd
 			break;
59dcbd
 		case opt_unix_home:
59dcbd
 			adcli_attrs_add (attrs, "unixHomeDirectory", optarg, NULL);
59dcbd
+			has_unix_attr = true;
59dcbd
 			break;
59dcbd
 		case opt_unix_uid:
59dcbd
 			adcli_attrs_add (attrs, "uidNumber", optarg, NULL);
59dcbd
+			has_unix_attr = true;
59dcbd
 			break;
59dcbd
 		case opt_unix_gid:
59dcbd
 			adcli_attrs_add (attrs, "gidNumber", optarg, NULL);
59dcbd
+			has_unix_attr = true;
59dcbd
 			break;
59dcbd
 		case opt_unix_shell:
59dcbd
 			adcli_attrs_add (attrs, "loginShell", optarg, NULL);
59dcbd
+			has_unix_attr = true;
59dcbd
 			break;
59dcbd
 		case opt_nis_domain:
59dcbd
 			adcli_attrs_add (attrs, "msSFU30NisDomain", optarg, NULL);
59dcbd
+			has_nis_domain = true;
59dcbd
 			break;
59dcbd
 		case opt_domain_ou:
59dcbd
 			ou = optarg;
59dcbd
@@ -242,6 +249,15 @@ adcli_tool_user_create (adcli_conn *conn,
59dcbd
 		      adcli_get_last_error ());
59dcbd
 	}
59dcbd
 
59dcbd
+	if (has_unix_attr && !has_nis_domain) {
59dcbd
+		res = adcli_get_nis_domain (entry, attrs);
59dcbd
+		if (res != ADCLI_SUCCESS) {
59dcbd
+			adcli_entry_unref (entry);
59dcbd
+			adcli_attrs_free (attrs);
59dcbd
+			errx (-res, "couldn't get NIS domain");
59dcbd
+		}
59dcbd
+	}
59dcbd
+
59dcbd
 	res = adcli_entry_create (entry, attrs);
59dcbd
 	if (res != ADCLI_SUCCESS) {
59dcbd
 		errx (-res, "creating user %s in domain %s failed: %s",
59dcbd
-- 
59dcbd
2.20.1
59dcbd