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

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