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

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