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

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