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

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