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

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